首页 > 其他 > 详细

330. Patching Array

时间:2016-07-10 06:24:22      阅读:178      评论:0      收藏:0      [点我收藏+]
    /*
     * 330. Patching Array
     * 2016-7-9 by Mingyang
     * hehe
     */
    public int minPatches(int[] nums, int n) {
        if (n <= 0)
            return 0;
        nums = nums == null ? new int[0] : nums;
        int current_ind = 0, ret = 0;
        long boundary_val = 1, sum = 0;
        while (boundary_val <= n) {
            if (current_ind < nums.length && nums[current_ind] <= boundary_val) {
                sum += nums[current_ind++];
                boundary_val = sum + 1;
            } else {
                ret++;
                sum += boundary_val;
                boundary_val = sum + 1;
            }
        }
        return ret;
    }

 

330. Patching Array

原文:http://www.cnblogs.com/zmyvszk/p/5657040.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!