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 ...
随机推荐
- java环境配置步骤
1. jdk下载 官网:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html ...
- jQuery检查某个元素在页面上是否存在
用jQuery检查某个元素在网页上是否存在时,应该根据获取元素的长度来判断,代码如下: if($("#tt").length > 0) { //元素存在时执行的代码 } 具体 ...
- 微信App支付通知验签
微信异步通知: [AcceptVerbs("POST")] public void Notify() { //编码(101-登录无效,102-账号无效,200-成功,201-失败, ...
- 激活windows10 LTSB 2016
激活windows10 LTSB 2016 slmgr /ipk DCPHK-NFMTC-H88MJ-PFHPY-QJ4BJ slmgr /skms kms.firadio.net slmgr /at ...
- iOS UITableViewCell的分割线向左延长15(cell长度为全宽)
iOS7情况下: tableView.separatorInset = UIEdgeInsetsZero; iOS8.9情况下: 首先在viewDidLoad方法中加上如下代码: if ([table ...
- interactivePopGestureRecognizer
苹果一直都在人机交互中尽力做到极致,在iOS7中,新增加了一个小小的功能,也就是这个api:self.navigationController.interactivePopGestureRecogni ...
- winfrom 捕获是否点击关闭按钮关闭的窗体
const int WM_SYSCOMMAND = 0x0112; const int SC_CLOSE = 0xF060; protected override void WndProc(ref M ...
- Maven中配置默认JDK版本
使用Maven构建项目时,默认是使用jdk1.3版本,目前很多使用泛型的项目肯定是无法通过编译,除了修改项目的pom文件外,还可以在Maven的配置文件settings.xml中添加如下配置来解决: ...
- Mysql连接到Visual studio注意
测试环境:mysql(mysql-connector-net-6.9.7.msi)+visual studio2012 1.mysql安装ok的情况下!在控制面板管理工具-数据源(ODBC)--用户D ...
- C语言打印最长字符串
编程在一个已知的字符串中找最长单词,假定字符串中只含字母和空格,空格用来分隔不同单词. ]; printf("请输入字符串:"); fgets(p, , stdin); ; ; ; ...