【Leetcode】Jump Game in JAVA
2014-11-15 23:36
429 查看
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 =
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 jumpGame { public static void main(String args[]){ int[] A = {3,0,0,0}; jumpGame jp = new jumpGame(); if(jp.canJump(A)) System.out.println("We win"); else System.out.println("we lose"); } public boolean canJump(int[] A) { int max = 0; int step = 1; if(A.length<=1) return true; if(A[0]==0&&A.length>1) return false; for(int i=0;i<A.length;i++){ step--; if(i+A[i]>max){ max = i+A[i]; step = A[i]; } if(step==0 && i<A.length-1) return false; } return true; } }
相关文章推荐
- 【Leetcode】经典的Jump Game in JAVA
- 【Leetcode】经典的Jump Game in JAVA
- leetcode:Jump Game II 【Java】
- [LeetCode][Java] Jump Game II
- LeetCode(045) Jump Game II (Java)
- [LeetCode][Java] Jump Game II
- Jump Game II leetcode java
- leetcode解题之45. Jump Game II&55. Jump Game java (跳跃游戏)
- Jump Game leetcode java
- [leetcode-45]Jump Game II(java)
- Jump Game leetcode java
- LeetCode 第四十五题(Jump Game II)Java
- Java for LeetCode 045 Jump Game II
- Jump Game II leetcode java
- [Leetcode] Jump Game II (Java)
- [leetcode刷题系列]Jump Game II
- leetcode Jump Game
- [LeetCode]Jump Game II
- [leetcode]Jump Game II
- LeetCode_Jump Game II