如果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

博客提到用Python实现题目时可考虑列表等数据结构,在实现过程中存在正确率低的问题,主要原因是有很多细节情况需要考虑,且博主表示自己的实现过程过于复杂。
1346

被折叠的 条评论
为什么被折叠?



