当前位置:网站首页>LeetCode_ 509 Fibonacci number
LeetCode_ 509 Fibonacci number
2022-04-21 23:28:00 【W__ winter】
1、 subject : Fibonacci number
Fibonacci number ( Usually use F(n) Express ) The sequence formed is called Fibonacci sequence . The sequence is composed of 0 and 1 Start , Each of the following numbers is the sum of the first two numbers . That is to say :
F(0) = 0,F(1) = 1
F(n) = F(n - 1) + F(n - 2), among n > 1
Given n , Please calculate F(n) .

2、 Their thinking
Five steps of dynamic planning
1、 determine dp The meaning of arrays and subscripts , The first i The Fibonacci value of the number is dp[i]
2、 Determine the recurrence formula , State transition equation dp[i] = dp[i - 1] + dp[i - 2];
3、dp How to initialize an array , How to initialize is also directly given to us in the topic
dp[0] = 0;
dp[1] = 1;
4、 Determine the traversal order , From recursive formula dp[i] = dp[i - 1] + dp[i - 2]; It can be seen that ,dp[i] It's dependence dp[i - 1] and dp[i - 2], Then the traversal order must be from front to back
5、 Give an example to deduce dp Array , According to this recurrence formula dp[i] = dp[i - 1] + dp[i - 2], Let's deduce , When N by 10 When ,dp The array should be the following sequence :0 1 1 2 3 5 8 13 21 34 55
3、 Code
class Solution {
public:
int fib(int N) {
if (N <= 1) return N;
vector<int> dp(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];
}
};
版权声明
本文为[W__ winter]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204212006528584.html
边栏推荐
- 6. Hiérarchie des protocoles et modèle de service (clé)
- 【ACM】46. Full Permutation (1. Here, the previous elements need to be used for permutation, so StartIndex is not used (only for combination and division); 2. Pay attention to whether the elements in t
- idea 解决项目包出现[wrapper(1)]
- Technology, products and brand are not problems. For SAIC Audi, these two points may be life and death
- 格局要大与视野要广,秉持人道主义精神【持续更新中,勿删】
- [express agent operation] how to do cross-border e-commerce in 2022? Express will focus on three things this year
- 【FFmpeg】命令行
- 341-Linux 连接数据库
- Simple and easy to collect navigation website source code
- uni-app 图片适配 动态计算图片高度
猜你喜欢

Single step analysis of double linked list insertion and deletion (14)

Mobile app Games / software / resource download station / software box source code

GIC spec之ITS和LPI中断5

Electronic address book management system based on C

Vs2019 configuring opencv4

云原生架构下的微服务选型和演进

BUUCTF 刷题记录

339-Leetcode 单词规律

leetcode:386. 字典序排数

6. Hiérarchie des protocoles et modèle de service (clé)
随机推荐
C language topic 1: three digits that can be composed of 1,2,3,4
Ruixin microchip AI part development record section 1 "PC side environment construction 2"
【转载】Postman-Omysql连接数据库
【acwing】1125. 牛的旅行***(floyd)
Buuctf, you drove me away
如何构建一个可“持续演进”的可观测体系?| QCon
MySQL Chapter 3 basic SQL syntax
Buuctf question brushing record
BUUCTF 刷题记录
6、协议层次化和服务模型(重点)
The three secret softwares of the leaders are practical and powerful, which are powerful tools for office efficiency and workplace promotion
File operation and IO
【速卖通代运营】2022跨境电商怎么做?速卖通今年重点要做三件事
TextView 倾斜属性
红星美凯龙阵痛:“挥刀“降杠杆、净利率腰斩
87 r K-means, hierarchical clustering, implementation of EM clustering
Chapter 2 installation of MySQL database
叹为观止,4款惊喜满满的高质量软件,使用起来倍感舒适
Selection and evolution of microservices under cloud native architecture
Changsha good man