The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. That is,

F(0) = 0,   F(1) = 1
F(N) = F(N - 1) + F(N - 2), for N > 1.

Given N, calculate F(N).

Example 1:

Input: 2
Output: 1
Explanation: F(2) = F(1) + F(0) = 1 + 0 = 1.

Example 2:

Input: 3
Output: 2
Explanation: F(3) = F(2) + F(1) = 1 + 1 = 2.

Example 3:

Input: 4
Output: 3
Explanation: F(4) = F(3) + F(2) = 2 + 1 = 3.

Note:

0 ≤ N ≤ 30.

Idea 1. dynamic programming, 经典的入门dp,
dp[i] = dp[i-2] + dp[i-1] (i >= 2)
Time complexity: O(n)
Space complexity: O(n)
 class Solution {
public int fib(int N) {
if(N <= 1) {
return N;
} int[] dp = new int[N+1];
dp[0] = 0;
dp[1] = 1; for(int i = 2; i <= N; ++i) {
dp[i] = dp[i-1] + dp[i-2];
} return dp[N];
}
}

Idea 1.b, 从上面的公式可以看出只需要前2位dp[i-2] and dp[i-1], 可以不用array dp[].

Time complxity: O(n)

Space complexity: O(1)

 class Solution {
public int fib(int N) {
int first = 0;
int second = 1;
int result = N; for(int i = 2; i <= N; ++i) {
result = first + second;
first = second;
second = result;
} return result;
}
}

Fibonacci Number LT509的更多相关文章

  1. Buge's Fibonacci Number Problem

    Buge's Fibonacci Number Problem Description snowingsea is having Buge’s discrete mathematics lesson, ...

  2. [UCSD白板题] The Last Digit of a Large Fibonacci Number

    Problem Introduction The Fibonacci numbers are defined as follows: \(F_0=0\), \(F_1=1\),and \(F_i=F_ ...

  3. [UCSD白板题 ]Small Fibonacci Number

    Problem Introduction The Fibonacci numbers are defined as follows: \(F_0=0\), \(F_1=1\),and \(F_i=F_ ...

  4. (斐波那契总结)Write a method to generate the nth Fibonacci number (CC150 8.1)

    根据CC150的解决方式和Introduction to Java programming总结: 使用了两种方式,递归和迭代 CC150提供的代码比较简洁,不过某些细节需要分析. 现在直接运行代码,输 ...

  5. 求四百万以内Fibonacci(number)数列偶数结果的总和

    又对啦...开心~~~~ 只是代码可能不符合PEP标准什么的... Each new term in the Fibonacci sequence is generated by adding the ...

  6. Fibonacci number

    https://github.com/Premiumlab/Python-for-Algorithms--Data-Structures--and-Interviews/blob/master/Moc ...

  7. Algorithms - Fibonacci Number

    斐波那契数列(Fibonacci Number)从数学的角度是以递归的方法定义的: \(F_0 = 0\) \(F_1 = 1\) \(F_n = F_{n-1} + F_{n-2}\) (\(n \ ...

  8. 【LEETCODE】44、509. Fibonacci Number

    package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...

  9. 【leetcode】509. Fibonacci Number

    problem 509. Fibonacci Number solution1: 递归调用 class Solution { public: int fib(int N) { ) return N; ...

随机推荐

  1. target=_blank攻击

    [target=_blank攻击] 在<a>标签中加入 rel="noopener noreferrer" 来避免. 参考:https://mathiasbynens. ...

  2. Linux 学习总结(二)

    一.用户与用户组管理 1.添加用户 useradd 选项 用户名 -c 指定一段注释性描述 -d 目录,指定用户目录,若目录不存在,-m 选项可以创建目录 -g 指定用户所属用户组 -s 指定用户登陆 ...

  3. CRM销售管理功能

    联系项目project:-------是一个大的项目,比如通知开会之类 每个坐席需要分配自己的联系任务,每个联系任务,有自己的完成未完成状态.同时关联着通话记录等 销售计划----销售项目 销售流程: ...

  4. Jenkins-cli基本用法

    基本的格式为 java -jar jenkins-cli.jar [-s JENKINS_URL] command [options][args] 下面具体介绍各个命令的作用及基本使用方法 1.    ...

  5. NumPy 字节交换

    NumPy 字节交换 在几乎所有的机器上,多字节对象都被存储为连续的字节序列.字节顺序,是跨越多字节的程序对象的存储规则. 大端模式:指数据的高字节保存在内存的低地址中,而数据的低字节保存在内存的高地 ...

  6. MongoDB之Array Object的特殊操作

    相比关系型数据库,Array[1,2,3,4,5]和Object{'name':'Wjs'}是MongoDB比较特殊的类型 db.Wjs.insert({"name":" ...

  7. PHP20-challenge12

      看下eval+var_dump的妙用.   eval会执行括号中的内容:var_dump中内容是字符串时输出的模式是 string(x) "xxxx".如果他俩结合,是可以很好 ...

  8. 关于vs2010开发的ASP项目部署到XPSP2系统上出现找不到Reportviewer.XX.文件的解决方案

    尝试方法如下: 1.将webform.dll.winform.dll.common.dll三个引用直接复制到服务器的Bin目录,未解决问题,提示无法正确加载,程序及已关闭等. 2.SQLSysClrT ...

  9. 从零开始写一个npm包及上传

    最近刚好自己需要写公有npm包及上传,虽然百度上资料都能找到,但是都是比较零零碎碎的,个人就来整理下,如何从零开始写一个npm包及上传. 该篇文件只记录一个大概的流程,一些细节没有记录. tips:  ...

  10. kraken-ejs创建一个项目【学习札记】

    Keep in Touch. 保持联络. Who’s calling? 是哪一位? You did right. 你做得对. You set me up! 你出卖我! kraken-express-e ...