Thursday, February 4, 2021, the weather is fine [Don’t lament the past, don’t waste the present, don’t fear the future]
Contents of this article
1. Introduction
Sword Finger Offer 53-I. Find the number I in the sorted array
2. Solution
2.1 lower_bound 结合 upper_bound
lower_bound (): returns the first is greater than or equal to a target element location, it is less than the return end ()
upper_bound, (): returns the first is greater than the target positions of the elements are less than or equal, then return end ()
class Solution {
public:
int search(vector<int>& nums, int target) {
vector<int>::iterator it_beg = lower_bound(nums.begin(),nums.end(),target);
// target不存在的情况
if(it_beg==nums.end() || *it_beg != target) return 0;
// 因为上面已经判断过target不存在的情况,所以这里要么返回end(),要么返回第一个
// 大于target的元素位置,这两种情况可以合并
vector<int>::iterator it_end = upper_bound(nums.begin(),nums.end(),target);
// 返回最终结果
return it_end - it_beg;
}
};
2.2 手撸 lower_bound()、upper_bound()
Here is the variant of binary search, lower_bound() and upper_bound() by hand.
The following is the code. There are 4 details in total. It is recommended to memorize it so as not to be nervous during the interview and forget whether it should be =. Should it be written as -1 or plus +1.
class Solution {
public:
int search(vector<int>& nums, int target) {
if(nums.empty()) return 0;
int idx_beg = 0, idx_end = 0;
int n = nums.size();
// 返回 第一个等于target 的元素位置(类似lower_bound,稍有改动),都不等于的话返回 0
int l = 0, r = n - 1; // 细节1:r = n - 1
while(l < r){
// 细节2:l < r
int m = l + (r - l)/2;
// 细节3:三种情况分开写,便于理解
if(nums[m] < target) l = m + 1;
else if(nums[m] == target) r = m;
else r = m - 1;
}
// 细节4:l == r 的情况单独判断
if(nums[l]==target) idx_beg = l;
else return 0;
// 返回 第一个大于target 的元素位置(类似upper_bound,稍有改动),都小于等于的话
// 返回最后的索引+1(其实就是对应end())
l = 0, r = n - 1; // 细节1:r = n - 1
while(l < r){
// 细节2:l < r
int m = l + (r - l)/2;
// 细节3:三种情况分开写,便于理解
if(nums[m] < target) l = m + 1;
else if(nums[m] == target) l = m + 1;
else r = m;
}
// 细节4:l == r 的情况单独判断
// 如果这里等于,那么就都小于等于target,返回最后的索引+1(对应end())
if(nums[l]==target) idx_end = l + 1;
// 否则,返回第一个大于target的元素位置
else idx_end = l;
return idx_end - idx_beg;
}
};
references
"Sword Finger Offer Second Edition"
https://www.cnblogs.com/mxj961116/p/11945444.html