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 ...
随机推荐
- Python之路【第十六篇】:Python并发编程|进程、线程
一.进程和线程 进程 假如有两个程序A和B,程序A在执行到一半的过程中,需要读取大量的数据输入(I/O操作), 而此时CPU只能静静地等待任务A读取完数据才能继续执行,这样就白白浪费了CPU资源. 是 ...
- for循环与if条件语句的复习运用
鉴于前面学了不少基础了,今天没有学新的内容.boyfriend给我出了几道简单的题目,慢慢的进步中. 1.# 计算1-100之间所有偶数的和 def sum(): sumone = 0 for i i ...
- golang ---cron
package main import ( l4g "github.com/alecthomas/log4go" "github.com/robfig/cron" ...
- Oracle解决锁表语句与批量生成解锁语句
--以下几个为相关表SELECT * FROM v$lock;SELECT * FROM v$sqlarea;SELECT * FROM v$session;SELECT * FROM v$proce ...
- Redis Cluster: (error) MOVED
I have a Redis cluster with the following nodes: 192.168.0.14:6379 master (slots from 0 to 16383) ...
- Golang slice和map的申明和初始化
1 前言 仅供记录使用. 2 代码 /** * @Author: FB * @Description: * @File: SliceMapInit.go * @Version: 1.0.0 * @Da ...
- python爬虫-有道翻译-js加密破解
有道翻译-js加密破解 这是本地爬取的网址:http://fanyi.youdao.com/ 一.分析请求 我们在页面中输入:水果,翻译后的英文就是:fruit.请求携带的参数有很多,先将参数数据保存 ...
- Softmax学习笔记
softmax用于多分类过程中,它将多个神经元的输出,映射到(0,1)区间内,可以看成概率来理解,从而来进行多分类! 他把一些输入映射为0-1之间的实数,并且归一化保证和为1,因此多分类的概率之和也刚 ...
- 重要知识点angularjs $http.get 和 $http.post 传递参数(!!!格式不一样!!!!)
$http.get请求数据的格式 $http.get(URL,{ params: { "id":id } }) .success(function(response, status ...
- fileinput 配置项大全,从源码中翻出了很多属性,没那么多时间一一验证,特发出来给大家参考参考
fileinput 配置项大全,从源码中翻出了很多属性,没那么多时间一一验证,特发出来给大家参考参考 fileinput 配置项大全 option 属性名 属性类型 描述说明 默认值 language ...