当前位置:网站首页>题解:在一个排序数组中查找元素第一个和最后一个的位置 (个人笔记)
题解:在一个排序数组中查找元素第一个和最后一个的位置 (个人笔记)
2022-07-29 05:09:00 【卷饼85】
题目
给你一个按照非递减顺序排列的整数数组 nums,和一个目标值 target。请你找出给定目标值在数组中的开始位置和结束位置。
如果数组中不存在目标值 target,返回 [-1, -1]。
你必须设计并实现时间复杂度为 O(log n) 的算法解决此问题。
示例 1:
输入:nums = [5,7,7,8,8,10], target = 8
输出:[3,4]
示例 2:
输入:nums = [5,7,7,8,8,10], target = 6
输出:[-1,-1]
示例 3:
输入:nums = [], target = 0
输出:[-1,-1]
提示:
0 <= nums.length <= 105
-109 <= nums[i] <= 109
nums 是一个非递减数组
-109 <= target <= 109
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/find-first-and-last-position-of-element-in-sorted-array
解题思路
采用二分查找
因为给的数组是升序排序,所以采用常规的二分法,判断是否有与target相等的值
如果有,则通过i++,来从i->mid中进行循环找出第一个与target相等的数组值的下表i,找到后break;
再通过j–,来从mid->j中进行循环找出第一个与target相等的数组值的下表j,找到后break;
最后返回return {i,j};
如果以上都不满足,则返回return {-1,-1};
时间复杂度O(n^2)
代码
class Solution {
public:
vector<int> searchRange(vector<int>& nums, int target) {
int i = 0 , j = nums.size()-1,mid=0;
while(i<=j)
{
mid = i + (j-i)/2;
if(nums[mid]>target)j=mid-1;
else if(nums[mid]==target)
{
for(;i<=mid;i++)
{
if(nums[i]==target){
break;}
}
for(;j>=mid;j--)
{
if(nums[j]==target){
break;}
}
return {
i,j};
}
else i = mid+1;
}
return {
-1,-1};
}
};
优化:
经过分析,可以给定一个bool类型来判断,如果数组中存在nums[mid]==target;
则bool类型的值为真,否则为假;
然后将原本在while循环里的for放到while循环外边
这样时间复杂度就变为O(log2^n)
代码
class Solution {
public:
vector<int> searchRange(vector<int>& nums, int target) {
int i = 0 , j = nums.size()-1,mid=0;
bool k = false;
while(i<=j)
{
mid = i + (j-i)/2;
if(nums[mid]>target)j=mid-1;
else if(nums[mid]==target)
{
k = true;break;
}
else i = mid+1;
}
if(k)
{
for(;i<=mid;i++)
{
if(nums[i]==target){
break;}
}
for(;j>=mid;j--)
{
if(nums[j]==target){
break;}
}
return {
i,j};
}
return {
-1,-1};
}
};
边栏推荐
- 321,京东言犀×NLPCC 2022挑战赛开赛!
- 文件结尾
- Vs code的安装步骤及环境配置
- 2022年泰迪杯数据挖掘挑战赛C题方案及赛后总结
- Qml类型:State 状态
- Why is Google's internal tools not suitable for you?
- 预约中,2022京东云产业融合新品发布会线上开启
- 365天挑战LeetCode1000题——Day 042 数组序号转换 + 相对名次 离散化处理
- Architecture analysis of three-tier project and parameter name injection of construction method
- C language handwritten qq-ai version
猜你喜欢
研发效能|Kubernetes核心技术剖析和DevOps落地经验
OCCT学习002-----环境搭建
Live broadcast preview | how to save 30% labor cost and shorten 80% trademark processing cycle?
51万奖池邀你参战!第二届阿里云ECS CloudBuild开发者大赛来袭
Container security open source detection tool - veinmind (mirror backdoor, malicious samples, sensitive information, weak password, etc.)
Li Yan, CEO of parallel cloud: cloudxr, opens the channel to the metauniverse
哈夫曼树以及哈夫曼编码在文件压缩上的应用
C语言 一级指针
Arfoundation starts from scratch 3- create an arfoundation project
容器安全开源检测工具--问脉 VeinMind(镜像后门、恶意样本、敏感信息、弱口令等)
随机推荐
7.2-function-overloading
Modification of annotation based three-tier project and the way of adding package scanning
ARFoundation从零开始8-Geospatial API(地理空间)开发
源码编译pytorch坑
NVIDIA Zhou Xijian: the last mile from design to digital marketing
During the appointment, the 2022 JD cloud industrial integration new product launch was launched online
Rimworld通过SteamCMD上传创意工坊的方法
【赛事预告】云上开发,高效智能——第二届阿里云ECS CloudBuild开发者大赛即将启动
Getting started with arfoundation tutorial 10- plane detection and placement
Webrtc audio anti weak network technology (Part 2)
In depth analysis of common cross end technology stacks of app
Arfoundation starts from scratch 8-geospatial API (geospatial) development
MySQL的详细安装使用教程(保姆式安装图文讲解)
This article takes you to understand the implementation of surround notification @around and final notification @after
Vs code的安装步骤及环境配置
Helm chart for Kubernetes
Yangyonglin, vice president of Rushi Technology: when traditional industries encounter "digital space"
C语言 一级指针
Handwritten student management system
SM integration is as simple as before, and the steps are clear (detailed)