LeetCode 509. 斐波那契数

Leetcode 509. 斐波那契数 斐波那契数 (通常用 F(n) 表示)形成的序列称为 斐波那契数列 。该数列由 0 和 1 开始,后面的每一项数字都是前面两项数字的和。也就是: 给定 n ,请计算 F(n) 。示例 1: 示例 2: 示例 3: 提示:Code: 阅读详情

LeetCode 509. 斐波那契数

题目描述

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

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

给定 n ,请计算 F(n) 。

输入:n = 2
输出:1
解释:F(2) = F(1) + F(0) = 1 + 0 = 1

斐波那契数

一、解题关键词

1、递归调用

二、解题报告

1.思路分析

2.时间复杂度

3.代码示例

tips:

class Solution {
    public static int fib(int n) {
//        递归次数
        if (n < 2){
            return n;
        }
        int p = 0,q = 0, r = 1;
        for (int i = 2; i <= n; i++) {
            p = q;
            q = r;
            r = p +q;
        }
        return r;
    }
}

2.知识点



总结

OPC与AI智能体集成实战:一人公司的技术栈构建与避坑指南 1. 先搞清楚“一人公司”和“AI工具”到底在解决什么问题 最近看到不少关于“裸辞搞OPC一人公司”和“AI时代骗局”的讨论,结合搜索热词里高频出现的 openclaw 、 AI编程 、 OPC ,我觉得有必要把这里面的水搅一搅,说点实在的。 首先得拆开看。标题里混了三个东西: 一人公司 、 OPC/OpenClaw 、 AI培训 。它们被放在一起讨论“骗局”,但本质上解决的是不同层面的问题。 一人公司 ,或者说个人工作室、独立开发者模式,核心是 用最小化团队(甚至一个人)去承接项目、开发产品或提供服务 。 阅读详情

相关推荐

均匀分布产生高斯分布

均匀分布产生高斯分布 文章目录均匀分布产生高斯分布简介方法和证明代码效果引用 简介 方法和证明 代码 效果 引用

gy_Rick的博客 5317

Leetcode 509. 斐波那契数 C++

