Leetcode: Jump Game
2014-11-09 12:16
239 查看
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 =
Greedy.
public class Solution {
public boolean canJump(int[] A) {
boolean[] can = new boolean[A.length];
can[0] = true;
for (int i = 1; i < A.length; i++) {
for (int j = 0; j < i; j++) {
if (can[j] && A[j] >= (i - j)) {
can[i] = true;
break;
}
}
}
return can[A.length - 1];
}
}
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.
Greedy.
public class Solution {
public boolean canJump(int[] A) {
boolean[] can = new boolean[A.length];
can[0] = true;
for (int i = 1; i < A.length; i++) {
for (int j = 0; j < i; j++) {
if (can[j] && A[j] >= (i - j)) {
can[i] = true;
break;
}
}
}
return can[A.length - 1];
}
}
相关文章推荐
- LeetCode-M-Jump Game
- LeetCode题解:Jump Game
- LeetCode55 Jump Game
- 【LeetCode】Jump Game (一维动态规划 + 线性扫描)
- LeetCode - Jump Game
- Leetcode之Jump Game问题
- leetcode第一刷_Jump Game
- leetcode-Jump Game(2014.2.23)
- (LeetCode) Jump Game
- [LeetCode] Jump Game
- 【LeetCode】Jump Game 解题报告
- LeetCode - Jump Game
- 【leetcode】第55题 Jump Game 题目+解析+代码
- [leetcode] Jump Game
- leetcode-m-Jump Game
- [Leetcode] jump game 跳跃游戏
- LeetCode 55: Jump Game
- leetCode 55.Jump Game(跳跃游戏) 解题思路和方法
- LeetCode | Jump Game
- 【LeetCode】55. Jump Game