Here clear Python code


  • 0
    S
    class Solution(object):
        def myAtoi(self, str):
            """
            :type str: str
            :rtype: int
            """
            str=str.encode('UTF-8')
            str=str.lstrip(' ')
            str=str.rstrip(' ')
            start=0
            p=''
            items=[]
            if str=='':
                return 0
            if str[0]=='+' or str[0]=='-':
                start=1
                if len(str)==1:
                    return 0
            elif str[0].isdigit()==False:
                return 0
            for i in range(start,len(str)):
                if str[i].isdigit()==True:
                    items.append(str[i])
                else:
                    break
            p=p.join(items)
            if p=='':
                p=0
            else:
                p=int(p)
            if str[0]=='-':
                p*=-1
            if p>2147483647:
                p=2147483647
            if p<-2147483648:
                p=-2147483648
            return p
    

Log in to reply
 

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