当前位置:网站首页>【剑指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;
}
};
边栏推荐
- Basic usage of kotlin
- Introduction of the connecting circuit between ad7606 and stm32
- Leetcode 244周赛-赛后补题题解【西兰花选手】
- Leetcode 781. rabbit hash table in forest / mathematical problem medium
- Digital storage oscilloscope based on FIFO idt7202-12
- 光电隔离电路设计方案(六款基于光耦、AD210AN的光电隔离电路图)
- Dan bin Investment Summit: on the importance of asset management!
- Leetcode interview question 17.21. water volume double pointer of histogram, monotonic stack /hard
- Spark TroubleShooting整理
- multimap案例
猜你喜欢

IJCAI 2022 outstanding papers were published, and 298 Chinese mainland authors won the first place in two items

EMC design scheme of CAN bus

Pictures to be delivered
USB interface electromagnetic compatibility (EMC) solution
MOS管防止电源反接的原理

STM32 CAN 通信 滤波设置问题

后台返回来的是这种数据,是什么格式啊

Spark 3.0 Adaptive Execution 代码实现及数据倾斜优化

/dev/loop1占用100%问题

QT (five) meta object properties
随机推荐
Network equipment hard core technology insider router 20 dpdk (V)
Unity 鼠标控制第一人称摄像机视角
Unity performance optimization ----- occlusion culling of rendering optimization (GPU)
Sword finger offer cut rope
学习Parquet文件格式
Network equipment hard core technology insider router Chapter 13 from deer by device to router (Part 1)
STM32F10x_ Hardware I2C read / write EEPROM (standard peripheral library version)
Spark 3.0 测试与使用
How to package AssetBundle
The design method of integral operation circuit is introduced in detail
shell脚本读取文本中的redis命令批量插入redis
CAN总线的EMC设计方案
4种单片机驱动继电器方案
Leetcode 244 week competition - post competition supplementary question solution [broccoli players]
STM32 can -- can ID filter analysis
Tools - common methods of markdown editor
Spark 任务Task调度异常分析
Leetcode 90. subset II backtracking /medium
使用双星号代替Math.pow()
STM32学习之CAN控制器简介