当前位置:网站首页>【2022初春】【LeetCode】45. 跳跃游戏 II

【2022初春】【LeetCode】45. 跳跃游戏 II

2022-06-09 05:58:00 之井

动规+贪心跳跃,思路是OK的,但是实现上一直有问题,需要多理解

class Solution {
    
    public int jump(int[] nums) {
    
        int ans = 0;
        int end = 0; 
        int maxlen = 0;
        for(int i=0; i<nums.length-1; i++){
    
            maxlen = Math.max(maxlen,i+nums[i]);
            if(i==end){
    
                end = maxlen;
                ans++;
            }
        }
        return ans;
    }
}
原网站

版权声明
本文为[之井]所创,转载请带上原文链接,感谢
https://blog.csdn.net/mdzz_z/article/details/124961936