842. 将数组拆分成斐波那契序列

给定一个数字字符串 S,比如 S = "123456579",我们可以将它分成斐波那契式的序列 [123, 456, 579]。

形式上,斐波那契式序列是一个非负整数列表 F,且满足:

0 <= F[i] <= 2^31 - 1,(也就是说,每个整数都符合 32 位有符号整数类型);
F.length >= 3;
对于所有的0 <= i < F.length - 2,都有 F[i] + F[i+1] = F[i+2] 成立。
另外,请注意,将字符串拆分成小块时,每个块的数字一定不要以零开头,除非这个块是数字 0 本身。

返回从 S 拆分出来的所有斐波那契式的序列块,如果不能拆分则返回 []。

示例 1:

输入:"123456579"
输出:[123,456,579]
示例 2:

输入: "11235813"
输出: [1,1,2,3,5,8,13]
示例 3:

输入: "112358130"
输出: []
解释: 这项任务无法完成。
示例 4:

输入:"0123"
输出:[]
解释:每个块的数字不能以零开头,因此 "01","2","3" 不是有效答案。
示例 5:

输入: "1101111"
输出: [110, 1, 111]
解释: 输出 [11,0,11,11] 也同样被接受。
提示:

1 <= S.length <= 200
字符串 S 中只含有数字。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/split-array-into-fibonacci-sequence
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

代码有点儿烂,做了好久,超过人数才5点几,真TM fuck。

 1 public class Solution {
 2     private String s;
 3     private int len = -1;
 4     private List<Integer> res;
 5     private int e1;
 6     private int e2;
 7     private int e3;
 8
 9     private String isContinue(int m,int n,int o){
10         String r = null;
11         String a = s.substring(m, n);
12         String b = s.substring(n, o);
13         if (a.length() > 10 || b.length() > 10)
14             return "";
15         long a0 = Long.parseLong(a);
16         long b0 = Long.parseLong(b);
17         if (a0 > 2147483647 || b0 > 2147483647)
18             return "";
19         e1 = (int)a0;
20         e2 = (int)b0;
21         if (a0+b0 > 2147483647)
22             return "";
23         e3 = (int)(a0+b0);
24         String c = String.valueOf(a0 + b0);
25         if (a.charAt(0)==‘0‘ && a.length()!=1 || b.charAt(0)==‘0‘ && b.length() != 1)
26             r = "";
27         else if (c.length() > len-o)
28             r = "";
29         else if (!s.substring(o,o+c.length()).equals(c))
30             r = "";
31         return (r != null) ? "" : c;
32     }
33
34     private boolean helper(int m, int n, int o){
35         if (isContinue(m,n,o).equals("")){
36             return false;
37         }
38
39         for (int i = n; i < len; i++) {
40             for (int j = i+1; j < len; j++) {
41                 String c = isContinue(m, i, j);
42                 if (c.equals(""))
43                     continue;
44                 if (s.substring(j,j+c.length()).equals(c)){
45                     if (c.length() > len-j)
46                         return false;
47
48                     res.add(e1);
49
50                     if (c.length() == len-j) {
51                         if (e2 == e1 || res.get(res.size()-1) != e2)
52                             res.add(e2);
53                         res.add(e3);
54                         return true;
55                     }
56                     if (helper(i, j, j+c.length()))
57                         return true;
58
59                     res.remove(res.size()-1);
60                 }
61             }
62         }
63         return false;
64     }
65     public List<Integer> splitIntoFibonacci(String S) {
66         s = S;
67         len = s.length();
68         res = new ArrayList<>();
69
70         for (int i = 1; i < len; i++) {
71             for (int j = i+1; j < len; j++) {
72                 if (helper(0,i,j))
73                     return res;
74             }
75         }
76         return res;
77     }
78
79     public static void main(String[] args) {
80         List<Integer> integers = new Solution().splitIntoFibonacci("3611537383985343591834441270352104793375145479938855071433500231900737525076071514982402115895535257195564161509167334647108949738176284385285234123461518508746752631120827113919550237703163294909");
81         System.out.println("integers = " + integers);
82     }
83 }

原文地址:https://www.cnblogs.com/yfs123456/p/11620933.html

时间: 2024-08-25 01:34:54

842. 将数组拆分成斐波那契序列的相关文章

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

最长斐波那契序列-LeetCode-873

英文版A sequence X_1, X_2, ..., X_n is fibonacci-like if: - n >= 3- X_i + X_{i+1} = X_{i+2} for all i + 2 <= n Given a strictly increasing array A of positive integers forming a sequence, find the length of the longest fibonacci-like subsequence of A.

斐波那契序列与跳台阶

转载请注明出处:http://blog.csdn.net/ns_code/article/details/25337983 剑指offer上的第就题,简单题,在九度OJ上测试通过. 主要注意以下几点: 1.用非递归实现,递归会超时 2.结果要用long long保存,不然会发生结果的溢出,从而得到负值 3.如果是在VC++6.0下编译的,long long是illegal的,要用_int64代替,同时输出的转化以字符也要用%64d代替%lld 时间限制:1 秒 内存限制:32 兆 题目描述: 大

斐波那契序列的求解

设斐波那契序列是f(n),其中 原文地址:https://www.cnblogs.com/gaoyixue/p/10464584.html

斐波那契序列的递归和非递归的实现

  斐波那契数列(Fibonacci sequence),又称黄金分割数列.因数学家列昂纳多·斐波那契(Leonardoda Fibonacci[1])以兔子繁殖为例子而引入,故又称为"兔子数列".   指的是这样一个数列:0.1.1.2.3.5.8.13.21.34.--在数学上,斐波纳契数列以如下被以递归的方法定义:         F(0)=0,F(1)=1,F(n)=F(n-1)+F(n-2)(n≥2,n∈N*)       #include<iostream> u

斐波拉契序列几种实现

今天遇到一个面试题:输入n,如果n<=1,f(n)=1,其他f(n)=f(n-1)+f(n-2), 打印f(n)从1到n的值. public class Fibonacci { private int n; private StringBuilder sb; private String format; @Before public void init() { n = 10; sb = new StringBuilder(n); format = "f(%s) = "; } 直接

使用python实现斐波那契序列

def f1(a,b,stop):     if a == stop:         print(a)         return True     elif b == stop:         print(a,b,sep=" ",end="\n")         return True     elif a > stop:         print("无效的输入")         return False     else:

leetcode笔记:Climbing Stairs(斐波那契数列问题)

一.题目描述 You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top? 题目的大意是,已知有n阶楼梯,每次只能爬1阶或2阶楼梯,问爬到第n阶楼梯共有几种爬法-_-||.题目可以看成是,设f(n)表示爬到第n 阶楼梯的方法数,为

二分查找和斐波那契查找

二分查找 说明:查找的数组或列表必须是有序的,若无序,先进行排序 复杂度:时间复杂度 O(log2n),空间复杂度O(n) C++源码(递归和非递归两个版本) #include <iostream> using namespace std; int a[] = { 1, 2, 3, 4, 5, 6, 8 }; int BinarySearch1(int l, int r, int value) { int mid = (l + r) / 2; if (l == r && a[l