当前位置:网站首页>Solution: find the position of the first and last element in a sorted array (personal notes)
Solution: find the position of the first and last element in a sorted array (personal notes)
2022-07-29 05:27:00 【Tortilla 85】
Find the first and last positions of elements in a sorted array
subject
Give you an array of integers in non decreasing order nums, And a target value target. Please find out the start position and end position of the given target value in the array .
If the target value does not exist in the array target, return [-1, -1].
You must design and implement a time complexity of O(log n) The algorithm to solve this problem .
Example 1:
Input :nums = [5,7,7,8,8,10], target = 8
Output :[3,4]
Example 2:
Input :nums = [5,7,7,8,8,10], target = 6
Output :[-1,-1]
Example 3:
Input :nums = [], target = 0
Output :[-1,-1]
Tips :
0 <= nums.length <= 105
-109 <= nums[i] <= 109
nums It is a group of non decreasing numbers
-109 <= target <= 109
source : Power button (LeetCode)
link :https://leetcode.cn/problems/find-first-and-last-position-of-element-in-sorted-array
Their thinking
Use binary search
Because the array given is sorted in ascending order , So the conventional dichotomy , Judge whether there is any connection with target Equal value
If there is , Through i++, Come from i->mid Loop through to find the first and target The following table of equal array values i, After finding break;
Re pass j–, Come from mid->j Loop through to find the first and target The following table of equal array values j, After finding break;
Finally back to return {i,j};
If none of the above is satisfied , Then return to return {-1,-1};
Time complexity O(n^2)
Code
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};
}
};
Optimize :
Through the analysis of , I can give you a bool Type to judge , If... Exists in the array nums[mid]==target;
be bool Value of type is true , Otherwise it is false ;
Then put the original in while In circulation for Put it in while Outside the loop
In this way, the time complexity becomes O(log2^n)
Code
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};
}
};
边栏推荐
- Xiaobai high salary shortcut Qt development game Snake
- 数据泄漏、删除事件频发,企业应如何构建安全防线?
- 365天挑战LeetCode1000题——Day 038 公交站间的距离 + 基于时间的键值存储 + 转变数组后最接近目标值的数组和 + 有界数组中指定下标处的最大值
- How rimworld uploads creative workshops through steamcmd
- Live broadcast Preview: integration of JD cloud Devops and jfrog product library
- Vs code的安装步骤及环境配置
- C语言 N皇后问题
- Webrtc audio anti weak network technology (Part 2)
- Teardown's method of lifting the time limit
- 时间复杂度和空间复杂度
猜你喜欢

小白高薪捷径-Qt开发游戏—贪吃蛇

预约中,2022京东云产业融合新品发布会线上开启

C 语言手写 QQ-AI 版
![[event preview] cloud development, efficient and intelligent - the second Alibaba cloud ECS cloudbuild developer competition is about to start](/img/6e/6b4deeedbfd9d6baa651019f3dabfa.jpg)
[event preview] cloud development, efficient and intelligent - the second Alibaba cloud ECS cloudbuild developer competition is about to start

365 day challenge leetcode 1000 questions - day 037 elements and the maximum side length of squares less than or equal to the threshold + the number of subsequences that meet the conditions

平行云CEO 李岩:CloudXR ,开启通往元宇宙的通道

Come on! See how Clickhouse, which has risen 16 places a year, can be implemented in jd.com

Cmu15-213 malloc lab experiment record

一维数组练习

最新坦克大战2022-全程开发笔记-1
随机推荐
The latest tank battle 2022 - Notes on the whole development -2
水一篇图的拓扑排序
整数溢出和打印
京东云金秋上云特惠进行中!扫码参与活动
携手数字人、数字空间、XR平台,阿里云与伙伴共同建设“新视界”
C language one-dimensional array
Getting started with arfoundation tutorial 10- plane detection and placement
串口通讯部分详解
osg3.6.5编译freetype失败
为啥谷歌的内部工具不适合你?
直播预告:京东云DevOps与JFrog制品库的融合
Live broadcast preview | how to improve enterprise immunity through "intelligent edge security"?
副作用和序列点
小白高薪捷径-Qt开发游戏—贪吃蛇
Qt版的贪食蛇游戏项目
CryEngine3 调试Shader方法
【C语言系列】—三种方法模拟实现strlen库函数的方法
vim编辑器使用
C语言函数实现输出I love you
Yangyonglin, vice president of Rushi Technology: when traditional industries encounter "digital space"