Skip to content

Commit eec6888

Browse files
committed
Sync LeetCode submission Runtime - 3 ms (90.88%), Memory - 18.3 MB (6.96%)
1 parent 7c80dc9 commit eec6888

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

0415-add-strings/solution.py

+9-11
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,27 @@
1-
# Approach 1 - Ordinals + Elementary Math
1+
# Approach 1: Elementary Math
22

3-
# Time: O(max(N1, N2))
4-
# Space: O(max(N1, N2))
3+
# Time: O(max(n1, n2))
4+
# Space: O(max(n1, n2))
55

66
class Solution:
77
def addStrings(self, num1: str, num2: str) -> str:
88
res = []
9+
10+
p1 = len(num1) - 1
11+
p2 = len(num2) - 1
912
carry = 0
10-
11-
p1, p2 = len(num1) - 1, len(num2) - 1
12-
13+
1314
while p1 >= 0 or p2 >= 0:
1415
x1 = ord(num1[p1]) - ord('0') if p1 >= 0 else 0
1516
x2 = ord(num2[p2]) - ord('0') if p2 >= 0 else 0
16-
1717
value = (x1 + x2 + carry) % 10
1818
carry = (x1 + x2 + carry) // 10
19-
2019
res.append(value)
2120
p1 -= 1
2221
p2 -= 1
23-
22+
2423
if carry:
2524
res.append(carry)
26-
25+
2726
return ''.join(str(x) for x in res[::-1])
2827

29-

0 commit comments

Comments
 (0)