-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.py
More file actions
29 lines (25 loc) · 776 Bytes
/
Solution.py
File metadata and controls
29 lines (25 loc) · 776 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
class Solution:
def myAtoi(self, str: str) -> int:
MAX_NUM = pow(2, 31) - 1
# discard whitespace
str = str.strip()
# check if can convert to int
if not str or (not str[0].isnumeric() and str[0] not in ['-', '+']):
return 0
ret = ''
index = 0
signal = 1
# set sigm
if str[0] == '-':
index += 1
signal = -1
elif str[0] == '+':
index += 1
while index < len(str):
if not str[index].isnumeric():
break
ret += str[index]
index += 1
if int(ret) > MAX_NUM:
return MAX_NUM if signal == 1 else -MAX_NUM - 1
return int(ret) * signal if ret else 0