-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultiply-strings.cpp
85 lines (83 loc) · 2.66 KB
/
multiply-strings.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include "header.h"
class Solution {
public:
string multiply(string num1, string num2) {
int l1 = num1.size(), l2 = num2.size();
string ans = "0";
for(int i = l1 - 1; i >= 0; i --) {
string temp = smallMultiplyWithDigits(num1[i] - '0', num2, l1 - i - 1);
ans = add(ans, temp);
}
return ans;
}
string smallMultiplyWithDigits(int small, string large, int digits) {
int step = 0;
int l = large.size();
string ans;
for(int i = l - 1; i >= 0; i --) {
int temp = (large[i] - '0') * small + step;
step = temp / 10;
temp %= 10;
ans += (temp + '0');
}
while(step > 0) {
int temp = step;
step = temp / 10;
temp %= 10;
ans += (temp + '0');
}
reverse(ans.begin(), ans.end());
string zeroes(digits, '0');
return (ans.size() == 0 && ans[0] == '0')? ans: ans + zeroes;
}
string add(string num1, string num2) {
int l1 = num1.size(), l2 = num2.size();
string ans;
int i = l1 - 1, j = l2 - 1;
int step = 0;
while(i >= 0 && j >= 0) {
int a = num1[i] - '0', b = num2[j] - '0';
int temp = a + b + step;
step = (temp > 9) ? 1: 0;
temp %= 10;
ans += (temp + '0');
i--; j--;
}
while(i >= 0) {
int a = num1[i] - '0';
int temp = a + step;
step = (temp > 9) ? 1: 0;
temp %= 10;
ans += (temp + '0');
i--;
}
while(j >= 0) {
int a = num2[j] - '0';
int temp = a + step;
step = (temp > 9) ? 1: 0;
temp %= 10;
ans += (temp + '0');
j--;
}
if(step > 0) {
ans += (step + '0');
step = 0;
}
reverse(ans.begin(), ans.end());
for(i = 0; i < ans.size(); i++) {
if(ans[i] != '0') {
break;
}
}
ans = ans.substr(i);
if(ans.size() == 0) ans = "0";
return ans;
}
};
int main() {
Solution a;
cout << a.add("100", "23") << endl;
cout << a.smallMultiplyWithDigits(11, "99", 2) << endl;
cout << a.multiply("123", "456") << endl;
cout << a.multiply("123", "0") << endl;
}