-
Notifications
You must be signed in to change notification settings - Fork 2
/
leetcode43.cpp
53 lines (51 loc) · 1.49 KB
/
leetcode43.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
46
47
48
49
50
51
52
53
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Solution {
public:
string multiply(string num1, string num2) {
int len1 = num1.length();
int len2 = num2.length();
vector<int> nums1(len1, 0);
vector<int> nums2(len2, 0);
for (int i = len1 - 1; i >= 0; i--)
nums1[len1 - 1 - i] = num1[i] - '0';
for (int i = len2 - 1; i >= 0; i--)
nums2[len2 - 1 - i] = num2[i] - '0';
vector<int> ans;
ans.reserve(len1 + len2);
int cur = 0;
int add = 0;
for (int i = 0; i < len1; i++) {
int tmp = nums1[i];
for (int j = 0; j < len2; j++) {
cur = tmp * nums2[j] + add;
if (cur >= 10) {
add = cur / 10;
cur = cur % 10;
} else
add = 0;
int k = i + j;
if (k >= ans.size())
ans.push_back(cur);
else {
cur += ans[k];
if (cur >= 10) {
cur = cur % 10;
add++;
}
ans[k] = cur;
}
}
if (add > 0)
ans.push_back(add);
}
string ret;
int size = ans.size();
ret.reserve(size);
for (int i = size - 1; i >= 0; i--)
ret.append(1, ans[i] + '0');
return ret;
}
};