-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleet43.cpp
More file actions
51 lines (36 loc) · 966 Bytes
/
leet43.cpp
File metadata and controls
51 lines (36 loc) · 966 Bytes
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
#include<iostream>
#include<string>
#include<vector>
#include<typeinfo>
using namespace std;
class Solution {
public:
string multiply(string num1, string num2) {
int m = num1.size();
int n = num2.size();
vector<int> res(m+n);
string ans = "";
for(int i = m-1; i>=0; --i){
for(int j = n-1; j>=0; --j){
int ind1 = i + j;
int ind2 = i + j + 1;
int sum = (num1[i]-'0')*(num2[j]-'0') + res[ind2];
res[ind1] += sum / 10;
res[ind2] = sum % 10;
}
}
for(auto a:res){
if (ans.size() !=0 || a != 0)
ans += to_string(a);
}
return ans.size() == 0 ? "0" : ans;
}
};
int main(){
string a = "123";
string b = "456";
Solution sol;
string ans = sol.multiply(a, b);
cout << ans << endl;
return 0;
}