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.
Note:
1 <= S.length <= 200
S
contains only digits.
Approach #1: C++.
class Solution {
public:
vector<int> splitIntoFibonacci(string S) {
vector<int> nums;
helper(S, nums, 0);
return nums;
} bool helper(string S, vector<int>& nums, int start) {
int len = S.length();
// if we reached end of string & we have more than 2 elements
// in our sequence then return true
if (start >= len && nums.size() >= 3) return true;
// since '0' in beginning is not allowed therefore if the current char is '0'
// then we can use it alone only and cann't extend it by adding more chars at the back.
// otherwise we make take upto 10 chars (length og MAX_INT)
int maxLen = (S[start] == '0') ? 1 : 10; // Try getting a solution by forming a number with 'i' chars begging with 'start'
for (int i = 1; i <= maxLen && start + i <= S.size(); ++i) {
long long temp = stoll(S.substr(start, i));
if (temp > INT_MAX) return false;
int sz = nums.size();
// If fibonacci property is not satisfied then we cann't get a solution
if (sz >= 2 && nums[sz-1] + nums[sz-2] < temp) return false;
if (sz <= 1 || nums[sz-1] + nums[sz-2] == temp) {
nums.push_back(temp);
if (helper(S, nums, start+i))
return true;
nums.pop_back();
}
}
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 Fibonacc ...
- 【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 ...
- [LeetCode] Split Array into Fibonacci Sequence 分割数组成斐波那契序列
Given a string S of digits, such as S = "123456579", we can split it into a Fibonacci-like ...
- Split Array into Consecutive Subsequences
659. Split Array into Consecutive Subsequences You are given an integer array sorted in ascending or ...
- 【每天一题ACM】 斐波那契数列(Fibonacci sequence)的实现
最近因为一些原因需要接触一些ACM的东西,想想写个blog当作笔记吧!同时也给有需要的人一些参考 话不多说,关于斐波那契数列(Fibonacci sequence)不了解的同学可以看看百度百科之类的, ...
- ***1133. Fibonacci Sequence(斐波那契数列,二分,数论)
1133. Fibonacci Sequence Time limit: 1.0 secondMemory limit: 64 MB is an infinite sequence of intege ...
随机推荐
- Linux学习笔记 -- 初识 Shell
Shell 是什么 Shell 是一个用C语言编写的程序,它是用户使用Linux的桥梁.Shell 是指一种应用程序,这个应用程序提供了一个界面,用户通过这个界面访问操作系统内核的服务.Shell既是 ...
- Tornado 高并发源码分析之四--- HTTPServer 与 TCPServer 对象
主要工作: 服务器启动的时候做的事: 1.把包含了各种配置信息的 application 对象封装到了 HttpServer 对象的 request_callback 字段中,等待被调用 2.TCPS ...
- ffmpeg相关时间概念
v_rescale_q用于计算Packet的PTS.av_rescale_q的返回值是一个很大的整数,且每次计算的结果间隔很大. 不同于avcodec_encode_video改变AVCodecCon ...
- Hadoop Serialization -- hadoop序列化详解 (2)
回顾: 回顾序列化,其实原书的结构很清晰,我截图给出书中的章节结构: 序列化最主要的,最底层的是实现writable接口,wiritable规定读和写的游戏规则 (void write(DataOut ...
- 优于jdbc的mybatis框架入门
1.什么是mybatis? MyBatis 是支持普通 SQL 查询,存储过程和高级映射的优秀持久层框架. MyBatis 消除了几乎所有的 JDBC 代码和参数的手工设置以及对结果集的检索. MyB ...
- PHP屏蔽关键字实现方法
方法一: 思路 用正则去匹配关键字,把关键字用别的字符替换: 1 2 3 $str = "/你大爷|你麻痹|什么玩意|SB|你他妈/"; // 关键字正则字符串 $string ...
- 微信小程序怎么获取用户输入
能够获取用户输入的组件,需要使用组件的属性bindchange将用户的输入内容同步到 AppService. <input id="myInput" bindchange=& ...
- 690. Employee Importance员工权限重要性
[抄题]: You are given a data structure of employee information, which includes the employee's unique i ...
- c语言练习 二维数组 年平均降水量 月平均降水量
#define YEARS 5#define MONTHES 12 int main(void) { const float rain[YEARS][MONTHES] = { {4.3,4.3,4.3 ...
- win32多线程 (一) 线程创建与结束等待
#include "stdafx.h"#include <Windows.h>#include <iostream> using namespace std ...