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 ...
随机推荐
- 自定义View(三)实现简单的可拖动、可缩放的ImageView
实现技术主要用到1.多点触摸 2.matrix的矩阵,平移.缩放 根据手指的数量判断是进行的拖动.还是缩放动作 package com.bi.xintest; import android.cont ...
- Nginx-->基础-->排错-->nginx错误总结
一.启动时错误 1.错误提示: 2016/11/16 17:36:41 [emerg] 2458#2458: getpwnam("nginx") failed 查看错误日志文件内容 ...
- JSONP跨域的原理解析( 一种脚本注入行为)
JavaScript是一种在Web开发中经常使用的前端动态脚本技术.在JavaScript中,有一个很重要的安全性限制, 被称为“some-Origin Policy”(同源策略).这一策略对于Jav ...
- composer 安装
安装composer wget http://curl.haxx.se/ca/cacert.pem curl -sS https://getcomposer.org/installer | php - ...
- 异步I/O操作
今天在看boost库的时候注意到异步I/O操作时,缓冲区有效性问题. 如何实现异步操作:以异步读操作为例async_read(buffer, handler): void handler() {} v ...
- 使用winpcap多线程抓包,以及简单的分析数据包
刚开始使用winpcap数据包的时候,我在抓包的时候使用了 pcap_loop(adhandle, 0, packet_handler, NULL); 这个回调函数进行抓包.同时在回调函数中分析IP地 ...
- [linux] 结构化命令-for
1 for命令 # for:迭代循环:默认空格为分隔符 for var in list do commands done 1.1 读取列表中的值 #!usr/bin/bash for test in ...
- 关于 Uncaught (in promise) DOMException: The play() request was interrupted by a call to pause() 错误
最近在做项目的时候发现一个如题的控制台报错. 一看右侧的报错文件是undefined 这下苦恼了,定位不到问题所在. 今天解决了这个问题,就来分享一下. 问题的关键所在是在执行了play()方法以后立 ...
- Android Webview 调用JS跳转到指定activity
JAVA: WebView wv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(save ...
- HBase安装及简单使用
通过之前的hadoop0.20.2的安装并调试成功,接下来我们继续安装hbase0.90.5.在安装hbase0.90.5之前,因为hbase0.90.5只支持jdk1.6,所以,我把之前的jdk1. ...