LeetCode-55. Jump Game
来源:互联网 发布:杭州淘宝拍摄基地在哪 编辑:程序博客网 时间:2024/05/18 01:11
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.
思路和解题步骤类似于Maximum Subarray的思路三
思路:
- 定义一个变量来保存最远可以到达的位置。
- 每次循环时,我们得到当前到达的最远位置nums[i]+i,所以我们要在当前可以到达的最远位置和之前记录下的最远位置中选择,取较大值保存。如果此时能到达的最远位置已经不小于数组长度,则直接返回true。
细节
- 考虑到nums={0}的情况,所以需要事先单独判断。
- 注意循环发生的判断条件。
Java实现
class Solution { public boolean canJump(int[] nums) { if(nums.length==1) return true; int reach=0; for(int i=0;i<nums.length&&i<=reach;++i) { reach=Math.max(nums[i]+i,reach); if(reach>=nums.length-1) return true; } return false; }}
阅读全文
0 0
- [LeetCode]55.Jump Game
- LeetCode 55.Jump Game
- LeetCode --- 55. Jump Game
- [Leetcode] 55. Jump Game
- [leetcode] 55.Jump Game
- [leetcode] 55.Jump Game
- *LeetCode 55. Jump Game
- LeetCode 55. Jump Game
- [leetcode] 55. Jump Game
- leetcode 55. Jump Game
- 55. Jump Game LeetCode
- 【LeetCode】55. Jump Game
- leetcode 55. Jump Game
- [LeetCode] 55. Jump Game
- LeetCode 55. Jump Game
- LeetCode *** 55. Jump Game
- Leetcode 55. Jump Game
- LeetCode-55.Jump Game
- 企服三会·PPT | 华夏基石谭长春: 技术+营销,迎接企业经营新时代
- 数据中心云化将是大势所趋 | 工信部称1亿以上用户信息泄露为特大网络安全事件
- 计算两经纬度间的距离
- 企服三会·PPT | 神州云动 CloudCC 孙满弟:打造中国的首款CRM生态
- Python NLTK结合stanford NLP工具包进行文本处理
- LeetCode-55. Jump Game
- AJAX请求action返回JSON并进行渲染
- JSON传值与PHP接收的几种情况
- hadoop-3.0.0-beta1运维手册(002):安装Guest操作系统
- java语言程序设计-基础篇(第八版)第三章课后题答案
- 植物细胞分割的源代码
- IT忍者神龟之Linux重启后Redis数据丢失解决方案
- Data Analytics for Beginners:第一节
- JVM致命错误日志(hs_err_pid.log)分析