Leetcode 55. Jump Game

55. Jump Game(跳跃游戏)三种解法(C++ & 注释) 55. Jump Game(跳跃游戏)1. 题目描述2.2.1 解题思路2.2 实例代码2.2.1 解题思路2.2 实例代码2.2.1 解题思路2.2 实例代码2. 参考资料 1. 题目描述 给定一个非负整数数组,你最初位于数组的第一个位置。 数组中的每个元素代表你在该位置可以跳跃的最大长度。 判断你是否能够到达最后一个位置。 示例 1: 输入: [2,3,1,1,4] 输出: true 解释: 我们可以先跳 1 步,从位置 0 到达 位置 1, 然后再从位置 1 跳 3步到达最后一个位置。 示例 2 阅读详情

55. Jump Game

Given an array of non-negative integers, you are initially positioned at the first index of the array.

Each element in the array represents your maximum jump length at that position.

Determine if you are able to reach the last index.

For example:
A = [2,3,1,1,4], return true.

A = [3,2,1,0,4], return false. 


题目链接:https://leetcode.com/problems/jump-game/description/

题目大意:给定一个数组,每个数字代表当前可向后走的最大步数。从第一个数字开始,求是否能走到最后一个数字位置,若能,返回true,否则返回false。

解题思路:纪录当前能走的最大步数,即从前一步的最大步数减一和当前下标对应步数中选择最大的一个,如果最大步数非零,向前走一步,直到走到最后一个数字。

代码如下:

class Solution {
public:
    bool canJump(vector<int>& nums) {
        int n = nums.size();
        int i = 0;
        int cur = nums[0];
        while(i < n - 1){
            cur = max(cur - 1,nums[i]);
            if(cur >= 1)
                i ++;
            else if(cur == 0 && i <= n-2){
                return false;
            }
        }
        return true;
    }
};




55.跳跃游戏(Jump Game) 题目描述 给定一个非负整数数组,你最初位于数组的第一个位置。 数组中的每个元素代表你在该位置可以跳跃的最大长度。 判断你是否能够到达最后一个位置。 示例 1: 输入: [2,3,1,1,4] 输出: true 解释: 从位置 0 到 1 跳 1 步, 然后跳 3 步到达最后一个位置。 示例 2: 输入: [3,2,1,0,4] 输出: false 解释: 无论怎样,你总会到达索引为 3 的位... 阅读详情

相关推荐

动态规划系列(三):LeetCode 55. Jump Game(青蛙跳跃游戏)

LeetCode 55. Jump Game(青蛙跳跃游戏)

坚持在Android领域深耕、突破、创新。 1118

LeetCode 55. 跳跃游戏 Jump Game(C语言)

题目描述: 给定一个非负整数数组,你最初位于数组的第一个位置。 数组中的每个元素代表你在该位置可以跳跃的最大长度。 判断你是否能够到达最后一个位置。 示例 1: 输入: [2,3,1,1,4] 输出: true 解释: 从位置 0 到 1 跳 1 步, 然后跳 3 步到达最后一个位置。 示例 2: 输入: [3,2,1,0,4] 输出: false 解释: 无论怎样,你总会到达索引为 3 的...

hang404的博客 970

fuxuemingzhu#Leetcode-Solution-All#55. Jump Game 跳跃游戏1

1.贪心算法中,作出的每步贪心决策都无法改变,因为贪心策略是由上一步的最优解推导下一步的最优解,而上一步之前的最优解则不作保留 2.由(1)中的介绍,可以知道贪

55. Jump Game(跳跃游戏)

这道题也是动态规划经典题,简单的说,可以从最后开始往回跳这个思路入手。首先要des变量保存为最后需要跳到的位置,然后往回一步一步跳,若在某个位置可以跳到最后的des位置,即往回跳,同时更新des为当前位置的值。 class Solution { public: bool canJump(vector& nums) { int l = nums.size();

BetweenRain的博客 600

在VM虚拟机的MobaXterm下安装JDK17

本文介绍了在Linux系统下安装配置JDK17的完整步骤:1)从Oracle或OpenJDK官网下载Linux版JDK17压缩包;2)上传至/usr/local目录;3)解压安装包;4)通过编辑/etc/profile文件配置环境变量,添加JAVA_HOME和PATH路径;5)使用source命令使配置生效;6)通过javac -version验证安装是否成功。整个过程涵盖了从下载到环境配置的完整流程,确保Java开发环境正确设置。

