842. 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.
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
不知道为什么要用dfs:其实通过dfs返回true/false也是可以的
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[一句话思路]:
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
忘了backtracing怎么写了:只有满足num = 倒数一二位求和时才进行下一步的backtracing
[二刷]:
num > 倒数一二位求和时退出,小于时还可以继续backtracing
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
不知道为什么要用dfs:其实通过dfs返回true/false也是可以的
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[算法思想:迭代/递归/分治/贪心]:
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
[是否头一次写此类driver funcion的代码] :
[潜台词] :
class Solution {
public List<Integer> splitIntoFibonacci(String S) {
//initialization
List<Integer> ans = new ArrayList<Integer>();
//go dfs
dfs(S, 0, ans);
return ans;
} public boolean dfs(String s, int index, List<Integer> ans) {
//exit case
if (index == s.length() && ans.size() >= 3) return true; //dfs in for loop
for (int i = index; i < s.length(); i++) {
//corner case: start from 0
if (s.charAt(index) == '0' && i > index) break; long number = Long.parseLong(s.substring(index, i + 1));
//corner case: number exceeds limit
if (number > Integer.MAX_VALUE) break; //length > 2 then break
int size = ans.size();
if (ans.size() >= 2 && number > ans.get(size - 2) + ans.get(size - 1)) break; //length < 1 or true, go backtracing
if (ans.size() <= 1 || number == ans.get(size - 2) + ans.get(size - 1)) {
ans.add((int)number); if (dfs(s, i + 1, ans))
return true; ans.remove(ans.size() - 1);
}
}
return false;
}
}
842. Split Array into Fibonacci Sequence能否把数列返回成斐波那契数列的更多相关文章
- LeetCode 842. Split Array into Fibonacci Sequence
原题链接在这里:https://leetcode.com/problems/split-array-into-fibonacci-sequence/ 题目: Given a string S of d ...
- 842. Split Array into Fibonacci Sequence
Given a string S of digits, such as S = "123456579", we can split it into a Fibonacci-like ...
- 【LeetCode】842. Split Array into Fibonacci Sequence 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 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 ...
- 用递归方法计算斐波那契数列(Recursion Fibonacci Sequence Python)
先科普一下什么叫斐波那契数列,以下内容摘自百度百科: 斐波那契数列(Fibonacci sequence),又称黄金分割数列.因意大利数学家列昂纳多·斐波那契(Leonardoda Fibonacci ...
- [LeetCode] Split Array into Fibonacci Sequence 分割数组成斐波那契序列
Given a string S of digits, such as S = "123456579", we can split it into a Fibonacci-like ...
- 【每天一题ACM】 斐波那契数列(Fibonacci sequence)的实现
最近因为一些原因需要接触一些ACM的东西,想想写个blog当作笔记吧!同时也给有需要的人一些参考 话不多说,关于斐波那契数列(Fibonacci sequence)不了解的同学可以看看百度百科之类的, ...
- python实现斐波那契数列(Fibonacci sequence)
使用Python实现斐波那契数列(Fibonacci sequence) 斐波那契数列形如 1,1,2,3,5,8,13,等等.也就是说,下一个值是序列中前两个值之和.写一个函数,给定N,返回第N个斐 ...
随机推荐
- 递归获取包下的class文件
```java(这个居然隐藏不了) public class TestUrl { public static void main(String[] args) { String pageName = ...
- [JS]如何理解JS中的类和对象
-------------------------------------------------------------------------------------------- 变量:自由的 ...
- 解决eclipse+adt出现的 loading data for android 问题
因为公司最近做的项目中有用到一些第三方demo,蛋疼的是这些demo还比较旧...eclipse的... 于是给自己的eclipse装上了ADT插件,但是...因为我的eclipse比较新,Versi ...
- Kubernetes1.9 二进制版集群+ipvs+coredns
节点构造如下 : 节点ip 节点角色 hostname 192.168.0.57 node bigdata3 192.168.0.56 node bigdata4 192.16 ...
- hdu1042-N!-(java大数)
题目:求n!(0<=n<=10000) import java.math.BigInteger;//操作大整数 import java.math.BigDecimal;//操作大小数 im ...
- html+js自定义颜色选择器
<!DOCTYPE html><html><head> <meta charset="utf-8"> <title>te ...
- 关于H5唤醒APP的功能实现(千辛万苦啊!)
首先,我是个后端,写java的,甚至不是搞移动端的,所以js这方面有点底子但不专业,对于出现的错误也请见谅,原来项目要求有个H5页面打开APP的功能就强行要做,没办法就想办法搞一下,网上的教程基本都是 ...
- GraphicsTier
[GraphicsTier] 1.enum GraphicsTier 2.enum ShaderQuality 3.enum BuildTargetGroup 4.EditorGraphicsSett ...
- c/c++ 中的重要函数
1,strtod: 函数原型: #include <cstdlib> double strtod(const char *nptr, char **endptr); strtod 原型 名 ...
- leetcode 数组类型题
// ConsoleApplication1.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <Windows.h& ...