英文版
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. If one does not exist, return 0.

(Recall that a subsequence is derived from another sequence A by deleting any number of elements (including none) from A, without changing the order of the remaining elements. For example, [3, 5, 8] is a subsequence of [3, 4, 5, 6, 7, 8].)

Example 1:

Input: [1,2,3,4,5,6,7,8]
Output: 5
Explanation:
The longest subsequence that is fibonacci-like: [1,2,3,5,8].

Example 2:

Input: [1,3,7,11,12,14,18]
Output: 3
Explanation:
The longest subsequence that is fibonacci-like:
[1,11,12], [3,11,14] or [7,11,18].

Note:

  • - 3 <= A.length <= 1000
  • - 1 <= A[0] < A[1] < ... < A[A.length - 1] <= 10^9

(The time limit has been reduced by 50% for submissions in Java, C, and C++.)

中文版:
给你一个严格单调递增的数组,请问数组里最长的斐波那契序列的长度是多少?例如,如果输入的数组是[1, 2, 3, 4, 5, 6, 7, 8],由于其中最长的斐波那契序列是1, 2, 3, 5, 8,因此输出应该是5。

分析:

思路一
在斐波那契序列中,第n个数字等于第n-1个数字与第n-2个数字之和。

考虑以数组中第i个数字(记为A[i])为结尾的最长斐波那契序列的长度。对于每一个j(0 <= j < i),A[j]都有可能是在某个斐波那契序列中A[i]前面的一个数字。如果存在一个k(k < j)满足A[k] + A[j] = A[i],那么这三个数字就组成了一个斐波那契序列。这个以A[i]为结尾、前一个数字是A[j]的斐波那契序列是在以A[j]为结尾、前一个数字是A[k]的序列的基础上增加了一个数字A[i],因此前者的长度是在后者的长度基础上加1。

我们可以用一个二维数组lengths来记录斐波那契序列的长度。二维数组中第i行第j列数字的含义是以输入数组中A[i]结尾、并且前一个数字是A[j]的斐波那契序列的长度。如果存在一个数字k,满足A[k] + A[j] = A[i],那么lengths[i][j] = lengths[j][k] + 1。如果不存在满足条件的k,那么意味这A[j]、A[i]不在任意一个斐波那契序列中,lengths[i][j]等于2。

二维数组lengths中的最大值就是输出值。

 class Solution {
public int lenLongestFibSubseq(int[] A) {
if (null == A || A.length == 0) {
return 0;
}
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < A.length; i ++) {
map.put(A[i], i);
} int[][] lengths = new int[A.length][A.length];
int maxLength = 1;
for (int i = 1; i < A.length; i ++) {
int num_3 = A[i];
int length = 2;
for (int j = i-1; j >= 0; j --) {
int num_2 = A[j];
int num_1 = num_3 - num_2; int len = 2;
if (num_1 < num_2 && map.containsKey(num_1)) {
len = lengths[j][map.get(num_1)] + 1;
}
lengths[i][j] = len;
length = Math.max(length, len);
}
maxLength = Math.max(maxLength, length);
}
return maxLength > 2 ? maxLength : 0;
}
}

思路二
双重循环枚举所有可能的情况

 class Solution {
public int lenLongestFibSubseq(int[] A) {
int N = A.length;
Set<Integer> S = new HashSet();
for (int x: A) S.add(x); int ans = 0;
for (int i = 0; i < N; ++i)
for (int j = i+1; j < N; ++j) {
int x = A[j], y = A[i] + A[j];
int length = 2;
while (S.contains(y)) {
// x, y -> y, x+y
int tmp = y;
y += x;
x = tmp;
ans = Math.max(ans, ++length);
}
} return ans >= 3 ? ans : 0;
}
}

