jump Game
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
.
Analysis:
This problem is quite the same with the Gas Station, if you can reach the current position, then you can reach every position between the current one and the original one, this can be proved with step by step, we may assume that we can reach the position between the current position and the last jumped position.
Code
public class Solution {
public boolean canJump(int[] nums) {
int temMax = 0;
int length = nums.length;
if(length == 0)
return true;
for(int i = 0; i < length; i++){
if(i > temMax)
return false;
int tem = i + nums[i]
if(tem > temMax)
temMx = tem;
}
return true;
}
}