842. Split Array into Fibonacci Sequence Medium
1/**
2 * [842] Split Array into Fibonacci Sequence
3 *
4 * You are given a string of digits num, such as "123456579". We can split it into a Fibonacci-like sequence [123, 456, 579].
5 * Formally, a Fibonacci-like sequence is a list f of non-negative integers such that:
6 *
7 * 0 <= f[i] < 2^31, (that is, each integer fits in a 32-bit signed integer type),
8 * f.length >= 3, and
9 * f[i] + f[i + 1] == f[i + 2] for all 0 <= i < f.length - 2.
10 *
11 * 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.
12 * Return any Fibonacci-like sequence split from num, or return [] if it cannot be done.
13 *
14 * Example 1:
15 *
16 * Input: num = "1101111"
17 * Output: [11,0,11,11]
18 * Explanation: The output [110, 1, 111] would also be accepted.
19 *
20 * Example 2:
21 *
22 * Input: num = "112358130"
23 * Output: []
24 * Explanation: The task is impossible.
25 *
26 * Example 3:
27 *
28 * Input: num = "0123"
29 * Output: []
30 * Explanation: Leading zeroes are not allowed, so "01", "2", "3" is not valid.
31 *
32 *
33 * Constraints:
34 *
35 * 1 <= num.length <= 200
36 * num contains only digits.
37 *
38 */
39pub struct Solution {}
40
41// problem: https://leetcode.com/problems/split-array-into-fibonacci-sequence/
42// discuss: https://leetcode.com/problems/split-array-into-fibonacci-sequence/discuss/?currentPage=1&orderBy=most_votes&query=
43
44// submission codes start here
45
46impl Solution {
47 pub fn split_into_fibonacci(num: String) -> Vec<i32> {
48 vec![]
49 }
50}
51
52// submission codes end
53
54#[cfg(test)]
55mod tests {
56 use super::*;
57
58 #[test]
59 fn test_842() {
60 }
61}
62
Back
© 2025 bowen.ge All Rights Reserved.