[抄题]:

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;
}
}

842. Split Array into Fibonacci Sequence能否把数列返回成斐波那契数列的更多相关文章

  1. LeetCode 842. Split Array into Fibonacci Sequence

    原题链接在这里:https://leetcode.com/problems/split-array-into-fibonacci-sequence/ 题目: Given a string S of d ...

  2. 842. Split Array into Fibonacci Sequence

    Given a string S of digits, such as S = "123456579", we can split it into a Fibonacci-like ...

  3. 【LeetCode】842. Split Array into Fibonacci Sequence 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  4. 842. Split Array into Fibonacci Sequence —— weekly contest 86

    题目链接:https://leetcode.com/problems/split-array-into-fibonacci-sequence/description/ 占坑. string 的数值转换 ...

  5. [Swift]LeetCode842. 将数组拆分成斐波那契序列 | Split Array into Fibonacci Sequence

    Given a string S of digits, such as S = "123456579", we can split it into a Fibonacci-like ...

  6. 用递归方法计算斐波那契数列(Recursion Fibonacci Sequence Python)

    先科普一下什么叫斐波那契数列,以下内容摘自百度百科: 斐波那契数列(Fibonacci sequence),又称黄金分割数列.因意大利数学家列昂纳多·斐波那契(Leonardoda Fibonacci ...

  7. [LeetCode] Split Array into Fibonacci Sequence 分割数组成斐波那契序列

    Given a string S of digits, such as S = "123456579", we can split it into a Fibonacci-like ...

  8. 【每天一题ACM】 斐波那契数列(Fibonacci sequence)的实现

    最近因为一些原因需要接触一些ACM的东西,想想写个blog当作笔记吧!同时也给有需要的人一些参考 话不多说,关于斐波那契数列(Fibonacci sequence)不了解的同学可以看看百度百科之类的, ...

  9. python实现斐波那契数列(Fibonacci sequence)

    使用Python实现斐波那契数列(Fibonacci sequence) 斐波那契数列形如 1,1,2,3,5,8,13,等等.也就是说,下一个值是序列中前两个值之和.写一个函数,给定N,返回第N个斐 ...

随机推荐

  1. golang使用Nsq(转)

    为什么要使用Nsq 最近一直在寻找一个高性能,高可用的消息队列做内部服务之间的通讯.一开始想到用zeromq,但在查找资料的过程中,意外的发现了Nsq这个由golang开发的消息队列,毕竟是golan ...

  2. for里的上一个/下一个下标的安全写法

    const len:int=10; for(var:int=0;i<len;i++){ var previ:int=(i-1+len)%len; var nexti:int=(i+1)%len; ...

  3. 一个docker镜像中的目录删除不了问题

    在一个容器中,删除一个目录,失败: bash-4.2# pwd /home/zxcdn/ottcache/tomcat bash-4.2# uname -a Linux 3516b6c97679 -. ...

  4. Spring/SpringBoot定义统一异常错误码返回

    配置 大致说下流程, 首先我们自定义一个自己的异常类CustomException,继承RuntimeException.再写一个异常管理类ExceptionManager,用来抛出自定义的异常. 然 ...

  5. React/anu实现弹出层2

    这次是使用了一个比较罕见的APIReactDOM.unstable_renderSubtreeIntoContainer,ReactDOM.unstable_renderSubtreeIntoCont ...

  6. react设置innerHTML

    <!DOCTYPE html> <html> <head> <meta content="text/html; charset=utf-8" ...

  7. app与jvm 反向代理时config的设置(用于在web页面显示npm(就如tomcat)产生的页面)

    dev: { // Various Dev Server settings contentBase: ROOT, host: ip, port: 8084,    //此端口为任意设置,不重复即可,为 ...

  8. Uni2D 入门 -- Skeletal Animation

    转载 csdn http://blog.csdn.net/kakashi8841/article/details/17615195 Skeletal Animation Uni2D V2.0 引进了一 ...

  9. Java学习04 (第一遍)

    封装.抽象.继承和多态.封装:在面向对象语言中,封装特性是由类来体现的,我们将现实生活中的一类实体定义成类,其中包括属性和行为(在Java中就是方法),就好像人类,可以具有name,sex,age等属 ...

  10. javascript中所有函数的参数都是按值传递的

    [javascript中所有函数的参数都是按值传递的] 参考:http://www.jb51.net/article/89297.htm