classSolution:defmyAtoi(self,str:str)->int:
strs =str.strip().split()iflen(strs)==0:return0
str0 = strs[0]
index =0if str0[0]=="-":
index +=1while index <len(str0)and(ord(str0[index])>=48andord(str0[index])<=57):
index +=1if index ==1:return0
res =int(str0[1:index])if res >2147483648:return-2147483648else:return-res
elif str0[0]=="+":
index +=1while index <len(str0)and(ord(str0[index])>=48andord(str0[index])<=57):
index +=1if index ==1:return0
res =int(str0[1:index])if res >2147483647:return2147483647else:return res
elif(ord(str0[index])>=48andord(str0[index])<=57):while index <len(str0)and(ord(str0[index])>=48andord(str0[index])<=57):
index +=1
res =int(str0[0:index])if res >2147483647:return2147483647else:return res
else:return0
Python3 solution2:
classSolution:defmyAtoi(self,str:str)->int:
index =0
sign =1
result =0
INT_MAX =2147483647iflen(str)==0:return0while index <len(str)andstr[index]==" ":
index +=1if index <len(str)and(str[index]=="-"orstr[index]=="+"):ifstr[index]=="-":
sign =-1
index +=1while index <len(str)andord(str[index])>=48andord(str[index])<=57:if((result >(INT_MAX //10))or((result ==(INT_MAX //10))and((ord(str[index])-48)>(INT_MAX %10)))):return2147483647if sign ==1else-2147483648
result = result *10+(ord(str[index])-48)
index +=1return result * sign