美文网首页
最优解:Jump Game II

最优解:Jump Game II

作者: 怎样会更好 | 来源:发表于2018-12-20 19:51 被阅读0次

    . 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.
    https://leetcode.com/problems/jump-game-ii/

    class Solution {
        public int jump(int[] nums) {
            int index  =0 ;
            int cur = 0;
            int last = 0;
            for(int i = 0,len = nums.length;i<len;i++){
                if(i>last){
                    last = cur;
                    ++index;
                }
                cur =  Math.max(cur,i+nums[i]);
            }
            return index;
        }
    }
    

    相关文章

      网友评论

          本文标题:最优解:Jump Game II

          本文链接:https://www.haomeiwen.com/subject/pyyukqtx.html