当前位置:网站首页>leetcode 509. Fibonacci number
leetcode 509. Fibonacci number
2022-07-07 07:06:00 【Blue feather birds】
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: n = 2
Output: 1
Explanation: F(2) = F(1) + F(0) = 1 + 0 = 1.
Example 2:
Input: n = 3
Output: 2
Explanation: F(3) = F(2) + F(1) = 1 + 1 = 2.
The famous Fibonacci Numbers ,F(0)=0, F(1)=1,
Followed by the sum of the first two numbers , Please n individual Fibonacci Numbers .
Ideas :
DP
It can be recorded with a one-dimensional array 0~n individual Fibonacci Numbers , Then the first i One number is F(i-2)+F(i-1),
But because only the first two are used , So just save it with two numbers .
public int fib(int n) {
if(n == 0) return 0;
if(n == 1) return 1;
int n1 = 0;
int n2 = 1;
for(int i = 2; i <= n; i ++) {
int tmp = n1 + n2;
n1 = n2;
n2 = tmp;
}
return n2;
}
边栏推荐
- Big coffee gathering | nextarch foundation cloud development meetup is coming
- 化工园区危化品企业安全风险智能化管控平台建设四大目标
- Config distributed configuration center
- After the promotion, sales volume and flow are both. Is it really easy to relax?
- Under what circumstances should we consider sub database and sub table
- oracle如何备份索引
- 2022年全国所有A级景区数据(13604条)
- Use of completable future
- From zero to one, I will teach you to build the "clip search by text" search service (2): 5 minutes to realize the prototype
- Mysql---- import and export & View & Index & execution plan
猜你喜欢
Jetpack Compose 远不止是一个UI框架这么简单~
Answer to the second stage of the assignment of "information security management and evaluation" of the higher vocational group of the 2018 Jiangsu Vocational College skills competition
Basic introduction of JWT
华为机试题素数伴侣
MySQL SQL的完整处理流程
[noi simulation] regional division (conclusion, structure)
Stack and queue-p78-8 [2011 unified examination true question]
Network foundation - header, encapsulation and unpacking
MOS tube parameters μ A method of Cox
Can 7-day zero foundation prove HCIA? Huawei certification system learning path sharing
随机推荐
如何给目标机器人建模并仿真【数学/控制意义】
Jetpack compose is much more than a UI framework~
2018年江苏省职业院校技能大赛高职组“信息安全管理与评估”赛项任务书
How to model and simulate the target robot [mathematical / control significance]
Jmeter 5.5版本发布说明
A slow SQL drags the whole system down
Sword finger offer high quality code
ESXI挂载移动(机械)硬盘详细教程
Sqlserver multithreaded query problem
MySQL view bin log and recover data
main函数在import语句中的特殊行为
[noi simulation] regional division (conclusion, structure)
毕业设计游戏商城
Answer to the first stage of the assignment of "information security management and evaluation" of the higher vocational group of the 2018 Jiangsu Vocational College skills competition
【NOI模拟赛】区域划分(结论,构造)
剑指offer-高质量的代码
Leetcode T1165: 日志分析
大促过后,销量与流量兼具,是否真的高枕无忧?
途家、木鸟、美团……民宿暑期战事将起
ip地址那点事