[leetcode]55. Jump Game(Java)
2017-06-26 10:55
281 查看
https://leetcode.com/problems/jump-game/#/description
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 =
Java Code:
package go.jacob.day626;
public class Demo2 {
/*
* 贪心算法的运用 Runtime: 8 ms.Your runtime beats 73.77 % of java submissions.
*/
public boolean canJump(int[] nums) {
// 异常输入
if (nums == null || nums.length < 1)
return false;
if (nums.length == 1)
return true;
int last = nums.length - 1;
for (int i = nums.length - 2; i >= 0; i--) {
if (i + nums[i] >= last)
last = i;
}
return last == 0;
}
}
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.
Java Code:
package go.jacob.day626;
public class Demo2 {
/*
* 贪心算法的运用 Runtime: 8 ms.Your runtime beats 73.77 % of java submissions.
*/
public boolean canJump(int[] nums) {
// 异常输入
if (nums == null || nums.length < 1)
return false;
if (nums.length == 1)
return true;
int last = nums.length - 1;
for (int i = nums.length - 2; i >= 0; i--) {
if (i + nums[i] >= last)
last = i;
}
return last == 0;
}
}
相关文章推荐
- Leetcode 55. Jump Game (Medium) (java)
- leetcode解题之45. Jump Game II&55. Jump Game java (跳跃游戏)
- (Java)LeetCode-55. Jump Game
- [LeetCode]55.Jump Game
- Java for LeetCode 055 Jump Game
- LeetCode解题报告 55. Jump Game [medium]
- LeetCode 55(Jump Game) Java
- [Leetcode] Jump Game (Java)
- [leetcode]55. Jump Game 贪心
- LeetCode 55. Jump Game and 45. Jump Game II 题解
- leetCode 55.Jump Game(跳跃游戏) 解题思路和方法
- [LeetCode] 55. Jump Game
- leetcode:Jump Game 【Java】
- 55. Jump Game (leetcode)
- [leetcode]55. Jump Game ,C++/PYTHON实现,medium难度
- LeetCode(055) Jump Game (Java)
- [LeetCode] 55. Jump Game 解题思路
- 【LeetCode】55. Jump Game
- 【LeetCode】55. Jump Game (Medium)
- LeetCode 55. Jump Game 解题报告