Skip to content

Commit 5bc741f

Browse files
committed
committed from zkp
1 parent e8b9f5c commit 5bc741f

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

LeetCode/myAtoi.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution:
2+
def myAtoi(self, str):
3+
num = ["{}".format(i) for i in range(10)]
4+
nums = num + ['-', '+']
5+
l = len(str)
6+
for i in range(len(str)):
7+
if str[i] == ' ':
8+
continue
9+
elif str[i] in nums:
10+
l = i
11+
break
12+
else:
13+
return 0
14+
r = len(str)
15+
for i in range(l + 1, len(str)):
16+
17+
if str[i] in num:
18+
# print(str[i])
19+
continue
20+
else:
21+
# print(i)
22+
r = i
23+
break
24+
# print(str[l:r])
25+
if str[l:r] in ['', '-', '+']:
26+
return 0
27+
n = int(str[l:r])
28+
L = pow(-2, 31)
29+
R = pow(2, 31) - 1
30+
if n < L:
31+
return L
32+
if n > R:
33+
return R
34+
return n

0 commit comments

Comments
 (0)