-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path34.cpp
More file actions
33 lines (33 loc) · 782 Bytes
/
Copy path34.cpp
File metadata and controls
33 lines (33 loc) · 782 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Solution {
public:
vector<int> searchRange(vector<int>& nums, int target) {
vector<int> found(2, 0);
int min = 0;
int max = nums.size()-1;
while(min <= max){
int mid = min + (max-min)/2;
if(nums[mid] == target){
int low = mid;
int high = mid;
while(low >= 0 && nums[low]==nums[mid]){
low--;
}
while(high < nums.size() && nums[high]==nums[mid]){
high++;
}
found[0] = ++low;
found[1] = --high;
return found;
}
else if(nums[mid] < target){
min = mid + 1;
}
else{
max = mid - 1;
}
}
found[0] = -1;
found[1] = -1;
return found;
}
};