366. Fibonacci
描述
查找斐波纳契数列中第 N 个数.
所谓的斐波纳契数列是指:
- 前2个数是 0 和 1 。
- 第 i 个数是第 i-1 个数和第i-2 个数的和。
斐波纳契数列的前10个数字是:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34 ...
public class Solution {
/**
* @param n: an integer
* @return: an ineger f(n)
*/
public int fibonacci(int n) {
// write your code here
if (n == 1){
return 0;
}
int[] arr = new int[n];
arr[0] = 0;
arr[1] = 1;
for(int i=2;i<n;i++) {
arr[i] = arr[i-1] + arr[i-2];
}
return arr[n-1];
}
}
Error:递归实现斐波拉契数列超时
class Solution {
/**
* @param n: an integer
* @return: an ineger f(n)
*/
public int fibonacci(int n) {
// write your code here
if (n == 1){
return 0;
}else if(n == 2){
return 1;
}
return fibonacci(n-1) + fibonacci(n-2);
}
}
Description
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, 8, 13, 21, 34 ...
366. Fibonacci的更多相关文章
- LintCode 366 Fibonacci
/* 1st method will lead to time limit *//* the time complexity is exponential sicne T(n) = T(n-1) + ...
- 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 ...
- 算法与数据结构(九) 查找表的顺序查找、折半查找、插值查找以及Fibonacci查找
今天这篇博客就聊聊几种常见的查找算法,当然本篇博客只是涉及了部分查找算法,接下来的几篇博客中都将会介绍关于查找的相关内容.本篇博客主要介绍查找表的顺序查找.折半查找.插值查找以及Fibonacci查找 ...
- #26 fibonacci seqs
Difficulty: Easy Topic: Fibonacci seqs Write a function which returns the first X fibonacci numbers. ...
- 关于java的递归写法,经典的Fibonacci数的问题
经典的Fibonacci数的问题 主要想展示一下迭代与递归,以及尾递归的三种写法,以及他们各自的时间性能. public class Fibonacci { /*迭代*/ public static ...
- 斐波拉契数列(Fibonacci) 的python实现方式
第一种:利用for循环 利用for循环时,不涉及到函数,但是这种方法对我种小小白来说比较好理解,一涉及到函数就比较抽象了... >>> fibs = [0,1] >>&g ...
- Codeforces Round #366 (Div. 2) ABC
Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...
随机推荐
- algorithm与numeric的一些常用函数
numeric中的accumulated的基本用法: 来自:https://blog.csdn.net/u011499425/article/details/52756242 #include < ...
- java测试
//信1705-1 20173527 刘津鑫package money;import java.io.IOException;import java.io.Serializable;import ja ...
- API接口加密方式说明
标签: 接口 2016年10月11日 19:41:20 13299人阅读 评论(0) 收藏 举报 分类: API(5) 版权声明:本文为博主原创文章,未经博主允许不得转载. http://blog ...
- docker批量删除none镜像
1.直接用docker images | grep none | awk ‘{print $3}’ |xgars docker rmi 通过关键字搜索,得到docker id,进行删除
- 步步为营-81-HttpModule(再谈Session)
说明:session用于记录数据信息并存放在服务器内存中,但是存在一些问题.例如当使用服务器集群是会出现session丢失等情况.虽然微软提供了一些解决方案(Session进程外存储,或者存到数据库中 ...
- Python os.walk() 方法遍历文件目录
概述 os.walk() 方法用于通过在目录树中游走输出在目录中的文件名,向上或者向下. os.walk() 方法是一个简单易用的文件.目录遍历器,可以帮助我们高效的处理文件.目录方面的事情. 在Un ...
- 史上最简单的 SpringCloud 教程 | 第一篇: 服务的注册与发现(Eureka)
一.spring cloud简介 spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理.服务发现.断路器.路由.微代理.事件总线.全局锁.决策竞选.分布式会话等等.它运 ...
- CMD批处理——forfiles命令使用,自动删除过期备份文件
公司服务器用来备份数据的硬盘过段时间就会被备份文件占满,弄得我老是要登录到服务器去手工删除那些老的文件,有时忘记了就会导致硬盘空间不足而无法备份.因为只要保留最近几天的备份,如果可以做一个批处理让系统 ...
- C#接口和泛型类
1.定义: 定义为一个约束,实现接口的类或者结构必须遵守该约定.借口是类之间交互的一个协议.定义了类之间的交互标准. 接口是类之间相互交互的一个抽象,把类之间需要交互的内容抽象出来定义成接口. 接口只 ...
- tomcat启动成功但是访问方面都是404
1.开发环境与错误情况 开发环境是jdk1.7+maven+git+spring MVC+spring+mybatis+mysql. 楼主做小例子的时候发现,tomcat成功启动,但是访问主页,页面提 ...