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

SSM integrates FreeMarker and common syntax

Extreme Programming -- Practice of root cause analysis
![[csp]202012-2 optimal threshold for period end forecast](/img/40/9b59bd692bcfe05d16614cc6d55d1f.png)
[csp]202012-2 optimal threshold for period end forecast

Schéma de cristallisation différentielle active et différence entre LV - PECL, LVDS et hcsl

Vant3 +ts packaged simple step advancer component

Byte flybook Human Resources Kit three sides

ESP32-C3 ESP-IDF 配置smartconfig 和 sntp 获取网络时间

USB to serial port - maximum peak serial port baud rate vs maximum continuous communication baud rate

An easy-to-use IDE for small programs

续2 asp.net core 路由程序基础使用演示0.2 默认控制器数据的获取到
随机推荐
MySQL学习笔记
Use applet to quickly generate app in seven steps
Write a select based concurrent server
LCD parameter interpretation and calculation
Lightweight and convenient small program to app technology solution to realize interconnection with wechat / traffic app
Changes in the third generation dri
vant3+ts+pinia tab选项卡列表页面点击进详情,详情页返回tab高亮在原位置,刷新高亮默认在第一项
Click the list page of vant3+ts+pinia tab to enter the details. The tab on the details page is highlighted in the original position, and the refresh highlight is in the first item by default
idea 常用快捷键
ftrace
js将数组中的0移动到末尾
High-Speed Layout Guidelines 未完...
认识函数原创
Sqlserver common statements and functions
Getting started with grpc swift
73. matrix zeroing (marking method)
联想回应笔记本太多太杂乱:现在是产品调整期 未来将分为数字/Air/ Pro三大系列
PHP implementation of infinite classification tree (recursion and Optimization)
C operation database added business data value content case school table
First principles of enterprise architecture