现附上代码:
class Solution {
public:
string multiply(string num1, string num2) {
if (num1 == "0" || num2 == "0")return "0";
vector<int> res (num1.size()+num2.size()-1,0);
int len = num1.size()+num2.size()-2;
string ans = "";
for (int i=0;i<num1.size();++i){
for (int j=0;j<num2.size();++j){
res[len-i-j] += (num1[i]-'0') * (num2[j]-'0');
}
}
//for_each(res.begin(),res.end(),[](int& pa){cout<<pa<<",";});
int overflow = 0;
int digit = 0;
//cout<<"Here"<<endl;
int tmp = 0;
for (int i=0;i<=len;++i){
tmp = res[i] + overflow;
overflow = tmp/10;
digit = tmp%10;
ans.append(1,digit+'0');
}
while (overflow > 0){
digit = overflow%10;
overflow = overflow/10;
ans.append(1,digit+'0');
}
reverse(ans.begin(),ans.end());
res.clear();
vector<int>().swap(res);
return ans;
}
};