LeetCode刷题:55. Jump Game 跳跃游戏

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/seagal890/article/details/89218924

LeetCode刷题:55. Jump Game 跳跃游戏

原题链接:https://leetcode.com/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.

Example 1:

Input: [2,3,1,1,4]
Output: true
Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index.
Example 2:

Input: [3,2,1,0,4]
Output: false
Explanation: You will always arrive at index 3 no matter what. Its maximum
             jump length is 0, which makes it impossible to reach the last index.

题目大意

给定一个非负的整数数组,你处在在数组的第一个位置(下标为0处)。数组中的每个元素表示该位置的最大跳跃长度。

确定是否能够到达最后一个索引。

例1:给定数组 [2,3,1,1,4],你处于数组的第一个位置,此位置数组元素值为2,即你可以从数组元素的第一个位置跳到第二个位置(此时只跳了了1步,没有超过最大值2步);然后数组第二个位置的元素值为3,这意味着你最大跳3步,而此时你直接跳3步,则跳到了数组的最后一个位置。

例2:给定数组 [3,2,1,0,4],你处于数组的第一个位置,此位置数组元素值为3,无论你怎么跳,你都会跳到数组元素为0的位置处。而此位置的数组元素为0,所以你无论如何也跳不到数组的最后一个位置。

算法设计

    /*
	 * 采用回溯算法(Backtracking),这是一个效率低下的解决方案。
	 * 思路:尝试从第一个位置到最后一个位置的每一个跳跃模式。
	 *     从第一个位置开始,跳到所有可以到达的索引。
	 *     重复这个过程,直到到达最后一个索引。
	 *     卡住时,回退。
	 * */
		
	public boolean canJumpFromPosition(int position, int[] nums) {
        if (position == nums.length - 1) {
            return true;
        }

        int furthestJump = Math.min(position + nums[position], nums.length - 1);
        for (int nextPosition = position + 1; nextPosition <= furthestJump; nextPosition++) {
            if (canJumpFromPosition(nextPosition, nums)) {
                return true;
            }
        }

        return false;
    }

    public boolean canJump(int[] nums) {
        return canJumpFromPosition(0, nums);
    }

完整的测试代码

package com.bean.algorithm.dp;

public class JumpGame55_1 {

	/*
	 * 采用回溯算法(Backtracking),这是一个效率低下的解决方案。
	 * 思路:
	 * 尝试从第一个位置到最后一个位置的每一个跳跃模式。
	 * 从第一个位置开始,跳到所有可以到达的索引。 
	 * 重复这个过程,直到到达最后一个索引。 
	 * 卡住时,回退。
	 */

	public boolean canJumpFromPosition(int position, int[] nums) {
		if (position == nums.length - 1) {
			return true;
		}

		int furthestJump = Math.min(position + nums[position], nums.length - 1);
		for (int nextPosition = position + 1; nextPosition <= furthestJump; nextPosition++) {
			if (canJumpFromPosition(nextPosition, nums)) {
				return true;
			}
		}

		return false;
	}

	public boolean canJump(int[] nums) {
		return canJumpFromPosition(0, nums);
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		JumpGame55_1 jump = new JumpGame55_1();

		// int[] array = { 2,3,1,1,4};
		int[] array = { 3, 2, 1, 0, 4 };
		boolean flag = jump.canJump(array);
		if (flag) {
			System.out.println("Jump Game is successed.");
		} else {
			System.out.println("Jump Game is failed.");
		}
	}

}

当数组为:int[] array = { 3, 2, 1, 0, 4 };  输出结果为:Jump Game is failed.

当数组为:int[] array = { 2,3,1,1,4}; 输出结果为:Jump Game is successed.

贪心算法设计

    public boolean canJump(int[] nums) {
        int lastPos = nums.length - 1;
        for (int i = nums.length - 1; i >= 0; i--) {
            if (i + nums[i] >= lastPos) {
                lastPos = i;
                System.out.println("lastPos = "+lastPos);
            }
        }
        return lastPos == 0;
    }

猜你喜欢

转载自blog.csdn.net/seagal890/article/details/89218924