力扣 43 字符串相乘 c++
时间: 2023-12-05 15:36:56 浏览: 149
好的,关于力扣 43 题字符串相乘,可以使用竖式乘法的思想,从低位到高位逐位相乘,最后再进行进位处理。具体实现可以参考以下代码:
```c++
class Solution {
public:
string multiply(string num1, string num2) {
int n1 = num1.size(), n2 = num2.size();
vector<int> res(n1 + n2);
for (int i = n1 - 1; i >= 0; i--) {
for (int j = n2 - 1; j >= 0; j--) {
int mul = (num1[i] - '0') * (num2[j] - '0');
int p1 = i + j, p2 = i + j + 1;
int sum = mul + res[p2];
res[p2] = sum % 10;
res[p1] += sum / 10;
}
}
string ans;
for (int num : res) {
if (!ans.empty() || num != 0) ans.push_back(num + '0');
}
return ans.empty() ? "0" : ans;
}
};
```
阅读全文