惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
T
The Blog of Author Tim Ferriss
S
Securelist
C
CXSECURITY Database RSS Feed - CXSecurity.com
T
Tor Project blog
S
Schneier on Security
Google DeepMind News
Google DeepMind News
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Blog — PlanetScale
Blog — PlanetScale
AWS News Blog
AWS News Blog
F
Fortinet All Blogs
C
Cisco Blogs
L
LangChain Blog
MongoDB | Blog
MongoDB | Blog
D
Docker
T
The Exploit Database - CXSecurity.com
The GitHub Blog
The GitHub Blog
月光博客
月光博客
量子位
A
Arctic Wolf
博客园 - 叶小钗
L
LINUX DO - 热门话题
J
Java Code Geeks
The Hacker News
The Hacker News
Security Latest
Security Latest
V
Vulnerabilities – Threatpost
M
MIT News - Artificial intelligence
博客园 - 【当耐特】
T
Tenable Blog
Microsoft Security Blog
Microsoft Security Blog
Recent Announcements
Recent Announcements
Simon Willison's Weblog
Simon Willison's Weblog
B
Blog
阮一峰的网络日志
阮一峰的网络日志
A
About on SuperTechFans
博客园_首页
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
Threat Research - Cisco Blogs
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Y
Y Combinator Blog
PCI Perspectives
PCI Perspectives
Hacker News: Ask HN
Hacker News: Ask HN
P
Palo Alto Networks Blog
小众软件
小众软件
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
O
OpenAI News
P
Privacy International News Feed
P
Privacy & Cybersecurity Law Blog
N
Netflix TechBlog - Medium

博客园 - Jack Niu

最简单的判断是否为IE浏览器的方法 - document.documentMode “Microsoft.Build.Tasks.Xaml.PartialClassGenerationTask“ task could not be loaded from the assembly ASP.NET Core MVC 入门到精通 - 1. 开发必备工具 (2021) ASP.NET Core MVC 入门到精通 - 3. 使用MediatR 算法 - 计算汉明距离 浏览器(Cache)的缓存逻辑(HTTP条件请求) 算法: 合并两个有序链表 前端进阶(2)使用fetch/axios时, 如何取消http请求 前端进阶(1)Web前端性能优化 2021 Top 100 C#/.NET Interview Questions And Answers 安装umi后,使用umi提示不是内部或者外部命令 解决国内不能访问github的问题 2021 .NET/dotnet Core/C# 面试题及参考答案 2021 Vue.js 面试题汇总及答案 CSS3 Flex Box 弹性盒子、弹性布局 Angular入门到精通系列教程(15)- 目录结构(工程结构)推荐 Angular入门到精通系列教程(14)- Angular 编译打包 & Docker发布 Angular入门到精通系列教程(13)- 路由守卫(Route Guards) Angular入门到精通系列教程(12)- 路由(Routing) Angular入门到精通系列教程(11)- 模块(NgModule),延迟加载模块
(算法) - 不使用递归,实现斐波那契数列
Jack Niu · 2021-04-19 · via 博客园 - Jack Niu

https://jackniu81.github.io/2021/04/18/Algorithm-Fibonacci-numbers/

1. 斐波那契数列

斐波那契数列:0, 1, 1, 2, 3, 5, 8, 13 ... ...

通常用 F(n) 表示,形成的序列称为 斐波那契数列 。该数列由 0 和 1 开始,后面的每一项数字都是前面两项数字的和。

F(0) = 0,F(1) = 1
F(n) = F(n - 1) + F(n - 2),其中 n > 1

敏捷开发时,我们估算Story Point,通常就是使用斐波那契数列。

2. 解题思路

2.1. 递归

F(n) = F(n - 1) + F(n - 2), 很简单,就不谈了。

2.2. 动态规划

动态规划的思想是,记录中间计算结果,计算后面相时,根据前面保存的结果直接计算,避免重复计算。

3. Javascript 实现

/**
 * @param {number} n
 * @return {number}
 */
var fib = function(n) {
    if(n===0)
        return 0;
    if(n===1)
        return 1;

    let arr = [0,1]
    for(let i=2;i<=n;i++){
        arr[i] = arr[i-1] + arr[i-2];
    }
    return arr[n];
};

还可以进一步优化,当前使用数组记录以及计算过的F(n),由于F(n) = F(n - 1) + F(n - 2),实际只需要记录最近的2个值即可,不需要使用数组记录全部数据。