- 核心代码(leetcode/牛客可执行)
class Solution {
public:
string multiply(string num1, string num2) {
int l1 = num1.length(), l2 = num2.length();
string res(l1+l2, '0');
int i, j;
for(i=l1-1; i>=0; i--){
for(j=l2-1; j>=0; j--){
int p1=i+j, p2=i+j+1;
int mul = (num1[i] -'0') * (num2[j] - '0') + (res[p2] - '0');
res[p2] = mul % 10 + '0';
res[p1] = res[p1] - '0' + (mul / 10 + '0');
}
}
int k =0;
while(k < res.length() && res[k] == '0'){
k++;
}
res = res.substr(k);
return res.length() == 0 ? "0" : res;
}
};
- 完整可运行代码
待补充