55. Jump Game
来源:互联网 发布:linux怎么读音是什么 编辑:程序博客网 时间:2024/06/15 08:17
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
.
public class Solution { public boolean canJump(int[] nums) { boolean res = false; int count = 0; for (int i = nums.length - 1; i >= 0; i --) { if (nums[i] >= count) { res = true; count = 0; } else { res = false; } count ++; } return res; }}当然也可以从前向后遍历,设一个reach数,从前向后遍历数组,reach取i+nums[i]和reach较大的一个。如果i大于reach了,说明到不了,停止遍历,返回false,如果i等于n,说明可以到,返回true。代码如下:
bool canJump(int A[], int n) { int i = 0; for (int reach = 0; i < n && i <= reach; ++i) reach = max(i + A[i], reach); return i == n;}
0 0
- 55. Jump Game && 45. Jump Game II
- 55. Jump Game. 45. Jump Game II
- [LeetCode]55.Jump Game
- LeetCode 55.Jump Game
- LeetCode --- 55. Jump Game
- [Leetcode] 55. Jump Game
- [leetcode] 55.Jump Game
- 55. Jump Game
- 55. Jump Game
- [leetcode] 55.Jump Game
- *LeetCode 55. Jump Game
- LeetCode 55. Jump Game
- [leetcode] 55. Jump Game
- 55. Jump Game
- leetcode 55. Jump Game
- 55. Jump Game LeetCode
- 【LeetCode】55. Jump Game
- 55. Jump Game
- 为什要使用BindService?为了调用服务中的方法
- codeforces 528D. Fuzzy Search (FFT优化DP)
- 453. Minimum Moves to Equal Array Elements
- leetcode总结帖
- c++ 调试问题 集合
- 55. Jump Game
- 【BZOJ 2618】[Cqoi2006]凸多边形 半平面交
- 来电去电自动录音
- 上下移动页面元素的代码
- BZOJ 1087 [SCOI2005] 互不侵犯King
- 多线程下变量-gcc原子操作 __sync_fetch_and_add等
- Android 通过WebService进行网络编程,使用工具类轻松实现
- EXT.NET GridPanel Column 根据ID读取实际值
- 2017-02-19 Hibernate(1)