# Python simple solution beates 90+% with tips

• ``````class Solution(object):
def myAtoi(self, str):
"""
:type str: str
:rtype: int
"""

# 1.continuous non-numeric symbols ahead of number：
# '+-2' expects 0

# 2.discards the rest behind the first number
# '1+111' expects 1

# 3.overflow:
# '2147483648' expects 2147483647
# '-2147483648' expects 2147483648

# 4.sign of the number

ret = 0
sign = 1 # store positive or negative
count = 0 # store the number of non-numeric symbols,if non-numeric symbols appear after number,break
for c in str:
count += 1
if '0'<= c <= '9':
ret *= 10
ret += int(c)
# if there is sign, sign must appear first
elif c == '+' and count == 1:
sign = 1
elif c == '-' and count == 1:
sign = -1
elif c == ' ' and count == 1:
count -=1
else:
break
if ret > 2147483647 and sign == 1:
return 2147483647
elif ret > 2147483648 and sign == -1:
return -2147483648
else:
return int(sign*ret)
``````

• Is there any other simpler way?

• I think this algorithm's complexity is O(n),it‘s simple in it's logic actually.But I don't know other simpler ways.If you know,please let me know,3Q~

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.