当前位置:网站首页>[sword finger offer] interview question 53-i: find the number 1 in the sorted array -- three templates for binary search
[sword finger offer] interview question 53-i: find the number 1 in the sorted array -- three templates for binary search
2022-07-27 15:52:00 【Jocelin47】

Method 1 : Binary search left and right boundaries
Reference resources labuladong Three templates of binary search , Search for target The left and right borders of .
You can also find the leftmost , Then find the same number after linear , But behind it is O(n) Complexity , If the same elements follow , There is no O(logn) fast .
Reference link :
https://mp.weixin.qq.com/s/M1KfTfNlu4OCK8i9PSAmug
Left closed right open and left closed right closed code :
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;
}
};
边栏推荐
- Summer Challenge harmonyos realizes a hand-painted board
- Use double stars instead of math.pow()
- C language: dynamic memory function
- Modify spark to support remote access to OSS files
- QT (IV) mixed development using code and UI files
- Hj8 consolidated statement record
- IP protocol of network layer
- C语言:动态内存函数
- C语言:数据的存储
- Fluent -- layout principle and constraints
猜你喜欢

flutter —— 布局原理与约束

Implement custom spark optimization rules

Binary Insertion Sort

网络层的IP协议

Half find

C language: string function and memory function

【剑指offer】面试题50:第一个只出现一次的字符——哈希表查找

Jump to the specified position when video continues playing

Insert sort directly

Causes and solutions of deadlock in threads
随机推荐
网络设备硬核技术内幕 路由器篇 22
复杂度分析
传美国政府将向部分美企发放对华为销售许可证!
Summer Challenge harmonyos realizes a hand-painted board
【剑指offer】面试题56-Ⅰ:数组中数字出现的次数Ⅰ
UDP message structure and precautions
线程间等待与唤醒机制、单例模式、阻塞队列、定时器
【剑指offer】面试题53-Ⅱ:0~n-1中缺失的数字——二分查找
Summary of network device hard core technology insider router (Part 2)
Huawei's general card identification function enables multiple card bindings with one key
网络原理(1)——基础原理概述
Zhaoqi scientific innovation and entrepreneurship competition planning and undertaking organization, mass entrepreneurship and innovation platform, project landing and docking
Interview focus - TCP protocol of transport layer
CAS比较交换的知识、ABA问题、锁升级的流程
Explanation of various attributes of "router link"
Spark troubleshooting finishing
股票开户佣金优惠,炒股开户哪家证券公司好网上开户安全吗
面试重点——传输层的TCP协议
[系统编程] 进程,线程问题总结
网络原理(2)——网络开发