The arguments are passed as strings.
The numbers may be way very large
Answer should be returned as a string
The returned "number" should not start with zeros e.g. 0123 is invalid
function multiply(a, b) {
// 通常的算法操作
/*
例如 123 * 456
123
* 456
---
738
615
492
----
56088
*/
// 为了方便计算,先把数字反转,进位就不朝前了,而是朝后 到最后再反转回来
var aa = a.split('').reverse(); //
var bb = b.split('').reverse();
var stack = [];
for (var i = 0; i < aa.length; i++) {
for (var j = 0; j < bb.length; j++) {
var m = aa[i] * bb[j];
stack[i + j] = (stack[i + j]) ? stack[i + j] + m : m;
}
}
// 组合成一维数组 给下面进位操作
for (var i = 0; i < stack.length; i++) {
var num = stack[i] % 10;
var move = Math.floor(stack[i] / 10);
stack[i] = num;
// 大于10 采取进位操作
if (stack[i + 1])
stack[i + 1] += move;
else if (move != 0)
stack[i + 1] = move;
}
return stack.reverse().join('').replace(/^(0(?!$))+/, ""); // 替换字符串前面的0
}