题目:
代码:
def myAtoi(self, s: str) -> int:
loca=0
if len(s)==0:
return 0
while loca<len(s) and s[loca]==' ':
loca=loca+1
if len(s)==loca:
return 0
if s[loca]!='+' and s[loca]!='-' and not s[loca].isdigit():
return 0
k=1
if s[loca]=='-':
k=-1
loca=loca+1
elif s[loca]=='+':
loca=loca+1
n=0
while loca<len(s) and s[loca].isdigit():
n=n*10+int(s[loca])
loca=loca+1
if k<0:
n=min(2147483648,n)
if k>0:
n=min(2147483647,n)
return n*k
教训使我成长:
1.再强调,python字符串单个字符是无法与int的+ -的。
2.int的乏味-2147483648---- 2147483647 因为零包含在+的里面,所以少一个!