Title Description
Converting a string to an integer, the request can not use the library function converts the integer string. Value of 0 or a character string is not a valid return value 0
Enter a description:
Enter a string including alphanumeric symbols, can be null
Output Description:
If it is a legitimate expression of the digital value is returned, otherwise 0
1 # -*- coding:utf-8 -*- 2 class Solution: 3 def StrToInt(self, s): 4 # write code here 5 dic={"0":0,"1":1,"2":2,"3":3,"4":4,"5":5,"6":6,"7":7,"8":8,"9":9} 6 flag="+" 7 if len(s)>0 and (s[0]=="+" or s[0]=="-"): 8 flag=s[0] 9 s=s[1:] 10 if s == "" or s.isdigit()==False: 11 return 0 12 count=len(s) 13 res=0 14 for e in s: 15 res+=pow(10,count-1)*dic[e] 16 count-=1 17 if flag=="-": 18 res=-1*res 19 if res<=-2147483649 or res >=2147483648: 20 res=0 21 return res
2019-12-27 19:37:16