-
Notifications
You must be signed in to change notification settings - Fork 160
/
Multiply_Strings.cc
34 lines (34 loc) · 1.16 KB
/
Multiply_Strings.cc
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
class Solution {
public:
string multiply(string num1, string num2) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if (num1 == "0" || num2 == "0") {
return "0";
}
string ret = "";
ret.append(num1.length() + num2.length(), '0');
reverse(num1.begin(), num1.end());
reverse(num2.begin(), num2.end());
int nxt = 0, cur, tmp;
for (int i = 0; i < num2.length(); i++) {
for (int j = 0; j < num1.length(); j++) {
cur = ret[i + j] - '0' + nxt + (num2[i] - '0') * (num1[j] - '0');
ret[i + j] = (cur % 10) + '0';
nxt = cur / 10;
}
tmp = num1.length();
while (nxt > 0) {
cur = ret[i + tmp] - '0' + nxt;
ret[i + tmp] = (cur % 10) + '0';
nxt = cur / 10;
tmp++;
}
}
while (ret.length() > 1 && ret[ret.length() - 1] == '0') {
ret.resize(ret.length() - 1);
}
reverse(ret.begin(), ret.end());
return ret;
}
};