当前位置:网站首页>剑指 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;
}
};
边栏推荐
- PSM总结
- How to run unity webgl after packaging (Firefox configuration)
- 简单ELK配置实现生产级别的日志采集和查询实践
- Usage details of staticlayout
- [postgraduate] bit by bit
- math_ (function & sequence) meaning of limit & misunderstanding and symbol sorting / neighborhood & de centring neighborhood & neighborhood radius
- [today in history] June 15: the first mobile phone virus; AI master simahe was born; Chromebook launch
- Online JSON to plaintext tool
- apache、iis6、ii7独立ip主机屏蔽拦截蜘蛛抓取(适用vps云主机服务器)
- 【522. 最长特殊序列 II】
猜你喜欢

Différences d'utilisation entre IsEmpty et isblank
![[today in history] June 15: the first mobile phone virus; AI master simahe was born; Chromebook launch](/img/d4/413c84a75f16a09867cfaa3d7f8736.png)
[today in history] June 15: the first mobile phone virus; AI master simahe was born; Chromebook launch

第一次使用gcc和makefile编写c程序

How to run unity webgl after packaging (Firefox configuration)

如何编写简洁代码?(上)

PSM总结

Gateway microservice routing failed to load microservice static resources

Simple file transfer protocol TFTP
![[today in history] June 24: Netease was established; The first consumer electronics exhibition was held; The first webcast in the world](/img/f7/b3239802d19d00f760bb3174649a89.jpg)
[today in history] June 24: Netease was established; The first consumer electronics exhibition was held; The first webcast in the world

分布式事务—基于消息补偿的最终一致性方案(本地消息表、消息队列)
随机推荐
微信小程序中生成二维码
RichView TRVStyle
论文阅读:Generative Adversarial Transformers
Différences d'utilisation entre IsEmpty et isblank
读书,使人内心宁静
分布式事务—基于消息补偿的最终一致性方案(本地消息表、消息队列)
[today in history] June 15: the first mobile phone virus; AI master simahe was born; Chromebook launch
Simple file transfer protocol TFTP
[today in history] June 24: Netease was established; The first consumer electronics exhibition was held; The first webcast in the world
Apache, IIS6, ii7 independent IP host shielding restricts IP access
[today in history] June 5: Lovelace and Babbage met; The pioneer of public key cryptography was born; Functional language design pioneer born
> Could not create task ‘:app:MyTest.main()‘. > SourceSet with name ‘main‘ not found.问题修复
Interview: how do lists duplicate objects according to their attributes?
apache、iis6、ii7独立ip主机屏蔽限制ip访问
在线文本按行批量反转工具
[today in history] June 23: Turing's birthday; The birth of the founder of the Internet; Reddit goes online
2-5 basic configuration -win2003 add attack surface
您的物联网安全性是否足够强大?
TensorRT 模型推理优化实现
[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