最优解:Jump Game II

2018-12-20  本文已影响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;
    }
}
上一篇下一篇

猜你喜欢

热点阅读