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 ...
随机推荐
- Fatal error: Uncaught Error: Call to undefined function mysqli_connect()
sudo apt-get install php7.2-mysql //版本号肯能不一样 sudo dpkg-reconfigure php7.2-mysql sudo /etc/init.d/m ...
- Python交换a,b两个数值的三种方式
# coding:utf-8 a = 1 b = 2 # 第一种方式 # t = a # 临时存放变量值 # a = b # b = t # 第二种方式 # a = a + b # a的值已经不是原始 ...
- Jmeter-JDBC Request参数化
一.参数化 1.选择Query Type(查询类型)为Prepared Select Statement 2.写好sql 3.在Parameter Values中输入参数,多个参数用‘,’隔开 4.P ...
- Html列表分页算法
public class PageHelper { /// <summary> /// 标签 /// </summary> public string Tag { get; s ...
- spring+redis的集成,redis做缓存
1.前言 Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API.我们都知道,在日常的应用中,数据库瓶颈是最容易出现的 ...
- 深入理解javascript原型和闭包——从【自由变量】到【作用域链】
一直对闭包和变量作用域链模糊不清!!!有时都怀疑自己是不是脑袋秀逗啦还是范萌!! 先解释一下什么是“自由变量”. 在A作用域中使用的变量x,却没有在A作用域中声明(即在其他作用域中声明的),对于A作用 ...
- NPOI导出Excel帮助类
工具类 using System; using System.Collections.Generic; using System.Data; using System.IO; using System ...
- linux下几个常用软件
Ubuntu 软件包地址 https://packages.ubuntu.com/ 一. 字体 不管是雅黑还是宋体,从windows cp过来后, 直接双击打开并安装 二. Meld 可视的diff和 ...
- C# 之 4个访问修饰符和8个声明修饰符详解
一.4个访问修饰符(是添加到类.结构或成员声明的关键字) [1] Public:公有的,是类型和类型成员的访问修饰符.对其访问没有限制. [2] Internal:内部的,是类型和类型成员的访问修饰符 ...
- 使用spark操作kudu
Spark与KUDU集成支持: DDL操作(创建/删除) 本地Kudu RDD Native Kudu数据源,用于DataFrame集成 从kudu读取数据 从Kudu执行插入/更新/ upsert ...