LeetCode 55. How to Solve Jump Game Problems
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, 4], return true.
A = [3, 2, 1, 0, 4], return false.
Idea: This question is zero more than that of jump Game II, and may not reach the final point. The Code uses the greedy idea and returns false if it can only go 0. The Code is as follows:
Public class Solution {public boolean canJump (int [] nums) {if (nums. length = 0) return false; int I = 0; // determines whether there is any 0. If there is no 0, the while (I <nums. length) {if (nums [I] = 0) {break;} I ++;} // No 0, certainly can reach if (I = nums. length) {return true;} I = 0; while (I <nums. length) {if (I + nums [I]> = nums. length-1) return true; if (nums [I] = 0) return false; int max = 0; int index = 0; // The maximum step for the next step (int j = I + 1; j-I <= nums [I]; j ++) {if (max <j-I + nums [j]) {max = j-I + nums [j]; index = j ;}} // go to the next index I = index;} return true ;}}