-
-
Notifications
You must be signed in to change notification settings - Fork 9k
/
Copy pathSolution.cpp
32 lines (32 loc) · 1011 Bytes
/
Solution.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
class Solution {
public:
vector<int> splitIntoFibonacci(string num) {
int n = num.size();
vector<int> ans;
function<bool(int)> dfs = [&](int i) -> bool {
if (i == n) {
return ans.size() > 2;
}
long long x = 0;
for (int j = i; j < n; ++j) {
if (j > i && num[i] == '0') {
break;
}
x = x * 10 + num[j] - '0';
if (x > INT_MAX || (ans.size() > 1 && x > (long long) ans[ans.size() - 1] + ans[ans.size() - 2])) {
break;
}
if (ans.size() < 2 || x == (long long) ans[ans.size() - 1] + ans[ans.size() - 2]) {
ans.push_back(x);
if (dfs(j + 1)) {
return true;
}
ans.pop_back();
}
}
return false;
};
dfs(0);
return ans;
}
};