forked from Garvit244/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path8.py
38 lines (36 loc) · 1.13 KB
/
8.py
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:
def myAtoi(self, str):
"""
:type str: str
:rtype: int
"""
str = str.strip()
number = ""
for x in str:
if x.isalpha() and number == "":
return 0
elif x.isalpha():
break
elif x == ".":
break
elif x == " ":
break
elif (x == "+" or x == "-") and number == "":
number = number + x
elif (x == "+" or x == "-") and number != "":
break
elif (x == "+" or x == "-") and (number[-1] == "+" or number[-1] == "-"):
return 0
elif (x == "+" or x == "-") and ("+" in number or "-" in number):
break
elif x.isdigit():
number = number + x
if number == "" or number == "+" or number == "-":
return 0
else:
if int(number) > ((2**31)-1):
return (2**31)-1
elif int(number) < -(2**31):
return -(2**31)
else:
return int(number)