-
Notifications
You must be signed in to change notification settings - Fork 3
/
306.cpp
28 lines (28 loc) · 1.04 KB
/
306.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
class Solution {
public:
bool backtracking(string& num, int currIdx, long long first, long long second, int count) {
if (currIdx == num.size() && count >= 3) {
return true;
}
for (int i = 1; currIdx + i <= num.size() && i < 18; ++i) {
string currStr = num.substr(currIdx, i);
if (currStr.size() > 1 && currStr[0] == '0') break;
long long curr = stoll(currStr);
if (first != -1 && second != -1) {
if (first + second == curr) {
if (backtracking(num, currIdx + i, second, curr, count + 1)) return true;
}
}
else if (first == -1 && second != -1) {
if (backtracking(num, currIdx + i, second, curr, count + 1)) return true;;
}
else {
if (backtracking(num, currIdx + i, -1, curr, count + 1)) return true;
}
}
return false;
}
bool isAdditiveNumber(string num) {
return backtracking(num, 0, -1, -1, 0);
}
};