forked from Aniket123cloud/hacktoberfestani-2k22
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path43. Multiply Strings.cpp
More file actions
46 lines (37 loc) · 1.19 KB
/
43. Multiply Strings.cpp
File metadata and controls
46 lines (37 loc) · 1.19 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
39
40
41
42
43
44
45
46
//Runtime: 4 ms, faster than 98.76% of C++ online submissions for Multiply Strings.
//Memory Usage: 7 MB, less than 32.37% of C++ online submissions for Multiply Strings.
class Solution {
public:
string multiply(string num1, string num2) {
int n1 = num1.size(), n2 = num2.size();
vector<int> res(n1+n2, 0);
reverse(num1.begin(), num1.end());
reverse(num2.begin(), num2.end());
for(int i = 0; i < n1; ++i){
for(int j = 0; j < n2; ++j){
res[i+j] += (num1[i]-'0') * (num2[j]-'0');
}
}
int carry = 0;
for(int i = 0; i < n1+n2; ++i){
int sum = (carry + res[i]);
res[i] = sum % 10;
carry = sum / 10;
// cout << res[i];
}
// cout << endl;
string ans;
int i = n1+n2-1;
while(i >= 0 && res[i] == 0){
--i;
}
// cout << i << endl;
//res[0...i] is meaningful
//ans is the reverse of res
for(; i >= 0; --i){
ans += (res[i]+'0');
}
if(ans == "") return "0";
return ans;
}
};