842. Split Array into Fibonacci Sequence能否把数列返回成斐波那契数列

[抄题]:

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.

 [暴力解法]:

时间分析:

空间分析:

 [优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

不知道为什么要用dfs:其实通过dfs返回true/false也是可以的

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[一句话思路]:

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

忘了backtracing怎么写了:只有满足num = 倒数一二位求和时才进行下一步的backtracing

[二刷]:

num > 倒数一二位求和时退出,小于时还可以继续backtracing

[三刷]:

[四刷]:

[五刷]:

  [五分钟肉眼debug的结果]:

[总结]:

不知道为什么要用dfs:其实通过dfs返回true/false也是可以的

[复杂度]:Time complexity: O(n) Space complexity: O(n)

[算法思想:迭代/递归/分治/贪心]:

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

 [代码风格] :

 [是否头一次写此类driver funcion的代码] :

 [潜台词] :

class Solution {
    public List<Integer> splitIntoFibonacci(String S) {
        //initialization
        List<Integer> ans = new ArrayList<Integer>();
        //go dfs
        dfs(S, 0, ans);
        return ans;
    }
    
    public boolean dfs(String s, int index, List<Integer> ans) {
        //exit case
        if (index == s.length() && ans.size() >= 3) return true;
        
        //dfs in for loop
        for (int i = index; i < s.length(); i++) {
         //corner case: start from 0
            if (s.charAt(index) == '0' && i > index) break;
            
            long number = Long.parseLong(s.substring(index, i + 1));
            //corner case: number exceeds limit
            if (number > Integer.MAX_VALUE) break;
            
            //length > 2 then break
            int size = ans.size();
            if (ans.size() >= 2 && number > ans.get(size - 2) + ans.get(size - 1)) break;
            
            //length < 1 or true, go backtracing
            if (ans.size() <= 1 || number == ans.get(size - 2) + ans.get(size - 1)) {
                ans.add((int)number);
                
                if (dfs(s, i + 1, ans)) 
                return true;
                
                ans.remove(ans.size() - 1);
            }
        }
        return false;
    }
}
View Code
原文地址:https://www.cnblogs.com/immiao0319/p/9550718.html