LeetCode 1060. Missing Element in Sorted Array
原题链接在这里:https://leetcode.com/problems/missing-element-in-sorted-array/
题目:
Given a sorted array A of unique numbers, find the K-th missing number starting from the leftmost number of the array.
Example 1:
Input: A = [4,7,9,10], K = 1
Output: 5
Explanation:
The first missing number is 5.
Example 2:
Input: A = [4,7,9,10], K = 3
Output: 8
Explanation:
The missing numbers are [5,6,8,...], hence the third missing number is 8.
Example 3:
Input: A = [1,2,4], K = 3
Output: 6
Explanation:
The missing numbers are [3,5,6,7,...], hence the third missing number is 6.
Note:
1 <= A.length <= 500001 <= A[i] <= 1e71 <= K <= 1e8
题解:
If the missing numbers count of the whole array < k, then missing number must be after nums[n-1]. res = nums[n-1] + missingCount.
Otherwise, need to find out the starting index to calculate the missing number.
Use binary search to have mid as candidate.
If missing count < k, then must fall on the right side. l = mid + 1.
Time Complexity: O(logn). n = nums.length.
Space: O(1).
AC Java:
class Solution {
public int missingElement(int[] nums, int k) {
int n = nums.length;
if(nums[n - 1] - nums[0] - (n - 1 - 0) < k){
return nums[n - 1] + k - missCount(nums, n - 1);
}
int l = 0;
int r = n - 1;
while(l < r){
int mid = l + (r - l) / 2;
if(missCount(nums, mid) < k){
l = mid + 1;
}else{
r = mid;
}
}
return nums[l - 1] + k - missCount(nums, l - 1);
}
private int missCount(int [] nums, int mid){
return nums[mid] - nums[0] - mid;
}
}
LeetCode 1060. Missing Element in Sorted Array的更多相关文章
- 【LeetCode】1060. Missing Element in Sorted Array 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...
- Leetcode 34 Find First and Last Position of Element in Sorted Array 解题思路 (python)
本人编程小白,如果有写的不对.或者能更完善的地方请个位批评指正! 这个是leetcode的第34题,这道题的tag是数组,需要用到二分搜索法来解答 34. Find First and Last Po ...
- 乘风破浪:LeetCode真题_034_Find First and Last Position of Element in Sorted Array
乘风破浪:LeetCode真题_034_Find First and Last Position of Element in Sorted Array 一.前言 这次我们还是要改造二分搜索,但是想法却 ...
- Find First and Last Position of Element in Sorted Array - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Find First and Last Position of Element in Sorted Array - LeetCode 注意点 nums可能 ...
- [LeetCode] 34. Search for a Range 搜索一个范围(Find First and Last Position of Element in Sorted Array)
原题目:Search for a Range, 现在题目改为: 34. Find First and Last Position of Element in Sorted Array Given an ...
- [array] leetcode - 33. Search in Rotated Sorted Array - Medium
leetcode - 33. Search in Rotated Sorted Array - Medium descrition Suppose an array sorted in ascendi ...
- [LeetCode] 033. Search in Rotated Sorted Array (Hard) (C++)
指数:[LeetCode] Leetcode 解决问题的指数 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 033. ...
- LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++>
LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++> 给出排序好的一维有重复元素的数组,随机取一个位置断开 ...
- LeetCode 33 Search in Rotated Sorted Array [binary search] <c++>
LeetCode 33 Search in Rotated Sorted Array [binary search] <c++> 给出排序好的一维无重复元素的数组,随机取一个位置断开,把前 ...
随机推荐
- linux终端命令行前缀设置为“当前目录”(非绝对路径)
操作 打开家目录下的隐藏文件.bashrc 定位到62行,将小写的\W改为大写,保存即可. 重新打开bash 注意: /etc/profile , /etc/bashrc等文件里的环境变量设置会被.b ...
- C++ Clock函数调用及用途
用途1 Clock函数可以有效地针对一些只能用随机化做的题目 为了提高该类代码的正确性,我们期望它运行的次数在要求时限内运行足够多 因此将Clock函数充当计时器 用途2 计时判断负环 原理: 给定一 ...
- js中常见的字符串方法(3)
match() match()方法只接受一个参数,要么是一个正则表达式,要么是一个 RegExp 对象. 调用这个方法本质上与调用RegExp的exec()方法相同, var text = " ...
- LOJ #3103. 「JSOI2019」节日庆典
题意 给定字符串 \(S\) ,对于 \(S\) 的每个前缀 \(T\) 求 \(T\) 所有循环同构串的字典序最小的串,输出其起始下标.(如有多个输出最靠前的) \(|S| \le 3 \times ...
- Scala 系列(一)—— Scala 简介及开发环境配置
一.Scala简介 1.1 概念 Scala 全称为 Scalable Language,即"可伸缩的语言",之所以这样命名,是因为它的设计目标是希望伴随着用户的需求一起成长.Sc ...
- (1)SQL Server内存浅探
1.前言 对于数据库引擎来说,内存是一个性能提升的重要解决手段.把数据缓存起来,可以避免在查询或更新数据时花费多余的时间,而这时间通常是从磁盘获取数据时用来等待磁盘寻址的.把执行计划缓存起来,可以避免 ...
- 实战Go内存泄露【转】
最近解决了我们项目中的一个内存泄露问题,事实再次证明pprof是一个好工具,但掌握好工具的正确用法,才能发挥好工具的威力,不然就算你手里有屠龙刀,也成不了天下第一,本文就是带你用pprof定位内存泄露 ...
- Java之路---Day11(接口)
2019-10-25-23:22:23 目录 1.接口的概念 2.接口的定义格式 3.接口包含的内容 4.接口的使用步骤 5.继承父类并实现多个接口 6.接口之间的多继承 接口的概念 接口是指对协定进 ...
- Sublime Text3的安装(package control error 或者 there are no package available for installation等问题)
Sublime是一款非常好用的代码编辑器.Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,多种语言的插件,代码段等.还可自定义键绑定,菜单和工具栏.Sublime Text 的 ...
- Hive性能优化【核心思想、运行模式、并行计算】
一.核心思想 把HQL当做MapReduce程序去优化. 注意,以下SQL不会转为MapReduce执行: 1.select仅查询本表字段. 2.where仅对本表字段做条件过滤. 二.启动Hive ...