/* 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) */

 public int fibonacci(int n) {
// declare an array to store the result
// it has to be n+2 to avoid out_of_bound
int[] f = new int[n+2];
f[1] = 0; // when input is 1 => zero
f[2] = 1; // when input is 2 => 1 int i = 3;
while (i <= n) { // it has to be incremental instead of decremental
f[i] = f[i-1] + f[i-2];
i++;
} return f[n];
}

/* 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. */

 public int fibonacci(int n) {
if (n < 3) return n-1; int first = 0;
int second = 1;
int third = 1; int i = 3;
while (i <= n) {
third = first + second;
first = second;
second = third;
i++;
}
return third;
}

LintCode 366 Fibonacci的更多相关文章

  1. lintcode:Fibonacci 斐波纳契数列

    题目: 斐波纳契数列 查找斐波纳契数列中第 N 个数. 所谓的斐波纳契数列是指: 前2个数是 0 和 1 . 第 i 个数是第 i-1 个数和第i-2 个数的和. 斐波纳契数列的前10个数字是: 0, ...

  2. 366. Fibonacci

    描述 查找斐波纳契数列中第 N 个数. 所谓的斐波纳契数列是指: 前2个数是 0 和 1 . 第 i 个数是第 i-1 个数和第i-2 个数的和. 斐波纳契数列的前10个数字是: 0, 1, 1, 2 ...

  3. LintCode:Fibonacci

    C++ class Solution{ public: /** * @param n: an integer * @return an integer f(n) */ int fibonacci(in ...

  4. 【未通过】LintCode #366 斐波纳契数列

    实现: public class Solution { /** * @param n: an integer * @return: an ineger f(n) */ public int fibon ...

  5. 366. Fibonacci【Naive】

    Find the Nth number in Fibonacci sequence. A Fibonacci sequence is defined as follow: The first two ...

  6. [LintCode]——目录

    Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...

  7. Lintcode记录

    汇总贴 56. Two Sum[easy] 167. Add Two Numbers[easy] 53. Reverse Words in a String[easy] 82. Single Numb ...

  8. lintcode bugfree and good codestyle note

    2016.12.4, 366 http://www.lintcode.com/en/problem/fibonacci/ 一刷使用递归算法,超时.二刷使用九章算术的算法,就是滚动指针的思路,以前写py ...

  9. LintCode题解之斐波纳契数列

    直接使用递归的方法会导致TLE,加个缓存就好了: public class Solution { private Integer[] buff = new Integer[1000]; /* * @p ...

随机推荐

  1. JS的prototype

    初步理解: 在说prototype和constructor之前我们先得看几个例子. 1 2 3 4 function name(obj){     alert(obj)//"uw3c&quo ...

  2. colorbox 自适应 高度

    $(".example3").colorbox({  inline: true, scrolling: false ,                    onComplete: ...

  3. Android Studio 简介及导入 jar 包和第三方开源库方[转]

    原文:http://blog.sina.com.cn/s/blog_693301190102v6au.html Android Studio 简介 几天前的晚上突然又想使用 Android Studi ...

  4. intelj对我来说比较常用的快捷键

    文件查找 CTRL+N   查找类 CTRL+SHIFT+N  查找文件 CTRL+SHIFT+ALT+N 查找类中的方法或变量 CTRL+ALT+B  找所有的子类 CTRL+G   定位行 CTR ...

  5. 第一课1、ROS

    ---恢复内容开始--- 1.什么是ROS ROS起源于2007年,斯坦福大学的人工智能实验室与机器人技术公司Willow Garage针对其个人机器人项目开发了ROS的雏形. ROS大致每年发布一个 ...

  6. 数学对象-Math

    Math 属性: PI        圆周率 例子:var x=Math.xxxx(); sqrt()  一个非负数的平方根    nan pow()   x的y次幂的值      Math.pow( ...

  7. Hibernate配置与事务管理

    数据库中 @num:代表一个变量 Set @num = 10; Select @num+@num from dual;  dual:临时表 得到结果 20 Hibernate:运用数据持久化,使用OR ...

  8. Shell--用户配置

    vim /etc/profileexport PS1='\[\e[1;33m\]\h\[\e[m\] \t [\[\e[1;36m\]\w\[\e[m\]] [\u] '   export LANG= ...

  9. oracle 11gr2 官方文档下载

    http://www.oracle.com/technetwork/database/enterprise-edition/documentation/index.html

  10. jquery之右下角消息提示框

    messager.js (function (jQuery) { var window; var obj = new Object(); obj.version = '@1.0'; obj.title ...