当前位置:网站首页>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;
}
边栏推荐
- libcurl返回curlcode说明
- Unity3d learning notes
- The startup of MySQL installed in RPM mode of Linux system failed
- JESD204B时钟网络
- Multithreading and high concurrency (9) -- other synchronization components of AQS (semaphore, reentrantreadwritelock, exchanger)
- What books can greatly improve programming ideas and abilities?
- 【NOI模拟赛】区域划分(结论,构造)
- Config distributed configuration center
- 栈题目:有效括号的嵌套深度
- 工具类:对象转map 驼峰转下划线 下划线转驼峰
猜你喜欢

企业如何进行数据治理?分享数据治理4个方面的经验总结

服装门店如何盈利?

Config distributed configuration center

Basic introduction of JWT

Bus message bus

Leetcode t1165: log analysis

Brand · consultation standardization

Complete process of MySQL SQL

SolidWorks GB Library (steel profile library, including aluminum profile, aluminum tube and other structures) installation and use tutorial (generating aluminum profile as an example)

Jetpack Compose 远不止是一个UI框架这么简单~
随机推荐
SolidWorks GB Library (steel profile library, including aluminum profile, aluminum tube and other structures) installation and use tutorial (generating aluminum profile as an example)
子组件传递给父组件
MySQL binlog related commands
化工园区危化品企业安全风险智能化管控平台建设四大目标
AddressSanitizer 技术初体验
Brand · consultation standardization
How DHCP router works
场馆怎么做体育培训?
偏执的非合格公司
Get the city according to IP
栈题目:有效括号的嵌套深度
2018年江苏省职业院校技能大赛高职组“信息安全管理与评估”赛项任务书
华为机试题素数伴侣
Libcurl returns curlcode description
Config distributed configuration center
Under what circumstances should we consider sub database and sub table
readonly 只读
toRefs API 与 toRef Api
一文带你了解静态路由的特点、目的及配置基本功能示例
After the promotion, sales volume and flow are both. Is it really easy to relax?