[LeetCode] 55. Jump Game

Python阿里面试题:给定一个非负整数数组,你最初位于数组的第一个位置。 数组中的每个元素代表你在该位置可以跳跃的最大长度。 你的目标是使用最少的跳跃次数到达数组的最后一个位置。 如题: 给定一个非负整数数组,你最初位于数组的第一个位置。 数组中的每个元素代表你在该位置可以跳跃的最大长度。 你的目标是使用最少的跳跃次数到达数组的最后一个位置。 示例: 输入: [2,3,1,1,4] 输出: 2 解释: 跳到最后一个位置的最小跳跃数是 2。 从下标为 0 跳到下标为 1 的位置,跳 1 步,然后跳 3 步到达数组的最后一个位置。 思路: 每到一个位置... 阅读详情

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.

Example 1:
Input: [2,3,1,1,4]
Output: true
Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index.

Example 2:
Input: [3,2,1,0,4]
Output: false
Explanation: You will always arrive at index 3 no matter what. Its maximum
jump length is 0, which makes it impossible to reach the last index.

解析

每一个数组的数组表示能跳的最大步数,判断能否从数组开始位置跳到末尾。

解法1:回溯

判断每个位置能否达到最末尾,如果能返回true,从每个位置的步数从大到小递归。
但是会超时。

class Solution {
public:
    bool canJump(vector<int>& nums) {
        return jump(nums, 0);
    }
    
    bool jump(vector<int>& nums, int i){
        if(i >= nums.size()-1)
            return true;
        
        for(int j = nums[i];j >= 1;j--){
            if(jump(nums, i+j))
               return true;
        }
        return false;
    }
};

解法2:动态规划

用一个数组dp表示跳到当前位置时,剩余的步数。当前位置的跳力和当前位置剩余步数的最大值决定了能跳的最远距离,所以下一位置的剩余步数为上一位置的最大距离-1;当有剩余步数小于0时,就返回false。

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

解法3:贪心

维护一个last表示能到末尾的位置,从后往前遍历,如果当前值和下标之和大于等于last,last更新为i。

class Solution {
public:
    bool canJump(vector<int>& nums) {
        int last = nums.size()-1;
        for(int i=nums.size()-1;i>=0;i--){
            if(i+nums[i] >= last)
                last = i;
        }
        return last == 0;
    }
};

参考

http://www.cnblogs.com/grandyang/p/4371526.html
https://leetcode.com/problems/jump-game/solution/

VisionMaster Qt二次开发:图像获取、COM组件渲染与本地化存储实战 本文详细介绍了基于VisionMaster进行Qt二次开发的核心实战流程,涵盖环境搭建、方案加载、图像数据获取、COM组件渲染与本地化存储。重点解析了如何通过COM组件在Qt界面中实时显示图像,以及将VM图像数据转换为QImage并保存为本地文件的关键步骤与常见问题,为工业视觉项目开发提供稳定高效的解决方案。 阅读详情

相关推荐

【Unity Shader】2D模型 3D模型混用的穿模问题(Spine在3D场景中出现前后穿模问题的解决办法)

问题描述: 在3D场景中使用Spine模型(2D), 因为Spine需要像Billboard类似的处理,永远是垂直与相机的, 所以一般情况下2D模型能正常显示且前后的阻挡关系也是没有问题的,如下图所示,一切都很美好.(这里用一个四边形,图中的福字表示2D模型) 可是如果后面蓝色的格子比较高,或者2D角色本身也比较高,那么就会出现模型穿插的问题,如下图所示: 如果吧2D模型竖立起来,前后关系肯定是没有问题的,所以也能避免模型穿插的问题,可是就没有Billboard效果,圆形的福字已经被压扁了,如下图:

Vitens的专栏 4590

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的博客 172

从零到一:手把手教你用DJI E-Port套件为M350 RTK开发第三方负载(PSDK V3实战)

