-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathSolution.java
35 lines (33 loc) · 1011 Bytes
/
Solution.java
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
class Solution {
private List<Integer> ans = new ArrayList<>();
private String num;
public List<Integer> splitIntoFibonacci(String num) {
this.num = num;
dfs(0);
return ans;
}
private boolean dfs(int i) {
if (i == num.length()) {
return ans.size() >= 3;
}
long x = 0;
for (int j = i; j < num.length(); ++j) {
if (j > i && num.charAt(i) == '0') {
break;
}
x = x * 10 + num.charAt(j) - '0';
if (x > Integer.MAX_VALUE
|| (ans.size() >= 2 && x > ans.get(ans.size() - 1) + ans.get(ans.size() - 2))) {
break;
}
if (ans.size() < 2 || x == ans.get(ans.size() - 1) + ans.get(ans.size() - 2)) {
ans.add((int) x);
if (dfs(j + 1)) {
return true;
}
ans.remove(ans.size() - 1);
}
}
return false;
}
}