Open meibin08 opened 5 years ago
js解题思路:
var addToArrayForm = (A, B) => {
let arrA = A.split("");
let arrB = B.split("");
if (A.length < B.length) {
[arrA, arrB] = [arrB, arrA];
}
arrB.unshift(...(new Array(arrA.length - arrB.length).fill(0)));
for (var i = arrA.length - 1; i >= 0; i--) {
arrA[i] = parseInt(arrA[i]) + parseInt(arrB[i]);
if (arrA[i] >= 10) {
arrA[i] = arrA[i] % 10;
if (i == 0) {
arrA.unshift(1);
} else {
arrA[i - 1] = parseInt(arrA[i - 1]) + 1;
}
}
};
return arrA.join("");;
};
addToStringForm("19","1")
阿里伯乐代码评测系统题(来自微信群,大佬的截图,自己汇总一下) 2个正整数字符串
A
、B
的相加,返回结果, PS:考虑超长字符串相加示例:
示例: