We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 7c80dc9 commit eec6888Copy full SHA for eec6888
0415-add-strings/solution.py
@@ -1,29 +1,27 @@
1
-# Approach 1 - Ordinals + Elementary Math
+# Approach 1: Elementary Math
2
3
-# Time: O(max(N1, N2))
4
-# Space: O(max(N1, N2))
+# Time: O(max(n1, n2))
+# Space: O(max(n1, n2))
5
6
class Solution:
7
def addStrings(self, num1: str, num2: str) -> str:
8
res = []
9
+
10
+ p1 = len(num1) - 1
11
+ p2 = len(num2) - 1
12
carry = 0
-
- p1, p2 = len(num1) - 1, len(num2) - 1
13
14
while p1 >= 0 or p2 >= 0:
15
x1 = ord(num1[p1]) - ord('0') if p1 >= 0 else 0
16
x2 = ord(num2[p2]) - ord('0') if p2 >= 0 else 0
17
value = (x1 + x2 + carry) % 10
18
carry = (x1 + x2 + carry) // 10
19
20
res.append(value)
21
p1 -= 1
22
p2 -= 1
23
24
if carry:
25
res.append(carry)
26
27
return ''.join(str(x) for x in res[::-1])
28
29
0 commit comments