-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path8. String to Integer (atoi).py
49 lines (49 loc) · 1.3 KB
/
8. String to Integer (atoi).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
39
40
41
42
43
44
45
46
47
48
49
class Solution(object):
def myAtoi(self, str):
"""
:type str: str
:rtype: int
"""
# while len(str)>=1:
# if str[0] == ' ':
# str=str[1:]
# else:
# break
str = str.lstrip()
sign = 1
if len(str) == 0:
return 0
if str[0] == '-':
sign = -1
str = str[1:]
elif str[0] == '+':
sign = 1
str = str[1:]
i=0
while len(str)>=1:
if str[0] == '0':
str=str[1:]
else:
break
if len(str) == 0:
return 0
if not (str[0]>='0' and str[0]<='9'):
return 0
i=0
while i < 12 and i < len(str):
if not (str[i]>='0' and str[i]<='9'):
str = str[:i]
break
i=i+1
if (len(str)==10):
if (sign == -1 and str > '2147483648'):
return -2147483648
if (sign == 1 and str > '2147483647'):
return 2147483647
if (len(str)>10):
if (sign == -1):
return -2147483648
if (sign == 1):
return 2147483647
result = int(str)*sign
return result