String to Integer (atoi)
class Solution:
maxNum = pow(2, 31)
def myAtoi(self, str):
"""
:type str: str
:rtype: int
"""
isStart = False
isF = False
result = 0
for char in str:
if not isStart:
if char == ' ':
continue
elif char not in "0123456789":
if isF:
result = -result
print(result)
return result
if char in "+-0123456789":
isStart = True
if char == '-':
isF = True
continue
elif char == '+':
continue
else:
result = result * 10 + int(char)
if isF:
if -result <= -self.maxNum:
result = -self.maxNum
print(result)
return result
else:
if result >= self.maxNum - 1:
result = self.maxNum - 1
print(result)
return result
else:
if isF:
result = -result
print(result)
return result
if isF:
result = -result
print(result)
return result
网友评论