45. 跳跃游戏 II Jump Game II

技术文章 10个月前 完美者
1,252 0

标签:always   integer   eps   one   rgba   present   ast   The   leetcode   

Given an array of non-negative integers nums, 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.

You can assume that you can always reach the last index.

 

方法:

贪心。从后往前,找能到达当前位置最远的那个。

 

public int jump(int[] nums) {
        int position = nums.length - 1;
        int steps = 0;
        while (position > 0) {
            for (int i = 0; i < position; i++) {
                if (i + nums[i] >= position) {
                    position = i;
                    steps++;
                    break;
                }
            }
        }
        return steps;
        
    }

 

参考链接:

https://leetcode.com/problems/jump-game-ii/

https://leetcode-cn.com/problems/jump-game-ii/

45. 跳跃游戏 II Jump Game II

标签:always   integer   eps   one   rgba   present   ast   The   leetcode   

原文地址:https://www.cnblogs.com/diameter/p/14177557.html

版权声明:完美者 发表于 2020-12-29 11:18:24。
转载请注明:45. 跳跃游戏 II Jump Game II | 完美导航

暂无评论

暂无评论...