class Solution {
public:
string multiplyPositive(string num1, string num2) {
reverse(num1.begin(), num1.end());
reverse(num2.begin(), num2.end());
int n = num1.length(), m = num2.length();
vector<int> product(n + m);
int carry = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
carry += (num1[i] - '0') * (num2[j] - '0') + product[i + j];
product[i + j] = carry % 10;
carry /= 10;
}
product[i + m] = carry;
carry = 0;
}
int k = n + m - 1;
while (k >= 0 && !product[k]) {
--k;
}
if (k < 0) {
return "0";
} else {
string s;
for (int i = k; i >= 0; --i) {
s += char('0' + product[i]);
}
return s;
}
}
string multiply(string num1, string num2) {
string s1, s2;
if (num1.length() == 0 || num2.length() == 0) {
return "0";
}
bool neg = false;
if (num1[0] == '-') {
s1 = num1.substr(1, num1.length());
neg = !neg;
} else {
s1 = num1;
}
if (num2[0] == '-') {
s2 = num2.substr(1, num2.length());
neg = !neg;
} else {
s2 = num2;
}
if (s1.length() == 0 || s2.length() == 0) {
return "0";
}
string s = multiplyPositive(s1, s2);
if (neg && s != "0") {
s = '-' + s;
}
return s;
}
};