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; ...
随机推荐
- Jetty 与 Tomcat 的比较
Tomcat 和 Jetty 都是作为一个 Servlet 引擎应用的比较广泛,可以将它们比作为中国与美国的关系,虽然 Jetty 正常成长为一个优秀的 Servlet 引擎,但是目前的 Tomcat ...
- 三层和MVC
一样写啊... 比如DAL层: C# code? 1 2 3 4 5 Public void Update(XXXXX.Models.News model) { db.Entry(XX ...
- 数据类型&分支流程控制(2)
1.数据类型 1.数据类型 局部变量:书写在方法中的变量: 全局变量:书写在类中,与方法平级的变量: -如果没有给变量赋初值 -全局变量会默认拥有一个初始值 -局部变量将没有初始值,这个时候不能使用这 ...
- cupp字典生成器使用
下载: clone git https://github.com/Mebus/cupp.git
- 解决在linux环境安装setuptools的相关错误
RuntimeError: Compression requires the (missing) zlib module 缺少zlib包 解决方案 yum install zlib yum i ...
- sqlserver自增主键
参考 https://www.cnblogs.com/michellexiaoqi/p/8031294.html 1.选中表: 2.右击鼠标,设计: 3.选中列(整数类 ...
- 【python】安装pymongo时出错
在python2.7的环境下,使用pip install pymongo安装模块报以下错误: Could not install packages due to an EnvironmentError ...
- Centos 7 下 LAMP 部署
一.介绍 LAMP is a combination of operating system and open-source software stack. The acronym of LAMP i ...
- AI图谱
- Wasserstein距离
https://blog.csdn.net/leviopku/article/details/81388306 https://blog.csdn.net/nockinonheavensdoor/ar ...