-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path842.split-array-into-fibonacci-sequence.java
More file actions
134 lines (123 loc) · 2.98 KB
/
Copy path842.split-array-into-fibonacci-sequence.java
File metadata and controls
134 lines (123 loc) · 2.98 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
* @lc app=leetcode id=842 lang=java
*
* [842] Split Array into Fibonacci Sequence
*
* https://leetcode.com/problems/split-array-into-fibonacci-sequence/description/
*
* algorithms
* Medium (35.19%)
* Total Accepted: 22.1K
* Total Submissions: 60.6K
* Testcase Example: '"123456579"'
*
* Given a string S of digits, such as S = "123456579", we can split it into a
* Fibonacci-like sequence [123, 456, 579].
*
* Formally, a Fibonacci-like sequence is a list F of non-negative integers
* such that:
*
*
* 0 <= F[i] <= 2^31 - 1, (that is, each integer fits a 32-bit signed integer
* type);
* F.length >= 3;
* and F[i] + F[i+1] = F[i+2] for all 0 <= i < F.length - 2.
*
*
* Also, note that when splitting the string into pieces, each piece must not
* have extra leading zeroes, except if the piece is the number 0 itself.
*
* Return any Fibonacci-like sequence split from S, or return [] if it cannot
* be done.
*
* Example 1:
*
*
* Input: "123456579"
* Output: [123,456,579]
*
*
* Example 2:
*
*
* Input: "11235813"
* Output: [1,1,2,3,5,8,13]
*
*
* Example 3:
*
*
* Input: "112358130"
* Output: []
* Explanation: The task is impossible.
*
*
* Example 4:
*
*
* Input: "0123"
* Output: []
* Explanation: Leading zeroes are not allowed, so "01", "2", "3" is not
* valid.
*
*
* Example 5:
*
*
* Input: "1101111"
* Output: [110, 1, 111]
* Explanation: The output [11, 0, 11, 11] would also be accepted.
*
*
* Note:
*
*
* 1 <= S.length <= 200
* S contains only digits.
*
*
*/
class Solution {
public List<Integer> splitIntoFibonacci(String S) {
List<Integer> res = new ArrayList<>();
for (int i = 1; i < S.length(); i++) {
for (int j = 1; i + j < S.length(); j++) {
String n1 = S.substring(0, i);
String n2 = S.substring(i, i + j);
if (!validate(n1) || !validate(n2)) {
break;
}
res.add(Integer.valueOf(n1));
res.add(Integer.valueOf(n2));
if (helper(Integer.valueOf(n1), Integer.valueOf(n2), S, i + j, res)) {
return res;
}
res.clear();
}
}
return res;
}
private boolean helper(int n1, int n2, String s, int index, List<Integer> l) {
if (index >= s.length()) {
return true;
}
int sum = n1 + n2;
if (!s.substring(index).startsWith(Integer.toString(sum))) {
return false;
}
l.add(sum);
if (!helper(n2, sum, s, Integer.toString(sum).length() + index, l)) {
l.remove(l.size() - 1);
return false;
}
return true;
}
private boolean validate(String s) {
try {
Integer.valueOf(s);
return !s.startsWith("0") || "0".equals(s);
} catch (NumberFormatException e) {
return false;
}
}
}