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

推荐订阅源

A
Arctic Wolf
T
Tenable Blog
T
Troy Hunt's Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Privacy & Cybersecurity Law Blog
NISL@THU
NISL@THU
Application and Cybersecurity Blog
Application and Cybersecurity Blog
H
Hacker News: Front Page
S
Secure Thoughts
AWS News Blog
AWS News Blog
L
LINUX DO - 最新话题
D
Darknet – Hacking Tools, Hacker News & Cyber Security
M
MIT News - Artificial intelligence
T
Tor Project blog
S
Schneier on Security
PCI Perspectives
PCI Perspectives
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
美团技术团队
Google DeepMind News
Google DeepMind News
V
Visual Studio Blog
爱范儿
爱范儿
Google DeepMind News
Google DeepMind News
Cyberwarzone
Cyberwarzone
T
The Exploit Database - CXSecurity.com
罗磊的独立博客
T
Threat Research - Cisco Blogs
Recent Commits to openclaw:main
Recent Commits to openclaw:main
V
V2EX
C
CXSECURITY Database RSS Feed - CXSecurity.com
Stack Overflow Blog
Stack Overflow Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
G
GRAHAM CLULEY
L
LINUX DO - 热门话题
D
Docker
J
Java Code Geeks
GbyAI
GbyAI
H
Heimdal Security Blog
The Hacker News
The Hacker News
MongoDB | Blog
MongoDB | Blog
V
Vulnerabilities – Threatpost
T
Tailwind CSS Blog
Cloudbric
Cloudbric
TaoSecurity Blog
TaoSecurity Blog
C
CERT Recently Published Vulnerability Notes
Y
Y Combinator Blog
Recorded Future
Recorded Future
Cisco Talos Blog
Cisco Talos Blog
T
Threatpost
The Register - Security
The Register - Security
Hacker News - Newest:
Hacker News - Newest: "LLM"

博客园 - 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个值即可,不需要使用数组记录全部数据。