LeetCode-330.Patching Array
/**
* 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的更多相关文章
- [LeetCode] 330. Patching Array 数组补丁
Given a sorted positive integer array nums and an integer n, add/patch elements to the array such th ...
- 330. Patching Array
Given a sorted positive integer array nums and an integer n, add/patch elements to the array such th ...
- 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 ...
- [LeetCode] Patching Array 补丁数组
Given a sorted positive integer array nums and an integer n, add/patch elements to the array such th ...
- LeetCode Patching Array
原题链接在这里:https://leetcode.com/problems/patching-array/ 题目: Given a sorted positive integer array nums ...
- [LeetCode] Shuffle an Array 数组洗牌
Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. int[] n ...
- [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( ...
- [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 ...
- [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. 这道 ...
- [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 ...
随机推荐
- What is ASP.NET SignalR
什么是ASP.NET SignalR ASP.NET SignalR是ASP.NET开发人员的新库,它使得为应用程序添加实时Web功能变得非常简单. 什么是“实时网络”功能?它能够让您的服务器端代码实 ...
- 《TCP/IP 详解 卷1:协议》第 9 章:广播和本地组播(IGMP 和 MLD)
我已经懒了,卷一已经是去年年底看完的,但怎么说卷一的坑开了就要填完啊-- 广播和本地组播(IGMP 和 MLD) 引言 有 4 种 IP 地址,单播(unicast).任播(anycast).组播(m ...
- Good Time 冲刺 六
一.今日完成任务情况 第六天 日期:2018.6.19 王怡镔:今天完善了页面,对部分不足进行改进. 于鑫宇:对界面进行完善. 胡雅馨:今天完成前端页面,并改善后端,完善项目. 黄 鹤:做完最后的打卡 ...
- 软工网络15团队作业8——Beta阶段项目总结
1.新成员 姓名 风格 擅长 角色 宣言 李家俊 乱写 都有所涉猎 测试 混就完事了 丁树乐 潇洒 与人沟通 测试 与其临渊羡鱼,不如退而结网 2.是否需要更换团队的PM 不需要 3.下一阶段需要改进 ...
- linux 无交互生成ssh rsa免秘证书
[root@xxx tmp]# man ssh-keygen NAME ssh-keygen - authentication key generation, management and conve ...
- Delphi 判断一个字符串是否为数字
//函 数 名: IsDigit//返 回 值: boolean//日 期:2011-03-01//参 数: String//功 能: 判断一个字符串是否为数字// ...
- Android CollapsingToolbarLayout
第一次看到这种用户体验是在Google Play Store App的应用详情的Activity中. 大的Banner图,能第一时间吸引用户的眼球,用不一样的Banner大图更具个性化的展示内容.图总 ...
- DAY5-Flask项目
1.验证参数(WTForms): 当URL为/book/search?q= &page=1 时 ,p=空格,验证器会通过,在forms验证层的book.py文件中添加DataRequired验 ...
- mysql中while循环以及变量声明以及dilimiter
首先我们查看一个正确的完整的一个存储过程 ①其中delimiter命令解释如下:默认情况下,delimiter是分号:.在命令行客户端中,如果有一行命令以分号结束,那么回车后,mysql将会执行该命令 ...
- shiro异常类型
<!-- 身份认证异常 --> <!-- 身份令牌异常,不支持的身份令牌 --> org.apache.shiro.authc.pam.UnsupportedTokenExce ...