-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ8.py
More file actions
38 lines (31 loc) · 1002 Bytes
/
Q8.py
File metadata and controls
38 lines (31 loc) · 1002 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
35
36
37
38
class Solution(object):
def myAtoi(self, str):
"""
:type str: str
:rtype: int
"""
if len(str) == 0:
return 0
while not str[0].isnumeric():
if (str[0] == '+' or str[0] == '-') and len(str)>1 and str[1].isnumeric() or str[0].isnumeric():
break
elif not str[0] == ' ' and not str[0].isnumeric():
return 0
else:
if len(str) < 2:
return 0
str = str[1:]
s = str[0]
if len(str)>1:
for a in str[1:]:
if a.isnumeric():
s += a
else:
break
result = int(s)
if result>0:
return result if result<=2**31-1 else 2**31-1
else:
return result if result>=-2**31 else -2**31
if __name__ == '__main__':
print(Solution().myAtoi(" +42afd"))