-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLC0415.cpp
More file actions
50 lines (47 loc) · 1.28 KB
/
LC0415.cpp
File metadata and controls
50 lines (47 loc) · 1.28 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
47
48
49
50
#include <iostream>
#include <string>
using namespace std;
class Solution {
public:
string addStrings(string num1, string num2) {
char rev[5105];
int i=0, p=0;
int l1 = num1.size();
int l2 = num2.size();
while (i<l1 && i<l2) {
int x = (num1[l1-i-1]-'0') + (num2[l2-i-1]-'0') + p;
rev[i] = '0' + (x%10);
p = x/10;
i++;
}
while (i<l1) {
int x = (num1[l1-i-1]-'0') + p;
rev[i] = '0' + (x%10);
p = x/10;
i++;
}
while (i<l2) {
int x = (num2[l2-i-1]-'0') + p;
rev[i] = '0' + (x%10);
p = x/10;
i++;
}
if (p>0) {
rev[i] = '0' + p;
p=0;
i++;
}
char result[5105];
for (int k=i-1; k>=0; k--) {
result[i-k-1] = rev[k];
}
result[i]='\0';
return string(result);
}
};
int main() {
Solution s;
cout << s.addStrings("123", "345") << endl;
cout << s.addStrings("1", "345") << endl;
cout << s.addStrings("123", "3") << endl;
}