My stupid python AC code


  • 0
    B
    def atoi(self, str):
        if not str:
            return 0
            
        length=len(str)
        
        result=0
        
        map={'1':1,'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9,'0':0}
        
        sign=1
        
        signflag=0
        numberflag=0
        
        for i in range(length):
            if str[i]==' ':
                if numberflag:
                    break
                if signflag:
                    break
                continue
            if str[i]=='-':
                sign=-1
                if signflag:
                    return 0
                signflag=1
                continue
            if str[i]=='+':
                if signflag:
                    return 0
                signflag=1
                continue
            if str[i] in map:
                if numberflag==0:
                    numberflag=1
                current=map[str[i]]
                result=result*10+current
            else:
                break
        if sign*result>=2147483648:
            return 2147483647
        elif sign*result<-2147483648:
            return -2147483648
        return sign*result
    

    It passed AC. i have to admit. It is stupid.

    how can one expect to use ATOI given today's technology. The request to reproduce an obsolete method is simply insane.


Log in to reply
 

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