forked from diwakar1593/CPP-Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfrst_lst_position.cpp
More file actions
27 lines (27 loc) · 852 Bytes
/
frst_lst_position.cpp
File metadata and controls
27 lines (27 loc) · 852 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
class Solution {
public:
vector<int> searchRange(vector<int>& nums, int target) {
vector<int> ans(2, -1);
int start = 0, end = (int)nums.size() - 1;
while(start <= end) {
int mid = start + (end-start)/2;
if(nums[mid] < target) start = mid+1;
else if(nums[mid]>target) end=mid-1;
else {
if(nums[mid] == target) ans[0] = mid;
end = mid-1;
}
}
start = 0, end = (int)nums.size() - 1;
while(start <= end) {
int mid = start + (end-start)/2;
if(nums[mid] > target) end = mid-1;
else if(nums[mid] < target) start = mid+1;
else {
if(nums[mid] == target) ans[1] = mid;
start = mid+1;
}
}
return ans;
}
};