CS-Notes/notes/算法.md

1637 lines
42 KiB
Markdown
Raw Normal View History

2018-02-20 10:40:05 +08:00
<!-- GFM-TOC -->
2018-02-22 14:47:22 +08:00
* [????](#????)
* [?](#?)
* [1. ???????](#1-???????)
* [2. ???????](#2-???????)
* [????](#????)
* [??????](#??????)
* [1. ???????](#1-???????)
* [2. ??????](#2-??????)
2018-02-20 10:40:05 +08:00
* [3. ThreeSum](#3-threesum)
2018-02-22 14:47:22 +08:00
* [4. ???????](#4-???????)
* [5. ???????](#5-???????)
2018-02-20 10:40:05 +08:00
* [union-find](#union-find)
2018-02-22 14:47:22 +08:00
* [1. quick-find ??](#1-quick-find-??)
* [2. quick-union ??](#2-quick-union-??)
* [3. ??? quick-union ??](#3-???-quick-union-??)
* [4. <20><>????????? quick-union ??](#4-<2D><>?????????-quick-union-??)
* [5. ???? union-find ??????](#5-????-union-find-??????)
* [????? ????](#?????-????)
* [??????????](#??????????)
* [1. ???](#1-???)
* [2. ???????](#2-???????)
* [3. ????????](#3-????????)
* [4. ??????????????????](#4-??????????????????)
* [5. ???????](#5-???????)
* [?<3F><>????](#?<3F><>????)
* [1. ?<3F><>????](#1-?<3F><>????)
* [2. ???????<3F><>????](#2-???????<3F><>????)
* [3. ???????<3F><>????](#3-???????<3F><>????)
* [????????](#????????)
* [1. ??????](#1-??????)
* [2. ?<3F><>?](#2-?<3F><>?)
* [3. ???????](#3-???????)
* [4. ?????](#4-?????)
* [4.1 ?<3F><>???????????](#41-?<3F><>???????????)
* [4.2 ?????](#42-?????)
* [4.3 ?????<3F><>?](#43-?????<3F><>?)
* [???????](#???????)
* [1. ??](#1-??)
* [2. ????????](#2-????????)
* [3. ???????](#3-???????)
* [4. ?????????](#4-?????????)
* [5. ??????](#5-??????)
* [6. ????](#6-????)
* [???](#???)
* [1. ??????????](#1-??????????)
* [2. Java ???????????](#2-java-???????????)
* [3. ?????<3F><>??????????](#3-?????<3F><>??????????)
* [?????? ????](#??????-????)
* [?????](#?????)
2018-02-21 16:33:00 +08:00
* [1. API](#1-api)
2018-02-22 14:47:22 +08:00
* [2. ????????](#2-????????)
* [3. ???????????](#3-???????????)
* [4. ???????????????](#4-???????????????)
* [5. ????????????](#5-????????????)
* [?????????](#?????????)
2018-02-21 16:33:00 +08:00
* [1. get()](#1-get)
* [2. put()](#2-put)
2018-02-22 14:47:22 +08:00
* [3. ????](#3-????)
2018-02-21 16:33:00 +08:00
* [4. floor()](#4-floor)
* [5. rank()](#5-rank)
* [6. min()](#6-min)
* [7. deleteMin()](#7-deletemin)
* [8. delete()](#8-delete)
* [9. keys()](#9-keys)
2018-02-22 14:47:22 +08:00
* [10. ???????](#10-???????)
* [????????](#????????)
* [2-3 ??????](#2-3-??????)
* [1. ???????](#1-???????)
* [2. ????](#2-????)
* [???????????](#???????????)
* [1. ?????](#1-?????)
* [2. ?????](#2-?????)
* [3. ??????](#3-??????)
* [4. ????](#4-????)
* [5. ?????<3F><>??](#5-?????<3F><>??)
* [6. ????](#6-????)
* [??<3F><>?](#??<3F><>?)
* [??<3F><>???](#??<3F><>???)
* [??????????????<3F><>?](#??????????????<3F><>?)
* [???????????????<3F><>?](#???????????????<3F><>?)
* [????](#????)
* [????](#????)
* [???](#???)
* [?????????<3F><>](#?????????<3F><>)
* [???](#???)
* [??????????????](#??????????????)
* [Java ?????????](#java-?????????)
* [????????](#????????)
* [??????????](#??????????)
2018-02-20 10:40:05 +08:00
<!-- GFM-TOC -->
2018-02-22 14:47:22 +08:00
# ????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
## ?
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
### 1. ???????
2018-02-20 10:40:05 +08:00
2018-02-21 16:33:00 +08:00
2018-02-20 10:40:05 +08:00
```java
public class ResizeArrayStack<Item> implements Iterable<Item> {
private Item[] a = (Item[]) new Object[1];
private int N = 0;
public void push(Item item) {
if (N >= a.length) {
resize(2 * a.length);
}
a[N++] = item;
}
public Item pop() {
Item item = a[--N];
if (N <= a.length / 4) {
resize(a.length / 2);
}
return item;
}
2018-02-22 14:47:22 +08:00
// ?????????<3F><>????????????????
2018-02-20 10:40:05 +08:00
private void resize(int size) {
Item[] tmp = (Item[]) new Object[size];
for (int i = 0; i < N; i++) {
tmp[i] = a[i];
}
a = tmp;
}
public boolean isEmpty() {
return N == 0;
}
public int size() {
return N;
}
@Override
public Iterator<Item> iterator() {
2018-02-22 14:47:22 +08:00
// ?????????????????????
2018-02-20 10:40:05 +08:00
return new ReverseArrayIterator();
}
private class ReverseArrayIterator implements Iterator<Item> {
private int i = N;
@Override
public boolean hasNext() {
return i > 0;
}
@Override
public Item next() {
return a[--i];
}
}
}
```
2018-02-22 14:47:22 +08:00
????????????????Java ?????????????????<3F><>?????????????????
2018-02-21 16:33:00 +08:00
```java
Item[] arr = (Item[]) new Object[N];
```
2018-02-22 14:47:22 +08:00
### 2. ???????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
?????????????<3F><>???????????<3F><>????????????????????????????? next ??????????????????????????????????????????????????????????
2018-02-20 10:40:05 +08:00
```java
public class Stack<Item> {
private Node top = null;
private int N = 0;
private class Node {
Item item;
Node next;
}
public boolean isEmpty() {
return N == 0;
}
public int size() {
return N;
}
public void push(Item item) {
Node newTop = new Node();
newTop.item = item;
newTop.next = top;
top = newTop;
N++;
}
public Item pop() {
Item item = top.item;
top = top.next;
N--;
return item;
}
}
```
2018-02-22 14:47:22 +08:00
## ????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
????????<3F><>??????????????? first ?? last ?????????????????<3F><>??
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
?????????????????????????????????????????????<3F><>?????????????<3F><>???????????????????????????????????????????????????????????????? next ?????????????????????????? first ????????????
2018-02-20 10:40:05 +08:00
```java
public class Queue<Item> {
private Node first;
private Node last;
int N = 0;
private class Node{
Item item;
Node next;
}
public boolean isEmpty(){
return N == 0;
}
public int size(){
return N;
}
2018-02-22 14:47:22 +08:00
// ?????
2018-02-20 10:40:05 +08:00
public void enqueue(Item item){
Node newNode = new Node();
newNode.item = item;
newNode.next = null;
if(isEmpty()){
last = newNode;
first = newNode;
} else{
last.next = newNode;
last = newNode;
}
N++;
}
2018-02-22 14:47:22 +08:00
// ??????
2018-02-20 10:40:05 +08:00
public Item dequeue(){
Node node = first;
first = first.next;
N--;
return node.item;
}
}
```
2018-02-22 14:47:22 +08:00
## ??????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
### 1. ???????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
???????????????????????????????????????????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
T(N)=aN<sup>3</sup> ???? lg(T(N))=3lgN+lga
2018-02-20 10:40:05 +08:00
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/5510045a-8f32-487f-a756-463e51a6dab0.png)
2018-02-22 14:47:22 +08:00
### 2. ??????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
**????**
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
??? \~f(N) ????????????? N ????????? f(N) ?????????? 1 ????? , ???? N<sup>3</sup>/6-N<sup>2</sup>/2+N/3 \~ N<sup>3</sup>/6??
2018-02-20 10:40:05 +08:00
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/ca3a793e-06e5-4ff3-b28e-a9c20540d164.png)
2018-02-22 14:47:22 +08:00
**??????????**
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
???????????????????????????????????????????????? N<sup>3</sup> ????????? Java ???????????????????????????
2018-02-20 10:40:05 +08:00
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/1ea4dc9a-c4dd-46b5-bb11-49f98d57ded1.png)
2018-02-22 14:47:22 +08:00
**?????**
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
???????????????????????<3F><>??????????<3F><>?????????????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
**??????**
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
??<3F><>???????????????????????????????????????????
2018-02-20 10:40:05 +08:00
### 3. ThreeSum
2018-02-22 14:47:22 +08:00
ThreeSum ???????????????????????????? 0 ????????
2018-02-20 10:40:05 +08:00
```java
public class ThreeSum {
public static int count(int[] a) {
int N = a.length;
int cnt = 0;
for (int i = 0; i < N; i++) {
for (int j = i + 1; j < N; j++) {
for (int k = j + 1; k < N; k++) {
if (a[i] + a[j] + a[k] == 0) {
cnt++;
}
}
}
}
return cnt;
}
}
```
2018-02-22 14:47:22 +08:00
?<3F><>?????????? if (a[i] + a[j] + a[k] == 0) ????????<3F><>????? N<sup>3</sup>/6-N<sup>2</sup>/2+N/3??????????????<3F><>???? \~N<sup>3</sup>/6????????????? N<sup>3</sup>??
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
**???**
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
??????????????????????????????????????????????????<3F><>????????????????????????????????? 0??
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
?<3F><>???????? ThreeSum ????????????????? N<sup>2</sup>logN??
2018-02-20 10:40:05 +08:00
```java
public class ThreeSumFast {
public static int count(int[] a) {
Arrays.sort(a);
int N = a.length;
int cnt = 0;
for (int i = 0; i < N; i++) {
for (int j = i + 1; j < N; j++) {
for (int k = j + 1; k < N; k++) {
2018-02-22 14:47:22 +08:00
// rank() ??????????????????<3F><>??<3F><>???????????????????? -1??????????????<3F><>??????? j????????????????????
2018-02-20 10:40:05 +08:00
if (BinarySearch.rank(-a[i] - a[j], a) > j) {
cnt++;
}
}
}
}
return cnt;
}
}
```
2018-02-22 14:47:22 +08:00
### 4. ???????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
??? T(N) \~ aN<sup>b</sup>lgN????? T(2N)/T(N) \~ 2<sup>b</sup>?????????????????? ThreeSum ???????????? \~N<sup>3</sup>/6?????????<3F><>????????????????
2018-02-20 10:40:05 +08:00
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/6f5ed46f-86d7-4852-a34f-c1cf1b6343a0.png)
2018-02-22 14:47:22 +08:00
??? T(2N)/T(N)\~2<sup>3</sup>??????? b ? 3??
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
### 5. ???????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
**????**
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
???????????????????????????????????????????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
**????**
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
??????????????<3F>y?????????<3F><>?????????????????????????????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
**?????????????????**
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
??????????????????????????????<3F><>?????????????????????????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
**???????**
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
?????????????????????????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
**???????**
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
?????<3F><>??????????????????????????????????????????????? N ???????? push() ???????????????????? N+4+8+16+...+2N=5N-4??N ????????<3F><>??????????????????????<3F><>????<3F><>?????????????????????????????<3F><>??????????????????????????
2018-02-20 10:40:05 +08:00
## union-find
2018-02-22 14:47:22 +08:00
**????**
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
?????????????????????????????????????<3F><>???????????????
2018-02-20 10:40:05 +08:00
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/5d387d02-6f96-44d6-b5d0-4538349f868e.png)
**API**
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/a9b91b7d-65d7-4aa3-8ef6-21876b05ad16.png)
2018-02-22 14:47:22 +08:00
**?????????**
2018-02-20 10:40:05 +08:00
```java
public class UF {
2018-02-22 14:47:22 +08:00
// ??? id ??????????????????
2018-02-20 10:40:05 +08:00
private int[] id;
public UF(int N) {
id = new int[N];
for (int i = 0; i < N; i++) {
id[i] = i;
}
}
public boolean connected(int p, int q) {
return find(p) == find(q);
}
}
```
2018-02-22 14:47:22 +08:00
### 1. quick-find ??
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
???????????????????<3F><>???? id ?????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
????????????????????????? id ????????<3F><>????????????????????? union ??????????????????????????????????<3F><>????<3F><>?? id ???????????????? id ???
2018-02-20 10:40:05 +08:00
```java
public int find(int p) {
return id[p];
}
public void union(int p, int q) {
int pID = find(p);
int qID = find(q);
if (pID == qID) return;
for (int i = 0; i < id.length; i++) {
if (id[i] == pID) id[i] = qID;
}
}
```
2018-02-22 14:47:22 +08:00
### 2. quick-union ??
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
?? union ????????? id ????????????? id ?????????? id ???<3F><>???????????????????????????????????<3F><>???????????????????????<3F><>????????????????????????????????????????????????????? id ??????????????? id???
2018-02-20 10:40:05 +08:00
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/9192dc0a-a7cd-4030-8df6-e388600644cf.jpg)
```java
public int find(int p) {
while (p != id[p]) p = id[p];
return p;
}
public void union(int p, int q) {
int pRoot = find(p);
int qRoot = find(q);
if (pRoot == qRoot) return;
id[pRoot] = qRoot;
}
```
2018-02-22 14:47:22 +08:00
????????????????? union ?????????? find ??????????????????????????????????????????
2018-02-20 10:40:05 +08:00
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/d206d090-d911-4263-a1fe-d6f63f5d1776.png)
2018-02-22 14:47:22 +08:00
### 3. ??? quick-union ??
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
????? quick-union ??????????????????? quick-union ?? union ??????????<3F><>????????????????<3F><>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
?????<3F><>????????? quick-union ??????????????????? lgN??
2018-02-20 10:40:05 +08:00
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/8d6af5ac-74eb-4e07-99aa-654b9f21f1d3.jpg)
```java
public class WeightedQuickUnionUF {
private int[] id;
2018-02-22 14:47:22 +08:00
// ??????????????
2018-02-20 10:40:05 +08:00
private int[] sz;
public WeightedQuickUnionUF(int N) {
id = new int[N];
sz = new int[N];
for (int i = 0; i < N; i++) {
id[i] = i;
sz[i] = 1;
}
}
public boolean connected(int p, int q) {
return find(p) == find(q);
}
public int find(int p) {
while (p != id[p]) p = id[p];
return p;
}
public void union(int p, int q) {
int i = find(p);
int j = find(q);
if (i == j) return;
if (sz[i] < sz[j]) {
id[i] = j;
sz[j] += sz[i];
} else {
id[j] = i;
sz[i] += sz[j];
}
}
}
```
2018-02-22 14:47:22 +08:00
### 4. <20><>????????? quick-union ??
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
?????????????????????????????????? find ??????????????<3F><>?
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
### 5. ???? union-find ??????
2018-02-20 10:40:05 +08:00
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/e5baeb38-0ec9-4ad7-8374-1cdb0dba74a6.jpg)
2018-02-22 14:47:22 +08:00
# ????? ????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
## ??????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
### 1. ???
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
???????????????? Java ?? Comparable ?????????? compareTo() ??????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
?<3F><>?????????????????????????????????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
?????????? less() ?? exch() ?????<3F><>?????????????????????????????????<3F><>?
2018-02-20 10:40:05 +08:00
```java
private static boolean less(Comparable v, Comparable w){
return v.compareTo(w) < 0;
}
private void exch(Comparable[] a, int i, int j){
Comparable t = a[i];
a[i] = a[j];
a[j] = t;
}
```
2018-02-22 14:47:22 +08:00
### 2. ???????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
????????<3F><>???<3F><>??????????????????????????<3F><>?<3F><>?????????????????????<3F><>???????????????????????????<3F><>?<3F><>????????????????????????????????????
2018-02-20 10:40:05 +08:00
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/222768a7-914f-4d64-b874-d98f3b926fb6.jpg)
```java
public class Selection {
public static void sort(Comparable[] a) {
int N = a.length;
for (int i = 0; i < N; i++) {
int min = i;
for (int j = i + 1; j < N; j++) {
if (less(a[j], a[min])) min = j;
}
exch(a, i, min);
}
}
}
```
2018-02-22 14:47:22 +08:00
?????????? \~N<sup>2</sup>/2 ?<3F><>??? \~N ?<3F><>????????????????????????????????????????????????????????????????????????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
### 3. ????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
???????????????????????<3F><>???<3F><>?????????????????????????????????????????????<3F><>???????????????????????
2018-02-20 10:40:05 +08:00
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/065c3bbb-3ea0-4dbf-8f26-01d0e0ba7db7.png)
```java
public class Insertion {
public static void sort(Comparable[] a) {
int N = a.length;
for (int i = 1; i < N; i++) {
for (int j = i; j > 0 && less(a[j], a[j - 1]); j--) {
exch(a, j, j - 1);
}
}
}
}
```
2018-02-22 14:47:22 +08:00
????????????????????????????????????????????????????????????????????????????????? \~N<sup>2</sup>/4 ?????? \~N<sup>2</sup>/4 ?<3F><>????????????????? \~N<sup>2</sup>/2 ?????? \~N<sup>2</sup>/2 ?<3F><>???????????????????????????????????????? N-1 ?<3F><>??? 0 ?<3F><>??????????????????????????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
???????????????????????<3F><>???????????<3F><>??
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
### 4. ??????????????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
??????????????????????????<3F><>?????????????????????????????????????????????????<3F><>???????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
### 5. ???????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
????????????<3F><>???????????????????????????????????????????????????????????????<3F><>?????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
????????????????????????????????????????????????????????????????????????????<3F><>?????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
?????????<3F><>?????????? h ?????<3F><>?????????? h ???????????????????????????????????<3F><> h??????? h=1??????????????????????????
2018-02-20 10:40:05 +08:00
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/8320bad6-3f91-4a15-8e3d-68e8f39649b5.png)
```java
public class Shell {
public static void sort(Comparable[] a) {
int N = a.length;
int h = 1;
while (h < N / 3) {
h = 3 * h + 1;// 1, 4, 13, 40, ...
}
while (h >= 1) {
for (int i = h; i < N; i++) {
for (int j = i; j >= h && less(a[j], a[j - h]); j -= h) {
exch(a, j, j - h);
}
}
h = h / 3;
}
}
}
```
2018-02-22 14:47:22 +08:00
??????????????????????????????????? 1, 4, 13, 40, ... ??????????????????????????? N ?????????????????<3F><>??????????????????????????????????????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
## ?<3F><>????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
?<3F><>?????????????????????????????????????<3F><>??????
2018-02-20 10:40:05 +08:00
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/dcf265ad-fe35-424d-b4b7-d149cdf239f4.png)
2018-02-22 14:47:22 +08:00
### 1. ?<3F><>????
2018-02-20 10:40:05 +08:00
```java
public class MergeSort {
private static Comparable[] aux;
private static void merge(Comparable[] a, int lo, int mid, int hi) {
int i = lo, j = mid + 1;
for (int k = lo; k <= hi; k++) {
2018-02-22 14:47:22 +08:00
aux[k] = a[k]; // ??????????????????
2018-02-20 10:40:05 +08:00
}
for (int k = lo; k <= hi; k++) {
if (i > mid) a[k] = aux[j++];
else if (j > hi) a[k] = aux[i++];
2018-02-22 14:47:22 +08:00
else if (aux[i].compareTo(a[j]) < 0) a[k] = aux[i++]; // ????????????????????
2018-02-20 10:40:05 +08:00
else a[k] = aux[j++];
}
}
}
```
2018-02-22 14:47:22 +08:00
### 2. ???????<3F><>????
2018-02-20 10:40:05 +08:00
```java
public static void sort(Comparable[] a) {
aux = new Comparable[a.length];
sort(a, 0, a.length - 1);
}
private static void sort(Comparable[] a, int lo, int hi) {
if (hi <= lo) return;
int mid = lo + (hi - lo) / 2;
sort(a, lo, mid);
sort(a, mid + 1, hi);
merge(a, lo, mid, hi);
}
```
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/6468a541-3a9a-4008-82b6-03a0fe941d2a.png)
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/c7665f73-c52f-4ce4-aed3-592bbd76265b.png)
2018-02-22 14:47:22 +08:00
?????????????????????<3F><>???? O(Nlg<sub>N</sub>)??
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
???<3F><>?????????????????????????<3F><>?????????????<3F><>???<3F>q?????????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
### 3. ???????<3F><>????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
??<3F><>??<3F><>??????<3F><>?????<3F><>??????????<3F><>
2018-02-20 10:40:05 +08:00
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/c7b9b4c8-83d1-4eb0-8408-ea6576a9ed90.png)
```java
public static void busort(Comparable[] a) {
int N = a.length;
aux = new Comparable[N];
for (int sz = 1; sz < N; sz += sz) {
for (int lo = 0; lo < N - sz; lo += sz + sz) {
merge(a, lo, lo + sz - 1, Math.min(lo + sz + sz - 1, N - 1));
}
}
}
```
2018-02-22 14:47:22 +08:00
## ????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
### 1. ??????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
?<3F><>????????????????????????????????????????<3F><>?????????????????????????????<3F><>????????????????????<3F><>????????<3F><>??????<3F><>????????????????????<3F><>??????????????????????????????????????????
2018-02-20 10:40:05 +08:00
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/61b4832d-71f3-413c-84b6-237e219b9fdc.png)
```java
public class QuickSort {
public static void sort(Comparable[] a) {
shuffle(a);
sort(a, 0, a.length - 1);
}
private static void sort(Comparable[] a, int lo, int hi) {
if (hi <= lo) return;
int j = partition(a, lo, hi);
sort(a, lo, j - 1);
sort(a, j + 1, hi);
}
}
```
2018-02-22 14:47:22 +08:00
### 2. ?<3F><>?
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
? a[lo] ????<3F><>????????????????????????????????????????????????????????????????????????????<3F><>???????????????????????????????????????????????????????????????????????<3F><>?????????? j ???????????<3F><>???<3F><>????????????????????????<3F><>???? a[lo] ??????????????????? a[j] ???????? j ???<3F><>?
2018-02-20 10:40:05 +08:00
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/e198c201-f386-4491-8ad6-f7e433bf992d.png)
```java
private static int partition(Comparable[] a, int lo, int hi) {
int i = lo, j = hi + 1;
Comparable v = a[lo];
while (true) {
while (less(a[++i], v)) if (i == hi) break;
while (less(v, a[--j])) if (j == lo) break;
if (i >= j) break;
exch(a, i, j);
}
exch(a, lo, j);
return j;
}
```
2018-02-22 14:47:22 +08:00
### 3. ???????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
????????????????????????????<3F><>???????????????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
?????????????????????<3F><>???????????????????????????????????????????????<3F><>?????? C<sub>N</sub>=2C<sub>N/2</sub>+N???????????? O(Nlg<sub>N</sub>)??
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
??????????????<3F><>???<3F><>??????<3F><>??????<3F><>???<3F><>??????<3F><>?????????????????????????? N<sup>2</sup>/2????????????????????????????<3F><>?????????????????????<3F><>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
### 4. ?????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
#### 4.1 ?<3F><>???????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
?????????????<3F><>?????????????????????<3F><>???<3F><>??????????????????????????????<3F><>?????<3F><>????<3F><>???????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
#### 4.2 ?????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
?????????????<3F><>???????????<3F><>??????<3F><>??????????????<3F><>?????????????????? 3 ??????????<3F><>???<3F><>????????<3F><>?????<3F><>????<3F><>?
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
#### 4.3 ?????<3F><>?
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
?????<3F><>?????????????<3F><>??????????<3F><>??????????????<3F><>????????????<3F><>?????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
?????<3F><>??????????????????????????????????????????????????????
2018-02-20 10:40:05 +08:00
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/9d2226dc-c4a3-40ec-9b3e-a46bf86af499.png)
```java
public class Quick3Way {
public static void sort(Comparable[] a, int lo, int hi) {
if (hi <= lo) return;
int lt = lo, i = lo + 1, gt = hi;
Comparable v = a[lo];
while (i <= gt) {
int cmp = a[i].compareTo(v);
if (cmp < 0) exch(a, lt++, i++);
else if (cmp > 0) exch(a, i, gt--);
else i++;
}
sort(a, lo, lt - 1);
sort(a, gt + 1, hi);
}
}
```
2018-02-22 14:47:22 +08:00
## ???????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
????????????????????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
### 1. ??
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
???<3F><>???????????????????????????????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
?????????????????????????????????????????????????????????<3F><>???????<3F><>?<3F><>?? k ?????????<3F><>??? k/2?????????????????<3F><>?<3F><>??? 2k ?? 2k+1??????????????????????? 0 ??<3F><>?????????????????????????
2018-02-20 10:40:05 +08:00
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/a9b6c1db-0f4a-4e91-8ac8-6b19bd106b51.png)
```java
public class MaxPQ<Key extends Comparable<Key> {
private Key[] pq;
private int N = 0;
public MaxPQ(int maxN) {
pq = (Key[]) new Comparable[maxN + 1];
}
public boolean isEmpty() {
return N == 0;
}
public int size() {
return N;
}
private boolean less(int i, int j) {
return pq[i].compareTo(pq[j]) < 0;
}
private void exch(int i, int j) {
Key t = pq[i];
pq[i] = pq[j];
pq[j] = t;
}
}
```
2018-02-22 14:47:22 +08:00
### 2. ????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
????<3F><>????????????????????????????????????????????????????????????????????<3F><>?????????????????????????????
2018-02-20 10:40:05 +08:00
```java
private void swim(int k) {
while (k > 1 && less(k / 2, k)) {
exch(k / 2, k);
k = k / 2;
}
}
```
2018-02-22 14:47:22 +08:00
???????????????????????<3F><>??????????????<3F><>???????????????????????????????????????????????????????????????????????<3F><>?????
2018-02-20 10:40:05 +08:00
```java
private void sink(int k) {
while (2 * k <= N) {
int j = 2 * k;
if (j < N && less(j, j + 1)) j++;
if (!less(k, j)) break;
exch(k, j);
k = j;
}
}
```
2018-02-22 14:47:22 +08:00
### 3. ???????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
??????????????<3F><>???????????????<3F><>?<3F><>?
2018-02-20 10:40:05 +08:00
```java
public void insert(Key v) {
pq[++N] = v;
swim(N);
}
```
2018-02-22 14:47:22 +08:00
### 4. ?????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
?????<3F>B??????????????????????????????????????????????????????????<3F><>?<3F><>?
2018-02-20 10:40:05 +08:00
```java
public Key delMax() {
Key max = pq[1];
exch(1, N--);
pq[N + 1] = null;
sink(1);
return max;
}
```
2018-02-22 14:47:22 +08:00
### 5. ??????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
????????????????????????????????????????????????????????????<3F><>????????????????????????????????????<3F><>??????????????????????????????<3F><>??????????<3F><>?????????????????????????<3F><>?????????????????????????????????????????????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
???????????????<3F><>???????????????????<3F>n??????????????????????????????????????????????????????????????????????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
???????<3F>n??????????????????????????<3F><>??????????????????????<3F><>????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????<3F><>?
2018-02-20 10:40:05 +08:00
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/a2670745-a7b1-497b-90a4-dbddc4e2006d.jpg)
```java
public static void sort(Comparable[] a){
int N = a.length;
for(int k = N/2; k >= 1; k--){
sink(a, k, N);
}
while(N > 1){
exch(a, 1, N--);
sink(a, 1, N);
}
}
```
2018-02-22 14:47:22 +08:00
### 6. ????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
????????? lgN?????????<3F><>??????????????????????? lgN??
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
???????????????? N ????????????????????????? NlgN??
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
??????????????????????????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
???????????????????????????????????<3F><>??????????????????????????<3F><>???
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
## ???
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
### 1. ??????????
2018-02-20 10:40:05 +08:00
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/be53c00b-2534-4dc6-ad03-c55995c47db9.jpg)
2018-02-22 14:47:22 +08:00
??????????????????????????????????????????????????????????<3F><>??????????????????????????????????????????? \~cNlgN??????? c ???????????????????????????<3F><>??????????<3F><>???????????<3F><>????????<3F><>????????????????????????????????????????????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
### 2. Java ???????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
Java ?????<3F><>?????????? java.util.Arrays.sort()????????????????????????<3F><>??????????????????????<3F><><EFBFBD><EFBFBD>????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
### 3. ?????<3F><>??????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
????????? partition() ????????????? a[lo] ?? a[hi] ?????????????????? j ??? a[lo..j-1] <20><>????? a[j]???? a[j+1..hi] ??????? a[j]???????? j=k??a[j] ????? k ??????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
??????????????????????????????????????????????? (N+N/2+N/4+..)?????????? k ??????????????<3F><>?? 2N??
2018-02-20 10:40:05 +08:00
```java
public static Comparable select(Comparable[] a, int k) {
int lo = 0, hi = a.length - 1;
while (hi > lo) {
int j = partion(a, lo, hi);
if (j == k) return a[k];
else if (j > k) hi = j - 1;
else lo = j + 1;
}
return a[k];
}
```
2018-02-22 14:47:22 +08:00
# ?????? ????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
?????????????????????????????<3F><>??????????????????????????<3F><>?
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
## ?????
2018-02-20 10:40:05 +08:00
2018-02-21 16:33:00 +08:00
### 1. API
2018-02-20 10:40:05 +08:00
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/b69d7184-ab62-4957-ba29-fb4fa25f9b65.jpg)
2018-02-22 14:47:22 +08:00
??????????? null ???????????????????????????? put(key, null) ??? delete(key) ????????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
### 2. ????????
2018-02-20 10:40:05 +08:00
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/ba6ae411-82da-4d86-a434-6776d1731e8e.jpg)
2018-02-22 14:47:22 +08:00
???????????????? Comparable ????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
????????????????????????????<3F><>???????????????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
### 3. ???????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
???????????????<3F><>??? N ??????????? \~N<sup>2</sup>/2 ?<3F><>???
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
### 4. ???????????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
????????????<3F><>????<3F><>??????<3F><>???
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
?????????? Key ????? Comparable ???????????? Value ????? Object ???????<3F><>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
rank() ??????????????????????????????????<3F><>???<3F><>???????????????????????????<3F><>??????????
2018-02-20 10:40:05 +08:00
```java
public class BinarySearchST<Key extends Comparable<Key>, Value> {
private Key[] keys;
private Value[] values;
private int N;
public BinarySearchST(int capacity) {
keys = (Key[]) new Comparable[capacity];
values = (Value[]) new Object[capacity];
}
public int size() {
return N;
}
public Value get(Key key) {
int i = rank(key);
if (i < N && keys[i].compareTo(key) == 0) {
return values[i];
}
return null;
}
public int rank(Key key) {
int lo = 0, hi = N - 1;
while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
int cmp = key.compareTo(keys[mid]);
if (cmp == 0) return mid;
else if (cmp < 0) hi = mid - 1;
else lo = mid + 1;
}
return lo;
}
public void put(Key key, Value value) {
int i = rank(key);
if (i < N && keys[i].compareTo(key) == 0) {
values[i] = value;
return;
}
for (int j = N; j > i; j--) {
keys[j] = keys[j - 1];
values[j] = values[j - 1];
}
keys[i] = key;
values[i] = value;
N++;
}
public Key ceiling(Key key){
int i = rank(key);
return keys[i];
}
}
```
2018-02-22 14:47:22 +08:00
### 5. ????????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
??????????????????? lgN+1 ?<3F><>??????????????????????????????????????????????????????????????????????????????????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
## ?????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
???<3F><>?????????????????????????????????????????????????????????????????????????????????BST?????????????????????????????????????????<3F><>???????????<3F><>????????????????????
2018-02-20 10:40:05 +08:00
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/25226bb2-92cc-40cb-9e7f-c44e79fbb64a.jpg)
2018-02-22 14:47:22 +08:00
???????????????????<3F><>????????????????????????????????
2018-02-20 10:40:05 +08:00
```java
public class BST<Key extends Comparable<Key>, Value> {
private Node root;
private class Node {
private Key key;
private Value val;
private Node left, right;
2018-02-22 14:47:22 +08:00
// ???????????????<3F><>??????
2018-02-21 16:33:00 +08:00
private int N;
2018-02-20 10:40:05 +08:00
public Node(Key key, Value val, int N) {
this.key = key;
this.val = val;
this.N = N;
}
}
public int size() {
return size(root);
}
private int size(Node x) {
if (x == null) return 0;
return x.N;
}
}
```
2018-02-21 16:33:00 +08:00
### 1. get()
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
???????????????<3F><>???<3F><>???????????????????????????????<3F><>???????????????<3F><>?????????????????<3F><>???????????<3F><>?????????????????<3F><>????
2018-02-20 10:40:05 +08:00
```java
public Value get(Key key) {
return get(root, key);
}
private Value get(Node x, Key key) {
if (x == null) return null;
int cmp = key.compareTo(x.key);
if (cmp == 0) return x.val;
else if (cmp < 0) return get(x.left, key);
else return get(x.right, key);
}
```
2018-02-21 16:33:00 +08:00
### 2. put()
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
???????????????????<3F><>?????????????????????????????????????????????????<3F><>?
2018-02-20 10:40:05 +08:00
```java
public void put(Key key, Value val) {
root = put(root, key, val);
}
private Node put(Node x, Key key, Value val) {
if (x == null) return new Node(key, val, 1);
int cmp = key.compareTo(x.key);
if (cmp == 0) x.val = val;
else if (cmp < 0) x.left = put(x.left, key, val);
else x.right = put(x.right, key, val);
x.N = size(x.left) + size(x.right) + 1;
return x;
}
```
2018-02-22 14:47:22 +08:00
### 3. ????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
???????????????????????????????????????????????????????????????????????????????????????????????????????? lgN????????????????????? N??
2018-02-20 10:40:05 +08:00
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/73a3983d-dd18-4373-897e-64b706a7e370.jpg)
2018-02-22 14:47:22 +08:00
????????????????????????????
2018-02-20 10:40:05 +08:00
2018-02-21 16:33:00 +08:00
### 4. floor()
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
??? key <20><>??????? key?????<3F><>????? key ?????????????????????<3F><>???? key ????????? key????<3F><>?????????????<3F><>???<3F><>????? key ????<3F><>????? key ???????????????????<3F><>????????????<3F><>????? key ?????????
2018-02-20 10:40:05 +08:00
```java
public Key floor(Key key) {
Node x = floor(root, key);
if (x == null) return null;
return x.key;
}
private Node floor(Node x, Key key) {
if (x == null) return null;
int cmp = key.compareTo(x.key);
if (cmp == 0) return x;
if (cmp < 0) return floor(x.left, key);
Node t = floor(x.right, key);
if (t != null) {
return t;
} else {
return x;
}
}
```
2018-02-21 16:33:00 +08:00
### 5. rank()
2018-02-20 10:40:05 +08:00
```java
public int rank(Key key) {
return rank(key, root);
}
private int rank(Key key, Node x) {
if (x == null) return 0;
int cmp = key.compareTo(x.key);
if (cmp == 0) return size(x.left);
else if (cmp < 0) return rank(key, x.left);
else return 1 + size(x.left) + rank(key, x.right);
}
```
2018-02-21 16:33:00 +08:00
### 6. min()
2018-02-20 10:40:05 +08:00
```java
private Node min(Node x) {
if (x.left == null) return x;
return min(x.left);
}
```
2018-02-21 16:33:00 +08:00
### 7. deleteMin()
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
???????<3F><>?????????????<3F><>????????????
2018-02-20 10:40:05 +08:00
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/6e2cb20a-8d2a-46fe-9ac7-68a2126b7bd5.jpg)
```java
public void deleteMin() {
root = deleteMin(root);
}
public Node deleteMin(Node x) {
if (x.left == null) return x.right;
x.left = deleteMin(x.left);
x.N = size(x.left) + size(x.right) + 1;
return x;
}
```
2018-02-21 16:33:00 +08:00
### 8. delete()
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
???????????????????????????????????????????<3F><>????????????????????????????<3F><>????<3F>I????
2018-02-20 10:40:05 +08:00
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/b488282d-bfe0-464f-9e91-1f5b83a975bd.jpg)
```java
public void delete(Key key) {
root = delete(root, key);
}
private Node delete(Node x, Key key) {
if (x == null) return null;
int cmp = key.compareTo(x.key);
if (cmp < 0) x.left = delete(x.left, key);
else if (cmp > 0) x.right = delete(x.right, key);
else {
if (x.right == null) return x.left;
if (x.left == null) return x.right;
Node t = x;
x = min(t.right);
x.right = deleteMin(t.right);
x.left = t.left;
}
x.N = size(x.left) + size(x.right) + 1;
return x;
}
```
2018-02-21 16:33:00 +08:00
### 9. keys()
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
???????????????????????????????<3F><>????
2018-02-20 10:40:05 +08:00
```java
public Iterable<Key> keys(Key lo, Key hi) {
Queue<Key> queue = new LinkedList<>();
keys(root, queue, lo, hi);
return queue;
}
private void keys(Node x, Queue<Key> queue, Key lo, Key hi) {
if (x == null) return;
int cmpLo = lo.compareTo(x.key);
int cmpHi = hi.compareTo(x.key);
if (cmpLo < 0) keys(x.left, queue, lo, hi);
if (cmpLo <= 0 && cmpHi >= 0) queue.add(x.key);
if (cmpHi > 0) keys(x.right, queue, lo, hi);
}
```
2018-02-22 14:47:22 +08:00
### 10. ???????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
??????????????????<3F><>??????????????????????????????????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
## ????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
### 2-3 ??????
2018-02-20 10:40:05 +08:00
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/2548f2ec-7b00-4ec7-b286-20fc3022e084.jpg)
2018-02-22 14:47:22 +08:00
??????????? 2-3 ???????????<3F><>??????????????????????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
#### 1. ???????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
?????????????????? 4- ??????????? 4- ??????? 3 ?? 2- ????????<3F><>?? 2- ????????????<3F><>?????????????????????? 4- ??????????<3F><>???????????????????? 4- ???
2018-02-20 10:40:05 +08:00
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/912174d8-0786-4222-b7ef-a611d36e5db9.jpg)
2018-02-22 14:47:22 +08:00
#### 2. ????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
2-3 ???????????????<3F><>??????????????????????????????????????????????????????<3F><>????<3F><>???????????????????????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
2-3 ?????????????????????????????????????????????????????????????????????? logN ???????? 10 ??????? 2-3 ????????????????? 30 ????????????????????????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
### ???????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
2-3 ???????????? 2- ???? 3- ??????????<3F><>?????????? 3- ????????????????????????????????????????????????????? 3- ????????????????????
2018-02-20 10:40:05 +08:00
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/7080a928-06ba-4e10-9792-b8dd190dc8e2.jpg)
2018-02-22 14:47:22 +08:00
??????????????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
1. ???????????????
2. ????????????????????????????<3F><>???????????????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
???????????????????????
2018-02-20 10:40:05 +08:00
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/62077f5d-a06d-4129-9b43-78715b82cb03.png)
```java
public class RedBlackBST<Key extends Comparable<Key>, Value> {
private Node root;
private static final boolean RED = true;
private static final boolean BLACK = false;
private class Node {
Key key;
Value val;
Node left, right;
int N;
boolean color;
Node(Key key, Value val, int n, boolean color) {
this.key = key;
this.val = val;
N = n;
this.color = color;
}
}
private boolean isRed(Node x) {
if (x == null) return false;
return x.color == RED;
}
}
```
2018-02-22 14:47:22 +08:00
#### 1. ?????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
??????????????????????????????????????????????????????????????????
2018-02-20 10:40:05 +08:00
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/33a4e822-2dd0-481e-ac89-7f6161034402.jpg)
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/5e0cef33-4087-4f21-a428-16d5fddda671.jpg)
```java
public Node rotateLeft(Node h) {
Node x = h.right;
h.right = x.left;
x.left = h;
x.color = h.color;
h.color = RED;
x.N = h.N;
h.N = 1 + size(h.left) + size(h.right);
return x;
}
```
2018-02-22 14:47:22 +08:00
#### 2. ?????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
????????????????????????????????????????????????????????
2018-02-20 10:40:05 +08:00
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/dfd078b2-aa4f-4c50-8319-232922d822b8.jpg)
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/3f8d8c9d-a9a9-4d7a-813c-2de05ee5a97e.jpg)
```java
public Node rotateRight(Node h) {
Node x = h.left;
h.left = x.right;
x.color = h.color;
h.color = RED;
x.N = h.N;
h.N = 1 + size(h.left) + size(h.right);
return x;
}
```
2018-02-22 14:47:22 +08:00
#### 3. ??????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
??? 4- ??????????<3F><>?????????????????????????????? 4- ?????????????????????????????????????????????????? 2-3 ?????????????<3F><>???????????
2018-02-20 10:40:05 +08:00
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/de7c5a31-55f5-4e9d-92ec-4ed5b2ec3828.jpg)
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/e5ad625e-729d-4a8d-923a-7c3df5773e1c.jpg)
```java
void flipColors(Node h){
h.color = RED;
h.left.color = BLACK;
h.right.color = BLACK;
}
```
2018-02-22 14:47:22 +08:00
#### 4. ????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
- ????????????????????????????????????????
- ??????????????????????????????????????????????
- ???????????????????????????????
2018-02-20 10:40:05 +08:00
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/40639782-5df2-4e96-a4f3-f9dd664d0ca1.jpg)
```java
public void put(Key key, Value val) {
root = put(root, key, val);
root.color = BLACK;
}
private Node put(Node x, Key key, Value val) {
if (x == null) return new Node(key, val, 1, RED);
int cmp = key.compareTo(x.key);
if (cmp == 0) x.val = val;
else if (cmp < 0) x.left = put(x.left, key, val);
else x.right = put(x.right, key, val);
if (isRed(x.right) && !isRed(x.left)) x = rotateLeft(x);
if (isRed(x.left) && isRed(x.left.left)) x = rotateRight(x);
if (isRed(x.left) && isRed(x.right)) flipColors(x);
x.N = size(x.left) + size(x.right) + 1;
return x;
}
```
2018-02-22 14:47:22 +08:00
????????<3F><>???????? BST ???????????????????????????????????<3F><>???????<3F><>?
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
???????????????????????????????????????????????????????flipColors() ?<3F><>????????????????????????????????????????????????????? 1.
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
#### 5. ?????<3F><>??
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
?????<3F><>??????? 2- ????<3F><>????????<3F><>???????????????????????????????????????<3F><>?????? 2- ????<3F><>??? 2- ???????? 3- ?????? 4- ??????????????????????????????? key?????????????????? key???????????? 2- ???????????????????? key ?????????????<3F><>????????<3F><>?????? 2- ??????????????????<3F><>???????????????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
1. ?????????????????? 2- ???????
2. ?????????????????? 2- ??????????????? 2- ??????????????? key ??????
3. ???????????????????????????? 2- ?????????????????<3F><>???<3F><>???????????????????? 4- ???
2018-02-20 10:40:05 +08:00
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/b001fa64-307c-49af-b4b2-2043fc26154e.png)
2018-02-22 14:47:22 +08:00
??????????????<3F><>???? 3- ?????? 4- ????????????????????????????????? 4- ???
2018-02-20 10:40:05 +08:00
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/70b66757-755c-4e17-a7b7-5ce808023643.png)
2018-02-22 14:47:22 +08:00
#### 6. ????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
????<3F><>? N ??????????????? 2lgN?????????????????????? 2-3 ???<3F><>?????????<3F><>???????????? 3- ?????????? 2- ???
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
????????????????????????????????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
## ??<3F><>?
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
??<3F><>??????????<3F><>???????<3F><>????????????????????????????<3F><>???????????????????????????????????????????????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
??????????????????????<3F><>??????????<3F><>??????????????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
### ??<3F><>???
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
?????????<3F><>? M ????<3F><>???<3F><>????????????????? [0, M-1] ?????????????????????? hash ???
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
??<3F><>??<3F><>?????????????????????????????????? hash ???
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
??<3F><>????????????????????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
1. ???????????????????? hash ???
2. ??<3F><>??????????????<3F><>?????????? hash ???????????????? hash ????????????
3. ??????????<3F><>??? hash ????????????? [0, M-1] ???????????????????????????<3F><>???????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
?????????????????????<3F><>? [0, M-1] ???????????????? k?????? k%M ???????? [0, M-1] ???? hash ?????? M ?????????????????????????<3F><>???????????????????? M ? 10<sup>k</sup>???????????<3F><>???? k <20><>??
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
?????????????????????????????????????????<3F><>??????????????????????????????????????????????????????????????????????<3F><>???????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
?????<3F><>??????????????????????? hash ??????????????????????? hash ????????????????<3F><>????????<3F><>????? R ????????????????????????<3F><>????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
???<3F><>?????????<3F><>??????????
2018-02-20 10:40:05 +08:00
```java
int hash = 0;
for(int i = 0; i < s.length(); i++)
hash = (R * hash + s.charAt(i)) % M;
```
2018-02-22 14:47:22 +08:00
????<3F><>??<3F><>?????????????????????????
2018-02-20 10:40:05 +08:00
```java
int hash = (((day * R + month) % M) * R + year) % M;
```
2018-02-22 14:47:22 +08:00
R ????????????????? 31??
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
Java ?<3F><>? hashCode() ????? hash ????????????????????????????????? hashCode() ??????????????????????????<3F><>??????????? 32 <20><>?????????????? 31 <20><>???????????????????<3F><>???<3F><>???????<3F><>???????????
2018-02-20 10:40:05 +08:00
```java
int hash = (x.hashCode() & 0x7fffffff) % M;
```
2018-02-22 14:47:22 +08:00
??? Java ????? HashMap ????????????????????????? Key ????? hashCode() ??????????????????????? M ???<3F><>???Java ?<3F><> hashCode() ??????????????????<3F><>? 32 <20><>??????Java ?<3F><>? String??Integer ?????? hashCode() ??????????????????????????????????? hashCode()??
2018-02-20 10:40:05 +08:00
```java
public class Transaction{
private final String who;
private final Date when;
private final double amount;
public int hashCode(){
int hash = 17;
hash = 31 * hash + who.hashCode();
hash = 31 * hash + when.hashCode();
hash = 31 * hash + ((Double) amount).hashCode();
return hash;
}
}
```
2018-02-22 14:47:22 +08:00
### ??????????????<3F><>?
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
????????????????<3F><> hash ????????????????????????????????????????????? Key ???????????????????????????
2018-02-20 10:40:05 +08:00
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/540133af-aaaf-4208-8f7f-33cb89ac9621.png)
2018-02-22 14:47:22 +08:00
???? N ??????M ?????? (N>M)????????????????????????????????????????<3F><>?????? N/M?????<3F><>???<3F><>???????????????????????? \~N/M??
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
### ???????????????<3F><>?
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
???????????<3F><>?????????????????????????????????<3F><>???<3F><>?????????????????????????<3F><> M ????????????? N??M>N)??
2018-02-20 10:40:05 +08:00
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/2b3410f1-9559-4dd1-bc3d-e3e572247be2.png)
```java
public class LinearProbingHashST<Key, Value> {
private int N;
private int M = 16;
private Key[] keys;
private Value[] vals;
public LinearProbingHashST() {
init();
}
public LinearProbingHashST(int M) {
this.M = M;
init();
}
private void init() {
keys = (Key[]) new Object[M];
vals = (Value[]) new Object[M];
}
private int hash(Key key) {
return (key.hashCode() & 0x7fffffff) % M;
}
}
```
2018-02-22 14:47:22 +08:00
#### ????
2018-02-20 10:40:05 +08:00
```java
public Value get(Key key) {
for (int i = hash(key); keys[i] != null; i = (i + 1) % M) {
if (keys[i].equals(key)) {
return vals[i];
}
}
return null;
}
```
2018-02-22 14:47:22 +08:00
#### ????
2018-02-20 10:40:05 +08:00
```java
public void put(Key key, Value val) {
int i;
for (i = hash(key); keys[i] != null; i = (i + 1) % M) {
if (keys[i].equals(key)) {
vals[i] = val;
return;
}
}
keys[i] = key;
vals[i] = val;
N++;
resize();
}
```
2018-02-22 14:47:22 +08:00
#### ???
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
???????????????????????????????????<3F><>??<3F><>?
2018-02-20 10:40:05 +08:00
```java
public void delete(Key key) {
if (!contains(key)) return;
int i = hash(key);
while (!key.equals(keys[i])) {
i = (i + 1) % M;
}
keys[i] = null;
vals[i] = null;
i = (i + 1) % M;
while (keys[i] != null) {
Key keyToRedo = keys[i];
Value valToRedo = vals[i];
keys[i] = null;
vals[i] = null;
N--;
put(keyToRedo, valToRedo);
i = (i + 1) % M;
}
N--;
resize();
}
```
2018-02-22 14:47:22 +08:00
#### ?????????<3F><>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
??????????????????????????????????????<3F><>??????????????????????????????<3F><>??????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
?? = N/M???? ?? ????????????????????? ?? <20><>?? 1/2 ?????????????? 1.5 ?? 2.5 ???
2018-02-20 10:40:05 +08:00
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/0ddebc5c-7c24-46b1-98db-4fa5e54db16b.png)
2018-02-22 14:47:22 +08:00
???????<3F><>????????????????????<3F><>????? ?? ?? [1/4, 1/2] ???
2018-02-20 10:40:05 +08:00
```java
private void resize() {
if (N >= M / 2) resize(2 * M);
else if (N <= M / 8) resize(M / 2);
}
private void resize(int cap) {
LinearProbingHashST<Key, Value> t = new LinearProbingHashST<>(cap);
for (int i = 0; i < M; i++) {
if (keys[i] != null) {
t.put(keys[i], vals[i]);
}
}
keys = t.keys;
vals = t.vals;
M = t.M;
}
```
2018-02-22 14:47:22 +08:00
????????????????<3F>Y??????<3F><>??????????????<3F><>?????????????????????????????????????<3F><>???????????????????????<3F>A????????????????????? 1??????????????????????????????????????????????????
2018-02-20 10:40:05 +08:00
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/01658047-0d86-4a7a-a8ca-7ea20fa1fdde.png)
2018-02-22 14:47:22 +08:00
## ???
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
### ??????????????
2018-02-20 10:40:05 +08:00
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/9ee83c8c-1165-476c-85a6-e6e434e5307a.jpg)
2018-02-22 14:47:22 +08:00
????????????<3F><>??????????????????<3F><>??????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
### Java ?????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
Java ?? java.util.TreeMap ?? java.util.HashMap ????????????????????????<3F><>??????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
### ????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
?????????????????????????????<3F><>????????????<3F><>??????????<3F><>???<3F><>??????<3F><>???????????????<3F><>?
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
### ??????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
?????????<3F>p?? N ?<3F><>???????????????????????????<3F><>???????<3F><>?????<3F><>?? 0 ???????????<3F><>??????????????<3F><>?? 0 ?????<3F><>??<3F><>?
2018-02-20 10:40:05 +08:00
```java
import java.util.HashMap;
public class SparseVector {
private HashMap<Integer, Double> hashMap;
public SparseVector(double[] vector) {
hashMap = new HashMap<>();
for (int i = 0; i < vector.length; i++) {
if (vector[i] != 0) {
hashMap.put(i, vector[i]);
}
}
}
public double get(int i) {
return hashMap.getOrDefault(i, 0.0);
}
public double dot(SparseVector other) {
double sum = 0;
for (int i : hashMap.keySet()) {
sum += this.get(i) * other.get(i);
}
return sum;
}
}
```