# 43. Multiply Strings

Given two non-negative integers `num1` and `num2` represented as strings, return the product of `num1` and `num2`, also represented as a string.

Note: You must not use any built-in BigInteger library or convert the inputs to integer directly.

Example 1:

``````Input: num1 = "2", num2 = "3"
Output: "6"
``````

Example 2:

``````Input: num1 = "123", num2 = "456"
Output: "56088"
``````

Constraints:

• `1 <= num1.length, num2.length <= 200`
• `num1` and `num2` consist of digits only.
• Both `num1` and `num2` do not contain any leading zero, except the number `0` itself.

``````    8 9  <- num2
7 6  <- num1
-------
5 4
4 8
6 3
5 6
-------
6 7 6 4
``````

``````class Solution {
public:
string multiply(string num1, string num2) {
string res;
int m = num1.size(), n = num2.size();
vector<int> vals(m + n);
for (int i = m - 1; i >= 0; --i) {
for (int j = n - 1; j >= 0; --j) {
int mul = (num1[i] - '0') * (num2[j] - '0');
int p1 = i + j, p2 = i + j + 1, sum = mul + vals[p2];
vals[p1] += sum / 10;
vals[p2] = sum % 10;
}
}
for (int val : vals) {
if (!res.empty() || val != 0) res.push_back(val + '0');
}
return res.empty() ? "0" : res;
}
};
``````

Github 同步地址：

https://github.com/grandyang/leetcode/issues/43

Plus One

Apply Discount to Prices

https://leetcode.com/problems/multiply-strings/

https://leetcode.com/problems/multiply-strings/discuss/17605/Easiest-JAVA-Solution-with-Graph-Explanation

LeetCode All in One 题目讲解汇总(持续更新中…)

（欢迎加入博主的知识星球，博主将及时答疑解惑，并分享刷题经验与总结，快快加入吧～）

|

Venmo 打赏

—|—

 微信打赏 Venmo 打赏
（欢迎加入博主的知识星球，博主将及时答疑解惑，并分享刷题经验与总结，试运营期间前五十位可享受半价优惠～）

×

Help us with donation