2022-11-21 12:04:27 +00:00
|
|
|
|
# 二分查找
|
|
|
|
|
|
2022-11-24 08:44:36 +00:00
|
|
|
|
适用于数组有序的情况下查找目标值。
|
|
|
|
|
|
|
|
|
|
## 写法一
|
|
|
|
|
|
|
|
|
|
针对左闭右闭区间(即 `[left, right]`):
|
2022-11-21 12:04:27 +00:00
|
|
|
|
|
|
|
|
|
```cpp
|
2022-11-24 08:44:36 +00:00
|
|
|
|
class Solution {
|
|
|
|
|
public:
|
|
|
|
|
int search(vector<int>& nums, int target) {
|
|
|
|
|
int left = 0;
|
|
|
|
|
int right = nums.size() - 1; // 定义target在左闭右闭的区间里,[left, right]
|
|
|
|
|
while (left <=
|
|
|
|
|
right) { // 当left==right,区间[left, right]依然有效,所以用 <=
|
|
|
|
|
int middle =
|
|
|
|
|
left + ((right - left) / 2); // 防止溢出 等同于(left + right)/2
|
|
|
|
|
if (nums[middle] > target) {
|
|
|
|
|
right = middle - 1; // target 在左区间,所以[left, middle - 1]
|
|
|
|
|
} else if (nums[middle] < target) {
|
|
|
|
|
left = middle + 1; // target 在右区间,所以[middle + 1, right]
|
|
|
|
|
} else { // nums[middle] == target
|
|
|
|
|
return middle; // 数组中找到目标值,直接返回下标
|
|
|
|
|
}
|
2022-11-21 12:04:27 +00:00
|
|
|
|
}
|
2022-11-24 08:44:36 +00:00
|
|
|
|
// 未找到目标值
|
|
|
|
|
return -1;
|
2022-11-21 12:04:27 +00:00
|
|
|
|
}
|
2022-11-24 08:44:36 +00:00
|
|
|
|
};
|
2022-11-21 12:04:27 +00:00
|
|
|
|
```
|
|
|
|
|
|
2022-11-24 08:44:36 +00:00
|
|
|
|
## 写法二
|
|
|
|
|
|
|
|
|
|
针对左闭右开(即 `[left, right)`):
|
2022-11-21 12:04:27 +00:00
|
|
|
|
|
|
|
|
|
```cpp
|
2022-11-24 08:44:36 +00:00
|
|
|
|
class Solution {
|
|
|
|
|
public:
|
|
|
|
|
int search(vector<int>& nums, int target) {
|
|
|
|
|
int left = 0;
|
|
|
|
|
int right = nums.size(); // 定义target在左闭右开的区间里,即:[left, right)
|
|
|
|
|
while (left < right) { // 因为left == right的时候,在[left,
|
|
|
|
|
// right)是无效的空间,所以使用 <
|
|
|
|
|
int middle = left + ((right - left) >> 1);
|
|
|
|
|
if (nums[middle] > target) {
|
|
|
|
|
right = middle; // target 在左区间,在[left, middle)中
|
|
|
|
|
} else if (nums[middle] < target) {
|
|
|
|
|
left = middle + 1; // target 在右区间,在[middle + 1, right)中
|
|
|
|
|
} else { // nums[middle] == target
|
|
|
|
|
return middle; // 数组中找到目标值,直接返回下标
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 未找到目标值
|
2022-11-21 12:04:27 +00:00
|
|
|
|
return -1;
|
|
|
|
|
}
|
2022-11-24 08:44:36 +00:00
|
|
|
|
};
|
2022-11-21 12:04:27 +00:00
|
|
|
|
```
|
2022-11-24 08:44:36 +00:00
|
|
|
|
|
|
|
|
|
## 相关题目
|
|
|
|
|
|
|
|
|
|
- [704. 二分查找](https://programmercarl.com/0704.%E4%BA%8C%E5%88%86%E6%9F%A5%E6%89%BE.html#_704-%E4%BA%8C%E5%88%86%E6%9F%A5%E6%89%BE)
|
|
|
|
|
- [35. 搜索插入位置](https://programmercarl.com/0035.%E6%90%9C%E7%B4%A2%E6%8F%92%E5%85%A5%E4%BD%8D%E7%BD%AE.html#_35-%E6%90%9C%E7%B4%A2%E6%8F%92%E5%85%A5%E4%BD%8D%E7%BD%AE)
|
|
|
|
|
- [34. 在排序数组中查找元素的第一个和最后一个位置](https://programmercarl.com/0034.%E5%9C%A8%E6%8E%92%E5%BA%8F%E6%95%B0%E7%BB%84%E4%B8%AD%E6%9F%A5%E6%89%BE%E5%85%83%E7%B4%A0%E7%9A%84%E7%AC%AC%E4%B8%80%E4%B8%AA%E5%92%8C%E6%9C%80%E5%90%8E%E4%B8%80%E4%B8%AA%E4%BD%8D%E7%BD%AE.html#_34-%E5%9C%A8%E6%8E%92%E5%BA%8F%E6%95%B0%E7%BB%84%E4%B8%AD%E6%9F%A5%E6%89%BE%E5%85%83%E7%B4%A0%E7%9A%84%E7%AC%AC%E4%B8%80%E4%B8%AA%E5%92%8C%E6%9C%80%E5%90%8E%E4%B8%80%E4%B8%AA%E4%BD%8D%E7%BD%AE)
|