File tree 1 file changed +6
-5
lines changed
scripts/algorithms/S/String to Integer (atoi)
1 file changed +6
-5
lines changed Original file line number Diff line number Diff line change
1
+ # Runtime: 69 ms (Top 20.00%) | Memory: 13.9 MB (Top 79.88%)
2
+
1
3
class Solution :
2
-
4
+
3
5
def assign_sign (self , sign ):
4
6
# verify that we haven't already got a sign
5
7
# "+42-" -> we don't want to return -42; hence check
@@ -10,13 +12,13 @@ def assign_sign(self, sign):
10
12
elif sign == "-" :
11
13
self .is_neg = True
12
14
return
13
-
15
+
14
16
def add_to_int (self , num ):
15
17
if not self .num :
16
18
self .num = num
17
19
else :
18
20
self .num = (self .num * 10 ) + num
19
-
21
+
20
22
def myAtoi (self , s : str ) -> int :
21
23
# remove the leading and trailing spaces
22
24
self .is_neg = False
@@ -38,7 +40,7 @@ def myAtoi(self, s: str) -> int:
38
40
except ValueError :
39
41
# it's neither a sign, nor a number; terminate
40
42
break
41
-
43
+
42
44
# outside the loop; compile the result
43
45
if not self .num :
44
46
return 0
@@ -51,4 +53,3 @@ def myAtoi(self, s: str) -> int:
51
53
self .num = upper_limit + 1
52
54
self .num = - 1 * self .num
53
55
return self .num
54
-
You can’t perform that action at this time.
0 commit comments