当前位置:网站首页>【剑指offer】面试题53-Ⅰ:在排序数组中查找数字1 —— 二分查找的三个模版
【剑指offer】面试题53-Ⅰ:在排序数组中查找数字1 —— 二分查找的三个模版
2022-07-27 14:24:00 【Jocelin47】

方法一:二分查找左右边界
参考labuladong的二分查找的三个模版,进行查找target的左右边界。
也可以找到最左边,然后线性找后面相同的数字,但是后面是O(n)的复杂度,如果后面都是相同的元素,就没有O(logn)快。
参考链接:
https://mp.weixin.qq.com/s/M1KfTfNlu4OCK8i9PSAmug
左闭右开以及左闭右闭的代码:
class Solution {
public:
int search(vector<int>& nums, int target) {
// int l =0,r=nums.size();
// while(l<r){
// int mid=(l+r)>>1;
// if(nums[mid]<target)l=mid+1;
// else r=mid;
// }
// if(l==nums.size()||nums[l]!=target) return 0;
// int left=l;
// l=0;
// r=nums.size();
// while(l<r){
// int mid=(l+r)>>1;
// if(nums[mid]>target) r=mid;
// else l=mid+1;
// }
// return l-1-left+1;
int left = getFirstTarget( 0, nums.size() - 1 , target, nums);
if( left == -1 )
return 0;
int right = getLastTarget( 0, nums.size() - 1 , target, nums);
return right - left + 1;
}
int getFirstTarget( int left, int right, int target, vector<int>& nums )
{
while(left <= right)
{
int mid = left + ( right - left ) / 2;
if( nums[mid] == target )
right = mid - 1;
else if( nums[mid] > target )
right = mid - 1;
else if( nums[mid] < target )
left = mid + 1;
}
if( left >= nums.size() || nums[left] != target )
return -1;
return left;
}
int getLastTarget( int left, int right,int target, vector<int>& nums )
{
while( left <= right )
{
int mid = ( left + right ) / 2;
if( nums[mid] == target )
left = mid + 1;
else if( nums[mid] > target )
right = mid - 1;
else if( nums[mid] < target )
left = mid + 1;
}
if( right < 0 || nums[right] != target )
return -1;
return right;
}
};
边栏推荐
- How to package AssetBundle
- Multi table query_ Sub query overview and multi table query_ Sub query situation 1 & situation 2 & situation 3
- Network equipment hard core technology insider router 19 dpdk (IV)
- Wechat applet realizes music search page
- With just two modifications, apple gave styleganv2 3D generation capabilities
- Principle of MOS tube to prevent reverse connection of power supply
- Dan bin Investment Summit: on the importance of asset management!
- 《终身成长》读书笔记(一)
- Sword finger offer cut rope
- Cap theory and base theory
猜你喜欢

DIY ultra detailed tutorial on making oscilloscope: (1) I'm not trying to make an oscilloscope

Unity 鼠标控制第一人称摄像机视角

Leetcode 781. rabbit hash table in forest / mathematical problem medium

西瓜书《机器学习》阅读笔记之第一章绪论

华云数据打造完善的信创人才培养体系 助力信创产业高质量发展

Four kinds of relay schemes driven by single chip microcomputer

CAN总线的EMC设计方案

Several basic uses of tl431-2.5v voltage reference chip

4种单片机驱动继电器方案

Selenium 报错:session not created: This version of ChromeDriver only supports Chrome version 81
随机推荐
谷粒商城配置CorsWebFilter后,报错:Resource sharing error:MultipleAllowOriginValues
Digital storage oscilloscope based on FIFO idt7202-12
使用解构交换两个变量的值
修改 Spark 支持远程访问OSS文件
《剑指Offer》剪绳子
With just two modifications, apple gave styleganv2 3D generation capabilities
Unity performance optimization ----- LOD (level of detail) of rendering optimization (GPU)
实现自定义Spark优化规则
What is the breakthrough point of digital transformation in the electronic manufacturing industry? Lean manufacturing is the key
Cap theory and base theory
一文读懂鼠标滚轮事件(wheelEvent)
Spark Filter算子在Parquet文件上的下推
光电隔离电路设计方案(六款基于光耦、AD210AN的光电隔离电路图)
"Sword finger offer" linked list inversion
Network equipment hard core technology insider router Chapter 9 Cisco asr9900 disassembly (II)
Network equipment hard core technology insider router Chapter 18 dpdk and its prequel (III)
Network equipment hard core technology insider router Chapter 5 tompkinson roaming the network world (Part 1)
2022-07-27 Daily: IJCAI 2022 outstanding papers were published, and 298 Chinese mainland authors won the first place in two items
《剑指Offer》 合并两个排序的链表
shell脚本读取文本中的redis命令批量插入redis