Sharing my easy understanding solution with explanation

• ``````string multiply(string num1, string num2) {
if(num1 == "0" || num2 == "0"){
return "0";
}
string result;
for(int i = num2.length() - 1; i >= 0; i--){
string temp = mul(num1, num2[i]);
for(int j = 0; j < num2.length() - 1 - i; j++){
temp.push_back('0');
}
}
return result;
}
string mul(string num, char c){
string result;
int carry = 0;
for(int i = num.length() - 1; i >= 0; i--){
int product = (num[i] - '0') * (c - '0') + carry;
int n = product % 10;
carry = product / 10;
result.insert(result.begin(), n + '0');
}
if(carry != 0){
result.insert(result.begin(), carry + '0');
}
return result;
}
string result;
int carry = 0;
for(int i = num1.length() - 1, j = num2.length() - 1; i >= 0 || j >= 0; i--, j--){
int o1 = i >= 0 ? num1[i] : '0';
int o2 = j >= 0 ? num2[j] : '0';
int sum = (o1 - '0') + (o2 - '0') + carry;
int n = sum % 10;
carry = sum / 10;
result.insert(result.begin(), n + '0');
}
if(carry != 0){
result.insert(result.begin(), carry + '0');
}
return result;
}
``````

This solution is straightforward because it just implements normal multiplication.

Function mul implements a string times a char. And function add implements a string adds another string.

Please try to remember how to do multiplication manually. This program follows that way.

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.