1060. Missing Element in Sorted Array 有序数组中的缺失元素

@TOC

# 题目描述

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`

# 解题方法

# 遍历

C++代码如下：

``````class Solution {
public:
int missingElement(vector<int>& nums, int k) {
sort(nums.begin(), nums.end());
int pre = nums[0];
for (int i = 1; i < nums.size(); ++i) {
if (k < nums[i] - pre) {
return pre + k;
} else {
k -= nums[i] - pre - 1;
}
pre = nums[i];
}
return pre + k;
}
};
``````

# 日期

2019 年 9 月 21 日 —— 莫生气，我若气病谁如意