当前位置:网站首页>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;
}
};
边栏推荐
- Excel knowledge and skills summary
- leetcode:714. The best time to buy and sell stocks includes handling fee [DP dual status]
- 抖音實戰~關注博主
- mysql修改密码报错需要怎么做
- RT-Thread 双向链表(学习笔记)
- A summary of my recent situation in June 2022
- 设计一个有getMin功能的栈
- [matlab traffic light identification] traffic light identification [including GUI source code 1908]
- native关键字的作用
- 代码理解:IMPROVING CONVOLUTIONAL MODELS FOR HANDWRITTEN TEXT RECOGNITION
猜你喜欢

2022-06-27:给出一个长度为n的01串,现在请你找到两个区间, 使得这两个区间中,1的个数相等,0的个数也相等, 这两个区间可以相交,但是不可以完全重叠,即两个区间的左右端点不可以完全一样。
![[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)

Why is the frame rate calculated by opencv wrong?

Audio and video technology development weekly

如何遍历collections.OrderedDict,服了又忘记items

Multithreading and high concurrency IV: varhandle, strong weak virtual reference and ThreadLocal

Web3来临时的风口浪尖

TACo:一种关于文字识别的数据增强技术

Multi project design and development · introduction to class library project

公司领导说,个人代码超10个Bug就开除,是什么体验?
随机推荐
Go language learning tutorial (14)
Lamaba expression learning and common functional interfaces
Little knowledge about function templates --
简单工厂模式
Multithreading and high concurrency V: detailed explanation of wait queue, executor and thread pool (key)
leetcode - 329. Longest increasing path in matrix
有大佬出现过mysql cdc用 datastream时,出现重复binlog消息的情况吗
Severe tire damage: the first rock band in the world to broadcast live on the Internet
Digital promising, easy to reach, Huawei accelerates the layout of the commercial market with "five pole" star products
Short video platform development, click links and pictures to automatically jump to a new page
How to traverse collections Ordereddict, taking it and forgetting items
native关键字的作用
Lingge leangoo agile Kanban tool adds the functions of exporting card documents and pasting shared brain map nodes
Database garbled
Multithreading and high concurrency six: source code analysis of thread pool
How to clean the nozzle of Epson l3153 printer
27 years, Microsoft IE is over!
【Matlab BP回归预测】GA优化BP回归预测(含优化前的对比)【含源码 1901期】
[proteus simulation] timer 1 external counting interrupt
Reading notes of top performance version 2 (II) -- Performance observation tool