build
This commit is contained in:
parent
cf431646e9
commit
4d318e8e6b
@ -125,8 +125,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
|
||||
/* 随机返回一个数组元素 */
|
||||
int randomAccess(int[] nums) {
|
||||
// 在区间 [0, nums.length) 中随机抽取一个数字
|
||||
int randomIndex = ThreadLocalRandom.current().
|
||||
nextInt(0, nums.length);
|
||||
int randomIndex = ThreadLocalRandom.current().nextInt(0, nums.length);
|
||||
// 获取并返回随机元素
|
||||
int randomNum = nums[randomIndex];
|
||||
return randomNum;
|
||||
@ -137,7 +136,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
|
||||
|
||||
```cpp title="array.cpp"
|
||||
/* 随机返回一个数组元素 */
|
||||
int randomAccess(int* nums, int size) {
|
||||
int randomAccess(int *nums, int size) {
|
||||
// 在区间 [0, size) 中随机抽取一个数字
|
||||
int randomIndex = rand() % size;
|
||||
// 获取并返回随机元素
|
||||
@ -268,9 +267,9 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
|
||||
|
||||
```cpp title="array.cpp"
|
||||
/* 扩展数组长度 */
|
||||
int* extend(int* nums, int size, int enlarge) {
|
||||
int *extend(int *nums, int size, int enlarge) {
|
||||
// 初始化一个扩展长度后的数组
|
||||
int* res = new int[size + enlarge];
|
||||
int *res = new int[size + enlarge];
|
||||
// 将原数组中的所有元素复制到新数组
|
||||
for (int i = 0; i < size; i++) {
|
||||
res[i] = nums[i];
|
||||
@ -427,7 +426,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
|
||||
|
||||
```cpp title="array.cpp"
|
||||
/* 在数组的索引 index 处插入元素 num */
|
||||
void insert(int* nums, int size, int num, int index) {
|
||||
void insert(int *nums, int size, int num, int index) {
|
||||
// 把索引 index 以及之后的所有元素向后移动一位
|
||||
for (int i = size - 1; i > index; i--) {
|
||||
nums[i] = nums[i - 1];
|
||||
@ -549,7 +548,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
|
||||
|
||||
```cpp title="array.cpp"
|
||||
/* 删除索引 index 处元素 */
|
||||
void remove(int* nums, int size, int index) {
|
||||
void remove(int *nums, int size, int index) {
|
||||
// 把索引 index 之后的所有元素向前移动一位
|
||||
for (int i = index; i < size - 1; i++) {
|
||||
nums[i] = nums[i + 1];
|
||||
@ -680,7 +679,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
|
||||
|
||||
```cpp title="array.cpp"
|
||||
/* 遍历数组 */
|
||||
void traverse(int* nums, int size) {
|
||||
void traverse(int *nums, int size) {
|
||||
int count = 0;
|
||||
// 通过索引遍历数组
|
||||
for (int i = 0; i < size; i++) {
|
||||
@ -836,7 +835,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
|
||||
|
||||
```cpp title="array.cpp"
|
||||
/* 在数组中查找指定元素 */
|
||||
int find(int* nums, int size, int target) {
|
||||
int find(int *nums, int size, int target) {
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (nums[i] == target)
|
||||
return i;
|
||||
|
||||
@ -363,8 +363,8 @@ comments: true
|
||||
|
||||
```cpp title="linked_list.cpp"
|
||||
/* 在链表的节点 n0 之后插入节点 P */
|
||||
void insert(ListNode* n0, ListNode* P) {
|
||||
ListNode* n1 = n0->next;
|
||||
void insert(ListNode *n0, ListNode *P) {
|
||||
ListNode *n1 = n0->next;
|
||||
P->next = n1;
|
||||
n0->next = P;
|
||||
}
|
||||
@ -477,12 +477,12 @@ comments: true
|
||||
|
||||
```cpp title="linked_list.cpp"
|
||||
/* 删除链表的节点 n0 之后的首个节点 */
|
||||
void remove(ListNode* n0) {
|
||||
void remove(ListNode *n0) {
|
||||
if (n0->next == nullptr)
|
||||
return;
|
||||
// n0 -> P -> n1
|
||||
ListNode* P = n0->next;
|
||||
ListNode* n1 = P->next;
|
||||
ListNode *P = n0->next;
|
||||
ListNode *n1 = P->next;
|
||||
n0->next = n1;
|
||||
// 释放内存
|
||||
delete P;
|
||||
@ -618,7 +618,7 @@ comments: true
|
||||
|
||||
```cpp title="linked_list.cpp"
|
||||
/* 访问链表中索引为 index 的节点 */
|
||||
ListNode* access(ListNode* head, int index) {
|
||||
ListNode *access(ListNode *head, int index) {
|
||||
for (int i = 0; i < index; i++) {
|
||||
if (head == nullptr)
|
||||
return nullptr;
|
||||
@ -764,7 +764,7 @@ comments: true
|
||||
|
||||
```cpp title="linked_list.cpp"
|
||||
/* 在链表中查找值为 target 的首个节点 */
|
||||
int find(ListNode* head, int target) {
|
||||
int find(ListNode *head, int target) {
|
||||
int index = 0;
|
||||
while (head != nullptr) {
|
||||
if (head->val == target)
|
||||
|
||||
@ -718,17 +718,17 @@ comments: true
|
||||
```java title="my_list.java"
|
||||
/* 列表类简易实现 */
|
||||
class MyList {
|
||||
private int[] nums; // 数组(存储列表元素)
|
||||
private int capacity = 10; // 列表容量
|
||||
private int size = 0; // 列表长度(即当前元素数量)
|
||||
private int extendRatio = 2; // 每次列表扩容的倍数
|
||||
private int[] nums; // 数组(存储列表元素)
|
||||
private int capacity = 10; // 列表容量
|
||||
private int size = 0; // 列表长度(即当前元素数量)
|
||||
private int extendRatio = 2; // 每次列表扩容的倍数
|
||||
|
||||
/* 构造方法 */
|
||||
public MyList() {
|
||||
nums = new int[capacity];
|
||||
}
|
||||
|
||||
/* 获取列表长度(即当前元素数量)*/
|
||||
/* 获取列表长度(即当前元素数量) */
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
@ -820,13 +820,13 @@ comments: true
|
||||
```cpp title="my_list.cpp"
|
||||
/* 列表类简易实现 */
|
||||
class MyList {
|
||||
private:
|
||||
int* nums; // 数组(存储列表元素)
|
||||
int numsCapacity = 10; // 列表容量
|
||||
int numsSize = 0; // 列表长度(即当前元素数量)
|
||||
int extendRatio = 2; // 每次列表扩容的倍数
|
||||
private:
|
||||
int *nums; // 数组(存储列表元素)
|
||||
int numsCapacity = 10; // 列表容量
|
||||
int numsSize = 0; // 列表长度(即当前元素数量)
|
||||
int extendRatio = 2; // 每次列表扩容的倍数
|
||||
|
||||
public:
|
||||
public:
|
||||
/* 构造方法 */
|
||||
MyList() {
|
||||
nums = new int[numsCapacity];
|
||||
@ -907,7 +907,7 @@ comments: true
|
||||
void extendCapacity() {
|
||||
// 新建一个长度为 size * extendRatio 的数组,并将原数组拷贝到新数组
|
||||
int newCapacity = capacity() * extendRatio;
|
||||
int* tmp = nums;
|
||||
int *tmp = nums;
|
||||
nums = new int[newCapacity];
|
||||
// 将原数组中的所有元素复制到新数组
|
||||
for (int i = 0; i < size(); i++) {
|
||||
|
||||
@ -978,7 +978,8 @@ $$
|
||||
/* 线性阶(递归实现) */
|
||||
void linearRecur(int n) {
|
||||
System.out.println("递归 n = " + n);
|
||||
if (n == 1) return;
|
||||
if (n == 1)
|
||||
return;
|
||||
linearRecur(n - 1);
|
||||
}
|
||||
```
|
||||
@ -989,7 +990,8 @@ $$
|
||||
/* 线性阶(递归实现) */
|
||||
void linearRecur(int n) {
|
||||
cout << "递归 n = " << n << endl;
|
||||
if (n == 1) return;
|
||||
if (n == 1)
|
||||
return;
|
||||
linearRecur(n - 1);
|
||||
}
|
||||
```
|
||||
@ -1254,7 +1256,8 @@ $$
|
||||
```java title="space_complexity.java"
|
||||
/* 平方阶(递归实现) */
|
||||
int quadraticRecur(int n) {
|
||||
if (n <= 0) return 0;
|
||||
if (n <= 0)
|
||||
return 0;
|
||||
// 数组 nums 长度为 n, n-1, ..., 2, 1
|
||||
int[] nums = new int[n];
|
||||
System.out.println("递归 n = " + n + " 中的 nums 长度 = " + nums.length);
|
||||
@ -1267,7 +1270,8 @@ $$
|
||||
```cpp title="space_complexity.cpp"
|
||||
/* 平方阶(递归实现) */
|
||||
int quadraticRecur(int n) {
|
||||
if (n <= 0) return 0;
|
||||
if (n <= 0)
|
||||
return 0;
|
||||
vector<int> nums(n);
|
||||
cout << "递归 n = " << n << " 中的 nums 长度 = " << nums.size() << endl;
|
||||
return quadraticRecur(n - 1);
|
||||
@ -1384,7 +1388,8 @@ $$
|
||||
```java title="space_complexity.java"
|
||||
/* 指数阶(建立满二叉树) */
|
||||
TreeNode buildTree(int n) {
|
||||
if (n == 0) return null;
|
||||
if (n == 0)
|
||||
return null;
|
||||
TreeNode root = new TreeNode(0);
|
||||
root.left = buildTree(n - 1);
|
||||
root.right = buildTree(n - 1);
|
||||
@ -1396,9 +1401,10 @@ $$
|
||||
|
||||
```cpp title="space_complexity.cpp"
|
||||
/* 指数阶(建立满二叉树) */
|
||||
TreeNode* buildTree(int n) {
|
||||
if (n == 0) return nullptr;
|
||||
TreeNode* root = new TreeNode(0);
|
||||
TreeNode *buildTree(int n) {
|
||||
if (n == 0)
|
||||
return nullptr;
|
||||
TreeNode *root = new TreeNode(0);
|
||||
root->left = buildTree(n - 1);
|
||||
root->right = buildTree(n - 1);
|
||||
return root;
|
||||
|
||||
@ -49,13 +49,13 @@ comments: true
|
||||
|
||||
```cpp title="leetcode_two_sum.cpp"
|
||||
/* 方法一:暴力枚举 */
|
||||
vector<int> twoSumBruteForce(vector<int>& nums, int target) {
|
||||
vector<int> twoSumBruteForce(vector<int> &nums, int target) {
|
||||
int size = nums.size();
|
||||
// 两层循环,时间复杂度 O(n^2)
|
||||
for (int i = 0; i < size - 1; i++) {
|
||||
for (int j = i + 1; j < size; j++) {
|
||||
if (nums[i] + nums[j] == target)
|
||||
return { i, j };
|
||||
return {i, j};
|
||||
}
|
||||
}
|
||||
return {};
|
||||
@ -224,14 +224,14 @@ comments: true
|
||||
|
||||
```cpp title="leetcode_two_sum.cpp"
|
||||
/* 方法二:辅助哈希表 */
|
||||
vector<int> twoSumHashTable(vector<int>& nums, int target) {
|
||||
vector<int> twoSumHashTable(vector<int> &nums, int target) {
|
||||
int size = nums.size();
|
||||
// 辅助哈希表,空间复杂度 O(n)
|
||||
unordered_map<int, int> dic;
|
||||
// 单层循环,时间复杂度 O(n)
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (dic.find(target - nums[i]) != dic.end()) {
|
||||
return { dic[target - nums[i]], i };
|
||||
return {dic[target - nums[i]], i};
|
||||
}
|
||||
dic.emplace(nums[i], i);
|
||||
}
|
||||
|
||||
@ -1060,7 +1060,7 @@ $$
|
||||
|
||||
```cpp title="time_complexity.cpp"
|
||||
/* 线性阶(遍历数组) */
|
||||
int arrayTraversal(vector<int>& nums) {
|
||||
int arrayTraversal(vector<int> &nums) {
|
||||
int count = 0;
|
||||
// 循环次数与数组长度成正比
|
||||
for (int num : nums) {
|
||||
@ -1345,7 +1345,7 @@ $$
|
||||
```java title="time_complexity.java"
|
||||
/* 平方阶(冒泡排序) */
|
||||
int bubbleSort(int[] nums) {
|
||||
int count = 0; // 计数器
|
||||
int count = 0; // 计数器
|
||||
// 外循环:待排序元素数量为 n-1, n-2, ..., 1
|
||||
for (int i = nums.length - 1; i > 0; i--) {
|
||||
// 内循环:冒泡操作
|
||||
@ -1355,7 +1355,7 @@ $$
|
||||
int tmp = nums[j];
|
||||
nums[j] = nums[j + 1];
|
||||
nums[j + 1] = tmp;
|
||||
count += 3; // 元素交换包含 3 个单元操作
|
||||
count += 3; // 元素交换包含 3 个单元操作
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1367,8 +1367,8 @@ $$
|
||||
|
||||
```cpp title="time_complexity.cpp"
|
||||
/* 平方阶(冒泡排序) */
|
||||
int bubbleSort(vector<int>& nums) {
|
||||
int count = 0; // 计数器
|
||||
int bubbleSort(vector<int> &nums) {
|
||||
int count = 0; // 计数器
|
||||
// 外循环:待排序元素数量为 n-1, n-2, ..., 1
|
||||
for (int i = nums.size() - 1; i > 0; i--) {
|
||||
// 内循环:冒泡操作
|
||||
@ -1378,7 +1378,7 @@ $$
|
||||
int tmp = nums[j];
|
||||
nums[j] = nums[j + 1];
|
||||
nums[j + 1] = tmp;
|
||||
count += 3; // 元素交换包含 3 个单元操作
|
||||
count += 3; // 元素交换包含 3 个单元操作
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1749,7 +1749,8 @@ $$
|
||||
```java title="time_complexity.java"
|
||||
/* 指数阶(递归实现) */
|
||||
int expRecur(int n) {
|
||||
if (n == 1) return 1;
|
||||
if (n == 1)
|
||||
return 1;
|
||||
return expRecur(n - 1) + expRecur(n - 1) + 1;
|
||||
}
|
||||
```
|
||||
@ -1759,7 +1760,8 @@ $$
|
||||
```cpp title="time_complexity.cpp"
|
||||
/* 指数阶(递归实现) */
|
||||
int expRecur(int n) {
|
||||
if (n == 1) return 1;
|
||||
if (n == 1)
|
||||
return 1;
|
||||
return expRecur(n - 1) + expRecur(n - 1) + 1;
|
||||
}
|
||||
```
|
||||
@ -1999,7 +2001,8 @@ $$
|
||||
```java title="time_complexity.java"
|
||||
/* 对数阶(递归实现) */
|
||||
int logRecur(float n) {
|
||||
if (n <= 1) return 0;
|
||||
if (n <= 1)
|
||||
return 0;
|
||||
return logRecur(n / 2) + 1;
|
||||
}
|
||||
```
|
||||
@ -2009,7 +2012,8 @@ $$
|
||||
```cpp title="time_complexity.cpp"
|
||||
/* 对数阶(递归实现) */
|
||||
int logRecur(float n) {
|
||||
if (n <= 1) return 0;
|
||||
if (n <= 1)
|
||||
return 0;
|
||||
return logRecur(n / 2) + 1;
|
||||
}
|
||||
```
|
||||
@ -2106,9 +2110,10 @@ $$
|
||||
```java title="time_complexity.java"
|
||||
/* 线性对数阶 */
|
||||
int linearLogRecur(float n) {
|
||||
if (n <= 1) return 1;
|
||||
int count = linearLogRecur(n / 2) +
|
||||
linearLogRecur(n / 2);
|
||||
if (n <= 1)
|
||||
return 1;
|
||||
int count = linearLogRecur(n / 2) +
|
||||
linearLogRecur(n / 2);
|
||||
for (int i = 0; i < n; i++) {
|
||||
count++;
|
||||
}
|
||||
@ -2121,9 +2126,9 @@ $$
|
||||
```cpp title="time_complexity.cpp"
|
||||
/* 线性对数阶 */
|
||||
int linearLogRecur(float n) {
|
||||
if (n <= 1) return 1;
|
||||
int count = linearLogRecur(n / 2) +
|
||||
linearLogRecur(n / 2);
|
||||
if (n <= 1)
|
||||
return 1;
|
||||
int count = linearLogRecur(n / 2) + linearLogRecur(n / 2);
|
||||
for (int i = 0; i < n; i++) {
|
||||
count++;
|
||||
}
|
||||
@ -2263,7 +2268,8 @@ $$
|
||||
```java title="time_complexity.java"
|
||||
/* 阶乘阶(递归实现) */
|
||||
int factorialRecur(int n) {
|
||||
if (n == 0) return 1;
|
||||
if (n == 0)
|
||||
return 1;
|
||||
int count = 0;
|
||||
// 从 1 个分裂出 n 个
|
||||
for (int i = 0; i < n; i++) {
|
||||
@ -2278,7 +2284,8 @@ $$
|
||||
```cpp title="time_complexity.cpp"
|
||||
/* 阶乘阶(递归实现) */
|
||||
int factorialRecur(int n) {
|
||||
if (n == 0) return 1;
|
||||
if (n == 0)
|
||||
return 1;
|
||||
int count = 0;
|
||||
// 从 1 个分裂出 n 个
|
||||
for (int i = 0; i < n; i++) {
|
||||
@ -2468,7 +2475,7 @@ $$
|
||||
}
|
||||
|
||||
/* 查找数组 nums 中数字 1 所在索引 */
|
||||
int findOne(vector<int>& nums) {
|
||||
int findOne(vector<int> &nums) {
|
||||
for (int i = 0; i < nums.size(); i++) {
|
||||
// 当元素 1 在数组头部时,达到最佳时间复杂度 O(1)
|
||||
// 当元素 1 在数组尾部时,达到最差时间复杂度 O(n)
|
||||
|
||||
@ -37,7 +37,7 @@ comments: true
|
||||
```java title="graph_adjacency_matrix.java"
|
||||
/* 基于邻接矩阵实现的无向图类 */
|
||||
class GraphAdjMat {
|
||||
List<Integer> vertices; // 顶点列表,元素代表“顶点值”,索引代表“顶点索引”
|
||||
List<Integer> vertices; // 顶点列表,元素代表“顶点值”,索引代表“顶点索引”
|
||||
List<List<Integer>> adjMat; // 邻接矩阵,行列索引对应“顶点索引”
|
||||
|
||||
/* 构造方法 */
|
||||
@ -130,16 +130,16 @@ comments: true
|
||||
vector<int> vertices; // 顶点列表,元素代表“顶点值”,索引代表“顶点索引”
|
||||
vector<vector<int>> adjMat; // 邻接矩阵,行列索引对应“顶点索引”
|
||||
|
||||
public:
|
||||
public:
|
||||
/* 构造方法 */
|
||||
GraphAdjMat(const vector<int>& vertices, const vector<vector<int>>& edges) {
|
||||
GraphAdjMat(const vector<int> &vertices, const vector<vector<int>> &edges) {
|
||||
// 添加顶点
|
||||
for (int val : vertices) {
|
||||
addVertex(val);
|
||||
}
|
||||
// 添加边
|
||||
// 请注意,edges 元素代表顶点索引,即对应 vertices 元素索引
|
||||
for (const vector<int>& edge : edges) {
|
||||
for (const vector<int> &edge : edges) {
|
||||
addEdge(edge[0], edge[1]);
|
||||
}
|
||||
}
|
||||
@ -157,7 +157,7 @@ comments: true
|
||||
// 在邻接矩阵中添加一行
|
||||
adjMat.emplace_back(n, 0);
|
||||
// 在邻接矩阵中添加一列
|
||||
for (vector<int>& row : adjMat) {
|
||||
for (vector<int> &row : adjMat) {
|
||||
row.push_back(0);
|
||||
}
|
||||
}
|
||||
@ -172,7 +172,7 @@ comments: true
|
||||
// 在邻接矩阵中删除索引 index 的行
|
||||
adjMat.erase(adjMat.begin() + index);
|
||||
// 在邻接矩阵中删除索引 index 的列
|
||||
for (vector<int>& row : adjMat) {
|
||||
for (vector<int> &row : adjMat) {
|
||||
row.erase(row.begin() + index);
|
||||
}
|
||||
}
|
||||
@ -203,9 +203,9 @@ comments: true
|
||||
/* 打印邻接矩阵 */
|
||||
void print() {
|
||||
cout << "顶点列表 = ";
|
||||
PrintUtil::printVector(vertices);
|
||||
printVector(vertices);
|
||||
cout << "邻接矩阵 =" << endl;
|
||||
PrintUtil::printVectorMatrix(adjMat);
|
||||
printVectorMatrix(adjMat);
|
||||
}
|
||||
};
|
||||
```
|
||||
@ -884,7 +884,86 @@ comments: true
|
||||
=== "C++"
|
||||
|
||||
```cpp title="graph_adjacency_list.cpp"
|
||||
[class]{GraphAdjList}-[func]{}
|
||||
/* 基于邻接表实现的无向图类 */
|
||||
class GraphAdjList {
|
||||
public:
|
||||
// 邻接表,key: 顶点,value:该顶点的所有邻接顶点
|
||||
unordered_map<Vertex *, vector<Vertex *>> adjList;
|
||||
|
||||
/* 在 vector 中删除指定节点 */
|
||||
void remove(vector<Vertex *> &vec, Vertex *vet) {
|
||||
for (int i = 0; i < vec.size(); i++) {
|
||||
if (vec[i] == vet) {
|
||||
vec.erase(vec.begin() + i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* 构造方法 */
|
||||
GraphAdjList(const vector<vector<Vertex *>> &edges) {
|
||||
// 添加所有顶点和边
|
||||
for (const vector<Vertex *> &edge : edges) {
|
||||
addVertex(edge[0]);
|
||||
addVertex(edge[1]);
|
||||
addEdge(edge[0], edge[1]);
|
||||
}
|
||||
}
|
||||
|
||||
/* 获取顶点数量 */
|
||||
int size() {
|
||||
return adjList.size();
|
||||
}
|
||||
|
||||
/* 添加边 */
|
||||
void addEdge(Vertex *vet1, Vertex *vet2) {
|
||||
if (!adjList.count(vet1) || !adjList.count(vet2) || vet1 == vet2)
|
||||
throw invalid_argument("不存在顶点");
|
||||
// 添加边 vet1 - vet2
|
||||
adjList[vet1].push_back(vet2);
|
||||
adjList[vet2].push_back(vet1);
|
||||
}
|
||||
|
||||
/* 删除边 */
|
||||
void removeEdge(Vertex *vet1, Vertex *vet2) {
|
||||
if (!adjList.count(vet1) || !adjList.count(vet2) || vet1 == vet2)
|
||||
throw invalid_argument("不存在顶点");
|
||||
// 删除边 vet1 - vet2
|
||||
remove(adjList[vet1], vet2);
|
||||
remove(adjList[vet2], vet1);
|
||||
}
|
||||
|
||||
/* 添加顶点 */
|
||||
void addVertex(Vertex *vet) {
|
||||
if (adjList.count(vet))
|
||||
return;
|
||||
// 在邻接表中添加一个新链表
|
||||
adjList[vet] = vector<Vertex *>();
|
||||
}
|
||||
|
||||
/* 删除顶点 */
|
||||
void removeVertex(Vertex *vet) {
|
||||
if (!adjList.count(vet))
|
||||
throw invalid_argument("不存在顶点");
|
||||
// 在邻接表中删除顶点 vet 对应的链表
|
||||
adjList.erase(vet);
|
||||
// 遍历其他顶点的链表,删除所有包含 vet 的边
|
||||
for (auto &adj : adjList) {
|
||||
remove(adj.second, vet);
|
||||
}
|
||||
}
|
||||
|
||||
/* 打印邻接表 */
|
||||
void print() {
|
||||
cout << "邻接表 =" << endl;
|
||||
for (auto &adj : adjList) {
|
||||
const auto &key = adj.first;
|
||||
const auto &vec = adj.second;
|
||||
cout << key->val << ": ";
|
||||
printVector(vetsToVals(vec));
|
||||
}
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
|
||||
@ -64,13 +64,13 @@ BFS 通常借助「队列」来实现。队列具有“先入先出”的性质
|
||||
```cpp title="graph_bfs.cpp"
|
||||
/* 广度优先遍历 BFS */
|
||||
// 使用邻接表来表示图,以便获取指定顶点的所有邻接顶点
|
||||
vector<Vertex*> graphBFS(GraphAdjList &graph, Vertex *startVet) {
|
||||
vector<Vertex *> graphBFS(GraphAdjList &graph, Vertex *startVet) {
|
||||
// 顶点遍历序列
|
||||
vector<Vertex*> res;
|
||||
vector<Vertex *> res;
|
||||
// 哈希表,用于记录已被访问过的顶点
|
||||
unordered_set<Vertex*> visited = { startVet };
|
||||
unordered_set<Vertex *> visited = {startVet};
|
||||
// 队列用于实现 BFS
|
||||
queue<Vertex*> que;
|
||||
queue<Vertex *> que;
|
||||
que.push(startVet);
|
||||
// 以顶点 vet 为起点,循环直至访问完所有顶点
|
||||
while (!que.empty()) {
|
||||
@ -80,8 +80,8 @@ BFS 通常借助「队列」来实现。队列具有“先入先出”的性质
|
||||
// 遍历该顶点的所有邻接顶点
|
||||
for (auto adjVet : graph.adjList[vet]) {
|
||||
if (visited.count(adjVet))
|
||||
continue; // 跳过已被访问过的顶点
|
||||
que.push(adjVet); // 只入队未访问的顶点
|
||||
continue; // 跳过已被访问过的顶点
|
||||
que.push(adjVet); // 只入队未访问的顶点
|
||||
visited.emplace(adjVet); // 标记该顶点已被访问
|
||||
}
|
||||
}
|
||||
@ -351,13 +351,13 @@ BFS 通常借助「队列」来实现。队列具有“先入先出”的性质
|
||||
|
||||
```cpp title="graph_dfs.cpp"
|
||||
/* 深度优先遍历 DFS 辅助函数 */
|
||||
void dfs(GraphAdjList& graph, unordered_set<Vertex*>& visited, vector<Vertex*>& res, Vertex* vet) {
|
||||
void dfs(GraphAdjList &graph, unordered_set<Vertex *> &visited, vector<Vertex *> &res, Vertex *vet) {
|
||||
res.push_back(vet); // 记录访问顶点
|
||||
visited.emplace(vet); // 标记该顶点已被访问
|
||||
// 遍历该顶点的所有邻接顶点
|
||||
for (Vertex* adjVet : graph.adjList[vet]) {
|
||||
for (Vertex *adjVet : graph.adjList[vet]) {
|
||||
if (visited.count(adjVet))
|
||||
continue; // 跳过已被访问过的顶点
|
||||
continue; // 跳过已被访问过的顶点
|
||||
// 递归访问邻接顶点
|
||||
dfs(graph, visited, res, adjVet);
|
||||
}
|
||||
@ -365,11 +365,11 @@ BFS 通常借助「队列」来实现。队列具有“先入先出”的性质
|
||||
|
||||
/* 深度优先遍历 DFS */
|
||||
// 使用邻接表来表示图,以便获取指定顶点的所有邻接顶点
|
||||
vector<Vertex*> graphDFS(GraphAdjList& graph, Vertex* startVet) {
|
||||
vector<Vertex *> graphDFS(GraphAdjList &graph, Vertex *startVet) {
|
||||
// 顶点遍历序列
|
||||
vector<Vertex*> res;
|
||||
vector<Vertex *> res;
|
||||
// 哈希表,用于记录已被访问过的顶点
|
||||
unordered_set<Vertex*> visited;
|
||||
unordered_set<Vertex *> visited;
|
||||
dfs(graph, visited, res, startVet);
|
||||
return res;
|
||||
}
|
||||
|
||||
@ -424,6 +424,7 @@ $$
|
||||
class Entry {
|
||||
public int key;
|
||||
public String val;
|
||||
|
||||
public Entry(int key, String val) {
|
||||
this.key = key;
|
||||
this.val = val;
|
||||
@ -433,6 +434,7 @@ $$
|
||||
/* 基于数组简易实现的哈希表 */
|
||||
class ArrayHashMap {
|
||||
private List<Entry> buckets;
|
||||
|
||||
public ArrayHashMap() {
|
||||
// 初始化数组,包含 100 个桶
|
||||
buckets = new ArrayList<>();
|
||||
@ -451,7 +453,8 @@ $$
|
||||
public String get(int key) {
|
||||
int index = hashFunc(key);
|
||||
Entry pair = buckets.get(index);
|
||||
if (pair == null) return null;
|
||||
if (pair == null)
|
||||
return null;
|
||||
return pair.val;
|
||||
}
|
||||
|
||||
@ -501,7 +504,7 @@ $$
|
||||
|
||||
/* 打印哈希表 */
|
||||
public void print() {
|
||||
for (Entry kv: entrySet()) {
|
||||
for (Entry kv : entrySet()) {
|
||||
System.out.println(kv.key + " -> " + kv.val);
|
||||
}
|
||||
}
|
||||
@ -513,7 +516,7 @@ $$
|
||||
```cpp title="array_hash_map.cpp"
|
||||
/* 键值对 int->String */
|
||||
struct Entry {
|
||||
public:
|
||||
public:
|
||||
int key;
|
||||
string val;
|
||||
Entry(int key, string val) {
|
||||
@ -524,12 +527,13 @@ $$
|
||||
|
||||
/* 基于数组简易实现的哈希表 */
|
||||
class ArrayHashMap {
|
||||
private:
|
||||
vector<Entry*> buckets;
|
||||
public:
|
||||
private:
|
||||
vector<Entry *> buckets;
|
||||
|
||||
public:
|
||||
ArrayHashMap() {
|
||||
// 初始化数组,包含 100 个桶
|
||||
buckets = vector<Entry*>(100);
|
||||
buckets = vector<Entry *>(100);
|
||||
}
|
||||
|
||||
/* 哈希函数 */
|
||||
@ -541,7 +545,7 @@ $$
|
||||
/* 查询操作 */
|
||||
string get(int key) {
|
||||
int index = hashFunc(key);
|
||||
Entry* pair = buckets[index];
|
||||
Entry *pair = buckets[index];
|
||||
if (pair == nullptr)
|
||||
return nullptr;
|
||||
return pair->val;
|
||||
@ -549,7 +553,7 @@ $$
|
||||
|
||||
/* 添加操作 */
|
||||
void put(int key, string val) {
|
||||
Entry* pair = new Entry(key, val);
|
||||
Entry *pair = new Entry(key, val);
|
||||
int index = hashFunc(key);
|
||||
buckets[index] = pair;
|
||||
}
|
||||
@ -562,9 +566,9 @@ $$
|
||||
}
|
||||
|
||||
/* 获取所有键值对 */
|
||||
vector<Entry*> entrySet() {
|
||||
vector<Entry*> entrySet;
|
||||
for (Entry* pair: buckets) {
|
||||
vector<Entry *> entrySet() {
|
||||
vector<Entry *> entrySet;
|
||||
for (Entry *pair : buckets) {
|
||||
if (pair != nullptr) {
|
||||
entrySet.push_back(pair);
|
||||
}
|
||||
@ -575,7 +579,7 @@ $$
|
||||
/* 获取所有键 */
|
||||
vector<int> keySet() {
|
||||
vector<int> keySet;
|
||||
for (Entry* pair: buckets) {
|
||||
for (Entry *pair : buckets) {
|
||||
if (pair != nullptr) {
|
||||
keySet.push_back(pair->key);
|
||||
}
|
||||
@ -586,8 +590,8 @@ $$
|
||||
/* 获取所有值 */
|
||||
vector<string> valueSet() {
|
||||
vector<string> valueSet;
|
||||
for (Entry* pair: buckets) {
|
||||
if (pair != nullptr){
|
||||
for (Entry *pair : buckets) {
|
||||
if (pair != nullptr) {
|
||||
valueSet.push_back(pair->val);
|
||||
}
|
||||
}
|
||||
@ -596,7 +600,7 @@ $$
|
||||
|
||||
/* 打印哈希表 */
|
||||
void print() {
|
||||
for (Entry* kv: entrySet()) {
|
||||
for (Entry *kv : entrySet()) {
|
||||
cout << kv->key << " -> " << kv->val << endl;
|
||||
}
|
||||
}
|
||||
|
||||
@ -355,7 +355,7 @@ comments: true
|
||||
/* 获取右子节点索引 */
|
||||
int right(int i) {
|
||||
return 2 * i + 2;
|
||||
}
|
||||
}
|
||||
|
||||
/* 获取父节点索引 */
|
||||
int parent(int i) {
|
||||
@ -667,7 +667,7 @@ comments: true
|
||||
void siftUp(int i) {
|
||||
while (true) {
|
||||
// 获取节点 i 的父节点
|
||||
int p = parent(i);
|
||||
int p = parent(i);
|
||||
// 当“越过根节点”或“节点无需修复”时,结束堆化
|
||||
if (p < 0 || maxHeap[i] <= maxHeap[p])
|
||||
break;
|
||||
@ -947,7 +947,8 @@ comments: true
|
||||
if (r < size() && maxHeap.get(r) > maxHeap.get(ma))
|
||||
ma = r;
|
||||
// 若节点 i 最大或索引 l, r 越界,则无需继续堆化,跳出
|
||||
if (ma == i) break;
|
||||
if (ma == i)
|
||||
break;
|
||||
// 交换两节点
|
||||
swap(i, ma);
|
||||
// 循环向下堆化
|
||||
@ -979,12 +980,12 @@ comments: true
|
||||
// 判断节点 i, l, r 中值最大的节点,记为 ma
|
||||
int l = left(i), r = right(i), ma = i;
|
||||
// 若节点 i 最大或索引 l, r 越界,则无需继续堆化,跳出
|
||||
if (l < size() && maxHeap[l] > maxHeap[ma])
|
||||
if (l < size() && maxHeap[l] > maxHeap[ma])
|
||||
ma = l;
|
||||
if (r < size() && maxHeap[r] > maxHeap[ma])
|
||||
ma = r;
|
||||
// 若节点 i 最大或索引 l, r 越界,则无需继续堆化,跳出
|
||||
if (ma == i)
|
||||
if (ma == i)
|
||||
break;
|
||||
swap(maxHeap[i], maxHeap[ma]);
|
||||
// 循环向下堆化
|
||||
|
||||
@ -72,17 +72,17 @@ $$
|
||||
|
||||
```cpp title="binary_search.cpp"
|
||||
/* 二分查找(双闭区间) */
|
||||
int binarySearch(vector<int>& nums, int target) {
|
||||
int binarySearch(vector<int> &nums, int target) {
|
||||
// 初始化双闭区间 [0, n-1] ,即 i, j 分别指向数组首元素、尾元素
|
||||
int i = 0, j = nums.size() - 1;
|
||||
// 循环,当搜索区间为空时跳出(当 i > j 时为空)
|
||||
while (i <= j) {
|
||||
int m = (i + j) / 2; // 计算中点索引 m
|
||||
if (nums[m] < target) // 此情况说明 target 在区间 [m+1, j] 中
|
||||
int m = (i + j) / 2; // 计算中点索引 m
|
||||
if (nums[m] < target) // 此情况说明 target 在区间 [m+1, j] 中
|
||||
i = m + 1;
|
||||
else if (nums[m] > target) // 此情况说明 target 在区间 [i, m-1] 中
|
||||
j = m - 1;
|
||||
else // 找到目标元素,返回其索引
|
||||
else // 找到目标元素,返回其索引
|
||||
return m;
|
||||
}
|
||||
// 未找到目标元素,返回 -1
|
||||
@ -283,17 +283,17 @@ $$
|
||||
|
||||
```cpp title="binary_search.cpp"
|
||||
/* 二分查找(左闭右开) */
|
||||
int binarySearch1(vector<int>& nums, int target) {
|
||||
int binarySearch1(vector<int> &nums, int target) {
|
||||
// 初始化左闭右开 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1
|
||||
int i = 0, j = nums.size();
|
||||
// 循环,当搜索区间为空时跳出(当 i = j 时为空)
|
||||
while (i < j) {
|
||||
int m = (i + j) / 2; // 计算中点索引 m
|
||||
if (nums[m] < target) // 此情况说明 target 在区间 [m+1, j) 中
|
||||
int m = (i + j) / 2; // 计算中点索引 m
|
||||
if (nums[m] < target) // 此情况说明 target 在区间 [m+1, j) 中
|
||||
i = m + 1;
|
||||
else if (nums[m] > target) // 此情况说明 target 在区间 [i, m) 中
|
||||
j = m;
|
||||
else // 找到目标元素,返回其索引
|
||||
else // 找到目标元素,返回其索引
|
||||
return m;
|
||||
}
|
||||
// 未找到目标元素,返回 -1
|
||||
|
||||
@ -149,7 +149,7 @@ comments: true
|
||||
|
||||
```cpp title="hashing_search.cpp"
|
||||
/* 哈希查找(链表) */
|
||||
ListNode* hashingSearchLinkedList(unordered_map<int, ListNode*> map, int target) {
|
||||
ListNode *hashingSearchLinkedList(unordered_map<int, ListNode *> map, int target) {
|
||||
// 哈希表的 key: 目标节点值,value: 节点对象
|
||||
// 若哈希表中无此 key ,返回 nullptr
|
||||
if (map.find(target) == map.end())
|
||||
|
||||
@ -34,7 +34,7 @@ comments: true
|
||||
|
||||
```cpp title="linear_search.cpp"
|
||||
/* 线性查找(数组) */
|
||||
int linearSearchArray(vector<int>& nums, int target) {
|
||||
int linearSearchArray(vector<int> &nums, int target) {
|
||||
// 遍历数组
|
||||
for (int i = 0; i < nums.size(); i++) {
|
||||
// 找到目标元素,返回其索引
|
||||
@ -190,7 +190,7 @@ comments: true
|
||||
|
||||
```cpp title="linear_search.cpp"
|
||||
/* 线性查找(链表) */
|
||||
ListNode* linearSearchLinkedList(ListNode* head, int target) {
|
||||
ListNode *linearSearchLinkedList(ListNode *head, int target) {
|
||||
// 遍历链表
|
||||
while (head != nullptr) {
|
||||
// 找到目标节点,返回之
|
||||
|
||||
@ -67,7 +67,7 @@ comments: true
|
||||
|
||||
```cpp title="bubble_sort.cpp"
|
||||
/* 冒泡排序 */
|
||||
void bubbleSort(vector<int>& nums) {
|
||||
void bubbleSort(vector<int> &nums) {
|
||||
// 外循环:待排序元素数量为 n-1, n-2, ..., 1
|
||||
for (int i = nums.size() - 1; i > 0; i--) {
|
||||
// 内循环:冒泡操作
|
||||
@ -244,7 +244,7 @@ comments: true
|
||||
=== "Java"
|
||||
|
||||
```java title="bubble_sort.java"
|
||||
/* 冒泡排序(标志优化)*/
|
||||
/* 冒泡排序(标志优化) */
|
||||
void bubbleSortWithFlag(int[] nums) {
|
||||
// 外循环:待排序元素数量为 n-1, n-2, ..., 1
|
||||
for (int i = nums.length - 1; i > 0; i--) {
|
||||
@ -256,10 +256,11 @@ comments: true
|
||||
int tmp = nums[j];
|
||||
nums[j] = nums[j + 1];
|
||||
nums[j + 1] = tmp;
|
||||
flag = true; // 记录交换元素
|
||||
flag = true; // 记录交换元素
|
||||
}
|
||||
}
|
||||
if (!flag) break; // 此轮冒泡未交换任何元素,直接跳出
|
||||
if (!flag)
|
||||
break; // 此轮冒泡未交换任何元素,直接跳出
|
||||
}
|
||||
}
|
||||
```
|
||||
@ -268,7 +269,7 @@ comments: true
|
||||
|
||||
```cpp title="bubble_sort.cpp"
|
||||
/* 冒泡排序(标志优化)*/
|
||||
void bubbleSortWithFlag(vector<int>& nums) {
|
||||
void bubbleSortWithFlag(vector<int> &nums) {
|
||||
// 外循环:待排序元素数量为 n-1, n-2, ..., 1
|
||||
for (int i = nums.size() - 1; i > 0; i--) {
|
||||
bool flag = false; // 初始化标志位
|
||||
@ -278,10 +279,11 @@ comments: true
|
||||
// 交换 nums[j] 与 nums[j + 1]
|
||||
// 这里使用了 std::swap() 函数
|
||||
swap(nums[j], nums[j + 1]);
|
||||
flag = true; // 记录交换元素
|
||||
flag = true; // 记录交换元素
|
||||
}
|
||||
}
|
||||
if (!flag) break; // 此轮冒泡未交换任何元素,直接跳出
|
||||
if (!flag)
|
||||
break; // 此轮冒泡未交换任何元素,直接跳出
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@ -56,7 +56,31 @@ comments: true
|
||||
=== "C++"
|
||||
|
||||
```cpp title="bucket_sort.cpp"
|
||||
[class]{}-[func]{bucketSort}
|
||||
/* 桶排序 */
|
||||
void bucketSort(vector<float> &nums) {
|
||||
// 初始化 k = n/2 个桶,预期向每个桶分配 2 个元素
|
||||
int k = nums.size() / 2;
|
||||
vector<vector<float>> buckets(k);
|
||||
// 1. 将数组元素分配到各个桶中
|
||||
for (float num : nums) {
|
||||
// 输入数据范围 [0, 1),使用 num * k 映射到索引范围 [0, k-1]
|
||||
int i = num * k;
|
||||
// 将 num 添加进桶 bucket_idx
|
||||
buckets[i].push_back(num);
|
||||
}
|
||||
// 2. 对各个桶执行排序
|
||||
for (vector<float> &bucket : buckets) {
|
||||
// 使用内置排序函数,也可以替换成其他排序算法
|
||||
sort(bucket.begin(), bucket.end());
|
||||
}
|
||||
// 3. 遍历桶合并结果
|
||||
int i = 0;
|
||||
for (vector<float> &bucket : buckets) {
|
||||
for (float num : bucket) {
|
||||
nums[i++] = num;
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
|
||||
@ -50,7 +50,7 @@ comments: true
|
||||
```cpp title="counting_sort.cpp"
|
||||
/* 计数排序 */
|
||||
// 简单实现,无法用于排序对象
|
||||
void countingSortNaive(vector<int>& nums) {
|
||||
void countingSortNaive(vector<int> &nums) {
|
||||
// 1. 统计数组最大元素 m
|
||||
int m = 0;
|
||||
for (int num : nums) {
|
||||
@ -311,7 +311,7 @@ $$
|
||||
```cpp title="counting_sort.cpp"
|
||||
/* 计数排序 */
|
||||
// 完整实现,可排序对象,并且是稳定排序
|
||||
void countingSort(vector<int>& nums) {
|
||||
void countingSort(vector<int> &nums) {
|
||||
// 1. 统计数组最大元素 m
|
||||
int m = 0;
|
||||
for (int num : nums) {
|
||||
@ -335,7 +335,7 @@ $$
|
||||
for (int i = n - 1; i >= 0; i--) {
|
||||
int num = nums[i];
|
||||
res[counter[num] - 1] = num; // 将 num 放置到对应索引处
|
||||
counter[num]--; // 令前缀和自减 1 ,得到下次放置 num 的索引
|
||||
counter[num]--; // 令前缀和自减 1 ,得到下次放置 num 的索引
|
||||
}
|
||||
// 使用结果数组 res 覆盖原数组 nums
|
||||
nums = res;
|
||||
|
||||
@ -34,10 +34,10 @@ comments: true
|
||||
int base = nums[i], j = i - 1;
|
||||
// 内循环:将 base 插入到左边的正确位置
|
||||
while (j >= 0 && nums[j] > base) {
|
||||
nums[j + 1] = nums[j]; // 1. 将 nums[j] 向右移动一位
|
||||
nums[j + 1] = nums[j]; // 1. 将 nums[j] 向右移动一位
|
||||
j--;
|
||||
}
|
||||
nums[j + 1] = base; // 2. 将 base 赋值到正确位置
|
||||
nums[j + 1] = base; // 2. 将 base 赋值到正确位置
|
||||
}
|
||||
}
|
||||
```
|
||||
@ -46,16 +46,16 @@ comments: true
|
||||
|
||||
```cpp title="insertion_sort.cpp"
|
||||
/* 插入排序 */
|
||||
void insertionSort(vector<int>& nums) {
|
||||
void insertionSort(vector<int> &nums) {
|
||||
// 外循环:base = nums[1], nums[2], ..., nums[n-1]
|
||||
for (int i = 1; i < nums.size(); i++) {
|
||||
int base = nums[i], j = i - 1;
|
||||
// 内循环:将 base 插入到左边的正确位置
|
||||
while (j >= 0 && nums[j] > base) {
|
||||
nums[j + 1] = nums[j]; // 1. 将 nums[j] 向右移动一位
|
||||
nums[j + 1] = nums[j]; // 1. 将 nums[j] 向右移动一位
|
||||
j--;
|
||||
}
|
||||
nums[j + 1] = base; // 2. 将 base 赋值到正确位置
|
||||
nums[j + 1] = base; // 2. 将 base 赋值到正确位置
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@ -65,13 +65,13 @@ comments: true
|
||||
// 右子数组区间 [mid + 1, right]
|
||||
void merge(int[] nums, int left, int mid, int right) {
|
||||
// 初始化辅助数组
|
||||
int[] tmp = Arrays.copyOfRange(nums, left, right + 1);
|
||||
// 左子数组的起始索引和结束索引
|
||||
int[] tmp = Arrays.copyOfRange(nums, left, right + 1);
|
||||
// 左子数组的起始索引和结束索引
|
||||
int leftStart = left - left, leftEnd = mid - left;
|
||||
// 右子数组的起始索引和结束索引
|
||||
// 右子数组的起始索引和结束索引
|
||||
int rightStart = mid + 1 - left, rightEnd = right - left;
|
||||
// i, j 分别指向左子数组、右子数组的首元素
|
||||
int i = leftStart, j = rightStart;
|
||||
int i = leftStart, j = rightStart;
|
||||
// 通过覆盖原数组 nums 来合并左子数组和右子数组
|
||||
for (int k = left; k <= right; k++) {
|
||||
// 若“左子数组已全部合并完”,则选取右子数组元素,并且 j++
|
||||
@ -89,7 +89,8 @@ comments: true
|
||||
/* 归并排序 */
|
||||
void mergeSort(int[] nums, int left, int right) {
|
||||
// 终止条件
|
||||
if (left >= right) return; // 当子数组长度为 1 时终止递归
|
||||
if (left >= right)
|
||||
return; // 当子数组长度为 1 时终止递归
|
||||
// 划分阶段
|
||||
int mid = (left + right) / 2; // 计算中点
|
||||
mergeSort(nums, left, mid); // 递归左子数组
|
||||
@ -105,15 +106,15 @@ comments: true
|
||||
/* 合并左子数组和右子数组 */
|
||||
// 左子数组区间 [left, mid]
|
||||
// 右子数组区间 [mid + 1, right]
|
||||
void merge(vector<int>& nums, int left, int mid, int right) {
|
||||
void merge(vector<int> &nums, int left, int mid, int right) {
|
||||
// 初始化辅助数组
|
||||
vector<int> tmp(nums.begin() + left, nums.begin() + right + 1);
|
||||
// 左子数组的起始索引和结束索引
|
||||
vector<int> tmp(nums.begin() + left, nums.begin() + right + 1);
|
||||
// 左子数组的起始索引和结束索引
|
||||
int leftStart = left - left, leftEnd = mid - left;
|
||||
// 右子数组的起始索引和结束索引
|
||||
// 右子数组的起始索引和结束索引
|
||||
int rightStart = mid + 1 - left, rightEnd = right - left;
|
||||
// i, j 分别指向左子数组、右子数组的首元素
|
||||
int i = leftStart, j = rightStart;
|
||||
int i = leftStart, j = rightStart;
|
||||
// 通过覆盖原数组 nums 来合并左子数组和右子数组
|
||||
for (int k = left; k <= right; k++) {
|
||||
// 若“左子数组已全部合并完”,则选取右子数组元素,并且 j++
|
||||
@ -129,9 +130,10 @@ comments: true
|
||||
}
|
||||
|
||||
/* 归并排序 */
|
||||
void mergeSort(vector<int>& nums, int left, int right) {
|
||||
void mergeSort(vector<int> &nums, int left, int right) {
|
||||
// 终止条件
|
||||
if (left >= right) return; // 当子数组长度为 1 时终止递归
|
||||
if (left >= right)
|
||||
return; // 当子数组长度为 1 时终止递归
|
||||
// 划分阶段
|
||||
int mid = (left + right) / 2; // 计算中点
|
||||
mergeSort(nums, left, mid); // 递归左子数组
|
||||
|
||||
@ -75,25 +75,25 @@ comments: true
|
||||
|
||||
```cpp title="quick_sort.cpp"
|
||||
/* 元素交换 */
|
||||
void swap(vector<int>& nums, int i, int j) {
|
||||
void swap(vector<int> &nums, int i, int j) {
|
||||
int tmp = nums[i];
|
||||
nums[i] = nums[j];
|
||||
nums[j] = tmp;
|
||||
}
|
||||
|
||||
/* 哨兵划分 */
|
||||
int partition(vector<int>& nums, int left, int right) {
|
||||
int partition(vector<int> &nums, int left, int right) {
|
||||
// 以 nums[left] 作为基准数
|
||||
int i = left, j = right;
|
||||
while (i < j) {
|
||||
while (i < j && nums[j] >= nums[left])
|
||||
j--; // 从右向左找首个小于基准数的元素
|
||||
j--; // 从右向左找首个小于基准数的元素
|
||||
while (i < j && nums[i] <= nums[left])
|
||||
i++; // 从左向右找首个大于基准数的元素
|
||||
swap(nums, i, j); // 交换这两个元素
|
||||
}
|
||||
swap(nums, i, left); // 将基准数交换至两子数组的分界线
|
||||
return i; // 返回基准数的索引
|
||||
swap(nums, i, left); // 将基准数交换至两子数组的分界线
|
||||
return i; // 返回基准数的索引
|
||||
}
|
||||
```
|
||||
|
||||
@ -316,7 +316,7 @@ comments: true
|
||||
|
||||
```cpp title="quick_sort.cpp"
|
||||
/* 快速排序 */
|
||||
void quickSort(vector<int>& nums, int left, int right) {
|
||||
void quickSort(vector<int> &nums, int left, int right) {
|
||||
// 子数组长度为 1 时终止递归
|
||||
if (left >= right)
|
||||
return;
|
||||
@ -514,7 +514,7 @@ comments: true
|
||||
|
||||
```cpp title="quick_sort.cpp"
|
||||
/* 选取三个元素的中位数 */
|
||||
int medianThree(vector<int>& nums, int left, int mid, int right) {
|
||||
int medianThree(vector<int> &nums, int left, int mid, int right) {
|
||||
// 此处使用异或运算来简化代码
|
||||
// 异或规则为 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1
|
||||
if ((nums[left] < nums[mid]) ^ (nums[left] < nums[right]))
|
||||
@ -526,7 +526,7 @@ comments: true
|
||||
}
|
||||
|
||||
/* 哨兵划分(三数取中值) */
|
||||
int partition(vector<int>& nums, int left, int right) {
|
||||
int partition(vector<int> &nums, int left, int right) {
|
||||
// 选取三个候选元素的中位数
|
||||
int med = medianThree(nums, left, (left + right) / 2, right);
|
||||
// 将中位数交换至数组最左端
|
||||
@ -535,13 +535,13 @@ comments: true
|
||||
int i = left, j = right;
|
||||
while (i < j) {
|
||||
while (i < j && nums[j] >= nums[left])
|
||||
j--; // 从右向左找首个小于基准数的元素
|
||||
j--; // 从右向左找首个小于基准数的元素
|
||||
while (i < j && nums[i] <= nums[left])
|
||||
i++; // 从左向右找首个大于基准数的元素
|
||||
swap(nums, i, j); // 交换这两个元素
|
||||
}
|
||||
swap(nums, i, left); // 将基准数交换至两子数组的分界线
|
||||
return i; // 返回基准数的索引
|
||||
swap(nums, i, left); // 将基准数交换至两子数组的分界线
|
||||
return i; // 返回基准数的索引
|
||||
}
|
||||
```
|
||||
|
||||
@ -807,8 +807,8 @@ comments: true
|
||||
int pivot = partition(nums, left, right);
|
||||
// 对两个子数组中较短的那个执行快排
|
||||
if (pivot - left < right - pivot) {
|
||||
quickSort(nums, left, pivot - 1); // 递归排序左子数组
|
||||
left = pivot + 1; // 剩余待排序区间为 [pivot + 1, right]
|
||||
quickSort(nums, left, pivot - 1); // 递归排序左子数组
|
||||
left = pivot + 1; // 剩余待排序区间为 [pivot + 1, right]
|
||||
} else {
|
||||
quickSort(nums, pivot + 1, right); // 递归排序右子数组
|
||||
right = pivot - 1; // 剩余待排序区间为 [left, pivot - 1]
|
||||
@ -821,18 +821,18 @@ comments: true
|
||||
|
||||
```cpp title="quick_sort.cpp"
|
||||
/* 快速排序(尾递归优化) */
|
||||
void quickSort(vector<int>& nums, int left, int right) {
|
||||
void quickSort(vector<int> &nums, int left, int right) {
|
||||
// 子数组长度为 1 时终止
|
||||
while (left < right) {
|
||||
// 哨兵划分操作
|
||||
int pivot = partition(nums, left, right);
|
||||
// 对两个子数组中较短的那个执行快排
|
||||
if (pivot - left < right - pivot) {
|
||||
quickSort(nums, left, pivot - 1); // 递归排序左子数组
|
||||
left = pivot + 1; // 剩余待排序区间为 [pivot + 1, right]
|
||||
quickSort(nums, left, pivot - 1); // 递归排序左子数组
|
||||
left = pivot + 1; // 剩余待排序区间为 [pivot + 1, right]
|
||||
} else {
|
||||
quickSort(nums, pivot + 1, right); // 递归排序右子数组
|
||||
right = pivot - 1; // 剩余待排序区间为 [left, pivot - 1]
|
||||
right = pivot - 1; // 剩余待排序区间为 [left, pivot - 1]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -71,7 +71,8 @@ $$
|
||||
// 获取数组的最大元素,用于判断最大位数
|
||||
int m = Integer.MIN_VALUE;
|
||||
for (int num : nums)
|
||||
if (num > m) m = num;
|
||||
if (num > m)
|
||||
m = num;
|
||||
// 按照从低位到高位的顺序遍历
|
||||
for (int exp = 1; exp <= m; exp *= 10)
|
||||
// 对数组元素的第 k 位执行计数排序
|
||||
@ -92,7 +93,7 @@ $$
|
||||
}
|
||||
|
||||
/* 计数排序(根据 nums 第 k 位排序) */
|
||||
void countingSortDigit(vector<int>& nums, int exp) {
|
||||
void countingSortDigit(vector<int> &nums, int exp) {
|
||||
// 十进制的位范围为 0~9 ,因此需要长度为 10 的桶
|
||||
vector<int> counter(10, 0);
|
||||
int n = nums.size();
|
||||
@ -119,7 +120,7 @@ $$
|
||||
}
|
||||
|
||||
/* 基数排序 */
|
||||
void radixSort(vector<int>& nums) {
|
||||
void radixSort(vector<int> &nums) {
|
||||
// 获取数组的最大元素,用于判断最大位数
|
||||
int m = *max_element(nums.begin(), nums.end());
|
||||
// 按照从低位到高位的顺序遍历
|
||||
|
||||
@ -323,9 +323,10 @@ comments: true
|
||||
```java title="linkedlist_deque.java"
|
||||
/* 双向链表节点 */
|
||||
class ListNode {
|
||||
int val; // 节点值
|
||||
int val; // 节点值
|
||||
ListNode next; // 后继节点引用(指针)
|
||||
ListNode prev; // 前驱节点引用(指针)
|
||||
|
||||
ListNode(int val) {
|
||||
this.val = val;
|
||||
prev = next = null;
|
||||
@ -335,7 +336,7 @@ comments: true
|
||||
/* 基于双向链表实现的双向队列 */
|
||||
class LinkedListDeque {
|
||||
private ListNode front, rear; // 头节点 front ,尾节点 rear
|
||||
private int queSize = 0; // 双向队列的长度
|
||||
private int queSize = 0; // 双向队列的长度
|
||||
|
||||
public LinkedListDeque() {
|
||||
front = rear = null;
|
||||
@ -368,7 +369,7 @@ comments: true
|
||||
// 将 node 添加至链表尾部
|
||||
rear.next = node;
|
||||
node.prev = rear;
|
||||
rear = node; // 更新尾节点
|
||||
rear = node; // 更新尾节点
|
||||
}
|
||||
queSize++; // 更新队列长度
|
||||
}
|
||||
@ -398,17 +399,17 @@ comments: true
|
||||
fNext.prev = null;
|
||||
front.next = null;
|
||||
}
|
||||
front = fNext; // 更新头节点
|
||||
front = fNext; // 更新头节点
|
||||
// 队尾出队操作
|
||||
} else {
|
||||
val = rear.val; // 暂存尾节点值
|
||||
val = rear.val; // 暂存尾节点值
|
||||
// 删除尾节点
|
||||
ListNode rPrev = rear.prev;
|
||||
if (rPrev != null) {
|
||||
rPrev.next = null;
|
||||
rear.prev = null;
|
||||
}
|
||||
rear = rPrev; // 更新尾节点
|
||||
rear = rPrev; // 更新尾节点
|
||||
}
|
||||
queSize--; // 更新队列长度
|
||||
return val;
|
||||
@ -452,21 +453,23 @@ comments: true
|
||||
```cpp title="linkedlist_deque.cpp"
|
||||
/* 双向链表节点 */
|
||||
struct DoublyListNode {
|
||||
int val; // 节点值
|
||||
DoublyListNode *next; // 后继节点指针
|
||||
DoublyListNode *prev; // 前驱节点指针
|
||||
DoublyListNode(int val) : val(val), prev(nullptr), next(nullptr) {}
|
||||
int val; // 节点值
|
||||
DoublyListNode *next; // 后继节点指针
|
||||
DoublyListNode *prev; // 前驱节点指针
|
||||
DoublyListNode(int val) : val(val), prev(nullptr), next(nullptr) {
|
||||
}
|
||||
};
|
||||
|
||||
/* 基于双向链表实现的双向队列 */
|
||||
class LinkedListDeque {
|
||||
private:
|
||||
private:
|
||||
DoublyListNode *front, *rear; // 头节点 front ,尾节点 rear
|
||||
int queSize = 0; // 双向队列的长度
|
||||
|
||||
public:
|
||||
public:
|
||||
/* 构造方法 */
|
||||
LinkedListDeque() : front(nullptr), rear(nullptr) {}
|
||||
LinkedListDeque() : front(nullptr), rear(nullptr) {
|
||||
}
|
||||
|
||||
/* 析构方法 */
|
||||
~LinkedListDeque() {
|
||||
@ -506,7 +509,7 @@ comments: true
|
||||
// 将 node 添加至链表尾部
|
||||
rear->next = node;
|
||||
node->prev = rear;
|
||||
rear = node; // 更新尾节点
|
||||
rear = node; // 更新尾节点
|
||||
}
|
||||
queSize++; // 更新队列长度
|
||||
}
|
||||
@ -537,7 +540,7 @@ comments: true
|
||||
front->next = nullptr;
|
||||
delete front;
|
||||
}
|
||||
front = fNext; // 更新头节点
|
||||
front = fNext; // 更新头节点
|
||||
// 队尾出队操作
|
||||
} else {
|
||||
val = rear->val; // 暂存尾节点值
|
||||
@ -548,7 +551,7 @@ comments: true
|
||||
rear->prev = nullptr;
|
||||
delete rear;
|
||||
}
|
||||
rear = rPrev; // 更新尾节点
|
||||
rear = rPrev; // 更新尾节点
|
||||
}
|
||||
queSize--; // 更新队列长度
|
||||
return val;
|
||||
@ -1366,8 +1369,8 @@ comments: true
|
||||
```java title="array_deque.java"
|
||||
/* 基于环形数组实现的双向队列 */
|
||||
class ArrayDeque {
|
||||
private int[] nums; // 用于存储双向队列元素的数组
|
||||
private int front; // 队首指针,指向队首元素
|
||||
private int[] nums; // 用于存储双向队列元素的数组
|
||||
private int front; // 队首指针,指向队首元素
|
||||
private int queSize; // 双向队列长度
|
||||
|
||||
/* 构造方法 */
|
||||
@ -1475,12 +1478,12 @@ comments: true
|
||||
```cpp title="array_deque.cpp"
|
||||
/* 基于环形数组实现的双向队列 */
|
||||
class ArrayDeque {
|
||||
private:
|
||||
vector<int> nums; // 用于存储双向队列元素的数组
|
||||
int front; // 队首指针,指向队首元素
|
||||
int queSize; // 双向队列长度
|
||||
private:
|
||||
vector<int> nums; // 用于存储双向队列元素的数组
|
||||
int front; // 队首指针,指向队首元素
|
||||
int queSize; // 双向队列长度
|
||||
|
||||
public:
|
||||
public:
|
||||
/* 构造方法 */
|
||||
ArrayDeque(int capacity) {
|
||||
nums.resize(capacity);
|
||||
|
||||
@ -282,7 +282,7 @@ comments: true
|
||||
```java title="linkedlist_queue.java"
|
||||
/* 基于链表实现的队列 */
|
||||
class LinkedListQueue {
|
||||
private ListNode front, rear; // 头节点 front ,尾节点 rear
|
||||
private ListNode front, rear; // 头节点 front ,尾节点 rear
|
||||
private int queSize = 0;
|
||||
|
||||
public LinkedListQueue() {
|
||||
@ -350,11 +350,11 @@ comments: true
|
||||
```cpp title="linkedlist_queue.cpp"
|
||||
/* 基于链表实现的队列 */
|
||||
class LinkedListQueue {
|
||||
private:
|
||||
ListNode *front, *rear; // 头节点 front ,尾节点 rear
|
||||
private:
|
||||
ListNode *front, *rear; // 头节点 front ,尾节点 rear
|
||||
int queSize;
|
||||
|
||||
public:
|
||||
public:
|
||||
LinkedListQueue() {
|
||||
front = nullptr;
|
||||
rear = nullptr;
|
||||
@ -379,7 +379,7 @@ comments: true
|
||||
/* 入队 */
|
||||
void push(int num) {
|
||||
// 尾节点后添加 num
|
||||
ListNode* node = new ListNode(num);
|
||||
ListNode *node = new ListNode(num);
|
||||
// 如果队列为空,则令头、尾节点都指向该节点
|
||||
if (front == nullptr) {
|
||||
front = node;
|
||||
@ -400,7 +400,7 @@ comments: true
|
||||
ListNode *tmp = front;
|
||||
front = front->next;
|
||||
// 释放内存
|
||||
delete tmp;
|
||||
delete tmp;
|
||||
queSize--;
|
||||
}
|
||||
|
||||
@ -413,7 +413,7 @@ comments: true
|
||||
|
||||
/* 将链表转化为 Vector 并返回 */
|
||||
vector<int> toVector() {
|
||||
ListNode* node = front;
|
||||
ListNode *node = front;
|
||||
vector<int> res(size());
|
||||
for (int i = 0; i < res.size(); i++) {
|
||||
res[i] = node->val;
|
||||
@ -955,8 +955,8 @@ comments: true
|
||||
```java title="array_queue.java"
|
||||
/* 基于环形数组实现的队列 */
|
||||
class ArrayQueue {
|
||||
private int[] nums; // 用于存储队列元素的数组
|
||||
private int front; // 队首指针,指向队首元素
|
||||
private int[] nums; // 用于存储队列元素的数组
|
||||
private int front; // 队首指针,指向队首元素
|
||||
private int queSize; // 队列长度
|
||||
|
||||
public ArrayQueue(int capacity) {
|
||||
@ -1026,13 +1026,13 @@ comments: true
|
||||
```cpp title="array_queue.cpp"
|
||||
/* 基于环形数组实现的队列 */
|
||||
class ArrayQueue {
|
||||
private:
|
||||
private:
|
||||
int *nums; // 用于存储队列元素的数组
|
||||
int front; // 队首指针,指向队首元素
|
||||
int queSize; // 队列长度
|
||||
int queCapacity; // 队列容量
|
||||
|
||||
public:
|
||||
public:
|
||||
ArrayQueue(int capacity) {
|
||||
// 初始化数组
|
||||
nums = new int[capacity];
|
||||
|
||||
@ -284,9 +284,9 @@ comments: true
|
||||
```java title="linkedlist_stack.java"
|
||||
/* 基于链表实现的栈 */
|
||||
class LinkedListStack {
|
||||
private ListNode stackPeek; // 将头节点作为栈顶
|
||||
private int stkSize = 0; // 栈的长度
|
||||
|
||||
private ListNode stackPeek; // 将头节点作为栈顶
|
||||
private int stkSize = 0; // 栈的长度
|
||||
|
||||
public LinkedListStack() {
|
||||
stackPeek = null;
|
||||
}
|
||||
@ -342,11 +342,11 @@ comments: true
|
||||
```cpp title="linkedlist_stack.cpp"
|
||||
/* 基于链表实现的栈 */
|
||||
class LinkedListStack {
|
||||
private:
|
||||
ListNode* stackTop; // 将头节点作为栈顶
|
||||
private:
|
||||
ListNode *stackTop; // 将头节点作为栈顶
|
||||
int stkSize; // 栈的长度
|
||||
|
||||
public:
|
||||
public:
|
||||
LinkedListStack() {
|
||||
stackTop = nullptr;
|
||||
stkSize = 0;
|
||||
@ -369,7 +369,7 @@ comments: true
|
||||
|
||||
/* 入栈 */
|
||||
void push(int num) {
|
||||
ListNode* node = new ListNode(num);
|
||||
ListNode *node = new ListNode(num);
|
||||
node->next = stackTop;
|
||||
stackTop = node;
|
||||
stkSize++;
|
||||
@ -394,7 +394,7 @@ comments: true
|
||||
|
||||
/* 将 List 转化为 Array 并返回 */
|
||||
vector<int> toVector() {
|
||||
ListNode* node = stackTop;
|
||||
ListNode *node = stackTop;
|
||||
vector<int> res(size());
|
||||
for (int i = res.size() - 1; i >= 0; i--) {
|
||||
res[i] = node->val;
|
||||
@ -864,6 +864,7 @@ comments: true
|
||||
/* 基于数组实现的栈 */
|
||||
class ArrayStack {
|
||||
private ArrayList<Integer> stack;
|
||||
|
||||
public ArrayStack() {
|
||||
// 初始化列表(动态数组)
|
||||
stack = new ArrayList<>();
|
||||
@ -910,10 +911,10 @@ comments: true
|
||||
```cpp title="array_stack.cpp"
|
||||
/* 基于数组实现的栈 */
|
||||
class ArrayStack {
|
||||
private:
|
||||
private:
|
||||
vector<int> stack;
|
||||
|
||||
public:
|
||||
|
||||
public:
|
||||
/* 获取栈的长度 */
|
||||
int size() {
|
||||
return stack.size();
|
||||
@ -937,7 +938,7 @@ comments: true
|
||||
|
||||
/* 访问栈顶元素 */
|
||||
int top() {
|
||||
if(empty())
|
||||
if (empty())
|
||||
throw out_of_range("栈为空");
|
||||
return stack.back();
|
||||
}
|
||||
|
||||
@ -177,13 +177,13 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
|
||||
|
||||
```cpp title="avl_tree.cpp"
|
||||
/* 获取节点高度 */
|
||||
int height(TreeNode* node) {
|
||||
int height(TreeNode *node) {
|
||||
// 空节点高度为 -1 ,叶节点高度为 0
|
||||
return node == nullptr ? -1 : node->height;
|
||||
}
|
||||
|
||||
/* 更新节点高度 */
|
||||
void updateHeight(TreeNode* node) {
|
||||
void updateHeight(TreeNode *node) {
|
||||
// 节点高度等于最高子树高度 + 1
|
||||
node->height = max(height(node->left), height(node->right)) + 1;
|
||||
}
|
||||
@ -331,7 +331,8 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
|
||||
/* 获取平衡因子 */
|
||||
int balanceFactor(TreeNode node) {
|
||||
// 空节点平衡因子为 0
|
||||
if (node == null) return 0;
|
||||
if (node == null)
|
||||
return 0;
|
||||
// 节点平衡因子 = 左子树高度 - 右子树高度
|
||||
return height(node.left) - height(node.right);
|
||||
}
|
||||
@ -341,9 +342,10 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
|
||||
|
||||
```cpp title="avl_tree.cpp"
|
||||
/* 获取平衡因子 */
|
||||
int balanceFactor(TreeNode* node) {
|
||||
int balanceFactor(TreeNode *node) {
|
||||
// 空节点平衡因子为 0
|
||||
if (node == nullptr) return 0;
|
||||
if (node == nullptr)
|
||||
return 0;
|
||||
// 节点平衡因子 = 左子树高度 - 右子树高度
|
||||
return height(node->left) - height(node->right);
|
||||
}
|
||||
@ -498,9 +500,9 @@ AVL 树的特点在于「旋转 Rotation」操作,它能够在不影响二叉
|
||||
|
||||
```cpp title="avl_tree.cpp"
|
||||
/* 右旋操作 */
|
||||
TreeNode* rightRotate(TreeNode* node) {
|
||||
TreeNode* child = node->left;
|
||||
TreeNode* grandChild = child->right;
|
||||
TreeNode *rightRotate(TreeNode *node) {
|
||||
TreeNode *child = node->left;
|
||||
TreeNode *grandChild = child->right;
|
||||
// 以 child 为原点,将 node 向右旋转
|
||||
child->right = node;
|
||||
node->left = grandChild;
|
||||
@ -682,9 +684,9 @@ AVL 树的特点在于「旋转 Rotation」操作,它能够在不影响二叉
|
||||
|
||||
```cpp title="avl_tree.cpp"
|
||||
/* 左旋操作 */
|
||||
TreeNode* leftRotate(TreeNode* node) {
|
||||
TreeNode* child = node->right;
|
||||
TreeNode* grandChild = child->left;
|
||||
TreeNode *leftRotate(TreeNode *node) {
|
||||
TreeNode *child = node->right;
|
||||
TreeNode *grandChild = child->left;
|
||||
// 以 child 为原点,将 node 向左旋转
|
||||
child->left = node;
|
||||
node->right = grandChild;
|
||||
@ -905,7 +907,7 @@ AVL 树的特点在于「旋转 Rotation」操作,它能够在不影响二叉
|
||||
|
||||
```cpp title="avl_tree.cpp"
|
||||
/* 执行旋转操作,使该子树重新恢复平衡 */
|
||||
TreeNode* rotate(TreeNode* node) {
|
||||
TreeNode *rotate(TreeNode *node) {
|
||||
// 获取节点 node 的平衡因子
|
||||
int _balanceFactor = balanceFactor(node);
|
||||
// 左偏树
|
||||
@ -1201,15 +1203,16 @@ AVL 树的特点在于「旋转 Rotation」操作,它能够在不影响二叉
|
||||
|
||||
/* 递归插入节点(辅助方法) */
|
||||
TreeNode insertHelper(TreeNode node, int val) {
|
||||
if (node == null) return new TreeNode(val);
|
||||
if (node == null)
|
||||
return new TreeNode(val);
|
||||
/* 1. 查找插入位置,并插入节点 */
|
||||
if (val < node.val)
|
||||
node.left = insertHelper(node.left, val);
|
||||
else if (val > node.val)
|
||||
node.right = insertHelper(node.right, val);
|
||||
else
|
||||
return node; // 重复节点不插入,直接返回
|
||||
updateHeight(node); // 更新节点高度
|
||||
return node; // 重复节点不插入,直接返回
|
||||
updateHeight(node); // 更新节点高度
|
||||
/* 2. 执行旋转操作,使该子树重新恢复平衡 */
|
||||
node = rotate(node);
|
||||
// 返回子树的根节点
|
||||
@ -1221,13 +1224,13 @@ AVL 树的特点在于「旋转 Rotation」操作,它能够在不影响二叉
|
||||
|
||||
```cpp title="avl_tree.cpp"
|
||||
/* 插入节点 */
|
||||
TreeNode* insert(int val) {
|
||||
TreeNode *insert(int val) {
|
||||
root = insertHelper(root, val);
|
||||
return root;
|
||||
}
|
||||
|
||||
/* 递归插入节点(辅助方法) */
|
||||
TreeNode* insertHelper(TreeNode* node, int val) {
|
||||
TreeNode *insertHelper(TreeNode *node, int val) {
|
||||
if (node == nullptr)
|
||||
return new TreeNode(val);
|
||||
/* 1. 查找插入位置,并插入节点 */
|
||||
@ -1236,8 +1239,8 @@ AVL 树的特点在于「旋转 Rotation」操作,它能够在不影响二叉
|
||||
else if (val > node->val)
|
||||
node->right = insertHelper(node->right, val);
|
||||
else
|
||||
return node; // 重复节点不插入,直接返回
|
||||
updateHeight(node); // 更新节点高度
|
||||
return node; // 重复节点不插入,直接返回
|
||||
updateHeight(node); // 更新节点高度
|
||||
/* 2. 执行旋转操作,使该子树重新恢复平衡 */
|
||||
node = rotate(node);
|
||||
// 返回子树的根节点
|
||||
@ -1472,7 +1475,8 @@ AVL 树的特点在于「旋转 Rotation」操作,它能够在不影响二叉
|
||||
|
||||
/* 递归删除节点(辅助方法) */
|
||||
TreeNode removeHelper(TreeNode node, int val) {
|
||||
if (node == null) return null;
|
||||
if (node == null)
|
||||
return null;
|
||||
/* 1. 查找节点,并删除之 */
|
||||
if (val < node.val)
|
||||
node.left = removeHelper(node.left, val);
|
||||
@ -1494,7 +1498,7 @@ AVL 树的特点在于「旋转 Rotation」操作,它能够在不影响二叉
|
||||
node.val = temp.val;
|
||||
}
|
||||
}
|
||||
updateHeight(node); // 更新节点高度
|
||||
updateHeight(node); // 更新节点高度
|
||||
/* 2. 执行旋转操作,使该子树重新恢复平衡 */
|
||||
node = rotate(node);
|
||||
// 返回子树的根节点
|
||||
@ -1503,7 +1507,8 @@ AVL 树的特点在于「旋转 Rotation」操作,它能够在不影响二叉
|
||||
|
||||
/* 获取中序遍历中的下一个节点(仅适用于 root 有左子节点的情况) */
|
||||
TreeNode getInOrderNext(TreeNode node) {
|
||||
if (node == null) return node;
|
||||
if (node == null)
|
||||
return node;
|
||||
// 循环访问左子节点,直到叶节点时为最小节点,跳出
|
||||
while (node.left != null) {
|
||||
node = node.left;
|
||||
@ -1516,13 +1521,13 @@ AVL 树的特点在于「旋转 Rotation」操作,它能够在不影响二叉
|
||||
|
||||
```cpp title="avl_tree.cpp"
|
||||
/* 删除节点 */
|
||||
TreeNode* remove(int val) {
|
||||
TreeNode *remove(int val) {
|
||||
root = removeHelper(root, val);
|
||||
return root;
|
||||
}
|
||||
|
||||
/* 递归删除节点(辅助方法) */
|
||||
TreeNode* removeHelper(TreeNode* node, int val) {
|
||||
TreeNode *removeHelper(TreeNode *node, int val) {
|
||||
if (node == nullptr)
|
||||
return nullptr;
|
||||
/* 1. 查找节点,并删除之 */
|
||||
@ -1532,7 +1537,7 @@ AVL 树的特点在于「旋转 Rotation」操作,它能够在不影响二叉
|
||||
node->right = removeHelper(node->right, val);
|
||||
else {
|
||||
if (node->left == nullptr || node->right == nullptr) {
|
||||
TreeNode* child = node->left != nullptr ? node->left : node->right;
|
||||
TreeNode *child = node->left != nullptr ? node->left : node->right;
|
||||
// 子节点数量 = 0 ,直接删除 node 并返回
|
||||
if (child == nullptr) {
|
||||
delete node;
|
||||
@ -1545,13 +1550,13 @@ AVL 树的特点在于「旋转 Rotation」操作,它能够在不影响二叉
|
||||
}
|
||||
} else {
|
||||
// 子节点数量 = 2 ,则将中序遍历的下个节点删除,并用该节点替换当前节点
|
||||
TreeNode* temp = getInOrderNext(node->right);
|
||||
TreeNode *temp = getInOrderNext(node->right);
|
||||
int tempVal = temp->val;
|
||||
node->right = removeHelper(node->right, temp->val);
|
||||
node->val = tempVal;
|
||||
}
|
||||
}
|
||||
updateHeight(node); // 更新节点高度
|
||||
updateHeight(node); // 更新节点高度
|
||||
/* 2. 执行旋转操作,使该子树重新恢复平衡 */
|
||||
node = rotate(node);
|
||||
// 返回子树的根节点
|
||||
@ -1559,7 +1564,7 @@ AVL 树的特点在于「旋转 Rotation」操作,它能够在不影响二叉
|
||||
}
|
||||
|
||||
/* 获取中序遍历中的下一个节点(仅适用于 root 有左子节点的情况) */
|
||||
TreeNode* getInOrderNext(TreeNode* node) {
|
||||
TreeNode *getInOrderNext(TreeNode *node) {
|
||||
if (node == nullptr)
|
||||
return node;
|
||||
// 循环访问左子节点,直到叶节点时为最小节点,跳出
|
||||
|
||||
@ -46,11 +46,14 @@ comments: true
|
||||
// 循环查找,越过叶节点后跳出
|
||||
while (cur != null) {
|
||||
// 目标节点在 cur 的右子树中
|
||||
if (cur.val < num) cur = cur.right;
|
||||
if (cur.val < num)
|
||||
cur = cur.right;
|
||||
// 目标节点在 cur 的左子树中
|
||||
else if (cur.val > num) cur = cur.left;
|
||||
else if (cur.val > num)
|
||||
cur = cur.left;
|
||||
// 找到目标节点,跳出循环
|
||||
else break;
|
||||
else
|
||||
break;
|
||||
}
|
||||
// 返回目标节点
|
||||
return cur;
|
||||
@ -61,16 +64,19 @@ comments: true
|
||||
|
||||
```cpp title="binary_search_tree.cpp"
|
||||
/* 查找节点 */
|
||||
TreeNode* search(int num) {
|
||||
TreeNode* cur = root;
|
||||
TreeNode *search(int num) {
|
||||
TreeNode *cur = root;
|
||||
// 循环查找,越过叶节点后跳出
|
||||
while (cur != nullptr) {
|
||||
// 目标节点在 cur 的右子树中
|
||||
if (cur->val < num) cur = cur->right;
|
||||
if (cur->val < num)
|
||||
cur = cur->right;
|
||||
// 目标节点在 cur 的左子树中
|
||||
else if (cur->val > num) cur = cur->left;
|
||||
else if (cur->val > num)
|
||||
cur = cur->left;
|
||||
// 找到目标节点,跳出循环
|
||||
else break;
|
||||
else
|
||||
break;
|
||||
}
|
||||
// 返回目标节点
|
||||
return cur;
|
||||
@ -259,22 +265,28 @@ comments: true
|
||||
/* 插入节点 */
|
||||
TreeNode insert(int num) {
|
||||
// 若树为空,直接提前返回
|
||||
if (root == null) return null;
|
||||
if (root == null)
|
||||
return null;
|
||||
TreeNode cur = root, pre = null;
|
||||
// 循环查找,越过叶节点后跳出
|
||||
while (cur != null) {
|
||||
// 找到重复节点,直接返回
|
||||
if (cur.val == num) return null;
|
||||
if (cur.val == num)
|
||||
return null;
|
||||
pre = cur;
|
||||
// 插入位置在 cur 的右子树中
|
||||
if (cur.val < num) cur = cur.right;
|
||||
if (cur.val < num)
|
||||
cur = cur.right;
|
||||
// 插入位置在 cur 的左子树中
|
||||
else cur = cur.left;
|
||||
else
|
||||
cur = cur.left;
|
||||
}
|
||||
// 插入节点 val
|
||||
TreeNode node = new TreeNode(num);
|
||||
if (pre.val < num) pre.right = node;
|
||||
else pre.left = node;
|
||||
if (pre.val < num)
|
||||
pre.right = node;
|
||||
else
|
||||
pre.left = node;
|
||||
return node;
|
||||
}
|
||||
```
|
||||
@ -283,24 +295,30 @@ comments: true
|
||||
|
||||
```cpp title="binary_search_tree.cpp"
|
||||
/* 插入节点 */
|
||||
TreeNode* insert(int num) {
|
||||
TreeNode *insert(int num) {
|
||||
// 若树为空,直接提前返回
|
||||
if (root == nullptr) return nullptr;
|
||||
if (root == nullptr)
|
||||
return nullptr;
|
||||
TreeNode *cur = root, *pre = nullptr;
|
||||
// 循环查找,越过叶节点后跳出
|
||||
while (cur != nullptr) {
|
||||
// 找到重复节点,直接返回
|
||||
if (cur->val == num) return nullptr;
|
||||
if (cur->val == num)
|
||||
return nullptr;
|
||||
pre = cur;
|
||||
// 插入位置在 cur 的右子树中
|
||||
if (cur->val < num) cur = cur->right;
|
||||
if (cur->val < num)
|
||||
cur = cur->right;
|
||||
// 插入位置在 cur 的左子树中
|
||||
else cur = cur->left;
|
||||
else
|
||||
cur = cur->left;
|
||||
}
|
||||
// 插入节点 val
|
||||
TreeNode* node = new TreeNode(num);
|
||||
if (pre->val < num) pre->right = node;
|
||||
else pre->left = node;
|
||||
TreeNode *node = new TreeNode(num);
|
||||
if (pre->val < num)
|
||||
pre->right = node;
|
||||
else
|
||||
pre->left = node;
|
||||
return node;
|
||||
}
|
||||
```
|
||||
@ -588,27 +606,34 @@ comments: true
|
||||
/* 删除节点 */
|
||||
TreeNode remove(int num) {
|
||||
// 若树为空,直接提前返回
|
||||
if (root == null) return null;
|
||||
if (root == null)
|
||||
return null;
|
||||
TreeNode cur = root, pre = null;
|
||||
// 循环查找,越过叶节点后跳出
|
||||
while (cur != null) {
|
||||
// 找到待删除节点,跳出循环
|
||||
if (cur.val == num) break;
|
||||
if (cur.val == num)
|
||||
break;
|
||||
pre = cur;
|
||||
// 待删除节点在 cur 的右子树中
|
||||
if (cur.val < num) cur = cur.right;
|
||||
if (cur.val < num)
|
||||
cur = cur.right;
|
||||
// 待删除节点在 cur 的左子树中
|
||||
else cur = cur.left;
|
||||
else
|
||||
cur = cur.left;
|
||||
}
|
||||
// 若无待删除节点,则直接返回
|
||||
if (cur == null) return null;
|
||||
if (cur == null)
|
||||
return null;
|
||||
// 子节点数量 = 0 or 1
|
||||
if (cur.left == null || cur.right == null) {
|
||||
// 当子节点数量 = 0 / 1 时, child = null / 该子节点
|
||||
TreeNode child = cur.left != null ? cur.left : cur.right;
|
||||
// 删除节点 cur
|
||||
if (pre.left == cur) pre.left = child;
|
||||
else pre.right = child;
|
||||
if (pre.left == cur)
|
||||
pre.left = child;
|
||||
else
|
||||
pre.right = child;
|
||||
}
|
||||
// 子节点数量 = 2
|
||||
else {
|
||||
@ -625,7 +650,8 @@ comments: true
|
||||
|
||||
/* 获取中序遍历中的下一个节点(仅适用于 root 有左子节点的情况) */
|
||||
TreeNode getInOrderNext(TreeNode root) {
|
||||
if (root == null) return root;
|
||||
if (root == null)
|
||||
return root;
|
||||
// 循环访问左子节点,直到叶节点时为最小节点,跳出
|
||||
while (root.left != null) {
|
||||
root = root.left;
|
||||
@ -638,36 +664,43 @@ comments: true
|
||||
|
||||
```cpp title="binary_search_tree.cpp"
|
||||
/* 删除节点 */
|
||||
TreeNode* remove(int num) {
|
||||
TreeNode *remove(int num) {
|
||||
// 若树为空,直接提前返回
|
||||
if (root == nullptr) return nullptr;
|
||||
if (root == nullptr)
|
||||
return nullptr;
|
||||
TreeNode *cur = root, *pre = nullptr;
|
||||
// 循环查找,越过叶节点后跳出
|
||||
while (cur != nullptr) {
|
||||
// 找到待删除节点,跳出循环
|
||||
if (cur->val == num) break;
|
||||
if (cur->val == num)
|
||||
break;
|
||||
pre = cur;
|
||||
// 待删除节点在 cur 的右子树中
|
||||
if (cur->val < num) cur = cur->right;
|
||||
if (cur->val < num)
|
||||
cur = cur->right;
|
||||
// 待删除节点在 cur 的左子树中
|
||||
else cur = cur->left;
|
||||
else
|
||||
cur = cur->left;
|
||||
}
|
||||
// 若无待删除节点,则直接返回
|
||||
if (cur == nullptr) return nullptr;
|
||||
if (cur == nullptr)
|
||||
return nullptr;
|
||||
// 子节点数量 = 0 or 1
|
||||
if (cur->left == nullptr || cur->right == nullptr) {
|
||||
// 当子节点数量 = 0 / 1 时, child = nullptr / 该子节点
|
||||
TreeNode* child = cur->left != nullptr ? cur->left : cur->right;
|
||||
TreeNode *child = cur->left != nullptr ? cur->left : cur->right;
|
||||
// 删除节点 cur
|
||||
if (pre->left == cur) pre->left = child;
|
||||
else pre->right = child;
|
||||
if (pre->left == cur)
|
||||
pre->left = child;
|
||||
else
|
||||
pre->right = child;
|
||||
// 释放内存
|
||||
delete cur;
|
||||
}
|
||||
// 子节点数量 = 2
|
||||
else {
|
||||
// 获取中序遍历中 cur 的下一个节点
|
||||
TreeNode* nex = getInOrderNext(cur->right);
|
||||
TreeNode *nex = getInOrderNext(cur->right);
|
||||
int tmp = nex->val;
|
||||
// 递归删除节点 nex
|
||||
remove(nex->val);
|
||||
@ -678,8 +711,9 @@ comments: true
|
||||
}
|
||||
|
||||
/* 获取中序遍历中的下一个节点(仅适用于 root 有左子节点的情况) */
|
||||
TreeNode* getInOrderNext(TreeNode* root) {
|
||||
if (root == nullptr) return root;
|
||||
TreeNode *getInOrderNext(TreeNode *root) {
|
||||
if (root == nullptr)
|
||||
return root;
|
||||
// 循环访问左子节点,直到叶节点时为最小节点,跳出
|
||||
while (root->left != nullptr) {
|
||||
root = root->left;
|
||||
|
||||
@ -32,12 +32,12 @@ comments: true
|
||||
// 初始化一个列表,用于保存遍历序列
|
||||
List<Integer> list = new ArrayList<>();
|
||||
while (!queue.isEmpty()) {
|
||||
TreeNode node = queue.poll(); // 队列出队
|
||||
list.add(node.val); // 保存节点值
|
||||
TreeNode node = queue.poll(); // 队列出队
|
||||
list.add(node.val); // 保存节点值
|
||||
if (node.left != null)
|
||||
queue.offer(node.left); // 左子节点入队
|
||||
queue.offer(node.left); // 左子节点入队
|
||||
if (node.right != null)
|
||||
queue.offer(node.right); // 右子节点入队
|
||||
queue.offer(node.right); // 右子节点入队
|
||||
}
|
||||
return list;
|
||||
}
|
||||
@ -47,18 +47,18 @@ comments: true
|
||||
|
||||
```cpp title="binary_tree_bfs.cpp"
|
||||
/* 层序遍历 */
|
||||
vector<int> levelOrder(TreeNode* root) {
|
||||
vector<int> levelOrder(TreeNode *root) {
|
||||
// 初始化队列,加入根节点
|
||||
queue<TreeNode*> queue;
|
||||
queue<TreeNode *> queue;
|
||||
queue.push(root);
|
||||
// 初始化一个列表,用于保存遍历序列
|
||||
vector<int> vec;
|
||||
while (!queue.empty()) {
|
||||
TreeNode* node = queue.front();
|
||||
queue.pop(); // 队列出队
|
||||
vec.push_back(node->val); // 保存节点值
|
||||
TreeNode *node = queue.front();
|
||||
queue.pop(); // 队列出队
|
||||
vec.push_back(node->val); // 保存节点值
|
||||
if (node->left != nullptr)
|
||||
queue.push(node->left); // 左子节点入队
|
||||
queue.push(node->left); // 左子节点入队
|
||||
if (node->right != nullptr)
|
||||
queue.push(node->right); // 右子节点入队
|
||||
}
|
||||
@ -277,7 +277,8 @@ comments: true
|
||||
```java title="binary_tree_dfs.java"
|
||||
/* 前序遍历 */
|
||||
void preOrder(TreeNode root) {
|
||||
if (root == null) return;
|
||||
if (root == null)
|
||||
return;
|
||||
// 访问优先级:根节点 -> 左子树 -> 右子树
|
||||
list.add(root.val);
|
||||
preOrder(root.left);
|
||||
@ -286,7 +287,8 @@ comments: true
|
||||
|
||||
/* 中序遍历 */
|
||||
void inOrder(TreeNode root) {
|
||||
if (root == null) return;
|
||||
if (root == null)
|
||||
return;
|
||||
// 访问优先级:左子树 -> 根节点 -> 右子树
|
||||
inOrder(root.left);
|
||||
list.add(root.val);
|
||||
@ -295,7 +297,8 @@ comments: true
|
||||
|
||||
/* 后序遍历 */
|
||||
void postOrder(TreeNode root) {
|
||||
if (root == null) return;
|
||||
if (root == null)
|
||||
return;
|
||||
// 访问优先级:左子树 -> 右子树 -> 根节点
|
||||
postOrder(root.left);
|
||||
postOrder(root.right);
|
||||
@ -307,8 +310,9 @@ comments: true
|
||||
|
||||
```cpp title="binary_tree_dfs.cpp"
|
||||
/* 前序遍历 */
|
||||
void preOrder(TreeNode* root) {
|
||||
if (root == nullptr) return;
|
||||
void preOrder(TreeNode *root) {
|
||||
if (root == nullptr)
|
||||
return;
|
||||
// 访问优先级:根节点 -> 左子树 -> 右子树
|
||||
vec.push_back(root->val);
|
||||
preOrder(root->left);
|
||||
@ -316,8 +320,9 @@ comments: true
|
||||
}
|
||||
|
||||
/* 中序遍历 */
|
||||
void inOrder(TreeNode* root) {
|
||||
if (root == nullptr) return;
|
||||
void inOrder(TreeNode *root) {
|
||||
if (root == nullptr)
|
||||
return;
|
||||
// 访问优先级:左子树 -> 根节点 -> 右子树
|
||||
inOrder(root->left);
|
||||
vec.push_back(root->val);
|
||||
@ -325,8 +330,9 @@ comments: true
|
||||
}
|
||||
|
||||
/* 后序遍历 */
|
||||
void postOrder(TreeNode* root) {
|
||||
if (root == nullptr) return;
|
||||
void postOrder(TreeNode *root) {
|
||||
if (root == nullptr)
|
||||
return;
|
||||
// 访问优先级:左子树 -> 右子树 -> 根节点
|
||||
postOrder(root->left);
|
||||
postOrder(root->right);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user