LeetCode 55 _ Jump Game 跳跃游戏

Description:

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.

Solution: 

这道题让我们计算以数字的值为步数,是否能跳到数组的末位

其实这道题挺简单的,不知道为什么是medium?

核心思路就是引入一个变量temp,遍历数组内所有的数,将当前能跳的最长距离存入temp(每次将当前元素能跳的距离与之前最远距离相比,将较远的那个存入)

如果当前能跳的距离落在最后一个数或其后,表示能跳到,输出true

但是肯定不是一直将整个数组遍历完的,不能成功跳到数组的末位的情况,如题目描述中的第二种情况,具有什么特点呢?

我们来看题目中的例子 [3,2,1,0,4],当在3位时,之前最远能跳到的地方temp为3,当前位置能跳的距离也为3,判断的条件即为两个情况都该处不能向前跳且之前能跳的距离也不大于此处。

同时需要保证在循环的时候数组不越界,在循环时加入i<nums.length即可

这道题还有一个特殊情况,当输入的数组为单一个0时,实际上也是可以到达的,但是按正常的情况是不存在,因为它不能遍历到i++的情况,所以在最开始再考虑到这个情况即可

Code:

public boolean canJump(int[] nums) {

    int temp = 0,int i = 0;

    if (nums.length == 1 && nums[0] == 0){
        return true;
    }

	while (i < nums.length && (i + nums[i] > i || temp > i)) {
		temp = Math.max(temp, i + nums[i]);
		if (temp >= nums.length - 1){
			return true;
		}
		i++;
	}
	return i == nums.length;
}

  

提交情况:

Runtime: 2 ms, faster than 97.64% of Java online submissions for Jump Game.

Memory Usage: 40 MB, less than 83.19% of Java online submissions for Jump Game.

 

其他答案:

LeetCode中还有一个答案,它的思路和这个大致一样,但是用了temp作为遍历的指针,写出来更为简洁,同时不用考虑到特殊情况。代码如下:

public boolean canJump(int[] nums) {
    int i = 0;
    for (int reach = 0; i < nums.length && i <= reach; ++i){
        reach = max(i + nums[i], reach);
        return i == nums.length;
    }
}

  

原文地址:https://www.cnblogs.com/zingg7/p/10623700.html

时间: 2024-10-10 15:52:36

LeetCode 55 _ Jump Game 跳跃游戏的相关文章

LeetCode(45): 跳跃游戏 II

Hard! 题目描述: 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 你的目标是使用最少的跳跃次数到达数组的最后一个位置. 示例: 输入: [2,3,1,1,4] 输出: 2 解释: 跳到最后一个位置的最小跳跃数是 2.   从下标为 0 跳到下标为 1 的位置,跳 1 步,然后跳 3 步到达数组的最后一个位置. 说明: 假设你总是可以到达数组的最后一个位置. 解题思路: 这题是之前那道Jump Game 跳跃游戏 的延伸,那题是问能不能

[Leetcode] 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 =[

【LeetCode每天一题】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:

LeetCode 55/45 Jump Game I/II-----Greedy**

一: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.

leetcode || 55、Jump Game

problem: 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 exa

[LeetCode#55, 45]Jump Game, Jump Game II

The problem: 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

[leetcode] 55. 跳跃游戏

55. 跳跃游戏 分明就是45. 跳跃游戏 II的缩水版本嘛..??,难度高的版本居然放在了前面,把像我这种有强迫症的必须按照题号刷题的人不放在眼里么... class Solution { public boolean canJump(int[] nums) { return jump(nums) != Integer.MAX_VALUE; } public int jump(int[] nums) { int f[] = new int[nums.length]; if (nums.leng

【LeetCode每天一题】Jump Game II(跳跃游戏II)

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.Your goal is to reach the last index in the minimum number of jumps.

Leetcode:Jump Game 跳跃楼梯

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 e