当前位置:网站首页>2022-7-7 Leetcode 34. Find the first and last positions of elements in a sorted array
2022-7-7 Leetcode 34. Find the first and last positions of elements in a sorted array
2022-07-07 13:39:00 【weixin_ fifty-one million one hundred and eighty-seven thousand】
1、 The left and right intervals should be searched separately , The time complexity is O ( l o g N ) O(logN) O(logN)
2、 When looking for the right boundary , Shrink left to right ; When looking for the left boundary , The right side shrinks to the left .
class Solution {
public:
int getR(vector<int>& nums, int target){
int l = 0, r = nums.size()-1;
int rightborder = -1;
while (l <= r){
int mid = l + (r - l)/2;
if (nums[mid] == target){
l = mid + 1;
rightborder = mid;
}else if (nums[mid] > target){
r = mid - 1;
}else {
l = mid + 1;
}
}
return rightborder;
}
int getL(vector<int>& nums, int target){
int l = 0, r = nums.size()-1;
int leftborder = -1;
while (l <= r){
int mid = l + (r - l)/2;
if (nums[mid] == target){
r = mid - 1;
leftborder = mid;
}else if (nums[mid] > target){
r = mid - 1;
}else {
l = mid + 1;
}
}
return leftborder;
}
vector<int> searchRange(vector<int>& nums, int target) {
int leftborder = getL(nums, target);
int rightborder = getR(nums, target);
return {
leftborder, rightborder};
}
};
边栏推荐
- [dark horse morning post] Huawei refutes rumors about "military master" Chen Chunhua; Hengchi 5 has a pre-sale price of 179000 yuan; Jay Chou's new album MV has played more than 100 million in 3 hours
- 我那“不好惹”的00后下属:不差钱,怼领导,抵制加班
- QQ的药,腾讯的票
- Introduction and basic use of stored procedures
- 2022-7-6 Leetcode 977.有序数组的平方
- MongoDB 遇见 spark(进行整合)
- Sliding rail stepping motor commissioning (national ocean vehicle competition) (STM32 master control)
- Mongodb meets spark (for integration)
- Signal strength (RSSI) knowledge sorting
- Clion mingw64 Chinese garbled code
猜你喜欢
Navicat运行sql文件导入数据不全或导入失败
cmake 学习使用笔记(一)
10 pictures open the door of CPU cache consistency
[dark horse morning post] Huawei refutes rumors about "military master" Chen Chunhua; Hengchi 5 has a pre-sale price of 179000 yuan; Jay Chou's new album MV has played more than 100 million in 3 hours
ESP32 ① 编译环境
我那“不好惹”的00后下属:不差钱,怼领导,抵制加班
如何让join跑得更快?
Ways to improve the performance of raspberry pie
Introduce six open source protocols in detail (instructions for programmers)
LIS longest ascending subsequence problem (dynamic programming, greed + dichotomy)
随机推荐
Custom thread pool rejection policy
供应链供需预估-[时间序列]
MongoDB的导入导出、备份恢复总结
Shell batch file name (excluding extension) lowercase to uppercase
Sliding rail stepping motor commissioning (national ocean vehicle competition) (STM32 master control)
提升树莓派性能的方法
Esp32 construction engineering add components
TPG x AIDU | AI leading talent recruitment plan in progress!
PAcP learning note 3: pcap method description
Data refresh of recyclerview
Indoor ROS robot navigation commissioning record (experience in selecting expansion radius)
JS slow motion animation principle teaching (super detail)
【面试高频题】难度 2.5/5,简单结合 DFS 的 Trie 模板级运用题
cmake 学习使用笔记(一)
Introduce six open source protocols in detail (instructions for programmers)
118. 杨辉三角
Clion mingw64 Chinese garbled code
Fast development board pinctrl and GPIO subsystem experiment for itop-imx6ull - modify the device tree file
User management summary of mongodb
Read PG in data warehouse in one article_ stat