当前位置:网站首页>34. 在排序数组中查找元素的第一个和最后一个位置 ●●
34. 在排序数组中查找元素的第一个和最后一个位置 ●●
2022-06-11 10:38:00 【chenyfan_】
34. 在排序数组中查找元素的第一个和最后一个位置 ●●
描述
给定一个按照升序排列的整数数组 nums,和一个目标值 target。
找出给定目标值在数组中的开始位置和结束位置。
如果数组中不存在目标值 target,返回 [-1, -1]。
进阶:
你可以设计并实现时间复杂度为 O(log n) 的算法解决此问题吗?
示例
输入:nums = [5,7,7,8,8,10], target = 8
输出:[3,4]
题解
1. 暴力
class Solution {
public:
vector<int> searchRange(vector<int>& nums, int target) {
// 1、遍历
int n = nums.size();
int left = -1; //左边界
int right = -1; //右边界
for ( int i = 0; i < n; ++i){
// 从左往右遍历
if (nums[i] == target){
if (left == -1){
left = i;
right = i;
}
else{
right = i;
}
}
}
return vector<int>{
left,right};
}
};
2. 二分查找
时间复杂度: O ( log n ) O(\log n) O(logn),其中 nn 为数组的长度。二分查找的时间复杂度为 O ( log n ) O(\log n) O(logn),一共会执行两次,因此总时间复杂度为 O ( log n ) O(\log n) O(logn)。
空间复杂度: O ( 1 ) O(1) O(1) 。只需要常数空间存放若干变量。
class Solution {
public:
vector<int> searchRange(vector<int>& nums, int target) {
// 找到第一个等于或大于target的数
int left = 0, right = nums.size() - 1;
while(left <= right){
int mid = (left + right) >> 1;
if(nums[mid] >= target){
right = mid - 1;
}else{
left = mid + 1;
}
}
int start = right + 1;
// 找到最后一个小于或等于target的数
left = start, right = nums.size() - 1;
while(left <= right){
int mid = (left + right) >> 1;
if(nums[mid] <= target){
left = mid + 1; // left 为 找到第一个大于target的数
}else{
right = mid - 1;
}
}
int end = left - 1; // left - 1为最后一个小于或等于target的数
if (start <= end){
// left > right 不存在target
return vector<int> {
start, end};
}
return vector<int> {
-1, -1};
}
};
边栏推荐
- minIni移植到littlefs
- Unity字体间距
- Window management learn more about windowmanagerservice
- Why is Web3 a game changer for the "creator economy"
- Dimension types for different CV tasks
- SAP Spartacus Reference App Structure
- 5. read the specified pathname -dirname
- 详述用网络分析仪测量DC-DC和PDN
- Using hystrix to implement fault-tolerant processing of microservices
- NewOJ Week 2---BCD
猜你喜欢

基于位置服务(LBS)的SSM的框架实现的兴趣社交软件平台设计与实现

Pl/sql compilation check in kingbasees

NFT products are alive

金仓数据库KingbaseES中的sys_checksums坏块检测功能

Cadence OrCAD capture design method to avoid misoperation graphic tutorial

Circuit board made of real gold -- golden finger

金仓数据库KingbaseES UDP监控工具的使用

Design and implementation of interest social software platform based on location service (LBS) SSM framework

MN梦奈宝塔主机系统V1.5版本发布

Tiktok encounters cultural conflict in the UK, and many employees leave in a short time
随机推荐
概率论:计算置信区间
金仓数据库KingbaseES中的sys_checksums坏块检测功能
VMware install win7 virtual machine
金仓数据库KingbaseES UDP监控工具的使用
使用 Ribbon 实现客户端负载均衡
Explain the physical layer consistency test of 2.5g/5g/10g Base-T Ethernet interface in detail!
云画质助手iApp源码
Pyspark case series 4-dataframe output to a single folder solution
2022年安全月各类活动方案汇报(28页)
Golang compilation and linking parameters, runtime
Design and implementation of interest social software platform based on location service (LBS) SSM framework
Ngui, chat scroll box, UI textlist
RxJs fromEvent 工作原理分析
Metro roadmap cloud development applet source code and configuration tutorial
Beginning an excellent emlog theme
Safety related website recommendations
NewOJ Week 2---BCD
MySQL foundation part common constraints summary part 2
GameFi:您需要了解的关于“即玩即赚”游戏经济的一切
FPGA infrastructure [reference ug998]