What's wrong with my code,why test case is wrong?


  • 0
    class Solution(object):
    def myAtoi(self, str):
        """
        :type str: str
        :rtype: int
        """
        if not str:return 0
        sign,res,i=1,0,0
        dict={'0':0,'1':1,'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9}
        str=str.split()[0]
        if i<len(str) and str[i]=='-':
            sign=-1
            i+=1
        elif i<len(str)and str[i]=='+':
            i+=1
        while i<len(str) and str[i] in dict:
            res=res*10+dict[str[i]]
            i+=1
        return sign*res
    

    but one test case is :input:'2147483648' output:2147483648,however excepted output is:2147483647.why,why,why?


  • 1
    L

    Because '2147483648' is bigger than MAX_INT which is 2147483647.Then it returns MAX_INT,not 2147483648.Forgive my poor English.


  • 0

    yeah,thanks for your answer,I didn't notice that!


Log in to reply
 

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