-
Notifications
You must be signed in to change notification settings - Fork 1
/
string_to_integer.py
59 lines (49 loc) · 1.78 KB
/
string_to_integer.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
class Solution:
def myAtoi(self, str: str) -> int:
# The heart of this problem is dealing with 'overflow'
# define int_max, int_min, and the sign (i.e., 1 and -1)
int_max = 2147483648 - 1
int_min = -2147483648
sign = 1
# edge case: empty string
if str == '':
return 0
# skip whitespace(s)
i = 0
# while (str[i]==' ') and (i<len(str)):
while (i<len(str)) and (str[i]==' '):
i += 1
# be careful: must check if 'i < len(str)'
if i<len(str):
if (str[i]=='-'):
sign = -1
i += 1
elif (str[i]=='+'):
sign = 1
i += 1
# check if 'str[i].isdigit()'
# then, check if 'my_digit * 10 will overflow'
# my_digit *= 10
# my_digit += current_digit
# fianlly, return 'sign * my_digit'
my_digit = 0
# while (str[i].isdigit()) and (i<len(str)):
while (i<len(str)) and (str[i].isdigit()):
if my_digit * 10 <= int_max:
my_digit = my_digit * 10
else:
if sign == 1:
return int_max
elif sign == -1:
return int_min
current_digit = int(str[i])
if my_digit + current_digit <= int_max:
my_digit = my_digit + current_digit
else:
if sign == 1:
return int_max
elif sign == -1:
return int_min
# remember to plus one
i += 1
return sign * my_digit