-
Notifications
You must be signed in to change notification settings - Fork 0
/
200805-1.cpp
73 lines (71 loc) · 1.62 KB
/
200805-1.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
// https://leetcode-cn.com/problems/additive-number/
#include <iostream>
#include <string>
using namespace std;
class Solution {
public:
bool isAdditiveNumber(string num) {
int n = num.size();
for (int a = 1; a < n; ++a) {
for (int b = 1; b < n; ++b) {
if (check(num.c_str(), n, 0, a, b)) {
return true;
}
}
}
return false;
}
private:
bool check(const char* s, int n, int start, int a, int b) {
//cout << "check: " << s << ", " << n << ", " << start << ", " << a << ", " << b << endl;
if (start + a + b > n) {
return false;
}
if (a > 1 && s[start] == '0') {
return false;
}
if (b > 1 && s[start + a] == '0') {
return false;
}
string r;
int c = 0;
for (int i = 1; i <= a || i <= b; ++i) {
int x = c;
if (i <= a) {
x += (s[start + a - i] - '0');
}
if (i <= b) {
x += (s[start + a + b - i] - '0');
}
if (x >= 10) {
c = 1;
x %= 10;
} else {
c = 0;
}
r = static_cast<char>('0' + x) + r;
}
if (c > 0) {
r = '1' + r;
}
//cout << ": " << string(s + start, s + start + a) << " + " << string(s + start + a, s + start + a + b) << " = " << r << endl;
if (start + a + b + r.size() > n) {
return false;
}
if (r != string(s + start + a + b, s + start + a + b + r.size())) {
return false;
}
if (start + a + b + r.size() == n) {
return true;
}
return check(s, n, start + a, b, r.size());
}
};
int main()
{
Solution s;
cout << s.isAdditiveNumber("112358") << endl; // answer: true
cout << s.isAdditiveNumber("199100199") << endl; // answer: true
cout << s.isAdditiveNumber("199001200") << endl; // answer: false
return 0;
}