File tree 1 file changed +31
-0
lines changed
1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change
1
+ /*Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2.
2
+
3
+ Note:
4
+
5
+ The length of both num1 and num2 is < 5100.
6
+ Both num1 and num2 contains only digits 0-9.
7
+ Both num1 and num2 does not contain any leading zero.
8
+ You must not use any built-in BigInteger library or convert the inputs to integer directly.*/
9
+
10
+ /**
11
+ * @param {string } num1
12
+ * @param {string } num2
13
+ * @return {string }
14
+ */
15
+ var addStrings = function ( num1 , num2 ) {
16
+ b = num1 . length >= num2 . length ? num1 : num2
17
+ s = num1 . length < num2 . length ? num1 : num2
18
+ ans = ""
19
+ carry = 0
20
+ for ( i = b . length - 1 , j = s . length - 1 ; i >= 0 ; i -- , j -- ) {
21
+ if ( s [ j ] != null )
22
+ sum = Number ( b [ i ] ) + Number ( s [ j ] ) + carry
23
+ else
24
+ sum = Number ( b [ i ] ) + carry
25
+ ans = sum % 10 + ans
26
+ carry = parseInt ( sum / 10 )
27
+ }
28
+ if ( carry == 1 )
29
+ return carry + ans
30
+ return ans
31
+ } ;
You can’t perform that action at this time.
0 commit comments