本文详细介绍了如何使用DJI E-Port套件为M350 RTK开发第三方负载,涵盖硬件配置、开发环境搭建及实战应用。通过PSDK V3开发套件,开发者可以高效实现负载与无人机的通信,适用于电力巡检等工业场景,提升开发效率与系统稳定性。

weixin_26841743的博客 701

【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的博客 288

mipi协议中的calibration和scramble模式

在MIPI(Mobile Industry Processor Interface)协议中,calibration(校准)和scramble(加扰)模式是两个重要的特性,它们分别用于优化数据传输的准确性和减少信号干扰。

flomingo1的专栏 2397

qt使用qxlsx实现xlsx、xls表格文件快速写入和读取

新年第二篇文章,收尾一个去年计划中发表文章和实现功能。

kbq的博客 1万+

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

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

mine_song的博客 3720

【车载以太网测试从入门到精通】系列文章目录汇总

物理层测试、数据链路层测试、以太网交换机测试、网络层测试、传输层测试、以太网TCP/IP协议自动化测试、以太网网络管理测试、以太网网络管理自动化测试、鲁棒性测试、SOME/IP测试、SOMEI/IP服务接口自动化测试、DoIP诊断测试、DoIP诊断自动化测试、DoIP BootLoader刷写、以太网网关路由测试、以太网网关路由自动化测试、AVB测试。

测试领域技术分享 3157

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的博客 525

手把手教你学SIMULINK模块库文件专栏--Scope 模块详解

模块是 Simulink 中的一个信号接收模块,用于实时显示信号波形。该模块可以捕捉并可视化仿真过程中信号的变化,帮助用户直观地分析系统的动态行为。模块都能提供强有力的支持。通过深入了解其参数的影响及结合 MATLAB 脚本的应用,读者可以更加有效地利用这一模块进行仿真和研究。此外,通过实际应用场景的扩展以及常见问题解答部分的帮助,能够更好地掌握。模块是一个非常实用的工具,适用于需要实时显示信号波形的各种场景。模块的参数,可以进一步增强学习体验。模块参数的影响,下面我们将详细探讨每个参数如何影响波形显示。

MHD0815的博客 312

解决anaconda占用C盘空间过多问题

anaconda就算安装在其他盘,默认会把索引、锁定文件、压缩包等都放到系统盘(C盘),我用了两年,占用30GB。 1. 卸载不用的虚拟环境 查看已安装的虚拟环境 conda env list 卸载不用的、过期的虚拟环境 conda remove -n (要卸载的环境的名字) --all 2. 删除索引缓存、锁定文件、未使用过的包和tar包 conda clean -a 清理完之后,删除了15GB,还剩15GB,舒服了 ...

沉迷单车的追风少年 3万+

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的博客 300

【RT-DETR改进涨点】ResNet18、34、50、101等多个版本移植到ultralytics仓库(RT-DETR官方一比一移植)

本文是本专栏的第一篇改进,我将RT-DETR官方版本中的ResNet18、ResNet34、ResNet50、ResNet101移植到ultralytics仓库,网上很多改进机制是将基础版本的也就是2015年发布的ResNet移植到ultralytics仓库中,但是其实RT-DETR实验的ResNet和其是有很大区别的在其中有很多细节上的不同(文章中我也会列出举例)

Snu77的博客 7216

LeetCode: 55. Jump Game(Medium)

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

aysk1112的博客 144

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

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

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

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) {

调包侠 305

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

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的博客 342

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. 题目 运行结果

卤小蛋学编程 414

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

【贪心 和 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的博客 538

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的博客 355

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 posit...

fengran_mark的博客 129

LeetCode OJ-55.Jump Game

LeetCode OJ-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

oYinGui1 539
上一篇: [LeetCode] 43. Multiply Strings
下一篇: [LeetCode] 45. Jump Game2
Peng_maple
博客等级 码龄10年 0粉丝 79原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值