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) {
            return (n-1);
        }

        // int sum = (n-1) + (n-2);

        return fibonacci(n-1) + fibonacci(n-2);

    }
}

/* 2nd method will need O(n) space, using DP */
/* T and S are both O(n) */

 1 public int fibonacci(int n) {
 2         // declare an array to store the result
 3         // it has to be n+2 to avoid out_of_bound
 4         int[] f = new int[n+2];
 5         f[1] = 0; // when input is 1 => zero
 6         f[2] = 1; // when input is 2 => 1
 7
 8         int i = 3;
 9         while (i <= n) { // it has to be incremental instead of decremental
10             f[i] = f[i-1] + f[i-2];
11             i++;
12         }
13
14         return f[n];
15     }

/* 3rd method will only need O(1) space */
/* We can optimize the space used in method 2 by storing the previous two numbers only */
/* because that is all we need to get the next Fibannaci number in series. */

 1 public int fibonacci(int n) {
 2     if (n < 3) return n-1;
 3
 4     int first = 0;
 5     int second = 1;
 6     int third = 1;
 7
 8     int i = 3;
 9     while (i <= n) {
10         third = first + second;
11         first = second;
12         second = third;
13         i++;
14     }
15     return third;
16 }
时间: 2024-10-12 20:40:07

LintCode 366 Fibonacci的相关文章

LintCode:Fibonacci

C++ 1 class Solution{ 2 public: 3 /** 4 * @param n: an integer 5 * @return an integer f(n) 6 */ 7 int fibonacci(int n) { 8 // write your code here 9 int a=0,b=1; 10 for (int i=1; i<n; i++) { 11 a = a+b; 12 b = a-b; 13 } 14 return a; 15 } 16 };

LintCode:剑指Offer

第1章: 9.Fizz Buzz :http://www.lintcode.com/zh-cn/problem/fizz-buzz/ 解法1:(%  String.valueOf) (1.rst;  2.for(1,n),15,5,3,else;  3.return) 1 public class Solution { 2 public List<String> fizzBuzz(int n) { 3 List<String> rst = new ArrayList<>

Lintcode Fibonacci

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,

lintcode bugfree and good codestyle note

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) {

【转】Fibonacci 斐波纳契堆优化 Dijkstra 最短路径算法

话不多说,拿来主义,直接上代码! PS:打印最短路径我还不晓得怎么加,如有哪位大神知道,还请mark一下! 1 /*********************************************************************** 2 * File: FibonacciHeap.java 3 * Author: Keith Schwarz ([email protected]) 4 * 5 * An implementation of a priority queue

LintCode Python 入门级题目 斐波纳契数列

原题描述: 查找斐波纳契数列中第 N 个数. 所谓的斐波纳契数列是指: 前2个数是 0 和 1 . 第 i 个数是第 i-1 个数和第i-2 个数的和. 斐波纳契数列的前10个数字是: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 ... 题目分析: 开始的想法,通过递归实现输出fib(n-1)+fib(n-2)的值计算,原理正确,算法复杂度高,导致运行时间超过lintcode限制: class Solution: # @param n: an integer # @retur

LintCode题解之斐波纳契数列

直接使用递归的方法会导致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

NYOJ 480 Fibonacci Again!

Fibonacci Again! 时间限制:1000 ms  |  内存限制:65535 KB 难度:2 描写叙述 求第n个斐波那契数是否是一个素数,n为整数 f[n]=f[n-1]+f[n-2] (2<n<30) f[1]=3,f[2]=7 输入 输入整数m,0<m<30,输入-1表示结束输入 输出 假设f[m]是素数 则输出Yes,否则输出No, 每行输出占一行. 例子输入 2 3 例子输出 Yes No #include<stdio.h> int f[35]={0

HDU1848 Fibonacci again and again

Fibonacci again and again Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 8198    Accepted Submission(s): 3412 Problem Description 任何一个大学生对菲波那契数列(Fibonacci numbers)应该都不会陌生,它是这样定义的:F(1)=1;F(2)=2;