如果python写 可以考虑列表等数据结构
正确率低主要是因为很多细节情况需要考虑
这一题的是实现过程自己做得太复杂了
class Solution:
def myAtoi(self, str: str) -> int:
if (str == ''):
return 0
INT_MAX = 2147483647
INT_MIN = -2147483648
i = 0
f = 0
while (str[i] == ' '):
i = i + 1
if i >= len(str):
return 0
if not(((str[i] >= '0') and (str[i] <= '9')) or (str[i] == '+') or (str[i] == '-')):
return 0
elif (str[i] == '-'):
f = 1
i = i + 1
elif (str[i] == '+'):
i = i + 1
s = ''
while (i < len(str) and str[i] >= '0' and str[i] <= '9'):
s = s + str[i]
i = i + 1
if (s == ''):
return 0
res = int(s)
if (res == 0):
return 0
elif (res > INT_MAX and f == 0):
return INT_MAX
elif (res > INT_MAX+1 and f == 1):
return INT_MIN
elif (f == 1):
return -res
else:
return res