码迷,mamicode.com
首页 > 其他好文 > 详细

45. 跳跃游戏 II Jump Game II

时间:2020-12-29 11:18:24      阅读:0      评论:0      收藏: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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!