-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFirstAndLastPos.cpp
More file actions
63 lines (53 loc) · 1.36 KB
/
FirstAndLastPos.cpp
File metadata and controls
63 lines (53 loc) · 1.36 KB
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
#include<iostream>
#include<vector>
using namespace std;
// todo: Given an array of integers nums sorted in ascending order, find the
// starting and ending position of a given target value.
// If target is not found in the array, return [-1, -1].
// You must write an algorithm with O(log n) runtime complexity.
int binSearch(int st, int end, int target, vector<int> nums)
{
if(st <= end)
{
int mid = st+(end-st)/2;
if(nums[mid] == target)
return mid;
if(nums[mid] > target)
return binSearch(st, mid-1, target, nums);
else
return binSearch(mid+1, end, target, nums);
}
return -1;
}
vector<int> searchRange(vector<int>& nums, int target) {
vector<int> v;
if(nums.size() == 0)
{
v.push_back(-1);
v.push_back(-1);
return v;
}
int st = binSearch(0, nums.size()-1, target, nums);
if(st == -1)
{
v.push_back(-1);
v.push_back(-1);
return v;
}
int end = st;
while (st>0 && nums[st-1] == nums[st])
st--;
while ((end<nums.size()-1) && nums[end+1] == nums[end])
end++;
v.push_back(st);
v.push_back(end);
return v;
}
int main()
{
vector<int> v = {8,8};
v = searchRange(v, 8);
for(int i=0; i<v.size(); i++)
cout << v[i] << " ";
}
// LC: Q.34