Leetcode-Jump Game
2014-11-17 07:04
441 查看
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 =
A =
Have you met this question in a real interview?
Analysis:
We record the max reachable length and update it at each A[i].
1. If maxReach<i, then cannot reach i return false;
2. If maxReach<i+A[i], then we update the maxReach to i+A[i].
3. Once maxReach>=A.length-1, we can reach the end, return true.
Solution:
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.
Have you met this question in a real interview?
Analysis:
We record the max reachable length and update it at each A[i].
1. If maxReach<i, then cannot reach i return false;
2. If maxReach<i+A[i], then we update the maxReach to i+A[i].
3. Once maxReach>=A.length-1, we can reach the end, return true.
Solution:
public class Solution { public boolean canJump(int[] A) { if (A.length==0 || A.length==1) return true; int len = A.length; int maxReach = A[0]; for (int i=1;i<len;i++) if (maxReach<i) return false; else { if (maxReach<i+A[i]) maxReach = i+A[i]; if (maxReach>=len-1) return true; } return false; } }
相关文章推荐
- leetcode_Jump Game
- LeetCode(一) jump game
- Jump Game -- LeetCode
- LeetCode | Jump Game
- [week 19][Leetcode]Jump Game
- 开始刷leetcode day13:Jump Game
- LeetCode - Jump Game
- leetcode做题总结,动态规划I(Triangle,Unique PathsI/II,Minimum Path Sum,Climbing Stairs,Jump Game,Word Break)
- 动态规划小结 - 一维动态规划 - 时间复杂度 O(n),题 [LeetCode] Jump Game,Decode Ways
- leetcode-55 Jump Game
- LeetCode--Jump Game
- leetcode-jump game
- LeetCode 55 Jump Game
- 数据结构与算法[LeetCode]——Jump Game I II
- LeetCode 55. Jump Game 解题报告
- [LeetCode] Jump Game
- leetcode 55|45. Jump Game 1|2
- LeetCode 55. Jump Game(跳格子)
- [Leetcode 56] 55 Jump Game
- Jump Game-Leetcode