File tree Expand file tree Collapse file tree 1 file changed +9
-11
lines changed Expand file tree Collapse file tree 1 file changed +9
-11
lines changed Original file line number Diff line number Diff line change 1
- # Approach 1 - Ordinals + Elementary Math
1
+ # Approach 1: Elementary Math
2
2
3
- # Time: O(max(N1, N2 ))
4
- # Space: O(max(N1, N2 ))
3
+ # Time: O(max(n1, n2 ))
4
+ # Space: O(max(n1, n2 ))
5
5
6
6
class Solution :
7
7
def addStrings (self , num1 : str , num2 : str ) -> str :
8
8
res = []
9
+
10
+ p1 = len (num1 ) - 1
11
+ p2 = len (num2 ) - 1
9
12
carry = 0
10
-
11
- p1 , p2 = len (num1 ) - 1 , len (num2 ) - 1
12
-
13
+
13
14
while p1 >= 0 or p2 >= 0 :
14
15
x1 = ord (num1 [p1 ]) - ord ('0' ) if p1 >= 0 else 0
15
16
x2 = ord (num2 [p2 ]) - ord ('0' ) if p2 >= 0 else 0
16
-
17
17
value = (x1 + x2 + carry ) % 10
18
18
carry = (x1 + x2 + carry ) // 10
19
-
20
19
res .append (value )
21
20
p1 -= 1
22
21
p2 -= 1
23
-
22
+
24
23
if carry :
25
24
res .append (carry )
26
-
25
+
27
26
return '' .join (str (x ) for x in res [::- 1 ])
28
27
29
-
You can’t perform that action at this time.
0 commit comments