-
Notifications
You must be signed in to change notification settings - Fork 481
/
0034.cpp
44 lines (43 loc) · 1.06 KB
/
0034.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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
static int x = [](){std::ios::sync_with_stdio(false);cin.tie(0);return 0;}();
class Solution {
public:
vector<int> searchRange(vector<int>& nums, int target)
{
int l = 0, r = nums.size();
vector<int> result(2, -1);
while (l < r)
{
int mid = (r - l)/2 + l;
if (nums[mid] >= target)
{
if (nums[mid] == target) result[0] = mid;
r = mid;
}
else l = mid + 1;
}
l = 0; r = nums.size();
while (l < r)
{
int mid = (r - l)/2 + l;
if (nums[mid] > target) r = mid;
else
{
if (nums[mid] == target) result[1] = mid;
l = mid + 1;
}
}
return result;
}
};
int main()
{
vector<int> arr = { 3, 2, 2, 3};
int target = 8;
auto result = Solution().searchRange(arr, target);
cout << result[0] << " " << result[1] << endl;
return 0;
}