2019-04-25 18:24:51 +08:00
|
|
|
|
<!-- GFM-TOC -->
|
2019-05-14 22:56:30 +08:00
|
|
|
|
* [1. 求开方](#1-求开方)
|
|
|
|
|
* [2. 大于给定元素的最小元素](#2-大于给定元素的最小元素)
|
|
|
|
|
* [3. 有序数组的 Single Element](#3-有序数组的-single-element)
|
|
|
|
|
* [4. 第一个错误的版本](#4-第一个错误的版本)
|
|
|
|
|
* [5. 旋转数组的最小数字](#5-旋转数组的最小数字)
|
|
|
|
|
* [6. 查找区间](#6-查找区间)
|
2019-04-25 18:24:51 +08:00
|
|
|
|
<!-- GFM-TOC -->
|
|
|
|
|
|
|
|
|
|
|
2019-05-14 22:56:30 +08:00
|
|
|
|
**正常实现**
|
2019-04-25 18:24:51 +08:00
|
|
|
|
|
2019-05-14 22:14:12 +08:00
|
|
|
|
```text
|
|
|
|
|
Input : [1,2,3,4,5]
|
|
|
|
|
key : 3
|
|
|
|
|
return the index : 2
|
|
|
|
|
```
|
|
|
|
|
|
2019-04-25 18:24:51 +08:00
|
|
|
|
```java
|
|
|
|
|
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-05-14 22:56:30 +08:00
|
|
|
|
**时间复杂度**
|
2019-04-25 18:24:51 +08:00
|
|
|
|
|
|
|
|
|
二分查找也称为折半查找,每次都能将查找区间减半,这种折半特性的算法时间复杂度为 O(logN)。
|
|
|
|
|
|
2019-05-14 22:56:30 +08:00
|
|
|
|
**m 计算**
|
2019-04-25 18:24:51 +08:00
|
|
|
|
|
|
|
|
|
有两种计算中值 m 的方式:
|
|
|
|
|
|
|
|
|
|
- m = (l + h) / 2
|
|
|
|
|
- m = l + (h - l) / 2
|
|
|
|
|
|
2019-05-14 22:14:12 +08:00
|
|
|
|
l + h 可能出现加法溢出,也就是说加法的结果大于整型能够表示的范围。但是 l 和 h 都为正数,因此 h - l 不会出现加法溢出问题。所以,最好使用第二种计算法方法。
|
2019-04-25 18:24:51 +08:00
|
|
|
|
|
2019-05-14 22:56:30 +08:00
|
|
|
|
**未成功查找的返回值**
|
2019-04-25 18:24:51 +08:00
|
|
|
|
|
|
|
|
|
循环退出时如果仍然没有查找到 key,那么表示查找失败。可以有两种返回值:
|
|
|
|
|
|
|
|
|
|
- -1:以一个错误码表示没有查找到 key
|
|
|
|
|
- l:将 key 插入到 nums 中的正确位置
|
|
|
|
|
|
2019-05-14 22:56:30 +08:00
|
|
|
|
**变种**
|
2019-04-25 18:24:51 +08:00
|
|
|
|
|
|
|
|
|
二分查找可以有很多变种,变种实现要注意边界值的判断。例如在一个有重复元素的数组中查找 key 的最左位置的实现如下:
|
|
|
|
|
|
|
|
|
|
```java
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
该实现和正常实现有以下不同:
|
|
|
|
|
|
|
|
|
|
- h 的赋值表达式为 h = m
|
2019-05-14 22:14:12 +08:00
|
|
|
|
- 循环条件为 l < h
|
2019-04-25 18:24:51 +08:00
|
|
|
|
- 最后返回 l 而不是 -1
|
|
|
|
|
|
|
|
|
|
在 nums[m] >= key 的情况下,可以推导出最左 key 位于 [l, m] 区间中,这是一个闭区间。h 的赋值表达式为 h = m,因为 m 位置也可能是解。
|
|
|
|
|
|
2019-05-14 22:14:12 +08:00
|
|
|
|
在 h 的赋值表达式为 h = m 的情况下,如果循环条件为 l <= h,那么会出现循环无法退出的情况,因此循环条件只能是 l < h。以下演示了循环条件为 l <= h 时循环无法退出的情况:
|
2019-04-25 18:24:51 +08:00
|
|
|
|
|
|
|
|
|
```text
|
|
|
|
|
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
|
|
|
|
|
...
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
当循环体退出时,不表示没有查找到 key,因此最后返回的结果不应该为 -1。为了验证有没有查找到,需要在调用端判断一下返回位置上的值和 key 是否相等。
|
|
|
|
|
|
2019-05-14 22:56:30 +08:00
|
|
|
|
# 1. 求开方
|
2019-04-25 18:24:51 +08:00
|
|
|
|
|
|
|
|
|
[69. Sqrt(x) (Easy)](https://leetcode.com/problems/sqrtx/description/)
|
|
|
|
|
|
|
|
|
|
```html
|
|
|
|
|
Input: 4
|
|
|
|
|
Output: 2
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
一个数 x 的开方 sqrt 一定在 0 \~ x 之间,并且满足 sqrt == x / sqrt。可以利用二分查找在 0 \~ x 之间查找 sqrt。
|
|
|
|
|
|
|
|
|
|
对于 x = 8,它的开方是 2.82842...,最后应该返回 2 而不是 3。在循环条件为 l <= h 并且循环退出时,h 总是比 l 小 1,也就是说 h = 2,l = 3,因此最后的返回值应该为 h 而不是 l。
|
|
|
|
|
|
|
|
|
|
```java
|
|
|
|
|
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-05-14 22:56:30 +08:00
|
|
|
|
# 2. 大于给定元素的最小元素
|
2019-04-25 18:24:51 +08:00
|
|
|
|
|
|
|
|
|
[744. Find Smallest Letter Greater Than Target (Easy)](https://leetcode.com/problems/find-smallest-letter-greater-than-target/description/)
|
|
|
|
|
|
|
|
|
|
```html
|
|
|
|
|
Input:
|
|
|
|
|
letters = ["c", "f", "j"]
|
|
|
|
|
target = "d"
|
|
|
|
|
Output: "f"
|
|
|
|
|
|
|
|
|
|
Input:
|
|
|
|
|
letters = ["c", "f", "j"]
|
|
|
|
|
target = "k"
|
|
|
|
|
Output: "c"
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
题目描述:给定一个有序的字符数组 letters 和一个字符 target,要求找出 letters 中大于 target 的最小字符,如果找不到就返回第 1 个字符。
|
|
|
|
|
|
|
|
|
|
```java
|
|
|
|
|
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-05-14 22:56:30 +08:00
|
|
|
|
# 3. 有序数组的 Single Element
|
2019-04-25 18:24:51 +08:00
|
|
|
|
|
|
|
|
|
[540. Single Element in a Sorted Array (Medium)](https://leetcode.com/problems/single-element-in-a-sorted-array/description/)
|
|
|
|
|
|
|
|
|
|
```html
|
|
|
|
|
Input: [1, 1, 2, 3, 3, 4, 4, 8, 8]
|
|
|
|
|
Output: 2
|
|
|
|
|
```
|
|
|
|
|
|
2019-05-14 22:14:12 +08:00
|
|
|
|
题目描述:一个有序数组只有一个数不出现两次,找出这个数。
|
|
|
|
|
|
|
|
|
|
要求以 O(logN) 时间复杂度进行求解,因此不能遍历数组并进行异或操作来求解,这么做的时间复杂度为 O(N)。
|
2019-04-25 18:24:51 +08:00
|
|
|
|
|
2019-05-14 22:14:12 +08:00
|
|
|
|
令 index 为 Single Element 在数组中的位置。在 index 之后,数组中原来存在的成对状态被改变。如果 m 为偶数,并且 m + 1 < index,那么 nums[m] == nums[m + 1];m + 1 >= index,那么 nums[m] != nums[m + 1]。
|
2019-04-25 18:24:51 +08:00
|
|
|
|
|
|
|
|
|
从上面的规律可以知道,如果 nums[m] == nums[m + 1],那么 index 所在的数组位置为 [m + 2, h],此时令 l = m + 2;如果 nums[m] != nums[m + 1],那么 index 所在的数组位置为 [l, m],此时令 h = m。
|
|
|
|
|
|
|
|
|
|
因为 h 的赋值表达式为 h = m,那么循环条件也就只能使用 l < h 这种形式。
|
|
|
|
|
|
|
|
|
|
```java
|
|
|
|
|
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-05-14 22:56:30 +08:00
|
|
|
|
# 4. 第一个错误的版本
|
2019-04-25 18:24:51 +08:00
|
|
|
|
|
|
|
|
|
[278. First Bad Version (Easy)](https://leetcode.com/problems/first-bad-version/description/)
|
|
|
|
|
|
2019-05-14 22:14:12 +08:00
|
|
|
|
题目描述:给定一个元素 n 代表有 [1, 2, ..., n] 版本,在第 x 位置开始出现错误版本,导致后面的版本都错误。可以调用 isBadVersion(int x) 知道某个版本是否错误,要求找到第一个错误的版本。
|
2019-04-25 18:24:51 +08:00
|
|
|
|
|
|
|
|
|
如果第 m 个版本出错,则表示第一个错误的版本在 [l, m] 之间,令 h = m;否则第一个错误的版本在 [m + 1, h] 之间,令 l = m + 1。
|
|
|
|
|
|
|
|
|
|
因为 h 的赋值表达式为 h = m,因此循环条件为 l < h。
|
|
|
|
|
|
|
|
|
|
```java
|
|
|
|
|
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-05-14 22:56:30 +08:00
|
|
|
|
# 5. 旋转数组的最小数字
|
2019-04-25 18:24:51 +08:00
|
|
|
|
|
|
|
|
|
[153. Find Minimum in Rotated Sorted Array (Medium)](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/description/)
|
|
|
|
|
|
|
|
|
|
```html
|
|
|
|
|
Input: [3,4,5,1,2],
|
|
|
|
|
Output: 1
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
```java
|
|
|
|
|
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-05-14 22:56:30 +08:00
|
|
|
|
# 6. 查找区间
|
2019-04-25 18:24:51 +08:00
|
|
|
|
|
2019-05-14 17:05:21 +08:00
|
|
|
|
[34. Find First and Last Position of Element in Sorted Array](https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/)
|
2019-04-25 18:24:51 +08:00
|
|
|
|
|
|
|
|
|
```html
|
|
|
|
|
Input: nums = [5,7,7,8,8,10], target = 8
|
|
|
|
|
Output: [3,4]
|
|
|
|
|
|
|
|
|
|
Input: nums = [5,7,7,8,8,10], target = 6
|
|
|
|
|
Output: [-1,-1]
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
```java
|
|
|
|
|
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)};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-06-08 12:07:53 +08:00
|
|
|
|
<img width="650px" src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/other/QQ截图20190608120206.png"></img>
|