JAVA求解跳跃游戏(Jump Game)
原题链接:https://leetcode.com/problems/jump-game/description/ JUMP GAME
原题链接:https://leetcode.com/problems/jump-game-ii/description/ JUMP GAMEII
这两个是一类问题
问题描述:给定一个非负整数数组,给定的初始化位置在数组的起始位置。数组中的每个元素代表着你能都在此位置跳跃的最大的距离。你的目标是用最少的跳跃数达到数组的末尾。
假设条件:你可以假设你总是有可能到达数组的末尾。
给出的一个举例为
For example:
Given array A = [2,3,1,1,4]
The minimum number of jumps to reach the last index is 2. (Jump 1 step from index 0 to 1, then 3 steps to the last index.)
算法设计—1
public static int canJump (int[] nums) {
if (nums.length == 1) {//当数组中只有一个元素时,返回0。
return 0;
}
int step = 0;//在开始之前初始化 step = 0
int position = 0;// 从当前的step开始的位置初始化为0
int reachable = 0;// 从当前的step可以到达的最远的位置
int furthe