英文版
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. vue 组件之间的数据传递

    父组件传数据给子组件 创建数据 获取json数据 子组件传数据给父组件 1. 子组件使用$emit监听数据 getAreaValues(){ this.$emit('getAreaValues', { ...

  2. Eclipse启动和手动启动tomcat访问localhost:8080显示404问题总结

    前言:建议对tomcat的文件结构和相关属性有较多了解.本文以eclipse的DynamicWebProject为讲解对象. 目录: eclipse添加tomcat关联注意点 tomcat404问题归 ...

  3. iOS根据图片url获取尺寸

    可以在UIImage的分类中加入下面的代码,并且引入系统的ImageIO.framework /** 根据图片的url获取尺寸 @param URL url @return CGSize */ + ( ...

  4. vector.clear()不能用来清零

    vector.clear()函数并不会把所有元素清零,笔者就曾经这样幻想过这个函数的作用,然而事实证明并不是. vector有两个参数,一个是size,表示当前vector容器内存储的元素个数,一个是 ...

  5. shell脚本实现无密码交互的SSH自动登陆

    ssh连接远程主机时候询问密码,跟su.sudo命令的默认行为一样,是不从stdin读入数据的,据称是为安全考虑,但是有时候在脚本当中确实需要无人守值的登陆. 搜索一下不难找到类似的例子,使用expe ...

  6. 云计算平台管理的三大利器Nagios、Ganglia和Splunk

    综合利用Nagios.Ganglia和Splunk搭建起的云计算平台监控体系,具备错误报警.性能调优.问题追踪和自动生成运维报表的功能.有了这套系统,就可轻松管理Hadoop/HBase云计算平台. ...

  7. HUE配置文件hue.ini 的mapred_clusters模块详解(图文详解)(分HA集群和非HA集群)

    不多说,直接上干货! 我的集群机器情况是 bigdatamaster(192.168.80.10).bigdataslave1(192.168.80.11)和bigdataslave2(192.168 ...

  8. __name__ == "__main__"的作用是什么?

    问题: __name__ == "__main__" 的作用是什么? # Threading example import time, thread def myfunction( ...

  9. xgb 绘制

    1.windows安装Graphviz2.38 安装地址:https://graphviz.gitlab.io/_pages/Download/Download_windows.html 2.在pyt ...

  10. mongodb-分组分页

    1, 添加测试数据 @Test public void save() { News n = null; ; i < ; i++) { n = new News(); n.setTitle(&qu ...