Skip to content

Instantly share code, notes, and snippets.

@DataSolveProblems
Created September 17, 2019 23:39
Show Gist options
  • Save DataSolveProblems/cb976cf78128d05a743633c02fc5d724 to your computer and use it in GitHub Desktop.
Save DataSolveProblems/cb976cf78128d05a743633c02fc5d724 to your computer and use it in GitHub Desktop.
class Solution:
def myAtoi(self, str: str) -> int:
str = str.strip()
isNegative = False
if str and str[0] == '-':
isNegative = True
if str and (str[0] == '+' or str[0] == '-'):
str = str[1:]
if not str:
return 0
digits = {i for i in '0123456789'}
result = 0
for c in str:
if c not in digits:
break
result = result * 10 + int(c)
if isNegative:
result = -result
result = max(min(result, 2**31 - 1), -2**31)
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment