Skip to content

Commit 93a0931

Browse files
committed
adding
1 parent 97ad745 commit 93a0931

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// https://leetcode.com/problems/add-strings/
2+
3+
/**
4+
* @param {string} num1
5+
* @param {string} num2
6+
* @return {string}
7+
*/
8+
var addStrings = function(num1, num2) {
9+
let i = num1.length - 1;
10+
let j = num2.length - 1;
11+
let carry = 0;
12+
let result = '';
13+
14+
while (i >= 0 || j >= 0 || carry > 0) {
15+
const digit1 = i >= 0 ? parseInt(num1[i]) : 0;
16+
const digit2 = j >= 0 ? parseInt(num2[j]) : 0;
17+
18+
const sum = digit1 + digit2 + carry;
19+
carry = Math.floor(sum / 10);
20+
result += (sum % 10).toString();
21+
22+
i--;
23+
j--;
24+
}
25+
26+
return result.split('').reverse().join('');
27+
};
28+
29+
// Example usage:
30+
console.log(addStrings("123", "987")); // Output: "1110"

0 commit comments

Comments
 (0)