45. 跳跃游戏 II Jump Game II

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/

posted @ 2020-12-23 11:21  diameter  阅读(79)  评论(0编辑  收藏  举报