qq_2937655951的博客 450

RK3588s Camera ISP调优详解(一)ISP调试工具及环境配置(附下载途径)

本文主要介绍了rkisp tuner工具的连接与使用,并附上了工具及依赖库的下载地址

zongheng_raobo的博客 6328

Leetcode55. Jump Game

Leetcode55. Jump Game Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that posi...

magic_jiayu的博客 175

论文阅读:Training language models to follow instructions with human feedback

这篇论文的目标是通过引入一种基于人类反馈的训练方法,来让语言模型更好地按照用户意图行事。这是因为大规模语言模型虽然强大,但有时候会生成不准确、有偏见或者对用户无帮助的内容,这说明它们和用户的需求之间存在一定的“错位”。论文提出了一个新方法,将 GPT-3 进行优化,称为示例数据与监督学习:让人类标注员生成一些正确行为的示例,并使用这些示例来初步优化模型。比较数据与奖励模型:让标注员比较模型生成的多个输出,并将其排序,从中学到模型输出的优劣规则。强化学习优化:基于这些规则,使用强化学习算法进一步优化模型。

CSPhD-winston的博客 1734

【LeetCode】解题55:Jump Game

LeetCode解题 55:Jump Game Problem 55: Jump Game [Medium]解题思路Solution (Java) Problem 55: Jump Game [Medium] Given an array of non-negative integers, you are initially positioned at the first index of the...

Fayedily的博客 289

Android 15.0 系统开机屏幕设置默认横屏显示

在15.0的系统ROM产品定制化开发中,对于产品需求来说,由于是宽屏设备所以产品需要开机默认横屏显示,开机横屏显示这就需要从 两部分来实现,一部分是系统开机动画横屏显示,另一部分是系统屏幕显示横屏显示,从这两方面就可以做到开机默认横屏显示了

安卓兼职framework和app工程师的博客 726

微信小程序 - 完美解决“code“:“NO_AUTH“,“message“:“商户号该产品权限未开通,请前往商户平台>产品中心检查后重试“(微信小程序报错问题排查解决)

微信小程序NO_AUTH: 商户号该产品权限未开通,请前往商户平台>产品中心检查后重试?微信小程序提示商户号该产品权限未开通,请前往商户平台>产品中心检查后重试,微信小程序"code":"NO_AUTH"错误码怎么解决,报错信息如下:httpResponseBody[{"code":"NO_AUTH","message":"商户号该产品权限未开通,请前往商户平台>产品中心检查后重试"}],微信小程序支付问题,如何解决这个问题,虚拟支付商户号,虚拟支付商户号里面没有产品支付的页面,排查商户号没问题,商户号配置

🏆 前端领域优质创作者 4470

leetcode解题之45. Jump Game II&55. Jump Game java (跳跃游戏)

leetcode解题之45. Jump Game II&55. Jump Game java (跳跃游戏)。动态规划,贪心。

mine_song的博客 3721

工业缺陷检测:基于深度学习的表面缺陷分类

工业缺陷检测:基于深度学习的表面缺陷分类 ,人工智能,计算机视觉,大模型,AI,在现代工业生产中,产品质量直接影响企业的竞争力和声誉,工业缺陷检测作为质量把控的重要环节,其技术的发展至关重要。传统的人工检测方式效率低、成本高且易受主观因素影响,难以满足大规模、高精度的生产需求。随着深度学习技术的蓬勃发展,基于深度学习的工业表面缺陷分类方法凭借强大的特征提取和模式识别能力,成为解决工业缺陷检测难题的有效途径。本文将深入探讨基于深度学习的工业表面缺陷分类技术,结合详细的代码示例和概念解析。

