LintCode 366 Fibonacci】的更多相关文章

/* 1st method will lead to time limit *//* the time complexity is exponential sicne T(n) = T(n-1) + T(n-2) */ class Solution { /** * @param n: an integer * @return an integer f(n) */ public int fibonacci(int n) { // write your code here if (n == 1 ||…
题目: 斐波纳契数列 查找斐波纳契数列中第 N 个数. 所谓的斐波纳契数列是指: 前2个数是 0 和 1 . 第 i 个数是第 i-1 个数和第i-2 个数的和. 斐波纳契数列的前10个数字是: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 ... 样例 给定 1,返回 0 给定 2,返回 1 给定 10,返回 34 解题: 好像很简单的...递归是最简单的,貌似很耗时,结果:Time Limit Exceeded Java程序: 递归程序 class Solution { /…
描述 查找斐波纳契数列中第 N 个数. 所谓的斐波纳契数列是指: 前2个数是 0 和 1 . 第 i 个数是第 i-1 个数和第i-2 个数的和. 斐波纳契数列的前10个数字是: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 ... public class Solution { /** * @param n: an integer * @return: an ineger f(n) */ public int fibonacci(int n) { // write your…
C++ class Solution{ public: /** * @param n: an integer * @return an integer f(n) */ int fibonacci(int n) { // write your code here ,b=; ; i<n; i++) { a = a+b; b = a-b; } return a; } };…
实现: public class Solution { /** * @param n: an integer * @return: an ineger f(n) */ public int fibonacci(int n) { // write your code here return foo(n); } public int foo(int x){ if(x == 1){ return 0; } else if(x == 2){ return 1; } return foo(x-2) + f…
Find the Nth number in Fibonacci sequence. A Fibonacci sequence is defined as follow: The first two numbers are 0 and 1. The i th number is the sum of i-1 th number and i-2 th number. The first ten numbers in Fibonacci sequence is: 0, 1, 1, 2, 3, 5,…
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (2016-02-10) For more problems and solutions, you can see my LintCode repository. I'll keep updating for full summary and better solutions. See cnblogs t…
汇总贴 56. Two Sum[easy] 167. Add Two Numbers[easy] 53. Reverse Words in a String[easy] 82. Single Number[easy] 17. Subsets[medium] 18. Subsets II[medium] 219. Insert Node in Sorted Linked List[Naive] 366. Fibonacci[Naive] 452. Remove Linked List Elemen…
2016.12.4, 366 http://www.lintcode.com/en/problem/fibonacci/ 一刷使用递归算法,超时.二刷使用九章算术的算法,就是滚动指针的思路,以前写python的时候也玩过,但是给忘了,这次又用c++拾起来了.lint有bug,不能用,很烦. class Solution { public: /** * @param n: an integer * @return an integer f(n) */ int fibonacci(int n) {…
直接使用递归的方法会导致TLE,加个缓存就好了: public class Solution { private Integer[] buff = new Integer[1000]; /* * @param n: an integer * @return: an ineger f(n) */ public int fibonacci(int n) { if(buff[n]!=null) return buff[n]; else if(n==1) return buff[1] = 0; else…