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 ...
随机推荐
- MySQL视图、触发器、事务、存储过程、内置函数、流程控制、索引
一.视图 1.什么是视图 视图就是通过查询得到一张虚拟表,然后保存下来,下次直接使用即可 2.为什么要用视图 如果频繁使用一张虚拟表,可以不用重复查询 3.如何使用视图 create view tea ...
- flask db操作
from flask import Flask from flask_sqlalchemy import SQLAlchemy app = Flask(__name__) # app.config[' ...
- Html 对象的常用事件列举
事件名称 触发时间 对象例举 OnBlur 对象失去输入焦点 窗口和所有的表单对象 OnChange 用户改变对象的值 文本框.文本区域.选择列表等 OnClick 用户鼠标点击 链接.按钮.单选钮. ...
- 全栈项目|小书架|服务器端-NodeJS+Koa2 实现评论功能
评论功能分析 上图可以看出评论功能主要实现了:评论的发布.评论列表的展示. 在不考虑子评论以及图片评论的场景下,评论功能主要有以下两个接口: 发布评论 获取评论列表(考虑分页) 评论 Model 的建 ...
- 无法定位 Local Database Runtime 安装。请验证 SQL Server Express 是否正确安装以及本地数据库运行时功能是否已启用。
错误描述: 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误.未找到或无法访问服务器.请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接. (provide ...
- swagger list Could not resolve reference because of: Could not resolve point
swagger list Could not resolve reference because of: Could not resolve point controller的参数要加 @Requ ...
- MongoDB和Java(4):Spring Data整合MongoDB(XML配置)
最近花了一些时间学习了下MongoDB数据库,感觉还是比较全面系统的,涉及了软件安装.客户端操作.安全认证.副本集和分布式集群搭建,以及使用Spring Data连接MongoDB进行数据操作,收获很 ...
- iOS之集成GoogleMap定位、搜索注意事项
简介: 最近花了些时间看了GoogleMap官方文件并集成到国际版app中,网上关于GoogleMap for iOS的讲解相对Android来说少一点,比较有帮助的几乎全是英文文档.下面是我开发过程 ...
- 【开发工具】-解决Myeclipse 的 Server窗口报空指针错误
Eclipse 或者 MyEclipse 查看 server面板的时候,报错,如图所示,错误 代码:java.lang.NullPointerException .另外,由于此错误,导致 项目不能够 ...
- sublime中Vue高亮插件安装
1.准备语法高亮插件vue-syntax-highlight. 下载地址: https://github.com/vuejs/vue-syntax-highlight 下载页面并下载: 解开压缩包vu ...