Skip to content

Commit ff4fc2b

Browse files
Merge pull request #19 from Pal-Sandeep/Pal-Sandeep-patch-1
Create 008_String_to_Integer_(atoi).py
2 parents 5034b9d + 307be30 commit ff4fc2b

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

008_String_to_Integer_(atoi).py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution:
2+
def myAtoi(self, s: str) -> int:
3+
strs=s.lstrip(' _')
4+
result = []
5+
minus=False
6+
ch=True
7+
if len(strs)>=2:
8+
if (strs[0]=='-' or strs[0]=='+') and (strs[1]=='-' or strs[1]=='+'):
9+
return 0
10+
if len(strs)==0:
11+
return 0
12+
if strs[0]=='+':
13+
strs=strs[1:]
14+
pass
15+
elif strs[0]=='-':
16+
minus =True
17+
strs=strs[1:]
18+
for i in range(len(strs)):
19+
if ord(strs[i]) in range(48,58):
20+
result.append(strs[i])
21+
ch=False
22+
else: break
23+
if ch:
24+
return 0
25+
26+
r=''.join(result)
27+
sum= int(r) if not minus else -int(r)
28+
if sum > 2**31-1 or sum < -2**31:
29+
return 2**31-1 if not minus else -2**31
30+
else: return sum

0 commit comments

Comments
 (0)