当前位置:网站首页>Sword finger offer 53 - I. find the number I in the sorted array (improved bisection)
Sword finger offer 53 - I. find the number I in the sorted array (improved bisection)
2022-06-28 04:40:00 【BugMaker-shen】

Binary search found one target, Then start counting on both sides
class Solution {
public:
int search(vector<int>& nums, int target) {
int l = 0;
int r = nums.size() - 1;
int pos = -1;
while(l <= r){
int mid = l + ((r - l) >> 1);
if(nums[mid] == target){
// find
pos = mid;
break;
}else if(nums[mid] < target){
l = mid + 1;
}else{
r = mid - 1;
}
}
if(pos == -1){
return 0;
}
int cnt = 1;
int left = pos - 1;
while(left >= 0 && nums[left] == target){
cnt++;
left--;
}
int right = pos + 1;
while(right < nums.size() && nums[right] == target){
cnt++;
right++;
}
return cnt;
}
};
We need to know , When n u m s [ m i d ] < t a r g e t nums[mid] < target nums[mid]<target when , l l l Will move to the right , in other words , l l l It is impossible to cross any one of the arrays t a r g e t target target value . for the first time m i d mid mid Point to t a r g e t target target when , Do not exit search , It's going to continue the cycle , until l = = r l==r l==r Just quit .
because l l l Will not cross any one t a r g e t target target value , r r r It is only possible to point to no greater than t a r g e t target target Value , The exit conditions we set are l = = r l==r l==r, If... Exists in the array target, When you finally quit , l l l and r r r It must point to the leftmost t a r g e t target target
class Solution {
public:
int search(vector<int>& nums, int target) {
int l = 0;
int r = nums.size() - 1;
int cnt = 0;
while(l < r){
int mid = l + ((r - l) >> 1);
if(nums[mid] >= target){
// here mid May point to target, Out of commission r = mid - 1 Cross current value
r = mid;
}else{
// At this time mid Less than target,l You can cross mid
l = mid + 1;
}
}
while(l < nums.size() && nums[l] == target){
l++;
cnt++;
}
return cnt;
}
};
边栏推荐
- The second round of free public classes of the red team is coming ~ 8:00 tomorrow night!
- Digital promising, easy to reach, Huawei accelerates the layout of the commercial market with "five pole" star products
- Simple factory mode
- Huawei's 9-year experience as a software testing director
- With favorable policies, more than 20 provinces and cities have launched the yuanuniverse development plan
- Introversion, lying flat and midlife crisis
- 华为9年经验的软件测试总监工作感悟—写给还在迷茫的朋友
- 27 years, Microsoft IE is over!
- 11_ Deliberate practice and elaboration
- native关键字的作用
猜你喜欢

抖音實戰~關注博主

Problems with cat and dog queues

Digital promising, easy to reach, Huawei accelerates the layout of the commercial market with "five pole" star products

10: 00 interview, came out at 10:02, the question is really too

Multi project design and development · introduction to class library project

Severe tire damage: the first rock band in the world to broadcast live on the Internet

设计一个有getMin功能的栈

100+ data science interview questions and answers Summary - machine learning and deep learning

2022年中國音頻市場年度綜合分析

Ppt production tips
随机推荐
100+ data science interview questions and answers Summary - machine learning and deep learning
求一个能判断表中数据,只填充不覆盖的sql
leetcode:714. The best time to buy and sell stocks includes handling fee [DP dual status]
In the era of video explosion, who is supporting the high-speed operation of video ecological network?
Genicam gentl standard ver1.5 (2)
控制器的功能和工作原理
Reverse a stack with recursive functions and stack operations only
[proteus simulation] timer 1 external counting interrupt
Lamaba expression learning and common functional interfaces
Ppt production tips
Password encryption MD5 and salt treatment
【Matlab红绿灯识别】红绿灯识别【含GUI源码 1908期】
Bitlock recovery occurs in win 10, and the blue screen error code is 0x1600007e
Tiktok actual battle ~ take off the blogger
How can we speed up the chunksplitter when CDC extracts MySQL data in full?
2022年中國音頻市場年度綜合分析
Google Earth engine (GEE) - global flood database V1 (2000-2018)
After launching the MES system, these changes have taken place in the enterprise
Design a stack with getmin function
AspNetCoreRateLimit 速率限制 接口访问限制 限流控制