LeetCode 842. Split Array into Fibonacci Sequence
原题链接在这里: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 all0 <= 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 <= S.length <= 200Scontains 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 Number, Fibonacci Number.
LeetCode 842. Split Array into Fibonacci Sequence的更多相关文章
- 【LeetCode】842. Split Array into Fibonacci Sequence 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 842. Split Array into Fibonacci Sequence
Given a string S of digits, such as S = "123456579", we can split it into a Fibonacci-like ...
- 842. Split Array into Fibonacci Sequence能否把数列返回成斐波那契数列
[抄题]: Given a string S of digits, such as S = "123456579", we can split it into a Fibonacc ...
- 842. Split Array into Fibonacci Sequence —— weekly contest 86
题目链接:https://leetcode.com/problems/split-array-into-fibonacci-sequence/description/ 占坑. string 的数值转换 ...
- [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 ...
- [LeetCode] Split Array into Fibonacci Sequence 分割数组成斐波那契序列
Given a string S of digits, such as S = "123456579", we can split it into a Fibonacci-like ...
- [LeetCode] 659. Split Array into Consecutive Subsequences 将数组分割成连续子序列
You are given an integer array sorted in ascending order (may contain duplicates), you need to split ...
- [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 ...
- [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 ...
随机推荐
- UV数据与风速风向数据转换
package com.qr.util; import java.text.DecimalFormat; /** * //TODO UV数据与风速风向数据转换 */ public class UVAn ...
- 关于Django数据库mysql连接错误问题Connection to api@localhost failed. [08001] Could not create connection to d
Connection to api@localhost failed. [08001] Could not create connection to d 错误类型 django连接mysql数据库错误 ...
- VMware Workstation 15 Player使用centos页面版本如何查看ip
首先运行要使用的centos镜像,输入密码登陆进去 因为是界面版,所以就不需要再镜像中输入命令,但是因为这样又找不到没法用ifconfig查看ip怎么办? 这个就是类似于一个系统页面版本的linux ...
- Windows 编译安装 nginx 服务器 + rtmp 模块
有关博客: <Windows 编译安装 nginx 服务器 + rtmp 模块>.<Ubuntu 编译安装 nginx>.<Arm-Linux 移植 Nginx> ...
- Glances - Linux上的实时系统监控工具(Centos安装)
Glances WebServer 模式 在 glances 的 WebServer 模式下,客户端只通过浏览器访问就可以获取远程服务器的运行状态. 安装成功后,使用 glances -w 命令即可 ...
- K8S 中的容器编排和应用编排
众所周知,Kubernetes 是一个容器编排平台,它有非常丰富的原始的 API 来支持容器编排,但是对于用户来说更加关心的是一个应用的编排,包含多容器和服务的组合,管理它们之间的依赖关系,以及如何管 ...
- spring加载多个配置文件如何配置
为应用指定多个配置文件: 多个配置文件的关系: 并列 包含 并列关系 即有多个配置文件,需要同时加载这多个配置文件: 可以使用可变参数,数组和统配符进行加载: 可变参数 String config1 ...
- 科普帖:Linux操作系统
使用计算机必然会接触操作系统,现代操作系统已经发展的十分成熟,一般用户都可以很轻松的使用计算机.然而,对于要利用计算机进行专业开发和应用的用户来说,需要更加深入地理解操作系统的原理和运行机制,这样才能 ...
- Flutter Platform Channels
Flutter Platform Channels(一) https://www.jianshu.com/p/33ac774f99b1 https://www.jianshu.com/p/c1e206 ...
- java反射 详解!!!!
java反射(特别通俗易懂) 反射是框架设计的灵魂 (使用的前提条件:必须先得到代表的字节码的Class,Class类用于表示.class文件(字节码)) 一.反射的概述 JAVA反射机制是在运行状态 ...