-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom-atoi.py
43 lines (40 loc) · 1.19 KB
/
custom-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
class Solution(object):
def myAtoi(self, s):
"""
:type s: str
:rtype: int
"""
start = 99999
flipper = 1
temp = ""
final = 0
for i,char in enumerate(s):
if i < start and char == "-":
flipper = -1
start = i
elif i < start and char == "+":
flipper = 1
start = i
elif i < start and char in "1234567890":
temp += char
start = i
elif i < start and char != " ":
return 0
elif i > start and char not in "1234567890":
break
else:
temp += char
length = len(temp)
#print(temp)
for j in range(length):
final += 10**(length-j-1) * self.chtoi(temp[j])
#print(self.chtoi(temp[j]))
if flipper == 1:
final = min(final, 2147483647)
else:
final = min(final, 2147483648)
return final * flipper
def chtoi(self, ch):
numbers = "9876543210"
realnums = [9,8,7,6,5,4,3,2,1,0]
return realnums[numbers.find(ch)]