当前位置:网站首页>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
边栏推荐
- leetcode491 递增子序列
- js快速排序
- 用好IDE,研发效能提速100%
- Esp-idf adds its own components
- Queue priority of message queue practice
- Leetcode151 flipping words in strings
- ESP-IDF 添加自己的组件
- DRM driven MMAP detailed explanation: (I) preliminary knowledge
- Advanced mountain -asp Net core router basic use demo 0.1
- Use the official go Library of mongodb to operate the original mongodb
猜你喜欢

Vant3+ts encapsulates uploader upload image component

An easy-to-use IDE for small programs

TypeScript常用类型(一)

js两数之和

1.5 what is an architect (serialization)

消息队列存储消息数据的 MySQL 表格

有源差分晶振原理图以及LV-PECL、LVDS、HCSL区别

轻量、便捷的小程序转App技术方案,实现与微信/流量App互联互通

Write a select based concurrent server

Explanation of core interrupt of Godson processor
随机推荐
Array sorts in the specified order
vant3 +ts 封装简易step进步器组件
TypeScript常用类型(一)
Use the official go Library of mongodb to operate the original mongodb
LCD parameter interpretation and calculation
Channel Original
Codeforces Round #398 (Div. 2) D. Cartons of milk
leetcode 300. 最长递增子序列
Authorization in Golang ProjectUseing Casbin
低代码平台调研结果
ftrace
Gossip about the source code of redis 89
Explanation of core interrupt of Godson processor
用好IDE,研发效能提速100%
Random talk about redis source code 91
Kali2022安装armitage
leetcode 647. Palindrome substring
极限编程--根源分析实践
Vant3+ts dropdownmenu drop-down menu, multi data can be scrolled
Use applet to quickly generate app in seven steps