File tree Expand file tree Collapse file tree 1 file changed +56
-0
lines changed Expand file tree Collapse file tree 1 file changed +56
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution {
2+ public:
3+ int firstOcc (vector<int >& nums, int target){
4+ int s=0 ;
5+ int e=nums.size ()-1 ;
6+ int mid=0 ;
7+ int p=-1 ;
8+ while (s<=e){
9+ mid=s+(e-s)/2 ;
10+ if (nums[mid]==target){
11+ p=mid;
12+ e=mid-1 ;
13+ }
14+ if (nums[mid]>target){
15+
16+ e=mid-1 ;
17+ }
18+ if (nums[mid]<target){
19+
20+ s=mid+1 ;
21+ }
22+
23+ }
24+ return p;
25+ }
26+ int lastOcc (vector<int >& nums, int target){
27+ int s=0 ;
28+ int e=nums.size ()-1 ;
29+ int mid=0 ;
30+ int p=-1 ;
31+ while (s<=e){
32+ mid=s+(e-s)/2 ;
33+ if (nums[mid]==target){
34+ p=mid;
35+ s=mid+1 ;
36+ }
37+ if (nums[mid]>target){
38+
39+ e=mid-1 ;
40+ }
41+ if (nums[mid]<target){
42+
43+ s=mid+1 ;
44+ }
45+
46+ }
47+ return p;
48+ }
49+
50+ vector<int > searchRange (vector<int >& nums, int target) {
51+ vector<int > ans;
52+ ans.push_back (firstOcc (nums,target));
53+ ans.push_back (lastOcc (nums,target));
54+ return ans;
55+ }
56+ };
You can’t perform that action at this time.
0 commit comments