最长的斐波那契子序列的长度

1、暴力法 

枚举所有可能的情况,计算每一种斐波那契数列的长度,取最大值。

	// 暴力法(不好实施)
	public int lenLongestFibSubseq(int[] A)
	{
		int a, b, aa;
		int longest = 0;
		int len = 2; // 当前斐波那契子序列的长度
		
		for (int i=0; i<A.length; i++)
		{
			a = A[i];
			for (int j=i+1; j<A.length; j++)
			{
				b = A[j];
				len = 2;
				for (int k=j+1; k<A.length; k++)
				{
					if (a + b > A[A.length-1])
						break;
					
					// 满足约束条件
					if (a + b == A[k])
					{
						a = b;
						b = A[k];
						len++;
					}
				}
				a = A[i]; // 这里很关键,必须恢复a
				longest = Math.max(longest, len);
			}
		}
		
		return longest < 3? 0: longest;
	}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
序言 Preface 方框目录 0Prologue(序论) 0.1Booksandalgorithms(书和算法) 0.2EnterFibonacci(斐波那契数列) 0.3Big-Onotation(大O记号) Exercises(习题) 1Algorithmswithnumbers(数的算法) 1.1Basicarithmetic(基本算术) 1.2Modulararithmetic(模运算) 1.3Primalitytesting(素性测试) 1.4Cryptography(密码学) 1.5Universalhashing(全域散列) Exercises(习题) Randomizedalgorithms:avirtualchapter(虚拟章:随机化算法) 2Divide-and-conqueralgorithms(分而治之算法) 2.1Multiplication(乘法) 2.2Recurrencerelations(递归关系) 2.3Mergesort(合并排序) 2.4Medians(中位数) 2.5Matrixmultiplication(矩阵乘法) 2.6ThefastFouriertransform(快速傅里叶变换) Exercises(习题) 3Decompositionsofgraphs(图的分解) 3.1Whygraphs?(图论) 3.2Depth-firstsearchinundirectedgraphs(无向图中的深度优先搜索) 3.3Depth-firstsearchindirectedgraphs(有向图中的深度优先搜索) 3.4Stronglyconnectedcomponents(强连通分量) Exercises(习题)—— 4Pathsingraphs(图的路径) 4.1Distances(距离) 4.2Breadth-firstsearch(广度优先搜索) 4.3Lengthsonedges(边的长度) 4.4Dijkstra’salgorithm(Dijkstra算法) 4.5Priorityqueueimplementations(实现优先队列) 4.6Shortestpathsinthepresenceofnegativeedges(带负权的边的图中的最短路径) 4.7Shortestpathsindags(有向无环图中的最短路径) Exercises(习题) 5Greedyalgorithms(贪婪算法) 5.1Minimumspanningtrees(最小生成树) 5.2Huffmanencoding(赫夫曼编码) 5.3Hornformulas(Horn公式) 5.4Setcover(集合覆盖) Exercises(习题) 6Dynamicprogramming(动态规划) 6.1Shortestpathsindags,revisited(回顾:有向无环图中的最短路径) 6.2Longestincreasingsubsequences(最长 递增子序列) 6.3Editdistance(编辑距离) 6.4Knapsack(背包问题) 6.5Chainmatrixmultiplication(链式矩阵乘法) 6.6Shortestpaths(最短路径) 6.7Independentsetsintrees(树中的独立集) Exercises(习题) 7Linearprogrammingandreductions(线性规划与归约) 7.1Anintroductiontolinearprogramming(线性规划入门) 7.2Flowsinnetworks(网络流) 7.3Bipartitematching(二部图匹配) 7.4Duality(对偶性) 7.5Zero-sumgames(零和游戏) 7.6Thesimplexalgorithm(单纯形算法) 7.7Postscript:circuitevaluation(附录:电路求值) Exercises(习题) 8NP-completeproblems(NP完全问题) 8.1Searchproblems(搜索问题) 8.2NP-completeproblems(NP完全问题) 8.3Thereductions(归约) Exercises(习题) 9CopingwithNP-completeness(处理NP完全问题) 9.1Intelligentexhaustivesearch(智能穷举搜索) 9.2Approximationalgorithms(近似算法) 9.3Localsearchheuristics(局部启发式搜索) Exercises(习题) 10Quantumalgorithms(量子算法) 10.1Qubits,

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值