Skip to content

Commit b43d4f2

Browse files
authored
Merge pull request #112 from ashwek/AtoI
0008 String to Integer(atoi) - Python
2 parents 22bd7ae + 748cb24 commit b43d4f2

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution:
2+
def myAtoi(self, s):
3+
"""
4+
:type s: str
5+
:rtype: int
6+
"""
7+
8+
try:
9+
s = list(s.split()[0])
10+
Sign = {'+':1, '-':-1}[s[0]]
11+
s.pop(0)
12+
except IndexError : return 0
13+
except KeyError: Sign = 1
14+
15+
Num = 0
16+
for i in s:
17+
try: Num = int(i) + (Num*10)
18+
except ValueError: break
19+
20+
Num = Sign*Num
21+
if Num > 2147483647:
22+
return 2147483647
23+
elif Num < -2147483648:
24+
return -2147483648
25+
return Num

0 commit comments

Comments
 (0)