最长斐波那契序列-LeetCode-873的更多相关文章

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

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

  2. HDU 5620 KK's Steel (斐波那契序列)

    KK's Steel 题目链接: http://acm.hust.edu.cn/vjudge/contest/121332#problem/J Description Our lovely KK ha ...

  3. pytho查找斐波那契序列中的值

    ''' 实现斐波那契序列,查找其中第N个数的值 ''' def FeiBSequence(list,N): length=len(list); i=0; while i<length: if N ...

  4. 爬楼梯问题-斐波那契序列的应用.md

    N 阶楼梯,一次可以爬1.2.3...n步,求爬楼梯的种类数 /** * 斐波那契序列 */ public class ClimbingStairs { // Sol 1: 递归 // 递归 公式:F ...

  5. 利用python实现二分法和斐波那契序列

    利用python实现二分法:我的实现思路如下 1.判断要查找的值是否大于最大值,如果大于则直接返回False 2.判断要查找的值是否小于最小值,如果小于则直接返回False 3.如果要查找的值在最大值 ...

  6. 【严蔚敏】【数据结构题集(C语言版)】1.17 求k阶斐波那契序列的第m项值的函数算法

    已知k阶斐波那契序列的定义为 f(0)=0,f(1)=0,...f(k-2)=0,f(k-1)=1; f(n)=f(n-1)+f(n-2)+...+f(n-k),n=k,k+1,... 试编写求k阶斐 ...

  7. 【剑指offer】斐波那契序列与跳台阶

    转载请注明出处:http://blog.csdn.net/ns_code/article/details/25337983 剑指offer上的第9题,简单题,在九度OJ上測试通过. 主要注意下面几点: ...

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

  9. 高精度处理斐波那契序列(C语言)

    #include<stdio.h> #include<string.h> //memset,strcpy,strlen函数头文件 int main(void) { ];//用来 ...

随机推荐

  1. 关于Ubuntu的默认python版本

    大部分Ubuntu系统默认python版本都是python2.x系列,但最新版本已经是3.5和3.6了,软件系统跟着版本走总是有诸多好处的,所以,以下是作者在修改Ubantu默认python版本时的一 ...

  2. c#连接oracle的几种方式

    一:通过System.Data.OracleClient(需要安装Oracle客户端并配置tnsnames.ora)1. 添加命名空间System.Data.OracleClient引用2. usin ...

  3. 目录打散-hash算法

    前几篇说了文件上传,都是上传到了WebRoot下的up目录,这样是不行的,文件多了性能就不行了.文件一般都是分目录存放的,这里讲建目录的一种算法.先看结果,经过本算法建的目录,结构是这样的,还以up目 ...

  4. 初学者必知的Python中优雅的用法 分类: Python 2015-05-11 15:02 782人阅读 评论(0) 收藏

    本文由 伯乐在线 - 淘小米 翻译,Daetalus 校稿.未经许可,禁止转载!英文出处:freepythontips.wordpress.com.欢迎加入翻译小组. Hi 朋友们.由于我最近都比较忙 ...

  5. CharacterRangeTable

    转载:https://github.com/jacoco/jacoco/wiki/CharacterRangeTable This page discusses a not yet available ...

  6. wap尝试调取app(网易新闻为例)

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  7. 接口文档管理神器RAP2安装和部署

    目录 一 RAP2 二 RAP2 安装需要的环境 2. 1 Node.js 安装: 2. 2 Mysql 5.7+ 安装 2 .3 Redis 安装见文章 2. 4 后端 rap2-delos 安装 ...

  8. springweb flux 服务器推送事件

    以前做服务器推送一般用轮询,后端主动给客户端推送不是很好解决.有时候也可以采用websocket 现在看了springwebflux,用它自带的方法做服务器推送方便多了. 代码如下: import o ...

  9. CentOS7.2配置Hadoop2.6.5

    Hadoop配置文件 /etc/profile 配置Java和Hadoop环境 export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk export CLAS ...

  10. 在C#中使用依赖注入

    依赖注入(Dependency Injection,缩写为DI)是一种实现(Inversion of Control,缩写为IoC)的方法.在编写C#代码时,使用这种方法能够解决一些场景的需求.本系列 ...