-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathatoi.py
More file actions
35 lines (31 loc) · 833 Bytes
/
atoi.py
File metadata and controls
35 lines (31 loc) · 833 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class Solution(object):
def myAtoi(self, str):
"""
:type str: str
:rtype: int
"""
str = str.strip()
if len(str) == 0:
return 0
is_neg = False
index = 0
if str[index] == '-':
is_neg = True
index += 1
elif str[index] == '+':
index += 1
ret_val = 0
for digit in xrange(index, len(str)):
if not str[digit].isdigit():
break
else:
ret_val *= 10
ret_val += int(str[digit])
if not is_neg and ret_val > 2147483647:
return 2147483647
elif is_neg and ret_val > 2147483648:
return -2147483648
if is_neg:
return -ret_val
else:
return ret_val