Some failed test cases


  • 0
    L

    Input: "+1"
    Expected: 1

    Input: " -0012a42"
    Expected: -12

    Input: " +0 123"
    Expected: 0

    Input: "2147483648"
    Expected: 2147483647

    Input: "2147483648"
    Expected: 2147483647

    Input: " - 321"
    Expected: 0

    My python solutino for your reference:

    class Solution:
    # @return an integer
    def atoi(self, str):
        res=0;
        minus=None;
        hasAddValue=False;
        for s in str:
            if(s==' '):
                if(hasAddValue==True):
                    break;
                else:
                    continue;
    
            if(s=='+' or s=='-'):
                if(minus==None):
                    minus= (s=='-');
                    hasAddValue=True;
                    continue;
                else:
                    return 0;
    
            if('0'<=s<='9'):
                res=res*10+ int(s);
                hasAddValue=True;
            else:
                break;
        if(minus==True):
            if(-res <-2147483648):
                return -2147483648;
            return -res;
        else:
            if(res>2147483647):
                return 2147483647;
            return res;

Log in to reply
 

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