Search for a Range
描述
Given a sorted array of integers, find the starting and ending position of a given target value.
Your algorithm's runtime complexity must be in the order of O(log n)
.
If the target is not found in the array, return [-1, -1]
.
For example,
Given [5, 7, 7, 8, 8, 10]
and target value 8,
return [3, 4]
.
分析
已经排好了序,用二分查找。
重新实现 lower_bound 和 upper_bound
- Java
- C++
// Search for a Range
// 重新实现 lower_bound 和 upper_bound
// 时间复杂度O(logn),空间复杂度O(1)
public class Solution {
public int[] searchRange(int[] nums, int target) {
int lower = lower_bound(nums, 0, nums.length, target);
int upper = upper_bound(nums, 0, nums.length, target);
if (lower == nums.length || nums[lower] != target)
return new int[]{-1, -1};
else
return new int[]{lower, upper-1};
}
int lower_bound (int[] A, int first, int last, int target) {
while (first != last) {
int mid = first + (last - first) / 2;
if (target > A[mid]) first = ++mid;
else last = mid;
}
return first;
}
int upper_bound (int[] A, int first, int last, int target) {
while (first != last) {
int mid = first + (last - first) / 2;
if (target >= A[mid]) first = ++mid; // 与 lower_bound 仅此不同
else last = mid;
}
return first;
}
}
// Search for a Range
// 重新实现 lower_bound 和 upper_bound
// 时间复杂度O(logn),空间复杂度O(1)
class Solution {
public:
vector<int> searchRange (vector<int>& nums, int target) {
auto lower = lower_bound(nums.begin(), nums.end(), target);
auto uppper = upper_bound(lower, nums.end(), target);
if (lower == nums.end() || *lower != target)
return vector<int> { -1, -1 };
else
return vector<int> {distance(nums.begin(), lower), distance(nums.begin(), prev(uppper))};
}
template<typename ForwardIterator, typename T>
ForwardIterator lower_bound (ForwardIterator first,
ForwardIterator last, T value) {
while (first != last) {
auto mid = next(first, distance(first, last) / 2);
if (value > *mid) first = ++mid;
else last = mid;
}
return first;
}
template<typename ForwardIterator, typename T>
ForwardIterator upper_bound (ForwardIterator first,
ForwardIterator last, T value) {
while (first != last) {
auto mid = next(first, distance (first, last) / 2);
if (value >= *mid) first = ++mid; // 与 lower_bound 仅此不同
else last = mid;
}
return first;
}
};