-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyAtoi.py
More file actions
34 lines (33 loc) · 891 Bytes
/
myAtoi.py
File metadata and controls
34 lines (33 loc) · 891 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
class Solution:
def myAtoi(self, str):
num = ["{}".format(i) for i in range(10)]
nums = num + ['-', '+']
l = len(str)
for i in range(len(str)):
if str[i] == ' ':
continue
elif str[i] in nums:
l = i
break
else:
return 0
r = len(str)
for i in range(l + 1, len(str)):
if str[i] in num:
# print(str[i])
continue
else:
# print(i)
r = i
break
# print(str[l:r])
if str[l:r] in ['', '-', '+']:
return 0
n = int(str[l:r])
L = pow(-2, 31)
R = pow(2, 31) - 1
if n < L:
return L
if n > R:
return R
return n