-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatoi.py
43 lines (31 loc) · 874 Bytes
/
atoi.py
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
36
37
38
39
40
41
42
43
INT_MIN = -pow(2, 31)
INT_MAX = pow(2, 31) - 1
signs = {'-', '+'}
valid_symbols = {'1', '2', '3', '4', '5', '6', '7', '8', '9', '0'}
class Solution(object):
def myAtoi(self, some_str):
"""
:type str: str
:rtype: int
"""
pure_str = some_str.strip()
ll = len(pure_str)
if ll < 1:
return 0
negative = False
idx = 0
if pure_str[idx] in signs:
if pure_str[idx] == '-':
negative = True
idx += 1
my_int = 0
while idx < ll and pure_str[idx] in valid_symbols:
my_int = my_int * 10 + int(pure_str[idx])
idx += 1
if negative:
my_int = - my_int
if my_int < INT_MIN:
return INT_MIN
if my_int > INT_MAX:
return INT_MAX
return my_int