-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathLC_34_FindFirstLastPositionElement.cpp
83 lines (67 loc) · 1.98 KB
/
LC_34_FindFirstLastPositionElement.cpp
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/
34. Find First and Last Position of Element in Sorted Array
*/
class Solution {
public:
vector<int> searchRange(vector<int>& nums, int target) {
int left=-1;
int right=-1;
//linear approach
// for(int i=0; i< nums.size(); i++){
// if(nums[i]!=target) continue;
// if(left==-1)
// left=i;
// right=i;
// }
//binary search approach
// left = binary_left(nums, target);
// right = binary_right(nums, target);
// return {left,right};
// cpp stl approach
int itl = lower_bound(nums.begin(), nums.end(), target) - nums.begin();
int itr = upper_bound(nums.begin(), nums.end(), target) - nums.begin() -1; //return value greater than target
if( itr<itl) return vector<int> {-1,-1};
return vector<int> {itl,itr};
}//end
int binary_left(vector<int>& nums, int target){
int n = nums.size();
int i=0;
int j=n-1;
int mid;
int res=-1;
while(i<=j){
mid=i+(j-i)/2;
if(nums[mid]<target)
i=mid+1;
else if(nums[mid]>target)
j=mid-1;
else
{
res=mid;
j=mid-1;
}
}
return res;
}
int binary_right(vector<int>& nums, int target){
int n = nums.size();
int i=0;
int j=n-1;
int mid;
int res=-1;
while(i<=j){
mid=i+(j-i)/2;
if(nums[mid]<target)
i=mid+1;
else if(nums[mid]>target)
j=mid-1;
else
{
res=mid;
i=mid+1;
}
}
return res;
}
};