注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号
x
O(N)的好做。但是用二分法为啥下面这种会出错呢?少的输入可以通过,最后两个很大的vector就出错,差一。顺求互相加米,谢谢
- int missingElement(vector<int>& nums, int k) {
- int n=nums.size();
- if (n==0 || k==0)
- return k;
- int start=nums[0], res=start+k;
-
- auto upBnd=upper_bound(nums.begin(), nums.end(), res);
- int dist=upBnd-nums.begin();
-
- if (dist==1)
- return res;
- else
- return res+dist-1;
- }
复制代码
1060. 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 <= 50000
1 <= A <= 1e7
1 <= K <= 1e8
|