-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmultiply-strings.cc
More file actions
38 lines (35 loc) · 1.13 KB
/
Copy pathmultiply-strings.cc
File metadata and controls
38 lines (35 loc) · 1.13 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
#include <string>
#include <algorithm>
#include <iostream>
using namespace std;
class Solution {
public:
string multiply(string num1, string num2) {
string result(num1.length()+num2.length()+1, '0');
string rnum1(num1.rbegin(), num1.rend());
string rnum2(num2.rbegin(), num2.rend());
for (std::size_t i=0; i<rnum1.length(); ++i) {
int x = rnum1[i] - '0';
int carry = 0;
for (std::size_t j=0; j<rnum2.length(); ++j) {
int exist = result[i+j] - '0';
int y = rnum2[j] - '0';
int r = x * y + carry + exist;
result[i+j] = r % 10 + '0';
carry = r / 10;
}
if (carry) {
result[i+num2.length()] = carry + '0';
}
}
std::reverse(result.begin(), result.end());
int pos = result.find_first_not_of('0');
if (pos == string::npos) return "0";
return result.substr(pos);
}
};
int main() {
Solution solution;
std::cout << solution.multiply("128371923718237987897", "9781283971239879") << std::endl;
return 0;
}