It is a terrible question, submit again and again to test correct answer. Share my Python


  • 3
    W
    class Solution(object):
        def myAtoi(self, str):
            """
            :type str: str
            :rtype: int
            """
            s,sign=list(str),1
            while len(s)>0 and s[0]==" ":
                s.pop(0)
            if len(s)==0:
                return 0
            if s[0]=='+':
                s.pop(0)
            elif s[0]=='-':
                sign=-1
                s.pop(0)
            a,i=0,0
            while i<len(s):
                if '0'<=s[i]<='9':
                    a=10*a+ord(s[i])-ord('0')
                    i+=1
                else:
                    break
            if sign*a>2**31-1:
                return 2**31-1 
            elif sign*a<-(2**31):
                return -2**31
            else:
                return sign*a

Log in to reply
 

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