-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd415.cpp
75 lines (62 loc) · 1.53 KB
/
add415.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <algorithm>
#include <iostream>
#include <string>
#define CTOI(x) x - '0'
#define ITOC(x) x + '0'
class Solution {
public:
std::string addStrings(std::string num1, std::string num2)
{
int carry = 0;
int i1 = num1.length() - 1;
int i2 = num2.length() - 1;
std::string res;
while (i1 >= 0 || i2 >= 0 || carry) {
int x = carry;
if (i1 >= 0) {
x += CTOI(num1[i1]);
i1--;
}
if (i2 >= 0) {
x += CTOI(num2[i2]);
i2--;
}
carry = x / 10;
res.push_back(ITOC(x % 10));
}
std::reverse(res.begin(), res.end());
return res;
}
};
int main()
{
std::string t11 = "3";
std::string t12 = "3";
std::string t21 = "9";
std::string t22 = "10";
std::string t31 = "69";
std::string t32 = "6969";
Solution s;
std::cout << s.addStrings(t11, t12) << std::endl;
std::cout << s.addStrings(t21, t22) << std::endl;
std::cout << s.addStrings(t31, t32) << std::endl;
return 0;
}
// class Solution {
// public:
// std::string addStrings(std::string num1, std::string num2)
// {
// if (num1.length() < num2.length()) std::swap(num1, num2);
// int carry = 0;
// int i1 = num1.length() - 1;
// int i2 = num2.length() - 1;
// std::string res;
// for (int x; i1 >= 0; i1--, i2--) {
// x = CTOI(num1[i1]) + (i2 >= 0 ? CTOI(num2[i2]) : 0) + carry;
// carry = x / 10;
// res.insert(res.begin(), ITOC(x % 10));
// }
// if (carry) res.insert(res.begin(), ITOC(carry));
// return res;
// }
// };