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 all 0 <= 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. 1 <= S.length <= 200
  2. 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的更多相关文章

  1. LeetCode 842. Split Array into Fibonacci Sequence

    原题链接在这里:https://leetcode.com/problems/split-array-into-fibonacci-sequence/ 题目: Given a string S of d ...

  2. 842. Split Array into Fibonacci Sequence能否把数列返回成斐波那契数列

    [抄题]: Given a string S of digits, such as S = "123456579", we can split it into a Fibonacc ...

  3. 【LeetCode】842. Split Array into Fibonacci Sequence 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  4. 842. Split Array into Fibonacci Sequence —— weekly contest 86

    题目链接:https://leetcode.com/problems/split-array-into-fibonacci-sequence/description/ 占坑. string 的数值转换 ...

  5. [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 ...

  6. [LeetCode] Split Array into Fibonacci Sequence 分割数组成斐波那契序列

    Given a string S of digits, such as S = "123456579", we can split it into a Fibonacci-like ...

  7. Split Array into Consecutive Subsequences

    659. Split Array into Consecutive Subsequences You are given an integer array sorted in ascending or ...

  8. 【每天一题ACM】 斐波那契数列(Fibonacci sequence)的实现

    最近因为一些原因需要接触一些ACM的东西,想想写个blog当作笔记吧!同时也给有需要的人一些参考 话不多说,关于斐波那契数列(Fibonacci sequence)不了解的同学可以看看百度百科之类的, ...

  9. ***1133. Fibonacci Sequence(斐波那契数列,二分,数论)

    1133. Fibonacci Sequence Time limit: 1.0 secondMemory limit: 64 MB is an infinite sequence of intege ...

随机推荐

  1. 第九章 Servlet工作原理解析

    9.1 从Servlet容器说起    Servlet容器:Jetty, Tomcat等. 这里以Tomcat为例,  真正管理Servlet的容器是Context容器,一个Context对应一个WE ...

  2. Lua调用C++带参数的方法

    C++代码: // LuaAndC.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> #i ...

  3. oracle自动创建表分区

    创建一个table,记录哪些表需要创建表分区 create table STAT_TABLE ( tablename VARCHAR2(), pre_partition_name VARCHAR2() ...

  4. UML中关系的分类及其概念——总结备忘

    UML中关系分类: 依赖:依赖是两个事物间的语义关系,其中一个事物(独立事物)发生变化会影响另一个事物(依赖事物)的语义. 关联:关联是类与类之间的联接,它使一个类知道另一类的属性和方法. 聚合:聚合 ...

  5. Bell数和Stirling数

    前面说到了Catalan数,现在来了一个Bell数和Stirling数.什么是Bell数,什么是Stirling数呢?两者的关系如何,有用于解决什么算法问题呢? Bell数是以Bell这个人命名的,组 ...

  6. 08-Location总结图解

    URI解析  首先要判断有没有精准匹配,能不能精准匹配.计算机里面没有什么这种差不多这种东西.跟人聊天才说差不多,最近过得怎么样啊,还行吧,差不多吧,这个不多是多还是不多啊. 预定义库->Gen ...

  7. 【bzoj4296】再见Xor

    4269: 再见Xor Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 176  Solved: 107[Submit][Status][Discuss ...

  8. 【bzoj1565】[NOI2009]植物大战僵尸

    1565: [NOI2009]植物大战僵尸 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 2164  Solved: 1001[Submit][Stat ...

  9. ubuntu16.04 安装openpose

    安装 Anaconda3 Tensorflow-cpu python3tensorflow 1.4.1+opencv3, protobuf, python3-tk ================== ...

  10. 无返回值的函数如何捕获出错情况(检查errno常量)

    在执行这个函数前,先清除errno,函数返回时,检查errno常量. 每次程序调用失败的时候,系统会自动用用错误代码填充errno这个全局变量,这样你只需要读errno这个全局变量就可以获得失败原因了 ...