资讯专栏INFORMATION COLUMN

[LeetCode] 842. Split Array into Fibonacci Sequenc

zhaofeihao / 2080人阅读

Problem

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 <= S.length <= 200
S contains only digits.

Solution
class Solution {
    public List splitIntoFibonacci(String S) {
        List res = new ArrayList<>();
        if (S == null) return res;
        helper(S, 0, res);
        return res;
    }
    private boolean helper(String str, int start, List res) {
        if (start == str.length() && res.size() > 2) return true;
        for (int i = start; i < str.length(); i++) {
            if (str.charAt(start) == "0" && i != start) break;
            long num = Long.parseLong(str.substring(start, i+1));
            if (num > Integer.MAX_VALUE) return false;
            int size = res.size();
            if (size >= 2 && num > res.get(size-2) + res.get(size-1)) break;
            if (size <= 1 || num == res.get(size-2) + res.get(size-1)) {
                res.add((int)num);
                if (helper(str, i+1, res)) return true;
                res.remove(res.size()-1);
            }
        }
        return false;
    }
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/72403.html

相关文章

  • [Leetcode] Binary Tree Longest Consecutive Sequenc

    摘要:递归法复杂度时间空间思路因为要找最长的连续路径,我们在遍历树的时候需要两个信息,一是目前连起来的路径有多长,二是目前路径的上一个节点的值。代码判断当前是否连续返回当前长度,左子树长度,和右子树长度中较大的那个 Binary Tree Longest Consecutive Sequence Given a binary tree, find the length of the lon...

    xi4oh4o 评论0 收藏0
  • leetcode410. Split Array Largest Sum

    摘要:在这里,边界被设置为该数组中可以得到的子数组元素和的最小值和最大值。在确定了数组元素和的上界和下界之后,就需要找出一种方法,来不断压缩区间直到最后一种。可以使用中间位置作为数组元素和的边界,即假设所有的连续数组的和都不会超过值。 题目要求 Given an array which consists of non-negative integers and an integer m, y...

    Jonathan Shieber 评论0 收藏0
  • [LintCode/LeetCode] Binary Tree Serialization

    摘要:这里要注意的是的用法。所以记住,用可以从自动分离出数组。跳过第一个元素并放入数组最快捷语句建立的用意记录处理过的结点并按处理所有结点和自己的连接下面先通过判断,再修改的符号的顺序,十分巧妙更轻便的解法 Problem Design an algorithm and write code to serialize and deserialize a binary tree. Writin...

    keithyau 评论0 收藏0
  • Licia:最全最实用的 JavaScript 工具库

    摘要:为了避免不同项目之间进行复制粘贴,可以将这些常用的函数封装到一起并发布包。目前所包含模块已达三百个,基本可以满足前端的日常工发需求。二使用打包工具该项目自带打包工具,可以通过配置文件或命令行扫描源码自动生成项目专用的工具库。 前言 在业务开发过程中,我们经常会重复使用日期格式化、cookie 操作、模板、浏览器判断、类型判断等功能。为了避免不同项目之间进行复制粘贴,可以将这些常用的函数...

    luxixing 评论0 收藏0
  • JavaScript中的算法(附10道面试常见算法题解决方法和思路)

    摘要:中的算法附道面试常见算法题解决方法和思路关注每日一道面试题详解面试过程通常从最初的电话面试开始,然后是现场面试,检查编程技能和文化契合度。值得记住的数组方法有和。一个好的解决方案是使用内置的方法。 JavaScript中的算法(附10道面试常见算法题解决方法和思路) 关注github每日一道面试题详解 Introduction 面试过程通常从最初的电话面试开始,然后是现场面试,检查编程...

    Cruise_Chan 评论0 收藏0

发表评论

0条评论

zhaofeihao

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<