当前位置:网站首页>[jzof] 10 Fibonacci series
[jzof] 10 Fibonacci series
2022-07-23 13:19:00 【Sighed, angry】

Use an array to store the calculated value , Prevent double counting :
class Solution {
public:
int f[50]{
0};
int Fibonacci(int n) {
if (n <= 2) return 1;
if (f[n] > 0) return f[n];
return f[n] = (Fibonacci(n-1)+Fibonacci(n-2));
}
};
Dynamic programming :
class Solution {
public:
int dp[50]{
0};
int Fibonacci(int n) {
dp[1] = 1, dp[2] =1;
for (int i = 3 ; i <= n ; i ++) dp[i] = dp[i-1]+dp[i-2];
return dp[n];
}
};
Continue to optimize dynamic programming method ( Reduce space complexity ):
class Solution {
public:
int Fibonacci(int n) {
int a = 1 , b = 1 , c = 1;
for (int i = 3 ; i <= n ; i ++) {
c = a+b , a = b , b = c;
}
return c;
}
};
边栏推荐
- Opencv image processing (Part 1): geometric transformation + morphological operation
- 太空射击 Part 1: 玩家精灵和控制
- 根据不同时间统计不同类型的数据(存储过程)
- 【JZOF】10斐波那契数列
- UI自动化
- Machine learning: Li Hang - statistical learning method (II) perceptron + code implementation (primitive + dual form)
- Beifu PLC and C transmit int array type variables through ads communication
- When using fastjson to parse and assign JSON data, the order of JSON fields is inconsistent
- 使用fastjson解析以及赋予json数据时,json字段顺序不一致问题
- 虚拟内存技术的来龙去脉(上)
猜你喜欢

Beifu PLC and C transmit structure type variables through ads communication

Space shooting Part 2-3: dealing with the collision between bullets and the enemy

Shooting lesson 1-3: image Sprite

谈谈学习和工作——钱学森

How to prevent repeated payment of orders?

Matplotlib-实现常见概率分布

Count different types of data according to different times (stored procedures)

What is the reason for the failure of video playback and RTMP repeated streaming on easygbs platform?

第八天笔记

Opencv image processing (medium) image smoothing + histogram
随机推荐
射击 第 1-01 课:入门
Beifu PLC and C transmit int type variables through ads communication
[ACTF2020 新生赛]BackupFile 1
软件测试岗位饱和了?自动化测试是新一代‘offer’技能
费曼学习法(Redis总结)
ZABBIX monitoring detailed installation to deployment
Signal integrity (SI) power integrity (PI) learning notes (32) power distribution network (4)
【JZOF】08 二叉树的下一个结点
Uncaught (in promise) Neo4jError: WebSocket connection failure. Due to security constraints in your
EasyGBS平臺出現錄像無法播放並存在RTMP重複推流現象,是什麼原因?
Beihui information is 12 years old | happy birthday
【NOI模拟赛】不知是哪一道CF的论文题(概率期望,鞅的停时定理)
第十一天笔记
Desensitize data
The relationship between method area, perpetual generation and meta space
Cortex-a series processor
【离线语音专题④】安信可VC离线语音开发板二次开发语音控制LED灯
Software testing jobs saturated? Automated testing is a new generation of 'offer' skills
Beifu PLC and C # regularly refresh IO through ads communication
OpenCV图像处理(上)几何变换+形态学操作