不踩坑,不知道坑有多深;不总结,踩过的坑白踩。 3万+

Leetcode 55. Jump Game dp

Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Determine if yo...

salmonwilliam的博客 529

2026最新版OpenClaw安装:Windows WSL2从零到跑通

Windows WSL2安装OpenClaw指南 本文详细介绍了在Windows系统上通过WSL2部署OpenClaw AI助理的完整流程。WSL2结合了Windows易用性和Linux高性能,特别适合Windows用户、开发者、学生和技术爱好者。文章涵盖硬件要求(推荐i5/16GB/SSD)、系统准备(Windows 10/11+WSL2)、环境部署(Python/Node.js/Redis)和OpenClaw配置(克隆/编译/初始化)等关键步骤。通过WSL2,用户可在保留Windows操作习惯的同

一键难忘的博客 2349

深度学习工具包 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")

leetcode 55 Jump Game 详细解答

leetcode 55 Jump Game 详细解答 解答1: 此题需要从后往前考虑, 需要初始化一个额外的数组dp,用以保存下标 i 是否能够到达数组nums最后的下标,如果能到达, dp[ i ] = True 如果不能到达,dp[ i ] = False 所以 示例1中的数组dp就是 [True, True, True, True] 示例2中的数组dp就是 [False, False, False, False, True] 如何判断下标 i 是否能到达数组nums最后的下标呢? 情况1: 如

qq_22210659的博客 303

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

LeetCode: 55. Jump Game(Medium)

1. 原题链接 https://leetcode.com/problems/jump-game/description/ 2. 题目要求 给定一个整型数组,数组中没有负数。从第一个元素开始,每个元素的值代表每一次你能从当前位置跳跃的步数。问能否跳到该数组的最后一个元素位置 注意:可以跳的步数超出数组长度依旧视为可以达到最后位置 3. 解题思路 从第一个元素开始遍历,记录下你所...

aysk1112的博客 144

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

LeetCode刷题:55. Jump Game 跳跃游戏

LeetCode刷题:55. Jump Game 跳跃游戏 原题链接:https://leetcode.com/problems/jump-game/ Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in th...

梅森上校的博客 业精于勤荒于嬉,形成于思毁于随。 470

LeetCode 55 --- Jump Game

题目链接: LeetCode 55 — Jump GameAC代码:public class Problem55 { public static void main(String[] args) { // TODO Auto-generated method stub } public boolean canJump(int[] nums) {

调包侠 306

LeetCode 55: Jump Game

LeetCode 55: Jump Game算法一思路(1)假定用start出发,用next记录从[start + 1, end]出发能到达的最远距离; (2)更新start和end; (3)如果next和end相等,表明不能继续,返回false;代码public class Solution { public boolean canJump(int[] nums) {

u010759408的博客 343

LeetCode 55. Jump Game

LeetCode 55. Jump GameDescription: Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump l

My Blog 233

【LeetCode 45 55 1306】Jump Game I,II,III【JAVA】

跳跃游戏,合计三道题。 55. Jump Game 1. 题目 运行结果 45. Jump Game II 1. 题目: 运行结果 运行结果 1306. Jump Game III 1. 题目 运行结果

卤小蛋学编程 420

LeetCode:55. Jump Game

LeetCode:55. Jump Game Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that pos...

Wisimer 218

LeetCode之55. Jump Game

Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Determine if yo...

iNiBuBian的博客 358

【贪心 和 DP】LeetCode 55. Jump Game

LeetCode 55. Jump Game 貌似一直就没做几个贪心的题,是时候好好学习一个了。。 参考网址:http://www.cnblogs.com/grandyang/p/4371526.html Solution1:贪心 时间复杂度O(n)O(n)O(n),空间复杂度O(1)O(1)O(1) 贪婪算法Greedy Algorithm,因为我们并不是很关心每一个位置上的剩余步数,...

Allenlzcoder的博客 539
上一篇: Leetcode 53. Maximum Subarray
下一篇: Leetcode 51. N-Queens
雨狮子
博客等级 码龄12年 24粉丝 185原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值
⌂ 换网址