forked from lennylxx/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
34.cpp
58 lines (49 loc) · 1.21 KB
/
34.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
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
vector<int> searchRange(vector<int>& nums, int target) {
vector<int> ans(2);
int n = nums.size();
int low = 0;
int high = n - 1;
int mid;
bool found = false;
while (low <= high) {
mid = low + (high - low) / 2;
if (nums[mid] == target) {
found = true;
break;
}
else if (nums[mid] < target) {
low = mid + 1;
}
else {
high = mid - 1;
}
}
if (found) {
low = high = mid;
while (low >= 0 && nums[low] == target) low--;
while (high <= n - 1 && nums[high] == target) high++;
ans[0] = low + 1;
ans[1] = high - 1;
}
else {
ans[0] = ans[1] = -1;
}
return ans;
}
};
int main () {
vector<int> nums = {5, 7, 7, 8, 8, 10};
int target = 8;
Solution s;
vector<int> ans = s.searchRange(nums, target);
for (int i = 0; i < ans.size(); i++) {
printf("%d ", ans[i]);
}
printf("\n");
return 0;
}