Fibonacci Number LT509
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.
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的更多相关文章
- Buge's Fibonacci Number Problem
Buge's Fibonacci Number Problem Description snowingsea is having Buge’s discrete mathematics lesson, ...
- [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_ ...
- [UCSD白板题 ]Small Fibonacci Number
Problem Introduction The Fibonacci numbers are defined as follows: \(F_0=0\), \(F_1=1\),and \(F_i=F_ ...
- (斐波那契总结)Write a method to generate the nth Fibonacci number (CC150 8.1)
根据CC150的解决方式和Introduction to Java programming总结: 使用了两种方式,递归和迭代 CC150提供的代码比较简洁,不过某些细节需要分析. 现在直接运行代码,输 ...
- 求四百万以内Fibonacci(number)数列偶数结果的总和
又对啦...开心~~~~ 只是代码可能不符合PEP标准什么的... Each new term in the Fibonacci sequence is generated by adding the ...
- Fibonacci number
https://github.com/Premiumlab/Python-for-Algorithms--Data-Structures--and-Interviews/blob/master/Moc ...
- Algorithms - Fibonacci Number
斐波那契数列(Fibonacci Number)从数学的角度是以递归的方法定义的: \(F_0 = 0\) \(F_1 = 1\) \(F_n = F_{n-1} + F_{n-2}\) (\(n \ ...
- 【LEETCODE】44、509. Fibonacci Number
package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...
- 【leetcode】509. Fibonacci Number
problem 509. Fibonacci Number solution1: 递归调用 class Solution { public: int fib(int N) { ) return N; ...
随机推荐
- 给tbody加垂直滚动条的具体思路
[给tbody加垂直滚动条的具体思路] 给tbody加垂直滚动条的思路就是把tbody设置成display:block,然后就对其高度设置一个固定值,overflow设置成auto即可 参考:http ...
- ChromDevTools
[ChromDevTools] 1.如何打开DevTools. 在Chrome菜单中选择 更多工具 > 开发者工具 在页面元素上右键点击,选择 “检查” 使用 快捷键 F12 2.切换 Devi ...
- 第二篇:Jmeter功能概要
一.jmeter工具组成部分: 1.资源生成器:用于生成测试过程中服务器,负载机的资源代码: 2.用户运行器:通常是一个脚本运行引擎,根据脚本的要求模拟指定用户行为,(lr中的controller) ...
- meta标签的使用
meta标签是html标记head区的一个关键标签,它位于HTML文档的<head>和<title>之间(有些也不是在<head>和<title>之间) ...
- 【Spider】使用命令行启动时,能正常抓取,但是在pycharm直接运行不能保存数据
通过cmd 运行命令scrapy crawl [爬虫名称]可以正常运行,并把数据保存到json文件和下载爬取的图片 但是在项目的spiders目录下的 firstTestSpider.py文件里添加: ...
- python写机器人玩僵尸骰子
python写机器人玩僵尸骰子由Al Sweigart用python发布注意:我正在为我的僵尸骰子模拟器寻找反馈,以及这一套指令.如果你觉得有什么地方可以改进,请发邮件到al@inventwithpy ...
- 缩点+染色+DFS codeforce467D
题目链接:https://vjudge.net/contest/219056#problem/A 推荐博客:https://blog.csdn.net/ck_boss/article/details/ ...
- Python+Selenium学习--定位一组对象
场景 从上一节的例子中可以看出,webdriver可以很方便的使用find_element方法来定位某个特定的对象,不过有时候我们却需要定位一组对象,这时候就需要使用find_elements方法. ...
- Oracle触发器用法实例详解
转自:https://www.jb51.net/article/80804.htm. 本文实例讲述了Oracle触发器用法.分享给大家供大家参考,具体如下: 一.触发器简介 触发器的定义就是说某个条件 ...
- AltiumDesigner 网络标号放置技巧
我们在使用AD画图的过程中,灵活的使用一些小技巧可以使我们的工作事半功倍,今天给大家介绍一种在画原理图过程中的小技巧. 我们在画原理图过程中经常会放置网络标号,尤其是很多芯片管脚上,例如现在我需要在这 ...