-
Notifications
You must be signed in to change notification settings - Fork 111
/
additive-number.cc
45 lines (44 loc) · 1.24 KB
/
additive-number.cc
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
// Additive Number
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define ROF(i, a, b) for (int i = (b); --i >= (a); )
class Solution {
public:
bool isAdditiveNumber(string num) {
int n = num.size();
ROF(u, 1, n)
if (num[u] != '0' || u == n-1)
FOR(v, max(0, 2*u-n), u)
if (num[v] != '0' || v == u-1) {
for (int p = n, q = u, r = v; ; ) {
int i = p, j = q, k = r, zero = 0, b = 0;
for (; i > q; i--) {
int t = num[i-1] - (j > r ? num[--j] : '0') - b;
if (t < 0)
t += 10, b = 1;
else
b = 0;
if (! t)
zero++;
else {
for (; zero; zero--)
if (! k || num[--k] != '0')
goto out;
if (! k || num[--k]-'0' != t)
goto out;
}
}
if (k == r && (! k || num[--k] != '0'))
goto out;
if (! k) {
if (j == r) return true;
goto out;
}
p = q;
q = r;
r = k;
}
out:;
}
return false;
}
};