feat: 34_find_first_and_last_position_of_element_in_sorted_array
This commit is contained in:
parent
9acabc6597
commit
944168338c
@ -0,0 +1,16 @@
|
|||||||
|
#include <bits/stdc++.h>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
vector<int> searchRange(vector<int>& nums, int target) {
|
||||||
|
if (!binary_search(nums.begin(), nums.end(), target)) {
|
||||||
|
return {-1, -1};
|
||||||
|
}
|
||||||
|
|
||||||
|
auto first = lower_bound(nums.begin(), nums.end(), target);
|
||||||
|
auto second = upper_bound(nums.begin(), nums.end(), target);
|
||||||
|
|
||||||
|
return {(int)(first - nums.begin()), (int)(second - nums.begin() - 1)};
|
||||||
|
}
|
||||||
|
};
|
Loading…
x
Reference in New Issue
Block a user