-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathLeetCode#36.cc
More file actions
40 lines (39 loc) · 1.1 KB
/
Copy pathLeetCode#36.cc
File metadata and controls
40 lines (39 loc) · 1.1 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
class Solution {
private:
bool isZero(string& num){
if(num.length()==1 && num[0]=='0')
return true;
return false;
}
public:
string multiply(string num1, string num2) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(isZero(num1) || isZero(num2)) return "0";
string ret;
int len1 = num1.length();
int len2 = num2.length();
reverse(num1.begin(),num1.end());
reverse(num2.begin(),num2.end());
for(int j=0;j<len2;j++){
int c = 0;
for(int i=0;i<len1;i++){
int v = (num1[i]-'0')*(num2[j]-'0')+c;
if(i+j==ret.length()){
ret+=(char)(v%10+'0');
c=v/10;
}
else{
v += ret[i+j]-'0';
ret[i+j]=(char)(v%10+'0');
c=v/10;
}
}
if(c){
ret+=(char)(c+'0');
}
}
reverse(ret.begin(),ret.end());
return ret;
}
};