当前位置:网站首页>[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;
}
};
边栏推荐
猜你喜欢

Beifu and C transmit real type through ads communication

EasyGBS平台出现录像无法播放并存在RTMP重复推流现象,是什么原因?

图像处理 图像特征提取与描述

Signal integrity (SI) power integrity (PI) learning notes (32) power distribution network (4)

转行软件测试有学历要求吗?低于大专是真的没出路吗?

Beifu PLC and C transmit structure type variables through ads communication

第十天笔记

JVM详细解析

ZABBIX monitoring detailed installation to deployment

Current limiting based on redis+lua
随机推荐
Step on the electric render process renderer to solve the problem of require is not defined
倍福PLC--C#ADS通信以通知的方式读取变量
Signal integrity (SI) power integrity (PI) learning notes (32) power distribution network (4)
Numpy: element selection of matrix
Complex networks - common drawing software and libraries
Unity 模型显示到UI前面,后面的UI抖动
第十二天笔记
The relationship between method area, perpetual generation and meta space
Talk about study and work -- Qian Xuesen
Shooting lesson 1-01: Introduction
Is it safe to open an account with Guosen Securities software? Will the information be leaked?
Redis distributed lock practice
OpenCV图像处理(中) 图像平滑+直方图
TI单芯片毫米波雷达代码走读(二十五)—— 角度维(3D)处理流程
【JZOF】12矩阵中的路径
倍福和C#通过ADS通信传输Real类型
[ACTF2020 新生赛]BackupFile 1
Beifu PLC and C transmit bool array variables through ads communication
Convert the specified seconds to minutes and seconds
Count different types of data according to different times (stored procedures)