当前位置:网站首页>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;
}
边栏推荐
- Sqlserver multithreaded query problem
- FPGA课程:JESD204B的应用场景(干货分享)
- Master-slave replication principle of MySQL
- Initial experience of addresssanitizer Technology
- Jetpack compose is much more than a UI framework~
- This article introduces you to the characteristics, purposes and basic function examples of static routing
- How to model and simulate the target robot [mathematical / control significance]
- How can brand e-commerce grow against the trend? See the future here!
- Stack and queue-p79-10 [2014 unified examination real question]
- MySQL binlog related commands
猜你喜欢

FPGA课程:JESD204B的应用场景(干货分享)

MOS管参数μCox得到的一种方法

LVS+Keepalived(DR模式)学习笔记

After the promotion, sales volume and flow are both. Is it really easy to relax?

Master-slave replication principle of MySQL

MySQL view bin log and recover data

ANR 原理及实践

Jetpack Compose 远不止是一个UI框架这么简单~

途家、木鸟、美团……民宿暑期战事将起

2018年江苏省职业院校技能大赛高职组“信息安全管理与评估”赛项任务书
随机推荐
MATLAB小技巧(30)非线性拟合 lsqcurefit
. Net 5 fluentftp connection FTP failure problem: this operation is only allowed using a successfully authenticated context
化工园区危化品企业安全风险智能化管控平台建设四大目标
ip地址那点事
企業如何進行數據治理?分享數據治理4個方面的經驗總結
ViewModelProvider.of 过时方法解决
JWT的基础介绍
多线程与高并发(9)——AQS其他同步组件(Semaphore、ReentrantReadWriteLock、Exchanger)
Under what circumstances should we consider sub database and sub table
AddressSanitizer 技术初体验
How can flinksql calculate the difference between a field before and after update when docking with CDC?
栈题目:有效括号的嵌套深度
How to install swoole under window
Anr principle and Practice
Prime partner of Huawei machine test questions
After the promotion, sales volume and flow are both. Is it really easy to relax?
JESD204B时钟网络
Several index utilization of joint index ABC
Take you to brush (niuke.com) C language hundred questions (the first day)
2022年全国所有A级景区数据(13604条)