Skip to content

Instantly share code, notes, and snippets.

@blitzblade
Created October 30, 2022 15:10
Show Gist options
  • Save blitzblade/bdeb16789cc86ba6d9216ba9d1c17473 to your computer and use it in GitHub Desktop.
Save blitzblade/bdeb16789cc86ba6d9216ba9d1c17473 to your computer and use it in GitHub Desktop.
#leetcode
class Solution:
def myAtoi(self, s: str) -> int:
s = s.strip()
if not s:
return 0
sign = 1
if s[0] == "-" or s[0] == "+":
if s[0] == "-":
sign = -1
s = s[1:]
new_s = ""
for c in s:
if c.isdigit():
new_s += c
else:
break
digit = 0
try:
digit += sign * int(new_s)
except:
print(f"'{new_s}' not a number")
if digit <= -2147483648:
return -2147483648
if digit >= 2147483647:
return 2147483647
return digit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment