当前位置:网站首页>JS for Fibonacci sequence
JS for Fibonacci sequence
2022-06-12 18:02:00 【Out of the autistic bird】
Fibonacci sequence
The first number is 0, The second term is 1, Each subsequent number is the sum of the first two numbers
// recursive O(2^n)
function fibonacci(n){
if(n<=0) return 0
if(n===1) return 1
return fibonacci(n-1) + fibonacci(n-2)
}

Optimize
- No recursion 、 Use the cycle
- Record intermediate results
- The time complexity is O(n)
// O(n)
function fibonacci(n){
if(n<=0) return 0
if(n===1) return 1
let n1 = 1 // Record n-1 Result
let n2 = 0 // Record n-2 Result
let res = 0
for(let i =2 ;i<=n;i++){
res = n1+n2
// Record intermediate results
n2 = n1
n1 = res
}
return res
}
Dynamic programming
- Use recursive thinking to analyze problems , Solve problems with loops
边栏推荐
- LCD parameter interpretation and calculation
- SSM integrates FreeMarker and common syntax
- An easy-to-use IDE for small programs
- Byte flybook Human Resources Kit three sides
- Message queuing MySQL tables that store message data
- Error record: illegalstateexception: optional int parameter 'XXXX' is
- Applet and app are owned at the same time? A technical scheme with both
- Window版本pytorch入门深度学习环境安装与配置
- 在同花顺开户证券安全吗
- TypeScript高级类型(二)
猜你喜欢

JDBC快速入门教程

小程序+App,低成本获客及活跃的一种技术组合思路

vant3+ts h5页面嵌套进app 与原生app通信

Variable of C #

C#的变量

Continued 2 asp Net core router basic use demonstration 0.2 acquisition of default controller data

JDBC quick start tutorial

vant3+ts 封装uploader上传图片组件

PHP implementation of infinite classification tree (recursion and Optimization)

USB转串口那些事儿—最大峰值串口波特率VS连续通信最高波特率
随机推荐
Small program +app, a low-cost and active technology combination idea
PHP implementation of infinite classification tree (recursion and Optimization)
TypeScript高级类型(二)
消息队列存储消息数据的 MySQL 表格
Is it safe to open an account in flush
Soringboot下RestTemplateConfig 配置打印请求响应日志
Recognize function originality
机器学习系列(5):朴素贝叶斯
EASYCODE template
js快速排序
An easy-to-use IDE for small programs
leetcode 300. Longest increasing subsequence
JS中的数组(含leetcode例题)<持续更新~>
Vulnhub[DC3]
C#简单介绍
Stream流注意点
js判断回文数
Tutoriel de démarrage rapide JDBC
小程序和App同时拥有?两者兼得的一种技术方案
Esp32-c3 esp-idf configuring smartconfig and SNTP to obtain network time