-
Notifications
You must be signed in to change notification settings - Fork 20
/
MultiplyStrings.js
94 lines (79 loc) · 2.06 KB
/
MultiplyStrings.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/**
* Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2.
*
* Note:
*
* The length of both num1 and num2 is < 110.
* Both num1 and num2 contains only digits 0-9.
* Both num1 and num2 does not contain any leading zero.
* You must not use any built-in BigInteger library or convert the inputs to integer directly.
*
* Accepted.
*/
/**
* @param {string} num1
* @param {string} num2
* @return {string}
*/
let multiply = function (num1, num2) {
let ints = new Array(num1.length + num2.length + 1);
for (let i = 0; i < ints.length; i++) {
ints[i] = 0;
}
for (let i = 0; i < num1.length; i++) {
for (let j = 0; j < num2.length; j++) {
ints[(num1.length - i - 1) + (num2.length - j - 1)] += (num1[i].charCodeAt(0) - '0'.charCodeAt(0)) * (num2[j].charCodeAt(0) - '0'.charCodeAt(0))
}
}
let res = [];
for (let m = 0; m < num1.length + num2.length; m++) {
let digit = ints[m] % 10;
let carry = parseInt(ints[m] / 10);
res.splice(0, 0, digit.toString());
if (m < (num1.length + num2.length - 1)) {
ints[m + 1] += carry
}
}
while (res.length > 0 && res[0] === "0") {
res.splice(0, 1)
}
if (res.length === 0) {
return "0"
}
return res.join('');
};
if (multiply("", "") === "0") {
console.log("pass")
} else {
console.error("failed")
}
if (multiply("0", "0") === "0") {
console.log("pass")
} else {
console.error("failed")
}
if (multiply("100", "1") === "100") {
console.log("pass")
} else {
console.error("failed")
}
if (multiply("123", "45") === "5535") {
console.log("pass")
} else {
console.error("failed")
}
if (multiply("99", "99") === "9801") {
console.log("pass")
} else {
console.error("failed")
}
if (multiply("123", "123") === "15129") {
console.log("pass")
} else {
console.error("failed")
}
if (multiply("123456789", "123456789") === "15241578750190521") {
console.log("pass")
} else {
console.error("failed")
}