-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path114MultiplyStrings.cpp
More file actions
35 lines (33 loc) · 949 Bytes
/
Copy path114MultiplyStrings.cpp
File metadata and controls
35 lines (33 loc) · 949 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
#include<iostream>
#include<vector>
#include<string>
using namespace std;
class Solution {
public:
string multiply(string num1, string num2) {
if (num1 == "0" || num2 == "0")
return "0";
int n = num1.size();
int m = num2.size();
vector<int>result(m + n, 0);
for(int i = n - 1; i >= 0; i--){
for(int j = m - 1; j >= 0; j--){
int mul = (num1[i] - '0') * (num2[j] - '0');
int sum = mul + result[i + j + 1];
result[i + j + 1] = sum % 10;
result[i + j] += sum/10;
}
}
string answer; int k = 0;
while (k < (int)result.size() && result[k] == 0) k++;
for(; k < (int)result.size(); k++){
answer.push_back(result[k] + '0');
}
return answer;
}
};
int main(){
Solution obj;
cout<<obj.multiply("234","325");
return 0;
}