forked from cpselvis/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution043.cpp
More file actions
70 lines (60 loc) · 1.21 KB
/
solution043.cpp
File metadata and controls
70 lines (60 loc) · 1.21 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/**
* Multiply Strings
*
* cpselvis(cpselvis@gmail.com)
* September 3th, 2016
*/
#include<iostream>
#include<vector>
using namespace std;
class Solution {
public:
string multiply(string num1, string num2) {
string ret = "";
int l1 = num1.size();
int l2 = num2.size();
vector<int> v1(l1);
vector<int> v2(l2);
vector<int> v3(l1 + l2);
if (num1 == "0" || num2 == "0")
{
return "0";
}
for (int i = 0; i < l1; i ++)
{
v1[l1 - i - 1] = num1[i] - '0';
}
for (int j = 0; j < l2; j ++)
{
v2[l2 - j - 1] = num2[j] - '0';
}
for (int i = 0; i < l1; i ++)
{
for (int j = 0; j < l2; j ++)
{
v3[i + j] += v1[i] * v2[j];
if (v3[i + j] >= 10)
{
v3[i + j + 1] += v3[i + j] / 10;
v3[i + j] %= 10;
}
}
}
for (int i = v3.size() - 1; i >= 0; i --)
{
if (i != v3.size() - 1 || (i == v3.size() - 1 && v3[i] != 0))
{
ret += v3[i] + '0';
}
}
return ret;
}
};
int main(int argc, char **argv)
{
Solution s;
cout << s.multiply("2", "2") << endl;
cout << s.multiply("12", "12") << endl;
cout << s.multiply("111", "111") << endl;
cout << s.multiply("123", "456") << endl;
}