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) */
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的更多相关文章
- lintcode:Fibonacci 斐波纳契数列
题目: 斐波纳契数列 查找斐波纳契数列中第 N 个数. 所谓的斐波纳契数列是指: 前2个数是 0 和 1 . 第 i 个数是第 i-1 个数和第i-2 个数的和. 斐波纳契数列的前10个数字是: 0, ...
- 366. Fibonacci
描述 查找斐波纳契数列中第 N 个数. 所谓的斐波纳契数列是指: 前2个数是 0 和 1 . 第 i 个数是第 i-1 个数和第i-2 个数的和. 斐波纳契数列的前10个数字是: 0, 1, 1, 2 ...
- LintCode:Fibonacci
C++ class Solution{ public: /** * @param n: an integer * @return an integer f(n) */ int fibonacci(in ...
- 【未通过】LintCode #366 斐波纳契数列
实现: public class Solution { /** * @param n: an integer * @return: an ineger f(n) */ public int fibon ...
- 366. Fibonacci【Naive】
Find the Nth number in Fibonacci sequence. A Fibonacci sequence is defined as follow: The first two ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- Lintcode记录
汇总贴 56. Two Sum[easy] 167. Add Two Numbers[easy] 53. Reverse Words in a String[easy] 82. Single Numb ...
- lintcode bugfree and good codestyle note
2016.12.4, 366 http://www.lintcode.com/en/problem/fibonacci/ 一刷使用递归算法,超时.二刷使用九章算术的算法,就是滚动指针的思路,以前写py ...
- LintCode题解之斐波纳契数列
直接使用递归的方法会导致TLE,加个缓存就好了: public class Solution { private Integer[] buff = new Integer[1000]; /* * @p ...
随机推荐
- 自适应UITableView的Cell高度问题
1.自己计算Cell的高度返回: 1>model中计算: // // InfoModel.h // OCDemo // // Created by 思 彭 on 16/12/27. // Cop ...
- 学习笔记-----Android的View绘制过程
边看源码边参考别人的博客等,做一下学习笔记. 要了解View的绘制,首先得知道View树的结构:(可以参考http://blog.csdn.net/qinjuning/article/details/ ...
- C# WinForm 中 MessageBox的使用详解
1.C# WinForm 中 MessageBox的使用详解:http://www.cnblogs.com/bq-blog/archive/2012/07/27/2611810.html
- Java(多态)动手动脑
1> 请看以下"变态"的类(参看示例ParentChildTest.java) 上述代码的特点是: 子类和父类定义了一模一样的字段和方法 运行以下测试代码 1. 上边的程序运 ...
- 分析‖为什么越来越多厂商开始发力VR一体机?
2015年下半年,国内VR头显市场的主旋律还是PC头显和手机盒子.到了2016年上半年,一体机逐渐上位,成为发布会上的主角. 近期IDEALENS启视在北京召开发布会,发布会的主角K2和K2Pro正是 ...
- HYSBZ 4551 (树状数组) 采花
题目:这里 题意: 在2016年,佳媛姐姐刚刚学习了树,非常开心.现在他想解决这样一个问题:给定一颗有根树(根为1),有以下 两种操作:1. 标记操作:对某个结点打上标记(在最开始,只有结点1有标记, ...
- 初学git(一):创建本地“仓库”
初学git(一):创建本地“仓库” 作为一个初学者,我是跟着廖学峰老师的官方博客学习,这里只是做个笔记,哈哈,关于git的历史.和其他版本控制的对比啥的,我就不说了.另外,我这里用的是Mac OS X ...
- CSS盒子模型的理解
标准的CSS盒子模型包括:内容(content).填充(padding).边框(border).边界(margin) 这些属性,可以把它转移到我们日常生活中的盒子(箱子)上来理解,日常生活中所见的盒子 ...
- 在github上搭建博客的问题
最近想到要建立一个自己的博客,以便记录自己在学习中遇到的问题.发现github免费提供空间,正好可以利用它来搭建自己的博客.毕竟github pages免费空间,不限制流量,每次的博客改动和博客模板的 ...
- jsp文件引入js文件的方式(项目部署于web容器中)
在页面中引入javascript文件的方式是多种多样的,本文介绍两种. 通过<script>标签插入js文件 通过这种方式引入的js,写对js文件和jsp文件的路径很重要.下面给出一个项目 ...