【leetcode】55. Jump Game
来源:互联网 发布:java学费多少 编辑:程序博客网 时间:2024/06/05 13:24
Difficulty:Medium
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
.
解题思路:
jump game I版本相对来说还是很简单的,由于题目描述中的限制条件是很少的,并且只需要判断能否到达最后一个,也就是考虑,在每一天所能到达的最远的距离。
这道题目只需要进行一次循环遍历就可以解决:
使用一个额外的变量arr记录当前状态下所能到达的最远的距离。
当当天的位置加上当天所能到达的最远的距离大于当前状况下所能到达的最远的距离arr时,更新arr,然后使用两种情况控制循环的结束,分别为当i>=n的时候也就是遍历了一遍也没有找到可行的方案数,而第二种情况就是,当前状况下所能到达的最远的距离arr已经小于了i,在i不断增大向前移动的过程中最远距离不变,所以产生了越界的情况,就结束了循环。
代码如下:
class Solution {public: bool canJump(vector<int>& nums) { int n=nums.size(); int arr=0; for(int i=0;i<=arr&&i<n;i++) { if(nums[i]+i>arr) arr=nums[i]+i; if(arr>=n-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
- DCOM的netprofm属性不能修改解决办法
- Mysql建表与索引使用规范详解
- 预处理标识符: __LINE__ __FILE__ __DATE__ __TIME__ ##
- C语言 __TIME__,__LINE__等的使用
- Python默认编码错误SyntaxError: Non-ASCII character '\xe5'之解决方法
- 【leetcode】55. Jump Game
- C语言linux环境下使用gettimeofday函数得到程序运行时间
- 在使用Imagepicker时候,点击recyclerView的item,进入删除界面,返回结果为空的问题
- ccf 2014-3 窗口
- 走进设计模式(一)-创建型模式
- Android短信验证码SMSSDK集成与使用
- 无法使用jedis连接远程服务器上的redis解决方案ENIED Redis is running in protected mode because prot
- linux命令笔记1
- 文件下载