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
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
66class 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-
You can’t perform that action at this time.
0 commit comments