-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSplitArrayIntoFibonacciSequences.java
39 lines (38 loc) · 1.23 KB
/
SplitArrayIntoFibonacciSequences.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
36
37
38
39
/*https://leetcode.com/problems/split-array-into-fibonacci-sequence/*/
class Solution {
List<Integer> list;
public List<Integer> splitIntoFibonacci(String num) {
list = new ArrayList<Integer>();
solve(num,0,-1,-1,new ArrayList<Integer>());
return list;
}
private boolean solve(String num, int index, int prev, int secondPrev, List<Integer> currList)
{
if (index == num.length())
{
if (currList.size() >= 3)
{
list = new ArrayList<Integer>(currList);
return true;
}
else return false;
}
int curr = 0;
for (int i = index; i < num.length(); ++i)
{
if (i == index+1 && num.charAt(index) == '0') break;
if (prev == -1 || secondPrev == -1)
{
if (i >= index+10) break;
}
curr = curr*10+num.charAt(i)-'0';
if (prev == -1 || secondPrev == -1 || (curr == prev+secondPrev && curr >= 0))
{
currList.add(curr);
if (solve(num,i+1,curr,prev,currList)) return true;
currList.remove(currList.size()-1);
}
}
return false;
}
}