当前位置:网站首页>剑指 Offer 53 - I. 在排序数组中查找数字 I(改进二分)
剑指 Offer 53 - I. 在排序数组中查找数字 I(改进二分)
2022-06-28 01:47:00 【BugMaker-shen】

二分查找找到一个target,然后往两侧开始计数
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){
// 找到
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;
}
};
我们要知道,当 n u m s [ m i d ] < t a r g e t nums[mid] < target nums[mid]<target时, l l l才会向右移动,也就是说, l l l不可能越过数组中任意一个 t a r g e t target target值。第一次 m i d mid mid指向 t a r g e t target target时,并不退出查找,而是继续循环,直到 l = = r l==r l==r才退出。
由于 l l l不会越过任意一个 t a r g e t target target值, r r r只可能指向不大于 t a r g e t target target的值,我们设定的退出条件是 l = = r l==r l==r,如果数组中存在target,那最终退出时, l l l和 r r r一定指向最左侧的 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){
// 这里mid可能指向target,不能使用r = mid - 1越过当前值
r = mid;
}else{
// 此时的mid小于target,l可以越过mid
l = mid + 1;
}
}
while(l < nums.size() && nums[l] == target){
l++;
cnt++;
}
return cnt;
}
};
边栏推荐
- Get 5 offers after being notified of layoffs
- Built in functions for MySQL database operations
- Summary of software testing tools in 2021 - fuzzy testing tools
- 读书,使人内心宁静
- A16z:元宇宙解锁游戏基础设施中的新机遇
- Heartless sword Chinese English bilingual poem 004 Sword
- 买股票应该下载什么软件最好最安全?
- 【522. 最长特殊序列 II】
- Différences d'utilisation entre IsEmpty et isblank
- 买股票通过券商经理的开户链接开户资金是否安全?想开户炒股
猜你喜欢

Domain Name System

Usage details of staticlayout

StaticLayout的使用详解

将PCAP转换为Json文件的神器:joy(安装篇)
![[today in history] June 18: JD was born; The online store platform Etsy was established; Facebook releases Libra white paper](/img/88/6cdd2b604522261e2a88020c5d6ae7.jpg)
[today in history] June 18: JD was born; The online store platform Etsy was established; Facebook releases Libra white paper

CMU puts forward a new NLP paradigm - reconstructing pre training, and achieving 134 high scores in college entrance examination English

多快好省,低门槛AI部署工具FastDeploy测试版来了!
![[today in history] June 3: Microsoft launched Bing search engine; Larry Roberts starts ARPANET; The father of Visual Basic was born](/img/9e/408ae022d9d5d8106e94e71b319d43.png)
[today in history] June 3: Microsoft launched Bing search engine; Larry Roberts starts ARPANET; The father of Visual Basic was born

Mixed programming of C language and assembly language in stm32

喜新厌旧?IT公司为什么宁愿花20k招人,也不愿涨薪留住老员工
随机推荐
Intel Ruixuan A380 graphics card will be launched in China
Severe Tire Damage:世界上第一个在互联网上直播的摇滚乐队
> Could not create task ‘:app:MyTest.main()‘. > SourceSet with name ‘main‘ not found.问题修复
[today in history] June 12: the United States entered the era of digital television; Mozilla's original developer was born; 3com merges with American Robotics
Usage details of staticlayout
Simple elk configuration to realize production level log collection and query practice
Initial linear regression
树莓派-环境设置和交叉编译
[today in history] June 16: Oracle Bone Inscriptions was established; Microsoft MSX was born; The inventor of fast Fourier transform was born
3年功能测试拿8K,被刚来的测试员反超,其实你在假装努力
Basic flask: template rendering + template filtering + control statement
[today in history] June 3: Microsoft launched Bing search engine; Larry Roberts starts ARPANET; The father of Visual Basic was born
数字化时代,企业须做好用户信息安全
导致系统性能失败的十个原因
Thesis reading: General advantageous transformers
apache、iis6、ii7独立ip主机屏蔽限制ip访问
如何编写简洁代码?(上)
[today in history] June 25: the father of notebook was born; Windows 98 release; First commercial use of generic product code
Online text batch inversion by line tool
[today in history] June 23: Turing's birthday; The birth of the founder of the Internet; Reddit goes online