forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
35 lines (34 loc) · 935 Bytes
/
Solution.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
class Solution {
public:
string multiply(string num1, string num2) {
int m = num1.size(), n = num2.size();
vector<int> res(m + n);
for (int i = 0; i < n; ++i)
{
int b = num2[n - 1 - i] - '0';
mul(num1, b, i, res);
}
string ans = "";
for (int v : res) ans.push_back(v + '0');
while (ans.size() > 1 && ans.back() == '0') ans.pop_back();
reverse(ans.begin(), ans.end());
return ans;
}
void mul(string A, int b, int i, vector<int>& res) {
for (int j = A.size() - 1, t = 0; j >= 0 || t > 0; --j)
{
if (j >= 0)
{
int a = A[j] - '0';
t += a * b;
}
res[i++] += t % 10;
if (res[i - 1] >= 10)
{
res[i - 1] %= 10;
++res[i];
}
t /= 10;
}
}
};