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

贪心算法.其实这个思路相当于利用了广度优先遍历的思想(或者树的层次遍历的思想),只不过这里的实现没有使用队列,因为我们这里的元素不像树那样是一层一层存放的,这里是顺序存储在一个从左到右的数组中,因此不像树的层次遍历那样需要用一个辅助的队列数据结构,来一层层的处理,这里用一个变量值就可以说明一层的“范围”,直接在原地数组中进行,类似于树的层序遍历的处理方法思想.

代码:

class Solution {
public:
    int jump(vector<int>& nums) {

        int n = nums.size();
        if(n == 1)
            return 0;
        //变量ret含义:第几圈
        int ret = 0;//初始化时刚开始时处于第0圈,所以ret初始值为0
        int last = 0;//刚开始圈的范围
        int cur = 0;
        //注意变量的值要和状态相对应
        for(int i = 0; i < n; ++i)
        {       //last变量的含义:上一圈元素确定的范围作为当前圈的范围
            if(i > last)//进入新的一圈
            {
                last = cur;
                ++ret;
                cur = i+nums[i];
            }
            else cur = max(cur, i+nums[i]);//当前圈的元素确定的范围作为下一圈的范围
        }

        return ret;
    }
};

方法2:

借助于队列的广度优先遍历,leetcode java用时13ms,C++用时20ms。

代码:

class Solution {
public:
    int jump(vector<int> &nums) {

        int n = nums.size();
        if(n == 0 || n ==1)
            return 0;
        queue<int> q;
        vector<bool> visited(n,false);
        q.push(0);
        visited[0] = true;
        int step = -1;//要特别注意这里的初始化,
        while(!q.empty())
        {
            step++;//变量的值的触发一定要和状态保持一致
            int size = q.size();
            while(size--)
            {
                int i = q.front();
                q.pop();
                //为了避免超时,这里对j的遍历采用从大到小的贪心策略
                for(int j = nums[i]; j >=1; j--)
                {
                        if(i + j < n-1 )
                        {
                            if(visited[i+j]==false)
                            {
                                q.push(i + j);
                                visited[i+j] = true;
                            }
                        }
                        else
                           return step+1;
                    }
            }
        }
        return step;
    }
};

  

  

原文地址:https://www.cnblogs.com/zjuhaohaoxuexi/p/11846225.html

时间: 2024-07-31 19:57:30

45. 跳跃游戏 II的相关文章

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

LeetCode(45): 跳跃游戏 II

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

跳跃游戏 II

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

[Leetcode]44.跳跃游戏Ⅰ&amp;&amp;45.跳跃游戏Ⅱ

跳跃游戏链接 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 判断你是否能够到达最后一个位置. 示例 1: 输入: [2,3,1,1,4] 输出: true 解释: 从位置 0 到 1 跳 1 步, 然后跳 3 步到达最后一个位置. 示例 2: 输入: [3,2,1,0,4] 输出: false 解释: 无论怎样,你总会到达索引为 3 的位置.但该位置的最大跳跃长度是 0 , 所以你永远不可能到达最后一个位置. 思路: 如果只是判断能否跳到

【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. 跳跃游戏2

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

跳跃游戏II

给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 你的目标是使用最少的跳跃次数到达数组的最后一个位置. 示例: 输入: [2,3,1,1,4]输出: 2解释: 跳到最后一个位置的最小跳跃数是 2.  从下标为 0 跳到下标为 1 的位置,跳 1 步,然后跳 3 步到达数组的最后一个位置.说明: 假设你总是可以到达数组的最后一个位置. 解答: public int jump(int[] nums) { int steps = 0; int ma