Python strange output error


  • 0
    R

    Could someone help to check the code? It always got a wrong answer for:
    Input:
    "-2147483649"
    Output:
    -2147483649
    Expected:
    -2147483648

    Thanks a lot

    class Solution:
    # @param {string} str
    # @return {integer}
    def myAtoi(self, str):
        str = str.strip()
        symSet = {'-', '+', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}
        if str == '':
            return 0
        if str[0] not in symSet:
            return 0
        i = 1
        while i < len(str):
            if (not str[i].isdigit()):
                break
            i += 1
        if str[0:i] == '-' or str[0:i] == '+':
            return 0
        if str[0] is '-':
            if int(str[1:i]) <= 2147483648:
                return -int(str[1:i])
            else:
                return -2147483648
        else:
            if int(str[0:i]) <= 2147483647:
                return int(str[0:i])
            else:
                return 2147483647

  • 0
    S

    it should be str[1:i+1]
    [1: i] is one digit less


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.