Skip to content

Commit 07cd31b

Browse files
committed
415
1 parent 3884ba0 commit 07cd31b

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

415/main.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @param {string} num1
3+
* @param {string} num2
4+
* @return {string}
5+
*/
6+
var addStrings = function(num1, num2) {
7+
const ans = []
8+
let remainder = 0
9+
let i = num1.length - 1
10+
let j = num2.length - 1
11+
while (remainder || i >= 0 || j >= 0) {
12+
let sum = (num1[i] == null ? 0 : +num1[i]) + (num2[j] == null ? 0 : +num2[j]) + remainder
13+
ans.push(sum % 10)
14+
remainder = Math.floor(sum / 10)
15+
j--
16+
i--
17+
}
18+
return ans.reverse().join('')
19+
};
20+
21+
console.log(addStrings('0', '0'))

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@
135135
- [398 Random Pick Index](./398)
136136
- [347 Top K Frequent Elements](./347)
137137
- [414 Third Maximum Number](./414)
138+
- [415 Add Strings](./415)
138139
- [442 Find All Duplicates in an Array](./442)
139140
- [448 Find All Numbers Disappeared in an Array](./448)
140141
- [495 Teemo Attacking](./495)

0 commit comments

Comments
 (0)