原题链接在这里: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. HLP帮助文件源文件RTF文件的编写

    https://www.cnblogs.com/gaodu2003/archive/2008/12/17/1356861.html 举例说明如下: 每一节的标题在RTF文件中一般以特有的脚注($)指定 ...

  2. 【数据结构】11.java源码关于TreeMap

    目录 1.TreehMap的内部结构 2.TreehMap构造函数 3.元素新增策略 4.元素删除 5.元素修改和查找 6.特殊操作 7.扩容 8.总结 1.TreeMap的内部结构 首先确认一点,t ...

  3. nginx+lua访问流量实时上报kafka

    在nginx这一层,接收到访问请求的时候,就把请求的流量上报发送给kafka storm才能去消费kafka中的实时的访问日志,然后去进行缓存热数据的统计 从lua脚本直接创建一个kafka prod ...

  4. java转换编码报错java.lang.IllegalArgumentException: URLDecoder: Illegal hex characters in escape (%) pattern

    Exception in thread "main" java.lang.IllegalArgumentException: URLDecoder: Illegal hex cha ...

  5. WPF 获取元素(Visual)相对于屏幕设备的缩放比例,可用于清晰显示图片

    原文:WPF 获取元素(Visual)相对于屏幕设备的缩放比例,可用于清晰显示图片 我们知道,在 WPF 中的坐标单位不是屏幕像素单位,所以如果需要知道某个控件的像素尺寸,以便做一些与屏幕像素尺寸相关 ...

  6. Object类的toString()和equals()方法

    我们知道,Object类是所有类的父类,因此也被称为根类.祖先.那么,我们就来看一看Object类的最常用的两个方法是如何用的. 1.toString方法: Object类的toString()方法默 ...

  7. 2019 乐逗游戏java面试笔试题 (含面试题解析)

      本人5年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.乐逗游戏等公司offer,岗位是Java后端开发,因为发展原因最终选择去了乐逗游戏,入职一年时间了,也成为了面 ...

  8. Spring Security 解析(一) —— 授权过程

    Spring Security 解析(一) -- 授权过程   在学习Spring Cloud 时,遇到了授权服务oauth 相关内容时,总是一知半解,因此决定先把Spring Security .S ...

  9. mysql-数据库查询语句汇总

    目录 数据库查询语句 ***** 添加数据补充: 所有的select 关键字 where 条件 distinct 去除重复记录 指定字段 取别名 group by having order limit ...

  10. float与position间的区别

    float与position间的区别:    个人理解为:脱离文档流不一定脱离文本流:但脱离文本流,则也脱离文档流.[如有更好的理解还望评论区一起探讨,共同学习进步]一.float 浮动(脱离文档流, ...