【Hot100】LeetCode—45. 跳跃游戏 II-3- ACM 实现

public class jumpGame {

    public static int jump(int[] nums){
        //1. 数据结构
        int res = 0;
        int nowCover = 0;
        int nextCover = 0;

        for(int i = 0 ; i < nums.length;i++){
            nextCover = Math.max(nextCover,i+nums[i]);
            if(i==nowCover){
                if(nowCover!=nums.length-1){
                    res++;
                    nowCover = nextCover;
                }
            }
        }
        return res;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("输入数组长度n");
        int n = sc.nextInt();
        int[] nums = new int[n];
        for(int i = 0; i < n;i++){
            nums[i] = sc.nextInt();
        }
        System.out.println("最小跳为"+jump(nums));
    }
}

上一篇:怎样优化 PostgreSQL 中对复杂条件筛选的执行效率?


下一篇:实现滚动分页查询功能:让你的应用流畅滚动