当前位置:网站首页>【LeetCode-中等】34. 在排序数组中查找元素的第一个和最后一个位置 - 数组双指针
【LeetCode-中等】34. 在排序数组中查找元素的第一个和最后一个位置 - 数组双指针
2022-07-20 02:23:00 【qmkn】
34. 在排序数组中查找元素的第一个和最后一个位置
要注意特殊情况时的处理
方法一:遍历
class Solution {
public:
vector<int> searchRange(vector<int>& nums, int target) {
vector<int> result = {
-1,-1};
// nums为空
if(nums.empty()) return result;
int low = 0;
int high = nums.size() - 1;
// low < high : nums = [2, 2] target = 3
while(nums[low] < target && low < high){
low++;
}
if(nums[low] != target) return result;
while(nums[high] > target){
high--;
}
result[0] = low;
result[1] = high;
return result;
}
};
方法二:二分查找
因为整个数组是非递减排序,可以使用二分法寻找第一个等于(或大于) target 的位置 leftIdx 和 第一个大于 target 的位置减一 rightIdx
由于 nums 数组中可能不存在 target ,因此得到的两个下标 leftIdx 和 rightIdx 进行校验,看是否符合条件,如果符合条件就返回 [leftIdx, rightIdx],否则返回 [-1,-1]。
class Solution {
public:
// 二分查找,若 lower 为 true,查找第一个大于等于 target 的下标,为 false 则查找第一个大于 target 的下标
int binarySearch(vector<int>& nums, int target, bool lower) {
int left = 0, right = (int)nums.size() - 1, ans = (int)nums.size();
while (left <= right) {
int mid = (left + right) / 2;
if (nums[mid] > target || (lower && nums[mid] >= target)) {
right = mid - 1;
ans = mid;
} else {
left = mid + 1;
}
}
return ans;
}
vector<int> searchRange(vector<int>& nums, int target) {
int leftIdx = binarySearch(nums, target, true);
int rightIdx = binarySearch(nums, target, false) - 1;
if (leftIdx <= rightIdx && rightIdx < nums.size() && nums[leftIdx] == target && nums[rightIdx] == target) {
return vector<int>{
leftIdx, rightIdx};
}
return vector<int>{
-1, -1};
}
};
边栏推荐
- 已解决No module named ‘flask_misaka‘【BUG解决】
- Redux 原理
- HandBrake安装问题:提示安装frameworks .NET
- 数据库系统原理与应用教程(022)—— MySQL 支持的数据类型总结
- 数据库系统原理与应用教程(026)—— MySQL 修改表中数据(二):删(delete from)
- Tutorial on principles and applications of database system (028) -- data integrity of MySQL (I): overview of data integrity
- WebRTC系列-SDP之类关系梳理
- LeetCode_ 78_ subset
- JSP自定义标签(一篇学会,每一行代码都有注释)
- 元宇宙的发展也是一个逐渐成为人们的生产和生活方式的过程
猜你喜欢
HW2021攻防演练经历碎碎念-见解
网络安全—综合渗透测试-CVE-2019-7238-Nexus远程代码执行
Explain the RDB and AOF of redis in detail
测试人遇到难以重现的bug,要怎么办?
infraversion和superaversion
力扣第 302 场周赛复盘
机器学习练习 8 -异常检测和推荐系统(协同过滤)
Ppde Q2 welcome | welcome 22 AI developers to join the propeller developer technical expert program!
Message queuing - getting started with message queuing
PHP(1)
随机推荐
Ppde Q2 welcome | welcome 22 AI developers to join the propeller developer technical expert program!
中职网络安全技能大赛P100-mssql数据库渗透测试
W806开发板体验
Day106.尚医通:数据字典列表、EasyExcel、数据字典导入导出、集成Redis缓存
中職網絡安全—2022年國賽逆向PE Reverse解題思路
Network security comprehensive penetration test cve-2019-7238-nexus remote code execution
iMeta | ggClusterNet微生物网络分析和可视化保姆级教程
ICLR 2022 | GNNAsKernel: 能提升任意GNN表达能力的通用框架
Leetcode exercises grammar supplementary summary notes
洛谷 P2440 木材加工
WebRTC系列-SDP之类关系梳理
剑指 Offer II 041. 滑动窗口的平均值_____使用队列 / 循环数组实现
NC | 全球变化背景下溶解有机碳和微生物的生态网络关系研究取得进展
Unity:文本输入框进行数值判定
力扣第 302 场周赛复盘
Harbor - image warehouse
iptables防止nmap端口扫描
mysql 排序索引字段的优化
Handbrake installation problem: prompt to install frameworks NET
网络安全—综合渗透测试-CVE-2017-12629-Apache Solr远程代码执行