-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultiplyStrings.cpp
More file actions
33 lines (28 loc) · 971 Bytes
/
multiplyStrings.cpp
File metadata and controls
33 lines (28 loc) · 971 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
/**
*Given two numbers represented as strings, return multiplication of the numbers as a string.
*Note:The numbers can be arbitrarily large and are non-negative.
*Converting the input string to integer is NOT allowed.
*You should NOT use internal library such as BigInteger.
*/
class Solution {
public:
string multiply(string num1, string num2) {
string res(num1.size()+num2.size(),'0');
for(int i = num1.size()-1; i >= 0; i--)
{
int carry = 0;
for(int j = num2.size()-1; j >= 0; j--)
{
int temp = res[i+j+1] - '0' +(num1[i] - '0')*(num2[j]-'0')+carry;
res[i+j+1] = temp%10 + '0';
carry = temp/10;
}
res[i] = res[i] - '0' + carry +'0';
}
size_t startpos = res.find_first_not_of("0");
if (string::npos != startpos) {
return res.substr(startpos);
}
return "0";
}
};