forked from avastino7/Algorithms-Data-Structures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Multiply Strings.cpp
31 lines (23 loc) · 909 Bytes
/
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
/*
Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2, also represented as a string.
Note: You must not use any built-in BigInteger library or convert the inputs to integer directly.
*/
class Solution {
public:
string multiply(string num1, string num2) {
if (num1 == "0" || num2 == "0") return "0";
vector<int> res(num1.size()+num2.size(), 0);
for (int i = num1.size()-1; i >= 0; i--) {
for (int j = num2.size()-1; j >= 0; j--) {
res[i + j + 1] += (num1[i]-'0') * (num2[j]-'0');
res[i + j] += res[i + j + 1] / 10;
res[i + j + 1] %= 10;
}
}
int i = 0;
string ans = "";
while (res[i] == 0) i++;
while (i < res.size()) ans += to_string(res[i++]);
return ans;
}
};