-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathMultiply_Strings.cpp
45 lines (45 loc) · 1.57 KB
/
Multiply_Strings.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
class Solution {
public:
string multiply(string num1, string num2) {
string product = "";
string result = "";
string zeros = "";
for(int i = num1.size() - 1; i >= 0; --i) {
int digit1 = (int) (num1[i] - '0');
int carry = 0;
for(int j = num2.size() - 1; j >= 0; --j) {
int digit2 = (int) (num2[j] - '0');
int multply = digit1 * digit2;
multply += carry;
product += to_string(multply % 10);
carry = multply / 10;
}
while(carry) {
product += to_string(carry % 10);
carry /= 10;
}
reverse(product.begin(), product.end());
if(result.length() > 0) {
string sum = "";
for(int k = result.size() - 1, l = product.size() - 1; k >= 0 || l >= 0 || carry > 0; --k, --l) {
int d1 = k >= 0 ? (int) (result[k] - '0') : 0;
int d2 = l >= 0 ? (int) (product[l] - '0') : 0;
int total = d1 + d2 + carry;
sum += to_string(total % 10);
carry = total / 10;
}
reverse(sum.begin(), sum.end());
result = sum;
} else {
result = product;
}
zeros += "0";
product = zeros;
}
int indx = 0;
while(indx < result.length() - 1 && result[indx] == '0') {
indx++;
}
return result.substr(indx);
}
};