-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy path0008-string-to-integer-atoi.py
More file actions
36 lines (32 loc) · 821 Bytes
/
0008-string-to-integer-atoi.py
File metadata and controls
36 lines (32 loc) · 821 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
class Solution:
def myAtoi(self, s: str) -> int:
x = ""
y = ""
start = 0
nums = "0123456789"
for i in range(len(s)):
if s[i] != " ":
start = i
break
for i in range(start, len(s)):
if s[i] in "-+":
if x == "" and y == "":
x = s[i]
else:
break
elif s[i] in nums:
y += s[i]
else:
break
if y == "":
return 0
if x == "-":
if int(y) > 2**31:
return -2**31
else:
return -int(y)
else:
if int(y) > 2**31-1:
return 2**31-1
else:
return int(y)