当前位置:网站首页>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;
}
};
边栏推荐
- Reading notes of top performance version 2 (II) -- Performance observation tool
- Password encryption MD5 and salt treatment
- Difference between curdate() and now()
- With the transformation of automatic empowerment, Feihe dairy accelerates its move towards digitalization!
- 论文详读:IMPROVING CONVOLUTIONAL MODELS FOR HANDWRITTEN TEXT RECOGNITION
- 2022-06-27:给出一个长度为n的01串,现在请你找到两个区间, 使得这两个区间中,1的个数相等,0的个数也相等, 这两个区间可以相交,但是不可以完全重叠,即两个区间的左右端点不可以完全一样。
- 【Matlab红绿灯识别】红绿灯识别【含GUI源码 1908期】
- 华为9年经验的软件测试总监工作感悟—写给还在迷茫的朋友
- How to clean the nozzle of Epson l3153 printer
- A summary of my recent situation in June 2022
猜你喜欢

The second round of free public classes of the red team is coming ~ 8:00 tomorrow night!

UI自动化测试框架搭建 —— 编写一个APP自动化
![[applet] solution document using font awesome Font Icon (picture and text)](/img/1b/d1b738e6e35e59cc4a417df4ea0e8d.png)
[applet] solution document using font awesome Font Icon (picture and text)

Bitlock recovery occurs in win 10, and the blue screen error code is 0x1600007e

控制器的功能和工作原理

The coming wave of Web3

政策利好,20多省市开启元宇宙发展规划

Annual comprehensive analysis of China's audio market in 2022

The company leader said that if the personal code exceeds 10 bugs, he will be dismissed. What is the experience?

测试开发必备技能:安全测试漏洞靶场实战
随机推荐
With the transformation of automatic empowerment, Feihe dairy accelerates its move towards digitalization!
代码理解:IMPROVING CONVOLUTIONAL MODELS FOR HANDWRITTEN TEXT RECOGNITION
OracleData安装问题
政策利好,20多省市开启元宇宙发展规划
[Matlab bp regression prediction] GA Optimized BP regression prediction (including comparison before optimization) [including source code 1901]
CDC全量抽取mysql数据时,怎么才能加快ChunkSplitter呢?
Design a stack with getmin function
[applet] solution document using font awesome Font Icon (picture and text)
成长一夏 挑战赛来袭 | 学习、创作两大赛道,开启导师报名啦!
Why is the frame rate calculated by opencv wrong?
多线程实现 重写run(),怎么注入使用mapper文件操作数据库
【Matlab BP回归预测】GA优化BP回归预测(含优化前的对比)【含源码 1901期】
Digital promising, easy to reach, Huawei accelerates the layout of the commercial market with "five pole" star products
Annual comprehensive analysis of China's audio market in 2022
A summary of my recent situation in June 2022
Web3来临时的风口浪尖
在线直播源码,JS动态效果之,侧边栏滚动固定效果
Audio and video technology development weekly
Flinkcdc collects Oracle, and the Oracle database is CDB's
The company leader said that if the personal code exceeds 10 bugs, he will be dismissed. What is the experience?