题目描述
示例
代码如下:
public class SplitIntoFibonacci {
LinkedList<Integer> res = new LinkedList<>();
public List<Integer> splitIntoFibonacci(String num) {
if(num.length() < 3) return res;
if(dfs(num, 0)) return res;
return new LinkedList<>();
}
public boolean dfs(String num, int start) {
if (start == num.length() && res.size() >= 3) {
return true;
}
for (int i = start; i < num.length(); i++) {
if (num.charAt(start) == '0' && i > start) {
break;
}
String s = num.substring(start, i + 1);
long cur = Long.valueOf(s);
if (cur > Integer.MAX_VALUE) {
break;
}
int size = res.size();
if (size >= 2) {
if(cur > res.get(size - 1) + res.get(size - 2)){
break;
}
if(cur < res.get(size - 1) + res.get(size - 2)){
continue;
}
}
res.add((int) cur);
if (dfs(num, i + 1))
return true;
res.removeLast();
}
return false;
}
}