Merge branch 'master' into master

pull/68/head
Yudong Jin 2022-12-11 02:33:05 +08:00 committed by GitHub
commit 60103f1a41
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
108 changed files with 922 additions and 152 deletions

View File

@ -39,29 +39,30 @@
- 代码导向,提供精简、可运行的算法代码;
- 讨论学习,提问一般能在三日内得到回复;
如果感觉本书对你有所帮助,请点个 Star :star: 支持一下,谢谢!
## 推荐语
> “一本通俗易懂的数据结构与算法入门书,引导读者手脑并用地学习,强烈推荐算法初学者阅读。”
>
> **—— 邓俊辉,清华大学计算机系教授**
## 贡献
我们正在加速更新本书,期待您来[一起参与创作](https://www.hello-algo.com/chapter_preface/contribution/),以帮助其他读者获取更优质的学习内容:
- 如果发现笔误、无效链接、内容缺失、文字歧义、解释不清晰等问题,烦请您帮忙修正;
- 欢迎您通过提交 Pull Request 来增添新内容,包括重写章节、新增章节、翻译代码至不同语言等;
- [代码翻译](https://github.com/krahets/hello-algo/issues/15) C++, Python, Go, JavaScript, TypeScript 正在进行中,期望您前来挑大梁;
- 欢迎您通过提交 Pull Request 来增添新内容,包括重写章节、新增章节等;
> 有任何问题请与我联系 WeChat: krahets-jyd
如果感觉本书对你有所帮助,请点个 Star :star: 支持一下,谢谢!
## 致谢
感谢本开源书的每一位撰稿人,是他们的无私奉献让这本书变得更好,他们是:
<a href="https://github.com/krahets/hello-algo/graphs/contributors">
<img src="https://contrib.rocks/image?repo=krahets/hello-algo" />
</a>
## To-Dos
- [x] [代码翻译](https://github.com/krahets/hello-algo/issues/15)Java, C++, Python, Go, JavaScript 正在进行中,其他语言请求大佬挑大梁
- [ ] 数据结构:散列表、堆(优先队列)、图
- [ ] 算法:搜索与回溯、选择 / 堆排序、动态规划、贪心、分治
## License
The texts, codes, images, photos, and videos in this repository are licensed under [CC BY-NC-SA-4.0](https://creativecommons.org/licenses/by-nc-sa/4.0/).

View File

@ -0,0 +1,43 @@
// File: binary_search.go
// Created Time: 2022-12-05
// Author: Slone123c (274325721@qq.com)
package chapter_searching
/* 二分查找(双闭区间) */
func binarySearch(nums []int, target int) int {
// 初始化双闭区间 [0, n-1] ,即 i, j 分别指向数组首元素、尾元素
i, j := 0, len(nums)-1
// 循环,当搜索区间为空时跳出(当 i > j 时为空)
for i <= j {
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 { // 找到目标元素,返回其索引
return m
}
}
// 未找到目标元素,返回 -1
return -1
}
/* 二分查找(左闭右开) */
func binarySearch1(nums []int, target int) int {
// 初始化左闭右开 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1
i, j := 0, len(nums)
// 循环,当搜索区间为空时跳出(当 i = j 时为空)
for i < j {
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 { // 找到目标元素,返回其索引
return m
}
}
// 未找到目标元素,返回 -1
return -1
}

View File

@ -0,0 +1,24 @@
// File: binary_search_test.go
// Created Time: 2022-12-05
// Author: Slone123c (274325721@qq.com)
package chapter_searching
import (
"fmt"
"testing"
)
func TestBinarySearch(t *testing.T) {
var (
target = 3
nums = []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
expected = 2
)
// 在数组中执行二分查找
actual := binarySearch(nums, target)
fmt.Println("目标元素 3 的索引 =", actual)
if actual != expected {
t.Errorf("目标元素 3 的索引 = %d, 应该为 %d", actual, expected)
}
}

View File

@ -0,0 +1,38 @@
// File: bubble_sort.go
// Created Time: 2022-12-06
// Author: Slone123c (274325721@qq.com)
package bubble_sort
/* 冒泡排序 */
func bubbleSort(nums []int) {
// 外循环:待排序元素数量为 n-1, n-2, ..., 1
for i := len(nums) - 1; i > 0; i-- {
// 内循环:冒泡操作
for j := 0; j < i; j++ {
if nums[j] > nums[j+1] {
// 交换 nums[j] 与 nums[j + 1]
nums[j], nums[j+1] = nums[j+1], nums[j]
}
}
}
}
/* 冒泡排序(标志优化)*/
func bubbleSortWithFlag(nums []int) {
// 外循环:待排序元素数量为 n-1, n-2, ..., 1
for i := len(nums) - 1; i > 0; i-- {
flag := false // 初始化标志位
// 内循环:冒泡操作
for j := 0; j < i; j++ {
if nums[j] > nums[j+1] {
// 交换 nums[j] 与 nums[j + 1]
nums[j], nums[j+1] = nums[j+1], nums[j]
flag = true // 记录交换元素
}
}
if flag == false { // 此轮冒泡未交换任何元素,直接跳出
break
}
}
}

View File

@ -0,0 +1,20 @@
// File: bubble_sort_test.go
// Created Time: 2022-12-06
// Author: Slone123c (274325721@qq.com)
package bubble_sort
import (
"fmt"
"testing"
)
func TestBubbleSort(t *testing.T) {
nums := []int{4, 1, 3, 1, 5, 2}
bubbleSort(nums)
fmt.Println("冒泡排序完成后 nums = ", nums)
nums1 := []int{4, 1, 3, 1, 5, 2}
bubbleSortWithFlag(nums1)
fmt.Println("冒泡排序完成后 nums1 = ", nums)
}

View File

@ -22,15 +22,15 @@ class ArrayHashMap {
private List<Entry> bucket;
public ArrayHashMap() {
// 初始化一个长度为 10 的桶(数组)
bucket = new ArrayList<>(10);
for (int i = 0; i < 10; i++) {
bucket = new ArrayList<>();
for (int i = 0; i < 100; i++) {
bucket.add(null);
}
}
/* 哈希函数 */
private int hashFunc(int key) {
int index = key % 10000;
int index = key % 100;
return index;
}
@ -102,23 +102,23 @@ public class array_hash_map {
/* 添加操作 */
// 在哈希表中添加键值对 (key, value)
map.put(10001, "小哈");
map.put(10002, "小啰");
map.put(10003, "小算");
map.put(10004, "小法");
map.put(10005, "小哇");
map.put(12836, "小哈");
map.put(15937, "小啰");
map.put(16750, "小算");
map.put(13276, "小法");
map.put(10583, "小鸭");
System.out.println("\n添加完成后哈希表为\nKey -> Value");
map.print();
/* 查询操作 */
// 向哈希表输入键 key ,得到值 value
String name = map.get(10002);
System.out.println("\n输入学号 10002 ,查询到姓名 " + name);
String name = map.get(15937);
System.out.println("\n输入学号 15937 ,查询到姓名 " + name);
/* 删除操作 */
// 在哈希表中删除键值对 (key, value)
map.remove(10005);
System.out.println("\n删除 10005 后,哈希表为\nKey -> Value");
map.remove(10583);
System.out.println("\n删除 10583 后,哈希表为\nKey -> Value");
map.print();
/* 遍历哈希表 */

View File

@ -15,23 +15,23 @@ public class hash_map {
/* 添加操作 */
// 在哈希表中添加键值对 (key, value)
map.put(10001, "小哈");
map.put(10002, "小啰");
map.put(10003, "小算");
map.put(10004, "小法");
map.put(10005, "小哇");
map.put(12836, "小哈");
map.put(15937, "小啰");
map.put(16750, "小算");
map.put(13276, "小法");
map.put(10583, "小鸭");
System.out.println("\n添加完成后哈希表为\nKey -> Value");
PrintUtil.printHashMap(map);
/* 查询操作 */
// 向哈希表输入键 key ,得到值 value
String name = map.get(10002);
System.out.println("\n输入学号 10002 ,查询到姓名 " + name);
String name = map.get(15937);
System.out.println("\n输入学号 15937 ,查询到姓名 " + name);
/* 删除操作 */
// 在哈希表中删除键值对 (key, value)
map.remove(10005);
System.out.println("\n删除 10005 后,哈希表为\nKey -> Value");
map.remove(10583);
System.out.println("\n删除 10583 后,哈希表为\nKey -> Value");
PrintUtil.printHashMap(map);
/* 遍历哈希表 */

View File

@ -0,0 +1,218 @@
/*
* File: avl_tree.java
* Created Time: 2022-12-10
* Author: Krahets (krahets@163.com)
*/
package chapter_tree;
import include.*;
// Tree class
class AVLTree {
TreeNode root; // 根节点
/* 获取结点高度 */
public int height(TreeNode node) {
// 空结点高度为 -1 ,叶结点高度为 0
return node == null ? -1 : node.height;
}
/* 更新结点高度 */
private void updateHeight(TreeNode node) {
node.height = Math.max(height(node.left), height(node.right)) + 1;
}
/* 获取平衡因子 */
public int balanceFactor(TreeNode node) {
if (node == null)
return 0;
return height(node.left) - height(node.right);
}
/* 右旋操作 */
private TreeNode rightRotate(TreeNode node) {
TreeNode child = node.left;
TreeNode grandChild = child.right;
child.right = node;
node.left = grandChild;
updateHeight(node);
updateHeight(child);
return child;
}
/* 左旋操作 */
private TreeNode leftRotate(TreeNode node) {
TreeNode child = node.right;
TreeNode grandChild = child.left;
child.left = node;
node.right = grandChild;
updateHeight(node);
updateHeight(child);
return child;
}
/* 执行旋转操作,使该子树重新恢复平衡 */
private TreeNode rotate(TreeNode node) {
int balanceFactor = balanceFactor(node);
// 根据失衡情况分为四种操作:右旋、左旋、先左后右、先右后左
if (balanceFactor > 1) {
if (balanceFactor(node.left) >= 0) {
// 右旋
return rightRotate(node);
} else {
// 先左旋后右旋
node.left = leftRotate(node.left);
return rightRotate(node);
}
}
if (balanceFactor < -1) {
if (balanceFactor(node.right) <= 0) {
// 左旋
return leftRotate(node);
} else {
// 先右旋后左旋
node.right = rightRotate(node.right);
return leftRotate(node);
}
}
return node;
}
/* 插入结点 */
public TreeNode insert(int val) {
root = insertHelper(root, val);
return root;
}
/* 递归插入结点 */
private TreeNode insertHelper(TreeNode node, int val) {
// 1. 查找插入位置,并插入结点
if (node == null)
return new TreeNode(val);
if (val < node.val)
node.left = insertHelper(node.left, val);
else if (val > node.val)
node.right = insertHelper(node.right, val);
else
return node; // 重复结点则直接返回
// 2. 更新结点高度
updateHeight(node);
// 3. 执行旋转操作,使该子树重新恢复平衡
node = rotate(node);
// 返回该子树的根节点
return node;
}
/* 删除结点 */
public TreeNode remove(int val) {
root = removeHelper(root, val);
return root;
}
/* 递归删除结点 */
private TreeNode removeHelper(TreeNode node, int val) {
// 1. 查找结点,并删除之
if (node == null)
return null;
if (val < node.val)
node.left = removeHelper(node.left, val);
else if (val > node.val)
node.right = removeHelper(node.right, val);
else {
if (node.left == null || node.right == null) {
TreeNode child = node.left != null ? node.left : node.right;
// 子结点数量 = 0 ,直接删除 node 并返回
if (child == null)
return null;
// 子结点数量 = 1 ,直接删除 node
else
node = child;
} else {
// 子结点数量 = 2 ,则将中序遍历的下个结点删除,并用该结点替换当前结点
TreeNode temp = minNode(node.right);
node.right = removeHelper(node.right, temp.val);
node.val = temp.val;
}
}
// 2. 更新结点高度
updateHeight(node);
// 3. 执行旋转操作,使该子树重新恢复平衡
node = rotate(node);
// 返回该子树的根节点
return node;
}
/* 获取最小结点 */
private TreeNode minNode(TreeNode node) {
if (node == null) return node;
// 循环访问左子结点,直到叶结点时为最小结点,跳出
while (node.left != null) {
node = node.left;
}
return node;
}
/* 查找结点 */
public TreeNode search(int val) {
TreeNode cur = root;
// 循环查找,越过叶结点后跳出
while (cur != null) {
// 目标结点在 root 的右子树中
if (cur.val < val) cur = cur.right;
// 目标结点在 root 的左子树中
else if (cur.val > val) cur = cur.left;
// 找到目标结点,跳出循环
else break;
}
// 返回目标结点
return cur;
}
}
public class avl_tree {
static void testInsert(AVLTree tree, int val) {
tree.insert(val);
System.out.println("\n插入结点 " + val + " 后AVL 树为");
PrintUtil.printTree(tree.root);
}
static void testRemove(AVLTree tree, int val) {
tree.remove(val);
System.out.println("\n删除结点 " + val + " 后AVL 树为");
PrintUtil.printTree(tree.root);
}
public static void main(String[] args) {
/* 初始化空 AVL 树 */
AVLTree avlTree = new AVLTree();
/* 插入结点 */
// 请关注插入结点后AVL 树是如何保持平衡的
testInsert(avlTree, 1);
testInsert(avlTree, 2);
testInsert(avlTree, 3);
testInsert(avlTree, 4);
testInsert(avlTree, 5);
testInsert(avlTree, 8);
testInsert(avlTree, 7);
testInsert(avlTree, 9);
testInsert(avlTree, 10);
testInsert(avlTree, 6);
/* 插入重复结点 */
testInsert(avlTree, 7);
/* 删除结点 */
// 请关注删除结点后AVL 树是如何保持平衡的
testRemove(avlTree, 8); // 删除度为 0 的结点
testRemove(avlTree, 5); // 删除度为 1 的结点
testRemove(avlTree, 4); // 删除度为 2 的结点
/* 查询结点 */
TreeNode node = avlTree.search(7);
System.out.println("\n查找到的结点对象为 " + node + ",结点值 = " + node.val);
}
}

8
codes/java/include/TreeNode.java Executable file → Normal file
View File

@ -12,10 +12,10 @@ import java.util.*;
* Definition for a binary tree node.
*/
public class TreeNode {
public int val;
public int height;
public TreeNode left;
public TreeNode right;
public int val; // 结点值
public int height; // 结点高度
public TreeNode left; // 左子结点引用
public TreeNode right; // 右子结点引用
public TreeNode(int x) {
val = x;

View File

@ -0,0 +1,84 @@
/**
* File: array_stack.js
* Created Time: 2022-12-09
* Author: S-N-O-R-L-A-X (snorlax.xu@outlook.com)
*/
/* 基于数组实现的栈 */
class ArrayStack {
stack;
constructor() {
this.stack = [];
}
/* 获取栈的长度 */
get size() {
return this.stack.length;
}
/* 判断栈是否为空 */
empty() {
return this.stack.length === 0;
}
/* 入栈 */
push(num) {
this.stack.push(num);
}
/* 出栈 */
pop() {
return this.stack.pop();
}
/* 访问栈顶元素 */
top() {
return this.stack[this.stack.length - 1];
}
/* 访问索引 index 处元素 */
get(index) {
return this.stack[index];
}
/* 返回 Array */
toArray() {
return this.stack;
}
};
/* Driver Code */
/* 初始化栈 */
const stack = new ArrayStack();
/* 元素入栈 */
stack.push(1);
stack.push(3);
stack.push(2);
stack.push(5);
stack.push(4);
console.log("栈 stack = ");
console.log(stack.toArray());
/* 访问栈顶元素 */
const top = stack.top();
console.log("栈顶元素 top = " + top);
/* 访问索引 index 处元素 */
const num = stack.get(3);
console.log("栈索引 3 处的元素为 num = " + num);
/* 元素出栈 */
const pop = stack.pop();
console.log("出栈元素 pop = " + pop + ",出栈后 stack = ");
console.log(stack.toArray());
/* 获取栈的长度 */
const size = stack.size;
console.log("栈的长度 size = " + size);
/* 判断是否为空 */
const empty = stack.empty();
console.log("栈是否为空 = " + empty);

View File

@ -0,0 +1,30 @@
/**
* File: queue.js
* Created Time: 2022-12-05
* Author: S-N-O-R-L-A-X (snorlax.xu@outlook.com)
*/
/* 初始化队列 */
// JavaScript 没有内置的队列,可以把 Array 当作队列来使用
// 注意:由于是数组,所以 shift() 的时间复杂度是 O(n)
const queue = [];
/* 元素入队 */
queue.push(1);
queue.push(3);
queue.push(2);
queue.push(5);
queue.push(4);
/* 访问队首元素 */
const peek = queue[0];
/* 元素出队 */
// O(n)
const poll = queue.shift();
/* 获取队列的长度 */
const size = queue.length;
/* 判断队列是否为空 */
const empty = queue.length === 0;

View File

@ -1,5 +1,5 @@
'''
File: que.py
File: queue.py
Created Time: 2022-11-29
Author: Peng Chen (pengchzn@gmail.com)
'''

View File

@ -0,0 +1,86 @@
/**
* File: array_stack.ts
* Created Time: 2022-12-08
* Author: S-N-O-R-L-A-X (snorlax.xu@outlook.com)
*/
/* 基于数组实现的栈 */
class ArrayStack {
private stack: number[];
constructor() {
this.stack = [];
}
/* 获取栈的长度 */
get size(): number {
return this.stack.length;
}
/* 判断栈是否为空 */
empty(): boolean {
return this.stack.length === 0;
}
/* 入栈 */
push(num: number): void {
this.stack.push(num);
}
/* 出栈 */
pop(): number | undefined {
return this.stack.pop();
}
/* 访问栈顶元素 */
top(): number | undefined {
return this.stack[this.stack.length - 1];
}
/* 访问索引 index 处元素 */
get(index: number): number | undefined {
return this.stack[index];
}
/* 返回 Array */
toArray() {
return this.stack;
}
};
/* Driver Code */
/* 初始化栈 */
const stack = new ArrayStack();
/* 元素入栈 */
stack.push(1);
stack.push(3);
stack.push(2);
stack.push(5);
stack.push(4);
console.log("栈 stack = ");
console.log(stack.toArray());
/* 访问栈顶元素 */
const top = stack.top();
console.log("栈顶元素 top = " + top);
/* 访问索引 index 处元素 */
const num = stack.get(3);
console.log("栈索引 3 处的元素为 num = " + num);
/* 元素出栈 */
const pop = stack.pop();
console.log("出栈元素 pop = " + pop + ",出栈后 stack = ");
console.log(stack.toArray());
/* 获取栈的长度 */
const size = stack.size;
console.log("栈的长度 size = " + size);
/* 判断是否为空 */
const empty = stack.empty();
console.log("栈是否为空 = " + empty);
export { };

View File

@ -0,0 +1,32 @@
/**
* File: queue.ts
* Created Time: 2022-12-05
* Author: S-N-O-R-L-A-X (snorlax.xu@outlook.com)
*/
/* 初始化队列 */
// TypeScript 没有内置的队列,可以把 Array 当作队列来使用
// 注意:由于是数组,所以 shift() 的时间复杂度是 O(n)
const queue: number[] = [];
/* 元素入队 */
queue.push(1);
queue.push(3);
queue.push(2);
queue.push(5);
queue.push(4);
/* 访问队首元素 */
const peek = queue[0];
/* 元素出队 */
// O(n)
const poll = queue.shift();
/* 获取队列的长度 */
const size = queue.length;
/* 判断队列是否为空 */
const empty = queue.length === 0;
export { };

Binary file not shown.

Before

Width:  |  Height:  |  Size: 59 KiB

After

Width:  |  Height:  |  Size: 59 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 100 KiB

After

Width:  |  Height:  |  Size: 100 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 72 KiB

After

Width:  |  Height:  |  Size: 72 KiB

View File

@ -65,13 +65,13 @@ comments: true
=== "C"
```c title="array.c"
```
=== "C#"
```csharp title="array.cs"
```
## 数组优点
@ -131,7 +131,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
=== "Go"
```go title="array.go"
```
=== "JavaScript"
@ -163,13 +163,13 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
=== "C"
```c title="array.c"
```
=== "C#"
```csharp title="array.cs"
```
## 数组缺点
@ -227,7 +227,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
=== "Go"
```go title="array.go"
```
=== "JavaScript"
@ -265,19 +265,19 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
=== "C"
```c title="array.c"
```
=== "C#"
```csharp title="array.cs"
```
**数组中插入或删除元素效率低下。** 假设我们想要在数组中间某位置插入一个元素,由于数组元素在内存中是 “紧挨着的” ,它们之间没有空间再放任何数据。因此,我们不得不将此索引之后的所有元素都向后移动一位,然后再把元素赋值给该索引。删除元素也是类似,需要把此索引之后的元素都向前移动一位。总体看有以下缺点:
- **时间复杂度高:** 数组的插入和删除的平均时间复杂度均为 $O(N)$ ,其中 $N$ 为数组长度。
- **丢失元素** 由于数组的长度不可变,因此在插入元素后,数组原来的末尾元素会丢失。
- **丢失元素:** 由于数组的长度不可变,因此在插入元素后,超出数组长度范围的元素会被丢失。
- **内存浪费:** 我们一般会初始化一个比较长的数组,只用前面一部分,这样在插入数据时,丢失的末尾元素都是我们不关心的,但这样做同时也会造成内存空间的浪费。
![array_insert_remove_element](array.assets/array_insert_remove_element.png)
@ -318,7 +318,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
// 将 num 赋给 index 处元素
nums[index] = num;
}
/* 删除索引 index 处元素 */
void remove(int* nums, int size, int index) {
// 把索引 index 之后的所有元素向前移动一位
@ -338,7 +338,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
nums[i] = nums[i - 1]
# 将 num 赋给 index 处元素
nums[index] = num
""" 删除索引 index 处元素 """
def remove(nums, index):
# 把索引 index 之后的所有元素向前移动一位
@ -349,7 +349,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
=== "Go"
```go title="array.go"
```
=== "JavaScript"
@ -364,7 +364,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
// 将 num 赋给 index 处元素
nums[index] = num;
}
/* 删除索引 index 处元素 */
function remove(nums, index){
// 把索引 index 之后的所有元素向前移动一位
@ -386,7 +386,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
// 将 num 赋给 index 处元素
nums[index] = num
}
/* 删除索引 index 处元素 */
function remove(nums: number[], index: number): void {
// 把索引 index 之后的所有元素向前移动一位
@ -399,13 +399,13 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
=== "C"
```c title="array.c"
```
=== "C#"
```csharp title="array.cs"
```
## 数组常用操作
@ -459,7 +459,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
=== "Go"
```go title="array.go"
```
=== "JavaScript"
@ -499,13 +499,13 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
=== "C"
```c title="array.c"
```
=== "C#"
```csharp title="array.cs"
```
**数组查找。** 通过遍历数组,查找数组内的指定元素,并输出对应索引。
@ -550,7 +550,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
=== "Go"
```go title="array.go"
```
=== "JavaScript"
@ -583,13 +583,13 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
=== "C"
```c title="array.c"
```
=== "C#"
```csharp title="array.cs"
```
## 数组典型应用

Binary file not shown.

Before

Width:  |  Height:  |  Size: 63 KiB

After

Width:  |  Height:  |  Size: 63 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 75 KiB

After

Width:  |  Height:  |  Size: 75 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 81 KiB

After

Width:  |  Height:  |  Size: 81 KiB

View File

@ -30,7 +30,7 @@ comments: true
### 理论估算
既然实际测试具有很大的局限性,那么我们是否可以仅通过一些计算,就获知算法的效率水平呢?答案是肯定的,我们将此估算方法称为「复杂度分析 Complexity Analysis」或「渐复杂度分析 Asymptotic Complexity Analysis」。
既然实际测试具有很大的局限性,那么我们是否可以仅通过一些计算,就获知算法的效率水平呢?答案是肯定的,我们将此估算方法称为「复杂度分析 Complexity Analysis」或「渐复杂度分析 Asymptotic Complexity Analysis」。
**复杂度分析评估随着输入数据量的增长,算法的运行时间和占用空间的增长趋势** 。根据时间和空间两方面,复杂度可分为「时间复杂度 Time Complexity」和「空间复杂度 Space Complexity」。

Binary file not shown.

Before

Width:  |  Height:  |  Size: 69 KiB

After

Width:  |  Height:  |  Size: 69 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 67 KiB

After

Width:  |  Height:  |  Size: 66 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 74 KiB

After

Width:  |  Height:  |  Size: 74 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 78 KiB

After

Width:  |  Height:  |  Size: 78 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 57 KiB

After

Width:  |  Height:  |  Size: 55 KiB

View File

@ -13,8 +13,8 @@ comments: true
### 时间复杂度
- 「时间复杂度」统计算法运行时间随着数据量变大时的增长趋势,可以有效评估算法效率,但在某些情况下可能失效,比如在输入数据量较小或时间复杂度相同时,无法精确对比算法效率的优劣性。
- 「最差时间复杂度」使用大 $O$ 符号表示,即函数渐上界,其反映当 $n$ 趋于正无穷时,$T(n)$ 处于何种增长级别。
- 推算时间复杂度分为两步,首先统计计算操作数量,再判断渐上界。
- 「最差时间复杂度」使用大 $O$ 符号表示,即函数渐上界,其反映当 $n$ 趋于正无穷时,$T(n)$ 处于何种增长级别。
- 推算时间复杂度分为两步,首先统计计算操作数量,再判断渐上界。
- 常见时间复杂度从小到大排列有 $O(1)$ , $O(\log n)$ , $O(n)$ , $O(n \log n)$ , $O(n^2)$ , $O(2^n)$ , $O(n!)$ 。
- 某些算法的时间复杂度不是恒定的,而是与输入数据的分布有关。时间复杂度分为「最差时间复杂度」和「最佳时间复杂度」,后者几乎不用,因为输入数据需要满足苛刻的条件才能达到最佳情况。
- 「平均时间复杂度」可以反映在随机数据输入下的算法效率,最贴合实际使用情况下的算法性能。计算平均时间复杂度需要统计输入数据的分布,以及综合后的数学期望。

Binary file not shown.

Before

Width:  |  Height:  |  Size: 64 KiB

After

Width:  |  Height:  |  Size: 64 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 71 KiB

After

Width:  |  Height:  |  Size: 71 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 63 KiB

After

Width:  |  Height:  |  Size: 63 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 60 KiB

After

Width:  |  Height:  |  Size: 60 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 66 KiB

After

Width:  |  Height:  |  Size: 65 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 46 KiB

After

Width:  |  Height:  |  Size: 49 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 66 KiB

After

Width:  |  Height:  |  Size: 65 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 67 KiB

After

Width:  |  Height:  |  Size: 67 KiB

View File

@ -203,7 +203,7 @@ $$
**时间复杂度也存在一定的局限性。** 比如,虽然算法 `A``C` 的时间复杂度相同,但是实际的运行时间有非常大的差别。再比如,虽然算法 `B``C` 的时间复杂度要更高,但在输入数据大小 $n$ 比较小时,算法 `B` 是要明显优于算法 `C` 的。即使存在这些问题,计算复杂度仍然是评判算法效率的最有效、最常用方法。
## 函数渐上界
## 函数渐上界
设算法「计算操作数量」为 $T(n)$ ,其是一个关于输入数据大小 $n$ 的函数。例如,以下算法的操作数量为
@ -284,34 +284,34 @@ $$
$T(n)$ 是个一次函数,说明时间增长趋势是线性的,因此易得时间复杂度是线性阶。
我们将线性阶的时间复杂度记为 $O(n)$ ,这个数学符号被称为「大 $O$ 记号 Big-$O$ Notation」代表函数 $T(n)$ 的「渐上界 asymptotic upper bound」。
我们将线性阶的时间复杂度记为 $O(n)$ ,这个数学符号被称为「大 $O$ 记号 Big-$O$ Notation」代表函数 $T(n)$ 的「渐上界 asymptotic upper bound」。
我们要推算时间复杂度,本质上是在计算「操作数量函数 $T(n)$ 」的渐进上界。下面我们先来看看函数渐进上界的数学定义。
我们要推算时间复杂度,本质上是在计算「操作数量函数 $T(n)$ 」的渐近上界。下面我们先来看看函数渐近上界的数学定义。
!!! abstract "函数渐上界"
!!! abstract "函数渐上界"
若存在正实数 $c$ 和实数 $n_0$ ,使得对于所有的 $n > n_0$ ,均有
$$
T(n) \leq c \cdot f(n)
$$
则可认为 $f(n)$ 给出了 $T(n)$ 的一个渐上界,记为
则可认为 $f(n)$ 给出了 $T(n)$ 的一个渐上界,记为
$$
T(n) = O(f(n))
$$
![asymptotic_upper_bound](time_complexity.assets/asymptotic_upper_bound.png)
<p align="center"> Fig. 函数的渐上界 </p>
<p align="center"> Fig. 函数的渐上界 </p>
本质上看,计算渐上界就是在找一个函数 $f(n)$ **使得在 $n$ 趋向于无穷大时,$T(n)$ 和 $f(n)$ 处于相同的增长级别(仅相差一个常数项 $c$ 的倍数)**。
本质上看,计算渐上界就是在找一个函数 $f(n)$ **使得在 $n$ 趋向于无穷大时,$T(n)$ 和 $f(n)$ 处于相同的增长级别(仅相差一个常数项 $c$ 的倍数)**。
!!! tip
上界的数学味儿有点重,如果你感觉没有完全理解,无需担心,因为在实际使用中我们只需要会推算即可,数学意义可以慢慢领悟。
上界的数学味儿有点重,如果你感觉没有完全理解,无需担心,因为在实际使用中我们只需要会推算即可,数学意义可以慢慢领悟。
## 推算方法
推算出 $f(n)$ 后,我们就得到时间复杂度 $O(f(n))$ 。那么,如何来确定渐进上界 $f(n)$ 呢?总体分为两步,首先「统计操作数量」,然后「判断渐进上界」。
推算出 $f(n)$ 后,我们就得到时间复杂度 $O(f(n))$ 。那么,如何来确定渐近上界 $f(n)$ 呢?总体分为两步,首先「统计操作数量」,然后「判断渐近上界」。
### 1. 统计操作数量
@ -416,7 +416,7 @@ $$
```
### 2. 判断渐上界
### 2. 判断渐上界
**时间复杂度由多项式 $T(n)$ 中最高阶的项来决定**。这是因为在 $n$ 趋于无穷大时,最高阶的项将处于主导作用,其它项的影响都可以被忽略。
@ -1330,7 +1330,7 @@ $$
- 当 `nums = [?, ?, ..., 1]`,即当末尾元素是 $1$ 时,则需完整遍历数组,此时达到 **最差时间复杂度 $O(n)$**
- 当 `nums = [1, ?, ?, ...]` ,即当首个数字为 $1$ 时,无论数组多长都不需要继续遍历,此时达到 **最佳时间复杂度 $\Omega(1)$**
「函数渐上界」使用大 $O$ 记号表示,代表「最差时间复杂度」。与之对应,「函数渐下界」用 $\Omega$ 记号Omega Notation来表示代表「最佳时间复杂度」。
「函数渐上界」使用大 $O$ 记号表示,代表「最差时间复杂度」。与之对应,「函数渐下界」用 $\Omega$ 记号Omega Notation来表示代表「最佳时间复杂度」。
=== "Java"

Binary file not shown.

Before

Width:  |  Height:  |  Size: 92 KiB

After

Width:  |  Height:  |  Size: 92 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 74 KiB

After

Width:  |  Height:  |  Size: 74 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 212 KiB

After

Width:  |  Height:  |  Size: 212 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 82 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 86 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 62 KiB

View File

@ -4,9 +4,34 @@ comments: true
# 哈希表
哈希表通过建立「键 Key」和「值 Value」之间的映射实现高效的元素查找。具体地查询操作(给定一个 Key 查询得到 Value时间复杂度为 $O(1)$ 。
哈希表通过建立「键 Key」和「值 Value」之间的映射实现高效的元素查找。具体地输入一个 Key ,在哈希表中查询并获取 Value 时间复杂度为 $O(1)$ 。
(图)
例如,给定一个包含 $n$ 个学生的数据库,每个学生有 "姓名 `name` ” 和 “学号 `id` ” 两项数据,希望实现一个查询功能:**输入一个学号,返回对应的姓名**,则可以使用哈希表实现。
![hash_map](hash_map.assets/hash_map.png)
<p align="center"> Fig. 哈希表抽象表示 </p>
## 哈希表优势
除了哈希表之外,还可以使用以下数据结构来实现上述查询功能:
- **无序数组:** 每个元素为 `[学号, 姓名]`
- **有序数组:** 将 `1.` 中的数组按照学号从小到大排序;
- **链表:** 每个结点的值为 `[学号, 姓名]`
- **二叉搜索树:** 每个结点的值为 `[学号, 姓名]` ,根据学号大小来构建树;
使用上述方法,各项操作的时间复杂度如下表所示(在此不做赘述,详解可见 [二叉搜索树章节](https://www.hello-algo.com/chapter_tree/binary_search_tree/#_6))。无论是查找元素、还是增删元素,哈希表的时间复杂度都是 $O(1)$ ,全面胜出!
<div class="center-table" markdown>
| | 无序数组 | 有序数组 | 链表 | 二叉搜索树 | 哈希表 |
| -------- | -------- | ----------- | ------ | ----------- | ------ |
| 查找元素 | $O(n)$ | $O(\log n)$ | $O(n)$ | $O(\log n)$ | $O(1)$ |
| 插入元素 | $O(1)$ | $O(n)$ | $O(1)$ | $O(\log n)$ | $O(1)$ |
| 删除元素 | $O(n)$ | $O(n)$ | $O(n)$ | $O(\log n)$ | $O(1)$ |
</div>
## 哈希表常用操作
@ -18,19 +43,19 @@ Map<Integer, String> map = new HashMap<>();
/* 添加操作 */
// 在哈希表中添加键值对 (key, value)
map.put(10001, "小哈");
map.put(10002, "小啰");
map.put(10003, "小算");
map.put(10004, "小法");
map.put(10005, "小哇");
map.put(12836, "小哈");
map.put(15937, "小啰");
map.put(16750, "小算");
map.put(13276, "小法");
map.put(10583, "小鸭");
/* 查询操作 */
// 向哈希表输入键 key ,得到值 value
String name = map.get(10002);
String name = map.get(15937);
/* 删除操作 */
// 在哈希表中删除键值对 (key, value)
map.remove(10005);
map.remove(10583);
```
遍历哈希表有三种方式,即 **遍历键值对、遍历键、遍历值**。
@ -51,28 +76,6 @@ for (String val: map.values()) {
}
```
## 哈希表优势
给定一个包含 $n$ 个学生的数据库,每个学生有 "姓名 `name` ” 和 “学号 `id` ” 两项数据,希望实现一个查询功能,即 **输入一个学号,返回对应的姓名**,那么可以使用哪些数据结构来存储呢?
- **无序数组:** 每个元素为 `[学号, 姓名]`
- **有序数组:** 将 `1.` 中的数组按照学号从小到大排序;
- **链表:** 每个结点的值为 `[学号, 姓名]`
- **二叉搜索树:** 每个结点的值为 `[学号, 姓名]` ,根据学号大小来构建树;
- **哈希表:** 以学号为 Key 、姓名为 Value 。
使用上述方法,各项操作的时间复杂度如下表所示(在此不做赘述,详解可见 [二叉搜索树章节](https://www.hello-algo.com/chapter_tree/binary_search_tree/#_6)**哈希表全面胜出!**
<div class="center-table" markdown>
| | 无序数组 | 有序数组 | 链表 | 二叉搜索树 | 哈希表 |
| ------------ | -------- | ----------- | ------ | ----------- | ------ |
| 查找指定元素 | $O(n)$ | $O(\log n)$ | $O(n)$ | $O(\log n)$ | $O(1)$ |
| 插入元素 | $O(1)$ | $O(n)$ | $O(1)$ | $O(\log n)$ | $O(1)$ |
| 删除元素 | $O(n)$ | $O(n)$ | $O(n)$ | $O(\log n)$ | $O(1)$ |
</div>
## 哈希函数
哈希表中存储元素的数据结构被称为「桶 Bucket」底层实现可能是数组、链表、二叉树红黑树或是它们的组合。
@ -87,10 +90,12 @@ for (String val: map.values()) {
以上述学生数据 `Key 学号 -> Value 姓名` 为例,我们可以将「哈希函数」设计为
$$
f(x) = x \% 10000
f(x) = x \% 100
$$
(图)
![hash_function](hash_map.assets/hash_function.png)
<p align="center"> Fig. 哈希函数 </p>
```java title="array_hash_map.java"
/* 键值对 int->String */
@ -107,16 +112,16 @@ class Entry {
class ArrayHashMap {
private List<Entry> bucket;
public ArrayHashMap() {
// 初始化一个长度为 10 的桶(数组)
// 初始化一个长度为 100 的桶(数组)
bucket = new ArrayList<>();
for (int i = 0; i < 10; i++) {
for (int i = 0; i < 100; i++) {
bucket.add(null);
}
}
/* 哈希函数 */
private int hashFunc(int key) {
int index = key % 10000;
int index = key % 100;
return index;
}
@ -146,8 +151,18 @@ class ArrayHashMap {
## 哈希冲突
细心的同学可能会发现,哈希函数 $f(x) = x \% 10000$ 会在某些情况下失效。例如,当输入的 Key 为 10001, 20001, 30001, ... 时,哈希函数的计算结果都是 1 ,指向同一个 Value ,表明不同学号指向了同一个人,这明显是不对的。
细心的同学可能会发现,**哈希函数 $f(x) = x \% 100$ 会在某些情况下失效**。具体地,当输入的 Key 后两位相同时,哈希函数的计算结果也相同,指向同一个 Value 。例如,分别查询两个学号 12836 和 20336 ,则有
$$
f(12836) = f(20336) = 36
$$
导致两个学号指向了同一个姓名,这明显是不对的。我们将这种现象称为「哈希冲突 Hash Collision」其会严重影响查询的正确性我们将如何避免哈希冲突的问题留在下章讨论。
上述现象被称为「哈希冲突 Hash Collision」其会严重影响查询的正确性我们将如何避免哈希冲突的问题留在下章讨论。
![hash_collision](hash_map.assets/hash_collision.png)
(图)
<p align="center"> Fig. 哈希冲突 </p>
综上所述,一个优秀的「哈希函数」应该具备以下特性:
- 尽量少地发生哈希冲突;
- 时间复杂度 $O(1)$ ,计算尽可能高效;
- 空间使用率高,即 “键值对占用空间 / 哈希表总占用空间” 尽可能大;

Binary file not shown.

Before

Width:  |  Height:  |  Size: 94 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 48 KiB

After

Width:  |  Height:  |  Size: 48 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 53 KiB

After

Width:  |  Height:  |  Size: 52 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 54 KiB

After

Width:  |  Height:  |  Size: 53 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 54 KiB

After

Width:  |  Height:  |  Size: 53 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 54 KiB

After

Width:  |  Height:  |  Size: 53 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 68 KiB

After

Width:  |  Height:  |  Size: 69 KiB

View File

@ -10,4 +10,4 @@ comments: true
<p align="center"> 力扣LeetCode全网阅读量最高博主 </p>
<p align="center"> 分享近百道算法题解,累积回复数千读者的评论问题 </p>
<p align="center"> 创作 LeetBook《图解算法数据结构》已免费售出 21 万本 </p>
<p align="center"> 创作 LeetBook《图解算法数据结构》已免费售出 22 万本 </p>

View File

@ -44,7 +44,7 @@ comments: true
首先介绍数据结构与算法的评价维度、算法效率的评估方法,引出了计算复杂度概念。
接下来,从 **函数渐上界** 入手,分别介绍了 **时间复杂度** 和 **空间复杂度** ,包括推算方法、常见类型、示例等。同时,剖析了 **最差、最佳、平均** 时间复杂度的联系与区别。
接下来,从 **函数渐上界** 入手,分别介绍了 **时间复杂度** 和 **空间复杂度** ,包括推算方法、常见类型、示例等。同时,剖析了 **最差、最佳、平均** 时间复杂度的联系与区别。
### 数据结构

Binary file not shown.

Before

Width:  |  Height:  |  Size: 80 KiB

After

Width:  |  Height:  |  Size: 80 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 51 KiB

After

Width:  |  Height:  |  Size: 51 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 52 KiB

After

Width:  |  Height:  |  Size: 52 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 62 KiB

After

Width:  |  Height:  |  Size: 62 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 52 KiB

After

Width:  |  Height:  |  Size: 52 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 62 KiB

After

Width:  |  Height:  |  Size: 62 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 52 KiB

After

Width:  |  Height:  |  Size: 52 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 62 KiB

After

Width:  |  Height:  |  Size: 62 KiB

View File

@ -123,7 +123,24 @@ $$
=== "Go"
```go title="binary_search.go"
/* 二分查找(左闭右开) */
func binarySearch1(nums []int, target int) int {
// 初始化左闭右开 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1
i, j := 0, len(nums)
// 循环,当搜索区间为空时跳出(当 i = j 时为空)
for i < j {
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 { // 找到目标元素,返回其索引
return m
}
}
// 未找到目标元素,返回 -1
return -1
}
```
=== "JavaScript"
@ -220,7 +237,24 @@ $$
=== "Go"
```go title="binary_search.go"
/* 二分查找(左闭右开) */
func binarySearch1(nums []int, target int) int {
// 初始化左闭右开 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1
i, j := 0, len(nums)
// 循环,当搜索区间为空时跳出(当 i = j 时为空)
for i < j {
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 { // 找到目标元素,返回其索引
return m
}
}
// 未找到目标元素,返回 -1
return -1
}
```
=== "JavaScript"
@ -294,7 +328,10 @@ $$
=== "Go"
```go title=""
// (i + j) 有可能超出 int 的取值范围
m := (i + j) / 2
// 更换为此写法则不会越界
m := i + (j - i) / 2
```
=== "JavaScript"

Binary file not shown.

Before

Width:  |  Height:  |  Size: 62 KiB

After

Width:  |  Height:  |  Size: 63 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 71 KiB

After

Width:  |  Height:  |  Size: 71 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 75 KiB

After

Width:  |  Height:  |  Size: 75 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 48 KiB

After

Width:  |  Height:  |  Size: 48 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 42 KiB

After

Width:  |  Height:  |  Size: 42 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 42 KiB

After

Width:  |  Height:  |  Size: 42 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 42 KiB

After

Width:  |  Height:  |  Size: 42 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 42 KiB

After

Width:  |  Height:  |  Size: 42 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 42 KiB

After

Width:  |  Height:  |  Size: 42 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 41 KiB

After

Width:  |  Height:  |  Size: 42 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 78 KiB

After

Width:  |  Height:  |  Size: 80 KiB

View File

@ -6,7 +6,7 @@ comments: true
「冒泡排序 Bubble Sort」是一种最基础的排序算法非常适合作为第一个学习的排序算法。顾名思义「冒泡」是该算法的核心操作。
!!! tip "为什么叫 “冒泡”"
!!! question "为什么叫 “冒泡”"
在水中,越大的泡泡浮力越大,所以最大的泡泡会最先浮到水面。
@ -112,7 +112,19 @@ comments: true
=== "Go"
```go title="bubble_sort.go"
/* 冒泡排序 */
func bubbleSort(nums []int) {
// 外循环:待排序元素数量为 n-1, n-2, ..., 1
for i := len(nums) - 1; i > 0; i-- {
// 内循环:冒泡操作
for j := 0; j < i; j++ {
if nums[j] > nums[j+1] {
// 交换 nums[j] 与 nums[j + 1]
nums[j], nums[j+1] = nums[j+1], nums[j]
}
}
}
}
```
=== "JavaScript"
@ -239,7 +251,24 @@ comments: true
=== "Go"
```go title="bubble_sort.go"
/* 冒泡排序(标志优化)*/
func bubbleSortWithFlag(nums []int) {
// 外循环:待排序元素数量为 n-1, n-2, ..., 1
for i := len(nums) - 1; i > 0; i-- {
flag := false // 初始化标志位
// 内循环:冒泡操作
for j := 0; j < i; j++ {
if nums[j] > nums[j+1] {
// 交换 nums[j] 与 nums[j + 1]
nums[j], nums[j+1] = nums[j+1], nums[j]
flag = true // 记录交换元素
}
}
if flag == false { // 此轮冒泡未交换任何元素,直接跳出
break
}
}
}
```
=== "JavaScript"

Binary file not shown.

Before

Width:  |  Height:  |  Size: 79 KiB

After

Width:  |  Height:  |  Size: 79 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 77 KiB

After

Width:  |  Height:  |  Size: 77 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 107 KiB

After

Width:  |  Height:  |  Size: 107 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 52 KiB

After

Width:  |  Height:  |  Size: 52 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 61 KiB

After

Width:  |  Height:  |  Size: 61 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 58 KiB

After

Width:  |  Height:  |  Size: 58 KiB

View File

@ -20,13 +20,13 @@ comments: true
<div class="center-table" markdown>
| 方法 | 描述 |
| --------- | ------------------------ |
| offer() | 元素入队,即将元素添加至队尾 |
| poll() | 队首元素出队 |
| front() | 访问队首元素 |
| size() | 获取队列的长度 |
| isEmpty() | 判断队列是否为空 |
| 方法 | 描述 |
| --------- | ---------------------------- |
| offer() | 元素入队,即将元素添加至队尾 |
| poll() | 队首元素出队 |
| front() | 访问队首元素 |
| size() | 获取队列的长度 |
| isEmpty() | 判断队列是否为空 |
</div>
@ -143,13 +143,59 @@ comments: true
=== "JavaScript"
```js title="queue.js"
/* 初始化队列 */
// JavaScript 没有内置的队列,可以把 Array 当作队列来使用
// 注意:由于是数组,所以 shift() 的时间复杂度是 O(n)
const queue = [];
/* 元素入队 */
queue.push(1);
queue.push(3);
queue.push(2);
queue.push(5);
queue.push(4);
/* 访问队首元素 */
const peek = queue[0];
/* 元素出队 */
// O(n)
const poll = queue.shift();
/* 获取队列的长度 */
const size = queue.length;
/* 判断队列是否为空 */
const empty = queue.length === 0;
```
=== "TypeScript"
```typescript title="queue.ts"
/* 初始化队列 */
// TypeScript 没有内置的队列,可以把 Array 当作队列来使用
// 注意:由于是数组,所以 shift() 的时间复杂度是 O(n)
const queue: number[] = [];
/* 元素入队 */
queue.push(1);
queue.push(3);
queue.push(2);
queue.push(5);
queue.push(4);
/* 访问队首元素 */
const peek = queue[0];
/* 元素出队 */
// O(n)
const poll = queue.shift();
/* 获取队列的长度 */
const size = queue.length;
/* 判断队列是否为空 */
const empty = queue.length === 0;
```
=== "C"

Binary file not shown.

Before

Width:  |  Height:  |  Size: 56 KiB

After

Width:  |  Height:  |  Size: 56 KiB

View File

@ -397,13 +397,13 @@ comments: true
=== "JavaScript"
```js title="linkedlist_stack.js"
```
=== "TypeScript"
```typescript title="linkedlist_stack.ts"
```
=== "C"
@ -587,13 +587,93 @@ comments: true
=== "JavaScript"
```js title="array_stack.js"
/* 基于数组实现的栈 */
class ArrayStack {
stack;
constructor() {
this.stack = [];
}
/* 获取栈的长度 */
get size() {
return this.stack.length;
}
/* 判断栈是否为空 */
empty() {
return this.stack.length === 0;
}
/* 入栈 */
push(num) {
this.stack.push(num);
}
/* 出栈 */
pop() {
return this.stack.pop();
}
/* 访问栈顶元素 */
top() {
return this.stack[this.stack.length - 1];
}
/* 访问索引 index 处元素 */
get(index) {
return this.stack[index];
}
/* 返回 Array */
toArray() {
return this.stack;
}
};
```
=== "TypeScript"
```typescript title="array_stack.ts"
/* 基于数组实现的栈 */
class ArrayStack {
private stack: number[];
constructor() {
this.stack = [];
}
/* 获取栈的长度 */
get size(): number {
return this.stack.length;
}
/* 判断栈是否为空 */
empty(): boolean {
return this.stack.length === 0;
}
/* 入栈 */
push(num: number): void {
this.stack.push(num);
}
/* 出栈 */
pop(): number | undefined {
return this.stack.pop();
}
/* 访问栈顶元素 */
top(): number | undefined {
return this.stack[this.stack.length - 1];
}
/* 访问索引 index 处元素 */
get(index: number): number | undefined {
return this.stack[index];
}
/* 返回 Array */
toArray() {
return this.stack;
}
};
```
=== "C"

Binary file not shown.

Before

Width:  |  Height:  |  Size: 51 KiB

After

Width:  |  Height:  |  Size: 51 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 66 KiB

After

Width:  |  Height:  |  Size: 66 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 91 KiB

After

Width:  |  Height:  |  Size: 90 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 90 KiB

After

Width:  |  Height:  |  Size: 90 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 95 KiB

After

Width:  |  Height:  |  Size: 94 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 60 KiB

After

Width:  |  Height:  |  Size: 60 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 84 KiB

After

Width:  |  Height:  |  Size: 84 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 88 KiB

After

Width:  |  Height:  |  Size: 87 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 94 KiB

After

Width:  |  Height:  |  Size: 93 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 56 KiB

After

Width:  |  Height:  |  Size: 56 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 57 KiB

After

Width:  |  Height:  |  Size: 57 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 59 KiB

After

Width:  |  Height:  |  Size: 59 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 60 KiB

After

Width:  |  Height:  |  Size: 60 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 76 KiB

After

Width:  |  Height:  |  Size: 76 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 59 KiB

After

Width:  |  Height:  |  Size: 59 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 67 KiB

After

Width:  |  Height:  |  Size: 67 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 75 KiB

After

Width:  |  Height:  |  Size: 75 KiB

Some files were not shown because too many files have changed in this diff Show More