-
Notifications
You must be signed in to change notification settings - Fork 0
/
34
67 lines (67 loc) · 1.85 KB
/
34
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
class Solution {
public:
vector<int> searchRange(vector<int>& nums, int target) {
vector<int> output{-1,-1};
if(nums.empty())
return output;
if(nums[0] > target || nums.back() < target)
return output;
int low = 0, low0 = 0, high = nums.size() - 1, mid = nums.size() / 2;
while(1){
if(low < nums.size() - 1){
if(nums[low + 1] >= target)
break;
}
else
break;
mid = (low + high) / 2;
if(nums[mid] < target)
low = mid;
else
high = mid;
}
low0 = low;
high = nums.size() - 1;
mid = nums.size() / 2;
while(1){
if(high >= 1){
if(nums[high - 1] <= target)
break;
}
else
break;
mid = (low + high) / 2;
if(nums[mid] > target)
high = mid;
else
low = mid;
}
low = low0;
if(low < high - 1){
if(nums[low] == target)
output[0] = low;
else
output[0] = low+1;
if(nums[high] == target)
output[1] = high;
else
output[1] = high - 1;
return output;
}
else if(low <= high){
if(nums[low] == target)
output[0] = low;
else if(nums[low + 1] == target)
output[0] = low + 1;
else
output[0] = -1;
if(nums[high] == target)
output[1] = high;
else if (nums[high - 1] == target)
output[1] = high - 1;
else output[1] = -1;
return output;
}
else return output;
}
};