1. 基本二分查找
class Solution {
public:
int search(vector<int>& nums, int target) {
int n = nums.size() - 1;
int left = 0;
int right = n;
while (left <= right) {
int mid = left + (right - left) / 2;
if (nums[mid] > target) // target 在左区间,在[left, middle)中
right = mid - 1;
else if (nums[mid] < target) // target 在右区间,在[middle + 1, right)中
left = mid + 1;
else if (nums[mid] == target)// nums[mid] == target
return mid;
}
return -1;
}
};
此处while( left <= right ),target在一个左闭右闭区间[left, right],故当nums[mid] > target时,nums[middle]一定不是target, 令right = mid - 1;
若while( left < right ),target在一个左闭右开区间[left, right),故当nums[mid] > target时,令right = mid;
2. 寻找排序数组左侧边界
int searchleft(vector<int>& nums, int target) {
int index = -1;
int left = 0;
int right = nums.size() - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (nums[mid] > target)
right = mid - 1;
else if (nums[mid] < target)
left = mid + 1;
else if (nums[mid] == target) {
index = mid;
right = mid - 1;
}
}
// 不存在target时, left == nums.size() || nums[left] != target
// if ( left == nums.size() || nums[left] != target )
// return -1;
// return left;
return index;
}
此处注意nums中不存在target的情况:
1. target比nums中的值都大,那么算法结束 left == len(nums),此时返回-1。
2. target比nums中的值都小,那么算法结束 nums[left] != target,此时返回-1。
也可以用index放在(nums[mid] == target)中避开判断不存在target情况。
3. 寻找排序数组右侧边界
int searchright(vector<int>& nums, int target) {
int index = -1;
int left = 0;
int right = nums.size() - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (nums[mid] > target)
right = mid - 1;
else if (nums[mid] < target)
left = mid + 1;
else if (nums[mid] == target) {
index = mid;
left = mid + 1;
}
}
// 不存在target时, right == -1 || nums[right] != target
// if (right == -1 || nums[right] != target)
// return -1;
// return right;
return index;
}
此处注意nums中不存在target的情况:
1. target比nums中的值都大,那么算法结束 nums[right] != target,此时返回-1。
2. target比nums中的值都小,那么算法结束 right == -1,此时返回-1。
也可以用index放在(nums[mid] == target)中避开判断不存在target情况。
最终进行合并可解leetcode 34. 在排序数组中查找元素的第一个和最后一个位置:
class Solution {
public:
int searchleft(vector<int>& nums, int target) {
int index = -1;
int left = 0;
int right = nums.size() - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (nums[mid] > target)
right = mid - 1;
else if (nums[mid] < target)
left = mid + 1;
else if (nums[mid] == target) {
index = mid;
right = mid - 1;
}
}
// 不存在target时, left == nums.size() || nums[left] != target
// if ( left == nums.size() || nums[left] != target )
// return -1;
// return left;
return index;
}
int searchright(vector<int>& nums, int target) {
int index = -1;
int left = 0;
int right = nums.size() - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (nums[mid] > target)
right = mid - 1;
else if (nums[mid] < target)
left = mid + 1;
else if (nums[mid] == target) {
index = mid;
left = mid + 1;
}
}
// 不存在target时, right == -1 || nums[right] != target
// if (right == -1 || nums[right] != target)
// return -1;
// return right;
return index;
}
vector<int> searchRange(vector<int>& nums, int target) {
int left = searchleft(nums, target);
int right = searchright(nums, target);
vector<int> ans = {left, right};
return ans;
}
};