2019-03-08 21:41:45 +08:00
|
|
|
|
<!-- GFM-TOC -->
|
|
|
|
|
* [原理](#原理)
|
|
|
|
|
* [1. 正常实现](#1-正常实现)
|
|
|
|
|
* [2. 时间复杂度](#2-时间复杂度)
|
|
|
|
|
* [3. m 计算](#3-m-计算)
|
|
|
|
|
* [4. 返回值](#4-返回值)
|
|
|
|
|
* [5. 变种](#5-变种)
|
|
|
|
|
* [例题](#例题)
|
|
|
|
|
* [1. 求开方](#1-求开方)
|
|
|
|
|
* [2. 大于给定元素的最小元素](#2-大于给定元素的最小元素)
|
|
|
|
|
* [3. 有序数组的 Single Element](#3-有序数组的-single-element)
|
|
|
|
|
* [4. 第一个错误的版本](#4-第一个错误的版本)
|
|
|
|
|
* [5. 旋转数组的最小数字](#5-旋转数组的最小数字)
|
|
|
|
|
* [6. 查找区间](#6-查找区间)
|
|
|
|
|
<!-- GFM-TOC -->
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 原理
|
|
|
|
|
|
|
|
|
|
## 1. 正常实现
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
|
|
|
|
```java
|
2019-03-08 21:41:45 +08:00
|
|
|
|
public int binarySearch(int[] nums, int key) {
|
|
|
|
|
int l = 0, h = nums.length - 1;
|
|
|
|
|
while (l <= h) {
|
|
|
|
|
int m = l + (h - l) / 2;
|
|
|
|
|
if (nums[m] == key) {
|
|
|
|
|
return m;
|
|
|
|
|
} else if (nums[m] > key) {
|
|
|
|
|
h = m - 1;
|
|
|
|
|
} else {
|
|
|
|
|
l = m + 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return -1;
|
2019-03-08 20:31:07 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
## 2. 时间复杂度
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
二分查找也称为折半查找,每次都能将查找区间减半,这种折半特性的算法时间复杂度为 O(logN)。
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
## 3. m 计算
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
有两种计算中值 m 的方式:
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
- m = (l + h) / 2
|
|
|
|
|
- m = l + (h - l) / 2
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
l + h 可能出现加法溢出,最好使用第二种方式。
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
## 4. 返回值
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
循环退出时如果仍然没有查找到 key,那么表示查找失败。可以有两种返回值:
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
- -1:以一个错误码表示没有查找到 key
|
|
|
|
|
- l:将 key 插入到 nums 中的正确位置
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
## 5. 变种
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
二分查找可以有很多变种,变种实现要注意边界值的判断。例如在一个有重复元素的数组中查找 key 的最左位置的实现如下:
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
|
|
|
|
```java
|
2019-03-08 21:41:45 +08:00
|
|
|
|
public int binarySearch(int[] nums, int key) {
|
|
|
|
|
int l = 0, h = nums.length - 1;
|
|
|
|
|
while (l < h) {
|
|
|
|
|
int m = l + (h - l) / 2;
|
|
|
|
|
if (nums[m] >= key) {
|
|
|
|
|
h = m;
|
|
|
|
|
} else {
|
|
|
|
|
l = m + 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return l;
|
2019-03-08 20:31:07 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
该实现和正常实现有以下不同:
|
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
- 循环条件为 l < h
|
|
|
|
|
- h 的赋值表达式为 h = m
|
|
|
|
|
- 最后返回 l 而不是 -1
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
在 nums[m] >= key 的情况下,可以推导出最左 key 位于 [l, m] 区间中,这是一个闭区间。h 的赋值表达式为 h = m,因为 m 位置也可能是解。
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
在 h 的赋值表达式为 h = mid 的情况下,如果循环条件为 l <= h,那么会出现循环无法退出的情况,因此循环条件只能是 l < h。以下演示了循环条件为 l <= h 时循环无法退出的情况:
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
|
|
|
|
```text
|
2019-03-08 21:41:45 +08:00
|
|
|
|
nums = {0, 1, 2}, key = 1
|
|
|
|
|
l m h
|
|
|
|
|
0 1 2 nums[m] >= key
|
|
|
|
|
0 0 1 nums[m] < key
|
|
|
|
|
1 1 1 nums[m] >= key
|
|
|
|
|
1 1 1 nums[m] >= key
|
2019-03-08 20:31:07 +08:00
|
|
|
|
...
|
|
|
|
|
```
|
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
当循环体退出时,不表示没有查找到 key,因此最后返回的结果不应该为 -1。为了验证有没有查找到,需要在调用端判断一下返回位置上的值和 key 是否相等。
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
# 例题
|
2019-03-08 20:41:28 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
## 1. 求开方
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
[69. Sqrt(x) (Easy)](https://leetcode.com/problems/sqrtx/description/)
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
|
|
|
|
```html
|
2019-03-08 21:41:45 +08:00
|
|
|
|
Input: 4
|
|
|
|
|
Output: 2
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
Input: 8
|
|
|
|
|
Output: 2
|
|
|
|
|
Explanation: The square root of 8 is 2.82842..., and since we want to return an integer, the decimal part will be truncated.
|
2019-03-08 20:31:07 +08:00
|
|
|
|
```
|
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
一个数 x 的开方 sqrt 一定在 0 \~ x 之间,并且满足 sqrt == x / sqrt。可以利用二分查找在 0 \~ x 之间查找 sqrt。
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
对于 x = 8,它的开方是 2.82842...,最后应该返回 2 而不是 3。在循环条件为 l <= h 并且循环退出时,h 总是比 l 小 1,也就是说 h = 2,l = 3,因此最后的返回值应该为 h 而不是 l。
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
|
|
|
|
```java
|
2019-03-08 21:41:45 +08:00
|
|
|
|
public int mySqrt(int x) {
|
|
|
|
|
if (x <= 1) {
|
|
|
|
|
return x;
|
|
|
|
|
}
|
|
|
|
|
int l = 1, h = x;
|
|
|
|
|
while (l <= h) {
|
|
|
|
|
int mid = l + (h - l) / 2;
|
|
|
|
|
int sqrt = x / mid;
|
|
|
|
|
if (sqrt == mid) {
|
|
|
|
|
return mid;
|
|
|
|
|
} else if (mid > sqrt) {
|
|
|
|
|
h = mid - 1;
|
|
|
|
|
} else {
|
|
|
|
|
l = mid + 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return h;
|
2019-03-08 20:31:07 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
## 2. 大于给定元素的最小元素
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
[744. Find Smallest Letter Greater Than Target (Easy)](https://leetcode.com/problems/find-smallest-letter-greater-than-target/description/)
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
|
|
|
|
```html
|
|
|
|
|
Input:
|
2019-03-08 21:41:45 +08:00
|
|
|
|
letters = ["c", "f", "j"]
|
|
|
|
|
target = "d"
|
|
|
|
|
Output: "f"
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
|
|
|
|
Input:
|
2019-03-08 21:41:45 +08:00
|
|
|
|
letters = ["c", "f", "j"]
|
|
|
|
|
target = "k"
|
|
|
|
|
Output: "c"
|
2019-03-08 20:31:07 +08:00
|
|
|
|
```
|
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
题目描述:给定一个有序的字符数组 letters 和一个字符 target,要求找出 letters 中大于 target 的最小字符,如果找不到就返回第 1 个字符。
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
|
|
|
|
```java
|
2019-03-08 21:41:45 +08:00
|
|
|
|
public char nextGreatestLetter(char[] letters, char target) {
|
|
|
|
|
int n = letters.length;
|
|
|
|
|
int l = 0, h = n - 1;
|
|
|
|
|
while (l <= h) {
|
|
|
|
|
int m = l + (h - l) / 2;
|
|
|
|
|
if (letters[m] <= target) {
|
|
|
|
|
l = m + 1;
|
|
|
|
|
} else {
|
|
|
|
|
h = m - 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return l < n ? letters[l] : letters[0];
|
2019-03-08 20:31:07 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
## 3. 有序数组的 Single Element
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
[540. Single Element in a Sorted Array (Medium)](https://leetcode.com/problems/single-element-in-a-sorted-array/description/)
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
|
|
|
|
```html
|
2019-03-08 21:41:45 +08:00
|
|
|
|
Input: [1, 1, 2, 3, 3, 4, 4, 8, 8]
|
|
|
|
|
Output: 2
|
2019-03-08 20:31:07 +08:00
|
|
|
|
```
|
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
题目描述:一个有序数组只有一个数不出现两次,找出这个数。要求以 O(logN) 时间复杂度进行求解。
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
令 index 为 Single Element 在数组中的位置。如果 m 为偶数,并且 m + 1 < index,那么 nums[m] == nums[m + 1];m + 1 >= index,那么 nums[m] != nums[m + 1]。
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
从上面的规律可以知道,如果 nums[m] == nums[m + 1],那么 index 所在的数组位置为 [m + 2, h],此时令 l = m + 2;如果 nums[m] != nums[m + 1],那么 index 所在的数组位置为 [l, m],此时令 h = m。
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
因为 h 的赋值表达式为 h = m,那么循环条件也就只能使用 l < h 这种形式。
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
|
|
|
|
```java
|
2019-03-08 21:41:45 +08:00
|
|
|
|
public int singleNonDuplicate(int[] nums) {
|
|
|
|
|
int l = 0, h = nums.length - 1;
|
|
|
|
|
while (l < h) {
|
|
|
|
|
int m = l + (h - l) / 2;
|
|
|
|
|
if (m % 2 == 1) {
|
|
|
|
|
m--; // 保证 l/h/m 都在偶数位,使得查找区间大小一直都是奇数
|
|
|
|
|
}
|
|
|
|
|
if (nums[m] == nums[m + 1]) {
|
|
|
|
|
l = m + 2;
|
|
|
|
|
} else {
|
|
|
|
|
h = m;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nums[l];
|
2019-03-08 20:31:07 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
## 4. 第一个错误的版本
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
[278. First Bad Version (Easy)](https://leetcode.com/problems/first-bad-version/description/)
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
题目描述:给定一个元素 n 代表有 [1, 2, ..., n] 版本,可以调用 isBadVersion(int x) 知道某个版本是否错误,要求找到第一个错误的版本。
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
如果第 m 个版本出错,则表示第一个错误的版本在 [l, m] 之间,令 h = m;否则第一个错误的版本在 [m + 1, h] 之间,令 l = m + 1。
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
因为 h 的赋值表达式为 h = m,因此循环条件为 l < h。
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
|
|
|
|
```java
|
2019-03-08 21:41:45 +08:00
|
|
|
|
public int firstBadVersion(int n) {
|
|
|
|
|
int l = 1, h = n;
|
|
|
|
|
while (l < h) {
|
|
|
|
|
int mid = l + (h - l) / 2;
|
|
|
|
|
if (isBadVersion(mid)) {
|
|
|
|
|
h = mid;
|
|
|
|
|
} else {
|
|
|
|
|
l = mid + 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return l;
|
2019-03-08 20:31:07 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
## 5. 旋转数组的最小数字
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
[153. Find Minimum in Rotated Sorted Array (Medium)](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/description/)
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
|
|
|
|
```html
|
2019-03-08 21:41:45 +08:00
|
|
|
|
Input: [3,4,5,1,2],
|
|
|
|
|
Output: 1
|
2019-03-08 20:31:07 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
```java
|
2019-03-08 21:41:45 +08:00
|
|
|
|
public int findMin(int[] nums) {
|
|
|
|
|
int l = 0, h = nums.length - 1;
|
|
|
|
|
while (l < h) {
|
|
|
|
|
int m = l + (h - l) / 2;
|
|
|
|
|
if (nums[m] <= nums[h]) {
|
|
|
|
|
h = m;
|
|
|
|
|
} else {
|
|
|
|
|
l = m + 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nums[l];
|
2019-03-08 20:31:07 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
## 6. 查找区间
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
[34. Search for a Range (Medium)](https://leetcode.com/problems/search-for-a-range/description/)
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
|
|
|
|
```html
|
2019-03-08 21:41:45 +08:00
|
|
|
|
Input: nums = [5,7,7,8,8,10], target = 8
|
|
|
|
|
Output: [3,4]
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
Input: nums = [5,7,7,8,8,10], target = 6
|
|
|
|
|
Output: [-1,-1]
|
2019-03-08 20:31:07 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
```java
|
2019-03-08 21:41:45 +08:00
|
|
|
|
public int[] searchRange(int[] nums, int target) {
|
|
|
|
|
int first = binarySearch(nums, target);
|
|
|
|
|
int last = binarySearch(nums, target + 1) - 1;
|
|
|
|
|
if (first == nums.length || nums[first] != target) {
|
|
|
|
|
return new int[]{-1, -1};
|
|
|
|
|
} else {
|
|
|
|
|
return new int[]{first, Math.max(first, last)};
|
|
|
|
|
}
|
2019-03-08 20:31:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
private int binarySearch(int[] nums, int target) {
|
|
|
|
|
int l = 0, h = nums.length; // 注意 h 的初始值
|
|
|
|
|
while (l < h) {
|
|
|
|
|
int m = l + (h - l) / 2;
|
|
|
|
|
if (nums[m] >= target) {
|
|
|
|
|
h = m;
|
|
|
|
|
} else {
|
|
|
|
|
l = m + 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return l;
|
2019-03-08 20:31:07 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|