当前位置:网站首页>Location of dichotomy
Location of dichotomy
2022-06-30 14:37:00 【Douglas_ LT】
A daily topic ing, Today is a day medium topic 34. Find First and Last Position of Element in Sorted Array
class Solution {
public:
vector<int> searchRange(vector<int>& nums, int target) {
int l=0,r=nums.size()-1,mid=0;
while(l<=r){
mid=(l+r)/2;
if(target>=nums[mid]){
l=mid+1;
}
else if(target<nums[mid]){
r=mid-1;
}
}
vector<int> ret(2,-1);
if(r==-1||nums[r]!=target){
return ret;
}
int i=r;
while(i>=0&&nums[i]==target){
i--;
}
ret[0]=i+1;
int j=r;
while(j<=nums.size()-1&&nums[j]==target){
j++;
}
ret[1]=j-1;
return ret;
}
};
边栏推荐
- Summary of FTP network protocol learning
- Race of golang
- JS to realize simple lottery function
- Upgrade centos7 mysql5.5 to mysql5.7 non RPM in the form of tar package
- V3 02——What‘s new in Chrome extensions
- V3 03_ Getting started
- Basic learning notes of C language
- Mysql database foundation: stored procedures and functions
- MySQL back to table query optimization
- About the problems encountered when using the timer class to stop with a button (why does the QPushButton (for the first time) need to be clicked twice to respond?)
猜你喜欢

DefCamp Capture the Flag (D-CTF) 2021-22 web

ThinkPHP v3.2 comment annotation injection write shell

Jetpack compose for perfect screen fit
![[geek challenge 2019] PHP problem solving record](/img/bf/038082e8ee1c91eaf6e35add39f760.jpg)
[geek challenge 2019] PHP problem solving record

Ctfshow getting started with the web (ThinkPHP topic)

JS to realize simple lottery function

【BUUCTF】 Have Fun

go channel && select

Getting started with shell Basics

Shell programming overview
随机推荐
val_ Loss decreases first and then increases or does not decrease but only increases
PHP 2D array change key name
notepad正则删除关键词所在行
Four isolation levels of MySQL
2021-08-07 native and package types
Implement a long-click list pop-up box on apiccloud
Go language mutex lock
How to realize selective screen recording for EV screen recording
Use of laravel repository mode
2021-08-05 leetcode notes
After the MySQL service on the local computer is started and stopped, some services will automatically stop when they are not used by other services or programs
Laravel artist command error
The difference between settimeout() and setinterval()
Data recovery software easyrecovery15 Download
JS array
Laravel configures passport and returns token using JWT
2021-05-12
On simple code crawling Youdao translation_ 0's problem (to be solved)
@component使用案例
Go common lock mutex and rwmutex