当前位置:网站首页>剑指 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;
}
};
边栏推荐
- [today in history] May 31: the father of Amiga was born; The co developer of basic language was born; BlackBerry BBM shutdown
- Heartless sword English Chinese bilingual poem 004 Meditation
- A16z:元宇宙解锁游戏基础设施中的新机遇
- 喜新厌旧?IT公司为什么宁愿花20k招人,也不愿涨薪留住老员工
- Mixed programming of C language and assembly language in stm32
- 腾讯游戏发布40多款产品与项目 其中12款为新游戏
- Apache——阿帕奇简介
- Summary of software testing tools in 2021 - fuzzy testing tools
- be fond of the new and tired of the old? Why do it companies prefer to spend 20K on recruiting rather than raise salaries to retain old employees
- Interview: how do lists duplicate objects according to their attributes?
猜你喜欢

Arduino Esp8266 Web LED控制

Le routage des microservices de la passerelle a échoué au chargement des ressources statiques des microservices
![[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](/img/91/d7d6137b95f6348f71692164614340.png)
[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

3年功能测试拿8K,被刚来的测试员反超,其实你在假装努力

【Kotlin】在Android官方文档中对其语法的基本介绍和理解

初始线性回归

Why are so many people keen on big factories because of the great pressure and competition?

ByteDance Interviewer: how to calculate the memory size occupied by a picture
![[plug in -statistical] statistics the number of code lines and related data](/img/84/ad5e78f7e0ed86d9c21cabe97b9c8e.png)
[plug in -statistical] statistics the number of code lines and related data
![[today in history] June 19: iPhone 3GS launched; Pascal was born; Anti terrorist elite begins testing](/img/ec/90961351a0de1eac26dd003bf25d34.png)
[today in history] June 19: iPhone 3GS launched; Pascal was born; Anti terrorist elite begins testing
随机推荐
Online text batch inversion by line tool
面试:Bitmap像素内存分配在堆内存还是在native中
3年功能测试拿8K,被刚来的测试员反超,其实你在假装努力
Livedata interview question bank and answers -- 7 consecutive questions for livedata interview~
基于流的深度生成模型
"Everyday Mathematics" serial 53: February 21
StaticLayout的使用详解
Packet capturing and sorting out external Fiddler -- understanding the toolbar [1]
JDBC and MySQL databases
Artifact for converting pcap to JSON file: joy (installation)
Raspberry pie - environment settings and cross compilation
LiveData 面试题库、解答---LiveData 面试 7 连问~
横向滚动的RecycleView一屏显示五个半,低于五个平均分布
Interview: how do lists duplicate objects according to their attributes?
[today in history] June 19: iPhone 3GS launched; Pascal was born; Anti terrorist elite begins testing
Review the submission of small papers for 2022 spring semester courses
Usage details of staticlayout
Summary of software testing tools in 2021 - fuzzy testing tools
Différences d'utilisation entre IsEmpty et isblank
第一次使用gcc和makefile编写c程序