-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultiply_strings.cpp
More file actions
34 lines (34 loc) · 995 Bytes
/
Copy pathmultiply_strings.cpp
File metadata and controls
34 lines (34 loc) · 995 Bytes
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
class Solution {
public:
string multiply(string num1, string num2) {
if (num1 == "0" || num2 == "0") {
return "0";
}
vector<int> answer(num1.size() + num2.size(), 0);
reverse(num1.begin(), num1.end());
reverse(num2.begin(), num2.end());
for (int i = 0; i < num1.size(); ++i) {
int a = (num1[i] - '0');
int carry = 0;
for (int j = 0; j < num2.size(); ++j) {
int b = (num2[j] - '0');
answer[i + j] += a*b + carry;
carry = answer[i + j] / 10;
answer[i + j] %= 10;
}
if (carry != 0) {
answer[i + num2.size()] += carry;
}
carry = 0;
}
string ans = "";
int i = answer.size() - 1;
while (answer[i] == 0) {
--i;
}
for (; i >= 0; --i) {
ans += to_string(answer[i]);
}
return ans;
}
};