[抄题]:

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. self, super理解

    self是方法参数列表中的第一个参数,是运行时决定的. super是编译器符号,是编译时决定的.super的含义为从父类开始寻找相应的方法,父类在编译的时候就已经决定了. 一个关键点:super并不代 ...

  2. pod install vs pod update

    Podfile文件,Podfile.lock文件 Podfile文件:指定依赖库的版本规则 Podfile.lock文件:记录当前工程中使用的依赖库的版本号 pod install会去安装podfil ...

  3. python字符串查找

    a = "string test" a.index("g") a.find("g")

  4. React之使用Context跨组件树传递数据

    ---------------------------------  讲解一 原文:https://blog.csdn.net/xuxiaoping1989/article/details/78480 ...

  5. ARP工作过程、ARP欺骗的原理和现象、如何防范ARP欺骗

      地址解析协议(Address Resolution Protocol,ARP)是在仅知道主机的IP地址时确定其物理地址的一种协议. 下面假设在一个局域网内,主机A要向主机B发送IP数据报. ARP ...

  6. 【原】wow64 x86/x64 代码切换过程分析

    下面以ntdll32!ZwQueryInformationProcess API为例分析 x86代码与x64代码之间的切换过程, 32bit的test程序: step1: ntdll32!ZwQuer ...

  7. Hibernate学习笔记1.2(Annotation版本的Helloworld)

    hibernate 3.0之后开始支持Annotation 接着1.1的项目 首先 需要创建model Teacher.java. package com.hw.hibernate.model; pu ...

  8. GIS案例学习笔记-明暗等高线提取地理模型构建

    GIS案例学习笔记-明暗等高线提取地理模型构建 联系方式:谢老师,135-4855-4328,xiexiaokui#qq.com 目的:针对数字高程模型,通过地形分析,建立明暗等高线提取模型,生成具有 ...

  9. linux最大允许的文件描述符open files数nofile修改

    open file resource limit 是linux中process可以打开的文件句柄数量.增加这个数值需要调整两个配置: 第一步, 修改系统最大允许的文件描述符 查看当前的设置: $ ca ...

  10. android有关生命周期探讨

    android生命周期直接贴一个经典图: 1.activity生命周期三段式,(开三步,跑,关三步,这三步都是回掉函数哦) 开 onCreate->onStart->onResume 跑 ...