原题链接在这里: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. 收藏单词TOEFL备份托福英语

    TOEFL托福词汇串讲(文本) alchemy(chem-化学)n. 炼金术 chemistry 化学 alder 赤杨树 联想:older 老人坐在赤杨树下 sloth 树懒 algae n.海藻 ...

  2. RHEL6.5 移植使用CentOS 的YUM 步骤

    问题:使用 Red Hat Enterprise Linux Server(RHEL) yum安装软件时显示 This system is not registered with RHN. RHN s ...

  3. [BZOJ5197] [CERC2017]Gambling Guide

    [BZOJ5197] [CERC2017]Gambling Guide 题目链接 https://www.lydsy.com/JudgeOnline/problem.php?id=5197 Solut ...

  4. Linux 6 本地网络配置方法

    在Linux 6 系统安装完成后,需要对网络进行一系列的配置,有些朋友喜欢使用桌面图形化工具的配置方式,这种配置方法虽说比较方便,但是在某些时候并不问题,总是会出现各种问题.特别是作为服务器用途的时候 ...

  5. C# vb .net实现倾斜效果滤镜

    在.net中,如何简单快捷地实现Photoshop滤镜组中的倾斜效果呢?答案是调用SharpImage!专业图像特效滤镜和合成类库.下面开始演示关键代码,您也可以在文末下载全部源码: 设置授权 第一步 ...

  6. linux mount一个目录到另外一个目录

      从linux内核2.4.0以后mount支持mount --bind 一个目录到另外一个目录 比如: [root@localhost wind]# mkdir test1 test2 dir3 m ...

  7. JavaScript判断是否是正确数值 isNaN

    NaN在JavaScript中表示不是数字 JavaScript中isNaN函数方法是返回一个 Boolean 值,指明提供的值是否是保留值 NaN (不是数字). 使用方法:isNaN(numVal ...

  8. 微信小程序代码开源啦

    想学习如何使用mpvue开发微信小程序吗? 想知道微信消息推送如何实现吗? 想知道如何用springboot开发小程序后台吗? 看这里就全都有了.耗时一个月打造的微信小程序:PSN折扣助手前后端所有源 ...

  9. 如何将一个react组件进行静态化调用

    ant-design的message组件可以使用message.xxx的方法调用,调用代码如下: import { message, Button } from 'antd'; const info ...

  10. 自定义hybris生成订单的ID格式

    在项目local.properties里做出如下定义: keygen.order.code.digits=8 keygen.order.code.start=00000000 keygen.order ...