/**
* nums的所有元素,假设最大能连续形成[1,sum] 当增加一个element的时候
* 会变成 [1,sum] [element+1,sum+element]两个区间,这两个区间有以下可能性:
* 1 相交: element < sum
* 2 连续: element = sum 是保持连续,并使得加的元素最少的最大值
* 3 相离: element > sum 大于sum就不连续了
* */
private int minPatches(int[] nums, int n) {
long sum = 1;//1 是必需得有,因为要形成[1,n]的分布,肯定得有1,如果nums没有,就要把其加上
int index= 0;
int add = 0;
while(sum <= n ){
if(index < nums.length && nums[index] <=sum){
sum += nums[index];
index++;
}
else{
sum += sum;
add++;
}
}
return add;
}

LeetCode-330.Patching Array的更多相关文章

  1. [LeetCode] 330. Patching Array 数组补丁

    Given a sorted positive integer array nums and an integer n, add/patch elements to the array such th ...

  2. 330. Patching Array

    Given a sorted positive integer array nums and an integer n, add/patch elements to the array such th ...

  3. LeetCode:Convert Sorted Array to Binary Search Tree,Convert Sorted List to Binary Search Tree

    LeetCode:Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in asce ...

  4. [LeetCode] Patching Array 补丁数组

    Given a sorted positive integer array nums and an integer n, add/patch elements to the array such th ...

  5. LeetCode Patching Array

    原题链接在这里:https://leetcode.com/problems/patching-array/ 题目: Given a sorted positive integer array nums ...

  6. [LeetCode] Shuffle an Array 数组洗牌

    Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. int[] n ...

  7. [LeetCode] Sort Transformed Array 变换数组排序

    Given a sorted array of integers nums and integer values a, b and c. Apply a function of the form f( ...

  8. [LeetCode] Product of Array Except Self 除本身之外的数组之积

    Given an array of n integers where n > 1, nums, return an array output such that output[i] is equ ...

  9. [LeetCode] Convert Sorted Array to Binary Search Tree 将有序数组转为二叉搜索树

    Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 这道 ...

  10. [LeetCode] Merge Sorted Array 混合插入有序数组

    Given two sorted integer arrays A and B, merge B into A as one sorted array. Note:You may assume tha ...

随机推荐

  1. 课堂讨论—Alpha版总结会议

    我们在课堂上针对第一阶段冲刺过程中存在的问题,展开了激烈的讨论,并投票选出需要改进的最主要三个问题. 有图有真相:

  2. MIT挑战(如何在12个月内自学完成MIT计算机科学的33门课程|内附MIT公开课程资源和学习顺序

    译者注:本文译自Scott H. Young的博客,Scott拥有超强的学习能力,曾在12个月内自学完成麻省理工学院计算机科学的33门课程.本文就是他个人对于这次MIT挑战的介绍和总结. 版权声明:本 ...

  3. Week2-作业1-part2.阅读与思考

    第一章.概论 原文: 在成熟的航空工业中,一个飞机发动机从构思到最后运行,不知道经历过多少人.多少工序.多少流程.多少相关知识的验证.我们无法想象,某个商用型号的发动机在飞行时发现问题,最初的设计师会 ...

  4. <mvc:annotation-driven/>的作用

    <mvc:annotation-driven>的作用是: 会自动注册DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapt ...

  5. ubuntu中启动VIM,以及学习VIM

    启动VIM:首先打开终端,然后输入vi回车,然后输入i或者a,进入. 学习VIM:首先打开终端,然后输入vimtutor回车,然后进入教程学习.

  6. 10th 本周工作量及进度统计

    本周PSP C(类别) C(内容) S(开始时间) ST(结束时间) I(中断时间) T(实际时间) 文档 11月22日 规格说明书练习 10:23­ 15:30 132 175 11月23日 知识点 ...

  7. 10th 规格说明书练习——吉林一日游

    活动规格说明书 吉林市一日游 版本:1.0 编订:王东涵 团队:2016级计算机技术全体同学 日期:2016-11-20 目录 1.引言 1.1 编写目的 1.2 背景 1.3 定义 1.4 参考资料 ...

  8. [51CTO]给您介绍Windows10各大版本之间区别

    给您介绍Windows10各大版本之间区别 随着win10的不断普及和推广,越来越多的朋友想安装win10系统了,但是很多朋友不知道win10哪个版本好用,为了让大家能够更好的选择win10系统版本, ...

  9. QVariant相当于一个包含大多数Qt数据类型的联合体(源码解读)

    将数据存储为一个Private结构体类型的成员变量d: <qvariant.cpp> 1 QVariant::QVariant(Type type) 2 { create(type, 0) ...

  10. 反射就是获取该类的.class文件里面的方法,属性

    反射就是获取该类的.class文件里面的方法,属性