[抄题]:

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. java ftp主动模式与被动模式

    首先介绍一下主动模式与被动模式: 1.PORT(主动模式) ftpClient.enterLocalActiveMode(); PORT中文为主动模式,工作的原理:FTP客户端连接到FTP服务器的21 ...

  2. js 滑动门的实现

    原理:滑动门,这里以图片进行实例,首先设定主盒子div的宽度和高度设定,并进行图片初始化位置的设定,然后将图片绑定事件,并设定要达到的效果 html代码: <!DOCTYPE html> ...

  3. linux 3.10 gro的理解和改进

    gro,将同一个flow的一定时间范围之内的skb进行合并,减少协议栈的消耗,用于收包性能提升.gro网上的资料很多,但是都很少谈到gro的改进,刚好身边有个同事也想改这块的内容, 所以将最近看的gr ...

  4. IPSec协议;IPv6为何增加对IPSec协议的支持

      IPSec由一系列的协议组成,除IP层的协议完全结构外,还包括了AH.ESP.ISAKMP.ISAKMP的因特网IP安全解释域.IKE.OAKLEY密钥协议确定等.ESP和AH定义协议.载荷头的格 ...

  5. 【原创】逆向练习(CrackMe)

    Write Up 登录的两个方法 方法1.用IDA分析 新版测试题.exe.在Strings Window中查找有一定意义的串. 从上面的窗口中,发现了CyberSwat和passwordisme这两 ...

  6. delphi控制本计算机和远程计算机关机等

    unit mainunit; {远程关机源码} interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Con ...

  7. 关于在VS2008和VS2010中禁用及卸载Visual Assist X的方法研究——转载

    禁用和启用   此方法对于VS2008和VS2010 都适用. 在VS2008或VS2010菜单栏中选择“VassistX”选项卡,找到“Enable/Disable Visual Assist X” ...

  8. float double

    float : 单精度浮点数 double : 双精度浮点数 两者的主要区别如下: 01.在内存中占有的字节数不同 单精度浮点数在机内存占4个字节 双精度浮点数在机内存占8个字节 02.有效数字位数不 ...

  9. python 删除模块

    import systry:    import librabbitmqexcept Exception:    passelse:    version = getattr(librabbitmq, ...

  10. flume 实际的使用

    1.No configuration found for this host:seqGenSrc bin/flume-ng agent --conf conf --conf-file conf/flu ...