Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f7ffc9c

Browse files
author
Mali Shivmangalsing
committed
043 multiply strings
1 parent abdef73 commit f7ffc9c

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Fails as number is out of range
3+
* @param {string} num1
4+
* @param {string} num2
5+
* @return {string}
6+
*/
7+
var multiply1 = function (num1, num2) {
8+
return (num1 * num2 * 1).toString();
9+
};
10+
11+
/**
12+
* @param {string} num1
13+
* @param {string} num2
14+
* @return {string}
15+
*/
16+
var multiply = function (num1, num2) {
17+
let m = num1.length, n = num2.length;
18+
let res = Array(m + n).fill(0);
19+
for (let i = m - 1; i >= 0; i--) {
20+
for (let j = n - 1; j >= 0; j--) {
21+
let temp = +num1.charAt(i) * +num2.charAt(j);
22+
let low = i + j + 1;
23+
let high = i + j;
24+
temp += res[low];
25+
res[low] = temp % 10;
26+
res[high] += Math.trunc(temp / 10);
27+
}
28+
}
29+
while (res[0] === 0) {
30+
res.shift();
31+
}
32+
return res.length === 0 ? "0" : res.join('');
33+
};
34+
35+
console.log(multiply("2","3"));
36+
console.log(multiply("123456789", "987654321"));
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Multiply Strings
2+
3+
Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2 also represented as a string.
4+
5+
## Example 1
6+
7+
Input: num1 = "2", num2 = "3"
8+
9+
Output: "6"
10+
11+
## Example 2
12+
13+
Input: num1 = "123", num2 = "456"
14+
15+
Output: "56088"
16+
17+
## Note
18+
19+
The length of both num1 and num2 is < 110.
20+
21+
Both num1 and num2 contain only digits 0-9.
22+
23+
Both num1 and num2 do not contain any leading zero, except the number 0 itself.
24+
25+
You must not use any built-in BigInteger library or convert the inputs to integer directly.
26+
27+
## More Info
28+
29+
<https://leetcode.com/problems/multiply-strings/>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /