当前位置:网站首页>[leetcode medium] 34. Find the first and last positions of elements in the sorted array - array double pointer
[leetcode medium] 34. Find the first and last positions of elements in the sorted array - array double pointer
2022-07-25 03:18:00 【qmkn】
34. Find the first and last positions of the elements in the sort array 
Pay attention to the handling of special situations
Method 1 : Traverse
class Solution {
public:
vector<int> searchRange(vector<int>& nums, int target) {
vector<int> result = {
-1,-1};
// nums It's empty
if(nums.empty()) return result;
int low = 0;
int high = nums.size() - 1;
// low < high : nums = [2, 2] target = 3
while(nums[low] < target && low < high){
low++;
}
if(nums[low] != target) return result;
while(nums[high] > target){
high--;
}
result[0] = low;
result[1] = high;
return result;
}
};
Method 2 : Two points search
Because the whole array is non decreasing , You can use dichotomy to find The first is equal to ( Or greater than ) target The location of leftIdx and The first one is greater than target Minus one rightIdx
because nums May not exist in the array target , So we get two subscripts leftIdx and rightIdx check , See if they meet the requirements , If the conditions are met, return [leftIdx, rightIdx], Otherwise return to [-1,-1].
class Solution {
public:
// Two points search , if lower by true, Find the first one greater than or equal to target The subscript , by false Then find the first one greater than target The subscript
int binarySearch(vector<int>& nums, int target, bool lower) {
int left = 0, right = (int)nums.size() - 1, ans = (int)nums.size();
while (left <= right) {
int mid = (left + right) / 2;
if (nums[mid] > target || (lower && nums[mid] >= target)) {
right = mid - 1;
ans = mid;
} else {
left = mid + 1;
}
}
return ans;
}
vector<int> searchRange(vector<int>& nums, int target) {
int leftIdx = binarySearch(nums, target, true);
int rightIdx = binarySearch(nums, target, false) - 1;
if (leftIdx <= rightIdx && rightIdx < nums.size() && nums[leftIdx] == target && nums[rightIdx] == target) {
return vector<int>{
leftIdx, rightIdx};
}
return vector<int>{
-1, -1};
}
};
边栏推荐
- Learning record XIII
- Hal library serial port for note taking
- Hashcode details
- Dc-1-practice
- [stm32f130rct6] idea and code of ultrasonic ranging module
- [jailhouse article] certify the uncertified rewards assessment of virtualization for mixed criticality
- hello csdn
- Import and export using poi
- mysql_ Case insensitive
- 55k is stable, and the recommendation system will always drop God!
猜你喜欢

JS foundation -- JSON

mysql_ Record the executed SQL

Chrome process architecture

Dc-1-practice

Import and export using poi

Direct insert sort / Hill sort

JS foundation -- math

Vscode configuration, eslint+prettier combined with detailed configuration steps, standardized development

Use and introduction of vim file editor

Use reflection to convert RDD to dataframe
随机推荐
[stm32f130rct6] idea and code of ultrasonic ranging module
NVM installation and use
Select sort / cardinality sort
Hal library serial port for note taking
Resolve the error: org.apache.ibatis.binding.bindingexception
Solution: owner's smart site supervision cloud platform
Riotboard development board series notes (4) -- using Vpu hardware decoding
Backtracking to solve combinatorial problems
Using one stack to sort another stack
Edit mathematical formulas in markdown
B. Making Towers
Innobackupex parameter description
Import and export using poi
JS construct binary tree
Solve mysql5.6 database specified key was too long; Max key length is 767 bytes
Interview question -- event cycle
mysql_ User table_ Field meaning
List title of force buckle summary
Dc-1-practice
55k is stable, and the recommendation system will always drop God!