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个斐 ...
随机推荐
- APP-12-视觉技术-身份证识别
1.Postman测试 图片转换为Base64:http://imgbase64.duoshitong.com/ Base64: Base64数据去掉表头文件:data:image/png;base6 ...
- Xcode 8 注释快捷键失效
sudo /usr/libexec/xpccachectl 重启
- FireDac 组件说明二
FDUpdateSQL 生成添加,删除,修改SQL语句 TFDMetaInfoQuery 查询数据源信息 TFDEventAlerter 负责处理数据库事件通知 使用TFDEventAlerter类来 ...
- git 配置提交过滤文件
1)在Git项目中定义.gitignore文件 2)在Git项目的设置中指定排除文件 3)定义Git全局的 .gitignore 文件
- vue .map 文件
参数: productionSourceMap:false 这个改为false.去掉打包产生的map文件 map文件的作用:定位线上错误代码位置;
- CSS VISUAL RULES
CSS VISUAL RULES Review Visual Rules Incredible work! You used CSS to alter text and images througho ...
- linux 时间相关的一些总结
仅作为内核代码中时间管理模块的笔记,3.10内核,很乱,不喜勿喷. 先有time,后有timer. 常用的time结构有哪些?除了大名鼎鼎的jiffies和jiffies64之外,还有常用的一些结构如 ...
- JAVAWEB 一一 userweb2(升级,servlet版,jstl和el)
创建数据库和表 首先,创建一个web项目 然后引入jar包(jstl.jar和standard.jar是jstl和el包,在jsp页面中需要手动加 <%@ taglib uri="ht ...
- selenium 浏览器常用设置和部署
一,chrome浏览器设置 from selenium import webdriver # 浏览器选项 chrome_options = webdriver.ChromeOptions() # 使用 ...
- 网络传输中利用fastjson将复杂嵌套数据类型Json格式转换(GeoJsonPolygon)
如果一个对象太复杂了,那么在网络传输键的JSON格式数据转换容易出问题. 比如下面一个类Area.java import lombok.AllArgsConstructor; import lombo ...