Created
September 17, 2019 23:39
-
-
Save DataSolveProblems/cb976cf78128d05a743633c02fc5d724 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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