-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0043.cpp
66 lines (59 loc) · 1.31 KB
/
0043.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
54
55
56
57
58
59
60
61
62
63
64
65
66
class Solution {
public:
string multiply(string num1, string num2) {
if (is_zero(num1) || is_zero(num2)) {
return "0";
}
reverse(num1.begin(), num1.end());
reverse(num2.begin(), num2.end());
string result, base;
int index = 0;
while (index < num2.size()) {
string temp = mul(num1, num2[index] - '0');
result = add(result, base + temp);
base.push_back('0');
++ index;
}
reverse(result.begin(), result.end());
return result;
}
bool is_zero(const string &a) {
return a.size() == 1 && a[0] == '0';
}
string add(const string &a, const string &b) {
string result;
int index = 0, carry = 0;
while (index < a.size() || index < b.size() || carry) {
int temp = carry;
if (index < a.size()) {
temp += a[index] - '0';
}
if (index < b.size()) {
temp += b[index] - '0';
}
carry = temp / 10;
temp %= 10;
result.push_back(temp + '0');
++ index;
}
return result;
}
string mul(string a, int digit) {
string result;
if (digit %10 == 10) {
return result + '0';
}
int carry = 0, index = 0;
while (index < a.size()) {
int temp = (a[index] - '0') * digit + carry;
carry = temp / 10;
temp %= 10;
result.push_back(temp + '0');
++ index;
}
if (carry) {
result.push_back(carry + '0');
}
return result;
}
};