Skip to content

Create 008_String_to_Integer_(atoi).py #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 7, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions 008_String_to_Integer_(atoi).py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class Solution:
def myAtoi(self, s: str) -> int:
strs=s.lstrip(' _')
result = []
minus=False
ch=True
if len(strs)>=2:
if (strs[0]=='-' or strs[0]=='+') and (strs[1]=='-' or strs[1]=='+'):
return 0
if len(strs)==0:
return 0
if strs[0]=='+':
strs=strs[1:]
pass
elif strs[0]=='-':
minus =True
strs=strs[1:]
for i in range(len(strs)):
if ord(strs[i]) in range(48,58):
result.append(strs[i])
ch=False
else: break
if ch:
return 0

r=''.join(result)
sum= int(r) if not minus else -int(r)
if sum > 2**31-1 or sum < -2**31:
return 2**31-1 if not minus else -2**31
else: return sum