原题链接在这里:https://leetcode.com/problems/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.

Note:

  1. 1 <= S.length <= 200
  2. S contains only digits.

题解:

The quesiton is asking for any sequence, not all of them.

Thus DFS state needs s, current starting index, current list item. DFS returns if s could be cut into Fibonacci sequence.

If any DFS comes to true, then just return current list, there is no need to do further more calculation.

For i >= start index, get the candidate from starting index, if it exceeds Integer, return false.

If current list item already has >= 2 items, but candidate != last 2 values sum, return false.

If current list item has 0 or 1 item, or has >= 2 items, but candidate == last 2 values sum, add candidate to res and continue DFS.

When gettting candidate, starting index could point to '0', '0' as candidate is fine, but '01' is not.

Thus here it needs to check when i > start && s.charAt(start) == '0', return false.

Time Complexity: O(expontenial).

Space: O(n). n = s.length(). stack space.

AC Java:

 class Solution {
public List<Integer> splitIntoFibonacci(String S) {
List<Integer> res = new ArrayList<>();
if(S == null || S.length() == 0){
return res;
} dfs(S, 0, res);
return res;
} private boolean dfs(String s, int start, List<Integer> res){
if(start == s.length() && res.size() > 2){
return true;
} for(int i = start; i<s.length(); i++){
if(s.charAt(start) == '0' && i > start){
return false;
} long candidate = Long.valueOf(s.substring(start, i+1));
if(candidate > Integer.MAX_VALUE){
return false;
} int size = res.size();
if(size >= 2 && candidate > res.get(size-1)+res.get(size-2)){
return false;
} if(size < 2 || candidate == res.get(size-1)+res.get(size-2)){
res.add((int)candidate);
if(dfs(s, i+1, res)){
return true;
} res.remove(res.size()-1);
}
} return false;
}
}

类似Additive NumberFibonacci Number.

LeetCode 842. Split Array into Fibonacci Sequence的更多相关文章

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

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

  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. 842. Split Array into Fibonacci Sequence能否把数列返回成斐波那契数列

    [抄题]: Given a string S of digits, such as S = "123456579", we can split it into a Fibonacc ...

  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. [LeetCode] Split Array into Fibonacci Sequence 分割数组成斐波那契序列

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

  7. [LeetCode] 659. Split Array into Consecutive Subsequences 将数组分割成连续子序列

    You are given an integer array sorted in ascending order (may contain duplicates), you need to split ...

  8. [LeetCode] 410. Split Array Largest Sum 分割数组的最大值

    Given an array which consists of non-negative integers and an integer m, you can split the array int ...

  9. [LeetCode] 548. Split Array with Equal Sum 分割数组成和相同的子数组

    Given an array with n integers, you need to find if there are triplets (i, j, k) which satisfies fol ...

随机推荐

  1. easyui中formatter的用法

    easyui中formatter的用法 当我们使用easyui需要对某一列进行格式化处理value数据时,可以使用formatter进行格式化 这里以一个商品表举例,商品表中有一个商品类型的字段,数据 ...

  2. java中通过Adb判断PC是否连接了移动设备

    最近用到PC端和移动端通过USB连接传输数据的方式,于是总在使用Adb命令,为了逻辑的严谨和代码容错,想在传输数据的之前,PC和移动端先建立一次会话,防止移动端还未连接就直接传输数据会报错,找了很久并 ...

  3. 我瞅瞅源码系列之---flask

     快速使用  通过werkzurg 了解wsgi  threading.local和高级  LocalStack和Local对象实现栈的管理  Flask源码之:配置加载  Flask源码之:路由加载 ...

  4. 【转】Isim——基本技巧

    来源:电子产品世界: 注:本文由NingHeChuan本人多出整理所得,原文章图片不清晰,自己整理配图后重新发表 安装好ISE,系统已经自带了ISim仿真软件,相比于专业的仿真软件Modelsim,I ...

  5. C# 历遍对象属性

    今天有个网友问如何历遍对象的所有公共属性,并且生成XML.采用序列化方式的话比较简单,我写个手工解析的例子,这样能让初学者更加理解也比较灵活,记录一下吧或许会有人用到. 对象模型: public cl ...

  6. postgresql学习之安装篇

    ---恢复内容开始--- 安装方法: 1.可以使用操作系统自带的安装源 2.可以使用官网下载的源码进行安装 3.可以使用编译好的包入.run格式的安装包安装(本文使用的是这种安装方法,下载地址http ...

  7. wind安装Jenkins+sonar+jdk

    最近公司在用Jenkins持续集成软件,自己研究的头痛,而且还是和C#项目融合到一起的,网上看到的都是Java的,我自己配了一套和C#的,和你们分享. Jenkins是一个开源软件项目,旨在提供一个开 ...

  8. MyBatis和spring整合简单实现

    spring和MyBatis整合: 导入spring和MyBatis的整合jar包,以及其依赖jar包: 导入MyBatis和spring的整合jar包. spring的核心jar包. 定义Mybat ...

  9. Spring 实例化Bean的3种方式

    要使用Spring中的Bean,需要先创建这个Bean的实例. 实例化Bean有3种方式: 构造器方式 静态工厂方式 实例工厂方式 构造器方式 构造器方式是最常用的.在Bean中写构造函数,然后在配置 ...

  10. Python爬虫系列:五、正则表达式

    1.了解正则表达式 正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的一些特定字符.及这些特定字符的组合,组成一个“规则字符串”,这个“规则字符串”用来表达对字符串的一种过滤逻辑. 正则表达式 ...