发现了简单的做法:
class Solution {
public:
string multiply(string num1, string num2) {
reverse(num1.begin(), num1.end());
reverse(num2.begin(), num2.end());
int sz1 = num1.size(), sz2 = num2.size();
vector<int> res(sz1+sz2, 0);
for (int i = 0; i < sz1; ++i)
for (int j = 0; j < sz2; ++j) {
res[i+j] += (num1[i]-'0') * (num2[j]-'0');//直接记录下来,最后考虑进位
}
int car = 0;
stringstream ss;
for (int num : res) {
num += car;
car = num / 10;
num %= 10;
ss << num;
}
string ret = ss.str();
reverse(ret.begin(), ret.end());
int i = 0;
while (i < ret.size() && ret[i] == '0')
++i;
return i == ret.size() ? "0" : ret.substr(i);
}
};