-
Notifications
You must be signed in to change notification settings - Fork 65
/
415.add-strings.js
58 lines (54 loc) · 1.07 KB
/
415.add-strings.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
* @lc app=leetcode.cn id=415 lang=javascript
*
* [415] Add Strings
*/
// @lc code=start
/**
* @param {string} num1
* @param {string} num2
* @return {string}
*/
/*
1 2 3 i
4 9 j
1 7 2
*/
var addStrings = function(num1, num2) {
let len1 = num1.length;
let len2 = num2.length;
let i = len1 - 1;
let j = len2 - 1;
let carry = 0; // 进位
let ans = '';
while (i >= 0 || j >= 0) {
let curr1 = i < 0 ? 0 : num1[i] * 1;
let curr2 = j < 0 ? 0 : num2[j] * 1;
let { result, nextCarry } = add(curr1, curr2, carry);
carry = nextCarry;
ans = result + ans;
i--;
j--;
}
if (carry !== 0) {
ans = carry + ans;
}
return ans;
};
function add(n1, n2, lastCarry) {
let nextCarry = 0;
let result = 0;
let sum = n1 + n2 + lastCarry;
if (sum >= 10) {
let strSum = String(sum);
result = strSum.slice(1) * 1;
nextCarry = strSum.slice(0, 1) * 1;
} else {
result = sum
}
return {
result,
nextCarry
}
}
// @lc code=end