当前位置:网站首页>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;
}
边栏推荐
- Several index utilization of joint index ABC
- MOS管参数μCox得到的一种方法
- Get the city according to IP
- How to share the same storage among multiple kubernetes clusters
- 从零到一,教你搭建「CLIP 以文搜图」搜索服务(二):5 分钟实现原型
- 2022/07/04学习记录
- unity3d学习笔记
- .net 5 FluentFTP连接FTP失败问题:This operation is only allowed using a successfully authenticated context
- How Oracle backs up indexes
- Networkx drawing and common library function coordinate drawing
猜你喜欢
MOS tube parameters μ A method of Cox
ESXI挂载移动(机械)硬盘详细教程
Take you to brush (niuke.com) C language hundred questions (the first day)
LVS+Keepalived(DR模式)学习笔记
企業如何進行數據治理?分享數據治理4個方面的經驗總結
一文带你了解静态路由的特点、目的及配置基本功能示例
How to do sports training in venues?
$refs:组件中获取元素对象或者子组件实例:
网络基础 —— 报头、封装和解包
Jetpack Compose 远不止是一个UI框架这么简单~
随机推荐
Redhat5 installing vmware tools under virtual machine
SolidWorks的GB库(钢型材库,包括铝型材、铝管等结构)安装及使用教程(生成铝型材为例)
一条慢SQL拖死整个系统
MOS管参数μCox得到的一种方法
Lvs+kept (DR mode) learning notes
. Net core accesses uncommon static file types (MIME types)
Prime partner of Huawei machine test questions
Algorithm --- bit count (kotlin)
How can flinksql calculate the difference between a field before and after update when docking with CDC?
品牌电商如何逆势增长?在这里预见未来!
How DHCP router works
MySQL binlog related commands
剑指offer-高质量的代码
[noi simulation] regional division (conclusion, structure)
Jmeter 5.5版本发布说明
oracle如何备份索引
How to install swoole under window
AddressSanitizer 技术初体验
分布式id解决方案
偏执的非合格公司