原题链接在这里: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. 1 <= A.length <= 50000
  2. 1 <= A[i] <= 1e7
  3. 1 <= 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的更多相关文章

  1. 【LeetCode】1060. Missing Element in Sorted Array 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...

  2. Leetcode 34 Find First and Last Position of Element in Sorted Array 解题思路 (python)

    本人编程小白,如果有写的不对.或者能更完善的地方请个位批评指正! 这个是leetcode的第34题,这道题的tag是数组,需要用到二分搜索法来解答 34. Find First and Last Po ...

  3. 乘风破浪:LeetCode真题_034_Find First and Last Position of Element in Sorted Array

    乘风破浪:LeetCode真题_034_Find First and Last Position of Element in Sorted Array 一.前言 这次我们还是要改造二分搜索,但是想法却 ...

  4. Find First and Last Position of Element in Sorted Array - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Find First and Last Position of Element in Sorted Array - LeetCode 注意点 nums可能 ...

  5. [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 ...

  6. [array] leetcode - 33. Search in Rotated Sorted Array - Medium

    leetcode - 33. Search in Rotated Sorted Array - Medium descrition Suppose an array sorted in ascendi ...

  7. [LeetCode] 033. Search in Rotated Sorted Array (Hard) (C++)

    指数:[LeetCode] Leetcode 解决问题的指数 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 033. ...

  8. LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++>

    LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++> 给出排序好的一维有重复元素的数组,随机取一个位置断开 ...

  9. LeetCode 33 Search in Rotated Sorted Array [binary search] <c++>

    LeetCode 33 Search in Rotated Sorted Array [binary search] <c++> 给出排序好的一维无重复元素的数组,随机取一个位置断开,把前 ...

随机推荐

  1. 用Scratch制作一个Hello World程序

    网上出现了很多Hello World程序,看的小编心里也痒痒的,为此这次作为南京小码王Scratch培训机构的小编,就为大家来详细的了解下Scratch制作Hello World程序的过程,现在就和小 ...

  2. 大数据之路【第十二篇】:数据挖掘--NLP文本相似度

    一.词频----TF • 假设:如果一个词很重要,应该会在文章中多次出现 • 词频——TF(Term Frequency):一个词在文章中出现的次数 • 也不是绝对的!出现次数最多的是“的”“是”“在 ...

  3. centos7中mysql的rpm包安装

    解决依赖 yum remove mysql-libs 执行命令:yum -y install autoconf 安装依赖 yum -y install autoconf 安装mysql rpm -iv ...

  4. php GD 和图像处理函数, 用 STHUPO.TTF 字体向图像写入文本

    php GD 和图像处理函数,   用  STHUPO.TTF 字体向图像写入文本 注意: 01)   imagettftext() 这个函数不能使用相对路径, 要想使用相对路径要先使用  puten ...

  5. Robot Arms AtCoder - 4432 (构造)

    大意: 给定平面上$n$个点$(x_i,y_i)$. 要求构造一个序列$d$, $d_i$表示每步走的距离, 再构造$n$个命令串, 要求从原点出发按照第$i$个命令走, 走完恰好到达$(x_i,y_ ...

  6. Spring Boot Freemarker特别篇之contextPath【从零开始学Spring Boot

      需求缘起:有人在群里@我:请教群主大神一个问题,spring boot  + freemarker 怎么获取contextPath 头疼死我了,网上没一个靠谱的 .我就看看之前博客中的 [Spri ...

  7. 解除Ubuntu系统的root登录图形界面限制

    Ubuntu18.04.1开发团队为了Ubuntu18.04.1系统的安全,默认root不能登录图形界面,普通用户需要使用root权限时,只能通过sudo [命令] [参数] 临时使用root权限,或 ...

  8. Matlab代码优化之道

    一. 遵守Performance Acceleration的规则 关于什么是“Performance Acceleration”请参阅matlab的帮助文件.1.只有使用以下数据类型,matlab才会 ...

  9. C# NPOI 导入与导出Excel文档 兼容xlsx, xls(xf13中已经引用了xlsx的npoi)

    这里使用的NPOI版本为: 2.1.3.1 官方下载地址: http://npoi.codeplex.com/releases 版本内包含.Net 2.0 与.Net 4.0 .Net 4.0中包含文 ...

  10. select_region_point和select_region_spatial

    一.select_region_point select_region_point(Regions:DestRegions:row,column:) 算子含义:选择包含给定像素的所有区域. Regio ...