Leetcode 509. 斐波那契数 题目 斐波那契数,通常用 F(n) 表示,形成的序列称为斐波那契数列。该数列由 0 和 1 开始,后面的每一项数字都是前面两项数字的和。也就是: F(0) = 0, F(1) = 1 F(N) = F(N - 1) + F(N - 2), 其中 N > 1. 给定 N,计算 F(N)。 测试样例 示例 1: 输入:2 输出:1 解释:F(2) = F(1) + F(0) = 1 + 0 = 1. 示例 2: 输入:3 输出:2 解释:F(3) = F(2

weixin_43750513的博客 220

Deepface实战避坑:人脸识别模型VGG-Face、Facenet、ArcFace怎么选?附各模型性能与速度实测对比

本文深入对比了DeepFace框架中VGG-Face、Facenet和ArcFace三种主流人脸识别模型的性能与适用场景,提供了详细的实测数据和选型建议。通过分析各模型在CPU/GPU环境下的推理速度、内存占用和准确率表现,帮助开发者根据实际需求(如门禁系统、相册分类或视频流分析)选择最优模型,并给出工程化部署的避坑指南和优化技巧。

weixin_42625070的博客 305

LeetCode509. 斐波那契数

LeetCode509. 斐波那契数 斐波那契数,通常用 F(n) 表示,形成的序列称为斐波那契数列。该数列由 0 和 1 开始,后面的每一项数字都是前面两项数字的和。也就是: F(0) = 0, F(1) = 1 F(N) = F(N - 1) + F(N - 2), 其中 N > 1. 给定 N,计算 F(N)。 来源:力扣(LeetCode) 链接:https://leetcod...

五更依旧朝花落的博客 180

Opencv实战:答题卡自动识别与评分系统(附完整代码解析)

本文详细介绍了如何使用OpenCV实现答题卡自动识别与评分系统,包含完整的代码解析。通过图像预处理、边缘检测、透视变换和答案识别等关键技术,系统能够快速准确地完成阅卷工作,适用于学校考试、资格认证等多种场景,大幅提升工作效率。

weixin_33737134的博客 415

TCP/IP协议栈之LwIP(三)---网际寻址与路由(IPv4 + ARP + IPv6)

一、网际寻址原理 当网络设备地址总数并不是很多的情况下,有了唯一地址(比如MAC地址)就可以定位相互通信的主体。然而,当地址总数越来越多时,如何高效地从中找出通信的目标地址将成为一个重要的问题。为此人们发现地址除了具有唯一性还需要具有层次性。MAC地址虽然具有唯一性,但没有层次性,也即当网络设备增加到一定程度,需要将网络分成多个数据链路时,MAC地址无法跨链路寻址,是真正负责最终通信的地址。要实现跨局域网的网际寻址,就要靠具有层次性的网络IP地址实现了。

流云 6820

golang力扣leetcode 509.斐波那契数

509.斐波那契数509.斐波那契数题解代码 509.斐波那契数 509.斐波那契数 题解 题目是简单,这里用map记忆化,节省时间 代码 package main var mp map[int]int = make(map[int]int) func fib(n int) int { return dfs(n) } func dfs(n int) int { if n < 2 { return n } if mp[n] != 0 { return mp[n] } mp[n]

清风 423

FPGA实战:手把手教你用Verilog实现Modbus CRC16校验(附完整代码)

本文详细介绍了在FPGA中实现Modbus RTU协议CRC16校验模块的完整实战方案。针对硬件设计中的时序、资源与性能挑战,文章从算法原理出发,提供了清晰的Verilog模块接口、状态机设计、查表ROM实现及优化策略,并附带了完整的可综合代码与测试平台,帮助工程师高效构建稳定可靠的工业通信系统。

weixin_29164185的博客 433

Leetcode509. 斐波那契数(GIF图解)

Leetcode509.斐波那契数 斐波那契数,通常用 F(n) 表示,形成的序列称为斐波那契数列。该数列由 0 和 1 开始,后面的每一项数字都是前面两项数字的和。也就是: F(0) = 0, F(1) = 1 F(N) = F(N - 1) + F(N - 2), 其中 N > 1. 给定 N,计算 F(N)。 **示例 1**: 输入:2 输出:1 解释:F(2) = F(1) +...

weixin_43294413的博客 1066

RTDETR多模态融合实战:基于注意力机制的红外与可见光目标检测优化

本文深入探讨了基于RTDETR模型的红外与可见光多模态目标检测优化方案。针对复杂光照下的检测难题,文章提出并实战演示了如何利用注意力机制(如SimAM与交叉注意力)实现RGB与IR特征的智能动态融合,从而构建更鲁棒的“全天候”检测系统,有效提升安防、自动驾驶等场景在夜间及恶劣天气下的检测精度与可靠性。

weixin_29083373的博客 188

ZeroClaw安装、使用方法详细全解

ZeroClaw 是一个用 Rust 编写的轻量级、高性能、完全自主的 AI 助手运行时框架

洛星核的博客 618

【LeetCode】 509 斐波那契数

存在递推关系的可以考虑动态规划。动态规划的空间复杂度的降阶优化可以通过滚动窗口动态记录参与递推的前项值来实现。

weixin_44961802的博客 490

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

#力扣 LeetCode509. 斐波那契数 @FDDLC

题目描述: https://leetcode-cn.com/problems/fibonacci-number/ Java代码: class Solution { //0 ≤ N ≤ 30 public int fib(int n) { return n<2?n:fib(n-1)+fib(n-2); } } Java代码二:数组 class Solution { //0 ≤ N ≤ 30 public int fib(int n) { if(

LiuXingchang的博客 152

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

力扣(LeetCode) 509. 斐波那契数

斐波那契数,通常用 F(n) 表示,形成的序列称为斐波那契数列。该数列由 0 和 1 开始,后面的每一项数字都是前面两项数字的和。也就是: F(0) = 0, F(1) = 1 F(N) = F(N - 1) + F(N - 2), 其中 N > 1. 给定 N,计算 F(N)。 示例 1: 输入:2 输出:1 解释:F(2) = F(1) + F(0) = 1 + 0 = 1. 示例 2:...

weixin_30840573的博客 130

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

力扣 leetcode 509. 斐波那契数(python)

Topic: 斐波那契数,通常用 F(n) 表示,形成的序列称为 斐波那契数列 。该数列由 0 和 1 开始,后面的每一项数字都是前面两项数字的和。也就是: F(0) = 0,F(1) = 1 F(n) = F(n - 1) + F(n - 2),其中 n > 1 给你 n ,请计算 F(n) 。 Example_1: 输入:2 输出:1 解释:F(2) = F(1) + F(0) = 1 + 0 = 1 Example_2: 输入:3 输出:2 解释:F(3) = F(2) + F(1)

weixin_50791900的博客 313

LeetCode(力扣)509. 斐波那契数Python

【代码】LeetCode(力扣)509. 斐波那契数Python。

qq_44953660的博客 280

509. 斐波那契数、Leetcode的Go实现

博客主页:🏆看看是李XX还是李歘歘🏆 🌺每天分享一些包括但不限于计算机基础、算法等相关的知识点🌺 💗点关注不迷路,总有一些📖知识点📖是你想要的💗 ⛽️今天的内容是Leetcode509. 斐波那契数⛽️💻💻💻 509. 斐波那契数 斐波那契数 (通常用 F(n) 表示)形成的序列称为 斐波那契数列 。该数列由 0 和 1 开始,后面的每一项数字都是前面两项数字的和。也就是: F(0) = 0,F(1)= 1 F(n) = F(n - 1) + F(n - 2),其中 n &g...

分享包括但不限于计算机基础知识、数据结构与算法、Golang技术栈。年与时驰,意与日去,遂成枯落,多不接世,悲守穷庐,将复何及! 588

LeetCode.509.斐波那契数

LeetCode.509.斐波那契数 难度:easy 两种方法动态规划方法和递归的方法: Java: 动态规划: 确定dp数组及下标位置:dp[i]的定义为:第i个数的斐波那契数值是dp[i] 递推公式: dp[i] = dp[i-1] +dp [i-2] dp数组初始化: dp[0] = 0; dp[1] = 1; 遍历顺序:从前往后 举例推导dp数组 //非压缩状态的版本 class Solution { public ...

超级马里奥 的博客 312

Leetcode—509.斐波那契数【简单】

运行结果 之后我会持续更新,如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持!

Atticus的博客 381

【LeetCode 509. 斐波那契数】

LeetCode 509. 斐波那契数 题目描述 解题思路 一、迭代 class Solution { public int fib(int n) { if(n == 0 || n == 1) return n; //dp[i]: 第i个斐波拉契数 dp[i] = dp[i-1] + dp[i-2] int[] dp = new int[n + 1]; dp[1] = 1; for(int i = 2; i <

QSilenceH的博客 546

【力扣 简单 C】509. 斐波那契数

C语言刷题

2503_92320911的博客 508

LeetCode 509. 斐波那契数(简单)

509. 斐波那契数 难度简单229收藏分享切换为英文接收动态反馈 斐波那契数,通常用F(n)表示,形成的序列称为斐波那契数列。该数列由0和1开始,后面的每一项数字都是前面两项数字的和。也就是: F(0) = 0,F(1)= 1 F(n) = F(n - 1) + F(n - 2),其中 n > 1 给你n,请计算F(n)。 示例 1: 输入:2 输出:1 解释:F(2) = F(1) + F(0) = 1 + 0 = 1 示例 2: 输入:3 输出...

VictorKa的博客 308

LeetCode_509.斐波那契数

LeetCode-cn_509 509.斐波那契数 斐波那契数,通常用F(n) 表示,形成的序列称为斐波那契数列。该数列由0 和 1 开始,后面的每一项数字都是前面两项数字的和。也就是: F(0) = 0, F(1)= 1 F(N) = F(N - 1) + F(N - 2), 其中 N > 1. 给定N,计算F(N)。 示例 1: 输入:2 输出:1 解释:F(2) =...

weixin_30532369的博客 181

LeetCode 509. 斐波那契数 三种方法 java实现

LeetCode 509. 斐波那契数 题目描述 LeetCode页面 方法一:常规方法 class Solution { public int fib(int n) { int res = 0; if (n < 2) { res = n; } else { res = fib(n - 1) + fib(n - 2); } return res; } } 方法二:滚动数组 参考于LeetCode官方题解 class Solution3

sinat_51673411的博客 1161
上一篇: LeetCode 70. 爬楼梯
下一篇: LeetCode 1137. 第 N 个泰波那契数
大涛小先生
博客等级 码龄8年 1粉丝 273原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大涛小先生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值