131 - 415 字符串相加
题目
解答
var addStrings = function(num1, num2) {
let res = [],
i = num1.length - 1,
j = num2.length - 1,
carry = 0
while (i >= 0 || j >= 0) {
const n1 = i < 0 ? 0 : parseInt(num1[i])
const n2 = j < 0 ? 0 : parseInt(num2[j])
const temp = n1 + n2 + carry
carry = ~~(temp / 10)
res.push(temp % 10)
i--
j--
}
if (carry) {
res.push(carry)
}
return res.reverse().join("")
};Last updated