forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathString to Integer (atoi).py
55 lines (50 loc) · 1.72 KB
/
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
50
51
52
53
54
55
# Runtime: 69 ms (Top 20.00%) | Memory: 13.9 MB (Top 79.88%)
class Solution:
def assign_sign(self, sign):
# verify that we haven't already got a sign
# "+42-" -> we don't want to return -42; hence check
if not self.is_neg and not self.is_pos:
# no sign has been set yet
if sign=="+":
self.is_pos = True
elif sign=="-":
self.is_neg = True
return
def add_to_int(self, num):
if not self.num:
self.num = num
else:
self.num = (self.num*10) + num
def myAtoi(self, s: str) -> int:
# remove the leading and trailing spaces
self.is_neg = False
self.is_pos = False
self.num = None
s=s.strip()
for i in s:
# ignore the rest of the string if a non digit character is read
if i in ("+","-"):
# only read the first symbol; break if second symbol is read
if self.is_pos or self.is_neg or isinstance(self.num, int):
# one of the two symbols is read or a number is read
break
self.assign_sign(i)
continue
try:
i = int(i)
self.add_to_int(i)
except ValueError:
# it's neither a sign, nor a number; terminate
break
# outside the loop; compile the result
if not self.num:
return 0
upper_limit = 2**31 - 1
if self.is_pos or (not self.is_pos and not self.is_neg):
if self.num > upper_limit:
self.num = upper_limit
elif self.is_neg:
if self.num > upper_limit+1:
self.num = upper_limit+1
self.num = -1 * self.num
return self.num