-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathatoi.py
52 lines (45 loc) · 1.74 KB
/
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
44
45
46
47
48
49
50
51
52
class Solution:
def myAtoi(self, s: str) -> int:
sign = 1
result = 0
index = 0
n = len(s)
INT_MAX = pow(2, 31) - 1
INT_MIN = -pow(2, 31)
while index < n and s[index] == ' ':
index += 1
if index < n and s[index] == '+':
sign = 1
index += 1
elif index < n and s[index] == '-':
sign = -1
index += 1
while index < n and s[index].isdigit():
digit = int(s[index])
if ((result > INT_MAX // 10) or (result == INT_MAX // 10 and digit > INT_MAX % 10)):
return INT_MAX if sign == 1 else INT_MIN
result = 10 * result + digit
index += 1
return sign * result
# Tests:
if __name__ == '__main__':
Instant = Solution()
Solve = Instant.myAtoi("+1") # "42" -> 42 | "-42" -> -42 | "words with 971" -> 0 | " 564" -> 564 | "-91283472332" -> -2147483647 | "3.1423 -> 3 | '+1' -> 1
print(Solve)
# all_str = list(map(str, s.split()))
# # for i in range(len(all_str)):
# print(all_str)
# if len(all_str) > 0:
# if all_str[0].replace('.','').isdigit() or all_str[0].lstrip('-').replace('.','').isdigit() or all_str[0].lstrip('+').replace('.','').isdigit():
# number = all_str[0].lstrip()
# number = number if isinstance(number, int) else float(number)
# if -2147483647 < number < 2147483647 or -2147483647 < number < 2147483647:
# return int(number)
# elif int(all_str[0]) < -2147483647 or float(all_str[0]) < -2147483647:
# return -2147483648
# elif int(all_str[0]) > 2147483647 or float(all_str[0]) < -2147483647:
# return 2147483647
# else:
# return 0
# else:
# return 0