Skip to content

Commit a09632f

Browse files
committed
[docs update]经典LeetCode题目推荐+红黑树内容完善
1 parent abaa691 commit a09632f

File tree

13 files changed

+80
-46
lines changed

13 files changed

+80
-46
lines changed

docs/.vuepress/sidebar/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,8 @@ export default sidebar({
239239
icon: "suanfaku",
240240
collapsible: true,
241241
children: [
242+
"classical-algorithm-problems-recommendations",
243+
"common-data-structures-leetcode-recommendations",
242244
"string-algorithm-problems",
243245
"linkedlist-algorithm-problems",
244246
"the-sword-refers-to-offer",

docs/cs-basics/algorithms/classical-algorithm-problems-recommended.md renamed to docs/cs-basics/algorithms/classical-algorithm-problems-recommendations.md

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
# 经典算法思想介绍
1+
---
2+
title: 经典算法思想总结(含LeetCode题目推荐)
3+
category: 计算机基础
4+
tag:
5+
- 算法
6+
---
27

38
## 贪心算法
49

@@ -19,27 +24,27 @@
1924

2025
121.买卖股票的最佳时机:https://leetcode.cn/problems/best-time-to-buy-and-sell-stock/
2126

22-
122.买卖股票的最佳时机IIhttps://leetcode.cn/problems/best-time-to-buy-and-sell-stock-ii/
27+
122.买卖股票的最佳时机 IIhttps://leetcode.cn/problems/best-time-to-buy-and-sell-stock-ii/
2328

2429
55.跳跃游戏:https://leetcode.cn/problems/jump-game/
2530

26-
45.跳跃游戏IIhttps://leetcode.cn/problems/jump-game-ii/
31+
45.跳跃游戏 IIhttps://leetcode.cn/problems/jump-game-ii/
2732

2833
## 动态规划
2934

3035
### 算法思想
3136

3237
动态规划中每一个状态一定是由上一个状态推导出来的,这一点就区分于贪心,贪心没有状态推导,而是从局部直接选最优的。
3338

34-
经典题目:01背包、完全背包
39+
经典题目:01 背包、完全背包
3540

3641
### 一般解题步骤
3742

38-
- 确定dp数组(dp table)以及下标的含义
43+
- 确定 dp 数组(dp table)以及下标的含义
3944
- 确定递推公式
40-
- dp数组如何初始化
45+
- dp 数组如何初始化
4146
- 确定遍历顺序
42-
- 举例推导dp数组
47+
- 举例推导 dp 数组
4348

4449
### LeetCode
4550

@@ -63,7 +68,7 @@
6368

6469
件时,就“回溯”返回,尝试别的路径。其本质就是穷举。
6570

66-
经典题目:8皇后
71+
经典题目:8 皇后
6772

6873
### 一般解题步骤
6974

@@ -77,19 +82,19 @@
7782

7883
39.组合总和:https://leetcode.cn/problems/combination-sum/
7984

80-
40.组合总和IIhttps://leetcode.cn/problems/combination-sum-ii/
85+
40.组合总和 IIhttps://leetcode.cn/problems/combination-sum-ii/
8186

8287
78.子集:https://leetcode.cn/problems/subsets/
8388

84-
90.子集IIhttps://leetcode.cn/problems/subsets-ii/
89+
90.子集 IIhttps://leetcode.cn/problems/subsets-ii/
8590

86-
51.N皇后https://leetcode.cn/problems/n-queens/
91+
51.N 皇后https://leetcode.cn/problems/n-queens/
8792

8893
## 分治算法
8994

9095
### 算法思想
9196

92-
将一个规模为N的问题分解为K个规模较小的子问题,这些子问题相互独立且与原问题性质相同。求出子问题的解,就可得到原问题的解。
97+
将一个规模为 N 的问题分解为 K 个规模较小的子问题,这些子问题相互独立且与原问题性质相同。求出子问题的解,就可得到原问题的解。
9398

9499
经典题目:二分查找、汉诺塔问题
95100

@@ -105,5 +110,5 @@
105110

106111
148.排序列表:https://leetcode.cn/problems/sort-list/
107112

108-
23.合并k个升序链表https://leetcode.cn/problems/merge-k-sorted-lists/
113+
23.合并 k 个升序链表https://leetcode.cn/problems/merge-k-sorted-lists/
109114

docs/cs-basics/data-structure/leetcode-recommended.md renamed to docs/cs-basics/algorithms/common-data-structures-leetcode-recommendations.md

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,43 @@
1-
# 数组
1+
---
2+
title: 常见数据结构经典LeetCode题目推荐
3+
category: 计算机基础
4+
tag:
5+
- 算法
6+
---
7+
8+
## 数组
29

310
704.二分查找:https://leetcode.cn/problems/binary-search/
411

5-
80.删除有序数组中的重复项IIhttps://leetcode.cn/problems/remove-duplicates-from-sorted-array-ii
12+
80.删除有序数组中的重复项 IIhttps://leetcode.cn/problems/remove-duplicates-from-sorted-array-ii
613

714
977.有序数组的平方:https://leetcode.cn/problems/squares-of-a-sorted-array/
815

9-
# 链表
16+
## 链表
1017

1118
707.设计链表:https://leetcode.cn/problems/design-linked-list/
1219

1320
206.反转链表:https://leetcode.cn/problems/reverse-linked-list/
1421

15-
92.反转链表IIhttps://leetcode.cn/problems/reverse-linked-list-ii/
22+
92.反转链表 IIhttps://leetcode.cn/problems/reverse-linked-list-ii/
1623

1724
61.旋转链表:https://leetcode.cn/problems/rotate-list/
1825

19-
# 栈与队列
26+
## 栈与队列
2027

2128
232.用栈实现队列:https://leetcode.cn/problems/implement-queue-using-stacks/
2229

2330
225.用队列实现栈:https://leetcode.cn/problems/implement-stack-using-queues/
2431

25-
347.前K个高频元素https://leetcode.cn/problems/top-k-frequent-elements/
32+
347.前 K 个高频元素https://leetcode.cn/problems/top-k-frequent-elements/
2633

2734
239.滑动窗口最大值:https://leetcode.cn/problems/sliding-window-maximum/
2835

29-
# 二叉树
36+
## 二叉树
3037

3138
105.从前序与中序遍历构造二叉树:https://leetcode.cn/problems/construct-binary-tree-from-preorder-and-inorder-traversal/
3239

33-
117.填充每个节点的下一个右侧节点指针IIhttps://leetcode.cn/problems/populating-next-right-pointers-in-each-node-ii
40+
117.填充每个节点的下一个右侧节点指针 IIhttps://leetcode.cn/problems/populating-next-right-pointers-in-each-node-ii
3441

3542
236.二叉树的最近公共祖先:https://leetcode.cn/problems/lowest-common-ancestor-of-a-binary-tree/
3643

@@ -40,10 +47,21 @@
4047

4148
530.二叉搜索树的最小绝对差:https://leetcode.cn/problems/minimum-absolute-difference-in-bst/
4249

43-
#
50+
##
4451

4552
200.岛屿数量:https://leetcode.cn/problems/number-of-islands/
4653

4754
207.课程表:https://leetcode.cn/problems/course-schedule/
4855

49-
210.课程表II:https://leetcode.cn/problems/course-schedule-ii/
56+
210.课程表 II:https://leetcode.cn/problems/course-schedule-ii/
57+
58+
##
59+
60+
215. 数组中的第 K 个最大元素:https://leetcode.cn/problems/kth-largest-element-in-an-array/
61+
62+
215. 数据流的中位数:https://leetcode.cn/problems/find-median-from-data-stream/
63+
64+
215. 前 K 个高频元素:https://leetcode.cn/problems/top-k-frequent-elements/
65+
66+
67+
-13.7 KB
Loading
-13.8 KB
Loading
-13.8 KB
Loading
-11.5 KB
Loading
-16.8 KB
Loading
-14 KB
Loading

docs/cs-basics/data-structure/red-black-tree.md

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,40 @@
11
---
2+
title: 红黑树
23
category: 计算机基础
34
tag:
45
- 数据结构
56
---
67

7-
# 红黑树
8+
## 红黑树介绍
89

9-
## 红黑树数据结构
10+
红黑树(Red Black Tree)是一种自平衡二叉查找树。它是在 1972 年由 Rudolf Bayer 发明的,当时被称为平衡二叉 B 树(symmetric binary B-trees)。后来,在 1978 年被 Leo J. Guibas 和 Robert Sedgewick 修改为如今的“红黑树”。
1011

11-
建立在 BST 二叉搜索树的基础上,AVL、2-3树、红黑树都是自平衡二叉树(统称B-树)。但相比于AVL树,高度平衡所带来的时间复杂度,红黑树对平衡的控制要宽松一些,红黑树只需要保证黑色节点平衡即可
12+
由于其自平衡的特性,保证了最坏情形下在 O(logn) 时间复杂度内完成查找、增加、删除等操作,性能表现稳定
1213

13-
## **红黑树特点**
14+
在 JDK 中,`TreeMap``TreeSet` 以及 JDK1.8 的 `HashMap` 底层都用到了红黑树。
15+
16+
## 为什么需要红黑树?
1417

15-
1. 每个节点非红即黑;
18+
红黑树的诞生就是为了解决二叉查找树的缺陷。
1619

17-
黑色决定平衡,红色不决定平衡。这对应了2-3树中一个节点内可以存放1~2个节点
20+
二叉查找树是一种基于比较的数据结构,它的每个节点都有一个键值,而且左子节点的键值小于父节点的键值,右子节点的键值大于父节点的键值。这样的结构可以方便地进行查找、插入和删除操作,因为只需要比较节点的键值就可以确定目标节点的位置。但是,二叉查找树有一个很大的问题,就是它的形状取决于节点插入的顺序。如果节点是按照升序或降序的方式插入的,那么二叉查找树就会退化成一个线性结构,也就是一个链表。这样的情况下,二叉查找树的性能就会大大降低,时间复杂度就会从 O(logn) 变为 O(n)
1821

19-
2. 根节点总是黑色的;
22+
红黑树的诞生就是为了解决二叉查找树的缺陷,因为二叉查找树在某些情况下会退化成一个线性结构。
2023

21-
3. 每个叶子节点都是黑色的空节点(NIL 节点);
24+
## **红黑树特点**
2225

23-
这里指的是红黑树都会有一个空的叶子节点,是红黑树自己的规则。
26+
1. 每个节点非红即黑。黑色决定平衡,红色不决定平衡。这对应了 2-3 树中一个节点内可以存放 1~2 个节点。
27+
2. 根节点总是黑色的。
28+
3. 每个叶子节点都是黑色的空节点(NIL 节点)。这里指的是红黑树都会有一个空的叶子节点,是红黑树自己的规则。
29+
4. 如果节点是红色的,则它的子节点必须是黑色的(反之不一定)。通常这条规则也叫不会有连续的红色节点。一个节点最多临时会有 3 个节点,中间是黑色节点,左右是红色节点。
30+
5. 从根节点到叶节点或空子节点的每条路径,必须包含相同数目的黑色节点(即相同的黑色高度)。每一层都只是有一个节点贡献了树高决定平衡性,也就是对应红黑树中的黑色节点。
2431

25-
4. 如果节点是红色的,则它的子节点必须是黑色的(反之不一定);
32+
正是这些特点才保证了红黑树的平衡,让红黑树的高度不会超过 2log(n+1)。
2633

27-
通常这条规则也叫不会有连续的红色节点。一个节点最多临时会有3个节点,中间是黑色节点,左右是红色节点。
2834

29-
5. 从根节点到叶节点或空子节点的每条路径,必须包含相同数目的黑色节点(即相同的黑色高度)。
35+
## 红黑树数据结构
3036

31-
每一层都只是有一个节点贡献了树高决定平衡性,也就是对应红黑树中的黑色节点
37+
建立在 BST 二叉搜索树的基础上,AVL、2-3 树、红黑树都是自平衡二叉树(统称 B-树)。但相比于 AVL 树,高度平衡所带来的时间复杂度,红黑树对平衡的控制要宽松一些,红黑树只需要保证黑色节点平衡即可
3238

3339
## 红黑树结构实现
3440

@@ -62,28 +68,27 @@ public class Node {
6268

6369
### 3.左旋调衡
6470

65-
#### 3.1一次左旋
71+
#### 3.1 一次左旋
6672

6773
![幻灯片3](pictures/红黑树/红黑树3.PNG)
6874

69-
#### 3.2右旋+左旋
75+
#### 3.2 右旋+左旋
7076

7177
![幻灯片4](pictures/红黑树/红黑树4.PNG)
7278

7379
### 4.右旋调衡
7480

75-
#### 4.1一次右旋
81+
#### 4.1 一次右旋
7682

7783
![幻灯片5](pictures/红黑树/红黑树5.PNG)
7884

79-
#### 4.2左旋+右旋
85+
#### 4.2 左旋+右旋
8086

8187
![幻灯片6](pictures/红黑树/红黑树6.PNG)
8288

83-
**红黑树的应用**:TreeMap、TreeSet 以及 JDK1.8 的 HashMap 底层都用到了红黑树。
84-
85-
**为什么要用红黑树?** 简单来说红黑树就是为了解决二叉查找树的缺陷,因为二叉查找树在某些情况下会退化成一个线性结构。详细了解可以查看 [漫画:什么是红黑树?](https://juejin.im/post/5a27c6946fb9a04509096248#comment)(也介绍到了二叉查找树,非常推荐)
89+
## 文章推荐
8690

87-
**相关阅读**[《红黑树深入剖析及 Java 实现》](https://zhuanlan.zhihu.com/p/24367771)(美团点评技术团队)
91+
- [《红黑树深入剖析及 Java 实现》 - 美团点评技术团队](https://zhuanlan.zhihu.com/p/24367771)
92+
- [漫画:什么是红黑树? - 程序员小灰](https://juejin.im/post/5a27c6946fb9a04509096248#comment)(也介绍到了二叉查找树,非常推荐)
8893

8994
<!-- @include: @article-footer.snippet.md -->

0 commit comments

Comments
 (0)