跳跃游戏II

给定一个非负整数数组,你最初位于数组的第一个位置。

数组中的每个元素代表你在该位置可以跳跃的最大长度。

你的目标是使用最少的跳跃次数到达数组的最后一个位置。

示例:

输入: [2,3,1,1,4]
输出: 2
解释: 跳到最后一个位置的最小跳跃数是 2。
  从下标为 0 跳到下标为 1 的位置,跳 1 步,然后跳 3 步到达数组的最后一个位置。
说明:

假设你总是可以到达数组的最后一个位置。

解答:

public int jump(int[] nums) {
    int steps = 0;
    int maxPosition=0;
    int end=0;
    for(int i=0;i<nums.length-1;i++){
      maxPosition=Math.max(maxPosition,nums[i]+i);
      if(i==end){
        steps++;
        end=maxPosition;
      }
    }
    return steps;
  }

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/jump-game-ii

原文地址:https://www.cnblogs.com/wuyouwei/p/11973860.html

时间: 2024-08-30 15:10:27

跳跃游戏II的相关文章

跳跃游戏 II

题目描述:给出一个非负整数数组,你最初定位在数组的第一个位置.数组中的每个元素代表你在那个位置可以跳跃的最大长度.你的目标是使用最少的跳跃次数到达数组的最后一个位置. 样例:给出数组A = [2,3,1,1,4],最少到达数组最后一个位置的跳跃次数是2(从数组下标0跳一步到数组下标1,然后跳3步到数组的最后一个位置,一共跳跃2次) 跟上一道题:"跳跃游戏"(详见:点击打开链接)基本逻辑是一样的,都是贪心法的应用.其实这道题当中,贪心法用得更明显了. 为了能够以最少的步数跳到最后,则每次

LeetCode(45): 跳跃游戏 II

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

【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.

[Swift]LeetCode45. 跳跃游戏 II | Jump Game 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-45.跳跃游戏II(hard)

给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 你的目标是使用最少的跳跃次数到达数组的最后一个位置. 示例: 输入: [2,3,1,1,4]输出: 2解释: 跳到最后一个位置的最小跳跃数是 2.  从下标为 0 跳到下标为 1 的位置,跳 1 步,然后跳 3 步到达数组的最后一个位置.说明: 假设你总是可以到达数组的最后一个位置. 来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/jump

leetcode——45. 跳跃游戏 II

我真的是超开心了,又做对了!!!!!!而且没走啥弯路!!!!!!! class Solution(object): def jump(self, nums): """ :type nums: List[int] :rtype: int """ if len(nums)<2: return 0 pact=0 i=0 while i<len(nums): if nums[i]>=len(nums)-i-1: pact+=1 retu

45. 跳跃游戏 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 j

[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 问题简介:这是一个跳跃游戏,规则是通过给定一个非负整数数组,最开始就处于数组第一位即索引为0处,数组元素数值代表可以跳跃的最大值,要求通过最小的跳跃次数达到数组最后的索引处 举例: 输入: [2,3,1,1,4] 输出: 2 解释: 这个数组的最小跳跃值是2,第一次从索引0处跳跃1到索引1处,第二次跳跃3到结尾处 解法一: 当数组长度为1即无法跳跃,结果为0,找到每个点可以达到的最右端的值,进行判定 小白刷题之路,请多指教— — 要么大器晚成,要么石沉大海 原文地址:h