diff --git a/chapter_appendix/terminology/index.html b/chapter_appendix/terminology/index.html index abcdbeb47..58cb97941 100644 --- a/chapter_appendix/terminology/index.html +++ b/chapter_appendix/terminology/index.html @@ -3435,7 +3435,7 @@
表 16-1 列出了书中出现的重要术语。建议你同时记住它们的中英文叫法,以便阅读英文文献。
+表 16-1 列出了书中出现的重要术语。建议读者同时记住它们的中英文叫法,以便阅读英文文献。
表 16-1 数据结构与算法的重要名词
「列表 list」是一个抽象的数据结构概念,它表示元素的有序集合,支持元素访问、修改、添加、删除和遍历等操作,无须使用者考虑容量限制的问题。列表可以基于链表或数组实现。
当使用数组实现列表时,长度不可变的性质会导致列表的实用性降低。这是因为我们通常无法事先确定需要存储多少数据,从而难以选择合适的列表长度。若长度过小,则很可能无法满足使用需求;若长度过大,则会造成内存空间的浪费。
+当使用数组实现列表时,长度不可变的性质会导致列表的实用性降低。这是因为我们通常无法事先确定需要存储多少数据,从而难以选择合适的列表长度。若长度过小,则很可能无法满足使用需求;若长度过大,则会造成内存空间浪费。
为解决此问题,我们可以使用「动态数组 dynamic array」来实现列表。它继承了数组的各项优点,并且可以在程序运行过程中进行动态扩容。
-实际上,许多编程语言中的标准库提供的列表都是基于动态数组实现的,例如 Python 中的 list
、Java 中的 ArrayList
、C++ 中的 vector
和 C# 中的 List
等。在接下来的讨论中,我们将把“列表”和“动态数组”视为等同的概念。
实际上,许多编程语言中的标准库提供的列表是基于动态数组实现的,例如 Python 中的 list
、Java 中的 ArrayList
、C++ 中的 vector
和 C# 中的 List
等。在接下来的讨论中,我们将把“列表”和“动态数组”视为等同的概念。
我们通常使用“无初始值”和“有初始值”这两种初始化方法:
diff --git a/chapter_array_and_linkedlist/ram_and_cache/index.html b/chapter_array_and_linkedlist/ram_and_cache/index.html index d3742471f..ef4651f73 100644 --- a/chapter_array_and_linkedlist/ram_and_cache/index.html +++ b/chapter_array_and_linkedlist/ram_and_cache/index.html @@ -3535,10 +3535,10 @@在本章的前两节中,我们探讨了数组和链表这两种基础且重要的数据结构,它们分别代表了“连续存储”和“分散存储”这两种不同的物理结构。
+在本章的前两节中,我们探讨了数组和链表这两种基础且重要的数据结构,它们分别代表了“连续存储”和“分散存储”两种物理结构。
实际上,物理结构在很大程度上决定了程序对内存和缓存的使用效率,进而影响算法程序的整体性能。
计算机中包括三种不同类型的存储设备:「硬盘 hard disk」、「内存 random-access memory, RAM」、「缓存 cache memory」。表 4-2 展示了它们在计算机系统中的不同角色和性能特点。
+计算机中包括三种类型的存储设备:「硬盘 hard disk」、「内存 random-access memory, RAM」、「缓存 cache memory」。表 4-2 展示了它们在计算机系统中的不同角色和性能特点。
表 4-2 计算机的存储设备
我们可以将计算机存储系统想象为图 4-9 所示的金字塔结构。越靠近金字塔顶端的存储设备的速度越快、容量越小、成本越高。这种多层级的设计并非偶然,而是计算机科学家和工程师们经过深思熟虑的结果。
Note
-计算机的存储层次结构体现了速度、容量和成本三者之间的精妙平衡。实际上,这种权衡普遍存在于所有工业领域,它要求我们在不同的优势和限制之间找到最佳的平衡点。
+计算机的存储层次结构体现了速度、容量和成本三者之间的精妙平衡。实际上,这种权衡普遍存在于所有工业领域,它要求我们在不同的优势和限制之间找到最佳平衡点。
总的来说,硬盘用于长期存储大量数据,内存用于临时存储程序运行中正在处理的数据,而缓存则用于存储经常访问的数据和指令,以提高程序运行效率。这三者共同协作,确保计算机系统的高效运行。
-如图 4-10 所示,在程序运行时,数据会从硬盘中被读取到内存中,供给 CPU 计算使用。缓存可以看作 CPU 的一部分,它通过智能地从内存加载数据,给 CPU 提供高速的数据读取,从而显著提升程序的执行效率,减少对较慢的内存的依赖。
+总的来说,硬盘用于长期存储大量数据,内存用于临时存储程序运行中正在处理的数据,而缓存则用于存储经常访问的数据和指令,以提高程序运行效率。三者共同协作,确保计算机系统高效运行。
+如图 4-10 所示,在程序运行时,数据会从硬盘中被读取到内存中,供 CPU 计算使用。缓存可以看作 CPU 的一部分,它通过智能地从内存加载数据,给 CPU 提供高速的数据读取,从而显著提升程序的执行效率,减少对较慢的内存的依赖。
图 4-10 硬盘、内存和缓存之间的数据流通
在内存空间利用方面,数组和链表具有各自的优势和局限。
-一方面,内存是有限的,且同一块内存不能被多个程序共享,因此我们希望数据结构能够尽可能高效地利用空间。数组的元素紧密排列,不需要额外的空间来存储链表节点间的引用(指针),因此空间效率更高。然而,数组需要一次性分配足够的连续内存空间,这可能导致内存的浪费,数组扩容也需要额外的时间和空间成本。相比之下,链表以“节点”为单位进行动态内存分配和回收,这种方式提供了更大的灵活性。
+在内存空间利用方面,数组和链表各自具有优势和局限性。
+一方面,内存是有限的,且同一块内存不能被多个程序共享,因此我们希望数据结构能够尽可能高效地利用空间。数组的元素紧密排列,不需要额外的空间来存储链表节点间的引用(指针),因此空间效率更高。然而,数组需要一次性分配足够的连续内存空间,这可能导致内存浪费,数组扩容也需要额外的时间和空间成本。相比之下,链表以“节点”为单位进行动态内存分配和回收,提供了更大的灵活性。
另一方面,在程序运行时,随着反复申请与释放内存,空闲内存的碎片化程度会越来越高,从而导致内存的利用效率降低。数组由于其连续的存储方式,相对不容易导致内存碎片化。相反,链表的元素是分散存储的,在频繁的插入与删除操作中,更容易导致内存碎片化。
缓存虽然在空间容量上远小于内存,但它比内存快得多,在程序执行速度上起着至关重要的作用。由于缓存的容量有限,它只能存储一小部分频繁访问的数据。因此,当 CPU 尝试访问的数据不在缓存中时,就会发生「缓存未命中 cache miss」,此时 CPU 不得不从速度较慢的内存中加载所需数据。
+缓存虽然在空间容量上远小于内存,但它比内存快得多,在程序执行速度上起着至关重要的作用。由于缓存的容量有限,只能存储一小部分频繁访问的数据,因此当 CPU 尝试访问的数据不在缓存中时,就会发生「缓存未命中 cache miss」,此时 CPU 不得不从速度较慢的内存中加载所需数据。
显然,“缓存未命中”越少,CPU 读写数据的效率就越高,程序性能也就越好。我们将 CPU 从缓存中成功获取数据的比例称为「缓存命中率 cache hit rate」,这个指标通常用来衡量缓存效率。
-为了尽可能达到更高效率,缓存会采取以下数据加载机制。
+为了尽可能达到更高的效率,缓存会采取以下数据加载机制。
实际上,数组和链表对缓存的利用效率也是不同的,主要体现在以下几个方面。
+实际上,数组和链表对缓存的利用效率是不同的,主要体现在以下几个方面。
总体而言,数组具有更高的缓存命中率,因此它在操作效率上通常优于链表。这使得在解决算法问题时,基于数组实现的数据结构往往更受欢迎。
需要注意的是,高缓存效率并不意味着数组在所有情况下都优于链表。实际应用中选择哪种数据结构,应根据具体需求来决定。例如,数组和链表都可以实现“栈”数据结构(下一章会详细介绍),但它们适用于不同场景。
数组存储在栈上和存储在堆上,对时间效率和空间效率是否有影响?
-存储在栈上和堆上的数组都被存储在连续内存空间内,数据操作效率是基本一致的。然而,栈和堆具有各自的特点,从而导致以下不同点。
+存储在栈上和堆上的数组都被存储在连续内存空间内,数据操作效率基本一致。然而,栈和堆具有各自的特点,从而导致以下不同点。
在删除节点中,需要断开该节点与其后继节点之间的引用指向吗?
-从数据结构与算法(做题)的角度看,不断开没有关系,只要保证程序的逻辑是正确的就行。从标准库的角度看,断开更加安全、逻辑更加清晰。如果不断开,假设被删除节点未被正常回收,那么它也会影响后继节点的内存回收。
+从数据结构与算法(做题)的角度看,不断开没有关系,只要保证程序的逻辑是正确的就行。从标准库的角度看,断开更加安全、逻辑更加清晰。如果不断开,假设被删除节点未被正常回收,那么它会影响后继节点的内存回收。
在算法中,重复执行某个任务是很常见的,其与复杂度分析息息相关。因此,在展开介绍时间复杂度和空间复杂度之前,我们先来了解如何在程序中实现重复执行任务,即两种基本的程序控制结构:迭代、递归。
+在算法中,重复执行某个任务是很常见的,它与复杂度分析息息相关。因此,在介绍时间复杂度和空间复杂度之前,我们先来了解如何在程序中实现重复执行任务,即两种基本的程序控制结构:迭代、递归。
「迭代 iteration」是一种重复执行某个任务的控制结构。在迭代中,程序会在满足一定的条件下重复执行某段代码,直到这个条件不再满足。
Tip
如果感觉以下内容理解困难,可以在读完“栈”章节后再来复习。
那么,迭代和递归具有什么内在联系呢?以上述的递归函数为例,求和操作在递归的“归”阶段进行。这意味着最初被调用的函数实际上是最后完成其求和操作的,这种工作机制与栈的“先入后出”原则是异曲同工的。
+那么,迭代和递归具有什么内在联系呢?以上述递归函数为例,求和操作在递归的“归”阶段进行。这意味着最初被调用的函数实际上是最后完成其求和操作的,这种工作机制与栈的“先入后出”原则异曲同工。
事实上,“调用栈”和“栈帧空间”这类递归术语已经暗示了递归与栈之间的密切关系。
因此,我们可以使用一个显式的栈来模拟调用栈的行为,从而将递归转化为迭代形式:
观察以上代码,当递归被转换为迭代后,代码变得更加复杂了。尽管迭代和递归在很多情况下可以互相转换,但也不一定值得这样做,有以下两点原因。
+观察以上代码,当递归转化为迭代后,代码变得更加复杂了。尽管迭代和递归在很多情况下可以互相转化,但不一定值得这样做,有以下两点原因。
总之,选择迭代还是递归取决于特定问题的性质。在编程实践中,权衡两者的优劣并根据情境选择合适的方法是至关重要的。
+总之,选择迭代还是递归取决于特定问题的性质。在编程实践中,权衡两者的优劣并根据情境选择合适的方法至关重要。
diff --git a/chapter_computational_complexity/space_complexity/index.html b/chapter_computational_complexity/space_complexity/index.html index 4048ca83b..b0407cbfb 100644 --- a/chapter_computational_complexity/space_complexity/index.html +++ b/chapter_computational_complexity/space_complexity/index.html @@ -5308,7 +5308,7 @@ O(1) < O(\log n) < O(n) < O(n^2) < O(2^n) \newline图 2-18 递归函数产生的平方阶空间复杂度
指数阶常见于二叉树。观察图 2-19 ,高度为 \(n\) 的“满二叉树”的节点数量为 \(2^n - 1\) ,占用 \(O(2^n)\) 空间:
+指数阶常见于二叉树。观察图 2-19 ,层数为 \(n\) 的“满二叉树”的节点数量为 \(2^n - 1\) ,占用 \(O(2^n)\) 空间:
请注意,表 3-1 针对的是 Java 的基本数据类型的情况。每种编程语言有各自的数据类型定义,它们的占用空间、取值范围和默认值可能会有所不同。
+请注意,表 3-1 针对的是 Java 的基本数据类型的情况。每种编程语言都有各自的数据类型定义,它们的占用空间、取值范围和默认值可能会有所不同。
int
可以是任意大小,只受限于可用内存;浮点数 float
是双精度 64 位;没有 char
类型,单个字符实际上是长度为 1 的字符串 str
。由于以上编程语言对字符数量的低估,它们不得不采取“代理对”的方式来表示超过 16 位长度的 Unicode 字符。这是一个不得已为之的无奈之举。一方面,包含代理对的字符串中,一个字符可能占用 2 字节或 4 字节,从而丧失了等长编码的优势。另一方面,处理代理对需要增加额外代码,这提高了编程的复杂性和调试难度。
出于以上原因,部分编程语言提出了一些不同的编码方案。
str
使用 Unicode 编码,并采用一种灵活的字符串表示,存储的字符长度取决于字符串中最大的 Unicode 码点。若字符串中全部是 ASCII 字符,则每个字符占用 1 个字节;如果有字符超出了 ASCII 范围,但全部在基本多语言平面(BMP)内,则每个字符占用 2 个字节;如果有超出 BMP 的字符,则每个字符占用 4 个字节。str
使用 Unicode 编码,并采用一种灵活的字符串表示,存储的字符长度取决于字符串中最大的 Unicode 码点。若字符串中全部是 ASCII 字符,则每个字符占用 1 字节;如果有字符超出了 ASCII 范围,但全部在基本多语言平面(BMP)内,则每个字符占用 2 字节;如果有超出 BMP 的字符,则每个字符占用 4 字节。string
类型在内部使用 UTF-8 编码。Go 语言还提供了 rune
类型,它用于表示单个 Unicode 码点。char
类型,用于表示单个 Unicode 码点。当算法程序运行时,正在处理的数据主要被存储在内存中。图 3-2 展示了一个计算机内存条,其中每个黑色方块都包含一块内存空间。我们可以将内存想象成一个巨大的 Excel 表格,其中每个单元格都可以存储一定大小的数据。
+当算法程序运行时,正在处理的数据主要存储在内存中。图 3-2 展示了一个计算机内存条,其中每个黑色方块都包含一块内存空间。我们可以将内存想象成一个巨大的 Excel 表格,其中每个单元格都可以存储一定大小的数据。
系统通过内存地址来访问目标位置的数据。如图 3-2 所示,计算机根据特定规则为表格中的每个单元格分配编号,确保每个内存空间都有唯一的内存地址。有了这些地址,程序便可以访问内存中的数据。
图 3-2 内存条、内存空间、内存地址
Tip
-值得说明的是,将内存比作 Excel 表格是一个简化的类比,实际内存的工作机制比较复杂,涉及到地址空间、内存管理、缓存机制、虚拟和物理内存等概念。
+值得说明的是,将内存比作 Excel 表格是一个简化的类比,实际内存的工作机制比较复杂,涉及地址空间、内存管理、缓存机制、虚拟内存和物理内存等概念。
内存是所有程序的共享资源,当某块内存被某个程序占用时,则无法被其他程序同时使用了。因此在数据结构与算法的设计中,内存资源是一个重要的考虑因素。比如,算法所占用的内存峰值不应超过系统剩余空闲内存;如果缺少连续大块的内存空间,那么所选用的数据结构必须能够存储在分散的内存空间内。
如图 3-3 所示,物理结构反映了数据在计算机内存中的存储方式,可分为连续空间存储(数组)和分散空间存储(链表)。物理结构从底层决定了数据的访问、更新、增删等操作方法,两种物理结构在时间效率和空间效率方面呈现出互补的特点。
diff --git a/chapter_data_structure/number_encoding/index.html b/chapter_data_structure/number_encoding/index.html index 0431c0839..f81375155 100644 --- a/chapter_data_structure/number_encoding/index.html +++ b/chapter_data_structure/number_encoding/index.html @@ -3525,7 +3525,7 @@图 3-4 原码、反码与补码之间的相互转换
-「原码 sign–magnitude」虽然最直观,但存在一些局限性。一方面,负数的原码不能直接用于运算。例如在原码下计算 \(1 + (-2)\) ,得到的结果是 \(-3\) ,这显然是不对的。
+「原码 sign-magnitude」虽然最直观,但存在一些局限性。一方面,负数的原码不能直接用于运算。例如在原码下计算 \(1 + (-2)\) ,得到的结果是 \(-3\) ,这显然是不对的。
为什么哈希表同时包含线性数据结构和非线性数据结构?
-哈希表底层是数组,而为了解决哈希冲突,我们可能会使用“链式地址”(后续哈希表章节会讲):数组中每个桶指向一个链表,当链表长度超过一定阈值时,又可能被转化为树(通常为红黑树)。 -从存储的角度来看,哈希表的底层是数组,其中每一个桶槽位可能包含一个值,也可能包含一个链表或树。因此,哈希表可能同时包含线性(数组、链表)和非线性(树)数据结构。
+哈希表底层是数组,而为了解决哈希冲突,我们可能会使用“链式地址”(后续“哈希冲突”章节会讲):数组中每个桶指向一个链表,当链表长度超过一定阈值时,又可能被转化为树(通常为红黑树)。 +从存储的角度来看,哈希表的底层是数组,其中每一个桶槽位可能包含一个值,也可能包含一个链表或一棵树。因此,哈希表可能同时包含线性数据结构(数组、链表)和非线性数据结构(树)。
char
类型的长度是 1 byte 吗?
char
类型的长度由编程语言采用的编码方法决定。例如,Java、JavaScript、TypeScript、C# 都采用 UTF-16 编码(保存 Unicode 码点),因此 char 类型的长度为 2 bytes 。
char
类型的长度由编程语言采用的编码方法决定。例如,Java、JavaScript、TypeScript、C# 都采用 UTF-16 编码(保存 Unicode 码点),因此 char 类型的长度为 2 bytes。
基于数组实现的数据结构也被称为“静态数据结构” 是否有歧义?因为栈也可以进行出栈和入栈等操作,这些操作都是“动态”的。
-栈确实可以实现动态的数据操作,但数据结构仍然是“静态”(长度不可变)的。尽管基于数组的数据结构可以动态地添加或删除元素,但它们的容量是固定的。如果数据量超出了预分配的大小,就需要创建一个新的更大的数组,并将老数组的内容复制到新数组中。
+基于数组实现的数据结构也称“静态数据结构” 是否有歧义?因为栈也可以进行出栈和入栈等操作,这些操作都是“动态”的。
+栈确实可以实现动态的数据操作,但数据结构仍然是“静态”(长度不可变)的。尽管基于数组的数据结构可以动态地添加或删除元素,但它们的容量是固定的。如果数据量超出了预分配的大小,就需要创建一个新的更大的数组,并将旧数组的内容复制到新数组中。
在构建栈(队列)的时候,未指定它的大小,为什么它们是“静态数据结构”呢?
-在高级编程语言中,我们无须人工指定栈(队列)的初始容量,这个工作是由类内部自动完成的。例如,Java 的 ArrayList 的初始容量通常为 10 。另外,扩容操作也是自动实现的。详见本书的“列表”章节。
+在高级编程语言中,我们无须人工指定栈(队列)的初始容量,这个工作由类内部自动完成。例如,Java 的 ArrayList 的初始容量通常为 10。另外,扩容操作也是自动实现的。详见后续的“列表”章节。
在该问题中,如果上一轮是跳 \(1\) 阶上来的,那么下一轮就必须跳 \(2\) 阶。这意味着,下一步选择不能由当前状态(当前所在楼梯阶数)独立决定,还和前一个状态(上轮所在楼梯阶数)有关。
不难发现,此问题已不满足无后效性,状态转移方程 \(dp[i] = dp[i-1] + dp[i-2]\) 也失效了,因为 \(dp[i-1]\) 代表本轮跳 \(1\) 阶,但其中包含了许多“上一轮是跳 \(1\) 阶上来的”方案,而为了满足约束,我们就不能将 \(dp[i-1]\) 直接计入 \(dp[i]\) 中。
-为此,我们需要扩展状态定义:状态 \([i, j]\) 表示处在第 \(i\) 阶并且上一轮跳了 \(j\) 阶,其中 \(j \in \{1, 2\}\) 。此状态定义有效地区分了上一轮跳了 \(1\) 阶还是 \(2\) 阶,我们可以据此来判断当前状态是从何而来的。
+为此,我们需要扩展状态定义:状态 \([i, j]\) 表示处在第 \(i\) 阶并且上一轮跳了 \(j\) 阶,其中 \(j \in \{1, 2\}\) 。此状态定义有效地区分了上一轮跳了 \(1\) 阶还是 \(2\) 阶,我们可以据此判断当前状态是从何而来的。
平方探测与线性探测类似,都是开放寻址的常见策略之一。当发生冲突时,平方探测不是简单地跳过一个固定的步数,而是跳过“探测次数的平方”的步数,即 \(1, 4, 9, \dots\) 步。
平方探测主要具有以下优势。
然而,平方探测也并不是完美的。
+然而,平方探测并不是完美的。
请注意,开放寻址(线性探测、平方探测和多次哈希)哈希表都存在“不能直接删除元素”的问题。
各个编程语言采取了不同的哈希表实现策略,以下举几个例子。
+各种编程语言采取了不同的哈希表实现策略,下面举几个例子。
哈希表的时间复杂度为什么不是 \(O(n)\) ?
+哈希表的时间复杂度在什么情况下是 \(O(n)\) ?
当哈希冲突比较严重时,哈希表的时间复杂度会退化至 \(O(n)\) 。当哈希函数设计得比较好、容量设置比较合理、冲突比较平均时,时间复杂度是 \(O(1)\) 。我们使用编程语言内置的哈希表时,通常认为时间复杂度是 \(O(1)\) 。
实际上,我们可以实现一种更为高效的建堆方法,共分为两步。
每当堆化一个节点后,以该节点为根节点的子树就形成一个合法的子堆。而由于是倒序遍历,因此堆是“自下而上”构建的。
之所以选择倒序遍历,是因为这样能够保证当前节点之下的子树已经是合法的子堆,这样堆化当前节点才是有效的。
-值得说明的是,叶节点没有子节点,天然就是合法的子堆,因此无须堆化。如以下代码所示,最后一个非叶节点是最后一个节点的父节点,我们从它开始倒序遍历并执行堆化。
+值得说明的是,由于叶节点没有子节点,因此它们天然就是合法的子堆,无须堆化。如以下代码所示,最后一个非叶节点是最后一个节点的父节点,我们从它开始倒序遍历并执行堆化:
撤销(undo)和反撤销(redo)具体是如何实现的?
-使用两个堆栈,栈 A
用于撤销,栈 B
用于反撤销。
使用两个栈,栈 A
用于撤销,栈 B
用于反撤销。
A
,并清空栈 B
。A
中弹出最近的操作,并将其压入栈 B
。\u52a8\u753b\u56fe\u89e3\u3001\u4e00\u952e\u8fd0\u884c\u7684\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u6559\u7a0b
\u5f00\u59cb\u9605\u8bfb \u4e0b\u8f7d\u4ee3\u7801 \u4e0b\u8f7d PDF
\u63a8\u8350\u8bed
Quote
\u201c\u4e00\u672c\u901a\u4fd7\u6613\u61c2\u7684\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u5165\u95e8\u4e66\uff0c\u5f15\u5bfc\u8bfb\u8005\u624b\u8111\u5e76\u7528\u5730\u5b66\u4e60\uff0c\u5f3a\u70c8\u63a8\u8350\u7b97\u6cd5\u521d\u5b66\u8005\u9605\u8bfb\u3002\u201d
\u2014\u2014 \u9093\u4fca\u8f89\uff0c\u6e05\u534e\u5927\u5b66\u8ba1\u7b97\u673a\u7cfb\u6559\u6388
Quote
\u201c\u5982\u679c\u6211\u5f53\u5e74\u5b66\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7684\u65f6\u5019\u6709\u300aHello \u7b97\u6cd5\u300b\uff0c\u5b66\u8d77\u6765\u5e94\u8be5\u4f1a\u7b80\u5355 10 \u500d\uff01\u201d
\u2014\u2014 \u674e\u6c90\uff0c\u4e9a\u9a6c\u900a\u8d44\u6df1\u9996\u5e2d\u79d1\u5b66\u5bb6
\u52a8\u753b\u56fe\u89e3\u5185\u5bb9\u6e05\u6670\u6613\u61c2\u5b66\u4e60\u66f2\u7ebf\u5e73\u6ed1
\"A picture is worth a thousand words.\"
\u201c\u4e00\u56fe\u80dc\u5343\u8a00\u201d
\u4e00\u952e\u8fd0\u884c\u5341\u4f59\u79cd\u7f16\u7a0b\u8bed\u8a00\u4ee3\u7801\u53ef\u76f4\u63a5\u8fd0\u884c
\"Talk is cheap. Show me the code.\"
\u201c\u5c11\u5439\u725b\uff0c\u770b\u4ee3\u7801\u201d
\u4e92\u52a9\u5b66\u4e60\u6b22\u8fce\u8ba8\u8bba\u4e0e\u63d0\u95ee\u8bfb\u8005\u95f4\u643a\u624b\u5171\u8fdb
\"\u8ffd\u98ce\u8d76\u6708\u83ab\u505c\u7559\"
\u201c\u5e73\u829c\u5c3d\u5904\u662f\u6625\u5c71\u201d
\u5e8f\u4e24\u5e74\u524d\uff0c\u6211\u5728\u529b\u6263\u4e0a\u5206\u4eab\u4e86\u201c\u5251\u6307 Offer\u201d\u7cfb\u5217\u9898\u89e3\uff0c\u53d7\u5230\u4e86\u8bb8\u591a\u540c\u5b66\u7684\u559c\u7231\u548c\u652f\u6301\u3002\u5728\u4e0e\u8bfb\u8005\u4ea4\u6d41\u671f\u95f4\uff0c\u6211\u6700\u5e38\u6536\u5230\u7684\u4e00\u4e2a\u95ee\u9898\u662f\u201c\u5982\u4f55\u5165\u95e8\u7b97\u6cd5\u201d\u3002\u9010\u6e10\u5730\uff0c\u6211\u5bf9\u8fd9\u4e2a\u95ee\u9898\u4ea7\u751f\u4e86\u6d53\u539a\u7684\u5174\u8da3\u3002
\u4e24\u773c\u4e00\u62b9\u9ed1\u5730\u5237\u9898\u4f3c\u4e4e\u662f\u6700\u53d7\u6b22\u8fce\u7684\u65b9\u6cd5\uff0c\u7b80\u5355\u76f4\u63a5\u4e14\u6709\u6548\u3002\u7136\u800c\u5237\u9898\u5c31\u5982\u540c\u73a9\u201c\u626b\u96f7\u201d\u6e38\u620f\uff0c\u81ea\u5b66\u80fd\u529b\u5f3a\u7684\u540c\u5b66\u80fd\u591f\u987a\u5229\u5c06\u5730\u96f7\u9010\u4e2a\u6392\u6389\uff0c\u800c\u57fa\u7840\u4e0d\u8db3\u7684\u540c\u5b66\u5f88\u53ef\u80fd\u88ab\u70b8\u7684\u6ee1\u5934\u662f\u5305\uff0c\u5e76\u5728\u632b\u6298\u4e2d\u6b65\u6b65\u9000\u7f29\u3002\u901a\u8bfb\u6559\u6750\u4e5f\u662f\u4e00\u79cd\u5e38\u89c1\u505a\u6cd5\uff0c\u4f46\u5bf9\u4e8e\u9762\u5411\u6c42\u804c\u7684\u540c\u5b66\u6765\u8bf4\uff0c\u6bd5\u4e1a\u5b63\u3001\u6295\u9012\u7b80\u5386\u3001\u51c6\u5907\u7b14\u8bd5\u9762\u8bd5\u5df2\u7ecf\u6d88\u8017\u4e86\u5927\u90e8\u5206\u7cbe\u529b\uff0c\u5543\u539a\u91cd\u7684\u4e66\u5f80\u5f80\u53d8\u6210\u4e86\u4e00\u9879\u8270\u5de8\u7684\u6311\u6218\u3002
\u5982\u679c\u4f60\u4e5f\u9762\u4e34\u7c7b\u4f3c\u7684\u56f0\u6270\uff0c\u90a3\u4e48\u5f88\u5e78\u8fd0\u8fd9\u672c\u4e66\u627e\u5230\u4e86\u4f60\u3002\u672c\u4e66\u662f\u6211\u5bf9\u8fd9\u4e2a\u95ee\u9898\u7ed9\u51fa\u7684\u7b54\u6848\uff0c\u5373\u4f7f\u4e0d\u662f\u6700\u4f18\u89e3\uff0c\u4e5f\u81f3\u5c11\u662f\u4e00\u6b21\u79ef\u6781\u7684\u5c1d\u8bd5\u3002\u672c\u4e66\u867d\u7136\u4e0d\u8db3\u4ee5\u8ba9\u4f60\u76f4\u63a5\u62ff\u5230 Offer \uff0c\u4f46\u4f1a\u5f15\u5bfc\u4f60\u63a2\u7d22\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7684\u201c\u77e5\u8bc6\u5730\u56fe\u201d\uff0c\u5e26\u4f60\u4e86\u89e3\u4e0d\u540c\u201c\u5730\u96f7\u201d\u7684\u5f62\u72b6\u3001\u5927\u5c0f\u548c\u5206\u5e03\u4f4d\u7f6e\uff0c\u8ba9\u4f60\u638c\u63e1\u5404\u79cd\u201c\u6392\u96f7\u65b9\u6cd5\u201d\u3002\u6709\u4e86\u8fd9\u4e9b\u672c\u9886\uff0c\u76f8\u4fe1\u4f60\u53ef\u4ee5\u66f4\u52a0\u81ea\u5982\u5730\u5237\u9898\u548c\u9605\u8bfb\u6587\u732e\uff0c\u9010\u6b65\u6784\u5efa\u8d77\u5b8c\u6574\u7684\u77e5\u8bc6\u4f53\u7cfb\u3002
\u6211\u6df1\u6df1\u8d5e\u540c\u8d39\u66fc\u6559\u6388\u6240\u8a00\uff1a\u201cKnowledge isn't free. You have to pay attention.\u201d\u4ece\u8fd9\u4e2a\u610f\u4e49\u4e0a\u770b\uff0c\u8fd9\u672c\u4e66\u5e76\u975e\u5b8c\u5168\u201c\u514d\u8d39\u201d\u3002\u4e3a\u4e86\u4e0d\u8f9c\u8d1f\u4f60\u4e3a\u672c\u4e66\u6240\u4ed8\u51fa\u7684\u5b9d\u8d35\u201c\u6ce8\u610f\u529b\u201d\uff0c\u6211\u4f1a\u5c3d\u6211\u6240\u80fd\uff0c\u6295\u5165\u6700\u5927\u7684\u201c\u6ce8\u610f\u529b\u201d\u6765\u5b8c\u6210\u8fd9\u672c\u4e66\u7684\u521b\u4f5c\u3002
\u4f5c\u8005\u7b80\u4ecb\u9773\u5b87\u680b (Krahets)\uff0c\u5927\u5382\u9ad8\u7ea7\u7b97\u6cd5\u5de5\u7a0b\u5e08\uff0c\u4e0a\u6d77\u4ea4\u901a\u5927\u5b66\u7855\u58eb\u3002\u529b\u6263\uff08LeetCode\uff09\u5168\u7f51\u9605\u8bfb\u91cf\u6700\u9ad8\u535a\u4e3b\uff0c\u53d1\u8868\u7684\u300a\u56fe\u89e3\u7b97\u6cd5\u6570\u636e\u7ed3\u6784\u300b\u5df2\u88ab\u8ba2\u9605 30 \u4e07\u672c\u3002
\u81f4\u8c22\u672c\u4e66\u5728\u5f00\u6e90\u793e\u533a\u4f17\u591a\u8d21\u732e\u8005\u7684\u5171\u540c\u52aa\u529b\u4e0b\u4e0d\u65ad\u5b8c\u5584\u3002\u611f\u8c22\u6bcf\u4e00\u4f4d\u6295\u5165\u65f6\u95f4\u4e0e\u7cbe\u529b\u7684\u64b0\u7a3f\u4eba\uff0c\u4ed6\u4eec\u662f\uff08\u6309\u7167 GitHub \u81ea\u52a8\u751f\u6210\u7684\u987a\u5e8f\u6392\u5217\uff09\uff1a
\u672c\u4e66\u7684\u4ee3\u7801\u5ba1\u9605\u5de5\u4f5c\u7531 Gonglja\u3001gvenusleo\u3001hpstory\u3001justin\u2010tse\u3001krahets\u3001night-cruise\u3001nuomi1\u3001Reanon \u548c sjinzh \u5b8c\u6210\uff08\u6309\u7167\u9996\u5b57\u6bcd\u987a\u5e8f\u6392\u5217\uff09\u3002\u611f\u8c22\u4ed6\u4eec\u4ed8\u51fa\u7684\u65f6\u95f4\u4e0e\u7cbe\u529b\uff0c\u6b63\u662f\u4ed6\u4eec\u786e\u4fdd\u4e86\u5404\u8bed\u8a00\u4ee3\u7801\u7684\u89c4\u8303\u4e0e\u7edf\u4e00\u3002
GongljaC, C++ gvenusleoDart hpstoryC# justin-tseJS, TS krahetsJava, Python night-cruiseRust nuomi1Swift ReanonGo, C sjinzhRust, Zig"},{"location":"chapter_appendix/","title":"\u7b2c 16 \u7ae0 \u00a0 \u9644\u5f55","text":""},{"location":"chapter_appendix/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"\u7531\u4e8e\u7b14\u8005\u80fd\u529b\u6709\u9650\uff0c\u4e66\u4e2d\u96be\u514d\u5b58\u5728\u4e00\u4e9b\u9057\u6f0f\u548c\u9519\u8bef\uff0c\u8bf7\u60a8\u8c05\u89e3\u3002\u5982\u679c\u60a8\u53d1\u73b0\u4e86\u7b14\u8bef\u3001\u94fe\u63a5\u5931\u6548\u3001\u5185\u5bb9\u7f3a\u5931\u3001\u6587\u5b57\u6b67\u4e49\u3001\u89e3\u91ca\u4e0d\u6e05\u6670\u6216\u884c\u6587\u7ed3\u6784\u4e0d\u5408\u7406\u7b49\u95ee\u9898\uff0c\u8bf7\u534f\u52a9\u6211\u4eec\u8fdb\u884c\u4fee\u6b63\uff0c\u4ee5\u7ed9\u8bfb\u8005\u63d0\u4f9b\u66f4\u4f18\u8d28\u7684\u5b66\u4e60\u8d44\u6e90\u3002
\u6240\u6709\u64b0\u7a3f\u4eba\u7684 GitHub ID \u5c06\u5728\u672c\u4e66\u4ed3\u5e93\u3001\u7f51\u9875\u7248\u548c PDF \u7248\u7684\u4e3b\u9875\u4e0a\u8fdb\u884c\u5c55\u793a\uff0c\u4ee5\u611f\u8c22\u4ed6\u4eec\u5bf9\u5f00\u6e90\u793e\u533a\u7684\u65e0\u79c1\u5949\u732e\u3002
\u5f00\u6e90\u7684\u9b45\u529b
\u7eb8\u8d28\u56fe\u4e66\u7684\u4e24\u6b21\u5370\u5237\u7684\u95f4\u9694\u65f6\u95f4\u5f80\u5f80\u8f83\u4e45\uff0c\u5185\u5bb9\u66f4\u65b0\u975e\u5e38\u4e0d\u65b9\u4fbf\u3002
\u800c\u5728\u672c\u5f00\u6e90\u4e66\u4e2d\uff0c\u5185\u5bb9\u66f4\u8fed\u7684\u65f6\u95f4\u88ab\u7f29\u77ed\u81f3\u6570\u65e5\u751a\u81f3\u51e0\u4e2a\u5c0f\u65f6\u3002
"},{"location":"chapter_appendix/contribution/#1","title":"1. \u00a0 \u5185\u5bb9\u5fae\u8c03","text":"\u5982\u56fe 16-3 \u6240\u793a\uff0c\u6bcf\u4e2a\u9875\u9762\u7684\u53f3\u4e0a\u89d2\u90fd\u6709\u201c\u7f16\u8f91\u56fe\u6807\u201d\u3002\u60a8\u53ef\u4ee5\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u4fee\u6539\u6587\u672c\u6216\u4ee3\u7801\u3002
\u56fe 16-3 \u00a0 \u9875\u9762\u7f16\u8f91\u6309\u952e
\u56fe\u7247\u65e0\u6cd5\u76f4\u63a5\u4fee\u6539\uff0c\u9700\u8981\u901a\u8fc7\u65b0\u5efa Issue \u6216\u8bc4\u8bba\u7559\u8a00\u6765\u63cf\u8ff0\u95ee\u9898\uff0c\u6211\u4eec\u4f1a\u5c3d\u5feb\u91cd\u65b0\u7ed8\u5236\u5e76\u66ff\u6362\u56fe\u7247\u3002
"},{"location":"chapter_appendix/contribution/#2","title":"2. \u00a0 \u5185\u5bb9\u521b\u4f5c","text":"\u5982\u679c\u60a8\u6709\u5174\u8da3\u53c2\u4e0e\u6b64\u5f00\u6e90\u9879\u76ee\uff0c\u5305\u62ec\u5c06\u4ee3\u7801\u7ffb\u8bd1\u6210\u5176\u4ed6\u7f16\u7a0b\u8bed\u8a00\u3001\u6269\u5c55\u6587\u7ae0\u5185\u5bb9\u7b49\uff0c\u90a3\u4e48\u9700\u8981\u5b9e\u65bd\u4ee5\u4e0b Pull Request \u5de5\u4f5c\u6d41\u7a0b\u3002
git clone
\u547d\u4ee4\u5c06\u4ed3\u5e93\u514b\u9686\u81f3\u672c\u5730\u3002\u5728 hello-algo
\u6839\u76ee\u5f55\u4e0b\uff0c\u6267\u884c\u4ee5\u4e0b Docker \u811a\u672c\uff0c\u5373\u53ef\u5728 http://localhost:8000
\u8bbf\u95ee\u672c\u9879\u76ee\uff1a
docker-compose up -d\n
\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5373\u53ef\u5220\u9664\u90e8\u7f72\uff1a
docker-compose down\n
"},{"location":"chapter_appendix/installation/","title":"16.1 \u00a0 \u7f16\u7a0b\u73af\u5883\u5b89\u88c5","text":""},{"location":"chapter_appendix/installation/#1611-ide","title":"16.1.1 \u00a0 \u5b89\u88c5 IDE","text":"\u63a8\u8350\u4f7f\u7528\u5f00\u6e90\u3001\u8f7b\u91cf\u7684 VSCode \u4f5c\u4e3a\u672c\u5730\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09\u3002\u8bbf\u95ee VSCode \u5b98\u7f51\uff0c\u6839\u636e\u64cd\u4f5c\u7cfb\u7edf\u9009\u62e9\u76f8\u5e94\u7248\u672c\u7684 VSCode \u8fdb\u884c\u4e0b\u8f7d\u548c\u5b89\u88c5\u3002
\u56fe 16-1 \u00a0 \u4ece\u5b98\u7f51\u4e0b\u8f7d VSCode
VSCode \u62e5\u6709\u5f3a\u5927\u7684\u6269\u5c55\u5305\u751f\u6001\u7cfb\u7edf\uff0c\u652f\u6301\u5927\u591a\u6570\u7f16\u7a0b\u8bed\u8a00\u7684\u8fd0\u884c\u548c\u8c03\u8bd5\u3002\u4ee5 Python \u4e3a\u4f8b\uff0c\u5b89\u88c5\u201cPython Extension Pack\u201d\u6269\u5c55\u5305\u4e4b\u540e\uff0c\u5373\u53ef\u8fdb\u884c Python \u4ee3\u7801\u8c03\u8bd5\u3002\u5b89\u88c5\u6b65\u9aa4\u5982\u56fe 16-2 \u6240\u793a\u3002
\u56fe 16-2 \u00a0 \u5b89\u88c5 VSCode \u6269\u5c55\u5305
"},{"location":"chapter_appendix/installation/#1612","title":"16.1.2 \u00a0 \u5b89\u88c5\u8bed\u8a00\u73af\u5883","text":""},{"location":"chapter_appendix/installation/#1-python","title":"1. \u00a0 Python \u73af\u5883","text":"python
\uff0c\u5b89\u88c5 Python Extension Pack \u3002pip install black
\uff0c\u5b89\u88c5\u4ee3\u7801\u683c\u5f0f\u5316\u5de5\u5177\u3002c++
\uff0c\u5b89\u88c5 C/C++ Extension Pack \u3002Clang_format_fallback Style
\u4ee3\u7801\u683c\u5f0f\u5316\u9009\u9879\uff0c\u8bbe\u7f6e\u4e3a { BasedOnStyle: Microsoft, BreakBeforeBraces: Attach }
\u3002java
\uff0c\u5b89\u88c5 Extension Pack for Java \u3002C# Dev Kit
\uff0c\u5b89\u88c5 C# Dev Kit \uff08\u914d\u7f6e\u6559\u7a0b\uff09\u3002go
\uff0c\u5b89\u88c5 Go \u3002Ctrl + Shift + P
\u547c\u51fa\u547d\u4ee4\u680f\uff0c\u8f93\u5165 go \uff0c\u9009\u62e9 Go: Install/Update Tools
\uff0c\u5168\u90e8\u52fe\u9009\u5e76\u5b89\u88c5\u5373\u53ef\u3002swift
\uff0c\u5b89\u88c5 Swift for Visual Studio Code \u3002javascript
\uff0c\u5b89\u88c5 JavaScript (ES6) code snippets \u3002Prettier
\uff0c\u5b89\u88c5\u4ee3\u7801\u683c\u5f0f\u5316\u5de5\u5177\u3002dart
\uff0c\u5b89\u88c5 Dart \u3002rust
\uff0c\u5b89\u88c5 rust-analyzer \u3002\u8868 16-1 \u5217\u51fa\u4e86\u4e66\u4e2d\u51fa\u73b0\u7684\u91cd\u8981\u672f\u8bed\u3002\u5efa\u8bae\u4f60\u540c\u65f6\u8bb0\u4f4f\u5b83\u4eec\u7684\u4e2d\u82f1\u6587\u53eb\u6cd5\uff0c\u4ee5\u4fbf\u9605\u8bfb\u82f1\u6587\u6587\u732e\u3002
\u8868 16-1 \u00a0 \u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7684\u91cd\u8981\u540d\u8bcd
\u4e2d\u6587 English \u4e2d\u6587 English \u7b97\u6cd5 algorithm \u5c42\u5e8f\u904d\u5386 level-order traversal \u6570\u636e\u7ed3\u6784 data structure \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 breadth-first traversal \u6e10\u8fd1\u590d\u6742\u5ea6\u5206\u6790 asymptotic complexity analysis \u6df1\u5ea6\u4f18\u5148\u904d\u5386 depth-first traversal \u65f6\u95f4\u590d\u6742\u5ea6 time complexity \u4e8c\u53c9\u641c\u7d22\u6811 binary search tree \u7a7a\u95f4\u590d\u6742\u5ea6 space complexity \u5e73\u8861\u4e8c\u53c9\u641c\u7d22\u6811 balanced binary search tree \u8fed\u4ee3 iteration \u5e73\u8861\u56e0\u5b50 balance factor \u9012\u5f52 recursion \u5806 heap \u5c3e\u9012\u5f52 tail recursion \u5927\u9876\u5806 max heap \u9012\u5f52\u6811 recursion tree \u5c0f\u9876\u5806 min heap \u5927 big- \u4f18\u5148\u961f\u5217 priority queue \u8bb0\u53f7 notation \u6e10\u8fd1\u4e0a\u754c asymptotic upper bound \u5806\u5316 heapify \u539f\u7801 sign\u2013magnitude \u56fe graph \u53cd\u7801 1\u2019s complement \u9876\u70b9 vertex \u8865\u7801 2\u2019s complement \u65e0\u5411\u56fe undirected graph \u6570\u7ec4 array \u6709\u5411\u56fe directed graph \u7d22\u5f15 index \u8fde\u901a\u56fe connected graph \u94fe\u8868 linked list \u975e\u8fde\u901a\u56fe disconnected graph \u94fe\u8868\u8282\u70b9 linked list node, list node \u6709\u6743\u56fe weighted graph \u5217\u8868 list \u90bb\u63a5 adjacency \u52a8\u6001\u6570\u7ec4 dynamic array \u8def\u5f84 path \u786c\u76d8 hard disk \u5165\u5ea6 in-degree \u5185\u5b58 random-access memory (RAM) \u51fa\u5ea6 out-degree \u7f13\u5b58 cache memory \u90bb\u63a5\u77e9\u9635 adjacency matrix \u7f13\u5b58\u672a\u547d\u4e2d cache miss \u90bb\u63a5\u8868 adjacency list \u7f13\u5b58\u547d\u4e2d\u7387 cache hit rate \u5e7f\u5ea6\u4f18\u5148\u641c\u7d22 breadth-first search \u6808 stack \u6df1\u5ea6\u4f18\u5148\u641c\u7d22 depth-first search \u961f\u5217 queue \u4e8c\u5206\u67e5\u627e binary search \u53cc\u5411\u961f\u5217 double-ended queue \u641c\u7d22\u7b97\u6cd5 searching algorithm \u54c8\u5e0c\u8868 hash table \u6392\u5e8f\u7b97\u6cd5 sorting algorithm \u6876 bucket \u9009\u62e9\u6392\u5e8f selection sort \u54c8\u5e0c\u51fd\u6570 hash function \u5192\u6ce1\u6392\u5e8f bubble sort \u54c8\u5e0c\u51b2\u7a81 hash collision \u63d2\u5165\u6392\u5e8f insertion sort \u8d1f\u8f7d\u56e0\u5b50 load factor \u5feb\u901f\u6392\u5e8f quick sort \u94fe\u5f0f\u5730\u5740 separate chaining \u5f52\u5e76\u6392\u5e8f merge sort \u5f00\u653e\u5bfb\u5740 open addressing \u5806\u6392\u5e8f heap sort \u7ebf\u6027\u63a2\u6d4b linear probing \u6876\u6392\u5e8f bucket sort \u61d2\u5220\u9664 lazy deletion \u8ba1\u6570\u6392\u5e8f counting sort \u4e8c\u53c9\u6811 binary tree \u57fa\u6570\u6392\u5e8f radix sort \u6811\u8282\u70b9 tree node \u5206\u6cbb divide and conquer \u5de6\u5b50\u8282\u70b9 left-child node \u6c49\u8bfa\u5854\u95ee\u9898 hanota problem \u53f3\u5b50\u8282\u70b9 right-child node \u56de\u6eaf\u7b97\u6cd5 backtracking algorithm \u7236\u8282\u70b9 parent node \u7ea6\u675f constraint \u5de6\u5b50\u6811 left subtree \u89e3 solution \u53f3\u5b50\u6811 right subtree \u72b6\u6001 state \u6839\u8282\u70b9 root node \u526a\u679d pruning \u53f6\u8282\u70b9 leaf node \u5168\u6392\u5217\u95ee\u9898 permutations problem \u8fb9 edge \u5b50\u96c6\u548c\u95ee\u9898 subset-sum problem \u5c42 level N \u7687\u540e\u95ee\u9898 N-queens problem \u5ea6 degree \u52a8\u6001\u89c4\u5212 dynamic programming \u9ad8\u5ea6 height \u521d\u59cb\u72b6\u6001 initial state \u6df1\u5ea6 depth \u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b state-trasition equation \u5b8c\u7f8e\u4e8c\u53c9\u6811 perfect binary tree \u80cc\u5305\u95ee\u9898 knapsack problem \u5b8c\u5168\u4e8c\u53c9\u6811 complete binary tree \u7f16\u8f91\u8ddd\u79bb\u95ee\u9898 edit distance problem \u5b8c\u6ee1\u4e8c\u53c9\u6811 full binary tree \u8d2a\u5fc3\u7b97\u6cd5 greedy algorithm \u5e73\u8861\u4e8c\u53c9\u6811 balanced binary tree AVL \u6811 AVL tree \u7ea2\u9ed1\u6811 red-black tree"},{"location":"chapter_array_and_linkedlist/","title":"\u7b2c 4 \u7ae0 \u00a0 \u6570\u7ec4\u4e0e\u94fe\u8868","text":"Abstract
\u6570\u636e\u7ed3\u6784\u7684\u4e16\u754c\u5982\u540c\u4e00\u5835\u539a\u5b9e\u7684\u7816\u5899\u3002
\u6570\u7ec4\u7684\u7816\u5757\u6574\u9f50\u6392\u5217\uff0c\u9010\u4e2a\u7d27\u8d34\u3002\u94fe\u8868\u7684\u7816\u5757\u5206\u6563\u5404\u5904\uff0c\u8fde\u63a5\u7684\u85e4\u8513\u81ea\u7531\u5730\u7a7f\u68ad\u4e8e\u7816\u7f1d\u4e4b\u95f4\u3002
"},{"location":"chapter_array_and_linkedlist/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"\u300c\u6570\u7ec4 array\u300d\u662f\u4e00\u79cd\u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff0c\u5176\u5c06\u76f8\u540c\u7c7b\u578b\u7684\u5143\u7d20\u5b58\u50a8\u5728\u8fde\u7eed\u7684\u5185\u5b58\u7a7a\u95f4\u4e2d\u3002\u6211\u4eec\u5c06\u5143\u7d20\u5728\u6570\u7ec4\u4e2d\u7684\u4f4d\u7f6e\u79f0\u4e3a\u8be5\u5143\u7d20\u7684\u300c\u7d22\u5f15 index\u300d\u3002\u56fe 4-1 \u5c55\u793a\u4e86\u6570\u7ec4\u7684\u4e3b\u8981\u672f\u8bed\u548c\u6982\u5ff5\u3002
\u56fe 4-1 \u00a0 \u6570\u7ec4\u5b9a\u4e49\u4e0e\u5b58\u50a8\u65b9\u5f0f
"},{"location":"chapter_array_and_linkedlist/array/#411","title":"4.1.1 \u00a0 \u6570\u7ec4\u5e38\u7528\u64cd\u4f5c","text":""},{"location":"chapter_array_and_linkedlist/array/#1","title":"1. \u00a0 \u521d\u59cb\u5316\u6570\u7ec4","text":"\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u9009\u7528\u6570\u7ec4\u7684\u4e24\u79cd\u521d\u59cb\u5316\u65b9\u5f0f\uff1a\u65e0\u521d\u59cb\u503c\u3001\u7ed9\u5b9a\u521d\u59cb\u503c\u3002\u5728\u672a\u6307\u5b9a\u521d\u59cb\u503c\u7684\u60c5\u51b5\u4e0b\uff0c\u5927\u591a\u6570\u7f16\u7a0b\u8bed\u8a00\u4f1a\u5c06\u6570\u7ec4\u5143\u7d20\u521d\u59cb\u5316\u4e3a \\(0\\) \uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig array.py# \u521d\u59cb\u5316\u6570\u7ec4\narr: list[int] = [0] * 5 # [ 0, 0, 0, 0, 0 ]\nnums: list[int] = [1, 3, 2, 5, 4] \n
array.cpp/* \u521d\u59cb\u5316\u6570\u7ec4 */\n// \u5b58\u50a8\u5728\u6808\u4e0a\nint arr[5];\nint nums[5] = { 1, 3, 2, 5, 4 };\n// \u5b58\u50a8\u5728\u5806\u4e0a\uff08\u9700\u8981\u624b\u52a8\u91ca\u653e\u7a7a\u95f4\uff09\nint* arr1 = new int[5];\nint* nums1 = new int[5] { 1, 3, 2, 5, 4 };\n
array.java/* \u521d\u59cb\u5316\u6570\u7ec4 */\nint[] arr = new int[5]; // { 0, 0, 0, 0, 0 }\nint[] nums = { 1, 3, 2, 5, 4 };\n
array.cs/* \u521d\u59cb\u5316\u6570\u7ec4 */\nint[] arr = new int[5]; // { 0, 0, 0, 0, 0 }\nint[] nums = [1, 3, 2, 5, 4];\n
array.go/* \u521d\u59cb\u5316\u6570\u7ec4 */\nvar arr [5]int\n// \u5728 Go \u4e2d\uff0c\u6307\u5b9a\u957f\u5ea6\u65f6\uff08[5]int\uff09\u4e3a\u6570\u7ec4\uff0c\u4e0d\u6307\u5b9a\u957f\u5ea6\u65f6\uff08[]int\uff09\u4e3a\u5207\u7247\n// \u7531\u4e8e Go \u7684\u6570\u7ec4\u88ab\u8bbe\u8ba1\u4e3a\u5728\u7f16\u8bd1\u671f\u786e\u5b9a\u957f\u5ea6\uff0c\u56e0\u6b64\u53ea\u80fd\u4f7f\u7528\u5e38\u91cf\u6765\u6307\u5b9a\u957f\u5ea6\n// \u4e3a\u4e86\u65b9\u4fbf\u5b9e\u73b0\u6269\u5bb9 extend() \u65b9\u6cd5\uff0c\u4ee5\u4e0b\u5c06\u5207\u7247\uff08Slice\uff09\u770b\u4f5c\u6570\u7ec4\uff08Array\uff09\nnums := []int{1, 3, 2, 5, 4}\n
array.swift/* \u521d\u59cb\u5316\u6570\u7ec4 */\nlet arr = Array(repeating: 0, count: 5) // [0, 0, 0, 0, 0]\nlet nums = [1, 3, 2, 5, 4]\n
array.js/* \u521d\u59cb\u5316\u6570\u7ec4 */\nvar arr = new Array(5).fill(0);\nvar nums = [1, 3, 2, 5, 4];\n
array.ts/* \u521d\u59cb\u5316\u6570\u7ec4 */\nlet arr: number[] = new Array(5).fill(0);\nlet nums: number[] = [1, 3, 2, 5, 4];\n
array.dart/* \u521d\u59cb\u5316\u6570\u7ec4 */\nList<int> arr = List.filled(5, 0); // [0, 0, 0, 0, 0]\nList<int> nums = [1, 3, 2, 5, 4];\n
array.rs/* \u521d\u59cb\u5316\u6570\u7ec4 */\nlet arr: Vec<i32> = vec![0; 5]; // [0, 0, 0, 0, 0]\nlet nums: Vec<i32> = vec![1, 3, 2, 5, 4];\n
array.c/* \u521d\u59cb\u5316\u6570\u7ec4 */\nint arr[5] = { 0 }; // { 0, 0, 0, 0, 0 }\nint nums[5] = { 1, 3, 2, 5, 4 };\n
array.zig// \u521d\u59cb\u5316\u6570\u7ec4\nvar arr = [_]i32{0} ** 5; // { 0, 0, 0, 0, 0 }\nvar nums = [_]i32{ 1, 3, 2, 5, 4 };\n
"},{"location":"chapter_array_and_linkedlist/array/#2","title":"2. \u00a0 \u8bbf\u95ee\u5143\u7d20","text":"\u6570\u7ec4\u5143\u7d20\u88ab\u5b58\u50a8\u5728\u8fde\u7eed\u7684\u5185\u5b58\u7a7a\u95f4\u4e2d\uff0c\u8fd9\u610f\u5473\u7740\u8ba1\u7b97\u6570\u7ec4\u5143\u7d20\u7684\u5185\u5b58\u5730\u5740\u975e\u5e38\u5bb9\u6613\u3002\u7ed9\u5b9a\u6570\u7ec4\u5185\u5b58\u5730\u5740\uff08\u9996\u5143\u7d20\u5185\u5b58\u5730\u5740\uff09\u548c\u67d0\u4e2a\u5143\u7d20\u7684\u7d22\u5f15\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u56fe 4-2 \u6240\u793a\u7684\u516c\u5f0f\u8ba1\u7b97\u5f97\u5230\u8be5\u5143\u7d20\u7684\u5185\u5b58\u5730\u5740\uff0c\u4ece\u800c\u76f4\u63a5\u8bbf\u95ee\u8be5\u5143\u7d20\u3002
\u56fe 4-2 \u00a0 \u6570\u7ec4\u5143\u7d20\u7684\u5185\u5b58\u5730\u5740\u8ba1\u7b97
\u89c2\u5bdf\u56fe 4-2 \uff0c\u6211\u4eec\u53d1\u73b0\u6570\u7ec4\u9996\u4e2a\u5143\u7d20\u7684\u7d22\u5f15\u4e3a \\(0\\) \uff0c\u8fd9\u4f3c\u4e4e\u6709\u4e9b\u53cd\u76f4\u89c9\uff0c\u56e0\u4e3a\u4ece \\(1\\) \u5f00\u59cb\u8ba1\u6570\u4f1a\u66f4\u81ea\u7136\u3002\u4f46\u4ece\u5730\u5740\u8ba1\u7b97\u516c\u5f0f\u7684\u89d2\u5ea6\u770b\uff0c\u7d22\u5f15\u672c\u8d28\u4e0a\u662f\u5185\u5b58\u5730\u5740\u7684\u504f\u79fb\u91cf\u3002\u9996\u4e2a\u5143\u7d20\u7684\u5730\u5740\u504f\u79fb\u91cf\u662f \\(0\\) \uff0c\u56e0\u6b64\u5b83\u7684\u7d22\u5f15\u4e3a \\(0\\) \u662f\u5408\u7406\u7684\u3002
\u5728\u6570\u7ec4\u4e2d\u8bbf\u95ee\u5143\u7d20\u975e\u5e38\u9ad8\u6548\uff0c\u6211\u4eec\u53ef\u4ee5\u5728 \\(O(1)\\) \u65f6\u95f4\u5185\u968f\u673a\u8bbf\u95ee\u6570\u7ec4\u4e2d\u7684\u4efb\u610f\u4e00\u4e2a\u5143\u7d20\u3002
PythonC++JavaC#GoSwiftJSTSDartRustCZig array.pydef random_access(nums: list[int]) -> int:\n \"\"\"\u968f\u673a\u8bbf\u95ee\u5143\u7d20\"\"\"\n # \u5728\u533a\u95f4 [0, len(nums)-1] \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\n random_index = random.randint(0, len(nums) - 1)\n # \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\n random_num = nums[random_index]\n return random_num\n
array.cpp/* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nint randomAccess(int *nums, int size) {\n // \u5728\u533a\u95f4 [0, size) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\n int randomIndex = rand() % size;\n // \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\n int randomNum = nums[randomIndex];\n return randomNum;\n}\n
array.java/* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nint randomAccess(int[] nums) {\n // \u5728\u533a\u95f4 [0, nums.length) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\n int randomIndex = ThreadLocalRandom.current().nextInt(0, nums.length);\n // \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\n int randomNum = nums[randomIndex];\n return randomNum;\n}\n
array.cs/* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nint RandomAccess(int[] nums) {\n Random random = new();\n // \u5728\u533a\u95f4 [0, nums.Length) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\n int randomIndex = random.Next(nums.Length);\n // \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\n int randomNum = nums[randomIndex];\n return randomNum;\n}\n
array.go/* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nfunc randomAccess(nums []int) (randomNum int) {\n // \u5728\u533a\u95f4 [0, nums.length) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\n randomIndex := rand.Intn(len(nums))\n // \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\n randomNum = nums[randomIndex]\n return\n}\n
array.swift/* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nfunc randomAccess(nums: [Int]) -> Int {\n // \u5728\u533a\u95f4 [0, nums.count) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\n let randomIndex = nums.indices.randomElement()!\n // \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\n let randomNum = nums[randomIndex]\n return randomNum\n}\n
array.js/* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nfunction randomAccess(nums) {\n // \u5728\u533a\u95f4 [0, nums.length) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\n const random_index = Math.floor(Math.random() * nums.length);\n // \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\n const random_num = nums[random_index];\n return random_num;\n}\n
array.ts/* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nfunction randomAccess(nums: number[]): number {\n // \u5728\u533a\u95f4 [0, nums.length) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\n const random_index = Math.floor(Math.random() * nums.length);\n // \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\n const random_num = nums[random_index];\n return random_num;\n}\n
array.dart/* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nint randomAccess(List<int> nums) {\n // \u5728\u533a\u95f4 [0, nums.length) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\n int randomIndex = Random().nextInt(nums.length);\n // \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\n int randomNum = nums[randomIndex];\n return randomNum;\n}\n
array.rs/* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nfn random_access(nums: &[i32]) -> i32 {\n // \u5728\u533a\u95f4 [0, nums.len()) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\n let random_index = rand::thread_rng().gen_range(0..nums.len());\n // \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\n let random_num = nums[random_index];\n random_num\n}\n
array.c/* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nint randomAccess(int *nums, int size) {\n // \u5728\u533a\u95f4 [0, size) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\n int randomIndex = rand() % size;\n // \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\n int randomNum = nums[randomIndex];\n return randomNum;\n}\n
array.zig// \u968f\u673a\u8bbf\u95ee\u5143\u7d20\nfn randomAccess(nums: []i32) i32 {\n // \u5728\u533a\u95f4 [0, nums.len) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6574\u6570\n var randomIndex = std.crypto.random.intRangeLessThan(usize, 0, nums.len);\n // \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\n var randomNum = nums[randomIndex];\n return randomNum;\n}\n
"},{"location":"chapter_array_and_linkedlist/array/#3","title":"3. \u00a0 \u63d2\u5165\u5143\u7d20","text":"\u6570\u7ec4\u5143\u7d20\u5728\u5185\u5b58\u4e2d\u662f\u201c\u7d27\u6328\u7740\u7684\u201d\uff0c\u5b83\u4eec\u4e4b\u95f4\u6ca1\u6709\u7a7a\u95f4\u518d\u5b58\u653e\u4efb\u4f55\u6570\u636e\u3002\u5982\u56fe 4-3 \u6240\u793a\uff0c\u5982\u679c\u60f3\u5728\u6570\u7ec4\u4e2d\u95f4\u63d2\u5165\u4e00\u4e2a\u5143\u7d20\uff0c\u5219\u9700\u8981\u5c06\u8be5\u5143\u7d20\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u4e4b\u540e\u518d\u628a\u5143\u7d20\u8d4b\u503c\u7ed9\u8be5\u7d22\u5f15\u3002
\u56fe 4-3 \u00a0 \u6570\u7ec4\u63d2\u5165\u5143\u7d20\u793a\u4f8b
\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u7531\u4e8e\u6570\u7ec4\u7684\u957f\u5ea6\u662f\u56fa\u5b9a\u7684\uff0c\u56e0\u6b64\u63d2\u5165\u4e00\u4e2a\u5143\u7d20\u5fc5\u5b9a\u4f1a\u5bfc\u81f4\u6570\u7ec4\u5c3e\u90e8\u5143\u7d20\u201c\u4e22\u5931\u201d\u3002\u6211\u4eec\u5c06\u8fd9\u4e2a\u95ee\u9898\u7684\u89e3\u51b3\u65b9\u6848\u7559\u5728\u201c\u5217\u8868\u201d\u7ae0\u8282\u4e2d\u8ba8\u8bba\u3002
PythonC++JavaC#GoSwiftJSTSDartRustCZig array.pydef insert(nums: list[int], num: int, index: int):\n \"\"\"\u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num\"\"\"\n # \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for i in range(len(nums) - 1, index, -1):\n nums[i] = nums[i - 1]\n # \u5c06 num \u8d4b\u7ed9 index \u5904\u7684\u5143\u7d20\n nums[index] = num\n
array.cpp/* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num */\nvoid insert(int *nums, int size, int num, int index) {\n // \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for (int i = size - 1; i > index; i--) {\n nums[i] = nums[i - 1];\n }\n // \u5c06 num \u8d4b\u7ed9 index \u5904\u7684\u5143\u7d20\n nums[index] = num;\n}\n
array.java/* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num */\nvoid insert(int[] nums, int num, int index) {\n // \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for (int i = nums.length - 1; i > index; i--) {\n nums[i] = nums[i - 1];\n }\n // \u5c06 num \u8d4b\u7ed9 index \u5904\u7684\u5143\u7d20\n nums[index] = num;\n}\n
array.cs/* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num */\nvoid Insert(int[] nums, int num, int index) {\n // \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for (int i = nums.Length - 1; i > index; i--) {\n nums[i] = nums[i - 1];\n }\n // \u5c06 num \u8d4b\u7ed9 index \u5904\u7684\u5143\u7d20\n nums[index] = num;\n}\n
array.go/* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num */\nfunc insert(nums []int, num int, index int) {\n // \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for i := len(nums) - 1; i > index; i-- {\n nums[i] = nums[i-1]\n }\n // \u5c06 num \u8d4b\u7ed9 index \u5904\u7684\u5143\u7d20\n nums[index] = num\n}\n
array.swift/* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num */\nfunc insert(nums: inout [Int], num: Int, index: Int) {\n // \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for i in nums.indices.dropFirst(index).reversed() {\n nums[i] = nums[i - 1]\n }\n // \u5c06 num \u8d4b\u7ed9 index \u5904\u7684\u5143\u7d20\n nums[index] = num\n}\n
array.js/* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num */\nfunction insert(nums, num, index) {\n // \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for (let i = nums.length - 1; i > index; i--) {\n nums[i] = nums[i - 1];\n }\n // \u5c06 num \u8d4b\u7ed9 index \u5904\u7684\u5143\u7d20\n nums[index] = num;\n}\n
array.ts/* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num */\nfunction insert(nums: number[], num: number, index: number): void {\n // \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for (let i = nums.length - 1; i > index; i--) {\n nums[i] = nums[i - 1];\n }\n // \u5c06 num \u8d4b\u7ed9 index \u5904\u7684\u5143\u7d20\n nums[index] = num;\n}\n
array.dart/* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 _num */\nvoid insert(List<int> nums, int _num, int index) {\n // \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for (var i = nums.length - 1; i > index; i--) {\n nums[i] = nums[i - 1];\n }\n // \u5c06 _num \u8d4b\u7ed9 index \u5904\u5143\u7d20\n nums[index] = _num;\n}\n
array.rs/* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num */\nfn insert(nums: &mut Vec<i32>, num: i32, index: usize) {\n // \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for i in (index + 1..nums.len()).rev() {\n nums[i] = nums[i - 1];\n }\n // \u5c06 num \u8d4b\u7ed9 index \u5904\u7684\u5143\u7d20\n nums[index] = num;\n}\n
array.c/* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num */\nvoid insert(int *nums, int size, int num, int index) {\n // \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for (int i = size - 1; i > index; i--) {\n nums[i] = nums[i - 1];\n }\n // \u5c06 num \u8d4b\u7ed9 index \u5904\u7684\u5143\u7d20\n nums[index] = num;\n}\n
array.zig// \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num\nfn insert(nums: []i32, num: i32, index: usize) void {\n // \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n var i = nums.len - 1;\n while (i > index) : (i -= 1) {\n nums[i] = nums[i - 1];\n }\n // \u5c06 num \u8d4b\u7ed9 index \u5904\u7684\u5143\u7d20\n nums[index] = num;\n}\n
"},{"location":"chapter_array_and_linkedlist/array/#4","title":"4. \u00a0 \u5220\u9664\u5143\u7d20","text":"\u540c\u7406\uff0c\u5982\u56fe 4-4 \u6240\u793a\uff0c\u82e5\u60f3\u5220\u9664\u7d22\u5f15 \\(i\\) \u5904\u7684\u5143\u7d20\uff0c\u5219\u9700\u8981\u628a\u7d22\u5f15 \\(i\\) \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\u3002
\u56fe 4-4 \u00a0 \u6570\u7ec4\u5220\u9664\u5143\u7d20\u793a\u4f8b
\u8bf7\u6ce8\u610f\uff0c\u5220\u9664\u5143\u7d20\u5b8c\u6210\u540e\uff0c\u539f\u5148\u672b\u5c3e\u7684\u5143\u7d20\u53d8\u5f97\u201c\u65e0\u610f\u4e49\u201d\u4e86\uff0c\u6240\u4ee5\u6211\u4eec\u65e0\u987b\u7279\u610f\u53bb\u4fee\u6539\u5b83\u3002
PythonC++JavaC#GoSwiftJSTSDartRustCZig array.pydef remove(nums: list[int], index: int):\n \"\"\"\u5220\u9664\u7d22\u5f15 index \u5904\u7684\u5143\u7d20\"\"\"\n # \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for i in range(index, len(nums) - 1):\n nums[i] = nums[i + 1]\n
array.cpp/* \u5220\u9664\u7d22\u5f15 index \u5904\u7684\u5143\u7d20 */\nvoid remove(int *nums, int size, int index) {\n // \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for (int i = index; i < size - 1; i++) {\n nums[i] = nums[i + 1];\n }\n}\n
array.java/* \u5220\u9664\u7d22\u5f15 index \u5904\u7684\u5143\u7d20 */\nvoid remove(int[] nums, int index) {\n // \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for (int i = index; i < nums.length - 1; i++) {\n nums[i] = nums[i + 1];\n }\n}\n
array.cs/* \u5220\u9664\u7d22\u5f15 index \u5904\u7684\u5143\u7d20 */\nvoid Remove(int[] nums, int index) {\n // \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for (int i = index; i < nums.Length - 1; i++) {\n nums[i] = nums[i + 1];\n }\n}\n
array.go/* \u5220\u9664\u7d22\u5f15 index \u5904\u7684\u5143\u7d20 */\nfunc remove(nums []int, index int) {\n // \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for i := index; i < len(nums)-1; i++ {\n nums[i] = nums[i+1]\n }\n}\n
array.swift/* \u5220\u9664\u7d22\u5f15 index \u5904\u7684\u5143\u7d20 */\nfunc remove(nums: inout [Int], index: Int) {\n // \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for i in nums.indices.dropFirst(index).dropLast() {\n nums[i] = nums[i + 1]\n }\n}\n
array.js/* \u5220\u9664\u7d22\u5f15 index \u5904\u7684\u5143\u7d20 */\nfunction remove(nums, index) {\n // \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for (let i = index; i < nums.length - 1; i++) {\n nums[i] = nums[i + 1];\n }\n}\n
array.ts/* \u5220\u9664\u7d22\u5f15 index \u5904\u7684\u5143\u7d20 */\nfunction remove(nums: number[], index: number): void {\n // \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for (let i = index; i < nums.length - 1; i++) {\n nums[i] = nums[i + 1];\n }\n}\n
array.dart/* \u5220\u9664\u7d22\u5f15 index \u5904\u7684\u5143\u7d20 */\nvoid remove(List<int> nums, int index) {\n // \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for (var i = index; i < nums.length - 1; i++) {\n nums[i] = nums[i + 1];\n }\n}\n
array.rs/* \u5220\u9664\u7d22\u5f15 index \u5904\u7684\u5143\u7d20 */\nfn remove(nums: &mut Vec<i32>, index: usize) {\n // \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for i in index..nums.len() - 1 {\n nums[i] = nums[i + 1];\n }\n}\n
array.c/* \u5220\u9664\u7d22\u5f15 index \u5904\u7684\u5143\u7d20 */\n// \u6ce8\u610f\uff1astdio.h \u5360\u7528\u4e86 remove \u5173\u952e\u8bcd\nvoid removeItem(int *nums, int size, int index) {\n // \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for (int i = index; i < size - 1; i++) {\n nums[i] = nums[i + 1];\n }\n}\n
array.zig// \u5220\u9664\u7d22\u5f15 index \u5904\u7684\u5143\u7d20\nfn remove(nums: []i32, index: usize) void {\n // \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n var i = index;\n while (i < nums.len - 1) : (i += 1) {\n nums[i] = nums[i + 1];\n }\n}\n
\u603b\u7684\u6765\u770b\uff0c\u6570\u7ec4\u7684\u63d2\u5165\u4e0e\u5220\u9664\u64cd\u4f5c\u6709\u4ee5\u4e0b\u7f3a\u70b9\u3002
\u5728\u5927\u591a\u6570\u7f16\u7a0b\u8bed\u8a00\u4e2d\uff0c\u6211\u4eec\u65e2\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\uff0c\u4e5f\u53ef\u4ee5\u76f4\u63a5\u904d\u5386\u83b7\u53d6\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig array.pydef traverse(nums: list[int]):\n \"\"\"\u904d\u5386\u6570\u7ec4\"\"\"\n count = 0\n # \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\n for i in range(len(nums)):\n count += nums[i]\n # \u76f4\u63a5\u904d\u5386\u6570\u7ec4\u5143\u7d20\n for num in nums:\n count += num\n # \u540c\u65f6\u904d\u5386\u6570\u636e\u7d22\u5f15\u548c\u5143\u7d20\n for i, num in enumerate(nums):\n count += nums[i]\n count += num\n
array.cpp/* \u904d\u5386\u6570\u7ec4 */\nvoid traverse(int *nums, int size) {\n int count = 0;\n // \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\n for (int i = 0; i < size; i++) {\n count += nums[i];\n }\n}\n
array.java/* \u904d\u5386\u6570\u7ec4 */\nvoid traverse(int[] nums) {\n int count = 0;\n // \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\n for (int i = 0; i < nums.length; i++) {\n count += nums[i];\n }\n // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\u5143\u7d20\n for (int num : nums) {\n count += num;\n }\n}\n
array.cs/* \u904d\u5386\u6570\u7ec4 */\nvoid Traverse(int[] nums) {\n int count = 0;\n // \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\n for (int i = 0; i < nums.Length; i++) {\n count += nums[i];\n }\n // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\u5143\u7d20\n foreach (int num in nums) {\n count += num;\n }\n}\n
array.go/* \u904d\u5386\u6570\u7ec4 */\nfunc traverse(nums []int) {\n count := 0\n // \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\n for i := 0; i < len(nums); i++ {\n count += nums[i]\n }\n count = 0\n // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\u5143\u7d20\n for _, num := range nums {\n count += num\n }\n // \u540c\u65f6\u904d\u5386\u6570\u636e\u7d22\u5f15\u548c\u5143\u7d20\n for i, num := range nums {\n count += nums[i]\n count += num\n }\n}\n
array.swift/* \u904d\u5386\u6570\u7ec4 */\nfunc traverse(nums: [Int]) {\n var count = 0\n // \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\n for i in nums.indices {\n count += nums[i]\n }\n // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\u5143\u7d20\n for num in nums {\n count += num\n }\n}\n
array.js/* \u904d\u5386\u6570\u7ec4 */\nfunction traverse(nums) {\n let count = 0;\n // \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\n for (let i = 0; i < nums.length; i++) {\n count += nums[i];\n }\n // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\u5143\u7d20\n for (const num of nums) {\n count += num;\n }\n}\n
array.ts/* \u904d\u5386\u6570\u7ec4 */\nfunction traverse(nums: number[]): void {\n let count = 0;\n // \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\n for (let i = 0; i < nums.length; i++) {\n count += nums[i];\n }\n // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\u5143\u7d20\n for (const num of nums) {\n count += num;\n }\n}\n
array.dart/* \u904d\u5386\u6570\u7ec4\u5143\u7d20 */\nvoid traverse(List<int> nums) {\n int count = 0;\n // \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\n for (var i = 0; i < nums.length; i++) {\n count += nums[i];\n }\n // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\u5143\u7d20\n for (int _num in nums) {\n count += _num;\n }\n // \u901a\u8fc7 forEach \u65b9\u6cd5\u904d\u5386\u6570\u7ec4\n nums.forEach((_num) {\n count += _num;\n });\n}\n
array.rs/* \u904d\u5386\u6570\u7ec4 */\nfn traverse(nums: &[i32]) {\n let mut _count = 0;\n // \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\n for i in 0..nums.len() {\n _count += nums[i];\n }\n // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\u5143\u7d20\n for num in nums {\n _count += num;\n }\n}\n
array.c/* \u904d\u5386\u6570\u7ec4 */\nvoid traverse(int *nums, int size) {\n int count = 0;\n // \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\n for (int i = 0; i < size; i++) {\n count += nums[i];\n }\n}\n
array.zig// \u904d\u5386\u6570\u7ec4\nfn traverse(nums: []i32) void {\n var count: i32 = 0;\n // \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\n var i: i32 = 0;\n while (i < nums.len) : (i += 1) {\n count += nums[i];\n }\n count = 0;\n // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\u5143\u7d20\n for (nums) |num| {\n count += num;\n }\n}\n
"},{"location":"chapter_array_and_linkedlist/array/#6","title":"6. \u00a0 \u67e5\u627e\u5143\u7d20","text":"\u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20\u9700\u8981\u904d\u5386\u6570\u7ec4\uff0c\u6bcf\u8f6e\u5224\u65ad\u5143\u7d20\u503c\u662f\u5426\u5339\u914d\uff0c\u82e5\u5339\u914d\u5219\u8f93\u51fa\u5bf9\u5e94\u7d22\u5f15\u3002
\u56e0\u4e3a\u6570\u7ec4\u662f\u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff0c\u6240\u4ee5\u4e0a\u8ff0\u67e5\u627e\u64cd\u4f5c\u88ab\u79f0\u4e3a\u201c\u7ebf\u6027\u67e5\u627e\u201d\u3002
PythonC++JavaC#GoSwiftJSTSDartRustCZig array.pydef find(nums: list[int], target: int) -> int:\n \"\"\"\u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20\"\"\"\n for i in range(len(nums)):\n if nums[i] == target:\n return i\n return -1\n
array.cpp/* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nint find(int *nums, int size, int target) {\n for (int i = 0; i < size; i++) {\n if (nums[i] == target)\n return i;\n }\n return -1;\n}\n
array.java/* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nint find(int[] nums, int target) {\n for (int i = 0; i < nums.length; i++) {\n if (nums[i] == target)\n return i;\n }\n return -1;\n}\n
array.cs/* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nint Find(int[] nums, int target) {\n for (int i = 0; i < nums.Length; i++) {\n if (nums[i] == target)\n return i;\n }\n return -1;\n}\n
array.go/* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nfunc find(nums []int, target int) (index int) {\n index = -1\n for i := 0; i < len(nums); i++ {\n if nums[i] == target {\n index = i\n break\n }\n }\n return\n}\n
array.swift/* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nfunc find(nums: [Int], target: Int) -> Int {\n for i in nums.indices {\n if nums[i] == target {\n return i\n }\n }\n return -1\n}\n
array.js/* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nfunction find(nums, target) {\n for (let i = 0; i < nums.length; i++) {\n if (nums[i] === target) return i;\n }\n return -1;\n}\n
array.ts/* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nfunction find(nums: number[], target: number): number {\n for (let i = 0; i < nums.length; i++) {\n if (nums[i] === target) {\n return i;\n }\n }\n return -1;\n}\n
array.dart/* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nint find(List<int> nums, int target) {\n for (var i = 0; i < nums.length; i++) {\n if (nums[i] == target) return i;\n }\n return -1;\n}\n
array.rs/* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nfn find(nums: &[i32], target: i32) -> Option<usize> {\n for i in 0..nums.len() {\n if nums[i] == target {\n return Some(i);\n }\n }\n None\n}\n
array.c/* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nint find(int *nums, int size, int target) {\n for (int i = 0; i < size; i++) {\n if (nums[i] == target)\n return i;\n }\n return -1;\n}\n
array.zig// \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20\nfn find(nums: []i32, target: i32) i32 {\n for (nums, 0..) |num, i| {\n if (num == target) return @intCast(i);\n }\n return -1;\n}\n
"},{"location":"chapter_array_and_linkedlist/array/#7","title":"7. \u00a0 \u6269\u5bb9\u6570\u7ec4","text":"\u5728\u590d\u6742\u7684\u7cfb\u7edf\u73af\u5883\u4e2d\uff0c\u7a0b\u5e8f\u96be\u4ee5\u4fdd\u8bc1\u6570\u7ec4\u4e4b\u540e\u7684\u5185\u5b58\u7a7a\u95f4\u662f\u53ef\u7528\u7684\uff0c\u4ece\u800c\u65e0\u6cd5\u5b89\u5168\u5730\u6269\u5c55\u6570\u7ec4\u5bb9\u91cf\u3002\u56e0\u6b64\u5728\u5927\u591a\u6570\u7f16\u7a0b\u8bed\u8a00\u4e2d\uff0c\u6570\u7ec4\u7684\u957f\u5ea6\u662f\u4e0d\u53ef\u53d8\u7684\u3002
\u5982\u679c\u6211\u4eec\u5e0c\u671b\u6269\u5bb9\u6570\u7ec4\uff0c\u5219\u9700\u91cd\u65b0\u5efa\u7acb\u4e00\u4e2a\u66f4\u5927\u7684\u6570\u7ec4\uff0c\u7136\u540e\u628a\u539f\u6570\u7ec4\u5143\u7d20\u4f9d\u6b21\u590d\u5236\u5230\u65b0\u6570\u7ec4\u3002\u8fd9\u662f\u4e00\u4e2a \\(O(n)\\) \u7684\u64cd\u4f5c\uff0c\u5728\u6570\u7ec4\u5f88\u5927\u7684\u60c5\u51b5\u4e0b\u975e\u5e38\u8017\u65f6\u3002\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig array.pydef extend(nums: list[int], enlarge: int) -> list[int]:\n \"\"\"\u6269\u5c55\u6570\u7ec4\u957f\u5ea6\"\"\"\n # \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\n res = [0] * (len(nums) + enlarge)\n # \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\n for i in range(len(nums)):\n res[i] = nums[i]\n # \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\n return res\n
array.cpp/* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\nint *extend(int *nums, int size, int enlarge) {\n // \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\n int *res = new int[size + enlarge];\n // \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\n for (int i = 0; i < size; i++) {\n res[i] = nums[i];\n }\n // \u91ca\u653e\u5185\u5b58\n delete[] nums;\n // \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\n return res;\n}\n
array.java/* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\nint[] extend(int[] nums, int enlarge) {\n // \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\n int[] res = new int[nums.length + enlarge];\n // \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\n for (int i = 0; i < nums.length; i++) {\n res[i] = nums[i];\n }\n // \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\n return res;\n}\n
array.cs/* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\nint[] Extend(int[] nums, int enlarge) {\n // \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\n int[] res = new int[nums.Length + enlarge];\n // \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\n for (int i = 0; i < nums.Length; i++) {\n res[i] = nums[i];\n }\n // \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\n return res;\n}\n
array.go/* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\nfunc extend(nums []int, enlarge int) []int {\n // \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\n res := make([]int, len(nums)+enlarge)\n // \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\n for i, num := range nums {\n res[i] = num\n }\n // \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\n return res\n}\n
array.swift/* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\nfunc extend(nums: [Int], enlarge: Int) -> [Int] {\n // \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\n var res = Array(repeating: 0, count: nums.count + enlarge)\n // \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\n for i in nums.indices {\n res[i] = nums[i]\n }\n // \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\n return res\n}\n
array.js/* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\n// \u8bf7\u6ce8\u610f\uff0cJavaScript \u7684 Array \u662f\u52a8\u6001\u6570\u7ec4\uff0c\u53ef\u4ee5\u76f4\u63a5\u6269\u5c55\n// \u4e3a\u4e86\u65b9\u4fbf\u5b66\u4e60\uff0c\u672c\u51fd\u6570\u5c06 Array \u770b\u4f5c\u957f\u5ea6\u4e0d\u53ef\u53d8\u7684\u6570\u7ec4\nfunction extend(nums, enlarge) {\n // \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\n const res = new Array(nums.length + enlarge).fill(0);\n // \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\n for (let i = 0; i < nums.length; i++) {\n res[i] = nums[i];\n }\n // \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\n return res;\n}\n
array.ts/* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\n// \u8bf7\u6ce8\u610f\uff0cTypeScript \u7684 Array \u662f\u52a8\u6001\u6570\u7ec4\uff0c\u53ef\u4ee5\u76f4\u63a5\u6269\u5c55\n// \u4e3a\u4e86\u65b9\u4fbf\u5b66\u4e60\uff0c\u672c\u51fd\u6570\u5c06 Array \u770b\u4f5c\u957f\u5ea6\u4e0d\u53ef\u53d8\u7684\u6570\u7ec4\nfunction extend(nums: number[], enlarge: number): number[] {\n // \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\n const res = new Array(nums.length + enlarge).fill(0);\n // \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\n for (let i = 0; i < nums.length; i++) {\n res[i] = nums[i];\n }\n // \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\n return res;\n}\n
array.dart/* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\nList<int> extend(List<int> nums, int enlarge) {\n // \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\n List<int> res = List.filled(nums.length + enlarge, 0);\n // \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\n for (var i = 0; i < nums.length; i++) {\n res[i] = nums[i];\n }\n // \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\n return res;\n}\n
array.rs/* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\nfn extend(nums: Vec<i32>, enlarge: usize) -> Vec<i32> {\n // \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\n let mut res: Vec<i32> = vec![0; nums.len() + enlarge];\n // \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\n for i in 0..nums.len() {\n res[i] = nums[i];\n }\n // \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\n res\n}\n
array.c/* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\nint *extend(int *nums, int size, int enlarge) {\n // \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\n int *res = (int *)malloc(sizeof(int) * (size + enlarge));\n // \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\n for (int i = 0; i < size; i++) {\n res[i] = nums[i];\n }\n // \u521d\u59cb\u5316\u6269\u5c55\u540e\u7684\u7a7a\u95f4\n for (int i = size; i < size + enlarge; i++) {\n res[i] = 0;\n }\n // \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\n return res;\n}\n
array.zig// \u6269\u5c55\u6570\u7ec4\u957f\u5ea6\nfn extend(mem_allocator: std.mem.Allocator, nums: []i32, enlarge: usize) ![]i32 {\n // \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\n var res = try mem_allocator.alloc(i32, nums.len + enlarge);\n @memset(res, 0);\n // \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\n std.mem.copy(i32, res, nums);\n // \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\n return res;\n}\n
"},{"location":"chapter_array_and_linkedlist/array/#412","title":"4.1.2 \u00a0 \u6570\u7ec4\u7684\u4f18\u70b9\u4e0e\u5c40\u9650\u6027","text":"\u6570\u7ec4\u5b58\u50a8\u5728\u8fde\u7eed\u7684\u5185\u5b58\u7a7a\u95f4\u5185\uff0c\u4e14\u5143\u7d20\u7c7b\u578b\u76f8\u540c\u3002\u8fd9\u79cd\u505a\u6cd5\u5305\u542b\u4e30\u5bcc\u7684\u5148\u9a8c\u4fe1\u606f\uff0c\u7cfb\u7edf\u53ef\u4ee5\u5229\u7528\u8fd9\u4e9b\u4fe1\u606f\u6765\u4f18\u5316\u6570\u636e\u7ed3\u6784\u7684\u64cd\u4f5c\u6548\u7387\u3002
\u8fde\u7eed\u7a7a\u95f4\u5b58\u50a8\u662f\u4e00\u628a\u53cc\u5203\u5251\uff0c\u5176\u5b58\u5728\u4ee5\u4e0b\u5c40\u9650\u6027\u3002
\u6570\u7ec4\u662f\u4e00\u79cd\u57fa\u7840\u4e14\u5e38\u89c1\u7684\u6570\u636e\u7ed3\u6784\uff0c\u65e2\u9891\u7e41\u5e94\u7528\u5728\u5404\u7c7b\u7b97\u6cd5\u4e4b\u4e2d\uff0c\u4e5f\u53ef\u7528\u4e8e\u5b9e\u73b0\u5404\u79cd\u590d\u6742\u6570\u636e\u7ed3\u6784\u3002
\u5185\u5b58\u7a7a\u95f4\u662f\u6240\u6709\u7a0b\u5e8f\u7684\u516c\u5171\u8d44\u6e90\uff0c\u5728\u4e00\u4e2a\u590d\u6742\u7684\u7cfb\u7edf\u8fd0\u884c\u73af\u5883\u4e0b\uff0c\u7a7a\u95f2\u7684\u5185\u5b58\u7a7a\u95f4\u53ef\u80fd\u6563\u843d\u5728\u5185\u5b58\u5404\u5904\u3002\u6211\u4eec\u77e5\u9053\uff0c\u5b58\u50a8\u6570\u7ec4\u7684\u5185\u5b58\u7a7a\u95f4\u5fc5\u987b\u662f\u8fde\u7eed\u7684\uff0c\u800c\u5f53\u6570\u7ec4\u975e\u5e38\u5927\u65f6\uff0c\u5185\u5b58\u53ef\u80fd\u65e0\u6cd5\u63d0\u4f9b\u5982\u6b64\u5927\u7684\u8fde\u7eed\u7a7a\u95f4\u3002\u6b64\u65f6\u94fe\u8868\u7684\u7075\u6d3b\u6027\u4f18\u52bf\u5c31\u4f53\u73b0\u51fa\u6765\u4e86\u3002
\u300c\u94fe\u8868 linked list\u300d\u662f\u4e00\u79cd\u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff0c\u5176\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u90fd\u662f\u4e00\u4e2a\u8282\u70b9\u5bf9\u8c61\uff0c\u5404\u4e2a\u8282\u70b9\u901a\u8fc7\u201c\u5f15\u7528\u201d\u76f8\u8fde\u63a5\u3002\u5f15\u7528\u8bb0\u5f55\u4e86\u4e0b\u4e00\u4e2a\u8282\u70b9\u7684\u5185\u5b58\u5730\u5740\uff0c\u901a\u8fc7\u5b83\u53ef\u4ee5\u4ece\u5f53\u524d\u8282\u70b9\u8bbf\u95ee\u5230\u4e0b\u4e00\u4e2a\u8282\u70b9\u3002
\u94fe\u8868\u7684\u8bbe\u8ba1\u4f7f\u5f97\u5404\u4e2a\u8282\u70b9\u53ef\u4ee5\u5206\u6563\u5b58\u50a8\u5728\u5185\u5b58\u5404\u5904\uff0c\u5b83\u4eec\u7684\u5185\u5b58\u5730\u5740\u65e0\u987b\u8fde\u7eed\u3002
\u56fe 4-5 \u00a0 \u94fe\u8868\u5b9a\u4e49\u4e0e\u5b58\u50a8\u65b9\u5f0f
\u89c2\u5bdf\u56fe 4-5 \uff0c\u94fe\u8868\u7684\u7ec4\u6210\u5355\u4f4d\u662f\u300c\u8282\u70b9 node\u300d\u5bf9\u8c61\u3002\u6bcf\u4e2a\u8282\u70b9\u90fd\u5305\u542b\u4e24\u9879\u6570\u636e\uff1a\u8282\u70b9\u7684\u201c\u503c\u201d\u548c\u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u201c\u5f15\u7528\u201d\u3002
\u5982\u4ee5\u4e0b\u4ee3\u7801\u6240\u793a\uff0c\u94fe\u8868\u8282\u70b9 ListNode
\u9664\u4e86\u5305\u542b\u503c\uff0c\u8fd8\u9700\u989d\u5916\u4fdd\u5b58\u4e00\u4e2a\u5f15\u7528\uff08\u6307\u9488\uff09\u3002\u56e0\u6b64\u5728\u76f8\u540c\u6570\u636e\u91cf\u4e0b\uff0c\u94fe\u8868\u6bd4\u6570\u7ec4\u5360\u7528\u66f4\u591a\u7684\u5185\u5b58\u7a7a\u95f4\u3002
class ListNode:\n \"\"\"\u94fe\u8868\u8282\u70b9\u7c7b\"\"\"\n def __init__(self, val: int):\n self.val: int = val # \u8282\u70b9\u503c\n self.next: ListNode | None = None # \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u5f15\u7528\n
/* \u94fe\u8868\u8282\u70b9\u7ed3\u6784\u4f53 */\nstruct ListNode {\n int val; // \u8282\u70b9\u503c\n ListNode *next; // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u6307\u9488\n ListNode(int x) : val(x), next(nullptr) {} // \u6784\u9020\u51fd\u6570\n};\n
/* \u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode {\n int val; // \u8282\u70b9\u503c\n ListNode next; // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u5f15\u7528\n ListNode(int x) { val = x; } // \u6784\u9020\u51fd\u6570\n}\n
/* \u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode(int x) { //\u6784\u9020\u51fd\u6570\n int val = x; // \u8282\u70b9\u503c\n ListNode? next; // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u5f15\u7528\n}\n
/* \u94fe\u8868\u8282\u70b9\u7ed3\u6784\u4f53 */\ntype ListNode struct {\n Val int // \u8282\u70b9\u503c\n Next *ListNode // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u6307\u9488\n}\n\n// NewListNode \u6784\u9020\u51fd\u6570\uff0c\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u94fe\u8868\nfunc NewListNode(val int) *ListNode {\n return &ListNode{\n Val: val,\n Next: nil,\n }\n}\n
/* \u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode {\n var val: Int // \u8282\u70b9\u503c\n var next: ListNode? // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u5f15\u7528\n\n init(x: Int) { // \u6784\u9020\u51fd\u6570\n val = x\n }\n}\n
/* \u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode {\n constructor(val, next) {\n this.val = (val === undefined ? 0 : val); // \u8282\u70b9\u503c\n this.next = (next === undefined ? null : next); // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u5f15\u7528\n }\n}\n
/* \u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode {\n val: number;\n next: ListNode | null;\n constructor(val?: number, next?: ListNode | null) {\n this.val = val === undefined ? 0 : val; // \u8282\u70b9\u503c\n this.next = next === undefined ? null : next; // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u5f15\u7528\n }\n}\n
/* \u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode {\n int val; // \u8282\u70b9\u503c\n ListNode? next; // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u5f15\u7528\n ListNode(this.val, [this.next]); // \u6784\u9020\u51fd\u6570\n}\n
use std::rc::Rc;\nuse std::cell::RefCell;\n/* \u94fe\u8868\u8282\u70b9\u7c7b */\n#[derive(Debug)]\nstruct ListNode {\n val: i32, // \u8282\u70b9\u503c\n next: Option<Rc<RefCell<ListNode>>>, // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u6307\u9488\n}\n
/* \u94fe\u8868\u8282\u70b9\u7ed3\u6784\u4f53 */\ntypedef struct ListNode {\n int val; // \u8282\u70b9\u503c\n struct ListNode *next; // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u6307\u9488\n} ListNode;\n\n/* \u6784\u9020\u51fd\u6570 */\nListNode *newListNode(int val) {\n ListNode *node;\n node = (ListNode *) malloc(sizeof(ListNode));\n node->val = val;\n node->next = NULL;\n return node;\n}\n
// \u94fe\u8868\u8282\u70b9\u7c7b\npub fn ListNode(comptime T: type) type {\n return struct {\n const Self = @This();\n\n val: T = 0, // \u8282\u70b9\u503c\n next: ?*Self = null, // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u6307\u9488\n\n // \u6784\u9020\u51fd\u6570\n pub fn init(self: *Self, x: i32) void {\n self.val = x;\n self.next = null;\n }\n };\n}\n
"},{"location":"chapter_array_and_linkedlist/linked_list/#421","title":"4.2.1 \u00a0 \u94fe\u8868\u5e38\u7528\u64cd\u4f5c","text":""},{"location":"chapter_array_and_linkedlist/linked_list/#1","title":"1. \u00a0 \u521d\u59cb\u5316\u94fe\u8868","text":"\u5efa\u7acb\u94fe\u8868\u5206\u4e3a\u4e24\u6b65\uff0c\u7b2c\u4e00\u6b65\u662f\u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\u5bf9\u8c61\uff0c\u7b2c\u4e8c\u6b65\u662f\u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\u5173\u7cfb\u3002\u521d\u59cb\u5316\u5b8c\u6210\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u4ece\u94fe\u8868\u7684\u5934\u8282\u70b9\u51fa\u53d1\uff0c\u901a\u8fc7\u5f15\u7528\u6307\u5411 next
\u4f9d\u6b21\u8bbf\u95ee\u6240\u6709\u8282\u70b9\u3002
# \u521d\u59cb\u5316\u94fe\u8868 1 -> 3 -> 2 -> 5 -> 4\n# \u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\nn0 = ListNode(1)\nn1 = ListNode(3)\nn2 = ListNode(2)\nn3 = ListNode(5)\nn4 = ListNode(4)\n# \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\nn0.next = n1\nn1.next = n2\nn2.next = n3\nn3.next = n4\n
linked_list.cpp/* \u521d\u59cb\u5316\u94fe\u8868 1 -> 3 -> 2 -> 5 -> 4 */\n// \u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\nListNode* n0 = new ListNode(1);\nListNode* n1 = new ListNode(3);\nListNode* n2 = new ListNode(2);\nListNode* n3 = new ListNode(5);\nListNode* n4 = new ListNode(4);\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\nn0->next = n1;\nn1->next = n2;\nn2->next = n3;\nn3->next = n4;\n
linked_list.java/* \u521d\u59cb\u5316\u94fe\u8868 1 -> 3 -> 2 -> 5 -> 4 */\n// \u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\nListNode n0 = new ListNode(1);\nListNode n1 = new ListNode(3);\nListNode n2 = new ListNode(2);\nListNode n3 = new ListNode(5);\nListNode n4 = new ListNode(4);\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\nn0.next = n1;\nn1.next = n2;\nn2.next = n3;\nn3.next = n4;\n
linked_list.cs/* \u521d\u59cb\u5316\u94fe\u8868 1 -> 3 -> 2 -> 5 -> 4 */\n// \u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\nListNode n0 = new(1);\nListNode n1 = new(3);\nListNode n2 = new(2);\nListNode n3 = new(5);\nListNode n4 = new(4);\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\nn0.next = n1;\nn1.next = n2;\nn2.next = n3;\nn3.next = n4;\n
linked_list.go/* \u521d\u59cb\u5316\u94fe\u8868 1 -> 3 -> 2 -> 5 -> 4 */\n// \u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\nn0 := NewListNode(1)\nn1 := NewListNode(3)\nn2 := NewListNode(2)\nn3 := NewListNode(5)\nn4 := NewListNode(4)\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\nn0.Next = n1\nn1.Next = n2\nn2.Next = n3\nn3.Next = n4\n
linked_list.swift/* \u521d\u59cb\u5316\u94fe\u8868 1 -> 3 -> 2 -> 5 -> 4 */\n// \u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\nlet n0 = ListNode(x: 1)\nlet n1 = ListNode(x: 3)\nlet n2 = ListNode(x: 2)\nlet n3 = ListNode(x: 5)\nlet n4 = ListNode(x: 4)\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\nn0.next = n1\nn1.next = n2\nn2.next = n3\nn3.next = n4\n
linked_list.js/* \u521d\u59cb\u5316\u94fe\u8868 1 -> 3 -> 2 -> 5 -> 4 */\n// \u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\nconst n0 = new ListNode(1);\nconst n1 = new ListNode(3);\nconst n2 = new ListNode(2);\nconst n3 = new ListNode(5);\nconst n4 = new ListNode(4);\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\nn0.next = n1;\nn1.next = n2;\nn2.next = n3;\nn3.next = n4;\n
linked_list.ts/* \u521d\u59cb\u5316\u94fe\u8868 1 -> 3 -> 2 -> 5 -> 4 */\n// \u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\nconst n0 = new ListNode(1);\nconst n1 = new ListNode(3);\nconst n2 = new ListNode(2);\nconst n3 = new ListNode(5);\nconst n4 = new ListNode(4);\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\nn0.next = n1;\nn1.next = n2;\nn2.next = n3;\nn3.next = n4;\n
linked_list.dart/* \u521d\u59cb\u5316\u94fe\u8868 1 -> 3 -> 2 -> 5 -> 4 */\\\n// \u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\nListNode n0 = ListNode(1);\nListNode n1 = ListNode(3);\nListNode n2 = ListNode(2);\nListNode n3 = ListNode(5);\nListNode n4 = ListNode(4);\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\nn0.next = n1;\nn1.next = n2;\nn2.next = n3;\nn3.next = n4;\n
linked_list.rs/* \u521d\u59cb\u5316\u94fe\u8868 1 -> 3 -> 2 -> 5 -> 4 */\n// \u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\nlet n0 = Rc::new(RefCell::new(ListNode { val: 1, next: None }));\nlet n1 = Rc::new(RefCell::new(ListNode { val: 3, next: None }));\nlet n2 = Rc::new(RefCell::new(ListNode { val: 2, next: None }));\nlet n3 = Rc::new(RefCell::new(ListNode { val: 5, next: None }));\nlet n4 = Rc::new(RefCell::new(ListNode { val: 4, next: None }));\n\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\nn0.borrow_mut().next = Some(n1.clone());\nn1.borrow_mut().next = Some(n2.clone());\nn2.borrow_mut().next = Some(n3.clone());\nn3.borrow_mut().next = Some(n4.clone());\n
linked_list.c/* \u521d\u59cb\u5316\u94fe\u8868 1 -> 3 -> 2 -> 5 -> 4 */\n// \u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\nListNode* n0 = newListNode(1);\nListNode* n1 = newListNode(3);\nListNode* n2 = newListNode(2);\nListNode* n3 = newListNode(5);\nListNode* n4 = newListNode(4);\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\nn0->next = n1;\nn1->next = n2;\nn2->next = n3;\nn3->next = n4;\n
linked_list.zig// \u521d\u59cb\u5316\u94fe\u8868\n// \u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\nvar n0 = inc.ListNode(i32){.val = 1};\nvar n1 = inc.ListNode(i32){.val = 3};\nvar n2 = inc.ListNode(i32){.val = 2};\nvar n3 = inc.ListNode(i32){.val = 5};\nvar n4 = inc.ListNode(i32){.val = 4};\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\nn0.next = &n1;\nn1.next = &n2;\nn2.next = &n3;\nn3.next = &n4;\n
\u6570\u7ec4\u6574\u4f53\u662f\u4e00\u4e2a\u53d8\u91cf\uff0c\u6bd4\u5982\u6570\u7ec4 nums
\u5305\u542b\u5143\u7d20 nums[0]
\u548c nums[1]
\u7b49\uff0c\u800c\u94fe\u8868\u662f\u7531\u591a\u4e2a\u72ec\u7acb\u7684\u8282\u70b9\u5bf9\u8c61\u7ec4\u6210\u7684\u3002\u6211\u4eec\u901a\u5e38\u5c06\u5934\u8282\u70b9\u5f53\u4f5c\u94fe\u8868\u7684\u4ee3\u79f0\uff0c\u6bd4\u5982\u4ee5\u4e0a\u4ee3\u7801\u4e2d\u7684\u94fe\u8868\u53ef\u8bb0\u4f5c\u94fe\u8868 n0
\u3002
\u5728\u94fe\u8868\u4e2d\u63d2\u5165\u8282\u70b9\u975e\u5e38\u5bb9\u6613\u3002\u5982\u56fe 4-6 \u6240\u793a\uff0c\u5047\u8bbe\u6211\u4eec\u60f3\u5728\u76f8\u90bb\u7684\u4e24\u4e2a\u8282\u70b9 n0
\u548c n1
\u4e4b\u95f4\u63d2\u5165\u4e00\u4e2a\u65b0\u8282\u70b9 P
\uff0c\u5219\u53ea\u9700\u6539\u53d8\u4e24\u4e2a\u8282\u70b9\u5f15\u7528\uff08\u6307\u9488\uff09\u5373\u53ef\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(1)\\) \u3002
\u76f8\u6bd4\u4e4b\u4e0b\uff0c\u5728\u6570\u7ec4\u4e2d\u63d2\u5165\u5143\u7d20\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \uff0c\u5728\u5927\u6570\u636e\u91cf\u4e0b\u7684\u6548\u7387\u8f83\u4f4e\u3002
\u56fe 4-6 \u00a0 \u94fe\u8868\u63d2\u5165\u8282\u70b9\u793a\u4f8b
PythonC++JavaC#GoSwiftJSTSDartRustCZig linked_list.pydef insert(n0: ListNode, P: ListNode):\n \"\"\"\u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P\"\"\"\n n1 = n0.next\n P.next = n1\n n0.next = P\n
linked_list.cpp/* \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P */\nvoid insert(ListNode *n0, ListNode *P) {\n ListNode *n1 = n0->next;\n P->next = n1;\n n0->next = P;\n}\n
linked_list.java/* \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P */\nvoid insert(ListNode n0, ListNode P) {\n ListNode n1 = n0.next;\n P.next = n1;\n n0.next = P;\n}\n
linked_list.cs/* \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P */\nvoid Insert(ListNode n0, ListNode P) {\n ListNode? n1 = n0.next;\n P.next = n1;\n n0.next = P;\n}\n
linked_list.go/* \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P */\nfunc insertNode(n0 *ListNode, P *ListNode) {\n n1 := n0.Next\n P.Next = n1\n n0.Next = P\n}\n
linked_list.swift/* \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P */\nfunc insert(n0: ListNode, P: ListNode) {\n let n1 = n0.next\n P.next = n1\n n0.next = P\n}\n
linked_list.js/* \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P */\nfunction insert(n0, P) {\n const n1 = n0.next;\n P.next = n1;\n n0.next = P;\n}\n
linked_list.ts/* \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P */\nfunction insert(n0: ListNode, P: ListNode): void {\n const n1 = n0.next;\n P.next = n1;\n n0.next = P;\n}\n
linked_list.dart/* \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P */\nvoid insert(ListNode n0, ListNode P) {\n ListNode? n1 = n0.next;\n P.next = n1;\n n0.next = P;\n}\n
linked_list.rs/* \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P */\n#[allow(non_snake_case)]\npub fn insert<T>(n0: &Rc<RefCell<ListNode<T>>>, P: Rc<RefCell<ListNode<T>>>) {\n let n1 = n0.borrow_mut().next.take();\n P.borrow_mut().next = n1;\n n0.borrow_mut().next = Some(P);\n}\n
linked_list.c/* \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P */\nvoid insert(ListNode *n0, ListNode *P) {\n ListNode *n1 = n0->next;\n P->next = n1;\n n0->next = P;\n}\n
linked_list.zig// \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P\nfn insert(n0: ?*inc.ListNode(i32), P: ?*inc.ListNode(i32)) void {\n var n1 = n0.?.next;\n P.?.next = n1;\n n0.?.next = P;\n}\n
"},{"location":"chapter_array_and_linkedlist/linked_list/#3","title":"3. \u00a0 \u5220\u9664\u8282\u70b9","text":"\u5982\u56fe 4-7 \u6240\u793a\uff0c\u5728\u94fe\u8868\u4e2d\u5220\u9664\u8282\u70b9\u4e5f\u975e\u5e38\u65b9\u4fbf\uff0c\u53ea\u9700\u6539\u53d8\u4e00\u4e2a\u8282\u70b9\u7684\u5f15\u7528\uff08\u6307\u9488\uff09\u5373\u53ef\u3002
\u8bf7\u6ce8\u610f\uff0c\u5c3d\u7ba1\u5728\u5220\u9664\u64cd\u4f5c\u5b8c\u6210\u540e\u8282\u70b9 P
\u4ecd\u7136\u6307\u5411 n1
\uff0c\u4f46\u5b9e\u9645\u4e0a\u904d\u5386\u6b64\u94fe\u8868\u5df2\u7ecf\u65e0\u6cd5\u8bbf\u95ee\u5230 P
\uff0c\u8fd9\u610f\u5473\u7740 P
\u5df2\u7ecf\u4e0d\u518d\u5c5e\u4e8e\u8be5\u94fe\u8868\u4e86\u3002
\u56fe 4-7 \u00a0 \u94fe\u8868\u5220\u9664\u8282\u70b9
PythonC++JavaC#GoSwiftJSTSDartRustCZig linked_list.pydef remove(n0: ListNode):\n \"\"\"\u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9\"\"\"\n if not n0.next:\n return\n # n0 -> P -> n1\n P = n0.next\n n1 = P.next\n n0.next = n1\n
linked_list.cpp/* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\nvoid remove(ListNode *n0) {\n if (n0->next == nullptr)\n return;\n // n0 -> P -> n1\n ListNode *P = n0->next;\n ListNode *n1 = P->next;\n n0->next = n1;\n // \u91ca\u653e\u5185\u5b58\n delete P;\n}\n
linked_list.java/* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\nvoid remove(ListNode n0) {\n if (n0.next == null)\n return;\n // n0 -> P -> n1\n ListNode P = n0.next;\n ListNode n1 = P.next;\n n0.next = n1;\n}\n
linked_list.cs/* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\nvoid Remove(ListNode n0) {\n if (n0.next == null)\n return;\n // n0 -> P -> n1\n ListNode P = n0.next;\n ListNode? n1 = P.next;\n n0.next = n1;\n}\n
linked_list.go/* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\nfunc removeItem(n0 *ListNode) {\n if n0.Next == nil {\n return\n }\n // n0 -> P -> n1\n P := n0.Next\n n1 := P.Next\n n0.Next = n1\n}\n
linked_list.swift/* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\nfunc remove(n0: ListNode) {\n if n0.next == nil {\n return\n }\n // n0 -> P -> n1\n let P = n0.next\n let n1 = P?.next\n n0.next = n1\n P?.next = nil\n}\n
linked_list.js/* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\nfunction remove(n0) {\n if (!n0.next) return;\n // n0 -> P -> n1\n const P = n0.next;\n const n1 = P.next;\n n0.next = n1;\n}\n
linked_list.ts/* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\nfunction remove(n0: ListNode): void {\n if (!n0.next) {\n return;\n }\n // n0 -> P -> n1\n const P = n0.next;\n const n1 = P.next;\n n0.next = n1;\n}\n
linked_list.dart/* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\nvoid remove(ListNode n0) {\n if (n0.next == null) return;\n // n0 -> P -> n1\n ListNode P = n0.next!;\n ListNode? n1 = P.next;\n n0.next = n1;\n}\n
linked_list.rs/* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\n#[allow(non_snake_case)]\npub fn remove<T>(n0: &Rc<RefCell<ListNode<T>>>) {\n if n0.borrow().next.is_none() {return};\n // n0 -> P -> n1\n let P = n0.borrow_mut().next.take();\n if let Some(node) = P {\n let n1 = node.borrow_mut().next.take();\n n0.borrow_mut().next = n1;\n }\n}\n
linked_list.c/* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\n// \u6ce8\u610f\uff1astdio.h \u5360\u7528\u4e86 remove \u5173\u952e\u8bcd\nvoid removeItem(ListNode *n0) {\n if (!n0->next)\n return;\n // n0 -> P -> n1\n ListNode *P = n0->next;\n ListNode *n1 = P->next;\n n0->next = n1;\n // \u91ca\u653e\u5185\u5b58\n free(P);\n}\n
linked_list.zig// \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9\nfn remove(n0: ?*inc.ListNode(i32)) void {\n if (n0.?.next == null) return;\n // n0 -> P -> n1\n var P = n0.?.next;\n var n1 = P.?.next;\n n0.?.next = n1;\n}\n
"},{"location":"chapter_array_and_linkedlist/linked_list/#4","title":"4. \u00a0 \u8bbf\u95ee\u8282\u70b9","text":"\u5728\u94fe\u8868\u4e2d\u8bbf\u95ee\u8282\u70b9\u7684\u6548\u7387\u8f83\u4f4e\u3002\u5982\u4e0a\u4e00\u8282\u6240\u8ff0\uff0c\u6211\u4eec\u53ef\u4ee5\u5728 \\(O(1)\\) \u65f6\u95f4\u4e0b\u8bbf\u95ee\u6570\u7ec4\u4e2d\u7684\u4efb\u610f\u5143\u7d20\u3002\u94fe\u8868\u5219\u4e0d\u7136\uff0c\u7a0b\u5e8f\u9700\u8981\u4ece\u5934\u8282\u70b9\u51fa\u53d1\uff0c\u9010\u4e2a\u5411\u540e\u904d\u5386\uff0c\u76f4\u81f3\u627e\u5230\u76ee\u6807\u8282\u70b9\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u8bbf\u95ee\u94fe\u8868\u7684\u7b2c \\(i\\) \u4e2a\u8282\u70b9\u9700\u8981\u5faa\u73af \\(i - 1\\) \u8f6e\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002
PythonC++JavaC#GoSwiftJSTSDartRustCZig linked_list.pydef access(head: ListNode, index: int) -> ListNode | None:\n \"\"\"\u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9\"\"\"\n for _ in range(index):\n if not head:\n return None\n head = head.next\n return head\n
linked_list.cpp/* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\nListNode *access(ListNode *head, int index) {\n for (int i = 0; i < index; i++) {\n if (head == nullptr)\n return nullptr;\n head = head->next;\n }\n return head;\n}\n
linked_list.java/* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\nListNode access(ListNode head, int index) {\n for (int i = 0; i < index; i++) {\n if (head == null)\n return null;\n head = head.next;\n }\n return head;\n}\n
linked_list.cs/* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\nListNode? Access(ListNode? head, int index) {\n for (int i = 0; i < index; i++) {\n if (head == null)\n return null;\n head = head.next;\n }\n return head;\n}\n
linked_list.go/* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\nfunc access(head *ListNode, index int) *ListNode {\n for i := 0; i < index; i++ {\n if head == nil {\n return nil\n }\n head = head.Next\n }\n return head\n}\n
linked_list.swift/* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\nfunc access(head: ListNode, index: Int) -> ListNode? {\n var head: ListNode? = head\n for _ in 0 ..< index {\n if head == nil {\n return nil\n }\n head = head?.next\n }\n return head\n}\n
linked_list.js/* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\nfunction access(head, index) {\n for (let i = 0; i < index; i++) {\n if (!head) {\n return null;\n }\n head = head.next;\n }\n return head;\n}\n
linked_list.ts/* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\nfunction access(head: ListNode | null, index: number): ListNode | null {\n for (let i = 0; i < index; i++) {\n if (!head) {\n return null;\n }\n head = head.next;\n }\n return head;\n}\n
linked_list.dart/* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\nListNode? access(ListNode? head, int index) {\n for (var i = 0; i < index; i++) {\n if (head == null) return null;\n head = head.next;\n }\n return head;\n}\n
linked_list.rs/* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\npub fn access<T>(head: Rc<RefCell<ListNode<T>>>, index: i32) -> Rc<RefCell<ListNode<T>>> {\n if index <= 0 {return head};\n if let Some(node) = &head.borrow_mut().next {\n return access(node.clone(), index - 1);\n }\n return head;\n}\n
linked_list.c/* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\nListNode *access(ListNode *head, int index) {\n while (head && head->next && index) {\n head = head->next;\n index--;\n }\n return head;\n}\n
linked_list.zig// \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9\nfn access(node: ?*inc.ListNode(i32), index: i32) ?*inc.ListNode(i32) {\n var head = node;\n var i: i32 = 0;\n while (i < index) : (i += 1) {\n head = head.?.next;\n if (head == null) return null;\n }\n return head;\n}\n
"},{"location":"chapter_array_and_linkedlist/linked_list/#5","title":"5. \u00a0 \u67e5\u627e\u8282\u70b9","text":"\u904d\u5386\u94fe\u8868\uff0c\u67e5\u627e\u5176\u4e2d\u503c\u4e3a target
\u7684\u8282\u70b9\uff0c\u8f93\u51fa\u8be5\u8282\u70b9\u5728\u94fe\u8868\u4e2d\u7684\u7d22\u5f15\u3002\u6b64\u8fc7\u7a0b\u4e5f\u5c5e\u4e8e\u7ebf\u6027\u67e5\u627e\u3002\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a
def find(head: ListNode, target: int) -> int:\n \"\"\"\u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9\"\"\"\n index = 0\n while head:\n if head.val == target:\n return index\n head = head.next\n index += 1\n return -1\n
linked_list.cpp/* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 */\nint find(ListNode *head, int target) {\n int index = 0;\n while (head != nullptr) {\n if (head->val == target)\n return index;\n head = head->next;\n index++;\n }\n return -1;\n}\n
linked_list.java/* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 */\nint find(ListNode head, int target) {\n int index = 0;\n while (head != null) {\n if (head.val == target)\n return index;\n head = head.next;\n index++;\n }\n return -1;\n}\n
linked_list.cs/* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 */\nint Find(ListNode? head, int target) {\n int index = 0;\n while (head != null) {\n if (head.val == target)\n return index;\n head = head.next;\n index++;\n }\n return -1;\n}\n
linked_list.go/* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 */\nfunc findNode(head *ListNode, target int) int {\n index := 0\n for head != nil {\n if head.Val == target {\n return index\n }\n head = head.Next\n index++\n }\n return -1\n}\n
linked_list.swift/* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 */\nfunc find(head: ListNode, target: Int) -> Int {\n var head: ListNode? = head\n var index = 0\n while head != nil {\n if head?.val == target {\n return index\n }\n head = head?.next\n index += 1\n }\n return -1\n}\n
linked_list.js/* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 */\nfunction find(head, target) {\n let index = 0;\n while (head !== null) {\n if (head.val === target) {\n return index;\n }\n head = head.next;\n index += 1;\n }\n return -1;\n}\n
linked_list.ts/* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 */\nfunction find(head: ListNode | null, target: number): number {\n let index = 0;\n while (head !== null) {\n if (head.val === target) {\n return index;\n }\n head = head.next;\n index += 1;\n }\n return -1;\n}\n
linked_list.dart/* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 */\nint find(ListNode? head, int target) {\n int index = 0;\n while (head != null) {\n if (head.val == target) {\n return index;\n }\n head = head.next;\n index++;\n }\n return -1;\n}\n
linked_list.rs/* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 */\npub fn find<T: PartialEq>(head: Rc<RefCell<ListNode<T>>>, target: T, index: i32) -> i32 {\n if head.borrow().val == target {return index};\n if let Some(node) = &head.borrow_mut().next {\n return find(node.clone(), target, index + 1);\n }\n return -1;\n}\n
linked_list.c/* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 */\nint find(ListNode *head, int target) {\n int index = 0;\n while (head) {\n if (head->val == target)\n return index;\n head = head->next;\n index++;\n }\n return -1;\n}\n
linked_list.zig// \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9\nfn find(node: ?*inc.ListNode(i32), target: i32) i32 {\n var head = node;\n var index: i32 = 0;\n while (head != null) {\n if (head.?.val == target) return index;\n head = head.?.next;\n index += 1;\n }\n return -1;\n}\n
"},{"location":"chapter_array_and_linkedlist/linked_list/#422-vs","title":"4.2.2 \u00a0 \u6570\u7ec4 vs. \u94fe\u8868","text":"\u8868 4-1 \u603b\u7ed3\u4e86\u6570\u7ec4\u548c\u94fe\u8868\u7684\u5404\u9879\u7279\u70b9\u5e76\u5bf9\u6bd4\u4e86\u64cd\u4f5c\u6548\u7387\u3002\u7531\u4e8e\u5b83\u4eec\u91c7\u7528\u4e24\u79cd\u76f8\u53cd\u7684\u5b58\u50a8\u7b56\u7565\uff0c\u56e0\u6b64\u5404\u79cd\u6027\u8d28\u548c\u64cd\u4f5c\u6548\u7387\u4e5f\u5448\u73b0\u5bf9\u7acb\u7684\u7279\u70b9\u3002
\u8868 4-1 \u00a0 \u6570\u7ec4\u4e0e\u94fe\u8868\u7684\u6548\u7387\u5bf9\u6bd4
\u6570\u7ec4 \u94fe\u8868 \u5b58\u50a8\u65b9\u5f0f \u8fde\u7eed\u5185\u5b58\u7a7a\u95f4 \u5206\u6563\u5185\u5b58\u7a7a\u95f4 \u5bb9\u91cf\u6269\u5c55 \u957f\u5ea6\u4e0d\u53ef\u53d8 \u53ef\u7075\u6d3b\u6269\u5c55 \u5185\u5b58\u6548\u7387 \u5143\u7d20\u5360\u7528\u5185\u5b58\u5c11\u3001\u4f46\u53ef\u80fd\u6d6a\u8d39\u7a7a\u95f4 \u5143\u7d20\u5360\u7528\u5185\u5b58\u591a \u8bbf\u95ee\u5143\u7d20 \\(O(1)\\) \\(O(n)\\) \u6dfb\u52a0\u5143\u7d20 \\(O(n)\\) \\(O(1)\\) \u5220\u9664\u5143\u7d20 \\(O(n)\\) \\(O(1)\\)"},{"location":"chapter_array_and_linkedlist/linked_list/#423","title":"4.2.3 \u00a0 \u5e38\u89c1\u94fe\u8868\u7c7b\u578b","text":"\u5982\u56fe 4-8 \u6240\u793a\uff0c\u5e38\u89c1\u7684\u94fe\u8868\u7c7b\u578b\u5305\u62ec\u4e09\u79cd\u3002
class ListNode:\n \"\"\"\u53cc\u5411\u94fe\u8868\u8282\u70b9\u7c7b\"\"\"\n def __init__(self, val: int):\n self.val: int = val # \u8282\u70b9\u503c\n self.next: ListNode | None = None # \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u5f15\u7528\n self.prev: ListNode | None = None # \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u5f15\u7528\n
/* \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7ed3\u6784\u4f53 */\nstruct ListNode {\n int val; // \u8282\u70b9\u503c\n ListNode *next; // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u6307\u9488\n ListNode *prev; // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u6307\u9488\n ListNode(int x) : val(x), next(nullptr), prev(nullptr) {} // \u6784\u9020\u51fd\u6570\n};\n
/* \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode {\n int val; // \u8282\u70b9\u503c\n ListNode next; // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u5f15\u7528\n ListNode prev; // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u5f15\u7528\n ListNode(int x) { val = x; } // \u6784\u9020\u51fd\u6570\n}\n
/* \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode(int x) { // \u6784\u9020\u51fd\u6570\n int val = x; // \u8282\u70b9\u503c\n ListNode next; // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u5f15\u7528\n ListNode prev; // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u5f15\u7528\n}\n
/* \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7ed3\u6784\u4f53 */\ntype DoublyListNode struct {\n Val int // \u8282\u70b9\u503c\n Next *DoublyListNode // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u6307\u9488\n Prev *DoublyListNode // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u6307\u9488\n}\n\n// NewDoublyListNode \u521d\u59cb\u5316\nfunc NewDoublyListNode(val int) *DoublyListNode {\n return &DoublyListNode{\n Val: val,\n Next: nil,\n Prev: nil,\n }\n}\n
/* \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode {\n var val: Int // \u8282\u70b9\u503c\n var next: ListNode? // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u5f15\u7528\n var prev: ListNode? // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u5f15\u7528\n\n init(x: Int) { // \u6784\u9020\u51fd\u6570\n val = x\n }\n}\n
/* \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode {\n constructor(val, next, prev) {\n this.val = val === undefined ? 0 : val; // \u8282\u70b9\u503c\n this.next = next === undefined ? null : next; // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u5f15\u7528\n this.prev = prev === undefined ? null : prev; // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u5f15\u7528\n }\n}\n
/* \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode {\n val: number;\n next: ListNode | null;\n prev: ListNode | null;\n constructor(val?: number, next?: ListNode | null, prev?: ListNode | null) {\n this.val = val === undefined ? 0 : val; // \u8282\u70b9\u503c\n this.next = next === undefined ? null : next; // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u5f15\u7528\n this.prev = prev === undefined ? null : prev; // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u5f15\u7528\n }\n}\n
/* \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode {\n int val; // \u8282\u70b9\u503c\n ListNode next; // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u5f15\u7528\n ListNode prev; // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u5f15\u7528\n ListNode(this.val, [this.next, this.prev]); // \u6784\u9020\u51fd\u6570\n}\n
use std::rc::Rc;\nuse std::cell::RefCell;\n\n/* \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7c7b\u578b */\n#[derive(Debug)]\nstruct ListNode {\n val: i32, // \u8282\u70b9\u503c\n next: Option<Rc<RefCell<ListNode>>>, // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u6307\u9488\n prev: Option<Rc<RefCell<ListNode>>>, // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u6307\u9488\n}\n\n/* \u6784\u9020\u51fd\u6570 */\nimpl ListNode {\n fn new(val: i32) -> Self {\n ListNode {\n val,\n next: None,\n prev: None,\n }\n }\n}\n
/* \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7ed3\u6784\u4f53 */\ntypedef struct ListNode {\n int val; // \u8282\u70b9\u503c\n struct ListNode *next; // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u6307\u9488\n struct ListNode *prev; // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u6307\u9488\n} ListNode;\n\n/* \u6784\u9020\u51fd\u6570 */\nListNode *newListNode(int val) {\n ListNode *node;\n node = (ListNode *) malloc(sizeof(ListNode));\n node->val = val;\n node->next = NULL;\n node->prev = NULL;\n return node;\n}\n
// \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7c7b\npub fn ListNode(comptime T: type) type {\n return struct {\n const Self = @This();\n\n val: T = 0, // \u8282\u70b9\u503c\n next: ?*Self = null, // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u6307\u9488\n prev: ?*Self = null, // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u6307\u9488\n\n // \u6784\u9020\u51fd\u6570\n pub fn init(self: *Self, x: i32) void {\n self.val = x;\n self.next = null;\n self.prev = null;\n }\n };\n}\n
\u56fe 4-8 \u00a0 \u5e38\u89c1\u94fe\u8868\u79cd\u7c7b
"},{"location":"chapter_array_and_linkedlist/linked_list/#424","title":"4.2.4 \u00a0 \u94fe\u8868\u5178\u578b\u5e94\u7528","text":"\u5355\u5411\u94fe\u8868\u901a\u5e38\u7528\u4e8e\u5b9e\u73b0\u6808\u3001\u961f\u5217\u3001\u54c8\u5e0c\u8868\u548c\u56fe\u7b49\u6570\u636e\u7ed3\u6784\u3002
\u53cc\u5411\u94fe\u8868\u5e38\u7528\u4e8e\u9700\u8981\u5feb\u901f\u67e5\u627e\u524d\u4e00\u4e2a\u548c\u540e\u4e00\u4e2a\u5143\u7d20\u7684\u573a\u666f\u3002
\u73af\u5f62\u94fe\u8868\u5e38\u7528\u4e8e\u9700\u8981\u5468\u671f\u6027\u64cd\u4f5c\u7684\u573a\u666f\uff0c\u6bd4\u5982\u64cd\u4f5c\u7cfb\u7edf\u7684\u8d44\u6e90\u8c03\u5ea6\u3002
\u300c\u5217\u8868 list\u300d\u662f\u4e00\u4e2a\u62bd\u8c61\u7684\u6570\u636e\u7ed3\u6784\u6982\u5ff5\uff0c\u5b83\u8868\u793a\u5143\u7d20\u7684\u6709\u5e8f\u96c6\u5408\uff0c\u652f\u6301\u5143\u7d20\u8bbf\u95ee\u3001\u4fee\u6539\u3001\u6dfb\u52a0\u3001\u5220\u9664\u548c\u904d\u5386\u7b49\u64cd\u4f5c\uff0c\u65e0\u987b\u4f7f\u7528\u8005\u8003\u8651\u5bb9\u91cf\u9650\u5236\u7684\u95ee\u9898\u3002\u5217\u8868\u53ef\u4ee5\u57fa\u4e8e\u94fe\u8868\u6216\u6570\u7ec4\u5b9e\u73b0\u3002
\u5f53\u4f7f\u7528\u6570\u7ec4\u5b9e\u73b0\u5217\u8868\u65f6\uff0c\u957f\u5ea6\u4e0d\u53ef\u53d8\u7684\u6027\u8d28\u4f1a\u5bfc\u81f4\u5217\u8868\u7684\u5b9e\u7528\u6027\u964d\u4f4e\u3002\u8fd9\u662f\u56e0\u4e3a\u6211\u4eec\u901a\u5e38\u65e0\u6cd5\u4e8b\u5148\u786e\u5b9a\u9700\u8981\u5b58\u50a8\u591a\u5c11\u6570\u636e\uff0c\u4ece\u800c\u96be\u4ee5\u9009\u62e9\u5408\u9002\u7684\u5217\u8868\u957f\u5ea6\u3002\u82e5\u957f\u5ea6\u8fc7\u5c0f\uff0c\u5219\u5f88\u53ef\u80fd\u65e0\u6cd5\u6ee1\u8db3\u4f7f\u7528\u9700\u6c42\uff1b\u82e5\u957f\u5ea6\u8fc7\u5927\uff0c\u5219\u4f1a\u9020\u6210\u5185\u5b58\u7a7a\u95f4\u7684\u6d6a\u8d39\u3002
\u4e3a\u89e3\u51b3\u6b64\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u300c\u52a8\u6001\u6570\u7ec4 dynamic array\u300d\u6765\u5b9e\u73b0\u5217\u8868\u3002\u5b83\u7ee7\u627f\u4e86\u6570\u7ec4\u7684\u5404\u9879\u4f18\u70b9\uff0c\u5e76\u4e14\u53ef\u4ee5\u5728\u7a0b\u5e8f\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u8fdb\u884c\u52a8\u6001\u6269\u5bb9\u3002
\u5b9e\u9645\u4e0a\uff0c\u8bb8\u591a\u7f16\u7a0b\u8bed\u8a00\u4e2d\u7684\u6807\u51c6\u5e93\u63d0\u4f9b\u7684\u5217\u8868\u90fd\u662f\u57fa\u4e8e\u52a8\u6001\u6570\u7ec4\u5b9e\u73b0\u7684\uff0c\u4f8b\u5982 Python \u4e2d\u7684 list
\u3001Java \u4e2d\u7684 ArrayList
\u3001C++ \u4e2d\u7684 vector
\u548c C# \u4e2d\u7684 List
\u7b49\u3002\u5728\u63a5\u4e0b\u6765\u7684\u8ba8\u8bba\u4e2d\uff0c\u6211\u4eec\u5c06\u628a\u201c\u5217\u8868\u201d\u548c\u201c\u52a8\u6001\u6570\u7ec4\u201d\u89c6\u4e3a\u7b49\u540c\u7684\u6982\u5ff5\u3002
\u6211\u4eec\u901a\u5e38\u4f7f\u7528\u201c\u65e0\u521d\u59cb\u503c\u201d\u548c\u201c\u6709\u521d\u59cb\u503c\u201d\u8fd9\u4e24\u79cd\u521d\u59cb\u5316\u65b9\u6cd5\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig list.py# \u521d\u59cb\u5316\u5217\u8868\n# \u65e0\u521d\u59cb\u503c\nnums1: list[int] = []\n# \u6709\u521d\u59cb\u503c\nnums: list[int] = [1, 3, 2, 5, 4]\n
list.cpp/* \u521d\u59cb\u5316\u5217\u8868 */\n// \u9700\u6ce8\u610f\uff0cC++ \u4e2d vector \u5373\u662f\u672c\u6587\u63cf\u8ff0\u7684 nums\n// \u65e0\u521d\u59cb\u503c\nvector<int> nums1;\n// \u6709\u521d\u59cb\u503c\nvector<int> nums = { 1, 3, 2, 5, 4 };\n
list.java/* \u521d\u59cb\u5316\u5217\u8868 */\n// \u65e0\u521d\u59cb\u503c\nList<Integer> nums1 = new ArrayList<>();\n// \u6709\u521d\u59cb\u503c\uff08\u6ce8\u610f\u6570\u7ec4\u7684\u5143\u7d20\u7c7b\u578b\u9700\u4e3a int[] \u7684\u5305\u88c5\u7c7b Integer[]\uff09\nInteger[] numbers = new Integer[] { 1, 3, 2, 5, 4 };\nList<Integer> nums = new ArrayList<>(Arrays.asList(numbers));\n
list.cs/* \u521d\u59cb\u5316\u5217\u8868 */\n// \u65e0\u521d\u59cb\u503c\nList<int> nums1 = [];\n// \u6709\u521d\u59cb\u503c\nint[] numbers = [1, 3, 2, 5, 4];\nList<int> nums = [.. numbers];\n
list_test.go/* \u521d\u59cb\u5316\u5217\u8868 */\n// \u65e0\u521d\u59cb\u503c\nnums1 := []int{}\n// \u6709\u521d\u59cb\u503c\nnums := []int{1, 3, 2, 5, 4}\n
list.swift/* \u521d\u59cb\u5316\u5217\u8868 */\n// \u65e0\u521d\u59cb\u503c\nlet nums1: [Int] = []\n// \u6709\u521d\u59cb\u503c\nvar nums = [1, 3, 2, 5, 4]\n
list.js/* \u521d\u59cb\u5316\u5217\u8868 */\n// \u65e0\u521d\u59cb\u503c\nconst nums1 = [];\n// \u6709\u521d\u59cb\u503c\nconst nums = [1, 3, 2, 5, 4];\n
list.ts/* \u521d\u59cb\u5316\u5217\u8868 */\n// \u65e0\u521d\u59cb\u503c\nconst nums1: number[] = [];\n// \u6709\u521d\u59cb\u503c\nconst nums: number[] = [1, 3, 2, 5, 4];\n
list.dart/* \u521d\u59cb\u5316\u5217\u8868 */\n// \u65e0\u521d\u59cb\u503c\nList<int> nums1 = [];\n// \u6709\u521d\u59cb\u503c\nList<int> nums = [1, 3, 2, 5, 4];\n
list.rs/* \u521d\u59cb\u5316\u5217\u8868 */\n// \u65e0\u521d\u59cb\u503c\nlet nums1: Vec<i32> = Vec::new();\n// \u6709\u521d\u59cb\u503c\nlet nums: Vec<i32> = vec![1, 3, 2, 5, 4];\n
list.c// C \u672a\u63d0\u4f9b\u5185\u7f6e\u52a8\u6001\u6570\u7ec4\n
list.zig// \u521d\u59cb\u5316\u5217\u8868\nvar nums = std.ArrayList(i32).init(std.heap.page_allocator);\ndefer nums.deinit();\ntry nums.appendSlice(&[_]i32{ 1, 3, 2, 5, 4 });\n
"},{"location":"chapter_array_and_linkedlist/list/#2","title":"2. \u00a0 \u8bbf\u95ee\u5143\u7d20","text":"\u5217\u8868\u672c\u8d28\u4e0a\u662f\u6570\u7ec4\uff0c\u56e0\u6b64\u53ef\u4ee5\u5728 \\(O(1)\\) \u65f6\u95f4\u5185\u8bbf\u95ee\u548c\u66f4\u65b0\u5143\u7d20\uff0c\u6548\u7387\u5f88\u9ad8\u3002
PythonC++JavaC#GoSwiftJSTSDartRustCZig list.py# \u8bbf\u95ee\u5143\u7d20\nnum: int = nums[1] # \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n\n# \u66f4\u65b0\u5143\u7d20\nnums[1] = 0 # \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n
list.cpp/* \u8bbf\u95ee\u5143\u7d20 */\nint num = nums[1]; // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n\n/* \u66f4\u65b0\u5143\u7d20 */\nnums[1] = 0; // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n
list.java/* \u8bbf\u95ee\u5143\u7d20 */\nint num = nums.get(1); // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n\n/* \u66f4\u65b0\u5143\u7d20 */\nnums.set(1, 0); // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n
list.cs/* \u8bbf\u95ee\u5143\u7d20 */\nint num = nums[1]; // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n\n/* \u66f4\u65b0\u5143\u7d20 */\nnums[1] = 0; // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n
list_test.go/* \u8bbf\u95ee\u5143\u7d20 */\nnum := nums[1] // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n\n/* \u66f4\u65b0\u5143\u7d20 */\nnums[1] = 0 // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n
list.swift/* \u8bbf\u95ee\u5143\u7d20 */\nlet num = nums[1] // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n\n/* \u66f4\u65b0\u5143\u7d20 */\nnums[1] = 0 // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n
list.js/* \u8bbf\u95ee\u5143\u7d20 */\nconst num = nums[1]; // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n\n/* \u66f4\u65b0\u5143\u7d20 */\nnums[1] = 0; // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n
list.ts/* \u8bbf\u95ee\u5143\u7d20 */\nconst num: number = nums[1]; // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n\n/* \u66f4\u65b0\u5143\u7d20 */\nnums[1] = 0; // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n
list.dart/* \u8bbf\u95ee\u5143\u7d20 */\nint num = nums[1]; // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n\n/* \u66f4\u65b0\u5143\u7d20 */\nnums[1] = 0; // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n
list.rs/* \u8bbf\u95ee\u5143\u7d20 */\nlet num: i32 = nums[1]; // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n/* \u66f4\u65b0\u5143\u7d20 */\nnums[1] = 0; // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n
list.c// C \u672a\u63d0\u4f9b\u5185\u7f6e\u52a8\u6001\u6570\u7ec4\n
list.zig// \u8bbf\u95ee\u5143\u7d20\nvar num = nums.items[1]; // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n\n// \u66f4\u65b0\u5143\u7d20\nnums.items[1] = 0; // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0 \n
"},{"location":"chapter_array_and_linkedlist/list/#3","title":"3. \u00a0 \u63d2\u5165\u4e0e\u5220\u9664\u5143\u7d20","text":"\u76f8\u8f83\u4e8e\u6570\u7ec4\uff0c\u5217\u8868\u53ef\u4ee5\u81ea\u7531\u5730\u6dfb\u52a0\u4e0e\u5220\u9664\u5143\u7d20\u3002\u5728\u5217\u8868\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(1)\\) \uff0c\u4f46\u63d2\u5165\u548c\u5220\u9664\u5143\u7d20\u7684\u6548\u7387\u4ecd\u4e0e\u6570\u7ec4\u76f8\u540c\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002
PythonC++JavaC#GoSwiftJSTSDartRustCZig list.py# \u6e05\u7a7a\u5217\u8868\nnums.clear()\n\n# \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20\nnums.append(1)\nnums.append(3)\nnums.append(2)\nnums.append(5)\nnums.append(4)\n\n# \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20\nnums.insert(3, 6) # \u5728\u7d22\u5f15 3 \u5904\u63d2\u5165\u6570\u5b57 6\n\n# \u5220\u9664\u5143\u7d20\nnums.pop(3) # \u5220\u9664\u7d22\u5f15 3 \u5904\u7684\u5143\u7d20\n
list.cpp/* \u6e05\u7a7a\u5217\u8868 */\nnums.clear();\n\n/* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nnums.push_back(1);\nnums.push_back(3);\nnums.push_back(2);\nnums.push_back(5);\nnums.push_back(4);\n\n/* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nnums.insert(nums.begin() + 3, 6); // \u5728\u7d22\u5f15 3 \u5904\u63d2\u5165\u6570\u5b57 6\n\n/* \u5220\u9664\u5143\u7d20 */\nnums.erase(nums.begin() + 3); // \u5220\u9664\u7d22\u5f15 3 \u5904\u7684\u5143\u7d20\n
list.java/* \u6e05\u7a7a\u5217\u8868 */\nnums.clear();\n\n/* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nnums.add(1);\nnums.add(3);\nnums.add(2);\nnums.add(5);\nnums.add(4);\n\n/* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nnums.add(3, 6); // \u5728\u7d22\u5f15 3 \u5904\u63d2\u5165\u6570\u5b57 6\n\n/* \u5220\u9664\u5143\u7d20 */\nnums.remove(3); // \u5220\u9664\u7d22\u5f15 3 \u5904\u7684\u5143\u7d20\n
list.cs/* \u6e05\u7a7a\u5217\u8868 */\nnums.Clear();\n\n/* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nnums.Add(1);\nnums.Add(3);\nnums.Add(2);\nnums.Add(5);\nnums.Add(4);\n\n/* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nnums.Insert(3, 6);\n\n/* \u5220\u9664\u5143\u7d20 */\nnums.RemoveAt(3);\n
list_test.go/* \u6e05\u7a7a\u5217\u8868 */\nnums = nil\n\n/* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nnums = append(nums, 1)\nnums = append(nums, 3)\nnums = append(nums, 2)\nnums = append(nums, 5)\nnums = append(nums, 4)\n\n/* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nnums = append(nums[:3], append([]int{6}, nums[3:]...)...) // \u5728\u7d22\u5f15 3 \u5904\u63d2\u5165\u6570\u5b57 6\n\n/* \u5220\u9664\u5143\u7d20 */\nnums = append(nums[:3], nums[4:]...) // \u5220\u9664\u7d22\u5f15 3 \u5904\u7684\u5143\u7d20\n
list.swift/* \u6e05\u7a7a\u5217\u8868 */\nnums.removeAll()\n\n/* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nnums.append(1)\nnums.append(3)\nnums.append(2)\nnums.append(5)\nnums.append(4)\n\n/* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nnums.insert(6, at: 3) // \u5728\u7d22\u5f15 3 \u5904\u63d2\u5165\u6570\u5b57 6\n\n/* \u5220\u9664\u5143\u7d20 */\nnums.remove(at: 3) // \u5220\u9664\u7d22\u5f15 3 \u5904\u7684\u5143\u7d20\n
list.js/* \u6e05\u7a7a\u5217\u8868 */\nnums.length = 0;\n\n/* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nnums.push(1);\nnums.push(3);\nnums.push(2);\nnums.push(5);\nnums.push(4);\n\n/* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nnums.splice(3, 0, 6);\n\n/* \u5220\u9664\u5143\u7d20 */\nnums.splice(3, 1);\n
list.ts/* \u6e05\u7a7a\u5217\u8868 */\nnums.length = 0;\n\n/* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nnums.push(1);\nnums.push(3);\nnums.push(2);\nnums.push(5);\nnums.push(4);\n\n/* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nnums.splice(3, 0, 6);\n\n/* \u5220\u9664\u5143\u7d20 */\nnums.splice(3, 1);\n
list.dart/* \u6e05\u7a7a\u5217\u8868 */\nnums.clear();\n\n/* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nnums.add(1);\nnums.add(3);\nnums.add(2);\nnums.add(5);\nnums.add(4);\n\n/* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nnums.insert(3, 6); // \u5728\u7d22\u5f15 3 \u5904\u63d2\u5165\u6570\u5b57 6\n\n/* \u5220\u9664\u5143\u7d20 */\nnums.removeAt(3); // \u5220\u9664\u7d22\u5f15 3 \u5904\u7684\u5143\u7d20\n
list.rs/* \u6e05\u7a7a\u5217\u8868 */\nnums.clear();\n\n/* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nnums.push(1);\nnums.push(3);\nnums.push(2);\nnums.push(5);\nnums.push(4);\n\n/* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nnums.insert(3, 6); // \u5728\u7d22\u5f15 3 \u5904\u63d2\u5165\u6570\u5b57 6\n\n/* \u5220\u9664\u5143\u7d20 */\nnums.remove(3); // \u5220\u9664\u7d22\u5f15 3 \u5904\u7684\u5143\u7d20\n
list.c// C \u672a\u63d0\u4f9b\u5185\u7f6e\u52a8\u6001\u6570\u7ec4\n
list.zig// \u6e05\u7a7a\u5217\u8868\nnums.clearRetainingCapacity();\n\n// \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20\ntry nums.append(1);\ntry nums.append(3);\ntry nums.append(2);\ntry nums.append(5);\ntry nums.append(4);\n\n// \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20\ntry nums.insert(3, 6); // \u5728\u7d22\u5f15 3 \u5904\u63d2\u5165\u6570\u5b57 6\n\n// \u5220\u9664\u5143\u7d20\n_ = nums.orderedRemove(3); // \u5220\u9664\u7d22\u5f15 3 \u5904\u7684\u5143\u7d20\n
"},{"location":"chapter_array_and_linkedlist/list/#4","title":"4. \u00a0 \u904d\u5386\u5217\u8868","text":"\u4e0e\u6570\u7ec4\u4e00\u6837\uff0c\u5217\u8868\u53ef\u4ee5\u6839\u636e\u7d22\u5f15\u904d\u5386\uff0c\u4e5f\u53ef\u4ee5\u76f4\u63a5\u904d\u5386\u5404\u5143\u7d20\u3002
PythonC++JavaC#GoSwiftJSTSDartRustCZig list.py# \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868\ncount = 0\nfor i in range(len(nums)):\n count += nums[i]\n\n# \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20\nfor num in nums:\n count += num\n
list.cpp/* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\nint count = 0;\nfor (int i = 0; i < nums.size(); i++) {\n count += nums[i];\n}\n\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\ncount = 0;\nfor (int num : nums) {\n count += num;\n}\n
list.java/* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\nint count = 0;\nfor (int i = 0; i < nums.size(); i++) {\n count += nums.get(i);\n}\n\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\nfor (int num : nums) {\n count += num;\n}\n
list.cs/* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\nint count = 0;\nfor (int i = 0; i < nums.Count; i++) {\n count += nums[i];\n}\n\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\ncount = 0;\nforeach (int num in nums) {\n count += num;\n}\n
list_test.go/* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\ncount := 0\nfor i := 0; i < len(nums); i++ {\n count += nums[i]\n}\n\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\ncount = 0\nfor _, num := range nums {\n count += num\n}\n
list.swift/* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\nvar count = 0\nfor i in nums.indices {\n count += nums[i]\n}\n\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\ncount = 0\nfor num in nums {\n count += num\n}\n
list.js/* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\nlet count = 0;\nfor (let i = 0; i < nums.length; i++) {\n count += nums[i];\n}\n\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\ncount = 0;\nfor (const num of nums) {\n count += num;\n}\n
list.ts/* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\nlet count = 0;\nfor (let i = 0; i < nums.length; i++) {\n count += nums[i];\n}\n\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\ncount = 0;\nfor (const num of nums) {\n count += num;\n}\n
list.dart/* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\nint count = 0;\nfor (var i = 0; i < nums.length; i++) {\n count += nums[i];\n}\n\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\ncount = 0;\nfor (var num in nums) {\n count += num;\n}\n
list.rs// \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868\nlet mut _count = 0;\nfor i in 0..nums.len() {\n _count += nums[i];\n}\n\n// \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20\n_count = 0;\nfor num in &nums {\n _count += num;\n}\n
list.c// C \u672a\u63d0\u4f9b\u5185\u7f6e\u52a8\u6001\u6570\u7ec4\n
list.zig// \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868\nvar count: i32 = 0;\nvar i: i32 = 0;\nwhile (i < nums.items.len) : (i += 1) {\n count += nums[i];\n}\n\n// \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20\ncount = 0;\nfor (nums.items) |num| {\n count += num;\n}\n
"},{"location":"chapter_array_and_linkedlist/list/#5","title":"5. \u00a0 \u62fc\u63a5\u5217\u8868","text":"\u7ed9\u5b9a\u4e00\u4e2a\u65b0\u5217\u8868 nums1
\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5176\u62fc\u63a5\u5230\u539f\u5217\u8868\u7684\u5c3e\u90e8\u3002
# \u62fc\u63a5\u4e24\u4e2a\u5217\u8868\nnums1: list[int] = [6, 8, 7, 10, 9]\nnums += nums1 # \u5c06\u5217\u8868 nums1 \u62fc\u63a5\u5230 nums \u4e4b\u540e\n
list.cpp/* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nvector<int> nums1 = { 6, 8, 7, 10, 9 };\n// \u5c06\u5217\u8868 nums1 \u62fc\u63a5\u5230 nums \u4e4b\u540e\nnums.insert(nums.end(), nums1.begin(), nums1.end());\n
list.java/* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nList<Integer> nums1 = new ArrayList<>(Arrays.asList(new Integer[] { 6, 8, 7, 10, 9 }));\nnums.addAll(nums1); // \u5c06\u5217\u8868 nums1 \u62fc\u63a5\u5230 nums \u4e4b\u540e\n
list.cs/* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nList<int> nums1 = [6, 8, 7, 10, 9];\nnums.AddRange(nums1); // \u5c06\u5217\u8868 nums1 \u62fc\u63a5\u5230 nums \u4e4b\u540e\n
list_test.go/* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nnums1 := []int{6, 8, 7, 10, 9}\nnums = append(nums, nums1...) // \u5c06\u5217\u8868 nums1 \u62fc\u63a5\u5230 nums \u4e4b\u540e\n
list.swift/* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nlet nums1 = [6, 8, 7, 10, 9]\nnums.append(contentsOf: nums1) // \u5c06\u5217\u8868 nums1 \u62fc\u63a5\u5230 nums \u4e4b\u540e\n
list.js/* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nconst nums1 = [6, 8, 7, 10, 9];\nnums.push(...nums1); // \u5c06\u5217\u8868 nums1 \u62fc\u63a5\u5230 nums \u4e4b\u540e\n
list.ts/* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nconst nums1: number[] = [6, 8, 7, 10, 9];\nnums.push(...nums1); // \u5c06\u5217\u8868 nums1 \u62fc\u63a5\u5230 nums \u4e4b\u540e\n
list.dart/* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nList<int> nums1 = [6, 8, 7, 10, 9];\nnums.addAll(nums1); // \u5c06\u5217\u8868 nums1 \u62fc\u63a5\u5230 nums \u4e4b\u540e\n
list.rs/* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nlet nums1: Vec<i32> = vec![6, 8, 7, 10, 9];\nnums.extend(nums1);\n
list.c// C \u672a\u63d0\u4f9b\u5185\u7f6e\u52a8\u6001\u6570\u7ec4\n
list.zig// \u62fc\u63a5\u4e24\u4e2a\u5217\u8868\nvar nums1 = std.ArrayList(i32).init(std.heap.page_allocator);\ndefer nums1.deinit();\ntry nums1.appendSlice(&[_]i32{ 6, 8, 7, 10, 9 });\ntry nums.insertSlice(nums.items.len, nums1.items); // \u5c06\u5217\u8868 nums1 \u62fc\u63a5\u5230 nums \u4e4b\u540e\n
"},{"location":"chapter_array_and_linkedlist/list/#6","title":"6. \u00a0 \u6392\u5e8f\u5217\u8868","text":"\u5b8c\u6210\u5217\u8868\u6392\u5e8f\u540e\uff0c\u6211\u4eec\u4fbf\u53ef\u4ee5\u4f7f\u7528\u5728\u6570\u7ec4\u7c7b\u7b97\u6cd5\u9898\u4e2d\u7ecf\u5e38\u8003\u67e5\u7684\u201c\u4e8c\u5206\u67e5\u627e\u201d\u548c\u201c\u53cc\u6307\u9488\u201d\u7b97\u6cd5\u3002
PythonC++JavaC#GoSwiftJSTSDartRustCZig list.py# \u6392\u5e8f\u5217\u8868\nnums.sort() # \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n
list.cpp/* \u6392\u5e8f\u5217\u8868 */\nsort(nums.begin(), nums.end()); // \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n
list.java/* \u6392\u5e8f\u5217\u8868 */\nCollections.sort(nums); // \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n
list.cs/* \u6392\u5e8f\u5217\u8868 */\nnums.Sort(); // \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n
list_test.go/* \u6392\u5e8f\u5217\u8868 */\nsort.Ints(nums) // \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n
list.swift/* \u6392\u5e8f\u5217\u8868 */\nnums.sort() // \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n
list.js/* \u6392\u5e8f\u5217\u8868 */ \nnums.sort((a, b) => a - b); // \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n
list.ts/* \u6392\u5e8f\u5217\u8868 */\nnums.sort((a, b) => a - b); // \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n
list.dart/* \u6392\u5e8f\u5217\u8868 */\nnums.sort(); // \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n
list.rs/* \u6392\u5e8f\u5217\u8868 */\nnums.sort(); // \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n
list.c// C \u672a\u63d0\u4f9b\u5185\u7f6e\u52a8\u6001\u6570\u7ec4\n
list.zig// \u6392\u5e8f\u5217\u8868\nstd.sort.sort(i32, nums.items, {}, comptime std.sort.asc(i32));\n
"},{"location":"chapter_array_and_linkedlist/list/#432","title":"4.3.2 \u00a0 \u5217\u8868\u5b9e\u73b0","text":"\u8bb8\u591a\u7f16\u7a0b\u8bed\u8a00\u5185\u7f6e\u4e86\u5217\u8868\uff0c\u4f8b\u5982 Java\u3001C++\u3001Python \u7b49\u3002\u5b83\u4eec\u7684\u5b9e\u73b0\u6bd4\u8f83\u590d\u6742\uff0c\u5404\u4e2a\u53c2\u6570\u7684\u8bbe\u5b9a\u4e5f\u975e\u5e38\u8003\u7a76\uff0c\u4f8b\u5982\u521d\u59cb\u5bb9\u91cf\u3001\u6269\u5bb9\u500d\u6570\u7b49\u3002\u611f\u5174\u8da3\u7684\u8bfb\u8005\u53ef\u4ee5\u67e5\u9605\u6e90\u7801\u8fdb\u884c\u5b66\u4e60\u3002
\u4e3a\u4e86\u52a0\u6df1\u5bf9\u5217\u8868\u5de5\u4f5c\u539f\u7406\u7684\u7406\u89e3\uff0c\u6211\u4eec\u5c1d\u8bd5\u5b9e\u73b0\u4e00\u4e2a\u7b80\u6613\u7248\u5217\u8868\uff0c\u5305\u62ec\u4ee5\u4e0b\u4e09\u4e2a\u91cd\u70b9\u8bbe\u8ba1\u3002
size
\uff0c\u7528\u4e8e\u8bb0\u5f55\u5217\u8868\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff0c\u5e76\u968f\u7740\u5143\u7d20\u63d2\u5165\u548c\u5220\u9664\u5b9e\u65f6\u66f4\u65b0\u3002\u6839\u636e\u6b64\u53d8\u91cf\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9a\u4f4d\u5217\u8868\u5c3e\u90e8\uff0c\u4ee5\u53ca\u5224\u65ad\u662f\u5426\u9700\u8981\u6269\u5bb9\u3002class MyList:\n \"\"\"\u5217\u8868\u7c7b\"\"\"\n\n def __init__(self):\n \"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n self._capacity: int = 10 # \u5217\u8868\u5bb9\u91cf\n self._arr: list[int] = [0] * self._capacity # \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\n self._size: int = 0 # \u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\n self._extend_ratio: int = 2 # \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n\n def size(self) -> int:\n \"\"\"\u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\"\"\"\n return self._size\n\n def capacity(self) -> int:\n \"\"\"\u83b7\u53d6\u5217\u8868\u5bb9\u91cf\"\"\"\n return self._capacity\n\n def get(self, index: int) -> int:\n \"\"\"\u8bbf\u95ee\u5143\u7d20\"\"\"\n # \u7d22\u5f15\u5982\u679c\u8d8a\u754c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\n if index < 0 or index >= self._size:\n raise IndexError(\"\u7d22\u5f15\u8d8a\u754c\")\n return self._arr[index]\n\n def set(self, num: int, index: int):\n \"\"\"\u66f4\u65b0\u5143\u7d20\"\"\"\n if index < 0 or index >= self._size:\n raise IndexError(\"\u7d22\u5f15\u8d8a\u754c\")\n self._arr[index] = num\n\n def add(self, num: int):\n \"\"\"\u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20\"\"\"\n # \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if self.size() == self.capacity():\n self.extend_capacity()\n self._arr[self._size] = num\n self._size += 1\n\n def insert(self, num: int, index: int):\n \"\"\"\u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20\"\"\"\n if index < 0 or index >= self._size:\n raise IndexError(\"\u7d22\u5f15\u8d8a\u754c\")\n # \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if self._size == self.capacity():\n self.extend_capacity()\n # \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for j in range(self._size - 1, index - 1, -1):\n self._arr[j + 1] = self._arr[j]\n self._arr[index] = num\n # \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n self._size += 1\n\n def remove(self, index: int) -> int:\n \"\"\"\u5220\u9664\u5143\u7d20\"\"\"\n if index < 0 or index >= self._size:\n raise IndexError(\"\u7d22\u5f15\u8d8a\u754c\")\n num = self._arr[index]\n # \u7d22\u5f15 i \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for j in range(index, self._size - 1):\n self._arr[j] = self._arr[j + 1]\n # \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n self._size -= 1\n # \u8fd4\u56de\u88ab\u5220\u9664\u5143\u7d20\n return num\n\n def extend_capacity(self):\n \"\"\"\u5217\u8868\u6269\u5bb9\"\"\"\n # \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a\u539f\u6570\u7ec4 __extend_ratio \u500d\u7684\u65b0\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u62f7\u8d1d\u5230\u65b0\u6570\u7ec4\n self._arr = self._arr + [0] * self.capacity() * (self._extend_ratio - 1)\n # \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\n self._capacity = len(self._arr)\n\n def to_array(self) -> list[int]:\n \"\"\"\u8fd4\u56de\u6709\u6548\u957f\u5ea6\u7684\u5217\u8868\"\"\"\n return self._arr[: self._size]\n
my_list.cpp/* \u5217\u8868\u7c7b */\nclass MyList {\n private:\n int *arr; // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\n int arrCapacity = 10; // \u5217\u8868\u5bb9\u91cf\n int arrSize = 0; // \u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\n int extendRatio = 2; // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n\n public:\n /* \u6784\u9020\u65b9\u6cd5 */\n MyList() {\n arr = new int[arrCapacity];\n }\n\n /* \u6790\u6784\u65b9\u6cd5 */\n ~MyList() {\n delete[] arr;\n }\n\n /* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09*/\n int size() {\n return arrSize;\n }\n\n /* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\n int capacity() {\n return arrCapacity;\n }\n\n /* \u8bbf\u95ee\u5143\u7d20 */\n int get(int index) {\n // \u7d22\u5f15\u5982\u679c\u8d8a\u754c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\n if (index < 0 || index >= size())\n throw out_of_range(\"\u7d22\u5f15\u8d8a\u754c\");\n return arr[index];\n }\n\n /* \u66f4\u65b0\u5143\u7d20 */\n void set(int index, int num) {\n if (index < 0 || index >= size())\n throw out_of_range(\"\u7d22\u5f15\u8d8a\u754c\");\n arr[index] = num;\n }\n\n /* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\n void add(int num) {\n // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if (size() == capacity())\n extendCapacity();\n arr[size()] = num;\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n arrSize++;\n }\n\n /* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\n void insert(int index, int num) {\n if (index < 0 || index >= size())\n throw out_of_range(\"\u7d22\u5f15\u8d8a\u754c\");\n // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if (size() == capacity())\n extendCapacity();\n // \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for (int j = size() - 1; j >= index; j--) {\n arr[j + 1] = arr[j];\n }\n arr[index] = num;\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n arrSize++;\n }\n\n /* \u5220\u9664\u5143\u7d20 */\n int remove(int index) {\n if (index < 0 || index >= size())\n throw out_of_range(\"\u7d22\u5f15\u8d8a\u754c\");\n int num = arr[index];\n // \u7d22\u5f15 i \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for (int j = index; j < size() - 1; j++) {\n arr[j] = arr[j + 1];\n }\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n arrSize--;\n // \u8fd4\u56de\u88ab\u5220\u9664\u5143\u7d20\n return num;\n }\n\n /* \u5217\u8868\u6269\u5bb9 */\n void extendCapacity() {\n // \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a\u539f\u6570\u7ec4 extendRatio \u500d\u7684\u65b0\u6570\u7ec4\n int newCapacity = capacity() * extendRatio;\n int *tmp = arr;\n arr = new int[newCapacity];\n // \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\n for (int i = 0; i < size(); i++) {\n arr[i] = tmp[i];\n }\n // \u91ca\u653e\u5185\u5b58\n delete[] tmp;\n arrCapacity = newCapacity;\n }\n\n /* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a Vector \u7528\u4e8e\u6253\u5370 */\n vector<int> toVector() {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n vector<int> vec(size());\n for (int i = 0; i < size(); i++) {\n vec[i] = arr[i];\n }\n return vec;\n }\n};\n
my_list.java/* \u5217\u8868\u7c7b */\nclass MyList {\n private int[] arr; // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\n private int capacity = 10; // \u5217\u8868\u5bb9\u91cf\n private int size = 0; // \u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\n private int extendRatio = 2; // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n\n /* \u6784\u9020\u65b9\u6cd5 */\n public MyList() {\n arr = new int[capacity];\n }\n\n /* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09 */\n public int size() {\n return size;\n }\n\n /* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\n public int capacity() {\n return capacity;\n }\n\n /* \u8bbf\u95ee\u5143\u7d20 */\n public int get(int index) {\n // \u7d22\u5f15\u5982\u679c\u8d8a\u754c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\n if (index < 0 || index >= size)\n throw new IndexOutOfBoundsException(\"\u7d22\u5f15\u8d8a\u754c\");\n return arr[index];\n }\n\n /* \u66f4\u65b0\u5143\u7d20 */\n public void set(int index, int num) {\n if (index < 0 || index >= size)\n throw new IndexOutOfBoundsException(\"\u7d22\u5f15\u8d8a\u754c\");\n arr[index] = num;\n }\n\n /* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\n public void add(int num) {\n // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if (size == capacity())\n extendCapacity();\n arr[size] = num;\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n size++;\n }\n\n /* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\n public void insert(int index, int num) {\n if (index < 0 || index >= size)\n throw new IndexOutOfBoundsException(\"\u7d22\u5f15\u8d8a\u754c\");\n // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if (size == capacity())\n extendCapacity();\n // \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for (int j = size - 1; j >= index; j--) {\n arr[j + 1] = arr[j];\n }\n arr[index] = num;\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n size++;\n }\n\n /* \u5220\u9664\u5143\u7d20 */\n public int remove(int index) {\n if (index < 0 || index >= size)\n throw new IndexOutOfBoundsException(\"\u7d22\u5f15\u8d8a\u754c\");\n int num = arr[index];\n // \u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for (int j = index; j < size - 1; j++) {\n arr[j] = arr[j + 1];\n }\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n size--;\n // \u8fd4\u56de\u88ab\u5220\u9664\u5143\u7d20\n return num;\n }\n\n /* \u5217\u8868\u6269\u5bb9 */\n public void extendCapacity() {\n // \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a\u539f\u6570\u7ec4 extendRatio \u500d\u7684\u65b0\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u62f7\u8d1d\u5230\u65b0\u6570\u7ec4\n arr = Arrays.copyOf(arr, capacity() * extendRatio);\n // \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\n capacity = arr.length;\n }\n\n /* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4 */\n public int[] toArray() {\n int size = size();\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n int[] arr = new int[size];\n for (int i = 0; i < size; i++) {\n arr[i] = get(i);\n }\n return arr;\n }\n}\n
my_list.cs/* \u5217\u8868\u7c7b */\nclass MyList {\n private int[] arr; // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\n private int arrCapacity = 10; // \u5217\u8868\u5bb9\u91cf\n private int arrSize = 0; // \u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\n private readonly int extendRatio = 2; // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n\n /* \u6784\u9020\u65b9\u6cd5 */\n public MyList() {\n arr = new int[arrCapacity];\n }\n\n /* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09*/\n public int Size() {\n return arrSize;\n }\n\n /* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\n public int Capacity() {\n return arrCapacity;\n }\n\n /* \u8bbf\u95ee\u5143\u7d20 */\n public int Get(int index) {\n // \u7d22\u5f15\u5982\u679c\u8d8a\u754c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\n if (index < 0 || index >= arrSize)\n throw new IndexOutOfRangeException(\"\u7d22\u5f15\u8d8a\u754c\");\n return arr[index];\n }\n\n /* \u66f4\u65b0\u5143\u7d20 */\n public void Set(int index, int num) {\n if (index < 0 || index >= arrSize)\n throw new IndexOutOfRangeException(\"\u7d22\u5f15\u8d8a\u754c\");\n arr[index] = num;\n }\n\n /* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\n public void Add(int num) {\n // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if (arrSize == arrCapacity)\n ExtendCapacity();\n arr[arrSize] = num;\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n arrSize++;\n }\n\n /* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\n public void Insert(int index, int num) {\n if (index < 0 || index >= arrSize)\n throw new IndexOutOfRangeException(\"\u7d22\u5f15\u8d8a\u754c\");\n // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if (arrSize == arrCapacity)\n ExtendCapacity();\n // \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for (int j = arrSize - 1; j >= index; j--) {\n arr[j + 1] = arr[j];\n }\n arr[index] = num;\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n arrSize++;\n }\n\n /* \u5220\u9664\u5143\u7d20 */\n public int Remove(int index) {\n if (index < 0 || index >= arrSize)\n throw new IndexOutOfRangeException(\"\u7d22\u5f15\u8d8a\u754c\");\n int num = arr[index];\n // \u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for (int j = index; j < arrSize - 1; j++) {\n arr[j] = arr[j + 1];\n }\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n arrSize--;\n // \u8fd4\u56de\u88ab\u5220\u9664\u5143\u7d20\n return num;\n }\n\n /* \u5217\u8868\u6269\u5bb9 */\n public void ExtendCapacity() {\n // \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a arrCapacity * extendRatio \u7684\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u62f7\u8d1d\u5230\u65b0\u6570\u7ec4\n Array.Resize(ref arr, arrCapacity * extendRatio);\n // \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\n arrCapacity = arr.Length;\n }\n\n /* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4 */\n public int[] ToArray() {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n int[] arr = new int[arrSize];\n for (int i = 0; i < arrSize; i++) {\n arr[i] = Get(i);\n }\n return arr;\n }\n}\n
my_list.go/* \u5217\u8868\u7c7b */\ntype myList struct {\n arrCapacity int\n arr []int\n arrSize int\n extendRatio int\n}\n\n/* \u6784\u9020\u51fd\u6570 */\nfunc newMyList() *myList {\n return &myList{\n arrCapacity: 10, // \u5217\u8868\u5bb9\u91cf\n arr: make([]int, 10), // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\n arrSize: 0, // \u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\n extendRatio: 2, // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n }\n}\n\n/* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09 */\nfunc (l *myList) size() int {\n return l.arrSize\n}\n\n/* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\nfunc (l *myList) capacity() int {\n return l.arrCapacity\n}\n\n/* \u8bbf\u95ee\u5143\u7d20 */\nfunc (l *myList) get(index int) int {\n // \u7d22\u5f15\u5982\u679c\u8d8a\u754c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\n if index < 0 || index >= l.arrSize {\n panic(\"\u7d22\u5f15\u8d8a\u754c\")\n }\n return l.arr[index]\n}\n\n/* \u66f4\u65b0\u5143\u7d20 */\nfunc (l *myList) set(num, index int) {\n if index < 0 || index >= l.arrSize {\n panic(\"\u7d22\u5f15\u8d8a\u754c\")\n }\n l.arr[index] = num\n}\n\n/* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nfunc (l *myList) add(num int) {\n // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if l.arrSize == l.arrCapacity {\n l.extendCapacity()\n }\n l.arr[l.arrSize] = num\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n l.arrSize++\n}\n\n/* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nfunc (l *myList) insert(num, index int) {\n if index < 0 || index >= l.arrSize {\n panic(\"\u7d22\u5f15\u8d8a\u754c\")\n }\n // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if l.arrSize == l.arrCapacity {\n l.extendCapacity()\n }\n // \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for j := l.arrSize - 1; j >= index; j-- {\n l.arr[j+1] = l.arr[j]\n }\n l.arr[index] = num\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n l.arrSize++\n}\n\n/* \u5220\u9664\u5143\u7d20 */\nfunc (l *myList) remove(index int) int {\n if index < 0 || index >= l.arrSize {\n panic(\"\u7d22\u5f15\u8d8a\u754c\")\n }\n num := l.arr[index]\n // \u7d22\u5f15 i \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for j := index; j < l.arrSize-1; j++ {\n l.arr[j] = l.arr[j+1]\n }\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n l.arrSize--\n // \u8fd4\u56de\u88ab\u5220\u9664\u5143\u7d20\n return num\n}\n\n/* \u5217\u8868\u6269\u5bb9 */\nfunc (l *myList) extendCapacity() {\n // \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a\u539f\u6570\u7ec4 extendRatio \u500d\u7684\u65b0\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u62f7\u8d1d\u5230\u65b0\u6570\u7ec4\n l.arr = append(l.arr, make([]int, l.arrCapacity*(l.extendRatio-1))...)\n // \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\n l.arrCapacity = len(l.arr)\n}\n\n/* \u8fd4\u56de\u6709\u6548\u957f\u5ea6\u7684\u5217\u8868 */\nfunc (l *myList) toArray() []int {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n return l.arr[:l.arrSize]\n}\n
my_list.swift/* \u5217\u8868\u7c7b */\nclass MyList {\n private var arr: [Int] // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\n private var _capacity = 10 // \u5217\u8868\u5bb9\u91cf\n private var _size = 0 // \u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\n private let extendRatio = 2 // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n\n /* \u6784\u9020\u65b9\u6cd5 */\n init() {\n arr = Array(repeating: 0, count: _capacity)\n }\n\n /* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09*/\n func size() -> Int {\n _size\n }\n\n /* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\n func capacity() -> Int {\n _capacity\n }\n\n /* \u8bbf\u95ee\u5143\u7d20 */\n func get(index: Int) -> Int {\n // \u7d22\u5f15\u5982\u679c\u8d8a\u754c\u5219\u629b\u51fa\u9519\u8bef\uff0c\u4e0b\u540c\n if index < 0 || index >= _size {\n fatalError(\"\u7d22\u5f15\u8d8a\u754c\")\n }\n return arr[index]\n }\n\n /* \u66f4\u65b0\u5143\u7d20 */\n func set(index: Int, num: Int) {\n if index < 0 || index >= _size {\n fatalError(\"\u7d22\u5f15\u8d8a\u754c\")\n }\n arr[index] = num\n }\n\n /* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\n func add(num: Int) {\n // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if _size == _capacity {\n extendCapacity()\n }\n arr[_size] = num\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n _size += 1\n }\n\n /* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\n func insert(index: Int, num: Int) {\n if index < 0 || index >= _size {\n fatalError(\"\u7d22\u5f15\u8d8a\u754c\")\n }\n // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if _size == _capacity {\n extendCapacity()\n }\n // \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for j in sequence(first: _size - 1, next: { $0 >= index + 1 ? $0 - 1 : nil }) {\n arr[j + 1] = arr[j]\n }\n arr[index] = num\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n _size += 1\n }\n\n /* \u5220\u9664\u5143\u7d20 */\n @discardableResult\n func remove(index: Int) -> Int {\n if index < 0 || index >= _size {\n fatalError(\"\u7d22\u5f15\u8d8a\u754c\")\n }\n let num = arr[index]\n // \u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for j in index ..< (_size - 1) {\n arr[j] = arr[j + 1]\n }\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n _size -= 1\n // \u8fd4\u56de\u88ab\u5220\u9664\u5143\u7d20\n return num\n }\n\n /* \u5217\u8868\u6269\u5bb9 */\n func extendCapacity() {\n // \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a\u539f\u6570\u7ec4 extendRatio \u500d\u7684\u65b0\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u62f7\u8d1d\u5230\u65b0\u6570\u7ec4\n arr = arr + Array(repeating: 0, count: _capacity * (extendRatio - 1))\n // \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\n _capacity = arr.count\n }\n\n /* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4 */\n func toArray() -> [Int] {\n var arr = Array(repeating: 0, count: _size)\n for i in 0 ..< _size {\n arr[i] = get(index: i)\n }\n return arr\n }\n}\n
my_list.js/* \u5217\u8868\u7c7b */\nclass MyList {\n #arr = new Array(); // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\n #capacity = 10; // \u5217\u8868\u5bb9\u91cf\n #size = 0; // \u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\n #extendRatio = 2; // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n\n /* \u6784\u9020\u65b9\u6cd5 */\n constructor() {\n this.#arr = new Array(this.#capacity);\n }\n\n /* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09*/\n size() {\n return this.#size;\n }\n\n /* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\n capacity() {\n return this.#capacity;\n }\n\n /* \u8bbf\u95ee\u5143\u7d20 */\n get(index) {\n // \u7d22\u5f15\u5982\u679c\u8d8a\u754c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\n if (index < 0 || index >= this.#size) throw new Error('\u7d22\u5f15\u8d8a\u754c');\n return this.#arr[index];\n }\n\n /* \u66f4\u65b0\u5143\u7d20 */\n set(index, num) {\n if (index < 0 || index >= this.#size) throw new Error('\u7d22\u5f15\u8d8a\u754c');\n this.#arr[index] = num;\n }\n\n /* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\n add(num) {\n // \u5982\u679c\u957f\u5ea6\u7b49\u4e8e\u5bb9\u91cf\uff0c\u5219\u9700\u8981\u6269\u5bb9\n if (this.#size === this.#capacity) {\n this.extendCapacity();\n }\n // \u5c06\u65b0\u5143\u7d20\u6dfb\u52a0\u5230\u5217\u8868\u5c3e\u90e8\n this.#arr[this.#size] = num;\n this.#size++;\n }\n\n /* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\n insert(index, num) {\n if (index < 0 || index >= this.#size) throw new Error('\u7d22\u5f15\u8d8a\u754c');\n // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if (this.#size === this.#capacity) {\n this.extendCapacity();\n }\n // \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for (let j = this.#size - 1; j >= index; j--) {\n this.#arr[j + 1] = this.#arr[j];\n }\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n this.#arr[index] = num;\n this.#size++;\n }\n\n /* \u5220\u9664\u5143\u7d20 */\n remove(index) {\n if (index < 0 || index >= this.#size) throw new Error('\u7d22\u5f15\u8d8a\u754c');\n let num = this.#arr[index];\n // \u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for (let j = index; j < this.#size - 1; j++) {\n this.#arr[j] = this.#arr[j + 1];\n }\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n this.#size--;\n // \u8fd4\u56de\u88ab\u5220\u9664\u5143\u7d20\n return num;\n }\n\n /* \u5217\u8868\u6269\u5bb9 */\n extendCapacity() {\n // \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a\u539f\u6570\u7ec4 extendRatio \u500d\u7684\u65b0\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u62f7\u8d1d\u5230\u65b0\u6570\u7ec4\n this.#arr = this.#arr.concat(\n new Array(this.capacity() * (this.#extendRatio - 1))\n );\n // \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\n this.#capacity = this.#arr.length;\n }\n\n /* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4 */\n toArray() {\n let size = this.size();\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n const arr = new Array(size);\n for (let i = 0; i < size; i++) {\n arr[i] = this.get(i);\n }\n return arr;\n }\n}\n
my_list.ts/* \u5217\u8868\u7c7b */\nclass MyList {\n private arr: Array<number>; // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\n private _capacity: number = 10; // \u5217\u8868\u5bb9\u91cf\n private _size: number = 0; // \u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\n private extendRatio: number = 2; // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n\n /* \u6784\u9020\u65b9\u6cd5 */\n constructor() {\n this.arr = new Array(this._capacity);\n }\n\n /* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09*/\n public size(): number {\n return this._size;\n }\n\n /* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\n public capacity(): number {\n return this._capacity;\n }\n\n /* \u8bbf\u95ee\u5143\u7d20 */\n public get(index: number): number {\n // \u7d22\u5f15\u5982\u679c\u8d8a\u754c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\n if (index < 0 || index >= this._size) throw new Error('\u7d22\u5f15\u8d8a\u754c');\n return this.arr[index];\n }\n\n /* \u66f4\u65b0\u5143\u7d20 */\n public set(index: number, num: number): void {\n if (index < 0 || index >= this._size) throw new Error('\u7d22\u5f15\u8d8a\u754c');\n this.arr[index] = num;\n }\n\n /* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\n public add(num: number): void {\n // \u5982\u679c\u957f\u5ea6\u7b49\u4e8e\u5bb9\u91cf\uff0c\u5219\u9700\u8981\u6269\u5bb9\n if (this._size === this._capacity) this.extendCapacity();\n // \u5c06\u65b0\u5143\u7d20\u6dfb\u52a0\u5230\u5217\u8868\u5c3e\u90e8\n this.arr[this._size] = num;\n this._size++;\n }\n\n /* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\n public insert(index: number, num: number): void {\n if (index < 0 || index >= this._size) throw new Error('\u7d22\u5f15\u8d8a\u754c');\n // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if (this._size === this._capacity) {\n this.extendCapacity();\n }\n // \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for (let j = this._size - 1; j >= index; j--) {\n this.arr[j + 1] = this.arr[j];\n }\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n this.arr[index] = num;\n this._size++;\n }\n\n /* \u5220\u9664\u5143\u7d20 */\n public remove(index: number): number {\n if (index < 0 || index >= this._size) throw new Error('\u7d22\u5f15\u8d8a\u754c');\n let num = this.arr[index];\n // \u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for (let j = index; j < this._size - 1; j++) {\n this.arr[j] = this.arr[j + 1];\n }\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n this._size--;\n // \u8fd4\u56de\u88ab\u5220\u9664\u5143\u7d20\n return num;\n }\n\n /* \u5217\u8868\u6269\u5bb9 */\n public extendCapacity(): void {\n // \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a size \u7684\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u62f7\u8d1d\u5230\u65b0\u6570\u7ec4\n this.arr = this.arr.concat(\n new Array(this.capacity() * (this.extendRatio - 1))\n );\n // \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\n this._capacity = this.arr.length;\n }\n\n /* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4 */\n public toArray(): number[] {\n let size = this.size();\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n const arr = new Array(size);\n for (let i = 0; i < size; i++) {\n arr[i] = this.get(i);\n }\n return arr;\n }\n}\n
my_list.dart/* \u5217\u8868\u7c7b */\nclass MyList {\n late List<int> _arr; // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\n int _capacity = 10; // \u5217\u8868\u5bb9\u91cf\n int _size = 0; // \u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\n int _extendRatio = 2; // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n\n /* \u6784\u9020\u65b9\u6cd5 */\n MyList() {\n _arr = List.filled(_capacity, 0);\n }\n\n /* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09*/\n int size() => _size;\n\n /* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\n int capacity() => _capacity;\n\n /* \u8bbf\u95ee\u5143\u7d20 */\n int get(int index) {\n if (index >= _size) throw RangeError('\u7d22\u5f15\u8d8a\u754c');\n return _arr[index];\n }\n\n /* \u66f4\u65b0\u5143\u7d20 */\n void set(int index, int _num) {\n if (index >= _size) throw RangeError('\u7d22\u5f15\u8d8a\u754c');\n _arr[index] = _num;\n }\n\n /* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\n void add(int _num) {\n // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if (_size == _capacity) extendCapacity();\n _arr[_size] = _num;\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n _size++;\n }\n\n /* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\n void insert(int index, int _num) {\n if (index >= _size) throw RangeError('\u7d22\u5f15\u8d8a\u754c');\n // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if (_size == _capacity) extendCapacity();\n // \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for (var j = _size - 1; j >= index; j--) {\n _arr[j + 1] = _arr[j];\n }\n _arr[index] = _num;\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n _size++;\n }\n\n /* \u5220\u9664\u5143\u7d20 */\n int remove(int index) {\n if (index >= _size) throw RangeError('\u7d22\u5f15\u8d8a\u754c');\n int _num = _arr[index];\n // \u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for (var j = index; j < _size - 1; j++) {\n _arr[j] = _arr[j + 1];\n }\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n _size--;\n // \u8fd4\u56de\u88ab\u5220\u9664\u5143\u7d20\n return _num;\n }\n\n /* \u5217\u8868\u6269\u5bb9 */\n void extendCapacity() {\n // \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a\u539f\u6570\u7ec4 _extendRatio \u500d\u7684\u65b0\u6570\u7ec4\n final _newNums = List.filled(_capacity * _extendRatio, 0);\n // \u5c06\u539f\u6570\u7ec4\u62f7\u8d1d\u5230\u65b0\u6570\u7ec4\n List.copyRange(_newNums, 0, _arr);\n // \u66f4\u65b0 _arr \u7684\u5f15\u7528\n _arr = _newNums;\n // \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\n _capacity = _arr.length;\n }\n\n /* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4 */\n List<int> toArray() {\n List<int> arr = [];\n for (var i = 0; i < _size; i++) {\n arr.add(get(i));\n }\n return arr;\n }\n}\n
my_list.rs/* \u5217\u8868\u7c7b */\n#[allow(dead_code)]\nstruct MyList {\n arr: Vec<i32>, // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\n capacity: usize, // \u5217\u8868\u5bb9\u91cf\n size: usize, // \u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\n extend_ratio: usize, // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n}\n\n#[allow(unused,unused_comparisons)]\nimpl MyList {\n /* \u6784\u9020\u65b9\u6cd5 */\n pub fn new(capacity: usize) -> Self {\n let mut vec = Vec::new(); \n vec.resize(capacity, 0);\n Self {\n arr: vec,\n capacity,\n size: 0,\n extend_ratio: 2,\n }\n }\n\n /* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09*/\n pub fn size(&self) -> usize {\n return self.size;\n }\n\n /* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\n pub fn capacity(&self) -> usize {\n return self.capacity;\n }\n\n /* \u8bbf\u95ee\u5143\u7d20 */\n pub fn get(&self, index: usize) -> i32 {\n // \u7d22\u5f15\u5982\u679c\u8d8a\u754c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\n if index >= self.size {panic!(\"\u7d22\u5f15\u8d8a\u754c\")};\n return self.arr[index];\n }\n\n /* \u66f4\u65b0\u5143\u7d20 */\n pub fn set(&mut self, index: usize, num: i32) {\n if index >= self.size {panic!(\"\u7d22\u5f15\u8d8a\u754c\")};\n self.arr[index] = num;\n }\n\n /* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\n pub fn add(&mut self, num: i32) {\n // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if self.size == self.capacity() {\n self.extend_capacity();\n }\n self.arr[self.size] = num;\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n self.size += 1;\n }\n\n /* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\n pub fn insert(&mut self, index: usize, num: i32) {\n if index >= self.size() {panic!(\"\u7d22\u5f15\u8d8a\u754c\")};\n // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if self.size == self.capacity() {\n self.extend_capacity();\n }\n // \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for j in (index..self.size).rev() {\n self.arr[j + 1] = self.arr[j];\n }\n self.arr[index] = num;\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n self.size += 1;\n }\n\n /* \u5220\u9664\u5143\u7d20 */\n pub fn remove(&mut self, index: usize) -> i32 {\n if index >= self.size() {panic!(\"\u7d22\u5f15\u8d8a\u754c\")};\n let num = self.arr[index];\n // \u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for j in (index..self.size - 1) {\n self.arr[j] = self.arr[j + 1];\n }\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n self.size -= 1;\n // \u8fd4\u56de\u88ab\u5220\u9664\u5143\u7d20\n return num;\n }\n\n /* \u5217\u8868\u6269\u5bb9 */\n pub fn extend_capacity(&mut self) {\n // \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a\u539f\u6570\u7ec4 extend_ratio \u500d\u7684\u65b0\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u62f7\u8d1d\u5230\u65b0\u6570\u7ec4\n let new_capacity = self.capacity * self.extend_ratio;\n self.arr.resize(new_capacity, 0);\n // \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\n self.capacity = new_capacity;\n }\n\n /* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4 */\n pub fn to_array(&mut self) -> Vec<i32> {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n let mut arr = Vec::new();\n for i in 0..self.size {\n arr.push(self.get(i));\n }\n arr\n }\n}\n
my_list.c/* \u5217\u8868\u7c7b */\ntypedef struct {\n int *arr; // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\n int capacity; // \u5217\u8868\u5bb9\u91cf\n int size; // \u5217\u8868\u5927\u5c0f\n int extendRatio; // \u5217\u8868\u6bcf\u6b21\u6269\u5bb9\u7684\u500d\u6570\n} MyList;\n\n/* \u6784\u9020\u51fd\u6570 */\nMyList *newMyList() {\n MyList *nums = malloc(sizeof(MyList));\n nums->capacity = 10;\n nums->arr = malloc(sizeof(int) * nums->capacity);\n nums->size = 0;\n nums->extendRatio = 2;\n return nums;\n}\n\n/* \u6790\u6784\u51fd\u6570 */\nvoid delMyList(MyList *nums) {\n free(nums->arr);\n free(nums);\n}\n\n/* \u83b7\u53d6\u5217\u8868\u957f\u5ea6 */\nint size(MyList *nums) {\n return nums->size;\n}\n\n/* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\nint capacity(MyList *nums) {\n return nums->capacity;\n}\n\n/* \u8bbf\u95ee\u5143\u7d20 */\nint get(MyList *nums, int index) {\n assert(index >= 0 && index < nums->size);\n return nums->arr[index];\n}\n\n/* \u66f4\u65b0\u5143\u7d20 */\nvoid set(MyList *nums, int index, int num) {\n assert(index >= 0 && index < nums->size);\n nums->arr[index] = num;\n}\n\n/* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nvoid add(MyList *nums, int num) {\n if (size(nums) == capacity(nums)) {\n extendCapacity(nums); // \u6269\u5bb9\n }\n nums->arr[size(nums)] = num;\n nums->size++;\n}\n\n/* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nvoid insert(MyList *nums, int index, int num) {\n assert(index >= 0 && index < size(nums));\n // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if (size(nums) == capacity(nums)) {\n extendCapacity(nums); // \u6269\u5bb9\n }\n for (int i = size(nums); i > index; --i) {\n nums->arr[i] = nums->arr[i - 1];\n }\n nums->arr[index] = num;\n nums->size++;\n}\n\n/* \u5220\u9664\u5143\u7d20 */\n// \u6ce8\u610f\uff1astdio.h \u5360\u7528\u4e86 remove \u5173\u952e\u8bcd\nint removeItem(MyList *nums, int index) {\n assert(index >= 0 && index < size(nums));\n int num = nums->arr[index];\n for (int i = index; i < size(nums) - 1; i++) {\n nums->arr[i] = nums->arr[i + 1];\n }\n nums->size--;\n return num;\n}\n\n/* \u5217\u8868\u6269\u5bb9 */\nvoid extendCapacity(MyList *nums) {\n // \u5148\u5206\u914d\u7a7a\u95f4\n int newCapacity = capacity(nums) * nums->extendRatio;\n int *extend = (int *)malloc(sizeof(int) * newCapacity);\n int *temp = nums->arr;\n\n // \u62f7\u8d1d\u65e7\u6570\u636e\u5230\u65b0\u6570\u636e\n for (int i = 0; i < size(nums); i++)\n extend[i] = nums->arr[i];\n\n // \u91ca\u653e\u65e7\u6570\u636e\n free(temp);\n\n // \u66f4\u65b0\u65b0\u6570\u636e\n nums->arr = extend;\n nums->capacity = newCapacity;\n}\n\n/* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a Array \u7528\u4e8e\u6253\u5370 */\nint *toArray(MyList *nums) {\n return nums->arr;\n}\n
my_list.zig// \u5217\u8868\u7c7b\nfn MyList(comptime T: type) type {\n return struct {\n const Self = @This();\n\n arr: []T = undefined, // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\n arrCapacity: usize = 10, // \u5217\u8868\u5bb9\u91cf\n numSize: usize = 0, // \u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\n extendRatio: usize = 2, // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n mem_arena: ?std.heap.ArenaAllocator = null,\n mem_allocator: std.mem.Allocator = undefined, // \u5185\u5b58\u5206\u914d\u5668\n\n // \u6784\u9020\u51fd\u6570\uff08\u5206\u914d\u5185\u5b58+\u521d\u59cb\u5316\u5217\u8868\uff09\n pub fn init(self: *Self, allocator: std.mem.Allocator) !void {\n if (self.mem_arena == null) {\n self.mem_arena = std.heap.ArenaAllocator.init(allocator);\n self.mem_allocator = self.mem_arena.?.allocator();\n }\n self.arr = try self.mem_allocator.alloc(T, self.arrCapacity);\n @memset(self.arr, @as(T, 0));\n }\n\n // \u6790\u6784\u51fd\u6570\uff08\u91ca\u653e\u5185\u5b58\uff09\n pub fn deinit(self: *Self) void {\n if (self.mem_arena == null) return;\n self.mem_arena.?.deinit();\n }\n\n // \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\n pub fn size(self: *Self) usize {\n return self.numSize;\n }\n\n // \u83b7\u53d6\u5217\u8868\u5bb9\u91cf\n pub fn capacity(self: *Self) usize {\n return self.arrCapacity;\n }\n\n // \u8bbf\u95ee\u5143\u7d20\n pub fn get(self: *Self, index: usize) T {\n // \u7d22\u5f15\u5982\u679c\u8d8a\u754c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\n if (index < 0 or index >= self.size()) @panic(\"\u7d22\u5f15\u8d8a\u754c\");\n return self.arr[index];\n } \n\n // \u66f4\u65b0\u5143\u7d20\n pub fn set(self: *Self, index: usize, num: T) void {\n // \u7d22\u5f15\u5982\u679c\u8d8a\u754c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\n if (index < 0 or index >= self.size()) @panic(\"\u7d22\u5f15\u8d8a\u754c\");\n self.arr[index] = num;\n } \n\n // \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20\n pub fn add(self: *Self, num: T) !void {\n // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if (self.size() == self.capacity()) try self.extendCapacity();\n self.arr[self.size()] = num;\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n self.numSize += 1;\n } \n\n // \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20\n pub fn insert(self: *Self, index: usize, num: T) !void {\n if (index < 0 or index >= self.size()) @panic(\"\u7d22\u5f15\u8d8a\u754c\");\n // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if (self.size() == self.capacity()) try self.extendCapacity();\n // \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n var j = self.size() - 1;\n while (j >= index) : (j -= 1) {\n self.arr[j + 1] = self.arr[j];\n }\n self.arr[index] = num;\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n self.numSize += 1;\n }\n\n // \u5220\u9664\u5143\u7d20\n pub fn remove(self: *Self, index: usize) T {\n if (index < 0 or index >= self.size()) @panic(\"\u7d22\u5f15\u8d8a\u754c\");\n var num = self.arr[index];\n // \u7d22\u5f15 i \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n var j = index;\n while (j < self.size() - 1) : (j += 1) {\n self.arr[j] = self.arr[j + 1];\n }\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n self.numSize -= 1;\n // \u8fd4\u56de\u88ab\u5220\u9664\u5143\u7d20\n return num;\n }\n\n // \u5217\u8868\u6269\u5bb9\n pub fn extendCapacity(self: *Self) !void {\n // \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a size * extendRatio \u7684\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u62f7\u8d1d\u5230\u65b0\u6570\u7ec4\n var newCapacity = self.capacity() * self.extendRatio;\n var extend = try self.mem_allocator.alloc(T, newCapacity);\n @memset(extend, @as(T, 0));\n // \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\n std.mem.copy(T, extend, self.arr);\n self.arr = extend;\n // \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\n self.arrCapacity = newCapacity;\n }\n\n // \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4\n pub fn toArray(self: *Self) ![]T {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n var arr = try self.mem_allocator.alloc(T, self.size());\n @memset(arr, @as(T, 0));\n for (arr, 0..) |*num, i| {\n num.* = self.get(i);\n }\n return arr;\n }\n };\n}\n
"},{"location":"chapter_array_and_linkedlist/ram_and_cache/","title":"4.4 \u00a0 \u5185\u5b58\u4e0e\u7f13\u5b58 *","text":"\u5728\u672c\u7ae0\u7684\u524d\u4e24\u8282\u4e2d\uff0c\u6211\u4eec\u63a2\u8ba8\u4e86\u6570\u7ec4\u548c\u94fe\u8868\u8fd9\u4e24\u79cd\u57fa\u7840\u4e14\u91cd\u8981\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5b83\u4eec\u5206\u522b\u4ee3\u8868\u4e86\u201c\u8fde\u7eed\u5b58\u50a8\u201d\u548c\u201c\u5206\u6563\u5b58\u50a8\u201d\u8fd9\u4e24\u79cd\u4e0d\u540c\u7684\u7269\u7406\u7ed3\u6784\u3002
\u5b9e\u9645\u4e0a\uff0c\u7269\u7406\u7ed3\u6784\u5728\u5f88\u5927\u7a0b\u5ea6\u4e0a\u51b3\u5b9a\u4e86\u7a0b\u5e8f\u5bf9\u5185\u5b58\u548c\u7f13\u5b58\u7684\u4f7f\u7528\u6548\u7387\uff0c\u8fdb\u800c\u5f71\u54cd\u7b97\u6cd5\u7a0b\u5e8f\u7684\u6574\u4f53\u6027\u80fd\u3002
"},{"location":"chapter_array_and_linkedlist/ram_and_cache/#441","title":"4.4.1 \u00a0 \u8ba1\u7b97\u673a\u5b58\u50a8\u8bbe\u5907","text":"\u8ba1\u7b97\u673a\u4e2d\u5305\u62ec\u4e09\u79cd\u4e0d\u540c\u7c7b\u578b\u7684\u5b58\u50a8\u8bbe\u5907\uff1a\u300c\u786c\u76d8 hard disk\u300d\u3001\u300c\u5185\u5b58 random-access memory, RAM\u300d\u3001\u300c\u7f13\u5b58 cache memory\u300d\u3002\u8868 4-2 \u5c55\u793a\u4e86\u5b83\u4eec\u5728\u8ba1\u7b97\u673a\u7cfb\u7edf\u4e2d\u7684\u4e0d\u540c\u89d2\u8272\u548c\u6027\u80fd\u7279\u70b9\u3002
\u8868 4-2 \u00a0 \u8ba1\u7b97\u673a\u7684\u5b58\u50a8\u8bbe\u5907
\u786c\u76d8 \u5185\u5b58 \u7f13\u5b58 \u7528\u9014 \u957f\u671f\u5b58\u50a8\u6570\u636e\uff0c\u5305\u62ec\u64cd\u4f5c\u7cfb\u7edf\u3001\u7a0b\u5e8f\u3001\u6587\u4ef6\u7b49 \u4e34\u65f6\u5b58\u50a8\u5f53\u524d\u8fd0\u884c\u7684\u7a0b\u5e8f\u548c\u6b63\u5728\u5904\u7406\u7684\u6570\u636e \u5b58\u50a8\u7ecf\u5e38\u8bbf\u95ee\u7684\u6570\u636e\u548c\u6307\u4ee4\uff0c\u51cf\u5c11 CPU \u8bbf\u95ee\u5185\u5b58\u7684\u6b21\u6570 \u6613\u5931\u6027 \u65ad\u7535\u540e\u6570\u636e\u4e0d\u4f1a\u4e22\u5931 \u65ad\u7535\u540e\u6570\u636e\u4f1a\u4e22\u5931 \u65ad\u7535\u540e\u6570\u636e\u4f1a\u4e22\u5931 \u5bb9\u91cf \u8f83\u5927\uff0cTB \u7ea7\u522b \u8f83\u5c0f\uff0cGB \u7ea7\u522b \u975e\u5e38\u5c0f\uff0cMB \u7ea7\u522b \u901f\u5ea6 \u8f83\u6162\uff0c\u51e0\u767e\u5230\u51e0\u5343 MB/s \u8f83\u5feb\uff0c\u51e0\u5341 GB/s \u975e\u5e38\u5feb\uff0c\u51e0\u5341\u5230\u51e0\u767e GB/s \u4ef7\u683c \u8f83\u4fbf\u5b9c\uff0c\u51e0\u6bdb\u5230\u51e0\u5143 / GB \u8f83\u8d35\uff0c\u51e0\u5341\u5230\u51e0\u767e\u5143 / GB \u975e\u5e38\u8d35\uff0c\u968f CPU \u6253\u5305\u8ba1\u4ef7\u6211\u4eec\u53ef\u4ee5\u5c06\u8ba1\u7b97\u673a\u5b58\u50a8\u7cfb\u7edf\u60f3\u8c61\u4e3a\u56fe 4-9 \u6240\u793a\u7684\u91d1\u5b57\u5854\u7ed3\u6784\u3002\u8d8a\u9760\u8fd1\u91d1\u5b57\u5854\u9876\u7aef\u7684\u5b58\u50a8\u8bbe\u5907\u7684\u901f\u5ea6\u8d8a\u5feb\u3001\u5bb9\u91cf\u8d8a\u5c0f\u3001\u6210\u672c\u8d8a\u9ad8\u3002\u8fd9\u79cd\u591a\u5c42\u7ea7\u7684\u8bbe\u8ba1\u5e76\u975e\u5076\u7136\uff0c\u800c\u662f\u8ba1\u7b97\u673a\u79d1\u5b66\u5bb6\u548c\u5de5\u7a0b\u5e08\u4eec\u7ecf\u8fc7\u6df1\u601d\u719f\u8651\u7684\u7ed3\u679c\u3002
\u56fe 4-9 \u00a0 \u8ba1\u7b97\u673a\u5b58\u50a8\u7cfb\u7edf
Note
\u8ba1\u7b97\u673a\u7684\u5b58\u50a8\u5c42\u6b21\u7ed3\u6784\u4f53\u73b0\u4e86\u901f\u5ea6\u3001\u5bb9\u91cf\u548c\u6210\u672c\u4e09\u8005\u4e4b\u95f4\u7684\u7cbe\u5999\u5e73\u8861\u3002\u5b9e\u9645\u4e0a\uff0c\u8fd9\u79cd\u6743\u8861\u666e\u904d\u5b58\u5728\u4e8e\u6240\u6709\u5de5\u4e1a\u9886\u57df\uff0c\u5b83\u8981\u6c42\u6211\u4eec\u5728\u4e0d\u540c\u7684\u4f18\u52bf\u548c\u9650\u5236\u4e4b\u95f4\u627e\u5230\u6700\u4f73\u7684\u5e73\u8861\u70b9\u3002
\u603b\u7684\u6765\u8bf4\uff0c\u786c\u76d8\u7528\u4e8e\u957f\u671f\u5b58\u50a8\u5927\u91cf\u6570\u636e\uff0c\u5185\u5b58\u7528\u4e8e\u4e34\u65f6\u5b58\u50a8\u7a0b\u5e8f\u8fd0\u884c\u4e2d\u6b63\u5728\u5904\u7406\u7684\u6570\u636e\uff0c\u800c\u7f13\u5b58\u5219\u7528\u4e8e\u5b58\u50a8\u7ecf\u5e38\u8bbf\u95ee\u7684\u6570\u636e\u548c\u6307\u4ee4\uff0c\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u8fd0\u884c\u6548\u7387\u3002\u8fd9\u4e09\u8005\u5171\u540c\u534f\u4f5c\uff0c\u786e\u4fdd\u8ba1\u7b97\u673a\u7cfb\u7edf\u7684\u9ad8\u6548\u8fd0\u884c\u3002
\u5982\u56fe 4-10 \u6240\u793a\uff0c\u5728\u7a0b\u5e8f\u8fd0\u884c\u65f6\uff0c\u6570\u636e\u4f1a\u4ece\u786c\u76d8\u4e2d\u88ab\u8bfb\u53d6\u5230\u5185\u5b58\u4e2d\uff0c\u4f9b\u7ed9 CPU \u8ba1\u7b97\u4f7f\u7528\u3002\u7f13\u5b58\u53ef\u4ee5\u770b\u4f5c CPU \u7684\u4e00\u90e8\u5206\uff0c\u5b83\u901a\u8fc7\u667a\u80fd\u5730\u4ece\u5185\u5b58\u52a0\u8f7d\u6570\u636e\uff0c\u7ed9 CPU \u63d0\u4f9b\u9ad8\u901f\u7684\u6570\u636e\u8bfb\u53d6\uff0c\u4ece\u800c\u663e\u8457\u63d0\u5347\u7a0b\u5e8f\u7684\u6267\u884c\u6548\u7387\uff0c\u51cf\u5c11\u5bf9\u8f83\u6162\u7684\u5185\u5b58\u7684\u4f9d\u8d56\u3002
\u56fe 4-10 \u00a0 \u786c\u76d8\u3001\u5185\u5b58\u548c\u7f13\u5b58\u4e4b\u95f4\u7684\u6570\u636e\u6d41\u901a
"},{"location":"chapter_array_and_linkedlist/ram_and_cache/#442","title":"4.4.2 \u00a0 \u6570\u636e\u7ed3\u6784\u7684\u5185\u5b58\u6548\u7387","text":"\u5728\u5185\u5b58\u7a7a\u95f4\u5229\u7528\u65b9\u9762\uff0c\u6570\u7ec4\u548c\u94fe\u8868\u5177\u6709\u5404\u81ea\u7684\u4f18\u52bf\u548c\u5c40\u9650\u3002
\u4e00\u65b9\u9762\uff0c\u5185\u5b58\u662f\u6709\u9650\u7684\uff0c\u4e14\u540c\u4e00\u5757\u5185\u5b58\u4e0d\u80fd\u88ab\u591a\u4e2a\u7a0b\u5e8f\u5171\u4eab\uff0c\u56e0\u6b64\u6211\u4eec\u5e0c\u671b\u6570\u636e\u7ed3\u6784\u80fd\u591f\u5c3d\u53ef\u80fd\u9ad8\u6548\u5730\u5229\u7528\u7a7a\u95f4\u3002\u6570\u7ec4\u7684\u5143\u7d20\u7d27\u5bc6\u6392\u5217\uff0c\u4e0d\u9700\u8981\u989d\u5916\u7684\u7a7a\u95f4\u6765\u5b58\u50a8\u94fe\u8868\u8282\u70b9\u95f4\u7684\u5f15\u7528\uff08\u6307\u9488\uff09\uff0c\u56e0\u6b64\u7a7a\u95f4\u6548\u7387\u66f4\u9ad8\u3002\u7136\u800c\uff0c\u6570\u7ec4\u9700\u8981\u4e00\u6b21\u6027\u5206\u914d\u8db3\u591f\u7684\u8fde\u7eed\u5185\u5b58\u7a7a\u95f4\uff0c\u8fd9\u53ef\u80fd\u5bfc\u81f4\u5185\u5b58\u7684\u6d6a\u8d39\uff0c\u6570\u7ec4\u6269\u5bb9\u4e5f\u9700\u8981\u989d\u5916\u7684\u65f6\u95f4\u548c\u7a7a\u95f4\u6210\u672c\u3002\u76f8\u6bd4\u4e4b\u4e0b\uff0c\u94fe\u8868\u4ee5\u201c\u8282\u70b9\u201d\u4e3a\u5355\u4f4d\u8fdb\u884c\u52a8\u6001\u5185\u5b58\u5206\u914d\u548c\u56de\u6536\uff0c\u8fd9\u79cd\u65b9\u5f0f\u63d0\u4f9b\u4e86\u66f4\u5927\u7684\u7075\u6d3b\u6027\u3002
\u53e6\u4e00\u65b9\u9762\uff0c\u5728\u7a0b\u5e8f\u8fd0\u884c\u65f6\uff0c\u968f\u7740\u53cd\u590d\u7533\u8bf7\u4e0e\u91ca\u653e\u5185\u5b58\uff0c\u7a7a\u95f2\u5185\u5b58\u7684\u788e\u7247\u5316\u7a0b\u5ea6\u4f1a\u8d8a\u6765\u8d8a\u9ad8\uff0c\u4ece\u800c\u5bfc\u81f4\u5185\u5b58\u7684\u5229\u7528\u6548\u7387\u964d\u4f4e\u3002\u6570\u7ec4\u7531\u4e8e\u5176\u8fde\u7eed\u7684\u5b58\u50a8\u65b9\u5f0f\uff0c\u76f8\u5bf9\u4e0d\u5bb9\u6613\u5bfc\u81f4\u5185\u5b58\u788e\u7247\u5316\u3002\u76f8\u53cd\uff0c\u94fe\u8868\u7684\u5143\u7d20\u662f\u5206\u6563\u5b58\u50a8\u7684\uff0c\u5728\u9891\u7e41\u7684\u63d2\u5165\u4e0e\u5220\u9664\u64cd\u4f5c\u4e2d\uff0c\u66f4\u5bb9\u6613\u5bfc\u81f4\u5185\u5b58\u788e\u7247\u5316\u3002
"},{"location":"chapter_array_and_linkedlist/ram_and_cache/#443","title":"4.4.3 \u00a0 \u6570\u636e\u7ed3\u6784\u7684\u7f13\u5b58\u6548\u7387","text":"\u7f13\u5b58\u867d\u7136\u5728\u7a7a\u95f4\u5bb9\u91cf\u4e0a\u8fdc\u5c0f\u4e8e\u5185\u5b58\uff0c\u4f46\u5b83\u6bd4\u5185\u5b58\u5feb\u5f97\u591a\uff0c\u5728\u7a0b\u5e8f\u6267\u884c\u901f\u5ea6\u4e0a\u8d77\u7740\u81f3\u5173\u91cd\u8981\u7684\u4f5c\u7528\u3002\u7531\u4e8e\u7f13\u5b58\u7684\u5bb9\u91cf\u6709\u9650\uff0c\u5b83\u53ea\u80fd\u5b58\u50a8\u4e00\u5c0f\u90e8\u5206\u9891\u7e41\u8bbf\u95ee\u7684\u6570\u636e\u3002\u56e0\u6b64\uff0c\u5f53 CPU \u5c1d\u8bd5\u8bbf\u95ee\u7684\u6570\u636e\u4e0d\u5728\u7f13\u5b58\u4e2d\u65f6\uff0c\u5c31\u4f1a\u53d1\u751f\u300c\u7f13\u5b58\u672a\u547d\u4e2d cache miss\u300d\uff0c\u6b64\u65f6 CPU \u4e0d\u5f97\u4e0d\u4ece\u901f\u5ea6\u8f83\u6162\u7684\u5185\u5b58\u4e2d\u52a0\u8f7d\u6240\u9700\u6570\u636e\u3002
\u663e\u7136\uff0c\u201c\u7f13\u5b58\u672a\u547d\u4e2d\u201d\u8d8a\u5c11\uff0cCPU \u8bfb\u5199\u6570\u636e\u7684\u6548\u7387\u5c31\u8d8a\u9ad8\uff0c\u7a0b\u5e8f\u6027\u80fd\u4e5f\u5c31\u8d8a\u597d\u3002\u6211\u4eec\u5c06 CPU \u4ece\u7f13\u5b58\u4e2d\u6210\u529f\u83b7\u53d6\u6570\u636e\u7684\u6bd4\u4f8b\u79f0\u4e3a\u300c\u7f13\u5b58\u547d\u4e2d\u7387 cache hit rate\u300d\uff0c\u8fd9\u4e2a\u6307\u6807\u901a\u5e38\u7528\u6765\u8861\u91cf\u7f13\u5b58\u6548\u7387\u3002
\u4e3a\u4e86\u5c3d\u53ef\u80fd\u8fbe\u5230\u66f4\u9ad8\u6548\u7387\uff0c\u7f13\u5b58\u4f1a\u91c7\u53d6\u4ee5\u4e0b\u6570\u636e\u52a0\u8f7d\u673a\u5236\u3002
\u5b9e\u9645\u4e0a\uff0c\u6570\u7ec4\u548c\u94fe\u8868\u5bf9\u7f13\u5b58\u7684\u5229\u7528\u6548\u7387\u4e5f\u662f\u4e0d\u540c\u7684\uff0c\u4e3b\u8981\u4f53\u73b0\u5728\u4ee5\u4e0b\u51e0\u4e2a\u65b9\u9762\u3002
\u603b\u4f53\u800c\u8a00\uff0c\u6570\u7ec4\u5177\u6709\u66f4\u9ad8\u7684\u7f13\u5b58\u547d\u4e2d\u7387\uff0c\u56e0\u6b64\u5b83\u5728\u64cd\u4f5c\u6548\u7387\u4e0a\u901a\u5e38\u4f18\u4e8e\u94fe\u8868\u3002\u8fd9\u4f7f\u5f97\u5728\u89e3\u51b3\u7b97\u6cd5\u95ee\u9898\u65f6\uff0c\u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6570\u636e\u7ed3\u6784\u5f80\u5f80\u66f4\u53d7\u6b22\u8fce\u3002
\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u9ad8\u7f13\u5b58\u6548\u7387\u5e76\u4e0d\u610f\u5473\u7740\u6570\u7ec4\u5728\u6240\u6709\u60c5\u51b5\u4e0b\u90fd\u4f18\u4e8e\u94fe\u8868\u3002\u5b9e\u9645\u5e94\u7528\u4e2d\u9009\u62e9\u54ea\u79cd\u6570\u636e\u7ed3\u6784\uff0c\u5e94\u6839\u636e\u5177\u4f53\u9700\u6c42\u6765\u51b3\u5b9a\u3002\u4f8b\u5982\uff0c\u6570\u7ec4\u548c\u94fe\u8868\u90fd\u53ef\u4ee5\u5b9e\u73b0\u201c\u6808\u201d\u6570\u636e\u7ed3\u6784\uff08\u4e0b\u4e00\u7ae0\u4f1a\u8be6\u7ec6\u4ecb\u7ecd\uff09\uff0c\u4f46\u5b83\u4eec\u9002\u7528\u4e8e\u4e0d\u540c\u573a\u666f\u3002
\u6570\u7ec4\u5b58\u50a8\u5728\u6808\u4e0a\u548c\u5b58\u50a8\u5728\u5806\u4e0a\uff0c\u5bf9\u65f6\u95f4\u6548\u7387\u548c\u7a7a\u95f4\u6548\u7387\u662f\u5426\u6709\u5f71\u54cd\uff1f
\u5b58\u50a8\u5728\u6808\u4e0a\u548c\u5806\u4e0a\u7684\u6570\u7ec4\u90fd\u88ab\u5b58\u50a8\u5728\u8fde\u7eed\u5185\u5b58\u7a7a\u95f4\u5185\uff0c\u6570\u636e\u64cd\u4f5c\u6548\u7387\u662f\u57fa\u672c\u4e00\u81f4\u7684\u3002\u7136\u800c\uff0c\u6808\u548c\u5806\u5177\u6709\u5404\u81ea\u7684\u7279\u70b9\uff0c\u4ece\u800c\u5bfc\u81f4\u4ee5\u4e0b\u4e0d\u540c\u70b9\u3002
\u4e3a\u4ec0\u4e48\u6570\u7ec4\u8981\u6c42\u76f8\u540c\u7c7b\u578b\u7684\u5143\u7d20\uff0c\u800c\u5728\u94fe\u8868\u4e2d\u5374\u6ca1\u6709\u5f3a\u8c03\u540c\u7c7b\u578b\u5462\uff1f
\u94fe\u8868\u7531\u8282\u70b9\u7ec4\u6210\uff0c\u8282\u70b9\u4e4b\u95f4\u901a\u8fc7\u5f15\u7528\uff08\u6307\u9488\uff09\u8fde\u63a5\uff0c\u5404\u4e2a\u8282\u70b9\u53ef\u4ee5\u5b58\u50a8\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\uff0c\u4f8b\u5982 int\u3001double\u3001string\u3001object \u7b49\u3002
\u76f8\u5bf9\u5730\uff0c\u6570\u7ec4\u5143\u7d20\u5219\u5fc5\u987b\u662f\u76f8\u540c\u7c7b\u578b\u7684\uff0c\u8fd9\u6837\u624d\u80fd\u901a\u8fc7\u8ba1\u7b97\u504f\u79fb\u91cf\u6765\u83b7\u53d6\u5bf9\u5e94\u5143\u7d20\u4f4d\u7f6e\u3002\u4f8b\u5982\uff0c\u6570\u7ec4\u540c\u65f6\u5305\u542b int \u548c long \u4e24\u79cd\u7c7b\u578b\uff0c\u5355\u4e2a\u5143\u7d20\u5206\u522b\u5360\u7528 4 bytes \u548c 8 bytes \uff0c\u6b64\u65f6\u5c31\u4e0d\u80fd\u7528\u4ee5\u4e0b\u516c\u5f0f\u8ba1\u7b97\u504f\u79fb\u91cf\u4e86\uff0c\u56e0\u4e3a\u6570\u7ec4\u4e2d\u5305\u542b\u4e86\u4e24\u79cd\u957f\u5ea6\u7684\u5143\u7d20\u3002
# \u5143\u7d20\u5185\u5b58\u5730\u5740 = \u6570\u7ec4\u5185\u5b58\u5730\u5740 + \u5143\u7d20\u957f\u5ea6 * \u5143\u7d20\u7d22\u5f15\n
\u5220\u9664\u8282\u70b9\u540e\uff0c\u662f\u5426\u9700\u8981\u628a P.next
\u8bbe\u4e3a \\(\\text{None}\\) \u5462\uff1f
\u4e0d\u4fee\u6539 P.next
\u4e5f\u53ef\u4ee5\u3002\u4ece\u8be5\u94fe\u8868\u7684\u89d2\u5ea6\u770b\uff0c\u4ece\u5934\u8282\u70b9\u904d\u5386\u5230\u5c3e\u8282\u70b9\u5df2\u7ecf\u4e0d\u4f1a\u9047\u5230 P
\u4e86\u3002\u8fd9\u610f\u5473\u7740\u8282\u70b9 P
\u5df2\u7ecf\u4ece\u94fe\u8868\u4e2d\u5220\u9664\u4e86\uff0c\u6b64\u65f6\u8282\u70b9 P
\u6307\u5411\u54ea\u91cc\u90fd\u4e0d\u4f1a\u5bf9\u8be5\u94fe\u8868\u4ea7\u751f\u5f71\u54cd\u3002
\u4ece\u5783\u573e\u56de\u6536\u7684\u89d2\u5ea6\u770b\uff0c\u5bf9\u4e8e Java\u3001Python\u3001Go \u7b49\u62e5\u6709\u81ea\u52a8\u5783\u573e\u56de\u6536\u673a\u5236\u7684\u8bed\u8a00\u6765\u8bf4\uff0c\u8282\u70b9 P
\u662f\u5426\u88ab\u56de\u6536\u53d6\u51b3\u4e8e\u662f\u5426\u4ecd\u5b58\u5728\u6307\u5411\u5b83\u7684\u5f15\u7528\uff0c\u800c\u4e0d\u662f P.next
\u7684\u503c\u3002\u5728 C \u548c C++ \u7b49\u8bed\u8a00\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u624b\u52a8\u91ca\u653e\u8282\u70b9\u5185\u5b58\u3002
\u5728\u94fe\u8868\u4e2d\u63d2\u5165\u548c\u5220\u9664\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u662f \\(O(1)\\) \u3002\u4f46\u662f\u589e\u5220\u4e4b\u524d\u90fd\u9700\u8981 \\(O(n)\\) \u7684\u65f6\u95f4\u67e5\u627e\u5143\u7d20\uff0c\u90a3\u4e3a\u4ec0\u4e48\u65f6\u95f4\u590d\u6742\u5ea6\u4e0d\u662f \\(O(n)\\) \u5462\uff1f
\u5982\u679c\u662f\u5148\u67e5\u627e\u5143\u7d20\u3001\u518d\u5220\u9664\u5143\u7d20\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u786e\u5b9e\u662f \\(O(n)\\) \u3002\u7136\u800c\uff0c\u94fe\u8868\u7684 \\(O(1)\\) \u589e\u5220\u7684\u4f18\u52bf\u53ef\u4ee5\u5728\u5176\u4ed6\u5e94\u7528\u4e0a\u5f97\u5230\u4f53\u73b0\u3002\u4f8b\u5982\uff0c\u53cc\u5411\u961f\u5217\u9002\u5408\u4f7f\u7528\u94fe\u8868\u5b9e\u73b0\uff0c\u6211\u4eec\u7ef4\u62a4\u4e00\u4e2a\u6307\u9488\u53d8\u91cf\u59cb\u7ec8\u6307\u5411\u5934\u8282\u70b9\u3001\u5c3e\u8282\u70b9\uff0c\u6bcf\u6b21\u63d2\u5165\u4e0e\u5220\u9664\u64cd\u4f5c\u90fd\u662f \\(O(1)\\) \u3002
\u56fe\u201c\u94fe\u8868\u5b9a\u4e49\u4e0e\u5b58\u50a8\u65b9\u5f0f\u201d\u4e2d\uff0c\u6d45\u84dd\u8272\u7684\u5b58\u50a8\u8282\u70b9\u6307\u9488\u662f\u5360\u7528\u4e00\u5757\u5185\u5b58\u5730\u5740\u5417\uff1f\u8fd8\u662f\u548c\u8282\u70b9\u503c\u5404\u5360\u4e00\u534a\u5462\uff1f
\u8be5\u793a\u610f\u56fe\u53ea\u662f\u5b9a\u6027\u8868\u793a\uff0c\u5b9a\u91cf\u8868\u793a\u9700\u8981\u6839\u636e\u5177\u4f53\u60c5\u51b5\u8fdb\u884c\u5206\u6790\u3002
\u5728\u5217\u8868\u672b\u5c3e\u6dfb\u52a0\u5143\u7d20\u662f\u5426\u65f6\u65f6\u523b\u523b\u90fd\u4e3a \\(O(1)\\) \uff1f
\u5982\u679c\u6dfb\u52a0\u5143\u7d20\u65f6\u8d85\u51fa\u5217\u8868\u957f\u5ea6\uff0c\u5219\u9700\u8981\u5148\u6269\u5bb9\u5217\u8868\u518d\u6dfb\u52a0\u3002\u7cfb\u7edf\u4f1a\u7533\u8bf7\u4e00\u5757\u65b0\u7684\u5185\u5b58\uff0c\u5e76\u5c06\u539f\u5217\u8868\u7684\u6240\u6709\u5143\u7d20\u642c\u8fd0\u8fc7\u53bb\uff0c\u8fd9\u65f6\u5019\u65f6\u95f4\u590d\u6742\u5ea6\u5c31\u4f1a\u662f \\(O(n)\\) \u3002
\u201c\u5217\u8868\u7684\u51fa\u73b0\u6781\u5927\u5730\u63d0\u9ad8\u4e86\u6570\u7ec4\u7684\u5b9e\u7528\u6027\uff0c\u4f46\u53ef\u80fd\u5bfc\u81f4\u90e8\u5206\u5185\u5b58\u7a7a\u95f4\u6d6a\u8d39\u201d\uff0c\u8fd9\u91cc\u7684\u7a7a\u95f4\u6d6a\u8d39\u662f\u6307\u989d\u5916\u589e\u52a0\u7684\u53d8\u91cf\u5982\u5bb9\u91cf\u3001\u957f\u5ea6\u3001\u6269\u5bb9\u500d\u6570\u6240\u5360\u7684\u5185\u5b58\u5417\uff1f
\u8fd9\u91cc\u7684\u7a7a\u95f4\u6d6a\u8d39\u4e3b\u8981\u6709\u4e24\u65b9\u9762\u542b\u4e49\uff1a\u4e00\u65b9\u9762\uff0c\u5217\u8868\u90fd\u4f1a\u8bbe\u5b9a\u4e00\u4e2a\u521d\u59cb\u957f\u5ea6\uff0c\u6211\u4eec\u4e0d\u4e00\u5b9a\u9700\u8981\u7528\u8fd9\u4e48\u591a\uff1b\u53e6\u4e00\u65b9\u9762\uff0c\u4e3a\u4e86\u9632\u6b62\u9891\u7e41\u6269\u5bb9\uff0c\u6269\u5bb9\u4e00\u822c\u4f1a\u4e58\u4ee5\u4e00\u4e2a\u7cfb\u6570\uff0c\u6bd4\u5982 \\(\\times 1.5\\) \u3002\u8fd9\u6837\u4e00\u6765\uff0c\u4e5f\u4f1a\u51fa\u73b0\u5f88\u591a\u7a7a\u4f4d\uff0c\u6211\u4eec\u901a\u5e38\u4e0d\u80fd\u5b8c\u5168\u586b\u6ee1\u5b83\u4eec\u3002
\u5728 Python \u4e2d\u521d\u59cb\u5316 n = [1, 2, 3]
\u540e\uff0c\u8fd9 3 \u4e2a\u5143\u7d20\u7684\u5730\u5740\u662f\u76f8\u8fde\u7684\uff0c\u4f46\u662f\u521d\u59cb\u5316 m = [2, 1, 3]
\u4f1a\u53d1\u73b0\u5b83\u4eec\u6bcf\u4e2a\u5143\u7d20\u7684 id \u5e76\u4e0d\u662f\u8fde\u7eed\u7684\uff0c\u800c\u662f\u5206\u522b\u8ddf n
\u4e2d\u7684\u76f8\u540c\u3002\u8fd9\u4e9b\u5143\u7d20\u7684\u5730\u5740\u4e0d\u8fde\u7eed\uff0c\u90a3\u4e48 m
\u8fd8\u662f\u6570\u7ec4\u5417\uff1f
\u5047\u5982\u628a\u5217\u8868\u5143\u7d20\u6362\u6210\u94fe\u8868\u8282\u70b9 n = [n1, n2, n3, n4, n5]
\uff0c\u901a\u5e38\u60c5\u51b5\u4e0b\u8fd9 5 \u4e2a\u8282\u70b9\u5bf9\u8c61\u4e5f\u5206\u6563\u5b58\u50a8\u5728\u5185\u5b58\u5404\u5904\u3002\u7136\u800c\uff0c\u7ed9\u5b9a\u4e00\u4e2a\u5217\u8868\u7d22\u5f15\uff0c\u6211\u4eec\u4ecd\u7136\u53ef\u4ee5\u5728 \\(O(1)\\) \u65f6\u95f4\u5185\u83b7\u53d6\u8282\u70b9\u5185\u5b58\u5730\u5740\uff0c\u4ece\u800c\u8bbf\u95ee\u5230\u5bf9\u5e94\u7684\u8282\u70b9\u3002\u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u4e2d\u5b58\u50a8\u7684\u662f\u8282\u70b9\u7684\u5f15\u7528\uff0c\u800c\u975e\u8282\u70b9\u672c\u8eab\u3002
\u4e0e\u8bb8\u591a\u8bed\u8a00\u4e0d\u540c\uff0cPython \u4e2d\u7684\u6570\u5b57\u4e5f\u88ab\u5305\u88c5\u4e3a\u5bf9\u8c61\uff0c\u5217\u8868\u4e2d\u5b58\u50a8\u7684\u4e0d\u662f\u6570\u5b57\u672c\u8eab\uff0c\u800c\u662f\u5bf9\u6570\u5b57\u7684\u5f15\u7528\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u4f1a\u53d1\u73b0\u4e24\u4e2a\u6570\u7ec4\u4e2d\u7684\u76f8\u540c\u6570\u5b57\u62e5\u6709\u540c\u4e00\u4e2a id \uff0c\u5e76\u4e14\u8fd9\u4e9b\u6570\u5b57\u7684\u5185\u5b58\u5730\u5740\u65e0\u987b\u8fde\u7eed\u3002
C++ STL \u91cc\u9762\u7684 std::list
\u5df2\u7ecf\u5b9e\u73b0\u4e86\u53cc\u5411\u94fe\u8868\uff0c\u4f46\u597d\u50cf\u4e00\u4e9b\u7b97\u6cd5\u4e66\u4e0a\u4e0d\u600e\u4e48\u76f4\u63a5\u4f7f\u7528\u5b83\uff0c\u662f\u4e0d\u662f\u56e0\u4e3a\u6709\u4ec0\u4e48\u5c40\u9650\u6027\u5462\uff1f
\u4e00\u65b9\u9762\uff0c\u6211\u4eec\u5f80\u5f80\u66f4\u9752\u7750\u4f7f\u7528\u6570\u7ec4\u5b9e\u73b0\u7b97\u6cd5\uff0c\u800c\u53ea\u5728\u5fc5\u8981\u65f6\u624d\u4f7f\u7528\u94fe\u8868\uff0c\u4e3b\u8981\u6709\u4e24\u4e2a\u539f\u56e0\u3002
std::list
\u901a\u5e38\u6bd4 std::vector
\u66f4\u5360\u7528\u7a7a\u95f4\u3002std::list
\u5bf9\u7f13\u5b58\u7684\u5229\u7528\u7387\u8f83\u4f4e\u3002\u4e00\u822c\u60c5\u51b5\u4e0b\uff0cstd::vector
\u7684\u6027\u80fd\u4f1a\u66f4\u597d\u3002\u53e6\u4e00\u65b9\u9762\uff0c\u5fc5\u8981\u4f7f\u7528\u94fe\u8868\u7684\u60c5\u51b5\u4e3b\u8981\u662f\u4e8c\u53c9\u6811\u548c\u56fe\u3002\u6808\u548c\u961f\u5217\u5f80\u5f80\u4f1a\u4f7f\u7528\u7f16\u7a0b\u8bed\u8a00\u63d0\u4f9b\u7684 stack
\u548c queue
\uff0c\u800c\u975e\u94fe\u8868\u3002
\u521d\u59cb\u5316\u5217\u8868 res = [0] * self.size()
\u64cd\u4f5c\uff0c\u4f1a\u5bfc\u81f4 res
\u7684\u6bcf\u4e2a\u5143\u7d20\u5f15\u7528\u76f8\u540c\u7684\u5730\u5740\u5417\uff1f
\u4e0d\u4f1a\u3002\u4f46\u4e8c\u7ef4\u6570\u7ec4\u4f1a\u6709\u8fd9\u4e2a\u95ee\u9898\uff0c\u4f8b\u5982\u521d\u59cb\u5316\u4e8c\u7ef4\u5217\u8868 res = [[0] * self.size()]
\uff0c\u5219\u591a\u6b21\u5f15\u7528\u4e86\u540c\u4e00\u4e2a\u5217\u8868 [0]
\u3002
\u5728\u5220\u9664\u8282\u70b9\u4e2d\uff0c\u9700\u8981\u65ad\u5f00\u8be5\u8282\u70b9\u4e0e\u5176\u540e\u7ee7\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\u6307\u5411\u5417\uff1f
\u4ece\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\uff08\u505a\u9898\uff09\u7684\u89d2\u5ea6\u770b\uff0c\u4e0d\u65ad\u5f00\u6ca1\u6709\u5173\u7cfb\uff0c\u53ea\u8981\u4fdd\u8bc1\u7a0b\u5e8f\u7684\u903b\u8f91\u662f\u6b63\u786e\u7684\u5c31\u884c\u3002\u4ece\u6807\u51c6\u5e93\u7684\u89d2\u5ea6\u770b\uff0c\u65ad\u5f00\u66f4\u52a0\u5b89\u5168\u3001\u903b\u8f91\u66f4\u52a0\u6e05\u6670\u3002\u5982\u679c\u4e0d\u65ad\u5f00\uff0c\u5047\u8bbe\u88ab\u5220\u9664\u8282\u70b9\u672a\u88ab\u6b63\u5e38\u56de\u6536\uff0c\u90a3\u4e48\u5b83\u4e5f\u4f1a\u5f71\u54cd\u540e\u7ee7\u8282\u70b9\u7684\u5185\u5b58\u56de\u6536\u3002
"},{"location":"chapter_backtracking/","title":"\u7b2c 13 \u7ae0 \u00a0 \u56de\u6eaf","text":"Abstract
\u6211\u4eec\u5982\u540c\u8ff7\u5bab\u4e2d\u7684\u63a2\u7d22\u8005\uff0c\u5728\u524d\u8fdb\u7684\u9053\u8def\u4e0a\u53ef\u80fd\u4f1a\u9047\u5230\u56f0\u96be\u3002
\u56de\u6eaf\u7684\u529b\u91cf\u8ba9\u6211\u4eec\u80fd\u591f\u91cd\u65b0\u5f00\u59cb\uff0c\u4e0d\u65ad\u5c1d\u8bd5\uff0c\u6700\u7ec8\u627e\u5230\u901a\u5f80\u5149\u660e\u7684\u51fa\u53e3\u3002
"},{"location":"chapter_backtracking/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"\u300c\u56de\u6eaf\u7b97\u6cd5 backtracking algorithm\u300d\u662f\u4e00\u79cd\u901a\u8fc7\u7a77\u4e3e\u6765\u89e3\u51b3\u95ee\u9898\u7684\u65b9\u6cd5\uff0c\u5b83\u7684\u6838\u5fc3\u601d\u60f3\u662f\u4ece\u4e00\u4e2a\u521d\u59cb\u72b6\u6001\u51fa\u53d1\uff0c\u66b4\u529b\u641c\u7d22\u6240\u6709\u53ef\u80fd\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u5f53\u9047\u5230\u6b63\u786e\u7684\u89e3\u5219\u5c06\u5176\u8bb0\u5f55\uff0c\u76f4\u5230\u627e\u5230\u89e3\u6216\u8005\u5c1d\u8bd5\u4e86\u6240\u6709\u53ef\u80fd\u7684\u9009\u62e9\u90fd\u65e0\u6cd5\u627e\u5230\u89e3\u4e3a\u6b62\u3002
\u56de\u6eaf\u7b97\u6cd5\u901a\u5e38\u91c7\u7528\u201c\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u201d\u6765\u904d\u5386\u89e3\u7a7a\u95f4\u3002\u5728\u201c\u4e8c\u53c9\u6811\u201d\u7ae0\u8282\u4e2d\uff0c\u6211\u4eec\u63d0\u5230\u524d\u5e8f\u3001\u4e2d\u5e8f\u548c\u540e\u5e8f\u904d\u5386\u90fd\u5c5e\u4e8e\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5229\u7528\u524d\u5e8f\u904d\u5386\u6784\u9020\u4e00\u4e2a\u56de\u6eaf\u95ee\u9898\uff0c\u9010\u6b65\u4e86\u89e3\u56de\u6eaf\u7b97\u6cd5\u7684\u5de5\u4f5c\u539f\u7406\u3002
\u4f8b\u9898\u4e00
\u7ed9\u5b9a\u4e00\u68f5\u4e8c\u53c9\u6811\uff0c\u641c\u7d22\u5e76\u8bb0\u5f55\u6240\u6709\u503c\u4e3a \\(7\\) \u7684\u8282\u70b9\uff0c\u8bf7\u8fd4\u56de\u8282\u70b9\u5217\u8868\u3002
\u5bf9\u4e8e\u6b64\u9898\uff0c\u6211\u4eec\u524d\u5e8f\u904d\u5386\u8fd9\u68f5\u6811\uff0c\u5e76\u5224\u65ad\u5f53\u524d\u8282\u70b9\u7684\u503c\u662f\u5426\u4e3a \\(7\\) \uff0c\u82e5\u662f\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u7684\u503c\u52a0\u5165\u7ed3\u679c\u5217\u8868 res
\u4e4b\u4e2d\u3002\u76f8\u5173\u8fc7\u7a0b\u5b9e\u73b0\u5982\u56fe 13-1 \u548c\u4ee5\u4e0b\u4ee3\u7801\u6240\u793a\uff1a
def pre_order(root: TreeNode):\n \"\"\"\u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e00\"\"\"\n if root is None:\n return\n if root.val == 7:\n # \u8bb0\u5f55\u89e3\n res.append(root)\n pre_order(root.left)\n pre_order(root.right)\n
preorder_traversal_i_compact.cpp/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e00 */\nvoid preOrder(TreeNode *root) {\n if (root == nullptr) {\n return;\n }\n if (root->val == 7) {\n // \u8bb0\u5f55\u89e3\n res.push_back(root);\n }\n preOrder(root->left);\n preOrder(root->right);\n}\n
preorder_traversal_i_compact.java/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e00 */\nvoid preOrder(TreeNode root) {\n if (root == null) {\n return;\n }\n if (root.val == 7) {\n // \u8bb0\u5f55\u89e3\n res.add(root);\n }\n preOrder(root.left);\n preOrder(root.right);\n}\n
preorder_traversal_i_compact.cs/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e00 */\nvoid PreOrder(TreeNode? root) {\n if (root == null) {\n return;\n }\n if (root.val == 7) {\n // \u8bb0\u5f55\u89e3\n res.Add(root);\n }\n PreOrder(root.left);\n PreOrder(root.right);\n}\n
preorder_traversal_i_compact.go/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e00 */\nfunc preOrderI(root *TreeNode, res *[]*TreeNode) {\n if root == nil {\n return\n }\n if (root.Val).(int) == 7 {\n // \u8bb0\u5f55\u89e3\n *res = append(*res, root)\n }\n preOrderI(root.Left, res)\n preOrderI(root.Right, res)\n}\n
preorder_traversal_i_compact.swift/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e00 */\nfunc preOrder(root: TreeNode?) {\n guard let root = root else {\n return\n }\n if root.val == 7 {\n // \u8bb0\u5f55\u89e3\n res.append(root)\n }\n preOrder(root: root.left)\n preOrder(root: root.right)\n}\n
preorder_traversal_i_compact.js/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e00 */\nfunction preOrder(root, res) {\n if (root === null) {\n return;\n }\n if (root.val === 7) {\n // \u8bb0\u5f55\u89e3\n res.push(root);\n }\n preOrder(root.left, res);\n preOrder(root.right, res);\n}\n
preorder_traversal_i_compact.ts/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e00 */\nfunction preOrder(root: TreeNode | null, res: TreeNode[]): void {\n if (root === null) {\n return;\n }\n if (root.val === 7) {\n // \u8bb0\u5f55\u89e3\n res.push(root);\n }\n preOrder(root.left, res);\n preOrder(root.right, res);\n}\n
preorder_traversal_i_compact.dart/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e00 */\nvoid preOrder(TreeNode? root, List<TreeNode> res) {\n if (root == null) {\n return;\n }\n if (root.val == 7) {\n // \u8bb0\u5f55\u89e3\n res.add(root);\n }\n preOrder(root.left, res);\n preOrder(root.right, res);\n}\n
preorder_traversal_i_compact.rs/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e00 */\nfn pre_order(res: &mut Vec<Rc<RefCell<TreeNode>>>, root: Option<Rc<RefCell<TreeNode>>>) {\n if root.is_none() {\n return;\n }\n if let Some(node) = root {\n if node.borrow().val == 7 {\n // \u8bb0\u5f55\u89e3\n res.push(node.clone());\n }\n pre_order(res, node.borrow().left.clone());\n pre_order(res, node.borrow().right.clone());\n }\n}\n
preorder_traversal_i_compact.c/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e00 */\nvoid preOrder(TreeNode *root) {\n if (root == NULL) {\n return;\n }\n if (root->val == 7) {\n // \u8bb0\u5f55\u89e3\n res[resSize++] = root;\n }\n preOrder(root->left);\n preOrder(root->right);\n}\n
preorder_traversal_i_compact.zig[class]{}-[func]{preOrder}\n
\u56fe 13-1 \u00a0 \u5728\u524d\u5e8f\u904d\u5386\u4e2d\u641c\u7d22\u8282\u70b9
"},{"location":"chapter_backtracking/backtracking_algorithm/#1311","title":"13.1.1 \u00a0 \u5c1d\u8bd5\u4e0e\u56de\u9000","text":"\u4e4b\u6240\u4ee5\u79f0\u4e4b\u4e3a\u56de\u6eaf\u7b97\u6cd5\uff0c\u662f\u56e0\u4e3a\u8be5\u7b97\u6cd5\u5728\u641c\u7d22\u89e3\u7a7a\u95f4\u65f6\u4f1a\u91c7\u7528\u201c\u5c1d\u8bd5\u201d\u4e0e\u201c\u56de\u9000\u201d\u7684\u7b56\u7565\u3002\u5f53\u7b97\u6cd5\u5728\u641c\u7d22\u8fc7\u7a0b\u4e2d\u9047\u5230\u67d0\u4e2a\u72b6\u6001\u65e0\u6cd5\u7ee7\u7eed\u524d\u8fdb\u6216\u65e0\u6cd5\u5f97\u5230\u6ee1\u8db3\u6761\u4ef6\u7684\u89e3\u65f6\uff0c\u5b83\u4f1a\u64a4\u9500\u4e0a\u4e00\u6b65\u7684\u9009\u62e9\uff0c\u9000\u56de\u5230\u4e4b\u524d\u7684\u72b6\u6001\uff0c\u5e76\u5c1d\u8bd5\u5176\u4ed6\u53ef\u80fd\u7684\u9009\u62e9\u3002
\u5bf9\u4e8e\u4f8b\u9898\u4e00\uff0c\u8bbf\u95ee\u6bcf\u4e2a\u8282\u70b9\u90fd\u4ee3\u8868\u4e00\u6b21\u201c\u5c1d\u8bd5\u201d\uff0c\u800c\u8d8a\u8fc7\u53f6\u8282\u70b9\u6216\u8fd4\u56de\u7236\u8282\u70b9\u7684 return
\u5219\u8868\u793a\u201c\u56de\u9000\u201d\u3002
\u503c\u5f97\u8bf4\u660e\u7684\u662f\uff0c\u56de\u9000\u5e76\u4e0d\u4ec5\u4ec5\u5305\u62ec\u51fd\u6570\u8fd4\u56de\u3002\u4e3a\u89e3\u91ca\u8fd9\u4e00\u70b9\uff0c\u6211\u4eec\u5bf9\u4f8b\u9898\u4e00\u7a0d\u4f5c\u62d3\u5c55\u3002
\u4f8b\u9898\u4e8c
\u5728\u4e8c\u53c9\u6811\u4e2d\u641c\u7d22\u6240\u6709\u503c\u4e3a \\(7\\) \u7684\u8282\u70b9\uff0c\u8bf7\u8fd4\u56de\u6839\u8282\u70b9\u5230\u8fd9\u4e9b\u8282\u70b9\u7684\u8def\u5f84\u3002
\u5728\u4f8b\u9898\u4e00\u4ee3\u7801\u7684\u57fa\u7840\u4e0a\uff0c\u6211\u4eec\u9700\u8981\u501f\u52a9\u4e00\u4e2a\u5217\u8868 path
\u8bb0\u5f55\u8bbf\u95ee\u8fc7\u7684\u8282\u70b9\u8def\u5f84\u3002\u5f53\u8bbf\u95ee\u5230\u503c\u4e3a \\(7\\) \u7684\u8282\u70b9\u65f6\uff0c\u5219\u590d\u5236 path
\u5e76\u6dfb\u52a0\u8fdb\u7ed3\u679c\u5217\u8868 res
\u3002\u904d\u5386\u5b8c\u6210\u540e\uff0cres
\u4e2d\u4fdd\u5b58\u7684\u5c31\u662f\u6240\u6709\u7684\u89e3\u3002\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a
def pre_order(root: TreeNode):\n \"\"\"\u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e8c\"\"\"\n if root is None:\n return\n # \u5c1d\u8bd5\n path.append(root)\n if root.val == 7:\n # \u8bb0\u5f55\u89e3\n res.append(list(path))\n pre_order(root.left)\n pre_order(root.right)\n # \u56de\u9000\n path.pop()\n
preorder_traversal_ii_compact.cpp/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e8c */\nvoid preOrder(TreeNode *root) {\n if (root == nullptr) {\n return;\n }\n // \u5c1d\u8bd5\n path.push_back(root);\n if (root->val == 7) {\n // \u8bb0\u5f55\u89e3\n res.push_back(path);\n }\n preOrder(root->left);\n preOrder(root->right);\n // \u56de\u9000\n path.pop_back();\n}\n
preorder_traversal_ii_compact.java/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e8c */\nvoid preOrder(TreeNode root) {\n if (root == null) {\n return;\n }\n // \u5c1d\u8bd5\n path.add(root);\n if (root.val == 7) {\n // \u8bb0\u5f55\u89e3\n res.add(new ArrayList<>(path));\n }\n preOrder(root.left);\n preOrder(root.right);\n // \u56de\u9000\n path.remove(path.size() - 1);\n}\n
preorder_traversal_ii_compact.cs/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e8c */\nvoid PreOrder(TreeNode? root) {\n if (root == null) {\n return;\n }\n // \u5c1d\u8bd5\n path.Add(root);\n if (root.val == 7) {\n // \u8bb0\u5f55\u89e3\n res.Add(new List<TreeNode>(path));\n }\n PreOrder(root.left);\n PreOrder(root.right);\n // \u56de\u9000\n path.RemoveAt(path.Count - 1);\n}\n
preorder_traversal_ii_compact.go/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e8c */\nfunc preOrderII(root *TreeNode, res *[][]*TreeNode, path *[]*TreeNode) {\n if root == nil {\n return\n }\n // \u5c1d\u8bd5\n *path = append(*path, root)\n if root.Val.(int) == 7 {\n // \u8bb0\u5f55\u89e3\n *res = append(*res, *path)\n }\n preOrderII(root.Left, res, path)\n preOrderII(root.Right, res, path)\n // \u56de\u9000\n *path = (*path)[:len(*path)-1]\n}\n
preorder_traversal_ii_compact.swift/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e8c */\nfunc preOrder(root: TreeNode?) {\n guard let root = root else {\n return\n }\n // \u5c1d\u8bd5\n path.append(root)\n if root.val == 7 {\n // \u8bb0\u5f55\u89e3\n res.append(path)\n }\n preOrder(root: root.left)\n preOrder(root: root.right)\n // \u56de\u9000\n path.removeLast()\n}\n
preorder_traversal_ii_compact.js/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e8c */\nfunction preOrder(root, path, res) {\n if (root === null) {\n return;\n }\n // \u5c1d\u8bd5\n path.push(root);\n if (root.val === 7) {\n // \u8bb0\u5f55\u89e3\n res.push([...path]);\n }\n preOrder(root.left, path, res);\n preOrder(root.right, path, res);\n // \u56de\u9000\n path.pop();\n}\n
preorder_traversal_ii_compact.ts/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e8c */\nfunction preOrder(\n root: TreeNode | null,\n path: TreeNode[],\n res: TreeNode[][]\n): void {\n if (root === null) {\n return;\n }\n // \u5c1d\u8bd5\n path.push(root);\n if (root.val === 7) {\n // \u8bb0\u5f55\u89e3\n res.push([...path]);\n }\n preOrder(root.left, path, res);\n preOrder(root.right, path, res);\n // \u56de\u9000\n path.pop();\n}\n
preorder_traversal_ii_compact.dart/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e8c */\nvoid preOrder(\n TreeNode? root,\n List<TreeNode> path,\n List<List<TreeNode>> res,\n) {\n if (root == null) {\n return;\n }\n\n // \u5c1d\u8bd5\n path.add(root);\n if (root.val == 7) {\n // \u8bb0\u5f55\u89e3\n res.add(List.from(path));\n }\n preOrder(root.left, path, res);\n preOrder(root.right, path, res);\n // \u56de\u9000\n path.removeLast();\n}\n
preorder_traversal_ii_compact.rs/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e8c */\nfn pre_order(res: &mut Vec<Vec<Rc<RefCell<TreeNode>>>>, path: &mut Vec<Rc<RefCell<TreeNode>>>, root: Option<Rc<RefCell<TreeNode>>>) {\n if root.is_none() {\n return;\n }\n if let Some(node) = root {\n // \u5c1d\u8bd5\n path.push(node.clone());\n if node.borrow().val == 7 {\n // \u8bb0\u5f55\u89e3\n res.push(path.clone());\n }\n pre_order(res, path, node.borrow().left.clone());\n pre_order(res, path, node.borrow().right.clone());\n // \u56de\u9000\n path.remove(path.len() - 1);\n }\n}\n
preorder_traversal_ii_compact.c/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e8c */\nvoid preOrder(TreeNode *root) {\n if (root == NULL) {\n return;\n }\n // \u5c1d\u8bd5\n path[pathSize++] = root;\n if (root->val == 7) {\n // \u8bb0\u5f55\u89e3\n for (int i = 0; i < pathSize; ++i) {\n res[resSize][i] = path[i];\n }\n resSize++;\n }\n preOrder(root->left);\n preOrder(root->right);\n // \u56de\u9000\n pathSize--;\n}\n
preorder_traversal_ii_compact.zig[class]{}-[func]{preOrder}\n
\u5728\u6bcf\u6b21\u201c\u5c1d\u8bd5\u201d\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u5c06\u5f53\u524d\u8282\u70b9\u6dfb\u52a0\u8fdb path
\u6765\u8bb0\u5f55\u8def\u5f84\uff1b\u800c\u5728\u201c\u56de\u9000\u201d\u524d\uff0c\u6211\u4eec\u9700\u8981\u5c06\u8be5\u8282\u70b9\u4ece path
\u4e2d\u5f39\u51fa\uff0c\u4ee5\u6062\u590d\u672c\u6b21\u5c1d\u8bd5\u4e4b\u524d\u7684\u72b6\u6001\u3002
\u89c2\u5bdf\u56fe 13-2 \u6240\u793a\u7684\u8fc7\u7a0b\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5c1d\u8bd5\u548c\u56de\u9000\u7406\u89e3\u4e3a\u201c\u524d\u8fdb\u201d\u4e0e\u201c\u64a4\u9500\u201d\uff0c\u4e24\u4e2a\u64cd\u4f5c\u4e92\u4e3a\u9006\u5411\u3002
<1><2><3><4><5><6><7><8><9><10><11>\u56fe 13-2 \u00a0 \u5c1d\u8bd5\u4e0e\u56de\u9000
"},{"location":"chapter_backtracking/backtracking_algorithm/#1312","title":"13.1.2 \u00a0 \u526a\u679d","text":"\u590d\u6742\u7684\u56de\u6eaf\u95ee\u9898\u901a\u5e38\u5305\u542b\u4e00\u4e2a\u6216\u591a\u4e2a\u7ea6\u675f\u6761\u4ef6\uff0c\u7ea6\u675f\u6761\u4ef6\u901a\u5e38\u53ef\u7528\u4e8e\u201c\u526a\u679d\u201d\u3002
\u4f8b\u9898\u4e09
\u5728\u4e8c\u53c9\u6811\u4e2d\u641c\u7d22\u6240\u6709\u503c\u4e3a \\(7\\) \u7684\u8282\u70b9\uff0c\u8bf7\u8fd4\u56de\u6839\u8282\u70b9\u5230\u8fd9\u4e9b\u8282\u70b9\u7684\u8def\u5f84\uff0c\u5e76\u8981\u6c42\u8def\u5f84\u4e2d\u4e0d\u5305\u542b\u503c\u4e3a \\(3\\) \u7684\u8282\u70b9\u3002
\u4e3a\u4e86\u6ee1\u8db3\u4ee5\u4e0a\u7ea6\u675f\u6761\u4ef6\uff0c\u6211\u4eec\u9700\u8981\u6dfb\u52a0\u526a\u679d\u64cd\u4f5c\uff1a\u5728\u641c\u7d22\u8fc7\u7a0b\u4e2d\uff0c\u82e5\u9047\u5230\u503c\u4e3a \\(3\\) \u7684\u8282\u70b9\uff0c\u5219\u63d0\u524d\u8fd4\u56de\uff0c\u4e0d\u518d\u7ee7\u7eed\u641c\u7d22\u3002\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig preorder_traversal_iii_compact.pydef pre_order(root: TreeNode):\n \"\"\"\u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e09\"\"\"\n # \u526a\u679d\n if root is None or root.val == 3:\n return\n # \u5c1d\u8bd5\n path.append(root)\n if root.val == 7:\n # \u8bb0\u5f55\u89e3\n res.append(list(path))\n pre_order(root.left)\n pre_order(root.right)\n # \u56de\u9000\n path.pop()\n
preorder_traversal_iii_compact.cpp/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e09 */\nvoid preOrder(TreeNode *root) {\n // \u526a\u679d\n if (root == nullptr || root->val == 3) {\n return;\n }\n // \u5c1d\u8bd5\n path.push_back(root);\n if (root->val == 7) {\n // \u8bb0\u5f55\u89e3\n res.push_back(path);\n }\n preOrder(root->left);\n preOrder(root->right);\n // \u56de\u9000\n path.pop_back();\n}\n
preorder_traversal_iii_compact.java/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e09 */\nvoid preOrder(TreeNode root) {\n // \u526a\u679d\n if (root == null || root.val == 3) {\n return;\n }\n // \u5c1d\u8bd5\n path.add(root);\n if (root.val == 7) {\n // \u8bb0\u5f55\u89e3\n res.add(new ArrayList<>(path));\n }\n preOrder(root.left);\n preOrder(root.right);\n // \u56de\u9000\n path.remove(path.size() - 1);\n}\n
preorder_traversal_iii_compact.cs/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e09 */\nvoid PreOrder(TreeNode? root) {\n // \u526a\u679d\n if (root == null || root.val == 3) {\n return;\n }\n // \u5c1d\u8bd5\n path.Add(root);\n if (root.val == 7) {\n // \u8bb0\u5f55\u89e3\n res.Add(new List<TreeNode>(path));\n }\n PreOrder(root.left);\n PreOrder(root.right);\n // \u56de\u9000\n path.RemoveAt(path.Count - 1);\n}\n
preorder_traversal_iii_compact.go/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e09 */\nfunc preOrderIII(root *TreeNode, res *[][]*TreeNode, path *[]*TreeNode) {\n // \u526a\u679d\n if root == nil || root.Val == 3 {\n return\n }\n // \u5c1d\u8bd5\n *path = append(*path, root)\n if root.Val.(int) == 7 {\n // \u8bb0\u5f55\u89e3\n *res = append(*res, *path)\n }\n preOrderIII(root.Left, res, path)\n preOrderIII(root.Right, res, path)\n // \u56de\u9000\n *path = (*path)[:len(*path)-1]\n}\n
preorder_traversal_iii_compact.swift/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e09 */\nfunc preOrder(root: TreeNode?) {\n // \u526a\u679d\n guard let root = root, root.val != 3 else {\n return\n }\n // \u5c1d\u8bd5\n path.append(root)\n if root.val == 7 {\n // \u8bb0\u5f55\u89e3\n res.append(path)\n }\n preOrder(root: root.left)\n preOrder(root: root.right)\n // \u56de\u9000\n path.removeLast()\n}\n
preorder_traversal_iii_compact.js/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e09 */\nfunction preOrder(root, path, res) {\n // \u526a\u679d\n if (root === null || root.val === 3) {\n return;\n }\n // \u5c1d\u8bd5\n path.push(root);\n if (root.val === 7) {\n // \u8bb0\u5f55\u89e3\n res.push([...path]);\n }\n preOrder(root.left, path, res);\n preOrder(root.right, path, res);\n // \u56de\u9000\n path.pop();\n}\n
preorder_traversal_iii_compact.ts/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e09 */\nfunction preOrder(\n root: TreeNode | null,\n path: TreeNode[],\n res: TreeNode[][]\n): void {\n // \u526a\u679d\n if (root === null || root.val === 3) {\n return;\n }\n // \u5c1d\u8bd5\n path.push(root);\n if (root.val === 7) {\n // \u8bb0\u5f55\u89e3\n res.push([...path]);\n }\n preOrder(root.left, path, res);\n preOrder(root.right, path, res);\n // \u56de\u9000\n path.pop();\n}\n
preorder_traversal_iii_compact.dart/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e09 */\nvoid preOrder(\n TreeNode? root,\n List<TreeNode> path,\n List<List<TreeNode>> res,\n) {\n if (root == null || root.val == 3) {\n return;\n }\n\n // \u5c1d\u8bd5\n path.add(root);\n if (root.val == 7) {\n // \u8bb0\u5f55\u89e3\n res.add(List.from(path));\n }\n preOrder(root.left, path, res);\n preOrder(root.right, path, res);\n // \u56de\u9000\n path.removeLast();\n}\n
preorder_traversal_iii_compact.rs/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e09 */\nfn pre_order(res: &mut Vec<Vec<Rc<RefCell<TreeNode>>>>, path: &mut Vec<Rc<RefCell<TreeNode>>>, root: Option<Rc<RefCell<TreeNode>>>) {\n // \u526a\u679d\n if root.is_none() || root.as_ref().unwrap().borrow().val == 3 {\n return;\n }\n if let Some(node) = root {\n // \u5c1d\u8bd5\n path.push(node.clone());\n if node.borrow().val == 7 {\n // \u8bb0\u5f55\u89e3\n res.push(path.clone());\n }\n pre_order(res, path, node.borrow().left.clone());\n pre_order(res, path, node.borrow().right.clone());\n // \u56de\u9000\n path.remove(path.len() - 1);\n }\n}\n
preorder_traversal_iii_compact.c/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e09 */\nvoid preOrder(TreeNode *root) {\n // \u526a\u679d\n if (root == NULL || root->val == 3) {\n return;\n }\n // \u5c1d\u8bd5\n path[pathSize++] = root;\n if (root->val == 7) {\n // \u8bb0\u5f55\u89e3\n for (int i = 0; i < pathSize; i++) {\n res[resSize][i] = path[i];\n }\n resSize++;\n }\n preOrder(root->left);\n preOrder(root->right);\n // \u56de\u9000\n pathSize--;\n}\n
preorder_traversal_iii_compact.zig[class]{}-[func]{preOrder}\n
\u201c\u526a\u679d\u201d\u662f\u4e00\u4e2a\u975e\u5e38\u5f62\u8c61\u7684\u540d\u8bcd\u3002\u5982\u56fe 13-3 \u6240\u793a\uff0c\u5728\u641c\u7d22\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u201c\u526a\u6389\u201d\u4e86\u4e0d\u6ee1\u8db3\u7ea6\u675f\u6761\u4ef6\u7684\u641c\u7d22\u5206\u652f\uff0c\u907f\u514d\u8bb8\u591a\u65e0\u610f\u4e49\u7684\u5c1d\u8bd5\uff0c\u4ece\u800c\u63d0\u9ad8\u4e86\u641c\u7d22\u6548\u7387\u3002
\u56fe 13-3 \u00a0 \u6839\u636e\u7ea6\u675f\u6761\u4ef6\u526a\u679d
"},{"location":"chapter_backtracking/backtracking_algorithm/#1313","title":"13.1.3 \u00a0 \u6846\u67b6\u4ee3\u7801","text":"\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c1d\u8bd5\u5c06\u56de\u6eaf\u7684\u201c\u5c1d\u8bd5\u3001\u56de\u9000\u3001\u526a\u679d\u201d\u7684\u4e3b\u4f53\u6846\u67b6\u63d0\u70bc\u51fa\u6765\uff0c\u63d0\u5347\u4ee3\u7801\u7684\u901a\u7528\u6027\u3002
\u5728\u4ee5\u4e0b\u6846\u67b6\u4ee3\u7801\u4e2d\uff0cstate
\u8868\u793a\u95ee\u9898\u7684\u5f53\u524d\u72b6\u6001\uff0cchoices
\u8868\u793a\u5f53\u524d\u72b6\u6001\u4e0b\u53ef\u4ee5\u505a\u51fa\u7684\u9009\u62e9\uff1a
def backtrack(state: State, choices: list[choice], res: list[state]):\n \"\"\"\u56de\u6eaf\u7b97\u6cd5\u6846\u67b6\"\"\"\n # \u5224\u65ad\u662f\u5426\u4e3a\u89e3\n if is_solution(state):\n # \u8bb0\u5f55\u89e3\n record_solution(state, res)\n # \u4e0d\u518d\u7ee7\u7eed\u641c\u7d22\n return\n # \u904d\u5386\u6240\u6709\u9009\u62e9\n for choice in choices:\n # \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\n if is_valid(state, choice):\n # \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n make_choice(state, choice)\n backtrack(state, choices, res)\n # \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n undo_choice(state, choice)\n
/* \u56de\u6eaf\u7b97\u6cd5\u6846\u67b6 */\nvoid backtrack(State *state, vector<Choice *> &choices, vector<State *> &res) {\n // \u5224\u65ad\u662f\u5426\u4e3a\u89e3\n if (isSolution(state)) {\n // \u8bb0\u5f55\u89e3\n recordSolution(state, res);\n // \u4e0d\u518d\u7ee7\u7eed\u641c\u7d22\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for (Choice choice : choices) {\n // \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\n if (isValid(state, choice)) {\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n makeChoice(state, choice);\n backtrack(state, choices, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n undoChoice(state, choice);\n }\n }\n}\n
/* \u56de\u6eaf\u7b97\u6cd5\u6846\u67b6 */\nvoid backtrack(State state, List<Choice> choices, List<State> res) {\n // \u5224\u65ad\u662f\u5426\u4e3a\u89e3\n if (isSolution(state)) {\n // \u8bb0\u5f55\u89e3\n recordSolution(state, res);\n // \u4e0d\u518d\u7ee7\u7eed\u641c\u7d22\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for (Choice choice : choices) {\n // \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\n if (isValid(state, choice)) {\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n makeChoice(state, choice);\n backtrack(state, choices, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n undoChoice(state, choice);\n }\n }\n}\n
/* \u56de\u6eaf\u7b97\u6cd5\u6846\u67b6 */\nvoid Backtrack(State state, List<Choice> choices, List<State> res) {\n // \u5224\u65ad\u662f\u5426\u4e3a\u89e3\n if (IsSolution(state)) {\n // \u8bb0\u5f55\u89e3\n RecordSolution(state, res);\n // \u4e0d\u518d\u7ee7\u7eed\u641c\u7d22\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n foreach (Choice choice in choices) {\n // \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\n if (IsValid(state, choice)) {\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n MakeChoice(state, choice);\n Backtrack(state, choices, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n UndoChoice(state, choice);\n }\n }\n}\n
/* \u56de\u6eaf\u7b97\u6cd5\u6846\u67b6 */\nfunc backtrack(state *State, choices []Choice, res *[]State) {\n // \u5224\u65ad\u662f\u5426\u4e3a\u89e3\n if isSolution(state) {\n // \u8bb0\u5f55\u89e3\n recordSolution(state, res)\n // \u4e0d\u518d\u7ee7\u7eed\u641c\u7d22\n return\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for _, choice := range choices {\n // \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\n if isValid(state, choice) {\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n makeChoice(state, choice)\n backtrack(state, choices, res)\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n undoChoice(state, choice)\n }\n }\n}\n
/* \u56de\u6eaf\u7b97\u6cd5\u6846\u67b6 */\nfunc backtrack(state: inout State, choices: [Choice], res: inout [State]) {\n // \u5224\u65ad\u662f\u5426\u4e3a\u89e3\n if isSolution(state: state) {\n // \u8bb0\u5f55\u89e3\n recordSolution(state: state, res: &res)\n // \u4e0d\u518d\u7ee7\u7eed\u641c\u7d22\n return\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for choice in choices {\n // \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\n if isValid(state: state, choice: choice) {\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n makeChoice(state: &state, choice: choice)\n backtrack(state: &state, choices: choices, res: &res)\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n undoChoice(state: &state, choice: choice)\n }\n }\n}\n
/* \u56de\u6eaf\u7b97\u6cd5\u6846\u67b6 */\nfunction backtrack(state, choices, res) {\n // \u5224\u65ad\u662f\u5426\u4e3a\u89e3\n if (isSolution(state)) {\n // \u8bb0\u5f55\u89e3\n recordSolution(state, res);\n // \u4e0d\u518d\u7ee7\u7eed\u641c\u7d22\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for (let choice of choices) {\n // \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\n if (isValid(state, choice)) {\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n makeChoice(state, choice);\n backtrack(state, choices, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n undoChoice(state, choice);\n }\n }\n}\n
/* \u56de\u6eaf\u7b97\u6cd5\u6846\u67b6 */\nfunction backtrack(state: State, choices: Choice[], res: State[]): void {\n // \u5224\u65ad\u662f\u5426\u4e3a\u89e3\n if (isSolution(state)) {\n // \u8bb0\u5f55\u89e3\n recordSolution(state, res);\n // \u4e0d\u518d\u7ee7\u7eed\u641c\u7d22\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for (let choice of choices) {\n // \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\n if (isValid(state, choice)) {\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n makeChoice(state, choice);\n backtrack(state, choices, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n undoChoice(state, choice);\n }\n }\n}\n
/* \u56de\u6eaf\u7b97\u6cd5\u6846\u67b6 */\nvoid backtrack(State state, List<Choice>, List<State> res) {\n // \u5224\u65ad\u662f\u5426\u4e3a\u89e3\n if (isSolution(state)) {\n // \u8bb0\u5f55\u89e3\n recordSolution(state, res);\n // \u4e0d\u518d\u7ee7\u7eed\u641c\u7d22\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for (Choice choice in choices) {\n // \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\n if (isValid(state, choice)) {\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n makeChoice(state, choice);\n backtrack(state, choices, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n undoChoice(state, choice);\n }\n }\n}\n
/* \u56de\u6eaf\u7b97\u6cd5\u6846\u67b6 */\nfn backtrack(state: &mut State, choices: &Vec<Choice>, res: &mut Vec<State>) {\n // \u5224\u65ad\u662f\u5426\u4e3a\u89e3\n if is_solution(state) {\n // \u8bb0\u5f55\u89e3\n record_solution(state, res);\n // \u4e0d\u518d\u7ee7\u7eed\u641c\u7d22\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for choice in choices {\n // \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\n if is_valid(state, choice) {\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n make_choice(state, choice);\n backtrack(state, choices, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n undo_choice(state, choice);\n }\n }\n}\n
/* \u56de\u6eaf\u7b97\u6cd5\u6846\u67b6 */\nvoid backtrack(State *state, Choice *choices, int numChoices, State *res, int numRes) {\n // \u5224\u65ad\u662f\u5426\u4e3a\u89e3\n if (isSolution(state)) {\n // \u8bb0\u5f55\u89e3\n recordSolution(state, res, numRes);\n // \u4e0d\u518d\u7ee7\u7eed\u641c\u7d22\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for (int i = 0; i < numChoices; i++) {\n // \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\n if (isValid(state, &choices[i])) {\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n makeChoice(state, &choices[i]);\n backtrack(state, choices, numChoices, res, numRes);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n undoChoice(state, &choices[i]);\n }\n }\n}\n
\n
\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u57fa\u4e8e\u6846\u67b6\u4ee3\u7801\u6765\u89e3\u51b3\u4f8b\u9898\u4e09\u3002\u72b6\u6001 state
\u4e3a\u8282\u70b9\u904d\u5386\u8def\u5f84\uff0c\u9009\u62e9 choices
\u4e3a\u5f53\u524d\u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u548c\u53f3\u5b50\u8282\u70b9\uff0c\u7ed3\u679c res
\u662f\u8def\u5f84\u5217\u8868\uff1a
def is_solution(state: list[TreeNode]) -> bool:\n \"\"\"\u5224\u65ad\u5f53\u524d\u72b6\u6001\u662f\u5426\u4e3a\u89e3\"\"\"\n return state and state[-1].val == 7\n\ndef record_solution(state: list[TreeNode], res: list[list[TreeNode]]):\n \"\"\"\u8bb0\u5f55\u89e3\"\"\"\n res.append(list(state))\n\ndef is_valid(state: list[TreeNode], choice: TreeNode) -> bool:\n \"\"\"\u5224\u65ad\u5728\u5f53\u524d\u72b6\u6001\u4e0b\uff0c\u8be5\u9009\u62e9\u662f\u5426\u5408\u6cd5\"\"\"\n return choice is not None and choice.val != 3\n\ndef make_choice(state: list[TreeNode], choice: TreeNode):\n \"\"\"\u66f4\u65b0\u72b6\u6001\"\"\"\n state.append(choice)\n\ndef undo_choice(state: list[TreeNode], choice: TreeNode):\n \"\"\"\u6062\u590d\u72b6\u6001\"\"\"\n state.pop()\n\ndef backtrack(\n state: list[TreeNode], choices: list[TreeNode], res: list[list[TreeNode]]\n):\n \"\"\"\u56de\u6eaf\u7b97\u6cd5\uff1a\u4f8b\u9898\u4e09\"\"\"\n # \u68c0\u67e5\u662f\u5426\u4e3a\u89e3\n if is_solution(state):\n # \u8bb0\u5f55\u89e3\n record_solution(state, res)\n # \u904d\u5386\u6240\u6709\u9009\u62e9\n for choice in choices:\n # \u526a\u679d\uff1a\u68c0\u67e5\u9009\u62e9\u662f\u5426\u5408\u6cd5\n if is_valid(state, choice):\n # \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n make_choice(state, choice)\n # \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state, [choice.left, choice.right], res)\n # \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n undo_choice(state, choice)\n
preorder_traversal_iii_template.cpp/* \u5224\u65ad\u5f53\u524d\u72b6\u6001\u662f\u5426\u4e3a\u89e3 */\nbool isSolution(vector<TreeNode *> &state) {\n return !state.empty() && state.back()->val == 7;\n}\n\n/* \u8bb0\u5f55\u89e3 */\nvoid recordSolution(vector<TreeNode *> &state, vector<vector<TreeNode *>> &res) {\n res.push_back(state);\n}\n\n/* \u5224\u65ad\u5728\u5f53\u524d\u72b6\u6001\u4e0b\uff0c\u8be5\u9009\u62e9\u662f\u5426\u5408\u6cd5 */\nbool isValid(vector<TreeNode *> &state, TreeNode *choice) {\n return choice != nullptr && choice->val != 3;\n}\n\n/* \u66f4\u65b0\u72b6\u6001 */\nvoid makeChoice(vector<TreeNode *> &state, TreeNode *choice) {\n state.push_back(choice);\n}\n\n/* \u6062\u590d\u72b6\u6001 */\nvoid undoChoice(vector<TreeNode *> &state, TreeNode *choice) {\n state.pop_back();\n}\n\n/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u4f8b\u9898\u4e09 */\nvoid backtrack(vector<TreeNode *> &state, vector<TreeNode *> &choices, vector<vector<TreeNode *>> &res) {\n // \u68c0\u67e5\u662f\u5426\u4e3a\u89e3\n if (isSolution(state)) {\n // \u8bb0\u5f55\u89e3\n recordSolution(state, res);\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for (TreeNode *choice : choices) {\n // \u526a\u679d\uff1a\u68c0\u67e5\u9009\u62e9\u662f\u5426\u5408\u6cd5\n if (isValid(state, choice)) {\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n makeChoice(state, choice);\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n vector<TreeNode *> nextChoices{choice->left, choice->right};\n backtrack(state, nextChoices, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n undoChoice(state, choice);\n }\n }\n}\n
preorder_traversal_iii_template.java/* \u5224\u65ad\u5f53\u524d\u72b6\u6001\u662f\u5426\u4e3a\u89e3 */\nboolean isSolution(List<TreeNode> state) {\n return !state.isEmpty() && state.get(state.size() - 1).val == 7;\n}\n\n/* \u8bb0\u5f55\u89e3 */\nvoid recordSolution(List<TreeNode> state, List<List<TreeNode>> res) {\n res.add(new ArrayList<>(state));\n}\n\n/* \u5224\u65ad\u5728\u5f53\u524d\u72b6\u6001\u4e0b\uff0c\u8be5\u9009\u62e9\u662f\u5426\u5408\u6cd5 */\nboolean isValid(List<TreeNode> state, TreeNode choice) {\n return choice != null && choice.val != 3;\n}\n\n/* \u66f4\u65b0\u72b6\u6001 */\nvoid makeChoice(List<TreeNode> state, TreeNode choice) {\n state.add(choice);\n}\n\n/* \u6062\u590d\u72b6\u6001 */\nvoid undoChoice(List<TreeNode> state, TreeNode choice) {\n state.remove(state.size() - 1);\n}\n\n/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u4f8b\u9898\u4e09 */\nvoid backtrack(List<TreeNode> state, List<TreeNode> choices, List<List<TreeNode>> res) {\n // \u68c0\u67e5\u662f\u5426\u4e3a\u89e3\n if (isSolution(state)) {\n // \u8bb0\u5f55\u89e3\n recordSolution(state, res);\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for (TreeNode choice : choices) {\n // \u526a\u679d\uff1a\u68c0\u67e5\u9009\u62e9\u662f\u5426\u5408\u6cd5\n if (isValid(state, choice)) {\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n makeChoice(state, choice);\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state, Arrays.asList(choice.left, choice.right), res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n undoChoice(state, choice);\n }\n }\n}\n
preorder_traversal_iii_template.cs/* \u5224\u65ad\u5f53\u524d\u72b6\u6001\u662f\u5426\u4e3a\u89e3 */\nbool IsSolution(List<TreeNode> state) {\n return state.Count != 0 && state[^1].val == 7;\n}\n\n/* \u8bb0\u5f55\u89e3 */\nvoid RecordSolution(List<TreeNode> state, List<List<TreeNode>> res) {\n res.Add(new List<TreeNode>(state));\n}\n\n/* \u5224\u65ad\u5728\u5f53\u524d\u72b6\u6001\u4e0b\uff0c\u8be5\u9009\u62e9\u662f\u5426\u5408\u6cd5 */\nbool IsValid(List<TreeNode> state, TreeNode choice) {\n return choice != null && choice.val != 3;\n}\n\n/* \u66f4\u65b0\u72b6\u6001 */\nvoid MakeChoice(List<TreeNode> state, TreeNode choice) {\n state.Add(choice);\n}\n\n/* \u6062\u590d\u72b6\u6001 */\nvoid UndoChoice(List<TreeNode> state, TreeNode choice) {\n state.RemoveAt(state.Count - 1);\n}\n\n/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u4f8b\u9898\u4e09 */\nvoid Backtrack(List<TreeNode> state, List<TreeNode> choices, List<List<TreeNode>> res) {\n // \u68c0\u67e5\u662f\u5426\u4e3a\u89e3\n if (IsSolution(state)) {\n // \u8bb0\u5f55\u89e3\n RecordSolution(state, res);\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n foreach (TreeNode choice in choices) {\n // \u526a\u679d\uff1a\u68c0\u67e5\u9009\u62e9\u662f\u5426\u5408\u6cd5\n if (IsValid(state, choice)) {\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n MakeChoice(state, choice);\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n Backtrack(state, [choice.left!, choice.right!], res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n UndoChoice(state, choice);\n }\n }\n}\n
preorder_traversal_iii_template.go/* \u5224\u65ad\u5f53\u524d\u72b6\u6001\u662f\u5426\u4e3a\u89e3 */\nfunc isSolution(state *[]*TreeNode) bool {\n return len(*state) != 0 && (*state)[len(*state)-1].Val == 7\n}\n\n/* \u8bb0\u5f55\u89e3 */\nfunc recordSolution(state *[]*TreeNode, res *[][]*TreeNode) {\n *res = append(*res, *state)\n}\n\n/* \u5224\u65ad\u5728\u5f53\u524d\u72b6\u6001\u4e0b\uff0c\u8be5\u9009\u62e9\u662f\u5426\u5408\u6cd5 */\nfunc isValid(state *[]*TreeNode, choice *TreeNode) bool {\n return choice != nil && choice.Val != 3\n}\n\n/* \u66f4\u65b0\u72b6\u6001 */\nfunc makeChoice(state *[]*TreeNode, choice *TreeNode) {\n *state = append(*state, choice)\n}\n\n/* \u6062\u590d\u72b6\u6001 */\nfunc undoChoice(state *[]*TreeNode, choice *TreeNode) {\n *state = (*state)[:len(*state)-1]\n}\n\n/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u4f8b\u9898\u4e09 */\nfunc backtrackIII(state *[]*TreeNode, choices *[]*TreeNode, res *[][]*TreeNode) {\n // \u68c0\u67e5\u662f\u5426\u4e3a\u89e3\n if isSolution(state) {\n // \u8bb0\u5f55\u89e3\n recordSolution(state, res)\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for _, choice := range *choices {\n // \u526a\u679d\uff1a\u68c0\u67e5\u9009\u62e9\u662f\u5426\u5408\u6cd5\n if isValid(state, choice) {\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n makeChoice(state, choice)\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n temp := make([]*TreeNode, 0)\n temp = append(temp, choice.Left, choice.Right)\n backtrackIII(state, &temp, res)\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n undoChoice(state, choice)\n }\n }\n}\n
preorder_traversal_iii_template.swift/* \u5224\u65ad\u5f53\u524d\u72b6\u6001\u662f\u5426\u4e3a\u89e3 */\nfunc isSolution(state: [TreeNode]) -> Bool {\n !state.isEmpty && state.last!.val == 7\n}\n\n/* \u8bb0\u5f55\u89e3 */\nfunc recordSolution(state: [TreeNode], res: inout [[TreeNode]]) {\n res.append(state)\n}\n\n/* \u5224\u65ad\u5728\u5f53\u524d\u72b6\u6001\u4e0b\uff0c\u8be5\u9009\u62e9\u662f\u5426\u5408\u6cd5 */\nfunc isValid(state: [TreeNode], choice: TreeNode?) -> Bool {\n choice != nil && choice!.val != 3\n}\n\n/* \u66f4\u65b0\u72b6\u6001 */\nfunc makeChoice(state: inout [TreeNode], choice: TreeNode) {\n state.append(choice)\n}\n\n/* \u6062\u590d\u72b6\u6001 */\nfunc undoChoice(state: inout [TreeNode], choice: TreeNode) {\n state.removeLast()\n}\n\n/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u4f8b\u9898\u4e09 */\nfunc backtrack(state: inout [TreeNode], choices: [TreeNode], res: inout [[TreeNode]]) {\n // \u68c0\u67e5\u662f\u5426\u4e3a\u89e3\n if isSolution(state: state) {\n recordSolution(state: state, res: &res)\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for choice in choices {\n // \u526a\u679d\uff1a\u68c0\u67e5\u9009\u62e9\u662f\u5426\u5408\u6cd5\n if isValid(state: state, choice: choice) {\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n makeChoice(state: &state, choice: choice)\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state: &state, choices: [choice.left, choice.right].compactMap { $0 }, res: &res)\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n undoChoice(state: &state, choice: choice)\n }\n }\n}\n
preorder_traversal_iii_template.js/* \u5224\u65ad\u5f53\u524d\u72b6\u6001\u662f\u5426\u4e3a\u89e3 */\nfunction isSolution(state) {\n return state && state[state.length - 1]?.val === 7;\n}\n\n/* \u8bb0\u5f55\u89e3 */\nfunction recordSolution(state, res) {\n res.push([...state]);\n}\n\n/* \u5224\u65ad\u5728\u5f53\u524d\u72b6\u6001\u4e0b\uff0c\u8be5\u9009\u62e9\u662f\u5426\u5408\u6cd5 */\nfunction isValid(state, choice) {\n return choice !== null && choice.val !== 3;\n}\n\n/* \u66f4\u65b0\u72b6\u6001 */\nfunction makeChoice(state, choice) {\n state.push(choice);\n}\n\n/* \u6062\u590d\u72b6\u6001 */\nfunction undoChoice(state) {\n state.pop();\n}\n\n/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u4f8b\u9898\u4e09 */\nfunction backtrack(state, choices, res) {\n // \u68c0\u67e5\u662f\u5426\u4e3a\u89e3\n if (isSolution(state)) {\n // \u8bb0\u5f55\u89e3\n recordSolution(state, res);\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for (const choice of choices) {\n // \u526a\u679d\uff1a\u68c0\u67e5\u9009\u62e9\u662f\u5426\u5408\u6cd5\n if (isValid(state, choice)) {\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n makeChoice(state, choice);\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state, [choice.left, choice.right], res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n undoChoice(state);\n }\n }\n}\n
preorder_traversal_iii_template.ts/* \u5224\u65ad\u5f53\u524d\u72b6\u6001\u662f\u5426\u4e3a\u89e3 */\nfunction isSolution(state: TreeNode[]): boolean {\n return state && state[state.length - 1]?.val === 7;\n}\n\n/* \u8bb0\u5f55\u89e3 */\nfunction recordSolution(state: TreeNode[], res: TreeNode[][]): void {\n res.push([...state]);\n}\n\n/* \u5224\u65ad\u5728\u5f53\u524d\u72b6\u6001\u4e0b\uff0c\u8be5\u9009\u62e9\u662f\u5426\u5408\u6cd5 */\nfunction isValid(state: TreeNode[], choice: TreeNode): boolean {\n return choice !== null && choice.val !== 3;\n}\n\n/* \u66f4\u65b0\u72b6\u6001 */\nfunction makeChoice(state: TreeNode[], choice: TreeNode): void {\n state.push(choice);\n}\n\n/* \u6062\u590d\u72b6\u6001 */\nfunction undoChoice(state: TreeNode[]): void {\n state.pop();\n}\n\n/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u4f8b\u9898\u4e09 */\nfunction backtrack(\n state: TreeNode[],\n choices: TreeNode[],\n res: TreeNode[][]\n): void {\n // \u68c0\u67e5\u662f\u5426\u4e3a\u89e3\n if (isSolution(state)) {\n // \u8bb0\u5f55\u89e3\n recordSolution(state, res);\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for (const choice of choices) {\n // \u526a\u679d\uff1a\u68c0\u67e5\u9009\u62e9\u662f\u5426\u5408\u6cd5\n if (isValid(state, choice)) {\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n makeChoice(state, choice);\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state, [choice.left, choice.right], res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n undoChoice(state);\n }\n }\n}\n
preorder_traversal_iii_template.dart/* \u5224\u65ad\u5f53\u524d\u72b6\u6001\u662f\u5426\u4e3a\u89e3 */\nbool isSolution(List<TreeNode> state) {\n return state.isNotEmpty && state.last.val == 7;\n}\n\n/* \u8bb0\u5f55\u89e3 */\nvoid recordSolution(List<TreeNode> state, List<List<TreeNode>> res) {\n res.add(List.from(state));\n}\n\n/* \u5224\u65ad\u5728\u5f53\u524d\u72b6\u6001\u4e0b\uff0c\u8be5\u9009\u62e9\u662f\u5426\u5408\u6cd5 */\nbool isValid(List<TreeNode> state, TreeNode? choice) {\n return choice != null && choice.val != 3;\n}\n\n/* \u66f4\u65b0\u72b6\u6001 */\nvoid makeChoice(List<TreeNode> state, TreeNode? choice) {\n state.add(choice!);\n}\n\n/* \u6062\u590d\u72b6\u6001 */\nvoid undoChoice(List<TreeNode> state, TreeNode? choice) {\n state.removeLast();\n}\n\n/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u4f8b\u9898\u4e09 */\nvoid backtrack(\n List<TreeNode> state,\n List<TreeNode?> choices,\n List<List<TreeNode>> res,\n) {\n // \u68c0\u67e5\u662f\u5426\u4e3a\u89e3\n if (isSolution(state)) {\n // \u8bb0\u5f55\u89e3\n recordSolution(state, res);\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for (TreeNode? choice in choices) {\n // \u526a\u679d\uff1a\u68c0\u67e5\u9009\u62e9\u662f\u5426\u5408\u6cd5\n if (isValid(state, choice)) {\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n makeChoice(state, choice);\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state, [choice!.left, choice.right], res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n undoChoice(state, choice);\n }\n }\n}\n
preorder_traversal_iii_template.rs/* \u5224\u65ad\u5f53\u524d\u72b6\u6001\u662f\u5426\u4e3a\u89e3 */\nfn is_solution(state: &mut Vec<Rc<RefCell<TreeNode>>>) -> bool {\n return !state.is_empty() && state.get(state.len() - 1).unwrap().borrow().val == 7;\n}\n\n/* \u8bb0\u5f55\u89e3 */\nfn record_solution(state: &mut Vec<Rc<RefCell<TreeNode>>>, res: &mut Vec<Vec<Rc<RefCell<TreeNode>>>>) {\n res.push(state.clone());\n}\n\n/* \u5224\u65ad\u5728\u5f53\u524d\u72b6\u6001\u4e0b\uff0c\u8be5\u9009\u62e9\u662f\u5426\u5408\u6cd5 */\nfn is_valid(_: &mut Vec<Rc<RefCell<TreeNode>>>, choice: Rc<RefCell<TreeNode>>) -> bool {\n return choice.borrow().val != 3;\n}\n\n/* \u66f4\u65b0\u72b6\u6001 */\nfn make_choice(state: &mut Vec<Rc<RefCell<TreeNode>>>, choice: Rc<RefCell<TreeNode>>) {\n state.push(choice);\n}\n\n/* \u6062\u590d\u72b6\u6001 */\nfn undo_choice(state: &mut Vec<Rc<RefCell<TreeNode>>>, _: Rc<RefCell<TreeNode>>) {\n state.remove(state.len() - 1);\n}\n\n/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u4f8b\u9898\u4e09 */\nfn backtrack(state: &mut Vec<Rc<RefCell<TreeNode>>>, choices: &mut Vec<Rc<RefCell<TreeNode>>>, res: &mut Vec<Vec<Rc<RefCell<TreeNode>>>>) {\n // \u68c0\u67e5\u662f\u5426\u4e3a\u89e3\n if is_solution(state) {\n // \u8bb0\u5f55\u89e3\n record_solution(state, res);\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for choice in choices {\n // \u526a\u679d\uff1a\u68c0\u67e5\u9009\u62e9\u662f\u5426\u5408\u6cd5\n if is_valid(state, choice.clone()) {\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n make_choice(state, choice.clone());\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state, &mut vec![choice.borrow().left.clone().unwrap(), choice.borrow().right.clone().unwrap()], res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n undo_choice(state, choice.clone());\n }\n }\n}\n
preorder_traversal_iii_template.c/* \u5224\u65ad\u5f53\u524d\u72b6\u6001\u662f\u5426\u4e3a\u89e3 */\nbool isSolution(void) {\n return pathSize > 0 && path[pathSize - 1]->val == 7;\n}\n\n/* \u8bb0\u5f55\u89e3 */\nvoid recordSolution(void) {\n for (int i = 0; i < pathSize; i++) {\n res[resSize][i] = path[i];\n }\n resSize++;\n}\n\n/* \u5224\u65ad\u5728\u5f53\u524d\u72b6\u6001\u4e0b\uff0c\u8be5\u9009\u62e9\u662f\u5426\u5408\u6cd5 */\nbool isValid(TreeNode *choice) {\n return choice != NULL && choice->val != 3;\n}\n\n/* \u66f4\u65b0\u72b6\u6001 */\nvoid makeChoice(TreeNode *choice) {\n path[pathSize++] = choice;\n}\n\n/* \u6062\u590d\u72b6\u6001 */\nvoid undoChoice(void) {\n pathSize--;\n}\n\n/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u4f8b\u9898\u4e09 */\nvoid backtrack(TreeNode *choices[2]) {\n // \u68c0\u67e5\u662f\u5426\u4e3a\u89e3\n if (isSolution()) {\n // \u8bb0\u5f55\u89e3\n recordSolution();\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for (int i = 0; i < 2; i++) {\n TreeNode *choice = choices[i];\n // \u526a\u679d\uff1a\u68c0\u67e5\u9009\u62e9\u662f\u5426\u5408\u6cd5\n if (isValid(choice)) {\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n makeChoice(choice);\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n TreeNode *nextChoices[2] = {choice->left, choice->right};\n backtrack(nextChoices);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n undoChoice();\n }\n }\n}\n
preorder_traversal_iii_template.zig[class]{}-[func]{isSolution}\n\n[class]{}-[func]{recordSolution}\n\n[class]{}-[func]{isValid}\n\n[class]{}-[func]{makeChoice}\n\n[class]{}-[func]{undoChoice}\n\n[class]{}-[func]{backtrack}\n
\u6839\u636e\u9898\u610f\uff0c\u6211\u4eec\u5728\u627e\u5230\u503c\u4e3a \\(7\\) \u7684\u8282\u70b9\u540e\u5e94\u8be5\u7ee7\u7eed\u641c\u7d22\uff0c\u56e0\u6b64\u9700\u8981\u5c06\u8bb0\u5f55\u89e3\u4e4b\u540e\u7684 return
\u8bed\u53e5\u5220\u9664\u3002\u56fe 13-4 \u5bf9\u6bd4\u4e86\u4fdd\u7559\u6216\u5220\u9664 return
\u8bed\u53e5\u7684\u641c\u7d22\u8fc7\u7a0b\u3002
\u56fe 13-4 \u00a0 \u4fdd\u7559\u4e0e\u5220\u9664 return \u7684\u641c\u7d22\u8fc7\u7a0b\u5bf9\u6bd4
\u76f8\u6bd4\u57fa\u4e8e\u524d\u5e8f\u904d\u5386\u7684\u4ee3\u7801\u5b9e\u73b0\uff0c\u57fa\u4e8e\u56de\u6eaf\u7b97\u6cd5\u6846\u67b6\u7684\u4ee3\u7801\u5b9e\u73b0\u867d\u7136\u663e\u5f97\u5570\u5506\uff0c\u4f46\u901a\u7528\u6027\u66f4\u597d\u3002\u5b9e\u9645\u4e0a\uff0c\u8bb8\u591a\u56de\u6eaf\u95ee\u9898\u53ef\u4ee5\u5728\u8be5\u6846\u67b6\u4e0b\u89e3\u51b3\u3002\u6211\u4eec\u53ea\u9700\u6839\u636e\u5177\u4f53\u95ee\u9898\u6765\u5b9a\u4e49 state
\u548c choices
\uff0c\u5e76\u5b9e\u73b0\u6846\u67b6\u4e2d\u7684\u5404\u4e2a\u65b9\u6cd5\u5373\u53ef\u3002
\u4e3a\u4e86\u66f4\u6e05\u6670\u5730\u5206\u6790\u7b97\u6cd5\u95ee\u9898\uff0c\u6211\u4eec\u603b\u7ed3\u4e00\u4e0b\u56de\u6eaf\u7b97\u6cd5\u4e2d\u5e38\u7528\u672f\u8bed\u7684\u542b\u4e49\uff0c\u5e76\u5bf9\u7167\u4f8b\u9898\u4e09\u7ed9\u51fa\u5bf9\u5e94\u793a\u4f8b\uff0c\u5982\u8868 13-1 \u6240\u793a\u3002
\u8868 13-1 \u00a0 \u5e38\u89c1\u7684\u56de\u6eaf\u7b97\u6cd5\u672f\u8bed
\u540d\u8bcd \u5b9a\u4e49 \u4f8b\u9898\u4e09 \u89e3\uff08solution\uff09 \u89e3\u662f\u6ee1\u8db3\u95ee\u9898\u7279\u5b9a\u6761\u4ef6\u7684\u7b54\u6848\uff0c\u53ef\u80fd\u6709\u4e00\u4e2a\u6216\u591a\u4e2a \u6839\u8282\u70b9\u5230\u8282\u70b9 \\(7\\) \u7684\u6ee1\u8db3\u7ea6\u675f\u6761\u4ef6\u7684\u6240\u6709\u8def\u5f84 \u7ea6\u675f\u6761\u4ef6\uff08constraint\uff09 \u7ea6\u675f\u6761\u4ef6\u662f\u95ee\u9898\u4e2d\u9650\u5236\u89e3\u7684\u53ef\u884c\u6027\u7684\u6761\u4ef6\uff0c\u901a\u5e38\u7528\u4e8e\u526a\u679d \u8def\u5f84\u4e2d\u4e0d\u5305\u542b\u8282\u70b9 \\(3\\) \u72b6\u6001\uff08state\uff09 \u72b6\u6001\u8868\u793a\u95ee\u9898\u5728\u67d0\u4e00\u65f6\u523b\u7684\u60c5\u51b5\uff0c\u5305\u62ec\u5df2\u7ecf\u505a\u51fa\u7684\u9009\u62e9 \u5f53\u524d\u5df2\u8bbf\u95ee\u7684\u8282\u70b9\u8def\u5f84\uff0c\u5373path
\u8282\u70b9\u5217\u8868 \u5c1d\u8bd5\uff08attempt\uff09 \u5c1d\u8bd5\u662f\u6839\u636e\u53ef\u7528\u9009\u62e9\u6765\u63a2\u7d22\u89e3\u7a7a\u95f4\u7684\u8fc7\u7a0b\uff0c\u5305\u62ec\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\uff0c\u68c0\u67e5\u662f\u5426\u4e3a\u89e3 \u9012\u5f52\u8bbf\u95ee\u5de6\uff08\u53f3\uff09\u5b50\u8282\u70b9\uff0c\u5c06\u8282\u70b9\u6dfb\u52a0\u8fdb path
\uff0c\u5224\u65ad\u8282\u70b9\u7684\u503c\u662f\u5426\u4e3a \\(7\\) \u56de\u9000\uff08backtracking\uff09 \u56de\u9000\u6307\u9047\u5230\u4e0d\u6ee1\u8db3\u7ea6\u675f\u6761\u4ef6\u7684\u72b6\u6001\u65f6\uff0c\u64a4\u9500\u524d\u9762\u505a\u51fa\u7684\u9009\u62e9\uff0c\u56de\u5230\u4e0a\u4e00\u4e2a\u72b6\u6001 \u5f53\u8d8a\u8fc7\u53f6\u8282\u70b9\u3001\u7ed3\u675f\u8282\u70b9\u8bbf\u95ee\u3001\u9047\u5230\u503c\u4e3a \\(3\\) \u7684\u8282\u70b9\u65f6\u7ec8\u6b62\u641c\u7d22\uff0c\u51fd\u6570\u8fd4\u56de \u526a\u679d\uff08pruning\uff09 \u526a\u679d\u662f\u6839\u636e\u95ee\u9898\u7279\u6027\u548c\u7ea6\u675f\u6761\u4ef6\u907f\u514d\u65e0\u610f\u4e49\u7684\u641c\u7d22\u8def\u5f84\u7684\u65b9\u6cd5\uff0c\u53ef\u63d0\u9ad8\u641c\u7d22\u6548\u7387 \u5f53\u9047\u5230\u503c\u4e3a \\(3\\) \u7684\u8282\u70b9\u65f6\uff0c\u5219\u4e0d\u518d\u7ee7\u7eed\u641c\u7d22 Tip
\u95ee\u9898\u3001\u89e3\u3001\u72b6\u6001\u7b49\u6982\u5ff5\u662f\u901a\u7528\u7684\uff0c\u5728\u5206\u6cbb\u3001\u56de\u6eaf\u3001\u52a8\u6001\u89c4\u5212\u3001\u8d2a\u5fc3\u7b49\u7b97\u6cd5\u4e2d\u90fd\u6709\u6d89\u53ca\u3002
"},{"location":"chapter_backtracking/backtracking_algorithm/#1315","title":"13.1.5 \u00a0 \u4f18\u70b9\u4e0e\u5c40\u9650\u6027","text":"\u56de\u6eaf\u7b97\u6cd5\u672c\u8d28\u4e0a\u662f\u4e00\u79cd\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u7b97\u6cd5\uff0c\u5b83\u5c1d\u8bd5\u6240\u6709\u53ef\u80fd\u7684\u89e3\u51b3\u65b9\u6848\u76f4\u5230\u627e\u5230\u6ee1\u8db3\u6761\u4ef6\u7684\u89e3\u3002\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u5728\u4e8e\u80fd\u591f\u627e\u5230\u6240\u6709\u53ef\u80fd\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u800c\u4e14\u5728\u5408\u7406\u7684\u526a\u679d\u64cd\u4f5c\u4e0b\uff0c\u5177\u6709\u5f88\u9ad8\u7684\u6548\u7387\u3002
\u7136\u800c\uff0c\u5728\u5904\u7406\u5927\u89c4\u6a21\u6216\u8005\u590d\u6742\u95ee\u9898\u65f6\uff0c\u56de\u6eaf\u7b97\u6cd5\u7684\u8fd0\u884c\u6548\u7387\u53ef\u80fd\u96be\u4ee5\u63a5\u53d7\u3002
\u5373\u4fbf\u5982\u6b64\uff0c\u56de\u6eaf\u7b97\u6cd5\u4ecd\u7136\u662f\u67d0\u4e9b\u641c\u7d22\u95ee\u9898\u548c\u7ea6\u675f\u6ee1\u8db3\u95ee\u9898\u7684\u6700\u4f73\u89e3\u51b3\u65b9\u6848\u3002\u5bf9\u4e8e\u8fd9\u4e9b\u95ee\u9898\uff0c\u7531\u4e8e\u65e0\u6cd5\u9884\u6d4b\u54ea\u4e9b\u9009\u62e9\u53ef\u751f\u6210\u6709\u6548\u7684\u89e3\uff0c\u56e0\u6b64\u6211\u4eec\u5fc5\u987b\u5bf9\u6240\u6709\u53ef\u80fd\u7684\u9009\u62e9\u8fdb\u884c\u904d\u5386\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5173\u952e\u662f\u5982\u4f55\u4f18\u5316\u6548\u7387\uff0c\u5e38\u89c1\u7684\u6548\u7387\u4f18\u5316\u65b9\u6cd5\u6709\u4e24\u79cd\u3002
\u56de\u6eaf\u7b97\u6cd5\u53ef\u7528\u4e8e\u89e3\u51b3\u8bb8\u591a\u641c\u7d22\u95ee\u9898\u3001\u7ea6\u675f\u6ee1\u8db3\u95ee\u9898\u548c\u7ec4\u5408\u4f18\u5316\u95ee\u9898\u3002
\u641c\u7d22\u95ee\u9898\uff1a\u8fd9\u7c7b\u95ee\u9898\u7684\u76ee\u6807\u662f\u627e\u5230\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u7684\u89e3\u51b3\u65b9\u6848\u3002
\u7ea6\u675f\u6ee1\u8db3\u95ee\u9898\uff1a\u8fd9\u7c7b\u95ee\u9898\u7684\u76ee\u6807\u662f\u627e\u5230\u6ee1\u8db3\u6240\u6709\u7ea6\u675f\u6761\u4ef6\u7684\u89e3\u3002
\u7ec4\u5408\u4f18\u5316\u95ee\u9898\uff1a\u8fd9\u7c7b\u95ee\u9898\u7684\u76ee\u6807\u662f\u5728\u4e00\u4e2a\u7ec4\u5408\u7a7a\u95f4\u4e2d\u627e\u5230\u6ee1\u8db3\u67d0\u4e9b\u6761\u4ef6\u7684\u6700\u4f18\u89e3\u3002
\u8bf7\u6ce8\u610f\uff0c\u5bf9\u4e8e\u8bb8\u591a\u7ec4\u5408\u4f18\u5316\u95ee\u9898\uff0c\u56de\u6eaf\u4e0d\u662f\u6700\u4f18\u89e3\u51b3\u65b9\u6848\u3002
Question
\u6839\u636e\u56fd\u9645\u8c61\u68cb\u7684\u89c4\u5219\uff0c\u7687\u540e\u53ef\u4ee5\u653b\u51fb\u4e0e\u540c\u5904\u4e00\u884c\u3001\u4e00\u5217\u6216\u4e00\u6761\u659c\u7ebf\u4e0a\u7684\u68cb\u5b50\u3002\u7ed9\u5b9a \\(n\\) \u4e2a\u7687\u540e\u548c\u4e00\u4e2a \\(n \\times n\\) \u5927\u5c0f\u7684\u68cb\u76d8\uff0c\u5bfb\u627e\u4f7f\u5f97\u6240\u6709\u7687\u540e\u4e4b\u95f4\u65e0\u6cd5\u76f8\u4e92\u653b\u51fb\u7684\u6446\u653e\u65b9\u6848\u3002
\u5982\u56fe 13-15 \u6240\u793a\uff0c\u5f53 \\(n = 4\\) \u65f6\uff0c\u5171\u53ef\u4ee5\u627e\u5230\u4e24\u4e2a\u89e3\u3002\u4ece\u56de\u6eaf\u7b97\u6cd5\u7684\u89d2\u5ea6\u770b\uff0c\\(n \\times n\\) \u5927\u5c0f\u7684\u68cb\u76d8\u5171\u6709 \\(n^2\\) \u4e2a\u683c\u5b50\uff0c\u7ed9\u51fa\u4e86\u6240\u6709\u7684\u9009\u62e9 choices
\u3002\u5728\u9010\u4e2a\u653e\u7f6e\u7687\u540e\u7684\u8fc7\u7a0b\u4e2d\uff0c\u68cb\u76d8\u72b6\u6001\u5728\u4e0d\u65ad\u5730\u53d8\u5316\uff0c\u6bcf\u4e2a\u65f6\u523b\u7684\u68cb\u76d8\u5c31\u662f\u72b6\u6001 state
\u3002
\u56fe 13-15 \u00a0 4 \u7687\u540e\u95ee\u9898\u7684\u89e3
\u56fe 13-16 \u5c55\u793a\u4e86\u672c\u9898\u7684\u4e09\u4e2a\u7ea6\u675f\u6761\u4ef6\uff1a\u591a\u4e2a\u7687\u540e\u4e0d\u80fd\u5728\u540c\u4e00\u884c\u3001\u540c\u4e00\u5217\u3001\u540c\u4e00\u5bf9\u89d2\u7ebf\u3002\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u5bf9\u89d2\u7ebf\u5206\u4e3a\u4e3b\u5bf9\u89d2\u7ebf \\
\u548c\u6b21\u5bf9\u89d2\u7ebf /
\u4e24\u79cd\u3002
\u56fe 13-16 \u00a0 n \u7687\u540e\u95ee\u9898\u7684\u7ea6\u675f\u6761\u4ef6
"},{"location":"chapter_backtracking/n_queens_problem/#1","title":"1. \u00a0 \u9010\u884c\u653e\u7f6e\u7b56\u7565","text":"\u7687\u540e\u7684\u6570\u91cf\u548c\u68cb\u76d8\u7684\u884c\u6570\u90fd\u4e3a \\(n\\) \uff0c\u56e0\u6b64\u6211\u4eec\u5bb9\u6613\u5f97\u5230\u4e00\u4e2a\u63a8\u8bba\uff1a\u68cb\u76d8\u6bcf\u884c\u90fd\u5141\u8bb8\u4e14\u53ea\u5141\u8bb8\u653e\u7f6e\u4e00\u4e2a\u7687\u540e\u3002
\u4e5f\u5c31\u662f\u8bf4\uff0c\u6211\u4eec\u53ef\u4ee5\u91c7\u53d6\u9010\u884c\u653e\u7f6e\u7b56\u7565\uff1a\u4ece\u7b2c\u4e00\u884c\u5f00\u59cb\uff0c\u5728\u6bcf\u884c\u653e\u7f6e\u4e00\u4e2a\u7687\u540e\uff0c\u76f4\u81f3\u6700\u540e\u4e00\u884c\u7ed3\u675f\u3002
\u56fe 13-17 \u6240\u793a\u4e3a \\(4\\) \u7687\u540e\u95ee\u9898\u7684\u9010\u884c\u653e\u7f6e\u8fc7\u7a0b\u3002\u53d7\u753b\u5e45\u9650\u5236\uff0c\u56fe 13-17 \u4ec5\u5c55\u5f00\u4e86\u7b2c\u4e00\u884c\u7684\u5176\u4e2d\u4e00\u4e2a\u641c\u7d22\u5206\u652f\uff0c\u5e76\u4e14\u5c06\u4e0d\u6ee1\u8db3\u5217\u7ea6\u675f\u548c\u5bf9\u89d2\u7ebf\u7ea6\u675f\u7684\u65b9\u6848\u90fd\u8fdb\u884c\u4e86\u526a\u679d\u3002
\u56fe 13-17 \u00a0 \u9010\u884c\u653e\u7f6e\u7b56\u7565
\u4ece\u672c\u8d28\u4e0a\u770b\uff0c\u9010\u884c\u653e\u7f6e\u7b56\u7565\u8d77\u5230\u4e86\u526a\u679d\u7684\u4f5c\u7528\uff0c\u5b83\u907f\u514d\u4e86\u540c\u4e00\u884c\u51fa\u73b0\u591a\u4e2a\u7687\u540e\u7684\u6240\u6709\u641c\u7d22\u5206\u652f\u3002
"},{"location":"chapter_backtracking/n_queens_problem/#2","title":"2. \u00a0 \u5217\u4e0e\u5bf9\u89d2\u7ebf\u526a\u679d","text":"\u4e3a\u4e86\u6ee1\u8db3\u5217\u7ea6\u675f\uff0c\u6211\u4eec\u53ef\u4ee5\u5229\u7528\u4e00\u4e2a\u957f\u5ea6\u4e3a \\(n\\) \u7684\u5e03\u5c14\u578b\u6570\u7ec4 cols
\u8bb0\u5f55\u6bcf\u4e00\u5217\u662f\u5426\u6709\u7687\u540e\u3002\u5728\u6bcf\u6b21\u51b3\u5b9a\u653e\u7f6e\u524d\uff0c\u6211\u4eec\u901a\u8fc7 cols
\u5c06\u5df2\u6709\u7687\u540e\u7684\u5217\u8fdb\u884c\u526a\u679d\uff0c\u5e76\u5728\u56de\u6eaf\u4e2d\u52a8\u6001\u66f4\u65b0 cols
\u7684\u72b6\u6001\u3002
\u90a3\u4e48\uff0c\u5982\u4f55\u5904\u7406\u5bf9\u89d2\u7ebf\u7ea6\u675f\u5462\uff1f\u8bbe\u68cb\u76d8\u4e2d\u67d0\u4e2a\u683c\u5b50\u7684\u884c\u5217\u7d22\u5f15\u4e3a \\((row, col)\\) \uff0c\u9009\u5b9a\u77e9\u9635\u4e2d\u7684\u67d0\u6761\u4e3b\u5bf9\u89d2\u7ebf\uff0c\u6211\u4eec\u53d1\u73b0\u8be5\u5bf9\u89d2\u7ebf\u4e0a\u6240\u6709\u683c\u5b50\u7684\u884c\u7d22\u5f15\u51cf\u5217\u7d22\u5f15\u90fd\u76f8\u7b49\uff0c\u5373\u5bf9\u89d2\u7ebf\u4e0a\u6240\u6709\u683c\u5b50\u7684 \\(row - col\\) \u4e3a\u6052\u5b9a\u503c\u3002
\u4e5f\u5c31\u662f\u8bf4\uff0c\u5982\u679c\u4e24\u4e2a\u683c\u5b50\u6ee1\u8db3 \\(row_1 - col_1 = row_2 - col_2\\) \uff0c\u5219\u5b83\u4eec\u4e00\u5b9a\u5904\u5728\u540c\u4e00\u6761\u4e3b\u5bf9\u89d2\u7ebf\u4e0a\u3002\u5229\u7528\u8be5\u89c4\u5f8b\uff0c\u6211\u4eec\u53ef\u4ee5\u501f\u52a9\u56fe 13-18 \u6240\u793a\u7684\u6570\u7ec4 diags1
\u8bb0\u5f55\u6bcf\u6761\u4e3b\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\u3002
\u540c\u7406\uff0c\u6b21\u5bf9\u89d2\u7ebf\u4e0a\u7684\u6240\u6709\u683c\u5b50\u7684 \\(row + col\\) \u662f\u6052\u5b9a\u503c\u3002\u6211\u4eec\u540c\u6837\u4e5f\u53ef\u4ee5\u501f\u52a9\u6570\u7ec4 diags2
\u6765\u5904\u7406\u6b21\u5bf9\u89d2\u7ebf\u7ea6\u675f\u3002
\u56fe 13-18 \u00a0 \u5904\u7406\u5217\u7ea6\u675f\u548c\u5bf9\u89d2\u7ebf\u7ea6\u675f
"},{"location":"chapter_backtracking/n_queens_problem/#3","title":"3. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"\u8bf7\u6ce8\u610f\uff0c\\(n\\) \u7ef4\u65b9\u9635\u4e2d \\(row - col\\) \u7684\u8303\u56f4\u662f \\([-n + 1, n - 1]\\) \uff0c\\(row + col\\) \u7684\u8303\u56f4\u662f \\([0, 2n - 2]\\) \uff0c\u6240\u4ee5\u4e3b\u5bf9\u89d2\u7ebf\u548c\u6b21\u5bf9\u89d2\u7ebf\u7684\u6570\u91cf\u90fd\u4e3a \\(2n - 1\\) \uff0c\u5373\u6570\u7ec4 diags1
\u548c diags2
\u7684\u957f\u5ea6\u90fd\u4e3a \\(2n - 1\\) \u3002
def backtrack(\n row: int,\n n: int,\n state: list[list[str]],\n res: list[list[list[str]]],\n cols: list[bool],\n diags1: list[bool],\n diags2: list[bool],\n):\n \"\"\"\u56de\u6eaf\u7b97\u6cd5\uff1aN \u7687\u540e\"\"\"\n # \u5f53\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u65f6\uff0c\u8bb0\u5f55\u89e3\n if row == n:\n res.append([list(row) for row in state])\n return\n # \u904d\u5386\u6240\u6709\u5217\n for col in range(n):\n # \u8ba1\u7b97\u8be5\u683c\u5b50\u5bf9\u5e94\u7684\u4e3b\u5bf9\u89d2\u7ebf\u548c\u526f\u5bf9\u89d2\u7ebf\n diag1 = row - col + n - 1\n diag2 = row + col\n # \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8be5\u683c\u5b50\u6240\u5728\u5217\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u526f\u5bf9\u89d2\u7ebf\u4e0a\u5b58\u5728\u7687\u540e\n if not cols[col] and not diags1[diag1] and not diags2[diag2]:\n # \u5c1d\u8bd5\uff1a\u5c06\u7687\u540e\u653e\u7f6e\u5728\u8be5\u683c\u5b50\n state[row][col] = \"Q\"\n cols[col] = diags1[diag1] = diags2[diag2] = True\n # \u653e\u7f6e\u4e0b\u4e00\u884c\n backtrack(row + 1, n, state, res, cols, diags1, diags2)\n # \u56de\u9000\uff1a\u5c06\u8be5\u683c\u5b50\u6062\u590d\u4e3a\u7a7a\u4f4d\n state[row][col] = \"#\"\n cols[col] = diags1[diag1] = diags2[diag2] = False\n\ndef n_queens(n: int) -> list[list[list[str]]]:\n \"\"\"\u6c42\u89e3 N \u7687\u540e\"\"\"\n # \u521d\u59cb\u5316 n*n \u5927\u5c0f\u7684\u68cb\u76d8\uff0c\u5176\u4e2d 'Q' \u4ee3\u8868\u7687\u540e\uff0c'#' \u4ee3\u8868\u7a7a\u4f4d\n state = [[\"#\" for _ in range(n)] for _ in range(n)]\n cols = [False] * n # \u8bb0\u5f55\u5217\u662f\u5426\u6709\u7687\u540e\n diags1 = [False] * (2 * n - 1) # \u8bb0\u5f55\u4e3b\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n diags2 = [False] * (2 * n - 1) # \u8bb0\u5f55\u526f\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n res = []\n backtrack(0, n, state, res, cols, diags1, diags2)\n\n return res\n
n_queens.cpp/* \u56de\u6eaf\u7b97\u6cd5\uff1aN \u7687\u540e */\nvoid backtrack(int row, int n, vector<vector<string>> &state, vector<vector<vector<string>>> &res, vector<bool> &cols,\n vector<bool> &diags1, vector<bool> &diags2) {\n // \u5f53\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u65f6\uff0c\u8bb0\u5f55\u89e3\n if (row == n) {\n res.push_back(state);\n return;\n }\n // \u904d\u5386\u6240\u6709\u5217\n for (int col = 0; col < n; col++) {\n // \u8ba1\u7b97\u8be5\u683c\u5b50\u5bf9\u5e94\u7684\u4e3b\u5bf9\u89d2\u7ebf\u548c\u526f\u5bf9\u89d2\u7ebf\n int diag1 = row - col + n - 1;\n int diag2 = row + col;\n // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8be5\u683c\u5b50\u6240\u5728\u5217\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u526f\u5bf9\u89d2\u7ebf\u4e0a\u5b58\u5728\u7687\u540e\n if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {\n // \u5c1d\u8bd5\uff1a\u5c06\u7687\u540e\u653e\u7f6e\u5728\u8be5\u683c\u5b50\n state[row][col] = \"Q\";\n cols[col] = diags1[diag1] = diags2[diag2] = true;\n // \u653e\u7f6e\u4e0b\u4e00\u884c\n backtrack(row + 1, n, state, res, cols, diags1, diags2);\n // \u56de\u9000\uff1a\u5c06\u8be5\u683c\u5b50\u6062\u590d\u4e3a\u7a7a\u4f4d\n state[row][col] = \"#\";\n cols[col] = diags1[diag1] = diags2[diag2] = false;\n }\n }\n}\n\n/* \u6c42\u89e3 N \u7687\u540e */\nvector<vector<vector<string>>> nQueens(int n) {\n // \u521d\u59cb\u5316 n*n \u5927\u5c0f\u7684\u68cb\u76d8\uff0c\u5176\u4e2d 'Q' \u4ee3\u8868\u7687\u540e\uff0c'#' \u4ee3\u8868\u7a7a\u4f4d\n vector<vector<string>> state(n, vector<string>(n, \"#\"));\n vector<bool> cols(n, false); // \u8bb0\u5f55\u5217\u662f\u5426\u6709\u7687\u540e\n vector<bool> diags1(2 * n - 1, false); // \u8bb0\u5f55\u4e3b\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n vector<bool> diags2(2 * n - 1, false); // \u8bb0\u5f55\u526f\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n vector<vector<vector<string>>> res;\n\n backtrack(0, n, state, res, cols, diags1, diags2);\n\n return res;\n}\n
n_queens.java/* \u56de\u6eaf\u7b97\u6cd5\uff1aN \u7687\u540e */\nvoid backtrack(int row, int n, List<List<String>> state, List<List<List<String>>> res,\n boolean[] cols, boolean[] diags1, boolean[] diags2) {\n // \u5f53\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u65f6\uff0c\u8bb0\u5f55\u89e3\n if (row == n) {\n List<List<String>> copyState = new ArrayList<>();\n for (List<String> sRow : state) {\n copyState.add(new ArrayList<>(sRow));\n }\n res.add(copyState);\n return;\n }\n // \u904d\u5386\u6240\u6709\u5217\n for (int col = 0; col < n; col++) {\n // \u8ba1\u7b97\u8be5\u683c\u5b50\u5bf9\u5e94\u7684\u4e3b\u5bf9\u89d2\u7ebf\u548c\u526f\u5bf9\u89d2\u7ebf\n int diag1 = row - col + n - 1;\n int diag2 = row + col;\n // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8be5\u683c\u5b50\u6240\u5728\u5217\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u526f\u5bf9\u89d2\u7ebf\u4e0a\u5b58\u5728\u7687\u540e\n if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {\n // \u5c1d\u8bd5\uff1a\u5c06\u7687\u540e\u653e\u7f6e\u5728\u8be5\u683c\u5b50\n state.get(row).set(col, \"Q\");\n cols[col] = diags1[diag1] = diags2[diag2] = true;\n // \u653e\u7f6e\u4e0b\u4e00\u884c\n backtrack(row + 1, n, state, res, cols, diags1, diags2);\n // \u56de\u9000\uff1a\u5c06\u8be5\u683c\u5b50\u6062\u590d\u4e3a\u7a7a\u4f4d\n state.get(row).set(col, \"#\");\n cols[col] = diags1[diag1] = diags2[diag2] = false;\n }\n }\n}\n\n/* \u6c42\u89e3 N \u7687\u540e */\nList<List<List<String>>> nQueens(int n) {\n // \u521d\u59cb\u5316 n*n \u5927\u5c0f\u7684\u68cb\u76d8\uff0c\u5176\u4e2d 'Q' \u4ee3\u8868\u7687\u540e\uff0c'#' \u4ee3\u8868\u7a7a\u4f4d\n List<List<String>> state = new ArrayList<>();\n for (int i = 0; i < n; i++) {\n List<String> row = new ArrayList<>();\n for (int j = 0; j < n; j++) {\n row.add(\"#\");\n }\n state.add(row);\n }\n boolean[] cols = new boolean[n]; // \u8bb0\u5f55\u5217\u662f\u5426\u6709\u7687\u540e\n boolean[] diags1 = new boolean[2 * n - 1]; // \u8bb0\u5f55\u4e3b\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n boolean[] diags2 = new boolean[2 * n - 1]; // \u8bb0\u5f55\u526f\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n List<List<List<String>>> res = new ArrayList<>();\n\n backtrack(0, n, state, res, cols, diags1, diags2);\n\n return res;\n}\n
n_queens.cs/* \u56de\u6eaf\u7b97\u6cd5\uff1aN \u7687\u540e */\nvoid Backtrack(int row, int n, List<List<string>> state, List<List<List<string>>> res,\n bool[] cols, bool[] diags1, bool[] diags2) {\n // \u5f53\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u65f6\uff0c\u8bb0\u5f55\u89e3\n if (row == n) {\n List<List<string>> copyState = [];\n foreach (List<string> sRow in state) {\n copyState.Add(new List<string>(sRow));\n }\n res.Add(copyState);\n return;\n }\n // \u904d\u5386\u6240\u6709\u5217\n for (int col = 0; col < n; col++) {\n // \u8ba1\u7b97\u8be5\u683c\u5b50\u5bf9\u5e94\u7684\u4e3b\u5bf9\u89d2\u7ebf\u548c\u526f\u5bf9\u89d2\u7ebf\n int diag1 = row - col + n - 1;\n int diag2 = row + col;\n // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8be5\u683c\u5b50\u6240\u5728\u5217\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u526f\u5bf9\u89d2\u7ebf\u4e0a\u5b58\u5728\u7687\u540e\n if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {\n // \u5c1d\u8bd5\uff1a\u5c06\u7687\u540e\u653e\u7f6e\u5728\u8be5\u683c\u5b50\n state[row][col] = \"Q\";\n cols[col] = diags1[diag1] = diags2[diag2] = true;\n // \u653e\u7f6e\u4e0b\u4e00\u884c\n Backtrack(row + 1, n, state, res, cols, diags1, diags2);\n // \u56de\u9000\uff1a\u5c06\u8be5\u683c\u5b50\u6062\u590d\u4e3a\u7a7a\u4f4d\n state[row][col] = \"#\";\n cols[col] = diags1[diag1] = diags2[diag2] = false;\n }\n }\n}\n\n/* \u6c42\u89e3 N \u7687\u540e */\nList<List<List<string>>> NQueens(int n) {\n // \u521d\u59cb\u5316 n*n \u5927\u5c0f\u7684\u68cb\u76d8\uff0c\u5176\u4e2d 'Q' \u4ee3\u8868\u7687\u540e\uff0c'#' \u4ee3\u8868\u7a7a\u4f4d\n List<List<string>> state = [];\n for (int i = 0; i < n; i++) {\n List<string> row = [];\n for (int j = 0; j < n; j++) {\n row.Add(\"#\");\n }\n state.Add(row);\n }\n bool[] cols = new bool[n]; // \u8bb0\u5f55\u5217\u662f\u5426\u6709\u7687\u540e\n bool[] diags1 = new bool[2 * n - 1]; // \u8bb0\u5f55\u4e3b\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n bool[] diags2 = new bool[2 * n - 1]; // \u8bb0\u5f55\u526f\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n List<List<List<string>>> res = [];\n\n Backtrack(0, n, state, res, cols, diags1, diags2);\n\n return res;\n}\n
n_queens.go/* \u56de\u6eaf\u7b97\u6cd5\uff1aN \u7687\u540e */\nfunc backtrack(row, n int, state *[][]string, res *[][][]string, cols, diags1, diags2 *[]bool) {\n // \u5f53\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u65f6\uff0c\u8bb0\u5f55\u89e3\n if row == n {\n newState := make([][]string, len(*state))\n for i, _ := range newState {\n newState[i] = make([]string, len((*state)[0]))\n copy(newState[i], (*state)[i])\n\n }\n *res = append(*res, newState)\n }\n // \u904d\u5386\u6240\u6709\u5217\n for col := 0; col < n; col++ {\n // \u8ba1\u7b97\u8be5\u683c\u5b50\u5bf9\u5e94\u7684\u4e3b\u5bf9\u89d2\u7ebf\u548c\u526f\u5bf9\u89d2\u7ebf\n diag1 := row - col + n - 1\n diag2 := row + col\n // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8be5\u683c\u5b50\u6240\u5728\u5217\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u526f\u5bf9\u89d2\u7ebf\u4e0a\u5b58\u5728\u7687\u540e\n if !(*cols)[col] && !(*diags1)[diag1] && !(*diags2)[diag2] {\n // \u5c1d\u8bd5\uff1a\u5c06\u7687\u540e\u653e\u7f6e\u5728\u8be5\u683c\u5b50\n (*state)[row][col] = \"Q\"\n (*cols)[col], (*diags1)[diag1], (*diags2)[diag2] = true, true, true\n // \u653e\u7f6e\u4e0b\u4e00\u884c\n backtrack(row+1, n, state, res, cols, diags1, diags2)\n // \u56de\u9000\uff1a\u5c06\u8be5\u683c\u5b50\u6062\u590d\u4e3a\u7a7a\u4f4d\n (*state)[row][col] = \"#\"\n (*cols)[col], (*diags1)[diag1], (*diags2)[diag2] = false, false, false\n }\n }\n}\n\n/* \u56de\u6eaf\u7b97\u6cd5\uff1aN \u7687\u540e */\nfunc backtrack(row, n int, state *[][]string, res *[][][]string, cols, diags1, diags2 *[]bool) {\n // \u5f53\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u65f6\uff0c\u8bb0\u5f55\u89e3\n if row == n {\n newState := make([][]string, len(*state))\n for i, _ := range newState {\n newState[i] = make([]string, len((*state)[0]))\n copy(newState[i], (*state)[i])\n\n }\n *res = append(*res, newState)\n }\n // \u904d\u5386\u6240\u6709\u5217\n for col := 0; col < n; col++ {\n // \u8ba1\u7b97\u8be5\u683c\u5b50\u5bf9\u5e94\u7684\u4e3b\u5bf9\u89d2\u7ebf\u548c\u526f\u5bf9\u89d2\u7ebf\n diag1 := row - col + n - 1\n diag2 := row + col\n // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8be5\u683c\u5b50\u6240\u5728\u5217\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u526f\u5bf9\u89d2\u7ebf\u4e0a\u5b58\u5728\u7687\u540e\n if !(*cols)[col] && !(*diags1)[diag1] && !(*diags2)[diag2] {\n // \u5c1d\u8bd5\uff1a\u5c06\u7687\u540e\u653e\u7f6e\u5728\u8be5\u683c\u5b50\n (*state)[row][col] = \"Q\"\n (*cols)[col], (*diags1)[diag1], (*diags2)[diag2] = true, true, true\n // \u653e\u7f6e\u4e0b\u4e00\u884c\n backtrack(row+1, n, state, res, cols, diags1, diags2)\n // \u56de\u9000\uff1a\u5c06\u8be5\u683c\u5b50\u6062\u590d\u4e3a\u7a7a\u4f4d\n (*state)[row][col] = \"#\"\n (*cols)[col], (*diags1)[diag1], (*diags2)[diag2] = false, false, false\n }\n }\n}\n\nfunc nQueens(n int) [][][]string {\n // \u521d\u59cb\u5316 n*n \u5927\u5c0f\u7684\u68cb\u76d8\uff0c\u5176\u4e2d 'Q' \u4ee3\u8868\u7687\u540e\uff0c'#' \u4ee3\u8868\u7a7a\u4f4d\n state := make([][]string, n)\n for i := 0; i < n; i++ {\n row := make([]string, n)\n for i := 0; i < n; i++ {\n row[i] = \"#\"\n }\n state[i] = row\n }\n // \u8bb0\u5f55\u5217\u662f\u5426\u6709\u7687\u540e\n cols := make([]bool, n)\n diags1 := make([]bool, 2*n-1)\n diags2 := make([]bool, 2*n-1)\n res := make([][][]string, 0)\n backtrack(0, n, &state, &res, &cols, &diags1, &diags2)\n return res\n}\n
n_queens.swift/* \u56de\u6eaf\u7b97\u6cd5\uff1aN \u7687\u540e */\nfunc backtrack(row: Int, n: Int, state: inout [[String]], res: inout [[[String]]], cols: inout [Bool], diags1: inout [Bool], diags2: inout [Bool]) {\n // \u5f53\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u65f6\uff0c\u8bb0\u5f55\u89e3\n if row == n {\n res.append(state)\n return\n }\n // \u904d\u5386\u6240\u6709\u5217\n for col in 0 ..< n {\n // \u8ba1\u7b97\u8be5\u683c\u5b50\u5bf9\u5e94\u7684\u4e3b\u5bf9\u89d2\u7ebf\u548c\u526f\u5bf9\u89d2\u7ebf\n let diag1 = row - col + n - 1\n let diag2 = row + col\n // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8be5\u683c\u5b50\u6240\u5728\u5217\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u526f\u5bf9\u89d2\u7ebf\u4e0a\u5b58\u5728\u7687\u540e\n if !cols[col] && !diags1[diag1] && !diags2[diag2] {\n // \u5c1d\u8bd5\uff1a\u5c06\u7687\u540e\u653e\u7f6e\u5728\u8be5\u683c\u5b50\n state[row][col] = \"Q\"\n cols[col] = true\n diags1[diag1] = true\n diags2[diag2] = true\n // \u653e\u7f6e\u4e0b\u4e00\u884c\n backtrack(row: row + 1, n: n, state: &state, res: &res, cols: &cols, diags1: &diags1, diags2: &diags2)\n // \u56de\u9000\uff1a\u5c06\u8be5\u683c\u5b50\u6062\u590d\u4e3a\u7a7a\u4f4d\n state[row][col] = \"#\"\n cols[col] = false\n diags1[diag1] = false\n diags2[diag2] = false\n }\n }\n}\n\n/* \u6c42\u89e3 N \u7687\u540e */\nfunc nQueens(n: Int) -> [[[String]]] {\n // \u521d\u59cb\u5316 n*n \u5927\u5c0f\u7684\u68cb\u76d8\uff0c\u5176\u4e2d 'Q' \u4ee3\u8868\u7687\u540e\uff0c'#' \u4ee3\u8868\u7a7a\u4f4d\n var state = Array(repeating: Array(repeating: \"#\", count: n), count: n)\n var cols = Array(repeating: false, count: n) // \u8bb0\u5f55\u5217\u662f\u5426\u6709\u7687\u540e\n var diags1 = Array(repeating: false, count: 2 * n - 1) // \u8bb0\u5f55\u4e3b\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n var diags2 = Array(repeating: false, count: 2 * n - 1) // \u8bb0\u5f55\u526f\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n var res: [[[String]]] = []\n\n backtrack(row: 0, n: n, state: &state, res: &res, cols: &cols, diags1: &diags1, diags2: &diags2)\n\n return res\n}\n
n_queens.js/* \u56de\u6eaf\u7b97\u6cd5\uff1aN \u7687\u540e */\nfunction backtrack(row, n, state, res, cols, diags1, diags2) {\n // \u5f53\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u65f6\uff0c\u8bb0\u5f55\u89e3\n if (row === n) {\n res.push(state.map((row) => row.slice()));\n return;\n }\n // \u904d\u5386\u6240\u6709\u5217\n for (let col = 0; col < n; col++) {\n // \u8ba1\u7b97\u8be5\u683c\u5b50\u5bf9\u5e94\u7684\u4e3b\u5bf9\u89d2\u7ebf\u548c\u526f\u5bf9\u89d2\u7ebf\n const diag1 = row - col + n - 1;\n const diag2 = row + col;\n // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8be5\u683c\u5b50\u6240\u5728\u5217\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u526f\u5bf9\u89d2\u7ebf\u4e0a\u5b58\u5728\u7687\u540e\n if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {\n // \u5c1d\u8bd5\uff1a\u5c06\u7687\u540e\u653e\u7f6e\u5728\u8be5\u683c\u5b50\n state[row][col] = 'Q';\n cols[col] = diags1[diag1] = diags2[diag2] = true;\n // \u653e\u7f6e\u4e0b\u4e00\u884c\n backtrack(row + 1, n, state, res, cols, diags1, diags2);\n // \u56de\u9000\uff1a\u5c06\u8be5\u683c\u5b50\u6062\u590d\u4e3a\u7a7a\u4f4d\n state[row][col] = '#';\n cols[col] = diags1[diag1] = diags2[diag2] = false;\n }\n }\n}\n\n/* \u6c42\u89e3 N \u7687\u540e */\nfunction nQueens(n) {\n // \u521d\u59cb\u5316 n*n \u5927\u5c0f\u7684\u68cb\u76d8\uff0c\u5176\u4e2d 'Q' \u4ee3\u8868\u7687\u540e\uff0c'#' \u4ee3\u8868\u7a7a\u4f4d\n const state = Array.from({ length: n }, () => Array(n).fill('#'));\n const cols = Array(n).fill(false); // \u8bb0\u5f55\u5217\u662f\u5426\u6709\u7687\u540e\n const diags1 = Array(2 * n - 1).fill(false); // \u8bb0\u5f55\u4e3b\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n const diags2 = Array(2 * n - 1).fill(false); // \u8bb0\u5f55\u526f\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n const res = [];\n\n backtrack(0, n, state, res, cols, diags1, diags2);\n return res;\n}\n
n_queens.ts/* \u56de\u6eaf\u7b97\u6cd5\uff1aN \u7687\u540e */\nfunction backtrack(\n row: number,\n n: number,\n state: string[][],\n res: string[][][],\n cols: boolean[],\n diags1: boolean[],\n diags2: boolean[]\n): void {\n // \u5f53\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u65f6\uff0c\u8bb0\u5f55\u89e3\n if (row === n) {\n res.push(state.map((row) => row.slice()));\n return;\n }\n // \u904d\u5386\u6240\u6709\u5217\n for (let col = 0; col < n; col++) {\n // \u8ba1\u7b97\u8be5\u683c\u5b50\u5bf9\u5e94\u7684\u4e3b\u5bf9\u89d2\u7ebf\u548c\u526f\u5bf9\u89d2\u7ebf\n const diag1 = row - col + n - 1;\n const diag2 = row + col;\n // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8be5\u683c\u5b50\u6240\u5728\u5217\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u526f\u5bf9\u89d2\u7ebf\u4e0a\u5b58\u5728\u7687\u540e\n if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {\n // \u5c1d\u8bd5\uff1a\u5c06\u7687\u540e\u653e\u7f6e\u5728\u8be5\u683c\u5b50\n state[row][col] = 'Q';\n cols[col] = diags1[diag1] = diags2[diag2] = true;\n // \u653e\u7f6e\u4e0b\u4e00\u884c\n backtrack(row + 1, n, state, res, cols, diags1, diags2);\n // \u56de\u9000\uff1a\u5c06\u8be5\u683c\u5b50\u6062\u590d\u4e3a\u7a7a\u4f4d\n state[row][col] = '#';\n cols[col] = diags1[diag1] = diags2[diag2] = false;\n }\n }\n}\n\n/* \u6c42\u89e3 N \u7687\u540e */\nfunction nQueens(n: number): string[][][] {\n // \u521d\u59cb\u5316 n*n \u5927\u5c0f\u7684\u68cb\u76d8\uff0c\u5176\u4e2d 'Q' \u4ee3\u8868\u7687\u540e\uff0c'#' \u4ee3\u8868\u7a7a\u4f4d\n const state = Array.from({ length: n }, () => Array(n).fill('#'));\n const cols = Array(n).fill(false); // \u8bb0\u5f55\u5217\u662f\u5426\u6709\u7687\u540e\n const diags1 = Array(2 * n - 1).fill(false); // \u8bb0\u5f55\u4e3b\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n const diags2 = Array(2 * n - 1).fill(false); // \u8bb0\u5f55\u526f\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n const res: string[][][] = [];\n\n backtrack(0, n, state, res, cols, diags1, diags2);\n return res;\n}\n
n_queens.dart/* \u56de\u6eaf\u7b97\u6cd5\uff1aN \u7687\u540e */\nvoid backtrack(\n int row,\n int n,\n List<List<String>> state,\n List<List<List<String>>> res,\n List<bool> cols,\n List<bool> diags1,\n List<bool> diags2,\n) {\n // \u5f53\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u65f6\uff0c\u8bb0\u5f55\u89e3\n if (row == n) {\n List<List<String>> copyState = [];\n for (List<String> sRow in state) {\n copyState.add(List.from(sRow));\n }\n res.add(copyState);\n return;\n }\n // \u904d\u5386\u6240\u6709\u5217\n for (int col = 0; col < n; col++) {\n // \u8ba1\u7b97\u8be5\u683c\u5b50\u5bf9\u5e94\u7684\u4e3b\u5bf9\u89d2\u7ebf\u548c\u526f\u5bf9\u89d2\u7ebf\n int diag1 = row - col + n - 1;\n int diag2 = row + col;\n // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8be5\u683c\u5b50\u6240\u5728\u5217\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u526f\u5bf9\u89d2\u7ebf\u4e0a\u5b58\u5728\u7687\u540e\n if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {\n // \u5c1d\u8bd5\uff1a\u5c06\u7687\u540e\u653e\u7f6e\u5728\u8be5\u683c\u5b50\n state[row][col] = \"Q\";\n cols[col] = true;\n diags1[diag1] = true;\n diags2[diag2] = true;\n // \u653e\u7f6e\u4e0b\u4e00\u884c\n backtrack(row + 1, n, state, res, cols, diags1, diags2);\n // \u56de\u9000\uff1a\u5c06\u8be5\u683c\u5b50\u6062\u590d\u4e3a\u7a7a\u4f4d\n state[row][col] = \"#\";\n cols[col] = false;\n diags1[diag1] = false;\n diags2[diag2] = false;\n }\n }\n}\n\n/* \u6c42\u89e3 N \u7687\u540e */\nList<List<List<String>>> nQueens(int n) {\n // \u521d\u59cb\u5316 n*n \u5927\u5c0f\u7684\u68cb\u76d8\uff0c\u5176\u4e2d 'Q' \u4ee3\u8868\u7687\u540e\uff0c'#' \u4ee3\u8868\u7a7a\u4f4d\n List<List<String>> state = List.generate(n, (index) => List.filled(n, \"#\"));\n List<bool> cols = List.filled(n, false); // \u8bb0\u5f55\u5217\u662f\u5426\u6709\u7687\u540e\n List<bool> diags1 = List.filled(2 * n - 1, false); // \u8bb0\u5f55\u4e3b\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n List<bool> diags2 = List.filled(2 * n - 1, false); // \u8bb0\u5f55\u526f\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n List<List<List<String>>> res = [];\n\n backtrack(0, n, state, res, cols, diags1, diags2);\n\n return res;\n}\n
n_queens.rs/* \u56de\u6eaf\u7b97\u6cd5\uff1aN \u7687\u540e */\nfn backtrack(row: usize, n: usize, state: &mut Vec<Vec<String>>, res: &mut Vec<Vec<Vec<String>>>,\n cols: &mut [bool], diags1: &mut [bool], diags2: &mut [bool]) {\n // \u5f53\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u65f6\uff0c\u8bb0\u5f55\u89e3\n if row == n {\n let mut copy_state: Vec<Vec<String>> = Vec::new();\n for s_row in state.clone() {\n copy_state.push(s_row);\n }\n res.push(copy_state);\n return;\n }\n // \u904d\u5386\u6240\u6709\u5217\n for col in 0..n {\n // \u8ba1\u7b97\u8be5\u683c\u5b50\u5bf9\u5e94\u7684\u4e3b\u5bf9\u89d2\u7ebf\u548c\u526f\u5bf9\u89d2\u7ebf\n let diag1 = row + n - 1 - col;\n let diag2 = row + col;\n // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8be5\u683c\u5b50\u6240\u5728\u5217\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u526f\u5bf9\u89d2\u7ebf\u4e0a\u5b58\u5728\u7687\u540e\n if !cols[col] && !diags1[diag1] && !diags2[diag2] {\n // \u5c1d\u8bd5\uff1a\u5c06\u7687\u540e\u653e\u7f6e\u5728\u8be5\u683c\u5b50\n state.get_mut(row).unwrap()[col] = \"Q\".into();\n (cols[col], diags1[diag1], diags2[diag2]) = (true, true, true);\n // \u653e\u7f6e\u4e0b\u4e00\u884c\n backtrack(row + 1, n, state, res, cols, diags1, diags2);\n // \u56de\u9000\uff1a\u5c06\u8be5\u683c\u5b50\u6062\u590d\u4e3a\u7a7a\u4f4d\n state.get_mut(row).unwrap()[col] = \"#\".into();\n (cols[col], diags1[diag1], diags2[diag2]) = (false, false, false);\n }\n }\n}\n\n/* \u6c42\u89e3 N \u7687\u540e */\nfn n_queens(n: usize) -> Vec<Vec<Vec<String>>> {\n // \u521d\u59cb\u5316 n*n \u5927\u5c0f\u7684\u68cb\u76d8\uff0c\u5176\u4e2d 'Q' \u4ee3\u8868\u7687\u540e\uff0c'#' \u4ee3\u8868\u7a7a\u4f4d\n let mut state: Vec<Vec<String>> = Vec::new();\n for _ in 0..n {\n let mut row: Vec<String> = Vec::new();\n for _ in 0..n {\n row.push(\"#\".into());\n }\n state.push(row);\n }\n let mut cols = vec![false; n]; // \u8bb0\u5f55\u5217\u662f\u5426\u6709\u7687\u540e\n let mut diags1 = vec![false; 2 * n - 1]; // \u8bb0\u5f55\u4e3b\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n let mut diags2 = vec![false; 2 * n - 1]; // \u8bb0\u5f55\u526f\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n let mut res: Vec<Vec<Vec<String>>> = Vec::new();\n\n backtrack(0, n, &mut state, &mut res, &mut cols, &mut diags1, &mut diags2);\n\n res\n}\n
n_queens.c/* \u56de\u6eaf\u7b97\u6cd5\uff1aN \u7687\u540e */\nvoid backtrack(int row, int n, char state[MAX_SIZE][MAX_SIZE], char ***res, int *resSize, bool cols[MAX_SIZE],\n bool diags1[2 * MAX_SIZE - 1], bool diags2[2 * MAX_SIZE - 1]) {\n // \u5f53\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u65f6\uff0c\u8bb0\u5f55\u89e3\n if (row == n) {\n res[*resSize] = (char **)malloc(sizeof(char *) * n);\n for (int i = 0; i < n; ++i) {\n res[*resSize][i] = (char *)malloc(sizeof(char) * (n + 1));\n strcpy(res[*resSize][i], state[i]);\n }\n (*resSize)++;\n return;\n }\n // \u904d\u5386\u6240\u6709\u5217\n for (int col = 0; col < n; col++) {\n // \u8ba1\u7b97\u8be5\u683c\u5b50\u5bf9\u5e94\u7684\u4e3b\u5bf9\u89d2\u7ebf\u548c\u526f\u5bf9\u89d2\u7ebf\n int diag1 = row - col + n - 1;\n int diag2 = row + col;\n // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8be5\u683c\u5b50\u6240\u5728\u5217\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u526f\u5bf9\u89d2\u7ebf\u4e0a\u5b58\u5728\u7687\u540e\n if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {\n // \u5c1d\u8bd5\uff1a\u5c06\u7687\u540e\u653e\u7f6e\u5728\u8be5\u683c\u5b50\n state[row][col] = 'Q';\n cols[col] = diags1[diag1] = diags2[diag2] = true;\n // \u653e\u7f6e\u4e0b\u4e00\u884c\n backtrack(row + 1, n, state, res, resSize, cols, diags1, diags2);\n // \u56de\u9000\uff1a\u5c06\u8be5\u683c\u5b50\u6062\u590d\u4e3a\u7a7a\u4f4d\n state[row][col] = '#';\n cols[col] = diags1[diag1] = diags2[diag2] = false;\n }\n }\n}\n\n/* \u6c42\u89e3 N \u7687\u540e */\nchar ***nQueens(int n, int *returnSize) {\n char state[MAX_SIZE][MAX_SIZE];\n // \u521d\u59cb\u5316 n*n \u5927\u5c0f\u7684\u68cb\u76d8\uff0c\u5176\u4e2d 'Q' \u4ee3\u8868\u7687\u540e\uff0c'#' \u4ee3\u8868\u7a7a\u4f4d\n for (int i = 0; i < n; ++i) {\n for (int j = 0; j < n; ++j) {\n state[i][j] = '#';\n }\n state[i][n] = '\\0';\n }\n bool cols[MAX_SIZE] = {false}; // \u8bb0\u5f55\u5217\u662f\u5426\u6709\u7687\u540e\n bool diags1[2 * MAX_SIZE - 1] = {false}; // \u8bb0\u5f55\u4e3b\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n bool diags2[2 * MAX_SIZE - 1] = {false}; // \u8bb0\u5f55\u526f\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n\n char ***res = (char ***)malloc(sizeof(char **) * MAX_SIZE);\n *returnSize = 0;\n backtrack(0, n, state, res, returnSize, cols, diags1, diags2);\n return res;\n}\n
n_queens.zig[class]{}-[func]{backtrack}\n\n[class]{}-[func]{nQueens}\n
\u9010\u884c\u653e\u7f6e \\(n\\) \u6b21\uff0c\u8003\u8651\u5217\u7ea6\u675f\uff0c\u5219\u4ece\u7b2c\u4e00\u884c\u5230\u6700\u540e\u4e00\u884c\u5206\u522b\u6709 \\(n\\)\u3001\\(n-1\\)\u3001\\(\\dots\\)\u3001\\(2\\)\u3001\\(1\\) \u4e2a\u9009\u62e9\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n!)\\) \u3002\u5b9e\u9645\u4e0a\uff0c\u6839\u636e\u5bf9\u89d2\u7ebf\u7ea6\u675f\u7684\u526a\u679d\u4e5f\u80fd\u591f\u5927\u5e45\u7f29\u5c0f\u641c\u7d22\u7a7a\u95f4\uff0c\u56e0\u800c\u641c\u7d22\u6548\u7387\u5f80\u5f80\u4f18\u4e8e\u4ee5\u4e0a\u65f6\u95f4\u590d\u6742\u5ea6\u3002
\u6570\u7ec4 state
\u4f7f\u7528 \\(O(n^2)\\) \u7a7a\u95f4\uff0c\u6570\u7ec4 cols
\u3001diags1
\u548c diags2
\u7686\u4f7f\u7528 \\(O(n)\\) \u7a7a\u95f4\u3002\u6700\u5927\u9012\u5f52\u6df1\u5ea6\u4e3a \\(n\\) \uff0c\u4f7f\u7528 \\(O(n)\\) \u6808\u5e27\u7a7a\u95f4\u3002\u56e0\u6b64\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\) \u3002
\u5168\u6392\u5217\u95ee\u9898\u662f\u56de\u6eaf\u7b97\u6cd5\u7684\u4e00\u4e2a\u5178\u578b\u5e94\u7528\u3002\u5b83\u7684\u5b9a\u4e49\u662f\u5728\u7ed9\u5b9a\u4e00\u4e2a\u96c6\u5408\uff08\u5982\u4e00\u4e2a\u6570\u7ec4\u6216\u5b57\u7b26\u4e32\uff09\u7684\u60c5\u51b5\u4e0b\uff0c\u627e\u51fa\u5176\u4e2d\u5143\u7d20\u7684\u6240\u6709\u53ef\u80fd\u7684\u6392\u5217\u3002
\u8868 13-2 \u5217\u4e3e\u4e86\u51e0\u4e2a\u793a\u4f8b\u6570\u636e\uff0c\u5305\u62ec\u8f93\u5165\u6570\u7ec4\u548c\u5bf9\u5e94\u7684\u6240\u6709\u6392\u5217\u3002
\u8868 13-2 \u00a0 \u5168\u6392\u5217\u793a\u4f8b
\u8f93\u5165\u6570\u7ec4 \u6240\u6709\u6392\u5217 \\([1]\\) \\([1]\\) \\([1, 2]\\) \\([1, 2], [2, 1]\\) \\([1, 2, 3]\\) \\([1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]\\)"},{"location":"chapter_backtracking/permutations_problem/#1321","title":"13.2.1 \u00a0 \u65e0\u76f8\u7b49\u5143\u7d20\u7684\u60c5\u51b5","text":"Question
\u8f93\u5165\u4e00\u4e2a\u6574\u6570\u6570\u7ec4\uff0c\u5176\u4e2d\u4e0d\u5305\u542b\u91cd\u590d\u5143\u7d20\uff0c\u8fd4\u56de\u6240\u6709\u53ef\u80fd\u7684\u6392\u5217\u3002
\u4ece\u56de\u6eaf\u7b97\u6cd5\u7684\u89d2\u5ea6\u770b\uff0c\u6211\u4eec\u53ef\u4ee5\u628a\u751f\u6210\u6392\u5217\u7684\u8fc7\u7a0b\u60f3\u8c61\u6210\u4e00\u7cfb\u5217\u9009\u62e9\u7684\u7ed3\u679c\u3002\u5047\u8bbe\u8f93\u5165\u6570\u7ec4\u4e3a \\([1, 2, 3]\\) \uff0c\u5982\u679c\u6211\u4eec\u5148\u9009\u62e9 \\(1\\) \uff0c\u518d\u9009\u62e9 \\(3\\) \uff0c\u6700\u540e\u9009\u62e9 \\(2\\) \uff0c\u5219\u83b7\u5f97\u6392\u5217 \\([1, 3, 2]\\) \u3002\u56de\u9000\u8868\u793a\u64a4\u9500\u4e00\u4e2a\u9009\u62e9\uff0c\u4e4b\u540e\u7ee7\u7eed\u5c1d\u8bd5\u5176\u4ed6\u9009\u62e9\u3002
\u4ece\u56de\u6eaf\u4ee3\u7801\u7684\u89d2\u5ea6\u770b\uff0c\u5019\u9009\u96c6\u5408 choices
\u662f\u8f93\u5165\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\uff0c\u72b6\u6001 state
\u662f\u76f4\u81f3\u76ee\u524d\u5df2\u88ab\u9009\u62e9\u7684\u5143\u7d20\u3002\u8bf7\u6ce8\u610f\uff0c\u6bcf\u4e2a\u5143\u7d20\u53ea\u5141\u8bb8\u88ab\u9009\u62e9\u4e00\u6b21\uff0c\u56e0\u6b64 state
\u4e2d\u7684\u6240\u6709\u5143\u7d20\u90fd\u5e94\u8be5\u662f\u552f\u4e00\u7684\u3002
\u5982\u56fe 13-5 \u6240\u793a\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u641c\u7d22\u8fc7\u7a0b\u5c55\u5f00\u6210\u4e00\u68f5\u9012\u5f52\u6811\uff0c\u6811\u4e2d\u7684\u6bcf\u4e2a\u8282\u70b9\u4ee3\u8868\u5f53\u524d\u72b6\u6001 state
\u3002\u4ece\u6839\u8282\u70b9\u5f00\u59cb\uff0c\u7ecf\u8fc7\u4e09\u8f6e\u9009\u62e9\u540e\u5230\u8fbe\u53f6\u8282\u70b9\uff0c\u6bcf\u4e2a\u53f6\u8282\u70b9\u90fd\u5bf9\u5e94\u4e00\u4e2a\u6392\u5217\u3002
\u56fe 13-5 \u00a0 \u5168\u6392\u5217\u7684\u9012\u5f52\u6811
"},{"location":"chapter_backtracking/permutations_problem/#1","title":"1. \u00a0 \u91cd\u590d\u9009\u62e9\u526a\u679d","text":"\u4e3a\u4e86\u5b9e\u73b0\u6bcf\u4e2a\u5143\u7d20\u53ea\u88ab\u9009\u62e9\u4e00\u6b21\uff0c\u6211\u4eec\u8003\u8651\u5f15\u5165\u4e00\u4e2a\u5e03\u5c14\u578b\u6570\u7ec4 selected
\uff0c\u5176\u4e2d selected[i]
\u8868\u793a choices[i]
\u662f\u5426\u5df2\u88ab\u9009\u62e9\uff0c\u5e76\u57fa\u4e8e\u5b83\u5b9e\u73b0\u4ee5\u4e0b\u526a\u679d\u64cd\u4f5c\u3002
choice[i]
\u540e\uff0c\u6211\u4eec\u5c31\u5c06 selected[i]
\u8d4b\u503c\u4e3a \\(\\text{True}\\) \uff0c\u4ee3\u8868\u5b83\u5df2\u88ab\u9009\u62e9\u3002choices
\u65f6\uff0c\u8df3\u8fc7\u6240\u6709\u5df2\u88ab\u9009\u62e9\u7684\u8282\u70b9\uff0c\u5373\u526a\u679d\u3002\u5982\u56fe 13-6 \u6240\u793a\uff0c\u5047\u8bbe\u6211\u4eec\u7b2c\u4e00\u8f6e\u9009\u62e9 1 \uff0c\u7b2c\u4e8c\u8f6e\u9009\u62e9 3 \uff0c\u7b2c\u4e09\u8f6e\u9009\u62e9 2 \uff0c\u5219\u9700\u8981\u5728\u7b2c\u4e8c\u8f6e\u526a\u6389\u5143\u7d20 1 \u7684\u5206\u652f\uff0c\u5728\u7b2c\u4e09\u8f6e\u526a\u6389\u5143\u7d20 1 \u548c\u5143\u7d20 3 \u7684\u5206\u652f\u3002
\u56fe 13-6 \u00a0 \u5168\u6392\u5217\u526a\u679d\u793a\u4f8b
\u89c2\u5bdf\u56fe 13-6 \u53d1\u73b0\uff0c\u8be5\u526a\u679d\u64cd\u4f5c\u5c06\u641c\u7d22\u7a7a\u95f4\u5927\u5c0f\u4ece \\(O(n^n)\\) \u51cf\u5c0f\u81f3 \\(O(n!)\\) \u3002
"},{"location":"chapter_backtracking/permutations_problem/#2","title":"2. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"\u60f3\u6e05\u695a\u4ee5\u4e0a\u4fe1\u606f\u4e4b\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5728\u6846\u67b6\u4ee3\u7801\u4e2d\u505a\u201c\u5b8c\u5f62\u586b\u7a7a\u201d\u4e86\u3002\u4e3a\u4e86\u7f29\u77ed\u6574\u4f53\u4ee3\u7801\uff0c\u6211\u4eec\u4e0d\u5355\u72ec\u5b9e\u73b0\u6846\u67b6\u4ee3\u7801\u4e2d\u7684\u5404\u4e2a\u51fd\u6570\uff0c\u800c\u662f\u5c06\u5b83\u4eec\u5c55\u5f00\u5728 backtrack()
\u51fd\u6570\u4e2d\uff1a
def backtrack(\n state: list[int], choices: list[int], selected: list[bool], res: list[list[int]]\n):\n \"\"\"\u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 I\"\"\"\n # \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n if len(state) == len(choices):\n res.append(list(state))\n return\n # \u904d\u5386\u6240\u6709\u9009\u62e9\n for i, choice in enumerate(choices):\n # \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20\n if not selected[i]:\n # \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n selected[i] = True\n state.append(choice)\n # \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state, choices, selected, res)\n # \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n selected[i] = False\n state.pop()\n\ndef permutations_i(nums: list[int]) -> list[list[int]]:\n \"\"\"\u5168\u6392\u5217 I\"\"\"\n res = []\n backtrack(state=[], choices=nums, selected=[False] * len(nums), res=res)\n return res\n
permutations_i.cpp/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 I */\nvoid backtrack(vector<int> &state, const vector<int> &choices, vector<bool> &selected, vector<vector<int>> &res) {\n // \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n if (state.size() == choices.size()) {\n res.push_back(state);\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for (int i = 0; i < choices.size(); i++) {\n int choice = choices[i];\n // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20\n if (!selected[i]) {\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n selected[i] = true;\n state.push_back(choice);\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state, choices, selected, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n selected[i] = false;\n state.pop_back();\n }\n }\n}\n\n/* \u5168\u6392\u5217 I */\nvector<vector<int>> permutationsI(vector<int> nums) {\n vector<int> state;\n vector<bool> selected(nums.size(), false);\n vector<vector<int>> res;\n backtrack(state, nums, selected, res);\n return res;\n}\n
permutations_i.java/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 I */\nvoid backtrack(List<Integer> state, int[] choices, boolean[] selected, List<List<Integer>> res) {\n // \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n if (state.size() == choices.length) {\n res.add(new ArrayList<Integer>(state));\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for (int i = 0; i < choices.length; i++) {\n int choice = choices[i];\n // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20\n if (!selected[i]) {\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n selected[i] = true;\n state.add(choice);\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state, choices, selected, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n selected[i] = false;\n state.remove(state.size() - 1);\n }\n }\n}\n\n/* \u5168\u6392\u5217 I */\nList<List<Integer>> permutationsI(int[] nums) {\n List<List<Integer>> res = new ArrayList<List<Integer>>();\n backtrack(new ArrayList<Integer>(), nums, new boolean[nums.length], res);\n return res;\n}\n
permutations_i.cs/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 I */\nvoid Backtrack(List<int> state, int[] choices, bool[] selected, List<List<int>> res) {\n // \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n if (state.Count == choices.Length) {\n res.Add(new List<int>(state));\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for (int i = 0; i < choices.Length; i++) {\n int choice = choices[i];\n // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20\n if (!selected[i]) {\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n selected[i] = true;\n state.Add(choice);\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n Backtrack(state, choices, selected, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n selected[i] = false;\n state.RemoveAt(state.Count - 1);\n }\n }\n}\n\n/* \u5168\u6392\u5217 I */\nList<List<int>> PermutationsI(int[] nums) {\n List<List<int>> res = [];\n Backtrack([], nums, new bool[nums.Length], res);\n return res;\n}\n
permutations_i.go/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 I */\nfunc backtrackI(state *[]int, choices *[]int, selected *[]bool, res *[][]int) {\n // \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n if len(*state) == len(*choices) {\n newState := append([]int{}, *state...)\n *res = append(*res, newState)\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for i := 0; i < len(*choices); i++ {\n choice := (*choices)[i]\n // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20\n if !(*selected)[i] {\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n (*selected)[i] = true\n *state = append(*state, choice)\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrackI(state, choices, selected, res)\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n (*selected)[i] = false\n *state = (*state)[:len(*state)-1]\n }\n }\n}\n\n/* \u5168\u6392\u5217 I */\nfunc permutationsI(nums []int) [][]int {\n res := make([][]int, 0)\n state := make([]int, 0)\n selected := make([]bool, len(nums))\n backtrackI(&state, &nums, &selected, &res)\n return res\n}\n
permutations_i.swift/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 I */\nfunc backtrack(state: inout [Int], choices: [Int], selected: inout [Bool], res: inout [[Int]]) {\n // \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n if state.count == choices.count {\n res.append(state)\n return\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for (i, choice) in choices.enumerated() {\n // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20\n if !selected[i] {\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n selected[i] = true\n state.append(choice)\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state: &state, choices: choices, selected: &selected, res: &res)\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n selected[i] = false\n state.removeLast()\n }\n }\n}\n\n/* \u5168\u6392\u5217 I */\nfunc permutationsI(nums: [Int]) -> [[Int]] {\n var state: [Int] = []\n var selected = Array(repeating: false, count: nums.count)\n var res: [[Int]] = []\n backtrack(state: &state, choices: nums, selected: &selected, res: &res)\n return res\n}\n
permutations_i.js/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 I */\nfunction backtrack(state, choices, selected, res) {\n // \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n if (state.length === choices.length) {\n res.push([...state]);\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n choices.forEach((choice, i) => {\n // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20\n if (!selected[i]) {\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n selected[i] = true;\n state.push(choice);\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state, choices, selected, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n selected[i] = false;\n state.pop();\n }\n });\n}\n\n/* \u5168\u6392\u5217 I */\nfunction permutationsI(nums) {\n const res = [];\n backtrack([], nums, Array(nums.length).fill(false), res);\n return res;\n}\n
permutations_i.ts/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 I */\nfunction backtrack(\n state: number[],\n choices: number[],\n selected: boolean[],\n res: number[][]\n): void {\n // \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n if (state.length === choices.length) {\n res.push([...state]);\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n choices.forEach((choice, i) => {\n // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20\n if (!selected[i]) {\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n selected[i] = true;\n state.push(choice);\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state, choices, selected, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n selected[i] = false;\n state.pop();\n }\n });\n}\n\n/* \u5168\u6392\u5217 I */\nfunction permutationsI(nums: number[]): number[][] {\n const res: number[][] = [];\n backtrack([], nums, Array(nums.length).fill(false), res);\n return res;\n}\n
permutations_i.dart/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 I */\nvoid backtrack(\n List<int> state,\n List<int> choices,\n List<bool> selected,\n List<List<int>> res,\n) {\n // \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n if (state.length == choices.length) {\n res.add(List.from(state));\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for (int i = 0; i < choices.length; i++) {\n int choice = choices[i];\n // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20\n if (!selected[i]) {\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n selected[i] = true;\n state.add(choice);\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state, choices, selected, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n selected[i] = false;\n state.removeLast();\n }\n }\n}\n\n/* \u5168\u6392\u5217 I */\nList<List<int>> permutationsI(List<int> nums) {\n List<List<int>> res = [];\n backtrack([], nums, List.filled(nums.length, false), res);\n return res;\n}\n
permutations_i.rs/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 I */\nfn backtrack(mut state: Vec<i32>, choices: &[i32], selected: &mut [bool], res: &mut Vec<Vec<i32>>) {\n // \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n if state.len() == choices.len() {\n res.push(state);\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for i in 0..choices.len() {\n let choice = choices[i];\n // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20\n if !selected[i] {\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n selected[i] = true;\n state.push(choice);\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state.clone(), choices, selected, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n selected[i] = false;\n state.remove(state.len() - 1);\n }\n }\n}\n\n/* \u5168\u6392\u5217 I */\nfn permutations_i(nums: &mut [i32]) -> Vec<Vec<i32>> {\n let mut res = Vec::new(); // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n backtrack(Vec::new(), nums, &mut vec![false; nums.len()], &mut res);\n res\n}\n
permutations_i.c/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 I */\nvoid backtrack(int *state, int stateSize, int *choices, int choicesSize, bool *selected, int **res, int *resSize) {\n // \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n if (stateSize == choicesSize) {\n res[*resSize] = (int *)malloc(choicesSize * sizeof(int));\n for (int i = 0; i < choicesSize; i++) {\n res[*resSize][i] = state[i];\n }\n (*resSize)++;\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for (int i = 0; i < choicesSize; i++) {\n int choice = choices[i];\n // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20\n if (!selected[i]) {\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n selected[i] = true;\n state[stateSize] = choice;\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state, stateSize + 1, choices, choicesSize, selected, res, resSize);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n selected[i] = false;\n }\n }\n}\n\n/* \u5168\u6392\u5217 I */\nint **permutationsI(int *nums, int numsSize, int *returnSize) {\n int *state = (int *)malloc(numsSize * sizeof(int));\n bool *selected = (bool *)malloc(numsSize * sizeof(bool));\n for (int i = 0; i < numsSize; i++) {\n selected[i] = false;\n }\n int **res = (int **)malloc(MAX_SIZE * sizeof(int *));\n *returnSize = 0;\n\n backtrack(state, 0, nums, numsSize, selected, res, returnSize);\n\n free(state);\n free(selected);\n\n return res;\n}\n
permutations_i.zig[class]{}-[func]{backtrack}\n\n[class]{}-[func]{permutationsI}\n
"},{"location":"chapter_backtracking/permutations_problem/#1322","title":"13.2.2 \u00a0 \u8003\u8651\u76f8\u7b49\u5143\u7d20\u7684\u60c5\u51b5","text":"Question
\u8f93\u5165\u4e00\u4e2a\u6574\u6570\u6570\u7ec4\uff0c\u6570\u7ec4\u4e2d\u53ef\u80fd\u5305\u542b\u91cd\u590d\u5143\u7d20\uff0c\u8fd4\u56de\u6240\u6709\u4e0d\u91cd\u590d\u7684\u6392\u5217\u3002
\u5047\u8bbe\u8f93\u5165\u6570\u7ec4\u4e3a \\([1, 1, 2]\\) \u3002\u4e3a\u4e86\u65b9\u4fbf\u533a\u5206\u4e24\u4e2a\u91cd\u590d\u5143\u7d20 \\(1\\) \uff0c\u6211\u4eec\u5c06\u7b2c\u4e8c\u4e2a \\(1\\) \u8bb0\u4e3a \\(\\hat{1}\\) \u3002
\u5982\u56fe 13-7 \u6240\u793a\uff0c\u4e0a\u8ff0\u65b9\u6cd5\u751f\u6210\u7684\u6392\u5217\u6709\u4e00\u534a\u662f\u91cd\u590d\u7684\u3002
\u56fe 13-7 \u00a0 \u91cd\u590d\u6392\u5217
\u90a3\u4e48\u5982\u4f55\u53bb\u9664\u91cd\u590d\u7684\u6392\u5217\u5462\uff1f\u6700\u76f4\u63a5\u5730\uff0c\u8003\u8651\u501f\u52a9\u4e00\u4e2a\u54c8\u5e0c\u8868\uff0c\u76f4\u63a5\u5bf9\u6392\u5217\u7ed3\u679c\u8fdb\u884c\u53bb\u91cd\u3002\u7136\u800c\u8fd9\u6837\u505a\u4e0d\u591f\u4f18\u96c5\uff0c\u56e0\u4e3a\u751f\u6210\u91cd\u590d\u6392\u5217\u7684\u641c\u7d22\u5206\u652f\u6ca1\u6709\u5fc5\u8981\uff0c\u5e94\u5f53\u63d0\u524d\u8bc6\u522b\u5e76\u526a\u679d\uff0c\u8fd9\u6837\u53ef\u4ee5\u8fdb\u4e00\u6b65\u63d0\u5347\u7b97\u6cd5\u6548\u7387\u3002
"},{"location":"chapter_backtracking/permutations_problem/#1_1","title":"1. \u00a0 \u76f8\u7b49\u5143\u7d20\u526a\u679d","text":"\u89c2\u5bdf\u56fe 13-8 \uff0c\u5728\u7b2c\u4e00\u8f6e\u4e2d\uff0c\u9009\u62e9 \\(1\\) \u6216\u9009\u62e9 \\(\\hat{1}\\) \u662f\u7b49\u4ef7\u7684\uff0c\u5728\u8fd9\u4e24\u4e2a\u9009\u62e9\u4e4b\u4e0b\u751f\u6210\u7684\u6240\u6709\u6392\u5217\u90fd\u662f\u91cd\u590d\u7684\u3002\u56e0\u6b64\u5e94\u8be5\u628a \\(\\hat{1}\\) \u526a\u679d\u3002
\u540c\u7406\uff0c\u5728\u7b2c\u4e00\u8f6e\u9009\u62e9 \\(2\\) \u4e4b\u540e\uff0c\u7b2c\u4e8c\u8f6e\u9009\u62e9\u4e2d\u7684 \\(1\\) \u548c \\(\\hat{1}\\) \u4e5f\u4f1a\u4ea7\u751f\u91cd\u590d\u5206\u652f\uff0c\u56e0\u6b64\u4e5f\u5e94\u5c06\u7b2c\u4e8c\u8f6e\u7684 \\(\\hat{1}\\) \u526a\u679d\u3002
\u4ece\u672c\u8d28\u4e0a\u770b\uff0c\u6211\u4eec\u7684\u76ee\u6807\u662f\u5728\u67d0\u4e00\u8f6e\u9009\u62e9\u4e2d\uff0c\u4fdd\u8bc1\u591a\u4e2a\u76f8\u7b49\u7684\u5143\u7d20\u4ec5\u88ab\u9009\u62e9\u4e00\u6b21\u3002
\u56fe 13-8 \u00a0 \u91cd\u590d\u6392\u5217\u526a\u679d
"},{"location":"chapter_backtracking/permutations_problem/#2_1","title":"2. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"\u5728\u4e0a\u4e00\u9898\u7684\u4ee3\u7801\u7684\u57fa\u7840\u4e0a\uff0c\u6211\u4eec\u8003\u8651\u5728\u6bcf\u4e00\u8f6e\u9009\u62e9\u4e2d\u5f00\u542f\u4e00\u4e2a\u54c8\u5e0c\u8868 duplicated
\uff0c\u7528\u4e8e\u8bb0\u5f55\u8be5\u8f6e\u4e2d\u5df2\u7ecf\u5c1d\u8bd5\u8fc7\u7684\u5143\u7d20\uff0c\u5e76\u5c06\u91cd\u590d\u5143\u7d20\u526a\u679d\uff1a
def backtrack(\n state: list[int], choices: list[int], selected: list[bool], res: list[list[int]]\n):\n \"\"\"\u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 II\"\"\"\n # \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n if len(state) == len(choices):\n res.append(list(state))\n return\n # \u904d\u5386\u6240\u6709\u9009\u62e9\n duplicated = set[int]()\n for i, choice in enumerate(choices):\n # \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20 \u4e14 \u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u76f8\u7b49\u5143\u7d20\n if not selected[i] and choice not in duplicated:\n # \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n duplicated.add(choice) # \u8bb0\u5f55\u9009\u62e9\u8fc7\u7684\u5143\u7d20\u503c\n selected[i] = True\n state.append(choice)\n # \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state, choices, selected, res)\n # \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n selected[i] = False\n state.pop()\n\ndef permutations_ii(nums: list[int]) -> list[list[int]]:\n \"\"\"\u5168\u6392\u5217 II\"\"\"\n res = []\n backtrack(state=[], choices=nums, selected=[False] * len(nums), res=res)\n return res\n
permutations_ii.cpp/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 II */\nvoid backtrack(vector<int> &state, const vector<int> &choices, vector<bool> &selected, vector<vector<int>> &res) {\n // \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n if (state.size() == choices.size()) {\n res.push_back(state);\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n unordered_set<int> duplicated;\n for (int i = 0; i < choices.size(); i++) {\n int choice = choices[i];\n // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20 \u4e14 \u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u76f8\u7b49\u5143\u7d20\n if (!selected[i] && duplicated.find(choice) == duplicated.end()) {\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n duplicated.emplace(choice); // \u8bb0\u5f55\u9009\u62e9\u8fc7\u7684\u5143\u7d20\u503c\n selected[i] = true;\n state.push_back(choice);\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state, choices, selected, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n selected[i] = false;\n state.pop_back();\n }\n }\n}\n\n/* \u5168\u6392\u5217 II */\nvector<vector<int>> permutationsII(vector<int> nums) {\n vector<int> state;\n vector<bool> selected(nums.size(), false);\n vector<vector<int>> res;\n backtrack(state, nums, selected, res);\n return res;\n}\n
permutations_ii.java/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 II */\nvoid backtrack(List<Integer> state, int[] choices, boolean[] selected, List<List<Integer>> res) {\n // \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n if (state.size() == choices.length) {\n res.add(new ArrayList<Integer>(state));\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n Set<Integer> duplicated = new HashSet<Integer>();\n for (int i = 0; i < choices.length; i++) {\n int choice = choices[i];\n // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20 \u4e14 \u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u76f8\u7b49\u5143\u7d20\n if (!selected[i] && !duplicated.contains(choice)) {\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n duplicated.add(choice); // \u8bb0\u5f55\u9009\u62e9\u8fc7\u7684\u5143\u7d20\u503c\n selected[i] = true;\n state.add(choice);\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state, choices, selected, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n selected[i] = false;\n state.remove(state.size() - 1);\n }\n }\n}\n\n/* \u5168\u6392\u5217 II */\nList<List<Integer>> permutationsII(int[] nums) {\n List<List<Integer>> res = new ArrayList<List<Integer>>();\n backtrack(new ArrayList<Integer>(), nums, new boolean[nums.length], res);\n return res;\n}\n
permutations_ii.cs/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 II */\nvoid Backtrack(List<int> state, int[] choices, bool[] selected, List<List<int>> res) {\n // \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n if (state.Count == choices.Length) {\n res.Add(new List<int>(state));\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n HashSet<int> duplicated = [];\n for (int i = 0; i < choices.Length; i++) {\n int choice = choices[i];\n // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20 \u4e14 \u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u76f8\u7b49\u5143\u7d20\n if (!selected[i] && !duplicated.Contains(choice)) {\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n duplicated.Add(choice); // \u8bb0\u5f55\u9009\u62e9\u8fc7\u7684\u5143\u7d20\u503c\n selected[i] = true;\n state.Add(choice);\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n Backtrack(state, choices, selected, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n selected[i] = false;\n state.RemoveAt(state.Count - 1);\n }\n }\n}\n\n/* \u5168\u6392\u5217 II */\nList<List<int>> PermutationsII(int[] nums) {\n List<List<int>> res = [];\n Backtrack([], nums, new bool[nums.Length], res);\n return res;\n}\n
permutations_ii.go/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 II */\nfunc backtrackII(state *[]int, choices *[]int, selected *[]bool, res *[][]int) {\n // \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n if len(*state) == len(*choices) {\n newState := append([]int{}, *state...)\n *res = append(*res, newState)\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n duplicated := make(map[int]struct{}, 0)\n for i := 0; i < len(*choices); i++ {\n choice := (*choices)[i]\n // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20 \u4e14 \u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u76f8\u7b49\u5143\u7d20\n if _, ok := duplicated[choice]; !ok && !(*selected)[i] {\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n // \u8bb0\u5f55\u9009\u62e9\u8fc7\u7684\u5143\u7d20\u503c\n duplicated[choice] = struct{}{}\n (*selected)[i] = true\n *state = append(*state, choice)\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrackI(state, choices, selected, res)\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n (*selected)[i] = false\n *state = (*state)[:len(*state)-1]\n }\n }\n}\n\n/* \u5168\u6392\u5217 II */\nfunc permutationsII(nums []int) [][]int {\n res := make([][]int, 0)\n state := make([]int, 0)\n selected := make([]bool, len(nums))\n backtrackII(&state, &nums, &selected, &res)\n return res\n}\n
permutations_ii.swift/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 II */\nfunc backtrack(state: inout [Int], choices: [Int], selected: inout [Bool], res: inout [[Int]]) {\n // \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n if state.count == choices.count {\n res.append(state)\n return\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n var duplicated: Set<Int> = []\n for (i, choice) in choices.enumerated() {\n // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20 \u4e14 \u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u76f8\u7b49\u5143\u7d20\n if !selected[i], !duplicated.contains(choice) {\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n duplicated.insert(choice) // \u8bb0\u5f55\u9009\u62e9\u8fc7\u7684\u5143\u7d20\u503c\n selected[i] = true\n state.append(choice)\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state: &state, choices: choices, selected: &selected, res: &res)\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n selected[i] = false\n state.removeLast()\n }\n }\n}\n\n/* \u5168\u6392\u5217 II */\nfunc permutationsII(nums: [Int]) -> [[Int]] {\n var state: [Int] = []\n var selected = Array(repeating: false, count: nums.count)\n var res: [[Int]] = []\n backtrack(state: &state, choices: nums, selected: &selected, res: &res)\n return res\n}\n
permutations_ii.js/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 II */\nfunction backtrack(state, choices, selected, res) {\n // \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n if (state.length === choices.length) {\n res.push([...state]);\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n const duplicated = new Set();\n choices.forEach((choice, i) => {\n // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20 \u4e14 \u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u76f8\u7b49\u5143\u7d20\n if (!selected[i] && !duplicated.has(choice)) {\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n duplicated.add(choice); // \u8bb0\u5f55\u9009\u62e9\u8fc7\u7684\u5143\u7d20\u503c\n selected[i] = true;\n state.push(choice);\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state, choices, selected, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n selected[i] = false;\n state.pop();\n }\n });\n}\n\n/* \u5168\u6392\u5217 II */\nfunction permutationsII(nums) {\n const res = [];\n backtrack([], nums, Array(nums.length).fill(false), res);\n return res;\n}\n
permutations_ii.ts/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 II */\nfunction backtrack(\n state: number[],\n choices: number[],\n selected: boolean[],\n res: number[][]\n): void {\n // \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n if (state.length === choices.length) {\n res.push([...state]);\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n const duplicated = new Set();\n choices.forEach((choice, i) => {\n // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20 \u4e14 \u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u76f8\u7b49\u5143\u7d20\n if (!selected[i] && !duplicated.has(choice)) {\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n duplicated.add(choice); // \u8bb0\u5f55\u9009\u62e9\u8fc7\u7684\u5143\u7d20\u503c\n selected[i] = true;\n state.push(choice);\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state, choices, selected, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n selected[i] = false;\n state.pop();\n }\n });\n}\n\n/* \u5168\u6392\u5217 II */\nfunction permutationsII(nums: number[]): number[][] {\n const res: number[][] = [];\n backtrack([], nums, Array(nums.length).fill(false), res);\n return res;\n}\n
permutations_ii.dart/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 II */\nvoid backtrack(\n List<int> state,\n List<int> choices,\n List<bool> selected,\n List<List<int>> res,\n) {\n // \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n if (state.length == choices.length) {\n res.add(List.from(state));\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n Set<int> duplicated = {};\n for (int i = 0; i < choices.length; i++) {\n int choice = choices[i];\n // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20 \u4e14 \u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u76f8\u7b49\u5143\u7d20\n if (!selected[i] && !duplicated.contains(choice)) {\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n duplicated.add(choice); // \u8bb0\u5f55\u9009\u62e9\u8fc7\u7684\u5143\u7d20\u503c\n selected[i] = true;\n state.add(choice);\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state, choices, selected, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n selected[i] = false;\n state.removeLast();\n }\n }\n}\n\n/* \u5168\u6392\u5217 II */\nList<List<int>> permutationsII(List<int> nums) {\n List<List<int>> res = [];\n backtrack([], nums, List.filled(nums.length, false), res);\n return res;\n}\n
permutations_ii.rs/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 II */\nfn backtrack(mut state: Vec<i32>, choices: &[i32], selected: &mut [bool], res: &mut Vec<Vec<i32>>) {\n // \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n if state.len() == choices.len() {\n res.push(state);\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n let mut duplicated = HashSet::<i32>::new();\n for i in 0..choices.len() {\n let choice = choices[i];\n // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20 \u4e14 \u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u76f8\u7b49\u5143\u7d20\n if !selected[i] && !duplicated.contains(&choice) {\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n duplicated.insert(choice); // \u8bb0\u5f55\u9009\u62e9\u8fc7\u7684\u5143\u7d20\u503c\n selected[i] = true;\n state.push(choice);\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state.clone(), choices, selected, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n selected[i] = false;\n state.remove(state.len() - 1);\n }\n }\n}\n\n/* \u5168\u6392\u5217 II */\nfn permutations_ii(nums: &mut [i32]) -> Vec<Vec<i32>> {\n let mut res = Vec::new();\n backtrack(Vec::new(), nums, &mut vec![false; nums.len()], &mut res);\n res\n}\n
permutations_ii.c/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 II */\nvoid backtrack(int *state, int stateSize, int *choices, int choicesSize, bool *selected, int **res, int *resSize) {\n // \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n if (stateSize == choicesSize) {\n res[*resSize] = (int *)malloc(choicesSize * sizeof(int));\n for (int i = 0; i < choicesSize; i++) {\n res[*resSize][i] = state[i];\n }\n (*resSize)++;\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n bool duplicated[MAX_SIZE] = {false};\n for (int i = 0; i < choicesSize; i++) {\n int choice = choices[i];\n // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20 \u4e14 \u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u76f8\u7b49\u5143\u7d20\n if (!selected[i] && !duplicated[choice]) {\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n duplicated[choice] = true; // \u8bb0\u5f55\u9009\u62e9\u8fc7\u7684\u5143\u7d20\u503c\n selected[i] = true;\n state[stateSize] = choice;\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state, stateSize + 1, choices, choicesSize, selected, res, resSize);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n selected[i] = false;\n }\n }\n}\n\n/* \u5168\u6392\u5217 II */\nint **permutationsII(int *nums, int numsSize, int *returnSize) {\n int *state = (int *)malloc(numsSize * sizeof(int));\n bool *selected = (bool *)malloc(numsSize * sizeof(bool));\n for (int i = 0; i < numsSize; i++) {\n selected[i] = false;\n }\n int **res = (int **)malloc(MAX_SIZE * sizeof(int *));\n *returnSize = 0;\n\n backtrack(state, 0, nums, numsSize, selected, res, returnSize);\n\n free(state);\n free(selected);\n\n return res;\n}\n
permutations_ii.zig[class]{}-[func]{backtrack}\n\n[class]{}-[func]{permutationsII}\n
\u5047\u8bbe\u5143\u7d20\u4e24\u4e24\u4e4b\u95f4\u4e92\u4e0d\u76f8\u540c\uff0c\u5219 \\(n\\) \u4e2a\u5143\u7d20\u5171\u6709 \\(n!\\) \u79cd\u6392\u5217\uff08\u9636\u4e58\uff09\uff1b\u5728\u8bb0\u5f55\u7ed3\u679c\u65f6\uff0c\u9700\u8981\u590d\u5236\u957f\u5ea6\u4e3a \\(n\\) \u7684\u5217\u8868\uff0c\u4f7f\u7528 \\(O(n)\\) \u65f6\u95f4\u3002\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n!n)\\) \u3002
\u6700\u5927\u9012\u5f52\u6df1\u5ea6\u4e3a \\(n\\) \uff0c\u4f7f\u7528 \\(O(n)\\) \u6808\u5e27\u7a7a\u95f4\u3002selected
\u4f7f\u7528 \\(O(n)\\) \u7a7a\u95f4\u3002\u540c\u4e00\u65f6\u523b\u6700\u591a\u5171\u6709 \\(n\\) \u4e2a duplicated
\uff0c\u4f7f\u7528 \\(O(n^2)\\) \u7a7a\u95f4\u3002\u56e0\u6b64\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\) \u3002
\u8bf7\u6ce8\u610f\uff0c\u867d\u7136 selected
\u548c duplicated
\u90fd\u7528\u4e8e\u526a\u679d\uff0c\u4f46\u4e24\u8005\u7684\u76ee\u6807\u4e0d\u540c\u3002
selected
\u3002\u5b83\u8bb0\u5f55\u7684\u662f\u5f53\u524d\u72b6\u6001\u4e2d\u5305\u542b\u54ea\u4e9b\u5143\u7d20\uff0c\u5176\u4f5c\u7528\u662f\u9632\u6b62 choices
\u4e2d\u7684\u4efb\u4e00\u5143\u7d20\u5728 state
\u4e2d\u91cd\u590d\u51fa\u73b0\u3002backtrack
\u51fd\u6570\uff09\u90fd\u5305\u542b\u4e00\u4e2a duplicated
\u3002\u5b83\u8bb0\u5f55\u7684\u662f\u5728\u672c\u8f6e\u904d\u5386\uff08for
\u5faa\u73af\uff09\u4e2d\u54ea\u4e9b\u5143\u7d20\u5df2\u88ab\u9009\u62e9\u8fc7\uff0c\u5176\u4f5c\u7528\u662f\u4fdd\u8bc1\u76f8\u7b49\u7684\u5143\u7d20\u53ea\u88ab\u9009\u62e9\u4e00\u6b21\u3002\u56fe 13-9 \u5c55\u793a\u4e86\u4e24\u4e2a\u526a\u679d\u6761\u4ef6\u7684\u751f\u6548\u8303\u56f4\u3002\u6ce8\u610f\uff0c\u6811\u4e2d\u7684\u6bcf\u4e2a\u8282\u70b9\u4ee3\u8868\u4e00\u4e2a\u9009\u62e9\uff0c\u4ece\u6839\u8282\u70b9\u5230\u53f6\u8282\u70b9\u7684\u8def\u5f84\u4e0a\u7684\u5404\u4e2a\u8282\u70b9\u6784\u6210\u4e00\u4e2a\u6392\u5217\u3002
\u56fe 13-9 \u00a0 \u4e24\u79cd\u526a\u679d\u6761\u4ef6\u7684\u4f5c\u7528\u8303\u56f4
"},{"location":"chapter_backtracking/subset_sum_problem/","title":"13.3 \u00a0 \u5b50\u96c6\u548c\u95ee\u9898","text":""},{"location":"chapter_backtracking/subset_sum_problem/#1331","title":"13.3.1 \u00a0 \u65e0\u91cd\u590d\u5143\u7d20\u7684\u60c5\u51b5","text":"Question
\u7ed9\u5b9a\u4e00\u4e2a\u6b63\u6574\u6570\u6570\u7ec4 nums
\u548c\u4e00\u4e2a\u76ee\u6807\u6b63\u6574\u6570 target
\uff0c\u8bf7\u627e\u51fa\u6240\u6709\u53ef\u80fd\u7684\u7ec4\u5408\uff0c\u4f7f\u5f97\u7ec4\u5408\u4e2d\u7684\u5143\u7d20\u548c\u7b49\u4e8e target
\u3002\u7ed9\u5b9a\u6570\u7ec4\u65e0\u91cd\u590d\u5143\u7d20\uff0c\u6bcf\u4e2a\u5143\u7d20\u53ef\u4ee5\u88ab\u9009\u53d6\u591a\u6b21\u3002\u8bf7\u4ee5\u5217\u8868\u5f62\u5f0f\u8fd4\u56de\u8fd9\u4e9b\u7ec4\u5408\uff0c\u5217\u8868\u4e2d\u4e0d\u5e94\u5305\u542b\u91cd\u590d\u7ec4\u5408\u3002
\u4f8b\u5982\uff0c\u8f93\u5165\u96c6\u5408 \\(\\{3, 4, 5\\}\\) \u548c\u76ee\u6807\u6574\u6570 \\(9\\) \uff0c\u89e3\u4e3a \\(\\{3, 3, 3\\}, \\{4, 5\\}\\) \u3002\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u4e24\u70b9\u3002
\u7c7b\u4f3c\u4e8e\u5168\u6392\u5217\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u628a\u5b50\u96c6\u7684\u751f\u6210\u8fc7\u7a0b\u60f3\u8c61\u6210\u4e00\u7cfb\u5217\u9009\u62e9\u7684\u7ed3\u679c\uff0c\u5e76\u5728\u9009\u62e9\u8fc7\u7a0b\u4e2d\u5b9e\u65f6\u66f4\u65b0\u201c\u5143\u7d20\u548c\u201d\uff0c\u5f53\u5143\u7d20\u548c\u7b49\u4e8e target
\u65f6\uff0c\u5c31\u5c06\u5b50\u96c6\u8bb0\u5f55\u81f3\u7ed3\u679c\u5217\u8868\u3002
\u800c\u4e0e\u5168\u6392\u5217\u95ee\u9898\u4e0d\u540c\u7684\u662f\uff0c\u672c\u9898\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u53ef\u4ee5\u88ab\u65e0\u9650\u6b21\u9009\u53d6\uff0c\u56e0\u6b64\u65e0\u987b\u501f\u52a9 selected
\u5e03\u5c14\u5217\u8868\u6765\u8bb0\u5f55\u5143\u7d20\u662f\u5426\u5df2\u88ab\u9009\u62e9\u3002\u6211\u4eec\u53ef\u4ee5\u5bf9\u5168\u6392\u5217\u4ee3\u7801\u8fdb\u884c\u5c0f\u5e45\u4fee\u6539\uff0c\u521d\u6b65\u5f97\u5230\u89e3\u9898\u4ee3\u7801\uff1a
def backtrack(\n state: list[int],\n target: int,\n total: int,\n choices: list[int],\n res: list[list[int]],\n):\n \"\"\"\u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I\"\"\"\n # \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n if total == target:\n res.append(list(state))\n return\n # \u904d\u5386\u6240\u6709\u9009\u62e9\n for i in range(len(choices)):\n # \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u8df3\u8fc7\u8be5\u9009\u62e9\n if total + choices[i] > target:\n continue\n # \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\n state.append(choices[i])\n # \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state, target, total + choices[i], choices, res)\n # \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n state.pop()\n\ndef subset_sum_i_naive(nums: list[int], target: int) -> list[list[int]]:\n \"\"\"\u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u590d\u5b50\u96c6\uff09\"\"\"\n state = [] # \u72b6\u6001\uff08\u5b50\u96c6\uff09\n total = 0 # \u5b50\u96c6\u548c\n res = [] # \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n backtrack(state, target, total, nums, res)\n return res\n
subset_sum_i_naive.cpp/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nvoid backtrack(vector<int> &state, int target, int total, vector<int> &choices, vector<vector<int>> &res) {\n // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n if (total == target) {\n res.push_back(state);\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for (size_t i = 0; i < choices.size(); i++) {\n // \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u8df3\u8fc7\u8be5\u9009\u62e9\n if (total + choices[i] > target) {\n continue;\n }\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\n state.push_back(choices[i]);\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state, target, total + choices[i], choices, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n state.pop_back();\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u590d\u5b50\u96c6\uff09 */\nvector<vector<int>> subsetSumINaive(vector<int> &nums, int target) {\n vector<int> state; // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n int total = 0; // \u5b50\u96c6\u548c\n vector<vector<int>> res; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n backtrack(state, target, total, nums, res);\n return res;\n}\n
subset_sum_i_naive.java/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nvoid backtrack(List<Integer> state, int target, int total, int[] choices, List<List<Integer>> res) {\n // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n if (total == target) {\n res.add(new ArrayList<>(state));\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for (int i = 0; i < choices.length; i++) {\n // \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u8df3\u8fc7\u8be5\u9009\u62e9\n if (total + choices[i] > target) {\n continue;\n }\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\n state.add(choices[i]);\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state, target, total + choices[i], choices, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n state.remove(state.size() - 1);\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u590d\u5b50\u96c6\uff09 */\nList<List<Integer>> subsetSumINaive(int[] nums, int target) {\n List<Integer> state = new ArrayList<>(); // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n int total = 0; // \u5b50\u96c6\u548c\n List<List<Integer>> res = new ArrayList<>(); // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n backtrack(state, target, total, nums, res);\n return res;\n}\n
subset_sum_i_naive.cs/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nvoid Backtrack(List<int> state, int target, int total, int[] choices, List<List<int>> res) {\n // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n if (total == target) {\n res.Add(new List<int>(state));\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for (int i = 0; i < choices.Length; i++) {\n // \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u8df3\u8fc7\u8be5\u9009\u62e9\n if (total + choices[i] > target) {\n continue;\n }\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\n state.Add(choices[i]);\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n Backtrack(state, target, total + choices[i], choices, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n state.RemoveAt(state.Count - 1);\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u590d\u5b50\u96c6\uff09 */\nList<List<int>> SubsetSumINaive(int[] nums, int target) {\n List<int> state = []; // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n int total = 0; // \u5b50\u96c6\u548c\n List<List<int>> res = []; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n Backtrack(state, target, total, nums, res);\n return res;\n}\n
subset_sum_i_naive.go/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nfunc backtrackSubsetSumINaive(total, target int, state, choices *[]int, res *[][]int) {\n // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n if target == total {\n newState := append([]int{}, *state...)\n *res = append(*res, newState)\n return\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for i := 0; i < len(*choices); i++ {\n // \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u8df3\u8fc7\u8be5\u9009\u62e9\n if total+(*choices)[i] > target {\n continue\n }\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\n *state = append(*state, (*choices)[i])\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrackSubsetSumINaive(total+(*choices)[i], target, state, choices, res)\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n *state = (*state)[:len(*state)-1]\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u590d\u5b50\u96c6\uff09 */\nfunc subsetSumINaive(nums []int, target int) [][]int {\n state := make([]int, 0) // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n total := 0 // \u5b50\u96c6\u548c\n res := make([][]int, 0) // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n backtrackSubsetSumINaive(total, target, &state, &nums, &res)\n return res\n}\n
subset_sum_i_naive.swift/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nfunc backtrack(state: inout [Int], target: Int, total: Int, choices: [Int], res: inout [[Int]]) {\n // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n if total == target {\n res.append(state)\n return\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for i in stride(from: 0, to: choices.count, by: 1) {\n // \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u8df3\u8fc7\u8be5\u9009\u62e9\n if total + choices[i] > target {\n continue\n }\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\n state.append(choices[i])\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state: &state, target: target, total: total + choices[i], choices: choices, res: &res)\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n state.removeLast()\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u590d\u5b50\u96c6\uff09 */\nfunc subsetSumINaive(nums: [Int], target: Int) -> [[Int]] {\n var state: [Int] = [] // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n let total = 0 // \u5b50\u96c6\u548c\n var res: [[Int]] = [] // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n backtrack(state: &state, target: target, total: total, choices: nums, res: &res)\n return res\n}\n
subset_sum_i_naive.js/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nfunction backtrack(state, target, total, choices, res) {\n // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n if (total === target) {\n res.push([...state]);\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for (let i = 0; i < choices.length; i++) {\n // \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u8df3\u8fc7\u8be5\u9009\u62e9\n if (total + choices[i] > target) {\n continue;\n }\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\n state.push(choices[i]);\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state, target, total + choices[i], choices, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n state.pop();\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u590d\u5b50\u96c6\uff09 */\nfunction subsetSumINaive(nums, target) {\n const state = []; // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n const total = 0; // \u5b50\u96c6\u548c\n const res = []; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n backtrack(state, target, total, nums, res);\n return res;\n}\n
subset_sum_i_naive.ts/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nfunction backtrack(\n state: number[],\n target: number,\n total: number,\n choices: number[],\n res: number[][]\n): void {\n // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n if (total === target) {\n res.push([...state]);\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for (let i = 0; i < choices.length; i++) {\n // \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u8df3\u8fc7\u8be5\u9009\u62e9\n if (total + choices[i] > target) {\n continue;\n }\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\n state.push(choices[i]);\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state, target, total + choices[i], choices, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n state.pop();\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u590d\u5b50\u96c6\uff09 */\nfunction subsetSumINaive(nums: number[], target: number): number[][] {\n const state = []; // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n const total = 0; // \u5b50\u96c6\u548c\n const res = []; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n backtrack(state, target, total, nums, res);\n return res;\n}\n
subset_sum_i_naive.dart/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nvoid backtrack(\n List<int> state,\n int target,\n int total,\n List<int> choices,\n List<List<int>> res,\n) {\n // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n if (total == target) {\n res.add(List.from(state));\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for (int i = 0; i < choices.length; i++) {\n // \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u8df3\u8fc7\u8be5\u9009\u62e9\n if (total + choices[i] > target) {\n continue;\n }\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\n state.add(choices[i]);\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state, target, total + choices[i], choices, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n state.removeLast();\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u590d\u5b50\u96c6\uff09 */\nList<List<int>> subsetSumINaive(List<int> nums, int target) {\n List<int> state = []; // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n int total = 0; // \u5143\u7d20\u548c\n List<List<int>> res = []; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n backtrack(state, target, total, nums, res);\n return res;\n}\n
subset_sum_i_naive.rs/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nfn backtrack(mut state: Vec<i32>, target: i32, total: i32, choices: &[i32], res: &mut Vec<Vec<i32>>) {\n // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n if total == target {\n res.push(state);\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for i in 0..choices.len() {\n // \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u8df3\u8fc7\u8be5\u9009\u62e9\n if total + choices[i] > target {\n continue;\n }\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\n state.push(choices[i]);\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state.clone(), target, total + choices[i], choices, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n state.pop();\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u590d\u5b50\u96c6\uff09 */\nfn subset_sum_i_naive(nums: &[i32], target: i32) -> Vec<Vec<i32>> {\n let state = Vec::new(); // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n let total = 0; // \u5b50\u96c6\u548c\n let mut res = Vec::new(); // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n backtrack(state, target, total, nums, &mut res);\n res\n}\n
subset_sum_i_naive.c/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nvoid backtrack(int target, int total, int *choices, int choicesSize) {\n // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n if (total == target) {\n for (int i = 0; i < stateSize; i++) {\n res[resSize][i] = state[i];\n }\n resColSizes[resSize++] = stateSize;\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for (int i = 0; i < choicesSize; i++) {\n // \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u8df3\u8fc7\u8be5\u9009\u62e9\n if (total + choices[i] > target) {\n continue;\n }\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\n state[stateSize++] = choices[i];\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(target, total + choices[i], choices, choicesSize);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n stateSize--;\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u590d\u5b50\u96c6\uff09 */\nvoid subsetSumINaive(int *nums, int numsSize, int target) {\n resSize = 0; // \u521d\u59cb\u5316\u89e3\u7684\u6570\u91cf\u4e3a0\n backtrack(target, 0, nums, numsSize);\n}\n
subset_sum_i_naive.zig[class]{}-[func]{backtrack}\n\n[class]{}-[func]{subsetSumINaive}\n
\u5411\u4ee5\u4e0a\u4ee3\u7801\u8f93\u5165\u6570\u7ec4 \\([3, 4, 5]\\) \u548c\u76ee\u6807\u5143\u7d20 \\(9\\) \uff0c\u8f93\u51fa\u7ed3\u679c\u4e3a \\([3, 3, 3], [4, 5], [5, 4]\\) \u3002\u867d\u7136\u6210\u529f\u627e\u51fa\u4e86\u6240\u6709\u548c\u4e3a \\(9\\) \u7684\u5b50\u96c6\uff0c\u4f46\u5176\u4e2d\u5b58\u5728\u91cd\u590d\u7684\u5b50\u96c6 \\([4, 5]\\) \u548c \\([5, 4]\\) \u3002
\u8fd9\u662f\u56e0\u4e3a\u641c\u7d22\u8fc7\u7a0b\u662f\u533a\u5206\u9009\u62e9\u987a\u5e8f\u7684\uff0c\u7136\u800c\u5b50\u96c6\u4e0d\u533a\u5206\u9009\u62e9\u987a\u5e8f\u3002\u5982\u56fe 13-10 \u6240\u793a\uff0c\u5148\u9009 \\(4\\) \u540e\u9009 \\(5\\) \u4e0e\u5148\u9009 \\(5\\) \u540e\u9009 \\(4\\) \u662f\u4e0d\u540c\u7684\u5206\u652f\uff0c\u4f46\u5bf9\u5e94\u540c\u4e00\u4e2a\u5b50\u96c6\u3002
\u56fe 13-10 \u00a0 \u5b50\u96c6\u641c\u7d22\u4e0e\u8d8a\u754c\u526a\u679d
\u4e3a\u4e86\u53bb\u9664\u91cd\u590d\u5b50\u96c6\uff0c\u4e00\u79cd\u76f4\u63a5\u7684\u601d\u8def\u662f\u5bf9\u7ed3\u679c\u5217\u8868\u8fdb\u884c\u53bb\u91cd\u3002\u4f46\u8fd9\u4e2a\u65b9\u6cd5\u6548\u7387\u5f88\u4f4e\uff0c\u6709\u4e24\u65b9\u9762\u539f\u56e0\u3002
target
\u8f83\u5927\u65f6\uff0c\u641c\u7d22\u8fc7\u7a0b\u4f1a\u4ea7\u751f\u5927\u91cf\u7684\u91cd\u590d\u5b50\u96c6\u3002\u6211\u4eec\u8003\u8651\u5728\u641c\u7d22\u8fc7\u7a0b\u4e2d\u901a\u8fc7\u526a\u679d\u8fdb\u884c\u53bb\u91cd\u3002\u89c2\u5bdf\u56fe 13-11 \uff0c\u91cd\u590d\u5b50\u96c6\u662f\u5728\u4ee5\u4e0d\u540c\u987a\u5e8f\u9009\u62e9\u6570\u7ec4\u5143\u7d20\u65f6\u4ea7\u751f\u7684\uff0c\u4f8b\u5982\u4ee5\u4e0b\u60c5\u51b5\u3002
1.
\u6b65\u4e2d\u751f\u6210\u7684\u5b50\u96c6\u5b8c\u5168\u91cd\u590d\u3002\u5728\u641c\u7d22\u8fc7\u7a0b\u4e2d\uff0c\u6bcf\u4e00\u5c42\u7684\u9009\u62e9\u90fd\u662f\u4ece\u5de6\u5230\u53f3\u88ab\u9010\u4e2a\u5c1d\u8bd5\u7684\uff0c\u56e0\u6b64\u8d8a\u9760\u53f3\u7684\u5206\u652f\u88ab\u526a\u6389\u7684\u8d8a\u591a\u3002
1.
\u6b65\u548c\u7b2c 2.
\u6b65\u4e2d\u63cf\u8ff0\u7684\u5b50\u96c6\u5b8c\u5168\u91cd\u590d\u3002\u56fe 13-11 \u00a0 \u4e0d\u540c\u9009\u62e9\u987a\u5e8f\u5bfc\u81f4\u7684\u91cd\u590d\u5b50\u96c6
\u603b\u7ed3\u6765\u770b\uff0c\u7ed9\u5b9a\u8f93\u5165\u6570\u7ec4 \\([x_1, x_2, \\dots, x_n]\\) \uff0c\u8bbe\u641c\u7d22\u8fc7\u7a0b\u4e2d\u7684\u9009\u62e9\u5e8f\u5217\u4e3a \\([x_{i_1}, x_{i_2}, \\dots, x_{i_m}]\\) \uff0c\u5219\u8be5\u9009\u62e9\u5e8f\u5217\u9700\u8981\u6ee1\u8db3 \\(i_1 \\leq i_2 \\leq \\dots \\leq i_m\\) \uff0c\u4e0d\u6ee1\u8db3\u8be5\u6761\u4ef6\u7684\u9009\u62e9\u5e8f\u5217\u90fd\u4f1a\u9020\u6210\u91cd\u590d\uff0c\u5e94\u5f53\u526a\u679d\u3002
"},{"location":"chapter_backtracking/subset_sum_problem/#3","title":"3. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"\u4e3a\u5b9e\u73b0\u8be5\u526a\u679d\uff0c\u6211\u4eec\u521d\u59cb\u5316\u53d8\u91cf start
\uff0c\u7528\u4e8e\u6307\u793a\u904d\u5386\u8d77\u59cb\u70b9\u3002\u5f53\u505a\u51fa\u9009\u62e9 \\(x_{i}\\) \u540e\uff0c\u8bbe\u5b9a\u4e0b\u4e00\u8f6e\u4ece\u7d22\u5f15 \\(i\\) \u5f00\u59cb\u904d\u5386\u3002\u8fd9\u6837\u505a\u5c31\u53ef\u4ee5\u8ba9\u9009\u62e9\u5e8f\u5217\u6ee1\u8db3 \\(i_1 \\leq i_2 \\leq \\dots \\leq i_m\\) \uff0c\u4ece\u800c\u4fdd\u8bc1\u5b50\u96c6\u552f\u4e00\u3002
\u9664\u6b64\u4e4b\u5916\uff0c\u6211\u4eec\u8fd8\u5bf9\u4ee3\u7801\u8fdb\u884c\u4e86\u4ee5\u4e0b\u4e24\u9879\u4f18\u5316\u3002
nums
\u6392\u5e8f\u3002\u5728\u904d\u5386\u6240\u6709\u9009\u62e9\u65f6\uff0c\u5f53\u5b50\u96c6\u548c\u8d85\u8fc7 target
\u65f6\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\uff0c\u56e0\u4e3a\u540e\u8fb9\u7684\u5143\u7d20\u66f4\u5927\uff0c\u5176\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target
\u3002total
\uff0c\u901a\u8fc7\u5728 target
\u4e0a\u6267\u884c\u51cf\u6cd5\u6765\u7edf\u8ba1\u5143\u7d20\u548c\uff0c\u5f53 target
\u7b49\u4e8e \\(0\\) \u65f6\u8bb0\u5f55\u89e3\u3002def backtrack(\n state: list[int], target: int, choices: list[int], start: int, res: list[list[int]]\n):\n \"\"\"\u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I\"\"\"\n # \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n if target == 0:\n res.append(list(state))\n return\n # \u904d\u5386\u6240\u6709\u9009\u62e9\n # \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n for i in range(start, len(choices)):\n # \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n # \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n if target - choices[i] < 0:\n break\n # \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n state.append(choices[i])\n # \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state, target - choices[i], choices, i, res)\n # \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n state.pop()\n\ndef subset_sum_i(nums: list[int], target: int) -> list[list[int]]:\n \"\"\"\u6c42\u89e3\u5b50\u96c6\u548c I\"\"\"\n state = [] # \u72b6\u6001\uff08\u5b50\u96c6\uff09\n nums.sort() # \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n start = 0 # \u904d\u5386\u8d77\u59cb\u70b9\n res = [] # \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n backtrack(state, target, nums, start, res)\n return res\n
subset_sum_i.cpp/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nvoid backtrack(vector<int> &state, int target, vector<int> &choices, int start, vector<vector<int>> &res) {\n // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n if (target == 0) {\n res.push_back(state);\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n // \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n for (int i = start; i < choices.size(); i++) {\n // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n // \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n if (target - choices[i] < 0) {\n break;\n }\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n state.push_back(choices[i]);\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state, target - choices[i], choices, i, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n state.pop_back();\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I */\nvector<vector<int>> subsetSumI(vector<int> &nums, int target) {\n vector<int> state; // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n sort(nums.begin(), nums.end()); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n int start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\n vector<vector<int>> res; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n backtrack(state, target, nums, start, res);\n return res;\n}\n
subset_sum_i.java/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nvoid backtrack(List<Integer> state, int target, int[] choices, int start, List<List<Integer>> res) {\n // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n if (target == 0) {\n res.add(new ArrayList<>(state));\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n // \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n for (int i = start; i < choices.length; i++) {\n // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n // \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n if (target - choices[i] < 0) {\n break;\n }\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n state.add(choices[i]);\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state, target - choices[i], choices, i, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n state.remove(state.size() - 1);\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I */\nList<List<Integer>> subsetSumI(int[] nums, int target) {\n List<Integer> state = new ArrayList<>(); // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n Arrays.sort(nums); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n int start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\n List<List<Integer>> res = new ArrayList<>(); // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n backtrack(state, target, nums, start, res);\n return res;\n}\n
subset_sum_i.cs/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nvoid Backtrack(List<int> state, int target, int[] choices, int start, List<List<int>> res) {\n // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n if (target == 0) {\n res.Add(new List<int>(state));\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n // \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n for (int i = start; i < choices.Length; i++) {\n // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n // \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n if (target - choices[i] < 0) {\n break;\n }\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n state.Add(choices[i]);\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n Backtrack(state, target - choices[i], choices, i, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n state.RemoveAt(state.Count - 1);\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I */\nList<List<int>> SubsetSumI(int[] nums, int target) {\n List<int> state = []; // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n Array.Sort(nums); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n int start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\n List<List<int>> res = []; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n Backtrack(state, target, nums, start, res);\n return res;\n}\n
subset_sum_i.go/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nfunc backtrackSubsetSumI(start, target int, state, choices *[]int, res *[][]int) {\n // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n if target == 0 {\n newState := append([]int{}, *state...)\n *res = append(*res, newState)\n return\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n // \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n for i := start; i < len(*choices); i++ {\n // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n // \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n if target-(*choices)[i] < 0 {\n break\n }\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n *state = append(*state, (*choices)[i])\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrackSubsetSumI(i, target-(*choices)[i], state, choices, res)\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n *state = (*state)[:len(*state)-1]\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I */\nfunc subsetSumI(nums []int, target int) [][]int {\n state := make([]int, 0) // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n sort.Ints(nums) // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n start := 0 // \u904d\u5386\u8d77\u59cb\u70b9\n res := make([][]int, 0) // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n backtrackSubsetSumI(start, target, &state, &nums, &res)\n return res\n}\n
subset_sum_i.swift/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nfunc backtrack(state: inout [Int], target: Int, choices: [Int], start: Int, res: inout [[Int]]) {\n // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n if target == 0 {\n res.append(state)\n return\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n // \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n for i in stride(from: start, to: choices.count, by: 1) {\n // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n // \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n if target - choices[i] < 0 {\n break\n }\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n state.append(choices[i])\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state: &state, target: target - choices[i], choices: choices, start: i, res: &res)\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n state.removeLast()\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I */\nfunc subsetSumI(nums: [Int], target: Int) -> [[Int]] {\n var state: [Int] = [] // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n let nums = nums.sorted() // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n let start = 0 // \u904d\u5386\u8d77\u59cb\u70b9\n var res: [[Int]] = [] // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n backtrack(state: &state, target: target, choices: nums, start: start, res: &res)\n return res\n}\n
subset_sum_i.js/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nfunction backtrack(state, target, choices, start, res) {\n // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n if (target === 0) {\n res.push([...state]);\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n // \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n for (let i = start; i < choices.length; i++) {\n // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n // \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n if (target - choices[i] < 0) {\n break;\n }\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n state.push(choices[i]);\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state, target - choices[i], choices, i, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n state.pop();\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I */\nfunction subsetSumI(nums, target) {\n const state = []; // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n nums.sort((a, b) => a - b); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n const start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\n const res = []; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n backtrack(state, target, nums, start, res);\n return res;\n}\n
subset_sum_i.ts/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nfunction backtrack(\n state: number[],\n target: number,\n choices: number[],\n start: number,\n res: number[][]\n): void {\n // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n if (target === 0) {\n res.push([...state]);\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n // \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n for (let i = start; i < choices.length; i++) {\n // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n // \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n if (target - choices[i] < 0) {\n break;\n }\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n state.push(choices[i]);\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state, target - choices[i], choices, i, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n state.pop();\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I */\nfunction subsetSumI(nums: number[], target: number): number[][] {\n const state = []; // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n nums.sort((a, b) => a - b); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n const start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\n const res = []; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n backtrack(state, target, nums, start, res);\n return res;\n}\n
subset_sum_i.dart/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nvoid backtrack(\n List<int> state,\n int target,\n List<int> choices,\n int start,\n List<List<int>> res,\n) {\n // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n if (target == 0) {\n res.add(List.from(state));\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n // \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n for (int i = start; i < choices.length; i++) {\n // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n // \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n if (target - choices[i] < 0) {\n break;\n }\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n state.add(choices[i]);\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state, target - choices[i], choices, i, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n state.removeLast();\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I */\nList<List<int>> subsetSumI(List<int> nums, int target) {\n List<int> state = []; // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n nums.sort(); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n int start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\n List<List<int>> res = []; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n backtrack(state, target, nums, start, res);\n return res;\n}\n
subset_sum_i.rs/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nfn backtrack(mut state: Vec<i32>, target: i32, choices: &[i32], start: usize, res: &mut Vec<Vec<i32>>) {\n // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n if target == 0 {\n res.push(state);\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n // \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n for i in start..choices.len() {\n // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n // \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n if target - choices[i] < 0 {\n break;\n }\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n state.push(choices[i]);\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state.clone(), target - choices[i], choices, i, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n state.pop();\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I */\nfn subset_sum_i(nums: &mut [i32], target: i32) -> Vec<Vec<i32>> {\n let state = Vec::new(); // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n nums.sort(); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n let start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\n let mut res = Vec::new(); // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n backtrack(state, target, nums, start, &mut res);\n res\n}\n
subset_sum_i.c/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nvoid backtrack(int target, int *choices, int choicesSize, int start) {\n // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n if (target == 0) {\n for (int i = 0; i < stateSize; ++i) {\n res[resSize][i] = state[i];\n }\n resColSizes[resSize++] = stateSize;\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n // \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n for (int i = start; i < choicesSize; i++) {\n // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n // \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n if (target - choices[i] < 0) {\n break;\n }\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n state[stateSize] = choices[i];\n stateSize++;\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(target - choices[i], choices, choicesSize, i);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n stateSize--;\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I */\nvoid subsetSumI(int *nums, int numsSize, int target) {\n qsort(nums, numsSize, sizeof(int), cmp); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n int start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\n backtrack(target, nums, numsSize, start);\n}\n
subset_sum_i.zig[class]{}-[func]{backtrack}\n\n[class]{}-[func]{subsetSumI}\n
\u56fe 13-12 \u6240\u793a\u4e3a\u5c06\u6570\u7ec4 \\([3, 4, 5]\\) \u548c\u76ee\u6807\u5143\u7d20 \\(9\\) \u8f93\u5165\u4ee5\u4e0a\u4ee3\u7801\u540e\u7684\u6574\u4f53\u56de\u6eaf\u8fc7\u7a0b\u3002
\u56fe 13-12 \u00a0 \u5b50\u96c6\u548c I \u56de\u6eaf\u8fc7\u7a0b
"},{"location":"chapter_backtracking/subset_sum_problem/#1332","title":"13.3.2 \u00a0 \u8003\u8651\u91cd\u590d\u5143\u7d20\u7684\u60c5\u51b5","text":"Question
\u7ed9\u5b9a\u4e00\u4e2a\u6b63\u6574\u6570\u6570\u7ec4 nums
\u548c\u4e00\u4e2a\u76ee\u6807\u6b63\u6574\u6570 target
\uff0c\u8bf7\u627e\u51fa\u6240\u6709\u53ef\u80fd\u7684\u7ec4\u5408\uff0c\u4f7f\u5f97\u7ec4\u5408\u4e2d\u7684\u5143\u7d20\u548c\u7b49\u4e8e target
\u3002\u7ed9\u5b9a\u6570\u7ec4\u53ef\u80fd\u5305\u542b\u91cd\u590d\u5143\u7d20\uff0c\u6bcf\u4e2a\u5143\u7d20\u53ea\u53ef\u88ab\u9009\u62e9\u4e00\u6b21\u3002\u8bf7\u4ee5\u5217\u8868\u5f62\u5f0f\u8fd4\u56de\u8fd9\u4e9b\u7ec4\u5408\uff0c\u5217\u8868\u4e2d\u4e0d\u5e94\u5305\u542b\u91cd\u590d\u7ec4\u5408\u3002
\u76f8\u6bd4\u4e8e\u4e0a\u9898\uff0c\u672c\u9898\u7684\u8f93\u5165\u6570\u7ec4\u53ef\u80fd\u5305\u542b\u91cd\u590d\u5143\u7d20\uff0c\u8fd9\u5f15\u5165\u4e86\u65b0\u7684\u95ee\u9898\u3002\u4f8b\u5982\uff0c\u7ed9\u5b9a\u6570\u7ec4 \\([4, \\hat{4}, 5]\\) \u548c\u76ee\u6807\u5143\u7d20 \\(9\\) \uff0c\u5219\u73b0\u6709\u4ee3\u7801\u7684\u8f93\u51fa\u7ed3\u679c\u4e3a \\([4, 5], [\\hat{4}, 5]\\) \uff0c\u51fa\u73b0\u4e86\u91cd\u590d\u5b50\u96c6\u3002
\u9020\u6210\u8fd9\u79cd\u91cd\u590d\u7684\u539f\u56e0\u662f\u76f8\u7b49\u5143\u7d20\u5728\u67d0\u8f6e\u4e2d\u88ab\u591a\u6b21\u9009\u62e9\u3002\u5728\u56fe 13-13 \u4e2d\uff0c\u7b2c\u4e00\u8f6e\u5171\u6709\u4e09\u4e2a\u9009\u62e9\uff0c\u5176\u4e2d\u4e24\u4e2a\u90fd\u4e3a \\(4\\) \uff0c\u4f1a\u4ea7\u751f\u4e24\u4e2a\u91cd\u590d\u7684\u641c\u7d22\u5206\u652f\uff0c\u4ece\u800c\u8f93\u51fa\u91cd\u590d\u5b50\u96c6\uff1b\u540c\u7406\uff0c\u7b2c\u4e8c\u8f6e\u7684\u4e24\u4e2a \\(4\\) \u4e5f\u4f1a\u4ea7\u751f\u91cd\u590d\u5b50\u96c6\u3002
\u56fe 13-13 \u00a0 \u76f8\u7b49\u5143\u7d20\u5bfc\u81f4\u7684\u91cd\u590d\u5b50\u96c6
"},{"location":"chapter_backtracking/subset_sum_problem/#1_1","title":"1. \u00a0 \u76f8\u7b49\u5143\u7d20\u526a\u679d","text":"\u4e3a\u89e3\u51b3\u6b64\u95ee\u9898\uff0c\u6211\u4eec\u9700\u8981\u9650\u5236\u76f8\u7b49\u5143\u7d20\u5728\u6bcf\u4e00\u8f6e\u4e2d\u53ea\u80fd\u88ab\u9009\u62e9\u4e00\u6b21\u3002\u5b9e\u73b0\u65b9\u5f0f\u6bd4\u8f83\u5de7\u5999\uff1a\u7531\u4e8e\u6570\u7ec4\u662f\u5df2\u6392\u5e8f\u7684\uff0c\u56e0\u6b64\u76f8\u7b49\u5143\u7d20\u90fd\u662f\u76f8\u90bb\u7684\u3002\u8fd9\u610f\u5473\u7740\u5728\u67d0\u8f6e\u9009\u62e9\u4e2d\uff0c\u82e5\u5f53\u524d\u5143\u7d20\u4e0e\u5176\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u5219\u8bf4\u660e\u5b83\u5df2\u7ecf\u88ab\u9009\u62e9\u8fc7\uff0c\u56e0\u6b64\u76f4\u63a5\u8df3\u8fc7\u5f53\u524d\u5143\u7d20\u3002
\u4e0e\u6b64\u540c\u65f6\uff0c\u672c\u9898\u89c4\u5b9a\u6bcf\u4e2a\u6570\u7ec4\u5143\u7d20\u53ea\u80fd\u88ab\u9009\u62e9\u4e00\u6b21\u3002\u5e78\u8fd0\u7684\u662f\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u5229\u7528\u53d8\u91cf start
\u6765\u6ee1\u8db3\u8be5\u7ea6\u675f\uff1a\u5f53\u505a\u51fa\u9009\u62e9 \\(x_{i}\\) \u540e\uff0c\u8bbe\u5b9a\u4e0b\u4e00\u8f6e\u4ece\u7d22\u5f15 \\(i + 1\\) \u5f00\u59cb\u5411\u540e\u904d\u5386\u3002\u8fd9\u6837\u65e2\u80fd\u53bb\u9664\u91cd\u590d\u5b50\u96c6\uff0c\u4e5f\u80fd\u907f\u514d\u91cd\u590d\u9009\u62e9\u5143\u7d20\u3002
def backtrack(\n state: list[int], target: int, choices: list[int], start: int, res: list[list[int]]\n):\n \"\"\"\u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II\"\"\"\n # \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n if target == 0:\n res.append(list(state))\n return\n # \u904d\u5386\u6240\u6709\u9009\u62e9\n # \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n # \u526a\u679d\u4e09\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\n for i in range(start, len(choices)):\n # \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n # \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n if target - choices[i] < 0:\n break\n # \u526a\u679d\u56db\uff1a\u5982\u679c\u8be5\u5143\u7d20\u4e0e\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u8bf4\u660e\u8be5\u641c\u7d22\u5206\u652f\u91cd\u590d\uff0c\u76f4\u63a5\u8df3\u8fc7\n if i > start and choices[i] == choices[i - 1]:\n continue\n # \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n state.append(choices[i])\n # \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state, target - choices[i], choices, i + 1, res)\n # \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n state.pop()\n\ndef subset_sum_ii(nums: list[int], target: int) -> list[list[int]]:\n \"\"\"\u6c42\u89e3\u5b50\u96c6\u548c II\"\"\"\n state = [] # \u72b6\u6001\uff08\u5b50\u96c6\uff09\n nums.sort() # \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n start = 0 # \u904d\u5386\u8d77\u59cb\u70b9\n res = [] # \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n backtrack(state, target, nums, start, res)\n return res\n
subset_sum_ii.cpp/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II */\nvoid backtrack(vector<int> &state, int target, vector<int> &choices, int start, vector<vector<int>> &res) {\n // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n if (target == 0) {\n res.push_back(state);\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n // \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n // \u526a\u679d\u4e09\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\n for (int i = start; i < choices.size(); i++) {\n // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n // \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n if (target - choices[i] < 0) {\n break;\n }\n // \u526a\u679d\u56db\uff1a\u5982\u679c\u8be5\u5143\u7d20\u4e0e\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u8bf4\u660e\u8be5\u641c\u7d22\u5206\u652f\u91cd\u590d\uff0c\u76f4\u63a5\u8df3\u8fc7\n if (i > start && choices[i] == choices[i - 1]) {\n continue;\n }\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n state.push_back(choices[i]);\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state, target - choices[i], choices, i + 1, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n state.pop_back();\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c II */\nvector<vector<int>> subsetSumII(vector<int> &nums, int target) {\n vector<int> state; // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n sort(nums.begin(), nums.end()); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n int start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\n vector<vector<int>> res; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n backtrack(state, target, nums, start, res);\n return res;\n}\n
subset_sum_ii.java/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II */\nvoid backtrack(List<Integer> state, int target, int[] choices, int start, List<List<Integer>> res) {\n // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n if (target == 0) {\n res.add(new ArrayList<>(state));\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n // \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n // \u526a\u679d\u4e09\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\n for (int i = start; i < choices.length; i++) {\n // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n // \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n if (target - choices[i] < 0) {\n break;\n }\n // \u526a\u679d\u56db\uff1a\u5982\u679c\u8be5\u5143\u7d20\u4e0e\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u8bf4\u660e\u8be5\u641c\u7d22\u5206\u652f\u91cd\u590d\uff0c\u76f4\u63a5\u8df3\u8fc7\n if (i > start && choices[i] == choices[i - 1]) {\n continue;\n }\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n state.add(choices[i]);\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state, target - choices[i], choices, i + 1, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n state.remove(state.size() - 1);\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c II */\nList<List<Integer>> subsetSumII(int[] nums, int target) {\n List<Integer> state = new ArrayList<>(); // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n Arrays.sort(nums); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n int start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\n List<List<Integer>> res = new ArrayList<>(); // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n backtrack(state, target, nums, start, res);\n return res;\n}\n
subset_sum_ii.cs/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II */\nvoid Backtrack(List<int> state, int target, int[] choices, int start, List<List<int>> res) {\n // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n if (target == 0) {\n res.Add(new List<int>(state));\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n // \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n // \u526a\u679d\u4e09\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\n for (int i = start; i < choices.Length; i++) {\n // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n // \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n if (target - choices[i] < 0) {\n break;\n }\n // \u526a\u679d\u56db\uff1a\u5982\u679c\u8be5\u5143\u7d20\u4e0e\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u8bf4\u660e\u8be5\u641c\u7d22\u5206\u652f\u91cd\u590d\uff0c\u76f4\u63a5\u8df3\u8fc7\n if (i > start && choices[i] == choices[i - 1]) {\n continue;\n }\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n state.Add(choices[i]);\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n Backtrack(state, target - choices[i], choices, i + 1, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n state.RemoveAt(state.Count - 1);\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c II */\nList<List<int>> SubsetSumII(int[] nums, int target) {\n List<int> state = []; // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n Array.Sort(nums); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n int start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\n List<List<int>> res = []; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n Backtrack(state, target, nums, start, res);\n return res;\n}\n
subset_sum_ii.go/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II */\nfunc backtrackSubsetSumII(start, target int, state, choices *[]int, res *[][]int) {\n // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n if target == 0 {\n newState := append([]int{}, *state...)\n *res = append(*res, newState)\n return\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n // \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n // \u526a\u679d\u4e09\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\n for i := start; i < len(*choices); i++ {\n // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n // \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n if target-(*choices)[i] < 0 {\n break\n }\n // \u526a\u679d\u56db\uff1a\u5982\u679c\u8be5\u5143\u7d20\u4e0e\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u8bf4\u660e\u8be5\u641c\u7d22\u5206\u652f\u91cd\u590d\uff0c\u76f4\u63a5\u8df3\u8fc7\n if i > start && (*choices)[i] == (*choices)[i-1] {\n continue\n }\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n *state = append(*state, (*choices)[i])\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrackSubsetSumII(i+1, target-(*choices)[i], state, choices, res)\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n *state = (*state)[:len(*state)-1]\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c II */\nfunc subsetSumII(nums []int, target int) [][]int {\n state := make([]int, 0) // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n sort.Ints(nums) // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n start := 0 // \u904d\u5386\u8d77\u59cb\u70b9\n res := make([][]int, 0) // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n backtrackSubsetSumII(start, target, &state, &nums, &res)\n return res\n}\n
subset_sum_ii.swift/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II */\nfunc backtrack(state: inout [Int], target: Int, choices: [Int], start: Int, res: inout [[Int]]) {\n // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n if target == 0 {\n res.append(state)\n return\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n // \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n // \u526a\u679d\u4e09\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\n for i in stride(from: start, to: choices.count, by: 1) {\n // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n // \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n if target - choices[i] < 0 {\n break\n }\n // \u526a\u679d\u56db\uff1a\u5982\u679c\u8be5\u5143\u7d20\u4e0e\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u8bf4\u660e\u8be5\u641c\u7d22\u5206\u652f\u91cd\u590d\uff0c\u76f4\u63a5\u8df3\u8fc7\n if i > start, choices[i] == choices[i - 1] {\n continue\n }\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n state.append(choices[i])\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state: &state, target: target - choices[i], choices: choices, start: i + 1, res: &res)\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n state.removeLast()\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c II */\nfunc subsetSumII(nums: [Int], target: Int) -> [[Int]] {\n var state: [Int] = [] // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n let nums = nums.sorted() // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n let start = 0 // \u904d\u5386\u8d77\u59cb\u70b9\n var res: [[Int]] = [] // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n backtrack(state: &state, target: target, choices: nums, start: start, res: &res)\n return res\n}\n
subset_sum_ii.js/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II */\nfunction backtrack(state, target, choices, start, res) {\n // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n if (target === 0) {\n res.push([...state]);\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n // \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n // \u526a\u679d\u4e09\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\n for (let i = start; i < choices.length; i++) {\n // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n // \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n if (target - choices[i] < 0) {\n break;\n }\n // \u526a\u679d\u56db\uff1a\u5982\u679c\u8be5\u5143\u7d20\u4e0e\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u8bf4\u660e\u8be5\u641c\u7d22\u5206\u652f\u91cd\u590d\uff0c\u76f4\u63a5\u8df3\u8fc7\n if (i > start && choices[i] === choices[i - 1]) {\n continue;\n }\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n state.push(choices[i]);\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state, target - choices[i], choices, i + 1, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n state.pop();\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c II */\nfunction subsetSumII(nums, target) {\n const state = []; // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n nums.sort((a, b) => a - b); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n const start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\n const res = []; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n backtrack(state, target, nums, start, res);\n return res;\n}\n
subset_sum_ii.ts/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II */\nfunction backtrack(\n state: number[],\n target: number,\n choices: number[],\n start: number,\n res: number[][]\n): void {\n // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n if (target === 0) {\n res.push([...state]);\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n // \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n // \u526a\u679d\u4e09\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\n for (let i = start; i < choices.length; i++) {\n // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n // \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n if (target - choices[i] < 0) {\n break;\n }\n // \u526a\u679d\u56db\uff1a\u5982\u679c\u8be5\u5143\u7d20\u4e0e\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u8bf4\u660e\u8be5\u641c\u7d22\u5206\u652f\u91cd\u590d\uff0c\u76f4\u63a5\u8df3\u8fc7\n if (i > start && choices[i] === choices[i - 1]) {\n continue;\n }\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n state.push(choices[i]);\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state, target - choices[i], choices, i + 1, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n state.pop();\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c II */\nfunction subsetSumII(nums: number[], target: number): number[][] {\n const state = []; // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n nums.sort((a, b) => a - b); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n const start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\n const res = []; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n backtrack(state, target, nums, start, res);\n return res;\n}\n
subset_sum_ii.dart/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II */\nvoid backtrack(\n List<int> state,\n int target,\n List<int> choices,\n int start,\n List<List<int>> res,\n) {\n // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n if (target == 0) {\n res.add(List.from(state));\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n // \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n // \u526a\u679d\u4e09\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\n for (int i = start; i < choices.length; i++) {\n // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n // \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n if (target - choices[i] < 0) {\n break;\n }\n // \u526a\u679d\u56db\uff1a\u5982\u679c\u8be5\u5143\u7d20\u4e0e\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u8bf4\u660e\u8be5\u641c\u7d22\u5206\u652f\u91cd\u590d\uff0c\u76f4\u63a5\u8df3\u8fc7\n if (i > start && choices[i] == choices[i - 1]) {\n continue;\n }\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n state.add(choices[i]);\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state, target - choices[i], choices, i + 1, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n state.removeLast();\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c II */\nList<List<int>> subsetSumII(List<int> nums, int target) {\n List<int> state = []; // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n nums.sort(); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n int start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\n List<List<int>> res = []; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n backtrack(state, target, nums, start, res);\n return res;\n}\n
subset_sum_ii.rs/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II */\nfn backtrack(mut state: Vec<i32>, target: i32, choices: &[i32], start: usize, res: &mut Vec<Vec<i32>>) {\n // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n if target == 0 {\n res.push(state);\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n // \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n // \u526a\u679d\u4e09\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\n for i in start..choices.len() {\n // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n // \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n if target - choices[i] < 0 {\n break;\n }\n // \u526a\u679d\u56db\uff1a\u5982\u679c\u8be5\u5143\u7d20\u4e0e\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u8bf4\u660e\u8be5\u641c\u7d22\u5206\u652f\u91cd\u590d\uff0c\u76f4\u63a5\u8df3\u8fc7\n if i > start && choices[i] == choices[i - 1] {\n continue;\n }\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n state.push(choices[i]);\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state.clone(), target - choices[i], choices, i, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n state.pop();\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c II */\nfn subset_sum_ii(nums: &mut [i32], target: i32) -> Vec<Vec<i32>> {\n let state = Vec::new(); // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n nums.sort(); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n let start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\n let mut res = Vec::new(); // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n backtrack(state, target, nums, start, &mut res);\n res\n}\n
subset_sum_ii.c/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II */\nvoid backtrack(int target, int *choices, int choicesSize, int start) {\n // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n if (target == 0) {\n for (int i = 0; i < stateSize; i++) {\n res[resSize][i] = state[i];\n }\n resColSizes[resSize++] = stateSize;\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n // \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n // \u526a\u679d\u4e09\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\n for (int i = start; i < choicesSize; i++) {\n // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\n if (target - choices[i] < 0) {\n continue;\n }\n // \u526a\u679d\u56db\uff1a\u5982\u679c\u8be5\u5143\u7d20\u4e0e\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u8bf4\u660e\u8be5\u641c\u7d22\u5206\u652f\u91cd\u590d\uff0c\u76f4\u63a5\u8df3\u8fc7\n if (i > start && choices[i] == choices[i - 1]) {\n continue;\n }\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n state[stateSize] = choices[i];\n stateSize++;\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(target - choices[i], choices, choicesSize, i + 1);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n stateSize--;\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c II */\nvoid subsetSumII(int *nums, int numsSize, int target) {\n // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n qsort(nums, numsSize, sizeof(int), cmp);\n // \u5f00\u59cb\u56de\u6eaf\n backtrack(target, nums, numsSize, 0);\n}\n
subset_sum_ii.zig[class]{}-[func]{backtrack}\n\n[class]{}-[func]{subsetSumII}\n
\u56fe 13-14 \u5c55\u793a\u4e86\u6570\u7ec4 \\([4, 4, 5]\\) \u548c\u76ee\u6807\u5143\u7d20 \\(9\\) \u7684\u56de\u6eaf\u8fc7\u7a0b\uff0c\u5171\u5305\u542b\u56db\u79cd\u526a\u679d\u64cd\u4f5c\u3002\u8bf7\u4f60\u5c06\u56fe\u793a\u4e0e\u4ee3\u7801\u6ce8\u91ca\u76f8\u7ed3\u5408\uff0c\u7406\u89e3\u6574\u4e2a\u641c\u7d22\u8fc7\u7a0b\uff0c\u4ee5\u53ca\u6bcf\u79cd\u526a\u679d\u64cd\u4f5c\u662f\u5982\u4f55\u5de5\u4f5c\u7684\u3002
\u56fe 13-14 \u00a0 \u5b50\u96c6\u548c II \u56de\u6eaf\u8fc7\u7a0b
"},{"location":"chapter_backtracking/summary/","title":"13.5 \u00a0 \u5c0f\u7ed3","text":""},{"location":"chapter_backtracking/summary/#1","title":"1. \u00a0 \u91cd\u70b9\u56de\u987e","text":"\u600e\u4e48\u7406\u89e3\u56de\u6eaf\u548c\u9012\u5f52\u7684\u5173\u7cfb\uff1f
\u603b\u7684\u6765\u770b\uff0c\u56de\u6eaf\u662f\u4e00\u79cd\u201c\u7b97\u6cd5\u7b56\u7565\u201d\uff0c\u800c\u9012\u5f52\u66f4\u50cf\u662f\u4e00\u4e2a\u201c\u5de5\u5177\u201d\u3002
Abstract
\u590d\u6742\u5ea6\u5206\u6790\u72b9\u5982\u6d69\u701a\u7684\u7b97\u6cd5\u5b87\u5b99\u4e2d\u7684\u65f6\u7a7a\u5411\u5bfc\u3002
\u5b83\u5e26\u9886\u6211\u4eec\u5728\u65f6\u95f4\u4e0e\u7a7a\u95f4\u8fd9\u4e24\u4e2a\u7ef4\u5ea6\u4e0a\u6df1\u5165\u63a2\u7d22\uff0c\u5bfb\u627e\u66f4\u4f18\u96c5\u7684\u89e3\u51b3\u65b9\u6848\u3002
"},{"location":"chapter_computational_complexity/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"\u5728\u7b97\u6cd5\u4e2d\uff0c\u91cd\u590d\u6267\u884c\u67d0\u4e2a\u4efb\u52a1\u662f\u5f88\u5e38\u89c1\u7684\uff0c\u5176\u4e0e\u590d\u6742\u5ea6\u5206\u6790\u606f\u606f\u76f8\u5173\u3002\u56e0\u6b64\uff0c\u5728\u5c55\u5f00\u4ecb\u7ecd\u65f6\u95f4\u590d\u6742\u5ea6\u548c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e4b\u524d\uff0c\u6211\u4eec\u5148\u6765\u4e86\u89e3\u5982\u4f55\u5728\u7a0b\u5e8f\u4e2d\u5b9e\u73b0\u91cd\u590d\u6267\u884c\u4efb\u52a1\uff0c\u5373\u4e24\u79cd\u57fa\u672c\u7684\u7a0b\u5e8f\u63a7\u5236\u7ed3\u6784\uff1a\u8fed\u4ee3\u3001\u9012\u5f52\u3002
"},{"location":"chapter_computational_complexity/iteration_and_recursion/#221","title":"2.2.1 \u00a0 \u8fed\u4ee3","text":"\u300c\u8fed\u4ee3 iteration\u300d\u662f\u4e00\u79cd\u91cd\u590d\u6267\u884c\u67d0\u4e2a\u4efb\u52a1\u7684\u63a7\u5236\u7ed3\u6784\u3002\u5728\u8fed\u4ee3\u4e2d\uff0c\u7a0b\u5e8f\u4f1a\u5728\u6ee1\u8db3\u4e00\u5b9a\u7684\u6761\u4ef6\u4e0b\u91cd\u590d\u6267\u884c\u67d0\u6bb5\u4ee3\u7801\uff0c\u76f4\u5230\u8fd9\u4e2a\u6761\u4ef6\u4e0d\u518d\u6ee1\u8db3\u3002
"},{"location":"chapter_computational_complexity/iteration_and_recursion/#1-for","title":"1. \u00a0 for \u5faa\u73af","text":"for
\u5faa\u73af\u662f\u6700\u5e38\u89c1\u7684\u8fed\u4ee3\u5f62\u5f0f\u4e4b\u4e00\uff0c\u9002\u5408\u5728\u9884\u5148\u77e5\u9053\u8fed\u4ee3\u6b21\u6570\u65f6\u4f7f\u7528\u3002
\u4ee5\u4e0b\u51fd\u6570\u57fa\u4e8e for
\u5faa\u73af\u5b9e\u73b0\u4e86\u6c42\u548c \\(1 + 2 + \\dots + n\\) \uff0c\u6c42\u548c\u7ed3\u679c\u4f7f\u7528\u53d8\u91cf res
\u8bb0\u5f55\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0cPython \u4e2d range(a, b)
\u5bf9\u5e94\u7684\u533a\u95f4\u662f\u201c\u5de6\u95ed\u53f3\u5f00\u201d\u7684\uff0c\u5bf9\u5e94\u7684\u904d\u5386\u8303\u56f4\u4e3a \\(a, a + 1, \\dots, b-1\\) \uff1a
def for_loop(n: int) -> int:\n \"\"\"for \u5faa\u73af\"\"\"\n res = 0\n # \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n for i in range(1, n + 1):\n res += i\n return res\n
iteration.cpp/* for \u5faa\u73af */\nint forLoop(int n) {\n int res = 0;\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n for (int i = 1; i <= n; ++i) {\n res += i;\n }\n return res;\n}\n
iteration.java/* for \u5faa\u73af */\nint forLoop(int n) {\n int res = 0;\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n for (int i = 1; i <= n; i++) {\n res += i;\n }\n return res;\n}\n
iteration.cs/* for \u5faa\u73af */\nint ForLoop(int n) {\n int res = 0;\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n for (int i = 1; i <= n; i++) {\n res += i;\n }\n return res;\n}\n
iteration.go/* for \u5faa\u73af */\nfunc forLoop(n int) int {\n res := 0\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n for i := 1; i <= n; i++ {\n res += i\n }\n return res\n}\n
iteration.swift/* for \u5faa\u73af */\nfunc forLoop(n: Int) -> Int {\n var res = 0\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n for i in 1 ... n {\n res += i\n }\n return res\n}\n
iteration.js/* for \u5faa\u73af */\nfunction forLoop(n) {\n let res = 0;\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n for (let i = 1; i <= n; i++) {\n res += i;\n }\n return res;\n}\n
iteration.ts/* for \u5faa\u73af */\nfunction forLoop(n: number): number {\n let res = 0;\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n for (let i = 1; i <= n; i++) {\n res += i;\n }\n return res;\n}\n
iteration.dart/* for \u5faa\u73af */\nint forLoop(int n) {\n int res = 0;\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n for (int i = 1; i <= n; i++) {\n res += i;\n }\n return res;\n}\n
iteration.rs/* for \u5faa\u73af */\nfn for_loop(n: i32) -> i32 {\n let mut res = 0;\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n for i in 1..=n {\n res += i;\n }\n res\n} \n
iteration.c/* for \u5faa\u73af */\nint forLoop(int n) {\n int res = 0;\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n for (int i = 1; i <= n; i++) {\n res += i;\n }\n return res;\n}\n
iteration.zig// for \u5faa\u73af\nfn forLoop(n: usize) i32 {\n var res: i32 = 0;\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n for (1..n+1) |i| {\n res = res + @as(i32, @intCast(i));\n }\n return res;\n} \n
\u56fe 2-1 \u662f\u8be5\u6c42\u548c\u51fd\u6570\u7684\u6d41\u7a0b\u6846\u56fe\u3002
\u56fe 2-1 \u00a0 \u6c42\u548c\u51fd\u6570\u7684\u6d41\u7a0b\u6846\u56fe
\u6b64\u6c42\u548c\u51fd\u6570\u7684\u64cd\u4f5c\u6570\u91cf\u4e0e\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u6210\u6b63\u6bd4\uff0c\u6216\u8005\u8bf4\u6210\u201c\u7ebf\u6027\u5173\u7cfb\u201d\u3002\u5b9e\u9645\u4e0a\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u63cf\u8ff0\u7684\u5c31\u662f\u8fd9\u4e2a\u201c\u7ebf\u6027\u5173\u7cfb\u201d\u3002\u76f8\u5173\u5185\u5bb9\u5c06\u4f1a\u5728\u4e0b\u4e00\u8282\u4e2d\u8be6\u7ec6\u4ecb\u7ecd\u3002
"},{"location":"chapter_computational_complexity/iteration_and_recursion/#2-while","title":"2. \u00a0 while \u5faa\u73af","text":"\u4e0e for
\u5faa\u73af\u7c7b\u4f3c\uff0cwhile
\u5faa\u73af\u4e5f\u662f\u4e00\u79cd\u5b9e\u73b0\u8fed\u4ee3\u7684\u65b9\u6cd5\u3002\u5728 while
\u5faa\u73af\u4e2d\uff0c\u7a0b\u5e8f\u6bcf\u8f6e\u90fd\u4f1a\u5148\u68c0\u67e5\u6761\u4ef6\uff0c\u5982\u679c\u6761\u4ef6\u4e3a\u771f\uff0c\u5219\u7ee7\u7eed\u6267\u884c\uff0c\u5426\u5219\u5c31\u7ed3\u675f\u5faa\u73af\u3002
\u4e0b\u9762\u6211\u4eec\u7528 while
\u5faa\u73af\u6765\u5b9e\u73b0\u6c42\u548c \\(1 + 2 + \\dots + n\\) \uff1a
def while_loop(n: int) -> int:\n \"\"\"while \u5faa\u73af\"\"\"\n res = 0\n i = 1 # \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n # \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n while i <= n:\n res += i\n i += 1 # \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n return res\n
iteration.cpp/* while \u5faa\u73af */\nint whileLoop(int n) {\n int res = 0;\n int i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n while (i <= n) {\n res += i;\n i++; // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n }\n return res;\n}\n
iteration.java/* while \u5faa\u73af */\nint whileLoop(int n) {\n int res = 0;\n int i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n while (i <= n) {\n res += i;\n i++; // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n }\n return res;\n}\n
iteration.cs/* while \u5faa\u73af */\nint WhileLoop(int n) {\n int res = 0;\n int i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n while (i <= n) {\n res += i;\n i += 1; // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n }\n return res;\n}\n
iteration.go/* while \u5faa\u73af */\nfunc whileLoop(n int) int {\n res := 0\n // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n i := 1\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n for i <= n {\n res += i\n // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n i++\n }\n return res\n}\n
iteration.swift/* while \u5faa\u73af */\nfunc whileLoop(n: Int) -> Int {\n var res = 0\n var i = 1 // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n while i <= n {\n res += i\n i += 1 // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n }\n return res\n}\n
iteration.js/* while \u5faa\u73af */\nfunction whileLoop(n) {\n let res = 0;\n let i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n while (i <= n) {\n res += i;\n i++; // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n }\n return res;\n}\n
iteration.ts/* while \u5faa\u73af */\nfunction whileLoop(n: number): number {\n let res = 0;\n let i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n while (i <= n) {\n res += i;\n i++; // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n }\n return res;\n}\n
iteration.dart/* while \u5faa\u73af */\nint whileLoop(int n) {\n int res = 0;\n int i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n while (i <= n) {\n res += i;\n i++; // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n }\n return res;\n}\n
iteration.rs/* while \u5faa\u73af */\nfn while_loop(n: i32) -> i32 {\n let mut res = 0;\n let mut i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n while i <= n {\n res += i;\n i += 1; // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n }\n res\n}\n
iteration.c/* while \u5faa\u73af */\nint whileLoop(int n) {\n int res = 0;\n int i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n while (i <= n) {\n res += i;\n i++; // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n }\n return res;\n}\n
iteration.zig// while \u5faa\u73af\nfn whileLoop(n: i32) i32 {\n var res: i32 = 0;\n var i: i32 = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n while (i <= n) {\n res += @intCast(i);\n i += 1;\n }\n return res;\n}\n
while
\u5faa\u73af\u6bd4 for
\u5faa\u73af\u7684\u81ea\u7531\u5ea6\u66f4\u9ad8\u3002\u5728 while
\u5faa\u73af\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u81ea\u7531\u5730\u8bbe\u8ba1\u6761\u4ef6\u53d8\u91cf\u7684\u521d\u59cb\u5316\u548c\u66f4\u65b0\u6b65\u9aa4\u3002
\u4f8b\u5982\u5728\u4ee5\u4e0b\u4ee3\u7801\u4e2d\uff0c\u6761\u4ef6\u53d8\u91cf \\(i\\) \u6bcf\u8f6e\u8fdb\u884c\u4e24\u6b21\u66f4\u65b0\uff0c\u8fd9\u79cd\u60c5\u51b5\u5c31\u4e0d\u592a\u65b9\u4fbf\u7528 for
\u5faa\u73af\u5b9e\u73b0\uff1a
def while_loop_ii(n: int) -> int:\n \"\"\"while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09\"\"\"\n res = 0\n i = 1 # \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n # \u5faa\u73af\u6c42\u548c 1, 4, 10, ...\n while i <= n:\n res += i\n # \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n i += 1\n i *= 2\n return res\n
iteration.cpp/* while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09 */\nint whileLoopII(int n) {\n int res = 0;\n int i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n // \u5faa\u73af\u6c42\u548c 1, 4, 10, ...\n while (i <= n) {\n res += i;\n // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n i++;\n i *= 2;\n }\n return res;\n}\n
iteration.java/* while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09 */\nint whileLoopII(int n) {\n int res = 0;\n int i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n // \u5faa\u73af\u6c42\u548c 1, 4, 10, ...\n while (i <= n) {\n res += i;\n // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n i++;\n i *= 2;\n }\n return res;\n}\n
iteration.cs/* while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09 */\nint WhileLoopII(int n) {\n int res = 0;\n int i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n // \u5faa\u73af\u6c42\u548c 1, 2, 4, 5...\n while (i <= n) {\n res += i;\n // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n i += 1; \n i *= 2;\n }\n return res;\n}\n
iteration.go/* while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09 */\nfunc whileLoopII(n int) int {\n res := 0\n // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n i := 1\n // \u5faa\u73af\u6c42\u548c 1, 4, 10, ...\n for i <= n {\n res += i\n // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n i++\n i *= 2\n }\n return res\n}\n
iteration.swift/* while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09 */\nfunc whileLoopII(n: Int) -> Int {\n var res = 0\n var i = 1 // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n // \u5faa\u73af\u6c42\u548c 1, 4, 10, ...\n while i <= n {\n res += i\n // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n i += 1\n i *= 2\n }\n return res\n}\n
iteration.js/* while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09 */\nfunction whileLoopII(n) {\n let res = 0;\n let i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n // \u5faa\u73af\u6c42\u548c 1, 4, 10, ...\n while (i <= n) {\n res += i;\n // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n i++;\n i *= 2;\n }\n return res;\n}\n
iteration.ts/* while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09 */\nfunction whileLoopII(n: number): number {\n let res = 0;\n let i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n // \u5faa\u73af\u6c42\u548c 1, 4, 10, ...\n while (i <= n) {\n res += i;\n // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n i++;\n i *= 2;\n }\n return res;\n}\n
iteration.dart/* while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09 */\nint whileLoopII(int n) {\n int res = 0;\n int i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n // \u5faa\u73af\u6c42\u548c 1, 4, 10, ...\n while (i <= n) {\n res += i;\n // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n i++;\n i *= 2;\n }\n return res;\n}\n
iteration.rs/* while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09 */\nfn while_loop_ii(n: i32) -> i32 {\n let mut res = 0;\n let mut i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n // \u5faa\u73af\u6c42\u548c 1, 4, 10, ...\n while i <= n {\n res += i;\n // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n i += 1;\n i *= 2;\n }\n res\n}\n
iteration.c/* while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09 */\nint whileLoopII(int n) {\n int res = 0;\n int i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n // \u5faa\u73af\u6c42\u548c 1, 4, 10, ...\n while (i <= n) {\n res += i;\n // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n i++;\n i *= 2;\n }\n return res;\n}\n
iteration.zig// while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09\nfn whileLoopII(n: i32) i32 {\n var res: i32 = 0;\n var i: i32 = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n // \u5faa\u73af\u6c42\u548c 1, 4, 10, ...\n while (i <= n) {\n res += @intCast(i);\n // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n i += 1;\n i *= 2;\n }\n return res;\n}\n
\u603b\u7684\u6765\u8bf4\uff0cfor
\u5faa\u73af\u7684\u4ee3\u7801\u66f4\u52a0\u7d27\u51d1\uff0cwhile
\u5faa\u73af\u66f4\u52a0\u7075\u6d3b\uff0c\u4e24\u8005\u90fd\u53ef\u4ee5\u5b9e\u73b0\u8fed\u4ee3\u7ed3\u6784\u3002\u9009\u62e9\u4f7f\u7528\u54ea\u4e00\u4e2a\u5e94\u8be5\u6839\u636e\u7279\u5b9a\u95ee\u9898\u7684\u9700\u6c42\u6765\u51b3\u5b9a\u3002
\u6211\u4eec\u53ef\u4ee5\u5728\u4e00\u4e2a\u5faa\u73af\u7ed3\u6784\u5185\u5d4c\u5957\u53e6\u4e00\u4e2a\u5faa\u73af\u7ed3\u6784\uff0c\u4e0b\u9762\u4ee5 for
\u5faa\u73af\u4e3a\u4f8b\uff1a
def nested_for_loop(n: int) -> str:\n \"\"\"\u53cc\u5c42 for \u5faa\u73af\"\"\"\n res = \"\"\n # \u5faa\u73af i = 1, 2, ..., n-1, n\n for i in range(1, n + 1):\n # \u5faa\u73af j = 1, 2, ..., n-1, n\n for j in range(1, n + 1):\n res += f\"({i}, {j}), \"\n return res\n
iteration.cpp/* \u53cc\u5c42 for \u5faa\u73af */\nstring nestedForLoop(int n) {\n ostringstream res;\n // \u5faa\u73af i = 1, 2, ..., n-1, n\n for (int i = 1; i <= n; ++i) {\n // \u5faa\u73af j = 1, 2, ..., n-1, n\n for (int j = 1; j <= n; ++j) {\n res << \"(\" << i << \", \" << j << \"), \";\n }\n }\n return res.str();\n}\n
iteration.java/* \u53cc\u5c42 for \u5faa\u73af */\nString nestedForLoop(int n) {\n StringBuilder res = new StringBuilder();\n // \u5faa\u73af i = 1, 2, ..., n-1, n\n for (int i = 1; i <= n; i++) {\n // \u5faa\u73af j = 1, 2, ..., n-1, n\n for (int j = 1; j <= n; j++) {\n res.append(\"(\" + i + \", \" + j + \"), \");\n }\n }\n return res.toString();\n}\n
iteration.cs/* \u53cc\u5c42 for \u5faa\u73af */\nstring NestedForLoop(int n) {\n StringBuilder res = new();\n // \u5faa\u73af i = 1, 2, ..., n-1, n\n for (int i = 1; i <= n; i++) {\n // \u5faa\u73af j = 1, 2, ..., n-1, n\n for (int j = 1; j <= n; j++) {\n res.Append($\"({i}, {j}), \");\n }\n }\n return res.ToString();\n}\n
iteration.go/* \u53cc\u5c42 for \u5faa\u73af */\nfunc nestedForLoop(n int) string {\n res := \"\"\n // \u5faa\u73af i = 1, 2, ..., n-1, n\n for i := 1; i <= n; i++ {\n for j := 1; j <= n; j++ {\n // \u5faa\u73af j = 1, 2, ..., n-1, n\n res += fmt.Sprintf(\"(%d, %d), \", i, j)\n }\n }\n return res\n}\n
iteration.swift/* \u53cc\u5c42 for \u5faa\u73af */\nfunc nestedForLoop(n: Int) -> String {\n var res = \"\"\n // \u5faa\u73af i = 1, 2, ..., n-1, n\n for i in 1 ... n {\n // \u5faa\u73af j = 1, 2, ..., n-1, n\n for j in 1 ... n {\n res.append(\"(\\(i), \\(j)), \")\n }\n }\n return res\n}\n
iteration.js/* \u53cc\u5c42 for \u5faa\u73af */\nfunction nestedForLoop(n) {\n let res = '';\n // \u5faa\u73af i = 1, 2, ..., n-1, n\n for (let i = 1; i <= n; i++) {\n // \u5faa\u73af j = 1, 2, ..., n-1, n\n for (let j = 1; j <= n; j++) {\n res += `(${i}, ${j}), `;\n }\n }\n return res;\n}\n
iteration.ts/* \u53cc\u5c42 for \u5faa\u73af */\nfunction nestedForLoop(n: number): string {\n let res = '';\n // \u5faa\u73af i = 1, 2, ..., n-1, n\n for (let i = 1; i <= n; i++) {\n // \u5faa\u73af j = 1, 2, ..., n-1, n\n for (let j = 1; j <= n; j++) {\n res += `(${i}, ${j}), `;\n }\n }\n return res;\n}\n
iteration.dart/* \u53cc\u5c42 for \u5faa\u73af */\nString nestedForLoop(int n) {\n String res = \"\";\n // \u5faa\u73af i = 1, 2, ..., n-1, n\n for (int i = 1; i <= n; i++) {\n // \u5faa\u73af j = 1, 2, ..., n-1, n\n for (int j = 1; j <= n; j++) {\n res += \"($i, $j), \";\n }\n }\n return res;\n}\n
iteration.rs/* \u53cc\u5c42 for \u5faa\u73af */\nfn nested_for_loop(n: i32) -> String {\n let mut res = vec![];\n // \u5faa\u73af i = 1, 2, ..., n-1, n\n for i in 1..=n {\n // \u5faa\u73af j = 1, 2, ..., n-1, n\n for j in 1..=n {\n res.push(format!(\"({}, {}), \", i, j));\n }\n }\n res.join(\"\")\n}\n
iteration.c/* \u53cc\u5c42 for \u5faa\u73af */\nchar *nestedForLoop(int n) {\n // n * n \u4e3a\u5bf9\u5e94\u70b9\u6570\u91cf\uff0c\"(i, j), \" \u5bf9\u5e94\u5b57\u7b26\u4e32\u957f\u6700\u5927\u4e3a 6+10*2\uff0c\u52a0\u4e0a\u6700\u540e\u4e00\u4e2a\u7a7a\u5b57\u7b26 \\0 \u7684\u989d\u5916\u7a7a\u95f4\n int size = n * n * 26 + 1;\n char *res = malloc(size * sizeof(char));\n // \u5faa\u73af i = 1, 2, ..., n-1, n\n for (int i = 1; i <= n; i++) {\n // \u5faa\u73af j = 1, 2, ..., n-1, n\n for (int j = 1; j <= n; j++) {\n char tmp[26];\n snprintf(tmp, sizeof(tmp), \"(%d, %d), \", i, j);\n strncat(res, tmp, size - strlen(res) - 1);\n }\n }\n return res;\n}\n
iteration.zig// \u53cc\u5c42 for \u5faa\u73af\nfn nestedForLoop(allocator: Allocator, n: usize) ![]const u8 {\n var res = std.ArrayList(u8).init(allocator);\n defer res.deinit();\n var buffer: [20]u8 = undefined;\n // \u5faa\u73af i = 1, 2, ..., n-1, n\n for (1..n+1) |i| {\n // \u5faa\u73af j = 1, 2, ..., n-1, n\n for (1..n+1) |j| {\n var _str = try std.fmt.bufPrint(&buffer, \"({d}, {d}), \", .{i, j});\n try res.appendSlice(_str);\n }\n }\n return res.toOwnedSlice();\n}\n
\u56fe 2-2 \u662f\u8be5\u5d4c\u5957\u5faa\u73af\u7684\u6d41\u7a0b\u6846\u56fe\u3002
\u56fe 2-2 \u00a0 \u5d4c\u5957\u5faa\u73af\u7684\u6d41\u7a0b\u6846\u56fe
\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u51fd\u6570\u7684\u64cd\u4f5c\u6570\u91cf\u4e0e \\(n^2\\) \u6210\u6b63\u6bd4\uff0c\u6216\u8005\u8bf4\u7b97\u6cd5\u8fd0\u884c\u65f6\u95f4\u548c\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u6210\u201c\u5e73\u65b9\u5173\u7cfb\u201d\u3002
\u6211\u4eec\u53ef\u4ee5\u7ee7\u7eed\u6dfb\u52a0\u5d4c\u5957\u5faa\u73af\uff0c\u6bcf\u4e00\u6b21\u5d4c\u5957\u90fd\u662f\u4e00\u6b21\u201c\u5347\u7ef4\u201d\uff0c\u5c06\u4f1a\u4f7f\u65f6\u95f4\u590d\u6742\u5ea6\u63d0\u9ad8\u81f3\u201c\u7acb\u65b9\u5173\u7cfb\u201d\u201c\u56db\u6b21\u65b9\u5173\u7cfb\u201d\uff0c\u4ee5\u6b64\u7c7b\u63a8\u3002
"},{"location":"chapter_computational_complexity/iteration_and_recursion/#222","title":"2.2.2 \u00a0 \u9012\u5f52","text":"\u300c\u9012\u5f52 recursion\u300d\u662f\u4e00\u79cd\u7b97\u6cd5\u7b56\u7565\uff0c\u901a\u8fc7\u51fd\u6570\u8c03\u7528\u81ea\u8eab\u6765\u89e3\u51b3\u95ee\u9898\u3002\u5b83\u4e3b\u8981\u5305\u542b\u4e24\u4e2a\u9636\u6bb5\u3002
\u800c\u4ece\u5b9e\u73b0\u7684\u89d2\u5ea6\u770b\uff0c\u9012\u5f52\u4ee3\u7801\u4e3b\u8981\u5305\u542b\u4e09\u4e2a\u8981\u7d20\u3002
\u89c2\u5bdf\u4ee5\u4e0b\u4ee3\u7801\uff0c\u6211\u4eec\u53ea\u9700\u8c03\u7528\u51fd\u6570 recur(n)
\uff0c\u5c31\u53ef\u4ee5\u5b8c\u6210 \\(1 + 2 + \\dots + n\\) \u7684\u8ba1\u7b97\uff1a
def recur(n: int) -> int:\n \"\"\"\u9012\u5f52\"\"\"\n # \u7ec8\u6b62\u6761\u4ef6\n if n == 1:\n return 1\n # \u9012\uff1a\u9012\u5f52\u8c03\u7528\n res = recur(n - 1)\n # \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n return n + res\n
recursion.cpp/* \u9012\u5f52 */\nint recur(int n) {\n // \u7ec8\u6b62\u6761\u4ef6\n if (n == 1)\n return 1;\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n int res = recur(n - 1);\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n return n + res;\n}\n
recursion.java/* \u9012\u5f52 */\nint recur(int n) {\n // \u7ec8\u6b62\u6761\u4ef6\n if (n == 1)\n return 1;\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n int res = recur(n - 1);\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n return n + res;\n}\n
recursion.cs/* \u9012\u5f52 */\nint Recur(int n) {\n // \u7ec8\u6b62\u6761\u4ef6\n if (n == 1)\n return 1;\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n int res = Recur(n - 1);\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n return n + res;\n}\n
recursion.go/* \u9012\u5f52 */\nfunc recur(n int) int {\n // \u7ec8\u6b62\u6761\u4ef6\n if n == 1 {\n return 1\n }\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n res := recur(n - 1)\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n return n + res\n}\n
recursion.swift/* \u9012\u5f52 */\nfunc recur(n: Int) -> Int {\n // \u7ec8\u6b62\u6761\u4ef6\n if n == 1 {\n return 1\n }\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n let res = recur(n: n - 1)\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n return n + res\n}\n
recursion.js/* \u9012\u5f52 */\nfunction recur(n) {\n // \u7ec8\u6b62\u6761\u4ef6\n if (n === 1) return 1;\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n const res = recur(n - 1);\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n return n + res;\n}\n
recursion.ts/* \u9012\u5f52 */\nfunction recur(n: number): number {\n // \u7ec8\u6b62\u6761\u4ef6\n if (n === 1) return 1;\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n const res = recur(n - 1);\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n return n + res;\n}\n
recursion.dart/* \u9012\u5f52 */\nint recur(int n) {\n // \u7ec8\u6b62\u6761\u4ef6\n if (n == 1) return 1;\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n int res = recur(n - 1);\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n return n + res;\n}\n
recursion.rs/* \u9012\u5f52 */\nfn recur(n: i32) -> i32 {\n // \u7ec8\u6b62\u6761\u4ef6\n if n == 1 {\n return 1;\n }\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n let res = recur(n - 1);\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n n + res\n}\n
recursion.c/* \u9012\u5f52 */\nint recur(int n) {\n // \u7ec8\u6b62\u6761\u4ef6\n if (n == 1)\n return 1;\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n int res = recur(n - 1);\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n return n + res;\n}\n
recursion.zig// \u9012\u5f52\u51fd\u6570\nfn recur(n: i32) i32 {\n // \u7ec8\u6b62\u6761\u4ef6\n if (n == 1) {\n return 1;\n }\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n var res: i32 = recur(n - 1);\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n return n + res;\n}\n
\u56fe 2-3 \u5c55\u793a\u4e86\u8be5\u51fd\u6570\u7684\u9012\u5f52\u8fc7\u7a0b\u3002
\u56fe 2-3 \u00a0 \u6c42\u548c\u51fd\u6570\u7684\u9012\u5f52\u8fc7\u7a0b
\u867d\u7136\u4ece\u8ba1\u7b97\u89d2\u5ea6\u770b\uff0c\u8fed\u4ee3\u4e0e\u9012\u5f52\u53ef\u4ee5\u5f97\u5230\u76f8\u540c\u7684\u7ed3\u679c\uff0c\u4f46\u5b83\u4eec\u4ee3\u8868\u4e86\u4e24\u79cd\u5b8c\u5168\u4e0d\u540c\u7684\u601d\u8003\u548c\u89e3\u51b3\u95ee\u9898\u7684\u8303\u5f0f\u3002
\u4ee5\u4e0a\u8ff0\u6c42\u548c\u51fd\u6570\u4e3a\u4f8b\uff0c\u8bbe\u95ee\u9898 \\(f(n) = 1 + 2 + \\dots + n\\) \u3002
\u9012\u5f52\u51fd\u6570\u6bcf\u6b21\u8c03\u7528\u81ea\u8eab\u65f6\uff0c\u7cfb\u7edf\u90fd\u4f1a\u4e3a\u65b0\u5f00\u542f\u7684\u51fd\u6570\u5206\u914d\u5185\u5b58\uff0c\u4ee5\u5b58\u50a8\u5c40\u90e8\u53d8\u91cf\u3001\u8c03\u7528\u5730\u5740\u548c\u5176\u4ed6\u4fe1\u606f\u7b49\u3002\u8fd9\u5c06\u5bfc\u81f4\u4e24\u65b9\u9762\u7684\u7ed3\u679c\u3002
\u5982\u56fe 2-4 \u6240\u793a\uff0c\u5728\u89e6\u53d1\u7ec8\u6b62\u6761\u4ef6\u524d\uff0c\u540c\u65f6\u5b58\u5728 \\(n\\) \u4e2a\u672a\u8fd4\u56de\u7684\u9012\u5f52\u51fd\u6570\uff0c\u9012\u5f52\u6df1\u5ea6\u4e3a \\(n\\) \u3002
\u56fe 2-4 \u00a0 \u9012\u5f52\u8c03\u7528\u6df1\u5ea6
\u5728\u5b9e\u9645\u4e2d\uff0c\u7f16\u7a0b\u8bed\u8a00\u5141\u8bb8\u7684\u9012\u5f52\u6df1\u5ea6\u901a\u5e38\u662f\u6709\u9650\u7684\uff0c\u8fc7\u6df1\u7684\u9012\u5f52\u53ef\u80fd\u5bfc\u81f4\u6808\u6ea2\u51fa\u9519\u8bef\u3002
"},{"location":"chapter_computational_complexity/iteration_and_recursion/#2","title":"2. \u00a0 \u5c3e\u9012\u5f52","text":"\u6709\u8da3\u7684\u662f\uff0c\u5982\u679c\u51fd\u6570\u5728\u8fd4\u56de\u524d\u7684\u6700\u540e\u4e00\u6b65\u624d\u8fdb\u884c\u9012\u5f52\u8c03\u7528\uff0c\u5219\u8be5\u51fd\u6570\u53ef\u4ee5\u88ab\u7f16\u8bd1\u5668\u6216\u89e3\u91ca\u5668\u4f18\u5316\uff0c\u4f7f\u5176\u5728\u7a7a\u95f4\u6548\u7387\u4e0a\u4e0e\u8fed\u4ee3\u76f8\u5f53\u3002\u8fd9\u79cd\u60c5\u51b5\u88ab\u79f0\u4e3a\u300c\u5c3e\u9012\u5f52 tail recursion\u300d\u3002
\u4ee5\u8ba1\u7b97 \\(1 + 2 + \\dots + n\\) \u4e3a\u4f8b\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u7ed3\u679c\u53d8\u91cf res
\u8bbe\u4e3a\u51fd\u6570\u53c2\u6570\uff0c\u4ece\u800c\u5b9e\u73b0\u5c3e\u9012\u5f52\uff1a
def tail_recur(n, res):\n \"\"\"\u5c3e\u9012\u5f52\"\"\"\n # \u7ec8\u6b62\u6761\u4ef6\n if n == 0:\n return res\n # \u5c3e\u9012\u5f52\u8c03\u7528\n return tail_recur(n - 1, res + n)\n
recursion.cpp/* \u5c3e\u9012\u5f52 */\nint tailRecur(int n, int res) {\n // \u7ec8\u6b62\u6761\u4ef6\n if (n == 0)\n return res;\n // \u5c3e\u9012\u5f52\u8c03\u7528\n return tailRecur(n - 1, res + n);\n}\n
recursion.java/* \u5c3e\u9012\u5f52 */\nint tailRecur(int n, int res) {\n // \u7ec8\u6b62\u6761\u4ef6\n if (n == 0)\n return res;\n // \u5c3e\u9012\u5f52\u8c03\u7528\n return tailRecur(n - 1, res + n);\n}\n
recursion.cs/* \u5c3e\u9012\u5f52 */\nint TailRecur(int n, int res) {\n // \u7ec8\u6b62\u6761\u4ef6\n if (n == 0)\n return res;\n // \u5c3e\u9012\u5f52\u8c03\u7528\n return TailRecur(n - 1, res + n);\n}\n
recursion.go/* \u5c3e\u9012\u5f52 */\nfunc tailRecur(n int, res int) int {\n // \u7ec8\u6b62\u6761\u4ef6\n if n == 0 {\n return res\n }\n // \u5c3e\u9012\u5f52\u8c03\u7528\n return tailRecur(n-1, res+n)\n}\n
recursion.swift/* \u5c3e\u9012\u5f52 */\nfunc tailRecur(n: Int, res: Int) -> Int {\n // \u7ec8\u6b62\u6761\u4ef6\n if n == 0 {\n return res\n }\n // \u5c3e\u9012\u5f52\u8c03\u7528\n return tailRecur(n: n - 1, res: res + n)\n}\n
recursion.js/* \u5c3e\u9012\u5f52 */\nfunction tailRecur(n, res) {\n // \u7ec8\u6b62\u6761\u4ef6\n if (n === 0) return res;\n // \u5c3e\u9012\u5f52\u8c03\u7528\n return tailRecur(n - 1, res + n);\n}\n
recursion.ts/* \u5c3e\u9012\u5f52 */\nfunction tailRecur(n: number, res: number): number {\n // \u7ec8\u6b62\u6761\u4ef6\n if (n === 0) return res;\n // \u5c3e\u9012\u5f52\u8c03\u7528\n return tailRecur(n - 1, res + n);\n}\n
recursion.dart/* \u5c3e\u9012\u5f52 */\nint tailRecur(int n, int res) {\n // \u7ec8\u6b62\u6761\u4ef6\n if (n == 0) return res;\n // \u5c3e\u9012\u5f52\u8c03\u7528\n return tailRecur(n - 1, res + n);\n}\n
recursion.rs/* \u5c3e\u9012\u5f52 */\nfn tail_recur(n: i32, res: i32) -> i32 {\n // \u7ec8\u6b62\u6761\u4ef6\n if n == 0 {\n return res;\n }\n // \u5c3e\u9012\u5f52\u8c03\u7528\n tail_recur(n - 1, res + n)\n}\n
recursion.c/* \u5c3e\u9012\u5f52 */\nint tailRecur(int n, int res) {\n // \u7ec8\u6b62\u6761\u4ef6\n if (n == 0)\n return res;\n // \u5c3e\u9012\u5f52\u8c03\u7528\n return tailRecur(n - 1, res + n);\n}\n
recursion.zig// \u5c3e\u9012\u5f52\u51fd\u6570\nfn tailRecur(n: i32, res: i32) i32 {\n // \u7ec8\u6b62\u6761\u4ef6\n if (n == 0) {\n return res;\n }\n // \u5c3e\u9012\u5f52\u8c03\u7528\n return tailRecur(n - 1, res + n);\n}\n
\u5c3e\u9012\u5f52\u7684\u6267\u884c\u8fc7\u7a0b\u5982\u56fe 2-5 \u6240\u793a\u3002\u5bf9\u6bd4\u666e\u901a\u9012\u5f52\u548c\u5c3e\u9012\u5f52\uff0c\u4e24\u8005\u7684\u6c42\u548c\u64cd\u4f5c\u7684\u6267\u884c\u70b9\u662f\u4e0d\u540c\u7684\u3002
\u56fe 2-5 \u00a0 \u5c3e\u9012\u5f52\u8fc7\u7a0b
Tip
\u8bf7\u6ce8\u610f\uff0c\u8bb8\u591a\u7f16\u8bd1\u5668\u6216\u89e3\u91ca\u5668\u5e76\u4e0d\u652f\u6301\u5c3e\u9012\u5f52\u4f18\u5316\u3002\u4f8b\u5982\uff0cPython \u9ed8\u8ba4\u4e0d\u652f\u6301\u5c3e\u9012\u5f52\u4f18\u5316\uff0c\u56e0\u6b64\u5373\u4f7f\u51fd\u6570\u662f\u5c3e\u9012\u5f52\u5f62\u5f0f\uff0c\u4ecd\u7136\u53ef\u80fd\u4f1a\u9047\u5230\u6808\u6ea2\u51fa\u95ee\u9898\u3002
"},{"location":"chapter_computational_complexity/iteration_and_recursion/#3_1","title":"3. \u00a0 \u9012\u5f52\u6811","text":"\u5f53\u5904\u7406\u4e0e\u201c\u5206\u6cbb\u201d\u76f8\u5173\u7684\u7b97\u6cd5\u95ee\u9898\u65f6\uff0c\u9012\u5f52\u5f80\u5f80\u6bd4\u8fed\u4ee3\u7684\u601d\u8def\u66f4\u52a0\u76f4\u89c2\u3001\u4ee3\u7801\u66f4\u52a0\u6613\u8bfb\u3002\u4ee5\u201c\u6590\u6ce2\u90a3\u5951\u6570\u5217\u201d\u4e3a\u4f8b\u3002
Question
\u7ed9\u5b9a\u4e00\u4e2a\u6590\u6ce2\u90a3\u5951\u6570\u5217 \\(0, 1, 1, 2, 3, 5, 8, 13, \\dots\\) \uff0c\u6c42\u8be5\u6570\u5217\u7684\u7b2c \\(n\\) \u4e2a\u6570\u5b57\u3002
\u8bbe\u6590\u6ce2\u90a3\u5951\u6570\u5217\u7684\u7b2c \\(n\\) \u4e2a\u6570\u5b57\u4e3a \\(f(n)\\) \uff0c\u6613\u5f97\u4e24\u4e2a\u7ed3\u8bba\u3002
\u6309\u7167\u9012\u63a8\u5173\u7cfb\u8fdb\u884c\u9012\u5f52\u8c03\u7528\uff0c\u5c06\u524d\u4e24\u4e2a\u6570\u5b57\u4f5c\u4e3a\u7ec8\u6b62\u6761\u4ef6\uff0c\u4fbf\u53ef\u5199\u51fa\u9012\u5f52\u4ee3\u7801\u3002\u8c03\u7528 fib(n)
\u5373\u53ef\u5f97\u5230\u6590\u6ce2\u90a3\u5951\u6570\u5217\u7684\u7b2c \\(n\\) \u4e2a\u6570\u5b57\uff1a
def fib(n: int) -> int:\n \"\"\"\u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52\"\"\"\n # \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\n if n == 1 or n == 2:\n return n - 1\n # \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\n res = fib(n - 1) + fib(n - 2)\n # \u8fd4\u56de\u7ed3\u679c f(n)\n return res\n
recursion.cpp/* \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 */\nint fib(int n) {\n // \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\n if (n == 1 || n == 2)\n return n - 1;\n // \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\n int res = fib(n - 1) + fib(n - 2);\n // \u8fd4\u56de\u7ed3\u679c f(n)\n return res;\n}\n
recursion.java/* \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 */\nint fib(int n) {\n // \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\n if (n == 1 || n == 2)\n return n - 1;\n // \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\n int res = fib(n - 1) + fib(n - 2);\n // \u8fd4\u56de\u7ed3\u679c f(n)\n return res;\n}\n
recursion.cs/* \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 */\nint Fib(int n) {\n // \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\n if (n == 1 || n == 2)\n return n - 1;\n // \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\n int res = Fib(n - 1) + Fib(n - 2);\n // \u8fd4\u56de\u7ed3\u679c f(n)\n return res;\n}\n
recursion.go/* \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 */\nfunc fib(n int) int {\n // \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\n if n == 1 || n == 2 {\n return n - 1\n }\n // \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\n res := fib(n-1) + fib(n-2)\n // \u8fd4\u56de\u7ed3\u679c f(n)\n return res\n}\n
recursion.swift/* \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 */\nfunc fib(n: Int) -> Int {\n // \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\n if n == 1 || n == 2 {\n return n - 1\n }\n // \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\n let res = fib(n: n - 1) + fib(n: n - 2)\n // \u8fd4\u56de\u7ed3\u679c f(n)\n return res\n}\n
recursion.js/* \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 */\nfunction fib(n) {\n // \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\n if (n === 1 || n === 2) return n - 1;\n // \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\n const res = fib(n - 1) + fib(n - 2);\n // \u8fd4\u56de\u7ed3\u679c f(n)\n return res;\n}\n
recursion.ts/* \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 */\nfunction fib(n: number): number {\n // \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\n if (n === 1 || n === 2) return n - 1;\n // \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\n const res = fib(n - 1) + fib(n - 2);\n // \u8fd4\u56de\u7ed3\u679c f(n)\n return res;\n}\n
recursion.dart/* \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 */\nint fib(int n) {\n // \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\n if (n == 1 || n == 2) return n - 1;\n // \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\n int res = fib(n - 1) + fib(n - 2);\n // \u8fd4\u56de\u7ed3\u679c f(n)\n return res;\n}\n
recursion.rs/* \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 */\nfn fib(n: i32) -> i32 {\n // \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\n if n == 1 || n == 2 {\n return n - 1;\n }\n // \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\n let res = fib(n - 1) + fib(n - 2);\n // \u8fd4\u56de\u7ed3\u679c\n res\n}\n
recursion.c/* \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 */\nint fib(int n) {\n // \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\n if (n == 1 || n == 2)\n return n - 1;\n // \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\n int res = fib(n - 1) + fib(n - 2);\n // \u8fd4\u56de\u7ed3\u679c f(n)\n return res;\n}\n
recursion.zig// \u6590\u6ce2\u90a3\u5951\u6570\u5217\nfn fib(n: i32) i32 {\n // \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\n if (n == 1 or n == 2) {\n return n - 1;\n }\n // \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\n var res: i32 = fib(n - 1) + fib(n - 2);\n // \u8fd4\u56de\u7ed3\u679c f(n)\n return res;\n}\n
\u89c2\u5bdf\u4ee5\u4e0a\u4ee3\u7801\uff0c\u6211\u4eec\u5728\u51fd\u6570\u5185\u9012\u5f52\u8c03\u7528\u4e86\u4e24\u4e2a\u51fd\u6570\uff0c\u8fd9\u610f\u5473\u7740\u4ece\u4e00\u4e2a\u8c03\u7528\u4ea7\u751f\u4e86\u4e24\u4e2a\u8c03\u7528\u5206\u652f\u3002\u5982\u56fe 2-6 \u6240\u793a\uff0c\u8fd9\u6837\u4e0d\u65ad\u9012\u5f52\u8c03\u7528\u4e0b\u53bb\uff0c\u6700\u7ec8\u5c06\u4ea7\u751f\u4e00\u68f5\u5c42\u6570\u4e3a \\(n\\) \u7684\u300c\u9012\u5f52\u6811 recursion tree\u300d\u3002
\u56fe 2-6 \u00a0 \u6590\u6ce2\u90a3\u5951\u6570\u5217\u7684\u9012\u5f52\u6811
\u4ece\u672c\u8d28\u4e0a\u770b\uff0c\u9012\u5f52\u4f53\u73b0\u4e86\u201c\u5c06\u95ee\u9898\u5206\u89e3\u4e3a\u66f4\u5c0f\u5b50\u95ee\u9898\u201d\u7684\u601d\u7ef4\u8303\u5f0f\uff0c\u8fd9\u79cd\u5206\u6cbb\u7b56\u7565\u81f3\u5173\u91cd\u8981\u3002
\u603b\u7ed3\u4ee5\u4e0a\u5185\u5bb9\uff0c\u5982\u8868 2-1 \u6240\u793a\uff0c\u8fed\u4ee3\u548c\u9012\u5f52\u5728\u5b9e\u73b0\u3001\u6027\u80fd\u548c\u9002\u7528\u6027\u4e0a\u6709\u6240\u4e0d\u540c\u3002
\u8868 2-1 \u00a0 \u8fed\u4ee3\u4e0e\u9012\u5f52\u7279\u70b9\u5bf9\u6bd4
\u8fed\u4ee3 \u9012\u5f52 \u5b9e\u73b0\u65b9\u5f0f \u5faa\u73af\u7ed3\u6784 \u51fd\u6570\u8c03\u7528\u81ea\u8eab \u65f6\u95f4\u6548\u7387 \u6548\u7387\u901a\u5e38\u8f83\u9ad8\uff0c\u65e0\u51fd\u6570\u8c03\u7528\u5f00\u9500 \u6bcf\u6b21\u51fd\u6570\u8c03\u7528\u90fd\u4f1a\u4ea7\u751f\u5f00\u9500 \u5185\u5b58\u4f7f\u7528 \u901a\u5e38\u4f7f\u7528\u56fa\u5b9a\u5927\u5c0f\u7684\u5185\u5b58\u7a7a\u95f4 \u7d2f\u79ef\u51fd\u6570\u8c03\u7528\u53ef\u80fd\u4f7f\u7528\u5927\u91cf\u7684\u6808\u5e27\u7a7a\u95f4 \u9002\u7528\u95ee\u9898 \u9002\u7528\u4e8e\u7b80\u5355\u5faa\u73af\u4efb\u52a1\uff0c\u4ee3\u7801\u76f4\u89c2\u3001\u53ef\u8bfb\u6027\u597d \u9002\u7528\u4e8e\u5b50\u95ee\u9898\u5206\u89e3\uff0c\u5982\u6811\u3001\u56fe\u3001\u5206\u6cbb\u3001\u56de\u6eaf\u7b49\uff0c\u4ee3\u7801\u7ed3\u6784\u7b80\u6d01\u3001\u6e05\u6670Tip
\u5982\u679c\u611f\u89c9\u4ee5\u4e0b\u5185\u5bb9\u7406\u89e3\u56f0\u96be\uff0c\u53ef\u4ee5\u5728\u8bfb\u5b8c\u201c\u6808\u201d\u7ae0\u8282\u540e\u518d\u6765\u590d\u4e60\u3002
\u90a3\u4e48\uff0c\u8fed\u4ee3\u548c\u9012\u5f52\u5177\u6709\u4ec0\u4e48\u5185\u5728\u8054\u7cfb\u5462\uff1f\u4ee5\u4e0a\u8ff0\u7684\u9012\u5f52\u51fd\u6570\u4e3a\u4f8b\uff0c\u6c42\u548c\u64cd\u4f5c\u5728\u9012\u5f52\u7684\u201c\u5f52\u201d\u9636\u6bb5\u8fdb\u884c\u3002\u8fd9\u610f\u5473\u7740\u6700\u521d\u88ab\u8c03\u7528\u7684\u51fd\u6570\u5b9e\u9645\u4e0a\u662f\u6700\u540e\u5b8c\u6210\u5176\u6c42\u548c\u64cd\u4f5c\u7684\uff0c\u8fd9\u79cd\u5de5\u4f5c\u673a\u5236\u4e0e\u6808\u7684\u201c\u5148\u5165\u540e\u51fa\u201d\u539f\u5219\u662f\u5f02\u66f2\u540c\u5de5\u7684\u3002
\u4e8b\u5b9e\u4e0a\uff0c\u201c\u8c03\u7528\u6808\u201d\u548c\u201c\u6808\u5e27\u7a7a\u95f4\u201d\u8fd9\u7c7b\u9012\u5f52\u672f\u8bed\u5df2\u7ecf\u6697\u793a\u4e86\u9012\u5f52\u4e0e\u6808\u4e4b\u95f4\u7684\u5bc6\u5207\u5173\u7cfb\u3002
\u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u8c03\u7528\u6808\u7684\u884c\u4e3a\uff0c\u4ece\u800c\u5c06\u9012\u5f52\u8f6c\u5316\u4e3a\u8fed\u4ee3\u5f62\u5f0f\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig recursion.pydef for_loop_recur(n: int) -> int:\n \"\"\"\u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52\"\"\"\n # \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\n stack = []\n res = 0\n # \u9012\uff1a\u9012\u5f52\u8c03\u7528\n for i in range(n, 0, -1):\n # \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\n stack.append(i)\n # \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n while stack:\n # \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\n res += stack.pop()\n # res = 1+2+3+...+n\n return res\n
recursion.cpp/* \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 */\nint forLoopRecur(int n) {\n // \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\n stack<int> stack;\n int res = 0;\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n for (int i = n; i > 0; i--) {\n // \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\n stack.push(i);\n }\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n while (!stack.empty()) {\n // \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\n res += stack.top();\n stack.pop();\n }\n // res = 1+2+3+...+n\n return res;\n}\n
recursion.java/* \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 */\nint forLoopRecur(int n) {\n // \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\n Stack<Integer> stack = new Stack<>();\n int res = 0;\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n for (int i = n; i > 0; i--) {\n // \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\n stack.push(i);\n }\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n while (!stack.isEmpty()) {\n // \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\n res += stack.pop();\n }\n // res = 1+2+3+...+n\n return res;\n}\n
recursion.cs/* \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 */\nint ForLoopRecur(int n) {\n // \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\n Stack<int> stack = new();\n int res = 0;\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n for (int i = n; i > 0; i--) {\n // \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\n stack.Push(i);\n }\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n while (stack.Count > 0) {\n // \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\n res += stack.Pop();\n }\n // res = 1+2+3+...+n\n return res;\n}\n
recursion.go/* \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 */\nfunc forLoopRecur(n int) int {\n // \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\n stack := list.New()\n res := 0\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n for i := n; i > 0; i-- {\n // \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\n stack.PushBack(i)\n }\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n for stack.Len() != 0 {\n // \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\n res += stack.Back().Value.(int)\n stack.Remove(stack.Back())\n }\n // res = 1+2+3+...+n\n return res\n}\n
recursion.swift/* \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 */\nfunc forLoopRecur(n: Int) -> Int {\n // \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\n var stack: [Int] = []\n var res = 0\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n for i in stride(from: n, to: 0, by: -1) {\n // \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\n stack.append(i)\n }\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n while !stack.isEmpty {\n // \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\n res += stack.removeLast()\n }\n // res = 1+2+3+...+n\n return res\n}\n
recursion.js/* \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 */\nfunction forLoopRecur(n) {\n // \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\n const stack = [];\n let res = 0;\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n for (let i = 1; i <= n; i++) {\n // \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\n stack.push(i);\n }\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n while (stack.length) { \n // \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\n res += stack.pop();\n }\n // res = 1+2+3+...+n\n return res;\n}\n
recursion.ts/* \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 */\nfunction forLoopRecur(n: number): number {\n // \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808 \n const stack: number[] = [];\n let res: number = 0;\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n for (let i = 1; i <= n; i++) {\n // \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\n stack.push(i);\n }\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n while (stack.length) { \n // \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\n res += stack.pop();\n }\n // res = 1+2+3+...+n\n return res;\n}\n
recursion.dart/* \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 */\nint forLoopRecur(int n) {\n // \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\n List<int> stack = [];\n int res = 0;\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n for (int i = n; i > 0; i--) {\n // \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\n stack.add(i);\n }\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n while (!stack.isEmpty) {\n // \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\n res += stack.removeLast();\n }\n // res = 1+2+3+...+n\n return res;\n}\n
recursion.rs/* \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 */\nfn for_loop_recur(n: i32) -> i32 {\n // \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\n let mut stack = Vec::new();\n let mut res = 0;\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n for i in (1..=n).rev() {\n // \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\n stack.push(i);\n }\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n while !stack.is_empty() {\n // \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\n res += stack.pop().unwrap();\n }\n // res = 1+2+3+...+n\n res\n}\n
recursion.c/* \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 */\nint forLoopRecur(int n) {\n int stack[1000]; // \u501f\u52a9\u4e00\u4e2a\u5927\u6570\u7ec4\u6765\u6a21\u62df\u6808\n int top = -1; // \u6808\u9876\u7d22\u5f15\n int res = 0;\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n for (int i = n; i > 0; i--) {\n // \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\n stack[1 + top++] = i;\n }\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n while (top >= 0) {\n // \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\n res += stack[top--];\n }\n // res = 1+2+3+...+n\n return res;\n}\n
recursion.zig// \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52\nfn forLoopRecur(comptime n: i32) i32 {\n // \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\n var stack: [n]i32 = undefined;\n var res: i32 = 0;\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n var i: usize = n;\n while (i > 0) {\n stack[i - 1] = @intCast(i);\n i -= 1;\n }\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n var index: usize = n;\n while (index > 0) {\n index -= 1;\n res += stack[index];\n }\n // res = 1+2+3+...+n\n return res;\n}\n
\u89c2\u5bdf\u4ee5\u4e0a\u4ee3\u7801\uff0c\u5f53\u9012\u5f52\u88ab\u8f6c\u6362\u4e3a\u8fed\u4ee3\u540e\uff0c\u4ee3\u7801\u53d8\u5f97\u66f4\u52a0\u590d\u6742\u4e86\u3002\u5c3d\u7ba1\u8fed\u4ee3\u548c\u9012\u5f52\u5728\u5f88\u591a\u60c5\u51b5\u4e0b\u53ef\u4ee5\u4e92\u76f8\u8f6c\u6362\uff0c\u4f46\u4e5f\u4e0d\u4e00\u5b9a\u503c\u5f97\u8fd9\u6837\u505a\uff0c\u6709\u4ee5\u4e0b\u4e24\u70b9\u539f\u56e0\u3002
\u603b\u4e4b\uff0c\u9009\u62e9\u8fed\u4ee3\u8fd8\u662f\u9012\u5f52\u53d6\u51b3\u4e8e\u7279\u5b9a\u95ee\u9898\u7684\u6027\u8d28\u3002\u5728\u7f16\u7a0b\u5b9e\u8df5\u4e2d\uff0c\u6743\u8861\u4e24\u8005\u7684\u4f18\u52a3\u5e76\u6839\u636e\u60c5\u5883\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u662f\u81f3\u5173\u91cd\u8981\u7684\u3002
"},{"location":"chapter_computational_complexity/performance_evaluation/","title":"2.1 \u00a0 \u7b97\u6cd5\u6548\u7387\u8bc4\u4f30","text":"\u5728\u7b97\u6cd5\u8bbe\u8ba1\u4e2d\uff0c\u6211\u4eec\u5148\u540e\u8ffd\u6c42\u4ee5\u4e0b\u4e24\u4e2a\u5c42\u9762\u7684\u76ee\u6807\u3002
\u4e5f\u5c31\u662f\u8bf4\uff0c\u5728\u80fd\u591f\u89e3\u51b3\u95ee\u9898\u7684\u524d\u63d0\u4e0b\uff0c\u7b97\u6cd5\u6548\u7387\u5df2\u6210\u4e3a\u8861\u91cf\u7b97\u6cd5\u4f18\u52a3\u7684\u4e3b\u8981\u8bc4\u4ef7\u6307\u6807\uff0c\u5b83\u5305\u62ec\u4ee5\u4e0b\u4e24\u4e2a\u7ef4\u5ea6\u3002
\u7b80\u800c\u8a00\u4e4b\uff0c\u6211\u4eec\u7684\u76ee\u6807\u662f\u8bbe\u8ba1\u201c\u65e2\u5feb\u53c8\u7701\u201d\u7684\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u3002\u800c\u6709\u6548\u5730\u8bc4\u4f30\u7b97\u6cd5\u6548\u7387\u81f3\u5173\u91cd\u8981\uff0c\u56e0\u4e3a\u53ea\u6709\u8fd9\u6837\u6211\u4eec\u624d\u80fd\u5c06\u5404\u79cd\u7b97\u6cd5\u8fdb\u884c\u5bf9\u6bd4\uff0c\u8fdb\u800c\u6307\u5bfc\u7b97\u6cd5\u8bbe\u8ba1\u4e0e\u4f18\u5316\u8fc7\u7a0b\u3002
\u6548\u7387\u8bc4\u4f30\u65b9\u6cd5\u4e3b\u8981\u5206\u4e3a\u4e24\u79cd\uff1a\u5b9e\u9645\u6d4b\u8bd5\u3001\u7406\u8bba\u4f30\u7b97\u3002
"},{"location":"chapter_computational_complexity/performance_evaluation/#211","title":"2.1.1 \u00a0 \u5b9e\u9645\u6d4b\u8bd5","text":"\u5047\u8bbe\u6211\u4eec\u73b0\u5728\u6709\u7b97\u6cd5 A
\u548c\u7b97\u6cd5 B
\uff0c\u5b83\u4eec\u90fd\u80fd\u89e3\u51b3\u540c\u4e00\u95ee\u9898\uff0c\u73b0\u5728\u9700\u8981\u5bf9\u6bd4\u8fd9\u4e24\u4e2a\u7b97\u6cd5\u7684\u6548\u7387\u3002\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u662f\u627e\u4e00\u53f0\u8ba1\u7b97\u673a\uff0c\u8fd0\u884c\u8fd9\u4e24\u4e2a\u7b97\u6cd5\uff0c\u5e76\u76d1\u63a7\u8bb0\u5f55\u5b83\u4eec\u7684\u8fd0\u884c\u65f6\u95f4\u548c\u5185\u5b58\u5360\u7528\u60c5\u51b5\u3002\u8fd9\u79cd\u8bc4\u4f30\u65b9\u5f0f\u80fd\u591f\u53cd\u6620\u771f\u5b9e\u60c5\u51b5\uff0c\u4f46\u4e5f\u5b58\u5728\u8f83\u5927\u7684\u5c40\u9650\u6027\u3002
\u4e00\u65b9\u9762\uff0c\u96be\u4ee5\u6392\u9664\u6d4b\u8bd5\u73af\u5883\u7684\u5e72\u6270\u56e0\u7d20\u3002\u786c\u4ef6\u914d\u7f6e\u4f1a\u5f71\u54cd\u7b97\u6cd5\u7684\u6027\u80fd\u3002\u6bd4\u5982\u5728\u67d0\u53f0\u8ba1\u7b97\u673a\u4e2d\uff0c\u7b97\u6cd5 A
\u7684\u8fd0\u884c\u65f6\u95f4\u6bd4\u7b97\u6cd5 B
\u77ed\uff1b\u4f46\u5728\u53e6\u4e00\u53f0\u914d\u7f6e\u4e0d\u540c\u7684\u8ba1\u7b97\u673a\u4e2d\uff0c\u53ef\u80fd\u5f97\u5230\u76f8\u53cd\u7684\u6d4b\u8bd5\u7ed3\u679c\u3002\u8fd9\u610f\u5473\u7740\u6211\u4eec\u9700\u8981\u5728\u5404\u79cd\u673a\u5668\u4e0a\u8fdb\u884c\u6d4b\u8bd5\uff0c\u7edf\u8ba1\u5e73\u5747\u6548\u7387\uff0c\u800c\u8fd9\u662f\u4e0d\u73b0\u5b9e\u7684\u3002
\u53e6\u4e00\u65b9\u9762\uff0c\u5c55\u5f00\u5b8c\u6574\u6d4b\u8bd5\u975e\u5e38\u8017\u8d39\u8d44\u6e90\u3002\u968f\u7740\u8f93\u5165\u6570\u636e\u91cf\u7684\u53d8\u5316\uff0c\u7b97\u6cd5\u4f1a\u8868\u73b0\u51fa\u4e0d\u540c\u7684\u6548\u7387\u3002\u4f8b\u5982\uff0c\u5728\u8f93\u5165\u6570\u636e\u91cf\u8f83\u5c0f\u65f6\uff0c\u7b97\u6cd5 A
\u7684\u8fd0\u884c\u65f6\u95f4\u6bd4\u7b97\u6cd5 B
\u77ed\uff1b\u800c\u5728\u8f93\u5165\u6570\u636e\u91cf\u8f83\u5927\u65f6\uff0c\u6d4b\u8bd5\u7ed3\u679c\u53ef\u80fd\u6070\u6070\u76f8\u53cd\u3002\u56e0\u6b64\uff0c\u4e3a\u4e86\u5f97\u5230\u6709\u8bf4\u670d\u529b\u7684\u7ed3\u8bba\uff0c\u6211\u4eec\u9700\u8981\u6d4b\u8bd5\u5404\u79cd\u89c4\u6a21\u7684\u8f93\u5165\u6570\u636e\uff0c\u800c\u8fd9\u9700\u8981\u8017\u8d39\u5927\u91cf\u7684\u8ba1\u7b97\u8d44\u6e90\u3002
\u7531\u4e8e\u5b9e\u9645\u6d4b\u8bd5\u5177\u6709\u8f83\u5927\u7684\u5c40\u9650\u6027\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u8003\u8651\u4ec5\u901a\u8fc7\u4e00\u4e9b\u8ba1\u7b97\u6765\u8bc4\u4f30\u7b97\u6cd5\u7684\u6548\u7387\u3002\u8fd9\u79cd\u4f30\u7b97\u65b9\u6cd5\u88ab\u79f0\u4e3a\u300c\u6e10\u8fd1\u590d\u6742\u5ea6\u5206\u6790 asymptotic complexity analysis\u300d\uff0c\u7b80\u79f0\u300c\u590d\u6742\u5ea6\u5206\u6790\u300d\u3002
\u590d\u6742\u5ea6\u5206\u6790\u80fd\u591f\u4f53\u73b0\u7b97\u6cd5\u8fd0\u884c\u6240\u9700\u7684\u65f6\u95f4\u548c\u7a7a\u95f4\u8d44\u6e90\u4e0e\u8f93\u5165\u6570\u636e\u5927\u5c0f\u4e4b\u95f4\u7684\u5173\u7cfb\u3002\u5b83\u63cf\u8ff0\u4e86\u968f\u7740\u8f93\u5165\u6570\u636e\u5927\u5c0f\u7684\u589e\u52a0\uff0c\u7b97\u6cd5\u6267\u884c\u6240\u9700\u65f6\u95f4\u548c\u7a7a\u95f4\u7684\u589e\u957f\u8d8b\u52bf\u3002\u8fd9\u4e2a\u5b9a\u4e49\u6709\u4e9b\u62d7\u53e3\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5176\u5206\u4e3a\u4e09\u4e2a\u91cd\u70b9\u6765\u7406\u89e3\u3002
\u590d\u6742\u5ea6\u5206\u6790\u514b\u670d\u4e86\u5b9e\u9645\u6d4b\u8bd5\u65b9\u6cd5\u7684\u5f0a\u7aef\uff0c\u4f53\u73b0\u5728\u4ee5\u4e0b\u4e24\u4e2a\u65b9\u9762\u3002
Tip
\u5982\u679c\u4f60\u4ecd\u5bf9\u590d\u6742\u5ea6\u7684\u6982\u5ff5\u611f\u5230\u56f0\u60d1\uff0c\u65e0\u987b\u62c5\u5fc3\uff0c\u6211\u4eec\u4f1a\u5728\u540e\u7eed\u7ae0\u8282\u4e2d\u8be6\u7ec6\u4ecb\u7ecd\u3002
\u590d\u6742\u5ea6\u5206\u6790\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u4e00\u628a\u8bc4\u4f30\u7b97\u6cd5\u6548\u7387\u7684\u201c\u6807\u5c3a\u201d\uff0c\u4f7f\u6211\u4eec\u53ef\u4ee5\u8861\u91cf\u6267\u884c\u67d0\u4e2a\u7b97\u6cd5\u6240\u9700\u7684\u65f6\u95f4\u548c\u7a7a\u95f4\u8d44\u6e90\uff0c\u5bf9\u6bd4\u4e0d\u540c\u7b97\u6cd5\u4e4b\u95f4\u7684\u6548\u7387\u3002
\u590d\u6742\u5ea6\u662f\u4e2a\u6570\u5b66\u6982\u5ff5\uff0c\u5bf9\u4e8e\u521d\u5b66\u8005\u53ef\u80fd\u6bd4\u8f83\u62bd\u8c61\uff0c\u5b66\u4e60\u96be\u5ea6\u76f8\u5bf9\u8f83\u9ad8\u3002\u4ece\u8fd9\u4e2a\u89d2\u5ea6\u770b\uff0c\u590d\u6742\u5ea6\u5206\u6790\u53ef\u80fd\u4e0d\u592a\u9002\u5408\u4f5c\u4e3a\u6700\u5148\u4ecb\u7ecd\u7684\u5185\u5bb9\u3002\u7136\u800c\uff0c\u5f53\u6211\u4eec\u8ba8\u8bba\u67d0\u4e2a\u6570\u636e\u7ed3\u6784\u6216\u7b97\u6cd5\u7684\u7279\u70b9\u65f6\uff0c\u96be\u4ee5\u907f\u514d\u8981\u5206\u6790\u5176\u8fd0\u884c\u901f\u5ea6\u548c\u7a7a\u95f4\u4f7f\u7528\u60c5\u51b5\u3002
\u7efc\u4e0a\u6240\u8ff0\uff0c\u5efa\u8bae\u4f60\u5728\u6df1\u5165\u5b66\u4e60\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u4e4b\u524d\uff0c\u5148\u5bf9\u590d\u6742\u5ea6\u5206\u6790\u5efa\u7acb\u521d\u6b65\u7684\u4e86\u89e3\uff0c\u4ee5\u4fbf\u80fd\u591f\u5b8c\u6210\u7b80\u5355\u7b97\u6cd5\u7684\u590d\u6742\u5ea6\u5206\u6790\u3002
"},{"location":"chapter_computational_complexity/space_complexity/","title":"2.4 \u00a0 \u7a7a\u95f4\u590d\u6742\u5ea6","text":"\u300c\u7a7a\u95f4\u590d\u6742\u5ea6 space complexity\u300d\u7528\u4e8e\u8861\u91cf\u7b97\u6cd5\u5360\u7528\u5185\u5b58\u7a7a\u95f4\u968f\u7740\u6570\u636e\u91cf\u53d8\u5927\u65f6\u7684\u589e\u957f\u8d8b\u52bf\u3002\u8fd9\u4e2a\u6982\u5ff5\u4e0e\u65f6\u95f4\u590d\u6742\u5ea6\u975e\u5e38\u7c7b\u4f3c\uff0c\u53ea\u9700\u5c06\u201c\u8fd0\u884c\u65f6\u95f4\u201d\u66ff\u6362\u4e3a\u201c\u5360\u7528\u5185\u5b58\u7a7a\u95f4\u201d\u3002
"},{"location":"chapter_computational_complexity/space_complexity/#241","title":"2.4.1 \u00a0 \u7b97\u6cd5\u76f8\u5173\u7a7a\u95f4","text":"\u7b97\u6cd5\u5728\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u4f7f\u7528\u7684\u5185\u5b58\u7a7a\u95f4\u4e3b\u8981\u5305\u62ec\u4ee5\u4e0b\u51e0\u79cd\u3002
\u4e00\u822c\u60c5\u51b5\u4e0b\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u7684\u7edf\u8ba1\u8303\u56f4\u662f\u201c\u6682\u5b58\u7a7a\u95f4\u201d\u52a0\u4e0a\u201c\u8f93\u51fa\u7a7a\u95f4\u201d\u3002
\u6682\u5b58\u7a7a\u95f4\u53ef\u4ee5\u8fdb\u4e00\u6b65\u5212\u5206\u4e3a\u4e09\u4e2a\u90e8\u5206\u3002
\u5728\u5206\u6790\u4e00\u6bb5\u7a0b\u5e8f\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u65f6\uff0c\u6211\u4eec\u901a\u5e38\u7edf\u8ba1\u6682\u5b58\u6570\u636e\u3001\u6808\u5e27\u7a7a\u95f4\u548c\u8f93\u51fa\u6570\u636e\u4e09\u90e8\u5206\uff0c\u5982\u56fe 2-15 \u6240\u793a\u3002
\u56fe 2-15 \u00a0 \u7b97\u6cd5\u4f7f\u7528\u7684\u76f8\u5173\u7a7a\u95f4
\u76f8\u5173\u4ee3\u7801\u5982\u4e0b\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZigclass Node:\n \"\"\"\u7c7b\"\"\"\n def __init__(self, x: int):\n self.val: int = x # \u8282\u70b9\u503c\n self.next: Node | None = None # \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u5f15\u7528\n\ndef function() -> int:\n \"\"\"\u51fd\u6570\"\"\"\n # \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c...\n return 0\n\ndef algorithm(n) -> int: # \u8f93\u5165\u6570\u636e\n A = 0 # \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff0c\u4e00\u822c\u7528\u5927\u5199\u5b57\u6bcd\u8868\u793a\uff09\n b = 0 # \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\n node = Node(0) # \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\n c = function() # \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\n return A + b + c # \u8f93\u51fa\u6570\u636e\n
/* \u7ed3\u6784\u4f53 */\nstruct Node {\n int val;\n Node *next;\n Node(int x) : val(x), next(nullptr) {}\n};\n\n/* \u51fd\u6570 */\nint func() {\n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c...\n return 0;\n}\n\nint algorithm(int n) { // \u8f93\u5165\u6570\u636e\n const int a = 0; // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\n int b = 0; // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\n Node* node = new Node(0); // \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\n int c = func(); // \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\n return a + b + c; // \u8f93\u51fa\u6570\u636e\n}\n
/* \u7c7b */\nclass Node {\n int val;\n Node next;\n Node(int x) { val = x; }\n}\n\n/* \u51fd\u6570 */\nint function() {\n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c...\n return 0;\n}\n\nint algorithm(int n) { // \u8f93\u5165\u6570\u636e\n final int a = 0; // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\n int b = 0; // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\n Node node = new Node(0); // \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\n int c = function(); // \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\n return a + b + c; // \u8f93\u51fa\u6570\u636e\n}\n
/* \u7c7b */\nclass Node(int x) {\n int val = x;\n Node next;\n}\n\n/* \u51fd\u6570 */\nint Function() {\n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c...\n return 0;\n}\n\nint Algorithm(int n) { // \u8f93\u5165\u6570\u636e\n const int a = 0; // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\n int b = 0; // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\n Node node = new(0); // \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\n int c = Function(); // \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\n return a + b + c; // \u8f93\u51fa\u6570\u636e\n}\n
/* \u7ed3\u6784\u4f53 */\ntype node struct {\n val int\n next *node\n}\n\n/* \u521b\u5efa node \u7ed3\u6784\u4f53 */\nfunc newNode(val int) *node {\n return &node{val: val}\n}\n\n/* \u51fd\u6570 */\nfunc function() int {\n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c...\n return 0\n}\n\nfunc algorithm(n int) int { // \u8f93\u5165\u6570\u636e\n const a = 0 // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\n b := 0 // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\n newNode(0) // \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\n c := function() // \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\n return a + b + c // \u8f93\u51fa\u6570\u636e\n}\n
/* \u7c7b */\nclass Node {\n var val: Int\n var next: Node?\n\n init(x: Int) {\n val = x\n }\n}\n\n/* \u51fd\u6570 */\nfunc function() -> Int {\n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c...\n return 0\n}\n\nfunc algorithm(n: Int) -> Int { // \u8f93\u5165\u6570\u636e\n let a = 0 // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\n var b = 0 // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\n let node = Node(x: 0) // \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\n let c = function() // \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\n return a + b + c // \u8f93\u51fa\u6570\u636e\n}\n
/* \u7c7b */\nclass Node {\n val;\n next;\n constructor(val) {\n this.val = val === undefined ? 0 : val; // \u8282\u70b9\u503c\n this.next = null; // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u5f15\u7528\n }\n}\n\n/* \u51fd\u6570 */\nfunction constFunc() {\n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0;\n}\n\nfunction algorithm(n) { // \u8f93\u5165\u6570\u636e\n const a = 0; // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\n let b = 0; // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\n const node = new Node(0); // \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\n const c = constFunc(); // \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\n return a + b + c; // \u8f93\u51fa\u6570\u636e\n}\n
/* \u7c7b */\nclass Node {\n val: number;\n next: Node | null;\n constructor(val?: number) {\n this.val = val === undefined ? 0 : val; // \u8282\u70b9\u503c\n this.next = null; // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u5f15\u7528\n }\n}\n\n/* \u51fd\u6570 */\nfunction constFunc(): number {\n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0;\n}\n\nfunction algorithm(n: number): number { // \u8f93\u5165\u6570\u636e\n const a = 0; // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\n let b = 0; // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\n const node = new Node(0); // \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\n const c = constFunc(); // \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\n return a + b + c; // \u8f93\u51fa\u6570\u636e\n}\n
/* \u7c7b */\nclass Node {\n int val;\n Node next;\n Node(this.val, [this.next]);\n}\n\n/* \u51fd\u6570 */\nint function() {\n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c...\n return 0;\n}\n\nint algorithm(int n) { // \u8f93\u5165\u6570\u636e\n const int a = 0; // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\n int b = 0; // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\n Node node = Node(0); // \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\n int c = function(); // \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\n return a + b + c; // \u8f93\u51fa\u6570\u636e\n}\n
use std::rc::Rc;\nuse std::cell::RefCell;\n\n/* \u7ed3\u6784\u4f53 */\nstruct Node {\n val: i32,\n next: Option<Rc<RefCell<Node>>>,\n}\n\n/* \u521b\u5efa Node \u7ed3\u6784\u4f53 */\nimpl Node {\n fn new(val: i32) -> Self {\n Self { val: val, next: None }\n }\n}\n\n/* \u51fd\u6570 */\nfn function() -> i32 { \n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c...\n return 0;\n}\n\nfn algorithm(n: i32) -> i32 { // \u8f93\u5165\u6570\u636e\n const a: i32 = 0; // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\n let mut b = 0; // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\n let node = Node::new(0); // \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\n let c = function(); // \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\n return a + b + c; // \u8f93\u51fa\u6570\u636e\n}\n
/* \u51fd\u6570 */\nint func() {\n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c...\n return 0;\n}\n\nint algorithm(int n) { // \u8f93\u5165\u6570\u636e\n const int a = 0; // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\n int b = 0; // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\n int c = func(); // \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\n return a + b + c; // \u8f93\u51fa\u6570\u636e\n}\n
\n
"},{"location":"chapter_computational_complexity/space_complexity/#242","title":"2.4.2 \u00a0 \u63a8\u7b97\u65b9\u6cd5","text":"\u7a7a\u95f4\u590d\u6742\u5ea6\u7684\u63a8\u7b97\u65b9\u6cd5\u4e0e\u65f6\u95f4\u590d\u6742\u5ea6\u5927\u81f4\u76f8\u540c\uff0c\u53ea\u9700\u5c06\u7edf\u8ba1\u5bf9\u8c61\u4ece\u201c\u64cd\u4f5c\u6570\u91cf\u201d\u8f6c\u4e3a\u201c\u4f7f\u7528\u7a7a\u95f4\u5927\u5c0f\u201d\u3002
\u800c\u4e0e\u65f6\u95f4\u590d\u6742\u5ea6\u4e0d\u540c\u7684\u662f\uff0c\u6211\u4eec\u901a\u5e38\u53ea\u5173\u6ce8\u6700\u5dee\u7a7a\u95f4\u590d\u6742\u5ea6\u3002\u8fd9\u662f\u56e0\u4e3a\u5185\u5b58\u7a7a\u95f4\u662f\u4e00\u9879\u786c\u6027\u8981\u6c42\uff0c\u6211\u4eec\u5fc5\u987b\u786e\u4fdd\u5728\u6240\u6709\u8f93\u5165\u6570\u636e\u4e0b\u90fd\u6709\u8db3\u591f\u7684\u5185\u5b58\u7a7a\u95f4\u9884\u7559\u3002
\u89c2\u5bdf\u4ee5\u4e0b\u4ee3\u7801\uff0c\u6700\u5dee\u7a7a\u95f4\u590d\u6742\u5ea6\u4e2d\u7684\u201c\u6700\u5dee\u201d\u6709\u4e24\u5c42\u542b\u4e49\u3002
nums
\u5360\u7528 \\(O(n)\\) \u7a7a\u95f4\uff0c\u56e0\u6b64\u6700\u5dee\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002nums
\u65f6\uff0c\u7a0b\u5e8f\u5360\u7528 \\(O(n)\\) \u7a7a\u95f4\uff0c\u56e0\u6b64\u6700\u5dee\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002def algorithm(n: int):\n a = 0 # O(1)\n b = [0] * 10000 # O(1)\n if n > 10:\n nums = [0] * n # O(n)\n
void algorithm(int n) {\n int a = 0; // O(1)\n vector<int> b(10000); // O(1)\n if (n > 10)\n vector<int> nums(n); // O(n)\n}\n
void algorithm(int n) {\n int a = 0; // O(1)\n int[] b = new int[10000]; // O(1)\n if (n > 10)\n int[] nums = new int[n]; // O(n)\n}\n
void Algorithm(int n) {\n int a = 0; // O(1)\n int[] b = new int[10000]; // O(1)\n if (n > 10) {\n int[] nums = new int[n]; // O(n)\n }\n}\n
func algorithm(n int) {\n a := 0 // O(1)\n b := make([]int, 10000) // O(1)\n var nums []int\n if n > 10 {\n nums := make([]int, n) // O(n)\n }\n fmt.Println(a, b, nums)\n}\n
func algorithm(n: Int) {\n let a = 0 // O(1)\n let b = Array(repeating: 0, count: 10000) // O(1)\n if n > 10 {\n let nums = Array(repeating: 0, count: n) // O(n)\n }\n}\n
function algorithm(n) {\n const a = 0; // O(1)\n const b = new Array(10000); // O(1)\n if (n > 10) {\n const nums = new Array(n); // O(n)\n }\n}\n
function algorithm(n: number): void {\n const a = 0; // O(1)\n const b = new Array(10000); // O(1)\n if (n > 10) {\n const nums = new Array(n); // O(n)\n }\n}\n
void algorithm(int n) {\n int a = 0; // O(1)\n List<int> b = List.filled(10000, 0); // O(1)\n if (n > 10) {\n List<int> nums = List.filled(n, 0); // O(n)\n }\n}\n
fn algorithm(n: i32) {\n let a = 0; // O(1)\n let b = [0; 10000]; // O(1)\n if n > 10 {\n let nums = vec![0; n as usize]; // O(n)\n }\n}\n
void algorithm(int n) {\n int a = 0; // O(1)\n int b[10000]; // O(1)\n if (n > 10)\n int nums[n] = {0}; // O(n)\n}\n
\n
\u5728\u9012\u5f52\u51fd\u6570\u4e2d\uff0c\u9700\u8981\u6ce8\u610f\u7edf\u8ba1\u6808\u5e27\u7a7a\u95f4\u3002\u89c2\u5bdf\u4ee5\u4e0b\u4ee3\u7801\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZigdef function() -> int:\n # \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0\n\ndef loop(n: int):\n \"\"\"\u5faa\u73af O(1)\"\"\"\n for _ in range(n):\n function()\n\ndef recur(n: int) -> int:\n \"\"\"\u9012\u5f52 O(n)\"\"\"\n if n == 1: return\n return recur(n - 1)\n
int func() {\n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0;\n}\n/* \u5faa\u73af O(1) */\nvoid loop(int n) {\n for (int i = 0; i < n; i++) {\n func();\n }\n}\n/* \u9012\u5f52 O(n) */\nvoid recur(int n) {\n if (n == 1) return;\n return recur(n - 1);\n}\n
int function() {\n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0;\n}\n/* \u5faa\u73af O(1) */\nvoid loop(int n) {\n for (int i = 0; i < n; i++) {\n function();\n }\n}\n/* \u9012\u5f52 O(n) */\nvoid recur(int n) {\n if (n == 1) return;\n return recur(n - 1);\n}\n
int Function() {\n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0;\n}\n/* \u5faa\u73af O(1) */\nvoid Loop(int n) {\n for (int i = 0; i < n; i++) {\n Function();\n }\n}\n/* \u9012\u5f52 O(n) */\nint Recur(int n) {\n if (n == 1) return 1;\n return Recur(n - 1);\n}\n
func function() int {\n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0\n}\n\n/* \u5faa\u73af O(1) */\nfunc loop(n int) {\n for i := 0; i < n; i++ {\n function()\n }\n}\n\n/* \u9012\u5f52 O(n) */\nfunc recur(n int) {\n if n == 1 {\n return\n }\n recur(n - 1)\n}\n
@discardableResult\nfunc function() -> Int {\n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0\n}\n\n/* \u5faa\u73af O(1) */\nfunc loop(n: Int) {\n for _ in 0 ..< n {\n function()\n }\n}\n\n/* \u9012\u5f52 O(n) */\nfunc recur(n: Int) {\n if n == 1 {\n return\n }\n recur(n: n - 1)\n}\n
function constFunc() {\n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0;\n}\n/* \u5faa\u73af O(1) */\nfunction loop(n) {\n for (let i = 0; i < n; i++) {\n constFunc();\n }\n}\n/* \u9012\u5f52 O(n) */\nfunction recur(n) {\n if (n === 1) return;\n return recur(n - 1);\n}\n
function constFunc(): number {\n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0;\n}\n/* \u5faa\u73af O(1) */\nfunction loop(n: number): void {\n for (let i = 0; i < n; i++) {\n constFunc();\n }\n}\n/* \u9012\u5f52 O(n) */\nfunction recur(n: number): void {\n if (n === 1) return;\n return recur(n - 1);\n}\n
int function() {\n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0;\n}\n/* \u5faa\u73af O(1) */\nvoid loop(int n) {\n for (int i = 0; i < n; i++) {\n function();\n }\n}\n/* \u9012\u5f52 O(n) */\nvoid recur(int n) {\n if (n == 1) return;\n return recur(n - 1);\n}\n
fn function() -> i32 {\n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0;\n}\n/* \u5faa\u73af O(1) */\nfn loop(n: i32) {\n for i in 0..n {\n function();\n }\n}\n/* \u9012\u5f52 O(n) */\nvoid recur(n: i32) {\n if n == 1 {\n return;\n }\n recur(n - 1);\n}\n
int func() {\n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0;\n}\n/* \u5faa\u73af O(1) */\nvoid loop(int n) {\n for (int i = 0; i < n; i++) {\n func();\n }\n}\n/* \u9012\u5f52 O(n) */\nvoid recur(int n) {\n if (n == 1) return;\n return recur(n - 1);\n}\n
\n
\u51fd\u6570 loop()
\u548c recur()
\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u90fd\u4e3a \\(O(n)\\) \uff0c\u4f46\u7a7a\u95f4\u590d\u6742\u5ea6\u4e0d\u540c\u3002
loop()
\u5728\u5faa\u73af\u4e2d\u8c03\u7528\u4e86 \\(n\\) \u6b21 function()
\uff0c\u6bcf\u8f6e\u4e2d\u7684 function()
\u90fd\u8fd4\u56de\u5e76\u91ca\u653e\u4e86\u6808\u5e27\u7a7a\u95f4\uff0c\u56e0\u6b64\u7a7a\u95f4\u590d\u6742\u5ea6\u4ecd\u4e3a \\(O(1)\\) \u3002recur()
\u5728\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u4f1a\u540c\u65f6\u5b58\u5728 \\(n\\) \u4e2a\u672a\u8fd4\u56de\u7684 recur()
\uff0c\u4ece\u800c\u5360\u7528 \\(O(n)\\) \u7684\u6808\u5e27\u7a7a\u95f4\u3002\u8bbe\u8f93\u5165\u6570\u636e\u5927\u5c0f\u4e3a \\(n\\) \uff0c\u56fe 2-16 \u5c55\u793a\u4e86\u5e38\u89c1\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u7c7b\u578b\uff08\u4ece\u4f4e\u5230\u9ad8\u6392\u5217\uff09\u3002
\\[ \\begin{aligned} O(1) < O(\\log n) < O(n) < O(n^2) < O(2^n) \\newline \\text{\u5e38\u6570\u9636} < \\text{\u5bf9\u6570\u9636} < \\text{\u7ebf\u6027\u9636} < \\text{\u5e73\u65b9\u9636} < \\text{\u6307\u6570\u9636} \\end{aligned} \\]\u56fe 2-16 \u00a0 \u5e38\u89c1\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u7c7b\u578b
"},{"location":"chapter_computational_complexity/space_complexity/#1-o1","title":"1. \u00a0 \u5e38\u6570\u9636 \\(O(1)\\)","text":"\u5e38\u6570\u9636\u5e38\u89c1\u4e8e\u6570\u91cf\u4e0e\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u65e0\u5173\u7684\u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u3002
\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5728\u5faa\u73af\u4e2d\u521d\u59cb\u5316\u53d8\u91cf\u6216\u8c03\u7528\u51fd\u6570\u800c\u5360\u7528\u7684\u5185\u5b58\uff0c\u5728\u8fdb\u5165\u4e0b\u4e00\u5faa\u73af\u540e\u5c31\u4f1a\u88ab\u91ca\u653e\uff0c\u56e0\u6b64\u4e0d\u4f1a\u7d2f\u79ef\u5360\u7528\u7a7a\u95f4\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4ecd\u4e3a \\(O(1)\\) \uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig space_complexity.pydef function() -> int:\n \"\"\"\u51fd\u6570\"\"\"\n # \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0\n\ndef constant(n: int):\n \"\"\"\u5e38\u6570\u9636\"\"\"\n # \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\n a = 0\n nums = [0] * 10000\n node = ListNode(0)\n # \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\n for _ in range(n):\n c = 0\n # \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\n for _ in range(n):\n function()\n
space_complexity.cpp/* \u51fd\u6570 */\nint func() {\n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0;\n}\n\n/* \u5e38\u6570\u9636 */\nvoid constant(int n) {\n // \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\n const int a = 0;\n int b = 0;\n vector<int> nums(10000);\n ListNode node(0);\n // \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\n for (int i = 0; i < n; i++) {\n int c = 0;\n }\n // \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\n for (int i = 0; i < n; i++) {\n func();\n }\n}\n
space_complexity.java/* \u51fd\u6570 */\nint function() {\n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0;\n}\n\n/* \u5e38\u6570\u9636 */\nvoid constant(int n) {\n // \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\n final int a = 0;\n int b = 0;\n int[] nums = new int[10000];\n ListNode node = new ListNode(0);\n // \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\n for (int i = 0; i < n; i++) {\n int c = 0;\n }\n // \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\n for (int i = 0; i < n; i++) {\n function();\n }\n}\n
space_complexity.cs/* \u51fd\u6570 */\nint Function() {\n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0;\n}\n\n/* \u5e38\u6570\u9636 */\nvoid Constant(int n) {\n // \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\n int a = 0;\n int b = 0;\n int[] nums = new int[10000];\n ListNode node = new(0);\n // \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\n for (int i = 0; i < n; i++) {\n int c = 0;\n }\n // \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\n for (int i = 0; i < n; i++) {\n Function();\n }\n}\n
space_complexity.go/* \u51fd\u6570 */\nfunc function() int {\n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c...\n return 0\n}\n\n/* \u5e38\u6570\u9636 */\nfunc spaceConstant(n int) {\n // \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\n const a = 0\n b := 0\n nums := make([]int, 10000)\n ListNode := newNode(0)\n // \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\n var c int\n for i := 0; i < n; i++ {\n c = 0\n }\n // \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\n for i := 0; i < n; i++ {\n function()\n }\n fmt.Println(a, b, nums, c, ListNode)\n}\n
space_complexity.swift/* \u51fd\u6570 */\n@discardableResult\nfunc function() -> Int {\n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0\n}\n\n/* \u5e38\u6570\u9636 */\nfunc constant(n: Int) {\n // \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\n let a = 0\n var b = 0\n let nums = Array(repeating: 0, count: 10000)\n let node = ListNode(x: 0)\n // \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\n for _ in 0 ..< n {\n let c = 0\n }\n // \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\n for _ in 0 ..< n {\n function()\n }\n}\n
space_complexity.js/* \u51fd\u6570 */\nfunction constFunc() {\n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0;\n}\n\n/* \u5e38\u6570\u9636 */\nfunction constant(n) {\n // \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\n const a = 0;\n const b = 0;\n const nums = new Array(10000);\n const node = new ListNode(0);\n // \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\n for (let i = 0; i < n; i++) {\n const c = 0;\n }\n // \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\n for (let i = 0; i < n; i++) {\n constFunc();\n }\n}\n
space_complexity.ts/* \u51fd\u6570 */\nfunction constFunc(): number {\n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0;\n}\n\n/* \u5e38\u6570\u9636 */\nfunction constant(n: number): void {\n // \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\n const a = 0;\n const b = 0;\n const nums = new Array(10000);\n const node = new ListNode(0);\n // \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\n for (let i = 0; i < n; i++) {\n const c = 0;\n }\n // \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\n for (let i = 0; i < n; i++) {\n constFunc();\n }\n}\n
space_complexity.dart/* \u51fd\u6570 */\nint function() {\n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0;\n}\n\n/* \u5e38\u6570\u9636 */\nvoid constant(int n) {\n // \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\n final int a = 0;\n int b = 0;\n List<int> nums = List.filled(10000, 0);\n ListNode node = ListNode(0);\n // \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\n for (var i = 0; i < n; i++) {\n int c = 0;\n }\n // \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\n for (var i = 0; i < n; i++) {\n function();\n }\n}\n
space_complexity.rs/* \u51fd\u6570 */\nfn function() ->i32 {\n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0;\n}\n\n/* \u5e38\u6570\u9636 */\n#[allow(unused)]\nfn constant(n: i32) {\n // \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\n const A: i32 = 0;\n let b = 0;\n let nums = vec![0; 10000];\n let node = ListNode::new(0);\n // \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\n for i in 0..n {\n let c = 0;\n }\n // \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\n for i in 0..n {\n function();\n }\n}\n
space_complexity.c/* \u51fd\u6570 */\nint func() {\n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0;\n}\n\n/* \u5e38\u6570\u9636 */\nvoid constant(int n) {\n // \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\n const int a = 0;\n int b = 0;\n int nums[1000];\n ListNode *node = newListNode(0);\n free(node);\n // \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\n for (int i = 0; i < n; i++) {\n int c = 0;\n }\n // \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\n for (int i = 0; i < n; i++) {\n func();\n }\n}\n
space_complexity.zig// \u51fd\u6570\nfn function() i32 {\n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0;\n}\n\n// \u5e38\u6570\u9636\nfn constant(n: i32) void {\n // \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\n const a: i32 = 0;\n var b: i32 = 0;\n var nums = [_]i32{0}**10000;\n var node = inc.ListNode(i32){.val = 0};\n var i: i32 = 0;\n // \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\n while (i < n) : (i += 1) {\n var c: i32 = 0;\n _ = c;\n }\n // \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\n i = 0;\n while (i < n) : (i += 1) {\n _ = function();\n }\n _ = a;\n _ = b;\n _ = nums;\n _ = node;\n}\n
"},{"location":"chapter_computational_complexity/space_complexity/#2-on","title":"2. \u00a0 \u7ebf\u6027\u9636 \\(O(n)\\)","text":"\u7ebf\u6027\u9636\u5e38\u89c1\u4e8e\u5143\u7d20\u6570\u91cf\u4e0e \\(n\\) \u6210\u6b63\u6bd4\u7684\u6570\u7ec4\u3001\u94fe\u8868\u3001\u6808\u3001\u961f\u5217\u7b49\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig space_complexity.pydef linear(n: int):\n \"\"\"\u7ebf\u6027\u9636\"\"\"\n # \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n nums = [0] * n\n # \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n hmap = dict[int, str]()\n for i in range(n):\n hmap[i] = str(i)\n
space_complexity.cpp/* \u7ebf\u6027\u9636 */\nvoid linear(int n) {\n // \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\n vector<int> nums(n);\n // \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n vector<ListNode> nodes;\n for (int i = 0; i < n; i++) {\n nodes.push_back(ListNode(i));\n }\n // \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n unordered_map<int, string> map;\n for (int i = 0; i < n; i++) {\n map[i] = to_string(i);\n }\n}\n
space_complexity.java/* \u7ebf\u6027\u9636 */\nvoid linear(int n) {\n // \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\n int[] nums = new int[n];\n // \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n List<ListNode> nodes = new ArrayList<>();\n for (int i = 0; i < n; i++) {\n nodes.add(new ListNode(i));\n }\n // \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n Map<Integer, String> map = new HashMap<>();\n for (int i = 0; i < n; i++) {\n map.put(i, String.valueOf(i));\n }\n}\n
space_complexity.cs/* \u7ebf\u6027\u9636 */\nvoid Linear(int n) {\n // \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\n int[] nums = new int[n];\n // \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n List<ListNode> nodes = [];\n for (int i = 0; i < n; i++) {\n nodes.Add(new ListNode(i));\n }\n // \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n Dictionary<int, string> map = [];\n for (int i = 0; i < n; i++) {\n map.Add(i, i.ToString());\n }\n}\n
space_complexity.go/* \u7ebf\u6027\u9636 */\nfunc spaceLinear(n int) {\n // \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\n _ = make([]int, n)\n // \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n var nodes []*node\n for i := 0; i < n; i++ {\n nodes = append(nodes, newNode(i))\n }\n // \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n m := make(map[int]string, n)\n for i := 0; i < n; i++ {\n m[i] = strconv.Itoa(i)\n }\n}\n
space_complexity.swift/* \u7ebf\u6027\u9636 */\nfunc linear(n: Int) {\n // \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\n let nums = Array(repeating: 0, count: n)\n // \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n let nodes = (0 ..< n).map { ListNode(x: $0) }\n // \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n let map = Dictionary(uniqueKeysWithValues: (0 ..< n).map { ($0, \"\\($0)\") })\n}\n
space_complexity.js/* \u7ebf\u6027\u9636 */\nfunction linear(n) {\n // \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\n const nums = new Array(n);\n // \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n const nodes = [];\n for (let i = 0; i < n; i++) {\n nodes.push(new ListNode(i));\n }\n // \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n const map = new Map();\n for (let i = 0; i < n; i++) {\n map.set(i, i.toString());\n }\n}\n
space_complexity.ts/* \u7ebf\u6027\u9636 */\nfunction linear(n: number): void {\n // \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\n const nums = new Array(n);\n // \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n const nodes: ListNode[] = [];\n for (let i = 0; i < n; i++) {\n nodes.push(new ListNode(i));\n }\n // \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n const map = new Map();\n for (let i = 0; i < n; i++) {\n map.set(i, i.toString());\n }\n}\n
space_complexity.dart/* \u7ebf\u6027\u9636 */\nvoid linear(int n) {\n // \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\n List<int> nums = List.filled(n, 0);\n // \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n List<ListNode> nodes = [];\n for (var i = 0; i < n; i++) {\n nodes.add(ListNode(i));\n }\n // \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n Map<int, String> map = HashMap();\n for (var i = 0; i < n; i++) {\n map.putIfAbsent(i, () => i.toString());\n }\n}\n
space_complexity.rs/* \u7ebf\u6027\u9636 */\n#[allow(unused)]\nfn linear(n: i32) {\n // \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\n let mut nums = vec![0; n as usize];\n // \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n let mut nodes = Vec::new();\n for i in 0..n {\n nodes.push(ListNode::new(i))\n }\n // \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n let mut map = HashMap::new();\n for i in 0..n {\n map.insert(i, i.to_string());\n }\n}\n
space_complexity.c/* \u54c8\u5e0c\u8868 */\ntypedef struct {\n int key;\n int val;\n UT_hash_handle hh; // \u57fa\u4e8e uthash.h \u5b9e\u73b0\n} HashTable;\n\n/* \u7ebf\u6027\u9636 */\nvoid linear(int n) {\n // \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\n int *nums = malloc(sizeof(int) * n);\n free(nums);\n\n // \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n ListNode **nodes = malloc(sizeof(ListNode *) * n);\n for (int i = 0; i < n; i++) {\n nodes[i] = newListNode(i);\n }\n // \u5185\u5b58\u91ca\u653e\n for (int i = 0; i < n; i++) {\n free(nodes[i]);\n }\n free(nodes);\n\n // \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n HashTable *h = NULL;\n for (int i = 0; i < n; i++) {\n HashTable *tmp = malloc(sizeof(HashTable));\n tmp->key = i;\n tmp->val = i;\n HASH_ADD_INT(h, key, tmp);\n }\n\n // \u5185\u5b58\u91ca\u653e\n HashTable *curr, *tmp;\n HASH_ITER(hh, h, curr, tmp) {\n HASH_DEL(h, curr);\n free(curr);\n }\n}\n
space_complexity.zig// \u7ebf\u6027\u9636\nfn linear(comptime n: i32) !void {\n // \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\n var nums = [_]i32{0}**n;\n // \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n var nodes = std.ArrayList(i32).init(std.heap.page_allocator);\n defer nodes.deinit();\n var i: i32 = 0;\n while (i < n) : (i += 1) {\n try nodes.append(i);\n }\n // \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n var map = std.AutoArrayHashMap(i32, []const u8).init(std.heap.page_allocator);\n defer map.deinit();\n var j: i32 = 0;\n while (j < n) : (j += 1) {\n const string = try std.fmt.allocPrint(std.heap.page_allocator, \"{d}\", .{j});\n defer std.heap.page_allocator.free(string);\n try map.put(i, string);\n }\n _ = nums;\n}\n
\u5982\u56fe 2-17 \u6240\u793a\uff0c\u6b64\u51fd\u6570\u7684\u9012\u5f52\u6df1\u5ea6\u4e3a \\(n\\) \uff0c\u5373\u540c\u65f6\u5b58\u5728 \\(n\\) \u4e2a\u672a\u8fd4\u56de\u7684 linear_recur()
\u51fd\u6570\uff0c\u4f7f\u7528 \\(O(n)\\) \u5927\u5c0f\u7684\u6808\u5e27\u7a7a\u95f4\uff1a
def linear_recur(n: int):\n \"\"\"\u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\"\"\"\n print(\"\u9012\u5f52 n =\", n)\n if n == 1:\n return\n linear_recur(n - 1)\n
space_complexity.cpp/* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nvoid linearRecur(int n) {\n cout << \"\u9012\u5f52 n = \" << n << endl;\n if (n == 1)\n return;\n linearRecur(n - 1);\n}\n
space_complexity.java/* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nvoid linearRecur(int n) {\n System.out.println(\"\u9012\u5f52 n = \" + n);\n if (n == 1)\n return;\n linearRecur(n - 1);\n}\n
space_complexity.cs/* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nvoid LinearRecur(int n) {\n Console.WriteLine(\"\u9012\u5f52 n = \" + n);\n if (n == 1) return;\n LinearRecur(n - 1);\n}\n
space_complexity.go/* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunc spaceLinearRecur(n int) {\n fmt.Println(\"\u9012\u5f52 n =\", n)\n if n == 1 {\n return\n }\n spaceLinearRecur(n - 1)\n}\n
space_complexity.swift/* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunc linearRecur(n: Int) {\n print(\"\u9012\u5f52 n = \\(n)\")\n if n == 1 {\n return\n }\n linearRecur(n: n - 1)\n}\n
space_complexity.js/* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction linearRecur(n) {\n console.log(`\u9012\u5f52 n = ${n}`);\n if (n === 1) return;\n linearRecur(n - 1);\n}\n
space_complexity.ts/* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction linearRecur(n: number): void {\n console.log(`\u9012\u5f52 n = ${n}`);\n if (n === 1) return;\n linearRecur(n - 1);\n}\n
space_complexity.dart/* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nvoid linearRecur(int n) {\n print('\u9012\u5f52 n = $n');\n if (n == 1) return;\n linearRecur(n - 1);\n}\n
space_complexity.rs/* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfn linear_recur(n: i32) {\n println!(\"\u9012\u5f52 n = {}\", n);\n if n == 1 {return};\n linear_recur(n - 1);\n}\n
space_complexity.c/* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nvoid linearRecur(int n) {\n printf(\"\u9012\u5f52 n = %d\\r\\n\", n);\n if (n == 1)\n return;\n linearRecur(n - 1);\n}\n
space_complexity.zig// \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\nfn linearRecur(comptime n: i32) void {\n std.debug.print(\"\u9012\u5f52 n = {}\\n\", .{n});\n if (n == 1) return;\n linearRecur(n - 1);\n}\n
\u56fe 2-17 \u00a0 \u9012\u5f52\u51fd\u6570\u4ea7\u751f\u7684\u7ebf\u6027\u9636\u7a7a\u95f4\u590d\u6742\u5ea6
"},{"location":"chapter_computational_complexity/space_complexity/#3-on2","title":"3. \u00a0 \u5e73\u65b9\u9636 \\(O(n^2)\\)","text":"\u5e73\u65b9\u9636\u5e38\u89c1\u4e8e\u77e9\u9635\u548c\u56fe\uff0c\u5143\u7d20\u6570\u91cf\u4e0e \\(n\\) \u6210\u5e73\u65b9\u5173\u7cfb\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig space_complexity.pydef quadratic(n: int):\n \"\"\"\u5e73\u65b9\u9636\"\"\"\n # \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\n num_matrix = [[0] * n for _ in range(n)]\n
space_complexity.cpp/* \u5e73\u65b9\u9636 */\nvoid quadratic(int n) {\n // \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\n vector<vector<int>> numMatrix;\n for (int i = 0; i < n; i++) {\n vector<int> tmp;\n for (int j = 0; j < n; j++) {\n tmp.push_back(0);\n }\n numMatrix.push_back(tmp);\n }\n}\n
space_complexity.java/* \u5e73\u65b9\u9636 */\nvoid quadratic(int n) {\n // \u77e9\u9635\u5360\u7528 O(n^2) \u7a7a\u95f4\n int[][] numMatrix = new int[n][n];\n // \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\n List<List<Integer>> numList = new ArrayList<>();\n for (int i = 0; i < n; i++) {\n List<Integer> tmp = new ArrayList<>();\n for (int j = 0; j < n; j++) {\n tmp.add(0);\n }\n numList.add(tmp);\n }\n}\n
space_complexity.cs/* \u5e73\u65b9\u9636 */\nvoid Quadratic(int n) {\n // \u77e9\u9635\u5360\u7528 O(n^2) \u7a7a\u95f4\n int[,] numMatrix = new int[n, n];\n // \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\n List<List<int>> numList = [];\n for (int i = 0; i < n; i++) {\n List<int> tmp = [];\n for (int j = 0; j < n; j++) {\n tmp.Add(0);\n }\n numList.Add(tmp);\n }\n}\n
space_complexity.go/* \u5e73\u65b9\u9636 */\nfunc spaceQuadratic(n int) {\n // \u77e9\u9635\u5360\u7528 O(n^2) \u7a7a\u95f4\n numMatrix := make([][]int, n)\n for i := 0; i < n; i++ {\n numMatrix[i] = make([]int, n)\n }\n}\n
space_complexity.swift/* \u5e73\u65b9\u9636 */\nfunc quadratic(n: Int) {\n // \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\n let numList = Array(repeating: Array(repeating: 0, count: n), count: n)\n}\n
space_complexity.js/* \u5e73\u65b9\u9636 */\nfunction quadratic(n) {\n // \u77e9\u9635\u5360\u7528 O(n^2) \u7a7a\u95f4\n const numMatrix = Array(n)\n .fill(null)\n .map(() => Array(n).fill(null));\n // \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\n const numList = [];\n for (let i = 0; i < n; i++) {\n const tmp = [];\n for (let j = 0; j < n; j++) {\n tmp.push(0);\n }\n numList.push(tmp);\n }\n}\n
space_complexity.ts/* \u5e73\u65b9\u9636 */\nfunction quadratic(n: number): void {\n // \u77e9\u9635\u5360\u7528 O(n^2) \u7a7a\u95f4\n const numMatrix = Array(n)\n .fill(null)\n .map(() => Array(n).fill(null));\n // \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\n const numList = [];\n for (let i = 0; i < n; i++) {\n const tmp = [];\n for (let j = 0; j < n; j++) {\n tmp.push(0);\n }\n numList.push(tmp);\n }\n}\n
space_complexity.dart/* \u5e73\u65b9\u9636 */\nvoid quadratic(int n) {\n // \u77e9\u9635\u5360\u7528 O(n^2) \u7a7a\u95f4\n List<List<int>> numMatrix = List.generate(n, (_) => List.filled(n, 0));\n // \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\n List<List<int>> numList = [];\n for (var i = 0; i < n; i++) {\n List<int> tmp = [];\n for (int j = 0; j < n; j++) {\n tmp.add(0);\n }\n numList.add(tmp);\n }\n}\n
space_complexity.rs/* \u5e73\u65b9\u9636 */\n#[allow(unused)]\nfn quadratic(n: i32) {\n // \u77e9\u9635\u5360\u7528 O(n^2) \u7a7a\u95f4\n let num_matrix = vec![vec![0; n as usize]; n as usize];\n // \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\n let mut num_list = Vec::new();\n for i in 0..n {\n let mut tmp = Vec::new();\n for j in 0..n {\n tmp.push(0);\n }\n num_list.push(tmp);\n }\n}\n
space_complexity.c/* \u5e73\u65b9\u9636 */\nvoid quadratic(int n) {\n // \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\n int **numMatrix = malloc(sizeof(int *) * n);\n for (int i = 0; i < n; i++) {\n int *tmp = malloc(sizeof(int) * n);\n for (int j = 0; j < n; j++) {\n tmp[j] = 0;\n }\n numMatrix[i] = tmp;\n }\n\n // \u5185\u5b58\u91ca\u653e\n for (int i = 0; i < n; i++) {\n free(numMatrix[i]);\n }\n free(numMatrix);\n}\n
space_complexity.zig// \u5e73\u65b9\u9636\nfn quadratic(n: i32) !void {\n // \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\n var nodes = std.ArrayList(std.ArrayList(i32)).init(std.heap.page_allocator);\n defer nodes.deinit();\n var i: i32 = 0;\n while (i < n) : (i += 1) {\n var tmp = std.ArrayList(i32).init(std.heap.page_allocator);\n defer tmp.deinit();\n var j: i32 = 0;\n while (j < n) : (j += 1) {\n try tmp.append(0);\n }\n try nodes.append(tmp);\n }\n}\n
\u5982\u56fe 2-18 \u6240\u793a\uff0c\u8be5\u51fd\u6570\u7684\u9012\u5f52\u6df1\u5ea6\u4e3a \\(n\\) \uff0c\u5728\u6bcf\u4e2a\u9012\u5f52\u51fd\u6570\u4e2d\u90fd\u521d\u59cb\u5316\u4e86\u4e00\u4e2a\u6570\u7ec4\uff0c\u957f\u5ea6\u5206\u522b\u4e3a \\(n\\)\u3001\\(n-1\\)\u3001\\(\\dots\\)\u3001\\(2\\)\u3001\\(1\\) \uff0c\u5e73\u5747\u957f\u5ea6\u4e3a \\(n / 2\\) \uff0c\u56e0\u6b64\u603b\u4f53\u5360\u7528 \\(O(n^2)\\) \u7a7a\u95f4\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig space_complexity.pydef quadratic_recur(n: int) -> int:\n \"\"\"\u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\"\"\"\n if n <= 0:\n return 0\n # \u6570\u7ec4 nums \u957f\u5ea6\u4e3a n, n-1, ..., 2, 1\n nums = [0] * n\n return quadratic_recur(n - 1)\n
space_complexity.cpp/* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint quadraticRecur(int n) {\n if (n <= 0)\n return 0;\n vector<int> nums(n);\n cout << \"\u9012\u5f52 n = \" << n << \" \u4e2d\u7684 nums \u957f\u5ea6 = \" << nums.size() << endl;\n return quadraticRecur(n - 1);\n}\n
space_complexity.java/* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint quadraticRecur(int n) {\n if (n <= 0)\n return 0;\n // \u6570\u7ec4 nums \u957f\u5ea6\u4e3a n, n-1, ..., 2, 1\n int[] nums = new int[n];\n System.out.println(\"\u9012\u5f52 n = \" + n + \" \u4e2d\u7684 nums \u957f\u5ea6 = \" + nums.length);\n return quadraticRecur(n - 1);\n}\n
space_complexity.cs/* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint QuadraticRecur(int n) {\n if (n <= 0) return 0;\n int[] nums = new int[n];\n Console.WriteLine(\"\u9012\u5f52 n = \" + n + \" \u4e2d\u7684 nums \u957f\u5ea6 = \" + nums.Length);\n return QuadraticRecur(n - 1);\n}\n
space_complexity.go/* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunc spaceQuadraticRecur(n int) int {\n if n <= 0 {\n return 0\n }\n nums := make([]int, n)\n fmt.Printf(\"\u9012\u5f52 n = %d \u4e2d\u7684 nums \u957f\u5ea6 = %d \\n\", n, len(nums))\n return spaceQuadraticRecur(n - 1)\n}\n
space_complexity.swift/* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\n@discardableResult\nfunc quadraticRecur(n: Int) -> Int {\n if n <= 0 {\n return 0\n }\n // \u6570\u7ec4 nums \u957f\u5ea6\u4e3a n, n-1, ..., 2, 1\n let nums = Array(repeating: 0, count: n)\n print(\"\u9012\u5f52 n = \\(n) \u4e2d\u7684 nums \u957f\u5ea6 = \\(nums.count)\")\n return quadraticRecur(n: n - 1)\n}\n
space_complexity.js/* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction quadraticRecur(n) {\n if (n <= 0) return 0;\n const nums = new Array(n);\n console.log(`\u9012\u5f52 n = ${n} \u4e2d\u7684 nums \u957f\u5ea6 = ${nums.length}`);\n return quadraticRecur(n - 1);\n}\n
space_complexity.ts/* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction quadraticRecur(n: number): number {\n if (n <= 0) return 0;\n const nums = new Array(n);\n console.log(`\u9012\u5f52 n = ${n} \u4e2d\u7684 nums \u957f\u5ea6 = ${nums.length}`);\n return quadraticRecur(n - 1);\n}\n
space_complexity.dart/* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint quadraticRecur(int n) {\n if (n <= 0) return 0;\n List<int> nums = List.filled(n, 0);\n print('\u9012\u5f52 n = $n \u4e2d\u7684 nums \u957f\u5ea6 = ${nums.length}');\n return quadraticRecur(n - 1);\n}\n
space_complexity.rs/* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfn quadratic_recur(n: i32) -> i32 {\n if n <= 0 {return 0};\n // \u6570\u7ec4 nums \u957f\u5ea6\u4e3a n, n-1, ..., 2, 1\n let nums = vec![0; n as usize];\n println!(\"\u9012\u5f52 n = {} \u4e2d\u7684 nums \u957f\u5ea6 = {}\", n, nums.len());\n return quadratic_recur(n - 1);\n}\n
space_complexity.c/* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint quadraticRecur(int n) {\n if (n <= 0)\n return 0;\n int *nums = malloc(sizeof(int) * n);\n printf(\"\u9012\u5f52 n = %d \u4e2d\u7684 nums \u957f\u5ea6 = %d\\r\\n\", n, n);\n int res = quadraticRecur(n - 1);\n free(nums);\n return res;\n}\n
space_complexity.zig// \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\nfn quadraticRecur(comptime n: i32) i32 {\n if (n <= 0) return 0;\n var nums = [_]i32{0}**n;\n std.debug.print(\"\u9012\u5f52 n = {} \u4e2d\u7684 nums \u957f\u5ea6 = {}\\n\", .{n, nums.len});\n return quadraticRecur(n - 1);\n}\n
\u56fe 2-18 \u00a0 \u9012\u5f52\u51fd\u6570\u4ea7\u751f\u7684\u5e73\u65b9\u9636\u7a7a\u95f4\u590d\u6742\u5ea6
"},{"location":"chapter_computational_complexity/space_complexity/#4-o2n","title":"4. \u00a0 \u6307\u6570\u9636 \\(O(2^n)\\)","text":"\u6307\u6570\u9636\u5e38\u89c1\u4e8e\u4e8c\u53c9\u6811\u3002\u89c2\u5bdf\u56fe 2-19 \uff0c\u9ad8\u5ea6\u4e3a \\(n\\) \u7684\u201c\u6ee1\u4e8c\u53c9\u6811\u201d\u7684\u8282\u70b9\u6570\u91cf\u4e3a \\(2^n - 1\\) \uff0c\u5360\u7528 \\(O(2^n)\\) \u7a7a\u95f4\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig space_complexity.pydef build_tree(n: int) -> TreeNode | None:\n \"\"\"\u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09\"\"\"\n if n == 0:\n return None\n root = TreeNode(0)\n root.left = build_tree(n - 1)\n root.right = build_tree(n - 1)\n return root\n
space_complexity.cpp/* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nTreeNode *buildTree(int n) {\n if (n == 0)\n return nullptr;\n TreeNode *root = new TreeNode(0);\n root->left = buildTree(n - 1);\n root->right = buildTree(n - 1);\n return root;\n}\n
space_complexity.java/* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nTreeNode buildTree(int n) {\n if (n == 0)\n return null;\n TreeNode root = new TreeNode(0);\n root.left = buildTree(n - 1);\n root.right = buildTree(n - 1);\n return root;\n}\n
space_complexity.cs/* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nTreeNode? BuildTree(int n) {\n if (n == 0) return null;\n TreeNode root = new(0) {\n left = BuildTree(n - 1),\n right = BuildTree(n - 1)\n };\n return root;\n}\n
space_complexity.go/* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nfunc buildTree(n int) *treeNode {\n if n == 0 {\n return nil\n }\n root := newTreeNode(0)\n root.left = buildTree(n - 1)\n root.right = buildTree(n - 1)\n return root\n}\n
space_complexity.swift/* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nfunc buildTree(n: Int) -> TreeNode? {\n if n == 0 {\n return nil\n }\n let root = TreeNode(x: 0)\n root.left = buildTree(n: n - 1)\n root.right = buildTree(n: n - 1)\n return root\n}\n
space_complexity.js/* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nfunction buildTree(n) {\n if (n === 0) return null;\n const root = new TreeNode(0);\n root.left = buildTree(n - 1);\n root.right = buildTree(n - 1);\n return root;\n}\n
space_complexity.ts/* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nfunction buildTree(n: number): TreeNode | null {\n if (n === 0) return null;\n const root = new TreeNode(0);\n root.left = buildTree(n - 1);\n root.right = buildTree(n - 1);\n return root;\n}\n
space_complexity.dart/* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nTreeNode? buildTree(int n) {\n if (n == 0) return null;\n TreeNode root = TreeNode(0);\n root.left = buildTree(n - 1);\n root.right = buildTree(n - 1);\n return root;\n}\n
space_complexity.rs/* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nfn build_tree(n: i32) -> Option<Rc<RefCell<TreeNode>>> {\n if n == 0 {return None};\n let root = TreeNode::new(0);\n root.borrow_mut().left = build_tree(n - 1);\n root.borrow_mut().right = build_tree(n - 1);\n return Some(root);\n}\n
space_complexity.c/* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nTreeNode *buildTree(int n) {\n if (n == 0)\n return NULL;\n TreeNode *root = newTreeNode(0);\n root->left = buildTree(n - 1);\n root->right = buildTree(n - 1);\n return root;\n}\n
space_complexity.zig// \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09\nfn buildTree(mem_allocator: std.mem.Allocator, n: i32) !?*inc.TreeNode(i32) {\n if (n == 0) return null;\n const root = try mem_allocator.create(inc.TreeNode(i32));\n root.init(0);\n root.left = try buildTree(mem_allocator, n - 1);\n root.right = try buildTree(mem_allocator, n - 1);\n return root;\n}\n
\u56fe 2-19 \u00a0 \u6ee1\u4e8c\u53c9\u6811\u4ea7\u751f\u7684\u6307\u6570\u9636\u7a7a\u95f4\u590d\u6742\u5ea6
"},{"location":"chapter_computational_complexity/space_complexity/#5-olog-n","title":"5. \u00a0 \u5bf9\u6570\u9636 \\(O(\\log n)\\)","text":"\u5bf9\u6570\u9636\u5e38\u89c1\u4e8e\u5206\u6cbb\u7b97\u6cd5\u3002\u4f8b\u5982\u5f52\u5e76\u6392\u5e8f\uff0c\u8f93\u5165\u957f\u5ea6\u4e3a \\(n\\) \u7684\u6570\u7ec4\uff0c\u6bcf\u8f6e\u9012\u5f52\u5c06\u6570\u7ec4\u4ece\u4e2d\u70b9\u5904\u5212\u5206\u4e3a\u4e24\u534a\uff0c\u5f62\u6210\u9ad8\u5ea6\u4e3a \\(\\log n\\) \u7684\u9012\u5f52\u6811\uff0c\u4f7f\u7528 \\(O(\\log n)\\) \u6808\u5e27\u7a7a\u95f4\u3002
\u518d\u4f8b\u5982\u5c06\u6570\u5b57\u8f6c\u5316\u4e3a\u5b57\u7b26\u4e32\uff0c\u8f93\u5165\u4e00\u4e2a\u6b63\u6574\u6570 \\(n\\) \uff0c\u5b83\u7684\u4f4d\u6570\u4e3a \\(\\log_{10} n + 1\\) \uff0c\u5373\u5bf9\u5e94\u5b57\u7b26\u4e32\u957f\u5ea6\u4e3a \\(\\log_{10} n + 1\\) \uff0c\u56e0\u6b64\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(\\log_{10} n + 1) = O(\\log n)\\) \u3002
"},{"location":"chapter_computational_complexity/space_complexity/#244","title":"2.4.4 \u00a0 \u6743\u8861\u65f6\u95f4\u4e0e\u7a7a\u95f4","text":"\u7406\u60f3\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u5e0c\u671b\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u548c\u7a7a\u95f4\u590d\u6742\u5ea6\u90fd\u80fd\u8fbe\u5230\u6700\u4f18\u3002\u7136\u800c\u5728\u5b9e\u9645\u60c5\u51b5\u4e2d\uff0c\u540c\u65f6\u4f18\u5316\u65f6\u95f4\u590d\u6742\u5ea6\u548c\u7a7a\u95f4\u590d\u6742\u5ea6\u901a\u5e38\u975e\u5e38\u56f0\u96be\u3002
\u964d\u4f4e\u65f6\u95f4\u590d\u6742\u5ea6\u901a\u5e38\u9700\u8981\u4ee5\u63d0\u5347\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a\u4ee3\u4ef7\uff0c\u53cd\u4e4b\u4ea6\u7136\u3002\u6211\u4eec\u5c06\u727a\u7272\u5185\u5b58\u7a7a\u95f4\u6765\u63d0\u5347\u7b97\u6cd5\u8fd0\u884c\u901f\u5ea6\u7684\u601d\u8def\u79f0\u4e3a\u201c\u4ee5\u7a7a\u95f4\u6362\u65f6\u95f4\u201d\uff1b\u53cd\u4e4b\uff0c\u5219\u79f0\u4e3a\u201c\u4ee5\u65f6\u95f4\u6362\u7a7a\u95f4\u201d\u3002
\u9009\u62e9\u54ea\u79cd\u601d\u8def\u53d6\u51b3\u4e8e\u6211\u4eec\u66f4\u770b\u91cd\u54ea\u4e2a\u65b9\u9762\u3002\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u65f6\u95f4\u6bd4\u7a7a\u95f4\u66f4\u5b9d\u8d35\uff0c\u56e0\u6b64\u201c\u4ee5\u7a7a\u95f4\u6362\u65f6\u95f4\u201d\u901a\u5e38\u662f\u66f4\u5e38\u7528\u7684\u7b56\u7565\u3002\u5f53\u7136\uff0c\u5728\u6570\u636e\u91cf\u5f88\u5927\u7684\u60c5\u51b5\u4e0b\uff0c\u63a7\u5236\u7a7a\u95f4\u590d\u6742\u5ea6\u4e5f\u975e\u5e38\u91cd\u8981\u3002
"},{"location":"chapter_computational_complexity/summary/","title":"2.5 \u00a0 \u5c0f\u7ed3","text":""},{"location":"chapter_computational_complexity/summary/#1","title":"1. \u00a0 \u91cd\u70b9\u56de\u987e","text":"\u7b97\u6cd5\u6548\u7387\u8bc4\u4f30
\u65f6\u95f4\u590d\u6742\u5ea6
\u7a7a\u95f4\u590d\u6742\u5ea6
\u5c3e\u9012\u5f52\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u662f \\(O(1)\\) \u5417\uff1f
\u7406\u8bba\u4e0a\uff0c\u5c3e\u9012\u5f52\u51fd\u6570\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u53ef\u4ee5\u4f18\u5316\u81f3 \\(O(1)\\) \u3002\u4e0d\u8fc7\u7edd\u5927\u591a\u6570\u7f16\u7a0b\u8bed\u8a00\uff08\u4f8b\u5982 Java\u3001Python\u3001C++\u3001Go\u3001C# \u7b49\uff09\u4e0d\u652f\u6301\u81ea\u52a8\u4f18\u5316\u5c3e\u9012\u5f52\uff0c\u56e0\u6b64\u901a\u5e38\u8ba4\u4e3a\u7a7a\u95f4\u590d\u6742\u5ea6\u662f \\(O(n)\\) \u3002
\u51fd\u6570\u548c\u65b9\u6cd5\u8fd9\u4e24\u4e2a\u672f\u8bed\u7684\u533a\u522b\u662f\u4ec0\u4e48\uff1f
\u300c\u51fd\u6570 function\u300d\u53ef\u4ee5\u88ab\u72ec\u7acb\u6267\u884c\uff0c\u6240\u6709\u53c2\u6570\u90fd\u4ee5\u663e\u5f0f\u4f20\u9012\u3002\u300c\u65b9\u6cd5 method\u300d\u4e0e\u4e00\u4e2a\u5bf9\u8c61\u5173\u8054\uff0c\u88ab\u9690\u5f0f\u4f20\u9012\u7ed9\u8c03\u7528\u5b83\u7684\u5bf9\u8c61\uff0c\u80fd\u591f\u5bf9\u7c7b\u7684\u5b9e\u4f8b\u4e2d\u5305\u542b\u7684\u6570\u636e\u8fdb\u884c\u64cd\u4f5c\u3002
\u4e0b\u9762\u4ee5\u51e0\u79cd\u5e38\u89c1\u7684\u7f16\u7a0b\u8bed\u8a00\u4e3a\u4f8b\u6765\u8bf4\u660e\u3002
\u56fe\u89e3\u201c\u5e38\u89c1\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u7c7b\u578b\u201d\u53cd\u6620\u7684\u662f\u5426\u662f\u5360\u7528\u7a7a\u95f4\u7684\u7edd\u5bf9\u5927\u5c0f\uff1f
\u4e0d\u662f\uff0c\u8be5\u56fe\u5c55\u793a\u7684\u662f\u7a7a\u95f4\u590d\u6742\u5ea6\uff0c\u5176\u53cd\u6620\u7684\u662f\u589e\u957f\u8d8b\u52bf\uff0c\u800c\u4e0d\u662f\u5360\u7528\u7a7a\u95f4\u7684\u7edd\u5bf9\u5927\u5c0f\u3002
\u5047\u8bbe\u53d6 \\(n = 8\\) \uff0c\u4f60\u53ef\u80fd\u4f1a\u53d1\u73b0\u6bcf\u6761\u66f2\u7ebf\u7684\u503c\u4e0e\u51fd\u6570\u5bf9\u5e94\u4e0d\u4e0a\u3002\u8fd9\u662f\u56e0\u4e3a\u6bcf\u6761\u66f2\u7ebf\u90fd\u5305\u542b\u4e00\u4e2a\u5e38\u6570\u9879\uff0c\u7528\u4e8e\u5c06\u53d6\u503c\u8303\u56f4\u538b\u7f29\u5230\u4e00\u4e2a\u89c6\u89c9\u8212\u9002\u7684\u8303\u56f4\u5185\u3002
\u5728\u5b9e\u9645\u4e2d\uff0c\u56e0\u4e3a\u6211\u4eec\u901a\u5e38\u4e0d\u77e5\u9053\u6bcf\u4e2a\u65b9\u6cd5\u7684\u201c\u5e38\u6570\u9879\u201d\u590d\u6742\u5ea6\u662f\u591a\u5c11\uff0c\u6240\u4ee5\u4e00\u822c\u65e0\u6cd5\u4ec5\u51ed\u590d\u6742\u5ea6\u6765\u9009\u62e9 \\(n = 8\\) \u4e4b\u4e0b\u7684\u6700\u4f18\u89e3\u6cd5\u3002\u4f46\u5bf9\u4e8e \\(n = 8^5\\) \u5c31\u5f88\u597d\u9009\u4e86\uff0c\u8fd9\u65f6\u589e\u957f\u8d8b\u52bf\u5df2\u7ecf\u5360\u4e3b\u5bfc\u4e86\u3002
"},{"location":"chapter_computational_complexity/time_complexity/","title":"2.3 \u00a0 \u65f6\u95f4\u590d\u6742\u5ea6","text":"\u8fd0\u884c\u65f6\u95f4\u53ef\u4ee5\u76f4\u89c2\u4e14\u51c6\u786e\u5730\u53cd\u6620\u7b97\u6cd5\u7684\u6548\u7387\u3002\u5982\u679c\u6211\u4eec\u60f3\u51c6\u786e\u9884\u4f30\u4e00\u6bb5\u4ee3\u7801\u7684\u8fd0\u884c\u65f6\u95f4\uff0c\u5e94\u8be5\u5982\u4f55\u64cd\u4f5c\u5462\uff1f
+
\u9700\u8981 1 ns \uff0c\u4e58\u6cd5\u64cd\u4f5c *
\u9700\u8981 10 ns \uff0c\u6253\u5370\u64cd\u4f5c print()
\u9700\u8981 5 ns \u7b49\u3002\u4f8b\u5982\u5728\u4ee5\u4e0b\u4ee3\u7801\u4e2d\uff0c\u8f93\u5165\u6570\u636e\u5927\u5c0f\u4e3a \\(n\\) \uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig# \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\ndef algorithm(n: int):\n a = 2 # 1 ns\n a = a + 1 # 1 ns\n a = a * 2 # 10 ns\n # \u5faa\u73af n \u6b21\n for _ in range(n): # 1 ns\n print(0) # 5 ns\n
// \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\nvoid algorithm(int n) {\n int a = 2; // 1 ns\n a = a + 1; // 1 ns\n a = a * 2; // 10 ns\n // \u5faa\u73af n \u6b21\n for (int i = 0; i < n; i++) { // 1 ns \uff0c\u6bcf\u8f6e\u90fd\u8981\u6267\u884c i++\n cout << 0 << endl; // 5 ns\n }\n}\n
// \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\nvoid algorithm(int n) {\n int a = 2; // 1 ns\n a = a + 1; // 1 ns\n a = a * 2; // 10 ns\n // \u5faa\u73af n \u6b21\n for (int i = 0; i < n; i++) { // 1 ns \uff0c\u6bcf\u8f6e\u90fd\u8981\u6267\u884c i++\n System.out.println(0); // 5 ns\n }\n}\n
// \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\nvoid Algorithm(int n) {\n int a = 2; // 1 ns\n a = a + 1; // 1 ns\n a = a * 2; // 10 ns\n // \u5faa\u73af n \u6b21\n for (int i = 0; i < n; i++) { // 1 ns \uff0c\u6bcf\u8f6e\u90fd\u8981\u6267\u884c i++\n Console.WriteLine(0); // 5 ns\n }\n}\n
// \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\nfunc algorithm(n int) {\n a := 2 // 1 ns\n a = a + 1 // 1 ns\n a = a * 2 // 10 ns\n // \u5faa\u73af n \u6b21\n for i := 0; i < n; i++ { // 1 ns\n fmt.Println(a) // 5 ns\n }\n}\n
// \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\nfunc algorithm(n: Int) {\n var a = 2 // 1 ns\n a = a + 1 // 1 ns\n a = a * 2 // 10 ns\n // \u5faa\u73af n \u6b21\n for _ in 0 ..< n { // 1 ns\n print(0) // 5 ns\n }\n}\n
// \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\nfunction algorithm(n) {\n var a = 2; // 1 ns\n a = a + 1; // 1 ns\n a = a * 2; // 10 ns\n // \u5faa\u73af n \u6b21\n for(let i = 0; i < n; i++) { // 1 ns \uff0c\u6bcf\u8f6e\u90fd\u8981\u6267\u884c i++\n console.log(0); // 5 ns\n }\n}\n
// \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\nfunction algorithm(n: number): void {\n var a: number = 2; // 1 ns\n a = a + 1; // 1 ns\n a = a * 2; // 10 ns\n // \u5faa\u73af n \u6b21\n for(let i = 0; i < n; i++) { // 1 ns \uff0c\u6bcf\u8f6e\u90fd\u8981\u6267\u884c i++\n console.log(0); // 5 ns\n }\n}\n
// \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\nvoid algorithm(int n) {\n int a = 2; // 1 ns\n a = a + 1; // 1 ns\n a = a * 2; // 10 ns\n // \u5faa\u73af n \u6b21\n for (int i = 0; i < n; i++) { // 1 ns \uff0c\u6bcf\u8f6e\u90fd\u8981\u6267\u884c i++\n print(0); // 5 ns\n }\n}\n
// \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\nfn algorithm(n: i32) {\n let mut a = 2; // 1 ns\n a = a + 1; // 1 ns\n a = a * 2; // 10 ns\n // \u5faa\u73af n \u6b21\n for _ in 0..n { // 1 ns \uff0c\u6bcf\u8f6e\u90fd\u8981\u6267\u884c i++\n println!(\"{}\", 0); // 5 ns\n }\n}\n
// \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\nvoid algorithm(int n) {\n int a = 2; // 1 ns\n a = a + 1; // 1 ns\n a = a * 2; // 10 ns\n // \u5faa\u73af n \u6b21\n for (int i = 0; i < n; i++) { // 1 ns \uff0c\u6bcf\u8f6e\u90fd\u8981\u6267\u884c i++\n printf(\"%d\", 0); // 5 ns\n }\n}\n
// \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\nfn algorithm(n: usize) void {\n var a: i32 = 2; // 1 ns\n a += 1; // 1 ns\n a *= 2; // 10 ns\n // \u5faa\u73af n \u6b21\n for (0..n) |_| { // 1 ns\n std.debug.print(\"{}\\n\", .{0}); // 5 ns\n }\n}\n
\u6839\u636e\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5f97\u5230\u7b97\u6cd5\u7684\u8fd0\u884c\u65f6\u95f4\u4e3a \\((6n + 12)\\) ns \uff1a
\\[ 1 + 1 + 10 + (1 + 5) \\times n = 6n + 12 \\]\u4f46\u5b9e\u9645\u4e0a\uff0c\u7edf\u8ba1\u7b97\u6cd5\u7684\u8fd0\u884c\u65f6\u95f4\u65e2\u4e0d\u5408\u7406\u4e5f\u4e0d\u73b0\u5b9e\u3002\u9996\u5148\uff0c\u6211\u4eec\u4e0d\u5e0c\u671b\u5c06\u9884\u4f30\u65f6\u95f4\u548c\u8fd0\u884c\u5e73\u53f0\u7ed1\u5b9a\uff0c\u56e0\u4e3a\u7b97\u6cd5\u9700\u8981\u5728\u5404\u79cd\u4e0d\u540c\u7684\u5e73\u53f0\u4e0a\u8fd0\u884c\u3002\u5176\u6b21\uff0c\u6211\u4eec\u5f88\u96be\u83b7\u77e5\u6bcf\u79cd\u64cd\u4f5c\u7684\u8fd0\u884c\u65f6\u95f4\uff0c\u8fd9\u7ed9\u9884\u4f30\u8fc7\u7a0b\u5e26\u6765\u4e86\u6781\u5927\u7684\u96be\u5ea6\u3002
"},{"location":"chapter_computational_complexity/time_complexity/#231","title":"2.3.1 \u00a0 \u7edf\u8ba1\u65f6\u95f4\u589e\u957f\u8d8b\u52bf","text":"\u65f6\u95f4\u590d\u6742\u5ea6\u5206\u6790\u7edf\u8ba1\u7684\u4e0d\u662f\u7b97\u6cd5\u8fd0\u884c\u65f6\u95f4\uff0c\u800c\u662f\u7b97\u6cd5\u8fd0\u884c\u65f6\u95f4\u968f\u7740\u6570\u636e\u91cf\u53d8\u5927\u65f6\u7684\u589e\u957f\u8d8b\u52bf\u3002
\u201c\u65f6\u95f4\u589e\u957f\u8d8b\u52bf\u201d\u8fd9\u4e2a\u6982\u5ff5\u6bd4\u8f83\u62bd\u8c61\uff0c\u6211\u4eec\u901a\u8fc7\u4e00\u4e2a\u4f8b\u5b50\u6765\u52a0\u4ee5\u7406\u89e3\u3002\u5047\u8bbe\u8f93\u5165\u6570\u636e\u5927\u5c0f\u4e3a \\(n\\) \uff0c\u7ed9\u5b9a\u4e09\u4e2a\u7b97\u6cd5 A
\u3001B
\u548c C
\uff1a
# \u7b97\u6cd5 A \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\ndef algorithm_A(n: int):\n print(0)\n# \u7b97\u6cd5 B \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\ndef algorithm_B(n: int):\n for _ in range(n):\n print(0)\n# \u7b97\u6cd5 C \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\ndef algorithm_C(n: int):\n for _ in range(1000000):\n print(0)\n
// \u7b97\u6cd5 A \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithm_A(int n) {\n cout << 0 << endl;\n}\n// \u7b97\u6cd5 B \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nvoid algorithm_B(int n) {\n for (int i = 0; i < n; i++) {\n cout << 0 << endl;\n }\n}\n// \u7b97\u6cd5 C \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithm_C(int n) {\n for (int i = 0; i < 1000000; i++) {\n cout << 0 << endl;\n }\n}\n
// \u7b97\u6cd5 A \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithm_A(int n) {\n System.out.println(0);\n}\n// \u7b97\u6cd5 B \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nvoid algorithm_B(int n) {\n for (int i = 0; i < n; i++) {\n System.out.println(0);\n }\n}\n// \u7b97\u6cd5 C \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithm_C(int n) {\n for (int i = 0; i < 1000000; i++) {\n System.out.println(0);\n }\n}\n
// \u7b97\u6cd5 A \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid AlgorithmA(int n) {\n Console.WriteLine(0);\n}\n// \u7b97\u6cd5 B \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nvoid AlgorithmB(int n) {\n for (int i = 0; i < n; i++) {\n Console.WriteLine(0);\n }\n}\n// \u7b97\u6cd5 C \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid AlgorithmC(int n) {\n for (int i = 0; i < 1000000; i++) {\n Console.WriteLine(0);\n }\n}\n
// \u7b97\u6cd5 A \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfunc algorithm_A(n int) {\n fmt.Println(0)\n}\n// \u7b97\u6cd5 B \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nfunc algorithm_B(n int) {\n for i := 0; i < n; i++ {\n fmt.Println(0)\n }\n}\n// \u7b97\u6cd5 C \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfunc algorithm_C(n int) {\n for i := 0; i < 1000000; i++ {\n fmt.Println(0)\n }\n}\n
// \u7b97\u6cd5 A \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfunc algorithmA(n: Int) {\n print(0)\n}\n\n// \u7b97\u6cd5 B \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nfunc algorithmB(n: Int) {\n for _ in 0 ..< n {\n print(0)\n }\n}\n\n// \u7b97\u6cd5 C \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfunc algorithmC(n: Int) {\n for _ in 0 ..< 1000000 {\n print(0)\n }\n}\n
// \u7b97\u6cd5 A \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfunction algorithm_A(n) {\n console.log(0);\n}\n// \u7b97\u6cd5 B \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nfunction algorithm_B(n) {\n for (let i = 0; i < n; i++) {\n console.log(0);\n }\n}\n// \u7b97\u6cd5 C \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfunction algorithm_C(n) {\n for (let i = 0; i < 1000000; i++) {\n console.log(0);\n }\n}\n
// \u7b97\u6cd5 A \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfunction algorithm_A(n: number): void {\n console.log(0);\n}\n// \u7b97\u6cd5 B \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nfunction algorithm_B(n: number): void {\n for (let i = 0; i < n; i++) {\n console.log(0);\n }\n}\n// \u7b97\u6cd5 C \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfunction algorithm_C(n: number): void {\n for (let i = 0; i < 1000000; i++) {\n console.log(0);\n }\n}\n
// \u7b97\u6cd5 A \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithmA(int n) {\n print(0);\n}\n// \u7b97\u6cd5 B \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nvoid algorithmB(int n) {\n for (int i = 0; i < n; i++) {\n print(0);\n }\n}\n// \u7b97\u6cd5 C \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithmC(int n) {\n for (int i = 0; i < 1000000; i++) {\n print(0);\n }\n}\n
// \u7b97\u6cd5 A \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfn algorithm_A(n: i32) {\n println!(\"{}\", 0);\n}\n// \u7b97\u6cd5 B \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nfn algorithm_B(n: i32) {\n for _ in 0..n {\n println!(\"{}\", 0);\n }\n}\n// \u7b97\u6cd5 C \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfn algorithm_C(n: i32) {\n for _ in 0..1000000 {\n println!(\"{}\", 0);\n }\n}\n
// \u7b97\u6cd5 A \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithm_A(int n) {\n printf(\"%d\", 0);\n}\n// \u7b97\u6cd5 B \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nvoid algorithm_B(int n) {\n for (int i = 0; i < n; i++) {\n printf(\"%d\", 0);\n }\n}\n// \u7b97\u6cd5 C \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithm_C(int n) {\n for (int i = 0; i < 1000000; i++) {\n printf(\"%d\", 0);\n }\n}\n
// \u7b97\u6cd5 A \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfn algorithm_A(n: usize) void {\n _ = n;\n std.debug.print(\"{}\\n\", .{0});\n}\n// \u7b97\u6cd5 B \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nfn algorithm_B(n: i32) void {\n for (0..n) |_| {\n std.debug.print(\"{}\\n\", .{0});\n }\n}\n// \u7b97\u6cd5 C \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfn algorithm_C(n: i32) void {\n _ = n;\n for (0..1000000) |_| { \n std.debug.print(\"{}\\n\", .{0});\n }\n}\n
\u56fe 2-7 \u5c55\u793a\u4e86\u4ee5\u4e0a\u4e09\u4e2a\u7b97\u6cd5\u51fd\u6570\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u3002
A
\u53ea\u6709 \\(1\\) \u4e2a\u6253\u5370\u64cd\u4f5c\uff0c\u7b97\u6cd5\u8fd0\u884c\u65f6\u95f4\u4e0d\u968f\u7740 \\(n\\) \u589e\u5927\u800c\u589e\u957f\u3002\u6211\u4eec\u79f0\u6b64\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a\u201c\u5e38\u6570\u9636\u201d\u3002B
\u4e2d\u7684\u6253\u5370\u64cd\u4f5c\u9700\u8981\u5faa\u73af \\(n\\) \u6b21\uff0c\u7b97\u6cd5\u8fd0\u884c\u65f6\u95f4\u968f\u7740 \\(n\\) \u589e\u5927\u5448\u7ebf\u6027\u589e\u957f\u3002\u6b64\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u88ab\u79f0\u4e3a\u201c\u7ebf\u6027\u9636\u201d\u3002C
\u4e2d\u7684\u6253\u5370\u64cd\u4f5c\u9700\u8981\u5faa\u73af \\(1000000\\) \u6b21\uff0c\u867d\u7136\u8fd0\u884c\u65f6\u95f4\u5f88\u957f\uff0c\u4f46\u5b83\u4e0e\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u65e0\u5173\u3002\u56e0\u6b64 C
\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u548c A
\u76f8\u540c\uff0c\u4ecd\u4e3a\u201c\u5e38\u6570\u9636\u201d\u3002\u56fe 2-7 \u00a0 \u7b97\u6cd5 A\u3001B \u548c C \u7684\u65f6\u95f4\u589e\u957f\u8d8b\u52bf
\u76f8\u8f83\u4e8e\u76f4\u63a5\u7edf\u8ba1\u7b97\u6cd5\u7684\u8fd0\u884c\u65f6\u95f4\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u5206\u6790\u6709\u54ea\u4e9b\u7279\u70b9\u5462\uff1f
B
\u7684\u8fd0\u884c\u65f6\u95f4\u5448\u7ebf\u6027\u589e\u957f\uff0c\u5728 \\(n > 1\\) \u65f6\u6bd4\u7b97\u6cd5 A
\u66f4\u6162\uff0c\u5728 \\(n > 1000000\\) \u65f6\u6bd4\u7b97\u6cd5 C
\u66f4\u6162\u3002\u4e8b\u5b9e\u4e0a\uff0c\u53ea\u8981\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u8db3\u591f\u5927\uff0c\u590d\u6742\u5ea6\u4e3a\u201c\u5e38\u6570\u9636\u201d\u7684\u7b97\u6cd5\u4e00\u5b9a\u4f18\u4e8e\u201c\u7ebf\u6027\u9636\u201d\u7684\u7b97\u6cd5\uff0c\u8fd9\u6b63\u662f\u65f6\u95f4\u589e\u957f\u8d8b\u52bf\u7684\u542b\u4e49\u3002A
\u548c C
\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u76f8\u540c\uff0c\u4f46\u5b9e\u9645\u8fd0\u884c\u65f6\u95f4\u5dee\u522b\u5f88\u5927\u3002\u540c\u6837\uff0c\u5c3d\u7ba1\u7b97\u6cd5 B
\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u6bd4 C
\u9ad8\uff0c\u4f46\u5728\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u8f83\u5c0f\u65f6\uff0c\u7b97\u6cd5 B
\u660e\u663e\u4f18\u4e8e\u7b97\u6cd5 C
\u3002\u5728\u8fd9\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u5f88\u96be\u4ec5\u51ed\u65f6\u95f4\u590d\u6742\u5ea6\u5224\u65ad\u7b97\u6cd5\u6548\u7387\u7684\u9ad8\u4f4e\u3002\u5f53\u7136\uff0c\u5c3d\u7ba1\u5b58\u5728\u4e0a\u8ff0\u95ee\u9898\uff0c\u590d\u6742\u5ea6\u5206\u6790\u4ecd\u7136\u662f\u8bc4\u5224\u7b97\u6cd5\u6548\u7387\u6700\u6709\u6548\u4e14\u5e38\u7528\u7684\u65b9\u6cd5\u3002\u7ed9\u5b9a\u4e00\u4e2a\u8f93\u5165\u5927\u5c0f\u4e3a \\(n\\) \u7684\u51fd\u6570\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZigdef algorithm(n: int):\n a = 1 # +1\n a = a + 1 # +1\n a = a * 2 # +1\n # \u5faa\u73af n \u6b21\n for i in range(n): # +1\n print(0) # +1\n
void algorithm(int n) {\n int a = 1; // +1\n a = a + 1; // +1\n a = a * 2; // +1\n // \u5faa\u73af n \u6b21\n for (int i = 0; i < n; i++) { // +1\uff08\u6bcf\u8f6e\u90fd\u6267\u884c i ++\uff09\n cout << 0 << endl; // +1\n }\n}\n
void algorithm(int n) {\n int a = 1; // +1\n a = a + 1; // +1\n a = a * 2; // +1\n // \u5faa\u73af n \u6b21\n for (int i = 0; i < n; i++) { // +1\uff08\u6bcf\u8f6e\u90fd\u6267\u884c i ++\uff09\n System.out.println(0); // +1\n }\n}\n
void Algorithm(int n) {\n int a = 1; // +1\n a = a + 1; // +1\n a = a * 2; // +1\n // \u5faa\u73af n \u6b21\n for (int i = 0; i < n; i++) { // +1\uff08\u6bcf\u8f6e\u90fd\u6267\u884c i ++\uff09\n Console.WriteLine(0); // +1\n }\n}\n
func algorithm(n int) {\n a := 1 // +1\n a = a + 1 // +1\n a = a * 2 // +1\n // \u5faa\u73af n \u6b21\n for i := 0; i < n; i++ { // +1\n fmt.Println(a) // +1\n }\n}\n
func algorithm(n: Int) {\n var a = 1 // +1\n a = a + 1 // +1\n a = a * 2 // +1\n // \u5faa\u73af n \u6b21\n for _ in 0 ..< n { // +1\n print(0) // +1\n }\n}\n
function algorithm(n) {\n var a = 1; // +1\n a += 1; // +1\n a *= 2; // +1\n // \u5faa\u73af n \u6b21\n for(let i = 0; i < n; i++){ // +1\uff08\u6bcf\u8f6e\u90fd\u6267\u884c i ++\uff09\n console.log(0); // +1\n }\n}\n
function algorithm(n: number): void{\n var a: number = 1; // +1\n a += 1; // +1\n a *= 2; // +1\n // \u5faa\u73af n \u6b21\n for(let i = 0; i < n; i++){ // +1\uff08\u6bcf\u8f6e\u90fd\u6267\u884c i ++\uff09\n console.log(0); // +1\n }\n}\n
void algorithm(int n) {\n int a = 1; // +1\n a = a + 1; // +1\n a = a * 2; // +1\n // \u5faa\u73af n \u6b21\n for (int i = 0; i < n; i++) { // +1\uff08\u6bcf\u8f6e\u90fd\u6267\u884c i ++\uff09\n print(0); // +1\n }\n}\n
fn algorithm(n: i32) {\n let mut a = 1; // +1\n a = a + 1; // +1\n a = a * 2; // +1\n\n // \u5faa\u73af n \u6b21\n for _ in 0..n { // +1\uff08\u6bcf\u8f6e\u90fd\u6267\u884c i ++\uff09\n println!(\"{}\", 0); // +1\n }\n}\n
void algorithm(int n) {\n int a = 1; // +1\n a = a + 1; // +1\n a = a * 2; // +1\n // \u5faa\u73af n \u6b21\n for (int i = 0; i < n; i++) { // +1\uff08\u6bcf\u8f6e\u90fd\u6267\u884c i ++\uff09\n printf(\"%d\", 0); // +1\n }\n} \n
fn algorithm(n: usize) void {\n var a: i32 = 1; // +1\n a += 1; // +1\n a *= 2; // +1\n // \u5faa\u73af n \u6b21\n for (0..n) |_| { // +1\uff08\u6bcf\u8f6e\u90fd\u6267\u884c i ++\uff09\n std.debug.print(\"{}\\n\", .{0}); // +1\n }\n}\n
\u8bbe\u7b97\u6cd5\u7684\u64cd\u4f5c\u6570\u91cf\u662f\u4e00\u4e2a\u5173\u4e8e\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u7684\u51fd\u6570\uff0c\u8bb0\u4e3a \\(T(n)\\) \uff0c\u5219\u4ee5\u4e0a\u51fd\u6570\u7684\u64cd\u4f5c\u6570\u91cf\u4e3a\uff1a
\\[ T(n) = 3 + 2n \\]\\(T(n)\\) \u662f\u4e00\u6b21\u51fd\u6570\uff0c\u8bf4\u660e\u5176\u8fd0\u884c\u65f6\u95f4\u7684\u589e\u957f\u8d8b\u52bf\u662f\u7ebf\u6027\u7684\uff0c\u56e0\u6b64\u5b83\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u662f\u7ebf\u6027\u9636\u3002
\u6211\u4eec\u5c06\u7ebf\u6027\u9636\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u8bb0\u4e3a \\(O(n)\\) \uff0c\u8fd9\u4e2a\u6570\u5b66\u7b26\u53f7\u79f0\u4e3a\u300c\u5927 \\(O\\) \u8bb0\u53f7 big-\\(O\\) notation\u300d\uff0c\u8868\u793a\u51fd\u6570 \\(T(n)\\) \u7684\u300c\u6e10\u8fd1\u4e0a\u754c asymptotic upper bound\u300d\u3002
\u65f6\u95f4\u590d\u6742\u5ea6\u5206\u6790\u672c\u8d28\u4e0a\u662f\u8ba1\u7b97\u201c\u64cd\u4f5c\u6570\u91cf \\(T(n)\\)\u201d\u7684\u6e10\u8fd1\u4e0a\u754c\uff0c\u5b83\u5177\u6709\u660e\u786e\u7684\u6570\u5b66\u5b9a\u4e49\u3002
\u51fd\u6570\u6e10\u8fd1\u4e0a\u754c
\u82e5\u5b58\u5728\u6b63\u5b9e\u6570 \\(c\\) \u548c\u5b9e\u6570 \\(n_0\\) \uff0c\u4f7f\u5f97\u5bf9\u4e8e\u6240\u6709\u7684 \\(n > n_0\\) \uff0c\u5747\u6709 \\(T(n) \\leq c \\cdot f(n)\\) \uff0c\u5219\u53ef\u8ba4\u4e3a \\(f(n)\\) \u7ed9\u51fa\u4e86 \\(T(n)\\) \u7684\u4e00\u4e2a\u6e10\u8fd1\u4e0a\u754c\uff0c\u8bb0\u4e3a \\(T(n) = O(f(n))\\) \u3002
\u5982\u56fe 2-8 \u6240\u793a\uff0c\u8ba1\u7b97\u6e10\u8fd1\u4e0a\u754c\u5c31\u662f\u5bfb\u627e\u4e00\u4e2a\u51fd\u6570 \\(f(n)\\) \uff0c\u4f7f\u5f97\u5f53 \\(n\\) \u8d8b\u5411\u4e8e\u65e0\u7a77\u5927\u65f6\uff0c\\(T(n)\\) \u548c \\(f(n)\\) \u5904\u4e8e\u76f8\u540c\u7684\u589e\u957f\u7ea7\u522b\uff0c\u4ec5\u76f8\u5dee\u4e00\u4e2a\u5e38\u6570\u9879 \\(c\\) \u7684\u500d\u6570\u3002
\u56fe 2-8 \u00a0 \u51fd\u6570\u7684\u6e10\u8fd1\u4e0a\u754c
"},{"location":"chapter_computational_complexity/time_complexity/#233","title":"2.3.3 \u00a0 \u63a8\u7b97\u65b9\u6cd5","text":"\u6e10\u8fd1\u4e0a\u754c\u7684\u6570\u5b66\u5473\u513f\u6709\u70b9\u91cd\uff0c\u5982\u679c\u4f60\u611f\u89c9\u6ca1\u6709\u5b8c\u5168\u7406\u89e3\uff0c\u4e5f\u65e0\u987b\u62c5\u5fc3\u3002\u6211\u4eec\u53ef\u4ee5\u5148\u638c\u63e1\u63a8\u7b97\u65b9\u6cd5\uff0c\u5728\u4e0d\u65ad\u7684\u5b9e\u8df5\u4e2d\uff0c\u5c31\u53ef\u4ee5\u9010\u6e10\u9886\u609f\u5176\u6570\u5b66\u610f\u4e49\u3002
\u6839\u636e\u5b9a\u4e49\uff0c\u786e\u5b9a \\(f(n)\\) \u4e4b\u540e\uff0c\u6211\u4eec\u4fbf\u53ef\u5f97\u5230\u65f6\u95f4\u590d\u6742\u5ea6 \\(O(f(n))\\) \u3002\u90a3\u4e48\u5982\u4f55\u786e\u5b9a\u6e10\u8fd1\u4e0a\u754c \\(f(n)\\) \u5462\uff1f\u603b\u4f53\u5206\u4e3a\u4e24\u6b65\uff1a\u9996\u5148\u7edf\u8ba1\u64cd\u4f5c\u6570\u91cf\uff0c\u7136\u540e\u5224\u65ad\u6e10\u8fd1\u4e0a\u754c\u3002
"},{"location":"chapter_computational_complexity/time_complexity/#1","title":"1. \u00a0 \u7b2c\u4e00\u6b65\uff1a\u7edf\u8ba1\u64cd\u4f5c\u6570\u91cf","text":"\u9488\u5bf9\u4ee3\u7801\uff0c\u9010\u884c\u4ece\u4e0a\u5230\u4e0b\u8ba1\u7b97\u5373\u53ef\u3002\u7136\u800c\uff0c\u7531\u4e8e\u4e0a\u8ff0 \\(c \\cdot f(n)\\) \u4e2d\u7684\u5e38\u6570\u9879 \\(c\\) \u53ef\u4ee5\u53d6\u4efb\u610f\u5927\u5c0f\uff0c\u56e0\u6b64\u64cd\u4f5c\u6570\u91cf \\(T(n)\\) \u4e2d\u7684\u5404\u79cd\u7cfb\u6570\u3001\u5e38\u6570\u9879\u90fd\u53ef\u4ee5\u5ffd\u7565\u3002\u6839\u636e\u6b64\u539f\u5219\uff0c\u53ef\u4ee5\u603b\u7ed3\u51fa\u4ee5\u4e0b\u8ba1\u6570\u7b80\u5316\u6280\u5de7\u3002
1.
\u70b9\u548c\u7b2c 2.
\u70b9\u7684\u6280\u5de7\u3002\u7ed9\u5b9a\u4e00\u4e2a\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u7528\u4e0a\u8ff0\u6280\u5de7\u6765\u7edf\u8ba1\u64cd\u4f5c\u6570\u91cf\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZigdef algorithm(n: int):\n a = 1 # +0\uff08\u6280\u5de7 1\uff09\n a = a + n # +0\uff08\u6280\u5de7 1\uff09\n # +n\uff08\u6280\u5de7 2\uff09\n for i in range(5 * n + 1):\n print(0)\n # +n*n\uff08\u6280\u5de7 3\uff09\n for i in range(2 * n):\n for j in range(n + 1):\n print(0)\n
void algorithm(int n) {\n int a = 1; // +0\uff08\u6280\u5de7 1\uff09\n a = a + n; // +0\uff08\u6280\u5de7 1\uff09\n // +n\uff08\u6280\u5de7 2\uff09\n for (int i = 0; i < 5 * n + 1; i++) {\n cout << 0 << endl;\n }\n // +n*n\uff08\u6280\u5de7 3\uff09\n for (int i = 0; i < 2 * n; i++) {\n for (int j = 0; j < n + 1; j++) {\n cout << 0 << endl;\n }\n }\n}\n
void algorithm(int n) {\n int a = 1; // +0\uff08\u6280\u5de7 1\uff09\n a = a + n; // +0\uff08\u6280\u5de7 1\uff09\n // +n\uff08\u6280\u5de7 2\uff09\n for (int i = 0; i < 5 * n + 1; i++) {\n System.out.println(0);\n }\n // +n*n\uff08\u6280\u5de7 3\uff09\n for (int i = 0; i < 2 * n; i++) {\n for (int j = 0; j < n + 1; j++) {\n System.out.println(0);\n }\n }\n}\n
void Algorithm(int n) {\n int a = 1; // +0\uff08\u6280\u5de7 1\uff09\n a = a + n; // +0\uff08\u6280\u5de7 1\uff09\n // +n\uff08\u6280\u5de7 2\uff09\n for (int i = 0; i < 5 * n + 1; i++) {\n Console.WriteLine(0);\n }\n // +n*n\uff08\u6280\u5de7 3\uff09\n for (int i = 0; i < 2 * n; i++) {\n for (int j = 0; j < n + 1; j++) {\n Console.WriteLine(0);\n }\n }\n}\n
func algorithm(n int) {\n a := 1 // +0\uff08\u6280\u5de7 1\uff09\n a = a + n // +0\uff08\u6280\u5de7 1\uff09\n // +n\uff08\u6280\u5de7 2\uff09\n for i := 0; i < 5 * n + 1; i++ {\n fmt.Println(0)\n }\n // +n*n\uff08\u6280\u5de7 3\uff09\n for i := 0; i < 2 * n; i++ {\n for j := 0; j < n + 1; j++ {\n fmt.Println(0)\n }\n }\n}\n
func algorithm(n: Int) {\n var a = 1 // +0\uff08\u6280\u5de7 1\uff09\n a = a + n // +0\uff08\u6280\u5de7 1\uff09\n // +n\uff08\u6280\u5de7 2\uff09\n for _ in 0 ..< (5 * n + 1) {\n print(0)\n }\n // +n*n\uff08\u6280\u5de7 3\uff09\n for _ in 0 ..< (2 * n) {\n for _ in 0 ..< (n + 1) {\n print(0)\n }\n }\n}\n
function algorithm(n) {\n let a = 1; // +0\uff08\u6280\u5de7 1\uff09\n a = a + n; // +0\uff08\u6280\u5de7 1\uff09\n // +n\uff08\u6280\u5de7 2\uff09\n for (let i = 0; i < 5 * n + 1; i++) {\n console.log(0);\n }\n // +n*n\uff08\u6280\u5de7 3\uff09\n for (let i = 0; i < 2 * n; i++) {\n for (let j = 0; j < n + 1; j++) {\n console.log(0);\n }\n }\n}\n
function algorithm(n: number): void {\n let a = 1; // +0\uff08\u6280\u5de7 1\uff09\n a = a + n; // +0\uff08\u6280\u5de7 1\uff09\n // +n\uff08\u6280\u5de7 2\uff09\n for (let i = 0; i < 5 * n + 1; i++) {\n console.log(0);\n }\n // +n*n\uff08\u6280\u5de7 3\uff09\n for (let i = 0; i < 2 * n; i++) {\n for (let j = 0; j < n + 1; j++) {\n console.log(0);\n }\n }\n}\n
void algorithm(int n) {\n int a = 1; // +0\uff08\u6280\u5de7 1\uff09\n a = a + n; // +0\uff08\u6280\u5de7 1\uff09\n // +n\uff08\u6280\u5de7 2\uff09\n for (int i = 0; i < 5 * n + 1; i++) {\n print(0);\n }\n // +n*n\uff08\u6280\u5de7 3\uff09\n for (int i = 0; i < 2 * n; i++) {\n for (int j = 0; j < n + 1; j++) {\n print(0);\n }\n }\n}\n
fn algorithm(n: i32) {\n let mut a = 1; // +0\uff08\u6280\u5de7 1\uff09\n a = a + n; // +0\uff08\u6280\u5de7 1\uff09\n\n // +n\uff08\u6280\u5de7 2\uff09\n for i in 0..(5 * n + 1) {\n println!(\"{}\", 0);\n }\n\n // +n*n\uff08\u6280\u5de7 3\uff09\n for i in 0..(2 * n) {\n for j in 0..(n + 1) {\n println!(\"{}\", 0);\n }\n }\n}\n
void algorithm(int n) {\n int a = 1; // +0\uff08\u6280\u5de7 1\uff09\n a = a + n; // +0\uff08\u6280\u5de7 1\uff09\n // +n\uff08\u6280\u5de7 2\uff09\n for (int i = 0; i < 5 * n + 1; i++) {\n printf(\"%d\", 0);\n }\n // +n*n\uff08\u6280\u5de7 3\uff09\n for (int i = 0; i < 2 * n; i++) {\n for (int j = 0; j < n + 1; j++) {\n printf(\"%d\", 0);\n }\n }\n}\n
fn algorithm(n: usize) void {\n var a: i32 = 1; // +0\uff08\u6280\u5de7 1\uff09\n a = a + @as(i32, @intCast(n)); // +0\uff08\u6280\u5de7 1\uff09\n\n // +n\uff08\u6280\u5de7 2\uff09\n for(0..(5 * n + 1)) |_| {\n std.debug.print(\"{}\\n\", .{0}); \n }\n\n // +n*n\uff08\u6280\u5de7 3\uff09\n for(0..(2 * n)) |_| {\n for(0..(n + 1)) |_| {\n std.debug.print(\"{}\\n\", .{0}); \n }\n }\n}\n
\u4ee5\u4e0b\u516c\u5f0f\u5c55\u793a\u4e86\u4f7f\u7528\u4e0a\u8ff0\u6280\u5de7\u524d\u540e\u7684\u7edf\u8ba1\u7ed3\u679c\uff0c\u4e24\u8005\u63a8\u7b97\u51fa\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u90fd\u4e3a \\(O(n^2)\\) \u3002
\\[ \\begin{aligned} T(n) & = 2n(n + 1) + (5n + 1) + 2 & \\text{\u5b8c\u6574\u7edf\u8ba1 (-.-|||)} \\newline & = 2n^2 + 7n + 3 \\newline T(n) & = n^2 + n & \\text{\u5077\u61d2\u7edf\u8ba1 (o.O)} \\end{aligned} \\]"},{"location":"chapter_computational_complexity/time_complexity/#2","title":"2. \u00a0 \u7b2c\u4e8c\u6b65\uff1a\u5224\u65ad\u6e10\u8fd1\u4e0a\u754c","text":"\u65f6\u95f4\u590d\u6742\u5ea6\u7531 \\(T(n)\\) \u4e2d\u6700\u9ad8\u9636\u7684\u9879\u6765\u51b3\u5b9a\u3002\u8fd9\u662f\u56e0\u4e3a\u5728 \\(n\\) \u8d8b\u4e8e\u65e0\u7a77\u5927\u65f6\uff0c\u6700\u9ad8\u9636\u7684\u9879\u5c06\u53d1\u6325\u4e3b\u5bfc\u4f5c\u7528\uff0c\u5176\u4ed6\u9879\u7684\u5f71\u54cd\u90fd\u53ef\u4ee5\u5ffd\u7565\u3002
\u8868 2-2 \u5c55\u793a\u4e86\u4e00\u4e9b\u4f8b\u5b50\uff0c\u5176\u4e2d\u4e00\u4e9b\u5938\u5f20\u7684\u503c\u662f\u4e3a\u4e86\u5f3a\u8c03\u201c\u7cfb\u6570\u65e0\u6cd5\u64bc\u52a8\u9636\u6570\u201d\u8fd9\u4e00\u7ed3\u8bba\u3002\u5f53 \\(n\\) \u8d8b\u4e8e\u65e0\u7a77\u5927\u65f6\uff0c\u8fd9\u4e9b\u5e38\u6570\u53d8\u5f97\u65e0\u8db3\u8f7b\u91cd\u3002
\u8868 2-2 \u00a0 \u4e0d\u540c\u64cd\u4f5c\u6570\u91cf\u5bf9\u5e94\u7684\u65f6\u95f4\u590d\u6742\u5ea6
\u64cd\u4f5c\u6570\u91cf \\(T(n)\\) \u65f6\u95f4\u590d\u6742\u5ea6 \\(O(f(n))\\) \\(100000\\) \\(O(1)\\) \\(3n + 2\\) \\(O(n)\\) \\(2n^2 + 3n + 2\\) \\(O(n^2)\\) \\(n^3 + 10000n^2\\) \\(O(n^3)\\) \\(2^n + 10000n^{10000}\\) \\(O(2^n)\\)"},{"location":"chapter_computational_complexity/time_complexity/#234","title":"2.3.4 \u00a0 \u5e38\u89c1\u7c7b\u578b","text":"\u8bbe\u8f93\u5165\u6570\u636e\u5927\u5c0f\u4e3a \\(n\\) \uff0c\u5e38\u89c1\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u7c7b\u578b\u5982\u56fe 2-9 \u6240\u793a\uff08\u6309\u7167\u4ece\u4f4e\u5230\u9ad8\u7684\u987a\u5e8f\u6392\u5217\uff09\u3002
\\[ \\begin{aligned} O(1) < O(\\log n) < O(n) < O(n \\log n) < O(n^2) < O(2^n) < O(n!) \\newline \\text{\u5e38\u6570\u9636} < \\text{\u5bf9\u6570\u9636} < \\text{\u7ebf\u6027\u9636} < \\text{\u7ebf\u6027\u5bf9\u6570\u9636} < \\text{\u5e73\u65b9\u9636} < \\text{\u6307\u6570\u9636} < \\text{\u9636\u4e58\u9636} \\end{aligned} \\]\u56fe 2-9 \u00a0 \u5e38\u89c1\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u7c7b\u578b
"},{"location":"chapter_computational_complexity/time_complexity/#1-o1","title":"1. \u00a0 \u5e38\u6570\u9636 \\(O(1)\\)","text":"\u5e38\u6570\u9636\u7684\u64cd\u4f5c\u6570\u91cf\u4e0e\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u65e0\u5173\uff0c\u5373\u4e0d\u968f\u7740 \\(n\\) \u7684\u53d8\u5316\u800c\u53d8\u5316\u3002
\u5728\u4ee5\u4e0b\u51fd\u6570\u4e2d\uff0c\u5c3d\u7ba1\u64cd\u4f5c\u6570\u91cf size
\u53ef\u80fd\u5f88\u5927\uff0c\u4f46\u7531\u4e8e\u5176\u4e0e\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u65e0\u5173\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4ecd\u4e3a \\(O(1)\\) \uff1a
def constant(n: int) -> int:\n \"\"\"\u5e38\u6570\u9636\"\"\"\n count = 0\n size = 100000\n for _ in range(size):\n count += 1\n return count\n
time_complexity.cpp/* \u5e38\u6570\u9636 */\nint constant(int n) {\n int count = 0;\n int size = 100000;\n for (int i = 0; i < size; i++)\n count++;\n return count;\n}\n
time_complexity.java/* \u5e38\u6570\u9636 */\nint constant(int n) {\n int count = 0;\n int size = 100000;\n for (int i = 0; i < size; i++)\n count++;\n return count;\n}\n
time_complexity.cs/* \u5e38\u6570\u9636 */\nint Constant(int n) {\n int count = 0;\n int size = 100000;\n for (int i = 0; i < size; i++)\n count++;\n return count;\n}\n
time_complexity.go/* \u5e38\u6570\u9636 */\nfunc constant(n int) int {\n count := 0\n size := 100000\n for i := 0; i < size; i++ {\n count++\n }\n return count\n}\n
time_complexity.swift/* \u5e38\u6570\u9636 */\nfunc constant(n: Int) -> Int {\n var count = 0\n let size = 100_000\n for _ in 0 ..< size {\n count += 1\n }\n return count\n}\n
time_complexity.js/* \u5e38\u6570\u9636 */\nfunction constant(n) {\n let count = 0;\n const size = 100000;\n for (let i = 0; i < size; i++) count++;\n return count;\n}\n
time_complexity.ts/* \u5e38\u6570\u9636 */\nfunction constant(n: number): number {\n let count = 0;\n const size = 100000;\n for (let i = 0; i < size; i++) count++;\n return count;\n}\n
time_complexity.dart/* \u5e38\u6570\u9636 */\nint constant(int n) {\n int count = 0;\n int size = 100000;\n for (var i = 0; i < size; i++) {\n count++;\n }\n return count;\n}\n
time_complexity.rs/* \u5e38\u6570\u9636 */\nfn constant(n: i32) -> i32 {\n _ = n;\n let mut count = 0;\n let size = 100_000;\n for _ in 0..size {\n count += 1;\n }\n count\n}\n
time_complexity.c/* \u5e38\u6570\u9636 */\nint constant(int n) {\n int count = 0;\n int size = 100000;\n int i = 0;\n for (int i = 0; i < size; i++) {\n count++;\n }\n return count;\n}\n
time_complexity.zig// \u5e38\u6570\u9636\nfn constant(n: i32) i32 {\n _ = n;\n var count: i32 = 0;\n const size: i32 = 100_000;\n var i: i32 = 0;\n while(i<size) : (i += 1) {\n count += 1;\n }\n return count;\n}\n
"},{"location":"chapter_computational_complexity/time_complexity/#2-on","title":"2. \u00a0 \u7ebf\u6027\u9636 \\(O(n)\\)","text":"\u7ebf\u6027\u9636\u7684\u64cd\u4f5c\u6570\u91cf\u76f8\u5bf9\u4e8e\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u4ee5\u7ebf\u6027\u7ea7\u522b\u589e\u957f\u3002\u7ebf\u6027\u9636\u901a\u5e38\u51fa\u73b0\u5728\u5355\u5c42\u5faa\u73af\u4e2d\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig time_complexity.pydef linear(n: int) -> int:\n \"\"\"\u7ebf\u6027\u9636\"\"\"\n count = 0\n for _ in range(n):\n count += 1\n return count\n
time_complexity.cpp/* \u7ebf\u6027\u9636 */\nint linear(int n) {\n int count = 0;\n for (int i = 0; i < n; i++)\n count++;\n return count;\n}\n
time_complexity.java/* \u7ebf\u6027\u9636 */\nint linear(int n) {\n int count = 0;\n for (int i = 0; i < n; i++)\n count++;\n return count;\n}\n
time_complexity.cs/* \u7ebf\u6027\u9636 */\nint Linear(int n) {\n int count = 0;\n for (int i = 0; i < n; i++)\n count++;\n return count;\n}\n
time_complexity.go/* \u7ebf\u6027\u9636 */\nfunc linear(n int) int {\n count := 0\n for i := 0; i < n; i++ {\n count++\n }\n return count\n}\n
time_complexity.swift/* \u7ebf\u6027\u9636 */\nfunc linear(n: Int) -> Int {\n var count = 0\n for _ in 0 ..< n {\n count += 1\n }\n return count\n}\n
time_complexity.js/* \u7ebf\u6027\u9636 */\nfunction linear(n) {\n let count = 0;\n for (let i = 0; i < n; i++) count++;\n return count;\n}\n
time_complexity.ts/* \u7ebf\u6027\u9636 */\nfunction linear(n: number): number {\n let count = 0;\n for (let i = 0; i < n; i++) count++;\n return count;\n}\n
time_complexity.dart/* \u7ebf\u6027\u9636 */\nint linear(int n) {\n int count = 0;\n for (var i = 0; i < n; i++) {\n count++;\n }\n return count;\n}\n
time_complexity.rs/* \u7ebf\u6027\u9636 */\nfn linear(n: i32) -> i32 {\n let mut count = 0;\n for _ in 0..n {\n count += 1;\n }\n count\n}\n
time_complexity.c/* \u7ebf\u6027\u9636 */\nint linear(int n) {\n int count = 0;\n for (int i = 0; i < n; i++) {\n count++;\n }\n return count;\n}\n
time_complexity.zig// \u7ebf\u6027\u9636\nfn linear(n: i32) i32 {\n var count: i32 = 0;\n var i: i32 = 0;\n while (i < n) : (i += 1) {\n count += 1;\n }\n return count;\n}\n
\u904d\u5386\u6570\u7ec4\u548c\u904d\u5386\u94fe\u8868\u7b49\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u5747\u4e3a \\(O(n)\\) \uff0c\u5176\u4e2d \\(n\\) \u4e3a\u6570\u7ec4\u6216\u94fe\u8868\u7684\u957f\u5ea6\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig time_complexity.pydef array_traversal(nums: list[int]) -> int:\n \"\"\"\u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09\"\"\"\n count = 0\n # \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\n for num in nums:\n count += 1\n return count\n
time_complexity.cpp/* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nint arrayTraversal(vector<int> &nums) {\n int count = 0;\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\n for (int num : nums) {\n count++;\n }\n return count;\n}\n
time_complexity.java/* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nint arrayTraversal(int[] nums) {\n int count = 0;\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\n for (int num : nums) {\n count++;\n }\n return count;\n}\n
time_complexity.cs/* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nint ArrayTraversal(int[] nums) {\n int count = 0;\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\n foreach (int num in nums) {\n count++;\n }\n return count;\n}\n
time_complexity.go/* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nfunc arrayTraversal(nums []int) int {\n count := 0\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\n for range nums {\n count++\n }\n return count\n}\n
time_complexity.swift/* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nfunc arrayTraversal(nums: [Int]) -> Int {\n var count = 0\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\n for _ in nums {\n count += 1\n }\n return count\n}\n
time_complexity.js/* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nfunction arrayTraversal(nums) {\n let count = 0;\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\n for (let i = 0; i < nums.length; i++) {\n count++;\n }\n return count;\n}\n
time_complexity.ts/* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nfunction arrayTraversal(nums: number[]): number {\n let count = 0;\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\n for (let i = 0; i < nums.length; i++) {\n count++;\n }\n return count;\n}\n
time_complexity.dart/* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nint arrayTraversal(List<int> nums) {\n int count = 0;\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\n for (var _num in nums) {\n count++;\n }\n return count;\n}\n
time_complexity.rs/* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nfn array_traversal(nums: &[i32]) -> i32 {\n let mut count = 0;\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\n for _ in nums {\n count += 1;\n }\n count\n}\n
time_complexity.c/* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nint arrayTraversal(int *nums, int n) {\n int count = 0;\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\n for (int i = 0; i < n; i++) {\n count++;\n }\n return count;\n}\n
time_complexity.zig// \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09\nfn arrayTraversal(nums: []i32) i32 {\n var count: i32 = 0;\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\n for (nums) |_| {\n count += 1;\n }\n return count;\n}\n
\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u9700\u6839\u636e\u8f93\u5165\u6570\u636e\u7684\u7c7b\u578b\u6765\u5177\u4f53\u786e\u5b9a\u3002\u6bd4\u5982\u5728\u7b2c\u4e00\u4e2a\u793a\u4f8b\u4e2d\uff0c\u53d8\u91cf \\(n\\) \u4e3a\u8f93\u5165\u6570\u636e\u5927\u5c0f\uff1b\u5728\u7b2c\u4e8c\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6570\u7ec4\u957f\u5ea6 \\(n\\) \u4e3a\u6570\u636e\u5927\u5c0f\u3002
"},{"location":"chapter_computational_complexity/time_complexity/#3-on2","title":"3. \u00a0 \u5e73\u65b9\u9636 \\(O(n^2)\\)","text":"\u5e73\u65b9\u9636\u7684\u64cd\u4f5c\u6570\u91cf\u76f8\u5bf9\u4e8e\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u4ee5\u5e73\u65b9\u7ea7\u522b\u589e\u957f\u3002\u5e73\u65b9\u9636\u901a\u5e38\u51fa\u73b0\u5728\u5d4c\u5957\u5faa\u73af\u4e2d\uff0c\u5916\u5c42\u5faa\u73af\u548c\u5185\u5c42\u5faa\u73af\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u90fd\u4e3a \\(O(n)\\) \uff0c\u56e0\u6b64\u603b\u4f53\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\) \uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig time_complexity.pydef quadratic(n: int) -> int:\n \"\"\"\u5e73\u65b9\u9636\"\"\"\n count = 0\n # \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u5e73\u65b9\u5173\u7cfb\n for i in range(n):\n for j in range(n):\n count += 1\n return count\n
time_complexity.cpp/* \u5e73\u65b9\u9636 */\nint quadratic(int n) {\n int count = 0;\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u5e73\u65b9\u5173\u7cfb\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n count++;\n }\n }\n return count;\n}\n
time_complexity.java/* \u5e73\u65b9\u9636 */\nint quadratic(int n) {\n int count = 0;\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u5e73\u65b9\u5173\u7cfb\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n count++;\n }\n }\n return count;\n}\n
time_complexity.cs/* \u5e73\u65b9\u9636 */\nint Quadratic(int n) {\n int count = 0;\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u5e73\u65b9\u5173\u7cfb\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n count++;\n }\n }\n return count;\n}\n
time_complexity.go/* \u5e73\u65b9\u9636 */\nfunc quadratic(n int) int {\n count := 0\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u5e73\u65b9\u5173\u7cfb\n for i := 0; i < n; i++ {\n for j := 0; j < n; j++ {\n count++\n }\n }\n return count\n}\n
time_complexity.swift/* \u5e73\u65b9\u9636 */\nfunc quadratic(n: Int) -> Int {\n var count = 0\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u5e73\u65b9\u5173\u7cfb\n for _ in 0 ..< n {\n for _ in 0 ..< n {\n count += 1\n }\n }\n return count\n}\n
time_complexity.js/* \u5e73\u65b9\u9636 */\nfunction quadratic(n) {\n let count = 0;\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u5e73\u65b9\u5173\u7cfb\n for (let i = 0; i < n; i++) {\n for (let j = 0; j < n; j++) {\n count++;\n }\n }\n return count;\n}\n
time_complexity.ts/* \u5e73\u65b9\u9636 */\nfunction quadratic(n: number): number {\n let count = 0;\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u5e73\u65b9\u5173\u7cfb\n for (let i = 0; i < n; i++) {\n for (let j = 0; j < n; j++) {\n count++;\n }\n }\n return count;\n}\n
time_complexity.dart/* \u5e73\u65b9\u9636 */\nint quadratic(int n) {\n int count = 0;\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u5e73\u65b9\u5173\u7cfb\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n count++;\n }\n }\n return count;\n}\n
time_complexity.rs/* \u5e73\u65b9\u9636 */\nfn quadratic(n: i32) -> i32 {\n let mut count = 0;\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u5e73\u65b9\u5173\u7cfb\n for _ in 0..n {\n for _ in 0..n {\n count += 1;\n }\n }\n count\n}\n
time_complexity.c/* \u5e73\u65b9\u9636 */\nint quadratic(int n) {\n int count = 0;\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u5e73\u65b9\u5173\u7cfb\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n count++;\n }\n }\n return count;\n}\n
time_complexity.zig// \u5e73\u65b9\u9636\nfn quadratic(n: i32) i32 {\n var count: i32 = 0;\n var i: i32 = 0;\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u5e73\u65b9\u5173\u7cfb\n while (i < n) : (i += 1) {\n var j: i32 = 0;\n while (j < n) : (j += 1) {\n count += 1;\n }\n }\n return count;\n}\n
\u56fe 2-10 \u5bf9\u6bd4\u4e86\u5e38\u6570\u9636\u3001\u7ebf\u6027\u9636\u548c\u5e73\u65b9\u9636\u4e09\u79cd\u65f6\u95f4\u590d\u6742\u5ea6\u3002
\u56fe 2-10 \u00a0 \u5e38\u6570\u9636\u3001\u7ebf\u6027\u9636\u548c\u5e73\u65b9\u9636\u7684\u65f6\u95f4\u590d\u6742\u5ea6
\u4ee5\u5192\u6ce1\u6392\u5e8f\u4e3a\u4f8b\uff0c\u5916\u5c42\u5faa\u73af\u6267\u884c \\(n - 1\\) \u6b21\uff0c\u5185\u5c42\u5faa\u73af\u6267\u884c \\(n-1\\)\u3001\\(n-2\\)\u3001\\(\\dots\\)\u3001\\(2\\)\u3001\\(1\\) \u6b21\uff0c\u5e73\u5747\u4e3a \\(n / 2\\) \u6b21\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O((n - 1) n / 2) = O(n^2)\\) \uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig time_complexity.pydef bubble_sort(nums: list[int]) -> int:\n \"\"\"\u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09\"\"\"\n count = 0 # \u8ba1\u6570\u5668\n # \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for i in range(len(nums) - 1, 0, -1):\n # \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n for j in range(i):\n if nums[j] > nums[j + 1]:\n # \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n tmp: int = nums[j]\n nums[j] = nums[j + 1]\n nums[j + 1] = tmp\n count += 3 # \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n return count\n
time_complexity.cpp/* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nint bubbleSort(vector<int> &nums) {\n int count = 0; // \u8ba1\u6570\u5668\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for (int i = nums.size() - 1; i > 0; i--) {\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n for (int j = 0; j < i; j++) {\n if (nums[j] > nums[j + 1]) {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n int tmp = nums[j];\n nums[j] = nums[j + 1];\n nums[j + 1] = tmp;\n count += 3; // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n }\n }\n }\n return count;\n}\n
time_complexity.java/* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nint bubbleSort(int[] nums) {\n int count = 0; // \u8ba1\u6570\u5668\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for (int i = nums.length - 1; i > 0; i--) {\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n for (int j = 0; j < i; j++) {\n if (nums[j] > nums[j + 1]) {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n int tmp = nums[j];\n nums[j] = nums[j + 1];\n nums[j + 1] = tmp;\n count += 3; // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n }\n }\n }\n return count;\n}\n
time_complexity.cs/* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nint BubbleSort(int[] nums) {\n int count = 0; // \u8ba1\u6570\u5668\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for (int i = nums.Length - 1; i > 0; i--) {\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef \n for (int j = 0; j < i; j++) {\n if (nums[j] > nums[j + 1]) {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n (nums[j + 1], nums[j]) = (nums[j], nums[j + 1]);\n count += 3; // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n }\n }\n }\n return count;\n}\n
time_complexity.go/* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nfunc bubbleSort(nums []int) int {\n count := 0 // \u8ba1\u6570\u5668\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for i := len(nums) - 1; i > 0; i-- {\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n for j := 0; j < i; j++ {\n if nums[j] > nums[j+1] {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n tmp := nums[j]\n nums[j] = nums[j+1]\n nums[j+1] = tmp\n count += 3 // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n }\n }\n }\n return count\n}\n
time_complexity.swift/* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nfunc bubbleSort(nums: inout [Int]) -> Int {\n var count = 0 // \u8ba1\u6570\u5668\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for i in stride(from: nums.count - 1, to: 0, by: -1) {\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef \n for j in 0 ..< i {\n if nums[j] > nums[j + 1] {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n let tmp = nums[j]\n nums[j] = nums[j + 1]\n nums[j + 1] = tmp\n count += 3 // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n }\n }\n }\n return count\n}\n
time_complexity.js/* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nfunction bubbleSort(nums) {\n let count = 0; // \u8ba1\u6570\u5668\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for (let i = nums.length - 1; i > 0; i--) {\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n for (let j = 0; j < i; j++) {\n if (nums[j] > nums[j + 1]) {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n let tmp = nums[j];\n nums[j] = nums[j + 1];\n nums[j + 1] = tmp;\n count += 3; // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n }\n }\n }\n return count;\n}\n
time_complexity.ts/* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nfunction bubbleSort(nums: number[]): number {\n let count = 0; // \u8ba1\u6570\u5668\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for (let i = nums.length - 1; i > 0; i--) {\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n for (let j = 0; j < i; j++) {\n if (nums[j] > nums[j + 1]) {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n let tmp = nums[j];\n nums[j] = nums[j + 1];\n nums[j + 1] = tmp;\n count += 3; // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n }\n }\n }\n return count;\n}\n
time_complexity.dart/* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nint bubbleSort(List<int> nums) {\n int count = 0; // \u8ba1\u6570\u5668\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for (var i = nums.length - 1; i > 0; i--) {\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n for (var j = 0; j < i; j++) {\n if (nums[j] > nums[j + 1]) {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n int tmp = nums[j];\n nums[j] = nums[j + 1];\n nums[j + 1] = tmp;\n count += 3; // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n }\n }\n }\n return count;\n}\n
time_complexity.rs/* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nfn bubble_sort(nums: &mut [i32]) -> i32 {\n let mut count = 0; // \u8ba1\u6570\u5668\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for i in (1..nums.len()).rev() {\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef \n for j in 0..i {\n if nums[j] > nums[j + 1] {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n let tmp = nums[j];\n nums[j] = nums[j + 1];\n nums[j + 1] = tmp;\n count += 3; // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n }\n }\n }\n count\n}\n
time_complexity.c/* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nint bubbleSort(int *nums, int n) {\n int count = 0; // \u8ba1\u6570\u5668\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for (int i = n - 1; i > 0; i--) {\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n for (int j = 0; j < i; j++) {\n if (nums[j] > nums[j + 1]) {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n int tmp = nums[j];\n nums[j] = nums[j + 1];\n nums[j + 1] = tmp;\n count += 3; // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n }\n }\n }\n return count;\n}\n
time_complexity.zig// \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09\nfn bubbleSort(nums: []i32) i32 {\n var count: i32 = 0; // \u8ba1\u6570\u5668 \n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n var i: i32 = @as(i32, @intCast(nums.len)) - 1;\n while (i > 0) : (i -= 1) {\n var j: usize = 0;\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef \n while (j < i) : (j += 1) {\n if (nums[j] > nums[j + 1]) {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n var tmp = nums[j];\n nums[j] = nums[j + 1];\n nums[j + 1] = tmp;\n count += 3; // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n }\n }\n }\n return count;\n}\n
"},{"location":"chapter_computational_complexity/time_complexity/#4-o2n","title":"4. \u00a0 \u6307\u6570\u9636 \\(O(2^n)\\)","text":"\u751f\u7269\u5b66\u7684\u201c\u7ec6\u80de\u5206\u88c2\u201d\u662f\u6307\u6570\u9636\u589e\u957f\u7684\u5178\u578b\u4f8b\u5b50\uff1a\u521d\u59cb\u72b6\u6001\u4e3a \\(1\\) \u4e2a\u7ec6\u80de\uff0c\u5206\u88c2\u4e00\u8f6e\u540e\u53d8\u4e3a \\(2\\) \u4e2a\uff0c\u5206\u88c2\u4e24\u8f6e\u540e\u53d8\u4e3a \\(4\\) \u4e2a\uff0c\u4ee5\u6b64\u7c7b\u63a8\uff0c\u5206\u88c2 \\(n\\) \u8f6e\u540e\u6709 \\(2^n\\) \u4e2a\u7ec6\u80de\u3002
\u56fe 2-11 \u548c\u4ee5\u4e0b\u4ee3\u7801\u6a21\u62df\u4e86\u7ec6\u80de\u5206\u88c2\u7684\u8fc7\u7a0b\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(2^n)\\) \u3002
PythonC++JavaC#GoSwiftJSTSDartRustCZig time_complexity.pydef exponential(n: int) -> int:\n \"\"\"\u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09\"\"\"\n count = 0\n base = 1\n # \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\n for _ in range(n):\n for _ in range(base):\n count += 1\n base *= 2\n # count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n return count\n
time_complexity.cpp/* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint exponential(int n) {\n int count = 0, base = 1;\n // \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < base; j++) {\n count++;\n }\n base *= 2;\n }\n // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n return count;\n}\n
time_complexity.java/* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint exponential(int n) {\n int count = 0, base = 1;\n // \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < base; j++) {\n count++;\n }\n base *= 2;\n }\n // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n return count;\n}\n
time_complexity.cs/* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint Exponential(int n) {\n int count = 0, bas = 1;\n // \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < bas; j++) {\n count++;\n }\n bas *= 2;\n }\n // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n return count;\n}\n
time_complexity.go/* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09*/\nfunc exponential(n int) int {\n count, base := 0, 1\n // \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\n for i := 0; i < n; i++ {\n for j := 0; j < base; j++ {\n count++\n }\n base *= 2\n }\n // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n return count\n}\n
time_complexity.swift/* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfunc exponential(n: Int) -> Int {\n var count = 0\n var base = 1\n // \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\n for _ in 0 ..< n {\n for _ in 0 ..< base {\n count += 1\n }\n base *= 2\n }\n // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n return count\n}\n
time_complexity.js/* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfunction exponential(n) {\n let count = 0,\n base = 1;\n // \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\n for (let i = 0; i < n; i++) {\n for (let j = 0; j < base; j++) {\n count++;\n }\n base *= 2;\n }\n // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n return count;\n}\n
time_complexity.ts/* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfunction exponential(n: number): number {\n let count = 0,\n base = 1;\n // \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\n for (let i = 0; i < n; i++) {\n for (let j = 0; j < base; j++) {\n count++;\n }\n base *= 2;\n }\n // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n return count;\n}\n
time_complexity.dart/* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint exponential(int n) {\n int count = 0, base = 1;\n // \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\n for (var i = 0; i < n; i++) {\n for (var j = 0; j < base; j++) {\n count++;\n }\n base *= 2;\n }\n // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n return count;\n}\n
time_complexity.rs/* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfn exponential(n: i32) -> i32 {\n let mut count = 0;\n let mut base = 1;\n // \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\n for _ in 0..n {\n for _ in 0..base {\n count += 1\n }\n base *= 2;\n }\n // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n count\n}\n
time_complexity.c/* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint exponential(int n) {\n int count = 0;\n int bas = 1;\n // \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < bas; j++) {\n count++;\n }\n bas *= 2;\n }\n // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n return count;\n}\n
time_complexity.zig// \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09\nfn exponential(n: i32) i32 {\n var count: i32 = 0;\n var bas: i32 = 1;\n var i: i32 = 0;\n // \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\n while (i < n) : (i += 1) {\n var j: i32 = 0;\n while (j < bas) : (j += 1) {\n count += 1;\n }\n bas *= 2;\n }\n // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n return count;\n}\n
\u56fe 2-11 \u00a0 \u6307\u6570\u9636\u7684\u65f6\u95f4\u590d\u6742\u5ea6
\u5728\u5b9e\u9645\u7b97\u6cd5\u4e2d\uff0c\u6307\u6570\u9636\u5e38\u51fa\u73b0\u4e8e\u9012\u5f52\u51fd\u6570\u4e2d\u3002\u4f8b\u5982\u5728\u4ee5\u4e0b\u4ee3\u7801\u4e2d\uff0c\u5176\u9012\u5f52\u5730\u4e00\u5206\u4e3a\u4e8c\uff0c\u7ecf\u8fc7 \\(n\\) \u6b21\u5206\u88c2\u540e\u505c\u6b62\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig time_complexity.pydef exp_recur(n: int) -> int:\n \"\"\"\u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\"\"\"\n if n == 1:\n return 1\n return exp_recur(n - 1) + exp_recur(n - 1) + 1\n
time_complexity.cpp/* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint expRecur(int n) {\n if (n == 1)\n return 1;\n return expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n
time_complexity.java/* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint expRecur(int n) {\n if (n == 1)\n return 1;\n return expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n
time_complexity.cs/* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint ExpRecur(int n) {\n if (n == 1) return 1;\n return ExpRecur(n - 1) + ExpRecur(n - 1) + 1;\n}\n
time_complexity.go/* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09*/\nfunc expRecur(n int) int {\n if n == 1 {\n return 1\n }\n return expRecur(n-1) + expRecur(n-1) + 1\n}\n
time_complexity.swift/* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunc expRecur(n: Int) -> Int {\n if n == 1 {\n return 1\n }\n return expRecur(n: n - 1) + expRecur(n: n - 1) + 1\n}\n
time_complexity.js/* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction expRecur(n) {\n if (n === 1) return 1;\n return expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n
time_complexity.ts/* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction expRecur(n: number): number {\n if (n === 1) return 1;\n return expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n
time_complexity.dart/* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint expRecur(int n) {\n if (n == 1) return 1;\n return expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n
time_complexity.rs/* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfn exp_recur(n: i32) -> i32 {\n if n == 1 {\n return 1;\n }\n exp_recur(n - 1) + exp_recur(n - 1) + 1\n}\n
time_complexity.c/* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint expRecur(int n) {\n if (n == 1)\n return 1;\n return expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n
time_complexity.zig// \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\nfn expRecur(n: i32) i32 {\n if (n == 1) return 1;\n return expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n
\u6307\u6570\u9636\u589e\u957f\u975e\u5e38\u8fc5\u901f\uff0c\u5728\u7a77\u4e3e\u6cd5\uff08\u66b4\u529b\u641c\u7d22\u3001\u56de\u6eaf\u7b49\uff09\u4e2d\u6bd4\u8f83\u5e38\u89c1\u3002\u5bf9\u4e8e\u6570\u636e\u89c4\u6a21\u8f83\u5927\u7684\u95ee\u9898\uff0c\u6307\u6570\u9636\u662f\u4e0d\u53ef\u63a5\u53d7\u7684\uff0c\u901a\u5e38\u9700\u8981\u4f7f\u7528\u52a8\u6001\u89c4\u5212\u6216\u8d2a\u5fc3\u7b97\u6cd5\u7b49\u6765\u89e3\u51b3\u3002
"},{"location":"chapter_computational_complexity/time_complexity/#5-olog-n","title":"5. \u00a0 \u5bf9\u6570\u9636 \\(O(\\log n)\\)","text":"\u4e0e\u6307\u6570\u9636\u76f8\u53cd\uff0c\u5bf9\u6570\u9636\u53cd\u6620\u4e86\u201c\u6bcf\u8f6e\u7f29\u51cf\u5230\u4e00\u534a\u201d\u7684\u60c5\u51b5\u3002\u8bbe\u8f93\u5165\u6570\u636e\u5927\u5c0f\u4e3a \\(n\\) \uff0c\u7531\u4e8e\u6bcf\u8f6e\u7f29\u51cf\u5230\u4e00\u534a\uff0c\u56e0\u6b64\u5faa\u73af\u6b21\u6570\u662f \\(\\log_2 n\\) \uff0c\u5373 \\(2^n\\) \u7684\u53cd\u51fd\u6570\u3002
\u56fe 2-12 \u548c\u4ee5\u4e0b\u4ee3\u7801\u6a21\u62df\u4e86\u201c\u6bcf\u8f6e\u7f29\u51cf\u5230\u4e00\u534a\u201d\u7684\u8fc7\u7a0b\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(\\log_2 n)\\) \uff0c\u7b80\u8bb0\u4e3a \\(O(\\log n)\\) \uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig time_complexity.pydef logarithmic(n: float) -> int:\n \"\"\"\u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09\"\"\"\n count = 0\n while n > 1:\n n = n / 2\n count += 1\n return count\n
time_complexity.cpp/* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint logarithmic(float n) {\n int count = 0;\n while (n > 1) {\n n = n / 2;\n count++;\n }\n return count;\n}\n
time_complexity.java/* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint logarithmic(float n) {\n int count = 0;\n while (n > 1) {\n n = n / 2;\n count++;\n }\n return count;\n}\n
time_complexity.cs/* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint Logarithmic(float n) {\n int count = 0;\n while (n > 1) {\n n /= 2;\n count++;\n }\n return count;\n}\n
time_complexity.go/* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09*/\nfunc logarithmic(n float64) int {\n count := 0\n for n > 1 {\n n = n / 2\n count++\n }\n return count\n}\n
time_complexity.swift/* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfunc logarithmic(n: Double) -> Int {\n var count = 0\n var n = n\n while n > 1 {\n n = n / 2\n count += 1\n }\n return count\n}\n
time_complexity.js/* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfunction logarithmic(n) {\n let count = 0;\n while (n > 1) {\n n = n / 2;\n count++;\n }\n return count;\n}\n
time_complexity.ts/* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfunction logarithmic(n: number): number {\n let count = 0;\n while (n > 1) {\n n = n / 2;\n count++;\n }\n return count;\n}\n
time_complexity.dart/* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint logarithmic(num n) {\n int count = 0;\n while (n > 1) {\n n = n / 2;\n count++;\n }\n return count;\n}\n
time_complexity.rs/* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfn logarithmic(mut n: f32) -> i32 {\n let mut count = 0;\n while n > 1.0 {\n n = n / 2.0;\n count += 1;\n }\n count\n}\n
time_complexity.c/* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint logarithmic(float n) {\n int count = 0;\n while (n > 1) {\n n = n / 2;\n count++;\n }\n return count;\n}\n
time_complexity.zig// \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09\nfn logarithmic(n: f32) i32 {\n var count: i32 = 0;\n var n_var = n;\n while (n_var > 1)\n {\n n_var = n_var / 2;\n count +=1;\n }\n return count;\n}\n
\u56fe 2-12 \u00a0 \u5bf9\u6570\u9636\u7684\u65f6\u95f4\u590d\u6742\u5ea6
\u4e0e\u6307\u6570\u9636\u7c7b\u4f3c\uff0c\u5bf9\u6570\u9636\u4e5f\u5e38\u51fa\u73b0\u4e8e\u9012\u5f52\u51fd\u6570\u4e2d\u3002\u4ee5\u4e0b\u4ee3\u7801\u5f62\u6210\u4e86\u4e00\u68f5\u9ad8\u5ea6\u4e3a \\(\\log_2 n\\) \u7684\u9012\u5f52\u6811\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig time_complexity.pydef log_recur(n: float) -> int:\n \"\"\"\u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\"\"\"\n if n <= 1:\n return 0\n return log_recur(n / 2) + 1\n
time_complexity.cpp/* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint logRecur(float n) {\n if (n <= 1)\n return 0;\n return logRecur(n / 2) + 1;\n}\n
time_complexity.java/* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint logRecur(float n) {\n if (n <= 1)\n return 0;\n return logRecur(n / 2) + 1;\n}\n
time_complexity.cs/* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint LogRecur(float n) {\n if (n <= 1) return 0;\n return LogRecur(n / 2) + 1;\n}\n
time_complexity.go/* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09*/\nfunc logRecur(n float64) int {\n if n <= 1 {\n return 0\n }\n return logRecur(n/2) + 1\n}\n
time_complexity.swift/* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunc logRecur(n: Double) -> Int {\n if n <= 1 {\n return 0\n }\n return logRecur(n: n / 2) + 1\n}\n
time_complexity.js/* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction logRecur(n) {\n if (n <= 1) return 0;\n return logRecur(n / 2) + 1;\n}\n
time_complexity.ts/* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction logRecur(n: number): number {\n if (n <= 1) return 0;\n return logRecur(n / 2) + 1;\n}\n
time_complexity.dart/* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint logRecur(num n) {\n if (n <= 1) return 0;\n return logRecur(n / 2) + 1;\n}\n
time_complexity.rs/* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfn log_recur(n: f32) -> i32 {\n if n <= 1.0 {\n return 0;\n }\n log_recur(n / 2.0) + 1\n}\n
time_complexity.c/* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint logRecur(float n) {\n if (n <= 1)\n return 0;\n return logRecur(n / 2) + 1;\n}\n
time_complexity.zig// \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\nfn logRecur(n: f32) i32 {\n if (n <= 1) return 0;\n return logRecur(n / 2) + 1;\n}\n
\u5bf9\u6570\u9636\u5e38\u51fa\u73b0\u4e8e\u57fa\u4e8e\u5206\u6cbb\u7b56\u7565\u7684\u7b97\u6cd5\u4e2d\uff0c\u4f53\u73b0\u4e86\u201c\u4e00\u5206\u4e3a\u591a\u201d\u548c\u201c\u5316\u7e41\u4e3a\u7b80\u201d\u7684\u7b97\u6cd5\u601d\u60f3\u3002\u5b83\u589e\u957f\u7f13\u6162\uff0c\u662f\u4ec5\u6b21\u4e8e\u5e38\u6570\u9636\u7684\u7406\u60f3\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u3002
\\(O(\\log n)\\) \u7684\u5e95\u6570\u662f\u591a\u5c11\uff1f
\u51c6\u786e\u6765\u8bf4\uff0c\u201c\u4e00\u5206\u4e3a \\(m\\)\u201d\u5bf9\u5e94\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u662f \\(O(\\log_m n)\\) \u3002\u800c\u901a\u8fc7\u5bf9\u6570\u6362\u5e95\u516c\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u5f97\u5230\u5177\u6709\u4e0d\u540c\u5e95\u6570\u3001\u76f8\u7b49\u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a
\\[ O(\\log_m n) = O(\\log_k n / \\log_k m) = O(\\log_k n) \\]\u4e5f\u5c31\u662f\u8bf4\uff0c\u5e95\u6570 \\(m\\) \u53ef\u4ee5\u5728\u4e0d\u5f71\u54cd\u590d\u6742\u5ea6\u7684\u524d\u63d0\u4e0b\u8f6c\u6362\u3002\u56e0\u6b64\u6211\u4eec\u901a\u5e38\u4f1a\u7701\u7565\u5e95\u6570 \\(m\\) \uff0c\u5c06\u5bf9\u6570\u9636\u76f4\u63a5\u8bb0\u4e3a \\(O(\\log n)\\) \u3002
"},{"location":"chapter_computational_complexity/time_complexity/#6-on-log-n","title":"6. \u00a0 \u7ebf\u6027\u5bf9\u6570\u9636 \\(O(n \\log n)\\)","text":"\u7ebf\u6027\u5bf9\u6570\u9636\u5e38\u51fa\u73b0\u4e8e\u5d4c\u5957\u5faa\u73af\u4e2d\uff0c\u4e24\u5c42\u5faa\u73af\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u5206\u522b\u4e3a \\(O(\\log n)\\) \u548c \\(O(n)\\) \u3002\u76f8\u5173\u4ee3\u7801\u5982\u4e0b\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig time_complexity.pydef linear_log_recur(n: float) -> int:\n \"\"\"\u7ebf\u6027\u5bf9\u6570\u9636\"\"\"\n if n <= 1:\n return 1\n count: int = linear_log_recur(n // 2) + linear_log_recur(n // 2)\n for _ in range(n):\n count += 1\n return count\n
time_complexity.cpp/* \u7ebf\u6027\u5bf9\u6570\u9636 */\nint linearLogRecur(float n) {\n if (n <= 1)\n return 1;\n int count = linearLogRecur(n / 2) + linearLogRecur(n / 2);\n for (int i = 0; i < n; i++) {\n count++;\n }\n return count;\n}\n
time_complexity.java/* \u7ebf\u6027\u5bf9\u6570\u9636 */\nint linearLogRecur(float n) {\n if (n <= 1)\n return 1;\n int count = linearLogRecur(n / 2) + linearLogRecur(n / 2);\n for (int i = 0; i < n; i++) {\n count++;\n }\n return count;\n}\n
time_complexity.cs/* \u7ebf\u6027\u5bf9\u6570\u9636 */\nint LinearLogRecur(float n) {\n if (n <= 1) return 1;\n int count = LinearLogRecur(n / 2) + LinearLogRecur(n / 2);\n for (int i = 0; i < n; i++) {\n count++;\n }\n return count;\n}\n
time_complexity.go/* \u7ebf\u6027\u5bf9\u6570\u9636 */\nfunc linearLogRecur(n float64) int {\n if n <= 1 {\n return 1\n }\n count := linearLogRecur(n/2) + linearLogRecur(n/2)\n for i := 0.0; i < n; i++ {\n count++\n }\n return count\n}\n
time_complexity.swift/* \u7ebf\u6027\u5bf9\u6570\u9636 */\nfunc linearLogRecur(n: Double) -> Int {\n if n <= 1 {\n return 1\n }\n var count = linearLogRecur(n: n / 2) + linearLogRecur(n: n / 2)\n for _ in stride(from: 0, to: n, by: 1) {\n count += 1\n }\n return count\n}\n
time_complexity.js/* \u7ebf\u6027\u5bf9\u6570\u9636 */\nfunction linearLogRecur(n) {\n if (n <= 1) return 1;\n let count = linearLogRecur(n / 2) + linearLogRecur(n / 2);\n for (let i = 0; i < n; i++) {\n count++;\n }\n return count;\n}\n
time_complexity.ts/* \u7ebf\u6027\u5bf9\u6570\u9636 */\nfunction linearLogRecur(n: number): number {\n if (n <= 1) return 1;\n let count = linearLogRecur(n / 2) + linearLogRecur(n / 2);\n for (let i = 0; i < n; i++) {\n count++;\n }\n return count;\n}\n
time_complexity.dart/* \u7ebf\u6027\u5bf9\u6570\u9636 */\nint linearLogRecur(num n) {\n if (n <= 1) return 1;\n int count = linearLogRecur(n / 2) + linearLogRecur(n / 2);\n for (var i = 0; i < n; i++) {\n count++;\n }\n return count;\n}\n
time_complexity.rs/* \u7ebf\u6027\u5bf9\u6570\u9636 */\nfn linear_log_recur(n: f32) -> i32 {\n if n <= 1.0 {\n return 1;\n }\n let mut count = linear_log_recur(n / 2.0) + linear_log_recur(n / 2.0);\n for _ in 0 ..n as i32 {\n count += 1;\n }\n return count\n}\n
time_complexity.c/* \u7ebf\u6027\u5bf9\u6570\u9636 */\nint linearLogRecur(float n) {\n if (n <= 1)\n return 1;\n int count = linearLogRecur(n / 2) + linearLogRecur(n / 2);\n for (int i = 0; i < n; i++) {\n count++;\n }\n return count;\n}\n
time_complexity.zig// \u7ebf\u6027\u5bf9\u6570\u9636\nfn linearLogRecur(n: f32) i32 {\n if (n <= 1) return 1;\n var count: i32 = linearLogRecur(n / 2) + linearLogRecur(n / 2);\n var i: f32 = 0;\n while (i < n) : (i += 1) {\n count += 1;\n }\n return count;\n}\n
\u56fe 2-13 \u5c55\u793a\u4e86\u7ebf\u6027\u5bf9\u6570\u9636\u7684\u751f\u6210\u65b9\u5f0f\u3002\u4e8c\u53c9\u6811\u7684\u6bcf\u4e00\u5c42\u7684\u64cd\u4f5c\u603b\u6570\u90fd\u4e3a \\(n\\) \uff0c\u6811\u5171\u6709 \\(\\log_2 n + 1\\) \u5c42\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n \\log n)\\) \u3002
\u56fe 2-13 \u00a0 \u7ebf\u6027\u5bf9\u6570\u9636\u7684\u65f6\u95f4\u590d\u6742\u5ea6
\u4e3b\u6d41\u6392\u5e8f\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u901a\u5e38\u4e3a \\(O(n \\log n)\\) \uff0c\u4f8b\u5982\u5feb\u901f\u6392\u5e8f\u3001\u5f52\u5e76\u6392\u5e8f\u3001\u5806\u6392\u5e8f\u7b49\u3002
"},{"location":"chapter_computational_complexity/time_complexity/#7-on","title":"7. \u00a0 \u9636\u4e58\u9636 \\(O(n!)\\)","text":"\u9636\u4e58\u9636\u5bf9\u5e94\u6570\u5b66\u4e0a\u7684\u201c\u5168\u6392\u5217\u201d\u95ee\u9898\u3002\u7ed9\u5b9a \\(n\\) \u4e2a\u4e92\u4e0d\u91cd\u590d\u7684\u5143\u7d20\uff0c\u6c42\u5176\u6240\u6709\u53ef\u80fd\u7684\u6392\u5217\u65b9\u6848\uff0c\u65b9\u6848\u6570\u91cf\u4e3a\uff1a
\\[ n! = n \\times (n - 1) \\times (n - 2) \\times \\dots \\times 2 \\times 1 \\]\u9636\u4e58\u901a\u5e38\u4f7f\u7528\u9012\u5f52\u5b9e\u73b0\u3002\u5982\u56fe 2-14 \u548c\u4ee5\u4e0b\u4ee3\u7801\u6240\u793a\uff0c\u7b2c\u4e00\u5c42\u5206\u88c2\u51fa \\(n\\) \u4e2a\uff0c\u7b2c\u4e8c\u5c42\u5206\u88c2\u51fa \\(n - 1\\) \u4e2a\uff0c\u4ee5\u6b64\u7c7b\u63a8\uff0c\u76f4\u81f3\u7b2c \\(n\\) \u5c42\u65f6\u505c\u6b62\u5206\u88c2\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig time_complexity.pydef factorial_recur(n: int) -> int:\n \"\"\"\u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\"\"\"\n if n == 0:\n return 1\n count = 0\n # \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\n for _ in range(n):\n count += factorial_recur(n - 1)\n return count\n
time_complexity.cpp/* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint factorialRecur(int n) {\n if (n == 0)\n return 1;\n int count = 0;\n // \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\n for (int i = 0; i < n; i++) {\n count += factorialRecur(n - 1);\n }\n return count;\n}\n
time_complexity.java/* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint factorialRecur(int n) {\n if (n == 0)\n return 1;\n int count = 0;\n // \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\n for (int i = 0; i < n; i++) {\n count += factorialRecur(n - 1);\n }\n return count;\n}\n
time_complexity.cs/* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint FactorialRecur(int n) {\n if (n == 0) return 1;\n int count = 0;\n // \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\n for (int i = 0; i < n; i++) {\n count += FactorialRecur(n - 1);\n }\n return count;\n}\n
time_complexity.go/* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunc factorialRecur(n int) int {\n if n == 0 {\n return 1\n }\n count := 0\n // \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\n for i := 0; i < n; i++ {\n count += factorialRecur(n - 1)\n }\n return count\n}\n
time_complexity.swift/* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunc factorialRecur(n: Int) -> Int {\n if n == 0 {\n return 1\n }\n var count = 0\n // \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\n for _ in 0 ..< n {\n count += factorialRecur(n: n - 1)\n }\n return count\n}\n
time_complexity.js/* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction factorialRecur(n) {\n if (n === 0) return 1;\n let count = 0;\n // \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\n for (let i = 0; i < n; i++) {\n count += factorialRecur(n - 1);\n }\n return count;\n}\n
time_complexity.ts/* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction factorialRecur(n: number): number {\n if (n === 0) return 1;\n let count = 0;\n // \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\n for (let i = 0; i < n; i++) {\n count += factorialRecur(n - 1);\n }\n return count;\n}\n
time_complexity.dart/* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint factorialRecur(int n) {\n if (n == 0) return 1;\n int count = 0;\n // \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\n for (var i = 0; i < n; i++) {\n count += factorialRecur(n - 1);\n }\n return count;\n}\n
time_complexity.rs/* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfn factorial_recur(n: i32) -> i32 {\n if n == 0 {\n return 1;\n }\n let mut count = 0;\n // \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\n for _ in 0..n {\n count += factorial_recur(n - 1);\n }\n count\n}\n
time_complexity.c/* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint factorialRecur(int n) {\n if (n == 0)\n return 1;\n int count = 0;\n for (int i = 0; i < n; i++) {\n count += factorialRecur(n - 1);\n }\n return count;\n}\n
time_complexity.zig// \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\nfn factorialRecur(n: i32) i32 {\n if (n == 0) return 1;\n var count: i32 = 0;\n var i: i32 = 0;\n // \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\n while (i < n) : (i += 1) {\n count += factorialRecur(n - 1);\n }\n return count;\n}\n
\u56fe 2-14 \u00a0 \u9636\u4e58\u9636\u7684\u65f6\u95f4\u590d\u6742\u5ea6
\u8bf7\u6ce8\u610f\uff0c\u56e0\u4e3a\u5f53 \\(n \\geq 4\\) \u65f6\u6052\u6709 \\(n! > 2^n\\) \uff0c\u6240\u4ee5\u9636\u4e58\u9636\u6bd4\u6307\u6570\u9636\u589e\u957f\u5f97\u66f4\u5feb\uff0c\u5728 \\(n\\) \u8f83\u5927\u65f6\u4e5f\u662f\u4e0d\u53ef\u63a5\u53d7\u7684\u3002
"},{"location":"chapter_computational_complexity/time_complexity/#235","title":"2.3.5 \u00a0 \u6700\u5dee\u3001\u6700\u4f73\u3001\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6","text":"\u7b97\u6cd5\u7684\u65f6\u95f4\u6548\u7387\u5f80\u5f80\u4e0d\u662f\u56fa\u5b9a\u7684\uff0c\u800c\u662f\u4e0e\u8f93\u5165\u6570\u636e\u7684\u5206\u5e03\u6709\u5173\u3002\u5047\u8bbe\u8f93\u5165\u4e00\u4e2a\u957f\u5ea6\u4e3a \\(n\\) \u7684\u6570\u7ec4 nums
\uff0c\u5176\u4e2d nums
\u7531\u4ece \\(1\\) \u81f3 \\(n\\) \u7684\u6570\u5b57\u7ec4\u6210\uff0c\u6bcf\u4e2a\u6570\u5b57\u53ea\u51fa\u73b0\u4e00\u6b21\uff1b\u4f46\u5143\u7d20\u987a\u5e8f\u662f\u968f\u673a\u6253\u4e71\u7684\uff0c\u4efb\u52a1\u76ee\u6807\u662f\u8fd4\u56de\u5143\u7d20 \\(1\\) \u7684\u7d22\u5f15\u3002\u6211\u4eec\u53ef\u4ee5\u5f97\u51fa\u4ee5\u4e0b\u7ed3\u8bba\u3002
nums = [?, ?, ..., 1]
\uff0c\u5373\u5f53\u672b\u5c3e\u5143\u7d20\u662f \\(1\\) \u65f6\uff0c\u9700\u8981\u5b8c\u6574\u904d\u5386\u6570\u7ec4\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 \\(O(n)\\) \u3002nums = [1, ?, ?, ...]
\uff0c\u5373\u5f53\u9996\u4e2a\u5143\u7d20\u4e3a \\(1\\) \u65f6\uff0c\u65e0\u8bba\u6570\u7ec4\u591a\u957f\u90fd\u4e0d\u9700\u8981\u7ee7\u7eed\u904d\u5386\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 \\(\\Omega(1)\\) \u3002\u201c\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6\u201d\u5bf9\u5e94\u51fd\u6570\u6e10\u8fd1\u4e0a\u754c\uff0c\u4f7f\u7528\u5927 \\(O\\) \u8bb0\u53f7\u8868\u793a\u3002\u76f8\u5e94\u5730\uff0c\u201c\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6\u201d\u5bf9\u5e94\u51fd\u6570\u6e10\u8fd1\u4e0b\u754c\uff0c\u7528 \\(\\Omega\\) \u8bb0\u53f7\u8868\u793a\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig worst_best_time_complexity.pydef random_numbers(n: int) -> list[int]:\n \"\"\"\u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a: 1, 2, ..., n \uff0c\u987a\u5e8f\u88ab\u6253\u4e71\"\"\"\n # \u751f\u6210\u6570\u7ec4 nums =: 1, 2, 3, ..., n\n nums = [i for i in range(1, n + 1)]\n # \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\n random.shuffle(nums)\n return nums\n\ndef find_one(nums: list[int]) -> int:\n \"\"\"\u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15\"\"\"\n for i in range(len(nums)):\n # \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n # \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\n if nums[i] == 1:\n return i\n return -1\n
worst_best_time_complexity.cpp/* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nvector<int> randomNumbers(int n) {\n vector<int> nums(n);\n // \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\n for (int i = 0; i < n; i++) {\n nums[i] = i + 1;\n }\n // \u4f7f\u7528\u7cfb\u7edf\u65f6\u95f4\u751f\u6210\u968f\u673a\u79cd\u5b50\n unsigned seed = chrono::system_clock::now().time_since_epoch().count();\n // \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\n shuffle(nums.begin(), nums.end(), default_random_engine(seed));\n return nums;\n}\n\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nint findOne(vector<int> &nums) {\n for (int i = 0; i < nums.size(); i++) {\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\n if (nums[i] == 1)\n return i;\n }\n return -1;\n}\n
worst_best_time_complexity.java/* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nint[] randomNumbers(int n) {\n Integer[] nums = new Integer[n];\n // \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\n for (int i = 0; i < n; i++) {\n nums[i] = i + 1;\n }\n // \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\n Collections.shuffle(Arrays.asList(nums));\n // Integer[] -> int[]\n int[] res = new int[n];\n for (int i = 0; i < n; i++) {\n res[i] = nums[i];\n }\n return res;\n}\n\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nint findOne(int[] nums) {\n for (int i = 0; i < nums.length; i++) {\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\n if (nums[i] == 1)\n return i;\n }\n return -1;\n}\n
worst_best_time_complexity.cs/* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nint[] RandomNumbers(int n) {\n int[] nums = new int[n];\n // \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\n for (int i = 0; i < n; i++) {\n nums[i] = i + 1;\n }\n\n // \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\n for (int i = 0; i < nums.Length; i++) {\n int index = new Random().Next(i, nums.Length);\n (nums[i], nums[index]) = (nums[index], nums[i]);\n }\n return nums;\n}\n\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nint FindOne(int[] nums) {\n for (int i = 0; i < nums.Length; i++) {\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\n if (nums[i] == 1)\n return i;\n }\n return -1;\n}\n
worst_best_time_complexity.go/* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nfunc randomNumbers(n int) []int {\n nums := make([]int, n)\n // \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\n for i := 0; i < n; i++ {\n nums[i] = i + 1\n }\n // \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\n rand.Shuffle(len(nums), func(i, j int) {\n nums[i], nums[j] = nums[j], nums[i]\n })\n return nums\n}\n\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nfunc findOne(nums []int) int {\n for i := 0; i < len(nums); i++ {\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\n if nums[i] == 1 {\n return i\n }\n }\n return -1\n}\n
worst_best_time_complexity.swift/* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nfunc randomNumbers(n: Int) -> [Int] {\n // \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\n var nums = Array(1 ... n)\n // \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\n nums.shuffle()\n return nums\n}\n\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nfunc findOne(nums: [Int]) -> Int {\n for i in nums.indices {\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\n if nums[i] == 1 {\n return i\n }\n }\n return -1\n}\n
worst_best_time_complexity.js/* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nfunction randomNumbers(n) {\n const nums = Array(n);\n // \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\n for (let i = 0; i < n; i++) {\n nums[i] = i + 1;\n }\n // \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\n for (let i = 0; i < n; i++) {\n const r = Math.floor(Math.random() * (i + 1));\n const temp = nums[i];\n nums[i] = nums[r];\n nums[r] = temp;\n }\n return nums;\n}\n\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nfunction findOne(nums) {\n for (let i = 0; i < nums.length; i++) {\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\n if (nums[i] === 1) {\n return i;\n }\n }\n return -1;\n}\n
worst_best_time_complexity.ts/* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nfunction randomNumbers(n: number): number[] {\n const nums = Array(n);\n // \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\n for (let i = 0; i < n; i++) {\n nums[i] = i + 1;\n }\n // \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\n for (let i = 0; i < n; i++) {\n const r = Math.floor(Math.random() * (i + 1));\n const temp = nums[i];\n nums[i] = nums[r];\n nums[r] = temp;\n }\n return nums;\n}\n\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nfunction findOne(nums: number[]): number {\n for (let i = 0; i < nums.length; i++) {\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\n if (nums[i] === 1) {\n return i;\n }\n }\n return -1;\n}\n
worst_best_time_complexity.dart/* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nList<int> randomNumbers(int n) {\n final nums = List.filled(n, 0);\n // \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\n for (var i = 0; i < n; i++) {\n nums[i] = i + 1;\n }\n // \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\n nums.shuffle();\n\n return nums;\n}\n\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nint findOne(List<int> nums) {\n for (var i = 0; i < nums.length; i++) {\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\n if (nums[i] == 1) return i;\n }\n\n return -1;\n}\n
worst_best_time_complexity.rs/* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nfn random_numbers(n: i32) -> Vec<i32> {\n // \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\n let mut nums = (1..=n).collect::<Vec<i32>>();\n // \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\n nums.shuffle(&mut thread_rng());\n nums\n}\n\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nfn find_one(nums: &[i32]) -> Option<usize> {\n for i in 0..nums.len() {\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\n if nums[i] == 1 {\n return Some(i);\n }\n }\n None\n}\n
worst_best_time_complexity.c/* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nint *randomNumbers(int n) {\n // \u5206\u914d\u5806\u533a\u5185\u5b58\uff08\u521b\u5efa\u4e00\u7ef4\u53ef\u53d8\u957f\u6570\u7ec4\uff1a\u6570\u7ec4\u4e2d\u5143\u7d20\u6570\u91cf\u4e3a n \uff0c\u5143\u7d20\u7c7b\u578b\u4e3a int \uff09\n int *nums = (int *)malloc(n * sizeof(int));\n // \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\n for (int i = 0; i < n; i++) {\n nums[i] = i + 1;\n }\n // \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\n for (int i = n - 1; i > 0; i--) {\n int j = rand() % (i + 1);\n int temp = nums[i];\n nums[i] = nums[j];\n nums[j] = temp;\n }\n return nums;\n}\n\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nint findOne(int *nums, int n) {\n for (int i = 0; i < n; i++) {\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\n if (nums[i] == 1)\n return i;\n }\n return -1;\n}\n
worst_best_time_complexity.zig// \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71\nfn randomNumbers(comptime n: usize) [n]i32 {\n var nums: [n]i32 = undefined;\n // \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\n for (&nums, 0..) |*num, i| {\n num.* = @as(i32, @intCast(i)) + 1;\n }\n // \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\n const rand = std.crypto.random;\n rand.shuffle(i32, &nums);\n return nums;\n}\n\n// \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15\nfn findOne(nums: []i32) i32 {\n for (nums, 0..) |num, i| {\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\n if (num == 1) return @intCast(i);\n }\n return -1;\n}\n
\u503c\u5f97\u8bf4\u660e\u7684\u662f\uff0c\u6211\u4eec\u5728\u5b9e\u9645\u4e2d\u5f88\u5c11\u4f7f\u7528\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6\uff0c\u56e0\u4e3a\u901a\u5e38\u53ea\u6709\u5728\u5f88\u5c0f\u6982\u7387\u4e0b\u624d\u80fd\u8fbe\u5230\uff0c\u53ef\u80fd\u4f1a\u5e26\u6765\u4e00\u5b9a\u7684\u8bef\u5bfc\u6027\u3002\u800c\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6\u66f4\u4e3a\u5b9e\u7528\uff0c\u56e0\u4e3a\u5b83\u7ed9\u51fa\u4e86\u4e00\u4e2a\u6548\u7387\u5b89\u5168\u503c\uff0c\u8ba9\u6211\u4eec\u53ef\u4ee5\u653e\u5fc3\u5730\u4f7f\u7528\u7b97\u6cd5\u3002
\u4ece\u4e0a\u8ff0\u793a\u4f8b\u53ef\u4ee5\u770b\u51fa\uff0c\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6\u548c\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6\u53ea\u51fa\u73b0\u4e8e\u201c\u7279\u6b8a\u7684\u6570\u636e\u5206\u5e03\u201d\uff0c\u8fd9\u4e9b\u60c5\u51b5\u7684\u51fa\u73b0\u6982\u7387\u53ef\u80fd\u5f88\u5c0f\uff0c\u5e76\u4e0d\u80fd\u771f\u5b9e\u5730\u53cd\u6620\u7b97\u6cd5\u8fd0\u884c\u6548\u7387\u3002\u76f8\u6bd4\u4e4b\u4e0b\uff0c\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u53ef\u4ee5\u4f53\u73b0\u7b97\u6cd5\u5728\u968f\u673a\u8f93\u5165\u6570\u636e\u4e0b\u7684\u8fd0\u884c\u6548\u7387\uff0c\u7528 \\(\\Theta\\) \u8bb0\u53f7\u6765\u8868\u793a\u3002
\u5bf9\u4e8e\u90e8\u5206\u7b97\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u7b80\u5355\u5730\u63a8\u7b97\u51fa\u968f\u673a\u6570\u636e\u5206\u5e03\u4e0b\u7684\u5e73\u5747\u60c5\u51b5\u3002\u6bd4\u5982\u4e0a\u8ff0\u793a\u4f8b\uff0c\u7531\u4e8e\u8f93\u5165\u6570\u7ec4\u662f\u88ab\u6253\u4e71\u7684\uff0c\u56e0\u6b64\u5143\u7d20 \\(1\\) \u51fa\u73b0\u5728\u4efb\u610f\u7d22\u5f15\u7684\u6982\u7387\u90fd\u662f\u76f8\u7b49\u7684\uff0c\u90a3\u4e48\u7b97\u6cd5\u7684\u5e73\u5747\u5faa\u73af\u6b21\u6570\u5c31\u662f\u6570\u7ec4\u957f\u5ea6\u7684\u4e00\u534a \\(n / 2\\) \uff0c\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(\\Theta(n / 2) = \\Theta(n)\\) \u3002
\u4f46\u5bf9\u4e8e\u8f83\u4e3a\u590d\u6742\u7684\u7b97\u6cd5\uff0c\u8ba1\u7b97\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u5f80\u5f80\u6bd4\u8f83\u56f0\u96be\uff0c\u56e0\u4e3a\u5f88\u96be\u5206\u6790\u51fa\u5728\u6570\u636e\u5206\u5e03\u4e0b\u7684\u6574\u4f53\u6570\u5b66\u671f\u671b\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u901a\u5e38\u4f7f\u7528\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6\u4f5c\u4e3a\u7b97\u6cd5\u6548\u7387\u7684\u8bc4\u5224\u6807\u51c6\u3002
\u4e3a\u4ec0\u4e48\u5f88\u5c11\u770b\u5230 \\(\\Theta\\) \u7b26\u53f7\uff1f
\u53ef\u80fd\u7531\u4e8e \\(O\\) \u7b26\u53f7\u8fc7\u4e8e\u6717\u6717\u4e0a\u53e3\uff0c\u56e0\u6b64\u6211\u4eec\u5e38\u5e38\u4f7f\u7528\u5b83\u6765\u8868\u793a\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u3002\u4f46\u4ece\u4e25\u683c\u610f\u4e49\u4e0a\u8bb2\uff0c\u8fd9\u79cd\u505a\u6cd5\u5e76\u4e0d\u89c4\u8303\u3002\u5728\u672c\u4e66\u548c\u5176\u4ed6\u8d44\u6599\u4e2d\uff0c\u82e5\u9047\u5230\u7c7b\u4f3c\u201c\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6 \\(O(n)\\)\u201d\u7684\u8868\u8ff0\uff0c\u8bf7\u5c06\u5176\u76f4\u63a5\u7406\u89e3\u4e3a \\(\\Theta(n)\\) \u3002
"},{"location":"chapter_data_structure/","title":"\u7b2c 3 \u7ae0 \u00a0 \u6570\u636e\u7ed3\u6784","text":"Abstract
\u6570\u636e\u7ed3\u6784\u5982\u540c\u4e00\u526f\u7a33\u56fa\u800c\u591a\u6837\u7684\u6846\u67b6\u3002
\u5b83\u4e3a\u6570\u636e\u7684\u6709\u5e8f\u7ec4\u7ec7\u63d0\u4f9b\u4e86\u84dd\u56fe\uff0c\u7b97\u6cd5\u5f97\u4ee5\u5728\u6b64\u57fa\u7840\u4e0a\u751f\u52a8\u8d77\u6765\u3002
"},{"location":"chapter_data_structure/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"\u5f53\u8c08\u53ca\u8ba1\u7b97\u673a\u4e2d\u7684\u6570\u636e\u65f6\uff0c\u6211\u4eec\u4f1a\u60f3\u5230\u6587\u672c\u3001\u56fe\u7247\u3001\u89c6\u9891\u3001\u8bed\u97f3\u30013D \u6a21\u578b\u7b49\u5404\u79cd\u5f62\u5f0f\u3002\u5c3d\u7ba1\u8fd9\u4e9b\u6570\u636e\u7684\u7ec4\u7ec7\u5f62\u5f0f\u5404\u5f02\uff0c\u4f46\u5b83\u4eec\u90fd\u7531\u5404\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6784\u6210\u3002
\u57fa\u672c\u6570\u636e\u7c7b\u578b\u662f CPU \u53ef\u4ee5\u76f4\u63a5\u8fdb\u884c\u8fd0\u7b97\u7684\u7c7b\u578b\uff0c\u5728\u7b97\u6cd5\u4e2d\u76f4\u63a5\u88ab\u4f7f\u7528\uff0c\u4e3b\u8981\u5305\u62ec\u4ee5\u4e0b\u51e0\u79cd\u3002
byte
\u3001short
\u3001int
\u3001long
\u3002float
\u3001double
\uff0c\u7528\u4e8e\u8868\u793a\u5c0f\u6570\u3002char
\uff0c\u7528\u4e8e\u8868\u793a\u5404\u79cd\u8bed\u8a00\u7684\u5b57\u6bcd\u3001\u6807\u70b9\u7b26\u53f7\u751a\u81f3\u8868\u60c5\u7b26\u53f7\u7b49\u3002bool
\uff0c\u7528\u4e8e\u8868\u793a\u201c\u662f\u201d\u4e0e\u201c\u5426\u201d\u5224\u65ad\u3002\u57fa\u672c\u6570\u636e\u7c7b\u578b\u4ee5\u4e8c\u8fdb\u5236\u7684\u5f62\u5f0f\u5b58\u50a8\u5728\u8ba1\u7b97\u673a\u4e2d\u3002\u4e00\u4e2a\u4e8c\u8fdb\u5236\u4f4d\u5373\u4e3a \\(1\\) \u6bd4\u7279\u3002\u5728\u7edd\u5927\u591a\u6570\u73b0\u4ee3\u64cd\u4f5c\u7cfb\u7edf\u4e2d\uff0c\\(1\\) \u5b57\u8282\uff08byte\uff09\u7531 \\(8\\) \u6bd4\u7279\uff08bit\uff09\u7ec4\u6210\u3002
\u57fa\u672c\u6570\u636e\u7c7b\u578b\u7684\u53d6\u503c\u8303\u56f4\u53d6\u51b3\u4e8e\u5176\u5360\u7528\u7684\u7a7a\u95f4\u5927\u5c0f\u3002\u4e0b\u9762\u4ee5 Java \u4e3a\u4f8b\u3002
byte
\u5360\u7528 \\(1\\) byte = \\(8\\) bits \uff0c\u53ef\u4ee5\u8868\u793a \\(2^{8}\\) \u4e2a\u6570\u5b57\u3002int
\u5360\u7528 \\(4\\) bytes = \\(32\\) bits \uff0c\u53ef\u4ee5\u8868\u793a \\(2^{32}\\) \u4e2a\u6570\u5b57\u3002\u8868 3-1 \u5217\u4e3e\u4e86 Java \u4e2d\u5404\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u7684\u5360\u7528\u7a7a\u95f4\u3001\u53d6\u503c\u8303\u56f4\u548c\u9ed8\u8ba4\u503c\u3002\u6b64\u8868\u683c\u65e0\u987b\u6b7b\u8bb0\u786c\u80cc\uff0c\u5927\u81f4\u7406\u89e3\u5373\u53ef\uff0c\u9700\u8981\u65f6\u53ef\u4ee5\u901a\u8fc7\u67e5\u8868\u6765\u56de\u5fc6\u3002
\u8868 3-1 \u00a0 \u57fa\u672c\u6570\u636e\u7c7b\u578b\u7684\u5360\u7528\u7a7a\u95f4\u548c\u53d6\u503c\u8303\u56f4
\u7c7b\u578b \u7b26\u53f7 \u5360\u7528\u7a7a\u95f4 \u6700\u5c0f\u503c \u6700\u5927\u503c \u9ed8\u8ba4\u503c \u6574\u6570byte
1 byte \\(-2^7\\) (\\(-128\\)) \\(2^7 - 1\\) (\\(127\\)) \\(0\\) short
2 bytes \\(-2^{15}\\) \\(2^{15} - 1\\) \\(0\\) int
4 bytes \\(-2^{31}\\) \\(2^{31} - 1\\) \\(0\\) long
8 bytes \\(-2^{63}\\) \\(2^{63} - 1\\) \\(0\\) \u6d6e\u70b9\u6570 float
4 bytes \\(1.175 \\times 10^{-38}\\) \\(3.403 \\times 10^{38}\\) \\(0.0\\text{f}\\) double
8 bytes \\(2.225 \\times 10^{-308}\\) \\(1.798 \\times 10^{308}\\) \\(0.0\\) \u5b57\u7b26 char
2 bytes \\(0\\) \\(2^{16} - 1\\) \\(0\\) \u5e03\u5c14 bool
1 byte \\(\\text{false}\\) \\(\\text{true}\\) \\(\\text{false}\\) \u8bf7\u6ce8\u610f\uff0c\u8868 3-1 \u9488\u5bf9\u7684\u662f Java \u7684\u57fa\u672c\u6570\u636e\u7c7b\u578b\u7684\u60c5\u51b5\u3002\u6bcf\u79cd\u7f16\u7a0b\u8bed\u8a00\u6709\u5404\u81ea\u7684\u6570\u636e\u7c7b\u578b\u5b9a\u4e49\uff0c\u5b83\u4eec\u7684\u5360\u7528\u7a7a\u95f4\u3001\u53d6\u503c\u8303\u56f4\u548c\u9ed8\u8ba4\u503c\u53ef\u80fd\u4f1a\u6709\u6240\u4e0d\u540c\u3002
int
\u53ef\u4ee5\u662f\u4efb\u610f\u5927\u5c0f\uff0c\u53ea\u53d7\u9650\u4e8e\u53ef\u7528\u5185\u5b58\uff1b\u6d6e\u70b9\u6570 float
\u662f\u53cc\u7cbe\u5ea6 64 \u4f4d\uff1b\u6ca1\u6709 char
\u7c7b\u578b\uff0c\u5355\u4e2a\u5b57\u7b26\u5b9e\u9645\u4e0a\u662f\u957f\u5ea6\u4e3a 1 \u7684\u5b57\u7b26\u4e32 str
\u3002char
\u7684\u5927\u5c0f\u5728 C \u548c C++ \u4e2d\u4e3a 1 \u5b57\u8282\uff0c\u5728\u5927\u591a\u6570\u7f16\u7a0b\u8bed\u8a00\u4e2d\u53d6\u51b3\u4e8e\u7279\u5b9a\u7684\u5b57\u7b26\u7f16\u7801\u65b9\u6cd5\uff0c\u8be6\u89c1\u201c\u5b57\u7b26\u7f16\u7801\u201d\u7ae0\u8282\u3002\u90a3\u4e48\uff0c\u57fa\u672c\u6570\u636e\u7c7b\u578b\u4e0e\u6570\u636e\u7ed3\u6784\u4e4b\u95f4\u6709\u4ec0\u4e48\u8054\u7cfb\u5462\uff1f\u6211\u4eec\u77e5\u9053\uff0c\u6570\u636e\u7ed3\u6784\u662f\u5728\u8ba1\u7b97\u673a\u4e2d\u7ec4\u7ec7\u4e0e\u5b58\u50a8\u6570\u636e\u7684\u65b9\u5f0f\u3002\u8fd9\u53e5\u8bdd\u7684\u4e3b\u8bed\u662f\u201c\u7ed3\u6784\u201d\u800c\u975e\u201c\u6570\u636e\u201d\u3002
\u5982\u679c\u60f3\u8868\u793a\u201c\u4e00\u6392\u6570\u5b57\u201d\uff0c\u6211\u4eec\u81ea\u7136\u4f1a\u60f3\u5230\u4f7f\u7528\u6570\u7ec4\u3002\u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u7684\u7ebf\u6027\u7ed3\u6784\u53ef\u4ee5\u8868\u793a\u6570\u5b57\u7684\u76f8\u90bb\u5173\u7cfb\u548c\u987a\u5e8f\u5173\u7cfb\uff0c\u4f46\u81f3\u4e8e\u5b58\u50a8\u7684\u5185\u5bb9\u662f\u6574\u6570 int
\u3001\u5c0f\u6570 float
\u6216\u662f\u5b57\u7b26 char
\uff0c\u5219\u4e0e\u201c\u6570\u636e\u7ed3\u6784\u201d\u65e0\u5173\u3002
\u6362\u53e5\u8bdd\u8bf4\uff0c\u57fa\u672c\u6570\u636e\u7c7b\u578b\u63d0\u4f9b\u4e86\u6570\u636e\u7684\u201c\u5185\u5bb9\u7c7b\u578b\u201d\uff0c\u800c\u6570\u636e\u7ed3\u6784\u63d0\u4f9b\u4e86\u6570\u636e\u7684\u201c\u7ec4\u7ec7\u65b9\u5f0f\u201d\u3002\u4f8b\u5982\u4ee5\u4e0b\u4ee3\u7801\uff0c\u6211\u4eec\u7528\u76f8\u540c\u7684\u6570\u636e\u7ed3\u6784\uff08\u6570\u7ec4\uff09\u6765\u5b58\u50a8\u4e0e\u8868\u793a\u4e0d\u540c\u7684\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff0c\u5305\u62ec int
\u3001float
\u3001char
\u3001bool
\u7b49\u3002
# \u4f7f\u7528\u591a\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6765\u521d\u59cb\u5316\u6570\u7ec4\nnumbers: list[int] = [0] * 5\ndecimals: list[float] = [0.0] * 5\n# Python \u7684\u5b57\u7b26\u5b9e\u9645\u4e0a\u662f\u957f\u5ea6\u4e3a 1 \u7684\u5b57\u7b26\u4e32\ncharacters: list[str] = ['0'] * 5\nbools: list[bool] = [False] * 5\n# Python \u7684\u5217\u8868\u53ef\u4ee5\u81ea\u7531\u5b58\u50a8\u5404\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u548c\u5bf9\u8c61\u5f15\u7528\ndata = [0, 0.0, 'a', False, ListNode(0)]\n
// \u4f7f\u7528\u591a\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6765\u521d\u59cb\u5316\u6570\u7ec4\nint numbers[5];\nfloat decimals[5];\nchar characters[5];\nbool bools[5];\n
// \u4f7f\u7528\u591a\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6765\u521d\u59cb\u5316\u6570\u7ec4\nint[] numbers = new int[5];\nfloat[] decimals = new float[5];\nchar[] characters = new char[5];\nboolean[] bools = new boolean[5];\n
// \u4f7f\u7528\u591a\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6765\u521d\u59cb\u5316\u6570\u7ec4\nint[] numbers = new int[5];\nfloat[] decimals = new float[5];\nchar[] characters = new char[5];\nbool[] bools = new bool[5];\n
// \u4f7f\u7528\u591a\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6765\u521d\u59cb\u5316\u6570\u7ec4\nvar numbers = [5]int{}\nvar decimals = [5]float64{}\nvar characters = [5]byte{}\nvar bools = [5]bool{}\n
// \u4f7f\u7528\u591a\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6765\u521d\u59cb\u5316\u6570\u7ec4\nlet numbers = Array(repeating: Int(), count: 5)\nlet decimals = Array(repeating: Double(), count: 5)\nlet characters = Array(repeating: Character(\"a\"), count: 5)\nlet bools = Array(repeating: Bool(), count: 5)\n
// JavaScript \u7684\u6570\u7ec4\u53ef\u4ee5\u81ea\u7531\u5b58\u50a8\u5404\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u548c\u5bf9\u8c61\nconst array = [0, 0.0, 'a', false];\n
// \u4f7f\u7528\u591a\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6765\u521d\u59cb\u5316\u6570\u7ec4\nconst numbers: number[] = [];\nconst characters: string[] = [];\nconst bools: boolean[] = [];\n
// \u4f7f\u7528\u591a\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6765\u521d\u59cb\u5316\u6570\u7ec4\nList<int> numbers = List.filled(5, 0);\nList<double> decimals = List.filled(5, 0.0);\nList<String> characters = List.filled(5, 'a');\nList<bool> bools = List.filled(5, false);\n
// \u4f7f\u7528\u591a\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6765\u521d\u59cb\u5316\u6570\u7ec4\nlet numbers: Vec<i32> = vec![0; 5];\nlet decimals: Vec<f32> = vec![0.0, 5];\nlet characters: Vec<char> = vec!['0'; 5];\nlet bools: Vec<bool> = vec![false; 5];\n
// \u4f7f\u7528\u591a\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6765\u521d\u59cb\u5316\u6570\u7ec4\nint numbers[10];\nfloat decimals[10];\nchar characters[10];\nbool bools[10];\n
\n
"},{"location":"chapter_data_structure/character_encoding/","title":"3.4 \u00a0 \u5b57\u7b26\u7f16\u7801 *","text":"\u5728\u8ba1\u7b97\u673a\u4e2d\uff0c\u6240\u6709\u6570\u636e\u90fd\u662f\u4ee5\u4e8c\u8fdb\u5236\u6570\u7684\u5f62\u5f0f\u5b58\u50a8\u7684\uff0c\u5b57\u7b26 char
\u4e5f\u4e0d\u4f8b\u5916\u3002\u4e3a\u4e86\u8868\u793a\u5b57\u7b26\uff0c\u6211\u4eec\u9700\u8981\u5efa\u7acb\u4e00\u5957\u201c\u5b57\u7b26\u96c6\u201d\uff0c\u89c4\u5b9a\u6bcf\u4e2a\u5b57\u7b26\u548c\u4e8c\u8fdb\u5236\u6570\u4e4b\u95f4\u7684\u4e00\u4e00\u5bf9\u5e94\u5173\u7cfb\u3002\u6709\u4e86\u5b57\u7b26\u96c6\u4e4b\u540e\uff0c\u8ba1\u7b97\u673a\u5c31\u53ef\u4ee5\u901a\u8fc7\u67e5\u8868\u5b8c\u6210\u4e8c\u8fdb\u5236\u6570\u5230\u5b57\u7b26\u7684\u8f6c\u6362\u3002
\u300cASCII \u7801\u300d\u662f\u6700\u65e9\u51fa\u73b0\u7684\u5b57\u7b26\u96c6\uff0c\u5176\u5168\u79f0\u4e3a American Standard Code for Information Interchange\uff08\u7f8e\u56fd\u6807\u51c6\u4fe1\u606f\u4ea4\u6362\u4ee3\u7801\uff09\u3002\u5b83\u4f7f\u7528 7 \u4f4d\u4e8c\u8fdb\u5236\u6570\uff08\u4e00\u4e2a\u5b57\u8282\u7684\u4f4e 7 \u4f4d\uff09\u8868\u793a\u4e00\u4e2a\u5b57\u7b26\uff0c\u6700\u591a\u80fd\u591f\u8868\u793a 128 \u4e2a\u4e0d\u540c\u7684\u5b57\u7b26\u3002\u5982\u56fe 3-6 \u6240\u793a\uff0cASCII \u7801\u5305\u62ec\u82f1\u6587\u5b57\u6bcd\u7684\u5927\u5c0f\u5199\u3001\u6570\u5b57 0 ~ 9\u3001\u4e00\u4e9b\u6807\u70b9\u7b26\u53f7\uff0c\u4ee5\u53ca\u4e00\u4e9b\u63a7\u5236\u5b57\u7b26\uff08\u5982\u6362\u884c\u7b26\u548c\u5236\u8868\u7b26\uff09\u3002
\u56fe 3-6 \u00a0 ASCII \u7801
\u7136\u800c\uff0cASCII \u7801\u4ec5\u80fd\u591f\u8868\u793a\u82f1\u6587\u3002\u968f\u7740\u8ba1\u7b97\u673a\u7684\u5168\u7403\u5316\uff0c\u8bde\u751f\u4e86\u4e00\u79cd\u80fd\u591f\u8868\u793a\u66f4\u591a\u8bed\u8a00\u7684\u5b57\u7b26\u96c6\u300cEASCII\u300d\u3002\u5b83\u5728 ASCII \u7684 7 \u4f4d\u57fa\u7840\u4e0a\u6269\u5c55\u5230 8 \u4f4d\uff0c\u80fd\u591f\u8868\u793a 256 \u4e2a\u4e0d\u540c\u7684\u5b57\u7b26\u3002
\u5728\u4e16\u754c\u8303\u56f4\u5185\uff0c\u9646\u7eed\u51fa\u73b0\u4e86\u4e00\u6279\u9002\u7528\u4e8e\u4e0d\u540c\u5730\u533a\u7684 EASCII \u5b57\u7b26\u96c6\u3002\u8fd9\u4e9b\u5b57\u7b26\u96c6\u7684\u524d 128 \u4e2a\u5b57\u7b26\u7edf\u4e00\u4e3a ASCII \u7801\uff0c\u540e 128 \u4e2a\u5b57\u7b26\u5b9a\u4e49\u4e0d\u540c\uff0c\u4ee5\u9002\u5e94\u4e0d\u540c\u8bed\u8a00\u7684\u9700\u6c42\u3002
"},{"location":"chapter_data_structure/character_encoding/#342-gbk","title":"3.4.2 \u00a0 GBK \u5b57\u7b26\u96c6","text":"\u540e\u6765\u4eba\u4eec\u53d1\u73b0\uff0cEASCII \u7801\u4ecd\u7136\u65e0\u6cd5\u6ee1\u8db3\u8bb8\u591a\u8bed\u8a00\u7684\u5b57\u7b26\u6570\u91cf\u8981\u6c42\u3002\u6bd4\u5982\u6c49\u5b57\u6709\u8fd1\u5341\u4e07\u4e2a\uff0c\u5149\u65e5\u5e38\u4f7f\u7528\u7684\u5c31\u6709\u51e0\u5343\u4e2a\u3002\u4e2d\u56fd\u56fd\u5bb6\u6807\u51c6\u603b\u5c40\u4e8e 1980 \u5e74\u53d1\u5e03\u4e86\u300cGB2312\u300d\u5b57\u7b26\u96c6\uff0c\u5176\u6536\u5f55\u4e86 6763 \u4e2a\u6c49\u5b57\uff0c\u57fa\u672c\u6ee1\u8db3\u4e86\u6c49\u5b57\u7684\u8ba1\u7b97\u673a\u5904\u7406\u9700\u8981\u3002
\u7136\u800c\uff0cGB2312 \u65e0\u6cd5\u5904\u7406\u90e8\u5206\u7f55\u89c1\u5b57\u548c\u7e41\u4f53\u5b57\u3002\u300cGBK\u300d\u5b57\u7b26\u96c6\u662f\u5728 GB2312 \u7684\u57fa\u7840\u4e0a\u6269\u5c55\u5f97\u5230\u7684\uff0c\u5b83\u5171\u6536\u5f55\u4e86 21886 \u4e2a\u6c49\u5b57\u3002\u5728 GBK \u7684\u7f16\u7801\u65b9\u6848\u4e2d\uff0cASCII \u5b57\u7b26\u4f7f\u7528\u4e00\u4e2a\u5b57\u8282\u8868\u793a\uff0c\u6c49\u5b57\u4f7f\u7528\u4e24\u4e2a\u5b57\u8282\u8868\u793a\u3002
"},{"location":"chapter_data_structure/character_encoding/#343-unicode","title":"3.4.3 \u00a0 Unicode \u5b57\u7b26\u96c6","text":"\u968f\u7740\u8ba1\u7b97\u673a\u6280\u672f\u7684\u84ec\u52c3\u53d1\u5c55\uff0c\u5b57\u7b26\u96c6\u4e0e\u7f16\u7801\u6807\u51c6\u767e\u82b1\u9f50\u653e\uff0c\u800c\u8fd9\u5e26\u6765\u4e86\u8bb8\u591a\u95ee\u9898\u3002\u4e00\u65b9\u9762\uff0c\u8fd9\u4e9b\u5b57\u7b26\u96c6\u4e00\u822c\u53ea\u5b9a\u4e49\u4e86\u7279\u5b9a\u8bed\u8a00\u7684\u5b57\u7b26\uff0c\u65e0\u6cd5\u5728\u591a\u8bed\u8a00\u73af\u5883\u4e0b\u6b63\u5e38\u5de5\u4f5c\u3002\u53e6\u4e00\u65b9\u9762\uff0c\u540c\u4e00\u79cd\u8bed\u8a00\u5b58\u5728\u591a\u79cd\u5b57\u7b26\u96c6\u6807\u51c6\uff0c\u5982\u679c\u4e24\u53f0\u8ba1\u7b97\u673a\u4f7f\u7528\u7684\u662f\u4e0d\u540c\u7684\u7f16\u7801\u6807\u51c6\uff0c\u5219\u5728\u4fe1\u606f\u4f20\u9012\u65f6\u5c31\u4f1a\u51fa\u73b0\u4e71\u7801\u3002
\u90a3\u4e2a\u65f6\u4ee3\u7684\u7814\u7a76\u4eba\u5458\u5c31\u5728\u60f3\uff1a\u5982\u679c\u63a8\u51fa\u4e00\u4e2a\u8db3\u591f\u5b8c\u6574\u7684\u5b57\u7b26\u96c6\uff0c\u5c06\u4e16\u754c\u8303\u56f4\u5185\u7684\u6240\u6709\u8bed\u8a00\u548c\u7b26\u53f7\u90fd\u6536\u5f55\u5176\u4e2d\uff0c\u4e0d\u5c31\u53ef\u4ee5\u89e3\u51b3\u8de8\u8bed\u8a00\u73af\u5883\u548c\u4e71\u7801\u95ee\u9898\u4e86\u5417\uff1f\u5728\u8fd9\u79cd\u60f3\u6cd5\u7684\u9a71\u52a8\u4e0b\uff0c\u4e00\u4e2a\u5927\u800c\u5168\u7684\u5b57\u7b26\u96c6 Unicode \u5e94\u8fd0\u800c\u751f\u3002
\u300cUnicode\u300d\u7684\u4e2d\u6587\u540d\u79f0\u4e3a\u201c\u7edf\u4e00\u7801\u201d\uff0c\u7406\u8bba\u4e0a\u80fd\u5bb9\u7eb3 100 \u591a\u4e07\u4e2a\u5b57\u7b26\u3002\u5b83\u81f4\u529b\u4e8e\u5c06\u5168\u7403\u8303\u56f4\u5185\u7684\u5b57\u7b26\u7eb3\u5165\u7edf\u4e00\u7684\u5b57\u7b26\u96c6\u4e4b\u4e2d\uff0c\u63d0\u4f9b\u4e00\u79cd\u901a\u7528\u7684\u5b57\u7b26\u96c6\u6765\u5904\u7406\u548c\u663e\u793a\u5404\u79cd\u8bed\u8a00\u6587\u5b57\uff0c\u51cf\u5c11\u56e0\u4e3a\u7f16\u7801\u6807\u51c6\u4e0d\u540c\u800c\u4ea7\u751f\u7684\u4e71\u7801\u95ee\u9898\u3002
\u81ea 1991 \u5e74\u53d1\u5e03\u4ee5\u6765\uff0cUnicode \u4e0d\u65ad\u6269\u5145\u65b0\u7684\u8bed\u8a00\u4e0e\u5b57\u7b26\u3002\u622a\u81f3 2022 \u5e74 9 \u6708\uff0cUnicode \u5df2\u7ecf\u5305\u542b 149186 \u4e2a\u5b57\u7b26\uff0c\u5305\u62ec\u5404\u79cd\u8bed\u8a00\u7684\u5b57\u7b26\u3001\u7b26\u53f7\u751a\u81f3\u8868\u60c5\u7b26\u53f7\u7b49\u3002\u5728\u5e9e\u5927\u7684 Unicode \u5b57\u7b26\u96c6\u4e2d\uff0c\u5e38\u7528\u7684\u5b57\u7b26\u5360\u7528 2 \u5b57\u8282\uff0c\u6709\u4e9b\u751f\u50fb\u7684\u5b57\u7b26\u5360\u7528 3 \u5b57\u8282\u751a\u81f3 4 \u5b57\u8282\u3002
Unicode \u662f\u4e00\u79cd\u901a\u7528\u5b57\u7b26\u96c6\uff0c\u672c\u8d28\u4e0a\u662f\u7ed9\u6bcf\u4e2a\u5b57\u7b26\u5206\u914d\u4e00\u4e2a\u7f16\u53f7\uff08\u79f0\u4e3a\u201c\u7801\u70b9\u201d\uff09\uff0c\u4f46\u5b83\u5e76\u6ca1\u6709\u89c4\u5b9a\u5728\u8ba1\u7b97\u673a\u4e2d\u5982\u4f55\u5b58\u50a8\u8fd9\u4e9b\u5b57\u7b26\u7801\u70b9\u3002\u6211\u4eec\u4e0d\u7981\u4f1a\u95ee\uff1a\u5f53\u591a\u79cd\u957f\u5ea6\u7684 Unicode \u7801\u70b9\u540c\u65f6\u51fa\u73b0\u5728\u4e00\u4e2a\u6587\u672c\u4e2d\u65f6\uff0c\u7cfb\u7edf\u5982\u4f55\u89e3\u6790\u5b57\u7b26\uff1f\u4f8b\u5982\u7ed9\u5b9a\u4e00\u4e2a\u957f\u5ea6\u4e3a 2 \u5b57\u8282\u7684\u7f16\u7801\uff0c\u7cfb\u7edf\u5982\u4f55\u786e\u8ba4\u5b83\u662f\u4e00\u4e2a 2 \u5b57\u8282\u7684\u5b57\u7b26\u8fd8\u662f\u4e24\u4e2a 1 \u5b57\u8282\u7684\u5b57\u7b26\uff1f
\u5bf9\u4e8e\u4ee5\u4e0a\u95ee\u9898\uff0c\u4e00\u79cd\u76f4\u63a5\u7684\u89e3\u51b3\u65b9\u6848\u662f\u5c06\u6240\u6709\u5b57\u7b26\u5b58\u50a8\u4e3a\u7b49\u957f\u7684\u7f16\u7801\u3002\u5982\u56fe 3-7 \u6240\u793a\uff0c\u201cHello\u201d\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u5360\u7528 1 \u5b57\u8282\uff0c\u201c\u7b97\u6cd5\u201d\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u5360\u7528 2 \u5b57\u8282\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u9ad8\u4f4d\u586b 0 \u5c06\u201cHello \u7b97\u6cd5\u201d\u4e2d\u7684\u6240\u6709\u5b57\u7b26\u90fd\u7f16\u7801\u4e3a 2 \u5b57\u8282\u957f\u5ea6\u3002\u8fd9\u6837\u7cfb\u7edf\u5c31\u53ef\u4ee5\u6bcf\u9694 2 \u5b57\u8282\u89e3\u6790\u4e00\u4e2a\u5b57\u7b26\uff0c\u6062\u590d\u8fd9\u4e2a\u77ed\u8bed\u7684\u5185\u5bb9\u4e86\u3002
\u56fe 3-7 \u00a0 Unicode \u7f16\u7801\u793a\u4f8b
\u7136\u800c ASCII \u7801\u5df2\u7ecf\u5411\u6211\u4eec\u8bc1\u660e\uff0c\u7f16\u7801\u82f1\u6587\u53ea\u9700 1 \u5b57\u8282\u3002\u82e5\u91c7\u7528\u4e0a\u8ff0\u65b9\u6848\uff0c\u82f1\u6587\u6587\u672c\u5360\u7528\u7a7a\u95f4\u7684\u5927\u5c0f\u5c06\u4f1a\u662f ASCII \u7f16\u7801\u4e0b\u7684\u4e24\u500d\uff0c\u975e\u5e38\u6d6a\u8d39\u5185\u5b58\u7a7a\u95f4\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u9700\u8981\u4e00\u79cd\u66f4\u52a0\u9ad8\u6548\u7684 Unicode \u7f16\u7801\u65b9\u6cd5\u3002
"},{"location":"chapter_data_structure/character_encoding/#344-utf-8","title":"3.4.4 \u00a0 UTF-8 \u7f16\u7801","text":"\u76ee\u524d\uff0cUTF-8 \u5df2\u6210\u4e3a\u56fd\u9645\u4e0a\u4f7f\u7528\u6700\u5e7f\u6cdb\u7684 Unicode \u7f16\u7801\u65b9\u6cd5\u3002\u5b83\u662f\u4e00\u79cd\u53ef\u53d8\u957f\u5ea6\u7684\u7f16\u7801\uff0c\u4f7f\u7528 1 \u5230 4 \u5b57\u8282\u6765\u8868\u793a\u4e00\u4e2a\u5b57\u7b26\uff0c\u6839\u636e\u5b57\u7b26\u7684\u590d\u6742\u6027\u800c\u53d8\u3002ASCII \u5b57\u7b26\u53ea\u9700 1 \u5b57\u8282\uff0c\u62c9\u4e01\u5b57\u6bcd\u548c\u5e0c\u814a\u5b57\u6bcd\u9700\u8981 2 \u5b57\u8282\uff0c\u5e38\u7528\u7684\u4e2d\u6587\u5b57\u7b26\u9700\u8981 3 \u5b57\u8282\uff0c\u5176\u4ed6\u7684\u4e00\u4e9b\u751f\u50fb\u5b57\u7b26\u9700\u8981 4 \u5b57\u8282\u3002
UTF-8 \u7684\u7f16\u7801\u89c4\u5219\u5e76\u4e0d\u590d\u6742\uff0c\u5206\u4e3a\u4ee5\u4e0b\u4e24\u79cd\u60c5\u51b5\u3002
\u56fe 3-8 \u5c55\u793a\u4e86\u201cHello\u7b97\u6cd5\u201d\u5bf9\u5e94\u7684 UTF-8 \u7f16\u7801\u3002\u89c2\u5bdf\u53d1\u73b0\uff0c\u7531\u4e8e\u6700\u9ad8 \\(n\\) \u4f4d\u90fd\u8bbe\u7f6e\u4e3a \\(1\\) \uff0c\u56e0\u6b64\u7cfb\u7edf\u53ef\u4ee5\u901a\u8fc7\u8bfb\u53d6\u6700\u9ad8\u4f4d \\(1\\) \u7684\u4e2a\u6570\u6765\u89e3\u6790\u51fa\u5b57\u7b26\u7684\u957f\u5ea6\u4e3a \\(n\\) \u3002
\u4f46\u4e3a\u4ec0\u4e48\u8981\u5c06\u5176\u4f59\u6240\u6709\u5b57\u8282\u7684\u9ad8 2 \u4f4d\u90fd\u8bbe\u7f6e\u4e3a \\(10\\) \u5462\uff1f\u5b9e\u9645\u4e0a\uff0c\u8fd9\u4e2a \\(10\\) \u80fd\u591f\u8d77\u5230\u6821\u9a8c\u7b26\u7684\u4f5c\u7528\u3002\u5047\u8bbe\u7cfb\u7edf\u4ece\u4e00\u4e2a\u9519\u8bef\u7684\u5b57\u8282\u5f00\u59cb\u89e3\u6790\u6587\u672c\uff0c\u5b57\u8282\u5934\u90e8\u7684 \\(10\\) \u80fd\u591f\u5e2e\u52a9\u7cfb\u7edf\u5feb\u901f\u5224\u65ad\u51fa\u5f02\u5e38\u3002
\u4e4b\u6240\u4ee5\u5c06 \\(10\\) \u5f53\u4f5c\u6821\u9a8c\u7b26\uff0c\u662f\u56e0\u4e3a\u5728 UTF-8 \u7f16\u7801\u89c4\u5219\u4e0b\uff0c\u4e0d\u53ef\u80fd\u6709\u5b57\u7b26\u7684\u6700\u9ad8\u4e24\u4f4d\u662f \\(10\\) \u3002\u8fd9\u4e2a\u7ed3\u8bba\u53ef\u4ee5\u7528\u53cd\u8bc1\u6cd5\u6765\u8bc1\u660e\uff1a\u5047\u8bbe\u4e00\u4e2a\u5b57\u7b26\u7684\u6700\u9ad8\u4e24\u4f4d\u662f \\(10\\) \uff0c\u8bf4\u660e\u8be5\u5b57\u7b26\u7684\u957f\u5ea6\u4e3a \\(1\\) \uff0c\u5bf9\u5e94 ASCII \u7801\u3002\u800c ASCII \u7801\u7684\u6700\u9ad8\u4f4d\u5e94\u8be5\u662f \\(0\\) \uff0c\u4e0e\u5047\u8bbe\u77db\u76fe\u3002
\u56fe 3-8 \u00a0 UTF-8 \u7f16\u7801\u793a\u4f8b
\u9664\u4e86 UTF-8 \u4e4b\u5916\uff0c\u5e38\u89c1\u7684\u7f16\u7801\u65b9\u5f0f\u8fd8\u5305\u62ec\u4ee5\u4e0b\u4e24\u79cd\u3002
\u4ece\u5b58\u50a8\u7a7a\u95f4\u5360\u7528\u7684\u89d2\u5ea6\u770b\uff0c\u4f7f\u7528 UTF-8 \u8868\u793a\u82f1\u6587\u5b57\u7b26\u975e\u5e38\u9ad8\u6548\uff0c\u56e0\u4e3a\u5b83\u4ec5\u9700 1 \u5b57\u8282\uff1b\u4f7f\u7528 UTF-16 \u7f16\u7801\u67d0\u4e9b\u975e\u82f1\u6587\u5b57\u7b26\uff08\u4f8b\u5982\u4e2d\u6587\uff09\u4f1a\u66f4\u52a0\u9ad8\u6548\uff0c\u56e0\u4e3a\u5b83\u4ec5\u9700 2 \u5b57\u8282\uff0c\u800c UTF-8 \u53ef\u80fd\u9700\u8981 3 \u5b57\u8282\u3002
\u4ece\u517c\u5bb9\u6027\u7684\u89d2\u5ea6\u770b\uff0cUTF-8 \u7684\u901a\u7528\u6027\u6700\u4f73\uff0c\u8bb8\u591a\u5de5\u5177\u548c\u5e93\u4f18\u5148\u652f\u6301 UTF-8 \u3002
"},{"location":"chapter_data_structure/character_encoding/#345","title":"3.4.5 \u00a0 \u7f16\u7a0b\u8bed\u8a00\u7684\u5b57\u7b26\u7f16\u7801","text":"\u5bf9\u4e8e\u4ee5\u5f80\u7684\u5927\u591a\u6570\u7f16\u7a0b\u8bed\u8a00\uff0c\u7a0b\u5e8f\u8fd0\u884c\u4e2d\u7684\u5b57\u7b26\u4e32\u90fd\u91c7\u7528 UTF-16 \u6216 UTF-32 \u8fd9\u7c7b\u7b49\u957f\u7684\u7f16\u7801\u3002\u5728\u7b49\u957f\u7f16\u7801\u4e0b\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u770b\u4f5c\u6570\u7ec4\u6765\u5904\u7406\uff0c\u8fd9\u79cd\u505a\u6cd5\u5177\u6709\u4ee5\u4e0b\u4f18\u70b9\u3002
\u5b9e\u9645\u4e0a\uff0c\u7f16\u7a0b\u8bed\u8a00\u7684\u5b57\u7b26\u7f16\u7801\u65b9\u6848\u8bbe\u8ba1\u662f\u4e00\u4e2a\u5f88\u6709\u8da3\u7684\u8bdd\u9898\uff0c\u6d89\u53ca\u8bb8\u591a\u56e0\u7d20\u3002
String
\u7c7b\u578b\u4f7f\u7528 UTF-16 \u7f16\u7801\uff0c\u6bcf\u4e2a\u5b57\u7b26\u5360\u7528 2 \u5b57\u8282\u3002\u8fd9\u662f\u56e0\u4e3a Java \u8bed\u8a00\u8bbe\u8ba1\u4e4b\u521d\uff0c\u4eba\u4eec\u8ba4\u4e3a 16 \u4f4d\u8db3\u4ee5\u8868\u793a\u6240\u6709\u53ef\u80fd\u7684\u5b57\u7b26\u3002\u7136\u800c\uff0c\u8fd9\u662f\u4e00\u4e2a\u4e0d\u6b63\u786e\u7684\u5224\u65ad\u3002\u540e\u6765 Unicode \u89c4\u8303\u6269\u5c55\u5230\u4e86\u8d85\u8fc7 16 \u4f4d\uff0c\u6240\u4ee5 Java \u4e2d\u7684\u5b57\u7b26\u73b0\u5728\u53ef\u80fd\u7531\u4e00\u5bf9 16 \u4f4d\u7684\u503c\uff08\u79f0\u4e3a\u201c\u4ee3\u7406\u5bf9\u201d\uff09\u8868\u793a\u3002\u7531\u4e8e\u4ee5\u4e0a\u7f16\u7a0b\u8bed\u8a00\u5bf9\u5b57\u7b26\u6570\u91cf\u7684\u4f4e\u4f30\uff0c\u5b83\u4eec\u4e0d\u5f97\u4e0d\u91c7\u53d6\u201c\u4ee3\u7406\u5bf9\u201d\u7684\u65b9\u5f0f\u6765\u8868\u793a\u8d85\u8fc7 16 \u4f4d\u957f\u5ea6\u7684 Unicode \u5b57\u7b26\u3002\u8fd9\u662f\u4e00\u4e2a\u4e0d\u5f97\u5df2\u4e3a\u4e4b\u7684\u65e0\u5948\u4e4b\u4e3e\u3002\u4e00\u65b9\u9762\uff0c\u5305\u542b\u4ee3\u7406\u5bf9\u7684\u5b57\u7b26\u4e32\u4e2d\uff0c\u4e00\u4e2a\u5b57\u7b26\u53ef\u80fd\u5360\u7528 2 \u5b57\u8282\u6216 4 \u5b57\u8282\uff0c\u4ece\u800c\u4e27\u5931\u4e86\u7b49\u957f\u7f16\u7801\u7684\u4f18\u52bf\u3002\u53e6\u4e00\u65b9\u9762\uff0c\u5904\u7406\u4ee3\u7406\u5bf9\u9700\u8981\u589e\u52a0\u989d\u5916\u4ee3\u7801\uff0c\u8fd9\u63d0\u9ad8\u4e86\u7f16\u7a0b\u7684\u590d\u6742\u6027\u548c\u8c03\u8bd5\u96be\u5ea6\u3002
\u51fa\u4e8e\u4ee5\u4e0a\u539f\u56e0\uff0c\u90e8\u5206\u7f16\u7a0b\u8bed\u8a00\u63d0\u51fa\u4e86\u4e00\u4e9b\u4e0d\u540c\u7684\u7f16\u7801\u65b9\u6848\u3002
str
\u4f7f\u7528 Unicode \u7f16\u7801\uff0c\u5e76\u91c7\u7528\u4e00\u79cd\u7075\u6d3b\u7684\u5b57\u7b26\u4e32\u8868\u793a\uff0c\u5b58\u50a8\u7684\u5b57\u7b26\u957f\u5ea6\u53d6\u51b3\u4e8e\u5b57\u7b26\u4e32\u4e2d\u6700\u5927\u7684 Unicode \u7801\u70b9\u3002\u82e5\u5b57\u7b26\u4e32\u4e2d\u5168\u90e8\u662f ASCII \u5b57\u7b26\uff0c\u5219\u6bcf\u4e2a\u5b57\u7b26\u5360\u7528 1 \u4e2a\u5b57\u8282\uff1b\u5982\u679c\u6709\u5b57\u7b26\u8d85\u51fa\u4e86 ASCII \u8303\u56f4\uff0c\u4f46\u5168\u90e8\u5728\u57fa\u672c\u591a\u8bed\u8a00\u5e73\u9762\uff08BMP\uff09\u5185\uff0c\u5219\u6bcf\u4e2a\u5b57\u7b26\u5360\u7528 2 \u4e2a\u5b57\u8282\uff1b\u5982\u679c\u6709\u8d85\u51fa BMP \u7684\u5b57\u7b26\uff0c\u5219\u6bcf\u4e2a\u5b57\u7b26\u5360\u7528 4 \u4e2a\u5b57\u8282\u3002string
\u7c7b\u578b\u5728\u5185\u90e8\u4f7f\u7528 UTF-8 \u7f16\u7801\u3002Go \u8bed\u8a00\u8fd8\u63d0\u4f9b\u4e86 rune
\u7c7b\u578b\uff0c\u5b83\u7528\u4e8e\u8868\u793a\u5355\u4e2a Unicode \u7801\u70b9\u3002char
\u7c7b\u578b\uff0c\u7528\u4e8e\u8868\u793a\u5355\u4e2a Unicode \u7801\u70b9\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u4ee5\u4e0a\u8ba8\u8bba\u7684\u90fd\u662f\u5b57\u7b26\u4e32\u5728\u7f16\u7a0b\u8bed\u8a00\u4e2d\u7684\u5b58\u50a8\u65b9\u5f0f\uff0c\u8fd9\u548c\u5b57\u7b26\u4e32\u5982\u4f55\u5728\u6587\u4ef6\u4e2d\u5b58\u50a8\u6216\u5728\u7f51\u7edc\u4e2d\u4f20\u8f93\u662f\u4e0d\u540c\u7684\u95ee\u9898\u3002\u5728\u6587\u4ef6\u5b58\u50a8\u6216\u7f51\u7edc\u4f20\u8f93\u4e2d\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u5c06\u5b57\u7b26\u4e32\u7f16\u7801\u4e3a UTF-8 \u683c\u5f0f\uff0c\u4ee5\u8fbe\u5230\u6700\u4f18\u7684\u517c\u5bb9\u6027\u548c\u7a7a\u95f4\u6548\u7387\u3002
"},{"location":"chapter_data_structure/classification_of_data_structure/","title":"3.1 \u00a0 \u6570\u636e\u7ed3\u6784\u5206\u7c7b","text":"\u5e38\u89c1\u7684\u6570\u636e\u7ed3\u6784\u5305\u62ec\u6570\u7ec4\u3001\u94fe\u8868\u3001\u6808\u3001\u961f\u5217\u3001\u54c8\u5e0c\u8868\u3001\u6811\u3001\u5806\u3001\u56fe\uff0c\u5b83\u4eec\u53ef\u4ee5\u4ece\u201c\u903b\u8f91\u7ed3\u6784\u201d\u548c\u201c\u7269\u7406\u7ed3\u6784\u201d\u4e24\u4e2a\u7ef4\u5ea6\u8fdb\u884c\u5206\u7c7b\u3002
"},{"location":"chapter_data_structure/classification_of_data_structure/#311","title":"3.1.1 \u00a0 \u903b\u8f91\u7ed3\u6784\uff1a\u7ebf\u6027\u4e0e\u975e\u7ebf\u6027","text":"\u903b\u8f91\u7ed3\u6784\u63ed\u793a\u4e86\u6570\u636e\u5143\u7d20\u4e4b\u95f4\u7684\u903b\u8f91\u5173\u7cfb\u3002\u5728\u6570\u7ec4\u548c\u94fe\u8868\u4e2d\uff0c\u6570\u636e\u6309\u7167\u4e00\u5b9a\u987a\u5e8f\u6392\u5217\uff0c\u4f53\u73b0\u4e86\u6570\u636e\u4e4b\u95f4\u7684\u7ebf\u6027\u5173\u7cfb\uff1b\u800c\u5728\u6811\u4e2d\uff0c\u6570\u636e\u4ece\u9876\u90e8\u5411\u4e0b\u6309\u5c42\u6b21\u6392\u5217\uff0c\u8868\u73b0\u51fa\u201c\u7956\u5148\u201d\u4e0e\u201c\u540e\u4ee3\u201d\u4e4b\u95f4\u7684\u6d3e\u751f\u5173\u7cfb\uff1b\u56fe\u5219\u7531\u8282\u70b9\u548c\u8fb9\u6784\u6210\uff0c\u53cd\u6620\u4e86\u590d\u6742\u7684\u7f51\u7edc\u5173\u7cfb\u3002
\u5982\u56fe 3-1 \u6240\u793a\uff0c\u903b\u8f91\u7ed3\u6784\u53ef\u5206\u4e3a\u201c\u7ebf\u6027\u201d\u548c\u201c\u975e\u7ebf\u6027\u201d\u4e24\u5927\u7c7b\u3002\u7ebf\u6027\u7ed3\u6784\u6bd4\u8f83\u76f4\u89c2\uff0c\u6307\u6570\u636e\u5728\u903b\u8f91\u5173\u7cfb\u4e0a\u5448\u7ebf\u6027\u6392\u5217\uff1b\u975e\u7ebf\u6027\u7ed3\u6784\u5219\u76f8\u53cd\uff0c\u5448\u975e\u7ebf\u6027\u6392\u5217\u3002
\u56fe 3-1 \u00a0 \u7ebf\u6027\u6570\u636e\u7ed3\u6784\u4e0e\u975e\u7ebf\u6027\u6570\u636e\u7ed3\u6784
\u975e\u7ebf\u6027\u6570\u636e\u7ed3\u6784\u53ef\u4ee5\u8fdb\u4e00\u6b65\u5212\u5206\u4e3a\u6811\u5f62\u7ed3\u6784\u548c\u7f51\u72b6\u7ed3\u6784\u3002
\u5f53\u7b97\u6cd5\u7a0b\u5e8f\u8fd0\u884c\u65f6\uff0c\u6b63\u5728\u5904\u7406\u7684\u6570\u636e\u4e3b\u8981\u88ab\u5b58\u50a8\u5728\u5185\u5b58\u4e2d\u3002\u56fe 3-2 \u5c55\u793a\u4e86\u4e00\u4e2a\u8ba1\u7b97\u673a\u5185\u5b58\u6761\uff0c\u5176\u4e2d\u6bcf\u4e2a\u9ed1\u8272\u65b9\u5757\u90fd\u5305\u542b\u4e00\u5757\u5185\u5b58\u7a7a\u95f4\u3002\u6211\u4eec\u53ef\u4ee5\u5c06\u5185\u5b58\u60f3\u8c61\u6210\u4e00\u4e2a\u5de8\u5927\u7684 Excel \u8868\u683c\uff0c\u5176\u4e2d\u6bcf\u4e2a\u5355\u5143\u683c\u90fd\u53ef\u4ee5\u5b58\u50a8\u4e00\u5b9a\u5927\u5c0f\u7684\u6570\u636e\u3002
\u7cfb\u7edf\u901a\u8fc7\u5185\u5b58\u5730\u5740\u6765\u8bbf\u95ee\u76ee\u6807\u4f4d\u7f6e\u7684\u6570\u636e\u3002\u5982\u56fe 3-2 \u6240\u793a\uff0c\u8ba1\u7b97\u673a\u6839\u636e\u7279\u5b9a\u89c4\u5219\u4e3a\u8868\u683c\u4e2d\u7684\u6bcf\u4e2a\u5355\u5143\u683c\u5206\u914d\u7f16\u53f7\uff0c\u786e\u4fdd\u6bcf\u4e2a\u5185\u5b58\u7a7a\u95f4\u90fd\u6709\u552f\u4e00\u7684\u5185\u5b58\u5730\u5740\u3002\u6709\u4e86\u8fd9\u4e9b\u5730\u5740\uff0c\u7a0b\u5e8f\u4fbf\u53ef\u4ee5\u8bbf\u95ee\u5185\u5b58\u4e2d\u7684\u6570\u636e\u3002
\u56fe 3-2 \u00a0 \u5185\u5b58\u6761\u3001\u5185\u5b58\u7a7a\u95f4\u3001\u5185\u5b58\u5730\u5740
Tip
\u503c\u5f97\u8bf4\u660e\u7684\u662f\uff0c\u5c06\u5185\u5b58\u6bd4\u4f5c Excel \u8868\u683c\u662f\u4e00\u4e2a\u7b80\u5316\u7684\u7c7b\u6bd4\uff0c\u5b9e\u9645\u5185\u5b58\u7684\u5de5\u4f5c\u673a\u5236\u6bd4\u8f83\u590d\u6742\uff0c\u6d89\u53ca\u5230\u5730\u5740\u7a7a\u95f4\u3001\u5185\u5b58\u7ba1\u7406\u3001\u7f13\u5b58\u673a\u5236\u3001\u865a\u62df\u548c\u7269\u7406\u5185\u5b58\u7b49\u6982\u5ff5\u3002
\u5185\u5b58\u662f\u6240\u6709\u7a0b\u5e8f\u7684\u5171\u4eab\u8d44\u6e90\uff0c\u5f53\u67d0\u5757\u5185\u5b58\u88ab\u67d0\u4e2a\u7a0b\u5e8f\u5360\u7528\u65f6\uff0c\u5219\u65e0\u6cd5\u88ab\u5176\u4ed6\u7a0b\u5e8f\u540c\u65f6\u4f7f\u7528\u4e86\u3002\u56e0\u6b64\u5728\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7684\u8bbe\u8ba1\u4e2d\uff0c\u5185\u5b58\u8d44\u6e90\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u8003\u8651\u56e0\u7d20\u3002\u6bd4\u5982\uff0c\u7b97\u6cd5\u6240\u5360\u7528\u7684\u5185\u5b58\u5cf0\u503c\u4e0d\u5e94\u8d85\u8fc7\u7cfb\u7edf\u5269\u4f59\u7a7a\u95f2\u5185\u5b58\uff1b\u5982\u679c\u7f3a\u5c11\u8fde\u7eed\u5927\u5757\u7684\u5185\u5b58\u7a7a\u95f4\uff0c\u90a3\u4e48\u6240\u9009\u7528\u7684\u6570\u636e\u7ed3\u6784\u5fc5\u987b\u80fd\u591f\u5b58\u50a8\u5728\u5206\u6563\u7684\u5185\u5b58\u7a7a\u95f4\u5185\u3002
\u5982\u56fe 3-3 \u6240\u793a\uff0c\u7269\u7406\u7ed3\u6784\u53cd\u6620\u4e86\u6570\u636e\u5728\u8ba1\u7b97\u673a\u5185\u5b58\u4e2d\u7684\u5b58\u50a8\u65b9\u5f0f\uff0c\u53ef\u5206\u4e3a\u8fde\u7eed\u7a7a\u95f4\u5b58\u50a8\uff08\u6570\u7ec4\uff09\u548c\u5206\u6563\u7a7a\u95f4\u5b58\u50a8\uff08\u94fe\u8868\uff09\u3002\u7269\u7406\u7ed3\u6784\u4ece\u5e95\u5c42\u51b3\u5b9a\u4e86\u6570\u636e\u7684\u8bbf\u95ee\u3001\u66f4\u65b0\u3001\u589e\u5220\u7b49\u64cd\u4f5c\u65b9\u6cd5\uff0c\u4e24\u79cd\u7269\u7406\u7ed3\u6784\u5728\u65f6\u95f4\u6548\u7387\u548c\u7a7a\u95f4\u6548\u7387\u65b9\u9762\u5448\u73b0\u51fa\u4e92\u8865\u7684\u7279\u70b9\u3002
\u56fe 3-3 \u00a0 \u8fde\u7eed\u7a7a\u95f4\u5b58\u50a8\u4e0e\u5206\u6563\u7a7a\u95f4\u5b58\u50a8
\u503c\u5f97\u8bf4\u660e\u7684\u662f\uff0c\u6240\u6709\u6570\u636e\u7ed3\u6784\u90fd\u662f\u57fa\u4e8e\u6570\u7ec4\u3001\u94fe\u8868\u6216\u4e8c\u8005\u7684\u7ec4\u5408\u5b9e\u73b0\u7684\u3002\u4f8b\u5982\uff0c\u6808\u548c\u961f\u5217\u65e2\u53ef\u4ee5\u4f7f\u7528\u6570\u7ec4\u5b9e\u73b0\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528\u94fe\u8868\u5b9e\u73b0\uff1b\u800c\u54c8\u5e0c\u8868\u7684\u5b9e\u73b0\u53ef\u80fd\u540c\u65f6\u5305\u542b\u6570\u7ec4\u548c\u94fe\u8868\u3002
\u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6570\u636e\u7ed3\u6784\u4e5f\u79f0\u201c\u9759\u6001\u6570\u636e\u7ed3\u6784\u201d\uff0c\u8fd9\u610f\u5473\u7740\u6b64\u7c7b\u6570\u636e\u7ed3\u6784\u5728\u521d\u59cb\u5316\u540e\u957f\u5ea6\u4e0d\u53ef\u53d8\u3002\u76f8\u5bf9\u5e94\u5730\uff0c\u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6570\u636e\u7ed3\u6784\u79f0\u201c\u52a8\u6001\u6570\u636e\u7ed3\u6784\u201d\uff0c\u8fd9\u7c7b\u6570\u636e\u7ed3\u6784\u5728\u521d\u59cb\u5316\u540e\uff0c\u4ecd\u53ef\u4ee5\u5728\u7a0b\u5e8f\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u5bf9\u5176\u957f\u5ea6\u8fdb\u884c\u8c03\u6574\u3002
Tip
\u5982\u679c\u4f60\u611f\u89c9\u7269\u7406\u7ed3\u6784\u7406\u89e3\u8d77\u6765\u6709\u56f0\u96be\uff0c\u5efa\u8bae\u5148\u9605\u8bfb\u4e0b\u4e00\u7ae0\uff0c\u7136\u540e\u518d\u56de\u987e\u672c\u8282\u5185\u5bb9\u3002
"},{"location":"chapter_data_structure/number_encoding/","title":"3.3 \u00a0 \u6570\u5b57\u7f16\u7801 *","text":"Note
\u5728\u672c\u4e66\u4e2d\uff0c\u6807\u9898\u5e26\u6709 * \u7b26\u53f7\u7684\u662f\u9009\u8bfb\u7ae0\u8282\u3002\u5982\u679c\u4f60\u65f6\u95f4\u6709\u9650\u6216\u611f\u5230\u7406\u89e3\u56f0\u96be\uff0c\u53ef\u4ee5\u5148\u8df3\u8fc7\uff0c\u7b49\u5b66\u5b8c\u5fc5\u8bfb\u7ae0\u8282\u540e\u518d\u5355\u72ec\u653b\u514b\u3002
"},{"location":"chapter_data_structure/number_encoding/#331","title":"3.3.1 \u00a0 \u6574\u6570\u7f16\u7801","text":"\u5728\u4e0a\u4e00\u8282\u7684\u8868\u683c\u4e2d\u6211\u4eec\u53d1\u73b0\uff0c\u6240\u6709\u6574\u6570\u7c7b\u578b\u80fd\u591f\u8868\u793a\u7684\u8d1f\u6570\u90fd\u6bd4\u6b63\u6570\u591a\u4e00\u4e2a\uff0c\u4f8b\u5982 byte
\u7684\u53d6\u503c\u8303\u56f4\u662f \\([-128, 127]\\) \u3002\u8fd9\u4e2a\u73b0\u8c61\u6bd4\u8f83\u53cd\u76f4\u89c9\uff0c\u5b83\u7684\u5185\u5728\u539f\u56e0\u6d89\u53ca\u539f\u7801\u3001\u53cd\u7801\u3001\u8865\u7801\u7684\u76f8\u5173\u77e5\u8bc6\u3002
\u9996\u5148\u9700\u8981\u6307\u51fa\uff0c\u6570\u5b57\u662f\u4ee5\u201c\u8865\u7801\u201d\u7684\u5f62\u5f0f\u5b58\u50a8\u5728\u8ba1\u7b97\u673a\u4e2d\u7684\u3002\u5728\u5206\u6790\u8fd9\u6837\u505a\u7684\u539f\u56e0\u4e4b\u524d\uff0c\u9996\u5148\u7ed9\u51fa\u4e09\u8005\u7684\u5b9a\u4e49\u3002
\u56fe 3-4 \u5c55\u793a\u4e86\u539f\u7801\u3001\u53cd\u7801\u548c\u8865\u7801\u4e4b\u95f4\u7684\u8f6c\u6362\u65b9\u6cd5\u3002
\u56fe 3-4 \u00a0 \u539f\u7801\u3001\u53cd\u7801\u4e0e\u8865\u7801\u4e4b\u95f4\u7684\u76f8\u4e92\u8f6c\u6362
\u300c\u539f\u7801 sign\u2013magnitude\u300d\u867d\u7136\u6700\u76f4\u89c2\uff0c\u4f46\u5b58\u5728\u4e00\u4e9b\u5c40\u9650\u6027\u3002\u4e00\u65b9\u9762\uff0c\u8d1f\u6570\u7684\u539f\u7801\u4e0d\u80fd\u76f4\u63a5\u7528\u4e8e\u8fd0\u7b97\u3002\u4f8b\u5982\u5728\u539f\u7801\u4e0b\u8ba1\u7b97 \\(1 + (-2)\\) \uff0c\u5f97\u5230\u7684\u7ed3\u679c\u662f \\(-3\\) \uff0c\u8fd9\u663e\u7136\u662f\u4e0d\u5bf9\u7684\u3002
\\[ \\begin{aligned} & 1 + (-2) \\newline & \\rightarrow 0000 \\; 0001 + 1000 \\; 0010 \\newline & = 1000 \\; 0011 \\newline & \\rightarrow -3 \\end{aligned} \\]\u4e3a\u4e86\u89e3\u51b3\u6b64\u95ee\u9898\uff0c\u8ba1\u7b97\u673a\u5f15\u5165\u4e86\u300c\u53cd\u7801 1's complement\u300d\u3002\u5982\u679c\u6211\u4eec\u5148\u5c06\u539f\u7801\u8f6c\u6362\u4e3a\u53cd\u7801\uff0c\u5e76\u5728\u53cd\u7801\u4e0b\u8ba1\u7b97 \\(1 + (-2)\\) \uff0c\u6700\u540e\u5c06\u7ed3\u679c\u4ece\u53cd\u7801\u8f6c\u6362\u56de\u539f\u7801\uff0c\u5219\u53ef\u5f97\u5230\u6b63\u786e\u7ed3\u679c \\(-1\\) \u3002
\\[ \\begin{aligned} & 1 + (-2) \\newline & \\rightarrow 0000 \\; 0001 \\; \\text{(\u539f\u7801)} + 1000 \\; 0010 \\; \\text{(\u539f\u7801)} \\newline & = 0000 \\; 0001 \\; \\text{(\u53cd\u7801)} + 1111 \\; 1101 \\; \\text{(\u53cd\u7801)} \\newline & = 1111 \\; 1110 \\; \\text{(\u53cd\u7801)} \\newline & = 1000 \\; 0001 \\; \\text{(\u539f\u7801)} \\newline & \\rightarrow -1 \\end{aligned} \\]\u53e6\u4e00\u65b9\u9762\uff0c\u6570\u5b57\u96f6\u7684\u539f\u7801\u6709 \\(+0\\) \u548c \\(-0\\) \u4e24\u79cd\u8868\u793a\u65b9\u5f0f\u3002\u8fd9\u610f\u5473\u7740\u6570\u5b57\u96f6\u5bf9\u5e94\u4e24\u4e2a\u4e0d\u540c\u7684\u4e8c\u8fdb\u5236\u7f16\u7801\uff0c\u8fd9\u53ef\u80fd\u4f1a\u5e26\u6765\u6b67\u4e49\u3002\u6bd4\u5982\u5728\u6761\u4ef6\u5224\u65ad\u4e2d\uff0c\u5982\u679c\u6ca1\u6709\u533a\u5206\u6b63\u96f6\u548c\u8d1f\u96f6\uff0c\u5219\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5224\u65ad\u7ed3\u679c\u51fa\u9519\u3002\u800c\u5982\u679c\u6211\u4eec\u60f3\u5904\u7406\u6b63\u96f6\u548c\u8d1f\u96f6\u6b67\u4e49\uff0c\u5219\u9700\u8981\u5f15\u5165\u989d\u5916\u7684\u5224\u65ad\u64cd\u4f5c\uff0c\u8fd9\u53ef\u80fd\u4f1a\u964d\u4f4e\u8ba1\u7b97\u673a\u7684\u8fd0\u7b97\u6548\u7387\u3002
\\[ \\begin{aligned} +0 & \\rightarrow 0000 \\; 0000 \\newline -0 & \\rightarrow 1000 \\; 0000 \\end{aligned} \\]\u4e0e\u539f\u7801\u4e00\u6837\uff0c\u53cd\u7801\u4e5f\u5b58\u5728\u6b63\u8d1f\u96f6\u6b67\u4e49\u95ee\u9898\uff0c\u56e0\u6b64\u8ba1\u7b97\u673a\u8fdb\u4e00\u6b65\u5f15\u5165\u4e86\u300c\u8865\u7801 2's complement\u300d\u3002\u6211\u4eec\u5148\u6765\u89c2\u5bdf\u4e00\u4e0b\u8d1f\u96f6\u7684\u539f\u7801\u3001\u53cd\u7801\u3001\u8865\u7801\u7684\u8f6c\u6362\u8fc7\u7a0b\uff1a
\\[ \\begin{aligned} -0 \\rightarrow \\; & 1000 \\; 0000 \\; \\text{(\u539f\u7801)} \\newline = \\; & 1111 \\; 1111 \\; \\text{(\u53cd\u7801)} \\newline = 1 \\; & 0000 \\; 0000 \\; \\text{(\u8865\u7801)} \\newline \\end{aligned} \\]\u5728\u8d1f\u96f6\u7684\u53cd\u7801\u57fa\u7840\u4e0a\u52a0 \\(1\\) \u4f1a\u4ea7\u751f\u8fdb\u4f4d\uff0c\u4f46 byte
\u7c7b\u578b\u7684\u957f\u5ea6\u53ea\u6709 8 \u4f4d\uff0c\u56e0\u6b64\u6ea2\u51fa\u5230\u7b2c 9 \u4f4d\u7684 \\(1\\) \u4f1a\u88ab\u820d\u5f03\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u8d1f\u96f6\u7684\u8865\u7801\u4e3a \\(0000 \\; 0000\\) \uff0c\u4e0e\u6b63\u96f6\u7684\u8865\u7801\u76f8\u540c\u3002\u8fd9\u610f\u5473\u7740\u5728\u8865\u7801\u8868\u793a\u4e2d\u53ea\u5b58\u5728\u4e00\u4e2a\u96f6\uff0c\u6b63\u8d1f\u96f6\u6b67\u4e49\u4ece\u800c\u5f97\u5230\u89e3\u51b3\u3002
\u8fd8\u5269\u6700\u540e\u4e00\u4e2a\u7591\u60d1\uff1abyte
\u7c7b\u578b\u7684\u53d6\u503c\u8303\u56f4\u662f \\([-128, 127]\\) \uff0c\u591a\u51fa\u6765\u7684\u4e00\u4e2a\u8d1f\u6570 \\(-128\\) \u662f\u5982\u4f55\u5f97\u5230\u7684\u5462\uff1f\u6211\u4eec\u6ce8\u610f\u5230\uff0c\u533a\u95f4 \\([-127, +127]\\) \u5185\u7684\u6240\u6709\u6574\u6570\u90fd\u6709\u5bf9\u5e94\u7684\u539f\u7801\u3001\u53cd\u7801\u548c\u8865\u7801\uff0c\u5e76\u4e14\u539f\u7801\u548c\u8865\u7801\u4e4b\u95f4\u53ef\u4ee5\u4e92\u76f8\u8f6c\u6362\u3002
\u7136\u800c\uff0c\u8865\u7801 \\(1000 \\; 0000\\) \u662f\u4e00\u4e2a\u4f8b\u5916\uff0c\u5b83\u5e76\u6ca1\u6709\u5bf9\u5e94\u7684\u539f\u7801\u3002\u6839\u636e\u8f6c\u6362\u65b9\u6cd5\uff0c\u6211\u4eec\u5f97\u5230\u8be5\u8865\u7801\u7684\u539f\u7801\u4e3a \\(0000 \\; 0000\\) \u3002\u8fd9\u663e\u7136\u662f\u77db\u76fe\u7684\uff0c\u56e0\u4e3a\u8be5\u539f\u7801\u8868\u793a\u6570\u5b57 \\(0\\) \uff0c\u5b83\u7684\u8865\u7801\u5e94\u8be5\u662f\u81ea\u8eab\u3002\u8ba1\u7b97\u673a\u89c4\u5b9a\u8fd9\u4e2a\u7279\u6b8a\u7684\u8865\u7801 \\(1000 \\; 0000\\) \u4ee3\u8868 \\(-128\\) \u3002\u5b9e\u9645\u4e0a\uff0c\\((-1) + (-127)\\) \u5728\u8865\u7801\u4e0b\u7684\u8ba1\u7b97\u7ed3\u679c\u5c31\u662f \\(-128\\) \u3002
\\[ \\begin{aligned} & (-127) + (-1) \\newline & \\rightarrow 1111 \\; 1111 \\; \\text{(\u539f\u7801)} + 1000 \\; 0001 \\; \\text{(\u539f\u7801)} \\newline & = 1000 \\; 0000 \\; \\text{(\u53cd\u7801)} + 1111 \\; 1110 \\; \\text{(\u53cd\u7801)} \\newline & = 1000 \\; 0001 \\; \\text{(\u8865\u7801)} + 1111 \\; 1111 \\; \\text{(\u8865\u7801)} \\newline & = 1000 \\; 0000 \\; \\text{(\u8865\u7801)} \\newline & \\rightarrow -128 \\end{aligned} \\]\u4f60\u53ef\u80fd\u5df2\u7ecf\u53d1\u73b0\u4e86\uff0c\u4e0a\u8ff0\u6240\u6709\u8ba1\u7b97\u90fd\u662f\u52a0\u6cd5\u8fd0\u7b97\u3002\u8fd9\u6697\u793a\u7740\u4e00\u4e2a\u91cd\u8981\u4e8b\u5b9e\uff1a\u8ba1\u7b97\u673a\u5185\u90e8\u7684\u786c\u4ef6\u7535\u8def\u4e3b\u8981\u662f\u57fa\u4e8e\u52a0\u6cd5\u8fd0\u7b97\u8bbe\u8ba1\u7684\u3002\u8fd9\u662f\u56e0\u4e3a\u52a0\u6cd5\u8fd0\u7b97\u76f8\u5bf9\u4e8e\u5176\u4ed6\u8fd0\u7b97\uff08\u6bd4\u5982\u4e58\u6cd5\u3001\u9664\u6cd5\u548c\u51cf\u6cd5\uff09\u6765\u8bf4\uff0c\u786c\u4ef6\u5b9e\u73b0\u8d77\u6765\u66f4\u7b80\u5355\uff0c\u66f4\u5bb9\u6613\u8fdb\u884c\u5e76\u884c\u5316\u5904\u7406\uff0c\u8fd0\u7b97\u901f\u5ea6\u66f4\u5feb\u3002
\u8bf7\u6ce8\u610f\uff0c\u8fd9\u5e76\u4e0d\u610f\u5473\u7740\u8ba1\u7b97\u673a\u53ea\u80fd\u505a\u52a0\u6cd5\u3002\u901a\u8fc7\u5c06\u52a0\u6cd5\u4e0e\u4e00\u4e9b\u57fa\u672c\u903b\u8f91\u8fd0\u7b97\u7ed3\u5408\uff0c\u8ba1\u7b97\u673a\u80fd\u591f\u5b9e\u73b0\u5404\u79cd\u5176\u4ed6\u7684\u6570\u5b66\u8fd0\u7b97\u3002\u4f8b\u5982\uff0c\u8ba1\u7b97\u51cf\u6cd5 \\(a - b\\) \u53ef\u4ee5\u8f6c\u6362\u4e3a\u8ba1\u7b97\u52a0\u6cd5 \\(a + (-b)\\) \uff1b\u8ba1\u7b97\u4e58\u6cd5\u548c\u9664\u6cd5\u53ef\u4ee5\u8f6c\u6362\u4e3a\u8ba1\u7b97\u591a\u6b21\u52a0\u6cd5\u6216\u51cf\u6cd5\u3002
\u73b0\u5728\u6211\u4eec\u53ef\u4ee5\u603b\u7ed3\u51fa\u8ba1\u7b97\u673a\u4f7f\u7528\u8865\u7801\u7684\u539f\u56e0\uff1a\u57fa\u4e8e\u8865\u7801\u8868\u793a\uff0c\u8ba1\u7b97\u673a\u53ef\u4ee5\u7528\u540c\u6837\u7684\u7535\u8def\u548c\u64cd\u4f5c\u6765\u5904\u7406\u6b63\u6570\u548c\u8d1f\u6570\u7684\u52a0\u6cd5\uff0c\u4e0d\u9700\u8981\u8bbe\u8ba1\u7279\u6b8a\u7684\u786c\u4ef6\u7535\u8def\u6765\u5904\u7406\u51cf\u6cd5\uff0c\u5e76\u4e14\u65e0\u987b\u7279\u522b\u5904\u7406\u6b63\u8d1f\u96f6\u7684\u6b67\u4e49\u95ee\u9898\u3002\u8fd9\u5927\u5927\u7b80\u5316\u4e86\u786c\u4ef6\u8bbe\u8ba1\uff0c\u63d0\u9ad8\u4e86\u8fd0\u7b97\u6548\u7387\u3002
\u8865\u7801\u7684\u8bbe\u8ba1\u975e\u5e38\u7cbe\u5999\uff0c\u56e0\u7bc7\u5e45\u5173\u7cfb\u6211\u4eec\u5c31\u5148\u4ecb\u7ecd\u5230\u8fd9\u91cc\uff0c\u5efa\u8bae\u6709\u5174\u8da3\u7684\u8bfb\u8005\u8fdb\u4e00\u6b65\u6df1\u5165\u4e86\u89e3\u3002
"},{"location":"chapter_data_structure/number_encoding/#332","title":"3.3.2 \u00a0 \u6d6e\u70b9\u6570\u7f16\u7801","text":"\u7ec6\u5fc3\u7684\u4f60\u53ef\u80fd\u4f1a\u53d1\u73b0\uff1aint
\u548c float
\u957f\u5ea6\u76f8\u540c\uff0c\u90fd\u662f 4 bytes \uff0c\u4f46\u4e3a\u4ec0\u4e48 float
\u7684\u53d6\u503c\u8303\u56f4\u8fdc\u5927\u4e8e int
\uff1f\u8fd9\u975e\u5e38\u53cd\u76f4\u89c9\uff0c\u56e0\u4e3a\u6309\u7406\u8bf4 float
\u9700\u8981\u8868\u793a\u5c0f\u6570\uff0c\u53d6\u503c\u8303\u56f4\u5e94\u8be5\u53d8\u5c0f\u624d\u5bf9\u3002
\u5b9e\u9645\u4e0a\uff0c\u8fd9\u662f\u56e0\u4e3a\u6d6e\u70b9\u6570 float
\u91c7\u7528\u4e86\u4e0d\u540c\u7684\u8868\u793a\u65b9\u5f0f\u3002\u8bb0\u4e00\u4e2a 32-bit \u957f\u5ea6\u7684\u4e8c\u8fdb\u5236\u6570\u4e3a\uff1a
\u6839\u636e IEEE 754 \u6807\u51c6\uff0c32-bit \u957f\u5ea6\u7684 float
\u7531\u4ee5\u4e0b\u4e09\u4e2a\u90e8\u5206\u6784\u6210\u3002
\u4e8c\u8fdb\u5236\u6570 float
\u5bf9\u5e94\u503c\u7684\u8ba1\u7b97\u65b9\u6cd5\u4e3a\uff1a
\u8f6c\u5316\u5230\u5341\u8fdb\u5236\u4e0b\u7684\u8ba1\u7b97\u516c\u5f0f\u4e3a\uff1a
\\[ \\text {val}=(-1)^{\\mathrm{S}} \\times 2^{\\mathrm{E} -127} \\times (1 + \\mathrm{N}) \\]\u5176\u4e2d\u5404\u9879\u7684\u53d6\u503c\u8303\u56f4\u4e3a\uff1a
\\[ \\begin{aligned} \\mathrm{S} \\in & \\{ 0, 1\\}, \\quad \\mathrm{E} \\in \\{ 1, 2, \\dots, 254 \\} \\newline (1 + \\mathrm{N}) = & (1 + \\sum_{i=1}^{23} b_{23-i} 2^{-i}) \\subset [1, 2 - 2^{-23}] \\end{aligned} \\]\u56fe 3-5 \u00a0 IEEE 754 \u6807\u51c6\u4e0b\u7684 float \u7684\u8ba1\u7b97\u793a\u4f8b
\u89c2\u5bdf\u56fe 3-5 \uff0c\u7ed9\u5b9a\u4e00\u4e2a\u793a\u4f8b\u6570\u636e \\(\\mathrm{S} = 0\\) \uff0c \\(\\mathrm{E} = 124\\) \uff0c\\(\\mathrm{N} = 2^{-2} + 2^{-3} = 0.375\\) \uff0c\u5219\u6709\uff1a
\\[ \\text { val } = (-1)^0 \\times 2^{124 - 127} \\times (1 + 0.375) = 0.171875 \\]\u73b0\u5728\u6211\u4eec\u53ef\u4ee5\u56de\u7b54\u6700\u521d\u7684\u95ee\u9898\uff1afloat
\u7684\u8868\u793a\u65b9\u5f0f\u5305\u542b\u6307\u6570\u4f4d\uff0c\u5bfc\u81f4\u5176\u53d6\u503c\u8303\u56f4\u8fdc\u5927\u4e8e int
\u3002\u6839\u636e\u4ee5\u4e0a\u8ba1\u7b97\uff0cfloat
\u53ef\u8868\u793a\u7684\u6700\u5927\u6b63\u6570\u4e3a \\(2^{254 - 127} \\times (2 - 2^{-23}) \\approx 3.4 \\times 10^{38}\\) \uff0c\u5207\u6362\u7b26\u53f7\u4f4d\u4fbf\u53ef\u5f97\u5230\u6700\u5c0f\u8d1f\u6570\u3002
\u5c3d\u7ba1\u6d6e\u70b9\u6570 float
\u6269\u5c55\u4e86\u53d6\u503c\u8303\u56f4\uff0c\u4f46\u5176\u526f\u4f5c\u7528\u662f\u727a\u7272\u4e86\u7cbe\u5ea6\u3002\u6574\u6570\u7c7b\u578b int
\u5c06\u5168\u90e8 32 \u4f4d\u7528\u4e8e\u8868\u793a\u6570\u5b57\uff0c\u6570\u5b57\u662f\u5747\u5300\u5206\u5e03\u7684\uff1b\u800c\u7531\u4e8e\u6307\u6570\u4f4d\u7684\u5b58\u5728\uff0c\u6d6e\u70b9\u6570 float
\u7684\u6570\u503c\u8d8a\u5927\uff0c\u76f8\u90bb\u4e24\u4e2a\u6570\u5b57\u4e4b\u95f4\u7684\u5dee\u503c\u5c31\u4f1a\u8d8b\u5411\u8d8a\u5927\u3002
\u5982\u8868 3-2 \u6240\u793a\uff0c\u6307\u6570\u4f4d \\(E = 0\\) \u548c \\(E = 255\\) \u5177\u6709\u7279\u6b8a\u542b\u4e49\uff0c\u7528\u4e8e\u8868\u793a\u96f6\u3001\u65e0\u7a77\u5927\u3001\\(\\mathrm{NaN}\\) \u7b49\u3002
\u8868 3-2 \u00a0 \u6307\u6570\u4f4d\u542b\u4e49
\u6307\u6570\u4f4d E \u5206\u6570\u4f4d \\(\\mathrm{N} = 0\\) \u5206\u6570\u4f4d \\(\\mathrm{N} \\ne 0\\) \u8ba1\u7b97\u516c\u5f0f \\(0\\) \\(\\pm 0\\) \u6b21\u6b63\u89c4\u6570 \\((-1)^{\\mathrm{S}} \\times 2^{-126} \\times (0.\\mathrm{N})\\) \\(1, 2, \\dots, 254\\) \u6b63\u89c4\u6570 \u6b63\u89c4\u6570 \\((-1)^{\\mathrm{S}} \\times 2^{(\\mathrm{E} -127)} \\times (1.\\mathrm{N})\\) \\(255\\) \\(\\pm \\infty\\) \\(\\mathrm{NaN}\\)\u503c\u5f97\u8bf4\u660e\u7684\u662f\uff0c\u6b21\u6b63\u89c4\u6570\u663e\u8457\u63d0\u5347\u4e86\u6d6e\u70b9\u6570\u7684\u7cbe\u5ea6\u3002\u6700\u5c0f\u6b63\u6b63\u89c4\u6570\u4e3a \\(2^{-126}\\) \uff0c\u6700\u5c0f\u6b63\u6b21\u6b63\u89c4\u6570\u4e3a \\(2^{-126} \\times 2^{-23}\\) \u3002
\u53cc\u7cbe\u5ea6 double
\u4e5f\u91c7\u7528\u7c7b\u4f3c\u4e8e float
\u7684\u8868\u793a\u65b9\u6cd5\uff0c\u5728\u6b64\u4e0d\u505a\u8d58\u8ff0\u3002
byte
\u3001short
\u3001int
\u3001long
\uff0c\u6d6e\u70b9\u6570 float
\u3001double
\uff0c\u5b57\u7b26 char
\u548c\u5e03\u5c14 boolean
\u3002\u5b83\u4eec\u7684\u53d6\u503c\u8303\u56f4\u53d6\u51b3\u4e8e\u5360\u7528\u7a7a\u95f4\u5927\u5c0f\u548c\u8868\u793a\u65b9\u5f0f\u3002\u4e3a\u4ec0\u4e48\u54c8\u5e0c\u8868\u540c\u65f6\u5305\u542b\u7ebf\u6027\u6570\u636e\u7ed3\u6784\u548c\u975e\u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff1f
\u54c8\u5e0c\u8868\u5e95\u5c42\u662f\u6570\u7ec4\uff0c\u800c\u4e3a\u4e86\u89e3\u51b3\u54c8\u5e0c\u51b2\u7a81\uff0c\u6211\u4eec\u53ef\u80fd\u4f1a\u4f7f\u7528\u201c\u94fe\u5f0f\u5730\u5740\u201d\uff08\u540e\u7eed\u54c8\u5e0c\u8868\u7ae0\u8282\u4f1a\u8bb2\uff09\uff1a\u6570\u7ec4\u4e2d\u6bcf\u4e2a\u6876\u6307\u5411\u4e00\u4e2a\u94fe\u8868\uff0c\u5f53\u94fe\u8868\u957f\u5ea6\u8d85\u8fc7\u4e00\u5b9a\u9608\u503c\u65f6\uff0c\u53c8\u53ef\u80fd\u88ab\u8f6c\u5316\u4e3a\u6811\uff08\u901a\u5e38\u4e3a\u7ea2\u9ed1\u6811\uff09\u3002 \u4ece\u5b58\u50a8\u7684\u89d2\u5ea6\u6765\u770b\uff0c\u54c8\u5e0c\u8868\u7684\u5e95\u5c42\u662f\u6570\u7ec4\uff0c\u5176\u4e2d\u6bcf\u4e00\u4e2a\u6876\u69fd\u4f4d\u53ef\u80fd\u5305\u542b\u4e00\u4e2a\u503c\uff0c\u4e5f\u53ef\u80fd\u5305\u542b\u4e00\u4e2a\u94fe\u8868\u6216\u6811\u3002\u56e0\u6b64\uff0c\u54c8\u5e0c\u8868\u53ef\u80fd\u540c\u65f6\u5305\u542b\u7ebf\u6027\uff08\u6570\u7ec4\u3001\u94fe\u8868\uff09\u548c\u975e\u7ebf\u6027\uff08\u6811\uff09\u6570\u636e\u7ed3\u6784\u3002
char
\u7c7b\u578b\u7684\u957f\u5ea6\u662f 1 byte \u5417\uff1f
char
\u7c7b\u578b\u7684\u957f\u5ea6\u7531\u7f16\u7a0b\u8bed\u8a00\u91c7\u7528\u7684\u7f16\u7801\u65b9\u6cd5\u51b3\u5b9a\u3002\u4f8b\u5982\uff0cJava\u3001JavaScript\u3001TypeScript\u3001C# \u90fd\u91c7\u7528 UTF-16 \u7f16\u7801\uff08\u4fdd\u5b58 Unicode \u7801\u70b9\uff09\uff0c\u56e0\u6b64 char \u7c7b\u578b\u7684\u957f\u5ea6\u4e3a 2 bytes \u3002
\u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6570\u636e\u7ed3\u6784\u4e5f\u88ab\u79f0\u4e3a\u201c\u9759\u6001\u6570\u636e\u7ed3\u6784\u201d \u662f\u5426\u6709\u6b67\u4e49\uff1f\u56e0\u4e3a\u6808\u4e5f\u53ef\u4ee5\u8fdb\u884c\u51fa\u6808\u548c\u5165\u6808\u7b49\u64cd\u4f5c\uff0c\u8fd9\u4e9b\u64cd\u4f5c\u90fd\u662f\u201c\u52a8\u6001\u201d\u7684\u3002
\u6808\u786e\u5b9e\u53ef\u4ee5\u5b9e\u73b0\u52a8\u6001\u7684\u6570\u636e\u64cd\u4f5c\uff0c\u4f46\u6570\u636e\u7ed3\u6784\u4ecd\u7136\u662f\u201c\u9759\u6001\u201d\uff08\u957f\u5ea6\u4e0d\u53ef\u53d8\uff09\u7684\u3002\u5c3d\u7ba1\u57fa\u4e8e\u6570\u7ec4\u7684\u6570\u636e\u7ed3\u6784\u53ef\u4ee5\u52a8\u6001\u5730\u6dfb\u52a0\u6216\u5220\u9664\u5143\u7d20\uff0c\u4f46\u5b83\u4eec\u7684\u5bb9\u91cf\u662f\u56fa\u5b9a\u7684\u3002\u5982\u679c\u6570\u636e\u91cf\u8d85\u51fa\u4e86\u9884\u5206\u914d\u7684\u5927\u5c0f\uff0c\u5c31\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u66f4\u5927\u7684\u6570\u7ec4\uff0c\u5e76\u5c06\u8001\u6570\u7ec4\u7684\u5185\u5bb9\u590d\u5236\u5230\u65b0\u6570\u7ec4\u4e2d\u3002
\u5728\u6784\u5efa\u6808\uff08\u961f\u5217\uff09\u7684\u65f6\u5019\uff0c\u672a\u6307\u5b9a\u5b83\u7684\u5927\u5c0f\uff0c\u4e3a\u4ec0\u4e48\u5b83\u4eec\u662f\u201c\u9759\u6001\u6570\u636e\u7ed3\u6784\u201d\u5462\uff1f
\u5728\u9ad8\u7ea7\u7f16\u7a0b\u8bed\u8a00\u4e2d\uff0c\u6211\u4eec\u65e0\u987b\u4eba\u5de5\u6307\u5b9a\u6808\uff08\u961f\u5217\uff09\u7684\u521d\u59cb\u5bb9\u91cf\uff0c\u8fd9\u4e2a\u5de5\u4f5c\u662f\u7531\u7c7b\u5185\u90e8\u81ea\u52a8\u5b8c\u6210\u7684\u3002\u4f8b\u5982\uff0cJava \u7684 ArrayList \u7684\u521d\u59cb\u5bb9\u91cf\u901a\u5e38\u4e3a 10 \u3002\u53e6\u5916\uff0c\u6269\u5bb9\u64cd\u4f5c\u4e5f\u662f\u81ea\u52a8\u5b9e\u73b0\u7684\u3002\u8be6\u89c1\u672c\u4e66\u7684\u201c\u5217\u8868\u201d\u7ae0\u8282\u3002
"},{"location":"chapter_divide_and_conquer/","title":"\u7b2c 12 \u7ae0 \u00a0 \u5206\u6cbb","text":"Abstract
\u96be\u9898\u88ab\u9010\u5c42\u62c6\u89e3\uff0c\u6bcf\u4e00\u6b21\u7684\u62c6\u89e3\u90fd\u4f7f\u5b83\u53d8\u5f97\u66f4\u4e3a\u7b80\u5355\u3002
\u5206\u800c\u6cbb\u4e4b\u63ed\u793a\u4e86\u4e00\u4e2a\u91cd\u8981\u7684\u4e8b\u5b9e\uff1a\u4ece\u7b80\u5355\u505a\u8d77\uff0c\u4e00\u5207\u90fd\u4e0d\u518d\u590d\u6742\u3002
"},{"location":"chapter_divide_and_conquer/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"\u6211\u4eec\u5df2\u7ecf\u5b66\u8fc7\uff0c\u641c\u7d22\u7b97\u6cd5\u5206\u4e3a\u4e24\u5927\u7c7b\u3002
\u5b9e\u9645\u4e0a\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(\\log n)\\) \u7684\u641c\u7d22\u7b97\u6cd5\u901a\u5e38\u662f\u57fa\u4e8e\u5206\u6cbb\u7b56\u7565\u5b9e\u73b0\u7684\uff0c\u4f8b\u5982\u4e8c\u5206\u67e5\u627e\u548c\u6811\u3002
\u4e8c\u5206\u67e5\u627e\u7684\u5206\u6cbb\u7b56\u7565\u5982\u4e0b\u6240\u793a\u3002
\u5206\u6cbb\u80fd\u591f\u63d0\u5347\u641c\u7d22\u6548\u7387\uff0c\u672c\u8d28\u4e0a\u662f\u56e0\u4e3a\u66b4\u529b\u641c\u7d22\u6bcf\u8f6e\u53ea\u80fd\u6392\u9664\u4e00\u4e2a\u9009\u9879\uff0c\u800c\u5206\u6cbb\u641c\u7d22\u6bcf\u8f6e\u53ef\u4ee5\u6392\u9664\u4e00\u534a\u9009\u9879\u3002
"},{"location":"chapter_divide_and_conquer/binary_search_recur/#1","title":"1. \u00a0 \u57fa\u4e8e\u5206\u6cbb\u5b9e\u73b0\u4e8c\u5206\u67e5\u627e","text":"\u5728\u4e4b\u524d\u7684\u7ae0\u8282\u4e2d\uff0c\u4e8c\u5206\u67e5\u627e\u662f\u57fa\u4e8e\u9012\u63a8\uff08\u8fed\u4ee3\uff09\u5b9e\u73b0\u7684\u3002\u73b0\u5728\u6211\u4eec\u57fa\u4e8e\u5206\u6cbb\uff08\u9012\u5f52\uff09\u6765\u5b9e\u73b0\u5b83\u3002
Question
\u7ed9\u5b9a\u4e00\u4e2a\u957f\u5ea6\u4e3a \\(n\\) \u7684\u6709\u5e8f\u6570\u7ec4 nums
\uff0c\u5176\u4e2d\u6240\u6709\u5143\u7d20\u90fd\u662f\u552f\u4e00\u7684\uff0c\u8bf7\u67e5\u627e\u5143\u7d20 target
\u3002
\u4ece\u5206\u6cbb\u89d2\u5ea6\uff0c\u6211\u4eec\u5c06\u641c\u7d22\u533a\u95f4 \\([i, j]\\) \u5bf9\u5e94\u7684\u5b50\u95ee\u9898\u8bb0\u4e3a \\(f(i, j)\\) \u3002
\u4ee5\u539f\u95ee\u9898 \\(f(0, n-1)\\) \u4e3a\u8d77\u59cb\u70b9\uff0c\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u8fdb\u884c\u4e8c\u5206\u67e5\u627e\u3002
1.
\u6b65\u548c\u7b2c 2.
\u6b65\uff0c\u76f4\u81f3\u627e\u5230 target
\u6216\u533a\u95f4\u4e3a\u7a7a\u65f6\u8fd4\u56de\u3002\u56fe 12-4 \u5c55\u793a\u4e86\u5728\u6570\u7ec4\u4e2d\u4e8c\u5206\u67e5\u627e\u5143\u7d20 \\(6\\) \u7684\u5206\u6cbb\u8fc7\u7a0b\u3002
\u56fe 12-4 \u00a0 \u4e8c\u5206\u67e5\u627e\u7684\u5206\u6cbb\u8fc7\u7a0b
\u5728\u5b9e\u73b0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u58f0\u660e\u4e00\u4e2a\u9012\u5f52\u51fd\u6570 dfs()
\u6765\u6c42\u89e3\u95ee\u9898 \\(f(i, j)\\) \uff1a
def dfs(nums: list[int], target: int, i: int, j: int) -> int:\n \"\"\"\u4e8c\u5206\u67e5\u627e\uff1a\u95ee\u9898 f(i, j)\"\"\"\n # \u82e5\u533a\u95f4\u4e3a\u7a7a\uff0c\u4ee3\u8868\u65e0\u76ee\u6807\u5143\u7d20\uff0c\u5219\u8fd4\u56de -1\n if i > j:\n return -1\n # \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n m = (i + j) // 2\n if nums[m] < target:\n # \u9012\u5f52\u5b50\u95ee\u9898 f(m+1, j)\n return dfs(nums, target, m + 1, j)\n elif nums[m] > target:\n # \u9012\u5f52\u5b50\u95ee\u9898 f(i, m-1)\n return dfs(nums, target, i, m - 1)\n else:\n # \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m\n\ndef binary_search(nums: list[int], target: int) -> int:\n \"\"\"\u4e8c\u5206\u67e5\u627e\"\"\"\n n = len(nums)\n # \u6c42\u89e3\u95ee\u9898 f(0, n-1)\n return dfs(nums, target, 0, n - 1)\n
binary_search_recur.cpp/* \u4e8c\u5206\u67e5\u627e\uff1a\u95ee\u9898 f(i, j) */\nint dfs(vector<int> &nums, int target, int i, int j) {\n // \u82e5\u533a\u95f4\u4e3a\u7a7a\uff0c\u4ee3\u8868\u65e0\u76ee\u6807\u5143\u7d20\uff0c\u5219\u8fd4\u56de -1\n if (i > j) {\n return -1;\n }\n // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n int m = (i + j) / 2;\n if (nums[m] < target) {\n // \u9012\u5f52\u5b50\u95ee\u9898 f(m+1, j)\n return dfs(nums, target, m + 1, j);\n } else if (nums[m] > target) {\n // \u9012\u5f52\u5b50\u95ee\u9898 f(i, m-1)\n return dfs(nums, target, i, m - 1);\n } else {\n // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m;\n }\n}\n\n/* \u4e8c\u5206\u67e5\u627e */\nint binarySearch(vector<int> &nums, int target) {\n int n = nums.size();\n // \u6c42\u89e3\u95ee\u9898 f(0, n-1)\n return dfs(nums, target, 0, n - 1);\n}\n
binary_search_recur.java/* \u4e8c\u5206\u67e5\u627e\uff1a\u95ee\u9898 f(i, j) */\nint dfs(int[] nums, int target, int i, int j) {\n // \u82e5\u533a\u95f4\u4e3a\u7a7a\uff0c\u4ee3\u8868\u65e0\u76ee\u6807\u5143\u7d20\uff0c\u5219\u8fd4\u56de -1\n if (i > j) {\n return -1;\n }\n // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n int m = (i + j) / 2;\n if (nums[m] < target) {\n // \u9012\u5f52\u5b50\u95ee\u9898 f(m+1, j)\n return dfs(nums, target, m + 1, j);\n } else if (nums[m] > target) {\n // \u9012\u5f52\u5b50\u95ee\u9898 f(i, m-1)\n return dfs(nums, target, i, m - 1);\n } else {\n // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m;\n }\n}\n\n/* \u4e8c\u5206\u67e5\u627e */\nint binarySearch(int[] nums, int target) {\n int n = nums.length;\n // \u6c42\u89e3\u95ee\u9898 f(0, n-1)\n return dfs(nums, target, 0, n - 1);\n}\n
binary_search_recur.cs/* \u4e8c\u5206\u67e5\u627e\uff1a\u95ee\u9898 f(i, j) */\nint DFS(int[] nums, int target, int i, int j) {\n // \u82e5\u533a\u95f4\u4e3a\u7a7a\uff0c\u4ee3\u8868\u65e0\u76ee\u6807\u5143\u7d20\uff0c\u5219\u8fd4\u56de -1\n if (i > j) {\n return -1;\n }\n // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n int m = (i + j) / 2;\n if (nums[m] < target) {\n // \u9012\u5f52\u5b50\u95ee\u9898 f(m+1, j)\n return DFS(nums, target, m + 1, j);\n } else if (nums[m] > target) {\n // \u9012\u5f52\u5b50\u95ee\u9898 f(i, m-1)\n return DFS(nums, target, i, m - 1);\n } else {\n // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m;\n }\n}\n\n/* \u4e8c\u5206\u67e5\u627e */\nint BinarySearch(int[] nums, int target) {\n int n = nums.Length;\n // \u6c42\u89e3\u95ee\u9898 f(0, n-1)\n return DFS(nums, target, 0, n - 1);\n}\n
binary_search_recur.go/* \u4e8c\u5206\u67e5\u627e\uff1a\u95ee\u9898 f(i, j) */\nfunc dfs(nums []int, target, i, j int) int {\n // \u5982\u679c\u533a\u95f4\u4e3a\u7a7a\uff0c\u4ee3\u8868\u6ca1\u6709\u76ee\u6807\u5143\u7d20\uff0c\u5219\u8fd4\u56de -1\n if i > j {\n return -1\n }\n // \u8ba1\u7b97\u7d22\u5f15\u4e2d\u70b9\n m := i + ((j - i) >> 1)\n //\u5224\u65ad\u4e2d\u70b9\u4e0e\u76ee\u6807\u5143\u7d20\u5927\u5c0f\n if nums[m] < target {\n // \u5c0f\u4e8e\u5219\u9012\u5f52\u53f3\u534a\u6570\u7ec4\n // \u9012\u5f52\u5b50\u95ee\u9898 f(m+1, j)\n return dfs(nums, target, m+1, j)\n } else if nums[m] > target {\n // \u5c0f\u4e8e\u5219\u9012\u5f52\u5de6\u534a\u6570\u7ec4\n // \u9012\u5f52\u5b50\u95ee\u9898 f(i, m-1)\n return dfs(nums, target, i, m-1)\n } else {\n // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m\n }\n}\n\n/* \u4e8c\u5206\u67e5\u627e */\nfunc binarySearch(nums []int, target int) int {\n n := len(nums)\n return dfs(nums, target, 0, n-1)\n}\n
binary_search_recur.swift/* \u4e8c\u5206\u67e5\u627e\uff1a\u95ee\u9898 f(i, j) */\nfunc dfs(nums: [Int], target: Int, i: Int, j: Int) -> Int {\n // \u82e5\u533a\u95f4\u4e3a\u7a7a\uff0c\u4ee3\u8868\u65e0\u76ee\u6807\u5143\u7d20\uff0c\u5219\u8fd4\u56de -1\n if i > j {\n return -1\n }\n // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n let m = (i + j) / 2\n if nums[m] < target {\n // \u9012\u5f52\u5b50\u95ee\u9898 f(m+1, j)\n return dfs(nums: nums, target: target, i: m + 1, j: j)\n } else if nums[m] > target {\n // \u9012\u5f52\u5b50\u95ee\u9898 f(i, m-1)\n return dfs(nums: nums, target: target, i: i, j: m - 1)\n } else {\n // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m\n }\n}\n\n/* \u4e8c\u5206\u67e5\u627e */\nfunc binarySearch(nums: [Int], target: Int) -> Int {\n let n = nums.count\n // \u6c42\u89e3\u95ee\u9898 f(0, n-1)\n return dfs(nums: nums, target: target, i: 0, j: n - 1)\n}\n
binary_search_recur.js/* \u4e8c\u5206\u67e5\u627e\uff1a\u95ee\u9898 f(i, j) */\nfunction dfs(nums, target, i, j) {\n // \u82e5\u533a\u95f4\u4e3a\u7a7a\uff0c\u4ee3\u8868\u65e0\u76ee\u6807\u5143\u7d20\uff0c\u5219\u8fd4\u56de -1\n if (i > j) {\n return -1;\n }\n // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n const m = i + ((j - i) >> 1);\n if (nums[m] < target) {\n // \u9012\u5f52\u5b50\u95ee\u9898 f(m+1, j)\n return dfs(nums, target, m + 1, j);\n } else if (nums[m] > target) {\n // \u9012\u5f52\u5b50\u95ee\u9898 f(i, m-1)\n return dfs(nums, target, i, m - 1);\n } else {\n // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m;\n }\n}\n\n/* \u4e8c\u5206\u67e5\u627e */\nfunction binarySearch(nums, target) {\n const n = nums.length;\n // \u6c42\u89e3\u95ee\u9898 f(0, n-1)\n return dfs(nums, target, 0, n - 1);\n}\n
binary_search_recur.ts/* \u4e8c\u5206\u67e5\u627e\uff1a\u95ee\u9898 f(i, j) */\nfunction dfs(nums: number[], target: number, i: number, j: number): number {\n // \u82e5\u533a\u95f4\u4e3a\u7a7a\uff0c\u4ee3\u8868\u65e0\u76ee\u6807\u5143\u7d20\uff0c\u5219\u8fd4\u56de -1\n if (i > j) {\n return -1;\n }\n // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n const m = i + ((j - i) >> 1);\n if (nums[m] < target) {\n // \u9012\u5f52\u5b50\u95ee\u9898 f(m+1, j)\n return dfs(nums, target, m + 1, j);\n } else if (nums[m] > target) {\n // \u9012\u5f52\u5b50\u95ee\u9898 f(i, m-1)\n return dfs(nums, target, i, m - 1);\n } else {\n // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m;\n }\n}\n\n/* \u4e8c\u5206\u67e5\u627e */\nfunction binarySearch(nums: number[], target: number): number {\n const n = nums.length;\n // \u6c42\u89e3\u95ee\u9898 f(0, n-1)\n return dfs(nums, target, 0, n - 1);\n}\n
binary_search_recur.dart/* \u4e8c\u5206\u67e5\u627e\uff1a\u95ee\u9898 f(i, j) */\nint dfs(List<int> nums, int target, int i, int j) {\n // \u82e5\u533a\u95f4\u4e3a\u7a7a\uff0c\u4ee3\u8868\u65e0\u76ee\u6807\u5143\u7d20\uff0c\u5219\u8fd4\u56de -1\n if (i > j) {\n return -1;\n }\n // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n int m = (i + j) ~/ 2;\n if (nums[m] < target) {\n // \u9012\u5f52\u5b50\u95ee\u9898 f(m+1, j)\n return dfs(nums, target, m + 1, j);\n } else if (nums[m] > target) {\n // \u9012\u5f52\u5b50\u95ee\u9898 f(i, m-1)\n return dfs(nums, target, i, m - 1);\n } else {\n // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m;\n }\n}\n\n/* \u4e8c\u5206\u67e5\u627e */\nint binarySearch(List<int> nums, int target) {\n int n = nums.length;\n // \u6c42\u89e3\u95ee\u9898 f(0, n-1)\n return dfs(nums, target, 0, n - 1);\n}\n
binary_search_recur.rs/* \u4e8c\u5206\u67e5\u627e\uff1a\u95ee\u9898 f(i, j) */\nfn dfs(nums: &[i32], target: i32, i: i32, j: i32) -> i32 {\n // \u82e5\u533a\u95f4\u4e3a\u7a7a\uff0c\u4ee3\u8868\u65e0\u76ee\u6807\u5143\u7d20\uff0c\u5219\u8fd4\u56de -1\n if i > j { return -1; }\n let m: i32 = (i + j) / 2;\n if nums[m as usize] < target {\n // \u9012\u5f52\u5b50\u95ee\u9898 f(m+1, j)\n return dfs(nums, target, m + 1, j);\n } else if nums[m as usize] > target {\n // \u9012\u5f52\u5b50\u95ee\u9898 f(i, m-1)\n return dfs(nums, target, i, m - 1);\n } else {\n // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m;\n }\n}\n\n/* \u4e8c\u5206\u67e5\u627e */\nfn binary_search(nums: &[i32], target: i32) -> i32 {\n let n = nums.len() as i32;\n // \u6c42\u89e3\u95ee\u9898 f(0, n-1)\n dfs(nums, target, 0, n - 1)\n}\n
binary_search_recur.c/* \u4e8c\u5206\u67e5\u627e\uff1a\u95ee\u9898 f(i, j) */\nint dfs(int nums[], int target, int i, int j) {\n // \u82e5\u533a\u95f4\u4e3a\u7a7a\uff0c\u4ee3\u8868\u65e0\u76ee\u6807\u5143\u7d20\uff0c\u5219\u8fd4\u56de -1\n if (i > j) {\n return -1;\n }\n // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n int m = (i + j) / 2;\n if (nums[m] < target) {\n // \u9012\u5f52\u5b50\u95ee\u9898 f(m+1, j)\n return dfs(nums, target, m + 1, j);\n } else if (nums[m] > target) {\n // \u9012\u5f52\u5b50\u95ee\u9898 f(i, m-1)\n return dfs(nums, target, i, m - 1);\n } else {\n // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m;\n }\n}\n\n/* \u4e8c\u5206\u67e5\u627e */\nint binarySearch(int nums[], int target, int numsSize) {\n int n = numsSize;\n // \u6c42\u89e3\u95ee\u9898 f(0, n-1)\n return dfs(nums, target, 0, n - 1);\n}\n
binary_search_recur.zig[class]{}-[func]{dfs}\n\n[class]{}-[func]{binarySearch}\n
"},{"location":"chapter_divide_and_conquer/build_binary_tree_problem/","title":"12.3 \u00a0 \u6784\u5efa\u4e8c\u53c9\u6811\u95ee\u9898","text":"Question
\u7ed9\u5b9a\u4e00\u68f5\u4e8c\u53c9\u6811\u7684\u524d\u5e8f\u904d\u5386 preorder
\u548c\u4e2d\u5e8f\u904d\u5386 inorder
\uff0c\u8bf7\u4ece\u4e2d\u6784\u5efa\u4e8c\u53c9\u6811\uff0c\u8fd4\u56de\u4e8c\u53c9\u6811\u7684\u6839\u8282\u70b9\u3002\u5047\u8bbe\u4e8c\u53c9\u6811\u4e2d\u6ca1\u6709\u503c\u91cd\u590d\u7684\u8282\u70b9\u3002
\u56fe 12-5 \u00a0 \u6784\u5efa\u4e8c\u53c9\u6811\u7684\u793a\u4f8b\u6570\u636e
"},{"location":"chapter_divide_and_conquer/build_binary_tree_problem/#1","title":"1. \u00a0 \u5224\u65ad\u662f\u5426\u4e3a\u5206\u6cbb\u95ee\u9898","text":"\u539f\u95ee\u9898\u5b9a\u4e49\u4e3a\u4ece preorder
\u548c inorder
\u6784\u5efa\u4e8c\u53c9\u6811\uff0c\u662f\u4e00\u4e2a\u5178\u578b\u7684\u5206\u6cbb\u95ee\u9898\u3002
\u6839\u636e\u4ee5\u4e0a\u5206\u6790\uff0c\u8fd9\u9053\u9898\u53ef\u4ee5\u4f7f\u7528\u5206\u6cbb\u6765\u6c42\u89e3\uff0c\u4f46\u5982\u4f55\u901a\u8fc7\u524d\u5e8f\u904d\u5386 preorder
\u548c\u4e2d\u5e8f\u904d\u5386 inorder
\u6765\u5212\u5206\u5de6\u5b50\u6811\u548c\u53f3\u5b50\u6811\u5462\uff1f
\u6839\u636e\u5b9a\u4e49\uff0cpreorder
\u548c inorder
\u90fd\u53ef\u4ee5\u5212\u5206\u4e3a\u4e09\u4e2a\u90e8\u5206\u3002
[ \u6839\u8282\u70b9 | \u5de6\u5b50\u6811 | \u53f3\u5b50\u6811 ]
\uff0c\u4f8b\u5982\u56fe 12-5 \u7684\u6811\u5bf9\u5e94 [ 3 | 9 | 2 1 7 ]
\u3002[ \u5de6\u5b50\u6811 | \u6839\u8282\u70b9 \uff5c \u53f3\u5b50\u6811 ]
\uff0c\u4f8b\u5982\u56fe 12-5 \u7684\u6811\u5bf9\u5e94 [ 9 | 3 | 1 2 7 ]
\u3002\u4ee5\u4e0a\u56fe\u6570\u636e\u4e3a\u4f8b\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u56fe 12-6 \u6240\u793a\u7684\u6b65\u9aa4\u5f97\u5230\u5212\u5206\u7ed3\u679c\u3002
inorder
\u4e2d\u7684\u7d22\u5f15\uff0c\u5229\u7528\u8be5\u7d22\u5f15\u53ef\u5c06 inorder
\u5212\u5206\u4e3a [ 9 | 3 \uff5c 1 2 7 ]
\u3002inorder
\u5212\u5206\u7ed3\u679c\uff0c\u6613\u5f97\u5de6\u5b50\u6811\u548c\u53f3\u5b50\u6811\u7684\u8282\u70b9\u6570\u91cf\u5206\u522b\u4e3a 1 \u548c 3 \uff0c\u4ece\u800c\u53ef\u5c06 preorder
\u5212\u5206\u4e3a [ 3 | 9 | 2 1 7 ]
\u3002\u56fe 12-6 \u00a0 \u5728\u524d\u5e8f\u904d\u5386\u548c\u4e2d\u5e8f\u904d\u5386\u4e2d\u5212\u5206\u5b50\u6811
"},{"location":"chapter_divide_and_conquer/build_binary_tree_problem/#3","title":"3. \u00a0 \u57fa\u4e8e\u53d8\u91cf\u63cf\u8ff0\u5b50\u6811\u533a\u95f4","text":"\u6839\u636e\u4ee5\u4e0a\u5212\u5206\u65b9\u6cd5\uff0c\u6211\u4eec\u5df2\u7ecf\u5f97\u5230\u6839\u8282\u70b9\u3001\u5de6\u5b50\u6811\u3001\u53f3\u5b50\u6811\u5728 preorder
\u548c inorder
\u4e2d\u7684\u7d22\u5f15\u533a\u95f4\u3002\u800c\u4e3a\u4e86\u63cf\u8ff0\u8fd9\u4e9b\u7d22\u5f15\u533a\u95f4\uff0c\u6211\u4eec\u9700\u8981\u501f\u52a9\u51e0\u4e2a\u6307\u9488\u53d8\u91cf\u3002
preorder
\u4e2d\u7684\u7d22\u5f15\u8bb0\u4e3a \\(i\\) \u3002inorder
\u4e2d\u7684\u7d22\u5f15\u8bb0\u4e3a \\(m\\) \u3002inorder
\u4e2d\u7684\u7d22\u5f15\u533a\u95f4\u8bb0\u4e3a \\([l, r]\\) \u3002\u5982\u8868 12-1 \u6240\u793a\uff0c\u901a\u8fc7\u4ee5\u4e0a\u53d8\u91cf\u5373\u53ef\u8868\u793a\u6839\u8282\u70b9\u5728 preorder
\u4e2d\u7684\u7d22\u5f15\uff0c\u4ee5\u53ca\u5b50\u6811\u5728 inorder
\u4e2d\u7684\u7d22\u5f15\u533a\u95f4\u3002
\u8868 12-1 \u00a0 \u6839\u8282\u70b9\u548c\u5b50\u6811\u5728\u524d\u5e8f\u904d\u5386\u548c\u4e2d\u5e8f\u904d\u5386\u4e2d\u7684\u7d22\u5f15
\u6839\u8282\u70b9\u5728preorder
\u4e2d\u7684\u7d22\u5f15 \u5b50\u6811\u5728 inorder
\u4e2d\u7684\u7d22\u5f15\u533a\u95f4 \u5f53\u524d\u6811 \\(i\\) \\([l, r]\\) \u5de6\u5b50\u6811 \\(i + 1\\) \\([l, m-1]\\) \u53f3\u5b50\u6811 \\(i + 1 + (m - l)\\) \\([m+1, r]\\) \u8bf7\u6ce8\u610f\uff0c\u53f3\u5b50\u6811\u6839\u8282\u70b9\u7d22\u5f15\u4e2d\u7684 \\((m-l)\\) \u7684\u542b\u4e49\u662f\u201c\u5de6\u5b50\u6811\u7684\u8282\u70b9\u6570\u91cf\u201d\uff0c\u5efa\u8bae\u914d\u5408\u56fe 12-7 \u7406\u89e3\u3002
\u56fe 12-7 \u00a0 \u6839\u8282\u70b9\u548c\u5de6\u53f3\u5b50\u6811\u7684\u7d22\u5f15\u533a\u95f4\u8868\u793a
"},{"location":"chapter_divide_and_conquer/build_binary_tree_problem/#4","title":"4. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"\u4e3a\u4e86\u63d0\u5347\u67e5\u8be2 \\(m\\) \u7684\u6548\u7387\uff0c\u6211\u4eec\u501f\u52a9\u4e00\u4e2a\u54c8\u5e0c\u8868 hmap
\u6765\u5b58\u50a8\u6570\u7ec4 inorder
\u4e2d\u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\uff1a
def dfs(\n preorder: list[int],\n inorder_map: dict[int, int],\n i: int,\n l: int,\n r: int,\n) -> TreeNode | None:\n \"\"\"\u6784\u5efa\u4e8c\u53c9\u6811\uff1a\u5206\u6cbb\"\"\"\n # \u5b50\u6811\u533a\u95f4\u4e3a\u7a7a\u65f6\u7ec8\u6b62\n if r - l < 0:\n return None\n # \u521d\u59cb\u5316\u6839\u8282\u70b9\n root = TreeNode(preorder[i])\n # \u67e5\u8be2 m \uff0c\u4ece\u800c\u5212\u5206\u5de6\u53f3\u5b50\u6811\n m = inorder_map[preorder[i]]\n # \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u5de6\u5b50\u6811\n root.left = dfs(preorder, inorder_map, i + 1, l, m - 1)\n # \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u53f3\u5b50\u6811\n root.right = dfs(preorder, inorder_map, i + 1 + m - l, m + 1, r)\n # \u8fd4\u56de\u6839\u8282\u70b9\n return root\n\ndef build_tree(preorder: list[int], inorder: list[int]) -> TreeNode | None:\n \"\"\"\u6784\u5efa\u4e8c\u53c9\u6811\"\"\"\n # \u521d\u59cb\u5316\u54c8\u5e0c\u8868\uff0c\u5b58\u50a8 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\n inorder_map = {val: i for i, val in enumerate(inorder)}\n root = dfs(preorder, inorder_map, 0, 0, len(inorder) - 1)\n return root\n
build_tree.cpp/* \u6784\u5efa\u4e8c\u53c9\u6811\uff1a\u5206\u6cbb */\nTreeNode *dfs(vector<int> &preorder, unordered_map<int, int> &inorderMap, int i, int l, int r) {\n // \u5b50\u6811\u533a\u95f4\u4e3a\u7a7a\u65f6\u7ec8\u6b62\n if (r - l < 0)\n return NULL;\n // \u521d\u59cb\u5316\u6839\u8282\u70b9\n TreeNode *root = new TreeNode(preorder[i]);\n // \u67e5\u8be2 m \uff0c\u4ece\u800c\u5212\u5206\u5de6\u53f3\u5b50\u6811\n int m = inorderMap[preorder[i]];\n // \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u5de6\u5b50\u6811\n root->left = dfs(preorder, inorderMap, i + 1, l, m - 1);\n // \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u53f3\u5b50\u6811\n root->right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r);\n // \u8fd4\u56de\u6839\u8282\u70b9\n return root;\n}\n\n/* \u6784\u5efa\u4e8c\u53c9\u6811 */\nTreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {\n // \u521d\u59cb\u5316\u54c8\u5e0c\u8868\uff0c\u5b58\u50a8 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\n unordered_map<int, int> inorderMap;\n for (int i = 0; i < inorder.size(); i++) {\n inorderMap[inorder[i]] = i;\n }\n TreeNode *root = dfs(preorder, inorderMap, 0, 0, inorder.size() - 1);\n return root;\n}\n
build_tree.java/* \u6784\u5efa\u4e8c\u53c9\u6811\uff1a\u5206\u6cbb */\nTreeNode dfs(int[] preorder, Map<Integer, Integer> inorderMap, int i, int l, int r) {\n // \u5b50\u6811\u533a\u95f4\u4e3a\u7a7a\u65f6\u7ec8\u6b62\n if (r - l < 0)\n return null;\n // \u521d\u59cb\u5316\u6839\u8282\u70b9\n TreeNode root = new TreeNode(preorder[i]);\n // \u67e5\u8be2 m \uff0c\u4ece\u800c\u5212\u5206\u5de6\u53f3\u5b50\u6811\n int m = inorderMap.get(preorder[i]);\n // \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u5de6\u5b50\u6811\n root.left = dfs(preorder, inorderMap, i + 1, l, m - 1);\n // \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u53f3\u5b50\u6811\n root.right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r);\n // \u8fd4\u56de\u6839\u8282\u70b9\n return root;\n}\n\n/* \u6784\u5efa\u4e8c\u53c9\u6811 */\nTreeNode buildTree(int[] preorder, int[] inorder) {\n // \u521d\u59cb\u5316\u54c8\u5e0c\u8868\uff0c\u5b58\u50a8 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\n Map<Integer, Integer> inorderMap = new HashMap<>();\n for (int i = 0; i < inorder.length; i++) {\n inorderMap.put(inorder[i], i);\n }\n TreeNode root = dfs(preorder, inorderMap, 0, 0, inorder.length - 1);\n return root;\n}\n
build_tree.cs/* \u6784\u5efa\u4e8c\u53c9\u6811\uff1a\u5206\u6cbb */\nTreeNode? DFS(int[] preorder, Dictionary<int, int> inorderMap, int i, int l, int r) {\n // \u5b50\u6811\u533a\u95f4\u4e3a\u7a7a\u65f6\u7ec8\u6b62\n if (r - l < 0)\n return null;\n // \u521d\u59cb\u5316\u6839\u8282\u70b9\n TreeNode root = new(preorder[i]);\n // \u67e5\u8be2 m \uff0c\u4ece\u800c\u5212\u5206\u5de6\u53f3\u5b50\u6811\n int m = inorderMap[preorder[i]];\n // \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u5de6\u5b50\u6811\n root.left = DFS(preorder, inorderMap, i + 1, l, m - 1);\n // \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u53f3\u5b50\u6811\n root.right = DFS(preorder, inorderMap, i + 1 + m - l, m + 1, r);\n // \u8fd4\u56de\u6839\u8282\u70b9\n return root;\n}\n\n/* \u6784\u5efa\u4e8c\u53c9\u6811 */\nTreeNode? BuildTree(int[] preorder, int[] inorder) {\n // \u521d\u59cb\u5316\u54c8\u5e0c\u8868\uff0c\u5b58\u50a8 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\n Dictionary<int, int> inorderMap = [];\n for (int i = 0; i < inorder.Length; i++) {\n inorderMap.TryAdd(inorder[i], i);\n }\n TreeNode? root = DFS(preorder, inorderMap, 0, 0, inorder.Length - 1);\n return root;\n}\n
build_tree.go/* \u6784\u5efa\u4e8c\u53c9\u6811\uff1a\u5206\u6cbb */\nfunc dfsBuildTree(preorder []int, inorderMap map[int]int, i, l, r int) *TreeNode {\n // \u5b50\u6811\u533a\u95f4\u4e3a\u7a7a\u65f6\u7ec8\u6b62\n if r-l < 0 {\n return nil\n }\n // \u521d\u59cb\u5316\u6839\u8282\u70b9\n root := NewTreeNode(preorder[i])\n // \u67e5\u8be2 m \uff0c\u4ece\u800c\u5212\u5206\u5de6\u53f3\u5b50\u6811\n m := inorderMap[preorder[i]]\n // \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u5de6\u5b50\u6811\n root.Left = dfsBuildTree(preorder, inorderMap, i+1, l, m-1)\n // \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u53f3\u5b50\u6811\n root.Right = dfsBuildTree(preorder, inorderMap, i+1+m-l, m+1, r)\n // \u8fd4\u56de\u6839\u8282\u70b9\n return root\n}\n\n/* \u6784\u5efa\u4e8c\u53c9\u6811 */\nfunc buildTree(preorder, inorder []int) *TreeNode {\n // \u521d\u59cb\u5316\u54c8\u5e0c\u8868\uff0c\u5b58\u50a8 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\n inorderMap := make(map[int]int, len(inorder))\n for i := 0; i < len(inorder); i++ {\n inorderMap[inorder[i]] = i\n }\n\n root := dfsBuildTree(preorder, inorderMap, 0, 0, len(inorder)-1)\n return root\n}\n
build_tree.swift/* \u6784\u5efa\u4e8c\u53c9\u6811\uff1a\u5206\u6cbb */\nfunc dfs(preorder: [Int], inorderMap: [Int: Int], i: Int, l: Int, r: Int) -> TreeNode? {\n // \u5b50\u6811\u533a\u95f4\u4e3a\u7a7a\u65f6\u7ec8\u6b62\n if r - l < 0 {\n return nil\n }\n // \u521d\u59cb\u5316\u6839\u8282\u70b9\n let root = TreeNode(x: preorder[i])\n // \u67e5\u8be2 m \uff0c\u4ece\u800c\u5212\u5206\u5de6\u53f3\u5b50\u6811\n let m = inorderMap[preorder[i]]!\n // \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u5de6\u5b50\u6811\n root.left = dfs(preorder: preorder, inorderMap: inorderMap, i: i + 1, l: l, r: m - 1)\n // \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u53f3\u5b50\u6811\n root.right = dfs(preorder: preorder, inorderMap: inorderMap, i: i + 1 + m - l, l: m + 1, r: r)\n // \u8fd4\u56de\u6839\u8282\u70b9\n return root\n}\n\n/* \u6784\u5efa\u4e8c\u53c9\u6811 */\nfunc buildTree(preorder: [Int], inorder: [Int]) -> TreeNode? {\n // \u521d\u59cb\u5316\u54c8\u5e0c\u8868\uff0c\u5b58\u50a8 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\n let inorderMap = inorder.enumerated().reduce(into: [:]) { $0[$1.element] = $1.offset }\n return dfs(preorder: preorder, inorderMap: inorderMap, i: 0, l: 0, r: inorder.count - 1)\n}\n
build_tree.js/* \u6784\u5efa\u4e8c\u53c9\u6811\uff1a\u5206\u6cbb */\nfunction dfs(preorder, inorderMap, i, l, r) {\n // \u5b50\u6811\u533a\u95f4\u4e3a\u7a7a\u65f6\u7ec8\u6b62\n if (r - l < 0) return null;\n // \u521d\u59cb\u5316\u6839\u8282\u70b9\n const root = new TreeNode(preorder[i]);\n // \u67e5\u8be2 m \uff0c\u4ece\u800c\u5212\u5206\u5de6\u53f3\u5b50\u6811\n const m = inorderMap.get(preorder[i]);\n // \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u5de6\u5b50\u6811\n root.left = dfs(preorder, inorderMap, i + 1, l, m - 1);\n // \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u53f3\u5b50\u6811\n root.right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r);\n // \u8fd4\u56de\u6839\u8282\u70b9\n return root;\n}\n\n/* \u6784\u5efa\u4e8c\u53c9\u6811 */\nfunction buildTree(preorder, inorder) {\n // \u521d\u59cb\u5316\u54c8\u5e0c\u8868\uff0c\u5b58\u50a8 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\n let inorderMap = new Map();\n for (let i = 0; i < inorder.length; i++) {\n inorderMap.set(inorder[i], i);\n }\n const root = dfs(preorder, inorderMap, 0, 0, inorder.length - 1);\n return root;\n}\n
build_tree.ts/* \u6784\u5efa\u4e8c\u53c9\u6811\uff1a\u5206\u6cbb */\nfunction dfs(\n preorder: number[],\n inorderMap: Map<number, number>,\n i: number,\n l: number,\n r: number\n): TreeNode | null {\n // \u5b50\u6811\u533a\u95f4\u4e3a\u7a7a\u65f6\u7ec8\u6b62\n if (r - l < 0) return null;\n // \u521d\u59cb\u5316\u6839\u8282\u70b9\n const root: TreeNode = new TreeNode(preorder[i]);\n // \u67e5\u8be2 m \uff0c\u4ece\u800c\u5212\u5206\u5de6\u53f3\u5b50\u6811\n const m = inorderMap.get(preorder[i]);\n // \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u5de6\u5b50\u6811\n root.left = dfs(preorder, inorderMap, i + 1, l, m - 1);\n // \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u53f3\u5b50\u6811\n root.right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r);\n // \u8fd4\u56de\u6839\u8282\u70b9\n return root;\n}\n\n/* \u6784\u5efa\u4e8c\u53c9\u6811 */\nfunction buildTree(preorder: number[], inorder: number[]): TreeNode | null {\n // \u521d\u59cb\u5316\u54c8\u5e0c\u8868\uff0c\u5b58\u50a8 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\n let inorderMap = new Map<number, number>();\n for (let i = 0; i < inorder.length; i++) {\n inorderMap.set(inorder[i], i);\n }\n const root = dfs(preorder, inorderMap, 0, 0, inorder.length - 1);\n return root;\n}\n
build_tree.dart/* \u6784\u5efa\u4e8c\u53c9\u6811\uff1a\u5206\u6cbb */\nTreeNode? dfs(\n List<int> preorder,\n Map<int, int> inorderMap,\n int i,\n int l,\n int r,\n) {\n // \u5b50\u6811\u533a\u95f4\u4e3a\u7a7a\u65f6\u7ec8\u6b62\n if (r - l < 0) {\n return null;\n }\n // \u521d\u59cb\u5316\u6839\u8282\u70b9\n TreeNode? root = TreeNode(preorder[i]);\n // \u67e5\u8be2 m \uff0c\u4ece\u800c\u5212\u5206\u5de6\u53f3\u5b50\u6811\n int m = inorderMap[preorder[i]]!;\n // \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u5de6\u5b50\u6811\n root.left = dfs(preorder, inorderMap, i + 1, l, m - 1);\n // \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u53f3\u5b50\u6811\n root.right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r);\n // \u8fd4\u56de\u6839\u8282\u70b9\n return root;\n}\n\n/* \u6784\u5efa\u4e8c\u53c9\u6811 */\nTreeNode? buildTree(List<int> preorder, List<int> inorder) {\n // \u521d\u59cb\u5316\u54c8\u5e0c\u8868\uff0c\u5b58\u50a8 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\n Map<int, int> inorderMap = {};\n for (int i = 0; i < inorder.length; i++) {\n inorderMap[inorder[i]] = i;\n }\n TreeNode? root = dfs(preorder, inorderMap, 0, 0, inorder.length - 1);\n return root;\n}\n
build_tree.rs/* \u6784\u5efa\u4e8c\u53c9\u6811\uff1a\u5206\u6cbb */\nfn dfs(preorder: &[i32], inorder_map: &HashMap<i32, i32>, i: i32, l: i32, r: i32) -> Option<Rc<RefCell<TreeNode>>> {\n // \u5b50\u6811\u533a\u95f4\u4e3a\u7a7a\u65f6\u7ec8\u6b62\n if r - l < 0 { return None; }\n // \u521d\u59cb\u5316\u6839\u8282\u70b9\n let root = TreeNode::new(preorder[i as usize]);\n // \u67e5\u8be2 m \uff0c\u4ece\u800c\u5212\u5206\u5de6\u53f3\u5b50\u6811\n let m = inorder_map.get(&preorder[i as usize]).unwrap();\n // \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u5de6\u5b50\u6811\n root.borrow_mut().left = dfs(preorder, inorder_map, i + 1, l, m - 1);\n // \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u53f3\u5b50\u6811\n root.borrow_mut().right = dfs(preorder, inorder_map, i + 1 + m - l, m + 1, r);\n // \u8fd4\u56de\u6839\u8282\u70b9\n Some(root)\n}\n\n/* \u6784\u5efa\u4e8c\u53c9\u6811 */\nfn build_tree(preorder: &[i32], inorder: &[i32]) -> Option<Rc<RefCell<TreeNode>>> {\n // \u521d\u59cb\u5316\u54c8\u5e0c\u8868\uff0c\u5b58\u50a8 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\n let mut inorder_map: HashMap<i32, i32> = HashMap::new();\n for i in 0..inorder.len() {\n inorder_map.insert(inorder[i], i as i32);\n }\n let root = dfs(preorder, &inorder_map, 0, 0, inorder.len() as i32 - 1);\n root\n}\n
build_tree.c/* \u6784\u5efa\u4e8c\u53c9\u6811\uff1a\u5206\u6cbb */\nTreeNode *dfs(int *preorder, int *inorderMap, int i, int l, int r, int size) {\n // \u5b50\u6811\u533a\u95f4\u4e3a\u7a7a\u65f6\u7ec8\u6b62\n if (r - l < 0)\n return NULL;\n // \u521d\u59cb\u5316\u6839\u8282\u70b9\n TreeNode *root = (TreeNode *)malloc(sizeof(TreeNode));\n root->val = preorder[i];\n root->left = NULL;\n root->right = NULL;\n // \u67e5\u8be2 m \uff0c\u4ece\u800c\u5212\u5206\u5de6\u53f3\u5b50\u6811\n int m = inorderMap[preorder[i]];\n // \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u5de6\u5b50\u6811\n root->left = dfs(preorder, inorderMap, i + 1, l, m - 1, size);\n // \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u53f3\u5b50\u6811\n root->right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r, size);\n // \u8fd4\u56de\u6839\u8282\u70b9\n return root;\n}\n\n/* \u6784\u5efa\u4e8c\u53c9\u6811 */\nTreeNode *buildTree(int *preorder, int preorderSize, int *inorder, int inorderSize) {\n // \u521d\u59cb\u5316\u54c8\u5e0c\u8868\uff0c\u5b58\u50a8 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\n int *inorderMap = (int *)malloc(sizeof(int) * MAX_SIZE);\n for (int i = 0; i < inorderSize; i++) {\n inorderMap[inorder[i]] = i;\n }\n TreeNode *root = dfs(preorder, inorderMap, 0, 0, inorderSize - 1, inorderSize);\n free(inorderMap);\n return root;\n}\n
build_tree.zig[class]{}-[func]{dfs}\n\n[class]{}-[func]{buildTree}\n
\u56fe 12-8 \u5c55\u793a\u4e86\u6784\u5efa\u4e8c\u53c9\u6811\u7684\u9012\u5f52\u8fc7\u7a0b\uff0c\u5404\u4e2a\u8282\u70b9\u662f\u5728\u5411\u4e0b\u201c\u9012\u201d\u7684\u8fc7\u7a0b\u4e2d\u5efa\u7acb\u7684\uff0c\u800c\u5404\u6761\u8fb9\uff08\u5f15\u7528\uff09\u662f\u5728\u5411\u4e0a\u201c\u5f52\u201d\u7684\u8fc7\u7a0b\u4e2d\u5efa\u7acb\u7684\u3002
<1><2><3><4><5><6><7><8><9>\u56fe 12-8 \u00a0 \u6784\u5efa\u4e8c\u53c9\u6811\u7684\u9012\u5f52\u8fc7\u7a0b
\u6bcf\u4e2a\u9012\u5f52\u51fd\u6570\u5185\u7684\u524d\u5e8f\u904d\u5386 preorder
\u548c\u4e2d\u5e8f\u904d\u5386 inorder
\u7684\u5212\u5206\u7ed3\u679c\u5982\u56fe 12-9 \u6240\u793a\u3002
\u56fe 12-9 \u00a0 \u6bcf\u4e2a\u9012\u5f52\u51fd\u6570\u4e2d\u7684\u5212\u5206\u7ed3\u679c
\u8bbe\u6811\u7684\u8282\u70b9\u6570\u91cf\u4e3a \\(n\\) \uff0c\u521d\u59cb\u5316\u6bcf\u4e00\u4e2a\u8282\u70b9\uff08\u6267\u884c\u4e00\u4e2a\u9012\u5f52\u51fd\u6570 dfs()
\uff09\u4f7f\u7528 \\(O(1)\\) \u65f6\u95f4\u3002\u56e0\u6b64\u603b\u4f53\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002
\u54c8\u5e0c\u8868\u5b58\u50a8 inorder
\u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002\u5728\u6700\u5dee\u60c5\u51b5\u4e0b\uff0c\u5373\u4e8c\u53c9\u6811\u9000\u5316\u4e3a\u94fe\u8868\u65f6\uff0c\u9012\u5f52\u6df1\u5ea6\u8fbe\u5230 \\(n\\) \uff0c\u4f7f\u7528 \\(O(n)\\) \u7684\u6808\u5e27\u7a7a\u95f4\u3002\u56e0\u6b64\u603b\u4f53\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002
\u300c\u5206\u6cbb divide and conquer\u300d\uff0c\u5168\u79f0\u5206\u800c\u6cbb\u4e4b\uff0c\u662f\u4e00\u79cd\u975e\u5e38\u91cd\u8981\u4e14\u5e38\u89c1\u7684\u7b97\u6cd5\u7b56\u7565\u3002\u5206\u6cbb\u901a\u5e38\u57fa\u4e8e\u9012\u5f52\u5b9e\u73b0\uff0c\u5305\u62ec\u201c\u5206\u201d\u548c\u201c\u6cbb\u201d\u4e24\u4e2a\u6b65\u9aa4\u3002
\u5982\u56fe 12-1 \u6240\u793a\uff0c\u201c\u5f52\u5e76\u6392\u5e8f\u201d\u662f\u5206\u6cbb\u7b56\u7565\u7684\u5178\u578b\u5e94\u7528\u4e4b\u4e00\u3002
\u56fe 12-1 \u00a0 \u5f52\u5e76\u6392\u5e8f\u7684\u5206\u6cbb\u7b56\u7565
"},{"location":"chapter_divide_and_conquer/divide_and_conquer/#1211","title":"12.1.1 \u00a0 \u5982\u4f55\u5224\u65ad\u5206\u6cbb\u95ee\u9898","text":"\u4e00\u4e2a\u95ee\u9898\u662f\u5426\u9002\u5408\u4f7f\u7528\u5206\u6cbb\u89e3\u51b3\uff0c\u901a\u5e38\u53ef\u4ee5\u53c2\u8003\u4ee5\u4e0b\u51e0\u4e2a\u5224\u65ad\u4f9d\u636e\u3002
\u663e\u7136\uff0c\u5f52\u5e76\u6392\u5e8f\u6ee1\u8db3\u4ee5\u4e0a\u4e09\u6761\u5224\u65ad\u4f9d\u636e\u3002
\u5206\u6cbb\u4e0d\u4ec5\u53ef\u4ee5\u6709\u6548\u5730\u89e3\u51b3\u7b97\u6cd5\u95ee\u9898\uff0c\u5f80\u5f80\u8fd8\u53ef\u4ee5\u63d0\u5347\u7b97\u6cd5\u6548\u7387\u3002\u5728\u6392\u5e8f\u7b97\u6cd5\u4e2d\uff0c\u5feb\u901f\u6392\u5e8f\u3001\u5f52\u5e76\u6392\u5e8f\u3001\u5806\u6392\u5e8f\u76f8\u8f83\u4e8e\u9009\u62e9\u3001\u5192\u6ce1\u3001\u63d2\u5165\u6392\u5e8f\u66f4\u5feb\uff0c\u5c31\u662f\u56e0\u4e3a\u5b83\u4eec\u5e94\u7528\u4e86\u5206\u6cbb\u7b56\u7565\u3002
\u90a3\u4e48\uff0c\u6211\u4eec\u4e0d\u7981\u53d1\u95ee\uff1a\u4e3a\u4ec0\u4e48\u5206\u6cbb\u53ef\u4ee5\u63d0\u5347\u7b97\u6cd5\u6548\u7387\uff0c\u5176\u5e95\u5c42\u903b\u8f91\u662f\u4ec0\u4e48\uff1f\u6362\u53e5\u8bdd\u8bf4\uff0c\u5c06\u5927\u95ee\u9898\u5206\u89e3\u4e3a\u591a\u4e2a\u5b50\u95ee\u9898\u3001\u89e3\u51b3\u5b50\u95ee\u9898\u3001\u5c06\u5b50\u95ee\u9898\u7684\u89e3\u5408\u5e76\u4e3a\u539f\u95ee\u9898\u7684\u89e3\uff0c\u8fd9\u51e0\u6b65\u7684\u6548\u7387\u4e3a\u4ec0\u4e48\u6bd4\u76f4\u63a5\u89e3\u51b3\u539f\u95ee\u9898\u7684\u6548\u7387\u66f4\u9ad8\uff1f\u8fd9\u4e2a\u95ee\u9898\u53ef\u4ee5\u4ece\u64cd\u4f5c\u6570\u91cf\u548c\u5e76\u884c\u8ba1\u7b97\u4e24\u65b9\u9762\u6765\u8ba8\u8bba\u3002
"},{"location":"chapter_divide_and_conquer/divide_and_conquer/#1","title":"1. \u00a0 \u64cd\u4f5c\u6570\u91cf\u4f18\u5316","text":"\u4ee5\u201c\u5192\u6ce1\u6392\u5e8f\u201d\u4e3a\u4f8b\uff0c\u5176\u5904\u7406\u4e00\u4e2a\u957f\u5ea6\u4e3a \\(n\\) \u7684\u6570\u7ec4\u9700\u8981 \\(O(n^2)\\) \u65f6\u95f4\u3002\u5047\u8bbe\u6211\u4eec\u6309\u7167\u56fe 12-2 \u6240\u793a\u7684\u65b9\u5f0f\uff0c\u5c06\u6570\u7ec4\u4ece\u4e2d\u70b9\u5904\u5206\u4e3a\u4e24\u4e2a\u5b50\u6570\u7ec4\uff0c\u5219\u5212\u5206\u9700\u8981 \\(O(n)\\) \u65f6\u95f4\uff0c\u6392\u5e8f\u6bcf\u4e2a\u5b50\u6570\u7ec4\u9700\u8981 \\(O((n / 2)^2)\\) \u65f6\u95f4\uff0c\u5408\u5e76\u4e24\u4e2a\u5b50\u6570\u7ec4\u9700\u8981 \\(O(n)\\) \u65f6\u95f4\uff0c\u603b\u4f53\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a\uff1a
\\[ O(n + (\\frac{n}{2})^2 \\times 2 + n) = O(\\frac{n^2}{2} + 2n) \\]\u56fe 12-2 \u00a0 \u5212\u5206\u6570\u7ec4\u524d\u540e\u7684\u5192\u6ce1\u6392\u5e8f
\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u8ba1\u7b97\u4ee5\u4e0b\u4e0d\u7b49\u5f0f\uff0c\u5176\u5de6\u8fb9\u548c\u53f3\u8fb9\u5206\u522b\u4e3a\u5212\u5206\u524d\u548c\u5212\u5206\u540e\u7684\u64cd\u4f5c\u603b\u6570\uff1a
\\[ \\begin{aligned} n^2 & > \\frac{n^2}{2} + 2n \\newline n^2 - \\frac{n^2}{2} - 2n & > 0 \\newline n(n - 4) & > 0 \\end{aligned} \\]\u8fd9\u610f\u5473\u7740\u5f53 \\(n > 4\\) \u65f6\uff0c\u5212\u5206\u540e\u7684\u64cd\u4f5c\u6570\u91cf\u66f4\u5c11\uff0c\u6392\u5e8f\u6548\u7387\u5e94\u8be5\u66f4\u9ad8\u3002\u8bf7\u6ce8\u610f\uff0c\u5212\u5206\u540e\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4ecd\u7136\u662f\u5e73\u65b9\u9636 \\(O(n^2)\\) \uff0c\u53ea\u662f\u590d\u6742\u5ea6\u4e2d\u7684\u5e38\u6570\u9879\u53d8\u5c0f\u4e86\u3002
\u8fdb\u4e00\u6b65\u60f3\uff0c\u5982\u679c\u6211\u4eec\u628a\u5b50\u6570\u7ec4\u4e0d\u65ad\u5730\u518d\u4ece\u4e2d\u70b9\u5904\u5212\u5206\u4e3a\u4e24\u4e2a\u5b50\u6570\u7ec4\uff0c\u76f4\u81f3\u5b50\u6570\u7ec4\u53ea\u5269\u4e00\u4e2a\u5143\u7d20\u65f6\u505c\u6b62\u5212\u5206\u5462\uff1f\u8fd9\u79cd\u601d\u8def\u5b9e\u9645\u4e0a\u5c31\u662f\u201c\u5f52\u5e76\u6392\u5e8f\u201d\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n \\log n)\\) \u3002
\u518d\u601d\u8003\uff0c\u5982\u679c\u6211\u4eec\u591a\u8bbe\u7f6e\u51e0\u4e2a\u5212\u5206\u70b9\uff0c\u5c06\u539f\u6570\u7ec4\u5e73\u5747\u5212\u5206\u4e3a \\(k\\) \u4e2a\u5b50\u6570\u7ec4\u5462\uff1f\u8fd9\u79cd\u60c5\u51b5\u4e0e\u201c\u6876\u6392\u5e8f\u201d\u975e\u5e38\u7c7b\u4f3c\uff0c\u5b83\u975e\u5e38\u9002\u5408\u6392\u5e8f\u6d77\u91cf\u6570\u636e\uff0c\u7406\u8bba\u4e0a\u65f6\u95f4\u590d\u6742\u5ea6\u53ef\u4ee5\u8fbe\u5230 \\(O(n + k)\\) \u3002
"},{"location":"chapter_divide_and_conquer/divide_and_conquer/#2","title":"2. \u00a0 \u5e76\u884c\u8ba1\u7b97\u4f18\u5316","text":"\u6211\u4eec\u77e5\u9053\uff0c\u5206\u6cbb\u751f\u6210\u7684\u5b50\u95ee\u9898\u662f\u76f8\u4e92\u72ec\u7acb\u7684\uff0c\u56e0\u6b64\u901a\u5e38\u53ef\u4ee5\u5e76\u884c\u89e3\u51b3\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5206\u6cbb\u4e0d\u4ec5\u53ef\u4ee5\u964d\u4f4e\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff0c\u8fd8\u6709\u5229\u4e8e\u64cd\u4f5c\u7cfb\u7edf\u7684\u5e76\u884c\u4f18\u5316\u3002
\u5e76\u884c\u4f18\u5316\u5728\u591a\u6838\u6216\u591a\u5904\u7406\u5668\u7684\u73af\u5883\u4e2d\u5c24\u5176\u6709\u6548\uff0c\u56e0\u4e3a\u7cfb\u7edf\u53ef\u4ee5\u540c\u65f6\u5904\u7406\u591a\u4e2a\u5b50\u95ee\u9898\uff0c\u66f4\u52a0\u5145\u5206\u5730\u5229\u7528\u8ba1\u7b97\u8d44\u6e90\uff0c\u4ece\u800c\u663e\u8457\u51cf\u5c11\u603b\u4f53\u7684\u8fd0\u884c\u65f6\u95f4\u3002
\u6bd4\u5982\u5728\u56fe 12-3 \u6240\u793a\u7684\u201c\u6876\u6392\u5e8f\u201d\u4e2d\uff0c\u6211\u4eec\u5c06\u6d77\u91cf\u7684\u6570\u636e\u5e73\u5747\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\uff0c\u5219\u53ef\u6240\u6709\u6876\u7684\u6392\u5e8f\u4efb\u52a1\u5206\u6563\u5230\u5404\u4e2a\u8ba1\u7b97\u5355\u5143\uff0c\u5b8c\u6210\u540e\u518d\u5408\u5e76\u7ed3\u679c\u3002
\u56fe 12-3 \u00a0 \u6876\u6392\u5e8f\u7684\u5e76\u884c\u8ba1\u7b97
"},{"location":"chapter_divide_and_conquer/divide_and_conquer/#1213","title":"12.1.3 \u00a0 \u5206\u6cbb\u5e38\u89c1\u5e94\u7528","text":"\u4e00\u65b9\u9762\uff0c\u5206\u6cbb\u53ef\u4ee5\u7528\u6765\u89e3\u51b3\u8bb8\u591a\u7ecf\u5178\u7b97\u6cd5\u95ee\u9898\u3002
\u53e6\u4e00\u65b9\u9762\uff0c\u5206\u6cbb\u5728\u7b97\u6cd5\u548c\u6570\u636e\u7ed3\u6784\u7684\u8bbe\u8ba1\u4e2d\u5e94\u7528\u975e\u5e38\u5e7f\u6cdb\u3002
\u53ef\u4ee5\u770b\u51fa\uff0c\u5206\u6cbb\u662f\u4e00\u79cd\u201c\u6da6\u7269\u7ec6\u65e0\u58f0\u201d\u7684\u7b97\u6cd5\u601d\u60f3\uff0c\u9690\u542b\u5728\u5404\u79cd\u7b97\u6cd5\u4e0e\u6570\u636e\u7ed3\u6784\u4e4b\u4e2d\u3002
"},{"location":"chapter_divide_and_conquer/hanota_problem/","title":"12.4 \u00a0 \u6c49\u8bfa\u5854\u95ee\u9898","text":"\u5728\u5f52\u5e76\u6392\u5e8f\u548c\u6784\u5efa\u4e8c\u53c9\u6811\u4e2d\uff0c\u6211\u4eec\u90fd\u662f\u5c06\u539f\u95ee\u9898\u5206\u89e3\u4e3a\u4e24\u4e2a\u89c4\u6a21\u4e3a\u539f\u95ee\u9898\u4e00\u534a\u7684\u5b50\u95ee\u9898\u3002\u7136\u800c\u5bf9\u4e8e\u6c49\u8bfa\u5854\u95ee\u9898\uff0c\u6211\u4eec\u91c7\u7528\u4e0d\u540c\u7684\u5206\u89e3\u7b56\u7565\u3002
Question
\u7ed9\u5b9a\u4e09\u6839\u67f1\u5b50\uff0c\u8bb0\u4e3a A
\u3001B
\u548c C
\u3002\u8d77\u59cb\u72b6\u6001\u4e0b\uff0c\u67f1\u5b50 A
\u4e0a\u5957\u7740 \\(n\\) \u4e2a\u5706\u76d8\uff0c\u5b83\u4eec\u4ece\u4e0a\u5230\u4e0b\u6309\u7167\u4ece\u5c0f\u5230\u5927\u7684\u987a\u5e8f\u6392\u5217\u3002\u6211\u4eec\u7684\u4efb\u52a1\u662f\u8981\u628a\u8fd9 \\(n\\) \u4e2a\u5706\u76d8\u79fb\u5230\u67f1\u5b50 C
\u4e0a\uff0c\u5e76\u4fdd\u6301\u5b83\u4eec\u7684\u539f\u6709\u987a\u5e8f\u4e0d\u53d8\uff08\u5982\u56fe 12-10 \u6240\u793a\uff09\u3002\u5728\u79fb\u52a8\u5706\u76d8\u7684\u8fc7\u7a0b\u4e2d\uff0c\u9700\u8981\u9075\u5b88\u4ee5\u4e0b\u89c4\u5219\u3002
\u56fe 12-10 \u00a0 \u6c49\u8bfa\u5854\u95ee\u9898\u793a\u4f8b
\u6211\u4eec\u5c06\u89c4\u6a21\u4e3a \\(i\\) \u7684\u6c49\u8bfa\u5854\u95ee\u9898\u8bb0\u4f5c \\(f(i)\\) \u3002\u4f8b\u5982 \\(f(3)\\) \u4ee3\u8868\u5c06 \\(3\\) \u4e2a\u5706\u76d8\u4ece A
\u79fb\u52a8\u81f3 C
\u7684\u6c49\u8bfa\u5854\u95ee\u9898\u3002
\u5982\u56fe 12-11 \u6240\u793a\uff0c\u5bf9\u4e8e\u95ee\u9898 \\(f(1)\\) \uff0c\u5373\u5f53\u53ea\u6709\u4e00\u4e2a\u5706\u76d8\u65f6\uff0c\u6211\u4eec\u5c06\u5b83\u76f4\u63a5\u4ece A
\u79fb\u52a8\u81f3 C
\u5373\u53ef\u3002
\u56fe 12-11 \u00a0 \u89c4\u6a21\u4e3a 1 \u95ee\u9898\u7684\u89e3
\u5982\u56fe 12-12 \u6240\u793a\uff0c\u5bf9\u4e8e\u95ee\u9898 \\(f(2)\\) \uff0c\u5373\u5f53\u6709\u4e24\u4e2a\u5706\u76d8\u65f6\uff0c\u7531\u4e8e\u8981\u65f6\u523b\u6ee1\u8db3\u5c0f\u5706\u76d8\u5728\u5927\u5706\u76d8\u4e4b\u4e0a\uff0c\u56e0\u6b64\u9700\u8981\u501f\u52a9 B
\u6765\u5b8c\u6210\u79fb\u52a8\u3002
A
\u79fb\u81f3 B
\u3002A
\u79fb\u81f3 C
\u3002B
\u79fb\u81f3 C
\u3002\u56fe 12-12 \u00a0 \u89c4\u6a21\u4e3a 2 \u95ee\u9898\u7684\u89e3
\u89e3\u51b3\u95ee\u9898 \\(f(2)\\) \u7684\u8fc7\u7a0b\u53ef\u603b\u7ed3\u4e3a\uff1a\u5c06\u4e24\u4e2a\u5706\u76d8\u501f\u52a9 B
\u4ece A
\u79fb\u81f3 C
\u3002\u5176\u4e2d\uff0cC
\u79f0\u4e3a\u76ee\u6807\u67f1\u3001B
\u79f0\u4e3a\u7f13\u51b2\u67f1\u3002
\u5bf9\u4e8e\u95ee\u9898 \\(f(3)\\) \uff0c\u5373\u5f53\u6709\u4e09\u4e2a\u5706\u76d8\u65f6\uff0c\u60c5\u51b5\u53d8\u5f97\u7a0d\u5fae\u590d\u6742\u4e86\u4e00\u4e9b\u3002
\u56e0\u4e3a\u5df2\u77e5 \\(f(1)\\) \u548c \\(f(2)\\) \u7684\u89e3\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ece\u5206\u6cbb\u89d2\u5ea6\u601d\u8003\uff0c\u5c06 A
\u9876\u90e8\u7684\u4e24\u4e2a\u5706\u76d8\u770b\u4f5c\u4e00\u4e2a\u6574\u4f53\uff0c\u6267\u884c\u56fe 12-13 \u6240\u793a\u7684\u6b65\u9aa4\u3002\u8fd9\u6837\u4e09\u4e2a\u5706\u76d8\u5c31\u88ab\u987a\u5229\u5730\u4ece A
\u79fb\u81f3 C
\u4e86\u3002
B
\u4e3a\u76ee\u6807\u67f1\u3001C
\u4e3a\u7f13\u51b2\u67f1\uff0c\u5c06\u4e24\u4e2a\u5706\u76d8\u4ece A
\u79fb\u81f3 B
\u3002A
\u4e2d\u5269\u4f59\u7684\u4e00\u4e2a\u5706\u76d8\u4ece A
\u76f4\u63a5\u79fb\u52a8\u81f3 C
\u3002C
\u4e3a\u76ee\u6807\u67f1\u3001A
\u4e3a\u7f13\u51b2\u67f1\uff0c\u5c06\u4e24\u4e2a\u5706\u76d8\u4ece B
\u79fb\u81f3 C
\u3002\u56fe 12-13 \u00a0 \u89c4\u6a21\u4e3a 3 \u95ee\u9898\u7684\u89e3
\u4ece\u672c\u8d28\u4e0a\u770b\uff0c\u6211\u4eec\u5c06\u95ee\u9898 \\(f(3)\\) \u5212\u5206\u4e3a\u4e24\u4e2a\u5b50\u95ee\u9898 \\(f(2)\\) \u548c\u5b50\u95ee\u9898 \\(f(1)\\) \u3002\u6309\u987a\u5e8f\u89e3\u51b3\u8fd9\u4e09\u4e2a\u5b50\u95ee\u9898\u4e4b\u540e\uff0c\u539f\u95ee\u9898\u968f\u4e4b\u5f97\u5230\u89e3\u51b3\u3002\u8fd9\u8bf4\u660e\u5b50\u95ee\u9898\u662f\u72ec\u7acb\u7684\uff0c\u800c\u4e14\u89e3\u53ef\u4ee5\u5408\u5e76\u3002
\u81f3\u6b64\uff0c\u6211\u4eec\u53ef\u603b\u7ed3\u51fa\u56fe 12-14 \u6240\u793a\u7684\u89e3\u51b3\u6c49\u8bfa\u5854\u95ee\u9898\u7684\u5206\u6cbb\u7b56\u7565\uff1a\u5c06\u539f\u95ee\u9898 \\(f(n)\\) \u5212\u5206\u4e3a\u4e24\u4e2a\u5b50\u95ee\u9898 \\(f(n-1)\\) \u548c\u4e00\u4e2a\u5b50\u95ee\u9898 \\(f(1)\\) \uff0c\u5e76\u6309\u7167\u4ee5\u4e0b\u987a\u5e8f\u89e3\u51b3\u8fd9\u4e09\u4e2a\u5b50\u95ee\u9898\u3002
C
\u4ece A
\u79fb\u81f3 B
\u3002A
\u76f4\u63a5\u79fb\u81f3 C
\u3002A
\u4ece B
\u79fb\u81f3 C
\u3002\u5bf9\u4e8e\u8fd9\u4e24\u4e2a\u5b50\u95ee\u9898 \\(f(n-1)\\) \uff0c\u53ef\u4ee5\u901a\u8fc7\u76f8\u540c\u7684\u65b9\u5f0f\u8fdb\u884c\u9012\u5f52\u5212\u5206\uff0c\u76f4\u81f3\u8fbe\u5230\u6700\u5c0f\u5b50\u95ee\u9898 \\(f(1)\\) \u3002\u800c \\(f(1)\\) \u7684\u89e3\u662f\u5df2\u77e5\u7684\uff0c\u53ea\u9700\u4e00\u6b21\u79fb\u52a8\u64cd\u4f5c\u5373\u53ef\u3002
\u56fe 12-14 \u00a0 \u89e3\u51b3\u6c49\u8bfa\u5854\u95ee\u9898\u7684\u5206\u6cbb\u7b56\u7565
"},{"location":"chapter_divide_and_conquer/hanota_problem/#3","title":"3. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"\u5728\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u58f0\u660e\u4e00\u4e2a\u9012\u5f52\u51fd\u6570 dfs(i, src, buf, tar)
\uff0c\u5b83\u7684\u4f5c\u7528\u662f\u5c06\u67f1 src
\u9876\u90e8\u7684 \\(i\\) \u4e2a\u5706\u76d8\u501f\u52a9\u7f13\u51b2\u67f1 buf
\u79fb\u52a8\u81f3\u76ee\u6807\u67f1 tar
\uff1a
def move(src: list[int], tar: list[int]):\n \"\"\"\u79fb\u52a8\u4e00\u4e2a\u5706\u76d8\"\"\"\n # \u4ece src \u9876\u90e8\u62ff\u51fa\u4e00\u4e2a\u5706\u76d8\n pan = src.pop()\n # \u5c06\u5706\u76d8\u653e\u5165 tar \u9876\u90e8\n tar.append(pan)\n\ndef dfs(i: int, src: list[int], buf: list[int], tar: list[int]):\n \"\"\"\u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 f(i)\"\"\"\n # \u82e5 src \u53ea\u5269\u4e0b\u4e00\u4e2a\u5706\u76d8\uff0c\u5219\u76f4\u63a5\u5c06\u5176\u79fb\u5230 tar\n if i == 1:\n move(src, tar)\n return\n # \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 src \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 tar \u79fb\u5230 buf\n dfs(i - 1, src, tar, buf)\n # \u5b50\u95ee\u9898 f(1) \uff1a\u5c06 src \u5269\u4f59\u4e00\u4e2a\u5706\u76d8\u79fb\u5230 tar\n move(src, tar)\n # \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 buf \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 src \u79fb\u5230 tar\n dfs(i - 1, buf, src, tar)\n\ndef solve_hanota(A: list[int], B: list[int], C: list[int]):\n \"\"\"\u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898\"\"\"\n n = len(A)\n # \u5c06 A \u9876\u90e8 n \u4e2a\u5706\u76d8\u501f\u52a9 B \u79fb\u5230 C\n dfs(n, A, B, C)\n
hanota.cpp/* \u79fb\u52a8\u4e00\u4e2a\u5706\u76d8 */\nvoid move(vector<int> &src, vector<int> &tar) {\n // \u4ece src \u9876\u90e8\u62ff\u51fa\u4e00\u4e2a\u5706\u76d8\n int pan = src.back();\n src.pop_back();\n // \u5c06\u5706\u76d8\u653e\u5165 tar \u9876\u90e8\n tar.push_back(pan);\n}\n\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 f(i) */\nvoid dfs(int i, vector<int> &src, vector<int> &buf, vector<int> &tar) {\n // \u82e5 src \u53ea\u5269\u4e0b\u4e00\u4e2a\u5706\u76d8\uff0c\u5219\u76f4\u63a5\u5c06\u5176\u79fb\u5230 tar\n if (i == 1) {\n move(src, tar);\n return;\n }\n // \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 src \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 tar \u79fb\u5230 buf\n dfs(i - 1, src, tar, buf);\n // \u5b50\u95ee\u9898 f(1) \uff1a\u5c06 src \u5269\u4f59\u4e00\u4e2a\u5706\u76d8\u79fb\u5230 tar\n move(src, tar);\n // \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 buf \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 src \u79fb\u5230 tar\n dfs(i - 1, buf, src, tar);\n}\n\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 */\nvoid solveHanota(vector<int> &A, vector<int> &B, vector<int> &C) {\n int n = A.size();\n // \u5c06 A \u9876\u90e8 n \u4e2a\u5706\u76d8\u501f\u52a9 B \u79fb\u5230 C\n dfs(n, A, B, C);\n}\n
hanota.java/* \u79fb\u52a8\u4e00\u4e2a\u5706\u76d8 */\nvoid move(List<Integer> src, List<Integer> tar) {\n // \u4ece src \u9876\u90e8\u62ff\u51fa\u4e00\u4e2a\u5706\u76d8\n Integer pan = src.remove(src.size() - 1);\n // \u5c06\u5706\u76d8\u653e\u5165 tar \u9876\u90e8\n tar.add(pan);\n}\n\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 f(i) */\nvoid dfs(int i, List<Integer> src, List<Integer> buf, List<Integer> tar) {\n // \u82e5 src \u53ea\u5269\u4e0b\u4e00\u4e2a\u5706\u76d8\uff0c\u5219\u76f4\u63a5\u5c06\u5176\u79fb\u5230 tar\n if (i == 1) {\n move(src, tar);\n return;\n }\n // \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 src \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 tar \u79fb\u5230 buf\n dfs(i - 1, src, tar, buf);\n // \u5b50\u95ee\u9898 f(1) \uff1a\u5c06 src \u5269\u4f59\u4e00\u4e2a\u5706\u76d8\u79fb\u5230 tar\n move(src, tar);\n // \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 buf \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 src \u79fb\u5230 tar\n dfs(i - 1, buf, src, tar);\n}\n\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 */\nvoid solveHanota(List<Integer> A, List<Integer> B, List<Integer> C) {\n int n = A.size();\n // \u5c06 A \u9876\u90e8 n \u4e2a\u5706\u76d8\u501f\u52a9 B \u79fb\u5230 C\n dfs(n, A, B, C);\n}\n
hanota.cs/* \u79fb\u52a8\u4e00\u4e2a\u5706\u76d8 */\nvoid Move(List<int> src, List<int> tar) {\n // \u4ece src \u9876\u90e8\u62ff\u51fa\u4e00\u4e2a\u5706\u76d8\n int pan = src[^1];\n src.RemoveAt(src.Count - 1);\n // \u5c06\u5706\u76d8\u653e\u5165 tar \u9876\u90e8\n tar.Add(pan);\n}\n\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 f(i) */\nvoid DFS(int i, List<int> src, List<int> buf, List<int> tar) {\n // \u82e5 src \u53ea\u5269\u4e0b\u4e00\u4e2a\u5706\u76d8\uff0c\u5219\u76f4\u63a5\u5c06\u5176\u79fb\u5230 tar\n if (i == 1) {\n Move(src, tar);\n return;\n }\n // \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 src \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 tar \u79fb\u5230 buf\n DFS(i - 1, src, tar, buf);\n // \u5b50\u95ee\u9898 f(1) \uff1a\u5c06 src \u5269\u4f59\u4e00\u4e2a\u5706\u76d8\u79fb\u5230 tar\n Move(src, tar);\n // \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 buf \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 src \u79fb\u5230 tar\n DFS(i - 1, buf, src, tar);\n}\n\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 */\nvoid SolveHanota(List<int> A, List<int> B, List<int> C) {\n int n = A.Count;\n // \u5c06 A \u9876\u90e8 n \u4e2a\u5706\u76d8\u501f\u52a9 B \u79fb\u5230 C\n DFS(n, A, B, C);\n}\n
hanota.go/* \u79fb\u52a8\u4e00\u4e2a\u5706\u76d8 */\nfunc move(src, tar *list.List) {\n // \u4ece src \u9876\u90e8\u62ff\u51fa\u4e00\u4e2a\u5706\u76d8\n pan := src.Back()\n // \u5c06\u5706\u76d8\u653e\u5165 tar \u9876\u90e8\n tar.PushBack(pan.Value)\n // \u79fb\u9664 src \u9876\u90e8\u5706\u76d8\n src.Remove(pan)\n}\n\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 f(i) */\nfunc dfsHanota(i int, src, buf, tar *list.List) {\n // \u82e5 src \u53ea\u5269\u4e0b\u4e00\u4e2a\u5706\u76d8\uff0c\u5219\u76f4\u63a5\u5c06\u5176\u79fb\u5230 tar\n if i == 1 {\n move(src, tar)\n return\n }\n // \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 src \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 tar \u79fb\u5230 buf\n dfsHanota(i-1, src, tar, buf)\n // \u5b50\u95ee\u9898 f(1) \uff1a\u5c06 src \u5269\u4f59\u4e00\u4e2a\u5706\u76d8\u79fb\u5230 tar\n move(src, tar)\n // \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 buf \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 src \u79fb\u5230 tar\n dfsHanota(i-1, buf, src, tar)\n}\n\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 */\nfunc solveHanota(A, B, C *list.List) {\n n := A.Len()\n // \u5c06 A \u9876\u90e8 n \u4e2a\u5706\u76d8\u501f\u52a9 B \u79fb\u5230 C\n dfsHanota(n, A, B, C)\n}\n
hanota.swift/* \u79fb\u52a8\u4e00\u4e2a\u5706\u76d8 */\nfunc move(src: inout [Int], tar: inout [Int]) {\n // \u4ece src \u9876\u90e8\u62ff\u51fa\u4e00\u4e2a\u5706\u76d8\n let pan = src.popLast()!\n // \u5c06\u5706\u76d8\u653e\u5165 tar \u9876\u90e8\n tar.append(pan)\n}\n\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 f(i) */\nfunc dfs(i: Int, src: inout [Int], buf: inout [Int], tar: inout [Int]) {\n // \u82e5 src \u53ea\u5269\u4e0b\u4e00\u4e2a\u5706\u76d8\uff0c\u5219\u76f4\u63a5\u5c06\u5176\u79fb\u5230 tar\n if i == 1 {\n move(src: &src, tar: &tar)\n return\n }\n // \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 src \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 tar \u79fb\u5230 buf\n dfs(i: i - 1, src: &src, buf: &tar, tar: &buf)\n // \u5b50\u95ee\u9898 f(1) \uff1a\u5c06 src \u5269\u4f59\u4e00\u4e2a\u5706\u76d8\u79fb\u5230 tar\n move(src: &src, tar: &tar)\n // \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 buf \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 src \u79fb\u5230 tar\n dfs(i: i - 1, src: &buf, buf: &src, tar: &tar)\n}\n\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 */\nfunc solveHanota(A: inout [Int], B: inout [Int], C: inout [Int]) {\n let n = A.count\n // \u5217\u8868\u5c3e\u90e8\u662f\u67f1\u5b50\u9876\u90e8\n // \u5c06 src \u9876\u90e8 n \u4e2a\u5706\u76d8\u501f\u52a9 B \u79fb\u5230 C\n dfs(i: n, src: &A, buf: &B, tar: &C)\n}\n
hanota.js/* \u79fb\u52a8\u4e00\u4e2a\u5706\u76d8 */\nfunction move(src, tar) {\n // \u4ece src \u9876\u90e8\u62ff\u51fa\u4e00\u4e2a\u5706\u76d8\n const pan = src.pop();\n // \u5c06\u5706\u76d8\u653e\u5165 tar \u9876\u90e8\n tar.push(pan);\n}\n\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 f(i) */\nfunction dfs(i, src, buf, tar) {\n // \u82e5 src \u53ea\u5269\u4e0b\u4e00\u4e2a\u5706\u76d8\uff0c\u5219\u76f4\u63a5\u5c06\u5176\u79fb\u5230 tar\n if (i === 1) {\n move(src, tar);\n return;\n }\n // \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 src \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 tar \u79fb\u5230 buf\n dfs(i - 1, src, tar, buf);\n // \u5b50\u95ee\u9898 f(1) \uff1a\u5c06 src \u5269\u4f59\u4e00\u4e2a\u5706\u76d8\u79fb\u5230 tar\n move(src, tar);\n // \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 buf \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 src \u79fb\u5230 tar\n dfs(i - 1, buf, src, tar);\n}\n\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 */\nfunction solveHanota(A, B, C) {\n const n = A.length;\n // \u5c06 A \u9876\u90e8 n \u4e2a\u5706\u76d8\u501f\u52a9 B \u79fb\u5230 C\n dfs(n, A, B, C);\n}\n
hanota.ts/* \u79fb\u52a8\u4e00\u4e2a\u5706\u76d8 */\nfunction move(src: number[], tar: number[]): void {\n // \u4ece src \u9876\u90e8\u62ff\u51fa\u4e00\u4e2a\u5706\u76d8\n const pan = src.pop();\n // \u5c06\u5706\u76d8\u653e\u5165 tar \u9876\u90e8\n tar.push(pan);\n}\n\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 f(i) */\nfunction dfs(i: number, src: number[], buf: number[], tar: number[]): void {\n // \u82e5 src \u53ea\u5269\u4e0b\u4e00\u4e2a\u5706\u76d8\uff0c\u5219\u76f4\u63a5\u5c06\u5176\u79fb\u5230 tar\n if (i === 1) {\n move(src, tar);\n return;\n }\n // \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 src \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 tar \u79fb\u5230 buf\n dfs(i - 1, src, tar, buf);\n // \u5b50\u95ee\u9898 f(1) \uff1a\u5c06 src \u5269\u4f59\u4e00\u4e2a\u5706\u76d8\u79fb\u5230 tar\n move(src, tar);\n // \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 buf \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 src \u79fb\u5230 tar\n dfs(i - 1, buf, src, tar);\n}\n\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 */\nfunction solveHanota(A: number[], B: number[], C: number[]): void {\n const n = A.length;\n // \u5c06 A \u9876\u90e8 n \u4e2a\u5706\u76d8\u501f\u52a9 B \u79fb\u5230 C\n dfs(n, A, B, C);\n}\n
hanota.dart/* \u79fb\u52a8\u4e00\u4e2a\u5706\u76d8 */\nvoid move(List<int> src, List<int> tar) {\n // \u4ece src \u9876\u90e8\u62ff\u51fa\u4e00\u4e2a\u5706\u76d8\n int pan = src.removeLast();\n // \u5c06\u5706\u76d8\u653e\u5165 tar \u9876\u90e8\n tar.add(pan);\n}\n\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 f(i) */\nvoid dfs(int i, List<int> src, List<int> buf, List<int> tar) {\n // \u82e5 src \u53ea\u5269\u4e0b\u4e00\u4e2a\u5706\u76d8\uff0c\u5219\u76f4\u63a5\u5c06\u5176\u79fb\u5230 tar\n if (i == 1) {\n move(src, tar);\n return;\n }\n // \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 src \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 tar \u79fb\u5230 buf\n dfs(i - 1, src, tar, buf);\n // \u5b50\u95ee\u9898 f(1) \uff1a\u5c06 src \u5269\u4f59\u4e00\u4e2a\u5706\u76d8\u79fb\u5230 tar\n move(src, tar);\n // \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 buf \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 src \u79fb\u5230 tar\n dfs(i - 1, buf, src, tar);\n}\n\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 */\nvoid solveHanota(List<int> A, List<int> B, List<int> C) {\n int n = A.length;\n // \u5c06 A \u9876\u90e8 n \u4e2a\u5706\u76d8\u501f\u52a9 B \u79fb\u5230 C\n dfs(n, A, B, C);\n}\n
hanota.rs/* \u79fb\u52a8\u4e00\u4e2a\u5706\u76d8 */\nfn move_pan(src: &mut Vec<i32>, tar: &mut Vec<i32>) {\n // \u4ece src \u9876\u90e8\u62ff\u51fa\u4e00\u4e2a\u5706\u76d8\n let pan = src.remove(src.len() - 1);\n // \u5c06\u5706\u76d8\u653e\u5165 tar \u9876\u90e8\n tar.push(pan);\n}\n\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 f(i) */\nfn dfs(i: i32, src: &mut Vec<i32>, buf: &mut Vec<i32>, tar: &mut Vec<i32>) {\n // \u82e5 src \u53ea\u5269\u4e0b\u4e00\u4e2a\u5706\u76d8\uff0c\u5219\u76f4\u63a5\u5c06\u5176\u79fb\u5230 tar\n if i == 1 {\n move_pan(src, tar);\n return;\n }\n // \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 src \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 tar \u79fb\u5230 buf\n dfs(i - 1, src, tar, buf);\n // \u5b50\u95ee\u9898 f(1) \uff1a\u5c06 src \u5269\u4f59\u4e00\u4e2a\u5706\u76d8\u79fb\u5230 tar\n move_pan(src, tar);\n // \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 buf \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 src \u79fb\u5230 tar\n dfs(i - 1, buf, src, tar);\n}\n\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 */\nfn solve_hanota(A: &mut Vec<i32>, B: &mut Vec<i32>, C: &mut Vec<i32>) {\n let n = A.len() as i32;\n // \u5c06 A \u9876\u90e8 n \u4e2a\u5706\u76d8\u501f\u52a9 B \u79fb\u5230 C\n dfs(n, A, B, C);\n}\n
hanota.c/* \u79fb\u52a8\u4e00\u4e2a\u5706\u76d8 */\nvoid move(int *src, int *srcSize, int *tar, int *tarSize) {\n // \u4ece src \u9876\u90e8\u62ff\u51fa\u4e00\u4e2a\u5706\u76d8\n int pan = src[*srcSize - 1];\n src[*srcSize - 1] = 0;\n (*srcSize)--;\n // \u5c06\u5706\u76d8\u653e\u5165 tar \u9876\u90e8\n tar[*tarSize] = pan;\n (*tarSize)++;\n}\n\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 f(i) */\nvoid dfs(int i, int *src, int *srcSize, int *buf, int *bufSize, int *tar, int *tarSize) {\n // \u82e5 src \u53ea\u5269\u4e0b\u4e00\u4e2a\u5706\u76d8\uff0c\u5219\u76f4\u63a5\u5c06\u5176\u79fb\u5230 tar\n if (i == 1) {\n move(src, srcSize, tar, tarSize);\n return;\n }\n // \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 src \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 tar \u79fb\u5230 buf\n dfs(i - 1, src, srcSize, tar, tarSize, buf, bufSize);\n // \u5b50\u95ee\u9898 f(1) \uff1a\u5c06 src \u5269\u4f59\u4e00\u4e2a\u5706\u76d8\u79fb\u5230 tar\n move(src, srcSize, tar, tarSize);\n // \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 buf \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 src \u79fb\u5230 tar\n dfs(i - 1, buf, bufSize, src, srcSize, tar, tarSize);\n}\n\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 */\nvoid solveHanota(int *A, int *ASize, int *B, int *BSize, int *C, int *CSize) {\n // \u5c06 A \u9876\u90e8 n \u4e2a\u5706\u76d8\u501f\u52a9 B \u79fb\u5230 C\n dfs(*ASize, A, ASize, B, BSize, C, CSize);\n}\n
hanota.zig[class]{}-[func]{move}\n\n[class]{}-[func]{dfs}\n\n[class]{}-[func]{solveHanota}\n
\u5982\u56fe 12-15 \u6240\u793a\uff0c\u6c49\u8bfa\u5854\u95ee\u9898\u5f62\u6210\u4e00\u68f5\u9ad8\u5ea6\u4e3a \\(n\\) \u7684\u9012\u5f52\u6811\uff0c\u6bcf\u4e2a\u8282\u70b9\u4ee3\u8868\u4e00\u4e2a\u5b50\u95ee\u9898\uff0c\u5bf9\u5e94\u4e00\u4e2a\u5f00\u542f\u7684 dfs()
\u51fd\u6570\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(2^n)\\) \uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002
\u56fe 12-15 \u00a0 \u6c49\u8bfa\u5854\u95ee\u9898\u7684\u9012\u5f52\u6811
Quote
\u6c49\u8bfa\u5854\u95ee\u9898\u6e90\u81ea\u4e00\u4e2a\u53e4\u8001\u7684\u4f20\u8bf4\u3002\u5728\u53e4\u5370\u5ea6\u7684\u4e00\u4e2a\u5bfa\u5e99\u91cc\uff0c\u50e7\u4fa3\u4eec\u6709\u4e09\u6839\u9ad8\u5927\u7684\u94bb\u77f3\u67f1\u5b50\uff0c\u4ee5\u53ca \\(64\\) \u4e2a\u5927\u5c0f\u4e0d\u4e00\u7684\u91d1\u5706\u76d8\u3002\u50e7\u4fa3\u4eec\u4e0d\u65ad\u5730\u79fb\u52a8\u5706\u76d8\uff0c\u4ed6\u4eec\u76f8\u4fe1\u5728\u6700\u540e\u4e00\u4e2a\u5706\u76d8\u88ab\u6b63\u786e\u653e\u7f6e\u7684\u90a3\u4e00\u523b\uff0c\u8fd9\u4e2a\u4e16\u754c\u5c31\u4f1a\u7ed3\u675f\u3002
\u7136\u800c\uff0c\u5373\u4f7f\u50e7\u4fa3\u4eec\u6bcf\u79d2\u949f\u79fb\u52a8\u4e00\u6b21\uff0c\u603b\u5171\u9700\u8981\u5927\u7ea6 \\(2^{64} \\approx 1.84\u00d710^{19}\\) \u79d2\uff0c\u5408\u7ea6 \\(5850\\) \u4ebf\u5e74\uff0c\u8fdc\u8fdc\u8d85\u8fc7\u4e86\u73b0\u5728\u5bf9\u5b87\u5b99\u5e74\u9f84\u7684\u4f30\u8ba1\u3002\u6240\u4ee5\uff0c\u5018\u82e5\u8fd9\u4e2a\u4f20\u8bf4\u662f\u771f\u7684\uff0c\u6211\u4eec\u5e94\u8be5\u4e0d\u9700\u8981\u62c5\u5fc3\u4e16\u754c\u672b\u65e5\u7684\u5230\u6765\u3002
"},{"location":"chapter_divide_and_conquer/summary/","title":"12.5 \u00a0 \u5c0f\u7ed3","text":"Abstract
\u5c0f\u6eaa\u6c47\u5165\u6cb3\u6d41\uff0c\u6c5f\u6cb3\u6c47\u5165\u5927\u6d77\u3002
\u52a8\u6001\u89c4\u5212\u5c06\u5c0f\u95ee\u9898\u7684\u89e3\u6c47\u96c6\u6210\u5927\u95ee\u9898\u7684\u7b54\u6848\uff0c\u4e00\u6b65\u6b65\u5f15\u9886\u6211\u4eec\u8d70\u5411\u89e3\u51b3\u95ee\u9898\u7684\u5f7c\u5cb8\u3002
"},{"location":"chapter_dynamic_programming/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"\u5728\u4e0a\u4e00\u8282\u4e2d\uff0c\u6211\u4eec\u5b66\u4e60\u4e86\u52a8\u6001\u89c4\u5212\u662f\u5982\u4f55\u901a\u8fc7\u5b50\u95ee\u9898\u5206\u89e3\u6765\u6c42\u89e3\u539f\u95ee\u9898\u7684\u3002\u5b9e\u9645\u4e0a\uff0c\u5b50\u95ee\u9898\u5206\u89e3\u662f\u4e00\u79cd\u901a\u7528\u7684\u7b97\u6cd5\u601d\u8def\uff0c\u5728\u5206\u6cbb\u3001\u52a8\u6001\u89c4\u5212\u3001\u56de\u6eaf\u4e2d\u7684\u4fa7\u91cd\u70b9\u4e0d\u540c\u3002
\u5b9e\u9645\u4e0a\uff0c\u52a8\u6001\u89c4\u5212\u5e38\u7528\u6765\u6c42\u89e3\u6700\u4f18\u5316\u95ee\u9898\uff0c\u5b83\u4eec\u4e0d\u4ec5\u5305\u542b\u91cd\u53e0\u5b50\u95ee\u9898\uff0c\u8fd8\u5177\u6709\u53e6\u5916\u4e24\u5927\u7279\u6027\uff1a\u6700\u4f18\u5b50\u7ed3\u6784\u3001\u65e0\u540e\u6548\u6027\u3002
"},{"location":"chapter_dynamic_programming/dp_problem_features/#1421","title":"14.2.1 \u00a0 \u6700\u4f18\u5b50\u7ed3\u6784","text":"\u6211\u4eec\u5bf9\u722c\u697c\u68af\u95ee\u9898\u7a0d\u4f5c\u6539\u52a8\uff0c\u4f7f\u4e4b\u66f4\u52a0\u9002\u5408\u5c55\u793a\u6700\u4f18\u5b50\u7ed3\u6784\u6982\u5ff5\u3002
\u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7
\u7ed9\u5b9a\u4e00\u4e2a\u697c\u68af\uff0c\u4f60\u6bcf\u6b65\u53ef\u4ee5\u4e0a \\(1\\) \u9636\u6216\u8005 \\(2\\) \u9636\uff0c\u6bcf\u4e00\u9636\u697c\u68af\u4e0a\u90fd\u8d34\u6709\u4e00\u4e2a\u975e\u8d1f\u6574\u6570\uff0c\u8868\u793a\u4f60\u5728\u8be5\u53f0\u9636\u6240\u9700\u8981\u4ed8\u51fa\u7684\u4ee3\u4ef7\u3002\u7ed9\u5b9a\u4e00\u4e2a\u975e\u8d1f\u6574\u6570\u6570\u7ec4 \\(cost\\) \uff0c\u5176\u4e2d \\(cost[i]\\) \u8868\u793a\u5728\u7b2c \\(i\\) \u4e2a\u53f0\u9636\u9700\u8981\u4ed8\u51fa\u7684\u4ee3\u4ef7\uff0c\\(cost[0]\\) \u4e3a\u5730\u9762\uff08\u8d77\u59cb\u70b9\uff09\u3002\u8bf7\u8ba1\u7b97\u6700\u5c11\u9700\u8981\u4ed8\u51fa\u591a\u5c11\u4ee3\u4ef7\u624d\u80fd\u5230\u8fbe\u9876\u90e8\uff1f
\u5982\u56fe 14-6 \u6240\u793a\uff0c\u82e5\u7b2c \\(1\\)\u3001\\(2\\)\u3001\\(3\\) \u9636\u7684\u4ee3\u4ef7\u5206\u522b\u4e3a \\(1\\)\u3001\\(10\\)\u3001\\(1\\) \uff0c\u5219\u4ece\u5730\u9762\u722c\u5230\u7b2c \\(3\\) \u9636\u7684\u6700\u5c0f\u4ee3\u4ef7\u4e3a \\(2\\) \u3002
\u56fe 14-6 \u00a0 \u722c\u5230\u7b2c 3 \u9636\u7684\u6700\u5c0f\u4ee3\u4ef7
\u8bbe \\(dp[i]\\) \u4e3a\u722c\u5230\u7b2c \\(i\\) \u9636\u7d2f\u8ba1\u4ed8\u51fa\u7684\u4ee3\u4ef7\uff0c\u7531\u4e8e\u7b2c \\(i\\) \u9636\u53ea\u53ef\u80fd\u4ece \\(i - 1\\) \u9636\u6216 \\(i - 2\\) \u9636\u8d70\u6765\uff0c\u56e0\u6b64 \\(dp[i]\\) \u53ea\u53ef\u80fd\u7b49\u4e8e \\(dp[i - 1] + cost[i]\\) \u6216 \\(dp[i - 2] + cost[i]\\) \u3002\u4e3a\u4e86\u5c3d\u53ef\u80fd\u51cf\u5c11\u4ee3\u4ef7\uff0c\u6211\u4eec\u5e94\u8be5\u9009\u62e9\u4e24\u8005\u4e2d\u8f83\u5c0f\u7684\u90a3\u4e00\u4e2a\uff1a
\\[ dp[i] = \\min(dp[i-1], dp[i-2]) + cost[i] \\]\u8fd9\u4fbf\u53ef\u4ee5\u5f15\u51fa\u6700\u4f18\u5b50\u7ed3\u6784\u7684\u542b\u4e49\uff1a\u539f\u95ee\u9898\u7684\u6700\u4f18\u89e3\u662f\u4ece\u5b50\u95ee\u9898\u7684\u6700\u4f18\u89e3\u6784\u5efa\u5f97\u6765\u7684\u3002
\u672c\u9898\u663e\u7136\u5177\u6709\u6700\u4f18\u5b50\u7ed3\u6784\uff1a\u6211\u4eec\u4ece\u4e24\u4e2a\u5b50\u95ee\u9898\u6700\u4f18\u89e3 \\(dp[i-1]\\) \u548c \\(dp[i-2]\\) \u4e2d\u6311\u9009\u51fa\u8f83\u4f18\u7684\u90a3\u4e00\u4e2a\uff0c\u5e76\u7528\u5b83\u6784\u5efa\u51fa\u539f\u95ee\u9898 \\(dp[i]\\) \u7684\u6700\u4f18\u89e3\u3002
\u90a3\u4e48\uff0c\u4e0a\u4e00\u8282\u7684\u722c\u697c\u68af\u9898\u76ee\u6709\u6ca1\u6709\u6700\u4f18\u5b50\u7ed3\u6784\u5462\uff1f\u5b83\u7684\u76ee\u6807\u662f\u6c42\u89e3\u65b9\u6848\u6570\u91cf\uff0c\u770b\u4f3c\u662f\u4e00\u4e2a\u8ba1\u6570\u95ee\u9898\uff0c\u4f46\u5982\u679c\u6362\u4e00\u79cd\u95ee\u6cd5\uff1a\u201c\u6c42\u89e3\u6700\u5927\u65b9\u6848\u6570\u91cf\u201d\u3002\u6211\u4eec\u610f\u5916\u5730\u53d1\u73b0\uff0c\u867d\u7136\u9898\u76ee\u4fee\u6539\u524d\u540e\u662f\u7b49\u4ef7\u7684\uff0c\u4f46\u6700\u4f18\u5b50\u7ed3\u6784\u6d6e\u73b0\u51fa\u6765\u4e86\uff1a\u7b2c \\(n\\) \u9636\u6700\u5927\u65b9\u6848\u6570\u91cf\u7b49\u4e8e\u7b2c \\(n-1\\) \u9636\u548c\u7b2c \\(n-2\\) \u9636\u6700\u5927\u65b9\u6848\u6570\u91cf\u4e4b\u548c\u3002\u6240\u4ee5\u8bf4\uff0c\u6700\u4f18\u5b50\u7ed3\u6784\u7684\u89e3\u91ca\u65b9\u5f0f\u6bd4\u8f83\u7075\u6d3b\uff0c\u5728\u4e0d\u540c\u95ee\u9898\u4e2d\u4f1a\u6709\u4e0d\u540c\u7684\u542b\u4e49\u3002
\u6839\u636e\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\uff0c\u4ee5\u53ca\u521d\u59cb\u72b6\u6001 \\(dp[1] = cost[1]\\) \u548c \\(dp[2] = cost[2]\\) \uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5f97\u5230\u52a8\u6001\u89c4\u5212\u4ee3\u7801\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig min_cost_climbing_stairs_dp.pydef min_cost_climbing_stairs_dp(cost: list[int]) -> int:\n \"\"\"\u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212\"\"\"\n n = len(cost) - 1\n if n == 1 or n == 2:\n return cost[n]\n # \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n dp = [0] * (n + 1)\n # \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n dp[1], dp[2] = cost[1], cost[2]\n # \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n for i in range(3, n + 1):\n dp[i] = min(dp[i - 1], dp[i - 2]) + cost[i]\n return dp[n]\n
min_cost_climbing_stairs_dp.cpp/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212 */\nint minCostClimbingStairsDP(vector<int> &cost) {\n int n = cost.size() - 1;\n if (n == 1 || n == 2)\n return cost[n];\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n vector<int> dp(n + 1);\n // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n dp[1] = cost[1];\n dp[2] = cost[2];\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n for (int i = 3; i <= n; i++) {\n dp[i] = min(dp[i - 1], dp[i - 2]) + cost[i];\n }\n return dp[n];\n}\n
min_cost_climbing_stairs_dp.java/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212 */\nint minCostClimbingStairsDP(int[] cost) {\n int n = cost.length - 1;\n if (n == 1 || n == 2)\n return cost[n];\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n int[] dp = new int[n + 1];\n // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n dp[1] = cost[1];\n dp[2] = cost[2];\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n for (int i = 3; i <= n; i++) {\n dp[i] = Math.min(dp[i - 1], dp[i - 2]) + cost[i];\n }\n return dp[n];\n}\n
min_cost_climbing_stairs_dp.cs/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212 */\nint MinCostClimbingStairsDP(int[] cost) {\n int n = cost.Length - 1;\n if (n == 1 || n == 2)\n return cost[n];\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n int[] dp = new int[n + 1];\n // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n dp[1] = cost[1];\n dp[2] = cost[2];\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n for (int i = 3; i <= n; i++) {\n dp[i] = Math.Min(dp[i - 1], dp[i - 2]) + cost[i];\n }\n return dp[n];\n}\n
min_cost_climbing_stairs_dp.go/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc minCostClimbingStairsDP(cost []int) int {\n n := len(cost) - 1\n if n == 1 || n == 2 {\n return cost[n]\n }\n min := func(a, b int) int {\n if a < b {\n return a\n }\n return b\n }\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n dp := make([]int, n+1)\n // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n dp[1] = cost[1]\n dp[2] = cost[2]\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n for i := 3; i <= n; i++ {\n dp[i] = min(dp[i-1], dp[i-2]) + cost[i]\n }\n return dp[n]\n}\n
min_cost_climbing_stairs_dp.swift/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc minCostClimbingStairsDP(cost: [Int]) -> Int {\n let n = cost.count - 1\n if n == 1 || n == 2 {\n return cost[n]\n }\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n var dp = Array(repeating: 0, count: n + 1)\n // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n dp[1] = cost[1]\n dp[2] = cost[2]\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n for i in stride(from: 3, through: n, by: 1) {\n dp[i] = min(dp[i - 1], dp[i - 2]) + cost[i]\n }\n return dp[n]\n}\n
min_cost_climbing_stairs_dp.js/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction minCostClimbingStairsDP(cost) {\n const n = cost.length - 1;\n if (n === 1 || n === 2) {\n return cost[n];\n }\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n const dp = new Array(n + 1);\n // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n dp[1] = cost[1];\n dp[2] = cost[2];\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n for (let i = 3; i <= n; i++) {\n dp[i] = Math.min(dp[i - 1], dp[i - 2]) + cost[i];\n }\n return dp[n];\n}\n
min_cost_climbing_stairs_dp.ts/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction minCostClimbingStairsDP(cost: Array<number>): number {\n const n = cost.length - 1;\n if (n === 1 || n === 2) {\n return cost[n];\n }\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n const dp = new Array(n + 1);\n // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n dp[1] = cost[1];\n dp[2] = cost[2];\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n for (let i = 3; i <= n; i++) {\n dp[i] = Math.min(dp[i - 1], dp[i - 2]) + cost[i];\n }\n return dp[n];\n}\n
min_cost_climbing_stairs_dp.dart/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212 */\nint minCostClimbingStairsDP(List<int> cost) {\n int n = cost.length - 1;\n if (n == 1 || n == 2) return cost[n];\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n List<int> dp = List.filled(n + 1, 0);\n // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n dp[1] = cost[1];\n dp[2] = cost[2];\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n for (int i = 3; i <= n; i++) {\n dp[i] = min(dp[i - 1], dp[i - 2]) + cost[i];\n }\n return dp[n];\n}\n
min_cost_climbing_stairs_dp.rs/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212 */\nfn min_cost_climbing_stairs_dp(cost: &[i32]) -> i32 {\n let n = cost.len() - 1;\n if n == 1 || n == 2 { return cost[n]; }\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n let mut dp = vec![-1; n + 1];\n // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n dp[1] = cost[1];\n dp[2] = cost[2];\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n for i in 3..=n {\n dp[i] = cmp::min(dp[i - 1], dp[i - 2]) + cost[i];\n }\n dp[n]\n}\n
min_cost_climbing_stairs_dp.c/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212 */\nint minCostClimbingStairsDP(int cost[], int costSize) {\n int n = costSize - 1;\n if (n == 1 || n == 2)\n return cost[n];\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n int *dp = calloc(n + 1, sizeof(int));\n // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n dp[1] = cost[1];\n dp[2] = cost[2];\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n for (int i = 3; i <= n; i++) {\n dp[i] = myMin(dp[i - 1], dp[i - 2]) + cost[i];\n }\n int res = dp[n];\n // \u91ca\u653e\u5185\u5b58\n free(dp);\n return res;\n}\n
min_cost_climbing_stairs_dp.zig// \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212\nfn minCostClimbingStairsDP(comptime cost: []i32) i32 {\n comptime var n = cost.len - 1;\n if (n == 1 or n == 2) {\n return cost[n];\n }\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n var dp = [_]i32{-1} ** (n + 1);\n // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n dp[1] = cost[1];\n dp[2] = cost[2];\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n for (3..n + 1) |i| {\n dp[i] = @min(dp[i - 1], dp[i - 2]) + cost[i];\n }\n return dp[n];\n}\n
\u56fe 14-7 \u5c55\u793a\u4e86\u4ee5\u4e0a\u4ee3\u7801\u7684\u52a8\u6001\u89c4\u5212\u8fc7\u7a0b\u3002
\u56fe 14-7 \u00a0 \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\u7684\u52a8\u6001\u89c4\u5212\u8fc7\u7a0b
\u672c\u9898\u4e5f\u53ef\u4ee5\u8fdb\u884c\u7a7a\u95f4\u4f18\u5316\uff0c\u5c06\u4e00\u7ef4\u538b\u7f29\u81f3\u96f6\u7ef4\uff0c\u4f7f\u5f97\u7a7a\u95f4\u590d\u6742\u5ea6\u4ece \\(O(n)\\) \u964d\u81f3 \\(O(1)\\) \uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig min_cost_climbing_stairs_dp.pydef min_cost_climbing_stairs_dp_comp(cost: list[int]) -> int:\n \"\"\"\u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\"\"\"\n n = len(cost) - 1\n if n == 1 or n == 2:\n return cost[n]\n a, b = cost[1], cost[2]\n for i in range(3, n + 1):\n a, b = b, min(a, b) + cost[i]\n return b\n
min_cost_climbing_stairs_dp.cpp/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint minCostClimbingStairsDPComp(vector<int> &cost) {\n int n = cost.size() - 1;\n if (n == 1 || n == 2)\n return cost[n];\n int a = cost[1], b = cost[2];\n for (int i = 3; i <= n; i++) {\n int tmp = b;\n b = min(a, tmp) + cost[i];\n a = tmp;\n }\n return b;\n}\n
min_cost_climbing_stairs_dp.java/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint minCostClimbingStairsDPComp(int[] cost) {\n int n = cost.length - 1;\n if (n == 1 || n == 2)\n return cost[n];\n int a = cost[1], b = cost[2];\n for (int i = 3; i <= n; i++) {\n int tmp = b;\n b = Math.min(a, tmp) + cost[i];\n a = tmp;\n }\n return b;\n}\n
min_cost_climbing_stairs_dp.cs/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint MinCostClimbingStairsDPComp(int[] cost) {\n int n = cost.Length - 1;\n if (n == 1 || n == 2)\n return cost[n];\n int a = cost[1], b = cost[2];\n for (int i = 3; i <= n; i++) {\n int tmp = b;\n b = Math.Min(a, tmp) + cost[i];\n a = tmp;\n }\n return b;\n}\n
min_cost_climbing_stairs_dp.go/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc minCostClimbingStairsDPComp(cost []int) int {\n n := len(cost) - 1\n if n == 1 || n == 2 {\n return cost[n]\n }\n min := func(a, b int) int {\n if a < b {\n return a\n }\n return b\n }\n // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n a, b := cost[1], cost[2]\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n for i := 3; i <= n; i++ {\n tmp := b\n b = min(a, tmp) + cost[i]\n a = tmp\n }\n return b\n}\n
min_cost_climbing_stairs_dp.swift/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc minCostClimbingStairsDPComp(cost: [Int]) -> Int {\n let n = cost.count - 1\n if n == 1 || n == 2 {\n return cost[n]\n }\n var (a, b) = (cost[1], cost[2])\n for i in stride(from: 3, through: n, by: 1) {\n (a, b) = (b, min(a, b) + cost[i])\n }\n return b\n}\n
min_cost_climbing_stairs_dp.js/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction minCostClimbingStairsDPComp(cost) {\n const n = cost.length - 1;\n if (n === 1 || n === 2) {\n return cost[n];\n }\n let a = cost[1],\n b = cost[2];\n for (let i = 3; i <= n; i++) {\n const tmp = b;\n b = Math.min(a, tmp) + cost[i];\n a = tmp;\n }\n return b;\n}\n
min_cost_climbing_stairs_dp.ts/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction minCostClimbingStairsDPComp(cost: Array<number>): number {\n const n = cost.length - 1;\n if (n === 1 || n === 2) {\n return cost[n];\n }\n let a = cost[1],\n b = cost[2];\n for (let i = 3; i <= n; i++) {\n const tmp = b;\n b = Math.min(a, tmp) + cost[i];\n a = tmp;\n }\n return b;\n}\n
min_cost_climbing_stairs_dp.dart/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint minCostClimbingStairsDPComp(List<int> cost) {\n int n = cost.length - 1;\n if (n == 1 || n == 2) return cost[n];\n int a = cost[1], b = cost[2];\n for (int i = 3; i <= n; i++) {\n int tmp = b;\n b = min(a, tmp) + cost[i];\n a = tmp;\n }\n return b;\n}\n
min_cost_climbing_stairs_dp.rs/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfn min_cost_climbing_stairs_dp_comp(cost: &[i32]) -> i32 {\n let n = cost.len() - 1;\n if n == 1 || n == 2 { return cost[n] };\n let (mut a, mut b) = (cost[1], cost[2]);\n for i in 3..=n {\n let tmp = b;\n b = cmp::min(a, tmp) + cost[i];\n a = tmp;\n }\n b\n}\n
min_cost_climbing_stairs_dp.c/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint minCostClimbingStairsDPComp(int cost[], int costSize) {\n int n = costSize - 1;\n if (n == 1 || n == 2)\n return cost[n];\n int a = cost[1], b = cost[2];\n for (int i = 3; i <= n; i++) {\n int tmp = b;\n b = myMin(a, tmp) + cost[i];\n a = tmp;\n }\n return b;\n}\n
min_cost_climbing_stairs_dp.zig// \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\nfn minCostClimbingStairsDPComp(cost: []i32) i32 {\n var n = cost.len - 1;\n if (n == 1 or n == 2) {\n return cost[n];\n }\n var a = cost[1];\n var b = cost[2];\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n for (3..n + 1) |i| {\n var tmp = b;\n b = @min(a, tmp) + cost[i];\n a = tmp;\n }\n return b;\n}\n
"},{"location":"chapter_dynamic_programming/dp_problem_features/#1422","title":"14.2.2 \u00a0 \u65e0\u540e\u6548\u6027","text":"\u65e0\u540e\u6548\u6027\u662f\u52a8\u6001\u89c4\u5212\u80fd\u591f\u6709\u6548\u89e3\u51b3\u95ee\u9898\u7684\u91cd\u8981\u7279\u6027\u4e4b\u4e00\uff0c\u5176\u5b9a\u4e49\u4e3a\uff1a\u7ed9\u5b9a\u4e00\u4e2a\u786e\u5b9a\u7684\u72b6\u6001\uff0c\u5b83\u7684\u672a\u6765\u53d1\u5c55\u53ea\u4e0e\u5f53\u524d\u72b6\u6001\u6709\u5173\uff0c\u800c\u4e0e\u8fc7\u53bb\u7ecf\u5386\u7684\u6240\u6709\u72b6\u6001\u65e0\u5173\u3002
\u4ee5\u722c\u697c\u68af\u95ee\u9898\u4e3a\u4f8b\uff0c\u7ed9\u5b9a\u72b6\u6001 \\(i\\) \uff0c\u5b83\u4f1a\u53d1\u5c55\u51fa\u72b6\u6001 \\(i+1\\) \u548c\u72b6\u6001 \\(i+2\\) \uff0c\u5206\u522b\u5bf9\u5e94\u8df3 \\(1\\) \u6b65\u548c\u8df3 \\(2\\) \u6b65\u3002\u5728\u505a\u51fa\u8fd9\u4e24\u79cd\u9009\u62e9\u65f6\uff0c\u6211\u4eec\u65e0\u987b\u8003\u8651\u72b6\u6001 \\(i\\) \u4e4b\u524d\u7684\u72b6\u6001\uff0c\u5b83\u4eec\u5bf9\u72b6\u6001 \\(i\\) \u7684\u672a\u6765\u6ca1\u6709\u5f71\u54cd\u3002
\u7136\u800c\uff0c\u5982\u679c\u6211\u4eec\u7ed9\u722c\u697c\u68af\u95ee\u9898\u6dfb\u52a0\u4e00\u4e2a\u7ea6\u675f\uff0c\u60c5\u51b5\u5c31\u4e0d\u4e00\u6837\u4e86\u3002
\u5e26\u7ea6\u675f\u722c\u697c\u68af
\u7ed9\u5b9a\u4e00\u4e2a\u5171\u6709 \\(n\\) \u9636\u7684\u697c\u68af\uff0c\u4f60\u6bcf\u6b65\u53ef\u4ee5\u4e0a \\(1\\) \u9636\u6216\u8005 \\(2\\) \u9636\uff0c\u4f46\u4e0d\u80fd\u8fde\u7eed\u4e24\u8f6e\u8df3 \\(1\\) \u9636\uff0c\u8bf7\u95ee\u6709\u591a\u5c11\u79cd\u65b9\u6848\u53ef\u4ee5\u722c\u5230\u697c\u9876\uff1f
\u5982\u56fe 14-8 \u6240\u793a\uff0c\u722c\u4e0a\u7b2c \\(3\\) \u9636\u4ec5\u5269 \\(2\\) \u79cd\u53ef\u884c\u65b9\u6848\uff0c\u5176\u4e2d\u8fde\u7eed\u4e09\u6b21\u8df3 \\(1\\) \u9636\u7684\u65b9\u6848\u4e0d\u6ee1\u8db3\u7ea6\u675f\u6761\u4ef6\uff0c\u56e0\u6b64\u88ab\u820d\u5f03\u3002
\u56fe 14-8 \u00a0 \u5e26\u7ea6\u675f\u722c\u5230\u7b2c 3 \u9636\u7684\u65b9\u6848\u6570\u91cf
\u5728\u8be5\u95ee\u9898\u4e2d\uff0c\u5982\u679c\u4e0a\u4e00\u8f6e\u662f\u8df3 \\(1\\) \u9636\u4e0a\u6765\u7684\uff0c\u90a3\u4e48\u4e0b\u4e00\u8f6e\u5c31\u5fc5\u987b\u8df3 \\(2\\) \u9636\u3002\u8fd9\u610f\u5473\u7740\uff0c\u4e0b\u4e00\u6b65\u9009\u62e9\u4e0d\u80fd\u7531\u5f53\u524d\u72b6\u6001\uff08\u5f53\u524d\u6240\u5728\u697c\u68af\u9636\u6570\uff09\u72ec\u7acb\u51b3\u5b9a\uff0c\u8fd8\u548c\u524d\u4e00\u4e2a\u72b6\u6001\uff08\u4e0a\u8f6e\u6240\u5728\u697c\u68af\u9636\u6570\uff09\u6709\u5173\u3002
\u4e0d\u96be\u53d1\u73b0\uff0c\u6b64\u95ee\u9898\u5df2\u4e0d\u6ee1\u8db3\u65e0\u540e\u6548\u6027\uff0c\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b \\(dp[i] = dp[i-1] + dp[i-2]\\) \u4e5f\u5931\u6548\u4e86\uff0c\u56e0\u4e3a \\(dp[i-1]\\) \u4ee3\u8868\u672c\u8f6e\u8df3 \\(1\\) \u9636\uff0c\u4f46\u5176\u4e2d\u5305\u542b\u4e86\u8bb8\u591a\u201c\u4e0a\u4e00\u8f6e\u662f\u8df3 \\(1\\) \u9636\u4e0a\u6765\u7684\u201d\u65b9\u6848\uff0c\u800c\u4e3a\u4e86\u6ee1\u8db3\u7ea6\u675f\uff0c\u6211\u4eec\u5c31\u4e0d\u80fd\u5c06 \\(dp[i-1]\\) \u76f4\u63a5\u8ba1\u5165 \\(dp[i]\\) \u4e2d\u3002
\u4e3a\u6b64\uff0c\u6211\u4eec\u9700\u8981\u6269\u5c55\u72b6\u6001\u5b9a\u4e49\uff1a\u72b6\u6001 \\([i, j]\\) \u8868\u793a\u5904\u5728\u7b2c \\(i\\) \u9636\u5e76\u4e14\u4e0a\u4e00\u8f6e\u8df3\u4e86 \\(j\\) \u9636\uff0c\u5176\u4e2d \\(j \\in \\{1, 2\\}\\) \u3002\u6b64\u72b6\u6001\u5b9a\u4e49\u6709\u6548\u5730\u533a\u5206\u4e86\u4e0a\u4e00\u8f6e\u8df3\u4e86 \\(1\\) \u9636\u8fd8\u662f \\(2\\) \u9636\uff0c\u6211\u4eec\u53ef\u4ee5\u636e\u6b64\u6765\u5224\u65ad\u5f53\u524d\u72b6\u6001\u662f\u4ece\u4f55\u800c\u6765\u7684\u3002
\u5982\u56fe 14-9 \u6240\u793a\uff0c\u5728\u8be5\u5b9a\u4e49\u4e0b\uff0c\\(dp[i, j]\\) \u8868\u793a\u72b6\u6001 \\([i, j]\\) \u5bf9\u5e94\u7684\u65b9\u6848\u6570\u3002\u6b64\u65f6\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u4e3a\uff1a
\\[ \\begin{cases} dp[i, 1] = dp[i-1, 2] \\\\ dp[i, 2] = dp[i-2, 1] + dp[i-2, 2] \\end{cases} \\]\u56fe 14-9 \u00a0 \u8003\u8651\u7ea6\u675f\u4e0b\u7684\u9012\u63a8\u5173\u7cfb
\u6700\u7ec8\uff0c\u8fd4\u56de \\(dp[n, 1] + dp[n, 2]\\) \u5373\u53ef\uff0c\u4e24\u8005\u4e4b\u548c\u4ee3\u8868\u722c\u5230\u7b2c \\(n\\) \u9636\u7684\u65b9\u6848\u603b\u6570\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig climbing_stairs_constraint_dp.pydef climbing_stairs_constraint_dp(n: int) -> int:\n \"\"\"\u5e26\u7ea6\u675f\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212\"\"\"\n if n == 1 or n == 2:\n return 1\n # \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n dp = [[0] * 3 for _ in range(n + 1)]\n # \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n dp[1][1], dp[1][2] = 1, 0\n dp[2][1], dp[2][2] = 0, 1\n # \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n for i in range(3, n + 1):\n dp[i][1] = dp[i - 1][2]\n dp[i][2] = dp[i - 2][1] + dp[i - 2][2]\n return dp[n][1] + dp[n][2]\n
climbing_stairs_constraint_dp.cpp/* \u5e26\u7ea6\u675f\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nint climbingStairsConstraintDP(int n) {\n if (n == 1 || n == 2) {\n return 1;\n }\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n vector<vector<int>> dp(n + 1, vector<int>(3, 0));\n // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n dp[1][1] = 1;\n dp[1][2] = 0;\n dp[2][1] = 0;\n dp[2][2] = 1;\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n for (int i = 3; i <= n; i++) {\n dp[i][1] = dp[i - 1][2];\n dp[i][2] = dp[i - 2][1] + dp[i - 2][2];\n }\n return dp[n][1] + dp[n][2];\n}\n
climbing_stairs_constraint_dp.java/* \u5e26\u7ea6\u675f\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nint climbingStairsConstraintDP(int n) {\n if (n == 1 || n == 2) {\n return 1;\n }\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n int[][] dp = new int[n + 1][3];\n // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n dp[1][1] = 1;\n dp[1][2] = 0;\n dp[2][1] = 0;\n dp[2][2] = 1;\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n for (int i = 3; i <= n; i++) {\n dp[i][1] = dp[i - 1][2];\n dp[i][2] = dp[i - 2][1] + dp[i - 2][2];\n }\n return dp[n][1] + dp[n][2];\n}\n
climbing_stairs_constraint_dp.cs/* \u5e26\u7ea6\u675f\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nint ClimbingStairsConstraintDP(int n) {\n if (n == 1 || n == 2) {\n return 1;\n }\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n int[,] dp = new int[n + 1, 3];\n // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n dp[1, 1] = 1;\n dp[1, 2] = 0;\n dp[2, 1] = 0;\n dp[2, 2] = 1;\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n for (int i = 3; i <= n; i++) {\n dp[i, 1] = dp[i - 1, 2];\n dp[i, 2] = dp[i - 2, 1] + dp[i - 2, 2];\n }\n return dp[n, 1] + dp[n, 2];\n}\n
climbing_stairs_constraint_dp.go/* \u5e26\u7ea6\u675f\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc climbingStairsConstraintDP(n int) int {\n if n == 1 || n == 2 {\n return 1\n }\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n dp := make([][3]int, n+1)\n // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n dp[1][1] = 1\n dp[1][2] = 0\n dp[2][1] = 0\n dp[2][2] = 1\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n for i := 3; i <= n; i++ {\n dp[i][1] = dp[i-1][2]\n dp[i][2] = dp[i-2][1] + dp[i-2][2]\n }\n return dp[n][1] + dp[n][2]\n}\n
climbing_stairs_constraint_dp.swift/* \u5e26\u7ea6\u675f\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc climbingStairsConstraintDP(n: Int) -> Int {\n if n == 1 || n == 2 {\n return 1\n }\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n var dp = Array(repeating: Array(repeating: 0, count: 3), count: n + 1)\n // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n dp[1][1] = 1\n dp[1][2] = 0\n dp[2][1] = 0\n dp[2][2] = 1\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n for i in stride(from: 3, through: n, by: 1) {\n dp[i][1] = dp[i - 1][2]\n dp[i][2] = dp[i - 2][1] + dp[i - 2][2]\n }\n return dp[n][1] + dp[n][2]\n}\n
climbing_stairs_constraint_dp.js/* \u5e26\u7ea6\u675f\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction climbingStairsConstraintDP(n) {\n if (n === 1 || n === 2) {\n return 1;\n }\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n const dp = Array.from(new Array(n + 1), () => new Array(3));\n // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n dp[1][1] = 1;\n dp[1][2] = 0;\n dp[2][1] = 0;\n dp[2][2] = 1;\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n for (let i = 3; i <= n; i++) {\n dp[i][1] = dp[i - 1][2];\n dp[i][2] = dp[i - 2][1] + dp[i - 2][2];\n }\n return dp[n][1] + dp[n][2];\n}\n
climbing_stairs_constraint_dp.ts/* \u5e26\u7ea6\u675f\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction climbingStairsConstraintDP(n: number): number {\n if (n === 1 || n === 2) {\n return 1;\n }\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n const dp = Array.from({ length: n + 1 }, () => new Array(3));\n // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n dp[1][1] = 1;\n dp[1][2] = 0;\n dp[2][1] = 0;\n dp[2][2] = 1;\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n for (let i = 3; i <= n; i++) {\n dp[i][1] = dp[i - 1][2];\n dp[i][2] = dp[i - 2][1] + dp[i - 2][2];\n }\n return dp[n][1] + dp[n][2];\n}\n
climbing_stairs_constraint_dp.dart/* \u5e26\u7ea6\u675f\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nint climbingStairsConstraintDP(int n) {\n if (n == 1 || n == 2) {\n return 1;\n }\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n List<List<int>> dp = List.generate(n + 1, (index) => List.filled(3, 0));\n // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n dp[1][1] = 1;\n dp[1][2] = 0;\n dp[2][1] = 0;\n dp[2][2] = 1;\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n for (int i = 3; i <= n; i++) {\n dp[i][1] = dp[i - 1][2];\n dp[i][2] = dp[i - 2][1] + dp[i - 2][2];\n }\n return dp[n][1] + dp[n][2];\n}\n
climbing_stairs_constraint_dp.rs/* \u5e26\u7ea6\u675f\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nfn climbing_stairs_constraint_dp(n: usize) -> i32 {\n if n == 1 || n == 2 { return 1 };\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n let mut dp = vec![vec![-1; 3]; n + 1];\n // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n dp[1][1] = 1;\n dp[1][2] = 0;\n dp[2][1] = 0;\n dp[2][2] = 1;\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n for i in 3..=n {\n dp[i][1] = dp[i - 1][2];\n dp[i][2] = dp[i - 2][1] + dp[i - 2][2];\n }\n dp[n][1] + dp[n][2]\n}\n
climbing_stairs_constraint_dp.c/* \u5e26\u7ea6\u675f\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nint climbingStairsConstraintDP(int n) {\n if (n == 1 || n == 2) {\n return 1;\n }\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n int **dp = malloc((n + 1) * sizeof(int *));\n for (int i = 0; i <= n; i++) {\n dp[i] = calloc(3, sizeof(int));\n }\n // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n dp[1][1] = 1;\n dp[1][2] = 0;\n dp[2][1] = 0;\n dp[2][2] = 1;\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n for (int i = 3; i <= n; i++) {\n dp[i][1] = dp[i - 1][2];\n dp[i][2] = dp[i - 2][1] + dp[i - 2][2];\n }\n int res = dp[n][1] + dp[n][2];\n // \u91ca\u653e\u5185\u5b58\n for (int i = 0; i <= n; i++) {\n free(dp[i]);\n }\n free(dp);\n return res;\n}\n
climbing_stairs_constraint_dp.zig// \u5e26\u7ea6\u675f\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212\nfn climbingStairsConstraintDP(comptime n: usize) i32 {\n if (n == 1 or n == 2) {\n return 1;\n }\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n var dp = [_][3]i32{ [_]i32{ -1, -1, -1 } } ** (n + 1);\n // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n dp[1][1] = 1;\n dp[1][2] = 0;\n dp[2][1] = 0;\n dp[2][2] = 1;\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n for (3..n + 1) |i| {\n dp[i][1] = dp[i - 1][2];\n dp[i][2] = dp[i - 2][1] + dp[i - 2][2];\n }\n return dp[n][1] + dp[n][2];\n}\n
\u5728\u4e0a\u9762\u7684\u6848\u4f8b\u4e2d\uff0c\u7531\u4e8e\u4ec5\u9700\u591a\u8003\u8651\u524d\u9762\u4e00\u4e2a\u72b6\u6001\uff0c\u56e0\u6b64\u6211\u4eec\u4ecd\u7136\u53ef\u4ee5\u901a\u8fc7\u6269\u5c55\u72b6\u6001\u5b9a\u4e49\uff0c\u4f7f\u5f97\u95ee\u9898\u91cd\u65b0\u6ee1\u8db3\u65e0\u540e\u6548\u6027\u3002\u7136\u800c\uff0c\u67d0\u4e9b\u95ee\u9898\u5177\u6709\u975e\u5e38\u4e25\u91cd\u7684\u201c\u6709\u540e\u6548\u6027\u201d\u3002
\u722c\u697c\u68af\u4e0e\u969c\u788d\u751f\u6210
\u7ed9\u5b9a\u4e00\u4e2a\u5171\u6709 \\(n\\) \u9636\u7684\u697c\u68af\uff0c\u4f60\u6bcf\u6b65\u53ef\u4ee5\u4e0a \\(1\\) \u9636\u6216\u8005 \\(2\\) \u9636\u3002\u89c4\u5b9a\u5f53\u722c\u5230\u7b2c \\(i\\) \u9636\u65f6\uff0c\u7cfb\u7edf\u81ea\u52a8\u4f1a\u5728\u7b2c \\(2i\\) \u9636\u4e0a\u653e\u4e0a\u969c\u788d\u7269\uff0c\u4e4b\u540e\u6240\u6709\u8f6e\u90fd\u4e0d\u5141\u8bb8\u8df3\u5230\u7b2c \\(2i\\) \u9636\u4e0a\u3002\u4f8b\u5982\uff0c\u524d\u4e24\u8f6e\u5206\u522b\u8df3\u5230\u4e86\u7b2c \\(2\\)\u3001\\(3\\) \u9636\u4e0a\uff0c\u5219\u4e4b\u540e\u5c31\u4e0d\u80fd\u8df3\u5230\u7b2c \\(4\\)\u3001\\(6\\) \u9636\u4e0a\u3002\u8bf7\u95ee\u6709\u591a\u5c11\u79cd\u65b9\u6848\u53ef\u4ee5\u722c\u5230\u697c\u9876\uff1f
\u5728\u8fd9\u4e2a\u95ee\u9898\u4e2d\uff0c\u4e0b\u6b21\u8df3\u8dc3\u4f9d\u8d56\u8fc7\u53bb\u6240\u6709\u7684\u72b6\u6001\uff0c\u56e0\u4e3a\u6bcf\u4e00\u6b21\u8df3\u8dc3\u90fd\u4f1a\u5728\u66f4\u9ad8\u7684\u9636\u68af\u4e0a\u8bbe\u7f6e\u969c\u788d\uff0c\u5e76\u5f71\u54cd\u672a\u6765\u7684\u8df3\u8dc3\u3002\u5bf9\u4e8e\u8fd9\u7c7b\u95ee\u9898\uff0c\u52a8\u6001\u89c4\u5212\u5f80\u5f80\u96be\u4ee5\u89e3\u51b3\u3002
\u5b9e\u9645\u4e0a\uff0c\u8bb8\u591a\u590d\u6742\u7684\u7ec4\u5408\u4f18\u5316\u95ee\u9898\uff08\u4f8b\u5982\u65c5\u884c\u5546\u95ee\u9898\uff09\u4e0d\u6ee1\u8db3\u65e0\u540e\u6548\u6027\u3002\u5bf9\u4e8e\u8fd9\u7c7b\u95ee\u9898\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u9009\u62e9\u4f7f\u7528\u5176\u4ed6\u65b9\u6cd5\uff0c\u4f8b\u5982\u542f\u53d1\u5f0f\u641c\u7d22\u3001\u9057\u4f20\u7b97\u6cd5\u3001\u5f3a\u5316\u5b66\u4e60\u7b49\uff0c\u4ece\u800c\u5728\u6709\u9650\u65f6\u95f4\u5185\u5f97\u5230\u53ef\u7528\u7684\u5c40\u90e8\u6700\u4f18\u89e3\u3002
"},{"location":"chapter_dynamic_programming/dp_solution_pipeline/","title":"14.3 \u00a0 \u52a8\u6001\u89c4\u5212\u89e3\u9898\u601d\u8def","text":"\u4e0a\u4e24\u8282\u4ecb\u7ecd\u4e86\u52a8\u6001\u89c4\u5212\u95ee\u9898\u7684\u4e3b\u8981\u7279\u5f81\uff0c\u63a5\u4e0b\u6765\u6211\u4eec\u4e00\u8d77\u63a2\u7a76\u4e24\u4e2a\u66f4\u52a0\u5b9e\u7528\u7684\u95ee\u9898\u3002
\u603b\u7684\u6765\u8bf4\uff0c\u5982\u679c\u4e00\u4e2a\u95ee\u9898\u5305\u542b\u91cd\u53e0\u5b50\u95ee\u9898\u3001\u6700\u4f18\u5b50\u7ed3\u6784\uff0c\u5e76\u6ee1\u8db3\u65e0\u540e\u6548\u6027\uff0c\u90a3\u4e48\u5b83\u901a\u5e38\u9002\u5408\u7528\u52a8\u6001\u89c4\u5212\u6c42\u89e3\u3002\u7136\u800c\uff0c\u6211\u4eec\u5f88\u96be\u4ece\u95ee\u9898\u63cf\u8ff0\u4e2d\u76f4\u63a5\u63d0\u53d6\u51fa\u8fd9\u4e9b\u7279\u6027\u3002\u56e0\u6b64\u6211\u4eec\u901a\u5e38\u4f1a\u653e\u5bbd\u6761\u4ef6\uff0c\u5148\u89c2\u5bdf\u95ee\u9898\u662f\u5426\u9002\u5408\u4f7f\u7528\u56de\u6eaf\uff08\u7a77\u4e3e\uff09\u89e3\u51b3\u3002
\u9002\u5408\u7528\u56de\u6eaf\u89e3\u51b3\u7684\u95ee\u9898\u901a\u5e38\u6ee1\u8db3\u201c\u51b3\u7b56\u6811\u6a21\u578b\u201d\uff0c\u8fd9\u79cd\u95ee\u9898\u53ef\u4ee5\u4f7f\u7528\u6811\u5f62\u7ed3\u6784\u6765\u63cf\u8ff0\uff0c\u5176\u4e2d\u6bcf\u4e00\u4e2a\u8282\u70b9\u4ee3\u8868\u4e00\u4e2a\u51b3\u7b56\uff0c\u6bcf\u4e00\u6761\u8def\u5f84\u4ee3\u8868\u4e00\u4e2a\u51b3\u7b56\u5e8f\u5217\u3002
\u6362\u53e5\u8bdd\u8bf4\uff0c\u5982\u679c\u95ee\u9898\u5305\u542b\u660e\u786e\u7684\u51b3\u7b56\u6982\u5ff5\uff0c\u5e76\u4e14\u89e3\u662f\u901a\u8fc7\u4e00\u7cfb\u5217\u51b3\u7b56\u4ea7\u751f\u7684\uff0c\u90a3\u4e48\u5b83\u5c31\u6ee1\u8db3\u51b3\u7b56\u6811\u6a21\u578b\uff0c\u901a\u5e38\u53ef\u4ee5\u4f7f\u7528\u56de\u6eaf\u6765\u89e3\u51b3\u3002
\u5728\u6b64\u57fa\u7840\u4e0a\uff0c\u52a8\u6001\u89c4\u5212\u95ee\u9898\u8fd8\u6709\u4e00\u4e9b\u5224\u65ad\u7684\u201c\u52a0\u5206\u9879\u201d\u3002
\u76f8\u5e94\u5730\uff0c\u4e5f\u5b58\u5728\u4e00\u4e9b\u201c\u51cf\u5206\u9879\u201d\u3002
\u5982\u679c\u4e00\u4e2a\u95ee\u9898\u6ee1\u8db3\u51b3\u7b56\u6811\u6a21\u578b\uff0c\u5e76\u5177\u6709\u8f83\u4e3a\u660e\u663e\u7684\u201c\u52a0\u5206\u9879\u201c\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5047\u8bbe\u5b83\u662f\u4e00\u4e2a\u52a8\u6001\u89c4\u5212\u95ee\u9898\uff0c\u5e76\u5728\u6c42\u89e3\u8fc7\u7a0b\u4e2d\u9a8c\u8bc1\u5b83\u3002
"},{"location":"chapter_dynamic_programming/dp_solution_pipeline/#1432","title":"14.3.2 \u00a0 \u95ee\u9898\u6c42\u89e3\u6b65\u9aa4","text":"\u52a8\u6001\u89c4\u5212\u7684\u89e3\u9898\u6d41\u7a0b\u4f1a\u56e0\u95ee\u9898\u7684\u6027\u8d28\u548c\u96be\u5ea6\u800c\u6709\u6240\u4e0d\u540c\uff0c\u4f46\u901a\u5e38\u9075\u5faa\u4ee5\u4e0b\u6b65\u9aa4\uff1a\u63cf\u8ff0\u51b3\u7b56\uff0c\u5b9a\u4e49\u72b6\u6001\uff0c\u5efa\u7acb \\(dp\\) \u8868\uff0c\u63a8\u5bfc\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\uff0c\u786e\u5b9a\u8fb9\u754c\u6761\u4ef6\u7b49\u3002
\u4e3a\u4e86\u66f4\u5f62\u8c61\u5730\u5c55\u793a\u89e3\u9898\u6b65\u9aa4\uff0c\u6211\u4eec\u4f7f\u7528\u4e00\u4e2a\u7ecf\u5178\u95ee\u9898\u201c\u6700\u5c0f\u8def\u5f84\u548c\u201d\u6765\u4e3e\u4f8b\u3002
Question
\u7ed9\u5b9a\u4e00\u4e2a \\(n \\times m\\) \u7684\u4e8c\u7ef4\u7f51\u683c grid
\uff0c\u7f51\u683c\u4e2d\u7684\u6bcf\u4e2a\u5355\u5143\u683c\u5305\u542b\u4e00\u4e2a\u975e\u8d1f\u6574\u6570\uff0c\u8868\u793a\u8be5\u5355\u5143\u683c\u7684\u4ee3\u4ef7\u3002\u673a\u5668\u4eba\u4ee5\u5de6\u4e0a\u89d2\u5355\u5143\u683c\u4e3a\u8d77\u59cb\u70b9\uff0c\u6bcf\u6b21\u53ea\u80fd\u5411\u4e0b\u6216\u8005\u5411\u53f3\u79fb\u52a8\u4e00\u6b65\uff0c\u76f4\u81f3\u5230\u8fbe\u53f3\u4e0b\u89d2\u5355\u5143\u683c\u3002\u8bf7\u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230\u53f3\u4e0b\u89d2\u7684\u6700\u5c0f\u8def\u5f84\u548c\u3002
\u56fe 14-10 \u5c55\u793a\u4e86\u4e00\u4e2a\u4f8b\u5b50\uff0c\u7ed9\u5b9a\u7f51\u683c\u7684\u6700\u5c0f\u8def\u5f84\u548c\u4e3a \\(13\\) \u3002
\u56fe 14-10 \u00a0 \u6700\u5c0f\u8def\u5f84\u548c\u793a\u4f8b\u6570\u636e
\u7b2c\u4e00\u6b65\uff1a\u601d\u8003\u6bcf\u8f6e\u7684\u51b3\u7b56\uff0c\u5b9a\u4e49\u72b6\u6001\uff0c\u4ece\u800c\u5f97\u5230 \\(dp\\) \u8868
\u672c\u9898\u7684\u6bcf\u4e00\u8f6e\u7684\u51b3\u7b56\u5c31\u662f\u4ece\u5f53\u524d\u683c\u5b50\u5411\u4e0b\u6216\u5411\u53f3\u8d70\u4e00\u6b65\u3002\u8bbe\u5f53\u524d\u683c\u5b50\u7684\u884c\u5217\u7d22\u5f15\u4e3a \\([i, j]\\) \uff0c\u5219\u5411\u4e0b\u6216\u5411\u53f3\u8d70\u4e00\u6b65\u540e\uff0c\u7d22\u5f15\u53d8\u4e3a \\([i+1, j]\\) \u6216 \\([i, j+1]\\) \u3002\u56e0\u6b64\uff0c\u72b6\u6001\u5e94\u5305\u542b\u884c\u7d22\u5f15\u548c\u5217\u7d22\u5f15\u4e24\u4e2a\u53d8\u91cf\uff0c\u8bb0\u4e3a \\([i, j]\\) \u3002
\u72b6\u6001 \\([i, j]\\) \u5bf9\u5e94\u7684\u5b50\u95ee\u9898\u4e3a\uff1a\u4ece\u8d77\u59cb\u70b9 \\([0, 0]\\) \u8d70\u5230 \\([i, j]\\) \u7684\u6700\u5c0f\u8def\u5f84\u548c\uff0c\u89e3\u8bb0\u4e3a \\(dp[i, j]\\) \u3002
\u81f3\u6b64\uff0c\u6211\u4eec\u5c31\u5f97\u5230\u4e86\u56fe 14-11 \u6240\u793a\u7684\u4e8c\u7ef4 \\(dp\\) \u77e9\u9635\uff0c\u5176\u5c3a\u5bf8\u4e0e\u8f93\u5165\u7f51\u683c \\(grid\\) \u76f8\u540c\u3002
\u56fe 14-11 \u00a0 \u72b6\u6001\u5b9a\u4e49\u4e0e dp \u8868
Note
\u52a8\u6001\u89c4\u5212\u548c\u56de\u6eaf\u8fc7\u7a0b\u53ef\u4ee5\u63cf\u8ff0\u4e3a\u4e00\u4e2a\u51b3\u7b56\u5e8f\u5217\uff0c\u800c\u72b6\u6001\u7531\u6240\u6709\u51b3\u7b56\u53d8\u91cf\u6784\u6210\u3002\u5b83\u5e94\u5f53\u5305\u542b\u63cf\u8ff0\u89e3\u9898\u8fdb\u5ea6\u7684\u6240\u6709\u53d8\u91cf\uff0c\u5176\u5305\u542b\u4e86\u8db3\u591f\u7684\u4fe1\u606f\uff0c\u80fd\u591f\u7528\u6765\u63a8\u5bfc\u51fa\u4e0b\u4e00\u4e2a\u72b6\u6001\u3002
\u6bcf\u4e2a\u72b6\u6001\u90fd\u5bf9\u5e94\u4e00\u4e2a\u5b50\u95ee\u9898\uff0c\u6211\u4eec\u4f1a\u5b9a\u4e49\u4e00\u4e2a \\(dp\\) \u8868\u6765\u5b58\u50a8\u6240\u6709\u5b50\u95ee\u9898\u7684\u89e3\uff0c\u72b6\u6001\u7684\u6bcf\u4e2a\u72ec\u7acb\u53d8\u91cf\u90fd\u662f \\(dp\\) \u8868\u7684\u4e00\u4e2a\u7ef4\u5ea6\u3002\u4ece\u672c\u8d28\u4e0a\u770b\uff0c\\(dp\\) \u8868\u662f\u72b6\u6001\u548c\u5b50\u95ee\u9898\u7684\u89e3\u4e4b\u95f4\u7684\u6620\u5c04\u3002
\u7b2c\u4e8c\u6b65\uff1a\u627e\u51fa\u6700\u4f18\u5b50\u7ed3\u6784\uff0c\u8fdb\u800c\u63a8\u5bfc\u51fa\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b
\u5bf9\u4e8e\u72b6\u6001 \\([i, j]\\) \uff0c\u5b83\u53ea\u80fd\u4ece\u4e0a\u8fb9\u683c\u5b50 \\([i-1, j]\\) \u548c\u5de6\u8fb9\u683c\u5b50 \\([i, j-1]\\) \u8f6c\u79fb\u800c\u6765\u3002\u56e0\u6b64\u6700\u4f18\u5b50\u7ed3\u6784\u4e3a\uff1a\u5230\u8fbe \\([i, j]\\) \u7684\u6700\u5c0f\u8def\u5f84\u548c\u7531 \\([i, j-1]\\) \u7684\u6700\u5c0f\u8def\u5f84\u548c\u4e0e \\([i-1, j]\\) \u7684\u6700\u5c0f\u8def\u5f84\u548c\u4e2d\u8f83\u5c0f\u7684\u90a3\u4e00\u4e2a\u51b3\u5b9a\u3002
\u6839\u636e\u4ee5\u4e0a\u5206\u6790\uff0c\u53ef\u63a8\u51fa\u56fe 14-12 \u6240\u793a\u7684\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\uff1a
\\[ dp[i, j] = \\min(dp[i-1, j], dp[i, j-1]) + grid[i, j] \\]\u56fe 14-12 \u00a0 \u6700\u4f18\u5b50\u7ed3\u6784\u4e0e\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b
Note
\u6839\u636e\u5b9a\u4e49\u597d\u7684 \\(dp\\) \u8868\uff0c\u601d\u8003\u539f\u95ee\u9898\u548c\u5b50\u95ee\u9898\u7684\u5173\u7cfb\uff0c\u627e\u51fa\u901a\u8fc7\u5b50\u95ee\u9898\u7684\u6700\u4f18\u89e3\u6765\u6784\u9020\u539f\u95ee\u9898\u7684\u6700\u4f18\u89e3\u7684\u65b9\u6cd5\uff0c\u5373\u6700\u4f18\u5b50\u7ed3\u6784\u3002
\u4e00\u65e6\u6211\u4eec\u627e\u5230\u4e86\u6700\u4f18\u5b50\u7ed3\u6784\uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u6784\u5efa\u51fa\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u3002
\u7b2c\u4e09\u6b65\uff1a\u786e\u5b9a\u8fb9\u754c\u6761\u4ef6\u548c\u72b6\u6001\u8f6c\u79fb\u987a\u5e8f
\u5728\u672c\u9898\u4e2d\uff0c\u5904\u5728\u9996\u884c\u7684\u72b6\u6001\u53ea\u80fd\u4ece\u5176\u5de6\u8fb9\u7684\u72b6\u6001\u5f97\u6765\uff0c\u5904\u5728\u9996\u5217\u7684\u72b6\u6001\u53ea\u80fd\u4ece\u5176\u4e0a\u8fb9\u7684\u72b6\u6001\u5f97\u6765\uff0c\u56e0\u6b64\u9996\u884c \\(i = 0\\) \u548c\u9996\u5217 \\(j = 0\\) \u662f\u8fb9\u754c\u6761\u4ef6\u3002
\u5982\u56fe 14-13 \u6240\u793a\uff0c\u7531\u4e8e\u6bcf\u4e2a\u683c\u5b50\u662f\u7531\u5176\u5de6\u65b9\u683c\u5b50\u548c\u4e0a\u65b9\u683c\u5b50\u8f6c\u79fb\u800c\u6765\uff0c\u56e0\u6b64\u6211\u4eec\u4f7f\u7528\u5faa\u73af\u6765\u904d\u5386\u77e9\u9635\uff0c\u5916\u5faa\u73af\u904d\u5386\u5404\u884c\uff0c\u5185\u5faa\u73af\u904d\u5386\u5404\u5217\u3002
\u56fe 14-13 \u00a0 \u8fb9\u754c\u6761\u4ef6\u4e0e\u72b6\u6001\u8f6c\u79fb\u987a\u5e8f
Note
\u8fb9\u754c\u6761\u4ef6\u5728\u52a8\u6001\u89c4\u5212\u4e2d\u7528\u4e8e\u521d\u59cb\u5316 \\(dp\\) \u8868\uff0c\u5728\u641c\u7d22\u4e2d\u7528\u4e8e\u526a\u679d\u3002
\u72b6\u6001\u8f6c\u79fb\u987a\u5e8f\u7684\u6838\u5fc3\u662f\u8981\u4fdd\u8bc1\u5728\u8ba1\u7b97\u5f53\u524d\u95ee\u9898\u7684\u89e3\u65f6\uff0c\u6240\u6709\u5b83\u4f9d\u8d56\u7684\u66f4\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\u90fd\u5df2\u7ecf\u88ab\u6b63\u786e\u5730\u8ba1\u7b97\u51fa\u6765\u3002
\u6839\u636e\u4ee5\u4e0a\u5206\u6790\uff0c\u6211\u4eec\u5df2\u7ecf\u53ef\u4ee5\u76f4\u63a5\u5199\u51fa\u52a8\u6001\u89c4\u5212\u4ee3\u7801\u3002\u7136\u800c\u5b50\u95ee\u9898\u5206\u89e3\u662f\u4e00\u79cd\u4ece\u9876\u81f3\u5e95\u7684\u601d\u60f3\uff0c\u56e0\u6b64\u6309\u7167\u201c\u66b4\u529b\u641c\u7d22 \\(\\rightarrow\\) \u8bb0\u5fc6\u5316\u641c\u7d22 \\(\\rightarrow\\) \u52a8\u6001\u89c4\u5212\u201d\u7684\u987a\u5e8f\u5b9e\u73b0\u66f4\u52a0\u7b26\u5408\u601d\u7ef4\u4e60\u60ef\u3002
"},{"location":"chapter_dynamic_programming/dp_solution_pipeline/#1","title":"1. \u00a0 \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u641c\u7d22","text":"\u4ece\u72b6\u6001 \\([i, j]\\) \u5f00\u59cb\u641c\u7d22\uff0c\u4e0d\u65ad\u5206\u89e3\u4e3a\u66f4\u5c0f\u7684\u72b6\u6001 \\([i-1, j]\\) \u548c \\([i, j-1]\\) \uff0c\u9012\u5f52\u51fd\u6570\u5305\u62ec\u4ee5\u4e0b\u8981\u7d20\u3002
\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig min_path_sum.pydef min_path_sum_dfs(grid: list[list[int]], i: int, j: int) -> int:\n \"\"\"\u6700\u5c0f\u8def\u5f84\u548c\uff1a\u66b4\u529b\u641c\u7d22\"\"\"\n # \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n if i == 0 and j == 0:\n return grid[0][0]\n # \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n if i < 0 or j < 0:\n return inf\n # \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n up = min_path_sum_dfs(grid, i - 1, j)\n left = min_path_sum_dfs(grid, i, j - 1)\n # \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n return min(left, up) + grid[i][j]\n
min_path_sum.cpp/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u66b4\u529b\u641c\u7d22 */\nint minPathSumDFS(vector<vector<int>> &grid, int i, int j) {\n // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n if (i == 0 && j == 0) {\n return grid[0][0];\n }\n // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n if (i < 0 || j < 0) {\n return INT_MAX;\n }\n // \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n int up = minPathSumDFS(grid, i - 1, j);\n int left = minPathSumDFS(grid, i, j - 1);\n // \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n return min(left, up) != INT_MAX ? min(left, up) + grid[i][j] : INT_MAX;\n}\n
min_path_sum.java/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u66b4\u529b\u641c\u7d22 */\nint minPathSumDFS(int[][] grid, int i, int j) {\n // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n if (i == 0 && j == 0) {\n return grid[0][0];\n }\n // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n if (i < 0 || j < 0) {\n return Integer.MAX_VALUE;\n }\n // \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n int up = minPathSumDFS(grid, i - 1, j);\n int left = minPathSumDFS(grid, i, j - 1);\n // \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n return Math.min(left, up) + grid[i][j];\n}\n
min_path_sum.cs/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u66b4\u529b\u641c\u7d22 */\nint MinPathSumDFS(int[][] grid, int i, int j) {\n // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n if (i == 0 && j == 0) {\n return grid[0][0];\n }\n // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n if (i < 0 || j < 0) {\n return int.MaxValue;\n }\n // \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n int up = MinPathSumDFS(grid, i - 1, j);\n int left = MinPathSumDFS(grid, i, j - 1);\n // \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n return Math.Min(left, up) + grid[i][j];\n}\n
min_path_sum.go/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u66b4\u529b\u641c\u7d22 */\nfunc minPathSumDFS(grid [][]int, i, j int) int {\n // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n if i == 0 && j == 0 {\n return grid[0][0]\n }\n // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n if i < 0 || j < 0 {\n return math.MaxInt\n }\n // \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n up := minPathSumDFS(grid, i-1, j)\n left := minPathSumDFS(grid, i, j-1)\n // \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n return int(math.Min(float64(left), float64(up))) + grid[i][j]\n}\n
min_path_sum.swift/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u66b4\u529b\u641c\u7d22 */\nfunc minPathSumDFS(grid: [[Int]], i: Int, j: Int) -> Int {\n // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n if i == 0, j == 0 {\n return grid[0][0]\n }\n // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n if i < 0 || j < 0 {\n return .max\n }\n // \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n let up = minPathSumDFS(grid: grid, i: i - 1, j: j)\n let left = minPathSumDFS(grid: grid, i: i, j: j - 1)\n // \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n return min(left, up) + grid[i][j]\n}\n
min_path_sum.js/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u66b4\u529b\u641c\u7d22 */\nfunction minPathSumDFS(grid, i, j) {\n // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n if (i === 0 && j === 0) {\n return grid[0][0];\n }\n // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n if (i < 0 || j < 0) {\n return Infinity;\n }\n // \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n const up = minPathSumDFS(grid, i - 1, j);\n const left = minPathSumDFS(grid, i, j - 1);\n // \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n return Math.min(left, up) + grid[i][j];\n}\n
min_path_sum.ts/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u66b4\u529b\u641c\u7d22 */\nfunction minPathSumDFS(\n grid: Array<Array<number>>,\n i: number,\n j: number\n): number {\n // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n if (i === 0 && j == 0) {\n return grid[0][0];\n }\n // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n if (i < 0 || j < 0) {\n return Infinity;\n }\n // \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n const up = minPathSumDFS(grid, i - 1, j);\n const left = minPathSumDFS(grid, i, j - 1);\n // \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n return Math.min(left, up) + grid[i][j];\n}\n
min_path_sum.dart/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u66b4\u529b\u641c\u7d22 */\nint minPathSumDFS(List<List<int>> grid, int i, int j) {\n // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n if (i == 0 && j == 0) {\n return grid[0][0];\n }\n // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n if (i < 0 || j < 0) {\n // \u5728 Dart \u4e2d\uff0cint \u7c7b\u578b\u662f\u56fa\u5b9a\u8303\u56f4\u7684\u6574\u6570\uff0c\u4e0d\u5b58\u5728\u8868\u793a\u201c\u65e0\u7a77\u5927\u201d\u7684\u503c\n return BigInt.from(2).pow(31).toInt();\n }\n // \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n int up = minPathSumDFS(grid, i - 1, j);\n int left = minPathSumDFS(grid, i, j - 1);\n // \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n return min(left, up) + grid[i][j];\n}\n
min_path_sum.rs/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u66b4\u529b\u641c\u7d22 */\nfn min_path_sum_dfs(grid: &Vec<Vec<i32>>, i: i32, j: i32) -> i32 {\n // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n if i == 0 && j == 0 {\n return grid[0][0];\n }\n // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n if i < 0 || j < 0 {\n return i32::MAX;\n }\n // \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n let up = min_path_sum_dfs(grid, i - 1, j);\n let left = min_path_sum_dfs(grid, i, j - 1);\n // \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n std::cmp::min(left, up) + grid[i as usize][j as usize]\n}\n
min_path_sum.c/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u66b4\u529b\u641c\u7d22 */\nint minPathSumDFS(int grid[MAX_SIZE][MAX_SIZE], int i, int j) {\n // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n if (i == 0 && j == 0) {\n return grid[0][0];\n }\n // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n if (i < 0 || j < 0) {\n return INT_MAX;\n }\n // \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n int up = minPathSumDFS(grid, i - 1, j);\n int left = minPathSumDFS(grid, i, j - 1);\n // \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n return myMin(left, up) != INT_MAX ? myMin(left, up) + grid[i][j] : INT_MAX;\n}\n
min_path_sum.zig// \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u66b4\u529b\u641c\u7d22\nfn minPathSumDFS(grid: anytype, i: i32, j: i32) i32 {\n // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n if (i == 0 and j == 0) {\n return grid[0][0];\n }\n // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n if (i < 0 or j < 0) {\n return std.math.maxInt(i32);\n }\n // \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n var up = minPathSumDFS(grid, i - 1, j);\n var left = minPathSumDFS(grid, i, j - 1);\n // \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n return @min(left, up) + grid[@as(usize, @intCast(i))][@as(usize, @intCast(j))];\n}\n
\u56fe 14-14 \u7ed9\u51fa\u4e86\u4ee5 \\(dp[2, 1]\\) \u4e3a\u6839\u8282\u70b9\u7684\u9012\u5f52\u6811\uff0c\u5176\u4e2d\u5305\u542b\u4e00\u4e9b\u91cd\u53e0\u5b50\u95ee\u9898\uff0c\u5176\u6570\u91cf\u4f1a\u968f\u7740\u7f51\u683c grid
\u7684\u5c3a\u5bf8\u53d8\u5927\u800c\u6025\u5267\u589e\u591a\u3002
\u4ece\u672c\u8d28\u4e0a\u770b\uff0c\u9020\u6210\u91cd\u53e0\u5b50\u95ee\u9898\u7684\u539f\u56e0\u4e3a\uff1a\u5b58\u5728\u591a\u6761\u8def\u5f84\u53ef\u4ee5\u4ece\u5de6\u4e0a\u89d2\u5230\u8fbe\u67d0\u4e00\u5355\u5143\u683c\u3002
\u56fe 14-14 \u00a0 \u66b4\u529b\u641c\u7d22\u9012\u5f52\u6811
\u6bcf\u4e2a\u72b6\u6001\u90fd\u6709\u5411\u4e0b\u548c\u5411\u53f3\u4e24\u79cd\u9009\u62e9\uff0c\u4ece\u5de6\u4e0a\u89d2\u8d70\u5230\u53f3\u4e0b\u89d2\u603b\u5171\u9700\u8981 \\(m + n - 2\\) \u6b65\uff0c\u6240\u4ee5\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(2^{m + n})\\) \u3002\u8bf7\u6ce8\u610f\uff0c\u8fd9\u79cd\u8ba1\u7b97\u65b9\u5f0f\u672a\u8003\u8651\u4e34\u8fd1\u7f51\u683c\u8fb9\u754c\u7684\u60c5\u51b5\uff0c\u5f53\u5230\u8fbe\u7f51\u7edc\u8fb9\u754c\u65f6\u53ea\u5269\u4e0b\u4e00\u79cd\u9009\u62e9\uff0c\u56e0\u6b64\u5b9e\u9645\u7684\u8def\u5f84\u6570\u91cf\u4f1a\u5c11\u4e00\u4e9b\u3002
"},{"location":"chapter_dynamic_programming/dp_solution_pipeline/#2","title":"2. \u00a0 \u65b9\u6cd5\u4e8c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22","text":"\u6211\u4eec\u5f15\u5165\u4e00\u4e2a\u548c\u7f51\u683c grid
\u76f8\u540c\u5c3a\u5bf8\u7684\u8bb0\u5fc6\u5217\u8868 mem
\uff0c\u7528\u4e8e\u8bb0\u5f55\u5404\u4e2a\u5b50\u95ee\u9898\u7684\u89e3\uff0c\u5e76\u5c06\u91cd\u53e0\u5b50\u95ee\u9898\u8fdb\u884c\u526a\u679d\uff1a
def min_path_sum_dfs_mem(\n grid: list[list[int]], mem: list[list[int]], i: int, j: int\n) -> int:\n \"\"\"\u6700\u5c0f\u8def\u5f84\u548c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22\"\"\"\n # \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n if i == 0 and j == 0:\n return grid[0][0]\n # \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n if i < 0 or j < 0:\n return inf\n # \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if mem[i][j] != -1:\n return mem[i][j]\n # \u5de6\u8fb9\u548c\u4e0a\u8fb9\u5355\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n up = min_path_sum_dfs_mem(grid, mem, i - 1, j)\n left = min_path_sum_dfs_mem(grid, mem, i, j - 1)\n # \u8bb0\u5f55\u5e76\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n mem[i][j] = min(left, up) + grid[i][j]\n return mem[i][j]\n
min_path_sum.cpp/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint minPathSumDFSMem(vector<vector<int>> &grid, vector<vector<int>> &mem, int i, int j) {\n // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n if (i == 0 && j == 0) {\n return grid[0][0];\n }\n // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n if (i < 0 || j < 0) {\n return INT_MAX;\n }\n // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if (mem[i][j] != -1) {\n return mem[i][j];\n }\n // \u5de6\u8fb9\u548c\u4e0a\u8fb9\u5355\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n int up = minPathSumDFSMem(grid, mem, i - 1, j);\n int left = minPathSumDFSMem(grid, mem, i, j - 1);\n // \u8bb0\u5f55\u5e76\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n mem[i][j] = min(left, up) != INT_MAX ? min(left, up) + grid[i][j] : INT_MAX;\n return mem[i][j];\n}\n
min_path_sum.java/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint minPathSumDFSMem(int[][] grid, int[][] mem, int i, int j) {\n // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n if (i == 0 && j == 0) {\n return grid[0][0];\n }\n // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n if (i < 0 || j < 0) {\n return Integer.MAX_VALUE;\n }\n // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if (mem[i][j] != -1) {\n return mem[i][j];\n }\n // \u5de6\u8fb9\u548c\u4e0a\u8fb9\u5355\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n int up = minPathSumDFSMem(grid, mem, i - 1, j);\n int left = minPathSumDFSMem(grid, mem, i, j - 1);\n // \u8bb0\u5f55\u5e76\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n mem[i][j] = Math.min(left, up) + grid[i][j];\n return mem[i][j];\n}\n
min_path_sum.cs/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint MinPathSumDFSMem(int[][] grid, int[][] mem, int i, int j) {\n // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n if (i == 0 && j == 0) {\n return grid[0][0];\n }\n // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n if (i < 0 || j < 0) {\n return int.MaxValue;\n }\n // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if (mem[i][j] != -1) {\n return mem[i][j];\n }\n // \u5de6\u8fb9\u548c\u4e0a\u8fb9\u5355\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n int up = MinPathSumDFSMem(grid, mem, i - 1, j);\n int left = MinPathSumDFSMem(grid, mem, i, j - 1);\n // \u8bb0\u5f55\u5e76\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n mem[i][j] = Math.Min(left, up) + grid[i][j];\n return mem[i][j];\n}\n
min_path_sum.go/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunc minPathSumDFSMem(grid, mem [][]int, i, j int) int {\n // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n if i == 0 && j == 0 {\n return grid[0][0]\n }\n // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n if i < 0 || j < 0 {\n return math.MaxInt\n }\n // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if mem[i][j] != -1 {\n return mem[i][j]\n }\n // \u5de6\u8fb9\u548c\u4e0a\u8fb9\u5355\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n up := minPathSumDFSMem(grid, mem, i-1, j)\n left := minPathSumDFSMem(grid, mem, i, j-1)\n // \u8bb0\u5f55\u5e76\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n mem[i][j] = int(math.Min(float64(left), float64(up))) + grid[i][j]\n return mem[i][j]\n}\n
min_path_sum.swift/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunc minPathSumDFSMem(grid: [[Int]], mem: inout [[Int]], i: Int, j: Int) -> Int {\n // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n if i == 0, j == 0 {\n return grid[0][0]\n }\n // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n if i < 0 || j < 0 {\n return .max\n }\n // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if mem[i][j] != -1 {\n return mem[i][j]\n }\n // \u5de6\u8fb9\u548c\u4e0a\u8fb9\u5355\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n let up = minPathSumDFSMem(grid: grid, mem: &mem, i: i - 1, j: j)\n let left = minPathSumDFSMem(grid: grid, mem: &mem, i: i, j: j - 1)\n // \u8bb0\u5f55\u5e76\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n mem[i][j] = min(left, up) + grid[i][j]\n return mem[i][j]\n}\n
min_path_sum.js/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunction minPathSumDFSMem(grid, mem, i, j) {\n // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n if (i === 0 && j === 0) {\n return grid[0][0];\n }\n // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n if (i < 0 || j < 0) {\n return Infinity;\n }\n // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if (mem[i][j] !== -1) {\n return mem[i][j];\n }\n // \u5de6\u8fb9\u548c\u4e0a\u8fb9\u5355\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n const up = minPathSumDFSMem(grid, mem, i - 1, j);\n const left = minPathSumDFSMem(grid, mem, i, j - 1);\n // \u8bb0\u5f55\u5e76\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n mem[i][j] = Math.min(left, up) + grid[i][j];\n return mem[i][j];\n}\n
min_path_sum.ts/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunction minPathSumDFSMem(\n grid: Array<Array<number>>,\n mem: Array<Array<number>>,\n i: number,\n j: number\n): number {\n // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n if (i === 0 && j === 0) {\n return grid[0][0];\n }\n // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n if (i < 0 || j < 0) {\n return Infinity;\n }\n // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if (mem[i][j] != -1) {\n return mem[i][j];\n }\n // \u5de6\u8fb9\u548c\u4e0a\u8fb9\u5355\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n const up = minPathSumDFSMem(grid, mem, i - 1, j);\n const left = minPathSumDFSMem(grid, mem, i, j - 1);\n // \u8bb0\u5f55\u5e76\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n mem[i][j] = Math.min(left, up) + grid[i][j];\n return mem[i][j];\n}\n
min_path_sum.dart/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint minPathSumDFSMem(List<List<int>> grid, List<List<int>> mem, int i, int j) {\n // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n if (i == 0 && j == 0) {\n return grid[0][0];\n }\n // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n if (i < 0 || j < 0) {\n // \u5728 Dart \u4e2d\uff0cint \u7c7b\u578b\u662f\u56fa\u5b9a\u8303\u56f4\u7684\u6574\u6570\uff0c\u4e0d\u5b58\u5728\u8868\u793a\u201c\u65e0\u7a77\u5927\u201d\u7684\u503c\n return BigInt.from(2).pow(31).toInt();\n }\n // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if (mem[i][j] != -1) {\n return mem[i][j];\n }\n // \u5de6\u8fb9\u548c\u4e0a\u8fb9\u5355\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n int up = minPathSumDFSMem(grid, mem, i - 1, j);\n int left = minPathSumDFSMem(grid, mem, i, j - 1);\n // \u8bb0\u5f55\u5e76\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n mem[i][j] = min(left, up) + grid[i][j];\n return mem[i][j];\n}\n
min_path_sum.rs/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfn min_path_sum_dfs_mem(grid: &Vec<Vec<i32>>, mem: &mut Vec<Vec<i32>>, i: i32, j: i32) -> i32 {\n // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n if i == 0 && j == 0 {\n return grid[0][0];\n }\n // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n if i < 0 || j < 0 {\n return i32::MAX;\n }\n // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if mem[i as usize][j as usize] != -1 {\n return mem[i as usize][j as usize];\n }\n // \u5de6\u8fb9\u548c\u4e0a\u8fb9\u5355\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n let up = min_path_sum_dfs_mem(grid, mem, i - 1, j);\n let left = min_path_sum_dfs_mem(grid, mem, i, j - 1);\n // \u8bb0\u5f55\u5e76\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n mem[i as usize][j as usize] = std::cmp::min(left, up) + grid[i as usize][j as usize];\n mem[i as usize][j as usize]\n}\n
min_path_sum.c/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint minPathSumDFSMem(int grid[MAX_SIZE][MAX_SIZE], int mem[MAX_SIZE][MAX_SIZE], int i, int j) {\n // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n if (i == 0 && j == 0) {\n return grid[0][0];\n }\n // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n if (i < 0 || j < 0) {\n return INT_MAX;\n }\n // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if (mem[i][j] != -1) {\n return mem[i][j];\n }\n // \u5de6\u8fb9\u548c\u4e0a\u8fb9\u5355\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n int up = minPathSumDFSMem(grid, mem, i - 1, j);\n int left = minPathSumDFSMem(grid, mem, i, j - 1);\n // \u8bb0\u5f55\u5e76\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n mem[i][j] = myMin(left, up) != INT_MAX ? myMin(left, up) + grid[i][j] : INT_MAX;\n return mem[i][j];\n}\n
min_path_sum.zig// \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22\nfn minPathSumDFSMem(grid: anytype, mem: anytype, i: i32, j: i32) i32 {\n // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n if (i == 0 and j == 0) {\n return grid[0][0];\n }\n // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n if (i < 0 or j < 0) {\n return std.math.maxInt(i32);\n }\n // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if (mem[@as(usize, @intCast(i))][@as(usize, @intCast(j))] != -1) {\n return mem[@as(usize, @intCast(i))][@as(usize, @intCast(j))];\n }\n // \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n var up = minPathSumDFSMem(grid, mem, i - 1, j);\n var left = minPathSumDFSMem(grid, mem, i, j - 1);\n // \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n // \u8bb0\u5f55\u5e76\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n mem[@as(usize, @intCast(i))][@as(usize, @intCast(j))] = @min(left, up) + grid[@as(usize, @intCast(i))][@as(usize, @intCast(j))];\n return mem[@as(usize, @intCast(i))][@as(usize, @intCast(j))];\n}\n
\u5982\u56fe 14-15 \u6240\u793a\uff0c\u5728\u5f15\u5165\u8bb0\u5fc6\u5316\u540e\uff0c\u6240\u6709\u5b50\u95ee\u9898\u7684\u89e3\u53ea\u9700\u8ba1\u7b97\u4e00\u6b21\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u53d6\u51b3\u4e8e\u72b6\u6001\u603b\u6570\uff0c\u5373\u7f51\u683c\u5c3a\u5bf8 \\(O(nm)\\) \u3002
\u56fe 14-15 \u00a0 \u8bb0\u5fc6\u5316\u641c\u7d22\u9012\u5f52\u6811
"},{"location":"chapter_dynamic_programming/dp_solution_pipeline/#3","title":"3. \u00a0 \u65b9\u6cd5\u4e09\uff1a\u52a8\u6001\u89c4\u5212","text":"\u57fa\u4e8e\u8fed\u4ee3\u5b9e\u73b0\u52a8\u6001\u89c4\u5212\u89e3\u6cd5\uff0c\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig min_path_sum.pydef min_path_sum_dp(grid: list[list[int]]) -> int:\n \"\"\"\u6700\u5c0f\u8def\u5f84\u548c\uff1a\u52a8\u6001\u89c4\u5212\"\"\"\n n, m = len(grid), len(grid[0])\n # \u521d\u59cb\u5316 dp \u8868\n dp = [[0] * m for _ in range(n)]\n dp[0][0] = grid[0][0]\n # \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n for j in range(1, m):\n dp[0][j] = dp[0][j - 1] + grid[0][j]\n # \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n for i in range(1, n):\n dp[i][0] = dp[i - 1][0] + grid[i][0]\n # \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n for i in range(1, n):\n for j in range(1, m):\n dp[i][j] = min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j]\n return dp[n - 1][m - 1]\n
min_path_sum.cpp/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u52a8\u6001\u89c4\u5212 */\nint minPathSumDP(vector<vector<int>> &grid) {\n int n = grid.size(), m = grid[0].size();\n // \u521d\u59cb\u5316 dp \u8868\n vector<vector<int>> dp(n, vector<int>(m));\n dp[0][0] = grid[0][0];\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n for (int j = 1; j < m; j++) {\n dp[0][j] = dp[0][j - 1] + grid[0][j];\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n for (int i = 1; i < n; i++) {\n dp[i][0] = dp[i - 1][0] + grid[i][0];\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n for (int i = 1; i < n; i++) {\n for (int j = 1; j < m; j++) {\n dp[i][j] = min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];\n }\n }\n return dp[n - 1][m - 1];\n}\n
min_path_sum.java/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u52a8\u6001\u89c4\u5212 */\nint minPathSumDP(int[][] grid) {\n int n = grid.length, m = grid[0].length;\n // \u521d\u59cb\u5316 dp \u8868\n int[][] dp = new int[n][m];\n dp[0][0] = grid[0][0];\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n for (int j = 1; j < m; j++) {\n dp[0][j] = dp[0][j - 1] + grid[0][j];\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n for (int i = 1; i < n; i++) {\n dp[i][0] = dp[i - 1][0] + grid[i][0];\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n for (int i = 1; i < n; i++) {\n for (int j = 1; j < m; j++) {\n dp[i][j] = Math.min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];\n }\n }\n return dp[n - 1][m - 1];\n}\n
min_path_sum.cs/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u52a8\u6001\u89c4\u5212 */\nint MinPathSumDP(int[][] grid) {\n int n = grid.Length, m = grid[0].Length;\n // \u521d\u59cb\u5316 dp \u8868\n int[,] dp = new int[n, m];\n dp[0, 0] = grid[0][0];\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n for (int j = 1; j < m; j++) {\n dp[0, j] = dp[0, j - 1] + grid[0][j];\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n for (int i = 1; i < n; i++) {\n dp[i, 0] = dp[i - 1, 0] + grid[i][0];\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n for (int i = 1; i < n; i++) {\n for (int j = 1; j < m; j++) {\n dp[i, j] = Math.Min(dp[i, j - 1], dp[i - 1, j]) + grid[i][j];\n }\n }\n return dp[n - 1, m - 1];\n}\n
min_path_sum.go/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc minPathSumDP(grid [][]int) int {\n n, m := len(grid), len(grid[0])\n // \u521d\u59cb\u5316 dp \u8868\n dp := make([][]int, n)\n for i := 0; i < n; i++ {\n dp[i] = make([]int, m)\n }\n dp[0][0] = grid[0][0]\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n for j := 1; j < m; j++ {\n dp[0][j] = dp[0][j-1] + grid[0][j]\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n for i := 1; i < n; i++ {\n dp[i][0] = dp[i-1][0] + grid[i][0]\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n for i := 1; i < n; i++ {\n for j := 1; j < m; j++ {\n dp[i][j] = int(math.Min(float64(dp[i][j-1]), float64(dp[i-1][j]))) + grid[i][j]\n }\n }\n return dp[n-1][m-1]\n}\n
min_path_sum.swift/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc minPathSumDP(grid: [[Int]]) -> Int {\n let n = grid.count\n let m = grid[0].count\n // \u521d\u59cb\u5316 dp \u8868\n var dp = Array(repeating: Array(repeating: 0, count: m), count: n)\n dp[0][0] = grid[0][0]\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n for j in stride(from: 1, to: m, by: 1) {\n dp[0][j] = dp[0][j - 1] + grid[0][j]\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n for i in stride(from: 1, to: n, by: 1) {\n dp[i][0] = dp[i - 1][0] + grid[i][0]\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n for i in stride(from: 1, to: n, by: 1) {\n for j in stride(from: 1, to: m, by: 1) {\n dp[i][j] = min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j]\n }\n }\n return dp[n - 1][m - 1]\n}\n
min_path_sum.js/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction minPathSumDP(grid) {\n const n = grid.length,\n m = grid[0].length;\n // \u521d\u59cb\u5316 dp \u8868\n const dp = Array.from({ length: n }, () =>\n Array.from({ length: m }, () => 0)\n );\n dp[0][0] = grid[0][0];\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n for (let j = 1; j < m; j++) {\n dp[0][j] = dp[0][j - 1] + grid[0][j];\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n for (let i = 1; i < n; i++) {\n dp[i][0] = dp[i - 1][0] + grid[i][0];\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n for (let i = 1; i < n; i++) {\n for (let j = 1; j < m; j++) {\n dp[i][j] = Math.min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];\n }\n }\n return dp[n - 1][m - 1];\n}\n
min_path_sum.ts/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction minPathSumDP(grid: Array<Array<number>>): number {\n const n = grid.length,\n m = grid[0].length;\n // \u521d\u59cb\u5316 dp \u8868\n const dp = Array.from({ length: n }, () =>\n Array.from({ length: m }, () => 0)\n );\n dp[0][0] = grid[0][0];\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n for (let j = 1; j < m; j++) {\n dp[0][j] = dp[0][j - 1] + grid[0][j];\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n for (let i = 1; i < n; i++) {\n dp[i][0] = dp[i - 1][0] + grid[i][0];\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n for (let i = 1; i < n; i++) {\n for (let j: number = 1; j < m; j++) {\n dp[i][j] = Math.min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];\n }\n }\n return dp[n - 1][m - 1];\n}\n
min_path_sum.dart/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u52a8\u6001\u89c4\u5212 */\nint minPathSumDP(List<List<int>> grid) {\n int n = grid.length, m = grid[0].length;\n // \u521d\u59cb\u5316 dp \u8868\n List<List<int>> dp = List.generate(n, (i) => List.filled(m, 0));\n dp[0][0] = grid[0][0];\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n for (int j = 1; j < m; j++) {\n dp[0][j] = dp[0][j - 1] + grid[0][j];\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n for (int i = 1; i < n; i++) {\n dp[i][0] = dp[i - 1][0] + grid[i][0];\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n for (int i = 1; i < n; i++) {\n for (int j = 1; j < m; j++) {\n dp[i][j] = min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];\n }\n }\n return dp[n - 1][m - 1];\n}\n
min_path_sum.rs/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u52a8\u6001\u89c4\u5212 */\nfn min_path_sum_dp(grid: &Vec<Vec<i32>>) -> i32 {\n let (n, m) = (grid.len(), grid[0].len());\n // \u521d\u59cb\u5316 dp \u8868\n let mut dp = vec![vec![0; m]; n];\n dp[0][0] = grid[0][0];\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n for j in 1..m {\n dp[0][j] = dp[0][j - 1] + grid[0][j];\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n for i in 1..n {\n dp[i][0] = dp[i - 1][0] + grid[i][0];\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n for i in 1..n {\n for j in 1..m {\n dp[i][j] = std::cmp::min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];\n }\n }\n dp[n - 1][m - 1]\n}\n
min_path_sum.c/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u52a8\u6001\u89c4\u5212 */\nint minPathSumDP(int grid[MAX_SIZE][MAX_SIZE], int n, int m) {\n // \u521d\u59cb\u5316 dp \u8868\n int **dp = malloc(n * sizeof(int *));\n for (int i = 0; i < n; i++) {\n dp[i] = calloc(m, sizeof(int));\n }\n dp[0][0] = grid[0][0];\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n for (int j = 1; j < m; j++) {\n dp[0][j] = dp[0][j - 1] + grid[0][j];\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n for (int i = 1; i < n; i++) {\n dp[i][0] = dp[i - 1][0] + grid[i][0];\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n for (int i = 1; i < n; i++) {\n for (int j = 1; j < m; j++) {\n dp[i][j] = myMin(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];\n }\n }\n int res = dp[n - 1][m - 1];\n // \u91ca\u653e\u5185\u5b58\n for (int i = 0; i < n; i++) {\n free(dp[i]);\n }\n return res;\n}\n
min_path_sum.zig// \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u52a8\u6001\u89c4\u5212\nfn minPathSumDP(comptime grid: anytype) i32 {\n comptime var n = grid.len;\n comptime var m = grid[0].len;\n // \u521d\u59cb\u5316 dp \u8868\n var dp = [_][m]i32{[_]i32{0} ** m} ** n;\n dp[0][0] = grid[0][0];\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n for (1..m) |j| {\n dp[0][j] = dp[0][j - 1] + grid[0][j];\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n for (1..n) |i| {\n dp[i][0] = dp[i - 1][0] + grid[i][0];\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n for (1..n) |i| {\n for (1..m) |j| {\n dp[i][j] = @min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];\n }\n }\n return dp[n - 1][m - 1];\n}\n
\u56fe 14-16 \u5c55\u793a\u4e86\u6700\u5c0f\u8def\u5f84\u548c\u7684\u72b6\u6001\u8f6c\u79fb\u8fc7\u7a0b\uff0c\u5176\u904d\u5386\u4e86\u6574\u4e2a\u7f51\u683c\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(nm)\\) \u3002
\u6570\u7ec4 dp
\u5927\u5c0f\u4e3a \\(n \\times m\\) \uff0c\u56e0\u6b64\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(nm)\\) \u3002
\u56fe 14-16 \u00a0 \u6700\u5c0f\u8def\u5f84\u548c\u7684\u52a8\u6001\u89c4\u5212\u8fc7\u7a0b
"},{"location":"chapter_dynamic_programming/dp_solution_pipeline/#4","title":"4. \u00a0 \u7a7a\u95f4\u4f18\u5316","text":"\u7531\u4e8e\u6bcf\u4e2a\u683c\u5b50\u53ea\u4e0e\u5176\u5de6\u8fb9\u548c\u4e0a\u8fb9\u7684\u683c\u5b50\u6709\u5173\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u53ea\u7528\u4e00\u4e2a\u5355\u884c\u6570\u7ec4\u6765\u5b9e\u73b0 \\(dp\\) \u8868\u3002
\u8bf7\u6ce8\u610f\uff0c\u56e0\u4e3a\u6570\u7ec4 dp
\u53ea\u80fd\u8868\u793a\u4e00\u884c\u7684\u72b6\u6001\uff0c\u6240\u4ee5\u6211\u4eec\u65e0\u6cd5\u63d0\u524d\u521d\u59cb\u5316\u9996\u5217\u72b6\u6001\uff0c\u800c\u662f\u5728\u904d\u5386\u6bcf\u884c\u65f6\u66f4\u65b0\u5b83\uff1a
def min_path_sum_dp_comp(grid: list[list[int]]) -> int:\n \"\"\"\u6700\u5c0f\u8def\u5f84\u548c\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\"\"\"\n n, m = len(grid), len(grid[0])\n # \u521d\u59cb\u5316 dp \u8868\n dp = [0] * m\n # \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n dp[0] = grid[0][0]\n for j in range(1, m):\n dp[j] = dp[j - 1] + grid[0][j]\n # \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n for i in range(1, n):\n # \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n dp[0] = dp[0] + grid[i][0]\n # \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n for j in range(1, m):\n dp[j] = min(dp[j - 1], dp[j]) + grid[i][j]\n return dp[m - 1]\n
min_path_sum.cpp/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint minPathSumDPComp(vector<vector<int>> &grid) {\n int n = grid.size(), m = grid[0].size();\n // \u521d\u59cb\u5316 dp \u8868\n vector<int> dp(m);\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n dp[0] = grid[0][0];\n for (int j = 1; j < m; j++) {\n dp[j] = dp[j - 1] + grid[0][j];\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n for (int i = 1; i < n; i++) {\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n dp[0] = dp[0] + grid[i][0];\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n for (int j = 1; j < m; j++) {\n dp[j] = min(dp[j - 1], dp[j]) + grid[i][j];\n }\n }\n return dp[m - 1];\n}\n
min_path_sum.java/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint minPathSumDPComp(int[][] grid) {\n int n = grid.length, m = grid[0].length;\n // \u521d\u59cb\u5316 dp \u8868\n int[] dp = new int[m];\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n dp[0] = grid[0][0];\n for (int j = 1; j < m; j++) {\n dp[j] = dp[j - 1] + grid[0][j];\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n for (int i = 1; i < n; i++) {\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n dp[0] = dp[0] + grid[i][0];\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n for (int j = 1; j < m; j++) {\n dp[j] = Math.min(dp[j - 1], dp[j]) + grid[i][j];\n }\n }\n return dp[m - 1];\n}\n
min_path_sum.cs/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint MinPathSumDPComp(int[][] grid) {\n int n = grid.Length, m = grid[0].Length;\n // \u521d\u59cb\u5316 dp \u8868\n int[] dp = new int[m];\n dp[0] = grid[0][0];\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n for (int j = 1; j < m; j++) {\n dp[j] = dp[j - 1] + grid[0][j];\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n for (int i = 1; i < n; i++) {\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n dp[0] = dp[0] + grid[i][0];\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n for (int j = 1; j < m; j++) {\n dp[j] = Math.Min(dp[j - 1], dp[j]) + grid[i][j];\n }\n }\n return dp[m - 1];\n}\n
min_path_sum.go/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc minPathSumDPComp(grid [][]int) int {\n n, m := len(grid), len(grid[0])\n // \u521d\u59cb\u5316 dp \u8868\n dp := make([]int, m)\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n dp[0] = grid[0][0]\n for j := 1; j < m; j++ {\n dp[j] = dp[j-1] + grid[0][j]\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n for i := 1; i < n; i++ {\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n dp[0] = dp[0] + grid[i][0]\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n for j := 1; j < m; j++ {\n dp[j] = int(math.Min(float64(dp[j-1]), float64(dp[j]))) + grid[i][j]\n }\n }\n return dp[m-1]\n}\n
min_path_sum.swift/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc minPathSumDPComp(grid: [[Int]]) -> Int {\n let n = grid.count\n let m = grid[0].count\n // \u521d\u59cb\u5316 dp \u8868\n var dp = Array(repeating: 0, count: m)\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n dp[0] = grid[0][0]\n for j in stride(from: 1, to: m, by: 1) {\n dp[j] = dp[j - 1] + grid[0][j]\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n for i in stride(from: 1, to: n, by: 1) {\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n dp[0] = dp[0] + grid[i][0]\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n for j in stride(from: 1, to: m, by: 1) {\n dp[j] = min(dp[j - 1], dp[j]) + grid[i][j]\n }\n }\n return dp[m - 1]\n}\n
min_path_sum.js/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction minPathSumDPComp(grid) {\n const n = grid.length,\n m = grid[0].length;\n // \u521d\u59cb\u5316 dp \u8868\n const dp = new Array(m);\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n dp[0] = grid[0][0];\n for (let j = 1; j < m; j++) {\n dp[j] = dp[j - 1] + grid[0][j];\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n for (let i = 1; i < n; i++) {\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n dp[0] = dp[0] + grid[i][0];\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n for (let j = 1; j < m; j++) {\n dp[j] = Math.min(dp[j - 1], dp[j]) + grid[i][j];\n }\n }\n return dp[m - 1];\n}\n
min_path_sum.ts/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction minPathSumDPComp(grid: Array<Array<number>>): number {\n const n = grid.length,\n m = grid[0].length;\n // \u521d\u59cb\u5316 dp \u8868\n const dp = new Array(m);\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n dp[0] = grid[0][0];\n for (let j = 1; j < m; j++) {\n dp[j] = dp[j - 1] + grid[0][j];\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n for (let i = 1; i < n; i++) {\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n dp[0] = dp[0] + grid[i][0];\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n for (let j = 1; j < m; j++) {\n dp[j] = Math.min(dp[j - 1], dp[j]) + grid[i][j];\n }\n }\n return dp[m - 1];\n}\n
min_path_sum.dart/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint minPathSumDPComp(List<List<int>> grid) {\n int n = grid.length, m = grid[0].length;\n // \u521d\u59cb\u5316 dp \u8868\n List<int> dp = List.filled(m, 0);\n dp[0] = grid[0][0];\n for (int j = 1; j < m; j++) {\n dp[j] = dp[j - 1] + grid[0][j];\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n for (int i = 1; i < n; i++) {\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n dp[0] = dp[0] + grid[i][0];\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n for (int j = 1; j < m; j++) {\n dp[j] = min(dp[j - 1], dp[j]) + grid[i][j];\n }\n }\n return dp[m - 1];\n}\n
min_path_sum.rs/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfn min_path_sum_dp_comp(grid: &Vec<Vec<i32>>) -> i32 {\n let (n, m) = (grid.len(), grid[0].len());\n // \u521d\u59cb\u5316 dp \u8868\n let mut dp = vec![0; m];\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n dp[0] = grid[0][0];\n for j in 1..m {\n dp[j] = dp[j - 1] + grid[0][j];\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n for i in 1..n {\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n dp[0] = dp[0] + grid[i][0];\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n for j in 1..m {\n dp[j] = std::cmp::min(dp[j - 1], dp[j]) + grid[i][j];\n }\n }\n dp[m - 1]\n}\n
min_path_sum.c/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint minPathSumDPComp(int grid[MAX_SIZE][MAX_SIZE], int n, int m) {\n // \u521d\u59cb\u5316 dp \u8868\n int *dp = calloc(m, sizeof(int));\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n dp[0] = grid[0][0];\n for (int j = 1; j < m; j++) {\n dp[j] = dp[j - 1] + grid[0][j];\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n for (int i = 1; i < n; i++) {\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n dp[0] = dp[0] + grid[i][0];\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n for (int j = 1; j < m; j++) {\n dp[j] = myMin(dp[j - 1], dp[j]) + grid[i][j];\n }\n }\n int res = dp[m - 1];\n // \u91ca\u653e\u5185\u5b58\n free(dp);\n return res;\n}\n
min_path_sum.zig// \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\nfn minPathSumDPComp(comptime grid: anytype) i32 {\n comptime var n = grid.len;\n comptime var m = grid[0].len;\n // \u521d\u59cb\u5316 dp \u8868\n var dp = [_]i32{0} ** m;\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n dp[0] = grid[0][0];\n for (1..m) |j| {\n dp[j] = dp[j - 1] + grid[0][j];\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n for (1..n) |i| {\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n dp[0] = dp[0] + grid[i][0];\n for (1..m) |j| {\n dp[j] = @min(dp[j - 1], dp[j]) + grid[i][j];\n }\n }\n return dp[m - 1];\n}\n
"},{"location":"chapter_dynamic_programming/edit_distance_problem/","title":"14.6 \u00a0 \u7f16\u8f91\u8ddd\u79bb\u95ee\u9898","text":"\u7f16\u8f91\u8ddd\u79bb\uff0c\u4e5f\u79f0 Levenshtein \u8ddd\u79bb\uff0c\u6307\u4e24\u4e2a\u5b57\u7b26\u4e32\u4e4b\u95f4\u4e92\u76f8\u8f6c\u6362\u7684\u6700\u5c11\u4fee\u6539\u6b21\u6570\uff0c\u901a\u5e38\u7528\u4e8e\u5728\u4fe1\u606f\u68c0\u7d22\u548c\u81ea\u7136\u8bed\u8a00\u5904\u7406\u4e2d\u5ea6\u91cf\u4e24\u4e2a\u5e8f\u5217\u7684\u76f8\u4f3c\u5ea6\u3002
Question
\u8f93\u5165\u4e24\u4e2a\u5b57\u7b26\u4e32 \\(s\\) \u548c \\(t\\) \uff0c\u8fd4\u56de\u5c06 \\(s\\) \u8f6c\u6362\u4e3a \\(t\\) \u6240\u9700\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570\u3002
\u4f60\u53ef\u4ee5\u5728\u4e00\u4e2a\u5b57\u7b26\u4e32\u4e2d\u8fdb\u884c\u4e09\u79cd\u7f16\u8f91\u64cd\u4f5c\uff1a\u63d2\u5165\u4e00\u4e2a\u5b57\u7b26\u3001\u5220\u9664\u4e00\u4e2a\u5b57\u7b26\u3001\u5c06\u5b57\u7b26\u66ff\u6362\u4e3a\u4efb\u610f\u4e00\u4e2a\u5b57\u7b26\u3002
\u5982\u56fe 14-27 \u6240\u793a\uff0c\u5c06 kitten
\u8f6c\u6362\u4e3a sitting
\u9700\u8981\u7f16\u8f91 3 \u6b65\uff0c\u5305\u62ec 2 \u6b21\u66ff\u6362\u64cd\u4f5c\u4e0e 1 \u6b21\u6dfb\u52a0\u64cd\u4f5c\uff1b\u5c06 hello
\u8f6c\u6362\u4e3a algo
\u9700\u8981 3 \u6b65\uff0c\u5305\u62ec 2 \u6b21\u66ff\u6362\u64cd\u4f5c\u548c 1 \u6b21\u5220\u9664\u64cd\u4f5c\u3002
\u56fe 14-27 \u00a0 \u7f16\u8f91\u8ddd\u79bb\u7684\u793a\u4f8b\u6570\u636e
\u7f16\u8f91\u8ddd\u79bb\u95ee\u9898\u53ef\u4ee5\u5f88\u81ea\u7136\u5730\u7528\u51b3\u7b56\u6811\u6a21\u578b\u6765\u89e3\u91ca\u3002\u5b57\u7b26\u4e32\u5bf9\u5e94\u6811\u8282\u70b9\uff0c\u4e00\u8f6e\u51b3\u7b56\uff08\u4e00\u6b21\u7f16\u8f91\u64cd\u4f5c\uff09\u5bf9\u5e94\u6811\u7684\u4e00\u6761\u8fb9\u3002
\u5982\u56fe 14-28 \u6240\u793a\uff0c\u5728\u4e0d\u9650\u5236\u64cd\u4f5c\u7684\u60c5\u51b5\u4e0b\uff0c\u6bcf\u4e2a\u8282\u70b9\u90fd\u53ef\u4ee5\u6d3e\u751f\u51fa\u8bb8\u591a\u6761\u8fb9\uff0c\u6bcf\u6761\u8fb9\u5bf9\u5e94\u4e00\u79cd\u64cd\u4f5c\uff0c\u8fd9\u610f\u5473\u7740\u4ece hello
\u8f6c\u6362\u5230 algo
\u6709\u8bb8\u591a\u79cd\u53ef\u80fd\u7684\u8def\u5f84\u3002
\u4ece\u51b3\u7b56\u6811\u7684\u89d2\u5ea6\u770b\uff0c\u672c\u9898\u7684\u76ee\u6807\u662f\u6c42\u89e3\u8282\u70b9 hello
\u548c\u8282\u70b9 algo
\u4e4b\u95f4\u7684\u6700\u77ed\u8def\u5f84\u3002
\u56fe 14-28 \u00a0 \u57fa\u4e8e\u51b3\u7b56\u6811\u6a21\u578b\u8868\u793a\u7f16\u8f91\u8ddd\u79bb\u95ee\u9898
"},{"location":"chapter_dynamic_programming/edit_distance_problem/#1","title":"1. \u00a0 \u52a8\u6001\u89c4\u5212\u601d\u8def","text":"\u7b2c\u4e00\u6b65\uff1a\u601d\u8003\u6bcf\u8f6e\u7684\u51b3\u7b56\uff0c\u5b9a\u4e49\u72b6\u6001\uff0c\u4ece\u800c\u5f97\u5230 \\(dp\\) \u8868
\u6bcf\u4e00\u8f6e\u7684\u51b3\u7b56\u662f\u5bf9\u5b57\u7b26\u4e32 \\(s\\) \u8fdb\u884c\u4e00\u6b21\u7f16\u8f91\u64cd\u4f5c\u3002
\u6211\u4eec\u5e0c\u671b\u5728\u7f16\u8f91\u64cd\u4f5c\u7684\u8fc7\u7a0b\u4e2d\uff0c\u95ee\u9898\u7684\u89c4\u6a21\u9010\u6e10\u7f29\u5c0f\uff0c\u8fd9\u6837\u624d\u80fd\u6784\u5efa\u5b50\u95ee\u9898\u3002\u8bbe\u5b57\u7b26\u4e32 \\(s\\) \u548c \\(t\\) \u7684\u957f\u5ea6\u5206\u522b\u4e3a \\(n\\) \u548c \\(m\\) \uff0c\u6211\u4eec\u5148\u8003\u8651\u4e24\u5b57\u7b26\u4e32\u5c3e\u90e8\u7684\u5b57\u7b26 \\(s[n-1]\\) \u548c \\(t[m-1]\\) \u3002
\u4e5f\u5c31\u662f\u8bf4\uff0c\u6211\u4eec\u5728\u5b57\u7b26\u4e32 \\(s\\) \u4e2d\u8fdb\u884c\u7684\u6bcf\u4e00\u8f6e\u51b3\u7b56\uff08\u7f16\u8f91\u64cd\u4f5c\uff09\uff0c\u90fd\u4f1a\u4f7f\u5f97 \\(s\\) \u548c \\(t\\) \u4e2d\u5269\u4f59\u7684\u5f85\u5339\u914d\u5b57\u7b26\u53d1\u751f\u53d8\u5316\u3002\u56e0\u6b64\uff0c\u72b6\u6001\u4e3a\u5f53\u524d\u5728 \\(s\\) \u548c \\(t\\) \u4e2d\u8003\u8651\u7684\u7b2c \\(i\\) \u548c\u7b2c \\(j\\) \u4e2a\u5b57\u7b26\uff0c\u8bb0\u4e3a \\([i, j]\\) \u3002
\u72b6\u6001 \\([i, j]\\) \u5bf9\u5e94\u7684\u5b50\u95ee\u9898\uff1a\u5c06 \\(s\\) \u7684\u524d \\(i\\) \u4e2a\u5b57\u7b26\u66f4\u6539\u4e3a \\(t\\) \u7684\u524d \\(j\\) \u4e2a\u5b57\u7b26\u6240\u9700\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570\u3002
\u81f3\u6b64\uff0c\u5f97\u5230\u4e00\u4e2a\u5c3a\u5bf8\u4e3a \\((i+1) \\times (j+1)\\) \u7684\u4e8c\u7ef4 \\(dp\\) \u8868\u3002
\u7b2c\u4e8c\u6b65\uff1a\u627e\u51fa\u6700\u4f18\u5b50\u7ed3\u6784\uff0c\u8fdb\u800c\u63a8\u5bfc\u51fa\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b
\u8003\u8651\u5b50\u95ee\u9898 \\(dp[i, j]\\) \uff0c\u5176\u5bf9\u5e94\u7684\u4e24\u4e2a\u5b57\u7b26\u4e32\u7684\u5c3e\u90e8\u5b57\u7b26\u4e3a \\(s[i-1]\\) \u548c \\(t[j-1]\\) \uff0c\u53ef\u6839\u636e\u4e0d\u540c\u7f16\u8f91\u64cd\u4f5c\u5206\u4e3a\u56fe 14-29 \u6240\u793a\u7684\u4e09\u79cd\u60c5\u51b5\u3002
\u56fe 14-29 \u00a0 \u7f16\u8f91\u8ddd\u79bb\u7684\u72b6\u6001\u8f6c\u79fb
\u6839\u636e\u4ee5\u4e0a\u5206\u6790\uff0c\u53ef\u5f97\u6700\u4f18\u5b50\u7ed3\u6784\uff1a\\(dp[i, j]\\) \u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570\u7b49\u4e8e \\(dp[i, j-1]\\)\u3001\\(dp[i-1, j]\\)\u3001\\(dp[i-1, j-1]\\) \u4e09\u8005\u4e2d\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570\uff0c\u518d\u52a0\u4e0a\u672c\u6b21\u7684\u7f16\u8f91\u6b65\u6570 \\(1\\) \u3002\u5bf9\u5e94\u7684\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u4e3a\uff1a
\\[ dp[i, j] = \\min(dp[i, j-1], dp[i-1, j], dp[i-1, j-1]) + 1 \\]\u8bf7\u6ce8\u610f\uff0c\u5f53 \\(s[i-1]\\) \u548c \\(t[j-1]\\) \u76f8\u540c\u65f6\uff0c\u65e0\u987b\u7f16\u8f91\u5f53\u524d\u5b57\u7b26\uff0c\u8fd9\u79cd\u60c5\u51b5\u4e0b\u7684\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u4e3a\uff1a
\\[ dp[i, j] = dp[i-1, j-1] \\]\u7b2c\u4e09\u6b65\uff1a\u786e\u5b9a\u8fb9\u754c\u6761\u4ef6\u548c\u72b6\u6001\u8f6c\u79fb\u987a\u5e8f
\u5f53\u4e24\u5b57\u7b26\u4e32\u90fd\u4e3a\u7a7a\u65f6\uff0c\u7f16\u8f91\u6b65\u6570\u4e3a \\(0\\) \uff0c\u5373 \\(dp[0, 0] = 0\\) \u3002\u5f53 \\(s\\) \u4e3a\u7a7a\u4f46 \\(t\\) \u4e0d\u4e3a\u7a7a\u65f6\uff0c\u6700\u5c11\u7f16\u8f91\u6b65\u6570\u7b49\u4e8e \\(t\\) \u7684\u957f\u5ea6\uff0c\u5373\u9996\u884c \\(dp[0, j] = j\\) \u3002\u5f53 \\(s\\) \u4e0d\u4e3a\u7a7a\u4f46 \\(t\\) \u4e3a\u7a7a\u65f6\uff0c\u6700\u5c11\u7f16\u8f91\u6b65\u6570\u7b49\u4e8e \\(s\\) \u7684\u957f\u5ea6\uff0c\u5373\u9996\u5217 \\(dp[i, 0] = i\\) \u3002
\u89c2\u5bdf\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\uff0c\u89e3 \\(dp[i, j]\\) \u4f9d\u8d56\u5de6\u65b9\u3001\u4e0a\u65b9\u3001\u5de6\u4e0a\u65b9\u7684\u89e3\uff0c\u56e0\u6b64\u901a\u8fc7\u4e24\u5c42\u5faa\u73af\u6b63\u5e8f\u904d\u5386\u6574\u4e2a \\(dp\\) \u8868\u5373\u53ef\u3002
"},{"location":"chapter_dynamic_programming/edit_distance_problem/#2","title":"2. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"PythonC++JavaC#GoSwiftJSTSDartRustCZig edit_distance.pydef edit_distance_dp(s: str, t: str) -> int:\n \"\"\"\u7f16\u8f91\u8ddd\u79bb\uff1a\u52a8\u6001\u89c4\u5212\"\"\"\n n, m = len(s), len(t)\n dp = [[0] * (m + 1) for _ in range(n + 1)]\n # \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n for i in range(1, n + 1):\n dp[i][0] = i\n for j in range(1, m + 1):\n dp[0][j] = j\n # \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n for i in range(1, n + 1):\n for j in range(1, m + 1):\n if s[i - 1] == t[j - 1]:\n # \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n dp[i][j] = dp[i - 1][j - 1]\n else:\n # \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n dp[i][j] = min(dp[i][j - 1], dp[i - 1][j], dp[i - 1][j - 1]) + 1\n return dp[n][m]\n
edit_distance.cpp/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u52a8\u6001\u89c4\u5212 */\nint editDistanceDP(string s, string t) {\n int n = s.length(), m = t.length();\n vector<vector<int>> dp(n + 1, vector<int>(m + 1, 0));\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n for (int i = 1; i <= n; i++) {\n dp[i][0] = i;\n }\n for (int j = 1; j <= m; j++) {\n dp[0][j] = j;\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n for (int i = 1; i <= n; i++) {\n for (int j = 1; j <= m; j++) {\n if (s[i - 1] == t[j - 1]) {\n // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n dp[i][j] = dp[i - 1][j - 1];\n } else {\n // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n dp[i][j] = min(min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1;\n }\n }\n }\n return dp[n][m];\n}\n
edit_distance.java/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u52a8\u6001\u89c4\u5212 */\nint editDistanceDP(String s, String t) {\n int n = s.length(), m = t.length();\n int[][] dp = new int[n + 1][m + 1];\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n for (int i = 1; i <= n; i++) {\n dp[i][0] = i;\n }\n for (int j = 1; j <= m; j++) {\n dp[0][j] = j;\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n for (int i = 1; i <= n; i++) {\n for (int j = 1; j <= m; j++) {\n if (s.charAt(i - 1) == t.charAt(j - 1)) {\n // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n dp[i][j] = dp[i - 1][j - 1];\n } else {\n // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n dp[i][j] = Math.min(Math.min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1;\n }\n }\n }\n return dp[n][m];\n}\n
edit_distance.cs/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u52a8\u6001\u89c4\u5212 */\nint EditDistanceDP(string s, string t) {\n int n = s.Length, m = t.Length;\n int[,] dp = new int[n + 1, m + 1];\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n for (int i = 1; i <= n; i++) {\n dp[i, 0] = i;\n }\n for (int j = 1; j <= m; j++) {\n dp[0, j] = j;\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n for (int i = 1; i <= n; i++) {\n for (int j = 1; j <= m; j++) {\n if (s[i - 1] == t[j - 1]) {\n // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n dp[i, j] = dp[i - 1, j - 1];\n } else {\n // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n dp[i, j] = Math.Min(Math.Min(dp[i, j - 1], dp[i - 1, j]), dp[i - 1, j - 1]) + 1;\n }\n }\n }\n return dp[n, m];\n}\n
edit_distance.go/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc editDistanceDP(s string, t string) int {\n n := len(s)\n m := len(t)\n dp := make([][]int, n+1)\n for i := 0; i <= n; i++ {\n dp[i] = make([]int, m+1)\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n for i := 1; i <= n; i++ {\n dp[i][0] = i\n }\n for j := 1; j <= m; j++ {\n dp[0][j] = j\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n for i := 1; i <= n; i++ {\n for j := 1; j <= m; j++ {\n if s[i-1] == t[j-1] {\n // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n dp[i][j] = dp[i-1][j-1]\n } else {\n // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n dp[i][j] = MinInt(MinInt(dp[i][j-1], dp[i-1][j]), dp[i-1][j-1]) + 1\n }\n }\n }\n return dp[n][m]\n}\n
edit_distance.swift/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc editDistanceDP(s: String, t: String) -> Int {\n let n = s.utf8CString.count\n let m = t.utf8CString.count\n var dp = Array(repeating: Array(repeating: 0, count: m + 1), count: n + 1)\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n for i in stride(from: 1, through: n, by: 1) {\n dp[i][0] = i\n }\n for j in stride(from: 1, through: m, by: 1) {\n dp[0][j] = j\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n for i in stride(from: 1, through: n, by: 1) {\n for j in stride(from: 1, through: m, by: 1) {\n if s.utf8CString[i - 1] == t.utf8CString[j - 1] {\n // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n dp[i][j] = dp[i - 1][j - 1]\n } else {\n // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n dp[i][j] = min(min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1\n }\n }\n }\n return dp[n][m]\n}\n
edit_distance.js/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction editDistanceDP(s, t) {\n const n = s.length,\n m = t.length;\n const dp = Array.from({ length: n + 1 }, () => new Array(m + 1).fill(0));\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n for (let i = 1; i <= n; i++) {\n dp[i][0] = i;\n }\n for (let j = 1; j <= m; j++) {\n dp[0][j] = j;\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n for (let i = 1; i <= n; i++) {\n for (let j = 1; j <= m; j++) {\n if (s.charAt(i - 1) === t.charAt(j - 1)) {\n // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n dp[i][j] = dp[i - 1][j - 1];\n } else {\n // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n dp[i][j] =\n Math.min(dp[i][j - 1], dp[i - 1][j], dp[i - 1][j - 1]) + 1;\n }\n }\n }\n return dp[n][m];\n}\n
edit_distance.ts/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction editDistanceDP(s: string, t: string): number {\n const n = s.length,\n m = t.length;\n const dp = Array.from({ length: n + 1 }, () =>\n Array.from({ length: m + 1 }, () => 0)\n );\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n for (let i = 1; i <= n; i++) {\n dp[i][0] = i;\n }\n for (let j = 1; j <= m; j++) {\n dp[0][j] = j;\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n for (let i = 1; i <= n; i++) {\n for (let j = 1; j <= m; j++) {\n if (s.charAt(i - 1) === t.charAt(j - 1)) {\n // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n dp[i][j] = dp[i - 1][j - 1];\n } else {\n // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n dp[i][j] =\n Math.min(dp[i][j - 1], dp[i - 1][j], dp[i - 1][j - 1]) + 1;\n }\n }\n }\n return dp[n][m];\n}\n
edit_distance.dart/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u52a8\u6001\u89c4\u5212 */\nint editDistanceDP(String s, String t) {\n int n = s.length, m = t.length;\n List<List<int>> dp = List.generate(n + 1, (_) => List.filled(m + 1, 0));\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n for (int i = 1; i <= n; i++) {\n dp[i][0] = i;\n }\n for (int j = 1; j <= m; j++) {\n dp[0][j] = j;\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n for (int i = 1; i <= n; i++) {\n for (int j = 1; j <= m; j++) {\n if (s[i - 1] == t[j - 1]) {\n // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n dp[i][j] = dp[i - 1][j - 1];\n } else {\n // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n dp[i][j] = min(min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1;\n }\n }\n }\n return dp[n][m];\n}\n
edit_distance.rs/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u52a8\u6001\u89c4\u5212 */\nfn edit_distance_dp(s: &str, t: &str) -> i32 {\n let (n, m) = (s.len(), t.len());\n let mut dp = vec![vec![0; m + 1]; n + 1];\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n for i in 1..= n {\n dp[i][0] = i as i32;\n }\n for j in 1..m {\n dp[0][j] = j as i32;\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n for i in 1..=n {\n for j in 1..=m {\n if s.chars().nth(i - 1) == t.chars().nth(j - 1) {\n // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n dp[i][j] = dp[i - 1][j - 1];\n } else {\n // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n dp[i][j] = std::cmp::min(std::cmp::min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1;\n }\n }\n }\n dp[n][m]\n}\n
edit_distance.c/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u52a8\u6001\u89c4\u5212 */\nint editDistanceDP(char *s, char *t, int n, int m) {\n int **dp = malloc((n + 1) * sizeof(int *));\n for (int i = 0; i <= n; i++) {\n dp[i] = calloc(m + 1, sizeof(int));\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n for (int i = 1; i <= n; i++) {\n dp[i][0] = i;\n }\n for (int j = 1; j <= m; j++) {\n dp[0][j] = j;\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n for (int i = 1; i <= n; i++) {\n for (int j = 1; j <= m; j++) {\n if (s[i - 1] == t[j - 1]) {\n // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n dp[i][j] = dp[i - 1][j - 1];\n } else {\n // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n dp[i][j] = myMin(myMin(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1;\n }\n }\n }\n int res = dp[n][m];\n // \u91ca\u653e\u5185\u5b58\n for (int i = 0; i <= n; i++) {\n free(dp[i]);\n }\n return res;\n}\n
edit_distance.zig// \u7f16\u8f91\u8ddd\u79bb\uff1a\u52a8\u6001\u89c4\u5212\nfn editDistanceDP(comptime s: []const u8, comptime t: []const u8) i32 {\n comptime var n = s.len;\n comptime var m = t.len;\n var dp = [_][m + 1]i32{[_]i32{0} ** (m + 1)} ** (n + 1);\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n for (1..n + 1) |i| {\n dp[i][0] = @intCast(i);\n }\n for (1..m + 1) |j| {\n dp[0][j] = @intCast(j);\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n for (1..n + 1) |i| {\n for (1..m + 1) |j| {\n if (s[i - 1] == t[j - 1]) {\n // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n dp[i][j] = dp[i - 1][j - 1];\n } else {\n // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n dp[i][j] = @min(@min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1;\n }\n }\n }\n return dp[n][m];\n}\n
\u5982\u56fe 14-30 \u6240\u793a\uff0c\u7f16\u8f91\u8ddd\u79bb\u95ee\u9898\u7684\u72b6\u6001\u8f6c\u79fb\u8fc7\u7a0b\u4e0e\u80cc\u5305\u95ee\u9898\u975e\u5e38\u7c7b\u4f3c\uff0c\u90fd\u53ef\u4ee5\u770b\u4f5c\u586b\u5199\u4e00\u4e2a\u4e8c\u7ef4\u7f51\u683c\u7684\u8fc7\u7a0b\u3002
<1><2><3><4><5><6><7><8><9><10><11><12><13><14><15>\u56fe 14-30 \u00a0 \u7f16\u8f91\u8ddd\u79bb\u7684\u52a8\u6001\u89c4\u5212\u8fc7\u7a0b
"},{"location":"chapter_dynamic_programming/edit_distance_problem/#3","title":"3. \u00a0 \u7a7a\u95f4\u4f18\u5316","text":"\u7531\u4e8e \\(dp[i,j]\\) \u662f\u7531\u4e0a\u65b9 \\(dp[i-1, j]\\)\u3001\u5de6\u65b9 \\(dp[i, j-1]\\)\u3001\u5de6\u4e0a\u65b9 \\(dp[i-1, j-1]\\) \u8f6c\u79fb\u800c\u6765\u7684\uff0c\u800c\u6b63\u5e8f\u904d\u5386\u4f1a\u4e22\u5931\u5de6\u4e0a\u65b9 \\(dp[i-1, j-1]\\) \uff0c\u5012\u5e8f\u904d\u5386\u65e0\u6cd5\u63d0\u524d\u6784\u5efa \\(dp[i, j-1]\\) \uff0c\u56e0\u6b64\u4e24\u79cd\u904d\u5386\u987a\u5e8f\u90fd\u4e0d\u53ef\u53d6\u3002
\u4e3a\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u53d8\u91cf leftup
\u6765\u6682\u5b58\u5de6\u4e0a\u65b9\u7684\u89e3 \\(dp[i-1, j-1]\\) \uff0c\u4ece\u800c\u53ea\u9700\u8003\u8651\u5de6\u65b9\u548c\u4e0a\u65b9\u7684\u89e3\u3002\u6b64\u65f6\u7684\u60c5\u51b5\u4e0e\u5b8c\u5168\u80cc\u5305\u95ee\u9898\u76f8\u540c\uff0c\u53ef\u4f7f\u7528\u6b63\u5e8f\u904d\u5386\u3002\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a
def edit_distance_dp_comp(s: str, t: str) -> int:\n \"\"\"\u7f16\u8f91\u8ddd\u79bb\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\"\"\"\n n, m = len(s), len(t)\n dp = [0] * (m + 1)\n # \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n for j in range(1, m + 1):\n dp[j] = j\n # \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n for i in range(1, n + 1):\n # \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n leftup = dp[0] # \u6682\u5b58 dp[i-1, j-1]\n dp[0] += 1\n # \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n for j in range(1, m + 1):\n temp = dp[j]\n if s[i - 1] == t[j - 1]:\n # \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n dp[j] = leftup\n else:\n # \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n dp[j] = min(dp[j - 1], dp[j], leftup) + 1\n leftup = temp # \u66f4\u65b0\u4e3a\u4e0b\u4e00\u8f6e\u7684 dp[i-1, j-1]\n return dp[m]\n
edit_distance.cpp/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint editDistanceDPComp(string s, string t) {\n int n = s.length(), m = t.length();\n vector<int> dp(m + 1, 0);\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n for (int j = 1; j <= m; j++) {\n dp[j] = j;\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n for (int i = 1; i <= n; i++) {\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n int leftup = dp[0]; // \u6682\u5b58 dp[i-1, j-1]\n dp[0] = i;\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n for (int j = 1; j <= m; j++) {\n int temp = dp[j];\n if (s[i - 1] == t[j - 1]) {\n // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n dp[j] = leftup;\n } else {\n // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n dp[j] = min(min(dp[j - 1], dp[j]), leftup) + 1;\n }\n leftup = temp; // \u66f4\u65b0\u4e3a\u4e0b\u4e00\u8f6e\u7684 dp[i-1, j-1]\n }\n }\n return dp[m];\n}\n
edit_distance.java/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint editDistanceDPComp(String s, String t) {\n int n = s.length(), m = t.length();\n int[] dp = new int[m + 1];\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n for (int j = 1; j <= m; j++) {\n dp[j] = j;\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n for (int i = 1; i <= n; i++) {\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n int leftup = dp[0]; // \u6682\u5b58 dp[i-1, j-1]\n dp[0] = i;\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n for (int j = 1; j <= m; j++) {\n int temp = dp[j];\n if (s.charAt(i - 1) == t.charAt(j - 1)) {\n // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n dp[j] = leftup;\n } else {\n // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n dp[j] = Math.min(Math.min(dp[j - 1], dp[j]), leftup) + 1;\n }\n leftup = temp; // \u66f4\u65b0\u4e3a\u4e0b\u4e00\u8f6e\u7684 dp[i-1, j-1]\n }\n }\n return dp[m];\n}\n
edit_distance.cs/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint EditDistanceDPComp(string s, string t) {\n int n = s.Length, m = t.Length;\n int[] dp = new int[m + 1];\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n for (int j = 1; j <= m; j++) {\n dp[j] = j;\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n for (int i = 1; i <= n; i++) {\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n int leftup = dp[0]; // \u6682\u5b58 dp[i-1, j-1]\n dp[0] = i;\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n for (int j = 1; j <= m; j++) {\n int temp = dp[j];\n if (s[i - 1] == t[j - 1]) {\n // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n dp[j] = leftup;\n } else {\n // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n dp[j] = Math.Min(Math.Min(dp[j - 1], dp[j]), leftup) + 1;\n }\n leftup = temp; // \u66f4\u65b0\u4e3a\u4e0b\u4e00\u8f6e\u7684 dp[i-1, j-1]\n }\n }\n return dp[m];\n}\n
edit_distance.go/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc editDistanceDPComp(s string, t string) int {\n n := len(s)\n m := len(t)\n dp := make([]int, m+1)\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n for j := 1; j <= m; j++ {\n dp[j] = j\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n for i := 1; i <= n; i++ {\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n leftUp := dp[0] // \u6682\u5b58 dp[i-1, j-1]\n dp[0] = i\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n for j := 1; j <= m; j++ {\n temp := dp[j]\n if s[i-1] == t[j-1] {\n // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n dp[j] = leftUp\n } else {\n // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n dp[j] = MinInt(MinInt(dp[j-1], dp[j]), leftUp) + 1\n }\n leftUp = temp // \u66f4\u65b0\u4e3a\u4e0b\u4e00\u8f6e\u7684 dp[i-1, j-1]\n }\n }\n return dp[m]\n}\n
edit_distance.swift/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc editDistanceDPComp(s: String, t: String) -> Int {\n let n = s.utf8CString.count\n let m = t.utf8CString.count\n var dp = Array(repeating: 0, count: m + 1)\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n for j in stride(from: 1, through: m, by: 1) {\n dp[j] = j\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n for i in stride(from: 1, through: n, by: 1) {\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n var leftup = dp[0] // \u6682\u5b58 dp[i-1, j-1]\n dp[0] = i\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n for j in stride(from: 1, through: m, by: 1) {\n let temp = dp[j]\n if s.utf8CString[i - 1] == t.utf8CString[j - 1] {\n // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n dp[j] = leftup\n } else {\n // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n dp[j] = min(min(dp[j - 1], dp[j]), leftup) + 1\n }\n leftup = temp // \u66f4\u65b0\u4e3a\u4e0b\u4e00\u8f6e\u7684 dp[i-1, j-1]\n }\n }\n return dp[m]\n}\n
edit_distance.js/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction editDistanceDPComp(s, t) {\n const n = s.length,\n m = t.length;\n const dp = new Array(m + 1).fill(0);\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n for (let j = 1; j <= m; j++) {\n dp[j] = j;\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n for (let i = 1; i <= n; i++) {\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n let leftup = dp[0]; // \u6682\u5b58 dp[i-1, j-1]\n dp[0] = i;\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n for (let j = 1; j <= m; j++) {\n const temp = dp[j];\n if (s.charAt(i - 1) === t.charAt(j - 1)) {\n // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n dp[j] = leftup;\n } else {\n // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n dp[j] = Math.min(dp[j - 1], dp[j], leftup) + 1;\n }\n leftup = temp; // \u66f4\u65b0\u4e3a\u4e0b\u4e00\u8f6e\u7684 dp[i-1, j-1]\n }\n }\n return dp[m];\n}\n
edit_distance.ts/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction editDistanceDPComp(s: string, t: string): number {\n const n = s.length,\n m = t.length;\n const dp = new Array(m + 1).fill(0);\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n for (let j = 1; j <= m; j++) {\n dp[j] = j;\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n for (let i = 1; i <= n; i++) {\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n let leftup = dp[0]; // \u6682\u5b58 dp[i-1, j-1]\n dp[0] = i;\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n for (let j = 1; j <= m; j++) {\n const temp = dp[j];\n if (s.charAt(i - 1) === t.charAt(j - 1)) {\n // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n dp[j] = leftup;\n } else {\n // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n dp[j] = Math.min(dp[j - 1], dp[j], leftup) + 1;\n }\n leftup = temp; // \u66f4\u65b0\u4e3a\u4e0b\u4e00\u8f6e\u7684 dp[i-1, j-1]\n }\n }\n return dp[m];\n}\n
edit_distance.dart/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint editDistanceDPComp(String s, String t) {\n int n = s.length, m = t.length;\n List<int> dp = List.filled(m + 1, 0);\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n for (int j = 1; j <= m; j++) {\n dp[j] = j;\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n for (int i = 1; i <= n; i++) {\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n int leftup = dp[0]; // \u6682\u5b58 dp[i-1, j-1]\n dp[0] = i;\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n for (int j = 1; j <= m; j++) {\n int temp = dp[j];\n if (s[i - 1] == t[j - 1]) {\n // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n dp[j] = leftup;\n } else {\n // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n dp[j] = min(min(dp[j - 1], dp[j]), leftup) + 1;\n }\n leftup = temp; // \u66f4\u65b0\u4e3a\u4e0b\u4e00\u8f6e\u7684 dp[i-1, j-1]\n }\n }\n return dp[m];\n}\n
edit_distance.rs/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfn edit_distance_dp_comp(s: &str, t: &str) -> i32 {\n let (n, m) = (s.len(), t.len());\n let mut dp = vec![0; m + 1];\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n for j in 1..m {\n dp[j] = j as i32;\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n for i in 1..=n {\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n let mut leftup = dp[0]; // \u6682\u5b58 dp[i-1, j-1]\n dp[0] = i as i32;\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n for j in 1..=m {\n let temp = dp[j];\n if s.chars().nth(i - 1) == t.chars().nth(j - 1) {\n // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n dp[j] = leftup;\n } else {\n // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n dp[j] = std::cmp::min(std::cmp::min(dp[j - 1], dp[j]), leftup) + 1;\n }\n leftup = temp; // \u66f4\u65b0\u4e3a\u4e0b\u4e00\u8f6e\u7684 dp[i-1, j-1]\n }\n }\n dp[m]\n}\n
edit_distance.c/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint editDistanceDPComp(char *s, char *t, int n, int m) {\n int *dp = calloc(m + 1, sizeof(int));\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n for (int j = 1; j <= m; j++) {\n dp[j] = j;\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n for (int i = 1; i <= n; i++) {\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n int leftup = dp[0]; // \u6682\u5b58 dp[i-1, j-1]\n dp[0] = i;\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n for (int j = 1; j <= m; j++) {\n int temp = dp[j];\n if (s[i - 1] == t[j - 1]) {\n // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n dp[j] = leftup;\n } else {\n // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n dp[j] = myMin(myMin(dp[j - 1], dp[j]), leftup) + 1;\n }\n leftup = temp; // \u66f4\u65b0\u4e3a\u4e0b\u4e00\u8f6e\u7684 dp[i-1, j-1]\n }\n }\n int res = dp[m];\n // \u91ca\u653e\u5185\u5b58\n free(dp);\n return res;\n}\n
edit_distance.zig// \u7f16\u8f91\u8ddd\u79bb\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\nfn editDistanceDPComp(comptime s: []const u8, comptime t: []const u8) i32 {\n comptime var n = s.len;\n comptime var m = t.len;\n var dp = [_]i32{0} ** (m + 1);\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n for (1..m + 1) |j| {\n dp[j] = @intCast(j);\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n for (1..n + 1) |i| {\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n var leftup = dp[0]; // \u6682\u5b58 dp[i-1, j-1]\n dp[0] = @intCast(i);\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n for (1..m + 1) |j| {\n var temp = dp[j];\n if (s[i - 1] == t[j - 1]) {\n // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n dp[j] = leftup;\n } else {\n // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n dp[j] = @min(@min(dp[j - 1], dp[j]), leftup) + 1;\n }\n leftup = temp; // \u66f4\u65b0\u4e3a\u4e0b\u4e00\u8f6e\u7684 dp[i-1, j-1]\n }\n }\n return dp[m];\n}\n
"},{"location":"chapter_dynamic_programming/intro_to_dynamic_programming/","title":"14.1 \u00a0 \u521d\u63a2\u52a8\u6001\u89c4\u5212","text":"\u300c\u52a8\u6001\u89c4\u5212 dynamic programming\u300d\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u7b97\u6cd5\u8303\u5f0f\uff0c\u5b83\u5c06\u4e00\u4e2a\u95ee\u9898\u5206\u89e3\u4e3a\u4e00\u7cfb\u5217\u66f4\u5c0f\u7684\u5b50\u95ee\u9898\uff0c\u5e76\u901a\u8fc7\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\u6765\u907f\u514d\u91cd\u590d\u8ba1\u7b97\uff0c\u4ece\u800c\u5927\u5e45\u63d0\u5347\u65f6\u95f4\u6548\u7387\u3002
\u5728\u672c\u8282\u4e2d\uff0c\u6211\u4eec\u4ece\u4e00\u4e2a\u7ecf\u5178\u4f8b\u9898\u5165\u624b\uff0c\u5148\u7ed9\u51fa\u5b83\u7684\u66b4\u529b\u56de\u6eaf\u89e3\u6cd5\uff0c\u89c2\u5bdf\u5176\u4e2d\u5305\u542b\u7684\u91cd\u53e0\u5b50\u95ee\u9898\uff0c\u518d\u9010\u6b65\u5bfc\u51fa\u66f4\u9ad8\u6548\u7684\u52a8\u6001\u89c4\u5212\u89e3\u6cd5\u3002
\u722c\u697c\u68af
\u7ed9\u5b9a\u4e00\u4e2a\u5171\u6709 \\(n\\) \u9636\u7684\u697c\u68af\uff0c\u4f60\u6bcf\u6b65\u53ef\u4ee5\u4e0a \\(1\\) \u9636\u6216\u8005 \\(2\\) \u9636\uff0c\u8bf7\u95ee\u6709\u591a\u5c11\u79cd\u65b9\u6848\u53ef\u4ee5\u722c\u5230\u697c\u9876\uff1f
\u5982\u56fe 14-1 \u6240\u793a\uff0c\u5bf9\u4e8e\u4e00\u4e2a \\(3\\) \u9636\u697c\u68af\uff0c\u5171\u6709 \\(3\\) \u79cd\u65b9\u6848\u53ef\u4ee5\u722c\u5230\u697c\u9876\u3002
\u56fe 14-1 \u00a0 \u722c\u5230\u7b2c 3 \u9636\u7684\u65b9\u6848\u6570\u91cf
\u672c\u9898\u7684\u76ee\u6807\u662f\u6c42\u89e3\u65b9\u6848\u6570\u91cf\uff0c\u6211\u4eec\u53ef\u4ee5\u8003\u8651\u901a\u8fc7\u56de\u6eaf\u6765\u7a77\u4e3e\u6240\u6709\u53ef\u80fd\u6027\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u5c06\u722c\u697c\u68af\u60f3\u8c61\u4e3a\u4e00\u4e2a\u591a\u8f6e\u9009\u62e9\u7684\u8fc7\u7a0b\uff1a\u4ece\u5730\u9762\u51fa\u53d1\uff0c\u6bcf\u8f6e\u9009\u62e9\u4e0a \\(1\\) \u9636\u6216 \\(2\\) \u9636\uff0c\u6bcf\u5f53\u5230\u8fbe\u697c\u68af\u9876\u90e8\u65f6\u5c31\u5c06\u65b9\u6848\u6570\u91cf\u52a0 \\(1\\) \uff0c\u5f53\u8d8a\u8fc7\u697c\u68af\u9876\u90e8\u65f6\u5c31\u5c06\u5176\u526a\u679d\u3002\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig climbing_stairs_backtrack.pydef backtrack(choices: list[int], state: int, n: int, res: list[int]) -> int:\n \"\"\"\u56de\u6eaf\"\"\"\n # \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\n if state == n:\n res[0] += 1\n # \u904d\u5386\u6240\u6709\u9009\u62e9\n for choice in choices:\n # \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\n if state + choice > n:\n continue\n # \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n backtrack(choices, state + choice, n, res)\n # \u56de\u9000\n\ndef climbing_stairs_backtrack(n: int) -> int:\n \"\"\"\u722c\u697c\u68af\uff1a\u56de\u6eaf\"\"\"\n choices = [1, 2] # \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u9636\u6216 2 \u9636\n state = 0 # \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\n res = [0] # \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\n backtrack(choices, state, n, res)\n return res[0]\n
climbing_stairs_backtrack.cpp/* \u56de\u6eaf */\nvoid backtrack(vector<int> &choices, int state, int n, vector<int> &res) {\n // \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\n if (state == n)\n res[0]++;\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for (auto &choice : choices) {\n // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\n if (state + choice > n)\n continue;\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n backtrack(choices, state + choice, n, res);\n // \u56de\u9000\n }\n}\n\n/* \u722c\u697c\u68af\uff1a\u56de\u6eaf */\nint climbingStairsBacktrack(int n) {\n vector<int> choices = {1, 2}; // \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u9636\u6216 2 \u9636\n int state = 0; // \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\n vector<int> res = {0}; // \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\n backtrack(choices, state, n, res);\n return res[0];\n}\n
climbing_stairs_backtrack.java/* \u56de\u6eaf */\nvoid backtrack(List<Integer> choices, int state, int n, List<Integer> res) {\n // \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\n if (state == n)\n res.set(0, res.get(0) + 1);\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for (Integer choice : choices) {\n // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\n if (state + choice > n)\n continue;\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n backtrack(choices, state + choice, n, res);\n // \u56de\u9000\n }\n}\n\n/* \u722c\u697c\u68af\uff1a\u56de\u6eaf */\nint climbingStairsBacktrack(int n) {\n List<Integer> choices = Arrays.asList(1, 2); // \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u9636\u6216 2 \u9636\n int state = 0; // \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\n List<Integer> res = new ArrayList<>();\n res.add(0); // \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\n backtrack(choices, state, n, res);\n return res.get(0);\n}\n
climbing_stairs_backtrack.cs/* \u56de\u6eaf */\nvoid Backtrack(List<int> choices, int state, int n, List<int> res) {\n // \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\n if (state == n)\n res[0]++;\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n foreach (int choice in choices) {\n // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\n if (state + choice > n)\n continue;\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n Backtrack(choices, state + choice, n, res);\n // \u56de\u9000\n }\n}\n\n/* \u722c\u697c\u68af\uff1a\u56de\u6eaf */\nint ClimbingStairsBacktrack(int n) {\n List<int> choices = [1, 2]; // \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u9636\u6216 2 \u9636\n int state = 0; // \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\n List<int> res = [0]; // \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\n Backtrack(choices, state, n, res);\n return res[0];\n}\n
climbing_stairs_backtrack.go/* \u56de\u6eaf */\nfunc backtrack(choices []int, state, n int, res []int) {\n // \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\n if state == n {\n res[0] = res[0] + 1\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for _, choice := range choices {\n // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\n if state+choice > n {\n continue\n }\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n backtrack(choices, state+choice, n, res)\n // \u56de\u9000\n }\n}\n\n/* \u722c\u697c\u68af\uff1a\u56de\u6eaf */\nfunc climbingStairsBacktrack(n int) int {\n // \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u9636\u6216 2 \u9636\n choices := []int{1, 2}\n // \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\n state := 0\n res := make([]int, 1)\n // \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\n res[0] = 0\n backtrack(choices, state, n, res)\n return res[0]\n}\n
climbing_stairs_backtrack.swift/* \u56de\u6eaf */\nfunc backtrack(choices: [Int], state: Int, n: Int, res: inout [Int]) {\n // \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\n if state == n {\n res[0] += 1\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for choice in choices {\n // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\n if state + choice > n {\n continue\n }\n backtrack(choices: choices, state: state + choice, n: n, res: &res)\n }\n}\n\n/* \u722c\u697c\u68af\uff1a\u56de\u6eaf */\nfunc climbingStairsBacktrack(n: Int) -> Int {\n let choices = [1, 2] // \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u9636\u6216 2 \u9636\n let state = 0 // \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\n var res: [Int] = []\n res.append(0) // \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\n backtrack(choices: choices, state: state, n: n, res: &res)\n return res[0]\n}\n
climbing_stairs_backtrack.js/* \u56de\u6eaf */\nfunction backtrack(choices, state, n, res) {\n // \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\n if (state === n) res.set(0, res.get(0) + 1);\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for (const choice of choices) {\n // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\n if (state + choice > n) continue;\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n backtrack(choices, state + choice, n, res);\n // \u56de\u9000\n }\n}\n\n/* \u722c\u697c\u68af\uff1a\u56de\u6eaf */\nfunction climbingStairsBacktrack(n) {\n const choices = [1, 2]; // \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u9636\u6216 2 \u9636\n const state = 0; // \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\n const res = new Map();\n res.set(0, 0); // \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\n backtrack(choices, state, n, res);\n return res.get(0);\n}\n
climbing_stairs_backtrack.ts/* \u56de\u6eaf */\nfunction backtrack(\n choices: number[],\n state: number,\n n: number,\n res: Map<0, any>\n): void {\n // \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\n if (state === n) res.set(0, res.get(0) + 1);\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for (const choice of choices) {\n // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\n if (state + choice > n) continue;\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n backtrack(choices, state + choice, n, res);\n // \u56de\u9000\n }\n}\n\n/* \u722c\u697c\u68af\uff1a\u56de\u6eaf */\nfunction climbingStairsBacktrack(n: number): number {\n const choices = [1, 2]; // \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u9636\u6216 2 \u9636\n const state = 0; // \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\n const res = new Map();\n res.set(0, 0); // \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\n backtrack(choices, state, n, res);\n return res.get(0);\n}\n
climbing_stairs_backtrack.dart/* \u56de\u6eaf */\nvoid backtrack(List<int> choices, int state, int n, List<int> res) {\n // \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\n if (state == n) {\n res[0]++;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for (int choice in choices) {\n // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\n if (state + choice > n) continue;\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n backtrack(choices, state + choice, n, res);\n // \u56de\u9000\n }\n}\n\n/* \u722c\u697c\u68af\uff1a\u56de\u6eaf */\nint climbingStairsBacktrack(int n) {\n List<int> choices = [1, 2]; // \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u9636\u6216 2 \u9636\n int state = 0; // \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\n List<int> res = [];\n res.add(0); // \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\n backtrack(choices, state, n, res);\n return res[0];\n}\n
climbing_stairs_backtrack.rs/* \u56de\u6eaf */\nfn backtrack(choices: &[i32], state: i32, n: i32, res: &mut [i32]) {\n // \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\n if state == n { res[0] = res[0] + 1; }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for &choice in choices {\n // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\n if state + choice > n { continue; }\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n backtrack(choices, state + choice, n, res);\n // \u56de\u9000\n }\n}\n\n/* \u722c\u697c\u68af\uff1a\u56de\u6eaf */\nfn climbing_stairs_backtrack(n: usize) -> i32 {\n let choices = vec![ 1, 2 ]; // \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u9636\u6216 2 \u9636\n let state = 0; // \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\n let mut res = Vec::new();\n res.push(0); // \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\n backtrack(&choices, state, n as i32, &mut res);\n res[0]\n}\n
climbing_stairs_backtrack.c/* \u56de\u6eaf */\nvoid backtrack(int *choices, int state, int n, int *res, int len) {\n // \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\n if (state == n)\n res[0]++;\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for (int i = 0; i < len; i++) {\n int choice = choices[i];\n // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\n if (state + choice > n)\n continue;\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n backtrack(choices, state + choice, n, res, len);\n // \u56de\u9000\n }\n}\n\n/* \u722c\u697c\u68af\uff1a\u56de\u6eaf */\nint climbingStairsBacktrack(int n) {\n int choices[2] = {1, 2}; // \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u9636\u6216 2 \u9636\n int state = 0; // \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\n int *res = (int *)malloc(sizeof(int));\n *res = 0; // \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\n int len = sizeof(choices) / sizeof(int);\n backtrack(choices, state, n, res, len);\n int result = *res;\n free(res);\n return result;\n}\n
climbing_stairs_backtrack.zig// \u56de\u6eaf\nfn backtrack(choices: []i32, state: i32, n: i32, res: std.ArrayList(i32)) void {\n // \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\n if (state == n) {\n res.items[0] = res.items[0] + 1;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for (choices) |choice| {\n // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\n if (state + choice > n) {\n continue;\n }\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n backtrack(choices, state + choice, n, res);\n // \u56de\u9000\n }\n}\n\n// \u722c\u697c\u68af\uff1a\u56de\u6eaf\nfn climbingStairsBacktrack(n: usize) !i32 {\n var choices = [_]i32{ 1, 2 }; // \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u9636\u6216 2 \u9636\n var state: i32 = 0; // \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\n var res = std.ArrayList(i32).init(std.heap.page_allocator);\n defer res.deinit();\n try res.append(0); // \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\n backtrack(&choices, state, @intCast(n), res);\n return res.items[0];\n}\n
"},{"location":"chapter_dynamic_programming/intro_to_dynamic_programming/#1411","title":"14.1.1 \u00a0 \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u641c\u7d22","text":"\u56de\u6eaf\u7b97\u6cd5\u901a\u5e38\u5e76\u4e0d\u663e\u5f0f\u5730\u5bf9\u95ee\u9898\u8fdb\u884c\u62c6\u89e3\uff0c\u800c\u662f\u5c06\u6c42\u89e3\u95ee\u9898\u770b\u4f5c\u4e00\u7cfb\u5217\u51b3\u7b56\u6b65\u9aa4\uff0c\u901a\u8fc7\u8bd5\u63a2\u548c\u526a\u679d\uff0c\u641c\u7d22\u6240\u6709\u53ef\u80fd\u7684\u89e3\u3002
\u6211\u4eec\u53ef\u4ee5\u5c1d\u8bd5\u4ece\u95ee\u9898\u5206\u89e3\u7684\u89d2\u5ea6\u5206\u6790\u8fd9\u9053\u9898\u3002\u8bbe\u722c\u5230\u7b2c \\(i\\) \u9636\u5171\u6709 \\(dp[i]\\) \u79cd\u65b9\u6848\uff0c\u90a3\u4e48 \\(dp[i]\\) \u5c31\u662f\u539f\u95ee\u9898\uff0c\u5176\u5b50\u95ee\u9898\u5305\u62ec\uff1a
\\[ dp[i-1], dp[i-2], \\dots, dp[2], dp[1] \\]\u7531\u4e8e\u6bcf\u8f6e\u53ea\u80fd\u4e0a \\(1\\) \u9636\u6216 \\(2\\) \u9636\uff0c\u56e0\u6b64\u5f53\u6211\u4eec\u7ad9\u5728\u7b2c \\(i\\) \u9636\u697c\u68af\u4e0a\u65f6\uff0c\u4e0a\u4e00\u8f6e\u53ea\u53ef\u80fd\u7ad9\u5728\u7b2c \\(i - 1\\) \u9636\u6216\u7b2c \\(i - 2\\) \u9636\u4e0a\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u6211\u4eec\u53ea\u80fd\u4ece\u7b2c \\(i -1\\) \u9636\u6216\u7b2c \\(i - 2\\) \u9636\u8fc8\u5411\u7b2c \\(i\\) \u9636\u3002
\u7531\u6b64\u4fbf\u53ef\u5f97\u51fa\u4e00\u4e2a\u91cd\u8981\u63a8\u8bba\uff1a\u722c\u5230\u7b2c \\(i - 1\\) \u9636\u7684\u65b9\u6848\u6570\u52a0\u4e0a\u722c\u5230\u7b2c \\(i - 2\\) \u9636\u7684\u65b9\u6848\u6570\u5c31\u7b49\u4e8e\u722c\u5230\u7b2c \\(i\\) \u9636\u7684\u65b9\u6848\u6570\u3002\u516c\u5f0f\u5982\u4e0b\uff1a
\\[ dp[i] = dp[i-1] + dp[i-2] \\]\u8fd9\u610f\u5473\u7740\u5728\u722c\u697c\u68af\u95ee\u9898\u4e2d\uff0c\u5404\u4e2a\u5b50\u95ee\u9898\u4e4b\u95f4\u5b58\u5728\u9012\u63a8\u5173\u7cfb\uff0c\u539f\u95ee\u9898\u7684\u89e3\u53ef\u4ee5\u7531\u5b50\u95ee\u9898\u7684\u89e3\u6784\u5efa\u5f97\u6765\u3002\u56fe 14-2 \u5c55\u793a\u4e86\u8be5\u9012\u63a8\u5173\u7cfb\u3002
\u56fe 14-2 \u00a0 \u65b9\u6848\u6570\u91cf\u9012\u63a8\u5173\u7cfb
\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u9012\u63a8\u516c\u5f0f\u5f97\u5230\u66b4\u529b\u641c\u7d22\u89e3\u6cd5\u3002\u4ee5 \\(dp[n]\\) \u4e3a\u8d77\u59cb\u70b9\uff0c\u9012\u5f52\u5730\u5c06\u4e00\u4e2a\u8f83\u5927\u95ee\u9898\u62c6\u89e3\u4e3a\u4e24\u4e2a\u8f83\u5c0f\u95ee\u9898\u7684\u548c\uff0c\u76f4\u81f3\u5230\u8fbe\u6700\u5c0f\u5b50\u95ee\u9898 \\(dp[1]\\) \u548c \\(dp[2]\\) \u65f6\u8fd4\u56de\u3002\u5176\u4e2d\uff0c\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\u662f\u5df2\u77e5\u7684\uff0c\u5373 \\(dp[1] = 1\\)\u3001\\(dp[2] = 2\\) \uff0c\u8868\u793a\u722c\u5230\u7b2c \\(1\\)\u3001\\(2\\) \u9636\u5206\u522b\u6709 \\(1\\)\u3001\\(2\\) \u79cd\u65b9\u6848\u3002
\u89c2\u5bdf\u4ee5\u4e0b\u4ee3\u7801\uff0c\u5b83\u548c\u6807\u51c6\u56de\u6eaf\u4ee3\u7801\u90fd\u5c5e\u4e8e\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\uff0c\u4f46\u66f4\u52a0\u7b80\u6d01\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig climbing_stairs_dfs.pydef dfs(i: int) -> int:\n \"\"\"\u641c\u7d22\"\"\"\n # \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n if i == 1 or i == 2:\n return i\n # dp[i] = dp[i-1] + dp[i-2]\n count = dfs(i - 1) + dfs(i - 2)\n return count\n\ndef climbing_stairs_dfs(n: int) -> int:\n \"\"\"\u722c\u697c\u68af\uff1a\u641c\u7d22\"\"\"\n return dfs(n)\n
climbing_stairs_dfs.cpp/* \u641c\u7d22 */\nint dfs(int i) {\n // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n if (i == 1 || i == 2)\n return i;\n // dp[i] = dp[i-1] + dp[i-2]\n int count = dfs(i - 1) + dfs(i - 2);\n return count;\n}\n\n/* \u722c\u697c\u68af\uff1a\u641c\u7d22 */\nint climbingStairsDFS(int n) {\n return dfs(n);\n}\n
climbing_stairs_dfs.java/* \u641c\u7d22 */\nint dfs(int i) {\n // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n if (i == 1 || i == 2)\n return i;\n // dp[i] = dp[i-1] + dp[i-2]\n int count = dfs(i - 1) + dfs(i - 2);\n return count;\n}\n\n/* \u722c\u697c\u68af\uff1a\u641c\u7d22 */\nint climbingStairsDFS(int n) {\n return dfs(n);\n}\n
climbing_stairs_dfs.cs/* \u641c\u7d22 */\nint DFS(int i) {\n // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n if (i == 1 || i == 2)\n return i;\n // dp[i] = dp[i-1] + dp[i-2]\n int count = DFS(i - 1) + DFS(i - 2);\n return count;\n}\n\n/* \u722c\u697c\u68af\uff1a\u641c\u7d22 */\nint ClimbingStairsDFS(int n) {\n return DFS(n);\n}\n
climbing_stairs_dfs.go/* \u641c\u7d22 */\nfunc dfs(i int) int {\n // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n if i == 1 || i == 2 {\n return i\n }\n // dp[i] = dp[i-1] + dp[i-2]\n count := dfs(i-1) + dfs(i-2)\n return count\n}\n\n/* \u722c\u697c\u68af\uff1a\u641c\u7d22 */\nfunc climbingStairsDFS(n int) int {\n return dfs(n)\n}\n
climbing_stairs_dfs.swift/* \u641c\u7d22 */\nfunc dfs(i: Int) -> Int {\n // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n if i == 1 || i == 2 {\n return i\n }\n // dp[i] = dp[i-1] + dp[i-2]\n let count = dfs(i: i - 1) + dfs(i: i - 2)\n return count\n}\n\n/* \u722c\u697c\u68af\uff1a\u641c\u7d22 */\nfunc climbingStairsDFS(n: Int) -> Int {\n dfs(i: n)\n}\n
climbing_stairs_dfs.js/* \u641c\u7d22 */\nfunction dfs(i) {\n // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n if (i === 1 || i === 2) return i;\n // dp[i] = dp[i-1] + dp[i-2]\n const count = dfs(i - 1) + dfs(i - 2);\n return count;\n}\n\n/* \u722c\u697c\u68af\uff1a\u641c\u7d22 */\nfunction climbingStairsDFS(n) {\n return dfs(n);\n}\n
climbing_stairs_dfs.ts/* \u641c\u7d22 */\nfunction dfs(i: number): number {\n // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n if (i === 1 || i === 2) return i;\n // dp[i] = dp[i-1] + dp[i-2]\n const count = dfs(i - 1) + dfs(i - 2);\n return count;\n}\n\n/* \u722c\u697c\u68af\uff1a\u641c\u7d22 */\nfunction climbingStairsDFS(n: number): number {\n return dfs(n);\n}\n
climbing_stairs_dfs.dart/* \u641c\u7d22 */\nint dfs(int i) {\n // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n if (i == 1 || i == 2) return i;\n // dp[i] = dp[i-1] + dp[i-2]\n int count = dfs(i - 1) + dfs(i - 2);\n return count;\n}\n\n/* \u722c\u697c\u68af\uff1a\u641c\u7d22 */\nint climbingStairsDFS(int n) {\n return dfs(n);\n}\n
climbing_stairs_dfs.rs/* \u641c\u7d22 */\nfn dfs(i: usize) -> i32 {\n // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n if i == 1 || i == 2 { return i as i32; }\n // dp[i] = dp[i-1] + dp[i-2]\n let count = dfs(i - 1) + dfs(i - 2);\n count\n}\n\n/* \u722c\u697c\u68af\uff1a\u641c\u7d22 */\nfn climbing_stairs_dfs(n: usize) -> i32 {\n dfs(n) \n}\n
climbing_stairs_dfs.c/* \u641c\u7d22 */\nint dfs(int i) {\n // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n if (i == 1 || i == 2)\n return i;\n // dp[i] = dp[i-1] + dp[i-2]\n int count = dfs(i - 1) + dfs(i - 2);\n return count;\n}\n\n/* \u722c\u697c\u68af\uff1a\u641c\u7d22 */\nint climbingStairsDFS(int n) {\n return dfs(n);\n}\n
climbing_stairs_dfs.zig// \u641c\u7d22\nfn dfs(i: usize) i32 {\n // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n if (i == 1 or i == 2) {\n return @intCast(i);\n }\n // dp[i] = dp[i-1] + dp[i-2]\n var count = dfs(i - 1) + dfs(i - 2);\n return count;\n}\n\n// \u722c\u697c\u68af\uff1a\u641c\u7d22\nfn climbingStairsDFS(comptime n: usize) i32 {\n return dfs(n);\n}\n
\u56fe 14-3 \u5c55\u793a\u4e86\u66b4\u529b\u641c\u7d22\u5f62\u6210\u7684\u9012\u5f52\u6811\u3002\u5bf9\u4e8e\u95ee\u9898 \\(dp[n]\\) \uff0c\u5176\u9012\u5f52\u6811\u7684\u6df1\u5ea6\u4e3a \\(n\\) \uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(2^n)\\) \u3002\u6307\u6570\u9636\u5c5e\u4e8e\u7206\u70b8\u5f0f\u589e\u957f\uff0c\u5982\u679c\u6211\u4eec\u8f93\u5165\u4e00\u4e2a\u6bd4\u8f83\u5927\u7684 \\(n\\) \uff0c\u5219\u4f1a\u9677\u5165\u6f2b\u957f\u7684\u7b49\u5f85\u4e4b\u4e2d\u3002
\u56fe 14-3 \u00a0 \u722c\u697c\u68af\u5bf9\u5e94\u9012\u5f52\u6811
\u89c2\u5bdf\u56fe 14-3 \uff0c\u6307\u6570\u9636\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u662f\u201c\u91cd\u53e0\u5b50\u95ee\u9898\u201d\u5bfc\u81f4\u7684\u3002\u4f8b\u5982 \\(dp[9]\\) \u88ab\u5206\u89e3\u4e3a \\(dp[8]\\) \u548c \\(dp[7]\\) \uff0c\\(dp[8]\\) \u88ab\u5206\u89e3\u4e3a \\(dp[7]\\) \u548c \\(dp[6]\\) \uff0c\u4e24\u8005\u90fd\u5305\u542b\u5b50\u95ee\u9898 \\(dp[7]\\) \u3002
\u4ee5\u6b64\u7c7b\u63a8\uff0c\u5b50\u95ee\u9898\u4e2d\u5305\u542b\u66f4\u5c0f\u7684\u91cd\u53e0\u5b50\u95ee\u9898\uff0c\u5b50\u5b50\u5b59\u5b59\u65e0\u7a77\u5c3d\u4e5f\u3002\u7edd\u5927\u90e8\u5206\u8ba1\u7b97\u8d44\u6e90\u90fd\u6d6a\u8d39\u5728\u8fd9\u4e9b\u91cd\u53e0\u7684\u95ee\u9898\u4e0a\u3002
"},{"location":"chapter_dynamic_programming/intro_to_dynamic_programming/#1412","title":"14.1.2 \u00a0 \u65b9\u6cd5\u4e8c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22","text":"\u4e3a\u4e86\u63d0\u5347\u7b97\u6cd5\u6548\u7387\uff0c\u6211\u4eec\u5e0c\u671b\u6240\u6709\u7684\u91cd\u53e0\u5b50\u95ee\u9898\u90fd\u53ea\u88ab\u8ba1\u7b97\u4e00\u6b21\u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u58f0\u660e\u4e00\u4e2a\u6570\u7ec4 mem
\u6765\u8bb0\u5f55\u6bcf\u4e2a\u5b50\u95ee\u9898\u7684\u89e3\uff0c\u5e76\u5728\u641c\u7d22\u8fc7\u7a0b\u4e2d\u5c06\u91cd\u53e0\u5b50\u95ee\u9898\u526a\u679d\u3002
mem[i]
\uff0c\u4ee5\u4fbf\u4e4b\u540e\u4f7f\u7528\u3002mem[i]
\u4e2d\u83b7\u53d6\u7ed3\u679c\uff0c\u4ece\u800c\u907f\u514d\u91cd\u590d\u8ba1\u7b97\u8be5\u5b50\u95ee\u9898\u3002\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig climbing_stairs_dfs_mem.pydef dfs(i: int, mem: list[int]) -> int:\n \"\"\"\u8bb0\u5fc6\u5316\u641c\u7d22\"\"\"\n # \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n if i == 1 or i == 2:\n return i\n # \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\n if mem[i] != -1:\n return mem[i]\n # dp[i] = dp[i-1] + dp[i-2]\n count = dfs(i - 1, mem) + dfs(i - 2, mem)\n # \u8bb0\u5f55 dp[i]\n mem[i] = count\n return count\n\ndef climbing_stairs_dfs_mem(n: int) -> int:\n \"\"\"\u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22\"\"\"\n # mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\n mem = [-1] * (n + 1)\n return dfs(n, mem)\n
climbing_stairs_dfs_mem.cpp/* \u8bb0\u5fc6\u5316\u641c\u7d22 */\nint dfs(int i, vector<int> &mem) {\n // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n if (i == 1 || i == 2)\n return i;\n // \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\n if (mem[i] != -1)\n return mem[i];\n // dp[i] = dp[i-1] + dp[i-2]\n int count = dfs(i - 1, mem) + dfs(i - 2, mem);\n // \u8bb0\u5f55 dp[i]\n mem[i] = count;\n return count;\n}\n\n/* \u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint climbingStairsDFSMem(int n) {\n // mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\n vector<int> mem(n + 1, -1);\n return dfs(n, mem);\n}\n
climbing_stairs_dfs_mem.java/* \u8bb0\u5fc6\u5316\u641c\u7d22 */\nint dfs(int i, int[] mem) {\n // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n if (i == 1 || i == 2)\n return i;\n // \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\n if (mem[i] != -1)\n return mem[i];\n // dp[i] = dp[i-1] + dp[i-2]\n int count = dfs(i - 1, mem) + dfs(i - 2, mem);\n // \u8bb0\u5f55 dp[i]\n mem[i] = count;\n return count;\n}\n\n/* \u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint climbingStairsDFSMem(int n) {\n // mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\n int[] mem = new int[n + 1];\n Arrays.fill(mem, -1);\n return dfs(n, mem);\n}\n
climbing_stairs_dfs_mem.cs/* \u8bb0\u5fc6\u5316\u641c\u7d22 */\nint DFS(int i, int[] mem) {\n // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n if (i == 1 || i == 2)\n return i;\n // \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\n if (mem[i] != -1)\n return mem[i];\n // dp[i] = dp[i-1] + dp[i-2]\n int count = DFS(i - 1, mem) + DFS(i - 2, mem);\n // \u8bb0\u5f55 dp[i]\n mem[i] = count;\n return count;\n}\n\n/* \u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint ClimbingStairsDFSMem(int n) {\n // mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\n int[] mem = new int[n + 1];\n Array.Fill(mem, -1);\n return DFS(n, mem);\n}\n
climbing_stairs_dfs_mem.go/* \u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunc dfsMem(i int, mem []int) int {\n // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n if i == 1 || i == 2 {\n return i\n }\n // \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\n if mem[i] != -1 {\n return mem[i]\n }\n // dp[i] = dp[i-1] + dp[i-2]\n count := dfsMem(i-1, mem) + dfsMem(i-2, mem)\n // \u8bb0\u5f55 dp[i]\n mem[i] = count\n return count\n}\n\n/* \u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunc climbingStairsDFSMem(n int) int {\n // mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\n mem := make([]int, n+1)\n for i := range mem {\n mem[i] = -1\n }\n return dfsMem(n, mem)\n}\n
climbing_stairs_dfs_mem.swift/* \u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunc dfs(i: Int, mem: inout [Int]) -> Int {\n // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n if i == 1 || i == 2 {\n return i\n }\n // \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\n if mem[i] != -1 {\n return mem[i]\n }\n // dp[i] = dp[i-1] + dp[i-2]\n let count = dfs(i: i - 1, mem: &mem) + dfs(i: i - 2, mem: &mem)\n // \u8bb0\u5f55 dp[i]\n mem[i] = count\n return count\n}\n\n/* \u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunc climbingStairsDFSMem(n: Int) -> Int {\n // mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\n var mem = Array(repeating: -1, count: n + 1)\n return dfs(i: n, mem: &mem)\n}\n
climbing_stairs_dfs_mem.js/* \u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunction dfs(i, mem) {\n // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n if (i === 1 || i === 2) return i;\n // \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\n if (mem[i] != -1) return mem[i];\n // dp[i] = dp[i-1] + dp[i-2]\n const count = dfs(i - 1, mem) + dfs(i - 2, mem);\n // \u8bb0\u5f55 dp[i]\n mem[i] = count;\n return count;\n}\n\n/* \u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunction climbingStairsDFSMem(n) {\n // mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\n const mem = new Array(n + 1).fill(-1);\n return dfs(n, mem);\n}\n
climbing_stairs_dfs_mem.ts/* \u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunction dfs(i: number, mem: number[]): number {\n // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n if (i === 1 || i === 2) return i;\n // \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\n if (mem[i] != -1) return mem[i];\n // dp[i] = dp[i-1] + dp[i-2]\n const count = dfs(i - 1, mem) + dfs(i - 2, mem);\n // \u8bb0\u5f55 dp[i]\n mem[i] = count;\n return count;\n}\n\n/* \u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunction climbingStairsDFSMem(n: number): number {\n // mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\n const mem = new Array(n + 1).fill(-1);\n return dfs(n, mem);\n}\n
climbing_stairs_dfs_mem.dart/* \u8bb0\u5fc6\u5316\u641c\u7d22 */\nint dfs(int i, List<int> mem) {\n // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n if (i == 1 || i == 2) return i;\n // \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\n if (mem[i] != -1) return mem[i];\n // dp[i] = dp[i-1] + dp[i-2]\n int count = dfs(i - 1, mem) + dfs(i - 2, mem);\n // \u8bb0\u5f55 dp[i]\n mem[i] = count;\n return count;\n}\n\n/* \u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint climbingStairsDFSMem(int n) {\n // mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\n List<int> mem = List.filled(n + 1, -1);\n return dfs(n, mem);\n}\n
climbing_stairs_dfs_mem.rs/* \u8bb0\u5fc6\u5316\u641c\u7d22 */\nfn dfs(i: usize, mem: &mut [i32]) -> i32 {\n // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n if i == 1 || i == 2 { return i as i32; }\n // \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\n if mem[i] != -1 { return mem[i]; }\n // dp[i] = dp[i-1] + dp[i-2]\n let count = dfs(i - 1, mem) + dfs(i - 2, mem);\n // \u8bb0\u5f55 dp[i]\n mem[i] = count;\n count\n}\n\n/* \u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfn climbing_stairs_dfs_mem(n: usize) -> i32 {\n // mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\n let mut mem = vec![-1; n + 1];\n dfs(n, &mut mem)\n}\n
climbing_stairs_dfs_mem.c/* \u8bb0\u5fc6\u5316\u641c\u7d22 */\nint dfs(int i, int *mem) {\n // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n if (i == 1 || i == 2)\n return i;\n // \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\n if (mem[i] != -1)\n return mem[i];\n // dp[i] = dp[i-1] + dp[i-2]\n int count = dfs(i - 1, mem) + dfs(i - 2, mem);\n // \u8bb0\u5f55 dp[i]\n mem[i] = count;\n return count;\n}\n\n/* \u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint climbingStairsDFSMem(int n) {\n // mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\n int *mem = (int *)malloc((n + 1) * sizeof(int));\n for (int i = 0; i <= n; i++) {\n mem[i] = -1;\n }\n int result = dfs(n, mem);\n free(mem);\n return result;\n}\n
climbing_stairs_dfs_mem.zig// \u8bb0\u5fc6\u5316\u641c\u7d22\nfn dfs(i: usize, mem: []i32) i32 {\n // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n if (i == 1 or i == 2) {\n return @intCast(i);\n }\n // \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\n if (mem[i] != -1) {\n return mem[i];\n }\n // dp[i] = dp[i-1] + dp[i-2]\n var count = dfs(i - 1, mem) + dfs(i - 2, mem);\n // \u8bb0\u5f55 dp[i]\n mem[i] = count;\n return count;\n}\n\n// \u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22\nfn climbingStairsDFSMem(comptime n: usize) i32 {\n // mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\n var mem = [_]i32{ -1 } ** (n + 1);\n return dfs(n, &mem);\n}\n
\u89c2\u5bdf\u56fe 14-4 \uff0c\u7ecf\u8fc7\u8bb0\u5fc6\u5316\u5904\u7406\u540e\uff0c\u6240\u6709\u91cd\u53e0\u5b50\u95ee\u9898\u90fd\u53ea\u9700\u8ba1\u7b97\u4e00\u6b21\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4f18\u5316\u81f3 \\(O(n)\\) \uff0c\u8fd9\u662f\u4e00\u4e2a\u5de8\u5927\u7684\u98de\u8dc3\u3002
\u56fe 14-4 \u00a0 \u8bb0\u5fc6\u5316\u641c\u7d22\u5bf9\u5e94\u9012\u5f52\u6811
"},{"location":"chapter_dynamic_programming/intro_to_dynamic_programming/#1413","title":"14.1.3 \u00a0 \u65b9\u6cd5\u4e09\uff1a\u52a8\u6001\u89c4\u5212","text":"\u8bb0\u5fc6\u5316\u641c\u7d22\u662f\u4e00\u79cd\u201c\u4ece\u9876\u81f3\u5e95\u201d\u7684\u65b9\u6cd5\uff1a\u6211\u4eec\u4ece\u539f\u95ee\u9898\uff08\u6839\u8282\u70b9\uff09\u5f00\u59cb\uff0c\u9012\u5f52\u5730\u5c06\u8f83\u5927\u5b50\u95ee\u9898\u5206\u89e3\u4e3a\u8f83\u5c0f\u5b50\u95ee\u9898\uff0c\u76f4\u81f3\u89e3\u5df2\u77e5\u7684\u6700\u5c0f\u5b50\u95ee\u9898\uff08\u53f6\u8282\u70b9\uff09\u3002\u4e4b\u540e\uff0c\u901a\u8fc7\u56de\u6eaf\u9010\u5c42\u6536\u96c6\u5b50\u95ee\u9898\u7684\u89e3\uff0c\u6784\u5efa\u51fa\u539f\u95ee\u9898\u7684\u89e3\u3002
\u4e0e\u4e4b\u76f8\u53cd\uff0c\u52a8\u6001\u89c4\u5212\u662f\u4e00\u79cd\u201c\u4ece\u5e95\u81f3\u9876\u201d\u7684\u65b9\u6cd5\uff1a\u4ece\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\u5f00\u59cb\uff0c\u8fed\u4ee3\u5730\u6784\u5efa\u66f4\u5927\u5b50\u95ee\u9898\u7684\u89e3\uff0c\u76f4\u81f3\u5f97\u5230\u539f\u95ee\u9898\u7684\u89e3\u3002
\u7531\u4e8e\u52a8\u6001\u89c4\u5212\u4e0d\u5305\u542b\u56de\u6eaf\u8fc7\u7a0b\uff0c\u56e0\u6b64\u53ea\u9700\u4f7f\u7528\u5faa\u73af\u8fed\u4ee3\u5b9e\u73b0\uff0c\u65e0\u987b\u4f7f\u7528\u9012\u5f52\u3002\u5728\u4ee5\u4e0b\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521d\u59cb\u5316\u4e00\u4e2a\u6570\u7ec4 dp
\u6765\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\uff0c\u5b83\u8d77\u5230\u4e86\u4e0e\u8bb0\u5fc6\u5316\u641c\u7d22\u4e2d\u6570\u7ec4 mem
\u76f8\u540c\u7684\u8bb0\u5f55\u4f5c\u7528\uff1a
def climbing_stairs_dp(n: int) -> int:\n \"\"\"\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212\"\"\"\n if n == 1 or n == 2:\n return n\n # \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n dp = [0] * (n + 1)\n # \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n dp[1], dp[2] = 1, 2\n # \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n for i in range(3, n + 1):\n dp[i] = dp[i - 1] + dp[i - 2]\n return dp[n]\n
climbing_stairs_dp.cpp/* \u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nint climbingStairsDP(int n) {\n if (n == 1 || n == 2)\n return n;\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n vector<int> dp(n + 1);\n // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n dp[1] = 1;\n dp[2] = 2;\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n for (int i = 3; i <= n; i++) {\n dp[i] = dp[i - 1] + dp[i - 2];\n }\n return dp[n];\n}\n
climbing_stairs_dp.java/* \u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nint climbingStairsDP(int n) {\n if (n == 1 || n == 2)\n return n;\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n int[] dp = new int[n + 1];\n // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n dp[1] = 1;\n dp[2] = 2;\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n for (int i = 3; i <= n; i++) {\n dp[i] = dp[i - 1] + dp[i - 2];\n }\n return dp[n];\n}\n
climbing_stairs_dp.cs/* \u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nint ClimbingStairsDP(int n) {\n if (n == 1 || n == 2)\n return n;\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n int[] dp = new int[n + 1];\n // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n dp[1] = 1;\n dp[2] = 2;\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n for (int i = 3; i <= n; i++) {\n dp[i] = dp[i - 1] + dp[i - 2];\n }\n return dp[n];\n}\n
climbing_stairs_dp.go/* \u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc climbingStairsDP(n int) int {\n if n == 1 || n == 2 {\n return n\n }\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n dp := make([]int, n+1)\n // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n dp[1] = 1\n dp[2] = 2\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n for i := 3; i <= n; i++ {\n dp[i] = dp[i-1] + dp[i-2]\n }\n return dp[n]\n}\n
climbing_stairs_dp.swift/* \u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc climbingStairsDP(n: Int) -> Int {\n if n == 1 || n == 2 {\n return n\n }\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n var dp = Array(repeating: 0, count: n + 1)\n // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n dp[1] = 1\n dp[2] = 2\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n for i in stride(from: 3, through: n, by: 1) {\n dp[i] = dp[i - 1] + dp[i - 2]\n }\n return dp[n]\n}\n
climbing_stairs_dp.js/* \u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction climbingStairsDP(n) {\n if (n === 1 || n === 2) return n;\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n const dp = new Array(n + 1).fill(-1);\n // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n dp[1] = 1;\n dp[2] = 2;\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n for (let i = 3; i <= n; i++) {\n dp[i] = dp[i - 1] + dp[i - 2];\n }\n return dp[n];\n}\n
climbing_stairs_dp.ts/* \u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction climbingStairsDP(n: number): number {\n if (n === 1 || n === 2) return n;\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n const dp = new Array(n + 1).fill(-1);\n // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n dp[1] = 1;\n dp[2] = 2;\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n for (let i = 3; i <= n; i++) {\n dp[i] = dp[i - 1] + dp[i - 2];\n }\n return dp[n];\n}\n
climbing_stairs_dp.dart/* \u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nint climbingStairsDP(int n) {\n if (n == 1 || n == 2) return n;\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n List<int> dp = List.filled(n + 1, 0);\n // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n dp[1] = 1;\n dp[2] = 2;\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n for (int i = 3; i <= n; i++) {\n dp[i] = dp[i - 1] + dp[i - 2];\n }\n return dp[n];\n}\n
climbing_stairs_dp.rs/* \u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nfn climbing_stairs_dp(n: usize) -> i32 {\n // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n if n == 1 || n == 2 { return n as i32; }\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n let mut dp = vec![-1; n + 1];\n // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n dp[1] = 1;\n dp[2] = 2;\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n for i in 3..=n {\n dp[i] = dp[i - 1] + dp[i - 2];\n }\n dp[n]\n}\n
climbing_stairs_dp.c/* \u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nint climbingStairsDP(int n) {\n if (n == 1 || n == 2)\n return n;\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n int *dp = (int *)malloc((n + 1) * sizeof(int));\n // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n dp[1] = 1;\n dp[2] = 2;\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n for (int i = 3; i <= n; i++) {\n dp[i] = dp[i - 1] + dp[i - 2];\n }\n int result = dp[n];\n free(dp);\n return result;\n}\n
climbing_stairs_dp.zig// \u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212\nfn climbingStairsDP(comptime n: usize) i32 {\n // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n if (n == 1 or n == 2) {\n return @intCast(n);\n }\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n var dp = [_]i32{-1} ** (n + 1);\n // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n dp[1] = 1;\n dp[2] = 2;\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n for (3..n + 1) |i| {\n dp[i] = dp[i - 1] + dp[i - 2];\n }\n return dp[n];\n}\n
\u56fe 14-5 \u6a21\u62df\u4e86\u4ee5\u4e0a\u4ee3\u7801\u7684\u6267\u884c\u8fc7\u7a0b\u3002
\u56fe 14-5 \u00a0 \u722c\u697c\u68af\u7684\u52a8\u6001\u89c4\u5212\u8fc7\u7a0b
\u4e0e\u56de\u6eaf\u7b97\u6cd5\u4e00\u6837\uff0c\u52a8\u6001\u89c4\u5212\u4e5f\u4f7f\u7528\u201c\u72b6\u6001\u201d\u6982\u5ff5\u6765\u8868\u793a\u95ee\u9898\u6c42\u89e3\u7684\u7279\u5b9a\u9636\u6bb5\uff0c\u6bcf\u4e2a\u72b6\u6001\u90fd\u5bf9\u5e94\u4e00\u4e2a\u5b50\u95ee\u9898\u4ee5\u53ca\u76f8\u5e94\u7684\u5c40\u90e8\u6700\u4f18\u89e3\u3002\u4f8b\u5982\uff0c\u722c\u697c\u68af\u95ee\u9898\u7684\u72b6\u6001\u5b9a\u4e49\u4e3a\u5f53\u524d\u6240\u5728\u697c\u68af\u9636\u6570 \\(i\\) \u3002
\u6839\u636e\u4ee5\u4e0a\u5185\u5bb9\uff0c\u6211\u4eec\u53ef\u4ee5\u603b\u7ed3\u51fa\u52a8\u6001\u89c4\u5212\u7684\u5e38\u7528\u672f\u8bed\u3002
dp
\u79f0\u4e3a\u300c\\(dp\\) \u8868\u300d\uff0c\\(dp[i]\\) \u8868\u793a\u72b6\u6001 \\(i\\) \u5bf9\u5e94\u5b50\u95ee\u9898\u7684\u89e3\u3002\u7ec6\u5fc3\u7684\u8bfb\u8005\u53ef\u80fd\u53d1\u73b0\u4e86\uff0c\u7531\u4e8e \\(dp[i]\\) \u53ea\u4e0e \\(dp[i-1]\\) \u548c \\(dp[i-2]\\) \u6709\u5173\uff0c\u56e0\u6b64\u6211\u4eec\u65e0\u987b\u4f7f\u7528\u4e00\u4e2a\u6570\u7ec4 dp
\u6765\u5b58\u50a8\u6240\u6709\u5b50\u95ee\u9898\u7684\u89e3\uff0c\u800c\u53ea\u9700\u4e24\u4e2a\u53d8\u91cf\u6eda\u52a8\u524d\u8fdb\u5373\u53ef\u3002\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a
def climbing_stairs_dp_comp(n: int) -> int:\n \"\"\"\u722c\u697c\u68af\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\"\"\"\n if n == 1 or n == 2:\n return n\n a, b = 1, 2\n for _ in range(3, n + 1):\n a, b = b, a + b\n return b\n
climbing_stairs_dp.cpp/* \u722c\u697c\u68af\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint climbingStairsDPComp(int n) {\n if (n == 1 || n == 2)\n return n;\n int a = 1, b = 2;\n for (int i = 3; i <= n; i++) {\n int tmp = b;\n b = a + b;\n a = tmp;\n }\n return b;\n}\n
climbing_stairs_dp.java/* \u722c\u697c\u68af\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint climbingStairsDPComp(int n) {\n if (n == 1 || n == 2)\n return n;\n int a = 1, b = 2;\n for (int i = 3; i <= n; i++) {\n int tmp = b;\n b = a + b;\n a = tmp;\n }\n return b;\n}\n
climbing_stairs_dp.cs/* \u722c\u697c\u68af\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint ClimbingStairsDPComp(int n) {\n if (n == 1 || n == 2)\n return n;\n int a = 1, b = 2;\n for (int i = 3; i <= n; i++) {\n int tmp = b;\n b = a + b;\n a = tmp;\n }\n return b;\n}\n
climbing_stairs_dp.go/* \u722c\u697c\u68af\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc climbingStairsDPComp(n int) int {\n if n == 1 || n == 2 {\n return n\n }\n a, b := 1, 2\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n for i := 3; i <= n; i++ {\n a, b = b, a+b\n }\n return b\n}\n
climbing_stairs_dp.swift/* \u722c\u697c\u68af\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc climbingStairsDPComp(n: Int) -> Int {\n if n == 1 || n == 2 {\n return n\n }\n var a = 1\n var b = 2\n for _ in stride(from: 3, through: n, by: 1) {\n (a, b) = (b, a + b)\n }\n return b\n}\n
climbing_stairs_dp.js/* \u722c\u697c\u68af\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction climbingStairsDPComp(n) {\n if (n === 1 || n === 2) return n;\n let a = 1,\n b = 2;\n for (let i = 3; i <= n; i++) {\n const tmp = b;\n b = a + b;\n a = tmp;\n }\n return b;\n}\n
climbing_stairs_dp.ts/* \u722c\u697c\u68af\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction climbingStairsDPComp(n: number): number {\n if (n === 1 || n === 2) return n;\n let a = 1,\n b = 2;\n for (let i = 3; i <= n; i++) {\n const tmp = b;\n b = a + b;\n a = tmp;\n }\n return b;\n}\n
climbing_stairs_dp.dart/* \u722c\u697c\u68af\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint climbingStairsDPComp(int n) {\n if (n == 1 || n == 2) return n;\n int a = 1, b = 2;\n for (int i = 3; i <= n; i++) {\n int tmp = b;\n b = a + b;\n a = tmp;\n }\n return b;\n}\n
climbing_stairs_dp.rs/* \u722c\u697c\u68af\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfn climbing_stairs_dp_comp(n: usize) -> i32 {\n if n == 1 || n == 2 { return n as i32; }\n let (mut a, mut b) = (1, 2);\n for _ in 3..=n {\n let tmp = b;\n b = a + b;\n a = tmp;\n }\n b\n}\n
climbing_stairs_dp.c/* \u722c\u697c\u68af\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint climbingStairsDPComp(int n) {\n if (n == 1 || n == 2)\n return n;\n int a = 1, b = 2;\n for (int i = 3; i <= n; i++) {\n int tmp = b;\n b = a + b;\n a = tmp;\n }\n return b;\n}\n
climbing_stairs_dp.zig// \u722c\u697c\u68af\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\nfn climbingStairsDPComp(comptime n: usize) i32 {\n if (n == 1 or n == 2) {\n return @intCast(n);\n }\n var a: i32 = 1;\n var b: i32 = 2;\n for (3..n + 1) |_| {\n var tmp = b;\n b = a + b;\n a = tmp;\n }\n return b;\n}\n
\u89c2\u5bdf\u4ee5\u4e0a\u4ee3\u7801\uff0c\u7531\u4e8e\u7701\u53bb\u4e86\u6570\u7ec4 dp
\u5360\u7528\u7684\u7a7a\u95f4\uff0c\u56e0\u6b64\u7a7a\u95f4\u590d\u6742\u5ea6\u4ece \\(O(n)\\) \u964d\u81f3 \\(O(1)\\) \u3002
\u5728\u52a8\u6001\u89c4\u5212\u95ee\u9898\u4e2d\uff0c\u5f53\u524d\u72b6\u6001\u5f80\u5f80\u4ec5\u4e0e\u524d\u9762\u6709\u9650\u4e2a\u72b6\u6001\u6709\u5173\uff0c\u8fd9\u65f6\u6211\u4eec\u53ef\u4ee5\u53ea\u4fdd\u7559\u5fc5\u8981\u7684\u72b6\u6001\uff0c\u901a\u8fc7\u201c\u964d\u7ef4\u201d\u6765\u8282\u7701\u5185\u5b58\u7a7a\u95f4\u3002\u8fd9\u79cd\u7a7a\u95f4\u4f18\u5316\u6280\u5de7\u88ab\u79f0\u4e3a\u201c\u6eda\u52a8\u53d8\u91cf\u201d\u6216\u201c\u6eda\u52a8\u6570\u7ec4\u201d\u3002
"},{"location":"chapter_dynamic_programming/knapsack_problem/","title":"14.4 \u00a0 0-1 \u80cc\u5305\u95ee\u9898","text":"\u80cc\u5305\u95ee\u9898\u662f\u4e00\u4e2a\u975e\u5e38\u597d\u7684\u52a8\u6001\u89c4\u5212\u5165\u95e8\u9898\u76ee\uff0c\u662f\u52a8\u6001\u89c4\u5212\u4e2d\u6700\u5e38\u89c1\u7684\u95ee\u9898\u5f62\u5f0f\u3002\u5176\u5177\u6709\u5f88\u591a\u53d8\u79cd\uff0c\u4f8b\u5982 0-1 \u80cc\u5305\u95ee\u9898\u3001\u5b8c\u5168\u80cc\u5305\u95ee\u9898\u3001\u591a\u91cd\u80cc\u5305\u95ee\u9898\u7b49\u3002
\u5728\u672c\u8282\u4e2d\uff0c\u6211\u4eec\u5148\u6765\u6c42\u89e3\u6700\u5e38\u89c1\u7684 0-1 \u80cc\u5305\u95ee\u9898\u3002
Question
\u7ed9\u5b9a \\(n\\) \u4e2a\u7269\u54c1\uff0c\u7b2c \\(i\\) \u4e2a\u7269\u54c1\u7684\u91cd\u91cf\u4e3a \\(wgt[i-1]\\)\u3001\u4ef7\u503c\u4e3a \\(val[i-1]\\) \uff0c\u548c\u4e00\u4e2a\u5bb9\u91cf\u4e3a \\(cap\\) \u7684\u80cc\u5305\u3002\u6bcf\u4e2a\u7269\u54c1\u53ea\u80fd\u9009\u62e9\u4e00\u6b21\uff0c\u95ee\u5728\u9650\u5b9a\u80cc\u5305\u5bb9\u91cf\u4e0b\u80fd\u653e\u5165\u7269\u54c1\u7684\u6700\u5927\u4ef7\u503c\u3002
\u89c2\u5bdf\u56fe 14-17 \uff0c\u7531\u4e8e\u7269\u54c1\u7f16\u53f7 \\(i\\) \u4ece \\(1\\) \u5f00\u59cb\u8ba1\u6570\uff0c\u6570\u7ec4\u7d22\u5f15\u4ece \\(0\\) \u5f00\u59cb\u8ba1\u6570\uff0c\u56e0\u6b64\u7269\u54c1 \\(i\\) \u5bf9\u5e94\u91cd\u91cf \\(wgt[i-1]\\) \u548c\u4ef7\u503c \\(val[i-1]\\) \u3002
\u56fe 14-17 \u00a0 0-1 \u80cc\u5305\u7684\u793a\u4f8b\u6570\u636e
\u6211\u4eec\u53ef\u4ee5\u5c06 0-1 \u80cc\u5305\u95ee\u9898\u770b\u4f5c\u4e00\u4e2a\u7531 \\(n\\) \u8f6e\u51b3\u7b56\u7ec4\u6210\u7684\u8fc7\u7a0b\uff0c\u5bf9\u4e8e\u6bcf\u4e2a\u7269\u4f53\u90fd\u6709\u4e0d\u653e\u5165\u548c\u653e\u5165\u4e24\u79cd\u51b3\u7b56\uff0c\u56e0\u6b64\u8be5\u95ee\u9898\u6ee1\u8db3\u51b3\u7b56\u6811\u6a21\u578b\u3002
\u8be5\u95ee\u9898\u7684\u76ee\u6807\u662f\u6c42\u89e3\u201c\u5728\u9650\u5b9a\u80cc\u5305\u5bb9\u91cf\u4e0b\u80fd\u653e\u5165\u7269\u54c1\u7684\u6700\u5927\u4ef7\u503c\u201d\uff0c\u56e0\u6b64\u8f83\u5927\u6982\u7387\u662f\u4e00\u4e2a\u52a8\u6001\u89c4\u5212\u95ee\u9898\u3002
\u7b2c\u4e00\u6b65\uff1a\u601d\u8003\u6bcf\u8f6e\u7684\u51b3\u7b56\uff0c\u5b9a\u4e49\u72b6\u6001\uff0c\u4ece\u800c\u5f97\u5230 \\(dp\\) \u8868
\u5bf9\u4e8e\u6bcf\u4e2a\u7269\u54c1\u6765\u8bf4\uff0c\u4e0d\u653e\u5165\u80cc\u5305\uff0c\u80cc\u5305\u5bb9\u91cf\u4e0d\u53d8\uff1b\u653e\u5165\u80cc\u5305\uff0c\u80cc\u5305\u5bb9\u91cf\u51cf\u5c0f\u3002\u7531\u6b64\u53ef\u5f97\u72b6\u6001\u5b9a\u4e49\uff1a\u5f53\u524d\u7269\u54c1\u7f16\u53f7 \\(i\\) \u548c\u5269\u4f59\u80cc\u5305\u5bb9\u91cf \\(c\\) \uff0c\u8bb0\u4e3a \\([i, c]\\) \u3002
\u72b6\u6001 \\([i, c]\\) \u5bf9\u5e94\u7684\u5b50\u95ee\u9898\u4e3a\uff1a\u524d \\(i\\) \u4e2a\u7269\u54c1\u5728\u5269\u4f59\u5bb9\u91cf\u4e3a \\(c\\) \u7684\u80cc\u5305\u4e2d\u7684\u6700\u5927\u4ef7\u503c\uff0c\u8bb0\u4e3a \\(dp[i, c]\\) \u3002
\u5f85\u6c42\u89e3\u7684\u662f \\(dp[n, cap]\\) \uff0c\u56e0\u6b64\u9700\u8981\u4e00\u4e2a\u5c3a\u5bf8\u4e3a \\((n+1) \\times (cap+1)\\) \u7684\u4e8c\u7ef4 \\(dp\\) \u8868\u3002
\u7b2c\u4e8c\u6b65\uff1a\u627e\u51fa\u6700\u4f18\u5b50\u7ed3\u6784\uff0c\u8fdb\u800c\u63a8\u5bfc\u51fa\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b
\u5f53\u6211\u4eec\u505a\u51fa\u7269\u54c1 \\(i\\) \u7684\u51b3\u7b56\u540e\uff0c\u5269\u4f59\u7684\u662f\u524d \\(i-1\\) \u4e2a\u7269\u54c1\u7684\u51b3\u7b56\uff0c\u53ef\u5206\u4e3a\u4ee5\u4e0b\u4e24\u79cd\u60c5\u51b5\u3002
\u4e0a\u8ff0\u5206\u6790\u5411\u6211\u4eec\u63ed\u793a\u4e86\u672c\u9898\u7684\u6700\u4f18\u5b50\u7ed3\u6784\uff1a\u6700\u5927\u4ef7\u503c \\(dp[i, c]\\) \u7b49\u4e8e\u4e0d\u653e\u5165\u7269\u54c1 \\(i\\) \u548c\u653e\u5165\u7269\u54c1 \\(i\\) \u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\u3002\u7531\u6b64\u53ef\u63a8\u5bfc\u51fa\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\uff1a
\\[ dp[i, c] = \\max(dp[i-1, c], dp[i-1, c - wgt[i-1]] + val[i-1]) \\]\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u82e5\u5f53\u524d\u7269\u54c1\u91cd\u91cf \\(wgt[i - 1]\\) \u8d85\u51fa\u5269\u4f59\u80cc\u5305\u5bb9\u91cf \\(c\\) \uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\u3002
\u7b2c\u4e09\u6b65\uff1a\u786e\u5b9a\u8fb9\u754c\u6761\u4ef6\u548c\u72b6\u6001\u8f6c\u79fb\u987a\u5e8f
\u5f53\u65e0\u7269\u54c1\u6216\u65e0\u5269\u4f59\u80cc\u5305\u5bb9\u91cf\u65f6\u6700\u5927\u4ef7\u503c\u4e3a \\(0\\) \uff0c\u5373\u9996\u5217 \\(dp[i, 0]\\) \u548c\u9996\u884c \\(dp[0, c]\\) \u90fd\u7b49\u4e8e \\(0\\) \u3002
\u5f53\u524d\u72b6\u6001 \\([i, c]\\) \u4ece\u4e0a\u65b9\u7684\u72b6\u6001 \\([i-1, c]\\) \u548c\u5de6\u4e0a\u65b9\u7684\u72b6\u6001 \\([i-1, c-wgt[i-1]]\\) \u8f6c\u79fb\u800c\u6765\uff0c\u56e0\u6b64\u901a\u8fc7\u4e24\u5c42\u5faa\u73af\u6b63\u5e8f\u904d\u5386\u6574\u4e2a \\(dp\\) \u8868\u5373\u53ef\u3002
\u6839\u636e\u4ee5\u4e0a\u5206\u6790\uff0c\u6211\u4eec\u63a5\u4e0b\u6765\u6309\u987a\u5e8f\u5b9e\u73b0\u66b4\u529b\u641c\u7d22\u3001\u8bb0\u5fc6\u5316\u641c\u7d22\u3001\u52a8\u6001\u89c4\u5212\u89e3\u6cd5\u3002
"},{"location":"chapter_dynamic_programming/knapsack_problem/#1","title":"1. \u00a0 \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u641c\u7d22","text":"\u641c\u7d22\u4ee3\u7801\u5305\u542b\u4ee5\u4e0b\u8981\u7d20\u3002
def knapsack_dfs(wgt: list[int], val: list[int], i: int, c: int) -> int:\n \"\"\"0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u7d22\"\"\"\n # \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n if i == 0 or c == 0:\n return 0\n # \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n if wgt[i - 1] > c:\n return knapsack_dfs(wgt, val, i - 1, c)\n # \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n no = knapsack_dfs(wgt, val, i - 1, c)\n yes = knapsack_dfs(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1]\n # \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n return max(no, yes)\n
knapsack.cpp/* 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u7d22 */\nint knapsackDFS(vector<int> &wgt, vector<int> &val, int i, int c) {\n // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n if (i == 0 || c == 0) {\n return 0;\n }\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n if (wgt[i - 1] > c) {\n return knapsackDFS(wgt, val, i - 1, c);\n }\n // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n int no = knapsackDFS(wgt, val, i - 1, c);\n int yes = knapsackDFS(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1];\n // \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n return max(no, yes);\n}\n
knapsack.java/* 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u7d22 */\nint knapsackDFS(int[] wgt, int[] val, int i, int c) {\n // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n if (i == 0 || c == 0) {\n return 0;\n }\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n if (wgt[i - 1] > c) {\n return knapsackDFS(wgt, val, i - 1, c);\n }\n // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n int no = knapsackDFS(wgt, val, i - 1, c);\n int yes = knapsackDFS(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1];\n // \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n return Math.max(no, yes);\n}\n
knapsack.cs/* 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u7d22 */\nint KnapsackDFS(int[] weight, int[] val, int i, int c) {\n // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n if (i == 0 || c == 0) {\n return 0;\n }\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n if (weight[i - 1] > c) {\n return KnapsackDFS(weight, val, i - 1, c);\n }\n // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n int no = KnapsackDFS(weight, val, i - 1, c);\n int yes = KnapsackDFS(weight, val, i - 1, c - weight[i - 1]) + val[i - 1];\n // \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n return Math.Max(no, yes);\n}\n
knapsack.go/* 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u7d22 */\nfunc knapsackDFS(wgt, val []int, i, c int) int {\n // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n if i == 0 || c == 0 {\n return 0\n }\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n if wgt[i-1] > c {\n return knapsackDFS(wgt, val, i-1, c)\n }\n // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n no := knapsackDFS(wgt, val, i-1, c)\n yes := knapsackDFS(wgt, val, i-1, c-wgt[i-1]) + val[i-1]\n // \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n return int(math.Max(float64(no), float64(yes)))\n}\n
knapsack.swift/* 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u7d22 */\nfunc knapsackDFS(wgt: [Int], val: [Int], i: Int, c: Int) -> Int {\n // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n if i == 0 || c == 0 {\n return 0\n }\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n if wgt[i - 1] > c {\n return knapsackDFS(wgt: wgt, val: val, i: i - 1, c: c)\n }\n // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n let no = knapsackDFS(wgt: wgt, val: val, i: i - 1, c: c)\n let yes = knapsackDFS(wgt: wgt, val: val, i: i - 1, c: c - wgt[i - 1]) + val[i - 1]\n // \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n return max(no, yes)\n}\n
knapsack.js/* 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u7d22 */\nfunction knapsackDFS(wgt, val, i, c) {\n // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n if (i === 0 || c === 0) {\n return 0;\n }\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n if (wgt[i - 1] > c) {\n return knapsackDFS(wgt, val, i - 1, c);\n }\n // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n const no = knapsackDFS(wgt, val, i - 1, c);\n const yes = knapsackDFS(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1];\n // \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n return Math.max(no, yes);\n}\n
knapsack.ts/* 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u7d22 */\nfunction knapsackDFS(\n wgt: Array<number>,\n val: Array<number>,\n i: number,\n c: number\n): number {\n // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n if (i === 0 || c === 0) {\n return 0;\n }\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n if (wgt[i - 1] > c) {\n return knapsackDFS(wgt, val, i - 1, c);\n }\n // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n const no = knapsackDFS(wgt, val, i - 1, c);\n const yes = knapsackDFS(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1];\n // \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n return Math.max(no, yes);\n}\n
knapsack.dart/* 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u7d22 */\nint knapsackDFS(List<int> wgt, List<int> val, int i, int c) {\n // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n if (i == 0 || c == 0) {\n return 0;\n }\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n if (wgt[i - 1] > c) {\n return knapsackDFS(wgt, val, i - 1, c);\n }\n // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n int no = knapsackDFS(wgt, val, i - 1, c);\n int yes = knapsackDFS(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1];\n // \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n return max(no, yes);\n}\n
knapsack.rs/* 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u7d22 */\nfn knapsack_dfs(wgt: &[i32], val: &[i32], i: usize, c: usize) -> i32 {\n // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n if i == 0 || c == 0 {\n return 0;\n }\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n if wgt[i - 1] > c as i32 {\n return knapsack_dfs(wgt, val, i - 1, c);\n }\n // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n let no = knapsack_dfs(wgt, val, i - 1, c);\n let yes = knapsack_dfs(wgt, val, i - 1, c - wgt[i - 1] as usize) + val[i - 1];\n // \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n std::cmp::max(no, yes)\n}\n
knapsack.c/* 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u7d22 */\nint knapsackDFS(int wgt[], int val[], int i, int c) {\n // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n if (i == 0 || c == 0) {\n return 0;\n }\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n if (wgt[i - 1] > c) {\n return knapsackDFS(wgt, val, i - 1, c);\n }\n // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n int no = knapsackDFS(wgt, val, i - 1, c);\n int yes = knapsackDFS(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1];\n // \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n return myMax(no, yes);\n}\n
knapsack.zig// 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u7d22\nfn knapsackDFS(wgt: []i32, val: []i32, i: usize, c: usize) i32 {\n // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n if (i == 0 or c == 0) {\n return 0;\n }\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n if (wgt[i - 1] > c) {\n return knapsackDFS(wgt, val, i - 1, c);\n }\n // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n var no = knapsackDFS(wgt, val, i - 1, c);\n var yes = knapsackDFS(wgt, val, i - 1, c - @as(usize, @intCast(wgt[i - 1]))) + val[i - 1];\n // \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n return @max(no, yes);\n}\n
\u5982\u56fe 14-18 \u6240\u793a\uff0c\u7531\u4e8e\u6bcf\u4e2a\u7269\u54c1\u90fd\u4f1a\u4ea7\u751f\u4e0d\u9009\u548c\u9009\u4e24\u6761\u641c\u7d22\u5206\u652f\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(2^n)\\) \u3002
\u89c2\u5bdf\u9012\u5f52\u6811\uff0c\u5bb9\u6613\u53d1\u73b0\u5176\u4e2d\u5b58\u5728\u91cd\u53e0\u5b50\u95ee\u9898\uff0c\u4f8b\u5982 \\(dp[1, 10]\\) \u7b49\u3002\u800c\u5f53\u7269\u54c1\u8f83\u591a\u3001\u80cc\u5305\u5bb9\u91cf\u8f83\u5927\uff0c\u5c24\u5176\u662f\u76f8\u540c\u91cd\u91cf\u7684\u7269\u54c1\u8f83\u591a\u65f6\uff0c\u91cd\u53e0\u5b50\u95ee\u9898\u7684\u6570\u91cf\u5c06\u4f1a\u5927\u5e45\u589e\u591a\u3002
\u56fe 14-18 \u00a0 0-1 \u80cc\u5305\u95ee\u9898\u7684\u66b4\u529b\u641c\u7d22\u9012\u5f52\u6811
"},{"location":"chapter_dynamic_programming/knapsack_problem/#2","title":"2. \u00a0 \u65b9\u6cd5\u4e8c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22","text":"\u4e3a\u4e86\u4fdd\u8bc1\u91cd\u53e0\u5b50\u95ee\u9898\u53ea\u88ab\u8ba1\u7b97\u4e00\u6b21\uff0c\u6211\u4eec\u501f\u52a9\u8bb0\u5fc6\u5217\u8868 mem
\u6765\u8bb0\u5f55\u5b50\u95ee\u9898\u7684\u89e3\uff0c\u5176\u4e2d mem[i][c]
\u5bf9\u5e94 \\(dp[i, c]\\) \u3002
\u5f15\u5165\u8bb0\u5fc6\u5316\u4e4b\u540e\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u53d6\u51b3\u4e8e\u5b50\u95ee\u9898\u6570\u91cf\uff0c\u4e5f\u5c31\u662f \\(O(n \\times cap)\\) \u3002\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig knapsack.pydef knapsack_dfs_mem(\n wgt: list[int], val: list[int], mem: list[list[int]], i: int, c: int\n) -> int:\n \"\"\"0-1 \u80cc\u5305\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22\"\"\"\n # \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n if i == 0 or c == 0:\n return 0\n # \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if mem[i][c] != -1:\n return mem[i][c]\n # \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n if wgt[i - 1] > c:\n return knapsack_dfs_mem(wgt, val, mem, i - 1, c)\n # \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n no = knapsack_dfs_mem(wgt, val, mem, i - 1, c)\n yes = knapsack_dfs_mem(wgt, val, mem, i - 1, c - wgt[i - 1]) + val[i - 1]\n # \u8bb0\u5f55\u5e76\u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n mem[i][c] = max(no, yes)\n return mem[i][c]\n
knapsack.cpp/* 0-1 \u80cc\u5305\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint knapsackDFSMem(vector<int> &wgt, vector<int> &val, vector<vector<int>> &mem, int i, int c) {\n // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n if (i == 0 || c == 0) {\n return 0;\n }\n // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if (mem[i][c] != -1) {\n return mem[i][c];\n }\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n if (wgt[i - 1] > c) {\n return knapsackDFSMem(wgt, val, mem, i - 1, c);\n }\n // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n int no = knapsackDFSMem(wgt, val, mem, i - 1, c);\n int yes = knapsackDFSMem(wgt, val, mem, i - 1, c - wgt[i - 1]) + val[i - 1];\n // \u8bb0\u5f55\u5e76\u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n mem[i][c] = max(no, yes);\n return mem[i][c];\n}\n
knapsack.java/* 0-1 \u80cc\u5305\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint knapsackDFSMem(int[] wgt, int[] val, int[][] mem, int i, int c) {\n // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n if (i == 0 || c == 0) {\n return 0;\n }\n // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if (mem[i][c] != -1) {\n return mem[i][c];\n }\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n if (wgt[i - 1] > c) {\n return knapsackDFSMem(wgt, val, mem, i - 1, c);\n }\n // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n int no = knapsackDFSMem(wgt, val, mem, i - 1, c);\n int yes = knapsackDFSMem(wgt, val, mem, i - 1, c - wgt[i - 1]) + val[i - 1];\n // \u8bb0\u5f55\u5e76\u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n mem[i][c] = Math.max(no, yes);\n return mem[i][c];\n}\n
knapsack.cs/* 0-1 \u80cc\u5305\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint KnapsackDFSMem(int[] weight, int[] val, int[][] mem, int i, int c) {\n // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n if (i == 0 || c == 0) {\n return 0;\n }\n // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if (mem[i][c] != -1) {\n return mem[i][c];\n }\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n if (weight[i - 1] > c) {\n return KnapsackDFSMem(weight, val, mem, i - 1, c);\n }\n // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n int no = KnapsackDFSMem(weight, val, mem, i - 1, c);\n int yes = KnapsackDFSMem(weight, val, mem, i - 1, c - weight[i - 1]) + val[i - 1];\n // \u8bb0\u5f55\u5e76\u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n mem[i][c] = Math.Max(no, yes);\n return mem[i][c];\n}\n
knapsack.go/* 0-1 \u80cc\u5305\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunc knapsackDFSMem(wgt, val []int, mem [][]int, i, c int) int {\n // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n if i == 0 || c == 0 {\n return 0\n }\n // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if mem[i][c] != -1 {\n return mem[i][c]\n }\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n if wgt[i-1] > c {\n return knapsackDFSMem(wgt, val, mem, i-1, c)\n }\n // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n no := knapsackDFSMem(wgt, val, mem, i-1, c)\n yes := knapsackDFSMem(wgt, val, mem, i-1, c-wgt[i-1]) + val[i-1]\n // \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n mem[i][c] = int(math.Max(float64(no), float64(yes)))\n return mem[i][c]\n}\n
knapsack.swift/* 0-1 \u80cc\u5305\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunc knapsackDFSMem(wgt: [Int], val: [Int], mem: inout [[Int]], i: Int, c: Int) -> Int {\n // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n if i == 0 || c == 0 {\n return 0\n }\n // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if mem[i][c] != -1 {\n return mem[i][c]\n }\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n if wgt[i - 1] > c {\n return knapsackDFSMem(wgt: wgt, val: val, mem: &mem, i: i - 1, c: c)\n }\n // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n let no = knapsackDFSMem(wgt: wgt, val: val, mem: &mem, i: i - 1, c: c)\n let yes = knapsackDFSMem(wgt: wgt, val: val, mem: &mem, i: i - 1, c: c - wgt[i - 1]) + val[i - 1]\n // \u8bb0\u5f55\u5e76\u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n mem[i][c] = max(no, yes)\n return mem[i][c]\n}\n
knapsack.js/* 0-1 \u80cc\u5305\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunction knapsackDFSMem(wgt, val, mem, i, c) {\n // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n if (i === 0 || c === 0) {\n return 0;\n }\n // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if (mem[i][c] !== -1) {\n return mem[i][c];\n }\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n if (wgt[i - 1] > c) {\n return knapsackDFSMem(wgt, val, mem, i - 1, c);\n }\n // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n const no = knapsackDFSMem(wgt, val, mem, i - 1, c);\n const yes =\n knapsackDFSMem(wgt, val, mem, i - 1, c - wgt[i - 1]) + val[i - 1];\n // \u8bb0\u5f55\u5e76\u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n mem[i][c] = Math.max(no, yes);\n return mem[i][c];\n}\n
knapsack.ts/* 0-1 \u80cc\u5305\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunction knapsackDFSMem(\n wgt: Array<number>,\n val: Array<number>,\n mem: Array<Array<number>>,\n i: number,\n c: number\n): number {\n // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n if (i === 0 || c === 0) {\n return 0;\n }\n // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if (mem[i][c] !== -1) {\n return mem[i][c];\n }\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n if (wgt[i - 1] > c) {\n return knapsackDFSMem(wgt, val, mem, i - 1, c);\n }\n // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n const no = knapsackDFSMem(wgt, val, mem, i - 1, c);\n const yes =\n knapsackDFSMem(wgt, val, mem, i - 1, c - wgt[i - 1]) + val[i - 1];\n // \u8bb0\u5f55\u5e76\u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n mem[i][c] = Math.max(no, yes);\n return mem[i][c];\n}\n
knapsack.dart/* 0-1 \u80cc\u5305\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint knapsackDFSMem(\n List<int> wgt,\n List<int> val,\n List<List<int>> mem,\n int i,\n int c,\n) {\n // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n if (i == 0 || c == 0) {\n return 0;\n }\n // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if (mem[i][c] != -1) {\n return mem[i][c];\n }\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n if (wgt[i - 1] > c) {\n return knapsackDFSMem(wgt, val, mem, i - 1, c);\n }\n // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n int no = knapsackDFSMem(wgt, val, mem, i - 1, c);\n int yes = knapsackDFSMem(wgt, val, mem, i - 1, c - wgt[i - 1]) + val[i - 1];\n // \u8bb0\u5f55\u5e76\u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n mem[i][c] = max(no, yes);\n return mem[i][c];\n}\n
knapsack.rs/* 0-1 \u80cc\u5305\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfn knapsack_dfs_mem(wgt: &[i32], val: &[i32], mem: &mut Vec<Vec<i32>>, i: usize, c: usize) -> i32 {\n // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n if i == 0 || c == 0 {\n return 0;\n }\n // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if mem[i][c] != -1 {\n return mem[i][c];\n }\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n if wgt[i - 1] > c as i32 {\n return knapsack_dfs_mem(wgt, val, mem, i - 1, c);\n }\n // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n let no = knapsack_dfs_mem(wgt, val, mem, i - 1, c);\n let yes = knapsack_dfs_mem(wgt, val, mem, i - 1, c - wgt[i - 1] as usize) + val[i - 1];\n // \u8bb0\u5f55\u5e76\u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n mem[i][c] = std::cmp::max(no, yes);\n mem[i][c]\n}\n
knapsack.c/* 0-1 \u80cc\u5305\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint knapsackDFSMem(int wgt[], int val[], int memCols, int **mem, int i, int c) {\n // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n if (i == 0 || c == 0) {\n return 0;\n }\n // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if (mem[i][c] != -1) {\n return mem[i][c];\n }\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n if (wgt[i - 1] > c) {\n return knapsackDFSMem(wgt, val, memCols, mem, i - 1, c);\n }\n // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n int no = knapsackDFSMem(wgt, val, memCols, mem, i - 1, c);\n int yes = knapsackDFSMem(wgt, val, memCols, mem, i - 1, c - wgt[i - 1]) + val[i - 1];\n // \u8bb0\u5f55\u5e76\u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n mem[i][c] = myMax(no, yes);\n return mem[i][c];\n}\n
knapsack.zig// 0-1 \u80cc\u5305\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22\nfn knapsackDFSMem(wgt: []i32, val: []i32, mem: anytype, i: usize, c: usize) i32 {\n // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n if (i == 0 or c == 0) {\n return 0;\n }\n // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if (mem[i][c] != -1) {\n return mem[i][c];\n }\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n if (wgt[i - 1] > c) {\n return knapsackDFSMem(wgt, val, mem, i - 1, c);\n }\n // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n var no = knapsackDFSMem(wgt, val, mem, i - 1, c);\n var yes = knapsackDFSMem(wgt, val, mem, i - 1, c - @as(usize, @intCast(wgt[i - 1]))) + val[i - 1];\n // \u8bb0\u5f55\u5e76\u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n mem[i][c] = @max(no, yes);\n return mem[i][c];\n}\n
\u56fe 14-19 \u5c55\u793a\u4e86\u5728\u8bb0\u5fc6\u5316\u641c\u7d22\u4e2d\u88ab\u526a\u6389\u7684\u641c\u7d22\u5206\u652f\u3002
\u56fe 14-19 \u00a0 0-1 \u80cc\u5305\u95ee\u9898\u7684\u8bb0\u5fc6\u5316\u641c\u7d22\u9012\u5f52\u6811
"},{"location":"chapter_dynamic_programming/knapsack_problem/#3","title":"3. \u00a0 \u65b9\u6cd5\u4e09\uff1a\u52a8\u6001\u89c4\u5212","text":"\u52a8\u6001\u89c4\u5212\u5b9e\u8d28\u4e0a\u5c31\u662f\u5728\u72b6\u6001\u8f6c\u79fb\u4e2d\u586b\u5145 \\(dp\\) \u8868\u7684\u8fc7\u7a0b\uff0c\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig knapsack.pydef knapsack_dp(wgt: list[int], val: list[int], cap: int) -> int:\n \"\"\"0-1 \u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212\"\"\"\n n = len(wgt)\n # \u521d\u59cb\u5316 dp \u8868\n dp = [[0] * (cap + 1) for _ in range(n + 1)]\n # \u72b6\u6001\u8f6c\u79fb\n for i in range(1, n + 1):\n for c in range(1, cap + 1):\n if wgt[i - 1] > c:\n # \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n dp[i][c] = dp[i - 1][c]\n else:\n # \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[i][c] = max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + val[i - 1])\n return dp[n][cap]\n
knapsack.cpp/* 0-1 \u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nint knapsackDP(vector<int> &wgt, vector<int> &val, int cap) {\n int n = wgt.size();\n // \u521d\u59cb\u5316 dp \u8868\n vector<vector<int>> dp(n + 1, vector<int>(cap + 1, 0));\n // \u72b6\u6001\u8f6c\u79fb\n for (int i = 1; i <= n; i++) {\n for (int c = 1; c <= cap; c++) {\n if (wgt[i - 1] > c) {\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n dp[i][c] = dp[i - 1][c];\n } else {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[i][c] = max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + val[i - 1]);\n }\n }\n }\n return dp[n][cap];\n}\n
knapsack.java/* 0-1 \u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nint knapsackDP(int[] wgt, int[] val, int cap) {\n int n = wgt.length;\n // \u521d\u59cb\u5316 dp \u8868\n int[][] dp = new int[n + 1][cap + 1];\n // \u72b6\u6001\u8f6c\u79fb\n for (int i = 1; i <= n; i++) {\n for (int c = 1; c <= cap; c++) {\n if (wgt[i - 1] > c) {\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n dp[i][c] = dp[i - 1][c];\n } else {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[i][c] = Math.max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + val[i - 1]);\n }\n }\n }\n return dp[n][cap];\n}\n
knapsack.cs/* 0-1 \u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nint KnapsackDP(int[] weight, int[] val, int cap) {\n int n = weight.Length;\n // \u521d\u59cb\u5316 dp \u8868\n int[,] dp = new int[n + 1, cap + 1];\n // \u72b6\u6001\u8f6c\u79fb\n for (int i = 1; i <= n; i++) {\n for (int c = 1; c <= cap; c++) {\n if (weight[i - 1] > c) {\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n dp[i, c] = dp[i - 1, c];\n } else {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[i, c] = Math.Max(dp[i - 1, c - weight[i - 1]] + val[i - 1], dp[i - 1, c]);\n }\n }\n }\n return dp[n, cap];\n}\n
knapsack.go/* 0-1 \u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc knapsackDP(wgt, val []int, cap int) int {\n n := len(wgt)\n // \u521d\u59cb\u5316 dp \u8868\n dp := make([][]int, n+1)\n for i := 0; i <= n; i++ {\n dp[i] = make([]int, cap+1)\n }\n // \u72b6\u6001\u8f6c\u79fb\n for i := 1; i <= n; i++ {\n for c := 1; c <= cap; c++ {\n if wgt[i-1] > c {\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n dp[i][c] = dp[i-1][c]\n } else {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[i][c] = int(math.Max(float64(dp[i-1][c]), float64(dp[i-1][c-wgt[i-1]]+val[i-1])))\n }\n }\n }\n return dp[n][cap]\n}\n
knapsack.swift/* 0-1 \u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc knapsackDP(wgt: [Int], val: [Int], cap: Int) -> Int {\n let n = wgt.count\n // \u521d\u59cb\u5316 dp \u8868\n var dp = Array(repeating: Array(repeating: 0, count: cap + 1), count: n + 1)\n // \u72b6\u6001\u8f6c\u79fb\n for i in stride(from: 1, through: n, by: 1) {\n for c in stride(from: 1, through: cap, by: 1) {\n if wgt[i - 1] > c {\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n dp[i][c] = dp[i - 1][c]\n } else {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[i][c] = max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + val[i - 1])\n }\n }\n }\n return dp[n][cap]\n}\n
knapsack.js/* 0-1 \u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction knapsackDP(wgt, val, cap) {\n const n = wgt.length;\n // \u521d\u59cb\u5316 dp \u8868\n const dp = Array(n + 1)\n .fill(0)\n .map(() => Array(cap + 1).fill(0));\n // \u72b6\u6001\u8f6c\u79fb\n for (let i = 1; i <= n; i++) {\n for (let c = 1; c <= cap; c++) {\n if (wgt[i - 1] > c) {\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n dp[i][c] = dp[i - 1][c];\n } else {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[i][c] = Math.max(\n dp[i - 1][c],\n dp[i - 1][c - wgt[i - 1]] + val[i - 1]\n );\n }\n }\n }\n return dp[n][cap];\n}\n
knapsack.ts/* 0-1 \u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction knapsackDP(\n wgt: Array<number>,\n val: Array<number>,\n cap: number\n): number {\n const n = wgt.length;\n // \u521d\u59cb\u5316 dp \u8868\n const dp = Array.from({ length: n + 1 }, () =>\n Array.from({ length: cap + 1 }, () => 0)\n );\n // \u72b6\u6001\u8f6c\u79fb\n for (let i = 1; i <= n; i++) {\n for (let c = 1; c <= cap; c++) {\n if (wgt[i - 1] > c) {\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n dp[i][c] = dp[i - 1][c];\n } else {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[i][c] = Math.max(\n dp[i - 1][c],\n dp[i - 1][c - wgt[i - 1]] + val[i - 1]\n );\n }\n }\n }\n return dp[n][cap];\n}\n
knapsack.dart/* 0-1 \u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nint knapsackDP(List<int> wgt, List<int> val, int cap) {\n int n = wgt.length;\n // \u521d\u59cb\u5316 dp \u8868\n List<List<int>> dp = List.generate(n + 1, (index) => List.filled(cap + 1, 0));\n // \u72b6\u6001\u8f6c\u79fb\n for (int i = 1; i <= n; i++) {\n for (int c = 1; c <= cap; c++) {\n if (wgt[i - 1] > c) {\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n dp[i][c] = dp[i - 1][c];\n } else {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[i][c] = max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + val[i - 1]);\n }\n }\n }\n return dp[n][cap];\n}\n
knapsack.rs/* 0-1 \u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nfn knapsack_dp(wgt: &[i32], val: &[i32], cap: usize) -> i32 {\n let n = wgt.len();\n // \u521d\u59cb\u5316 dp \u8868\n let mut dp = vec![vec![0; cap + 1]; n + 1];\n // \u72b6\u6001\u8f6c\u79fb\n for i in 1..=n {\n for c in 1..=cap {\n if wgt[i - 1] > c as i32 {\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n dp[i][c] = dp[i - 1][c];\n } else {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[i][c] = std::cmp::max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1] as usize] + val[i - 1]);\n }\n }\n }\n dp[n][cap]\n}\n
knapsack.c/* 0-1 \u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nint knapsackDP(int wgt[], int val[], int cap, int wgtSize) {\n int n = wgtSize;\n // \u521d\u59cb\u5316 dp \u8868\n int **dp = malloc((n + 1) * sizeof(int *));\n for (int i = 0; i <= n; i++) {\n dp[i] = calloc(cap + 1, sizeof(int));\n }\n // \u72b6\u6001\u8f6c\u79fb\n for (int i = 1; i <= n; i++) {\n for (int c = 1; c <= cap; c++) {\n if (wgt[i - 1] > c) {\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n dp[i][c] = dp[i - 1][c];\n } else {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[i][c] = myMax(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + val[i - 1]);\n }\n }\n }\n int res = dp[n][cap];\n // \u91ca\u653e\u5185\u5b58\n for (int i = 0; i <= n; i++) {\n free(dp[i]);\n }\n return res;\n}\n
knapsack.zig// 0-1 \u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212\nfn knapsackDP(comptime wgt: []i32, val: []i32, comptime cap: usize) i32 {\n comptime var n = wgt.len;\n // \u521d\u59cb\u5316 dp \u8868\n var dp = [_][cap + 1]i32{[_]i32{0} ** (cap + 1)} ** (n + 1);\n // \u72b6\u6001\u8f6c\u79fb\n for (1..n + 1) |i| {\n for (1..cap + 1) |c| {\n if (wgt[i - 1] > c) {\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n dp[i][c] = dp[i - 1][c];\n } else {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[i][c] = @max(dp[i - 1][c], dp[i - 1][c - @as(usize, @intCast(wgt[i - 1]))] + val[i - 1]);\n }\n }\n }\n return dp[n][cap];\n}\n
\u5982\u56fe 14-20 \u6240\u793a\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u548c\u7a7a\u95f4\u590d\u6742\u5ea6\u90fd\u7531\u6570\u7ec4 dp
\u5927\u5c0f\u51b3\u5b9a\uff0c\u5373 \\(O(n \\times cap)\\) \u3002
\u56fe 14-20 \u00a0 0-1 \u80cc\u5305\u95ee\u9898\u7684\u52a8\u6001\u89c4\u5212\u8fc7\u7a0b
"},{"location":"chapter_dynamic_programming/knapsack_problem/#4","title":"4. \u00a0 \u7a7a\u95f4\u4f18\u5316","text":"\u7531\u4e8e\u6bcf\u4e2a\u72b6\u6001\u90fd\u53ea\u4e0e\u5176\u4e0a\u4e00\u884c\u7684\u72b6\u6001\u6709\u5173\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e24\u4e2a\u6570\u7ec4\u6eda\u52a8\u524d\u8fdb\uff0c\u5c06\u7a7a\u95f4\u590d\u6742\u5ea6\u4ece \\(O(n^2)\\) \u964d\u81f3 \\(O(n)\\) \u3002
\u8fdb\u4e00\u6b65\u601d\u8003\uff0c\u6211\u4eec\u80fd\u5426\u4ec5\u7528\u4e00\u4e2a\u6570\u7ec4\u5b9e\u73b0\u7a7a\u95f4\u4f18\u5316\u5462\uff1f\u89c2\u5bdf\u53ef\u77e5\uff0c\u6bcf\u4e2a\u72b6\u6001\u90fd\u662f\u7531\u6b63\u4e0a\u65b9\u6216\u5de6\u4e0a\u65b9\u7684\u683c\u5b50\u8f6c\u79fb\u8fc7\u6765\u7684\u3002\u5047\u8bbe\u53ea\u6709\u4e00\u4e2a\u6570\u7ec4\uff0c\u5f53\u5f00\u59cb\u904d\u5386\u7b2c \\(i\\) \u884c\u65f6\uff0c\u8be5\u6570\u7ec4\u5b58\u50a8\u7684\u4ecd\u7136\u662f\u7b2c \\(i-1\\) \u884c\u7684\u72b6\u6001\u3002
\u56fe 14-21 \u5c55\u793a\u4e86\u5728\u5355\u4e2a\u6570\u7ec4\u4e0b\u4ece\u7b2c \\(i = 1\\) \u884c\u8f6c\u6362\u81f3\u7b2c \\(i = 2\\) \u884c\u7684\u8fc7\u7a0b\u3002\u8bf7\u601d\u8003\u6b63\u5e8f\u904d\u5386\u548c\u5012\u5e8f\u904d\u5386\u7684\u533a\u522b\u3002
<1><2><3><4><5><6>\u56fe 14-21 \u00a0 0-1 \u80cc\u5305\u7684\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\u8fc7\u7a0b
\u5728\u4ee3\u7801\u5b9e\u73b0\u4e2d\uff0c\u6211\u4eec\u4ec5\u9700\u5c06\u6570\u7ec4 dp
\u7684\u7b2c\u4e00\u7ef4 \\(i\\) \u76f4\u63a5\u5220\u9664\uff0c\u5e76\u4e14\u628a\u5185\u5faa\u73af\u66f4\u6539\u4e3a\u5012\u5e8f\u904d\u5386\u5373\u53ef\uff1a
def knapsack_dp_comp(wgt: list[int], val: list[int], cap: int) -> int:\n \"\"\"0-1 \u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\"\"\"\n n = len(wgt)\n # \u521d\u59cb\u5316 dp \u8868\n dp = [0] * (cap + 1)\n # \u72b6\u6001\u8f6c\u79fb\n for i in range(1, n + 1):\n # \u5012\u5e8f\u904d\u5386\n for c in range(cap, 0, -1):\n if wgt[i - 1] > c:\n # \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n dp[c] = dp[c]\n else:\n # \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1])\n return dp[cap]\n
knapsack.cpp/* 0-1 \u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint knapsackDPComp(vector<int> &wgt, vector<int> &val, int cap) {\n int n = wgt.size();\n // \u521d\u59cb\u5316 dp \u8868\n vector<int> dp(cap + 1, 0);\n // \u72b6\u6001\u8f6c\u79fb\n for (int i = 1; i <= n; i++) {\n // \u5012\u5e8f\u904d\u5386\n for (int c = cap; c >= 1; c--) {\n if (wgt[i - 1] <= c) {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n }\n }\n }\n return dp[cap];\n}\n
knapsack.java/* 0-1 \u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint knapsackDPComp(int[] wgt, int[] val, int cap) {\n int n = wgt.length;\n // \u521d\u59cb\u5316 dp \u8868\n int[] dp = new int[cap + 1];\n // \u72b6\u6001\u8f6c\u79fb\n for (int i = 1; i <= n; i++) {\n // \u5012\u5e8f\u904d\u5386\n for (int c = cap; c >= 1; c--) {\n if (wgt[i - 1] <= c) {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[c] = Math.max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n }\n }\n }\n return dp[cap];\n}\n
knapsack.cs/* 0-1 \u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint KnapsackDPComp(int[] weight, int[] val, int cap) {\n int n = weight.Length;\n // \u521d\u59cb\u5316 dp \u8868\n int[] dp = new int[cap + 1];\n // \u72b6\u6001\u8f6c\u79fb\n for (int i = 1; i <= n; i++) {\n // \u5012\u5e8f\u904d\u5386\n for (int c = cap; c > 0; c--) {\n if (weight[i - 1] > c) {\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n dp[c] = dp[c];\n } else {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[c] = Math.Max(dp[c], dp[c - weight[i - 1]] + val[i - 1]);\n }\n }\n }\n return dp[cap];\n}\n
knapsack.go/* 0-1 \u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc knapsackDPComp(wgt, val []int, cap int) int {\n n := len(wgt)\n // \u521d\u59cb\u5316 dp \u8868\n dp := make([]int, cap+1)\n // \u72b6\u6001\u8f6c\u79fb\n for i := 1; i <= n; i++ {\n // \u5012\u5e8f\u904d\u5386\n for c := cap; c >= 1; c-- {\n if wgt[i-1] <= c {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[c] = int(math.Max(float64(dp[c]), float64(dp[c-wgt[i-1]]+val[i-1])))\n }\n }\n }\n return dp[cap]\n}\n
knapsack.swift/* 0-1 \u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc knapsackDPComp(wgt: [Int], val: [Int], cap: Int) -> Int {\n let n = wgt.count\n // \u521d\u59cb\u5316 dp \u8868\n var dp = Array(repeating: 0, count: cap + 1)\n // \u72b6\u6001\u8f6c\u79fb\n for i in stride(from: 1, through: n, by: 1) {\n // \u5012\u5e8f\u904d\u5386\n for c in stride(from: cap, through: 1, by: -1) {\n if wgt[i - 1] <= c {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1])\n }\n }\n }\n return dp[cap]\n}\n
knapsack.js/* 0-1 \u80cc\u5305\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction knapsackDPComp(wgt, val, cap) {\n const n = wgt.length;\n // \u521d\u59cb\u5316 dp \u8868\n const dp = Array(cap + 1).fill(0);\n // \u72b6\u6001\u8f6c\u79fb\n for (let i = 1; i <= n; i++) {\n // \u5012\u5e8f\u904d\u5386\n for (let c = cap; c >= 1; c--) {\n if (wgt[i - 1] <= c) {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[c] = Math.max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n }\n }\n }\n return dp[cap];\n}\n
knapsack.ts/* 0-1 \u80cc\u5305\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction knapsackDPComp(\n wgt: Array<number>,\n val: Array<number>,\n cap: number\n): number {\n const n = wgt.length;\n // \u521d\u59cb\u5316 dp \u8868\n const dp = Array(cap + 1).fill(0);\n // \u72b6\u6001\u8f6c\u79fb\n for (let i = 1; i <= n; i++) {\n // \u5012\u5e8f\u904d\u5386\n for (let c = cap; c >= 1; c--) {\n if (wgt[i - 1] <= c) {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[c] = Math.max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n }\n }\n }\n return dp[cap];\n}\n
knapsack.dart/* 0-1 \u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint knapsackDPComp(List<int> wgt, List<int> val, int cap) {\n int n = wgt.length;\n // \u521d\u59cb\u5316 dp \u8868\n List<int> dp = List.filled(cap + 1, 0);\n // \u72b6\u6001\u8f6c\u79fb\n for (int i = 1; i <= n; i++) {\n // \u5012\u5e8f\u904d\u5386\n for (int c = cap; c >= 1; c--) {\n if (wgt[i - 1] <= c) {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n }\n }\n }\n return dp[cap];\n}\n
knapsack.rs/* 0-1 \u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfn knapsack_dp_comp(wgt: &[i32], val: &[i32], cap: usize) -> i32 {\n let n = wgt.len();\n // \u521d\u59cb\u5316 dp \u8868\n let mut dp = vec![0; cap + 1];\n // \u72b6\u6001\u8f6c\u79fb\n for i in 1..=n {\n // \u5012\u5e8f\u904d\u5386\n for c in (1..=cap).rev() {\n if wgt[i - 1] <= c as i32 {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[c] = std::cmp::max(dp[c], dp[c - wgt[i - 1] as usize] + val[i - 1]);\n }\n }\n }\n dp[cap]\n}\n
knapsack.c/* 0-1 \u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint knapsackDPComp(int wgt[], int val[], int cap, int wgtSize) {\n int n = wgtSize;\n // \u521d\u59cb\u5316 dp \u8868\n int *dp = calloc(cap + 1, sizeof(int));\n // \u72b6\u6001\u8f6c\u79fb\n for (int i = 1; i <= n; i++) {\n // \u5012\u5e8f\u904d\u5386\n for (int c = cap; c >= 1; c--) {\n if (wgt[i - 1] <= c) {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[c] = myMax(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n }\n }\n }\n int res = dp[cap];\n // \u91ca\u653e\u5185\u5b58\n free(dp);\n return res;\n}\n
knapsack.zig// 0-1 \u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\nfn knapsackDPComp(wgt: []i32, val: []i32, comptime cap: usize) i32 {\n var n = wgt.len;\n // \u521d\u59cb\u5316 dp \u8868\n var dp = [_]i32{0} ** (cap + 1);\n // \u72b6\u6001\u8f6c\u79fb\n for (1..n + 1) |i| {\n // \u5012\u5e8f\u904d\u5386\n var c = cap;\n while (c > 0) : (c -= 1) {\n if (wgt[i - 1] < c) {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[c] = @max(dp[c], dp[c - @as(usize, @intCast(wgt[i - 1]))] + val[i - 1]);\n }\n }\n }\n return dp[cap];\n}\n
"},{"location":"chapter_dynamic_programming/summary/","title":"14.7 \u00a0 \u5c0f\u7ed3","text":"\u80cc\u5305\u95ee\u9898
\u7f16\u8f91\u8ddd\u79bb\u95ee\u9898
\u5728\u672c\u8282\u4e2d\uff0c\u6211\u4eec\u5148\u6c42\u89e3\u53e6\u4e00\u4e2a\u5e38\u89c1\u7684\u80cc\u5305\u95ee\u9898\uff1a\u5b8c\u5168\u80cc\u5305\uff0c\u518d\u4e86\u89e3\u5b83\u7684\u4e00\u79cd\u7279\u4f8b\uff1a\u96f6\u94b1\u5151\u6362\u3002
"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#1451","title":"14.5.1 \u00a0 \u5b8c\u5168\u80cc\u5305","text":"Question
\u7ed9\u5b9a \\(n\\) \u4e2a\u7269\u54c1\uff0c\u7b2c \\(i\\) \u4e2a\u7269\u54c1\u7684\u91cd\u91cf\u4e3a \\(wgt[i-1]\\)\u3001\u4ef7\u503c\u4e3a \\(val[i-1]\\) \uff0c\u548c\u4e00\u4e2a\u5bb9\u91cf\u4e3a \\(cap\\) \u7684\u80cc\u5305\u3002\u6bcf\u4e2a\u7269\u54c1\u53ef\u4ee5\u91cd\u590d\u9009\u53d6\uff0c\u95ee\u5728\u9650\u5b9a\u80cc\u5305\u5bb9\u91cf\u4e0b\u80fd\u653e\u5165\u7269\u54c1\u7684\u6700\u5927\u4ef7\u503c\u3002\u793a\u4f8b\u5982\u56fe 14-22 \u6240\u793a\u3002
\u56fe 14-22 \u00a0 \u5b8c\u5168\u80cc\u5305\u95ee\u9898\u7684\u793a\u4f8b\u6570\u636e
"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#1","title":"1. \u00a0 \u52a8\u6001\u89c4\u5212\u601d\u8def","text":"\u5b8c\u5168\u80cc\u5305\u95ee\u9898\u548c 0-1 \u80cc\u5305\u95ee\u9898\u975e\u5e38\u76f8\u4f3c\uff0c\u533a\u522b\u4ec5\u5728\u4e8e\u4e0d\u9650\u5236\u7269\u54c1\u7684\u9009\u62e9\u6b21\u6570\u3002
\u5728\u5b8c\u5168\u80cc\u5305\u95ee\u9898\u7684\u89c4\u5b9a\u4e0b\uff0c\u72b6\u6001 \\([i, c]\\) \u7684\u53d8\u5316\u5206\u4e3a\u4e24\u79cd\u60c5\u51b5\u3002
\u4ece\u800c\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u53d8\u4e3a\uff1a
\\[ dp[i, c] = \\max(dp[i-1, c], dp[i, c - wgt[i-1]] + val[i-1]) \\]"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#2","title":"2. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"\u5bf9\u6bd4\u4e24\u9053\u9898\u76ee\u7684\u4ee3\u7801\uff0c\u72b6\u6001\u8f6c\u79fb\u4e2d\u6709\u4e00\u5904\u4ece \\(i-1\\) \u53d8\u4e3a \\(i\\) \uff0c\u5176\u4f59\u5b8c\u5168\u4e00\u81f4\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig unbounded_knapsack.pydef unbounded_knapsack_dp(wgt: list[int], val: list[int], cap: int) -> int:\n \"\"\"\u5b8c\u5168\u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212\"\"\"\n n = len(wgt)\n # \u521d\u59cb\u5316 dp \u8868\n dp = [[0] * (cap + 1) for _ in range(n + 1)]\n # \u72b6\u6001\u8f6c\u79fb\n for i in range(1, n + 1):\n for c in range(1, cap + 1):\n if wgt[i - 1] > c:\n # \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n dp[i][c] = dp[i - 1][c]\n else:\n # \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[i][c] = max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1])\n return dp[n][cap]\n
unbounded_knapsack.cpp/* \u5b8c\u5168\u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nint unboundedKnapsackDP(vector<int> &wgt, vector<int> &val, int cap) {\n int n = wgt.size();\n // \u521d\u59cb\u5316 dp \u8868\n vector<vector<int>> dp(n + 1, vector<int>(cap + 1, 0));\n // \u72b6\u6001\u8f6c\u79fb\n for (int i = 1; i <= n; i++) {\n for (int c = 1; c <= cap; c++) {\n if (wgt[i - 1] > c) {\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n dp[i][c] = dp[i - 1][c];\n } else {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[i][c] = max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1]);\n }\n }\n }\n return dp[n][cap];\n}\n
unbounded_knapsack.java/* \u5b8c\u5168\u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nint unboundedKnapsackDP(int[] wgt, int[] val, int cap) {\n int n = wgt.length;\n // \u521d\u59cb\u5316 dp \u8868\n int[][] dp = new int[n + 1][cap + 1];\n // \u72b6\u6001\u8f6c\u79fb\n for (int i = 1; i <= n; i++) {\n for (int c = 1; c <= cap; c++) {\n if (wgt[i - 1] > c) {\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n dp[i][c] = dp[i - 1][c];\n } else {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[i][c] = Math.max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1]);\n }\n }\n }\n return dp[n][cap];\n}\n
unbounded_knapsack.cs/* \u5b8c\u5168\u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nint UnboundedKnapsackDP(int[] wgt, int[] val, int cap) {\n int n = wgt.Length;\n // \u521d\u59cb\u5316 dp \u8868\n int[,] dp = new int[n + 1, cap + 1];\n // \u72b6\u6001\u8f6c\u79fb\n for (int i = 1; i <= n; i++) {\n for (int c = 1; c <= cap; c++) {\n if (wgt[i - 1] > c) {\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n dp[i, c] = dp[i - 1, c];\n } else {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[i, c] = Math.Max(dp[i - 1, c], dp[i, c - wgt[i - 1]] + val[i - 1]);\n }\n }\n }\n return dp[n, cap];\n}\n
unbounded_knapsack.go/* \u5b8c\u5168\u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc unboundedKnapsackDP(wgt, val []int, cap int) int {\n n := len(wgt)\n // \u521d\u59cb\u5316 dp \u8868\n dp := make([][]int, n+1)\n for i := 0; i <= n; i++ {\n dp[i] = make([]int, cap+1)\n }\n // \u72b6\u6001\u8f6c\u79fb\n for i := 1; i <= n; i++ {\n for c := 1; c <= cap; c++ {\n if wgt[i-1] > c {\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n dp[i][c] = dp[i-1][c]\n } else {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[i][c] = int(math.Max(float64(dp[i-1][c]), float64(dp[i][c-wgt[i-1]]+val[i-1])))\n }\n }\n }\n return dp[n][cap]\n}\n
unbounded_knapsack.swift/* \u5b8c\u5168\u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc unboundedKnapsackDP(wgt: [Int], val: [Int], cap: Int) -> Int {\n let n = wgt.count\n // \u521d\u59cb\u5316 dp \u8868\n var dp = Array(repeating: Array(repeating: 0, count: cap + 1), count: n + 1)\n // \u72b6\u6001\u8f6c\u79fb\n for i in stride(from: 1, through: n, by: 1) {\n for c in stride(from: 1, through: cap, by: 1) {\n if wgt[i - 1] > c {\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n dp[i][c] = dp[i - 1][c]\n } else {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[i][c] = max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1])\n }\n }\n }\n return dp[n][cap]\n}\n
unbounded_knapsack.js/* \u5b8c\u5168\u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction unboundedKnapsackDP(wgt, val, cap) {\n const n = wgt.length;\n // \u521d\u59cb\u5316 dp \u8868\n const dp = Array.from({ length: n + 1 }, () =>\n Array.from({ length: cap + 1 }, () => 0)\n );\n // \u72b6\u6001\u8f6c\u79fb\n for (let i = 1; i <= n; i++) {\n for (let c = 1; c <= cap; c++) {\n if (wgt[i - 1] > c) {\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n dp[i][c] = dp[i - 1][c];\n } else {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[i][c] = Math.max(\n dp[i - 1][c],\n dp[i][c - wgt[i - 1]] + val[i - 1]\n );\n }\n }\n }\n return dp[n][cap];\n}\n
unbounded_knapsack.ts/* \u5b8c\u5168\u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction unboundedKnapsackDP(\n wgt: Array<number>,\n val: Array<number>,\n cap: number\n): number {\n const n = wgt.length;\n // \u521d\u59cb\u5316 dp \u8868\n const dp = Array.from({ length: n + 1 }, () =>\n Array.from({ length: cap + 1 }, () => 0)\n );\n // \u72b6\u6001\u8f6c\u79fb\n for (let i = 1; i <= n; i++) {\n for (let c = 1; c <= cap; c++) {\n if (wgt[i - 1] > c) {\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n dp[i][c] = dp[i - 1][c];\n } else {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[i][c] = Math.max(\n dp[i - 1][c],\n dp[i][c - wgt[i - 1]] + val[i - 1]\n );\n }\n }\n }\n return dp[n][cap];\n}\n
unbounded_knapsack.dart/* \u5b8c\u5168\u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nint unboundedKnapsackDP(List<int> wgt, List<int> val, int cap) {\n int n = wgt.length;\n // \u521d\u59cb\u5316 dp \u8868\n List<List<int>> dp = List.generate(n + 1, (index) => List.filled(cap + 1, 0));\n // \u72b6\u6001\u8f6c\u79fb\n for (int i = 1; i <= n; i++) {\n for (int c = 1; c <= cap; c++) {\n if (wgt[i - 1] > c) {\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n dp[i][c] = dp[i - 1][c];\n } else {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[i][c] = max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1]);\n }\n }\n }\n return dp[n][cap];\n}\n
unbounded_knapsack.rs/* \u5b8c\u5168\u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nfn unbounded_knapsack_dp(wgt: &[i32], val: &[i32], cap: usize) -> i32 {\n let n = wgt.len();\n // \u521d\u59cb\u5316 dp \u8868\n let mut dp = vec![vec![0; cap + 1]; n + 1];\n // \u72b6\u6001\u8f6c\u79fb\n for i in 1..=n {\n for c in 1..=cap {\n if wgt[i - 1] > c as i32 {\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n dp[i][c] = dp[i - 1][c];\n } else {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[i][c] = std::cmp::max(dp[i - 1][c], dp[i][c - wgt[i - 1] as usize] + val[i - 1]);\n }\n }\n }\n return dp[n][cap];\n}\n
unbounded_knapsack.c/* \u5b8c\u5168\u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nint unboundedKnapsackDP(int wgt[], int val[], int cap, int wgtSize) {\n int n = wgtSize;\n // \u521d\u59cb\u5316 dp \u8868\n int **dp = malloc((n + 1) * sizeof(int *));\n for (int i = 0; i <= n; i++) {\n dp[i] = calloc(cap + 1, sizeof(int));\n }\n // \u72b6\u6001\u8f6c\u79fb\n for (int i = 1; i <= n; i++) {\n for (int c = 1; c <= cap; c++) {\n if (wgt[i - 1] > c) {\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n dp[i][c] = dp[i - 1][c];\n } else {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[i][c] = myMax(dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1]);\n }\n }\n }\n int res = dp[n][cap];\n // \u91ca\u653e\u5185\u5b58\n for (int i = 0; i <= n; i++) {\n free(dp[i]);\n }\n return res;\n}\n
unbounded_knapsack.zig// \u5b8c\u5168\u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212\nfn unboundedKnapsackDP(comptime wgt: []i32, val: []i32, comptime cap: usize) i32 {\n comptime var n = wgt.len;\n // \u521d\u59cb\u5316 dp \u8868\n var dp = [_][cap + 1]i32{[_]i32{0} ** (cap + 1)} ** (n + 1);\n // \u72b6\u6001\u8f6c\u79fb\n for (1..n + 1) |i| {\n for (1..cap + 1) |c| {\n if (wgt[i - 1] > c) {\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n dp[i][c] = dp[i - 1][c];\n } else {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[i][c] = @max(dp[i - 1][c], dp[i][c - @as(usize, @intCast(wgt[i - 1]))] + val[i - 1]);\n }\n }\n }\n return dp[n][cap];\n}\n
"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#3","title":"3. \u00a0 \u7a7a\u95f4\u4f18\u5316","text":"\u7531\u4e8e\u5f53\u524d\u72b6\u6001\u662f\u4ece\u5de6\u8fb9\u548c\u4e0a\u8fb9\u7684\u72b6\u6001\u8f6c\u79fb\u800c\u6765\u7684\uff0c\u56e0\u6b64\u7a7a\u95f4\u4f18\u5316\u540e\u5e94\u8be5\u5bf9 \\(dp\\) \u8868\u4e2d\u7684\u6bcf\u4e00\u884c\u8fdb\u884c\u6b63\u5e8f\u904d\u5386\u3002
\u8fd9\u4e2a\u904d\u5386\u987a\u5e8f\u4e0e 0-1 \u80cc\u5305\u6b63\u597d\u76f8\u53cd\u3002\u8bf7\u501f\u52a9\u56fe 14-23 \u6765\u7406\u89e3\u4e24\u8005\u7684\u533a\u522b\u3002
<1><2><3><4><5><6>\u56fe 14-23 \u00a0 \u5b8c\u5168\u80cc\u5305\u95ee\u9898\u5728\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\u8fc7\u7a0b
\u4ee3\u7801\u5b9e\u73b0\u6bd4\u8f83\u7b80\u5355\uff0c\u4ec5\u9700\u5c06\u6570\u7ec4 dp
\u7684\u7b2c\u4e00\u7ef4\u5220\u9664\uff1a
def unbounded_knapsack_dp_comp(wgt: list[int], val: list[int], cap: int) -> int:\n \"\"\"\u5b8c\u5168\u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\"\"\"\n n = len(wgt)\n # \u521d\u59cb\u5316 dp \u8868\n dp = [0] * (cap + 1)\n # \u72b6\u6001\u8f6c\u79fb\n for i in range(1, n + 1):\n # \u6b63\u5e8f\u904d\u5386\n for c in range(1, cap + 1):\n if wgt[i - 1] > c:\n # \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n dp[c] = dp[c]\n else:\n # \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1])\n return dp[cap]\n
unbounded_knapsack.cpp/* \u5b8c\u5168\u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint unboundedKnapsackDPComp(vector<int> &wgt, vector<int> &val, int cap) {\n int n = wgt.size();\n // \u521d\u59cb\u5316 dp \u8868\n vector<int> dp(cap + 1, 0);\n // \u72b6\u6001\u8f6c\u79fb\n for (int i = 1; i <= n; i++) {\n for (int c = 1; c <= cap; c++) {\n if (wgt[i - 1] > c) {\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n dp[c] = dp[c];\n } else {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n }\n }\n }\n return dp[cap];\n}\n
unbounded_knapsack.java/* \u5b8c\u5168\u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint unboundedKnapsackDPComp(int[] wgt, int[] val, int cap) {\n int n = wgt.length;\n // \u521d\u59cb\u5316 dp \u8868\n int[] dp = new int[cap + 1];\n // \u72b6\u6001\u8f6c\u79fb\n for (int i = 1; i <= n; i++) {\n for (int c = 1; c <= cap; c++) {\n if (wgt[i - 1] > c) {\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n dp[c] = dp[c];\n } else {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[c] = Math.max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n }\n }\n }\n return dp[cap];\n}\n
unbounded_knapsack.cs/* \u5b8c\u5168\u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint UnboundedKnapsackDPComp(int[] wgt, int[] val, int cap) {\n int n = wgt.Length;\n // \u521d\u59cb\u5316 dp \u8868\n int[] dp = new int[cap + 1];\n // \u72b6\u6001\u8f6c\u79fb\n for (int i = 1; i <= n; i++) {\n for (int c = 1; c <= cap; c++) {\n if (wgt[i - 1] > c) {\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n dp[c] = dp[c];\n } else {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[c] = Math.Max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n }\n }\n }\n return dp[cap];\n}\n
unbounded_knapsack.go/* \u5b8c\u5168\u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc unboundedKnapsackDPComp(wgt, val []int, cap int) int {\n n := len(wgt)\n // \u521d\u59cb\u5316 dp \u8868\n dp := make([]int, cap+1)\n // \u72b6\u6001\u8f6c\u79fb\n for i := 1; i <= n; i++ {\n for c := 1; c <= cap; c++ {\n if wgt[i-1] > c {\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n dp[c] = dp[c]\n } else {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[c] = int(math.Max(float64(dp[c]), float64(dp[c-wgt[i-1]]+val[i-1])))\n }\n }\n }\n return dp[cap]\n}\n
unbounded_knapsack.swift/* \u5b8c\u5168\u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc unboundedKnapsackDPComp(wgt: [Int], val: [Int], cap: Int) -> Int {\n let n = wgt.count\n // \u521d\u59cb\u5316 dp \u8868\n var dp = Array(repeating: 0, count: cap + 1)\n // \u72b6\u6001\u8f6c\u79fb\n for i in stride(from: 1, through: n, by: 1) {\n for c in stride(from: 1, through: cap, by: 1) {\n if wgt[i - 1] > c {\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n dp[c] = dp[c]\n } else {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1])\n }\n }\n }\n return dp[cap]\n}\n
unbounded_knapsack.js/* \u5b8c\u5168\u80cc\u5305\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction unboundedKnapsackDPComp(wgt, val, cap) {\n const n = wgt.length;\n // \u521d\u59cb\u5316 dp \u8868\n const dp = Array.from({ length: cap + 1 }, () => 0);\n // \u72b6\u6001\u8f6c\u79fb\n for (let i = 1; i <= n; i++) {\n for (let c = 1; c <= cap; c++) {\n if (wgt[i - 1] > c) {\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n dp[c] = dp[c];\n } else {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[c] = Math.max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n }\n }\n }\n return dp[cap];\n}\n
unbounded_knapsack.ts/* \u5b8c\u5168\u80cc\u5305\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction unboundedKnapsackDPComp(\n wgt: Array<number>,\n val: Array<number>,\n cap: number\n): number {\n const n = wgt.length;\n // \u521d\u59cb\u5316 dp \u8868\n const dp = Array.from({ length: cap + 1 }, () => 0);\n // \u72b6\u6001\u8f6c\u79fb\n for (let i = 1; i <= n; i++) {\n for (let c = 1; c <= cap; c++) {\n if (wgt[i - 1] > c) {\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n dp[c] = dp[c];\n } else {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[c] = Math.max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n }\n }\n }\n return dp[cap];\n}\n
unbounded_knapsack.dart/* \u5b8c\u5168\u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint unboundedKnapsackDPComp(List<int> wgt, List<int> val, int cap) {\n int n = wgt.length;\n // \u521d\u59cb\u5316 dp \u8868\n List<int> dp = List.filled(cap + 1, 0);\n // \u72b6\u6001\u8f6c\u79fb\n for (int i = 1; i <= n; i++) {\n for (int c = 1; c <= cap; c++) {\n if (wgt[i - 1] > c) {\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n dp[c] = dp[c];\n } else {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n }\n }\n }\n return dp[cap];\n}\n
unbounded_knapsack.rs/* \u5b8c\u5168\u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfn unbounded_knapsack_dp_comp(wgt: &[i32], val: &[i32], cap: usize) -> i32 {\n let n = wgt.len();\n // \u521d\u59cb\u5316 dp \u8868\n let mut dp = vec![0; cap + 1];\n // \u72b6\u6001\u8f6c\u79fb\n for i in 1..=n {\n for c in 1..=cap {\n if wgt[i - 1] > c as i32 {\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n dp[c] = dp[c];\n } else {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[c] = std::cmp::max(dp[c], dp[c - wgt[i - 1] as usize] + val[i - 1]);\n }\n }\n }\n dp[cap]\n}\n
unbounded_knapsack.c/* \u5b8c\u5168\u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint unboundedKnapsackDPComp(int wgt[], int val[], int cap, int wgtSize) {\n int n = wgtSize;\n // \u521d\u59cb\u5316 dp \u8868\n int *dp = calloc(cap + 1, sizeof(int));\n // \u72b6\u6001\u8f6c\u79fb\n for (int i = 1; i <= n; i++) {\n for (int c = 1; c <= cap; c++) {\n if (wgt[i - 1] > c) {\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n dp[c] = dp[c];\n } else {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[c] = myMax(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n }\n }\n }\n int res = dp[cap];\n // \u91ca\u653e\u5185\u5b58\n free(dp);\n return res;\n}\n
unbounded_knapsack.zig// \u5b8c\u5168\u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\nfn unboundedKnapsackDPComp(comptime wgt: []i32, val: []i32, comptime cap: usize) i32 {\n comptime var n = wgt.len;\n // \u521d\u59cb\u5316 dp \u8868\n var dp = [_]i32{0} ** (cap + 1);\n // \u72b6\u6001\u8f6c\u79fb\n for (1..n + 1) |i| {\n for (1..cap + 1) |c| {\n if (wgt[i - 1] > c) {\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n dp[c] = dp[c];\n } else {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[c] = @max(dp[c], dp[c - @as(usize, @intCast(wgt[i - 1]))] + val[i - 1]);\n }\n }\n }\n return dp[cap];\n}\n
"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#1452","title":"14.5.2 \u00a0 \u96f6\u94b1\u5151\u6362\u95ee\u9898","text":"\u80cc\u5305\u95ee\u9898\u662f\u4e00\u5927\u7c7b\u52a8\u6001\u89c4\u5212\u95ee\u9898\u7684\u4ee3\u8868\uff0c\u5176\u62e5\u6709\u5f88\u591a\u53d8\u79cd\uff0c\u4f8b\u5982\u96f6\u94b1\u5151\u6362\u95ee\u9898\u3002
Question
\u7ed9\u5b9a \\(n\\) \u79cd\u786c\u5e01\uff0c\u7b2c \\(i\\) \u79cd\u786c\u5e01\u7684\u9762\u503c\u4e3a \\(coins[i - 1]\\) \uff0c\u76ee\u6807\u91d1\u989d\u4e3a \\(amt\\) \uff0c\u6bcf\u79cd\u786c\u5e01\u53ef\u4ee5\u91cd\u590d\u9009\u53d6\uff0c\u95ee\u80fd\u591f\u51d1\u51fa\u76ee\u6807\u91d1\u989d\u7684\u6700\u5c11\u786c\u5e01\u6570\u91cf\u3002\u5982\u679c\u65e0\u6cd5\u51d1\u51fa\u76ee\u6807\u91d1\u989d\uff0c\u5219\u8fd4\u56de \\(-1\\) \u3002\u793a\u4f8b\u5982\u56fe 14-24 \u6240\u793a\u3002
\u56fe 14-24 \u00a0 \u96f6\u94b1\u5151\u6362\u95ee\u9898\u7684\u793a\u4f8b\u6570\u636e
"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#1_1","title":"1. \u00a0 \u52a8\u6001\u89c4\u5212\u601d\u8def","text":"\u96f6\u94b1\u5151\u6362\u53ef\u4ee5\u770b\u4f5c\u5b8c\u5168\u80cc\u5305\u95ee\u9898\u7684\u4e00\u79cd\u7279\u6b8a\u60c5\u51b5\uff0c\u4e24\u8005\u5177\u6709\u4ee5\u4e0b\u8054\u7cfb\u4e0e\u4e0d\u540c\u70b9\u3002
\u7b2c\u4e00\u6b65\uff1a\u601d\u8003\u6bcf\u8f6e\u7684\u51b3\u7b56\uff0c\u5b9a\u4e49\u72b6\u6001\uff0c\u4ece\u800c\u5f97\u5230 \\(dp\\) \u8868
\u72b6\u6001 \\([i, a]\\) \u5bf9\u5e94\u7684\u5b50\u95ee\u9898\u4e3a\uff1a\u524d \\(i\\) \u79cd\u786c\u5e01\u80fd\u591f\u51d1\u51fa\u91d1\u989d \\(a\\) \u7684\u6700\u5c11\u786c\u5e01\u6570\u91cf\uff0c\u8bb0\u4e3a \\(dp[i, a]\\) \u3002
\u4e8c\u7ef4 \\(dp\\) \u8868\u7684\u5c3a\u5bf8\u4e3a \\((n+1) \\times (amt+1)\\) \u3002
\u7b2c\u4e8c\u6b65\uff1a\u627e\u51fa\u6700\u4f18\u5b50\u7ed3\u6784\uff0c\u8fdb\u800c\u63a8\u5bfc\u51fa\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b
\u672c\u9898\u4e0e\u5b8c\u5168\u80cc\u5305\u95ee\u9898\u7684\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u5b58\u5728\u4ee5\u4e0b\u4e24\u70b9\u5dee\u5f02\u3002
\u7b2c\u4e09\u6b65\uff1a\u786e\u5b9a\u8fb9\u754c\u6761\u4ef6\u548c\u72b6\u6001\u8f6c\u79fb\u987a\u5e8f
\u5f53\u76ee\u6807\u91d1\u989d\u4e3a \\(0\\) \u65f6\uff0c\u51d1\u51fa\u5b83\u7684\u6700\u5c11\u786c\u5e01\u6570\u91cf\u4e3a \\(0\\) \uff0c\u5373\u9996\u5217\u6240\u6709 \\(dp[i, 0]\\) \u90fd\u7b49\u4e8e \\(0\\) \u3002
\u5f53\u65e0\u786c\u5e01\u65f6\uff0c\u65e0\u6cd5\u51d1\u51fa\u4efb\u610f \\(> 0\\) \u7684\u76ee\u6807\u91d1\u989d\uff0c\u5373\u662f\u65e0\u6548\u89e3\u3002\u4e3a\u4f7f\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u4e2d\u7684 \\(\\min()\\) \u51fd\u6570\u80fd\u591f\u8bc6\u522b\u5e76\u8fc7\u6ee4\u65e0\u6548\u89e3\uff0c\u6211\u4eec\u8003\u8651\u4f7f\u7528 \\(+ \\infty\\) \u6765\u8868\u793a\u5b83\u4eec\uff0c\u5373\u4ee4\u9996\u884c\u6240\u6709 \\(dp[0, a]\\) \u90fd\u7b49\u4e8e \\(+ \\infty\\) \u3002
"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#2_1","title":"2. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"\u5927\u591a\u6570\u7f16\u7a0b\u8bed\u8a00\u5e76\u672a\u63d0\u4f9b \\(+ \\infty\\) \u53d8\u91cf\uff0c\u53ea\u80fd\u4f7f\u7528\u6574\u578b int
\u7684\u6700\u5927\u503c\u6765\u4ee3\u66ff\u3002\u800c\u8fd9\u53c8\u4f1a\u5bfc\u81f4\u5927\u6570\u8d8a\u754c\uff1a\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u4e2d\u7684 \\(+ 1\\) \u64cd\u4f5c\u53ef\u80fd\u53d1\u751f\u6ea2\u51fa\u3002
\u4e3a\u6b64\uff0c\u6211\u4eec\u91c7\u7528\u6570\u5b57 \\(amt + 1\\) \u6765\u8868\u793a\u65e0\u6548\u89e3\uff0c\u56e0\u4e3a\u51d1\u51fa \\(amt\\) \u7684\u786c\u5e01\u6570\u91cf\u6700\u591a\u4e3a \\(amt\\) \u3002\u6700\u540e\u8fd4\u56de\u524d\uff0c\u5224\u65ad \\(dp[n, amt]\\) \u662f\u5426\u7b49\u4e8e \\(amt + 1\\) \uff0c\u82e5\u662f\u5219\u8fd4\u56de \\(-1\\) \uff0c\u4ee3\u8868\u65e0\u6cd5\u51d1\u51fa\u76ee\u6807\u91d1\u989d\u3002\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig coin_change.pydef coin_change_dp(coins: list[int], amt: int) -> int:\n \"\"\"\u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212\"\"\"\n n = len(coins)\n MAX = amt + 1\n # \u521d\u59cb\u5316 dp \u8868\n dp = [[0] * (amt + 1) for _ in range(n + 1)]\n # \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n for a in range(1, amt + 1):\n dp[0][a] = MAX\n # \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n for i in range(1, n + 1):\n for a in range(1, amt + 1):\n if coins[i - 1] > a:\n # \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[i][a] = dp[i - 1][a]\n else:\n # \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n dp[i][a] = min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1)\n return dp[n][amt] if dp[n][amt] != MAX else -1\n
coin_change.cpp/* \u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212 */\nint coinChangeDP(vector<int> &coins, int amt) {\n int n = coins.size();\n int MAX = amt + 1;\n // \u521d\u59cb\u5316 dp \u8868\n vector<vector<int>> dp(n + 1, vector<int>(amt + 1, 0));\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n for (int a = 1; a <= amt; a++) {\n dp[0][a] = MAX;\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n for (int i = 1; i <= n; i++) {\n for (int a = 1; a <= amt; a++) {\n if (coins[i - 1] > a) {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[i][a] = dp[i - 1][a];\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n dp[i][a] = min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1);\n }\n }\n }\n return dp[n][amt] != MAX ? dp[n][amt] : -1;\n}\n
coin_change.java/* \u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212 */\nint coinChangeDP(int[] coins, int amt) {\n int n = coins.length;\n int MAX = amt + 1;\n // \u521d\u59cb\u5316 dp \u8868\n int[][] dp = new int[n + 1][amt + 1];\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n for (int a = 1; a <= amt; a++) {\n dp[0][a] = MAX;\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n for (int i = 1; i <= n; i++) {\n for (int a = 1; a <= amt; a++) {\n if (coins[i - 1] > a) {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[i][a] = dp[i - 1][a];\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n dp[i][a] = Math.min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1);\n }\n }\n }\n return dp[n][amt] != MAX ? dp[n][amt] : -1;\n}\n
coin_change.cs/* \u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212 */\nint CoinChangeDP(int[] coins, int amt) {\n int n = coins.Length;\n int MAX = amt + 1;\n // \u521d\u59cb\u5316 dp \u8868\n int[,] dp = new int[n + 1, amt + 1];\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n for (int a = 1; a <= amt; a++) {\n dp[0, a] = MAX;\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n for (int i = 1; i <= n; i++) {\n for (int a = 1; a <= amt; a++) {\n if (coins[i - 1] > a) {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[i, a] = dp[i - 1, a];\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n dp[i, a] = Math.Min(dp[i - 1, a], dp[i, a - coins[i - 1]] + 1);\n }\n }\n }\n return dp[n, amt] != MAX ? dp[n, amt] : -1;\n}\n
coin_change.go/* \u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc coinChangeDP(coins []int, amt int) int {\n n := len(coins)\n max := amt + 1\n // \u521d\u59cb\u5316 dp \u8868\n dp := make([][]int, n+1)\n for i := 0; i <= n; i++ {\n dp[i] = make([]int, amt+1)\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n for a := 1; a <= amt; a++ {\n dp[0][a] = max\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n for i := 1; i <= n; i++ {\n for a := 1; a <= amt; a++ {\n if coins[i-1] > a {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[i][a] = dp[i-1][a]\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n dp[i][a] = int(math.Min(float64(dp[i-1][a]), float64(dp[i][a-coins[i-1]]+1)))\n }\n }\n }\n if dp[n][amt] != max {\n return dp[n][amt]\n }\n return -1\n}\n
coin_change.swift/* \u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc coinChangeDP(coins: [Int], amt: Int) -> Int {\n let n = coins.count\n let MAX = amt + 1\n // \u521d\u59cb\u5316 dp \u8868\n var dp = Array(repeating: Array(repeating: 0, count: amt + 1), count: n + 1)\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n for a in stride(from: 1, through: amt, by: 1) {\n dp[0][a] = MAX\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n for i in stride(from: 1, through: n, by: 1) {\n for a in stride(from: 1, through: amt, by: 1) {\n if coins[i - 1] > a {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[i][a] = dp[i - 1][a]\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n dp[i][a] = min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1)\n }\n }\n }\n return dp[n][amt] != MAX ? dp[n][amt] : -1\n}\n
coin_change.js/* \u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction coinChangeDP(coins, amt) {\n const n = coins.length;\n const MAX = amt + 1;\n // \u521d\u59cb\u5316 dp \u8868\n const dp = Array.from({ length: n + 1 }, () =>\n Array.from({ length: amt + 1 }, () => 0)\n );\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n for (let a = 1; a <= amt; a++) {\n dp[0][a] = MAX;\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n for (let i = 1; i <= n; i++) {\n for (let a = 1; a <= amt; a++) {\n if (coins[i - 1] > a) {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[i][a] = dp[i - 1][a];\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n dp[i][a] = Math.min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1);\n }\n }\n }\n return dp[n][amt] !== MAX ? dp[n][amt] : -1;\n}\n
coin_change.ts/* \u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction coinChangeDP(coins: Array<number>, amt: number): number {\n const n = coins.length;\n const MAX = amt + 1;\n // \u521d\u59cb\u5316 dp \u8868\n const dp = Array.from({ length: n + 1 }, () =>\n Array.from({ length: amt + 1 }, () => 0)\n );\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n for (let a = 1; a <= amt; a++) {\n dp[0][a] = MAX;\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n for (let i = 1; i <= n; i++) {\n for (let a = 1; a <= amt; a++) {\n if (coins[i - 1] > a) {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[i][a] = dp[i - 1][a];\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n dp[i][a] = Math.min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1);\n }\n }\n }\n return dp[n][amt] !== MAX ? dp[n][amt] : -1;\n}\n
coin_change.dart/* \u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212 */\nint coinChangeDP(List<int> coins, int amt) {\n int n = coins.length;\n int MAX = amt + 1;\n // \u521d\u59cb\u5316 dp \u8868\n List<List<int>> dp = List.generate(n + 1, (index) => List.filled(amt + 1, 0));\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n for (int a = 1; a <= amt; a++) {\n dp[0][a] = MAX;\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n for (int i = 1; i <= n; i++) {\n for (int a = 1; a <= amt; a++) {\n if (coins[i - 1] > a) {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[i][a] = dp[i - 1][a];\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n dp[i][a] = min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1);\n }\n }\n }\n return dp[n][amt] != MAX ? dp[n][amt] : -1;\n}\n
coin_change.rs/* \u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212 */\nfn coin_change_dp(coins: &[i32], amt: usize) -> i32 {\n let n = coins.len();\n let max = amt + 1;\n // \u521d\u59cb\u5316 dp \u8868\n let mut dp = vec![vec![0; amt + 1]; n + 1];\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n for a in 1..= amt {\n dp[0][a] = max;\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n for i in 1..=n {\n for a in 1..=amt {\n if coins[i - 1] > a as i32 {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[i][a] = dp[i - 1][a];\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n dp[i][a] = std::cmp::min(dp[i - 1][a], dp[i][a - coins[i - 1] as usize] + 1);\n }\n }\n }\n if dp[n][amt] != max { return dp[n][amt] as i32; } else { -1 }\n}\n
coin_change.c/* \u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212 */\nint coinChangeDP(int coins[], int amt, int coinsSize) {\n int n = coinsSize;\n int MAX = amt + 1;\n // \u521d\u59cb\u5316 dp \u8868\n int **dp = malloc((n + 1) * sizeof(int *));\n for (int i = 0; i <= n; i++) {\n dp[i] = calloc(amt + 1, sizeof(int));\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n for (int a = 1; a <= amt; a++) {\n dp[0][a] = MAX;\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n for (int i = 1; i <= n; i++) {\n for (int a = 1; a <= amt; a++) {\n if (coins[i - 1] > a) {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[i][a] = dp[i - 1][a];\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n dp[i][a] = myMin(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1);\n }\n }\n }\n int res = dp[n][amt] != MAX ? dp[n][amt] : -1;\n // \u91ca\u653e\u5185\u5b58\n for (int i = 0; i <= n; i++) {\n free(dp[i]);\n }\n free(dp);\n return res;\n}\n
coin_change.zig// \u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212\nfn coinChangeDP(comptime coins: []i32, comptime amt: usize) i32 {\n comptime var n = coins.len;\n comptime var max = amt + 1;\n // \u521d\u59cb\u5316 dp \u8868\n var dp = [_][amt + 1]i32{[_]i32{0} ** (amt + 1)} ** (n + 1);\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n for (1..amt + 1) |a| {\n dp[0][a] = max;\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n for (1..n + 1) |i| {\n for (1..amt + 1) |a| {\n if (coins[i - 1] > @as(i32, @intCast(a))) {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[i][a] = dp[i - 1][a];\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n dp[i][a] = @min(dp[i - 1][a], dp[i][a - @as(usize, @intCast(coins[i - 1]))] + 1);\n }\n }\n }\n if (dp[n][amt] != max) {\n return @intCast(dp[n][amt]);\n } else {\n return -1;\n }\n}\n
\u56fe 14-25 \u5c55\u793a\u4e86\u96f6\u94b1\u5151\u6362\u7684\u52a8\u6001\u89c4\u5212\u8fc7\u7a0b\uff0c\u548c\u5b8c\u5168\u80cc\u5305\u95ee\u9898\u975e\u5e38\u76f8\u4f3c\u3002
<1><2><3><4><5><6><7><8><9><10><11><12><13><14><15>\u56fe 14-25 \u00a0 \u96f6\u94b1\u5151\u6362\u95ee\u9898\u7684\u52a8\u6001\u89c4\u5212\u8fc7\u7a0b
"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#3_1","title":"3. \u00a0 \u7a7a\u95f4\u4f18\u5316","text":"\u96f6\u94b1\u5151\u6362\u7684\u7a7a\u95f4\u4f18\u5316\u7684\u5904\u7406\u65b9\u5f0f\u548c\u5b8c\u5168\u80cc\u5305\u95ee\u9898\u4e00\u81f4\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig coin_change.pydef coin_change_dp_comp(coins: list[int], amt: int) -> int:\n \"\"\"\u96f6\u94b1\u5151\u6362\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\"\"\"\n n = len(coins)\n MAX = amt + 1\n # \u521d\u59cb\u5316 dp \u8868\n dp = [MAX] * (amt + 1)\n dp[0] = 0\n # \u72b6\u6001\u8f6c\u79fb\n for i in range(1, n + 1):\n # \u6b63\u5e8f\u904d\u5386\n for a in range(1, amt + 1):\n if coins[i - 1] > a:\n # \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[a] = dp[a]\n else:\n # \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n dp[a] = min(dp[a], dp[a - coins[i - 1]] + 1)\n return dp[amt] if dp[amt] != MAX else -1\n
coin_change.cpp/* \u96f6\u94b1\u5151\u6362\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint coinChangeDPComp(vector<int> &coins, int amt) {\n int n = coins.size();\n int MAX = amt + 1;\n // \u521d\u59cb\u5316 dp \u8868\n vector<int> dp(amt + 1, MAX);\n dp[0] = 0;\n // \u72b6\u6001\u8f6c\u79fb\n for (int i = 1; i <= n; i++) {\n for (int a = 1; a <= amt; a++) {\n if (coins[i - 1] > a) {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[a] = dp[a];\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n dp[a] = min(dp[a], dp[a - coins[i - 1]] + 1);\n }\n }\n }\n return dp[amt] != MAX ? dp[amt] : -1;\n}\n
coin_change.java/* \u96f6\u94b1\u5151\u6362\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint coinChangeDPComp(int[] coins, int amt) {\n int n = coins.length;\n int MAX = amt + 1;\n // \u521d\u59cb\u5316 dp \u8868\n int[] dp = new int[amt + 1];\n Arrays.fill(dp, MAX);\n dp[0] = 0;\n // \u72b6\u6001\u8f6c\u79fb\n for (int i = 1; i <= n; i++) {\n for (int a = 1; a <= amt; a++) {\n if (coins[i - 1] > a) {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[a] = dp[a];\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n dp[a] = Math.min(dp[a], dp[a - coins[i - 1]] + 1);\n }\n }\n }\n return dp[amt] != MAX ? dp[amt] : -1;\n}\n
coin_change.cs/* \u96f6\u94b1\u5151\u6362\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint CoinChangeDPComp(int[] coins, int amt) {\n int n = coins.Length;\n int MAX = amt + 1;\n // \u521d\u59cb\u5316 dp \u8868\n int[] dp = new int[amt + 1];\n Array.Fill(dp, MAX);\n dp[0] = 0;\n // \u72b6\u6001\u8f6c\u79fb\n for (int i = 1; i <= n; i++) {\n for (int a = 1; a <= amt; a++) {\n if (coins[i - 1] > a) {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[a] = dp[a];\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n dp[a] = Math.Min(dp[a], dp[a - coins[i - 1]] + 1);\n }\n }\n }\n return dp[amt] != MAX ? dp[amt] : -1;\n}\n
coin_change.go/* \u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc coinChangeDPComp(coins []int, amt int) int {\n n := len(coins)\n max := amt + 1\n // \u521d\u59cb\u5316 dp \u8868\n dp := make([]int, amt+1)\n for i := 1; i <= amt; i++ {\n dp[i] = max\n }\n // \u72b6\u6001\u8f6c\u79fb\n for i := 1; i <= n; i++ {\n // \u5012\u5e8f\u904d\u5386\n for a := 1; a <= amt; a++ {\n if coins[i-1] > a {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[a] = dp[a]\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n dp[a] = int(math.Min(float64(dp[a]), float64(dp[a-coins[i-1]]+1)))\n }\n }\n }\n if dp[amt] != max {\n return dp[amt]\n }\n return -1\n}\n
coin_change.swift/* \u96f6\u94b1\u5151\u6362\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc coinChangeDPComp(coins: [Int], amt: Int) -> Int {\n let n = coins.count\n let MAX = amt + 1\n // \u521d\u59cb\u5316 dp \u8868\n var dp = Array(repeating: MAX, count: amt + 1)\n dp[0] = 0\n // \u72b6\u6001\u8f6c\u79fb\n for i in stride(from: 1, through: n, by: 1) {\n for a in stride(from: 1, through: amt, by: 1) {\n if coins[i - 1] > a {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[a] = dp[a]\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n dp[a] = min(dp[a], dp[a - coins[i - 1]] + 1)\n }\n }\n }\n return dp[amt] != MAX ? dp[amt] : -1\n}\n
coin_change.js/* \u96f6\u94b1\u5151\u6362\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction coinChangeDPComp(coins, amt) {\n const n = coins.length;\n const MAX = amt + 1;\n // \u521d\u59cb\u5316 dp \u8868\n const dp = Array.from({ length: amt + 1 }, () => MAX);\n dp[0] = 0;\n // \u72b6\u6001\u8f6c\u79fb\n for (let i = 1; i <= n; i++) {\n for (let a = 1; a <= amt; a++) {\n if (coins[i - 1] > a) {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[a] = dp[a];\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n dp[a] = Math.min(dp[a], dp[a - coins[i - 1]] + 1);\n }\n }\n }\n return dp[amt] !== MAX ? dp[amt] : -1;\n}\n
coin_change.ts/* \u96f6\u94b1\u5151\u6362\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction coinChangeDPComp(coins: Array<number>, amt: number): number {\n const n = coins.length;\n const MAX = amt + 1;\n // \u521d\u59cb\u5316 dp \u8868\n const dp = Array.from({ length: amt + 1 }, () => MAX);\n dp[0] = 0;\n // \u72b6\u6001\u8f6c\u79fb\n for (let i = 1; i <= n; i++) {\n for (let a = 1; a <= amt; a++) {\n if (coins[i - 1] > a) {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[a] = dp[a];\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n dp[a] = Math.min(dp[a], dp[a - coins[i - 1]] + 1);\n }\n }\n }\n return dp[amt] !== MAX ? dp[amt] : -1;\n}\n
coin_change.dart/* \u96f6\u94b1\u5151\u6362\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint coinChangeDPComp(List<int> coins, int amt) {\n int n = coins.length;\n int MAX = amt + 1;\n // \u521d\u59cb\u5316 dp \u8868\n List<int> dp = List.filled(amt + 1, MAX);\n dp[0] = 0;\n // \u72b6\u6001\u8f6c\u79fb\n for (int i = 1; i <= n; i++) {\n for (int a = 1; a <= amt; a++) {\n if (coins[i - 1] > a) {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[a] = dp[a];\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n dp[a] = min(dp[a], dp[a - coins[i - 1]] + 1);\n }\n }\n }\n return dp[amt] != MAX ? dp[amt] : -1;\n}\n
coin_change.rs/* \u96f6\u94b1\u5151\u6362\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfn coin_change_dp_comp(coins: &[i32], amt: usize) -> i32 {\n let n = coins.len();\n let max = amt + 1;\n // \u521d\u59cb\u5316 dp \u8868\n let mut dp = vec![0; amt + 1];\n dp.fill(max);\n dp[0] = 0;\n // \u72b6\u6001\u8f6c\u79fb\n for i in 1..=n {\n for a in 1..=amt {\n if coins[i - 1] > a as i32 {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[a] = dp[a];\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n dp[a] = std::cmp::min(dp[a], dp[a - coins[i - 1] as usize] + 1);\n }\n }\n }\n if dp[amt] != max { return dp[amt] as i32; } else { -1 }\n}\n
coin_change.c/* \u96f6\u94b1\u5151\u6362\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint coinChangeDPComp(int coins[], int amt, int coinsSize) {\n int n = coinsSize;\n int MAX = amt + 1;\n // \u521d\u59cb\u5316 dp \u8868\n int *dp = calloc(amt + 1, sizeof(int));\n dp[0] = 0;\n // \u72b6\u6001\u8f6c\u79fb\n for (int i = 1; i <= n; i++) {\n for (int a = 1; a <= amt; a++) {\n if (coins[i - 1] > a) {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[a] = dp[a];\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n dp[a] = myMin(dp[a], dp[a - coins[i - 1]] + 1);\n }\n }\n }\n int res = dp[amt] != MAX ? dp[amt] : -1;\n // \u91ca\u653e\u5185\u5b58\n free(dp);\n return res;\n}\n
coin_change.zig// \u96f6\u94b1\u5151\u6362\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\nfn coinChangeDPComp(comptime coins: []i32, comptime amt: usize) i32 {\n comptime var n = coins.len;\n comptime var max = amt + 1;\n // \u521d\u59cb\u5316 dp \u8868\n var dp = [_]i32{0} ** (amt + 1);\n @memset(&dp, max);\n dp[0] = 0;\n // \u72b6\u6001\u8f6c\u79fb\n for (1..n + 1) |i| {\n for (1..amt + 1) |a| {\n if (coins[i - 1] > @as(i32, @intCast(a))) {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[a] = dp[a];\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n dp[a] = @min(dp[a], dp[a - @as(usize, @intCast(coins[i - 1]))] + 1);\n }\n }\n }\n if (dp[amt] != max) {\n return @intCast(dp[amt]);\n } else {\n return -1;\n }\n}\n
"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#1453-ii","title":"14.5.3 \u00a0 \u96f6\u94b1\u5151\u6362\u95ee\u9898 II","text":"Question
\u7ed9\u5b9a \\(n\\) \u79cd\u786c\u5e01\uff0c\u7b2c \\(i\\) \u79cd\u786c\u5e01\u7684\u9762\u503c\u4e3a \\(coins[i - 1]\\) \uff0c\u76ee\u6807\u91d1\u989d\u4e3a \\(amt\\) \uff0c\u6bcf\u79cd\u786c\u5e01\u53ef\u4ee5\u91cd\u590d\u9009\u53d6\uff0c\u95ee\u51d1\u51fa\u76ee\u6807\u91d1\u989d\u7684\u786c\u5e01\u7ec4\u5408\u6570\u91cf\u3002\u793a\u4f8b\u5982\u56fe 14-26 \u6240\u793a\u3002
\u56fe 14-26 \u00a0 \u96f6\u94b1\u5151\u6362\u95ee\u9898 II \u7684\u793a\u4f8b\u6570\u636e
"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#1_2","title":"1. \u00a0 \u52a8\u6001\u89c4\u5212\u601d\u8def","text":"\u76f8\u6bd4\u4e8e\u4e0a\u4e00\u9898\uff0c\u672c\u9898\u76ee\u6807\u662f\u6c42\u7ec4\u5408\u6570\u91cf\uff0c\u56e0\u6b64\u5b50\u95ee\u9898\u53d8\u4e3a\uff1a\u524d \\(i\\) \u79cd\u786c\u5e01\u80fd\u591f\u51d1\u51fa\u91d1\u989d \\(a\\) \u7684\u7ec4\u5408\u6570\u91cf\u3002\u800c \\(dp\\) \u8868\u4ecd\u7136\u662f\u5c3a\u5bf8\u4e3a \\((n+1) \\times (amt + 1)\\) \u7684\u4e8c\u7ef4\u77e9\u9635\u3002
\u5f53\u524d\u72b6\u6001\u7684\u7ec4\u5408\u6570\u91cf\u7b49\u4e8e\u4e0d\u9009\u5f53\u524d\u786c\u5e01\u4e0e\u9009\u5f53\u524d\u786c\u5e01\u8fd9\u4e24\u79cd\u51b3\u7b56\u7684\u7ec4\u5408\u6570\u91cf\u4e4b\u548c\u3002\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u4e3a\uff1a
\\[ dp[i, a] = dp[i-1, a] + dp[i, a - coins[i-1]] \\]\u5f53\u76ee\u6807\u91d1\u989d\u4e3a \\(0\\) \u65f6\uff0c\u65e0\u987b\u9009\u62e9\u4efb\u4f55\u786c\u5e01\u5373\u53ef\u51d1\u51fa\u76ee\u6807\u91d1\u989d\uff0c\u56e0\u6b64\u5e94\u5c06\u9996\u5217\u6240\u6709 \\(dp[i, 0]\\) \u90fd\u521d\u59cb\u5316\u4e3a \\(1\\) \u3002\u5f53\u65e0\u786c\u5e01\u65f6\uff0c\u65e0\u6cd5\u51d1\u51fa\u4efb\u4f55 \\(>0\\) \u7684\u76ee\u6807\u91d1\u989d\uff0c\u56e0\u6b64\u9996\u884c\u6240\u6709 \\(dp[0, a]\\) \u90fd\u7b49\u4e8e \\(0\\) \u3002
"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#2_2","title":"2. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"PythonC++JavaC#GoSwiftJSTSDartRustCZig coin_change_ii.pydef coin_change_ii_dp(coins: list[int], amt: int) -> int:\n \"\"\"\u96f6\u94b1\u5151\u6362 II\uff1a\u52a8\u6001\u89c4\u5212\"\"\"\n n = len(coins)\n # \u521d\u59cb\u5316 dp \u8868\n dp = [[0] * (amt + 1) for _ in range(n + 1)]\n # \u521d\u59cb\u5316\u9996\u5217\n for i in range(n + 1):\n dp[i][0] = 1\n # \u72b6\u6001\u8f6c\u79fb\n for i in range(1, n + 1):\n for a in range(1, amt + 1):\n if coins[i - 1] > a:\n # \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[i][a] = dp[i - 1][a]\n else:\n # \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]]\n return dp[n][amt]\n
coin_change_ii.cpp/* \u96f6\u94b1\u5151\u6362 II\uff1a\u52a8\u6001\u89c4\u5212 */\nint coinChangeIIDP(vector<int> &coins, int amt) {\n int n = coins.size();\n // \u521d\u59cb\u5316 dp \u8868\n vector<vector<int>> dp(n + 1, vector<int>(amt + 1, 0));\n // \u521d\u59cb\u5316\u9996\u5217\n for (int i = 0; i <= n; i++) {\n dp[i][0] = 1;\n }\n // \u72b6\u6001\u8f6c\u79fb\n for (int i = 1; i <= n; i++) {\n for (int a = 1; a <= amt; a++) {\n if (coins[i - 1] > a) {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[i][a] = dp[i - 1][a];\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]];\n }\n }\n }\n return dp[n][amt];\n}\n
coin_change_ii.java/* \u96f6\u94b1\u5151\u6362 II\uff1a\u52a8\u6001\u89c4\u5212 */\nint coinChangeIIDP(int[] coins, int amt) {\n int n = coins.length;\n // \u521d\u59cb\u5316 dp \u8868\n int[][] dp = new int[n + 1][amt + 1];\n // \u521d\u59cb\u5316\u9996\u5217\n for (int i = 0; i <= n; i++) {\n dp[i][0] = 1;\n }\n // \u72b6\u6001\u8f6c\u79fb\n for (int i = 1; i <= n; i++) {\n for (int a = 1; a <= amt; a++) {\n if (coins[i - 1] > a) {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[i][a] = dp[i - 1][a];\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]];\n }\n }\n }\n return dp[n][amt];\n}\n
coin_change_ii.cs/* \u96f6\u94b1\u5151\u6362 II\uff1a\u52a8\u6001\u89c4\u5212 */\nint CoinChangeIIDP(int[] coins, int amt) {\n int n = coins.Length;\n // \u521d\u59cb\u5316 dp \u8868\n int[,] dp = new int[n + 1, amt + 1];\n // \u521d\u59cb\u5316\u9996\u5217\n for (int i = 0; i <= n; i++) {\n dp[i, 0] = 1;\n }\n // \u72b6\u6001\u8f6c\u79fb\n for (int i = 1; i <= n; i++) {\n for (int a = 1; a <= amt; a++) {\n if (coins[i - 1] > a) {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[i, a] = dp[i - 1, a];\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n dp[i, a] = dp[i - 1, a] + dp[i, a - coins[i - 1]];\n }\n }\n }\n return dp[n, amt];\n}\n
coin_change_ii.go/* \u96f6\u94b1\u5151\u6362 II\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc coinChangeIIDP(coins []int, amt int) int {\n n := len(coins)\n // \u521d\u59cb\u5316 dp \u8868\n dp := make([][]int, n+1)\n for i := 0; i <= n; i++ {\n dp[i] = make([]int, amt+1)\n }\n // \u521d\u59cb\u5316\u9996\u5217\n for i := 0; i <= n; i++ {\n dp[i][0] = 1\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n for i := 1; i <= n; i++ {\n for a := 1; a <= amt; a++ {\n if coins[i-1] > a {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[i][a] = dp[i-1][a]\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n dp[i][a] = dp[i-1][a] + dp[i][a-coins[i-1]]\n }\n }\n }\n return dp[n][amt]\n}\n
coin_change_ii.swift/* \u96f6\u94b1\u5151\u6362 II\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc coinChangeIIDP(coins: [Int], amt: Int) -> Int {\n let n = coins.count\n // \u521d\u59cb\u5316 dp \u8868\n var dp = Array(repeating: Array(repeating: 0, count: amt + 1), count: n + 1)\n // \u521d\u59cb\u5316\u9996\u5217\n for i in stride(from: 0, through: n, by: 1) {\n dp[i][0] = 1\n }\n // \u72b6\u6001\u8f6c\u79fb\n for i in stride(from: 1, through: n, by: 1) {\n for a in stride(from: 1, through: amt, by: 1) {\n if coins[i - 1] > a {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[i][a] = dp[i - 1][a]\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]]\n }\n }\n }\n return dp[n][amt]\n}\n
coin_change_ii.js/* \u96f6\u94b1\u5151\u6362 II\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction coinChangeIIDP(coins, amt) {\n const n = coins.length;\n // \u521d\u59cb\u5316 dp \u8868\n const dp = Array.from({ length: n + 1 }, () =>\n Array.from({ length: amt + 1 }, () => 0)\n );\n // \u521d\u59cb\u5316\u9996\u5217\n for (let i = 0; i <= n; i++) {\n dp[i][0] = 1;\n }\n // \u72b6\u6001\u8f6c\u79fb\n for (let i = 1; i <= n; i++) {\n for (let a = 1; a <= amt; a++) {\n if (coins[i - 1] > a) {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[i][a] = dp[i - 1][a];\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]];\n }\n }\n }\n return dp[n][amt];\n}\n
coin_change_ii.ts/* \u96f6\u94b1\u5151\u6362 II\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction coinChangeIIDP(coins: Array<number>, amt: number): number {\n const n = coins.length;\n // \u521d\u59cb\u5316 dp \u8868\n const dp = Array.from({ length: n + 1 }, () =>\n Array.from({ length: amt + 1 }, () => 0)\n );\n // \u521d\u59cb\u5316\u9996\u5217\n for (let i = 0; i <= n; i++) {\n dp[i][0] = 1;\n }\n // \u72b6\u6001\u8f6c\u79fb\n for (let i = 1; i <= n; i++) {\n for (let a = 1; a <= amt; a++) {\n if (coins[i - 1] > a) {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[i][a] = dp[i - 1][a];\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]];\n }\n }\n }\n return dp[n][amt];\n}\n
coin_change_ii.dart/* \u96f6\u94b1\u5151\u6362 II\uff1a\u52a8\u6001\u89c4\u5212 */\nint coinChangeIIDP(List<int> coins, int amt) {\n int n = coins.length;\n // \u521d\u59cb\u5316 dp \u8868\n List<List<int>> dp = List.generate(n + 1, (index) => List.filled(amt + 1, 0));\n // \u521d\u59cb\u5316\u9996\u5217\n for (int i = 0; i <= n; i++) {\n dp[i][0] = 1;\n }\n // \u72b6\u6001\u8f6c\u79fb\n for (int i = 1; i <= n; i++) {\n for (int a = 1; a <= amt; a++) {\n if (coins[i - 1] > a) {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[i][a] = dp[i - 1][a];\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]];\n }\n }\n }\n return dp[n][amt];\n}\n
coin_change_ii.rs/* \u96f6\u94b1\u5151\u6362 II\uff1a\u52a8\u6001\u89c4\u5212 */\nfn coin_change_ii_dp(coins: &[i32], amt: usize) -> i32 {\n let n = coins.len();\n // \u521d\u59cb\u5316 dp \u8868\n let mut dp = vec![vec![0; amt + 1]; n + 1];\n // \u521d\u59cb\u5316\u9996\u5217\n for i in 0..= n {\n dp[i][0] = 1;\n }\n // \u72b6\u6001\u8f6c\u79fb\n for i in 1..=n {\n for a in 1..=amt {\n if coins[i - 1] > a as i32 {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[i][a] = dp[i - 1][a];\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1] as usize];\n }\n }\n }\n dp[n][amt]\n}\n
coin_change_ii.c/* \u96f6\u94b1\u5151\u6362 II\uff1a\u52a8\u6001\u89c4\u5212 */\nint coinChangeIIDP(int coins[], int amt, int coinsSize) {\n int n = coinsSize;\n // \u521d\u59cb\u5316 dp \u8868\n int **dp = malloc((n + 1) * sizeof(int *));\n for (int i = 0; i <= n; i++) {\n dp[i] = calloc(amt + 1, sizeof(int));\n }\n // \u521d\u59cb\u5316\u9996\u5217\n for (int i = 0; i <= n; i++) {\n dp[i][0] = 1;\n }\n // \u72b6\u6001\u8f6c\u79fb\n for (int i = 1; i <= n; i++) {\n for (int a = 1; a <= amt; a++) {\n if (coins[i - 1] > a) {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[i][a] = dp[i - 1][a];\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]];\n }\n }\n }\n int res = dp[n][amt];\n // \u91ca\u653e\u5185\u5b58\n for (int i = 0; i <= n; i++) {\n free(dp[i]);\n }\n free(dp);\n return res;\n}\n
coin_change_ii.zig// \u96f6\u94b1\u5151\u6362 II\uff1a\u52a8\u6001\u89c4\u5212\nfn coinChangeIIDP(comptime coins: []i32, comptime amt: usize) i32 {\n comptime var n = coins.len;\n // \u521d\u59cb\u5316 dp \u8868\n var dp = [_][amt + 1]i32{[_]i32{0} ** (amt + 1)} ** (n + 1);\n // \u521d\u59cb\u5316\u9996\u5217\n for (0..n + 1) |i| {\n dp[i][0] = 1;\n }\n // \u72b6\u6001\u8f6c\u79fb\n for (1..n + 1) |i| {\n for (1..amt + 1) |a| {\n if (coins[i - 1] > @as(i32, @intCast(a))) {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[i][a] = dp[i - 1][a];\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n dp[i][a] = dp[i - 1][a] + dp[i][a - @as(usize, @intCast(coins[i - 1]))];\n }\n }\n }\n return dp[n][amt];\n}\n
"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#3_2","title":"3. \u00a0 \u7a7a\u95f4\u4f18\u5316","text":"\u7a7a\u95f4\u4f18\u5316\u5904\u7406\u65b9\u5f0f\u76f8\u540c\uff0c\u5220\u9664\u786c\u5e01\u7ef4\u5ea6\u5373\u53ef\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig coin_change_ii.pydef coin_change_ii_dp_comp(coins: list[int], amt: int) -> int:\n \"\"\"\u96f6\u94b1\u5151\u6362 II\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\"\"\"\n n = len(coins)\n # \u521d\u59cb\u5316 dp \u8868\n dp = [0] * (amt + 1)\n dp[0] = 1\n # \u72b6\u6001\u8f6c\u79fb\n for i in range(1, n + 1):\n # \u6b63\u5e8f\u904d\u5386\n for a in range(1, amt + 1):\n if coins[i - 1] > a:\n # \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[a] = dp[a]\n else:\n # \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n dp[a] = dp[a] + dp[a - coins[i - 1]]\n return dp[amt]\n
coin_change_ii.cpp/* \u96f6\u94b1\u5151\u6362 II\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint coinChangeIIDPComp(vector<int> &coins, int amt) {\n int n = coins.size();\n // \u521d\u59cb\u5316 dp \u8868\n vector<int> dp(amt + 1, 0);\n dp[0] = 1;\n // \u72b6\u6001\u8f6c\u79fb\n for (int i = 1; i <= n; i++) {\n for (int a = 1; a <= amt; a++) {\n if (coins[i - 1] > a) {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[a] = dp[a];\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n dp[a] = dp[a] + dp[a - coins[i - 1]];\n }\n }\n }\n return dp[amt];\n}\n
coin_change_ii.java/* \u96f6\u94b1\u5151\u6362 II\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint coinChangeIIDPComp(int[] coins, int amt) {\n int n = coins.length;\n // \u521d\u59cb\u5316 dp \u8868\n int[] dp = new int[amt + 1];\n dp[0] = 1;\n // \u72b6\u6001\u8f6c\u79fb\n for (int i = 1; i <= n; i++) {\n for (int a = 1; a <= amt; a++) {\n if (coins[i - 1] > a) {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[a] = dp[a];\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n dp[a] = dp[a] + dp[a - coins[i - 1]];\n }\n }\n }\n return dp[amt];\n}\n
coin_change_ii.cs/* \u96f6\u94b1\u5151\u6362 II\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint CoinChangeIIDPComp(int[] coins, int amt) {\n int n = coins.Length;\n // \u521d\u59cb\u5316 dp \u8868\n int[] dp = new int[amt + 1];\n dp[0] = 1;\n // \u72b6\u6001\u8f6c\u79fb\n for (int i = 1; i <= n; i++) {\n for (int a = 1; a <= amt; a++) {\n if (coins[i - 1] > a) {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[a] = dp[a];\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n dp[a] = dp[a] + dp[a - coins[i - 1]];\n }\n }\n }\n return dp[amt];\n}\n
coin_change_ii.go/* \u96f6\u94b1\u5151\u6362 II\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc coinChangeIIDPComp(coins []int, amt int) int {\n n := len(coins)\n // \u521d\u59cb\u5316 dp \u8868\n dp := make([]int, amt+1)\n dp[0] = 1\n // \u72b6\u6001\u8f6c\u79fb\n for i := 1; i <= n; i++ {\n // \u5012\u5e8f\u904d\u5386\n for a := 1; a <= amt; a++ {\n if coins[i-1] > a {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[a] = dp[a]\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n dp[a] = dp[a] + dp[a-coins[i-1]]\n }\n }\n }\n return dp[amt]\n}\n
coin_change_ii.swift/* \u96f6\u94b1\u5151\u6362 II\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc coinChangeIIDPComp(coins: [Int], amt: Int) -> Int {\n let n = coins.count\n // \u521d\u59cb\u5316 dp \u8868\n var dp = Array(repeating: 0, count: amt + 1)\n dp[0] = 1\n // \u72b6\u6001\u8f6c\u79fb\n for i in stride(from: 1, through: n, by: 1) {\n for a in stride(from: 1, through: amt, by: 1) {\n if coins[i - 1] > a {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[a] = dp[a]\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n dp[a] = dp[a] + dp[a - coins[i - 1]]\n }\n }\n }\n return dp[amt]\n}\n
coin_change_ii.js/* \u96f6\u94b1\u5151\u6362 II\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction coinChangeIIDPComp(coins, amt) {\n const n = coins.length;\n // \u521d\u59cb\u5316 dp \u8868\n const dp = Array.from({ length: amt + 1 }, () => 0);\n dp[0] = 1;\n // \u72b6\u6001\u8f6c\u79fb\n for (let i = 1; i <= n; i++) {\n for (let a = 1; a <= amt; a++) {\n if (coins[i - 1] > a) {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[a] = dp[a];\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n dp[a] = dp[a] + dp[a - coins[i - 1]];\n }\n }\n }\n return dp[amt];\n}\n
coin_change_ii.ts/* \u96f6\u94b1\u5151\u6362 II\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction coinChangeIIDPComp(coins: Array<number>, amt: number): number {\n const n = coins.length;\n // \u521d\u59cb\u5316 dp \u8868\n const dp = Array.from({ length: amt + 1 }, () => 0);\n dp[0] = 1;\n // \u72b6\u6001\u8f6c\u79fb\n for (let i = 1; i <= n; i++) {\n for (let a = 1; a <= amt; a++) {\n if (coins[i - 1] > a) {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[a] = dp[a];\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n dp[a] = dp[a] + dp[a - coins[i - 1]];\n }\n }\n }\n return dp[amt];\n}\n
coin_change_ii.dart/* \u96f6\u94b1\u5151\u6362 II\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint coinChangeIIDPComp(List<int> coins, int amt) {\n int n = coins.length;\n // \u521d\u59cb\u5316 dp \u8868\n List<int> dp = List.filled(amt + 1, 0);\n dp[0] = 1;\n // \u72b6\u6001\u8f6c\u79fb\n for (int i = 1; i <= n; i++) {\n for (int a = 1; a <= amt; a++) {\n if (coins[i - 1] > a) {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[a] = dp[a];\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n dp[a] = dp[a] + dp[a - coins[i - 1]];\n }\n }\n }\n return dp[amt];\n}\n
coin_change_ii.rs/* \u96f6\u94b1\u5151\u6362 II\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfn coin_change_ii_dp_comp(coins: &[i32], amt: usize) -> i32 {\n let n = coins.len();\n // \u521d\u59cb\u5316 dp \u8868\n let mut dp = vec![0; amt + 1];\n dp[0] = 1;\n // \u72b6\u6001\u8f6c\u79fb\n for i in 1..=n {\n for a in 1..=amt {\n if coins[i - 1] > a as i32 {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[a] = dp[a];\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n dp[a] = dp[a] + dp[a - coins[i - 1] as usize];\n }\n }\n }\n dp[amt]\n}\n
coin_change_ii.c/* \u96f6\u94b1\u5151\u6362 II\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint coinChangeIIDPComp(int coins[], int amt, int coinsSize) {\n int n = coinsSize;\n // \u521d\u59cb\u5316 dp \u8868\n int *dp = calloc(amt + 1, sizeof(int));\n dp[0] = 1;\n // \u72b6\u6001\u8f6c\u79fb\n for (int i = 1; i <= n; i++) {\n for (int a = 1; a <= amt; a++) {\n if (coins[i - 1] > a) {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[a] = dp[a];\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n dp[a] = dp[a] + dp[a - coins[i - 1]];\n }\n }\n }\n int res = dp[amt];\n // \u91ca\u653e\u5185\u5b58\n free(dp);\n return res;\n}\n
coin_change_ii.zig// \u96f6\u94b1\u5151\u6362 II\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\nfn coinChangeIIDPComp(comptime coins: []i32, comptime amt: usize) i32 {\n comptime var n = coins.len;\n // \u521d\u59cb\u5316 dp \u8868\n var dp = [_]i32{0} ** (amt + 1);\n dp[0] = 1;\n // \u72b6\u6001\u8f6c\u79fb\n for (1..n + 1) |i| {\n for (1..amt + 1) |a| {\n if (coins[i - 1] > @as(i32, @intCast(a))) {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[a] = dp[a];\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n dp[a] = dp[a] + dp[a - @as(usize, @intCast(coins[i - 1]))];\n }\n }\n }\n return dp[amt];\n}\n
"},{"location":"chapter_graph/","title":"\u7b2c 9 \u7ae0 \u00a0 \u56fe","text":"Abstract
\u5728\u751f\u547d\u65c5\u9014\u4e2d\uff0c\u6211\u4eec\u5c31\u50cf\u662f\u4e00\u4e2a\u4e2a\u8282\u70b9\uff0c\u88ab\u65e0\u6570\u770b\u4e0d\u89c1\u7684\u8fb9\u76f8\u8fde\u3002
\u6bcf\u4e00\u6b21\u7684\u76f8\u8bc6\u4e0e\u76f8\u79bb\uff0c\u90fd\u5728\u8fd9\u5f20\u5de8\u5927\u7684\u7f51\u7edc\u56fe\u4e2d\u7559\u4e0b\u72ec\u7279\u7684\u5370\u8bb0\u3002
"},{"location":"chapter_graph/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"\u300c\u56fe graph\u300d\u662f\u4e00\u79cd\u975e\u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff0c\u7531\u300c\u9876\u70b9 vertex\u300d\u548c\u300c\u8fb9 edge\u300d\u7ec4\u6210\u3002\u6211\u4eec\u53ef\u4ee5\u5c06\u56fe \\(G\\) \u62bd\u8c61\u5730\u8868\u793a\u4e3a\u4e00\u7ec4\u9876\u70b9 \\(V\\) \u548c\u4e00\u7ec4\u8fb9 \\(E\\) \u7684\u96c6\u5408\u3002\u4ee5\u4e0b\u793a\u4f8b\u5c55\u793a\u4e86\u4e00\u4e2a\u5305\u542b 5 \u4e2a\u9876\u70b9\u548c 7 \u6761\u8fb9\u7684\u56fe\u3002
\\[ \\begin{aligned} V & = \\{ 1, 2, 3, 4, 5 \\} \\newline E & = \\{ (1,2), (1,3), (1,5), (2,3), (2,4), (2,5), (4,5) \\} \\newline G & = \\{ V, E \\} \\newline \\end{aligned} \\]\u5982\u679c\u5c06\u9876\u70b9\u770b\u4f5c\u8282\u70b9\uff0c\u5c06\u8fb9\u770b\u4f5c\u8fde\u63a5\u5404\u4e2a\u8282\u70b9\u7684\u5f15\u7528\uff08\u6307\u9488\uff09\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5c06\u56fe\u770b\u4f5c\u4e00\u79cd\u4ece\u94fe\u8868\u62d3\u5c55\u800c\u6765\u7684\u6570\u636e\u7ed3\u6784\u3002\u5982\u56fe 9-1 \u6240\u793a\uff0c\u76f8\u8f83\u4e8e\u7ebf\u6027\u5173\u7cfb\uff08\u94fe\u8868\uff09\u548c\u5206\u6cbb\u5173\u7cfb\uff08\u6811\uff09\uff0c\u7f51\u7edc\u5173\u7cfb\uff08\u56fe\uff09\u7684\u81ea\u7531\u5ea6\u66f4\u9ad8\uff0c\u56e0\u800c\u66f4\u4e3a\u590d\u6742\u3002
\u56fe 9-1 \u00a0 \u94fe\u8868\u3001\u6811\u3001\u56fe\u4e4b\u95f4\u7684\u5173\u7cfb
"},{"location":"chapter_graph/graph/#911","title":"9.1.1 \u00a0 \u56fe\u5e38\u89c1\u7c7b\u578b\u4e0e\u672f\u8bed","text":"\u6839\u636e\u8fb9\u662f\u5426\u5177\u6709\u65b9\u5411\uff0c\u53ef\u5206\u4e3a\u300c\u65e0\u5411\u56fe undirected graph\u300d\u548c\u300c\u6709\u5411\u56fe directed graph\u300d\uff0c\u5982\u56fe 9-2 \u6240\u793a\u3002
\u56fe 9-2 \u00a0 \u6709\u5411\u56fe\u4e0e\u65e0\u5411\u56fe
\u6839\u636e\u6240\u6709\u9876\u70b9\u662f\u5426\u8fde\u901a\uff0c\u53ef\u5206\u4e3a\u300c\u8fde\u901a\u56fe connected graph\u300d\u548c\u300c\u975e\u8fde\u901a\u56fe disconnected graph\u300d\uff0c\u5982\u56fe 9-3 \u6240\u793a\u3002
\u56fe 9-3 \u00a0 \u8fde\u901a\u56fe\u4e0e\u975e\u8fde\u901a\u56fe
\u6211\u4eec\u8fd8\u53ef\u4ee5\u4e3a\u8fb9\u6dfb\u52a0\u201c\u6743\u91cd\u201d\u53d8\u91cf\uff0c\u4ece\u800c\u5f97\u5230\u5982\u56fe 9-4 \u6240\u793a\u7684\u300c\u6709\u6743\u56fe weighted graph\u300d\u3002\u4f8b\u5982\u5728\u201c\u738b\u8005\u8363\u8000\u201d\u7b49\u624b\u6e38\u4e2d\uff0c\u7cfb\u7edf\u4f1a\u6839\u636e\u5171\u540c\u6e38\u620f\u65f6\u95f4\u6765\u8ba1\u7b97\u73a9\u5bb6\u4e4b\u95f4\u7684\u201c\u4eb2\u5bc6\u5ea6\u201d\uff0c\u8fd9\u79cd\u4eb2\u5bc6\u5ea6\u7f51\u7edc\u5c31\u53ef\u4ee5\u7528\u6709\u6743\u56fe\u6765\u8868\u793a\u3002
\u56fe 9-4 \u00a0 \u6709\u6743\u56fe\u4e0e\u65e0\u6743\u56fe
\u56fe\u6570\u636e\u7ed3\u6784\u5305\u542b\u4ee5\u4e0b\u5e38\u7528\u672f\u8bed\u3002
\u56fe\u7684\u5e38\u7528\u8868\u793a\u65b9\u5f0f\u5305\u62ec\u201c\u90bb\u63a5\u77e9\u9635\u201d\u548c\u201c\u90bb\u63a5\u8868\u201d\u3002\u4ee5\u4e0b\u4f7f\u7528\u65e0\u5411\u56fe\u8fdb\u884c\u4e3e\u4f8b\u3002
"},{"location":"chapter_graph/graph/#1","title":"1. \u00a0 \u90bb\u63a5\u77e9\u9635","text":"\u8bbe\u56fe\u7684\u9876\u70b9\u6570\u91cf\u4e3a \\(n\\) \uff0c\u300c\u90bb\u63a5\u77e9\u9635 adjacency matrix\u300d\u4f7f\u7528\u4e00\u4e2a \\(n \\times n\\) \u5927\u5c0f\u7684\u77e9\u9635\u6765\u8868\u793a\u56fe\uff0c\u6bcf\u4e00\u884c\uff08\u5217\uff09\u4ee3\u8868\u4e00\u4e2a\u9876\u70b9\uff0c\u77e9\u9635\u5143\u7d20\u4ee3\u8868\u8fb9\uff0c\u7528 \\(1\\) \u6216 \\(0\\) \u8868\u793a\u4e24\u4e2a\u9876\u70b9\u4e4b\u95f4\u662f\u5426\u5b58\u5728\u8fb9\u3002
\u5982\u56fe 9-5 \u6240\u793a\uff0c\u8bbe\u90bb\u63a5\u77e9\u9635\u4e3a \\(M\\)\u3001\u9876\u70b9\u5217\u8868\u4e3a \\(V\\) \uff0c\u90a3\u4e48\u77e9\u9635\u5143\u7d20 \\(M[i, j] = 1\\) \u8868\u793a\u9876\u70b9 \\(V[i]\\) \u5230\u9876\u70b9 \\(V[j]\\) \u4e4b\u95f4\u5b58\u5728\u8fb9\uff0c\u53cd\u4e4b \\(M[i, j] = 0\\) \u8868\u793a\u4e24\u9876\u70b9\u4e4b\u95f4\u65e0\u8fb9\u3002
\u56fe 9-5 \u00a0 \u56fe\u7684\u90bb\u63a5\u77e9\u9635\u8868\u793a
\u90bb\u63a5\u77e9\u9635\u5177\u6709\u4ee5\u4e0b\u7279\u6027\u3002
\u4f7f\u7528\u90bb\u63a5\u77e9\u9635\u8868\u793a\u56fe\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u77e9\u9635\u5143\u7d20\u4ee5\u83b7\u53d6\u8fb9\uff0c\u56e0\u6b64\u589e\u5220\u67e5\u6539\u64cd\u4f5c\u7684\u6548\u7387\u5f88\u9ad8\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u5747\u4e3a \\(O(1)\\) \u3002\u7136\u800c\uff0c\u77e9\u9635\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\) \uff0c\u5185\u5b58\u5360\u7528\u8f83\u591a\u3002
"},{"location":"chapter_graph/graph/#2","title":"2. \u00a0 \u90bb\u63a5\u8868","text":"\u300c\u90bb\u63a5\u8868 adjacency list\u300d\u4f7f\u7528 \\(n\\) \u4e2a\u94fe\u8868\u6765\u8868\u793a\u56fe\uff0c\u94fe\u8868\u8282\u70b9\u8868\u793a\u9876\u70b9\u3002\u7b2c \\(i\\) \u4e2a\u94fe\u8868\u5bf9\u5e94\u9876\u70b9 \\(i\\) \uff0c\u5176\u4e2d\u5b58\u50a8\u4e86\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\uff08\u4e0e\u8be5\u9876\u70b9\u76f8\u8fde\u7684\u9876\u70b9\uff09\u3002\u56fe 9-6 \u5c55\u793a\u4e86\u4e00\u4e2a\u4f7f\u7528\u90bb\u63a5\u8868\u5b58\u50a8\u7684\u56fe\u7684\u793a\u4f8b\u3002
\u56fe 9-6 \u00a0 \u56fe\u7684\u90bb\u63a5\u8868\u8868\u793a
\u90bb\u63a5\u8868\u4ec5\u5b58\u50a8\u5b9e\u9645\u5b58\u5728\u7684\u8fb9\uff0c\u800c\u8fb9\u7684\u603b\u6570\u901a\u5e38\u8fdc\u5c0f\u4e8e \\(n^2\\) \uff0c\u56e0\u6b64\u5b83\u66f4\u52a0\u8282\u7701\u7a7a\u95f4\u3002\u7136\u800c\uff0c\u5728\u90bb\u63a5\u8868\u4e2d\u9700\u8981\u901a\u8fc7\u904d\u5386\u94fe\u8868\u6765\u67e5\u627e\u8fb9\uff0c\u56e0\u6b64\u5176\u65f6\u95f4\u6548\u7387\u4e0d\u5982\u90bb\u63a5\u77e9\u9635\u3002
\u89c2\u5bdf\u56fe 9-6 \uff0c\u90bb\u63a5\u8868\u7ed3\u6784\u4e0e\u54c8\u5e0c\u8868\u4e2d\u7684\u201c\u94fe\u5f0f\u5730\u5740\u201d\u975e\u5e38\u76f8\u4f3c\uff0c\u56e0\u6b64\u6211\u4eec\u4e5f\u53ef\u4ee5\u91c7\u7528\u7c7b\u4f3c\u7684\u65b9\u6cd5\u6765\u4f18\u5316\u6548\u7387\u3002\u6bd4\u5982\u5f53\u94fe\u8868\u8f83\u957f\u65f6\uff0c\u53ef\u4ee5\u5c06\u94fe\u8868\u8f6c\u5316\u4e3a AVL \u6811\u6216\u7ea2\u9ed1\u6811\uff0c\u4ece\u800c\u5c06\u65f6\u95f4\u6548\u7387\u4ece \\(O(n)\\) \u4f18\u5316\u81f3 \\(O(\\log n)\\) \uff1b\u8fd8\u53ef\u4ee5\u628a\u94fe\u8868\u8f6c\u6362\u4e3a\u54c8\u5e0c\u8868\uff0c\u4ece\u800c\u5c06\u65f6\u95f4\u590d\u6742\u5ea6\u964d\u81f3 \\(O(1)\\) \u3002
"},{"location":"chapter_graph/graph/#913","title":"9.1.3 \u00a0 \u56fe\u5e38\u89c1\u5e94\u7528","text":"\u5982\u8868 9-1 \u6240\u793a\uff0c\u8bb8\u591a\u73b0\u5b9e\u7cfb\u7edf\u53ef\u4ee5\u7528\u56fe\u6765\u5efa\u6a21\uff0c\u76f8\u5e94\u7684\u95ee\u9898\u4e5f\u53ef\u4ee5\u7ea6\u5316\u4e3a\u56fe\u8ba1\u7b97\u95ee\u9898\u3002
\u8868 9-1 \u00a0 \u73b0\u5b9e\u751f\u6d3b\u4e2d\u5e38\u89c1\u7684\u56fe
\u9876\u70b9 \u8fb9 \u56fe\u8ba1\u7b97\u95ee\u9898 \u793e\u4ea4\u7f51\u7edc \u7528\u6237 \u597d\u53cb\u5173\u7cfb \u6f5c\u5728\u597d\u53cb\u63a8\u8350 \u5730\u94c1\u7ebf\u8def \u7ad9\u70b9 \u7ad9\u70b9\u95f4\u7684\u8fde\u901a\u6027 \u6700\u77ed\u8def\u7ebf\u63a8\u8350 \u592a\u9633\u7cfb \u661f\u4f53 \u661f\u4f53\u95f4\u7684\u4e07\u6709\u5f15\u529b\u4f5c\u7528 \u884c\u661f\u8f68\u9053\u8ba1\u7b97"},{"location":"chapter_graph/graph_operations/","title":"9.2 \u00a0 \u56fe\u57fa\u7840\u64cd\u4f5c","text":"\u56fe\u7684\u57fa\u7840\u64cd\u4f5c\u53ef\u5206\u4e3a\u5bf9\u201c\u8fb9\u201d\u7684\u64cd\u4f5c\u548c\u5bf9\u201c\u9876\u70b9\u201d\u7684\u64cd\u4f5c\u3002\u5728\u201c\u90bb\u63a5\u77e9\u9635\u201d\u548c\u201c\u90bb\u63a5\u8868\u201d\u4e24\u79cd\u8868\u793a\u65b9\u6cd5\u4e0b\uff0c\u5b9e\u73b0\u65b9\u5f0f\u6709\u6240\u4e0d\u540c\u3002
"},{"location":"chapter_graph/graph_operations/#921","title":"9.2.1 \u00a0 \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u7684\u5b9e\u73b0","text":"\u7ed9\u5b9a\u4e00\u4e2a\u9876\u70b9\u6570\u91cf\u4e3a \\(n\\) \u7684\u65e0\u5411\u56fe\uff0c\u5219\u5404\u79cd\u64cd\u4f5c\u7684\u5b9e\u73b0\u65b9\u5f0f\u5982\u56fe 9-7 \u6240\u793a\u3002
vertices
\uff0c\u4f7f\u7528 \\(O(n)\\) \u65f6\u95f4\uff1b\u521d\u59cb\u5316 \\(n \\times n\\) \u5927\u5c0f\u7684\u90bb\u63a5\u77e9\u9635 adjMat
\uff0c\u4f7f\u7528 \\(O(n^2)\\) \u65f6\u95f4\u3002\u56fe 9-7 \u00a0 \u90bb\u63a5\u77e9\u9635\u7684\u521d\u59cb\u5316\u3001\u589e\u5220\u8fb9\u3001\u589e\u5220\u9876\u70b9
\u4ee5\u4e0b\u662f\u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u8868\u793a\u56fe\u7684\u5b9e\u73b0\u4ee3\u7801\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig graph_adjacency_matrix.pyclass GraphAdjMat:\n \"\"\"\u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b\"\"\"\n\n def __init__(self, vertices: list[int], edges: list[list[int]]):\n \"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n # \u9876\u70b9\u5217\u8868\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\n self.vertices: list[int] = []\n # \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n self.adj_mat: list[list[int]] = []\n # \u6dfb\u52a0\u9876\u70b9\n for val in vertices:\n self.add_vertex(val)\n # \u6dfb\u52a0\u8fb9\n # \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n for e in edges:\n self.add_edge(e[0], e[1])\n\n def size(self) -> int:\n \"\"\"\u83b7\u53d6\u9876\u70b9\u6570\u91cf\"\"\"\n return len(self.vertices)\n\n def add_vertex(self, val: int):\n \"\"\"\u6dfb\u52a0\u9876\u70b9\"\"\"\n n = self.size()\n # \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\n self.vertices.append(val)\n # \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\n new_row = [0] * n\n self.adj_mat.append(new_row)\n # \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\n for row in self.adj_mat:\n row.append(0)\n\n def remove_vertex(self, index: int):\n \"\"\"\u5220\u9664\u9876\u70b9\"\"\"\n if index >= self.size():\n raise IndexError()\n # \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\n self.vertices.pop(index)\n # \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\n self.adj_mat.pop(index)\n # \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\n for row in self.adj_mat:\n row.pop(index)\n\n def add_edge(self, i: int, j: int):\n \"\"\"\u6dfb\u52a0\u8fb9\"\"\"\n # \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n # \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n if i < 0 or j < 0 or i >= self.size() or j >= self.size() or i == j:\n raise IndexError()\n # \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u5173\u4e8e\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\n self.adj_mat[i][j] = 1\n self.adj_mat[j][i] = 1\n\n def remove_edge(self, i: int, j: int):\n \"\"\"\u5220\u9664\u8fb9\"\"\"\n # \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n # \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n if i < 0 or j < 0 or i >= self.size() or j >= self.size() or i == j:\n raise IndexError()\n self.adj_mat[i][j] = 0\n self.adj_mat[j][i] = 0\n\n def print(self):\n \"\"\"\u6253\u5370\u90bb\u63a5\u77e9\u9635\"\"\"\n print(\"\u9876\u70b9\u5217\u8868 =\", self.vertices)\n print(\"\u90bb\u63a5\u77e9\u9635 =\")\n print_matrix(self.adj_mat)\n
graph_adjacency_matrix.cpp/* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjMat {\n vector<int> vertices; // \u9876\u70b9\u5217\u8868\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\n vector<vector<int>> adjMat; // \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n\n public:\n /* \u6784\u9020\u65b9\u6cd5 */\n GraphAdjMat(const vector<int> &vertices, const vector<vector<int>> &edges) {\n // \u6dfb\u52a0\u9876\u70b9\n for (int val : vertices) {\n addVertex(val);\n }\n // \u6dfb\u52a0\u8fb9\n // \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n for (const vector<int> &edge : edges) {\n addEdge(edge[0], edge[1]);\n }\n }\n\n /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n int size() const {\n return vertices.size();\n }\n\n /* \u6dfb\u52a0\u9876\u70b9 */\n void addVertex(int val) {\n int n = size();\n // \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\n vertices.push_back(val);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\n adjMat.emplace_back(vector<int>(n, 0));\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\n for (vector<int> &row : adjMat) {\n row.push_back(0);\n }\n }\n\n /* \u5220\u9664\u9876\u70b9 */\n void removeVertex(int index) {\n if (index >= size()) {\n throw out_of_range(\"\u9876\u70b9\u4e0d\u5b58\u5728\");\n }\n // \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\n vertices.erase(vertices.begin() + index);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\n adjMat.erase(adjMat.begin() + index);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\n for (vector<int> &row : adjMat) {\n row.erase(row.begin() + index);\n }\n }\n\n /* \u6dfb\u52a0\u8fb9 */\n // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n void addEdge(int i, int j) {\n // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n if (i < 0 || j < 0 || i >= size() || j >= size() || i == j) {\n throw out_of_range(\"\u9876\u70b9\u4e0d\u5b58\u5728\");\n }\n // \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u5173\u4e8e\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\n adjMat[i][j] = 1;\n adjMat[j][i] = 1;\n }\n\n /* \u5220\u9664\u8fb9 */\n // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n void removeEdge(int i, int j) {\n // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n if (i < 0 || j < 0 || i >= size() || j >= size() || i == j) {\n throw out_of_range(\"\u9876\u70b9\u4e0d\u5b58\u5728\");\n }\n adjMat[i][j] = 0;\n adjMat[j][i] = 0;\n }\n\n /* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\n void print() {\n cout << \"\u9876\u70b9\u5217\u8868 = \";\n printVector(vertices);\n cout << \"\u90bb\u63a5\u77e9\u9635 =\" << endl;\n printVectorMatrix(adjMat);\n }\n};\n
graph_adjacency_matrix.java/* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjMat {\n List<Integer> vertices; // \u9876\u70b9\u5217\u8868\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\n List<List<Integer>> adjMat; // \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n\n /* \u6784\u9020\u65b9\u6cd5 */\n public GraphAdjMat(int[] vertices, int[][] edges) {\n this.vertices = new ArrayList<>();\n this.adjMat = new ArrayList<>();\n // \u6dfb\u52a0\u9876\u70b9\n for (int val : vertices) {\n addVertex(val);\n }\n // \u6dfb\u52a0\u8fb9\n // \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n for (int[] e : edges) {\n addEdge(e[0], e[1]);\n }\n }\n\n /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n public int size() {\n return vertices.size();\n }\n\n /* \u6dfb\u52a0\u9876\u70b9 */\n public void addVertex(int val) {\n int n = size();\n // \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\n vertices.add(val);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\n List<Integer> newRow = new ArrayList<>(n);\n for (int j = 0; j < n; j++) {\n newRow.add(0);\n }\n adjMat.add(newRow);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\n for (List<Integer> row : adjMat) {\n row.add(0);\n }\n }\n\n /* \u5220\u9664\u9876\u70b9 */\n public void removeVertex(int index) {\n if (index >= size())\n throw new IndexOutOfBoundsException();\n // \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\n vertices.remove(index);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\n adjMat.remove(index);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\n for (List<Integer> row : adjMat) {\n row.remove(index);\n }\n }\n\n /* \u6dfb\u52a0\u8fb9 */\n // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n public void addEdge(int i, int j) {\n // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n if (i < 0 || j < 0 || i >= size() || j >= size() || i == j)\n throw new IndexOutOfBoundsException();\n // \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u5173\u4e8e\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\n adjMat.get(i).set(j, 1);\n adjMat.get(j).set(i, 1);\n }\n\n /* \u5220\u9664\u8fb9 */\n // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n public void removeEdge(int i, int j) {\n // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n if (i < 0 || j < 0 || i >= size() || j >= size() || i == j)\n throw new IndexOutOfBoundsException();\n adjMat.get(i).set(j, 0);\n adjMat.get(j).set(i, 0);\n }\n\n /* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\n public void print() {\n System.out.print(\"\u9876\u70b9\u5217\u8868 = \");\n System.out.println(vertices);\n System.out.println(\"\u90bb\u63a5\u77e9\u9635 =\");\n PrintUtil.printMatrix(adjMat);\n }\n}\n
graph_adjacency_matrix.cs/* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjMat {\n List<int> vertices; // \u9876\u70b9\u5217\u8868\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\n List<List<int>> adjMat; // \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n\n /* \u6784\u9020\u51fd\u6570 */\n public GraphAdjMat(int[] vertices, int[][] edges) {\n this.vertices = [];\n this.adjMat = [];\n // \u6dfb\u52a0\u9876\u70b9\n foreach (int val in vertices) {\n AddVertex(val);\n }\n // \u6dfb\u52a0\u8fb9\n // \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n foreach (int[] e in edges) {\n AddEdge(e[0], e[1]);\n }\n }\n\n /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n int Size() {\n return vertices.Count;\n }\n\n /* \u6dfb\u52a0\u9876\u70b9 */\n public void AddVertex(int val) {\n int n = Size();\n // \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\n vertices.Add(val);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\n List<int> newRow = new(n);\n for (int j = 0; j < n; j++) {\n newRow.Add(0);\n }\n adjMat.Add(newRow);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\n foreach (List<int> row in adjMat) {\n row.Add(0);\n }\n }\n\n /* \u5220\u9664\u9876\u70b9 */\n public void RemoveVertex(int index) {\n if (index >= Size())\n throw new IndexOutOfRangeException();\n // \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\n vertices.RemoveAt(index);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\n adjMat.RemoveAt(index);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\n foreach (List<int> row in adjMat) {\n row.RemoveAt(index);\n }\n }\n\n /* \u6dfb\u52a0\u8fb9 */\n // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n public void AddEdge(int i, int j) {\n // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n if (i < 0 || j < 0 || i >= Size() || j >= Size() || i == j)\n throw new IndexOutOfRangeException();\n // \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u5173\u4e8e\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\n adjMat[i][j] = 1;\n adjMat[j][i] = 1;\n }\n\n /* \u5220\u9664\u8fb9 */\n // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n public void RemoveEdge(int i, int j) {\n // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n if (i < 0 || j < 0 || i >= Size() || j >= Size() || i == j)\n throw new IndexOutOfRangeException();\n adjMat[i][j] = 0;\n adjMat[j][i] = 0;\n }\n\n /* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\n public void Print() {\n Console.Write(\"\u9876\u70b9\u5217\u8868 = \");\n PrintUtil.PrintList(vertices);\n Console.WriteLine(\"\u90bb\u63a5\u77e9\u9635 =\");\n PrintUtil.PrintMatrix(adjMat);\n }\n}\n
graph_adjacency_matrix.go/* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\ntype graphAdjMat struct {\n // \u9876\u70b9\u5217\u8868\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\n vertices []int\n // \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n adjMat [][]int\n}\n\n/* \u6784\u9020\u51fd\u6570 */\nfunc newGraphAdjMat(vertices []int, edges [][]int) *graphAdjMat {\n // \u6dfb\u52a0\u9876\u70b9\n n := len(vertices)\n adjMat := make([][]int, n)\n for i := range adjMat {\n adjMat[i] = make([]int, n)\n }\n // \u521d\u59cb\u5316\u56fe\n g := &graphAdjMat{\n vertices: vertices,\n adjMat: adjMat,\n }\n // \u6dfb\u52a0\u8fb9\n // \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n for i := range edges {\n g.addEdge(edges[i][0], edges[i][1])\n }\n return g\n}\n\n/* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\nfunc (g *graphAdjMat) size() int {\n return len(g.vertices)\n}\n\n/* \u6dfb\u52a0\u9876\u70b9 */\nfunc (g *graphAdjMat) addVertex(val int) {\n n := g.size()\n // \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\n g.vertices = append(g.vertices, val)\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\n newRow := make([]int, n)\n g.adjMat = append(g.adjMat, newRow)\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\n for i := range g.adjMat {\n g.adjMat[i] = append(g.adjMat[i], 0)\n }\n}\n\n/* \u5220\u9664\u9876\u70b9 */\nfunc (g *graphAdjMat) removeVertex(index int) {\n if index >= g.size() {\n return\n }\n // \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\n g.vertices = append(g.vertices[:index], g.vertices[index+1:]...)\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\n g.adjMat = append(g.adjMat[:index], g.adjMat[index+1:]...)\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\n for i := range g.adjMat {\n g.adjMat[i] = append(g.adjMat[i][:index], g.adjMat[i][index+1:]...)\n }\n}\n\n/* \u6dfb\u52a0\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nfunc (g *graphAdjMat) addEdge(i, j int) {\n // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n if i < 0 || j < 0 || i >= g.size() || j >= g.size() || i == j {\n fmt.Errorf(\"%s\", \"Index Out Of Bounds Exception\")\n }\n // \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u5173\u4e8e\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\n g.adjMat[i][j] = 1\n g.adjMat[j][i] = 1\n}\n\n/* \u5220\u9664\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nfunc (g *graphAdjMat) removeEdge(i, j int) {\n // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n if i < 0 || j < 0 || i >= g.size() || j >= g.size() || i == j {\n fmt.Errorf(\"%s\", \"Index Out Of Bounds Exception\")\n }\n g.adjMat[i][j] = 0\n g.adjMat[j][i] = 0\n}\n\n/* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\nfunc (g *graphAdjMat) print() {\n fmt.Printf(\"\\t\u9876\u70b9\u5217\u8868 = %v\\n\", g.vertices)\n fmt.Printf(\"\\t\u90bb\u63a5\u77e9\u9635 = \\n\")\n for i := range g.adjMat {\n fmt.Printf(\"\\t\\t\\t%v\\n\", g.adjMat[i])\n }\n}\n
graph_adjacency_matrix.swift/* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjMat {\n private var vertices: [Int] // \u9876\u70b9\u5217\u8868\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\n private var adjMat: [[Int]] // \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n\n /* \u6784\u9020\u65b9\u6cd5 */\n init(vertices: [Int], edges: [[Int]]) {\n self.vertices = []\n adjMat = []\n // \u6dfb\u52a0\u9876\u70b9\n for val in vertices {\n addVertex(val: val)\n }\n // \u6dfb\u52a0\u8fb9\n // \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n for e in edges {\n addEdge(i: e[0], j: e[1])\n }\n }\n\n /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n func size() -> Int {\n vertices.count\n }\n\n /* \u6dfb\u52a0\u9876\u70b9 */\n func addVertex(val: Int) {\n let n = size()\n // \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\n vertices.append(val)\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\n let newRow = Array(repeating: 0, count: n)\n adjMat.append(newRow)\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\n for i in adjMat.indices {\n adjMat[i].append(0)\n }\n }\n\n /* \u5220\u9664\u9876\u70b9 */\n func removeVertex(index: Int) {\n if index >= size() {\n fatalError(\"\u8d8a\u754c\")\n }\n // \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\n vertices.remove(at: index)\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\n adjMat.remove(at: index)\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\n for i in adjMat.indices {\n adjMat[i].remove(at: index)\n }\n }\n\n /* \u6dfb\u52a0\u8fb9 */\n // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n func addEdge(i: Int, j: Int) {\n // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n if i < 0 || j < 0 || i >= size() || j >= size() || i == j {\n fatalError(\"\u8d8a\u754c\")\n }\n // \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u5173\u4e8e\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\n adjMat[i][j] = 1\n adjMat[j][i] = 1\n }\n\n /* \u5220\u9664\u8fb9 */\n // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n func removeEdge(i: Int, j: Int) {\n // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n if i < 0 || j < 0 || i >= size() || j >= size() || i == j {\n fatalError(\"\u8d8a\u754c\")\n }\n adjMat[i][j] = 0\n adjMat[j][i] = 0\n }\n\n /* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\n func print() {\n Swift.print(\"\u9876\u70b9\u5217\u8868 = \", terminator: \"\")\n Swift.print(vertices)\n Swift.print(\"\u90bb\u63a5\u77e9\u9635 =\")\n PrintUtil.printMatrix(matrix: adjMat)\n }\n}\n
graph_adjacency_matrix.js/* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjMat {\n vertices; // \u9876\u70b9\u5217\u8868\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\n adjMat; // \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n\n /* \u6784\u9020\u51fd\u6570 */\n constructor(vertices, edges) {\n this.vertices = [];\n this.adjMat = [];\n // \u6dfb\u52a0\u9876\u70b9\n for (const val of vertices) {\n this.addVertex(val);\n }\n // \u6dfb\u52a0\u8fb9\n // \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n for (const e of edges) {\n this.addEdge(e[0], e[1]);\n }\n }\n\n /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n size() {\n return this.vertices.length;\n }\n\n /* \u6dfb\u52a0\u9876\u70b9 */\n addVertex(val) {\n const n = this.size();\n // \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\n this.vertices.push(val);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\n const newRow = [];\n for (let j = 0; j < n; j++) {\n newRow.push(0);\n }\n this.adjMat.push(newRow);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\n for (const row of this.adjMat) {\n row.push(0);\n }\n }\n\n /* \u5220\u9664\u9876\u70b9 */\n removeVertex(index) {\n if (index >= this.size()) {\n throw new RangeError('Index Out Of Bounds Exception');\n }\n // \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\n this.vertices.splice(index, 1);\n\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\n this.adjMat.splice(index, 1);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\n for (const row of this.adjMat) {\n row.splice(index, 1);\n }\n }\n\n /* \u6dfb\u52a0\u8fb9 */\n // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n addEdge(i, j) {\n // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n if (i < 0 || j < 0 || i >= this.size() || j >= this.size() || i === j) {\n throw new RangeError('Index Out Of Bounds Exception');\n }\n // \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u5173\u4e8e\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) === (j, i)\n this.adjMat[i][j] = 1;\n this.adjMat[j][i] = 1;\n }\n\n /* \u5220\u9664\u8fb9 */\n // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n removeEdge(i, j) {\n // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n if (i < 0 || j < 0 || i >= this.size() || j >= this.size() || i === j) {\n throw new RangeError('Index Out Of Bounds Exception');\n }\n this.adjMat[i][j] = 0;\n this.adjMat[j][i] = 0;\n }\n\n /* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\n print() {\n console.log('\u9876\u70b9\u5217\u8868 = ', this.vertices);\n console.log('\u90bb\u63a5\u77e9\u9635 =', this.adjMat);\n }\n}\n
graph_adjacency_matrix.ts/* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjMat {\n vertices: number[]; // \u9876\u70b9\u5217\u8868\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\n adjMat: number[][]; // \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n\n /* \u6784\u9020\u51fd\u6570 */\n constructor(vertices: number[], edges: number[][]) {\n this.vertices = [];\n this.adjMat = [];\n // \u6dfb\u52a0\u9876\u70b9\n for (const val of vertices) {\n this.addVertex(val);\n }\n // \u6dfb\u52a0\u8fb9\n // \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n for (const e of edges) {\n this.addEdge(e[0], e[1]);\n }\n }\n\n /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n size(): number {\n return this.vertices.length;\n }\n\n /* \u6dfb\u52a0\u9876\u70b9 */\n addVertex(val: number): void {\n const n: number = this.size();\n // \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\n this.vertices.push(val);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\n const newRow: number[] = [];\n for (let j: number = 0; j < n; j++) {\n newRow.push(0);\n }\n this.adjMat.push(newRow);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\n for (const row of this.adjMat) {\n row.push(0);\n }\n }\n\n /* \u5220\u9664\u9876\u70b9 */\n removeVertex(index: number): void {\n if (index >= this.size()) {\n throw new RangeError('Index Out Of Bounds Exception');\n }\n // \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\n this.vertices.splice(index, 1);\n\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\n this.adjMat.splice(index, 1);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\n for (const row of this.adjMat) {\n row.splice(index, 1);\n }\n }\n\n /* \u6dfb\u52a0\u8fb9 */\n // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n addEdge(i: number, j: number): void {\n // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n if (i < 0 || j < 0 || i >= this.size() || j >= this.size() || i === j) {\n throw new RangeError('Index Out Of Bounds Exception');\n }\n // \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u5173\u4e8e\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) === (j, i)\n this.adjMat[i][j] = 1;\n this.adjMat[j][i] = 1;\n }\n\n /* \u5220\u9664\u8fb9 */\n // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n removeEdge(i: number, j: number): void {\n // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n if (i < 0 || j < 0 || i >= this.size() || j >= this.size() || i === j) {\n throw new RangeError('Index Out Of Bounds Exception');\n }\n this.adjMat[i][j] = 0;\n this.adjMat[j][i] = 0;\n }\n\n /* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\n print(): void {\n console.log('\u9876\u70b9\u5217\u8868 = ', this.vertices);\n console.log('\u90bb\u63a5\u77e9\u9635 =', this.adjMat);\n }\n}\n
graph_adjacency_matrix.dart/* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjMat {\n List<int> vertices = []; // \u9876\u70b9\u5143\u7d20\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\n List<List<int>> adjMat = []; //\u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n\n /* \u6784\u9020\u65b9\u6cd5 */\n GraphAdjMat(List<int> vertices, List<List<int>> edges) {\n this.vertices = [];\n this.adjMat = [];\n // \u6dfb\u52a0\u9876\u70b9\n for (int val in vertices) {\n addVertex(val);\n }\n // \u6dfb\u52a0\u8fb9\n // \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n for (List<int> e in edges) {\n addEdge(e[0], e[1]);\n }\n }\n\n /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n int size() {\n return vertices.length;\n }\n\n /* \u6dfb\u52a0\u9876\u70b9 */\n void addVertex(int val) {\n int n = size();\n // \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\n vertices.add(val);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\n List<int> newRow = List.filled(n, 0, growable: true);\n adjMat.add(newRow);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\n for (List<int> row in adjMat) {\n row.add(0);\n }\n }\n\n /* \u5220\u9664\u9876\u70b9 */\n void removeVertex(int index) {\n if (index >= size()) {\n throw IndexError;\n }\n // \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\n vertices.removeAt(index);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\n adjMat.removeAt(index);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\n for (List<int> row in adjMat) {\n row.removeAt(index);\n }\n }\n\n /* \u6dfb\u52a0\u8fb9 */\n // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n void addEdge(int i, int j) {\n // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n if (i < 0 || j < 0 || i >= size() || j >= size() || i == j) {\n throw IndexError;\n }\n // \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u5173\u4e8e\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\n adjMat[i][j] = 1;\n adjMat[j][i] = 1;\n }\n\n /* \u5220\u9664\u8fb9 */\n // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n void removeEdge(int i, int j) {\n // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n if (i < 0 || j < 0 || i >= size() || j >= size() || i == j) {\n throw IndexError;\n }\n adjMat[i][j] = 0;\n adjMat[j][i] = 0;\n }\n\n /* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\n void printAdjMat() {\n print(\"\u9876\u70b9\u5217\u8868 = $vertices\");\n print(\"\u90bb\u63a5\u77e9\u9635 = \");\n printMatrix(adjMat);\n }\n}\n
graph_adjacency_matrix.rs/* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b\u578b */\npub struct GraphAdjMat {\n // \u9876\u70b9\u5217\u8868\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\n pub vertices: Vec<i32>,\n // \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n pub adj_mat: Vec<Vec<i32>>,\n}\n\nimpl GraphAdjMat {\n /* \u6784\u9020\u65b9\u6cd5 */\n pub fn new(vertices: Vec<i32>, edges: Vec<[usize; 2]>) -> Self {\n let mut graph = GraphAdjMat {\n vertices: vec![],\n adj_mat: vec![],\n };\n // \u6dfb\u52a0\u9876\u70b9\n for val in vertices {\n graph.add_vertex(val);\n }\n // \u6dfb\u52a0\u8fb9\n // \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n for edge in edges {\n graph.add_edge(edge[0], edge[1])\n }\n\n graph\n }\n\n /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n pub fn size(&self) -> usize {\n self.vertices.len()\n }\n\n /* \u6dfb\u52a0\u9876\u70b9 */\n pub fn add_vertex(&mut self, val: i32) {\n let n = self.size();\n // \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\n self.vertices.push(val);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\n self.adj_mat.push(vec![0; n]);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\n for row in &mut self.adj_mat {\n row.push(0);\n }\n }\n\n /* \u5220\u9664\u9876\u70b9 */\n pub fn remove_vertex(&mut self, index: usize) {\n if index >= self.size() {\n panic!(\"index error\")\n }\n // \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\n self.vertices.remove(index);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\n self.adj_mat.remove(index);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\n for row in &mut self.adj_mat {\n row.remove(index);\n }\n }\n\n /* \u6dfb\u52a0\u8fb9 */\n pub fn add_edge(&mut self, i: usize, j: usize) {\n // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n if i >= self.size() || j >= self.size() || i == j {\n panic!(\"index error\")\n }\n // \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u5173\u4e8e\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\n self.adj_mat[i][j] = 1;\n self.adj_mat[j][i] = 1;\n }\n\n /* \u5220\u9664\u8fb9 */\n // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n pub fn remove_edge(&mut self, i: usize, j: usize) {\n // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n if i >= self.size() || j >= self.size() || i == j {\n panic!(\"index error\")\n }\n self.adj_mat[i][j] = 0;\n self.adj_mat[j][i] = 0;\n }\n\n /* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\n pub fn print(&self) {\n println!(\"\u9876\u70b9\u5217\u8868 = {:?}\", self.vertices);\n println!(\"\u90bb\u63a5\u77e9\u9635 =\");\n println!(\"[\");\n for row in &self.adj_mat {\n println!(\" {:?},\", row);\n }\n println!(\"]\")\n }\n}\n
graph_adjacency_matrix.c/* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7ed3\u6784\u4f53 */\ntypedef struct {\n int vertices[MAX_SIZE];\n int adjMat[MAX_SIZE][MAX_SIZE];\n int size;\n} GraphAdjMat;\n\n/* \u6784\u9020\u51fd\u6570 */\nGraphAdjMat *newGraphAdjMat() {\n GraphAdjMat *graph = (GraphAdjMat *)malloc(sizeof(GraphAdjMat));\n graph->size = 0;\n for (int i = 0; i < MAX_SIZE; i++) {\n for (int j = 0; j < MAX_SIZE; j++) {\n graph->adjMat[i][j] = 0;\n }\n }\n return graph;\n}\n\n/* \u6790\u6784\u51fd\u6570 */\nvoid delGraphAdjMat(GraphAdjMat *graph) {\n free(graph);\n}\n\n/* \u6dfb\u52a0\u9876\u70b9 */\nvoid addVertex(GraphAdjMat *graph, int val) {\n if (graph->size == MAX_SIZE) {\n fprintf(stderr, \"\u56fe\u7684\u9876\u70b9\u6570\u91cf\u5df2\u8fbe\u6700\u5927\u503c\\n\");\n return;\n }\n // \u6dfb\u52a0\u7b2c n \u4e2a\u9876\u70b9\uff0c\u5e76\u5c06\u7b2c n \u884c\u548c\u5217\u7f6e\u96f6\n int n = graph->size;\n graph->vertices[n] = val;\n for (int i = 0; i <= n; i++) {\n graph->adjMat[n][i] = graph->adjMat[i][n] = 0;\n }\n graph->size++;\n}\n\n/* \u5220\u9664\u9876\u70b9 */\nvoid removeVertex(GraphAdjMat *graph, int index) {\n if (index < 0 || index >= graph->size) {\n fprintf(stderr, \"\u9876\u70b9\u7d22\u5f15\u8d8a\u754c\\n\");\n return;\n }\n // \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\n for (int i = index; i < graph->size - 1; i++) {\n graph->vertices[i] = graph->vertices[i + 1];\n }\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\n for (int i = index; i < graph->size - 1; i++) {\n for (int j = 0; j < graph->size; j++) {\n graph->adjMat[i][j] = graph->adjMat[i + 1][j];\n }\n }\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\n for (int i = 0; i < graph->size; i++) {\n for (int j = index; j < graph->size - 1; j++) {\n graph->adjMat[i][j] = graph->adjMat[i][j + 1];\n }\n }\n graph->size--;\n}\n\n/* \u6dfb\u52a0\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nvoid addEdge(GraphAdjMat *graph, int i, int j) {\n if (i < 0 || j < 0 || i >= graph->size || j >= graph->size || i == j) {\n fprintf(stderr, \"\u8fb9\u7d22\u5f15\u8d8a\u754c\u6216\u76f8\u7b49\\n\");\n return;\n }\n graph->adjMat[i][j] = 1;\n graph->adjMat[j][i] = 1;\n}\n\n/* \u5220\u9664\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nvoid removeEdge(GraphAdjMat *graph, int i, int j) {\n if (i < 0 || j < 0 || i >= graph->size || j >= graph->size || i == j) {\n fprintf(stderr, \"\u8fb9\u7d22\u5f15\u8d8a\u754c\u6216\u76f8\u7b49\\n\");\n return;\n }\n graph->adjMat[i][j] = 0;\n graph->adjMat[j][i] = 0;\n}\n\n/* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\nvoid printGraphAdjMat(GraphAdjMat *graph) {\n printf(\"\u9876\u70b9\u5217\u8868 = \");\n printArray(graph->vertices, graph->size);\n printf(\"\u90bb\u63a5\u77e9\u9635 =\\n\");\n for (int i = 0; i < graph->size; i++) {\n printArray(graph->adjMat[i], graph->size);\n }\n}\n
graph_adjacency_matrix.zig[class]{GraphAdjMat}-[func]{}\n
"},{"location":"chapter_graph/graph_operations/#922","title":"9.2.2 \u00a0 \u57fa\u4e8e\u90bb\u63a5\u8868\u7684\u5b9e\u73b0","text":"\u8bbe\u65e0\u5411\u56fe\u7684\u9876\u70b9\u603b\u6570\u4e3a \\(n\\)\u3001\u8fb9\u603b\u6570\u4e3a \\(m\\) \uff0c\u5219\u53ef\u6839\u636e\u56fe 9-8 \u6240\u793a\u7684\u65b9\u6cd5\u5b9e\u73b0\u5404\u79cd\u64cd\u4f5c\u3002
\u56fe 9-8 \u00a0 \u90bb\u63a5\u8868\u7684\u521d\u59cb\u5316\u3001\u589e\u5220\u8fb9\u3001\u589e\u5220\u9876\u70b9
\u4ee5\u4e0b\u662f\u90bb\u63a5\u8868\u7684\u4ee3\u7801\u5b9e\u73b0\u3002\u5bf9\u6bd4\u56fe 9-8 \uff0c\u5b9e\u9645\u4ee3\u7801\u6709\u4ee5\u4e0b\u4e0d\u540c\u3002
key
\u4e3a\u9876\u70b9\u5b9e\u4f8b\uff0cvalue
\u4e3a\u8be5\u9876\u70b9\u7684\u90bb\u63a5\u9876\u70b9\u5217\u8868\uff08\u94fe\u8868\uff09\u3002\u53e6\u5916\uff0c\u6211\u4eec\u5728\u90bb\u63a5\u8868\u4e2d\u4f7f\u7528 Vertex
\u7c7b\u6765\u8868\u793a\u9876\u70b9\uff0c\u8fd9\u6837\u505a\u7684\u539f\u56e0\u662f\uff1a\u5982\u679c\u4e0e\u90bb\u63a5\u77e9\u9635\u4e00\u6837\uff0c\u7528\u5217\u8868\u7d22\u5f15\u6765\u533a\u5206\u4e0d\u540c\u9876\u70b9\uff0c\u90a3\u4e48\u5047\u8bbe\u8981\u5220\u9664\u7d22\u5f15\u4e3a \\(i\\) \u7684\u9876\u70b9\uff0c\u5219\u9700\u904d\u5386\u6574\u4e2a\u90bb\u63a5\u8868\uff0c\u5c06\u6240\u6709\u5927\u4e8e \\(i\\) \u7684\u7d22\u5f15\u5168\u90e8\u51cf \\(1\\) \uff0c\u6548\u7387\u5f88\u4f4e\u3002\u800c\u5982\u679c\u6bcf\u4e2a\u9876\u70b9\u90fd\u662f\u552f\u4e00\u7684 Vertex
\u5b9e\u4f8b\uff0c\u5220\u9664\u67d0\u4e00\u9876\u70b9\u4e4b\u540e\u5c31\u65e0\u987b\u6539\u52a8\u5176\u4ed6\u9876\u70b9\u4e86\u3002
class GraphAdjList:\n \"\"\"\u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b\"\"\"\n\n def __init__(self, edges: list[list[Vertex]]):\n \"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n # \u90bb\u63a5\u8868\uff0ckey\uff1a\u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n self.adj_list = dict[Vertex, list[Vertex]]()\n # \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\n for edge in edges:\n self.add_vertex(edge[0])\n self.add_vertex(edge[1])\n self.add_edge(edge[0], edge[1])\n\n def size(self) -> int:\n \"\"\"\u83b7\u53d6\u9876\u70b9\u6570\u91cf\"\"\"\n return len(self.adj_list)\n\n def add_edge(self, vet1: Vertex, vet2: Vertex):\n \"\"\"\u6dfb\u52a0\u8fb9\"\"\"\n if vet1 not in self.adj_list or vet2 not in self.adj_list or vet1 == vet2:\n raise ValueError()\n # \u6dfb\u52a0\u8fb9 vet1 - vet2\n self.adj_list[vet1].append(vet2)\n self.adj_list[vet2].append(vet1)\n\n def remove_edge(self, vet1: Vertex, vet2: Vertex):\n \"\"\"\u5220\u9664\u8fb9\"\"\"\n if vet1 not in self.adj_list or vet2 not in self.adj_list or vet1 == vet2:\n raise ValueError()\n # \u5220\u9664\u8fb9 vet1 - vet2\n self.adj_list[vet1].remove(vet2)\n self.adj_list[vet2].remove(vet1)\n\n def add_vertex(self, vet: Vertex):\n \"\"\"\u6dfb\u52a0\u9876\u70b9\"\"\"\n if vet in self.adj_list:\n return\n # \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\n self.adj_list[vet] = []\n\n def remove_vertex(self, vet: Vertex):\n \"\"\"\u5220\u9664\u9876\u70b9\"\"\"\n if vet not in self.adj_list:\n raise ValueError()\n # \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\n self.adj_list.pop(vet)\n # \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\n for vertex in self.adj_list:\n if vet in self.adj_list[vertex]:\n self.adj_list[vertex].remove(vet)\n\n def print(self):\n \"\"\"\u6253\u5370\u90bb\u63a5\u8868\"\"\"\n print(\"\u90bb\u63a5\u8868 =\")\n for vertex in self.adj_list:\n tmp = [v.val for v in self.adj_list[vertex]]\n print(f\"{vertex.val}: {tmp},\")\n
graph_adjacency_list.cpp/* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjList {\n public:\n // \u90bb\u63a5\u8868\uff0ckey\uff1a\u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n unordered_map<Vertex *, vector<Vertex *>> adjList;\n\n /* \u5728 vector \u4e2d\u5220\u9664\u6307\u5b9a\u8282\u70b9 */\n void remove(vector<Vertex *> &vec, Vertex *vet) {\n for (int i = 0; i < vec.size(); i++) {\n if (vec[i] == vet) {\n vec.erase(vec.begin() + i);\n break;\n }\n }\n }\n\n /* \u6784\u9020\u65b9\u6cd5 */\n GraphAdjList(const vector<vector<Vertex *>> &edges) {\n // \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\n for (const vector<Vertex *> &edge : edges) {\n addVertex(edge[0]);\n addVertex(edge[1]);\n addEdge(edge[0], edge[1]);\n }\n }\n\n /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n int size() {\n return adjList.size();\n }\n\n /* \u6dfb\u52a0\u8fb9 */\n void addEdge(Vertex *vet1, Vertex *vet2) {\n if (!adjList.count(vet1) || !adjList.count(vet2) || vet1 == vet2)\n throw invalid_argument(\"\u4e0d\u5b58\u5728\u9876\u70b9\");\n // \u6dfb\u52a0\u8fb9 vet1 - vet2\n adjList[vet1].push_back(vet2);\n adjList[vet2].push_back(vet1);\n }\n\n /* \u5220\u9664\u8fb9 */\n void removeEdge(Vertex *vet1, Vertex *vet2) {\n if (!adjList.count(vet1) || !adjList.count(vet2) || vet1 == vet2)\n throw invalid_argument(\"\u4e0d\u5b58\u5728\u9876\u70b9\");\n // \u5220\u9664\u8fb9 vet1 - vet2\n remove(adjList[vet1], vet2);\n remove(adjList[vet2], vet1);\n }\n\n /* \u6dfb\u52a0\u9876\u70b9 */\n void addVertex(Vertex *vet) {\n if (adjList.count(vet))\n return;\n // \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\n adjList[vet] = vector<Vertex *>();\n }\n\n /* \u5220\u9664\u9876\u70b9 */\n void removeVertex(Vertex *vet) {\n if (!adjList.count(vet))\n throw invalid_argument(\"\u4e0d\u5b58\u5728\u9876\u70b9\");\n // \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\n adjList.erase(vet);\n // \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\n for (auto &adj : adjList) {\n remove(adj.second, vet);\n }\n }\n\n /* \u6253\u5370\u90bb\u63a5\u8868 */\n void print() {\n cout << \"\u90bb\u63a5\u8868 =\" << endl;\n for (auto &adj : adjList) {\n const auto &key = adj.first;\n const auto &vec = adj.second;\n cout << key->val << \": \";\n printVector(vetsToVals(vec));\n }\n }\n};\n
graph_adjacency_list.java/* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjList {\n // \u90bb\u63a5\u8868\uff0ckey\uff1a\u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n Map<Vertex, List<Vertex>> adjList;\n\n /* \u6784\u9020\u65b9\u6cd5 */\n public GraphAdjList(Vertex[][] edges) {\n this.adjList = new HashMap<>();\n // \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\n for (Vertex[] edge : edges) {\n addVertex(edge[0]);\n addVertex(edge[1]);\n addEdge(edge[0], edge[1]);\n }\n }\n\n /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n public int size() {\n return adjList.size();\n }\n\n /* \u6dfb\u52a0\u8fb9 */\n public void addEdge(Vertex vet1, Vertex vet2) {\n if (!adjList.containsKey(vet1) || !adjList.containsKey(vet2) || vet1 == vet2)\n throw new IllegalArgumentException();\n // \u6dfb\u52a0\u8fb9 vet1 - vet2\n adjList.get(vet1).add(vet2);\n adjList.get(vet2).add(vet1);\n }\n\n /* \u5220\u9664\u8fb9 */\n public void removeEdge(Vertex vet1, Vertex vet2) {\n if (!adjList.containsKey(vet1) || !adjList.containsKey(vet2) || vet1 == vet2)\n throw new IllegalArgumentException();\n // \u5220\u9664\u8fb9 vet1 - vet2\n adjList.get(vet1).remove(vet2);\n adjList.get(vet2).remove(vet1);\n }\n\n /* \u6dfb\u52a0\u9876\u70b9 */\n public void addVertex(Vertex vet) {\n if (adjList.containsKey(vet))\n return;\n // \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\n adjList.put(vet, new ArrayList<>());\n }\n\n /* \u5220\u9664\u9876\u70b9 */\n public void removeVertex(Vertex vet) {\n if (!adjList.containsKey(vet))\n throw new IllegalArgumentException();\n // \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\n adjList.remove(vet);\n // \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\n for (List<Vertex> list : adjList.values()) {\n list.remove(vet);\n }\n }\n\n /* \u6253\u5370\u90bb\u63a5\u8868 */\n public void print() {\n System.out.println(\"\u90bb\u63a5\u8868 =\");\n for (Map.Entry<Vertex, List<Vertex>> pair : adjList.entrySet()) {\n List<Integer> tmp = new ArrayList<>();\n for (Vertex vertex : pair.getValue())\n tmp.add(vertex.val);\n System.out.println(pair.getKey().val + \": \" + tmp + \",\");\n }\n }\n}\n
graph_adjacency_list.cs/* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjList {\n // \u90bb\u63a5\u8868\uff0ckey\uff1a\u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n public Dictionary<Vertex, List<Vertex>> adjList;\n\n /* \u6784\u9020\u51fd\u6570 */\n public GraphAdjList(Vertex[][] edges) {\n adjList = [];\n // \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\n foreach (Vertex[] edge in edges) {\n AddVertex(edge[0]);\n AddVertex(edge[1]);\n AddEdge(edge[0], edge[1]);\n }\n }\n\n /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n int Size() {\n return adjList.Count;\n }\n\n /* \u6dfb\u52a0\u8fb9 */\n public void AddEdge(Vertex vet1, Vertex vet2) {\n if (!adjList.ContainsKey(vet1) || !adjList.ContainsKey(vet2) || vet1 == vet2)\n throw new InvalidOperationException();\n // \u6dfb\u52a0\u8fb9 vet1 - vet2\n adjList[vet1].Add(vet2);\n adjList[vet2].Add(vet1);\n }\n\n /* \u5220\u9664\u8fb9 */\n public void RemoveEdge(Vertex vet1, Vertex vet2) {\n if (!adjList.ContainsKey(vet1) || !adjList.ContainsKey(vet2) || vet1 == vet2)\n throw new InvalidOperationException();\n // \u5220\u9664\u8fb9 vet1 - vet2\n adjList[vet1].Remove(vet2);\n adjList[vet2].Remove(vet1);\n }\n\n /* \u6dfb\u52a0\u9876\u70b9 */\n public void AddVertex(Vertex vet) {\n if (adjList.ContainsKey(vet))\n return;\n // \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\n adjList.Add(vet, []);\n }\n\n /* \u5220\u9664\u9876\u70b9 */\n public void RemoveVertex(Vertex vet) {\n if (!adjList.ContainsKey(vet))\n throw new InvalidOperationException();\n // \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\n adjList.Remove(vet);\n // \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\n foreach (List<Vertex> list in adjList.Values) {\n list.Remove(vet);\n }\n }\n\n /* \u6253\u5370\u90bb\u63a5\u8868 */\n public void Print() {\n Console.WriteLine(\"\u90bb\u63a5\u8868 =\");\n foreach (KeyValuePair<Vertex, List<Vertex>> pair in adjList) {\n List<int> tmp = [];\n foreach (Vertex vertex in pair.Value)\n tmp.Add(vertex.val);\n Console.WriteLine(pair.Key.val + \": [\" + string.Join(\", \", tmp) + \"],\");\n }\n }\n}\n
graph_adjacency_list.go/* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\ntype graphAdjList struct {\n // \u90bb\u63a5\u8868\uff0ckey\uff1a\u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n adjList map[Vertex][]Vertex\n}\n\n/* \u6784\u9020\u51fd\u6570 */\nfunc newGraphAdjList(edges [][]Vertex) *graphAdjList {\n g := &graphAdjList{\n adjList: make(map[Vertex][]Vertex),\n }\n // \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\n for _, edge := range edges {\n g.addVertex(edge[0])\n g.addVertex(edge[1])\n g.addEdge(edge[0], edge[1])\n }\n return g\n}\n\n/* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\nfunc (g *graphAdjList) size() int {\n return len(g.adjList)\n}\n\n/* \u6dfb\u52a0\u8fb9 */\nfunc (g *graphAdjList) addEdge(vet1 Vertex, vet2 Vertex) {\n _, ok1 := g.adjList[vet1]\n _, ok2 := g.adjList[vet2]\n if !ok1 || !ok2 || vet1 == vet2 {\n panic(\"error\")\n }\n // \u6dfb\u52a0\u8fb9 vet1 - vet2, \u6dfb\u52a0\u533f\u540d struct{},\n g.adjList[vet1] = append(g.adjList[vet1], vet2)\n g.adjList[vet2] = append(g.adjList[vet2], vet1)\n}\n\n/* \u5220\u9664\u8fb9 */\nfunc (g *graphAdjList) removeEdge(vet1 Vertex, vet2 Vertex) {\n _, ok1 := g.adjList[vet1]\n _, ok2 := g.adjList[vet2]\n if !ok1 || !ok2 || vet1 == vet2 {\n panic(\"error\")\n }\n // \u5220\u9664\u8fb9 vet1 - vet2\n g.adjList[vet1] = DeleteSliceElms(g.adjList[vet1], vet2)\n g.adjList[vet2] = DeleteSliceElms(g.adjList[vet2], vet1)\n}\n\n/* \u6dfb\u52a0\u9876\u70b9 */\nfunc (g *graphAdjList) addVertex(vet Vertex) {\n _, ok := g.adjList[vet]\n if ok {\n return\n }\n // \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\n g.adjList[vet] = make([]Vertex, 0)\n}\n\n/* \u5220\u9664\u9876\u70b9 */\nfunc (g *graphAdjList) removeVertex(vet Vertex) {\n _, ok := g.adjList[vet]\n if !ok {\n panic(\"error\")\n }\n // \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\n delete(g.adjList, vet)\n // \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\n for v, list := range g.adjList {\n g.adjList[v] = DeleteSliceElms(list, vet)\n }\n}\n\n/* \u6253\u5370\u90bb\u63a5\u8868 */\nfunc (g *graphAdjList) print() {\n var builder strings.Builder\n fmt.Printf(\"\u90bb\u63a5\u8868 = \\n\")\n for k, v := range g.adjList {\n builder.WriteString(\"\\t\\t\" + strconv.Itoa(k.Val) + \": \")\n for _, vet := range v {\n builder.WriteString(strconv.Itoa(vet.Val) + \" \")\n }\n fmt.Println(builder.String())\n builder.Reset()\n }\n}\n
graph_adjacency_list.swift/* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjList {\n // \u90bb\u63a5\u8868\uff0ckey\uff1a\u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n public private(set) var adjList: [Vertex: [Vertex]]\n\n /* \u6784\u9020\u65b9\u6cd5 */\n public init(edges: [[Vertex]]) {\n adjList = [:]\n // \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\n for edge in edges {\n addVertex(vet: edge[0])\n addVertex(vet: edge[1])\n addEdge(vet1: edge[0], vet2: edge[1])\n }\n }\n\n /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n public func size() -> Int {\n adjList.count\n }\n\n /* \u6dfb\u52a0\u8fb9 */\n public func addEdge(vet1: Vertex, vet2: Vertex) {\n if adjList[vet1] == nil || adjList[vet2] == nil || vet1 == vet2 {\n fatalError(\"\u53c2\u6570\u9519\u8bef\")\n }\n // \u6dfb\u52a0\u8fb9 vet1 - vet2\n adjList[vet1]?.append(vet2)\n adjList[vet2]?.append(vet1)\n }\n\n /* \u5220\u9664\u8fb9 */\n public func removeEdge(vet1: Vertex, vet2: Vertex) {\n if adjList[vet1] == nil || adjList[vet2] == nil || vet1 == vet2 {\n fatalError(\"\u53c2\u6570\u9519\u8bef\")\n }\n // \u5220\u9664\u8fb9 vet1 - vet2\n adjList[vet1]?.removeAll(where: { $0 == vet2 })\n adjList[vet2]?.removeAll(where: { $0 == vet1 })\n }\n\n /* \u6dfb\u52a0\u9876\u70b9 */\n public func addVertex(vet: Vertex) {\n if adjList[vet] != nil {\n return\n }\n // \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\n adjList[vet] = []\n }\n\n /* \u5220\u9664\u9876\u70b9 */\n public func removeVertex(vet: Vertex) {\n if adjList[vet] == nil {\n fatalError(\"\u53c2\u6570\u9519\u8bef\")\n }\n // \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\n adjList.removeValue(forKey: vet)\n // \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\n for key in adjList.keys {\n adjList[key]?.removeAll(where: { $0 == vet })\n }\n }\n\n /* \u6253\u5370\u90bb\u63a5\u8868 */\n public func print() {\n Swift.print(\"\u90bb\u63a5\u8868 =\")\n for pair in adjList {\n var tmp: [Int] = []\n for vertex in pair.value {\n tmp.append(vertex.val)\n }\n Swift.print(\"\\(pair.key.val): \\(tmp),\")\n }\n }\n}\n
graph_adjacency_list.js/* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjList {\n // \u90bb\u63a5\u8868\uff0ckey\uff1a\u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n adjList;\n\n /* \u6784\u9020\u65b9\u6cd5 */\n constructor(edges) {\n this.adjList = new Map();\n // \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\n for (const edge of edges) {\n this.addVertex(edge[0]);\n this.addVertex(edge[1]);\n this.addEdge(edge[0], edge[1]);\n }\n }\n\n /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n size() {\n return this.adjList.size;\n }\n\n /* \u6dfb\u52a0\u8fb9 */\n addEdge(vet1, vet2) {\n if (\n !this.adjList.has(vet1) ||\n !this.adjList.has(vet2) ||\n vet1 === vet2\n ) {\n throw new Error('Illegal Argument Exception');\n }\n // \u6dfb\u52a0\u8fb9 vet1 - vet2\n this.adjList.get(vet1).push(vet2);\n this.adjList.get(vet2).push(vet1);\n }\n\n /* \u5220\u9664\u8fb9 */\n removeEdge(vet1, vet2) {\n if (\n !this.adjList.has(vet1) ||\n !this.adjList.has(vet2) ||\n vet1 === vet2\n ) {\n throw new Error('Illegal Argument Exception');\n }\n // \u5220\u9664\u8fb9 vet1 - vet2\n this.adjList.get(vet1).splice(this.adjList.get(vet1).indexOf(vet2), 1);\n this.adjList.get(vet2).splice(this.adjList.get(vet2).indexOf(vet1), 1);\n }\n\n /* \u6dfb\u52a0\u9876\u70b9 */\n addVertex(vet) {\n if (this.adjList.has(vet)) return;\n // \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\n this.adjList.set(vet, []);\n }\n\n /* \u5220\u9664\u9876\u70b9 */\n removeVertex(vet) {\n if (!this.adjList.has(vet)) {\n throw new Error('Illegal Argument Exception');\n }\n // \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\n this.adjList.delete(vet);\n // \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\n for (const set of this.adjList.values()) {\n const index = set.indexOf(vet);\n if (index > -1) {\n set.splice(index, 1);\n }\n }\n }\n\n /* \u6253\u5370\u90bb\u63a5\u8868 */\n print() {\n console.log('\u90bb\u63a5\u8868 =');\n for (const [key, value] of this.adjList) {\n const tmp = [];\n for (const vertex of value) {\n tmp.push(vertex.val);\n }\n console.log(key.val + ': ' + tmp.join());\n }\n }\n}\n
graph_adjacency_list.ts/* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjList {\n // \u90bb\u63a5\u8868\uff0ckey\uff1a\u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n adjList: Map<Vertex, Vertex[]>;\n\n /* \u6784\u9020\u65b9\u6cd5 */\n constructor(edges: Vertex[][]) {\n this.adjList = new Map();\n // \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\n for (const edge of edges) {\n this.addVertex(edge[0]);\n this.addVertex(edge[1]);\n this.addEdge(edge[0], edge[1]);\n }\n }\n\n /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n size(): number {\n return this.adjList.size;\n }\n\n /* \u6dfb\u52a0\u8fb9 */\n addEdge(vet1: Vertex, vet2: Vertex): void {\n if (\n !this.adjList.has(vet1) ||\n !this.adjList.has(vet2) ||\n vet1 === vet2\n ) {\n throw new Error('Illegal Argument Exception');\n }\n // \u6dfb\u52a0\u8fb9 vet1 - vet2\n this.adjList.get(vet1).push(vet2);\n this.adjList.get(vet2).push(vet1);\n }\n\n /* \u5220\u9664\u8fb9 */\n removeEdge(vet1: Vertex, vet2: Vertex): void {\n if (\n !this.adjList.has(vet1) ||\n !this.adjList.has(vet2) ||\n vet1 === vet2\n ) {\n throw new Error('Illegal Argument Exception');\n }\n // \u5220\u9664\u8fb9 vet1 - vet2\n this.adjList.get(vet1).splice(this.adjList.get(vet1).indexOf(vet2), 1);\n this.adjList.get(vet2).splice(this.adjList.get(vet2).indexOf(vet1), 1);\n }\n\n /* \u6dfb\u52a0\u9876\u70b9 */\n addVertex(vet: Vertex): void {\n if (this.adjList.has(vet)) return;\n // \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\n this.adjList.set(vet, []);\n }\n\n /* \u5220\u9664\u9876\u70b9 */\n removeVertex(vet: Vertex): void {\n if (!this.adjList.has(vet)) {\n throw new Error('Illegal Argument Exception');\n }\n // \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\n this.adjList.delete(vet);\n // \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\n for (const set of this.adjList.values()) {\n const index: number = set.indexOf(vet);\n if (index > -1) {\n set.splice(index, 1);\n }\n }\n }\n\n /* \u6253\u5370\u90bb\u63a5\u8868 */\n print(): void {\n console.log('\u90bb\u63a5\u8868 =');\n for (const [key, value] of this.adjList.entries()) {\n const tmp = [];\n for (const vertex of value) {\n tmp.push(vertex.val);\n }\n console.log(key.val + ': ' + tmp.join());\n }\n }\n}\n
graph_adjacency_list.dart/* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjList {\n // \u90bb\u63a5\u8868\uff0ckey\uff1a\u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n Map<Vertex, List<Vertex>> adjList = {};\n\n /* \u6784\u9020\u65b9\u6cd5 */\n GraphAdjList(List<List<Vertex>> edges) {\n for (List<Vertex> edge in edges) {\n addVertex(edge[0]);\n addVertex(edge[1]);\n addEdge(edge[0], edge[1]);\n }\n }\n\n /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n int size() {\n return adjList.length;\n }\n\n /* \u6dfb\u52a0\u8fb9 */\n void addEdge(Vertex vet1, Vertex vet2) {\n if (!adjList.containsKey(vet1) ||\n !adjList.containsKey(vet2) ||\n vet1 == vet2) {\n throw ArgumentError;\n }\n // \u6dfb\u52a0\u8fb9 vet1 - vet2\n adjList[vet1]!.add(vet2);\n adjList[vet2]!.add(vet1);\n }\n\n /* \u5220\u9664\u8fb9 */\n void removeEdge(Vertex vet1, Vertex vet2) {\n if (!adjList.containsKey(vet1) ||\n !adjList.containsKey(vet2) ||\n vet1 == vet2) {\n throw ArgumentError;\n }\n // \u5220\u9664\u8fb9 vet1 - vet2\n adjList[vet1]!.remove(vet2);\n adjList[vet2]!.remove(vet1);\n }\n\n /* \u6dfb\u52a0\u9876\u70b9 */\n void addVertex(Vertex vet) {\n if (adjList.containsKey(vet)) return;\n // \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\n adjList[vet] = [];\n }\n\n /* \u5220\u9664\u9876\u70b9 */\n void removeVertex(Vertex vet) {\n if (!adjList.containsKey(vet)) {\n throw ArgumentError;\n }\n // \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\n adjList.remove(vet);\n // \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\n adjList.forEach((key, value) {\n value.remove(vet);\n });\n }\n\n /* \u6253\u5370\u90bb\u63a5\u8868 */\n void printAdjList() {\n print(\"\u90bb\u63a5\u8868 =\");\n adjList.forEach((key, value) {\n List<int> tmp = [];\n for (Vertex vertex in value) {\n tmp.add(vertex.val);\n }\n print(\"${key.val}: $tmp,\");\n });\n }\n}\n
graph_adjacency_list.rs/* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b\u578b */\npub struct GraphAdjList {\n // \u90bb\u63a5\u8868\uff0ckey\uff1a\u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n pub adj_list: HashMap<Vertex, Vec<Vertex>>,\n}\n\nimpl GraphAdjList {\n /* \u6784\u9020\u65b9\u6cd5 */\n pub fn new(edges: Vec<[Vertex; 2]>) -> Self {\n let mut graph = GraphAdjList {\n adj_list: HashMap::new(),\n };\n // \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\n for edge in edges {\n graph.add_vertex(edge[0]);\n graph.add_vertex(edge[1]);\n graph.add_edge(edge[0], edge[1]);\n }\n\n graph\n }\n\n /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n #[allow(unused)]\n pub fn size(&self) -> usize {\n self.adj_list.len()\n }\n\n /* \u6dfb\u52a0\u8fb9 */\n pub fn add_edge(&mut self, vet1: Vertex, vet2: Vertex) {\n if !self.adj_list.contains_key(&vet1) || !self.adj_list.contains_key(&vet2) || vet1 == vet2\n {\n panic!(\"value error\");\n }\n // \u6dfb\u52a0\u8fb9 vet1 - vet2\n self.adj_list.get_mut(&vet1).unwrap().push(vet2);\n self.adj_list.get_mut(&vet2).unwrap().push(vet1);\n }\n\n /* \u5220\u9664\u8fb9 */\n #[allow(unused)]\n pub fn remove_edge(&mut self, vet1: Vertex, vet2: Vertex) {\n if !self.adj_list.contains_key(&vet1) || !self.adj_list.contains_key(&vet2) || vet1 == vet2\n {\n panic!(\"value error\");\n }\n // \u5220\u9664\u8fb9 vet1 - vet2\n self.adj_list\n .get_mut(&vet1)\n .unwrap()\n .retain(|&vet| vet != vet2);\n self.adj_list\n .get_mut(&vet2)\n .unwrap()\n .retain(|&vet| vet != vet1);\n }\n\n /* \u6dfb\u52a0\u9876\u70b9 */\n pub fn add_vertex(&mut self, vet: Vertex) {\n if self.adj_list.contains_key(&vet) {\n return;\n }\n // \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\n self.adj_list.insert(vet, vec![]);\n }\n\n /* \u5220\u9664\u9876\u70b9 */\n #[allow(unused)]\n pub fn remove_vertex(&mut self, vet: Vertex) {\n if !self.adj_list.contains_key(&vet) {\n panic!(\"value error\");\n }\n // \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\n self.adj_list.remove(&vet);\n // \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\n for list in self.adj_list.values_mut() {\n list.retain(|&v| v != vet);\n }\n }\n\n /* \u6253\u5370\u90bb\u63a5\u8868 */\n pub fn print(&self) {\n println!(\"\u90bb\u63a5\u8868 =\");\n for (vertex, list) in &self.adj_list {\n let list = list.iter().map(|vertex| vertex.val).collect::<Vec<i32>>();\n println!(\"{}: {:?},\", vertex.val, list);\n }\n }\n}\n
graph_adjacency_list.c/* \u8282\u70b9\u7ed3\u6784\u4f53 */\ntypedef struct AdjListNode {\n Vertex *vertex; // \u9876\u70b9\n struct AdjListNode *next; // \u540e\u7ee7\u8282\u70b9\n} AdjListNode;\n\n/* \u67e5\u627e\u9876\u70b9\u5bf9\u5e94\u7684\u8282\u70b9 */\nAdjListNode *findNode(GraphAdjList *graph, Vertex *vet) {\n for (int i = 0; i < graph->size; i++) {\n if (graph->heads[i]->vertex == vet) {\n return graph->heads[i];\n }\n }\n return NULL;\n}\n\n/* \u6dfb\u52a0\u8fb9\u8f85\u52a9\u51fd\u6570 */\nvoid addEdgeHelper(AdjListNode *head, Vertex *vet) {\n AdjListNode *node = (AdjListNode *)malloc(sizeof(AdjListNode));\n node->vertex = vet;\n // \u5934\u63d2\u6cd5\n node->next = head->next;\n head->next = node;\n}\n\n/* \u5220\u9664\u8fb9\u8f85\u52a9\u51fd\u6570 */\nvoid removeEdgeHelper(AdjListNode *head, Vertex *vet) {\n AdjListNode *pre = head;\n AdjListNode *cur = head->next;\n // \u5728\u94fe\u8868\u4e2d\u641c\u7d22 vet \u5bf9\u5e94\u8282\u70b9\n while (cur != NULL && cur->vertex != vet) {\n pre = cur;\n cur = cur->next;\n }\n if (cur == NULL)\n return;\n // \u5c06 vet \u5bf9\u5e94\u8282\u70b9\u4ece\u94fe\u8868\u4e2d\u5220\u9664\n pre->next = cur->next;\n // \u91ca\u653e\u5185\u5b58\n free(cur);\n}\n\n/* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\ntypedef struct {\n AdjListNode *heads[MAX_SIZE]; // \u8282\u70b9\u6570\u7ec4\n int size; // \u8282\u70b9\u6570\u91cf\n} GraphAdjList;\n\n/* \u6784\u9020\u51fd\u6570 */\nGraphAdjList *newGraphAdjList() {\n GraphAdjList *graph = (GraphAdjList *)malloc(sizeof(GraphAdjList));\n if (!graph) {\n return NULL;\n }\n graph->size = 0;\n for (int i = 0; i < MAX_SIZE; i++) {\n graph->heads[i] = NULL;\n }\n return graph;\n}\n\n/* \u6790\u6784\u51fd\u6570 */\nvoid delGraphAdjList(GraphAdjList *graph) {\n for (int i = 0; i < graph->size; i++) {\n AdjListNode *cur = graph->heads[i];\n while (cur != NULL) {\n AdjListNode *next = cur->next;\n if (cur != graph->heads[i]) {\n free(cur);\n }\n cur = next;\n }\n free(graph->heads[i]->vertex);\n free(graph->heads[i]);\n }\n free(graph);\n}\n\n/* \u67e5\u627e\u9876\u70b9\u5bf9\u5e94\u7684\u8282\u70b9 */\nAdjListNode *findNode(GraphAdjList *graph, Vertex *vet) {\n for (int i = 0; i < graph->size; i++) {\n if (graph->heads[i]->vertex == vet) {\n return graph->heads[i];\n }\n }\n return NULL;\n}\n\n/* \u6dfb\u52a0\u8fb9 */\nvoid addEdge(GraphAdjList *graph, Vertex *vet1, Vertex *vet2) {\n AdjListNode *head1 = findNode(graph, vet1);\n AdjListNode *head2 = findNode(graph, vet2);\n assert(head1 != NULL && head2 != NULL && head1 != head2);\n // \u6dfb\u52a0\u8fb9 vet1 - vet2\n addEdgeHelper(head1, vet2);\n addEdgeHelper(head2, vet1);\n}\n\n/* \u5220\u9664\u8fb9 */\nvoid removeEdge(GraphAdjList *graph, Vertex *vet1, Vertex *vet2) {\n AdjListNode *head1 = findNode(graph, vet1);\n AdjListNode *head2 = findNode(graph, vet2);\n assert(head1 != NULL && head2 != NULL);\n // \u5220\u9664\u8fb9 vet1 - vet2\n removeEdgeHelper(head1, head2->vertex);\n removeEdgeHelper(head2, head1->vertex);\n}\n\n/* \u6dfb\u52a0\u9876\u70b9 */\nvoid addVertex(GraphAdjList *graph, Vertex *vet) {\n assert(graph != NULL && graph->size < MAX_SIZE);\n AdjListNode *head = (AdjListNode *)malloc(sizeof(AdjListNode));\n head->vertex = vet;\n head->next = NULL;\n // \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\n graph->heads[graph->size++] = head;\n}\n\n/* \u5220\u9664\u9876\u70b9 */\nvoid removeVertex(GraphAdjList *graph, Vertex *vet) {\n AdjListNode *node = findNode(graph, vet);\n assert(node != NULL);\n // \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\n AdjListNode *cur = node, *pre = NULL;\n while (cur) {\n pre = cur;\n cur = cur->next;\n free(pre);\n }\n // \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\n for (int i = 0; i < graph->size; i++) {\n cur = graph->heads[i];\n pre = NULL;\n while (cur) {\n pre = cur;\n cur = cur->next;\n if (cur && cur->vertex == vet) {\n pre->next = cur->next;\n free(cur);\n break;\n }\n }\n }\n // \u5c06\u8be5\u9876\u70b9\u4e4b\u540e\u7684\u9876\u70b9\u5411\u524d\u79fb\u52a8\uff0c\u4ee5\u586b\u8865\u7a7a\u7f3a\n int i;\n for (i = 0; i < graph->size; i++) {\n if (graph->heads[i] == node)\n break;\n }\n for (int j = i; j < graph->size - 1; j++) {\n graph->heads[j] = graph->heads[j + 1];\n }\n graph->size--;\n free(vet);\n}\n
graph_adjacency_list.zig[class]{GraphAdjList}-[func]{}\n
"},{"location":"chapter_graph/graph_operations/#923","title":"9.2.3 \u00a0 \u6548\u7387\u5bf9\u6bd4","text":"\u8bbe\u56fe\u4e2d\u5171\u6709 \\(n\\) \u4e2a\u9876\u70b9\u548c \\(m\\) \u6761\u8fb9\uff0c\u8868 9-2 \u5bf9\u6bd4\u4e86\u90bb\u63a5\u77e9\u9635\u548c\u90bb\u63a5\u8868\u7684\u65f6\u95f4\u6548\u7387\u548c\u7a7a\u95f4\u6548\u7387\u3002
\u8868 9-2 \u00a0 \u90bb\u63a5\u77e9\u9635\u4e0e\u90bb\u63a5\u8868\u5bf9\u6bd4
\u90bb\u63a5\u77e9\u9635 \u90bb\u63a5\u8868\uff08\u94fe\u8868\uff09 \u90bb\u63a5\u8868\uff08\u54c8\u5e0c\u8868\uff09 \u5224\u65ad\u662f\u5426\u90bb\u63a5 \\(O(1)\\) \\(O(m)\\) \\(O(1)\\) \u6dfb\u52a0\u8fb9 \\(O(1)\\) \\(O(1)\\) \\(O(1)\\) \u5220\u9664\u8fb9 \\(O(1)\\) \\(O(m)\\) \\(O(1)\\) \u6dfb\u52a0\u9876\u70b9 \\(O(n)\\) \\(O(1)\\) \\(O(1)\\) \u5220\u9664\u9876\u70b9 \\(O(n^2)\\) \\(O(n + m)\\) \\(O(n)\\) \u5185\u5b58\u7a7a\u95f4\u5360\u7528 \\(O(n^2)\\) \\(O(n + m)\\) \\(O(n + m)\\)\u89c2\u5bdf\u8868 9-2 \uff0c\u4f3c\u4e4e\u90bb\u63a5\u8868\uff08\u54c8\u5e0c\u8868\uff09\u7684\u65f6\u95f4\u6548\u7387\u4e0e\u7a7a\u95f4\u6548\u7387\u6700\u4f18\u3002\u4f46\u5b9e\u9645\u4e0a\uff0c\u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u64cd\u4f5c\u8fb9\u7684\u6548\u7387\u66f4\u9ad8\uff0c\u53ea\u9700\u4e00\u6b21\u6570\u7ec4\u8bbf\u95ee\u6216\u8d4b\u503c\u64cd\u4f5c\u5373\u53ef\u3002\u7efc\u5408\u6765\u770b\uff0c\u90bb\u63a5\u77e9\u9635\u4f53\u73b0\u4e86\u201c\u4ee5\u7a7a\u95f4\u6362\u65f6\u95f4\u201d\u7684\u539f\u5219\uff0c\u800c\u90bb\u63a5\u8868\u4f53\u73b0\u4e86\u201c\u4ee5\u65f6\u95f4\u6362\u7a7a\u95f4\u201d\u7684\u539f\u5219\u3002
"},{"location":"chapter_graph/graph_traversal/","title":"9.3 \u00a0 \u56fe\u7684\u904d\u5386","text":"\u6811\u4ee3\u8868\u7684\u662f\u201c\u4e00\u5bf9\u591a\u201d\u7684\u5173\u7cfb\uff0c\u800c\u56fe\u5219\u5177\u6709\u66f4\u9ad8\u7684\u81ea\u7531\u5ea6\uff0c\u53ef\u4ee5\u8868\u793a\u4efb\u610f\u7684\u201c\u591a\u5bf9\u591a\u201d\u5173\u7cfb\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u628a\u6811\u770b\u4f5c\u56fe\u7684\u4e00\u79cd\u7279\u4f8b\u3002\u663e\u7136\uff0c\u6811\u7684\u904d\u5386\u64cd\u4f5c\u4e5f\u662f\u56fe\u7684\u904d\u5386\u64cd\u4f5c\u7684\u4e00\u79cd\u7279\u4f8b\u3002
\u56fe\u548c\u6811\u90fd\u9700\u8981\u5e94\u7528\u641c\u7d22\u7b97\u6cd5\u6765\u5b9e\u73b0\u904d\u5386\u64cd\u4f5c\u3002\u56fe\u7684\u904d\u5386\u65b9\u5f0f\u53ef\u5206\u4e3a\u4e24\u79cd\uff1a\u300c\u5e7f\u5ea6\u4f18\u5148\u904d\u5386 breadth-first traversal\u300d\u548c\u300c\u6df1\u5ea6\u4f18\u5148\u904d\u5386 depth-first traversal\u300d\u3002\u5b83\u4eec\u4e5f\u5e38\u88ab\u79f0\u4e3a\u300c\u5e7f\u5ea6\u4f18\u5148\u641c\u7d22 breadth-first search\u300d\u548c\u300c\u6df1\u5ea6\u4f18\u5148\u641c\u7d22 depth-first search\u300d\uff0c\u7b80\u79f0 BFS \u548c DFS \u3002
"},{"location":"chapter_graph/graph_traversal/#931","title":"9.3.1 \u00a0 \u5e7f\u5ea6\u4f18\u5148\u904d\u5386","text":"\u5e7f\u5ea6\u4f18\u5148\u904d\u5386\u662f\u4e00\u79cd\u7531\u8fd1\u53ca\u8fdc\u7684\u904d\u5386\u65b9\u5f0f\uff0c\u4ece\u67d0\u4e2a\u8282\u70b9\u51fa\u53d1\uff0c\u59cb\u7ec8\u4f18\u5148\u8bbf\u95ee\u8ddd\u79bb\u6700\u8fd1\u7684\u9876\u70b9\uff0c\u5e76\u4e00\u5c42\u5c42\u5411\u5916\u6269\u5f20\u3002\u5982\u56fe 9-9 \u6240\u793a\uff0c\u4ece\u5de6\u4e0a\u89d2\u9876\u70b9\u51fa\u53d1\uff0c\u9996\u5148\u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\uff0c\u7136\u540e\u904d\u5386\u4e0b\u4e00\u4e2a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\uff0c\u4ee5\u6b64\u7c7b\u63a8\uff0c\u76f4\u81f3\u6240\u6709\u9876\u70b9\u8bbf\u95ee\u5b8c\u6bd5\u3002
\u56fe 9-9 \u00a0 \u56fe\u7684\u5e7f\u5ea6\u4f18\u5148\u904d\u5386
"},{"location":"chapter_graph/graph_traversal/#1","title":"1. \u00a0 \u7b97\u6cd5\u5b9e\u73b0","text":"BFS \u901a\u5e38\u501f\u52a9\u961f\u5217\u6765\u5b9e\u73b0\uff0c\u4ee3\u7801\u5982\u4e0b\u6240\u793a\u3002\u961f\u5217\u5177\u6709\u201c\u5148\u5165\u5148\u51fa\u201d\u7684\u6027\u8d28\uff0c\u8fd9\u4e0e BFS \u7684\u201c\u7531\u8fd1\u53ca\u8fdc\u201d\u7684\u601d\u60f3\u5f02\u66f2\u540c\u5de5\u3002
startVet
\u52a0\u5165\u961f\u5217\uff0c\u5e76\u5f00\u542f\u5faa\u73af\u30022.
\uff0c\u76f4\u5230\u6240\u6709\u9876\u70b9\u88ab\u8bbf\u95ee\u5b8c\u6bd5\u540e\u7ed3\u675f\u3002\u4e3a\u4e86\u9632\u6b62\u91cd\u590d\u904d\u5386\u9876\u70b9\uff0c\u6211\u4eec\u9700\u8981\u501f\u52a9\u4e00\u4e2a\u54c8\u5e0c\u8868 visited
\u6765\u8bb0\u5f55\u54ea\u4e9b\u8282\u70b9\u5df2\u88ab\u8bbf\u95ee\u3002
def graph_bfs(graph: GraphAdjList, start_vet: Vertex) -> list[Vertex]:\n \"\"\"\u5e7f\u5ea6\u4f18\u5148\u904d\u5386 BFS\"\"\"\n # \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n # \u9876\u70b9\u904d\u5386\u5e8f\u5217\n res = []\n # \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n visited = set[Vertex]([start_vet])\n # \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\n que = deque[Vertex]([start_vet])\n # \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\n while len(que) > 0:\n vet = que.popleft() # \u961f\u9996\u9876\u70b9\u51fa\u961f\n res.append(vet) # \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n # \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n for adj_vet in graph.adj_list[vet]:\n if adj_vet in visited:\n continue # \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n que.append(adj_vet) # \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\n visited.add(adj_vet) # \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n # \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\n return res\n
graph_bfs.cpp/* \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 BFS */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nvector<Vertex *> graphBFS(GraphAdjList &graph, Vertex *startVet) {\n // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n vector<Vertex *> res;\n // \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n unordered_set<Vertex *> visited = {startVet};\n // \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\n queue<Vertex *> que;\n que.push(startVet);\n // \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\n while (!que.empty()) {\n Vertex *vet = que.front();\n que.pop(); // \u961f\u9996\u9876\u70b9\u51fa\u961f\n res.push_back(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n for (auto adjVet : graph.adjList[vet]) {\n if (visited.count(adjVet))\n continue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n que.push(adjVet); // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\n visited.emplace(adjVet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n }\n }\n // \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\n return res;\n}\n
graph_bfs.java/* \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 BFS */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nList<Vertex> graphBFS(GraphAdjList graph, Vertex startVet) {\n // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n List<Vertex> res = new ArrayList<>();\n // \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n Set<Vertex> visited = new HashSet<>();\n visited.add(startVet);\n // \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\n Queue<Vertex> que = new LinkedList<>();\n que.offer(startVet);\n // \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\n while (!que.isEmpty()) {\n Vertex vet = que.poll(); // \u961f\u9996\u9876\u70b9\u51fa\u961f\n res.add(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n for (Vertex adjVet : graph.adjList.get(vet)) {\n if (visited.contains(adjVet))\n continue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n que.offer(adjVet); // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\n visited.add(adjVet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n }\n }\n // \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\n return res;\n}\n
graph_bfs.cs/* \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 BFS */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nList<Vertex> GraphBFS(GraphAdjList graph, Vertex startVet) {\n // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n List<Vertex> res = [];\n // \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n HashSet<Vertex> visited = [startVet];\n // \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\n Queue<Vertex> que = new();\n que.Enqueue(startVet);\n // \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\n while (que.Count > 0) {\n Vertex vet = que.Dequeue(); // \u961f\u9996\u9876\u70b9\u51fa\u961f\n res.Add(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n foreach (Vertex adjVet in graph.adjList[vet]) {\n if (visited.Contains(adjVet)) {\n continue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n }\n que.Enqueue(adjVet); // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\n visited.Add(adjVet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n }\n }\n\n // \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\n return res;\n}\n
graph_bfs.go/* \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 BFS */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfunc graphBFS(g *graphAdjList, startVet Vertex) []Vertex {\n // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n res := make([]Vertex, 0)\n // \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n visited := make(map[Vertex]struct{})\n visited[startVet] = struct{}{}\n // \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS, \u4f7f\u7528\u5207\u7247\u6a21\u62df\u961f\u5217\n queue := make([]Vertex, 0)\n queue = append(queue, startVet)\n // \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\n for len(queue) > 0 {\n // \u961f\u9996\u9876\u70b9\u51fa\u961f\n vet := queue[0]\n queue = queue[1:]\n // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n res = append(res, vet)\n // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n for _, adjVet := range g.adjList[vet] {\n _, isExist := visited[adjVet]\n // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\n if !isExist {\n queue = append(queue, adjVet)\n visited[adjVet] = struct{}{}\n }\n }\n }\n // \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\n return res\n}\n
graph_bfs.swift/* \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 BFS */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfunc graphBFS(graph: GraphAdjList, startVet: Vertex) -> [Vertex] {\n // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n var res: [Vertex] = []\n // \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n var visited: Set<Vertex> = [startVet]\n // \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\n var que: [Vertex] = [startVet]\n // \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\n while !que.isEmpty {\n let vet = que.removeFirst() // \u961f\u9996\u9876\u70b9\u51fa\u961f\n res.append(vet) // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n for adjVet in graph.adjList[vet] ?? [] {\n if visited.contains(adjVet) {\n continue // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n }\n que.append(adjVet) // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\n visited.insert(adjVet) // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n }\n }\n // \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\n return res\n}\n
graph_bfs.js/* \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 BFS */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfunction graphBFS(graph, startVet) {\n // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n const res = [];\n // \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n const visited = new Set();\n visited.add(startVet);\n // \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\n const que = [startVet];\n // \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\n while (que.length) {\n const vet = que.shift(); // \u961f\u9996\u9876\u70b9\u51fa\u961f\n res.push(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n for (const adjVet of graph.adjList.get(vet) ?? []) {\n if (visited.has(adjVet)) {\n continue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n }\n que.push(adjVet); // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\n visited.add(adjVet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n }\n }\n // \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\n return res;\n}\n
graph_bfs.ts/* \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 BFS */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfunction graphBFS(graph: GraphAdjList, startVet: Vertex): Vertex[] {\n // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n const res: Vertex[] = [];\n // \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n const visited: Set<Vertex> = new Set();\n visited.add(startVet);\n // \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\n const que = [startVet];\n // \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\n while (que.length) {\n const vet = que.shift(); // \u961f\u9996\u9876\u70b9\u51fa\u961f\n res.push(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n for (const adjVet of graph.adjList.get(vet) ?? []) {\n if (visited.has(adjVet)) {\n continue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n }\n que.push(adjVet); // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\n visited.add(adjVet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n }\n }\n // \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\n return res;\n}\n
graph_bfs.dart/* \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 BFS */\nList<Vertex> graphBFS(GraphAdjList graph, Vertex startVet) {\n // \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n List<Vertex> res = [];\n // \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n Set<Vertex> visited = {};\n visited.add(startVet);\n // \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\n Queue<Vertex> que = Queue();\n que.add(startVet);\n // \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\n while (que.isNotEmpty) {\n Vertex vet = que.removeFirst(); // \u961f\u9996\u9876\u70b9\u51fa\u961f\n res.add(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n for (Vertex adjVet in graph.adjList[vet]!) {\n if (visited.contains(adjVet)) {\n continue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n }\n que.add(adjVet); // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\n visited.add(adjVet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n }\n }\n // \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\n return res;\n}\n
graph_bfs.rs/* \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 BFS */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfn graph_bfs(graph: GraphAdjList, start_vet: Vertex) -> Vec<Vertex> {\n // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n let mut res = vec![];\n // \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n let mut visited = HashSet::new();\n visited.insert(start_vet);\n // \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\n let mut que = VecDeque::new();\n que.push_back(start_vet);\n // \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\n while !que.is_empty() {\n let vet = que.pop_front().unwrap(); // \u961f\u9996\u9876\u70b9\u51fa\u961f\n res.push(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n if let Some(adj_vets) = graph.adj_list.get(&vet) {\n for &adj_vet in adj_vets {\n if visited.contains(&adj_vet) {\n continue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n }\n que.push_back(adj_vet); // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\n visited.insert(adj_vet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n }\n }\n }\n // \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\n res\n}\n
graph_bfs.c/* \u8282\u70b9\u961f\u5217\u7ed3\u6784\u4f53 */\ntypedef struct {\n Vertex *vertices[MAX_SIZE];\n int front, rear, size;\n} Queue;\n\n/* \u6784\u9020\u51fd\u6570 */\nQueue *newQueue() {\n Queue *q = (Queue *)malloc(sizeof(Queue));\n q->front = q->rear = q->size = 0;\n return q;\n}\n\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nint isEmpty(Queue *q) {\n return q->size == 0;\n}\n\n/* \u5165\u961f\u64cd\u4f5c */\nvoid enqueue(Queue *q, Vertex *vet) {\n q->vertices[q->rear] = vet;\n q->rear = (q->rear + 1) % MAX_SIZE;\n q->size++;\n}\n\n/* \u51fa\u961f\u64cd\u4f5c */\nVertex *dequeue(Queue *q) {\n Vertex *vet = q->vertices[q->front];\n q->front = (q->front + 1) % MAX_SIZE;\n q->size--;\n return vet;\n}\n\n/* \u68c0\u67e5\u9876\u70b9\u662f\u5426\u5df2\u88ab\u8bbf\u95ee */\nint isVisited(Vertex **visited, int size, Vertex *vet) {\n // \u904d\u5386\u67e5\u627e\u8282\u70b9\uff0c\u4f7f\u7528 O(n) \u65f6\u95f4\n for (int i = 0; i < size; i++) {\n if (visited[i] == vet)\n return 1;\n }\n return 0;\n}\n\n/* \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 BFS */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nvoid graphBFS(GraphAdjList *graph, Vertex *startVet, Vertex **res, int *resSize, Vertex **visited, int *visitedSize) {\n // \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\n Queue *queue = newQueue();\n enqueue(queue, startVet);\n visited[(*visitedSize)++] = startVet;\n // \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\n while (!isEmpty(queue)) {\n Vertex *vet = dequeue(queue); // \u961f\u9996\u9876\u70b9\u51fa\u961f\n res[(*resSize)++] = vet; // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n AdjListNode *node = findNode(graph, vet);\n while (node != NULL) {\n // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n if (!isVisited(visited, *visitedSize, node->vertex)) {\n enqueue(queue, node->vertex); // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\n visited[(*visitedSize)++] = node->vertex; // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n }\n node = node->next;\n }\n }\n // \u91ca\u653e\u5185\u5b58\n free(queue);\n}\n
graph_bfs.zig[class]{}-[func]{graphBFS}\n
\u4ee3\u7801\u76f8\u5bf9\u62bd\u8c61\uff0c\u5efa\u8bae\u5bf9\u7167\u56fe 9-10 \u6765\u52a0\u6df1\u7406\u89e3\u3002
<1><2><3><4><5><6><7><8><9><10><11>\u56fe 9-10 \u00a0 \u56fe\u7684\u5e7f\u5ea6\u4f18\u5148\u904d\u5386\u6b65\u9aa4
\u5e7f\u5ea6\u4f18\u5148\u904d\u5386\u7684\u5e8f\u5217\u662f\u5426\u552f\u4e00\uff1f
\u4e0d\u552f\u4e00\u3002\u5e7f\u5ea6\u4f18\u5148\u904d\u5386\u53ea\u8981\u6c42\u6309\u201c\u7531\u8fd1\u53ca\u8fdc\u201d\u7684\u987a\u5e8f\u904d\u5386\uff0c\u800c\u591a\u4e2a\u76f8\u540c\u8ddd\u79bb\u7684\u9876\u70b9\u7684\u904d\u5386\u987a\u5e8f\u5141\u8bb8\u88ab\u4efb\u610f\u6253\u4e71\u3002\u4ee5\u56fe 9-10 \u4e3a\u4f8b\uff0c\u9876\u70b9 \\(1\\)\u3001\\(3\\) \u7684\u8bbf\u95ee\u987a\u5e8f\u53ef\u4ee5\u4ea4\u6362\uff0c\u9876\u70b9 \\(2\\)\u3001\\(4\\)\u3001\\(6\\) \u7684\u8bbf\u95ee\u987a\u5e8f\u4e5f\u53ef\u4ee5\u4efb\u610f\u4ea4\u6362\u3002
"},{"location":"chapter_graph/graph_traversal/#2","title":"2. \u00a0 \u590d\u6742\u5ea6\u5206\u6790","text":"\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u6240\u6709\u9876\u70b9\u90fd\u4f1a\u5165\u961f\u5e76\u51fa\u961f\u4e00\u6b21\uff0c\u4f7f\u7528 \\(O(|V|)\\) \u65f6\u95f4\uff1b\u5728\u904d\u5386\u90bb\u63a5\u9876\u70b9\u7684\u8fc7\u7a0b\u4e2d\uff0c\u7531\u4e8e\u662f\u65e0\u5411\u56fe\uff0c\u56e0\u6b64\u6240\u6709\u8fb9\u90fd\u4f1a\u88ab\u8bbf\u95ee \\(2\\) \u6b21\uff0c\u4f7f\u7528 \\(O(2|E|)\\) \u65f6\u95f4\uff1b\u603b\u4f53\u4f7f\u7528 \\(O(|V| + |E|)\\) \u65f6\u95f4\u3002
\u7a7a\u95f4\u590d\u6742\u5ea6\uff1a\u5217\u8868 res
\uff0c\u54c8\u5e0c\u8868 visited
\uff0c\u961f\u5217 que
\u4e2d\u7684\u9876\u70b9\u6570\u91cf\u6700\u591a\u4e3a \\(|V|\\) \uff0c\u4f7f\u7528 \\(O(|V|)\\) \u7a7a\u95f4\u3002
\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u662f\u4e00\u79cd\u4f18\u5148\u8d70\u5230\u5e95\u3001\u65e0\u8def\u53ef\u8d70\u518d\u56de\u5934\u7684\u904d\u5386\u65b9\u5f0f\u3002\u5982\u56fe 9-11 \u6240\u793a\uff0c\u4ece\u5de6\u4e0a\u89d2\u9876\u70b9\u51fa\u53d1\uff0c\u8bbf\u95ee\u5f53\u524d\u9876\u70b9\u7684\u67d0\u4e2a\u90bb\u63a5\u9876\u70b9\uff0c\u76f4\u5230\u8d70\u5230\u5c3d\u5934\u65f6\u8fd4\u56de\uff0c\u518d\u7ee7\u7eed\u8d70\u5230\u5c3d\u5934\u5e76\u8fd4\u56de\uff0c\u4ee5\u6b64\u7c7b\u63a8\uff0c\u76f4\u81f3\u6240\u6709\u9876\u70b9\u904d\u5386\u5b8c\u6210\u3002
\u56fe 9-11 \u00a0 \u56fe\u7684\u6df1\u5ea6\u4f18\u5148\u904d\u5386
"},{"location":"chapter_graph/graph_traversal/#1_1","title":"1. \u00a0 \u7b97\u6cd5\u5b9e\u73b0","text":"\u8fd9\u79cd\u201c\u8d70\u5230\u5c3d\u5934\u518d\u8fd4\u56de\u201d\u7684\u7b97\u6cd5\u8303\u5f0f\u901a\u5e38\u57fa\u4e8e\u9012\u5f52\u6765\u5b9e\u73b0\u3002\u4e0e\u5e7f\u5ea6\u4f18\u5148\u904d\u5386\u7c7b\u4f3c\uff0c\u5728\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u4e2d\uff0c\u6211\u4eec\u4e5f\u9700\u8981\u501f\u52a9\u4e00\u4e2a\u54c8\u5e0c\u8868 visited
\u6765\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\uff0c\u4ee5\u907f\u514d\u91cd\u590d\u8bbf\u95ee\u9876\u70b9\u3002
def dfs(graph: GraphAdjList, visited: set[Vertex], res: list[Vertex], vet: Vertex):\n \"\"\"\u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS \u8f85\u52a9\u51fd\u6570\"\"\"\n res.append(vet) # \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n visited.add(vet) # \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n # \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n for adjVet in graph.adj_list[vet]:\n if adjVet in visited:\n continue # \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n # \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\n dfs(graph, visited, res, adjVet)\n\ndef graph_dfs(graph: GraphAdjList, start_vet: Vertex) -> list[Vertex]:\n \"\"\"\u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS\"\"\"\n # \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n # \u9876\u70b9\u904d\u5386\u5e8f\u5217\n res = []\n # \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n visited = set[Vertex]()\n dfs(graph, visited, res, start_vet)\n return res\n
graph_dfs.cpp/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS \u8f85\u52a9\u51fd\u6570 */\nvoid dfs(GraphAdjList &graph, unordered_set<Vertex *> &visited, vector<Vertex *> &res, Vertex *vet) {\n res.push_back(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n visited.emplace(vet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n for (Vertex *adjVet : graph.adjList[vet]) {\n if (visited.count(adjVet))\n continue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n // \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\n dfs(graph, visited, res, adjVet);\n }\n}\n\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nvector<Vertex *> graphDFS(GraphAdjList &graph, Vertex *startVet) {\n // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n vector<Vertex *> res;\n // \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n unordered_set<Vertex *> visited;\n dfs(graph, visited, res, startVet);\n return res;\n}\n
graph_dfs.java/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS \u8f85\u52a9\u51fd\u6570 */\nvoid dfs(GraphAdjList graph, Set<Vertex> visited, List<Vertex> res, Vertex vet) {\n res.add(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n visited.add(vet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n for (Vertex adjVet : graph.adjList.get(vet)) {\n if (visited.contains(adjVet))\n continue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n // \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\n dfs(graph, visited, res, adjVet);\n }\n}\n\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nList<Vertex> graphDFS(GraphAdjList graph, Vertex startVet) {\n // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n List<Vertex> res = new ArrayList<>();\n // \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n Set<Vertex> visited = new HashSet<>();\n dfs(graph, visited, res, startVet);\n return res;\n}\n
graph_dfs.cs/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS \u8f85\u52a9\u51fd\u6570 */\nvoid DFS(GraphAdjList graph, HashSet<Vertex> visited, List<Vertex> res, Vertex vet) {\n res.Add(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n visited.Add(vet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n foreach (Vertex adjVet in graph.adjList[vet]) {\n if (visited.Contains(adjVet)) {\n continue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9 \n }\n // \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\n DFS(graph, visited, res, adjVet);\n }\n}\n\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nList<Vertex> GraphDFS(GraphAdjList graph, Vertex startVet) {\n // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n List<Vertex> res = [];\n // \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n HashSet<Vertex> visited = [];\n DFS(graph, visited, res, startVet);\n return res;\n}\n
graph_dfs.go/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS \u8f85\u52a9\u51fd\u6570 */\nfunc dfs(g *graphAdjList, visited map[Vertex]struct{}, res *[]Vertex, vet Vertex) {\n // append \u64cd\u4f5c\u4f1a\u8fd4\u56de\u65b0\u7684\u7684\u5f15\u7528\uff0c\u5fc5\u987b\u8ba9\u539f\u5f15\u7528\u91cd\u65b0\u8d4b\u503c\u4e3a\u65b0slice\u7684\u5f15\u7528\n *res = append(*res, vet)\n visited[vet] = struct{}{}\n // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n for _, adjVet := range g.adjList[vet] {\n _, isExist := visited[adjVet]\n // \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\n if !isExist {\n dfs(g, visited, res, adjVet)\n }\n }\n}\n\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfunc graphDFS(g *graphAdjList, startVet Vertex) []Vertex {\n // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n res := make([]Vertex, 0)\n // \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n visited := make(map[Vertex]struct{})\n dfs(g, visited, &res, startVet)\n // \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\n return res\n}\n
graph_dfs.swift/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS \u8f85\u52a9\u51fd\u6570 */\nfunc dfs(graph: GraphAdjList, visited: inout Set<Vertex>, res: inout [Vertex], vet: Vertex) {\n res.append(vet) // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n visited.insert(vet) // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n for adjVet in graph.adjList[vet] ?? [] {\n if visited.contains(adjVet) {\n continue // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n }\n // \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\n dfs(graph: graph, visited: &visited, res: &res, vet: adjVet)\n }\n}\n\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfunc graphDFS(graph: GraphAdjList, startVet: Vertex) -> [Vertex] {\n // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n var res: [Vertex] = []\n // \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n var visited: Set<Vertex> = []\n dfs(graph: graph, visited: &visited, res: &res, vet: startVet)\n return res\n}\n
graph_dfs.js/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfunction dfs(graph, visited, res, vet) {\n res.push(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n visited.add(vet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n for (const adjVet of graph.adjList.get(vet)) {\n if (visited.has(adjVet)) {\n continue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n }\n // \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\n dfs(graph, visited, res, adjVet);\n }\n}\n\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfunction graphDFS(graph, startVet) {\n // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n const res = [];\n // \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n const visited = new Set();\n dfs(graph, visited, res, startVet);\n return res;\n}\n
graph_dfs.ts/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS \u8f85\u52a9\u51fd\u6570 */\nfunction dfs(\n graph: GraphAdjList,\n visited: Set<Vertex>,\n res: Vertex[],\n vet: Vertex\n): void {\n res.push(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n visited.add(vet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n for (const adjVet of graph.adjList.get(vet)) {\n if (visited.has(adjVet)) {\n continue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n }\n // \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\n dfs(graph, visited, res, adjVet);\n }\n}\n\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfunction graphDFS(graph: GraphAdjList, startVet: Vertex): Vertex[] {\n // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n const res: Vertex[] = [];\n // \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n const visited: Set<Vertex> = new Set();\n dfs(graph, visited, res, startVet);\n return res;\n}\n
graph_dfs.dart/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS \u8f85\u52a9\u51fd\u6570 */\nvoid dfs(\n GraphAdjList graph,\n Set<Vertex> visited,\n List<Vertex> res,\n Vertex vet,\n) {\n res.add(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n visited.add(vet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n for (Vertex adjVet in graph.adjList[vet]!) {\n if (visited.contains(adjVet)) {\n continue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n }\n // \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\n dfs(graph, visited, res, adjVet);\n }\n}\n\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS */\nList<Vertex> graphDFS(GraphAdjList graph, Vertex startVet) {\n // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n List<Vertex> res = [];\n // \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n Set<Vertex> visited = {};\n dfs(graph, visited, res, startVet);\n return res;\n}\n
graph_dfs.rs/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS \u8f85\u52a9\u51fd\u6570 */\nfn dfs(graph: &GraphAdjList, visited: &mut HashSet<Vertex>, res: &mut Vec<Vertex>, vet: Vertex) {\n res.push(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n visited.insert(vet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n if let Some(adj_vets) = graph.adj_list.get(&vet) {\n for &adj_vet in adj_vets {\n if visited.contains(&adj_vet) {\n continue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n }\n // \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\n dfs(graph, visited, res, adj_vet);\n }\n }\n}\n\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfn graph_dfs(graph: GraphAdjList, start_vet: Vertex) -> Vec<Vertex> {\n // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n let mut res = vec![];\n // \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n let mut visited = HashSet::new();\n dfs(&graph, &mut visited, &mut res, start_vet);\n\n res\n}\n
graph_dfs.c/* \u68c0\u67e5\u9876\u70b9\u662f\u5426\u5df2\u88ab\u8bbf\u95ee */\nint isVisited(Vertex **res, int size, Vertex *vet) {\n // \u904d\u5386\u67e5\u627e\u8282\u70b9\uff0c\u4f7f\u7528 O(n) \u65f6\u95f4\n for (int i = 0; i < size; i++) {\n if (res[i] == vet) {\n return 1;\n }\n }\n return 0;\n}\n\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS \u8f85\u52a9\u51fd\u6570 */\nvoid dfs(GraphAdjList *graph, Vertex **res, int *resSize, Vertex *vet) {\n // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n res[(*resSize)++] = vet;\n // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n AdjListNode *node = findNode(graph, vet);\n while (node != NULL) {\n // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n if (!isVisited(res, *resSize, node->vertex)) {\n // \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\n dfs(graph, res, resSize, node->vertex);\n }\n node = node->next;\n }\n}\n\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nvoid graphDFS(GraphAdjList *graph, Vertex *startVet, Vertex **res, int *resSize) {\n dfs(graph, res, resSize, startVet);\n}\n
graph_dfs.zig[class]{}-[func]{dfs}\n\n[class]{}-[func]{graphDFS}\n
\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u7684\u7b97\u6cd5\u6d41\u7a0b\u5982\u56fe 9-12 \u6240\u793a\u3002
\u4e3a\u4e86\u52a0\u6df1\u7406\u89e3\uff0c\u5efa\u8bae\u5c06\u56fe 9-12 \u4e0e\u4ee3\u7801\u7ed3\u5408\u8d77\u6765\uff0c\u5728\u8111\u4e2d\u6a21\u62df\uff08\u6216\u8005\u7528\u7b14\u753b\u4e0b\u6765\uff09\u6574\u4e2a DFS \u8fc7\u7a0b\uff0c\u5305\u62ec\u6bcf\u4e2a\u9012\u5f52\u65b9\u6cd5\u4f55\u65f6\u5f00\u542f\u3001\u4f55\u65f6\u8fd4\u56de\u3002
<1><2><3><4><5><6><7><8><9><10><11>\u56fe 9-12 \u00a0 \u56fe\u7684\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u6b65\u9aa4
\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u7684\u5e8f\u5217\u662f\u5426\u552f\u4e00\uff1f
\u4e0e\u5e7f\u5ea6\u4f18\u5148\u904d\u5386\u7c7b\u4f3c\uff0c\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u5e8f\u5217\u7684\u987a\u5e8f\u4e5f\u4e0d\u662f\u552f\u4e00\u7684\u3002\u7ed9\u5b9a\u67d0\u9876\u70b9\uff0c\u5148\u5f80\u54ea\u4e2a\u65b9\u5411\u63a2\u7d22\u90fd\u53ef\u4ee5\uff0c\u5373\u90bb\u63a5\u9876\u70b9\u7684\u987a\u5e8f\u53ef\u4ee5\u4efb\u610f\u6253\u4e71\uff0c\u90fd\u662f\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u3002
\u4ee5\u6811\u7684\u904d\u5386\u4e3a\u4f8b\uff0c\u201c\u6839 \\(\\rightarrow\\) \u5de6 \\(\\rightarrow\\) \u53f3\u201d\u201c\u5de6 \\(\\rightarrow\\) \u6839 \\(\\rightarrow\\) \u53f3\u201d\u201c\u5de6 \\(\\rightarrow\\) \u53f3 \\(\\rightarrow\\) \u6839\u201d\u5206\u522b\u5bf9\u5e94\u524d\u5e8f\u3001\u4e2d\u5e8f\u3001\u540e\u5e8f\u904d\u5386\uff0c\u5b83\u4eec\u5c55\u793a\u4e86\u4e09\u79cd\u904d\u5386\u4f18\u5148\u7ea7\uff0c\u7136\u800c\u8fd9\u4e09\u8005\u90fd\u5c5e\u4e8e\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u3002
"},{"location":"chapter_graph/graph_traversal/#2_1","title":"2. \u00a0 \u590d\u6742\u5ea6\u5206\u6790","text":"\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u6240\u6709\u9876\u70b9\u90fd\u4f1a\u88ab\u8bbf\u95ee \\(1\\) \u6b21\uff0c\u4f7f\u7528 \\(O(|V|)\\) \u65f6\u95f4\uff1b\u6240\u6709\u8fb9\u90fd\u4f1a\u88ab\u8bbf\u95ee \\(2\\) \u6b21\uff0c\u4f7f\u7528 \\(O(2|E|)\\) \u65f6\u95f4\uff1b\u603b\u4f53\u4f7f\u7528 \\(O(|V| + |E|)\\) \u65f6\u95f4\u3002
\u7a7a\u95f4\u590d\u6742\u5ea6\uff1a\u5217\u8868 res
\uff0c\u54c8\u5e0c\u8868 visited
\u9876\u70b9\u6570\u91cf\u6700\u591a\u4e3a \\(|V|\\) \uff0c\u9012\u5f52\u6df1\u5ea6\u6700\u5927\u4e3a \\(|V|\\) \uff0c\u56e0\u6b64\u4f7f\u7528 \\(O(|V|)\\) \u7a7a\u95f4\u3002
\u8def\u5f84\u7684\u5b9a\u4e49\u662f\u9876\u70b9\u5e8f\u5217\u8fd8\u662f\u8fb9\u5e8f\u5217\uff1f
\u7ef4\u57fa\u767e\u79d1\u4e0a\u4e0d\u540c\u8bed\u8a00\u7248\u672c\u7684\u5b9a\u4e49\u4e0d\u4e00\u81f4\uff1a\u82f1\u6587\u7248\u662f\u201c\u8def\u5f84\u662f\u4e00\u4e2a\u8fb9\u5e8f\u5217\u201d\uff0c\u800c\u4e2d\u6587\u7248\u662f\u201c\u8def\u5f84\u662f\u4e00\u4e2a\u9876\u70b9\u5e8f\u5217\u201d\u3002\u4ee5\u4e0b\u662f\u82f1\u6587\u7248\u539f\u6587\uff1aIn graph theory, a path in a graph is a finite or infinite sequence of edges which joins a sequence of vertices. \u5728\u672c\u6587\u4e2d\uff0c\u8def\u5f84\u88ab\u89c6\u4e3a\u4e00\u4e2a\u8fb9\u5e8f\u5217\uff0c\u800c\u4e0d\u662f\u4e00\u4e2a\u9876\u70b9\u5e8f\u5217\u3002\u8fd9\u662f\u56e0\u4e3a\u4e24\u4e2a\u9876\u70b9\u4e4b\u95f4\u53ef\u80fd\u5b58\u5728\u591a\u6761\u8fb9\u8fde\u63a5\uff0c\u6b64\u65f6\u6bcf\u6761\u8fb9\u90fd\u5bf9\u5e94\u4e00\u6761\u8def\u5f84\u3002
\u975e\u8fde\u901a\u56fe\u4e2d\u662f\u5426\u4f1a\u6709\u65e0\u6cd5\u904d\u5386\u5230\u7684\u70b9\uff1f
\u5728\u975e\u8fde\u901a\u56fe\u4e2d\uff0c\u4ece\u67d0\u4e2a\u9876\u70b9\u51fa\u53d1\uff0c\u81f3\u5c11\u6709\u4e00\u4e2a\u9876\u70b9\u65e0\u6cd5\u5230\u8fbe\u3002\u904d\u5386\u975e\u8fde\u901a\u56fe\u9700\u8981\u8bbe\u7f6e\u591a\u4e2a\u8d77\u70b9\uff0c\u4ee5\u904d\u5386\u5230\u56fe\u7684\u6240\u6709\u8fde\u901a\u5206\u91cf\u3002
\u5728\u90bb\u63a5\u8868\u4e2d\uff0c\u201c\u4e0e\u8be5\u9876\u70b9\u76f8\u8fde\u7684\u6240\u6709\u9876\u70b9\u201d\u7684\u9876\u70b9\u987a\u5e8f\u662f\u5426\u6709\u8981\u6c42\uff1f
\u53ef\u4ee5\u662f\u4efb\u610f\u987a\u5e8f\u3002\u4f46\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u6309\u7167\u6307\u5b9a\u89c4\u5219\u6765\u6392\u5e8f\uff0c\u6bd4\u5982\u6309\u7167\u9876\u70b9\u6dfb\u52a0\u7684\u6b21\u5e8f\uff0c\u6216\u8005\u6309\u7167\u9876\u70b9\u503c\u5927\u5c0f\u7684\u987a\u5e8f\u7b49\uff0c\u8fd9\u6837\u6709\u52a9\u4e8e\u5feb\u901f\u67e5\u627e\u201c\u5e26\u6709\u67d0\u79cd\u6781\u503c\u201d\u7684\u9876\u70b9\u3002
"},{"location":"chapter_greedy/","title":"\u7b2c 15 \u7ae0 \u00a0 \u8d2a\u5fc3","text":"Abstract
\u5411\u65e5\u8475\u671d\u7740\u592a\u9633\u8f6c\u52a8\uff0c\u65f6\u523b\u8ffd\u6c42\u81ea\u8eab\u6210\u957f\u7684\u6700\u5927\u53ef\u80fd\u3002
\u8d2a\u5fc3\u7b56\u7565\u5728\u4e00\u8f6e\u8f6e\u7684\u7b80\u5355\u9009\u62e9\u4e2d\uff0c\u9010\u6b65\u5bfc\u5411\u6700\u4f73\u7b54\u6848\u3002
"},{"location":"chapter_greedy/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"Question
\u7ed9\u5b9a \\(n\\) \u4e2a\u7269\u54c1\uff0c\u7b2c \\(i\\) \u4e2a\u7269\u54c1\u7684\u91cd\u91cf\u4e3a \\(wgt[i-1]\\)\u3001\u4ef7\u503c\u4e3a \\(val[i-1]\\) \uff0c\u548c\u4e00\u4e2a\u5bb9\u91cf\u4e3a \\(cap\\) \u7684\u80cc\u5305\u3002\u6bcf\u4e2a\u7269\u54c1\u53ea\u80fd\u9009\u62e9\u4e00\u6b21\uff0c\u4f46\u53ef\u4ee5\u9009\u62e9\u7269\u54c1\u7684\u4e00\u90e8\u5206\uff0c\u4ef7\u503c\u6839\u636e\u9009\u62e9\u7684\u91cd\u91cf\u6bd4\u4f8b\u8ba1\u7b97\uff0c\u95ee\u5728\u9650\u5b9a\u80cc\u5305\u5bb9\u91cf\u4e0b\u80cc\u5305\u4e2d\u7269\u54c1\u7684\u6700\u5927\u4ef7\u503c\u3002\u793a\u4f8b\u5982\u56fe 15-3 \u6240\u793a\u3002
\u56fe 15-3 \u00a0 \u5206\u6570\u80cc\u5305\u95ee\u9898\u7684\u793a\u4f8b\u6570\u636e
\u5206\u6570\u80cc\u5305\u95ee\u9898\u548c 0-1 \u80cc\u5305\u95ee\u9898\u6574\u4f53\u4e0a\u975e\u5e38\u76f8\u4f3c\uff0c\u72b6\u6001\u5305\u542b\u5f53\u524d\u7269\u54c1 \\(i\\) \u548c\u5bb9\u91cf \\(c\\) \uff0c\u76ee\u6807\u662f\u6c42\u9650\u5b9a\u80cc\u5305\u5bb9\u91cf\u4e0b\u7684\u6700\u5927\u4ef7\u503c\u3002
\u4e0d\u540c\u70b9\u5728\u4e8e\uff0c\u672c\u9898\u5141\u8bb8\u53ea\u9009\u62e9\u7269\u54c1\u7684\u4e00\u90e8\u5206\u3002\u5982\u56fe 15-4 \u6240\u793a\uff0c\u6211\u4eec\u53ef\u4ee5\u5bf9\u7269\u54c1\u4efb\u610f\u5730\u8fdb\u884c\u5207\u5206\uff0c\u5e76\u6309\u7167\u91cd\u91cf\u6bd4\u4f8b\u6765\u8ba1\u7b97\u76f8\u5e94\u4ef7\u503c\u3002
\u56fe 15-4 \u00a0 \u7269\u54c1\u5728\u5355\u4f4d\u91cd\u91cf\u4e0b\u7684\u4ef7\u503c
"},{"location":"chapter_greedy/fractional_knapsack_problem/#1","title":"1. \u00a0 \u8d2a\u5fc3\u7b56\u7565\u786e\u5b9a","text":"\u6700\u5927\u5316\u80cc\u5305\u5185\u7269\u54c1\u603b\u4ef7\u503c\uff0c\u672c\u8d28\u4e0a\u662f\u6700\u5927\u5316\u5355\u4f4d\u91cd\u91cf\u4e0b\u7684\u7269\u54c1\u4ef7\u503c\u3002\u7531\u6b64\u4fbf\u53ef\u63a8\u7406\u51fa\u56fe 15-5 \u6240\u793a\u7684\u8d2a\u5fc3\u7b56\u7565\u3002
\u56fe 15-5 \u00a0 \u5206\u6570\u80cc\u5305\u95ee\u9898\u7684\u8d2a\u5fc3\u7b56\u7565
"},{"location":"chapter_greedy/fractional_knapsack_problem/#2","title":"2. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"\u6211\u4eec\u5efa\u7acb\u4e86\u4e00\u4e2a\u7269\u54c1\u7c7b Item
\uff0c\u4ee5\u4fbf\u5c06\u7269\u54c1\u6309\u7167\u5355\u4f4d\u4ef7\u503c\u8fdb\u884c\u6392\u5e8f\u3002\u5faa\u73af\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u5f53\u80cc\u5305\u5df2\u6ee1\u65f6\u8df3\u51fa\u5e76\u8fd4\u56de\u89e3\uff1a
class Item:\n \"\"\"\u7269\u54c1\"\"\"\n\n def __init__(self, w: int, v: int):\n self.w = w # \u7269\u54c1\u91cd\u91cf\n self.v = v # \u7269\u54c1\u4ef7\u503c\n\ndef fractional_knapsack(wgt: list[int], val: list[int], cap: int) -> int:\n \"\"\"\u5206\u6570\u80cc\u5305\uff1a\u8d2a\u5fc3\"\"\"\n # \u521b\u5efa\u7269\u54c1\u5217\u8868\uff0c\u5305\u542b\u4e24\u4e2a\u5c5e\u6027\uff1a\u91cd\u91cf\u3001\u4ef7\u503c\n items = [Item(w, v) for w, v in zip(wgt, val)]\n # \u6309\u7167\u5355\u4f4d\u4ef7\u503c item.v / item.w \u4ece\u9ad8\u5230\u4f4e\u8fdb\u884c\u6392\u5e8f\n items.sort(key=lambda item: item.v / item.w, reverse=True)\n # \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\n res = 0\n for item in items:\n if item.w <= cap:\n # \u82e5\u5269\u4f59\u5bb9\u91cf\u5145\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u6574\u4e2a\u88c5\u8fdb\u80cc\u5305\n res += item.v\n cap -= item.w\n else:\n # \u82e5\u5269\u4f59\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88c5\u8fdb\u80cc\u5305\n res += (item.v / item.w) * cap\n # \u5df2\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u56e0\u6b64\u8df3\u51fa\u5faa\u73af\n break\n return res\n
fractional_knapsack.cpp/* \u7269\u54c1 */\nclass Item {\n public:\n int w; // \u7269\u54c1\u91cd\u91cf\n int v; // \u7269\u54c1\u4ef7\u503c\n\n Item(int w, int v) : w(w), v(v) {\n }\n};\n\n/* \u5206\u6570\u80cc\u5305\uff1a\u8d2a\u5fc3 */\ndouble fractionalKnapsack(vector<int> &wgt, vector<int> &val, int cap) {\n // \u521b\u5efa\u7269\u54c1\u5217\u8868\uff0c\u5305\u542b\u4e24\u4e2a\u5c5e\u6027\uff1a\u91cd\u91cf\u3001\u4ef7\u503c\n vector<Item> items;\n for (int i = 0; i < wgt.size(); i++) {\n items.push_back(Item(wgt[i], val[i]));\n }\n // \u6309\u7167\u5355\u4f4d\u4ef7\u503c item.v / item.w \u4ece\u9ad8\u5230\u4f4e\u8fdb\u884c\u6392\u5e8f\n sort(items.begin(), items.end(), [](Item &a, Item &b) { return (double)a.v / a.w > (double)b.v / b.w; });\n // \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\n double res = 0;\n for (auto &item : items) {\n if (item.w <= cap) {\n // \u82e5\u5269\u4f59\u5bb9\u91cf\u5145\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u6574\u4e2a\u88c5\u8fdb\u80cc\u5305\n res += item.v;\n cap -= item.w;\n } else {\n // \u82e5\u5269\u4f59\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88c5\u8fdb\u80cc\u5305\n res += (double)item.v / item.w * cap;\n // \u5df2\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u56e0\u6b64\u8df3\u51fa\u5faa\u73af\n break;\n }\n }\n return res;\n}\n
fractional_knapsack.java/* \u7269\u54c1 */\nclass Item {\n int w; // \u7269\u54c1\u91cd\u91cf\n int v; // \u7269\u54c1\u4ef7\u503c\n\n public Item(int w, int v) {\n this.w = w;\n this.v = v;\n }\n}\n\n/* \u5206\u6570\u80cc\u5305\uff1a\u8d2a\u5fc3 */\ndouble fractionalKnapsack(int[] wgt, int[] val, int cap) {\n // \u521b\u5efa\u7269\u54c1\u5217\u8868\uff0c\u5305\u542b\u4e24\u4e2a\u5c5e\u6027\uff1a\u91cd\u91cf\u3001\u4ef7\u503c\n Item[] items = new Item[wgt.length];\n for (int i = 0; i < wgt.length; i++) {\n items[i] = new Item(wgt[i], val[i]);\n }\n // \u6309\u7167\u5355\u4f4d\u4ef7\u503c item.v / item.w \u4ece\u9ad8\u5230\u4f4e\u8fdb\u884c\u6392\u5e8f\n Arrays.sort(items, Comparator.comparingDouble(item -> -((double) item.v / item.w)));\n // \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\n double res = 0;\n for (Item item : items) {\n if (item.w <= cap) {\n // \u82e5\u5269\u4f59\u5bb9\u91cf\u5145\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u6574\u4e2a\u88c5\u8fdb\u80cc\u5305\n res += item.v;\n cap -= item.w;\n } else {\n // \u82e5\u5269\u4f59\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88c5\u8fdb\u80cc\u5305\n res += (double) item.v / item.w * cap;\n // \u5df2\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u56e0\u6b64\u8df3\u51fa\u5faa\u73af\n break;\n }\n }\n return res;\n}\n
fractional_knapsack.cs/* \u7269\u54c1 */\nclass Item(int w, int v) {\n public int w = w; // \u7269\u54c1\u91cd\u91cf\n public int v = v; // \u7269\u54c1\u4ef7\u503c\n}\n\n/* \u5206\u6570\u80cc\u5305\uff1a\u8d2a\u5fc3 */\ndouble FractionalKnapsack(int[] wgt, int[] val, int cap) {\n // \u521b\u5efa\u7269\u54c1\u5217\u8868\uff0c\u5305\u542b\u4e24\u4e2a\u5c5e\u6027\uff1a\u91cd\u91cf\u3001\u4ef7\u503c\n Item[] items = new Item[wgt.Length];\n for (int i = 0; i < wgt.Length; i++) {\n items[i] = new Item(wgt[i], val[i]);\n }\n // \u6309\u7167\u5355\u4f4d\u4ef7\u503c item.v / item.w \u4ece\u9ad8\u5230\u4f4e\u8fdb\u884c\u6392\u5e8f\n Array.Sort(items, (x, y) => (y.v / y.w).CompareTo(x.v / x.w));\n // \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\n double res = 0;\n foreach (Item item in items) {\n if (item.w <= cap) {\n // \u82e5\u5269\u4f59\u5bb9\u91cf\u5145\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u6574\u4e2a\u88c5\u8fdb\u80cc\u5305\n res += item.v;\n cap -= item.w;\n } else {\n // \u82e5\u5269\u4f59\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88c5\u8fdb\u80cc\u5305\n res += (double)item.v / item.w * cap;\n // \u5df2\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u56e0\u6b64\u8df3\u51fa\u5faa\u73af\n break;\n }\n }\n return res;\n}\n
fractional_knapsack.go/* \u7269\u54c1 */\ntype Item struct {\n w int // \u7269\u54c1\u91cd\u91cf\n v int // \u7269\u54c1\u4ef7\u503c\n}\n\n/* \u5206\u6570\u80cc\u5305\uff1a\u8d2a\u5fc3 */\nfunc fractionalKnapsack(wgt []int, val []int, cap int) float64 {\n // \u521b\u5efa\u7269\u54c1\u5217\u8868\uff0c\u5305\u542b\u4e24\u4e2a\u5c5e\u6027\uff1a\u91cd\u91cf\u3001\u4ef7\u503c\n items := make([]Item, len(wgt))\n for i := 0; i < len(wgt); i++ {\n items[i] = Item{wgt[i], val[i]}\n }\n // \u6309\u7167\u5355\u4f4d\u4ef7\u503c item.v / item.w \u4ece\u9ad8\u5230\u4f4e\u8fdb\u884c\u6392\u5e8f\n sort.Slice(items, func(i, j int) bool {\n return float64(items[i].v)/float64(items[i].w) > float64(items[j].v)/float64(items[j].w)\n })\n // \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\n res := 0.0\n for _, item := range items {\n if item.w <= cap {\n // \u82e5\u5269\u4f59\u5bb9\u91cf\u5145\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u6574\u4e2a\u88c5\u8fdb\u80cc\u5305\n res += float64(item.v)\n cap -= item.w\n } else {\n // \u82e5\u5269\u4f59\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88c5\u8fdb\u80cc\u5305\n res += float64(item.v) / float64(item.w) * float64(cap)\n // \u5df2\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u56e0\u6b64\u8df3\u51fa\u5faa\u73af\n break\n }\n }\n return res\n}\n
fractional_knapsack.swift/* \u7269\u54c1 */\nclass Item {\n var w: Int // \u7269\u54c1\u91cd\u91cf\n var v: Int // \u7269\u54c1\u4ef7\u503c\n\n init(w: Int, v: Int) {\n self.w = w\n self.v = v\n }\n}\n\n/* \u5206\u6570\u80cc\u5305\uff1a\u8d2a\u5fc3 */\nfunc fractionalKnapsack(wgt: [Int], val: [Int], cap: Int) -> Double {\n // \u521b\u5efa\u7269\u54c1\u5217\u8868\uff0c\u5305\u542b\u4e24\u4e2a\u5c5e\u6027\uff1a\u91cd\u91cf\u3001\u4ef7\u503c\n var items = zip(wgt, val).map { Item(w: $0, v: $1) }\n // \u6309\u7167\u5355\u4f4d\u4ef7\u503c item.v / item.w \u4ece\u9ad8\u5230\u4f4e\u8fdb\u884c\u6392\u5e8f\n items.sort(by: { -(Double($0.v) / Double($0.w)) < -(Double($1.v) / Double($1.w)) })\n // \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\n var res = 0.0\n var cap = cap\n for item in items {\n if item.w <= cap {\n // \u82e5\u5269\u4f59\u5bb9\u91cf\u5145\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u6574\u4e2a\u88c5\u8fdb\u80cc\u5305\n res += Double(item.v)\n cap -= item.w\n } else {\n // \u82e5\u5269\u4f59\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88c5\u8fdb\u80cc\u5305\n res += Double(item.v) / Double(item.w) * Double(cap)\n // \u5df2\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u56e0\u6b64\u8df3\u51fa\u5faa\u73af\n break\n }\n }\n return res\n}\n
fractional_knapsack.js/* \u7269\u54c1 */\nclass Item {\n constructor(w, v) {\n this.w = w; // \u7269\u54c1\u91cd\u91cf\n this.v = v; // \u7269\u54c1\u4ef7\u503c\n }\n}\n\n/* \u5206\u6570\u80cc\u5305\uff1a\u8d2a\u5fc3 */\nfunction fractionalKnapsack(wgt, val, cap) {\n // \u521b\u5efa\u7269\u54c1\u5217\u8868\uff0c\u5305\u542b\u4e24\u4e2a\u5c5e\u6027\uff1a\u91cd\u91cf\u3001\u4ef7\u503c\n const items = wgt.map((w, i) => new Item(w, val[i]));\n // \u6309\u7167\u5355\u4f4d\u4ef7\u503c item.v / item.w \u4ece\u9ad8\u5230\u4f4e\u8fdb\u884c\u6392\u5e8f\n items.sort((a, b) => b.v / b.w - a.v / a.w);\n // \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\n let res = 0;\n for (const item of items) {\n if (item.w <= cap) {\n // \u82e5\u5269\u4f59\u5bb9\u91cf\u5145\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u6574\u4e2a\u88c5\u8fdb\u80cc\u5305\n res += item.v;\n cap -= item.w;\n } else {\n // \u82e5\u5269\u4f59\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88c5\u8fdb\u80cc\u5305\n res += (item.v / item.w) * cap;\n // \u5df2\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u56e0\u6b64\u8df3\u51fa\u5faa\u73af\n break;\n }\n }\n return res;\n}\n
fractional_knapsack.ts/* \u7269\u54c1 */\nclass Item {\n w: number; // \u7269\u54c1\u91cd\u91cf\n v: number; // \u7269\u54c1\u4ef7\u503c\n\n constructor(w: number, v: number) {\n this.w = w;\n this.v = v;\n }\n}\n\n/* \u5206\u6570\u80cc\u5305\uff1a\u8d2a\u5fc3 */\nfunction fractionalKnapsack(wgt: number[], val: number[], cap: number): number {\n // \u521b\u5efa\u7269\u54c1\u5217\u8868\uff0c\u5305\u542b\u4e24\u4e2a\u5c5e\u6027\uff1a\u91cd\u91cf\u3001\u4ef7\u503c\n const items: Item[] = wgt.map((w, i) => new Item(w, val[i]));\n // \u6309\u7167\u5355\u4f4d\u4ef7\u503c item.v / item.w \u4ece\u9ad8\u5230\u4f4e\u8fdb\u884c\u6392\u5e8f\n items.sort((a, b) => b.v / b.w - a.v / a.w);\n // \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\n let res = 0;\n for (const item of items) {\n if (item.w <= cap) {\n // \u82e5\u5269\u4f59\u5bb9\u91cf\u5145\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u6574\u4e2a\u88c5\u8fdb\u80cc\u5305\n res += item.v;\n cap -= item.w;\n } else {\n // \u82e5\u5269\u4f59\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88c5\u8fdb\u80cc\u5305\n res += (item.v / item.w) * cap;\n // \u5df2\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u56e0\u6b64\u8df3\u51fa\u5faa\u73af\n break;\n }\n }\n return res;\n}\n
fractional_knapsack.dart/* \u7269\u54c1 */\nclass Item {\n int w; // \u7269\u54c1\u91cd\u91cf\n int v; // \u7269\u54c1\u4ef7\u503c\n\n Item(this.w, this.v);\n}\n\n/* \u5206\u6570\u80cc\u5305\uff1a\u8d2a\u5fc3 */\ndouble fractionalKnapsack(List<int> wgt, List<int> val, int cap) {\n // \u521b\u5efa\u7269\u54c1\u5217\u8868\uff0c\u5305\u542b\u4e24\u4e2a\u5c5e\u6027\uff1a\u91cd\u91cf\u3001\u4ef7\u503c\n List<Item> items = List.generate(wgt.length, (i) => Item(wgt[i], val[i]));\n // \u6309\u7167\u5355\u4f4d\u4ef7\u503c item.v / item.w \u4ece\u9ad8\u5230\u4f4e\u8fdb\u884c\u6392\u5e8f\n items.sort((a, b) => (b.v / b.w).compareTo(a.v / a.w));\n // \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\n double res = 0;\n for (Item item in items) {\n if (item.w <= cap) {\n // \u82e5\u5269\u4f59\u5bb9\u91cf\u5145\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u6574\u4e2a\u88c5\u8fdb\u80cc\u5305\n res += item.v;\n cap -= item.w;\n } else {\n // \u82e5\u5269\u4f59\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88c5\u8fdb\u80cc\u5305\n res += item.v / item.w * cap;\n // \u5df2\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u56e0\u6b64\u8df3\u51fa\u5faa\u73af\n break;\n }\n }\n return res;\n}\n
fractional_knapsack.rs/* \u7269\u54c1 */\nstruct Item {\n w: i32, // \u7269\u54c1\u91cd\u91cf\n v: i32, // \u7269\u54c1\u4ef7\u503c\n}\n\nimpl Item {\n fn new(w: i32, v: i32) -> Self {\n Self { w, v }\n }\n}\n\n/* \u5206\u6570\u80cc\u5305\uff1a\u8d2a\u5fc3 */\nfn fractional_knapsack(wgt: &[i32], val: &[i32], mut cap: i32) -> f64 {\n // \u521b\u5efa\u7269\u54c1\u5217\u8868\uff0c\u5305\u542b\u4e24\u4e2a\u5c5e\u6027\uff1a\u91cd\u91cf\u3001\u4ef7\u503c\n let mut items = wgt\n .iter()\n .zip(val.iter())\n .map(|(&w, &v)| Item::new(w, v))\n .collect::<Vec<Item>>();\n // \u6309\u7167\u5355\u4f4d\u4ef7\u503c item.v / item.w \u4ece\u9ad8\u5230\u4f4e\u8fdb\u884c\u6392\u5e8f\n items.sort_by(|a, b| {\n (b.v as f64 / b.w as f64)\n .partial_cmp(&(a.v as f64 / a.w as f64))\n .unwrap()\n });\n // \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\n let mut res = 0.0;\n for item in &items {\n if item.w <= cap {\n // \u82e5\u5269\u4f59\u5bb9\u91cf\u5145\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u6574\u4e2a\u88c5\u8fdb\u80cc\u5305\n res += item.v as f64;\n cap -= item.w;\n } else {\n // \u82e5\u5269\u4f59\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88c5\u8fdb\u80cc\u5305\n res += item.v as f64 / item.w as f64 * cap as f64;\n // \u5df2\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u56e0\u6b64\u8df3\u51fa\u5faa\u73af\n break;\n }\n }\n res\n}\n
fractional_knapsack.c/* \u7269\u54c1 */\ntypedef struct {\n int w; // \u7269\u54c1\u91cd\u91cf\n int v; // \u7269\u54c1\u4ef7\u503c\n} Item;\n\n/* \u5206\u6570\u80cc\u5305\uff1a\u8d2a\u5fc3 */\nfloat fractionalKnapsack(int wgt[], int val[], int itemCount, int cap) {\n // \u521b\u5efa\u7269\u54c1\u5217\u8868\uff0c\u5305\u542b\u4e24\u4e2a\u5c5e\u6027\uff1a\u91cd\u91cf\u3001\u4ef7\u503c\n Item *items = malloc(sizeof(Item) * itemCount);\n for (int i = 0; i < itemCount; i++) {\n items[i] = (Item){.w = wgt[i], .v = val[i]};\n }\n // \u6309\u7167\u5355\u4f4d\u4ef7\u503c item.v / item.w \u4ece\u9ad8\u5230\u4f4e\u8fdb\u884c\u6392\u5e8f\n qsort(items, (size_t)itemCount, sizeof(Item), sortByValueDensity);\n // \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\n float res = 0.0;\n for (int i = 0; i < itemCount; i++) {\n if (items[i].w <= cap) {\n // \u82e5\u5269\u4f59\u5bb9\u91cf\u5145\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u6574\u4e2a\u88c5\u8fdb\u80cc\u5305\n res += items[i].v;\n cap -= items[i].w;\n } else {\n // \u82e5\u5269\u4f59\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88c5\u8fdb\u80cc\u5305\n res += (float)cap / items[i].w * items[i].v;\n cap = 0;\n break;\n }\n }\n free(items);\n return res;\n}\n
fractional_knapsack.zig[class]{Item}-[func]{}\n\n[class]{}-[func]{fractionalKnapsack}\n
\u5728\u6700\u5dee\u60c5\u51b5\u4e0b\uff0c\u9700\u8981\u904d\u5386\u6574\u4e2a\u7269\u54c1\u5217\u8868\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \uff0c\u5176\u4e2d \\(n\\) \u4e3a\u7269\u54c1\u6570\u91cf\u3002
\u7531\u4e8e\u521d\u59cb\u5316\u4e86\u4e00\u4e2a Item
\u5bf9\u8c61\u5217\u8868\uff0c\u56e0\u6b64\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002
\u91c7\u7528\u53cd\u8bc1\u6cd5\u3002\u5047\u8bbe\u7269\u54c1 \\(x\\) \u662f\u5355\u4f4d\u4ef7\u503c\u6700\u9ad8\u7684\u7269\u54c1\uff0c\u4f7f\u7528\u67d0\u7b97\u6cd5\u6c42\u5f97\u6700\u5927\u4ef7\u503c\u4e3a res
\uff0c\u4f46\u8be5\u89e3\u4e2d\u4e0d\u5305\u542b\u7269\u54c1 \\(x\\) \u3002
\u73b0\u5728\u4ece\u80cc\u5305\u4e2d\u62ff\u51fa\u5355\u4f4d\u91cd\u91cf\u7684\u4efb\u610f\u7269\u54c1\uff0c\u5e76\u66ff\u6362\u4e3a\u5355\u4f4d\u91cd\u91cf\u7684\u7269\u54c1 \\(x\\) \u3002\u7531\u4e8e\u7269\u54c1 \\(x\\) \u7684\u5355\u4f4d\u4ef7\u503c\u6700\u9ad8\uff0c\u56e0\u6b64\u66ff\u6362\u540e\u7684\u603b\u4ef7\u503c\u4e00\u5b9a\u5927\u4e8e res
\u3002\u8fd9\u4e0e res
\u662f\u6700\u4f18\u89e3\u77db\u76fe\uff0c\u8bf4\u660e\u6700\u4f18\u89e3\u4e2d\u5fc5\u987b\u5305\u542b\u7269\u54c1 \\(x\\) \u3002
\u5bf9\u4e8e\u8be5\u89e3\u4e2d\u7684\u5176\u4ed6\u7269\u54c1\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u6784\u5efa\u51fa\u4e0a\u8ff0\u77db\u76fe\u3002\u603b\u800c\u8a00\u4e4b\uff0c\u5355\u4f4d\u4ef7\u503c\u66f4\u5927\u7684\u7269\u54c1\u603b\u662f\u66f4\u4f18\u9009\u62e9\uff0c\u8fd9\u8bf4\u660e\u8d2a\u5fc3\u7b56\u7565\u662f\u6709\u6548\u7684\u3002
\u5982\u56fe 15-6 \u6240\u793a\uff0c\u5982\u679c\u5c06\u7269\u54c1\u91cd\u91cf\u548c\u7269\u54c1\u5355\u4f4d\u4ef7\u503c\u5206\u522b\u770b\u4f5c\u4e00\u5f20\u4e8c\u7ef4\u56fe\u8868\u7684\u6a2a\u8f74\u548c\u7eb5\u8f74\uff0c\u5219\u5206\u6570\u80cc\u5305\u95ee\u9898\u53ef\u8f6c\u5316\u4e3a\u201c\u6c42\u5728\u6709\u9650\u6a2a\u8f74\u533a\u95f4\u4e0b\u7684\u6700\u5927\u56f4\u6210\u9762\u79ef\u201d\u3002\u8fd9\u4e2a\u7c7b\u6bd4\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u4ece\u51e0\u4f55\u89d2\u5ea6\u7406\u89e3\u8d2a\u5fc3\u7b56\u7565\u7684\u6709\u6548\u6027\u3002
\u56fe 15-6 \u00a0 \u5206\u6570\u80cc\u5305\u95ee\u9898\u7684\u51e0\u4f55\u8868\u793a
"},{"location":"chapter_greedy/greedy_algorithm/","title":"15.1 \u00a0 \u8d2a\u5fc3\u7b97\u6cd5","text":"\u300c\u8d2a\u5fc3\u7b97\u6cd5 greedy algorithm\u300d\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u89e3\u51b3\u4f18\u5316\u95ee\u9898\u7684\u7b97\u6cd5\uff0c\u5176\u57fa\u672c\u601d\u60f3\u662f\u5728\u95ee\u9898\u7684\u6bcf\u4e2a\u51b3\u7b56\u9636\u6bb5\uff0c\u90fd\u9009\u62e9\u5f53\u524d\u770b\u8d77\u6765\u6700\u4f18\u7684\u9009\u62e9\uff0c\u5373\u8d2a\u5fc3\u5730\u505a\u51fa\u5c40\u90e8\u6700\u4f18\u7684\u51b3\u7b56\uff0c\u4ee5\u671f\u83b7\u5f97\u5168\u5c40\u6700\u4f18\u89e3\u3002\u8d2a\u5fc3\u7b97\u6cd5\u7b80\u6d01\u4e14\u9ad8\u6548\uff0c\u5728\u8bb8\u591a\u5b9e\u9645\u95ee\u9898\u4e2d\u6709\u7740\u5e7f\u6cdb\u7684\u5e94\u7528\u3002
\u8d2a\u5fc3\u7b97\u6cd5\u548c\u52a8\u6001\u89c4\u5212\u90fd\u5e38\u7528\u4e8e\u89e3\u51b3\u4f18\u5316\u95ee\u9898\u3002\u5b83\u4eec\u4e4b\u95f4\u5b58\u5728\u4e00\u4e9b\u76f8\u4f3c\u4e4b\u5904\uff0c\u6bd4\u5982\u90fd\u4f9d\u8d56\u6700\u4f18\u5b50\u7ed3\u6784\u6027\u8d28\uff0c\u4f46\u5de5\u4f5c\u539f\u7406\u4e0d\u540c\u3002
\u6211\u4eec\u5148\u901a\u8fc7\u4f8b\u9898\u201c\u96f6\u94b1\u5151\u6362\u201d\u4e86\u89e3\u8d2a\u5fc3\u7b97\u6cd5\u7684\u5de5\u4f5c\u539f\u7406\u3002\u8fd9\u9053\u9898\u5df2\u7ecf\u5728\u201c\u5b8c\u5168\u80cc\u5305\u95ee\u9898\u201d\u7ae0\u8282\u4e2d\u4ecb\u7ecd\u8fc7\uff0c\u76f8\u4fe1\u4f60\u5bf9\u5b83\u5e76\u4e0d\u964c\u751f\u3002
Question
\u7ed9\u5b9a \\(n\\) \u79cd\u786c\u5e01\uff0c\u7b2c \\(i\\) \u79cd\u786c\u5e01\u7684\u9762\u503c\u4e3a \\(coins[i - 1]\\) \uff0c\u76ee\u6807\u91d1\u989d\u4e3a \\(amt\\) \uff0c\u6bcf\u79cd\u786c\u5e01\u53ef\u4ee5\u91cd\u590d\u9009\u53d6\uff0c\u95ee\u80fd\u591f\u51d1\u51fa\u76ee\u6807\u91d1\u989d\u7684\u6700\u5c11\u786c\u5e01\u6570\u91cf\u3002\u5982\u679c\u65e0\u6cd5\u51d1\u51fa\u76ee\u6807\u91d1\u989d\u5219\u8fd4\u56de \\(-1\\) \u3002
\u672c\u9898\u91c7\u53d6\u7684\u8d2a\u5fc3\u7b56\u7565\u5982\u56fe 15-1 \u6240\u793a\u3002\u7ed9\u5b9a\u76ee\u6807\u91d1\u989d\uff0c\u6211\u4eec\u8d2a\u5fc3\u5730\u9009\u62e9\u4e0d\u5927\u4e8e\u4e14\u6700\u63a5\u8fd1\u5b83\u7684\u786c\u5e01\uff0c\u4e0d\u65ad\u5faa\u73af\u8be5\u6b65\u9aa4\uff0c\u76f4\u81f3\u51d1\u51fa\u76ee\u6807\u91d1\u989d\u4e3a\u6b62\u3002
\u56fe 15-1 \u00a0 \u96f6\u94b1\u5151\u6362\u7684\u8d2a\u5fc3\u7b56\u7565
\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\u6240\u793a\u3002\u4f60\u53ef\u80fd\u4f1a\u4e0d\u7531\u5730\u53d1\u51fa\u611f\u53f9\uff1aSo clean \uff01\u8d2a\u5fc3\u7b97\u6cd5\u4ec5\u7528\u7ea6\u5341\u884c\u4ee3\u7801\u5c31\u89e3\u51b3\u4e86\u96f6\u94b1\u5151\u6362\u95ee\u9898\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig coin_change_greedy.pydef coin_change_greedy(coins: list[int], amt: int) -> int:\n \"\"\"\u96f6\u94b1\u5151\u6362\uff1a\u8d2a\u5fc3\"\"\"\n # \u5047\u8bbe coins \u5217\u8868\u6709\u5e8f\n i = len(coins) - 1\n count = 0\n # \u5faa\u73af\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u5230\u65e0\u5269\u4f59\u91d1\u989d\n while amt > 0:\n # \u627e\u5230\u5c0f\u4e8e\u4e14\u6700\u63a5\u8fd1\u5269\u4f59\u91d1\u989d\u7684\u786c\u5e01\n while i > 0 and coins[i] > amt:\n i -= 1\n # \u9009\u62e9 coins[i]\n amt -= coins[i]\n count += 1\n # \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5219\u8fd4\u56de -1\n return count if amt == 0 else -1\n
coin_change_greedy.cpp/* \u96f6\u94b1\u5151\u6362\uff1a\u8d2a\u5fc3 */\nint coinChangeGreedy(vector<int> &coins, int amt) {\n // \u5047\u8bbe coins \u5217\u8868\u6709\u5e8f\n int i = coins.size() - 1;\n int count = 0;\n // \u5faa\u73af\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u5230\u65e0\u5269\u4f59\u91d1\u989d\n while (amt > 0) {\n // \u627e\u5230\u5c0f\u4e8e\u4e14\u6700\u63a5\u8fd1\u5269\u4f59\u91d1\u989d\u7684\u786c\u5e01\n while (i > 0 && coins[i] > amt) {\n i--;\n }\n // \u9009\u62e9 coins[i]\n amt -= coins[i];\n count++;\n }\n // \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5219\u8fd4\u56de -1\n return amt == 0 ? count : -1;\n}\n
coin_change_greedy.java/* \u96f6\u94b1\u5151\u6362\uff1a\u8d2a\u5fc3 */\nint coinChangeGreedy(int[] coins, int amt) {\n // \u5047\u8bbe coins \u5217\u8868\u6709\u5e8f\n int i = coins.length - 1;\n int count = 0;\n // \u5faa\u73af\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u5230\u65e0\u5269\u4f59\u91d1\u989d\n while (amt > 0) {\n // \u627e\u5230\u5c0f\u4e8e\u4e14\u6700\u63a5\u8fd1\u5269\u4f59\u91d1\u989d\u7684\u786c\u5e01\n while (i > 0 && coins[i] > amt) {\n i--;\n }\n // \u9009\u62e9 coins[i]\n amt -= coins[i];\n count++;\n }\n // \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5219\u8fd4\u56de -1\n return amt == 0 ? count : -1;\n}\n
coin_change_greedy.cs/* \u96f6\u94b1\u5151\u6362\uff1a\u8d2a\u5fc3 */\nint CoinChangeGreedy(int[] coins, int amt) {\n // \u5047\u8bbe coins \u5217\u8868\u6709\u5e8f\n int i = coins.Length - 1;\n int count = 0;\n // \u5faa\u73af\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u5230\u65e0\u5269\u4f59\u91d1\u989d\n while (amt > 0) {\n // \u627e\u5230\u5c0f\u4e8e\u4e14\u6700\u63a5\u8fd1\u5269\u4f59\u91d1\u989d\u7684\u786c\u5e01\n while (i > 0 && coins[i] > amt) {\n i--;\n }\n // \u9009\u62e9 coins[i]\n amt -= coins[i];\n count++;\n }\n // \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5219\u8fd4\u56de -1\n return amt == 0 ? count : -1;\n}\n
coin_change_greedy.go/* \u96f6\u94b1\u5151\u6362\uff1a\u8d2a\u5fc3 */\nfunc coinChangeGreedy(coins []int, amt int) int {\n // \u5047\u8bbe coins \u5217\u8868\u6709\u5e8f\n i := len(coins) - 1\n count := 0\n // \u5faa\u73af\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u5230\u65e0\u5269\u4f59\u91d1\u989d\n for amt > 0 {\n // \u627e\u5230\u5c0f\u4e8e\u4e14\u6700\u63a5\u8fd1\u5269\u4f59\u91d1\u989d\u7684\u786c\u5e01\n for i > 0 && coins[i] > amt {\n i--\n }\n // \u9009\u62e9 coins[i]\n amt -= coins[i]\n count++\n }\n // \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5219\u8fd4\u56de -1\n if amt != 0 {\n return -1\n }\n return count\n}\n
coin_change_greedy.swift/* \u96f6\u94b1\u5151\u6362\uff1a\u8d2a\u5fc3 */\nfunc coinChangeGreedy(coins: [Int], amt: Int) -> Int {\n // \u5047\u8bbe coins \u5217\u8868\u6709\u5e8f\n var i = coins.count - 1\n var count = 0\n var amt = amt\n // \u5faa\u73af\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u5230\u65e0\u5269\u4f59\u91d1\u989d\n while amt > 0 {\n // \u627e\u5230\u5c0f\u4e8e\u4e14\u6700\u63a5\u8fd1\u5269\u4f59\u91d1\u989d\u7684\u786c\u5e01\n while i > 0 && coins[i] > amt {\n i -= 1\n }\n // \u9009\u62e9 coins[i]\n amt -= coins[i]\n count += 1\n }\n // \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5219\u8fd4\u56de -1\n return amt == 0 ? count : -1\n}\n
coin_change_greedy.js/* \u96f6\u94b1\u5151\u6362\uff1a\u8d2a\u5fc3 */\nfunction coinChangeGreedy(coins, amt) {\n // \u5047\u8bbe coins \u6570\u7ec4\u6709\u5e8f\n let i = coins.length - 1;\n let count = 0;\n // \u5faa\u73af\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u5230\u65e0\u5269\u4f59\u91d1\u989d\n while (amt > 0) {\n // \u627e\u5230\u5c0f\u4e8e\u4e14\u6700\u63a5\u8fd1\u5269\u4f59\u91d1\u989d\u7684\u786c\u5e01\n while (i > 0 && coins[i] > amt) {\n i--;\n }\n // \u9009\u62e9 coins[i]\n amt -= coins[i];\n count++;\n }\n // \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5219\u8fd4\u56de -1\n return amt === 0 ? count : -1;\n}\n
coin_change_greedy.ts/* \u96f6\u94b1\u5151\u6362\uff1a\u8d2a\u5fc3 */\nfunction coinChangeGreedy(coins: number[], amt: number): number {\n // \u5047\u8bbe coins \u6570\u7ec4\u6709\u5e8f\n let i = coins.length - 1;\n let count = 0;\n // \u5faa\u73af\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u5230\u65e0\u5269\u4f59\u91d1\u989d\n while (amt > 0) {\n // \u627e\u5230\u5c0f\u4e8e\u4e14\u6700\u63a5\u8fd1\u5269\u4f59\u91d1\u989d\u7684\u786c\u5e01\n while (i > 0 && coins[i] > amt) {\n i--;\n }\n // \u9009\u62e9 coins[i]\n amt -= coins[i];\n count++;\n }\n // \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5219\u8fd4\u56de -1\n return amt === 0 ? count : -1;\n}\n
coin_change_greedy.dart/* \u96f6\u94b1\u5151\u6362\uff1a\u8d2a\u5fc3 */\nint coinChangeGreedy(List<int> coins, int amt) {\n // \u5047\u8bbe coins \u5217\u8868\u6709\u5e8f\n int i = coins.length - 1;\n int count = 0;\n // \u5faa\u73af\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u5230\u65e0\u5269\u4f59\u91d1\u989d\n while (amt > 0) {\n // \u627e\u5230\u5c0f\u4e8e\u4e14\u6700\u63a5\u8fd1\u5269\u4f59\u91d1\u989d\u7684\u786c\u5e01\n while (i > 0 && coins[i] > amt) {\n i--;\n }\n // \u9009\u62e9 coins[i]\n amt -= coins[i];\n count++;\n }\n // \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5219\u8fd4\u56de -1\n return amt == 0 ? count : -1;\n}\n
coin_change_greedy.rs/* \u96f6\u94b1\u5151\u6362\uff1a\u8d2a\u5fc3 */\nfn coin_change_greedy(coins: &[i32], mut amt: i32) -> i32 {\n // \u5047\u8bbe coins \u5217\u8868\u6709\u5e8f\n let mut i = coins.len() - 1;\n let mut count = 0;\n // \u5faa\u73af\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u5230\u65e0\u5269\u4f59\u91d1\u989d\n while amt > 0 {\n // \u627e\u5230\u5c0f\u4e8e\u4e14\u6700\u63a5\u8fd1\u5269\u4f59\u91d1\u989d\u7684\u786c\u5e01\n while i > 0 && coins[i] > amt {\n i -= 1;\n }\n // \u9009\u62e9 coins[i]\n amt -= coins[i];\n count += 1;\n }\n // \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5219\u8fd4\u56de -1\n if amt == 0 {\n count\n } else {\n -1\n }\n}\n
coin_change_greedy.c/* \u96f6\u94b1\u5151\u6362\uff1a\u8d2a\u5fc3 */\nint coinChangeGreedy(int *coins, int size, int amt) {\n // \u5047\u8bbe coins \u5217\u8868\u6709\u5e8f\n int i = size - 1;\n int count = 0;\n // \u5faa\u73af\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u5230\u65e0\u5269\u4f59\u91d1\u989d\n while (amt > 0) {\n // \u627e\u5230\u5c0f\u4e8e\u4e14\u6700\u63a5\u8fd1\u5269\u4f59\u91d1\u989d\u7684\u786c\u5e01\n while (i > 0 && coins[i] > amt) {\n i--;\n }\n // \u9009\u62e9 coins[i]\n amt -= coins[i];\n count++;\n }\n // \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5219\u8fd4\u56de -1\n return amt == 0 ? count : -1;\n}\n
coin_change_greedy.zig[class]{}-[func]{coinChangeGreedy}\n
"},{"location":"chapter_greedy/greedy_algorithm/#1511","title":"15.1.1 \u00a0 \u8d2a\u5fc3\u7684\u4f18\u70b9\u4e0e\u5c40\u9650\u6027","text":"\u8d2a\u5fc3\u7b97\u6cd5\u4e0d\u4ec5\u64cd\u4f5c\u76f4\u63a5\u3001\u5b9e\u73b0\u7b80\u5355\uff0c\u800c\u4e14\u901a\u5e38\u6548\u7387\u4e5f\u5f88\u9ad8\u3002\u5728\u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0c\u8bb0\u786c\u5e01\u6700\u5c0f\u9762\u503c\u4e3a \\(\\min(coins)\\) \uff0c\u5219\u8d2a\u5fc3\u9009\u62e9\u6700\u591a\u5faa\u73af \\(amt / \\min(coins)\\) \u6b21\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(amt / \\min(coins))\\) \u3002\u8fd9\u6bd4\u52a8\u6001\u89c4\u5212\u89e3\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6 \\(O(n \\times amt)\\) \u63d0\u5347\u4e86\u4e00\u4e2a\u6570\u91cf\u7ea7\u3002
\u7136\u800c\uff0c\u5bf9\u4e8e\u67d0\u4e9b\u786c\u5e01\u9762\u503c\u7ec4\u5408\uff0c\u8d2a\u5fc3\u7b97\u6cd5\u5e76\u4e0d\u80fd\u627e\u5230\u6700\u4f18\u89e3\u3002\u56fe 15-2 \u7ed9\u51fa\u4e86\u4e24\u4e2a\u793a\u4f8b\u3002
\u56fe 15-2 \u00a0 \u8d2a\u5fc3\u65e0\u6cd5\u627e\u51fa\u6700\u4f18\u89e3\u7684\u793a\u4f8b
\u4e5f\u5c31\u662f\u8bf4\uff0c\u5bf9\u4e8e\u96f6\u94b1\u5151\u6362\u95ee\u9898\uff0c\u8d2a\u5fc3\u7b97\u6cd5\u65e0\u6cd5\u4fdd\u8bc1\u627e\u5230\u5168\u5c40\u6700\u4f18\u89e3\uff0c\u5e76\u4e14\u6709\u53ef\u80fd\u627e\u5230\u975e\u5e38\u5dee\u7684\u89e3\u3002\u5b83\u66f4\u9002\u5408\u7528\u52a8\u6001\u89c4\u5212\u89e3\u51b3\u3002
\u4e00\u822c\u60c5\u51b5\u4e0b\uff0c\u8d2a\u5fc3\u7b97\u6cd5\u7684\u9002\u7528\u60c5\u51b5\u5206\u4ee5\u4e0b\u4e24\u79cd\u3002
\u90a3\u4e48\u95ee\u9898\u6765\u4e86\uff0c\u4ec0\u4e48\u6837\u7684\u95ee\u9898\u9002\u5408\u7528\u8d2a\u5fc3\u7b97\u6cd5\u6c42\u89e3\u5462\uff1f\u6216\u8005\u8bf4\uff0c\u8d2a\u5fc3\u7b97\u6cd5\u5728\u4ec0\u4e48\u60c5\u51b5\u4e0b\u53ef\u4ee5\u4fdd\u8bc1\u627e\u5230\u6700\u4f18\u89e3\uff1f
\u76f8\u8f83\u4e8e\u52a8\u6001\u89c4\u5212\uff0c\u8d2a\u5fc3\u7b97\u6cd5\u7684\u4f7f\u7528\u6761\u4ef6\u66f4\u52a0\u82db\u523b\uff0c\u5176\u4e3b\u8981\u5173\u6ce8\u95ee\u9898\u7684\u4e24\u4e2a\u6027\u8d28\u3002
\u6700\u4f18\u5b50\u7ed3\u6784\u5df2\u7ecf\u5728\u201c\u52a8\u6001\u89c4\u5212\u201d\u7ae0\u8282\u4e2d\u4ecb\u7ecd\u8fc7\uff0c\u8fd9\u91cc\u4e0d\u518d\u8d58\u8ff0\u3002\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u4e00\u4e9b\u95ee\u9898\u7684\u6700\u4f18\u5b50\u7ed3\u6784\u5e76\u4e0d\u660e\u663e\uff0c\u4f46\u4ecd\u7136\u53ef\u4f7f\u7528\u8d2a\u5fc3\u7b97\u6cd5\u89e3\u51b3\u3002
\u6211\u4eec\u4e3b\u8981\u63a2\u7a76\u8d2a\u5fc3\u9009\u62e9\u6027\u8d28\u7684\u5224\u65ad\u65b9\u6cd5\u3002\u867d\u7136\u5b83\u7684\u63cf\u8ff0\u770b\u4e0a\u53bb\u6bd4\u8f83\u7b80\u5355\uff0c\u4f46\u5b9e\u9645\u4e0a\u5bf9\u4e8e\u8bb8\u591a\u95ee\u9898\uff0c\u8bc1\u660e\u8d2a\u5fc3\u9009\u62e9\u6027\u8d28\u5e76\u975e\u6613\u4e8b\u3002
\u4f8b\u5982\u96f6\u94b1\u5151\u6362\u95ee\u9898\uff0c\u6211\u4eec\u867d\u7136\u80fd\u591f\u5bb9\u6613\u5730\u4e3e\u51fa\u53cd\u4f8b\uff0c\u5bf9\u8d2a\u5fc3\u9009\u62e9\u6027\u8d28\u8fdb\u884c\u8bc1\u4f2a\uff0c\u4f46\u8bc1\u5b9e\u7684\u96be\u5ea6\u8f83\u5927\u3002\u5982\u679c\u95ee\uff1a\u6ee1\u8db3\u4ec0\u4e48\u6761\u4ef6\u7684\u786c\u5e01\u7ec4\u5408\u53ef\u4ee5\u4f7f\u7528\u8d2a\u5fc3\u7b97\u6cd5\u6c42\u89e3\uff1f\u6211\u4eec\u5f80\u5f80\u53ea\u80fd\u51ed\u501f\u76f4\u89c9\u6216\u4e3e\u4f8b\u5b50\u6765\u7ed9\u51fa\u4e00\u4e2a\u6a21\u68f1\u4e24\u53ef\u7684\u7b54\u6848\uff0c\u800c\u96be\u4ee5\u7ed9\u51fa\u4e25\u8c28\u7684\u6570\u5b66\u8bc1\u660e\u3002
Quote
\u6709\u4e00\u7bc7\u8bba\u6587\u7ed9\u51fa\u4e86\u4e00\u4e2a \\(O(n^3)\\) \u65f6\u95f4\u590d\u6742\u5ea6\u7684\u7b97\u6cd5\uff0c\u7528\u4e8e\u5224\u65ad\u4e00\u4e2a\u786c\u5e01\u7ec4\u5408\u80fd\u5426\u4f7f\u7528\u8d2a\u5fc3\u7b97\u6cd5\u627e\u51fa\u4efb\u610f\u91d1\u989d\u7684\u6700\u4f18\u89e3\u3002
Pearson, David. A polynomial-time algorithm for the change-making problem. Operations Research Letters 33.3 (2005): 231-234.
"},{"location":"chapter_greedy/greedy_algorithm/#1513","title":"15.1.3 \u00a0 \u8d2a\u5fc3\u89e3\u9898\u6b65\u9aa4","text":"\u8d2a\u5fc3\u95ee\u9898\u7684\u89e3\u51b3\u6d41\u7a0b\u5927\u4f53\u53ef\u5206\u4e3a\u4ee5\u4e0b\u4e09\u6b65\u3002
\u786e\u5b9a\u8d2a\u5fc3\u7b56\u7565\u662f\u6c42\u89e3\u95ee\u9898\u7684\u6838\u5fc3\u6b65\u9aa4\uff0c\u4f46\u5b9e\u65bd\u8d77\u6765\u53ef\u80fd\u5e76\u4e0d\u5bb9\u6613\uff0c\u4e3b\u8981\u6709\u4ee5\u4e0b\u539f\u56e0\u3002
\u4e3a\u4e86\u4fdd\u8bc1\u6b63\u786e\u6027\uff0c\u6211\u4eec\u5e94\u8be5\u5bf9\u8d2a\u5fc3\u7b56\u7565\u8fdb\u884c\u4e25\u8c28\u7684\u6570\u5b66\u8bc1\u660e\uff0c\u901a\u5e38\u9700\u8981\u7528\u5230\u53cd\u8bc1\u6cd5\u6216\u6570\u5b66\u5f52\u7eb3\u6cd5\u3002
\u7136\u800c\uff0c\u6b63\u786e\u6027\u8bc1\u660e\u4e5f\u5f88\u53ef\u80fd\u4e0d\u662f\u4e00\u4ef6\u6613\u4e8b\u3002\u5982\u82e5\u6ca1\u6709\u5934\u7eea\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u9009\u62e9\u9762\u5411\u6d4b\u8bd5\u7528\u4f8b\u8fdb\u884c\u4ee3\u7801\u8c03\u8bd5\uff0c\u4e00\u6b65\u6b65\u4fee\u6539\u4e0e\u9a8c\u8bc1\u8d2a\u5fc3\u7b56\u7565\u3002
"},{"location":"chapter_greedy/greedy_algorithm/#1514","title":"15.1.4 \u00a0 \u8d2a\u5fc3\u5178\u578b\u4f8b\u9898","text":"\u8d2a\u5fc3\u7b97\u6cd5\u5e38\u5e38\u5e94\u7528\u5728\u6ee1\u8db3\u8d2a\u5fc3\u9009\u62e9\u6027\u8d28\u548c\u6700\u4f18\u5b50\u7ed3\u6784\u7684\u4f18\u5316\u95ee\u9898\u4e2d\uff0c\u4ee5\u4e0b\u5217\u4e3e\u4e86\u4e00\u4e9b\u5178\u578b\u7684\u8d2a\u5fc3\u7b97\u6cd5\u95ee\u9898\u3002
Question
\u8f93\u5165\u4e00\u4e2a\u6570\u7ec4 \\(ht\\) \uff0c\u5176\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u4ee3\u8868\u4e00\u4e2a\u5782\u76f4\u9694\u677f\u7684\u9ad8\u5ea6\u3002\u6570\u7ec4\u4e2d\u7684\u4efb\u610f\u4e24\u4e2a\u9694\u677f\uff0c\u4ee5\u53ca\u5b83\u4eec\u4e4b\u95f4\u7684\u7a7a\u95f4\u53ef\u4ee5\u7ec4\u6210\u4e00\u4e2a\u5bb9\u5668\u3002
\u5bb9\u5668\u7684\u5bb9\u91cf\u7b49\u4e8e\u9ad8\u5ea6\u548c\u5bbd\u5ea6\u7684\u4e58\u79ef\uff08\u9762\u79ef\uff09\uff0c\u5176\u4e2d\u9ad8\u5ea6\u7531\u8f83\u77ed\u7684\u9694\u677f\u51b3\u5b9a\uff0c\u5bbd\u5ea6\u662f\u4e24\u4e2a\u9694\u677f\u7684\u6570\u7ec4\u7d22\u5f15\u4e4b\u5dee\u3002
\u8bf7\u5728\u6570\u7ec4\u4e2d\u9009\u62e9\u4e24\u4e2a\u9694\u677f\uff0c\u4f7f\u5f97\u7ec4\u6210\u7684\u5bb9\u5668\u7684\u5bb9\u91cf\u6700\u5927\uff0c\u8fd4\u56de\u6700\u5927\u5bb9\u91cf\u3002\u793a\u4f8b\u5982\u56fe 15-7 \u6240\u793a\u3002
\u56fe 15-7 \u00a0 \u6700\u5927\u5bb9\u91cf\u95ee\u9898\u7684\u793a\u4f8b\u6570\u636e
\u5bb9\u5668\u7531\u4efb\u610f\u4e24\u4e2a\u9694\u677f\u56f4\u6210\uff0c\u56e0\u6b64\u672c\u9898\u7684\u72b6\u6001\u4e3a\u4e24\u4e2a\u9694\u677f\u7684\u7d22\u5f15\uff0c\u8bb0\u4e3a \\([i, j]\\) \u3002
\u6839\u636e\u9898\u610f\uff0c\u5bb9\u91cf\u7b49\u4e8e\u9ad8\u5ea6\u4e58\u4ee5\u5bbd\u5ea6\uff0c\u5176\u4e2d\u9ad8\u5ea6\u7531\u77ed\u677f\u51b3\u5b9a\uff0c\u5bbd\u5ea6\u662f\u4e24\u9694\u677f\u7684\u6570\u7ec4\u7d22\u5f15\u4e4b\u5dee\u3002\u8bbe\u5bb9\u91cf\u4e3a \\(cap[i, j]\\) \uff0c\u5219\u53ef\u5f97\u8ba1\u7b97\u516c\u5f0f\uff1a
\\[ cap[i, j] = \\min(ht[i], ht[j]) \\times (j - i) \\]\u8bbe\u6570\u7ec4\u957f\u5ea6\u4e3a \\(n\\) \uff0c\u4e24\u4e2a\u9694\u677f\u7684\u7ec4\u5408\u6570\u91cf\uff08\u72b6\u6001\u603b\u6570\uff09\u4e3a \\(C_n^2 = \\frac{n(n - 1)}{2}\\) \u4e2a\u3002\u6700\u76f4\u63a5\u5730\uff0c\u6211\u4eec\u53ef\u4ee5\u7a77\u4e3e\u6240\u6709\u72b6\u6001\uff0c\u4ece\u800c\u6c42\u5f97\u6700\u5927\u5bb9\u91cf\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\) \u3002
"},{"location":"chapter_greedy/max_capacity_problem/#1","title":"1. \u00a0 \u8d2a\u5fc3\u7b56\u7565\u786e\u5b9a","text":"\u8fd9\u9053\u9898\u8fd8\u6709\u66f4\u9ad8\u6548\u7387\u7684\u89e3\u6cd5\u3002\u5982\u56fe 15-8 \u6240\u793a\uff0c\u73b0\u9009\u53d6\u4e00\u4e2a\u72b6\u6001 \\([i, j]\\) \uff0c\u5176\u6ee1\u8db3\u7d22\u5f15 \\(i < j\\) \u4e14\u9ad8\u5ea6 \\(ht[i] < ht[j]\\) \uff0c\u5373 \\(i\\) \u4e3a\u77ed\u677f\u3001\\(j\\) \u4e3a\u957f\u677f\u3002
\u56fe 15-8 \u00a0 \u521d\u59cb\u72b6\u6001
\u5982\u56fe 15-9 \u6240\u793a\uff0c\u82e5\u6b64\u65f6\u5c06\u957f\u677f \\(j\\) \u5411\u77ed\u677f \\(i\\) \u9760\u8fd1\uff0c\u5219\u5bb9\u91cf\u4e00\u5b9a\u53d8\u5c0f\u3002
\u8fd9\u662f\u56e0\u4e3a\u5728\u79fb\u52a8\u957f\u677f \\(j\\) \u540e\uff0c\u5bbd\u5ea6 \\(j-i\\) \u80af\u5b9a\u53d8\u5c0f\uff1b\u800c\u9ad8\u5ea6\u7531\u77ed\u677f\u51b3\u5b9a\uff0c\u56e0\u6b64\u9ad8\u5ea6\u53ea\u53ef\u80fd\u4e0d\u53d8\uff08 \\(i\\) \u4ecd\u4e3a\u77ed\u677f\uff09\u6216\u53d8\u5c0f\uff08\u79fb\u52a8\u540e\u7684 \\(j\\) \u6210\u4e3a\u77ed\u677f\uff09\u3002
\u56fe 15-9 \u00a0 \u5411\u5185\u79fb\u52a8\u957f\u677f\u540e\u7684\u72b6\u6001
\u53cd\u5411\u601d\u8003\uff0c\u6211\u4eec\u53ea\u6709\u5411\u5185\u6536\u7f29\u77ed\u677f \\(i\\) \uff0c\u624d\u6709\u53ef\u80fd\u4f7f\u5bb9\u91cf\u53d8\u5927\u3002\u56e0\u4e3a\u867d\u7136\u5bbd\u5ea6\u4e00\u5b9a\u53d8\u5c0f\uff0c\u4f46\u9ad8\u5ea6\u53ef\u80fd\u4f1a\u53d8\u5927\uff08\u79fb\u52a8\u540e\u7684\u77ed\u677f \\(i\\) \u53ef\u80fd\u4f1a\u53d8\u957f\uff09\u3002\u4f8b\u5982\u5728\u56fe 15-10 \u4e2d\uff0c\u79fb\u52a8\u77ed\u677f\u540e\u9762\u79ef\u53d8\u5927\u3002
\u56fe 15-10 \u00a0 \u5411\u5185\u79fb\u52a8\u77ed\u677f\u540e\u7684\u72b6\u6001
\u7531\u6b64\u4fbf\u53ef\u63a8\u51fa\u672c\u9898\u7684\u8d2a\u5fc3\u7b56\u7565\uff1a\u521d\u59cb\u5316\u4e24\u6307\u9488\u5206\u5217\u5bb9\u5668\u4e24\u7aef\uff0c\u6bcf\u8f6e\u5411\u5185\u6536\u7f29\u77ed\u677f\u5bf9\u5e94\u7684\u6307\u9488\uff0c\u76f4\u81f3\u4e24\u6307\u9488\u76f8\u9047\u3002
\u56fe 15-11 \u5c55\u793a\u4e86\u8d2a\u5fc3\u7b56\u7565\u7684\u6267\u884c\u8fc7\u7a0b\u3002
2.
\u6b65\u548c\u7b2c 3.
\u6b65\uff0c\u76f4\u81f3 \\(i\\) \u548c \\(j\\) \u76f8\u9047\u65f6\u7ed3\u675f\u3002\u56fe 15-11 \u00a0 \u6700\u5927\u5bb9\u91cf\u95ee\u9898\u7684\u8d2a\u5fc3\u8fc7\u7a0b
"},{"location":"chapter_greedy/max_capacity_problem/#2","title":"2. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"\u4ee3\u7801\u5faa\u73af\u6700\u591a \\(n\\) \u8f6e\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002
\u53d8\u91cf \\(i\\)\u3001\\(j\\)\u3001\\(res\\) \u4f7f\u7528\u5e38\u6570\u5927\u5c0f\u7684\u989d\u5916\u7a7a\u95f4\uff0c\u56e0\u6b64\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(1)\\) \u3002
PythonC++JavaC#GoSwiftJSTSDartRustCZig max_capacity.pydef max_capacity(ht: list[int]) -> int:\n \"\"\"\u6700\u5927\u5bb9\u91cf\uff1a\u8d2a\u5fc3\"\"\"\n # \u521d\u59cb\u5316 i, j \u5206\u5217\u6570\u7ec4\u4e24\u7aef\n i, j = 0, len(ht) - 1\n # \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u4e3a 0\n res = 0\n # \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u81f3\u4e24\u677f\u76f8\u9047\n while i < j:\n # \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\n cap = min(ht[i], ht[j]) * (j - i)\n res = max(res, cap)\n # \u5411\u5185\u79fb\u52a8\u77ed\u677f\n if ht[i] < ht[j]:\n i += 1\n else:\n j -= 1\n return res\n
max_capacity.cpp/* \u6700\u5927\u5bb9\u91cf\uff1a\u8d2a\u5fc3 */\nint maxCapacity(vector<int> &ht) {\n // \u521d\u59cb\u5316 i, j \u5206\u5217\u6570\u7ec4\u4e24\u7aef\n int i = 0, j = ht.size() - 1;\n // \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u4e3a 0\n int res = 0;\n // \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u81f3\u4e24\u677f\u76f8\u9047\n while (i < j) {\n // \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\n int cap = min(ht[i], ht[j]) * (j - i);\n res = max(res, cap);\n // \u5411\u5185\u79fb\u52a8\u77ed\u677f\n if (ht[i] < ht[j]) {\n i++;\n } else {\n j--;\n }\n }\n return res;\n}\n
max_capacity.java/* \u6700\u5927\u5bb9\u91cf\uff1a\u8d2a\u5fc3 */\nint maxCapacity(int[] ht) {\n // \u521d\u59cb\u5316 i, j \u5206\u5217\u6570\u7ec4\u4e24\u7aef\n int i = 0, j = ht.length - 1;\n // \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u4e3a 0\n int res = 0;\n // \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u81f3\u4e24\u677f\u76f8\u9047\n while (i < j) {\n // \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\n int cap = Math.min(ht[i], ht[j]) * (j - i);\n res = Math.max(res, cap);\n // \u5411\u5185\u79fb\u52a8\u77ed\u677f\n if (ht[i] < ht[j]) {\n i++;\n } else {\n j--;\n }\n }\n return res;\n}\n
max_capacity.cs/* \u6700\u5927\u5bb9\u91cf\uff1a\u8d2a\u5fc3 */\nint MaxCapacity(int[] ht) {\n // \u521d\u59cb\u5316 i, j \u5206\u5217\u6570\u7ec4\u4e24\u7aef\n int i = 0, j = ht.Length - 1;\n // \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u4e3a 0\n int res = 0;\n // \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u81f3\u4e24\u677f\u76f8\u9047\n while (i < j) {\n // \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\n int cap = Math.Min(ht[i], ht[j]) * (j - i);\n res = Math.Max(res, cap);\n // \u5411\u5185\u79fb\u52a8\u77ed\u677f\n if (ht[i] < ht[j]) {\n i++;\n } else {\n j--;\n }\n }\n return res;\n}\n
max_capacity.go/* \u6700\u5927\u5bb9\u91cf\uff1a\u8d2a\u5fc3 */\nfunc maxCapacity(ht []int) int {\n // \u521d\u59cb\u5316 i, j \u5206\u5217\u6570\u7ec4\u4e24\u7aef\n i, j := 0, len(ht)-1\n // \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u4e3a 0\n res := 0\n // \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u81f3\u4e24\u677f\u76f8\u9047\n for i < j {\n // \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\n capacity := int(math.Min(float64(ht[i]), float64(ht[j]))) * (j - i)\n res = int(math.Max(float64(res), float64(capacity)))\n // \u5411\u5185\u79fb\u52a8\u77ed\u677f\n if ht[i] < ht[j] {\n i++\n } else {\n j--\n }\n }\n return res\n}\n
max_capacity.swift/* \u6700\u5927\u5bb9\u91cf\uff1a\u8d2a\u5fc3 */\nfunc maxCapacity(ht: [Int]) -> Int {\n // \u521d\u59cb\u5316 i, j \u5206\u5217\u6570\u7ec4\u4e24\u7aef\n var i = 0, j = ht.count - 1\n // \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u4e3a 0\n var res = 0\n // \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u81f3\u4e24\u677f\u76f8\u9047\n while i < j {\n // \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\n let cap = min(ht[i], ht[j]) * (j - i)\n res = max(res, cap)\n // \u5411\u5185\u79fb\u52a8\u77ed\u677f\n if ht[i] < ht[j] {\n i += 1\n } else {\n j -= 1\n }\n }\n return res\n}\n
max_capacity.js/* \u6700\u5927\u5bb9\u91cf\uff1a\u8d2a\u5fc3 */\nfunction maxCapacity(ht) {\n // \u521d\u59cb\u5316 i, j \u5206\u5217\u6570\u7ec4\u4e24\u7aef\n let i = 0,\n j = ht.length - 1;\n // \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u4e3a 0\n let res = 0;\n // \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u81f3\u4e24\u677f\u76f8\u9047\n while (i < j) {\n // \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\n const cap = Math.min(ht[i], ht[j]) * (j - i);\n res = Math.max(res, cap);\n // \u5411\u5185\u79fb\u52a8\u77ed\u677f\n if (ht[i] < ht[j]) {\n i += 1;\n } else {\n j -= 1;\n }\n }\n return res;\n}\n
max_capacity.ts/* \u6700\u5927\u5bb9\u91cf\uff1a\u8d2a\u5fc3 */\nfunction maxCapacity(ht: number[]): number {\n // \u521d\u59cb\u5316 i, j \u5206\u5217\u6570\u7ec4\u4e24\u7aef\n let i = 0,\n j = ht.length - 1;\n // \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u4e3a 0\n let res = 0;\n // \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u81f3\u4e24\u677f\u76f8\u9047\n while (i < j) {\n // \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\n const cap: number = Math.min(ht[i], ht[j]) * (j - i);\n res = Math.max(res, cap);\n // \u5411\u5185\u79fb\u52a8\u77ed\u677f\n if (ht[i] < ht[j]) {\n i += 1;\n } else {\n j -= 1;\n }\n }\n return res;\n}\n
max_capacity.dart/* \u6700\u5927\u5bb9\u91cf\uff1a\u8d2a\u5fc3 */\nint maxCapacity(List<int> ht) {\n // \u521d\u59cb\u5316 i, j \u5206\u5217\u6570\u7ec4\u4e24\u7aef\n int i = 0, j = ht.length - 1;\n // \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u4e3a 0\n int res = 0;\n // \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u81f3\u4e24\u677f\u76f8\u9047\n while (i < j) {\n // \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\n int cap = min(ht[i], ht[j]) * (j - i);\n res = max(res, cap);\n // \u5411\u5185\u79fb\u52a8\u77ed\u677f\n if (ht[i] < ht[j]) {\n i++;\n } else {\n j--;\n }\n }\n return res;\n}\n
max_capacity.rs/* \u6700\u5927\u5bb9\u91cf\uff1a\u8d2a\u5fc3 */\nfn max_capacity(ht: &[i32]) -> i32 {\n // \u521d\u59cb\u5316 i, j \u5206\u5217\u6570\u7ec4\u4e24\u7aef\n let mut i = 0;\n let mut j = ht.len() - 1;\n // \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u4e3a 0\n let mut res = 0;\n // \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u81f3\u4e24\u677f\u76f8\u9047\n while i < j {\n // \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\n let cap = std::cmp::min(ht[i], ht[j]) * (j - i) as i32;\n res = std::cmp::max(res, cap);\n // \u5411\u5185\u79fb\u52a8\u77ed\u677f\n if ht[i] < ht[j] {\n i += 1;\n } else {\n j -= 1;\n }\n }\n res\n}\n
max_capacity.c/* \u6700\u5927\u5bb9\u91cf\uff1a\u8d2a\u5fc3 */\nint maxCapacity(int ht[], int htLength) {\n // \u521d\u59cb\u5316 i, j \u5206\u5217\u6570\u7ec4\u4e24\u7aef\n int i = 0;\n int j = htLength - 1;\n // \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u4e3a 0\n int res = 0;\n // \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u81f3\u4e24\u677f\u76f8\u9047\n while (i < j) {\n // \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\n int capacity = myMin(ht[i], ht[j]) * (j - i);\n res = myMax(res, capacity);\n // \u5411\u5185\u79fb\u52a8\u77ed\u677f\n if (ht[i] < ht[j]) {\n i++;\n } else {\n j--;\n }\n }\n return res;\n}\n
max_capacity.zig[class]{}-[func]{maxCapacity}\n
"},{"location":"chapter_greedy/max_capacity_problem/#3","title":"3. \u00a0 \u6b63\u786e\u6027\u8bc1\u660e","text":"\u4e4b\u6240\u4ee5\u8d2a\u5fc3\u6bd4\u7a77\u4e3e\u66f4\u5feb\uff0c\u662f\u56e0\u4e3a\u6bcf\u8f6e\u7684\u8d2a\u5fc3\u9009\u62e9\u90fd\u4f1a\u201c\u8df3\u8fc7\u201d\u4e00\u4e9b\u72b6\u6001\u3002
\u6bd4\u5982\u5728\u72b6\u6001 \\(cap[i, j]\\) \u4e0b\uff0c\\(i\\) \u4e3a\u77ed\u677f\u3001\\(j\\) \u4e3a\u957f\u677f\u3002\u82e5\u8d2a\u5fc3\u5730\u5c06\u77ed\u677f \\(i\\) \u5411\u5185\u79fb\u52a8\u4e00\u683c\uff0c\u4f1a\u5bfc\u81f4\u56fe 15-12 \u6240\u793a\u7684\u72b6\u6001\u88ab\u201c\u8df3\u8fc7\u201d\u3002\u8fd9\u610f\u5473\u7740\u4e4b\u540e\u65e0\u6cd5\u9a8c\u8bc1\u8fd9\u4e9b\u72b6\u6001\u7684\u5bb9\u91cf\u5927\u5c0f\u3002
\\[ cap[i, i+1], cap[i, i+2], \\dots, cap[i, j-2], cap[i, j-1] \\]\u56fe 15-12 \u00a0 \u79fb\u52a8\u77ed\u677f\u5bfc\u81f4\u88ab\u8df3\u8fc7\u7684\u72b6\u6001
\u89c2\u5bdf\u53d1\u73b0\uff0c\u8fd9\u4e9b\u88ab\u8df3\u8fc7\u7684\u72b6\u6001\u5b9e\u9645\u4e0a\u5c31\u662f\u5c06\u957f\u677f \\(j\\) \u5411\u5185\u79fb\u52a8\u7684\u6240\u6709\u72b6\u6001\u3002\u524d\u9762\u6211\u4eec\u5df2\u7ecf\u8bc1\u660e\u5185\u79fb\u957f\u677f\u4e00\u5b9a\u4f1a\u5bfc\u81f4\u5bb9\u91cf\u53d8\u5c0f\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u88ab\u8df3\u8fc7\u7684\u72b6\u6001\u90fd\u4e0d\u53ef\u80fd\u662f\u6700\u4f18\u89e3\uff0c\u8df3\u8fc7\u5b83\u4eec\u4e0d\u4f1a\u5bfc\u81f4\u9519\u8fc7\u6700\u4f18\u89e3\u3002
\u4ee5\u4e0a\u5206\u6790\u8bf4\u660e\uff0c\u79fb\u52a8\u77ed\u677f\u7684\u64cd\u4f5c\u662f\u201c\u5b89\u5168\u201d\u7684\uff0c\u8d2a\u5fc3\u7b56\u7565\u662f\u6709\u6548\u7684\u3002
"},{"location":"chapter_greedy/max_product_cutting_problem/","title":"15.4 \u00a0 \u6700\u5927\u5207\u5206\u4e58\u79ef\u95ee\u9898","text":"Question
\u7ed9\u5b9a\u4e00\u4e2a\u6b63\u6574\u6570 \\(n\\) \uff0c\u5c06\u5176\u5207\u5206\u4e3a\u81f3\u5c11\u4e24\u4e2a\u6b63\u6574\u6570\u7684\u548c\uff0c\u6c42\u5207\u5206\u540e\u6240\u6709\u6574\u6570\u7684\u4e58\u79ef\u6700\u5927\u662f\u591a\u5c11\uff0c\u5982\u56fe 15-13 \u6240\u793a\u3002
\u56fe 15-13 \u00a0 \u6700\u5927\u5207\u5206\u4e58\u79ef\u7684\u95ee\u9898\u5b9a\u4e49
\u5047\u8bbe\u6211\u4eec\u5c06 \\(n\\) \u5207\u5206\u4e3a \\(m\\) \u4e2a\u6574\u6570\u56e0\u5b50\uff0c\u5176\u4e2d\u7b2c \\(i\\) \u4e2a\u56e0\u5b50\u8bb0\u4e3a \\(n_i\\) \uff0c\u5373
\\[ n = \\sum_{i=1}^{m}n_i \\]\u672c\u9898\u7684\u76ee\u6807\u662f\u6c42\u5f97\u6240\u6709\u6574\u6570\u56e0\u5b50\u7684\u6700\u5927\u4e58\u79ef\uff0c\u5373
\\[ \\max(\\prod_{i=1}^{m}n_i) \\]\u6211\u4eec\u9700\u8981\u601d\u8003\u7684\u662f\uff1a\u5207\u5206\u6570\u91cf \\(m\\) \u5e94\u8be5\u591a\u5927\uff0c\u6bcf\u4e2a \\(n_i\\) \u5e94\u8be5\u662f\u591a\u5c11\uff1f
"},{"location":"chapter_greedy/max_product_cutting_problem/#1","title":"1. \u00a0 \u8d2a\u5fc3\u7b56\u7565\u786e\u5b9a","text":"\u6839\u636e\u7ecf\u9a8c\uff0c\u4e24\u4e2a\u6574\u6570\u7684\u4e58\u79ef\u5f80\u5f80\u6bd4\u5b83\u4eec\u7684\u52a0\u548c\u66f4\u5927\u3002\u5047\u8bbe\u4ece \\(n\\) \u4e2d\u5206\u51fa\u4e00\u4e2a\u56e0\u5b50 \\(2\\) \uff0c\u5219\u5b83\u4eec\u7684\u4e58\u79ef\u4e3a \\(2(n-2)\\) \u3002\u6211\u4eec\u5c06\u8be5\u4e58\u79ef\u4e0e \\(n\\) \u4f5c\u6bd4\u8f83\uff1a
\\[ \\begin{aligned} 2(n-2) & \\geq n \\newline 2n - n - 4 & \\geq 0 \\newline n & \\geq 4 \\end{aligned} \\]\u5982\u56fe 15-14 \u6240\u793a\uff0c\u5f53 \\(n \\geq 4\\) \u65f6\uff0c\u5207\u5206\u51fa\u4e00\u4e2a \\(2\\) \u540e\u4e58\u79ef\u4f1a\u53d8\u5927\uff0c\u8fd9\u8bf4\u660e\u5927\u4e8e\u7b49\u4e8e \\(4\\) \u7684\u6574\u6570\u90fd\u5e94\u8be5\u88ab\u5207\u5206\u3002
\u8d2a\u5fc3\u7b56\u7565\u4e00\uff1a\u5982\u679c\u5207\u5206\u65b9\u6848\u4e2d\u5305\u542b \\(\\geq 4\\) \u7684\u56e0\u5b50\uff0c\u90a3\u4e48\u5b83\u5c31\u5e94\u8be5\u88ab\u7ee7\u7eed\u5207\u5206\u3002\u6700\u7ec8\u7684\u5207\u5206\u65b9\u6848\u53ea\u5e94\u51fa\u73b0 \\(1\\)\u3001\\(2\\)\u3001\\(3\\) \u8fd9\u4e09\u79cd\u56e0\u5b50\u3002
\u56fe 15-14 \u00a0 \u5207\u5206\u5bfc\u81f4\u4e58\u79ef\u53d8\u5927
\u63a5\u4e0b\u6765\u601d\u8003\u54ea\u4e2a\u56e0\u5b50\u662f\u6700\u4f18\u7684\u3002\u5728 \\(1\\)\u3001\\(2\\)\u3001\\(3\\) \u8fd9\u4e09\u4e2a\u56e0\u5b50\u4e2d\uff0c\u663e\u7136 \\(1\\) \u662f\u6700\u5dee\u7684\uff0c\u56e0\u4e3a \\(1 \\times (n-1) < n\\) \u6052\u6210\u7acb\uff0c\u5373\u5207\u5206\u51fa \\(1\\) \u53cd\u800c\u4f1a\u5bfc\u81f4\u4e58\u79ef\u51cf\u5c0f\u3002
\u5982\u56fe 15-15 \u6240\u793a\uff0c\u5f53 \\(n = 6\\) \u65f6\uff0c\u6709 \\(3 \\times 3 > 2 \\times 2 \\times 2\\) \u3002\u8fd9\u610f\u5473\u7740\u5207\u5206\u51fa \\(3\\) \u6bd4\u5207\u5206\u51fa \\(2\\) \u66f4\u4f18\u3002
\u8d2a\u5fc3\u7b56\u7565\u4e8c\uff1a\u5728\u5207\u5206\u65b9\u6848\u4e2d\uff0c\u6700\u591a\u53ea\u5e94\u5b58\u5728\u4e24\u4e2a \\(2\\) \u3002\u56e0\u4e3a\u4e09\u4e2a \\(2\\) \u603b\u662f\u53ef\u4ee5\u66ff\u6362\u4e3a\u4e24\u4e2a \\(3\\) \uff0c\u4ece\u800c\u83b7\u5f97\u66f4\u5927\u7684\u4e58\u79ef\u3002
\u56fe 15-15 \u00a0 \u6700\u4f18\u5207\u5206\u56e0\u5b50
\u7efc\u4e0a\u6240\u8ff0\uff0c\u53ef\u63a8\u7406\u51fa\u4ee5\u4e0b\u8d2a\u5fc3\u7b56\u7565\u3002
\u5982\u56fe 15-16 \u6240\u793a\uff0c\u6211\u4eec\u65e0\u987b\u901a\u8fc7\u5faa\u73af\u6765\u5207\u5206\u6574\u6570\uff0c\u800c\u53ef\u4ee5\u5229\u7528\u5411\u4e0b\u6574\u9664\u8fd0\u7b97\u5f97\u5230 \\(3\\) \u7684\u4e2a\u6570 \\(a\\) \uff0c\u7528\u53d6\u6a21\u8fd0\u7b97\u5f97\u5230\u4f59\u6570 \\(b\\) \uff0c\u6b64\u65f6\u6709\uff1a
\\[ n = 3 a + b \\]\u8bf7\u6ce8\u610f\uff0c\u5bf9\u4e8e \\(n \\leq 3\\) \u7684\u8fb9\u754c\u60c5\u51b5\uff0c\u5fc5\u987b\u62c6\u5206\u51fa\u4e00\u4e2a \\(1\\) \uff0c\u4e58\u79ef\u4e3a \\(1 \\times (n - 1)\\) \u3002
PythonC++JavaC#GoSwiftJSTSDartRustCZig max_product_cutting.pydef max_product_cutting(n: int) -> int:\n \"\"\"\u6700\u5927\u5207\u5206\u4e58\u79ef\uff1a\u8d2a\u5fc3\"\"\"\n # \u5f53 n <= 3 \u65f6\uff0c\u5fc5\u987b\u5207\u5206\u51fa\u4e00\u4e2a 1\n if n <= 3:\n return 1 * (n - 1)\n # \u8d2a\u5fc3\u5730\u5207\u5206\u51fa 3 \uff0ca \u4e3a 3 \u7684\u4e2a\u6570\uff0cb \u4e3a\u4f59\u6570\n a, b = n // 3, n % 3\n if b == 1:\n # \u5f53\u4f59\u6570\u4e3a 1 \u65f6\uff0c\u5c06\u4e00\u5bf9 1 * 3 \u8f6c\u5316\u4e3a 2 * 2\n return int(math.pow(3, a - 1)) * 2 * 2\n if b == 2:\n # \u5f53\u4f59\u6570\u4e3a 2 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n return int(math.pow(3, a)) * 2\n # \u5f53\u4f59\u6570\u4e3a 0 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n return int(math.pow(3, a))\n
max_product_cutting.cpp/* \u6700\u5927\u5207\u5206\u4e58\u79ef\uff1a\u8d2a\u5fc3 */\nint maxProductCutting(int n) {\n // \u5f53 n <= 3 \u65f6\uff0c\u5fc5\u987b\u5207\u5206\u51fa\u4e00\u4e2a 1\n if (n <= 3) {\n return 1 * (n - 1);\n }\n // \u8d2a\u5fc3\u5730\u5207\u5206\u51fa 3 \uff0ca \u4e3a 3 \u7684\u4e2a\u6570\uff0cb \u4e3a\u4f59\u6570\n int a = n / 3;\n int b = n % 3;\n if (b == 1) {\n // \u5f53\u4f59\u6570\u4e3a 1 \u65f6\uff0c\u5c06\u4e00\u5bf9 1 * 3 \u8f6c\u5316\u4e3a 2 * 2\n return (int)pow(3, a - 1) * 2 * 2;\n }\n if (b == 2) {\n // \u5f53\u4f59\u6570\u4e3a 2 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n return (int)pow(3, a) * 2;\n }\n // \u5f53\u4f59\u6570\u4e3a 0 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n return (int)pow(3, a);\n}\n
max_product_cutting.java/* \u6700\u5927\u5207\u5206\u4e58\u79ef\uff1a\u8d2a\u5fc3 */\nint maxProductCutting(int n) {\n // \u5f53 n <= 3 \u65f6\uff0c\u5fc5\u987b\u5207\u5206\u51fa\u4e00\u4e2a 1\n if (n <= 3) {\n return 1 * (n - 1);\n }\n // \u8d2a\u5fc3\u5730\u5207\u5206\u51fa 3 \uff0ca \u4e3a 3 \u7684\u4e2a\u6570\uff0cb \u4e3a\u4f59\u6570\n int a = n / 3;\n int b = n % 3;\n if (b == 1) {\n // \u5f53\u4f59\u6570\u4e3a 1 \u65f6\uff0c\u5c06\u4e00\u5bf9 1 * 3 \u8f6c\u5316\u4e3a 2 * 2\n return (int) Math.pow(3, a - 1) * 2 * 2;\n }\n if (b == 2) {\n // \u5f53\u4f59\u6570\u4e3a 2 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n return (int) Math.pow(3, a) * 2;\n }\n // \u5f53\u4f59\u6570\u4e3a 0 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n return (int) Math.pow(3, a);\n}\n
max_product_cutting.cs/* \u6700\u5927\u5207\u5206\u4e58\u79ef\uff1a\u8d2a\u5fc3 */\nint MaxProductCutting(int n) {\n // \u5f53 n <= 3 \u65f6\uff0c\u5fc5\u987b\u5207\u5206\u51fa\u4e00\u4e2a 1\n if (n <= 3) {\n return 1 * (n - 1);\n }\n // \u8d2a\u5fc3\u5730\u5207\u5206\u51fa 3 \uff0ca \u4e3a 3 \u7684\u4e2a\u6570\uff0cb \u4e3a\u4f59\u6570\n int a = n / 3;\n int b = n % 3;\n if (b == 1) {\n // \u5f53\u4f59\u6570\u4e3a 1 \u65f6\uff0c\u5c06\u4e00\u5bf9 1 * 3 \u8f6c\u5316\u4e3a 2 * 2\n return (int)Math.Pow(3, a - 1) * 2 * 2;\n }\n if (b == 2) {\n // \u5f53\u4f59\u6570\u4e3a 2 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n return (int)Math.Pow(3, a) * 2;\n }\n // \u5f53\u4f59\u6570\u4e3a 0 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n return (int)Math.Pow(3, a);\n}\n
max_product_cutting.go/* \u6700\u5927\u5207\u5206\u4e58\u79ef\uff1a\u8d2a\u5fc3 */\nfunc maxProductCutting(n int) int {\n // \u5f53 n <= 3 \u65f6\uff0c\u5fc5\u987b\u5207\u5206\u51fa\u4e00\u4e2a 1\n if n <= 3 {\n return 1 * (n - 1)\n }\n // \u8d2a\u5fc3\u5730\u5207\u5206\u51fa 3 \uff0ca \u4e3a 3 \u7684\u4e2a\u6570\uff0cb \u4e3a\u4f59\u6570\n a := n / 3\n b := n % 3\n if b == 1 {\n // \u5f53\u4f59\u6570\u4e3a 1 \u65f6\uff0c\u5c06\u4e00\u5bf9 1 * 3 \u8f6c\u5316\u4e3a 2 * 2\n return int(math.Pow(3, float64(a-1))) * 2 * 2\n }\n if b == 2 {\n // \u5f53\u4f59\u6570\u4e3a 2 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n return int(math.Pow(3, float64(a))) * 2\n }\n // \u5f53\u4f59\u6570\u4e3a 0 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n return int(math.Pow(3, float64(a)))\n}\n
max_product_cutting.swift/* \u6700\u5927\u5207\u5206\u4e58\u79ef\uff1a\u8d2a\u5fc3 */\nfunc maxProductCutting(n: Int) -> Int {\n // \u5f53 n <= 3 \u65f6\uff0c\u5fc5\u987b\u5207\u5206\u51fa\u4e00\u4e2a 1\n if n <= 3 {\n return 1 * (n - 1)\n }\n // \u8d2a\u5fc3\u5730\u5207\u5206\u51fa 3 \uff0ca \u4e3a 3 \u7684\u4e2a\u6570\uff0cb \u4e3a\u4f59\u6570\n let a = n / 3\n let b = n % 3\n if b == 1 {\n // \u5f53\u4f59\u6570\u4e3a 1 \u65f6\uff0c\u5c06\u4e00\u5bf9 1 * 3 \u8f6c\u5316\u4e3a 2 * 2\n return pow(3, a - 1) * 2 * 2\n }\n if b == 2 {\n // \u5f53\u4f59\u6570\u4e3a 2 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n return pow(3, a) * 2\n }\n // \u5f53\u4f59\u6570\u4e3a 0 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n return pow(3, a)\n}\n
max_product_cutting.js/* \u6700\u5927\u5207\u5206\u4e58\u79ef\uff1a\u8d2a\u5fc3 */\nfunction maxProductCutting(n) {\n // \u5f53 n <= 3 \u65f6\uff0c\u5fc5\u987b\u5207\u5206\u51fa\u4e00\u4e2a 1\n if (n <= 3) {\n return 1 * (n - 1);\n }\n // \u8d2a\u5fc3\u5730\u5207\u5206\u51fa 3 \uff0ca \u4e3a 3 \u7684\u4e2a\u6570\uff0cb \u4e3a\u4f59\u6570\n let a = Math.floor(n / 3);\n let b = n % 3;\n if (b === 1) {\n // \u5f53\u4f59\u6570\u4e3a 1 \u65f6\uff0c\u5c06\u4e00\u5bf9 1 * 3 \u8f6c\u5316\u4e3a 2 * 2\n return Math.pow(3, a - 1) * 2 * 2;\n }\n if (b === 2) {\n // \u5f53\u4f59\u6570\u4e3a 2 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n return Math.pow(3, a) * 2;\n }\n // \u5f53\u4f59\u6570\u4e3a 0 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n return Math.pow(3, a);\n}\n
max_product_cutting.ts/* \u6700\u5927\u5207\u5206\u4e58\u79ef\uff1a\u8d2a\u5fc3 */\nfunction maxProductCutting(n: number): number {\n // \u5f53 n <= 3 \u65f6\uff0c\u5fc5\u987b\u5207\u5206\u51fa\u4e00\u4e2a 1\n if (n <= 3) {\n return 1 * (n - 1);\n }\n // \u8d2a\u5fc3\u5730\u5207\u5206\u51fa 3 \uff0ca \u4e3a 3 \u7684\u4e2a\u6570\uff0cb \u4e3a\u4f59\u6570\n let a: number = Math.floor(n / 3);\n let b: number = n % 3;\n if (b === 1) {\n // \u5f53\u4f59\u6570\u4e3a 1 \u65f6\uff0c\u5c06\u4e00\u5bf9 1 * 3 \u8f6c\u5316\u4e3a 2 * 2\n return Math.pow(3, a - 1) * 2 * 2;\n }\n if (b === 2) {\n // \u5f53\u4f59\u6570\u4e3a 2 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n return Math.pow(3, a) * 2;\n }\n // \u5f53\u4f59\u6570\u4e3a 0 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n return Math.pow(3, a);\n}\n
max_product_cutting.dart/* \u6700\u5927\u5207\u5206\u4e58\u79ef\uff1a\u8d2a\u5fc3 */\nint maxProductCutting(int n) {\n // \u5f53 n <= 3 \u65f6\uff0c\u5fc5\u987b\u5207\u5206\u51fa\u4e00\u4e2a 1\n if (n <= 3) {\n return 1 * (n - 1);\n }\n // \u8d2a\u5fc3\u5730\u5207\u5206\u51fa 3 \uff0ca \u4e3a 3 \u7684\u4e2a\u6570\uff0cb \u4e3a\u4f59\u6570\n int a = n ~/ 3;\n int b = n % 3;\n if (b == 1) {\n // \u5f53\u4f59\u6570\u4e3a 1 \u65f6\uff0c\u5c06\u4e00\u5bf9 1 * 3 \u8f6c\u5316\u4e3a 2 * 2\n return (pow(3, a - 1) * 2 * 2).toInt();\n }\n if (b == 2) {\n // \u5f53\u4f59\u6570\u4e3a 2 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n return (pow(3, a) * 2).toInt();\n }\n // \u5f53\u4f59\u6570\u4e3a 0 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n return pow(3, a).toInt();\n}\n
max_product_cutting.rs/* \u6700\u5927\u5207\u5206\u4e58\u79ef\uff1a\u8d2a\u5fc3 */\nfn max_product_cutting(n: i32) -> i32 {\n // \u5f53 n <= 3 \u65f6\uff0c\u5fc5\u987b\u5207\u5206\u51fa\u4e00\u4e2a 1\n if n <= 3 {\n return 1 * (n - 1);\n }\n // \u8d2a\u5fc3\u5730\u5207\u5206\u51fa 3 \uff0ca \u4e3a 3 \u7684\u4e2a\u6570\uff0cb \u4e3a\u4f59\u6570\n let a = n / 3;\n let b = n % 3;\n if b == 1 {\n // \u5f53\u4f59\u6570\u4e3a 1 \u65f6\uff0c\u5c06\u4e00\u5bf9 1 * 3 \u8f6c\u5316\u4e3a 2 * 2\n 3_i32.pow(a as u32 - 1) * 2 * 2\n } else if b == 2 {\n // \u5f53\u4f59\u6570\u4e3a 2 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n 3_i32.pow(a as u32) * 2\n } else {\n // \u5f53\u4f59\u6570\u4e3a 0 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n 3_i32.pow(a as u32)\n }\n}\n
max_product_cutting.c/* \u6700\u5927\u5207\u5206\u4e58\u79ef\uff1a\u8d2a\u5fc3 */\nint maxProductCutting(int n) {\n // \u5f53 n <= 3 \u65f6\uff0c\u5fc5\u987b\u5207\u5206\u51fa\u4e00\u4e2a 1\n if (n <= 3) {\n return 1 * (n - 1);\n }\n // \u8d2a\u5fc3\u5730\u5207\u5206\u51fa 3 \uff0ca \u4e3a 3 \u7684\u4e2a\u6570\uff0cb \u4e3a\u4f59\u6570\n int a = n / 3;\n int b = n % 3;\n if (b == 1) {\n // \u5f53\u4f59\u6570\u4e3a 1 \u65f6\uff0c\u5c06\u4e00\u5bf9 1 * 3 \u8f6c\u5316\u4e3a 2 * 2\n return pow(3, a - 1) * 2 * 2;\n }\n if (b == 2) {\n // \u5f53\u4f59\u6570\u4e3a 2 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n return pow(3, a) * 2;\n }\n // \u5f53\u4f59\u6570\u4e3a 0 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n return pow(3, a);\n}\n
max_product_cutting.zig[class]{}-[func]{maxProductCutting}\n
\u56fe 15-16 \u00a0 \u6700\u5927\u5207\u5206\u4e58\u79ef\u7684\u8ba1\u7b97\u65b9\u6cd5
\u65f6\u95f4\u590d\u6742\u5ea6\u53d6\u51b3\u4e8e\u7f16\u7a0b\u8bed\u8a00\u7684\u5e42\u8fd0\u7b97\u7684\u5b9e\u73b0\u65b9\u6cd5\u3002\u4ee5 Python \u4e3a\u4f8b\uff0c\u5e38\u7528\u7684\u5e42\u8ba1\u7b97\u51fd\u6570\u6709\u4e09\u79cd\u3002
**
\u548c\u51fd\u6570 pow()
\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u5747\u4e3a \\(O(\\log\u2061 a)\\) \u3002math.pow()
\u5185\u90e8\u8c03\u7528 C \u8bed\u8a00\u5e93\u7684 pow()
\u51fd\u6570\uff0c\u5176\u6267\u884c\u6d6e\u70b9\u53d6\u5e42\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(1)\\) \u3002\u53d8\u91cf \\(a\\) \u548c \\(b\\) \u4f7f\u7528\u5e38\u6570\u5927\u5c0f\u7684\u989d\u5916\u7a7a\u95f4\uff0c\u56e0\u6b64\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(1)\\) \u3002
"},{"location":"chapter_greedy/max_product_cutting_problem/#3","title":"3. \u00a0 \u6b63\u786e\u6027\u8bc1\u660e","text":"\u4f7f\u7528\u53cd\u8bc1\u6cd5\uff0c\u53ea\u5206\u6790 \\(n \\geq 3\\) \u7684\u60c5\u51b5\u3002
Abstract
\u5728\u8ba1\u7b97\u673a\u4e16\u754c\u4e2d\uff0c\u54c8\u5e0c\u8868\u5982\u540c\u4e00\u4f4d\u806a\u6167\u7684\u56fe\u4e66\u7ba1\u7406\u5458\u3002
\u4ed6\u77e5\u9053\u5982\u4f55\u8ba1\u7b97\u7d22\u4e66\u53f7\uff0c\u4ece\u800c\u53ef\u4ee5\u5feb\u901f\u627e\u5230\u76ee\u6807\u56fe\u4e66\u3002
"},{"location":"chapter_hashing/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"\u524d\u4e24\u8282\u4ecb\u7ecd\u4e86\u54c8\u5e0c\u8868\u7684\u5de5\u4f5c\u539f\u7406\u548c\u54c8\u5e0c\u51b2\u7a81\u7684\u5904\u7406\u65b9\u6cd5\u3002\u7136\u800c\u65e0\u8bba\u662f\u5f00\u653e\u5bfb\u5740\u8fd8\u662f\u94fe\u5f0f\u5730\u5740\uff0c\u5b83\u4eec\u53ea\u80fd\u4fdd\u8bc1\u54c8\u5e0c\u8868\u53ef\u4ee5\u5728\u53d1\u751f\u51b2\u7a81\u65f6\u6b63\u5e38\u5de5\u4f5c\uff0c\u800c\u65e0\u6cd5\u51cf\u5c11\u54c8\u5e0c\u51b2\u7a81\u7684\u53d1\u751f\u3002
\u5982\u679c\u54c8\u5e0c\u51b2\u7a81\u8fc7\u4e8e\u9891\u7e41\uff0c\u54c8\u5e0c\u8868\u7684\u6027\u80fd\u5219\u4f1a\u6025\u5267\u52a3\u5316\u3002\u5982\u56fe 6-8 \u6240\u793a\uff0c\u5bf9\u4e8e\u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868\uff0c\u7406\u60f3\u60c5\u51b5\u4e0b\u952e\u503c\u5bf9\u5747\u5300\u5206\u5e03\u5728\u5404\u4e2a\u6876\u4e2d\uff0c\u8fbe\u5230\u6700\u4f73\u67e5\u8be2\u6548\u7387\uff1b\u6700\u5dee\u60c5\u51b5\u4e0b\u6240\u6709\u952e\u503c\u5bf9\u90fd\u5b58\u50a8\u5230\u540c\u4e00\u4e2a\u6876\u4e2d\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u9000\u5316\u81f3 \\(O(n)\\) \u3002
\u56fe 6-8 \u00a0 \u54c8\u5e0c\u51b2\u7a81\u7684\u6700\u4f73\u60c5\u51b5\u4e0e\u6700\u5dee\u60c5\u51b5
\u952e\u503c\u5bf9\u7684\u5206\u5e03\u60c5\u51b5\u7531\u54c8\u5e0c\u51fd\u6570\u51b3\u5b9a\u3002\u56de\u5fc6\u54c8\u5e0c\u51fd\u6570\u7684\u8ba1\u7b97\u6b65\u9aa4\uff0c\u5148\u8ba1\u7b97\u54c8\u5e0c\u503c\uff0c\u518d\u5bf9\u6570\u7ec4\u957f\u5ea6\u53d6\u6a21\uff1a
index = hash(key) % capacity\n
\u89c2\u5bdf\u4ee5\u4e0a\u516c\u5f0f\uff0c\u5f53\u54c8\u5e0c\u8868\u5bb9\u91cf capacity
\u56fa\u5b9a\u65f6\uff0c\u54c8\u5e0c\u7b97\u6cd5 hash()
\u51b3\u5b9a\u4e86\u8f93\u51fa\u503c\uff0c\u8fdb\u800c\u51b3\u5b9a\u4e86\u952e\u503c\u5bf9\u5728\u54c8\u5e0c\u8868\u4e2d\u7684\u5206\u5e03\u60c5\u51b5\u3002
\u8fd9\u610f\u5473\u7740\uff0c\u4e3a\u4e86\u964d\u4f4e\u54c8\u5e0c\u51b2\u7a81\u7684\u53d1\u751f\u6982\u7387\uff0c\u6211\u4eec\u5e94\u5f53\u5c06\u6ce8\u610f\u529b\u96c6\u4e2d\u5728\u54c8\u5e0c\u7b97\u6cd5 hash()
\u7684\u8bbe\u8ba1\u4e0a\u3002
\u4e3a\u4e86\u5b9e\u73b0\u201c\u65e2\u5feb\u53c8\u7a33\u201d\u7684\u54c8\u5e0c\u8868\u6570\u636e\u7ed3\u6784\uff0c\u54c8\u5e0c\u7b97\u6cd5\u5e94\u5177\u5907\u4ee5\u4e0b\u7279\u70b9\u3002
\u5b9e\u9645\u4e0a\uff0c\u54c8\u5e0c\u7b97\u6cd5\u9664\u4e86\u53ef\u4ee5\u7528\u4e8e\u5b9e\u73b0\u54c8\u5e0c\u8868\uff0c\u8fd8\u5e7f\u6cdb\u5e94\u7528\u4e8e\u5176\u4ed6\u9886\u57df\u4e2d\u3002
\u5bf9\u4e8e\u5bc6\u7801\u5b66\u7684\u76f8\u5173\u5e94\u7528\uff0c\u4e3a\u4e86\u9632\u6b62\u4ece\u54c8\u5e0c\u503c\u63a8\u5bfc\u51fa\u539f\u59cb\u5bc6\u7801\u7b49\u9006\u5411\u5de5\u7a0b\uff0c\u54c8\u5e0c\u7b97\u6cd5\u9700\u8981\u5177\u5907\u66f4\u9ad8\u7b49\u7ea7\u7684\u5b89\u5168\u7279\u6027\u3002
\u8bf7\u6ce8\u610f\uff0c\u201c\u5747\u5300\u5206\u5e03\u201d\u4e0e\u201c\u6297\u78b0\u649e\u6027\u201d\u662f\u4e24\u4e2a\u72ec\u7acb\u7684\u6982\u5ff5\uff0c\u6ee1\u8db3\u5747\u5300\u5206\u5e03\u4e0d\u4e00\u5b9a\u6ee1\u8db3\u6297\u78b0\u649e\u6027\u3002\u4f8b\u5982\uff0c\u5728\u968f\u673a\u8f93\u5165 key
\u4e0b\uff0c\u54c8\u5e0c\u51fd\u6570 key % 100
\u53ef\u4ee5\u4ea7\u751f\u5747\u5300\u5206\u5e03\u7684\u8f93\u51fa\u3002\u7136\u800c\u8be5\u54c8\u5e0c\u7b97\u6cd5\u8fc7\u4e8e\u7b80\u5355\uff0c\u6240\u6709\u540e\u4e24\u4f4d\u76f8\u7b49\u7684 key
\u7684\u8f93\u51fa\u90fd\u76f8\u540c\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u4ece\u54c8\u5e0c\u503c\u53cd\u63a8\u51fa\u53ef\u7528\u7684 key
\uff0c\u4ece\u800c\u7834\u89e3\u5bc6\u7801\u3002
\u54c8\u5e0c\u7b97\u6cd5\u7684\u8bbe\u8ba1\u662f\u4e00\u4e2a\u9700\u8981\u8003\u8651\u8bb8\u591a\u56e0\u7d20\u7684\u590d\u6742\u95ee\u9898\u3002\u7136\u800c\u5bf9\u4e8e\u67d0\u4e9b\u8981\u6c42\u4e0d\u9ad8\u7684\u573a\u666f\uff0c\u6211\u4eec\u4e5f\u80fd\u8bbe\u8ba1\u4e00\u4e9b\u7b80\u5355\u7684\u54c8\u5e0c\u7b97\u6cd5\u3002
def add_hash(key: str) -> int:\n \"\"\"\u52a0\u6cd5\u54c8\u5e0c\"\"\"\n hash = 0\n modulus = 1000000007\n for c in key:\n hash += ord(c)\n return hash % modulus\n\ndef mul_hash(key: str) -> int:\n \"\"\"\u4e58\u6cd5\u54c8\u5e0c\"\"\"\n hash = 0\n modulus = 1000000007\n for c in key:\n hash = 31 * hash + ord(c)\n return hash % modulus\n\ndef xor_hash(key: str) -> int:\n \"\"\"\u5f02\u6216\u54c8\u5e0c\"\"\"\n hash = 0\n modulus = 1000000007\n for c in key:\n hash ^= ord(c)\n return hash % modulus\n\ndef rot_hash(key: str) -> int:\n \"\"\"\u65cb\u8f6c\u54c8\u5e0c\"\"\"\n hash = 0\n modulus = 1000000007\n for c in key:\n hash = (hash << 4) ^ (hash >> 28) ^ ord(c)\n return hash % modulus\n
simple_hash.cpp/* \u52a0\u6cd5\u54c8\u5e0c */\nint addHash(string key) {\n long long hash = 0;\n const int MODULUS = 1000000007;\n for (unsigned char c : key) {\n hash = (hash + (int)c) % MODULUS;\n }\n return (int)hash;\n}\n\n/* \u4e58\u6cd5\u54c8\u5e0c */\nint mulHash(string key) {\n long long hash = 0;\n const int MODULUS = 1000000007;\n for (unsigned char c : key) {\n hash = (31 * hash + (int)c) % MODULUS;\n }\n return (int)hash;\n}\n\n/* \u5f02\u6216\u54c8\u5e0c */\nint xorHash(string key) {\n int hash = 0;\n const int MODULUS = 1000000007;\n for (unsigned char c : key) {\n hash ^= (int)c;\n }\n return hash & MODULUS;\n}\n\n/* \u65cb\u8f6c\u54c8\u5e0c */\nint rotHash(string key) {\n long long hash = 0;\n const int MODULUS = 1000000007;\n for (unsigned char c : key) {\n hash = ((hash << 4) ^ (hash >> 28) ^ (int)c) % MODULUS;\n }\n return (int)hash;\n}\n
simple_hash.java/* \u52a0\u6cd5\u54c8\u5e0c */\nint addHash(String key) {\n long hash = 0;\n final int MODULUS = 1000000007;\n for (char c : key.toCharArray()) {\n hash = (hash + (int) c) % MODULUS;\n }\n return (int) hash;\n}\n\n/* \u4e58\u6cd5\u54c8\u5e0c */\nint mulHash(String key) {\n long hash = 0;\n final int MODULUS = 1000000007;\n for (char c : key.toCharArray()) {\n hash = (31 * hash + (int) c) % MODULUS;\n }\n return (int) hash;\n}\n\n/* \u5f02\u6216\u54c8\u5e0c */\nint xorHash(String key) {\n int hash = 0;\n final int MODULUS = 1000000007;\n for (char c : key.toCharArray()) {\n hash ^= (int) c;\n }\n return hash & MODULUS;\n}\n\n/* \u65cb\u8f6c\u54c8\u5e0c */\nint rotHash(String key) {\n long hash = 0;\n final int MODULUS = 1000000007;\n for (char c : key.toCharArray()) {\n hash = ((hash << 4) ^ (hash >> 28) ^ (int) c) % MODULUS;\n }\n return (int) hash;\n}\n
simple_hash.cs/* \u52a0\u6cd5\u54c8\u5e0c */\nint AddHash(string key) {\n long hash = 0;\n const int MODULUS = 1000000007;\n foreach (char c in key) {\n hash = (hash + c) % MODULUS;\n }\n return (int)hash;\n}\n\n/* \u4e58\u6cd5\u54c8\u5e0c */\nint MulHash(string key) {\n long hash = 0;\n const int MODULUS = 1000000007;\n foreach (char c in key) {\n hash = (31 * hash + c) % MODULUS;\n }\n return (int)hash;\n}\n\n/* \u5f02\u6216\u54c8\u5e0c */\nint XorHash(string key) {\n int hash = 0;\n const int MODULUS = 1000000007;\n foreach (char c in key) {\n hash ^= c;\n }\n return hash & MODULUS;\n}\n\n/* \u65cb\u8f6c\u54c8\u5e0c */\nint RotHash(string key) {\n long hash = 0;\n const int MODULUS = 1000000007;\n foreach (char c in key) {\n hash = ((hash << 4) ^ (hash >> 28) ^ c) % MODULUS;\n }\n return (int)hash;\n}\n
simple_hash.go/* \u52a0\u6cd5\u54c8\u5e0c */\nfunc addHash(key string) int {\n var hash int64\n var modulus int64\n\n modulus = 1000000007\n for _, b := range []byte(key) {\n hash = (hash + int64(b)) % modulus\n }\n return int(hash)\n}\n\n/* \u4e58\u6cd5\u54c8\u5e0c */\nfunc mulHash(key string) int {\n var hash int64\n var modulus int64\n\n modulus = 1000000007\n for _, b := range []byte(key) {\n hash = (31*hash + int64(b)) % modulus\n }\n return int(hash)\n}\n\n/* \u5f02\u6216\u54c8\u5e0c */\nfunc xorHash(key string) int {\n hash := 0\n modulus := 1000000007\n for _, b := range []byte(key) {\n fmt.Println(int(b))\n hash ^= int(b)\n hash = (31*hash + int(b)) % modulus\n }\n return hash & modulus\n}\n\n/* \u65cb\u8f6c\u54c8\u5e0c */\nfunc rotHash(key string) int {\n var hash int64\n var modulus int64\n\n modulus = 1000000007\n for _, b := range []byte(key) {\n hash = ((hash << 4) ^ (hash >> 28) ^ int64(b)) % modulus\n }\n return int(hash)\n}\n
simple_hash.swift/* \u52a0\u6cd5\u54c8\u5e0c */\nfunc addHash(key: String) -> Int {\n var hash = 0\n let MODULUS = 1_000_000_007\n for c in key {\n for scalar in c.unicodeScalars {\n hash = (hash + Int(scalar.value)) % MODULUS\n }\n }\n return hash\n}\n\n/* \u4e58\u6cd5\u54c8\u5e0c */\nfunc mulHash(key: String) -> Int {\n var hash = 0\n let MODULUS = 1_000_000_007\n for c in key {\n for scalar in c.unicodeScalars {\n hash = (31 * hash + Int(scalar.value)) % MODULUS\n }\n }\n return hash\n}\n\n/* \u5f02\u6216\u54c8\u5e0c */\nfunc xorHash(key: String) -> Int {\n var hash = 0\n let MODULUS = 1_000_000_007\n for c in key {\n for scalar in c.unicodeScalars {\n hash ^= Int(scalar.value)\n }\n }\n return hash & MODULUS\n}\n\n/* \u65cb\u8f6c\u54c8\u5e0c */\nfunc rotHash(key: String) -> Int {\n var hash = 0\n let MODULUS = 1_000_000_007\n for c in key {\n for scalar in c.unicodeScalars {\n hash = ((hash << 4) ^ (hash >> 28) ^ Int(scalar.value)) % MODULUS\n }\n }\n return hash\n}\n
simple_hash.js/* \u52a0\u6cd5\u54c8\u5e0c */\nfunction addHash(key) {\n let hash = 0;\n const MODULUS = 1000000007;\n for (const c of key) {\n hash = (hash + c.charCodeAt(0)) % MODULUS;\n }\n return hash;\n}\n\n/* \u4e58\u6cd5\u54c8\u5e0c */\nfunction mulHash(key) {\n let hash = 0;\n const MODULUS = 1000000007;\n for (const c of key) {\n hash = (31 * hash + c.charCodeAt(0)) % MODULUS;\n }\n return hash;\n}\n\n/* \u5f02\u6216\u54c8\u5e0c */\nfunction xorHash(key) {\n let hash = 0;\n const MODULUS = 1000000007;\n for (const c of key) {\n hash ^= c.charCodeAt(0);\n }\n return hash & MODULUS;\n}\n\n/* \u65cb\u8f6c\u54c8\u5e0c */\nfunction rotHash(key) {\n let hash = 0;\n const MODULUS = 1000000007;\n for (const c of key) {\n hash = ((hash << 4) ^ (hash >> 28) ^ c.charCodeAt(0)) % MODULUS;\n }\n return hash;\n}\n
simple_hash.ts/* \u52a0\u6cd5\u54c8\u5e0c */\nfunction addHash(key: string): number {\n let hash = 0;\n const MODULUS = 1000000007;\n for (const c of key) {\n hash = (hash + c.charCodeAt(0)) % MODULUS;\n }\n return hash;\n}\n\n/* \u4e58\u6cd5\u54c8\u5e0c */\nfunction mulHash(key: string): number {\n let hash = 0;\n const MODULUS = 1000000007;\n for (const c of key) {\n hash = (31 * hash + c.charCodeAt(0)) % MODULUS;\n }\n return hash;\n}\n\n/* \u5f02\u6216\u54c8\u5e0c */\nfunction xorHash(key: string): number {\n let hash = 0;\n const MODULUS = 1000000007;\n for (const c of key) {\n hash ^= c.charCodeAt(0);\n }\n return hash & MODULUS;\n}\n\n/* \u65cb\u8f6c\u54c8\u5e0c */\nfunction rotHash(key: string): number {\n let hash = 0;\n const MODULUS = 1000000007;\n for (const c of key) {\n hash = ((hash << 4) ^ (hash >> 28) ^ c.charCodeAt(0)) % MODULUS;\n }\n return hash;\n}\n
simple_hash.dart/* \u52a0\u6cd5\u54c8\u5e0c */\nint addHash(String key) {\n int hash = 0;\n final int MODULUS = 1000000007;\n for (int i = 0; i < key.length; i++) {\n hash = (hash + key.codeUnitAt(i)) % MODULUS;\n }\n return hash;\n}\n\n/* \u4e58\u6cd5\u54c8\u5e0c */\nint mulHash(String key) {\n int hash = 0;\n final int MODULUS = 1000000007;\n for (int i = 0; i < key.length; i++) {\n hash = (31 * hash + key.codeUnitAt(i)) % MODULUS;\n }\n return hash;\n}\n\n/* \u5f02\u6216\u54c8\u5e0c */\nint xorHash(String key) {\n int hash = 0;\n final int MODULUS = 1000000007;\n for (int i = 0; i < key.length; i++) {\n hash ^= key.codeUnitAt(i);\n }\n return hash & MODULUS;\n}\n\n/* \u65cb\u8f6c\u54c8\u5e0c */\nint rotHash(String key) {\n int hash = 0;\n final int MODULUS = 1000000007;\n for (int i = 0; i < key.length; i++) {\n hash = ((hash << 4) ^ (hash >> 28) ^ key.codeUnitAt(i)) % MODULUS;\n }\n return hash;\n}\n
simple_hash.rs/* \u52a0\u6cd5\u54c8\u5e0c */\nfn add_hash(key: &str) -> i32 {\n let mut hash = 0_i64;\n const MODULUS: i64 = 1000000007;\n\n for c in key.chars() {\n hash = (hash + c as i64) % MODULUS;\n }\n\n hash as i32\n} \n\n/* \u4e58\u6cd5\u54c8\u5e0c */\nfn mul_hash(key: &str) -> i32 {\n let mut hash = 0_i64;\n const MODULUS: i64 = 1000000007;\n\n for c in key.chars() {\n hash = (31 * hash + c as i64) % MODULUS;\n }\n\n hash as i32\n}\n\n/* \u5f02\u6216\u54c8\u5e0c */\nfn xor_hash(key: &str) -> i32 {\n let mut hash = 0_i64;\n const MODULUS: i64 = 1000000007;\n\n for c in key.chars() {\n hash ^= c as i64;\n }\n\n (hash & MODULUS) as i32\n}\n\n/* \u65cb\u8f6c\u54c8\u5e0c */\nfn rot_hash(key: &str) -> i32 {\n let mut hash = 0_i64;\n const MODULUS: i64 = 1000000007;\n\n for c in key.chars() {\n hash = ((hash << 4) ^ (hash >> 28) ^ c as i64) % MODULUS;\n }\n\n hash as i32\n}\n
simple_hash.c/* \u52a0\u6cd5\u54c8\u5e0c */\nint addHash(char *key) {\n long long hash = 0;\n const int MODULUS = 1000000007;\n for (int i = 0; i < strlen(key); i++) {\n hash = (hash + (unsigned char)key[i]) % MODULUS;\n }\n return (int)hash;\n}\n\n/* \u4e58\u6cd5\u54c8\u5e0c */\nint mulHash(char *key) {\n long long hash = 0;\n const int MODULUS = 1000000007;\n for (int i = 0; i < strlen(key); i++) {\n hash = (31 * hash + (unsigned char)key[i]) % MODULUS;\n }\n return (int)hash;\n}\n\n/* \u5f02\u6216\u54c8\u5e0c */\nint xorHash(char *key) {\n int hash = 0;\n const int MODULUS = 1000000007;\n\n for (int i = 0; i < strlen(key); i++) {\n hash ^= (unsigned char)key[i];\n }\n return hash & MODULUS;\n}\n\n/* \u65cb\u8f6c\u54c8\u5e0c */\nint rotHash(char *key) {\n long long hash = 0;\n const int MODULUS = 1000000007;\n for (int i = 0; i < strlen(key); i++) {\n hash = ((hash << 4) ^ (hash >> 28) ^ (unsigned char)key[i]) % MODULUS;\n }\n\n return (int)hash;\n}\n
simple_hash.zig[class]{}-[func]{addHash}\n\n[class]{}-[func]{mulHash}\n\n[class]{}-[func]{xorHash}\n\n[class]{}-[func]{rotHash}\n
\u89c2\u5bdf\u53d1\u73b0\uff0c\u6bcf\u79cd\u54c8\u5e0c\u7b97\u6cd5\u7684\u6700\u540e\u4e00\u6b65\u90fd\u662f\u5bf9\u5927\u8d28\u6570 \\(1000000007\\) \u53d6\u6a21\uff0c\u4ee5\u786e\u4fdd\u54c8\u5e0c\u503c\u5728\u5408\u9002\u7684\u8303\u56f4\u5185\u3002\u503c\u5f97\u601d\u8003\u7684\u662f\uff0c\u4e3a\u4ec0\u4e48\u8981\u5f3a\u8c03\u5bf9\u8d28\u6570\u53d6\u6a21\uff0c\u6216\u8005\u8bf4\u5bf9\u5408\u6570\u53d6\u6a21\u7684\u5f0a\u7aef\u662f\u4ec0\u4e48\uff1f\u8fd9\u662f\u4e00\u4e2a\u6709\u8da3\u7684\u95ee\u9898\u3002
\u5148\u629b\u51fa\u7ed3\u8bba\uff1a\u4f7f\u7528\u5927\u8d28\u6570\u4f5c\u4e3a\u6a21\u6570\uff0c\u53ef\u4ee5\u6700\u5927\u5316\u5730\u4fdd\u8bc1\u54c8\u5e0c\u503c\u7684\u5747\u5300\u5206\u5e03\u3002\u56e0\u4e3a\u8d28\u6570\u4e0d\u4e0e\u5176\u4ed6\u6570\u5b57\u5b58\u5728\u516c\u7ea6\u6570\uff0c\u53ef\u4ee5\u51cf\u5c11\u56e0\u53d6\u6a21\u64cd\u4f5c\u800c\u4ea7\u751f\u7684\u5468\u671f\u6027\u6a21\u5f0f\uff0c\u4ece\u800c\u907f\u514d\u54c8\u5e0c\u51b2\u7a81\u3002
\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u5047\u8bbe\u6211\u4eec\u9009\u62e9\u5408\u6570 \\(9\\) \u4f5c\u4e3a\u6a21\u6570\uff0c\u5b83\u53ef\u4ee5\u88ab \\(3\\) \u6574\u9664\uff0c\u90a3\u4e48\u6240\u6709\u53ef\u4ee5\u88ab \\(3\\) \u6574\u9664\u7684 key
\u90fd\u4f1a\u88ab\u6620\u5c04\u5230 \\(0\\)\u3001\\(3\\)\u3001\\(6\\) \u8fd9\u4e09\u4e2a\u54c8\u5e0c\u503c\u3002
\u5982\u679c\u8f93\u5165 key
\u6070\u597d\u6ee1\u8db3\u8fd9\u79cd\u7b49\u5dee\u6570\u5217\u7684\u6570\u636e\u5206\u5e03\uff0c\u90a3\u4e48\u54c8\u5e0c\u503c\u5c31\u4f1a\u51fa\u73b0\u805a\u5806\uff0c\u4ece\u800c\u52a0\u91cd\u54c8\u5e0c\u51b2\u7a81\u3002\u73b0\u5728\uff0c\u5047\u8bbe\u5c06 modulus
\u66ff\u6362\u4e3a\u8d28\u6570 \\(13\\) \uff0c\u7531\u4e8e key
\u548c modulus
\u4e4b\u95f4\u4e0d\u5b58\u5728\u516c\u7ea6\u6570\uff0c\u56e0\u6b64\u8f93\u51fa\u7684\u54c8\u5e0c\u503c\u7684\u5747\u5300\u6027\u4f1a\u660e\u663e\u63d0\u5347\u3002
\u503c\u5f97\u8bf4\u660e\u7684\u662f\uff0c\u5982\u679c\u80fd\u591f\u4fdd\u8bc1 key
\u662f\u968f\u673a\u5747\u5300\u5206\u5e03\u7684\uff0c\u90a3\u4e48\u9009\u62e9\u8d28\u6570\u6216\u8005\u5408\u6570\u4f5c\u4e3a\u6a21\u6570\u90fd\u53ef\u4ee5\uff0c\u5b83\u4eec\u90fd\u80fd\u8f93\u51fa\u5747\u5300\u5206\u5e03\u7684\u54c8\u5e0c\u503c\u3002\u800c\u5f53 key
\u7684\u5206\u5e03\u5b58\u5728\u67d0\u79cd\u5468\u671f\u6027\u65f6\uff0c\u5bf9\u5408\u6570\u53d6\u6a21\u66f4\u5bb9\u6613\u51fa\u73b0\u805a\u96c6\u73b0\u8c61\u3002
\u603b\u800c\u8a00\u4e4b\uff0c\u6211\u4eec\u901a\u5e38\u9009\u53d6\u8d28\u6570\u4f5c\u4e3a\u6a21\u6570\uff0c\u5e76\u4e14\u8fd9\u4e2a\u8d28\u6570\u6700\u597d\u8db3\u591f\u5927\uff0c\u4ee5\u5c3d\u53ef\u80fd\u6d88\u9664\u5468\u671f\u6027\u6a21\u5f0f\uff0c\u63d0\u5347\u54c8\u5e0c\u7b97\u6cd5\u7684\u7a33\u5065\u6027\u3002
"},{"location":"chapter_hashing/hash_algorithm/#633","title":"6.3.3 \u00a0 \u5e38\u89c1\u54c8\u5e0c\u7b97\u6cd5","text":"\u4e0d\u96be\u53d1\u73b0\uff0c\u4ee5\u4e0a\u4ecb\u7ecd\u7684\u7b80\u5355\u54c8\u5e0c\u7b97\u6cd5\u90fd\u6bd4\u8f83\u201c\u8106\u5f31\u201d\uff0c\u8fdc\u8fdc\u6ca1\u6709\u8fbe\u5230\u54c8\u5e0c\u7b97\u6cd5\u7684\u8bbe\u8ba1\u76ee\u6807\u3002\u4f8b\u5982\uff0c\u7531\u4e8e\u52a0\u6cd5\u548c\u5f02\u6216\u6ee1\u8db3\u4ea4\u6362\u5f8b\uff0c\u56e0\u6b64\u52a0\u6cd5\u54c8\u5e0c\u548c\u5f02\u6216\u54c8\u5e0c\u65e0\u6cd5\u533a\u5206\u5185\u5bb9\u76f8\u540c\u4f46\u987a\u5e8f\u4e0d\u540c\u7684\u5b57\u7b26\u4e32\uff0c\u8fd9\u53ef\u80fd\u4f1a\u52a0\u5267\u54c8\u5e0c\u51b2\u7a81\uff0c\u5e76\u5f15\u8d77\u4e00\u4e9b\u5b89\u5168\u95ee\u9898\u3002
\u5728\u5b9e\u9645\u4e2d\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u7528\u4e00\u4e9b\u6807\u51c6\u54c8\u5e0c\u7b97\u6cd5\uff0c\u4f8b\u5982 MD5\u3001SHA-1\u3001SHA-2\u3001SHA-3 \u7b49\u3002\u5b83\u4eec\u53ef\u4ee5\u5c06\u4efb\u610f\u957f\u5ea6\u7684\u8f93\u5165\u6570\u636e\u6620\u5c04\u5230\u6052\u5b9a\u957f\u5ea6\u7684\u54c8\u5e0c\u503c\u3002
\u8fd1\u4e00\u4e2a\u4e16\u7eaa\u4ee5\u6765\uff0c\u54c8\u5e0c\u7b97\u6cd5\u5904\u5728\u4e0d\u65ad\u5347\u7ea7\u4e0e\u4f18\u5316\u7684\u8fc7\u7a0b\u4e2d\u3002\u4e00\u90e8\u5206\u7814\u7a76\u4eba\u5458\u52aa\u529b\u63d0\u5347\u54c8\u5e0c\u7b97\u6cd5\u7684\u6027\u80fd\uff0c\u53e6\u4e00\u90e8\u5206\u7814\u7a76\u4eba\u5458\u548c\u9ed1\u5ba2\u5219\u81f4\u529b\u4e8e\u5bfb\u627e\u54c8\u5e0c\u7b97\u6cd5\u7684\u5b89\u5168\u6027\u95ee\u9898\u3002\u8868 6-2 \u5c55\u793a\u4e86\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u5e38\u89c1\u7684\u54c8\u5e0c\u7b97\u6cd5\u3002
\u8868 6-2 \u00a0 \u5e38\u89c1\u7684\u54c8\u5e0c\u7b97\u6cd5
MD5 SHA-1 SHA-2 SHA-3 \u63a8\u51fa\u65f6\u95f4 1992 1995 2002 2008 \u8f93\u51fa\u957f\u5ea6 128 bits 160 bits 256/512 bits 224/256/384/512 bits \u54c8\u5e0c\u51b2\u7a81 \u8f83\u591a \u8f83\u591a \u5f88\u5c11 \u5f88\u5c11 \u5b89\u5168\u7b49\u7ea7 \u4f4e\uff0c\u5df2\u88ab\u6210\u529f\u653b\u51fb \u4f4e\uff0c\u5df2\u88ab\u6210\u529f\u653b\u51fb \u9ad8 \u9ad8 \u5e94\u7528 \u5df2\u88ab\u5f03\u7528\uff0c\u4ecd\u7528\u4e8e\u6570\u636e\u5b8c\u6574\u6027\u68c0\u67e5 \u5df2\u88ab\u5f03\u7528 \u52a0\u5bc6\u8d27\u5e01\u4ea4\u6613\u9a8c\u8bc1\u3001\u6570\u5b57\u7b7e\u540d\u7b49 \u53ef\u7528\u4e8e\u66ff\u4ee3 SHA-2"},{"location":"chapter_hashing/hash_algorithm/#634","title":"6.3.4 \u00a0 \u6570\u636e\u7ed3\u6784\u7684\u54c8\u5e0c\u503c","text":"\u6211\u4eec\u77e5\u9053\uff0c\u54c8\u5e0c\u8868\u7684 key
\u53ef\u4ee5\u662f\u6574\u6570\u3001\u5c0f\u6570\u6216\u5b57\u7b26\u4e32\u7b49\u6570\u636e\u7c7b\u578b\u3002\u7f16\u7a0b\u8bed\u8a00\u901a\u5e38\u4f1a\u4e3a\u8fd9\u4e9b\u6570\u636e\u7c7b\u578b\u63d0\u4f9b\u5185\u7f6e\u7684\u54c8\u5e0c\u7b97\u6cd5\uff0c\u7528\u4e8e\u8ba1\u7b97\u54c8\u5e0c\u8868\u4e2d\u7684\u6876\u7d22\u5f15\u3002\u4ee5 Python \u4e3a\u4f8b\uff0c\u6211\u4eec\u53ef\u4ee5\u8c03\u7528 hash()
\u51fd\u6570\u6765\u8ba1\u7b97\u5404\u79cd\u6570\u636e\u7c7b\u578b\u7684\u54c8\u5e0c\u503c\u3002
Tip
\u8bf7\u6ce8\u610f\uff0c\u4e0d\u540c\u7f16\u7a0b\u8bed\u8a00\u7684\u5185\u7f6e\u54c8\u5e0c\u503c\u8ba1\u7b97\u51fd\u6570\u7684\u5b9a\u4e49\u548c\u65b9\u6cd5\u4e0d\u540c\u3002
PythonC++JavaC#GoSwiftJSTSDartRustCZig built_in_hash.pynum = 3\nhash_num = hash(num)\n# \u6574\u6570 3 \u7684\u54c8\u5e0c\u503c\u4e3a 3\n\nbol = True\nhash_bol = hash(bol)\n# \u5e03\u5c14\u91cf True \u7684\u54c8\u5e0c\u503c\u4e3a 1\n\ndec = 3.14159\nhash_dec = hash(dec)\n# \u5c0f\u6570 3.14159 \u7684\u54c8\u5e0c\u503c\u4e3a 326484311674566659\n\nstr = \"Hello \u7b97\u6cd5\"\nhash_str = hash(str)\n# \u5b57\u7b26\u4e32\u201cHello \u7b97\u6cd5\u201d\u7684\u54c8\u5e0c\u503c\u4e3a 4617003410720528961\n\ntup = (12836, \"\u5c0f\u54c8\")\nhash_tup = hash(tup)\n# \u5143\u7ec4 (12836, '\u5c0f\u54c8') \u7684\u54c8\u5e0c\u503c\u4e3a 1029005403108185979\n\nobj = ListNode(0)\nhash_obj = hash(obj)\n# \u8282\u70b9\u5bf9\u8c61 <ListNode object at 0x1058fd810> \u7684\u54c8\u5e0c\u503c\u4e3a 274267521\n
built_in_hash.cppint num = 3;\nsize_t hashNum = hash<int>()(num);\n// \u6574\u6570 3 \u7684\u54c8\u5e0c\u503c\u4e3a 3\n\nbool bol = true;\nsize_t hashBol = hash<bool>()(bol);\n// \u5e03\u5c14\u91cf 1 \u7684\u54c8\u5e0c\u503c\u4e3a 1\n\ndouble dec = 3.14159;\nsize_t hashDec = hash<double>()(dec);\n// \u5c0f\u6570 3.14159 \u7684\u54c8\u5e0c\u503c\u4e3a 4614256650576692846\n\nstring str = \"Hello \u7b97\u6cd5\";\nsize_t hashStr = hash<string>()(str);\n// \u5b57\u7b26\u4e32\u201cHello \u7b97\u6cd5\u201d\u7684\u54c8\u5e0c\u503c\u4e3a 15466937326284535026\n\n// \u5728 C++ \u4e2d\uff0c\u5185\u7f6e std:hash() \u4ec5\u63d0\u4f9b\u57fa\u672c\u6570\u636e\u7c7b\u578b\u7684\u54c8\u5e0c\u503c\u8ba1\u7b97\n// \u6570\u7ec4\u3001\u5bf9\u8c61\u7684\u54c8\u5e0c\u503c\u8ba1\u7b97\u9700\u8981\u81ea\u884c\u5b9e\u73b0\n
built_in_hash.javaint num = 3;\nint hashNum = Integer.hashCode(num);\n// \u6574\u6570 3 \u7684\u54c8\u5e0c\u503c\u4e3a 3\n\nboolean bol = true;\nint hashBol = Boolean.hashCode(bol);\n// \u5e03\u5c14\u91cf true \u7684\u54c8\u5e0c\u503c\u4e3a 1231\n\ndouble dec = 3.14159;\nint hashDec = Double.hashCode(dec);\n// \u5c0f\u6570 3.14159 \u7684\u54c8\u5e0c\u503c\u4e3a -1340954729\n\nString str = \"Hello \u7b97\u6cd5\";\nint hashStr = str.hashCode();\n// \u5b57\u7b26\u4e32\u201cHello \u7b97\u6cd5\u201d\u7684\u54c8\u5e0c\u503c\u4e3a -727081396\n\nObject[] arr = { 12836, \"\u5c0f\u54c8\" };\nint hashTup = Arrays.hashCode(arr);\n// \u6570\u7ec4 [12836, \u5c0f\u54c8] \u7684\u54c8\u5e0c\u503c\u4e3a 1151158\n\nListNode obj = new ListNode(0);\nint hashObj = obj.hashCode();\n// \u8282\u70b9\u5bf9\u8c61 utils.ListNode@7dc5e7b4 \u7684\u54c8\u5e0c\u503c\u4e3a 2110121908\n
built_in_hash.csint num = 3;\nint hashNum = num.GetHashCode();\n// \u6574\u6570 3 \u7684\u54c8\u5e0c\u503c\u4e3a 3;\n\nbool bol = true;\nint hashBol = bol.GetHashCode();\n// \u5e03\u5c14\u91cf true \u7684\u54c8\u5e0c\u503c\u4e3a 1;\n\ndouble dec = 3.14159;\nint hashDec = dec.GetHashCode();\n// \u5c0f\u6570 3.14159 \u7684\u54c8\u5e0c\u503c\u4e3a -1340954729;\n\nstring str = \"Hello \u7b97\u6cd5\";\nint hashStr = str.GetHashCode();\n// \u5b57\u7b26\u4e32\u201cHello \u7b97\u6cd5\u201d\u7684\u54c8\u5e0c\u503c\u4e3a -586107568;\n\nobject[] arr = [12836, \"\u5c0f\u54c8\"];\nint hashTup = arr.GetHashCode();\n// \u6570\u7ec4 [12836, \u5c0f\u54c8] \u7684\u54c8\u5e0c\u503c\u4e3a 42931033;\n\nListNode obj = new(0);\nint hashObj = obj.GetHashCode();\n// \u8282\u70b9\u5bf9\u8c61 0 \u7684\u54c8\u5e0c\u503c\u4e3a 39053774;\n
built_in_hash.go// Go \u672a\u63d0\u4f9b\u5185\u7f6e hash code \u51fd\u6570\n
built_in_hash.swiftlet num = 3\nlet hashNum = num.hashValue\n// \u6574\u6570 3 \u7684\u54c8\u5e0c\u503c\u4e3a 9047044699613009734\n\nlet bol = true\nlet hashBol = bol.hashValue\n// \u5e03\u5c14\u91cf true \u7684\u54c8\u5e0c\u503c\u4e3a -4431640247352757451\n\nlet dec = 3.14159\nlet hashDec = dec.hashValue\n// \u5c0f\u6570 3.14159 \u7684\u54c8\u5e0c\u503c\u4e3a -2465384235396674631\n\nlet str = \"Hello \u7b97\u6cd5\"\nlet hashStr = str.hashValue\n// \u5b57\u7b26\u4e32\u201cHello \u7b97\u6cd5\u201d\u7684\u54c8\u5e0c\u503c\u4e3a -7850626797806988787\n\nlet arr = [AnyHashable(12836), AnyHashable(\"\u5c0f\u54c8\")]\nlet hashTup = arr.hashValue\n// \u6570\u7ec4 [AnyHashable(12836), AnyHashable(\"\u5c0f\u54c8\")] \u7684\u54c8\u5e0c\u503c\u4e3a -2308633508154532996\n\nlet obj = ListNode(x: 0)\nlet hashObj = obj.hashValue\n// \u8282\u70b9\u5bf9\u8c61 utils.ListNode \u7684\u54c8\u5e0c\u503c\u4e3a -2434780518035996159\n
built_in_hash.js// JavaScript \u672a\u63d0\u4f9b\u5185\u7f6e hash code \u51fd\u6570\n
built_in_hash.ts// TypeScript \u672a\u63d0\u4f9b\u5185\u7f6e hash code \u51fd\u6570\n
built_in_hash.dartint num = 3;\nint hashNum = num.hashCode;\n// \u6574\u6570 3 \u7684\u54c8\u5e0c\u503c\u4e3a 34803\n\nbool bol = true;\nint hashBol = bol.hashCode;\n// \u5e03\u5c14\u503c true \u7684\u54c8\u5e0c\u503c\u4e3a 1231\n\ndouble dec = 3.14159;\nint hashDec = dec.hashCode;\n// \u5c0f\u6570 3.14159 \u7684\u54c8\u5e0c\u503c\u4e3a 2570631074981783\n\nString str = \"Hello \u7b97\u6cd5\";\nint hashStr = str.hashCode;\n// \u5b57\u7b26\u4e32\u201cHello \u7b97\u6cd5\u201d\u7684\u54c8\u5e0c\u503c\u4e3a 468167534\n\nList arr = [12836, \"\u5c0f\u54c8\"];\nint hashArr = arr.hashCode;\n// \u6570\u7ec4 [12836, \u5c0f\u54c8] \u7684\u54c8\u5e0c\u503c\u4e3a 976512528\n\nListNode obj = new ListNode(0);\nint hashObj = obj.hashCode;\n// \u8282\u70b9\u5bf9\u8c61 Instance of 'ListNode' \u7684\u54c8\u5e0c\u503c\u4e3a 1033450432\n
built_in_hash.rsuse std::collections::hash_map::DefaultHasher;\nuse std::hash::{Hash, Hasher};\n\nlet num = 3;\nlet mut num_hasher = DefaultHasher::new();\nnum.hash(&mut num_hasher);\nlet hash_num = num_hasher.finish();\n// \u6574\u6570 3 \u7684\u54c8\u5e0c\u503c\u4e3a 568126464209439262\n\nlet bol = true;\nlet mut bol_hasher = DefaultHasher::new();\nbol.hash(&mut bol_hasher);\nlet hash_bol = bol_hasher.finish();\n// \u5e03\u5c14\u91cf true \u7684\u54c8\u5e0c\u503c\u4e3a 4952851536318644461\n\nlet dec: f32 = 3.14159;\nlet mut dec_hasher = DefaultHasher::new();\ndec.to_bits().hash(&mut dec_hasher);\nlet hash_dec = dec_hasher.finish();\nprintln!(\"\u5c0f\u6570 {} \u7684\u54c8\u5e0c\u503c\u4e3a {}\", dec, hash_dec);\n// \u5c0f\u6570 3.14159 \u7684\u54c8\u5e0c\u503c\u4e3a 2566941990314602357\n\nlet str = \"Hello \u7b97\u6cd5\";\nlet mut str_hasher = DefaultHasher::new();\nstr.hash(&mut str_hasher);\nlet hash_str = str_hasher.finish();\n// \u5b57\u7b26\u4e32\u201cHello \u7b97\u6cd5\u201d\u7684\u54c8\u5e0c\u503c\u4e3a 16092673739211250988\n\nlet arr = (&12836, &\"\u5c0f\u54c8\");\nlet mut tup_hasher = DefaultHasher::new();\narr.hash(&mut tup_hasher);\nlet hash_tup = tup_hasher.finish();\n// \u5143\u7ec4 (12836, \"\u5c0f\u54c8\") \u7684\u54c8\u5e0c\u503c\u4e3a 1885128010422702749\n\nlet node = ListNode::new(42);\nlet mut hasher = DefaultHasher::new();\nnode.borrow().val.hash(&mut hasher);\nlet hash = hasher.finish();\n// \u8282\u70b9\u5bf9\u8c61 RefCell { value: ListNode { val: 42, next: None } } \u7684\u54c8\u5e0c\u503c\u4e3a15387811073369036852\n
built_in_hash.c// C \u672a\u63d0\u4f9b\u5185\u7f6e hash code \u51fd\u6570\n
built_in_hash.zig\n
\u5728\u8bb8\u591a\u7f16\u7a0b\u8bed\u8a00\u4e2d\uff0c\u53ea\u6709\u4e0d\u53ef\u53d8\u5bf9\u8c61\u624d\u53ef\u4f5c\u4e3a\u54c8\u5e0c\u8868\u7684 key
\u3002\u5047\u5982\u6211\u4eec\u5c06\u5217\u8868\uff08\u52a8\u6001\u6570\u7ec4\uff09\u4f5c\u4e3a key
\uff0c\u5f53\u5217\u8868\u7684\u5185\u5bb9\u53d1\u751f\u53d8\u5316\u65f6\uff0c\u5b83\u7684\u54c8\u5e0c\u503c\u4e5f\u968f\u4e4b\u6539\u53d8\uff0c\u6211\u4eec\u5c31\u65e0\u6cd5\u5728\u54c8\u5e0c\u8868\u4e2d\u67e5\u8be2\u5230\u539f\u5148\u7684 value
\u4e86\u3002
\u867d\u7136\u81ea\u5b9a\u4e49\u5bf9\u8c61\uff08\u6bd4\u5982\u94fe\u8868\u8282\u70b9\uff09\u7684\u6210\u5458\u53d8\u91cf\u662f\u53ef\u53d8\u7684\uff0c\u4f46\u5b83\u662f\u53ef\u54c8\u5e0c\u7684\u3002\u8fd9\u662f\u56e0\u4e3a\u5bf9\u8c61\u7684\u54c8\u5e0c\u503c\u901a\u5e38\u662f\u57fa\u4e8e\u5185\u5b58\u5730\u5740\u751f\u6210\u7684\uff0c\u5373\u4f7f\u5bf9\u8c61\u7684\u5185\u5bb9\u53d1\u751f\u4e86\u53d8\u5316\uff0c\u4f46\u5b83\u7684\u5185\u5b58\u5730\u5740\u4e0d\u53d8\uff0c\u54c8\u5e0c\u503c\u4ecd\u7136\u662f\u4e0d\u53d8\u7684\u3002
\u7ec6\u5fc3\u7684\u4f60\u53ef\u80fd\u53d1\u73b0\u5728\u4e0d\u540c\u63a7\u5236\u53f0\u4e2d\u8fd0\u884c\u7a0b\u5e8f\u65f6\uff0c\u8f93\u51fa\u7684\u54c8\u5e0c\u503c\u662f\u4e0d\u540c\u7684\u3002\u8fd9\u662f\u56e0\u4e3a Python \u89e3\u91ca\u5668\u5728\u6bcf\u6b21\u542f\u52a8\u65f6\uff0c\u90fd\u4f1a\u4e3a\u5b57\u7b26\u4e32\u54c8\u5e0c\u51fd\u6570\u52a0\u5165\u4e00\u4e2a\u968f\u673a\u7684\u76d0\uff08Salt\uff09\u503c\u3002\u8fd9\u79cd\u505a\u6cd5\u53ef\u4ee5\u6709\u6548\u9632\u6b62 HashDoS \u653b\u51fb\uff0c\u63d0\u5347\u54c8\u5e0c\u7b97\u6cd5\u7684\u5b89\u5168\u6027\u3002
"},{"location":"chapter_hashing/hash_collision/","title":"6.2 \u00a0 \u54c8\u5e0c\u51b2\u7a81","text":"\u4e0a\u4e00\u8282\u63d0\u5230\uff0c\u901a\u5e38\u60c5\u51b5\u4e0b\u54c8\u5e0c\u51fd\u6570\u7684\u8f93\u5165\u7a7a\u95f4\u8fdc\u5927\u4e8e\u8f93\u51fa\u7a7a\u95f4\uff0c\u56e0\u6b64\u7406\u8bba\u4e0a\u54c8\u5e0c\u51b2\u7a81\u662f\u4e0d\u53ef\u907f\u514d\u7684\u3002\u6bd4\u5982\uff0c\u8f93\u5165\u7a7a\u95f4\u4e3a\u5168\u4f53\u6574\u6570\uff0c\u8f93\u51fa\u7a7a\u95f4\u4e3a\u6570\u7ec4\u5bb9\u91cf\u5927\u5c0f\uff0c\u5219\u5fc5\u7136\u6709\u591a\u4e2a\u6574\u6570\u6620\u5c04\u81f3\u540c\u4e00\u6876\u7d22\u5f15\u3002
\u54c8\u5e0c\u51b2\u7a81\u4f1a\u5bfc\u81f4\u67e5\u8be2\u7ed3\u679c\u9519\u8bef\uff0c\u4e25\u91cd\u5f71\u54cd\u54c8\u5e0c\u8868\u7684\u53ef\u7528\u6027\u3002\u4e3a\u89e3\u51b3\u8be5\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u6bcf\u5f53\u9047\u5230\u54c8\u5e0c\u51b2\u7a81\u5c31\u8fdb\u884c\u54c8\u5e0c\u8868\u6269\u5bb9\uff0c\u76f4\u81f3\u51b2\u7a81\u6d88\u5931\u4e3a\u6b62\u3002\u6b64\u65b9\u6cd5\u7b80\u5355\u7c97\u66b4\u4e14\u6709\u6548\uff0c\u4f46\u6548\u7387\u592a\u4f4e\uff0c\u56e0\u4e3a\u54c8\u5e0c\u8868\u6269\u5bb9\u9700\u8981\u8fdb\u884c\u5927\u91cf\u7684\u6570\u636e\u642c\u8fd0\u4e0e\u54c8\u5e0c\u503c\u8ba1\u7b97\u3002\u4e3a\u4e86\u63d0\u5347\u6548\u7387\uff0c\u6211\u4eec\u53ef\u4ee5\u91c7\u7528\u4ee5\u4e0b\u7b56\u7565\u3002
\u54c8\u5e0c\u8868\u7684\u7ed3\u6784\u6539\u826f\u65b9\u6cd5\u4e3b\u8981\u5305\u62ec\u201c\u94fe\u5f0f\u5730\u5740\u201d\u548c\u201c\u5f00\u653e\u5bfb\u5740\u201d\u3002
"},{"location":"chapter_hashing/hash_collision/#621","title":"6.2.1 \u00a0 \u94fe\u5f0f\u5730\u5740","text":"\u5728\u539f\u59cb\u54c8\u5e0c\u8868\u4e2d\uff0c\u6bcf\u4e2a\u6876\u4ec5\u80fd\u5b58\u50a8\u4e00\u4e2a\u952e\u503c\u5bf9\u3002\u300c\u94fe\u5f0f\u5730\u5740 separate chaining\u300d\u5c06\u5355\u4e2a\u5143\u7d20\u8f6c\u6362\u4e3a\u94fe\u8868\uff0c\u5c06\u952e\u503c\u5bf9\u4f5c\u4e3a\u94fe\u8868\u8282\u70b9\uff0c\u5c06\u6240\u6709\u53d1\u751f\u51b2\u7a81\u7684\u952e\u503c\u5bf9\u90fd\u5b58\u50a8\u5728\u540c\u4e00\u94fe\u8868\u4e2d\u3002\u56fe 6-5 \u5c55\u793a\u4e86\u4e00\u4e2a\u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868\u7684\u4f8b\u5b50\u3002
\u56fe 6-5 \u00a0 \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868
\u57fa\u4e8e\u94fe\u5f0f\u5730\u5740\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868\u7684\u64cd\u4f5c\u65b9\u6cd5\u53d1\u751f\u4e86\u4ee5\u4e0b\u53d8\u5316\u3002
key
\uff0c\u7ecf\u8fc7\u54c8\u5e0c\u51fd\u6570\u5f97\u5230\u6876\u7d22\u5f15\uff0c\u5373\u53ef\u8bbf\u95ee\u94fe\u8868\u5934\u8282\u70b9\uff0c\u7136\u540e\u904d\u5386\u94fe\u8868\u5e76\u5bf9\u6bd4 key
\u4ee5\u67e5\u627e\u76ee\u6807\u952e\u503c\u5bf9\u3002\u94fe\u5f0f\u5730\u5740\u5b58\u5728\u4ee5\u4e0b\u5c40\u9650\u6027\u3002
\u4ee5\u4e0b\u4ee3\u7801\u7ed9\u51fa\u4e86\u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868\u7684\u7b80\u5355\u5b9e\u73b0\uff0c\u9700\u8981\u6ce8\u610f\u4e24\u70b9\u3002
class HashMapChaining:\n \"\"\"\u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868\"\"\"\n\n def __init__(self):\n \"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n self.size = 0 # \u952e\u503c\u5bf9\u6570\u91cf\n self.capacity = 4 # \u54c8\u5e0c\u8868\u5bb9\u91cf\n self.load_thres = 2.0 / 3.0 # \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n self.extend_ratio = 2 # \u6269\u5bb9\u500d\u6570\n self.buckets = [[] for _ in range(self.capacity)] # \u6876\u6570\u7ec4\n\n def hash_func(self, key: int) -> int:\n \"\"\"\u54c8\u5e0c\u51fd\u6570\"\"\"\n return key % self.capacity\n\n def load_factor(self) -> float:\n \"\"\"\u8d1f\u8f7d\u56e0\u5b50\"\"\"\n return self.size / self.capacity\n\n def get(self, key: int) -> str | None:\n \"\"\"\u67e5\u8be2\u64cd\u4f5c\"\"\"\n index = self.hash_func(key)\n bucket = self.buckets[index]\n # \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \u5219\u8fd4\u56de\u5bf9\u5e94 val\n for pair in bucket:\n if pair.key == key:\n return pair.val\n # \u82e5\u672a\u627e\u5230 key \u5219\u8fd4\u56de None\n return None\n\n def put(self, key: int, val: str):\n \"\"\"\u6dfb\u52a0\u64cd\u4f5c\"\"\"\n # \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if self.load_factor() > self.load_thres:\n self.extend()\n index = self.hash_func(key)\n bucket = self.buckets[index]\n # \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\n for pair in bucket:\n if pair.key == key:\n pair.val = val\n return\n # \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\n pair = Pair(key, val)\n bucket.append(pair)\n self.size += 1\n\n def remove(self, key: int):\n \"\"\"\u5220\u9664\u64cd\u4f5c\"\"\"\n index = self.hash_func(key)\n bucket = self.buckets[index]\n # \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\n for pair in bucket:\n if pair.key == key:\n bucket.remove(pair)\n self.size -= 1\n break\n\n def extend(self):\n \"\"\"\u6269\u5bb9\u54c8\u5e0c\u8868\"\"\"\n # \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n buckets = self.buckets\n # \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n self.capacity *= self.extend_ratio\n self.buckets = [[] for _ in range(self.capacity)]\n self.size = 0\n # \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n for bucket in buckets:\n for pair in bucket:\n self.put(pair.key, pair.val)\n\n def print(self):\n \"\"\"\u6253\u5370\u54c8\u5e0c\u8868\"\"\"\n for bucket in self.buckets:\n res = []\n for pair in bucket:\n res.append(str(pair.key) + \" -> \" + pair.val)\n print(res)\n
hash_map_chaining.cpp/* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\nclass HashMapChaining {\n private:\n int size; // \u952e\u503c\u5bf9\u6570\u91cf\n int capacity; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n double loadThres; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n int extendRatio; // \u6269\u5bb9\u500d\u6570\n vector<vector<Pair *>> buckets; // \u6876\u6570\u7ec4\n\n public:\n /* \u6784\u9020\u65b9\u6cd5 */\n HashMapChaining() : size(0), capacity(4), loadThres(2.0 / 3.0), extendRatio(2) {\n buckets.resize(capacity);\n }\n\n /* \u6790\u6784\u65b9\u6cd5 */\n ~HashMapChaining() {\n for (auto &bucket : buckets) {\n for (Pair *pair : bucket) {\n // \u91ca\u653e\u5185\u5b58\n delete pair;\n }\n }\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n int hashFunc(int key) {\n return key % capacity;\n }\n\n /* \u8d1f\u8f7d\u56e0\u5b50 */\n double loadFactor() {\n return (double)size / (double)capacity;\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n string get(int key) {\n int index = hashFunc(key);\n // \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \u5219\u8fd4\u56de\u5bf9\u5e94 val\n for (Pair *pair : buckets[index]) {\n if (pair->key == key) {\n return pair->val;\n }\n }\n // \u82e5\u672a\u627e\u5230 key \u5219\u8fd4\u56de\u7a7a\u5b57\u7b26\u4e32\n return \"\";\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n void put(int key, string val) {\n // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if (loadFactor() > loadThres) {\n extend();\n }\n int index = hashFunc(key);\n // \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\n for (Pair *pair : buckets[index]) {\n if (pair->key == key) {\n pair->val = val;\n return;\n }\n }\n // \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\n buckets[index].push_back(new Pair(key, val));\n size++;\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n void remove(int key) {\n int index = hashFunc(key);\n auto &bucket = buckets[index];\n // \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\n for (int i = 0; i < bucket.size(); i++) {\n if (bucket[i]->key == key) {\n Pair *tmp = bucket[i];\n bucket.erase(bucket.begin() + i); // \u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\n delete tmp; // \u91ca\u653e\u5185\u5b58\n size--;\n return;\n }\n }\n }\n\n /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n void extend() {\n // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n vector<vector<Pair *>> bucketsTmp = buckets;\n // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n capacity *= extendRatio;\n buckets.clear();\n buckets.resize(capacity);\n size = 0;\n // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n for (auto &bucket : bucketsTmp) {\n for (Pair *pair : bucket) {\n put(pair->key, pair->val);\n // \u91ca\u653e\u5185\u5b58\n delete pair;\n }\n }\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n void print() {\n for (auto &bucket : buckets) {\n cout << \"[\";\n for (Pair *pair : bucket) {\n cout << pair->key << \" -> \" << pair->val << \", \";\n }\n cout << \"]\\n\";\n }\n }\n};\n
hash_map_chaining.java/* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\nclass HashMapChaining {\n int size; // \u952e\u503c\u5bf9\u6570\u91cf\n int capacity; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n double loadThres; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n int extendRatio; // \u6269\u5bb9\u500d\u6570\n List<List<Pair>> buckets; // \u6876\u6570\u7ec4\n\n /* \u6784\u9020\u65b9\u6cd5 */\n public HashMapChaining() {\n size = 0;\n capacity = 4;\n loadThres = 2.0 / 3.0;\n extendRatio = 2;\n buckets = new ArrayList<>(capacity);\n for (int i = 0; i < capacity; i++) {\n buckets.add(new ArrayList<>());\n }\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n int hashFunc(int key) {\n return key % capacity;\n }\n\n /* \u8d1f\u8f7d\u56e0\u5b50 */\n double loadFactor() {\n return (double) size / capacity;\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n String get(int key) {\n int index = hashFunc(key);\n List<Pair> bucket = buckets.get(index);\n // \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \u5219\u8fd4\u56de\u5bf9\u5e94 val\n for (Pair pair : bucket) {\n if (pair.key == key) {\n return pair.val;\n }\n }\n // \u82e5\u672a\u627e\u5230 key \u5219\u8fd4\u56de null\n return null;\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n void put(int key, String val) {\n // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if (loadFactor() > loadThres) {\n extend();\n }\n int index = hashFunc(key);\n List<Pair> bucket = buckets.get(index);\n // \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\n for (Pair pair : bucket) {\n if (pair.key == key) {\n pair.val = val;\n return;\n }\n }\n // \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\n Pair pair = new Pair(key, val);\n bucket.add(pair);\n size++;\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n void remove(int key) {\n int index = hashFunc(key);\n List<Pair> bucket = buckets.get(index);\n // \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\n for (Pair pair : bucket) {\n if (pair.key == key) {\n bucket.remove(pair);\n size--;\n break;\n }\n }\n }\n\n /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n void extend() {\n // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n List<List<Pair>> bucketsTmp = buckets;\n // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n capacity *= extendRatio;\n buckets = new ArrayList<>(capacity);\n for (int i = 0; i < capacity; i++) {\n buckets.add(new ArrayList<>());\n }\n size = 0;\n // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n for (List<Pair> bucket : bucketsTmp) {\n for (Pair pair : bucket) {\n put(pair.key, pair.val);\n }\n }\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n void print() {\n for (List<Pair> bucket : buckets) {\n List<String> res = new ArrayList<>();\n for (Pair pair : bucket) {\n res.add(pair.key + \" -> \" + pair.val);\n }\n System.out.println(res);\n }\n }\n}\n
hash_map_chaining.cs/* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\nclass HashMapChaining {\n int size; // \u952e\u503c\u5bf9\u6570\u91cf\n int capacity; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n double loadThres; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n int extendRatio; // \u6269\u5bb9\u500d\u6570\n List<List<Pair>> buckets; // \u6876\u6570\u7ec4\n\n /* \u6784\u9020\u65b9\u6cd5 */\n public HashMapChaining() {\n size = 0;\n capacity = 4;\n loadThres = 2.0 / 3.0;\n extendRatio = 2;\n buckets = new List<List<Pair>>(capacity);\n for (int i = 0; i < capacity; i++) {\n buckets.Add([]);\n }\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n int HashFunc(int key) {\n return key % capacity;\n }\n\n /* \u8d1f\u8f7d\u56e0\u5b50 */\n double LoadFactor() {\n return (double)size / capacity;\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n public string? Get(int key) {\n int index = HashFunc(key);\n // \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \u5219\u8fd4\u56de\u5bf9\u5e94 val\n foreach (Pair pair in buckets[index]) {\n if (pair.key == key) {\n return pair.val;\n }\n }\n // \u82e5\u672a\u627e\u5230 key \u5219\u8fd4\u56de null\n return null;\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n public void Put(int key, string val) {\n // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if (LoadFactor() > loadThres) {\n Extend();\n }\n int index = HashFunc(key);\n // \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\n foreach (Pair pair in buckets[index]) {\n if (pair.key == key) {\n pair.val = val;\n return;\n }\n }\n // \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\n buckets[index].Add(new Pair(key, val));\n size++;\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n public void Remove(int key) {\n int index = HashFunc(key);\n // \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\n foreach (Pair pair in buckets[index].ToList()) {\n if (pair.key == key) {\n buckets[index].Remove(pair);\n size--;\n break;\n }\n }\n }\n\n /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n void Extend() {\n // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n List<List<Pair>> bucketsTmp = buckets;\n // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n capacity *= extendRatio;\n buckets = new List<List<Pair>>(capacity);\n for (int i = 0; i < capacity; i++) {\n buckets.Add([]);\n }\n size = 0;\n // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n foreach (List<Pair> bucket in bucketsTmp) {\n foreach (Pair pair in bucket) {\n Put(pair.key, pair.val);\n }\n }\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n public void Print() {\n foreach (List<Pair> bucket in buckets) {\n List<string> res = [];\n foreach (Pair pair in bucket) {\n res.Add(pair.key + \" -> \" + pair.val);\n }\n foreach (string kv in res) {\n Console.WriteLine(kv);\n }\n }\n }\n}\n
hash_map_chaining.go/* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\ntype hashMapChaining struct {\n size int // \u952e\u503c\u5bf9\u6570\u91cf\n capacity int // \u54c8\u5e0c\u8868\u5bb9\u91cf\n loadThres float64 // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n extendRatio int // \u6269\u5bb9\u500d\u6570\n buckets [][]pair // \u6876\u6570\u7ec4\n}\n\n/* \u6784\u9020\u65b9\u6cd5 */\nfunc newHashMapChaining() *hashMapChaining {\n buckets := make([][]pair, 4)\n for i := 0; i < 4; i++ {\n buckets[i] = make([]pair, 0)\n }\n return &hashMapChaining{\n size: 0,\n capacity: 4,\n loadThres: 2.0 / 3.0,\n extendRatio: 2,\n buckets: buckets,\n }\n}\n\n/* \u54c8\u5e0c\u51fd\u6570 */\nfunc (m *hashMapChaining) hashFunc(key int) int {\n return key % m.capacity\n}\n\n/* \u8d1f\u8f7d\u56e0\u5b50 */\nfunc (m *hashMapChaining) loadFactor() float64 {\n return float64(m.size / m.capacity)\n}\n\n/* \u67e5\u8be2\u64cd\u4f5c */\nfunc (m *hashMapChaining) get(key int) string {\n idx := m.hashFunc(key)\n bucket := m.buckets[idx]\n // \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \u5219\u8fd4\u56de\u5bf9\u5e94 val\n for _, p := range bucket {\n if p.key == key {\n return p.val\n }\n }\n // \u82e5\u672a\u627e\u5230 key \u5219\u8fd4\u56de\u7a7a\u5b57\u7b26\u4e32\n return \"\"\n}\n\n/* \u6dfb\u52a0\u64cd\u4f5c */\nfunc (m *hashMapChaining) put(key int, val string) {\n // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if m.loadFactor() > m.loadThres {\n m.extend()\n }\n idx := m.hashFunc(key)\n // \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\n for _, p := range m.buckets[idx] {\n if p.key == key {\n p.val = val\n return\n }\n }\n // \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\n p := pair{\n key: key,\n val: val,\n }\n m.buckets[idx] = append(m.buckets[idx], p)\n m.size += 1\n}\n\n/* \u5220\u9664\u64cd\u4f5c */\nfunc (m *hashMapChaining) remove(key int) {\n idx := m.hashFunc(key)\n // \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\n for i, p := range m.buckets[idx] {\n if p.key == key {\n // \u5207\u7247\u5220\u9664\n m.buckets[idx] = append(m.buckets[idx][:i], m.buckets[idx][i+1:]...)\n m.size -= 1\n break\n }\n }\n}\n\n/* \u6269\u5bb9\u54c8\u5e0c\u8868 */\nfunc (m *hashMapChaining) extend() {\n // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n tmpBuckets := make([][]pair, len(m.buckets))\n for i := 0; i < len(m.buckets); i++ {\n tmpBuckets[i] = make([]pair, len(m.buckets[i]))\n copy(tmpBuckets[i], m.buckets[i])\n }\n // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n m.capacity *= m.extendRatio\n m.buckets = make([][]pair, m.capacity)\n for i := 0; i < m.capacity; i++ {\n m.buckets[i] = make([]pair, 0)\n }\n m.size = 0\n // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n for _, bucket := range tmpBuckets {\n for _, p := range bucket {\n m.put(p.key, p.val)\n }\n }\n}\n\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nfunc (m *hashMapChaining) print() {\n var builder strings.Builder\n\n for _, bucket := range m.buckets {\n builder.WriteString(\"[\")\n for _, p := range bucket {\n builder.WriteString(strconv.Itoa(p.key) + \" -> \" + p.val + \" \")\n }\n builder.WriteString(\"]\")\n fmt.Println(builder.String())\n builder.Reset()\n }\n}\n
hash_map_chaining.swift/* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\nclass HashMapChaining {\n var size: Int // \u952e\u503c\u5bf9\u6570\u91cf\n var capacity: Int // \u54c8\u5e0c\u8868\u5bb9\u91cf\n var loadThres: Double // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n var extendRatio: Int // \u6269\u5bb9\u500d\u6570\n var buckets: [[Pair]] // \u6876\u6570\u7ec4\n\n /* \u6784\u9020\u65b9\u6cd5 */\n init() {\n size = 0\n capacity = 4\n loadThres = 2.0 / 3.0\n extendRatio = 2\n buckets = Array(repeating: [], count: capacity)\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n func hashFunc(key: Int) -> Int {\n key % capacity\n }\n\n /* \u8d1f\u8f7d\u56e0\u5b50 */\n func loadFactor() -> Double {\n Double(size / capacity)\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n func get(key: Int) -> String? {\n let index = hashFunc(key: key)\n let bucket = buckets[index]\n // \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \u5219\u8fd4\u56de\u5bf9\u5e94 val\n for pair in bucket {\n if pair.key == key {\n return pair.val\n }\n }\n // \u82e5\u672a\u627e\u5230 key \u5219\u8fd4\u56de nil\n return nil\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n func put(key: Int, val: String) {\n // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if loadFactor() > loadThres {\n extend()\n }\n let index = hashFunc(key: key)\n let bucket = buckets[index]\n // \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\n for pair in bucket {\n if pair.key == key {\n pair.val = val\n return\n }\n }\n // \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\n let pair = Pair(key: key, val: val)\n buckets[index].append(pair)\n size += 1\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n func remove(key: Int) {\n let index = hashFunc(key: key)\n let bucket = buckets[index]\n // \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\n for (pairIndex, pair) in bucket.enumerated() {\n if pair.key == key {\n buckets[index].remove(at: pairIndex)\n }\n }\n size -= 1\n }\n\n /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n func extend() {\n // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n let bucketsTmp = buckets\n // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n capacity *= extendRatio\n buckets = Array(repeating: [], count: capacity)\n size = 0\n // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n for bucket in bucketsTmp {\n for pair in bucket {\n put(key: pair.key, val: pair.val)\n }\n }\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n func print() {\n for bucket in buckets {\n let res = bucket.map { \"\\($0.key) -> \\($0.val)\" }\n Swift.print(res)\n }\n }\n}\n
hash_map_chaining.js/* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\nclass HashMapChaining {\n #size; // \u952e\u503c\u5bf9\u6570\u91cf\n #capacity; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n #loadThres; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n #extendRatio; // \u6269\u5bb9\u500d\u6570\n #buckets; // \u6876\u6570\u7ec4\n\n /* \u6784\u9020\u65b9\u6cd5 */\n constructor() {\n this.#size = 0;\n this.#capacity = 4;\n this.#loadThres = 2.0 / 3.0;\n this.#extendRatio = 2;\n this.#buckets = new Array(this.#capacity).fill(null).map((x) => []);\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n #hashFunc(key) {\n return key % this.#capacity;\n }\n\n /* \u8d1f\u8f7d\u56e0\u5b50 */\n #loadFactor() {\n return this.#size / this.#capacity;\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n get(key) {\n const index = this.#hashFunc(key);\n const bucket = this.#buckets[index];\n // \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \u5219\u8fd4\u56de\u5bf9\u5e94 val\n for (const pair of bucket) {\n if (pair.key === key) {\n return pair.val;\n }\n }\n // \u82e5\u672a\u627e\u5230 key \u5219\u8fd4\u56de null\n return null;\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n put(key, val) {\n // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if (this.#loadFactor() > this.#loadThres) {\n this.#extend();\n }\n const index = this.#hashFunc(key);\n const bucket = this.#buckets[index];\n // \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\n for (const pair of bucket) {\n if (pair.key === key) {\n pair.val = val;\n return;\n }\n }\n // \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\n const pair = new Pair(key, val);\n bucket.push(pair);\n this.#size++;\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n remove(key) {\n const index = this.#hashFunc(key);\n let bucket = this.#buckets[index];\n // \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\n for (let i = 0; i < bucket.length; i++) {\n if (bucket[i].key === key) {\n bucket.splice(i, 1);\n this.#size--;\n break;\n }\n }\n }\n\n /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n #extend() {\n // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n const bucketsTmp = this.#buckets;\n // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n this.#capacity *= this.#extendRatio;\n this.#buckets = new Array(this.#capacity).fill(null).map((x) => []);\n this.#size = 0;\n // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n for (const bucket of bucketsTmp) {\n for (const pair of bucket) {\n this.put(pair.key, pair.val);\n }\n }\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n print() {\n for (const bucket of this.#buckets) {\n let res = [];\n for (const pair of bucket) {\n res.push(pair.key + ' -> ' + pair.val);\n }\n console.log(res);\n }\n }\n}\n
hash_map_chaining.ts/* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\nclass HashMapChaining {\n #size: number; // \u952e\u503c\u5bf9\u6570\u91cf\n #capacity: number; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n #loadThres: number; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n #extendRatio: number; // \u6269\u5bb9\u500d\u6570\n #buckets: Pair[][]; // \u6876\u6570\u7ec4\n\n /* \u6784\u9020\u65b9\u6cd5 */\n constructor() {\n this.#size = 0;\n this.#capacity = 4;\n this.#loadThres = 2.0 / 3.0;\n this.#extendRatio = 2;\n this.#buckets = new Array(this.#capacity).fill(null).map((x) => []);\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n #hashFunc(key: number): number {\n return key % this.#capacity;\n }\n\n /* \u8d1f\u8f7d\u56e0\u5b50 */\n #loadFactor(): number {\n return this.#size / this.#capacity;\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n get(key: number): string | null {\n const index = this.#hashFunc(key);\n const bucket = this.#buckets[index];\n // \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \u5219\u8fd4\u56de\u5bf9\u5e94 val\n for (const pair of bucket) {\n if (pair.key === key) {\n return pair.val;\n }\n }\n // \u82e5\u672a\u627e\u5230 key \u5219\u8fd4\u56de null\n return null;\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n put(key: number, val: string): void {\n // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if (this.#loadFactor() > this.#loadThres) {\n this.#extend();\n }\n const index = this.#hashFunc(key);\n const bucket = this.#buckets[index];\n // \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\n for (const pair of bucket) {\n if (pair.key === key) {\n pair.val = val;\n return;\n }\n }\n // \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\n const pair = new Pair(key, val);\n bucket.push(pair);\n this.#size++;\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n remove(key: number): void {\n const index = this.#hashFunc(key);\n let bucket = this.#buckets[index];\n // \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\n for (let i = 0; i < bucket.length; i++) {\n if (bucket[i].key === key) {\n bucket.splice(i, 1);\n this.#size--;\n break;\n }\n }\n }\n\n /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n #extend(): void {\n // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n const bucketsTmp = this.#buckets;\n // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n this.#capacity *= this.#extendRatio;\n this.#buckets = new Array(this.#capacity).fill(null).map((x) => []);\n this.#size = 0;\n // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n for (const bucket of bucketsTmp) {\n for (const pair of bucket) {\n this.put(pair.key, pair.val);\n }\n }\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n print(): void {\n for (const bucket of this.#buckets) {\n let res = [];\n for (const pair of bucket) {\n res.push(pair.key + ' -> ' + pair.val);\n }\n console.log(res);\n }\n }\n}\n
hash_map_chaining.dart/* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\nclass HashMapChaining {\n late int size; // \u952e\u503c\u5bf9\u6570\u91cf\n late int capacity; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n late double loadThres; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n late int extendRatio; // \u6269\u5bb9\u500d\u6570\n late List<List<Pair>> buckets; // \u6876\u6570\u7ec4\n\n /* \u6784\u9020\u65b9\u6cd5 */\n HashMapChaining() {\n size = 0;\n capacity = 4;\n loadThres = 2.0 / 3.0;\n extendRatio = 2;\n buckets = List.generate(capacity, (_) => []);\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n int hashFunc(int key) {\n return key % capacity;\n }\n\n /* \u8d1f\u8f7d\u56e0\u5b50 */\n double loadFactor() {\n return size / capacity;\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n String? get(int key) {\n int index = hashFunc(key);\n List<Pair> bucket = buckets[index];\n // \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \u5219\u8fd4\u56de\u5bf9\u5e94 val\n for (Pair pair in bucket) {\n if (pair.key == key) {\n return pair.val;\n }\n }\n // \u82e5\u672a\u627e\u5230 key \u5219\u8fd4\u56de null\n return null;\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n void put(int key, String val) {\n // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if (loadFactor() > loadThres) {\n extend();\n }\n int index = hashFunc(key);\n List<Pair> bucket = buckets[index];\n // \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\n for (Pair pair in bucket) {\n if (pair.key == key) {\n pair.val = val;\n return;\n }\n }\n // \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\n Pair pair = Pair(key, val);\n bucket.add(pair);\n size++;\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n void remove(int key) {\n int index = hashFunc(key);\n List<Pair> bucket = buckets[index];\n // \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\n for (Pair pair in bucket) {\n if (pair.key == key) {\n bucket.remove(pair);\n size--;\n break;\n }\n }\n }\n\n /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n void extend() {\n // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n List<List<Pair>> bucketsTmp = buckets;\n // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n capacity *= extendRatio;\n buckets = List.generate(capacity, (_) => []);\n size = 0;\n // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n for (List<Pair> bucket in bucketsTmp) {\n for (Pair pair in bucket) {\n put(pair.key, pair.val);\n }\n }\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n void printHashMap() {\n for (List<Pair> bucket in buckets) {\n List<String> res = [];\n for (Pair pair in bucket) {\n res.add(\"${pair.key} -> ${pair.val}\");\n }\n print(res);\n }\n }\n}\n
hash_map_chaining.rs/* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\nstruct HashMapChaining {\n size: i32,\n capacity: i32,\n load_thres: f32,\n extend_ratio: i32,\n buckets: Vec<Vec<Pair>>,\n}\n\nimpl HashMapChaining {\n /* \u6784\u9020\u65b9\u6cd5 */\n fn new() -> Self {\n Self {\n size: 0,\n capacity: 4,\n load_thres: 2.0 / 3.0,\n extend_ratio: 2,\n buckets: vec![vec![]; 4],\n }\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n fn hash_func(&self, key: i32) -> usize {\n key as usize % self.capacity as usize\n }\n\n /* \u8d1f\u8f7d\u56e0\u5b50 */\n fn load_factor(&self) -> f32 {\n self.size as f32 / self.capacity as f32\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n fn remove(&mut self, key: i32) -> Option<String> {\n let index = self.hash_func(key);\n let bucket = &mut self.buckets[index];\n\n // \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\n for i in 0..bucket.len() {\n if bucket[i].key == key {\n let pair = bucket.remove(i);\n self.size -= 1;\n return Some(pair.val);\n }\n }\n\n // \u82e5\u672a\u627e\u5230 key \u5219\u8fd4\u56de None\n None\n }\n\n /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n fn extend(&mut self) {\n // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n let buckets_tmp = std::mem::replace(&mut self.buckets, vec![]);\n\n // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n self.capacity *= self.extend_ratio;\n self.buckets = vec![Vec::new(); self.capacity as usize];\n self.size = 0;\n\n // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n for bucket in buckets_tmp {\n for pair in bucket {\n self.put(pair.key, pair.val);\n }\n }\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n fn print(&self) {\n for bucket in &self.buckets {\n let mut res = Vec::new();\n for pair in bucket {\n res.push(format!(\"{} -> {}\", pair.key, pair.val));\n }\n println!(\"{:?}\", res);\n }\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n fn put(&mut self, key: i32, val: String) {\n // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if self.load_factor() > self.load_thres {\n self.extend();\n }\n\n let index = self.hash_func(key);\n let bucket = &mut self.buckets[index];\n\n // \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\n for pair in bucket {\n if pair.key == key {\n pair.val = val.clone();\n return;\n }\n }\n let bucket = &mut self.buckets[index];\n\n // \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\n let pair = Pair {\n key,\n val: val.clone(),\n };\n bucket.push(pair);\n self.size += 1;\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n fn get(&self, key: i32) -> Option<&str> {\n let index = self.hash_func(key);\n let bucket = &self.buckets[index];\n\n // \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \u5219\u8fd4\u56de\u5bf9\u5e94 val\n for pair in bucket {\n if pair.key == key {\n return Some(&pair.val);\n }\n }\n\n // \u82e5\u672a\u627e\u5230 key \u5219\u8fd4\u56de None\n None\n }\n}\n
hash_map_chaining.c/* \u94fe\u8868\u8282\u70b9 */\ntypedef struct Node {\n Pair *pair;\n struct Node *next;\n} Node;\n\n/* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\ntypedef struct {\n int size; // \u952e\u503c\u5bf9\u6570\u91cf\n int capacity; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n double loadThres; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n int extendRatio; // \u6269\u5bb9\u500d\u6570\n Node **buckets; // \u6876\u6570\u7ec4\n} HashMapChaining;\n\n/* \u6784\u9020\u51fd\u6570 */\nHashMapChaining *newHashMapChaining() {\n HashMapChaining *hashMap = (HashMapChaining *)malloc(sizeof(HashMapChaining));\n hashMap->size = 0;\n hashMap->capacity = 4;\n hashMap->loadThres = 2.0 / 3.0;\n hashMap->extendRatio = 2;\n hashMap->buckets = (Node **)malloc(hashMap->capacity * sizeof(Node *));\n for (int i = 0; i < hashMap->capacity; i++) {\n hashMap->buckets[i] = NULL;\n }\n return hashMap;\n}\n\n/* \u6790\u6784\u51fd\u6570 */\nvoid delHashMapChaining(HashMapChaining *hashMap) {\n for (int i = 0; i < hashMap->capacity; i++) {\n Node *cur = hashMap->buckets[i];\n while (cur) {\n Node *tmp = cur;\n cur = cur->next;\n free(tmp->pair);\n free(tmp);\n }\n }\n free(hashMap->buckets);\n free(hashMap);\n}\n\n/* \u54c8\u5e0c\u51fd\u6570 */\nint hashFunc(HashMapChaining *hashMap, int key) {\n return key % hashMap->capacity;\n}\n\n/* \u8d1f\u8f7d\u56e0\u5b50 */\ndouble loadFactor(HashMapChaining *hashMap) {\n return (double)hashMap->size / (double)hashMap->capacity;\n}\n\n/* \u67e5\u8be2\u64cd\u4f5c */\nchar *get(HashMapChaining *hashMap, int key) {\n int index = hashFunc(hashMap, key);\n // \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \u5219\u8fd4\u56de\u5bf9\u5e94 val\n Node *cur = hashMap->buckets[index];\n while (cur) {\n if (cur->pair->key == key) {\n return cur->pair->val;\n }\n cur = cur->next;\n }\n return \"\"; // \u82e5\u672a\u627e\u5230 key \u5219\u8fd4\u56de\u7a7a\u5b57\u7b26\u4e32\n}\n\n/* \u6dfb\u52a0\u64cd\u4f5c */\nvoid put(HashMapChaining *hashMap, int key, const char *val) {\n // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if (loadFactor(hashMap) > hashMap->loadThres) {\n extend(hashMap);\n }\n int index = hashFunc(hashMap, key);\n // \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\n Node *cur = hashMap->buckets[index];\n while (cur) {\n if (cur->pair->key == key) {\n strcpy(cur->pair->val, val); // \u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\n return;\n }\n cur = cur->next;\n }\n // \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\n Pair *newPair = (Pair *)malloc(sizeof(Pair));\n newPair->key = key;\n strcpy(newPair->val, val);\n Node *newNode = (Node *)malloc(sizeof(Node));\n newNode->pair = newPair;\n newNode->next = hashMap->buckets[index];\n hashMap->buckets[index] = newNode;\n hashMap->size++;\n}\n\n/* \u6269\u5bb9\u54c8\u5e0c\u8868 */\nvoid extend(HashMapChaining *hashMap) {\n // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n int oldCapacity = hashMap->capacity;\n Node **oldBuckets = hashMap->buckets;\n // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n hashMap->capacity *= hashMap->extendRatio;\n hashMap->buckets = (Node **)malloc(hashMap->capacity * sizeof(Node *));\n for (int i = 0; i < hashMap->capacity; i++) {\n hashMap->buckets[i] = NULL;\n }\n hashMap->size = 0;\n // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n for (int i = 0; i < oldCapacity; i++) {\n Node *cur = oldBuckets[i];\n while (cur) {\n put(hashMap, cur->pair->key, cur->pair->val);\n Node *temp = cur;\n cur = cur->next;\n // \u91ca\u653e\u5185\u5b58\n free(temp->pair);\n free(temp);\n }\n }\n\n free(oldBuckets);\n}\n\n/* \u5220\u9664\u64cd\u4f5c */\nvoid removeItem(HashMapChaining *hashMap, int key) {\n int index = hashFunc(hashMap, key);\n Node *cur = hashMap->buckets[index];\n Node *pre = NULL;\n while (cur) {\n if (cur->pair->key == key) {\n // \u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\n if (pre) {\n pre->next = cur->next;\n } else {\n hashMap->buckets[index] = cur->next;\n }\n // \u91ca\u653e\u5185\u5b58\n free(cur->pair);\n free(cur);\n hashMap->size--;\n return;\n }\n pre = cur;\n cur = cur->next;\n }\n}\n\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nvoid print(HashMapChaining *hashMap) {\n for (int i = 0; i < hashMap->capacity; i++) {\n Node *cur = hashMap->buckets[i];\n printf(\"[\");\n while (cur) {\n printf(\"%d -> %s, \", cur->pair->key, cur->pair->val);\n cur = cur->next;\n }\n printf(\"]\\n\");\n }\n}\n
hash_map_chaining.zig[class]{HashMapChaining}-[func]{}\n
\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u5f53\u94fe\u8868\u5f88\u957f\u65f6\uff0c\u67e5\u8be2\u6548\u7387 \\(O(n)\\) \u5f88\u5dee\u3002\u6b64\u65f6\u53ef\u4ee5\u5c06\u94fe\u8868\u8f6c\u6362\u4e3a\u201cAVL \u6811\u201d\u6216\u201c\u7ea2\u9ed1\u6811\u201d\uff0c\u4ece\u800c\u5c06\u67e5\u8be2\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4f18\u5316\u81f3 \\(O(\\log n)\\) \u3002
"},{"location":"chapter_hashing/hash_collision/#622","title":"6.2.2 \u00a0 \u5f00\u653e\u5bfb\u5740","text":"\u300c\u5f00\u653e\u5bfb\u5740 open addressing\u300d\u4e0d\u5f15\u5165\u989d\u5916\u7684\u6570\u636e\u7ed3\u6784\uff0c\u800c\u662f\u901a\u8fc7\u201c\u591a\u6b21\u63a2\u6d4b\u201d\u6765\u5904\u7406\u54c8\u5e0c\u51b2\u7a81\uff0c\u63a2\u6d4b\u65b9\u5f0f\u4e3b\u8981\u5305\u62ec\u7ebf\u6027\u63a2\u6d4b\u3001\u5e73\u65b9\u63a2\u6d4b\u3001\u591a\u6b21\u54c8\u5e0c\u7b49\u3002
\u4e0b\u9762\u4ee5\u7ebf\u6027\u63a2\u6d4b\u4e3a\u4f8b\uff0c\u4ecb\u7ecd\u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868\u7684\u5de5\u4f5c\u673a\u5236\u3002
"},{"location":"chapter_hashing/hash_collision/#1","title":"1. \u00a0 \u7ebf\u6027\u63a2\u6d4b","text":"\u7ebf\u6027\u63a2\u6d4b\u91c7\u7528\u56fa\u5b9a\u6b65\u957f\u7684\u7ebf\u6027\u641c\u7d22\u6765\u8fdb\u884c\u63a2\u6d4b\uff0c\u5176\u64cd\u4f5c\u65b9\u6cd5\u4e0e\u666e\u901a\u54c8\u5e0c\u8868\u6709\u6240\u4e0d\u540c\u3002
value
\u5373\u53ef\uff1b\u5982\u679c\u9047\u5230\u7a7a\u6876\uff0c\u8bf4\u660e\u76ee\u6807\u5143\u7d20\u4e0d\u5728\u54c8\u5e0c\u8868\u4e2d\uff0c\u8fd4\u56de \\(\\text{None}\\) \u3002\u56fe 6-6 \u5c55\u793a\u4e86\u5f00\u653e\u5bfb\u5740\uff08\u7ebf\u6027\u63a2\u6d4b\uff09\u54c8\u5e0c\u8868\u7684\u952e\u503c\u5bf9\u5206\u5e03\u3002\u6839\u636e\u6b64\u54c8\u5e0c\u51fd\u6570\uff0c\u6700\u540e\u4e24\u4f4d\u76f8\u540c\u7684 key
\u90fd\u4f1a\u88ab\u6620\u5c04\u5230\u76f8\u540c\u7684\u6876\u3002\u800c\u901a\u8fc7\u7ebf\u6027\u63a2\u6d4b\uff0c\u5b83\u4eec\u88ab\u4f9d\u6b21\u5b58\u50a8\u5728\u8be5\u6876\u4ee5\u53ca\u4e4b\u4e0b\u7684\u6876\u4e2d\u3002
\u56fe 6-6 \u00a0 \u5f00\u653e\u5bfb\u5740\u548c\u7ebf\u6027\u63a2\u6d4b
\u7136\u800c\uff0c\u7ebf\u6027\u63a2\u6d4b\u5bb9\u6613\u4ea7\u751f\u201c\u805a\u96c6\u73b0\u8c61\u201d\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u6570\u7ec4\u4e2d\u8fde\u7eed\u88ab\u5360\u7528\u7684\u4f4d\u7f6e\u8d8a\u957f\uff0c\u8fd9\u4e9b\u8fde\u7eed\u4f4d\u7f6e\u53d1\u751f\u54c8\u5e0c\u51b2\u7a81\u7684\u53ef\u80fd\u6027\u8d8a\u5927\uff0c\u4ece\u800c\u8fdb\u4e00\u6b65\u4fc3\u4f7f\u8be5\u4f4d\u7f6e\u7684\u805a\u5806\u751f\u957f\uff0c\u5f62\u6210\u6076\u6027\u5faa\u73af\uff0c\u6700\u7ec8\u5bfc\u81f4\u589e\u5220\u67e5\u6539\u64cd\u4f5c\u6548\u7387\u52a3\u5316\u3002
\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u6211\u4eec\u4e0d\u80fd\u5728\u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868\u4e2d\u76f4\u63a5\u5220\u9664\u5143\u7d20\u3002\u8fd9\u662f\u56e0\u4e3a\u5220\u9664\u5143\u7d20\u4f1a\u5728\u6570\u7ec4\u5185\u4ea7\u751f\u4e00\u4e2a\u7a7a\u6876 \\(\\text{None}\\) \uff0c\u800c\u5f53\u67e5\u8be2\u5143\u7d20\u65f6\uff0c\u7ebf\u6027\u63a2\u6d4b\u5230\u8be5\u7a7a\u6876\u5c31\u4f1a\u8fd4\u56de\uff0c\u56e0\u6b64\u5728\u8be5\u7a7a\u6876\u4e4b\u4e0b\u7684\u5143\u7d20\u90fd\u65e0\u6cd5\u518d\u88ab\u8bbf\u95ee\u5230\uff0c\u7a0b\u5e8f\u53ef\u80fd\u8bef\u5224\u8fd9\u4e9b\u5143\u7d20\u4e0d\u5b58\u5728\u3002
\u56fe 6-7 \u00a0 \u5728\u5f00\u653e\u5bfb\u5740\u4e2d\u5220\u9664\u5143\u7d20\u5bfc\u81f4\u7684\u67e5\u8be2\u95ee\u9898
\u4e3a\u4e86\u89e3\u51b3\u8be5\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u91c7\u7528\u300c\u61d2\u5220\u9664 lazy deletion\u300d\u673a\u5236\uff1a\u5b83\u4e0d\u76f4\u63a5\u4ece\u54c8\u5e0c\u8868\u4e2d\u79fb\u9664\u5143\u7d20\uff0c\u800c\u662f\u5229\u7528\u4e00\u4e2a\u5e38\u91cf TOMBSTONE
\u6765\u6807\u8bb0\u8fd9\u4e2a\u6876\u3002\u5728\u8be5\u673a\u5236\u4e0b\uff0c\\(\\text{None}\\) \u548c TOMBSTONE
\u90fd\u4ee3\u8868\u7a7a\u6876\uff0c\u90fd\u53ef\u4ee5\u653e\u7f6e\u952e\u503c\u5bf9\u3002\u4f46\u4e0d\u540c\u7684\u662f\uff0c\u7ebf\u6027\u63a2\u6d4b\u5230 TOMBSTONE
\u65f6\u5e94\u8be5\u7ee7\u7eed\u904d\u5386\uff0c\u56e0\u4e3a\u5176\u4e4b\u4e0b\u53ef\u80fd\u8fd8\u5b58\u5728\u952e\u503c\u5bf9\u3002
\u7136\u800c\uff0c\u61d2\u5220\u9664\u53ef\u80fd\u4f1a\u52a0\u901f\u54c8\u5e0c\u8868\u7684\u6027\u80fd\u9000\u5316\u3002\u8fd9\u662f\u56e0\u4e3a\u6bcf\u6b21\u5220\u9664\u64cd\u4f5c\u90fd\u4f1a\u4ea7\u751f\u4e00\u4e2a\u5220\u9664\u6807\u8bb0\uff0c\u968f\u7740 TOMBSTONE
\u7684\u589e\u52a0\uff0c\u641c\u7d22\u65f6\u95f4\u4e5f\u4f1a\u589e\u52a0\uff0c\u56e0\u4e3a\u7ebf\u6027\u63a2\u6d4b\u53ef\u80fd\u9700\u8981\u8df3\u8fc7\u591a\u4e2a TOMBSTONE
\u624d\u80fd\u627e\u5230\u76ee\u6807\u5143\u7d20\u3002
\u4e3a\u6b64\uff0c\u8003\u8651\u5728\u7ebf\u6027\u63a2\u6d4b\u4e2d\u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a TOMBSTONE
\u7684\u7d22\u5f15\uff0c\u5e76\u5c06\u641c\u7d22\u5230\u7684\u76ee\u6807\u5143\u7d20\u4e0e\u8be5 TOMBSTONE
\u4ea4\u6362\u4f4d\u7f6e\u3002\u8fd9\u6837\u505a\u7684\u597d\u5904\u662f\u5f53\u6bcf\u6b21\u67e5\u8be2\u6216\u6dfb\u52a0\u5143\u7d20\u65f6\uff0c\u5143\u7d20\u4f1a\u88ab\u79fb\u52a8\u81f3\u8ddd\u79bb\u7406\u60f3\u4f4d\u7f6e\uff08\u63a2\u6d4b\u8d77\u59cb\u70b9\uff09\u66f4\u8fd1\u7684\u6876\uff0c\u4ece\u800c\u4f18\u5316\u67e5\u8be2\u6548\u7387\u3002
\u4ee5\u4e0b\u4ee3\u7801\u5b9e\u73b0\u4e86\u4e00\u4e2a\u5305\u542b\u61d2\u5220\u9664\u7684\u5f00\u653e\u5bfb\u5740\uff08\u7ebf\u6027\u63a2\u6d4b\uff09\u54c8\u5e0c\u8868\u3002\u4e3a\u4e86\u66f4\u52a0\u5145\u5206\u5730\u4f7f\u7528\u54c8\u5e0c\u8868\u7684\u7a7a\u95f4\uff0c\u6211\u4eec\u5c06\u54c8\u5e0c\u8868\u770b\u4f5c\u4e00\u4e2a\u201c\u73af\u5f62\u6570\u7ec4\u201d\uff0c\u5f53\u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u56de\u5230\u5934\u90e8\u7ee7\u7eed\u904d\u5386\u3002
PythonC++JavaC#GoSwiftJSTSDartRustCZig hash_map_open_addressing.pyclass HashMapOpenAddressing:\n \"\"\"\u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868\"\"\"\n\n def __init__(self):\n \"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n self.size = 0 # \u952e\u503c\u5bf9\u6570\u91cf\n self.capacity = 4 # \u54c8\u5e0c\u8868\u5bb9\u91cf\n self.load_thres = 2.0 / 3.0 # \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n self.extend_ratio = 2 # \u6269\u5bb9\u500d\u6570\n self.buckets: list[Pair | None] = [None] * self.capacity # \u6876\u6570\u7ec4\n self.TOMBSTONE = Pair(-1, \"-1\") # \u5220\u9664\u6807\u8bb0\n\n def hash_func(self, key: int) -> int:\n \"\"\"\u54c8\u5e0c\u51fd\u6570\"\"\"\n return key % self.capacity\n\n def load_factor(self) -> float:\n \"\"\"\u8d1f\u8f7d\u56e0\u5b50\"\"\"\n return self.size / self.capacity\n\n def find_bucket(self, key: int) -> int:\n \"\"\"\u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\"\"\"\n index = self.hash_func(key)\n first_tombstone = -1\n # \u7ebf\u6027\u63a2\u6d4b\uff0c\u5f53\u9047\u5230\u7a7a\u6876\u65f6\u8df3\u51fa\n while self.buckets[index] is not None:\n # \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5bf9\u5e94\u6876\u7d22\u5f15\n if self.buckets[index].key == key:\n # \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\n if first_tombstone != -1:\n self.buckets[first_tombstone] = self.buckets[index]\n self.buckets[index] = self.TOMBSTONE\n return first_tombstone # \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\n return index # \u8fd4\u56de\u6876\u7d22\u5f15\n # \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\n if first_tombstone == -1 and self.buckets[index] is self.TOMBSTONE:\n first_tombstone = index\n # \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\n index = (index + 1) % self.capacity\n # \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\n return index if first_tombstone == -1 else first_tombstone\n\n def get(self, key: int) -> str:\n \"\"\"\u67e5\u8be2\u64cd\u4f5c\"\"\"\n # \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n index = self.find_bucket(key)\n # \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n if self.buckets[index] not in [None, self.TOMBSTONE]:\n return self.buckets[index].val\n # \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de None\n return None\n\n def put(self, key: int, val: str):\n \"\"\"\u6dfb\u52a0\u64cd\u4f5c\"\"\"\n # \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if self.load_factor() > self.load_thres:\n self.extend()\n # \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n index = self.find_bucket(key)\n # \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val \u5e76\u8fd4\u56de\n if self.buckets[index] not in [None, self.TOMBSTONE]:\n self.buckets[index].val = val\n return\n # \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\n self.buckets[index] = Pair(key, val)\n self.size += 1\n\n def remove(self, key: int):\n \"\"\"\u5220\u9664\u64cd\u4f5c\"\"\"\n # \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n index = self.find_bucket(key)\n # \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\n if self.buckets[index] not in [None, self.TOMBSTONE]:\n self.buckets[index] = self.TOMBSTONE\n self.size -= 1\n\n def extend(self):\n \"\"\"\u6269\u5bb9\u54c8\u5e0c\u8868\"\"\"\n # \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n buckets_tmp = self.buckets\n # \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n self.capacity *= self.extend_ratio\n self.buckets = [None] * self.capacity\n self.size = 0\n # \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n for pair in buckets_tmp:\n if pair not in [None, self.TOMBSTONE]:\n self.put(pair.key, pair.val)\n\n def print(self):\n \"\"\"\u6253\u5370\u54c8\u5e0c\u8868\"\"\"\n for pair in self.buckets:\n if pair is None:\n print(\"None\")\n elif pair is self.TOMBSTONE:\n print(\"TOMBSTONE\")\n else:\n print(pair.key, \"->\", pair.val)\n
hash_map_open_addressing.cpp/* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\nclass HashMapOpenAddressing {\n private:\n int size; // \u952e\u503c\u5bf9\u6570\u91cf\n int capacity = 4; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n const double loadThres = 2.0 / 3.0; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n const int extendRatio = 2; // \u6269\u5bb9\u500d\u6570\n vector<Pair *> buckets; // \u6876\u6570\u7ec4\n Pair *TOMBSTONE = new Pair(-1, \"-1\"); // \u5220\u9664\u6807\u8bb0\n\n public:\n /* \u6784\u9020\u65b9\u6cd5 */\n HashMapOpenAddressing() : size(0), buckets(capacity, nullptr) {\n }\n\n /* \u6790\u6784\u65b9\u6cd5 */\n ~HashMapOpenAddressing() {\n for (Pair *pair : buckets) {\n if (pair != nullptr && pair != TOMBSTONE) {\n delete pair;\n }\n }\n delete TOMBSTONE;\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n int hashFunc(int key) {\n return key % capacity;\n }\n\n /* \u8d1f\u8f7d\u56e0\u5b50 */\n double loadFactor() {\n return (double)size / capacity;\n }\n\n /* \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15 */\n int findBucket(int key) {\n int index = hashFunc(key);\n int firstTombstone = -1;\n // \u7ebf\u6027\u63a2\u6d4b\uff0c\u5f53\u9047\u5230\u7a7a\u6876\u65f6\u8df3\u51fa\n while (buckets[index] != nullptr) {\n // \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5bf9\u5e94\u6876\u7d22\u5f15\n if (buckets[index]->key == key) {\n // \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\n if (firstTombstone != -1) {\n buckets[firstTombstone] = buckets[index];\n buckets[index] = TOMBSTONE;\n return firstTombstone; // \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\n }\n return index; // \u8fd4\u56de\u6876\u7d22\u5f15\n }\n // \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\n if (firstTombstone == -1 && buckets[index] == TOMBSTONE) {\n firstTombstone = index;\n }\n // \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\n index = (index + 1) % capacity;\n }\n // \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\n return firstTombstone == -1 ? index : firstTombstone;\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n string get(int key) {\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n int index = findBucket(key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n if (buckets[index] != nullptr && buckets[index] != TOMBSTONE) {\n return buckets[index]->val;\n }\n // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u7a7a\u5b57\u7b26\u4e32\n return \"\";\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n void put(int key, string val) {\n // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if (loadFactor() > loadThres) {\n extend();\n }\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n int index = findBucket(key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val \u5e76\u8fd4\u56de\n if (buckets[index] != nullptr && buckets[index] != TOMBSTONE) {\n buckets[index]->val = val;\n return;\n }\n // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\n buckets[index] = new Pair(key, val);\n size++;\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n void remove(int key) {\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n int index = findBucket(key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\n if (buckets[index] != nullptr && buckets[index] != TOMBSTONE) {\n delete buckets[index];\n buckets[index] = TOMBSTONE;\n size--;\n }\n }\n\n /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n void extend() {\n // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n vector<Pair *> bucketsTmp = buckets;\n // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n capacity *= extendRatio;\n buckets = vector<Pair *>(capacity, nullptr);\n size = 0;\n // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n for (Pair *pair : bucketsTmp) {\n if (pair != nullptr && pair != TOMBSTONE) {\n put(pair->key, pair->val);\n delete pair;\n }\n }\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n void print() {\n for (Pair *pair : buckets) {\n if (pair == nullptr) {\n cout << \"nullptr\" << endl;\n } else if (pair == TOMBSTONE) {\n cout << \"TOMBSTONE\" << endl;\n } else {\n cout << pair->key << \" -> \" << pair->val << endl;\n }\n }\n }\n};\n
hash_map_open_addressing.java/* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\nclass HashMapOpenAddressing {\n private int size; // \u952e\u503c\u5bf9\u6570\u91cf\n private int capacity = 4; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n private final double loadThres = 2.0 / 3.0; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n private final int extendRatio = 2; // \u6269\u5bb9\u500d\u6570\n private Pair[] buckets; // \u6876\u6570\u7ec4\n private final Pair TOMBSTONE = new Pair(-1, \"-1\"); // \u5220\u9664\u6807\u8bb0\n\n /* \u6784\u9020\u65b9\u6cd5 */\n public HashMapOpenAddressing() {\n size = 0;\n buckets = new Pair[capacity];\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n private int hashFunc(int key) {\n return key % capacity;\n }\n\n /* \u8d1f\u8f7d\u56e0\u5b50 */\n private double loadFactor() {\n return (double) size / capacity;\n }\n\n /* \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15 */\n private int findBucket(int key) {\n int index = hashFunc(key);\n int firstTombstone = -1;\n // \u7ebf\u6027\u63a2\u6d4b\uff0c\u5f53\u9047\u5230\u7a7a\u6876\u65f6\u8df3\u51fa\n while (buckets[index] != null) {\n // \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5bf9\u5e94\u6876\u7d22\u5f15\n if (buckets[index].key == key) {\n // \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\n if (firstTombstone != -1) {\n buckets[firstTombstone] = buckets[index];\n buckets[index] = TOMBSTONE;\n return firstTombstone; // \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\n }\n return index; // \u8fd4\u56de\u6876\u7d22\u5f15\n }\n // \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\n if (firstTombstone == -1 && buckets[index] == TOMBSTONE) {\n firstTombstone = index;\n }\n // \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\n index = (index + 1) % capacity;\n }\n // \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\n return firstTombstone == -1 ? index : firstTombstone;\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n public String get(int key) {\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n int index = findBucket(key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n if (buckets[index] != null && buckets[index] != TOMBSTONE) {\n return buckets[index].val;\n }\n // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de null\n return null;\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n public void put(int key, String val) {\n // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if (loadFactor() > loadThres) {\n extend();\n }\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n int index = findBucket(key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val \u5e76\u8fd4\u56de\n if (buckets[index] != null && buckets[index] != TOMBSTONE) {\n buckets[index].val = val;\n return;\n }\n // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\n buckets[index] = new Pair(key, val);\n size++;\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n public void remove(int key) {\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n int index = findBucket(key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\n if (buckets[index] != null && buckets[index] != TOMBSTONE) {\n buckets[index] = TOMBSTONE;\n size--;\n }\n }\n\n /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n private void extend() {\n // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n Pair[] bucketsTmp = buckets;\n // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n capacity *= extendRatio;\n buckets = new Pair[capacity];\n size = 0;\n // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n for (Pair pair : bucketsTmp) {\n if (pair != null && pair != TOMBSTONE) {\n put(pair.key, pair.val);\n }\n }\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n public void print() {\n for (Pair pair : buckets) {\n if (pair == null) {\n System.out.println(\"null\");\n } else if (pair == TOMBSTONE) {\n System.out.println(\"TOMBSTONE\");\n } else {\n System.out.println(pair.key + \" -> \" + pair.val);\n }\n }\n }\n}\n
hash_map_open_addressing.cs/* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\nclass HashMapOpenAddressing {\n int size; // \u952e\u503c\u5bf9\u6570\u91cf\n int capacity = 4; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n double loadThres = 2.0 / 3.0; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n int extendRatio = 2; // \u6269\u5bb9\u500d\u6570\n Pair[] buckets; // \u6876\u6570\u7ec4\n Pair TOMBSTONE = new(-1, \"-1\"); // \u5220\u9664\u6807\u8bb0\n\n /* \u6784\u9020\u65b9\u6cd5 */\n public HashMapOpenAddressing() {\n size = 0;\n buckets = new Pair[capacity];\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n int HashFunc(int key) {\n return key % capacity;\n }\n\n /* \u8d1f\u8f7d\u56e0\u5b50 */\n double LoadFactor() {\n return (double)size / capacity;\n }\n\n /* \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15 */\n int FindBucket(int key) {\n int index = HashFunc(key);\n int firstTombstone = -1;\n // \u7ebf\u6027\u63a2\u6d4b\uff0c\u5f53\u9047\u5230\u7a7a\u6876\u65f6\u8df3\u51fa\n while (buckets[index] != null) {\n // \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5bf9\u5e94\u6876\u7d22\u5f15\n if (buckets[index].key == key) {\n // \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\n if (firstTombstone != -1) {\n buckets[firstTombstone] = buckets[index];\n buckets[index] = TOMBSTONE;\n return firstTombstone; // \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\n }\n return index; // \u8fd4\u56de\u6876\u7d22\u5f15\n }\n // \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\n if (firstTombstone == -1 && buckets[index] == TOMBSTONE) {\n firstTombstone = index;\n }\n // \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\n index = (index + 1) % capacity;\n }\n // \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\n return firstTombstone == -1 ? index : firstTombstone;\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n public string? Get(int key) {\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n int index = FindBucket(key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n if (buckets[index] != null && buckets[index] != TOMBSTONE) {\n return buckets[index].val;\n }\n // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de null\n return null;\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n public void Put(int key, string val) {\n // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if (LoadFactor() > loadThres) {\n Extend();\n }\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n int index = FindBucket(key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val \u5e76\u8fd4\u56de\n if (buckets[index] != null && buckets[index] != TOMBSTONE) {\n buckets[index].val = val;\n return;\n }\n // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\n buckets[index] = new Pair(key, val);\n size++;\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n public void Remove(int key) {\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n int index = FindBucket(key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\n if (buckets[index] != null && buckets[index] != TOMBSTONE) {\n buckets[index] = TOMBSTONE;\n size--;\n }\n }\n\n /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n void Extend() {\n // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n Pair[] bucketsTmp = buckets;\n // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n capacity *= extendRatio;\n buckets = new Pair[capacity];\n size = 0;\n // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n foreach (Pair pair in bucketsTmp) {\n if (pair != null && pair != TOMBSTONE) {\n Put(pair.key, pair.val);\n }\n }\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n public void Print() {\n foreach (Pair pair in buckets) {\n if (pair == null) {\n Console.WriteLine(\"null\");\n } else if (pair == TOMBSTONE) {\n Console.WriteLine(\"TOMBSTONE\");\n } else {\n Console.WriteLine(pair.key + \" -> \" + pair.val);\n }\n }\n }\n}\n
hash_map_open_addressing.go/* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\ntype hashMapOpenAddressing struct {\n size int // \u952e\u503c\u5bf9\u6570\u91cf\n capacity int // \u54c8\u5e0c\u8868\u5bb9\u91cf\n loadThres float64 // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n extendRatio int // \u6269\u5bb9\u500d\u6570\n buckets []pair // \u6876\u6570\u7ec4\n removed pair // \u5220\u9664\u6807\u8bb0\n}\n\n/* \u6784\u9020\u65b9\u6cd5 */\nfunc newHashMapOpenAddressing() *hashMapOpenAddressing {\n buckets := make([]pair, 4)\n return &hashMapOpenAddressing{\n size: 0,\n capacity: 4,\n loadThres: 2.0 / 3.0,\n extendRatio: 2,\n buckets: buckets,\n removed: pair{\n key: -1,\n val: \"-1\",\n },\n }\n}\n\n/* \u54c8\u5e0c\u51fd\u6570 */\nfunc (m *hashMapOpenAddressing) hashFunc(key int) int {\n return key % m.capacity\n}\n\n/* \u8d1f\u8f7d\u56e0\u5b50 */\nfunc (m *hashMapOpenAddressing) loadFactor() float64 {\n return float64(m.size) / float64(m.capacity)\n}\n\n/* \u67e5\u8be2\u64cd\u4f5c */\nfunc (m *hashMapOpenAddressing) get(key int) string {\n idx := m.hashFunc(key)\n // \u7ebf\u6027\u63a2\u6d4b\uff0c\u4ece index \u5f00\u59cb\u5411\u540e\u904d\u5386\n for i := 0; i < m.capacity; i++ {\n // \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\n j := (idx + i) % m.capacity\n // \u82e5\u9047\u5230\u7a7a\u6876\uff0c\u8bf4\u660e\u65e0\u6b64 key \uff0c\u5219\u8fd4\u56de null\n if m.buckets[j] == (pair{}) {\n return \"\"\n }\n // \u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n if m.buckets[j].key == key && m.buckets[j] != m.removed {\n return m.buckets[j].val\n }\n }\n // \u82e5\u672a\u627e\u5230 key \u5219\u8fd4\u56de\u7a7a\u5b57\u7b26\u4e32\n return \"\"\n}\n\n/* \u6dfb\u52a0\u64cd\u4f5c */\nfunc (m *hashMapOpenAddressing) put(key int, val string) {\n // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if m.loadFactor() > m.loadThres {\n m.extend()\n }\n idx := m.hashFunc(key)\n // \u7ebf\u6027\u63a2\u6d4b\uff0c\u4ece index \u5f00\u59cb\u5411\u540e\u904d\u5386\n for i := 0; i < m.capacity; i++ {\n // \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\n j := (idx + i) % m.capacity\n // \u82e5\u9047\u5230\u7a7a\u6876\u3001\u6216\u5e26\u6709\u5220\u9664\u6807\u8bb0\u7684\u6876\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u653e\u5165\u8be5\u6876\n if m.buckets[j] == (pair{}) || m.buckets[j] == m.removed {\n m.buckets[j] = pair{\n key: key,\n val: val,\n }\n m.size += 1\n return\n }\n // \u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val\n if m.buckets[j].key == key {\n m.buckets[j].val = val\n }\n }\n}\n\n/* \u5220\u9664\u64cd\u4f5c */\nfunc (m *hashMapOpenAddressing) remove(key int) {\n idx := m.hashFunc(key)\n // \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\n // \u7ebf\u6027\u63a2\u6d4b\uff0c\u4ece index \u5f00\u59cb\u5411\u540e\u904d\u5386\n for i := 0; i < m.capacity; i++ {\n // \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\n j := (idx + i) % m.capacity\n // \u82e5\u9047\u5230\u7a7a\u6876\uff0c\u8bf4\u660e\u65e0\u6b64 key \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if m.buckets[j] == (pair{}) {\n return\n }\n // \u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u6807\u8bb0\u5220\u9664\u5e76\u8fd4\u56de\n if m.buckets[j].key == key {\n m.buckets[j] = m.removed\n m.size -= 1\n }\n }\n}\n\n/* \u6269\u5bb9\u54c8\u5e0c\u8868 */\nfunc (m *hashMapOpenAddressing) extend() {\n // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n tmpBuckets := make([]pair, len(m.buckets))\n copy(tmpBuckets, m.buckets)\n\n // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n m.capacity *= m.extendRatio\n m.buckets = make([]pair, m.capacity)\n m.size = 0\n // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n for _, p := range tmpBuckets {\n if p != (pair{}) && p != m.removed {\n m.put(p.key, p.val)\n }\n }\n}\n\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nfunc (m *hashMapOpenAddressing) print() {\n for _, p := range m.buckets {\n if p != (pair{}) {\n fmt.Println(strconv.Itoa(p.key) + \" -> \" + p.val)\n } else {\n fmt.Println(\"nil\")\n }\n }\n}\n
hash_map_open_addressing.swift/* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\nclass HashMapOpenAddressing {\n var size: Int // \u952e\u503c\u5bf9\u6570\u91cf\n var capacity: Int // \u54c8\u5e0c\u8868\u5bb9\u91cf\n var loadThres: Double // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n var extendRatio: Int // \u6269\u5bb9\u500d\u6570\n var buckets: [Pair?] // \u6876\u6570\u7ec4\n var TOMBSTONE: Pair // \u5220\u9664\u6807\u8bb0\n\n /* \u6784\u9020\u65b9\u6cd5 */\n init() {\n size = 0\n capacity = 4\n loadThres = 2.0 / 3.0\n extendRatio = 2\n buckets = Array(repeating: nil, count: capacity)\n TOMBSTONE = Pair(key: -1, val: \"-1\")\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n func hashFunc(key: Int) -> Int {\n key % capacity\n }\n\n /* \u8d1f\u8f7d\u56e0\u5b50 */\n func loadFactor() -> Double {\n Double(size / capacity)\n }\n\n /* \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15 */\n func findBucket(key: Int) -> Int {\n var index = hashFunc(key: key)\n var firstTombstone = -1\n // \u7ebf\u6027\u63a2\u6d4b\uff0c\u5f53\u9047\u5230\u7a7a\u6876\u65f6\u8df3\u51fa\n while buckets[index] != nil {\n // \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5bf9\u5e94\u6876\u7d22\u5f15\n if buckets[index]!.key == key {\n // \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\n if firstTombstone != -1 {\n buckets[firstTombstone] = buckets[index]\n buckets[index] = TOMBSTONE\n return firstTombstone // \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\n }\n return index // \u8fd4\u56de\u6876\u7d22\u5f15\n }\n // \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\n if firstTombstone == -1 && buckets[index] == TOMBSTONE {\n firstTombstone = index\n }\n // \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\n index = (index + 1) % capacity\n }\n // \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\n return firstTombstone == -1 ? index : firstTombstone\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n func get(key: Int) -> String? {\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n let index = findBucket(key: key)\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n if buckets[index] != nil, buckets[index] != TOMBSTONE {\n return buckets[index]!.val\n }\n // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de null\n return nil\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n func put(key: Int, val: String) {\n // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if loadFactor() > loadThres {\n extend()\n }\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n let index = findBucket(key: key)\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val \u5e76\u8fd4\u56de\n if buckets[index] != nil, buckets[index] != TOMBSTONE {\n buckets[index]!.val = val\n return\n }\n // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\n buckets[index] = Pair(key: key, val: val)\n size += 1\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n func remove(key: Int) {\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n let index = findBucket(key: key)\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\n if buckets[index] != nil, buckets[index] != TOMBSTONE {\n buckets[index] = TOMBSTONE\n size -= 1\n }\n }\n\n /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n func extend() {\n // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n let bucketsTmp = buckets\n // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n capacity *= extendRatio\n buckets = Array(repeating: nil, count: capacity)\n size = 0\n // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n for pair in bucketsTmp {\n if let pair, pair != TOMBSTONE {\n put(key: pair.key, val: pair.val)\n }\n }\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n func print() {\n for pair in buckets {\n if pair == nil {\n Swift.print(\"null\")\n } else if pair == TOMBSTONE {\n Swift.print(\"TOMBSTONE\")\n } else {\n Swift.print(\"\\(pair!.key) -> \\(pair!.val)\")\n }\n }\n }\n}\n
hash_map_open_addressing.js/* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\nclass HashMapOpenAddressing {\n #size; // \u952e\u503c\u5bf9\u6570\u91cf\n #capacity; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n #loadThres; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n #extendRatio; // \u6269\u5bb9\u500d\u6570\n #buckets; // \u6876\u6570\u7ec4\n #TOMBSTONE; // \u5220\u9664\u6807\u8bb0\n\n /* \u6784\u9020\u65b9\u6cd5 */\n constructor() {\n this.#size = 0; // \u952e\u503c\u5bf9\u6570\u91cf\n this.#capacity = 4; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n this.#loadThres = 2.0 / 3.0; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n this.#extendRatio = 2; // \u6269\u5bb9\u500d\u6570\n this.#buckets = Array(this.#capacity).fill(null); // \u6876\u6570\u7ec4\n this.#TOMBSTONE = new Pair(-1, '-1'); // \u5220\u9664\u6807\u8bb0\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n #hashFunc(key) {\n return key % this.#capacity;\n }\n\n /* \u8d1f\u8f7d\u56e0\u5b50 */\n #loadFactor() {\n return this.#size / this.#capacity;\n }\n\n /* \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15 */\n #findBucket(key) {\n let index = this.#hashFunc(key);\n let firstTombstone = -1;\n // \u7ebf\u6027\u63a2\u6d4b\uff0c\u5f53\u9047\u5230\u7a7a\u6876\u65f6\u8df3\u51fa\n while (this.#buckets[index] !== null) {\n // \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5bf9\u5e94\u6876\u7d22\u5f15\n if (this.#buckets[index].key === key) {\n // \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\n if (firstTombstone !== -1) {\n this.#buckets[firstTombstone] = this.#buckets[index];\n this.#buckets[index] = this.#TOMBSTONE;\n return firstTombstone; // \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\n }\n return index; // \u8fd4\u56de\u6876\u7d22\u5f15\n }\n // \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\n if (\n firstTombstone === -1 &&\n this.#buckets[index] === this.#TOMBSTONE\n ) {\n firstTombstone = index;\n }\n // \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\n index = (index + 1) % this.#capacity;\n }\n // \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\n return firstTombstone === -1 ? index : firstTombstone;\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n get(key) {\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n const index = this.#findBucket(key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n if (\n this.#buckets[index] !== null &&\n this.#buckets[index] !== this.#TOMBSTONE\n ) {\n return this.#buckets[index].val;\n }\n // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de null\n return null;\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n put(key, val) {\n // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if (this.#loadFactor() > this.#loadThres) {\n this.#extend();\n }\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n const index = this.#findBucket(key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val \u5e76\u8fd4\u56de\n if (\n this.#buckets[index] !== null &&\n this.#buckets[index] !== this.#TOMBSTONE\n ) {\n this.#buckets[index].val = val;\n return;\n }\n // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\n this.#buckets[index] = new Pair(key, val);\n this.#size++;\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n remove(key) {\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n const index = this.#findBucket(key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\n if (\n this.#buckets[index] !== null &&\n this.#buckets[index] !== this.#TOMBSTONE\n ) {\n this.#buckets[index] = this.#TOMBSTONE;\n this.#size--;\n }\n }\n\n /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n #extend() {\n // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n const bucketsTmp = this.#buckets;\n // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n this.#capacity *= this.#extendRatio;\n this.#buckets = Array(this.#capacity).fill(null);\n this.#size = 0;\n // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n for (const pair of bucketsTmp) {\n if (pair !== null && pair !== this.#TOMBSTONE) {\n this.put(pair.key, pair.val);\n }\n }\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n print() {\n for (const pair of this.#buckets) {\n if (pair === null) {\n console.log('null');\n } else if (pair === this.#TOMBSTONE) {\n console.log('TOMBSTONE');\n } else {\n console.log(pair.key + ' -> ' + pair.val);\n }\n }\n }\n}\n
hash_map_open_addressing.ts/* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\nclass HashMapOpenAddressing {\n private size: number; // \u952e\u503c\u5bf9\u6570\u91cf\n private capacity: number; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n private loadThres: number; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n private extendRatio: number; // \u6269\u5bb9\u500d\u6570\n private buckets: Array<Pair | null>; // \u6876\u6570\u7ec4\n private TOMBSTONE: Pair; // \u5220\u9664\u6807\u8bb0\n\n /* \u6784\u9020\u65b9\u6cd5 */\n constructor() {\n this.size = 0; // \u952e\u503c\u5bf9\u6570\u91cf\n this.capacity = 4; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n this.loadThres = 2.0 / 3.0; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n this.extendRatio = 2; // \u6269\u5bb9\u500d\u6570\n this.buckets = Array(this.capacity).fill(null); // \u6876\u6570\u7ec4\n this.TOMBSTONE = new Pair(-1, '-1'); // \u5220\u9664\u6807\u8bb0\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n private hashFunc(key: number): number {\n return key % this.capacity;\n }\n\n /* \u8d1f\u8f7d\u56e0\u5b50 */\n private loadFactor(): number {\n return this.size / this.capacity;\n }\n\n /* \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15 */\n private findBucket(key: number): number {\n let index = this.hashFunc(key);\n let firstTombstone = -1;\n // \u7ebf\u6027\u63a2\u6d4b\uff0c\u5f53\u9047\u5230\u7a7a\u6876\u65f6\u8df3\u51fa\n while (this.buckets[index] !== null) {\n // \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5bf9\u5e94\u6876\u7d22\u5f15\n if (this.buckets[index]!.key === key) {\n // \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\n if (firstTombstone !== -1) {\n this.buckets[firstTombstone] = this.buckets[index];\n this.buckets[index] = this.TOMBSTONE;\n return firstTombstone; // \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\n }\n return index; // \u8fd4\u56de\u6876\u7d22\u5f15\n }\n // \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\n if (\n firstTombstone === -1 &&\n this.buckets[index] === this.TOMBSTONE\n ) {\n firstTombstone = index;\n }\n // \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\n index = (index + 1) % this.capacity;\n }\n // \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\n return firstTombstone === -1 ? index : firstTombstone;\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n get(key: number): string | null {\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n const index = this.findBucket(key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n if (\n this.buckets[index] !== null &&\n this.buckets[index] !== this.TOMBSTONE\n ) {\n return this.buckets[index]!.val;\n }\n // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de null\n return null;\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n put(key: number, val: string): void {\n // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if (this.loadFactor() > this.loadThres) {\n this.extend();\n }\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n const index = this.findBucket(key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val \u5e76\u8fd4\u56de\n if (\n this.buckets[index] !== null &&\n this.buckets[index] !== this.TOMBSTONE\n ) {\n this.buckets[index]!.val = val;\n return;\n }\n // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\n this.buckets[index] = new Pair(key, val);\n this.size++;\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n remove(key: number): void {\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n const index = this.findBucket(key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\n if (\n this.buckets[index] !== null &&\n this.buckets[index] !== this.TOMBSTONE\n ) {\n this.buckets[index] = this.TOMBSTONE;\n this.size--;\n }\n }\n\n /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n private extend(): void {\n // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n const bucketsTmp = this.buckets;\n // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n this.capacity *= this.extendRatio;\n this.buckets = Array(this.capacity).fill(null);\n this.size = 0;\n // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n for (const pair of bucketsTmp) {\n if (pair !== null && pair !== this.TOMBSTONE) {\n this.put(pair.key, pair.val);\n }\n }\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n print(): void {\n for (const pair of this.buckets) {\n if (pair === null) {\n console.log('null');\n } else if (pair === this.TOMBSTONE) {\n console.log('TOMBSTONE');\n } else {\n console.log(pair.key + ' -> ' + pair.val);\n }\n }\n }\n}\n
hash_map_open_addressing.dart/* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\nclass HashMapOpenAddressing {\n late int _size; // \u952e\u503c\u5bf9\u6570\u91cf\n int _capacity = 4; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n double _loadThres = 2.0 / 3.0; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n int _extendRatio = 2; // \u6269\u5bb9\u500d\u6570\n late List<Pair?> _buckets; // \u6876\u6570\u7ec4\n Pair _TOMBSTONE = Pair(-1, \"-1\"); // \u5220\u9664\u6807\u8bb0\n\n /* \u6784\u9020\u65b9\u6cd5 */\n HashMapOpenAddressing() {\n _size = 0;\n _buckets = List.generate(_capacity, (index) => null);\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n int hashFunc(int key) {\n return key % _capacity;\n }\n\n /* \u8d1f\u8f7d\u56e0\u5b50 */\n double loadFactor() {\n return _size / _capacity;\n }\n\n /* \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15 */\n int findBucket(int key) {\n int index = hashFunc(key);\n int firstTombstone = -1;\n // \u7ebf\u6027\u63a2\u6d4b\uff0c\u5f53\u9047\u5230\u7a7a\u6876\u65f6\u8df3\u51fa\n while (_buckets[index] != null) {\n // \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5bf9\u5e94\u6876\u7d22\u5f15\n if (_buckets[index]!.key == key) {\n // \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\n if (firstTombstone != -1) {\n _buckets[firstTombstone] = _buckets[index];\n _buckets[index] = _TOMBSTONE;\n return firstTombstone; // \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\n }\n return index; // \u8fd4\u56de\u6876\u7d22\u5f15\n }\n // \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\n if (firstTombstone == -1 && _buckets[index] == _TOMBSTONE) {\n firstTombstone = index;\n }\n // \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\n index = (index + 1) % _capacity;\n }\n // \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\n return firstTombstone == -1 ? index : firstTombstone;\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n String? get(int key) {\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n int index = findBucket(key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n if (_buckets[index] != null && _buckets[index] != _TOMBSTONE) {\n return _buckets[index]!.val;\n }\n // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de null\n return null;\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n void put(int key, String val) {\n // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if (loadFactor() > _loadThres) {\n extend();\n }\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n int index = findBucket(key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val \u5e76\u8fd4\u56de\n if (_buckets[index] != null && _buckets[index] != _TOMBSTONE) {\n _buckets[index]!.val = val;\n return;\n }\n // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\n _buckets[index] = new Pair(key, val);\n _size++;\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n void remove(int key) {\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n int index = findBucket(key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\n if (_buckets[index] != null && _buckets[index] != _TOMBSTONE) {\n _buckets[index] = _TOMBSTONE;\n _size--;\n }\n }\n\n /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n void extend() {\n // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n List<Pair?> bucketsTmp = _buckets;\n // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n _capacity *= _extendRatio;\n _buckets = List.generate(_capacity, (index) => null);\n _size = 0;\n // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n for (Pair? pair in bucketsTmp) {\n if (pair != null && pair != _TOMBSTONE) {\n put(pair.key, pair.val);\n }\n }\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n void printHashMap() {\n for (Pair? pair in _buckets) {\n if (pair == null) {\n print(\"null\");\n } else if (pair == _TOMBSTONE) {\n print(\"TOMBSTONE\");\n } else {\n print(\"${pair.key} -> ${pair.val}\");\n }\n }\n }\n}\n
hash_map_open_addressing.rs/* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\nstruct HashMapOpenAddressing {\n size: usize, // \u952e\u503c\u5bf9\u6570\u91cf\n capacity: usize, // \u54c8\u5e0c\u8868\u5bb9\u91cf\n load_thres: f64, // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n extend_ratio: usize, // \u6269\u5bb9\u500d\u6570\n buckets: Vec<Option<Pair>>, // \u6876\u6570\u7ec4\n TOMBSTONE: Option<Pair>, // \u5220\u9664\u6807\u8bb0\n}\n\n\nimpl HashMapOpenAddressing {\n /* \u6784\u9020\u65b9\u6cd5 */\n fn new() -> Self {\n Self {\n size: 0,\n capacity: 4,\n load_thres: 2.0 / 3.0,\n extend_ratio: 2,\n buckets: vec![None; 4],\n TOMBSTONE: Some(Pair {key: -1, val: \"-1\".to_string()}),\n }\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n fn hash_func(&self, key: i32) -> usize {\n (key % self.capacity as i32) as usize\n }\n\n /* \u8d1f\u8f7d\u56e0\u5b50 */\n fn load_factor(&self) -> f64 {\n self.size as f64 / self.capacity as f64\n }\n\n /* \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15 */\n fn find_bucket(&mut self, key: i32) -> usize {\n let mut index = self.hash_func(key);\n let mut first_tombstone = -1;\n // \u7ebf\u6027\u63a2\u6d4b\uff0c\u5f53\u9047\u5230\u7a7a\u6876\u65f6\u8df3\u51fa\n while self.buckets[index].is_some() {\n // \u82e5\u9047\u5230 key\uff0c\u8fd4\u56de\u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n if self.buckets[index].as_ref().unwrap().key == key {\n // \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u5efa\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\n if first_tombstone != -1 {\n self.buckets[first_tombstone as usize] = self.buckets[index].take();\n self.buckets[index] = self.TOMBSTONE.clone();\n return first_tombstone as usize; // \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\n }\n return index; // \u8fd4\u56de\u6876\u7d22\u5f15\n }\n // \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\n if first_tombstone == -1 && self.buckets[index] == self.TOMBSTONE {\n first_tombstone = index as i32;\n }\n // \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\n index = (index + 1) % self.capacity;\n }\n // \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\n if first_tombstone == -1 { index } else { first_tombstone as usize }\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n fn get(&mut self, key: i32) -> Option<&str> {\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n let index = self.find_bucket(key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n if self.buckets[index].is_some() && self.buckets[index] != self.TOMBSTONE {\n return self.buckets[index].as_ref().map(|pair| &pair.val as &str);\n }\n // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de null\n None\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n fn put(&mut self, key: i32, val: String) {\n // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if self.load_factor() > self.load_thres {\n self.extend();\n }\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n let index = self.find_bucket(key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val \u5e76\u8fd4\u56de\n if self.buckets[index].is_some() && self.buckets[index] != self.TOMBSTONE {\n self.buckets[index].as_mut().unwrap().val = val;\n return;\n }\n // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\n self.buckets[index] = Some(Pair { key, val });\n self.size += 1;\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n fn remove(&mut self, key: i32) {\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n let index = self.find_bucket(key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\n if self.buckets[index].is_some() && self.buckets[index] != self.TOMBSTONE {\n self.buckets[index] = self.TOMBSTONE.clone();\n self.size -= 1;\n }\n }\n\n /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n fn extend(&mut self) {\n // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n let buckets_tmp = self.buckets.clone();\n // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n self.capacity *= self.extend_ratio;\n self.buckets = vec![None; self.capacity];\n self.size = 0;\n\n // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n for pair in buckets_tmp {\n if pair.is_none() || pair == self.TOMBSTONE {\n continue;\n }\n let pair = pair.unwrap();\n\n self.put(pair.key, pair.val);\n }\n }\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n fn print(&self) {\n for pair in &self.buckets {\n if pair.is_none() {\n println!(\"null\");\n } else if pair == &self.TOMBSTONE {\n println!(\"TOMBSTONE\");\n } else {\n let pair = pair.as_ref().unwrap();\n println!(\"{} -> {}\", pair.key, pair.val);\n }\n }\n }\n}\n
hash_map_open_addressing.c/* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\ntypedef struct {\n int size; // \u952e\u503c\u5bf9\u6570\u91cf\n int capacity; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n double loadThres; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n int extendRatio; // \u6269\u5bb9\u500d\u6570\n Pair **buckets; // \u6876\u6570\u7ec4\n Pair *TOMBSTONE; // \u5220\u9664\u6807\u8bb0\n} HashMapOpenAddressing;\n\n/* \u6784\u9020\u51fd\u6570 */\nHashMapOpenAddressing *newHashMapOpenAddressing() {\n HashMapOpenAddressing *hashMap = (HashMapOpenAddressing *)malloc(sizeof(HashMapOpenAddressing));\n hashMap->size = 0;\n hashMap->capacity = 4;\n hashMap->loadThres = 2.0 / 3.0;\n hashMap->extendRatio = 2;\n hashMap->buckets = (Pair **)malloc(sizeof(Pair *) * hashMap->capacity);\n hashMap->TOMBSTONE = (Pair *)malloc(sizeof(Pair));\n hashMap->TOMBSTONE->key = -1;\n hashMap->TOMBSTONE->val = \"-1\";\n\n return hashMap;\n}\n\n/* \u6790\u6784\u51fd\u6570 */\nvoid delHashMapOpenAddressing(HashMapOpenAddressing *hashMap) {\n for (int i = 0; i < hashMap->capacity; i++) {\n Pair *pair = hashMap->buckets[i];\n if (pair != NULL && pair != hashMap->TOMBSTONE) {\n free(pair->val);\n free(pair);\n }\n }\n}\n\n/* \u54c8\u5e0c\u51fd\u6570 */\nint hashFunc(HashMapOpenAddressing *hashMap, int key) {\n return key % hashMap->capacity;\n}\n\n/* \u8d1f\u8f7d\u56e0\u5b50 */\ndouble loadFactor(HashMapOpenAddressing *hashMap) {\n return (double)hashMap->size / (double)hashMap->capacity;\n}\n\n/* \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15 */\nint findBucket(HashMapOpenAddressing *hashMap, int key) {\n int index = hashFunc(hashMap, key);\n int firstTombstone = -1;\n // \u7ebf\u6027\u63a2\u6d4b\uff0c\u5f53\u9047\u5230\u7a7a\u6876\u65f6\u8df3\u51fa\n while (hashMap->buckets[index] != NULL) {\n // \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5bf9\u5e94\u6876\u7d22\u5f15\n if (hashMap->buckets[index]->key == key) {\n // \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\n if (firstTombstone != -1) {\n hashMap->buckets[firstTombstone] = hashMap->buckets[index];\n hashMap->buckets[index] = hashMap->TOMBSTONE;\n return firstTombstone; // \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\n }\n return index; // \u8fd4\u56de\u6876\u7d22\u5f15\n }\n // \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\n if (firstTombstone == -1 && hashMap->buckets[index] == hashMap->TOMBSTONE) {\n firstTombstone = index;\n }\n // \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\n index = (index + 1) % hashMap->capacity;\n }\n // \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\n return firstTombstone == -1 ? index : firstTombstone;\n}\n\n/* \u67e5\u8be2\u64cd\u4f5c */\nchar *get(HashMapOpenAddressing *hashMap, int key) {\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n int index = findBucket(hashMap, key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n if (hashMap->buckets[index] != NULL && hashMap->buckets[index] != hashMap->TOMBSTONE) {\n return hashMap->buckets[index]->val;\n }\n // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u7a7a\u5b57\u7b26\u4e32\n return \"\";\n}\n\n/* \u6dfb\u52a0\u64cd\u4f5c */\nvoid put(HashMapOpenAddressing *hashMap, int key, char *val) {\n // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if (loadFactor(hashMap) > hashMap->loadThres) {\n extend(hashMap);\n }\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n int index = findBucket(hashMap, key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val \u5e76\u8fd4\u56de\n if (hashMap->buckets[index] != NULL && hashMap->buckets[index] != hashMap->TOMBSTONE) {\n free(hashMap->buckets[index]->val);\n hashMap->buckets[index]->val = (char *)malloc(sizeof(strlen(val + 1)));\n strcpy(hashMap->buckets[index]->val, val);\n hashMap->buckets[index]->val[strlen(val)] = '\\0';\n return;\n }\n // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\n Pair *pair = (Pair *)malloc(sizeof(Pair));\n pair->key = key;\n pair->val = (char *)malloc(sizeof(strlen(val + 1)));\n strcpy(pair->val, val);\n pair->val[strlen(val)] = '\\0';\n\n hashMap->buckets[index] = pair;\n hashMap->size++;\n}\n\n/* \u5220\u9664\u64cd\u4f5c */\nvoid removeItem(HashMapOpenAddressing *hashMap, int key) {\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n int index = findBucket(hashMap, key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\n if (hashMap->buckets[index] != NULL && hashMap->buckets[index] != hashMap->TOMBSTONE) {\n Pair *pair = hashMap->buckets[index];\n free(pair->val);\n free(pair);\n hashMap->buckets[index] = hashMap->TOMBSTONE;\n hashMap->size--;\n }\n}\n\n/* \u6269\u5bb9\u54c8\u5e0c\u8868 */\nvoid extend(HashMapOpenAddressing *hashMap) {\n // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n Pair **bucketsTmp = hashMap->buckets;\n int oldCapacity = hashMap->capacity;\n // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n hashMap->capacity *= hashMap->extendRatio;\n hashMap->buckets = (Pair **)malloc(sizeof(Pair *) * hashMap->capacity);\n hashMap->size = 0;\n // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n for (int i = 0; i < oldCapacity; i++) {\n Pair *pair = bucketsTmp[i];\n if (pair != NULL && pair != hashMap->TOMBSTONE) {\n put(hashMap, pair->key, pair->val);\n free(pair->val);\n free(pair);\n }\n }\n free(bucketsTmp);\n}\n\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nvoid print(HashMapOpenAddressing *hashMap) {\n for (int i = 0; i < hashMap->capacity; i++) {\n Pair *pair = hashMap->buckets[i];\n if (pair == NULL) {\n printf(\"NULL\\n\");\n } else if (pair == hashMap->TOMBSTONE) {\n printf(\"TOMBSTONE\\n\");\n } else {\n printf(\"%d -> %s\\n\", pair->key, pair->val);\n }\n }\n}\n
hash_map_open_addressing.zig[class]{HashMapOpenAddressing}-[func]{}\n
"},{"location":"chapter_hashing/hash_collision/#2","title":"2. \u00a0 \u5e73\u65b9\u63a2\u6d4b","text":"\u5e73\u65b9\u63a2\u6d4b\u4e0e\u7ebf\u6027\u63a2\u6d4b\u7c7b\u4f3c\uff0c\u90fd\u662f\u5f00\u653e\u5bfb\u5740\u7684\u5e38\u89c1\u7b56\u7565\u4e4b\u4e00\u3002\u5f53\u53d1\u751f\u51b2\u7a81\u65f6\uff0c\u5e73\u65b9\u63a2\u6d4b\u4e0d\u662f\u7b80\u5355\u5730\u8df3\u8fc7\u4e00\u4e2a\u56fa\u5b9a\u7684\u6b65\u6570\uff0c\u800c\u662f\u8df3\u8fc7\u201c\u63a2\u6d4b\u6b21\u6570\u7684\u5e73\u65b9\u201d\u7684\u6b65\u6570\uff0c\u5373 \\(1, 4, 9, \\dots\\) \u6b65\u3002
\u5e73\u65b9\u63a2\u6d4b\u4e3b\u8981\u5177\u6709\u4ee5\u4e0b\u4f18\u52bf\u3002
\u7136\u800c\uff0c\u5e73\u65b9\u63a2\u6d4b\u4e5f\u5e76\u4e0d\u662f\u5b8c\u7f8e\u7684\u3002
\u987e\u540d\u601d\u4e49\uff0c\u591a\u6b21\u54c8\u5e0c\u65b9\u6cd5\u4f7f\u7528\u591a\u4e2a\u54c8\u5e0c\u51fd\u6570 \\(f_1(x)\\)\u3001\\(f_2(x)\\)\u3001\\(f_3(x)\\)\u3001\\(\\dots\\) \u8fdb\u884c\u63a2\u6d4b\u3002
\u4e0e\u7ebf\u6027\u63a2\u6d4b\u76f8\u6bd4\uff0c\u591a\u6b21\u54c8\u5e0c\u65b9\u6cd5\u4e0d\u6613\u4ea7\u751f\u805a\u96c6\uff0c\u4f46\u591a\u4e2a\u54c8\u5e0c\u51fd\u6570\u4f1a\u5e26\u6765\u989d\u5916\u7684\u8ba1\u7b97\u91cf\u3002
Tip
\u8bf7\u6ce8\u610f\uff0c\u5f00\u653e\u5bfb\u5740\uff08\u7ebf\u6027\u63a2\u6d4b\u3001\u5e73\u65b9\u63a2\u6d4b\u548c\u591a\u6b21\u54c8\u5e0c\uff09\u54c8\u5e0c\u8868\u90fd\u5b58\u5728\u201c\u4e0d\u80fd\u76f4\u63a5\u5220\u9664\u5143\u7d20\u201d\u7684\u95ee\u9898\u3002
"},{"location":"chapter_hashing/hash_collision/#623","title":"6.2.3 \u00a0 \u7f16\u7a0b\u8bed\u8a00\u7684\u9009\u62e9","text":"\u5404\u4e2a\u7f16\u7a0b\u8bed\u8a00\u91c7\u53d6\u4e86\u4e0d\u540c\u7684\u54c8\u5e0c\u8868\u5b9e\u73b0\u7b56\u7565\uff0c\u4ee5\u4e0b\u4e3e\u51e0\u4e2a\u4f8b\u5b50\u3002
\u300c\u54c8\u5e0c\u8868 hash table\u300d\uff0c\u53c8\u79f0\u300c\u6563\u5217\u8868\u300d\uff0c\u5176\u901a\u8fc7\u5efa\u7acb\u952e key
\u4e0e\u503c value
\u4e4b\u95f4\u7684\u6620\u5c04\uff0c\u5b9e\u73b0\u9ad8\u6548\u7684\u5143\u7d20\u67e5\u8be2\u3002\u5177\u4f53\u800c\u8a00\uff0c\u6211\u4eec\u5411\u54c8\u5e0c\u8868\u8f93\u5165\u4e00\u4e2a\u952e key
\uff0c\u5219\u53ef\u4ee5\u5728 \\(O(1)\\) \u65f6\u95f4\u5185\u83b7\u53d6\u5bf9\u5e94\u7684\u503c value
\u3002
\u5982\u56fe 6-1 \u6240\u793a\uff0c\u7ed9\u5b9a \\(n\\) \u4e2a\u5b66\u751f\uff0c\u6bcf\u4e2a\u5b66\u751f\u90fd\u6709\u201c\u59d3\u540d\u201d\u548c\u201c\u5b66\u53f7\u201d\u4e24\u9879\u6570\u636e\u3002\u5047\u5982\u6211\u4eec\u5e0c\u671b\u5b9e\u73b0\u201c\u8f93\u5165\u4e00\u4e2a\u5b66\u53f7\uff0c\u8fd4\u56de\u5bf9\u5e94\u7684\u59d3\u540d\u201d\u7684\u67e5\u8be2\u529f\u80fd\uff0c\u5219\u53ef\u4ee5\u91c7\u7528\u56fe 6-1 \u6240\u793a\u7684\u54c8\u5e0c\u8868\u6765\u5b9e\u73b0\u3002
\u56fe 6-1 \u00a0 \u54c8\u5e0c\u8868\u7684\u62bd\u8c61\u8868\u793a
\u9664\u54c8\u5e0c\u8868\u5916\uff0c\u6570\u7ec4\u548c\u94fe\u8868\u4e5f\u53ef\u4ee5\u5b9e\u73b0\u67e5\u8be2\u529f\u80fd\uff0c\u5b83\u4eec\u7684\u6548\u7387\u5bf9\u6bd4\u5982\u8868 6-1 \u6240\u793a\u3002
\u8868 6-1 \u00a0 \u5143\u7d20\u67e5\u8be2\u6548\u7387\u5bf9\u6bd4
\u6570\u7ec4 \u94fe\u8868 \u54c8\u5e0c\u8868 \u67e5\u627e\u5143\u7d20 \\(O(n)\\) \\(O(n)\\) \\(O(1)\\) \u6dfb\u52a0\u5143\u7d20 \\(O(1)\\) \\(O(1)\\) \\(O(1)\\) \u5220\u9664\u5143\u7d20 \\(O(n)\\) \\(O(n)\\) \\(O(1)\\)\u89c2\u5bdf\u53d1\u73b0\uff0c\u5728\u54c8\u5e0c\u8868\u4e2d\u8fdb\u884c\u589e\u5220\u67e5\u6539\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u90fd\u662f \\(O(1)\\) \uff0c\u975e\u5e38\u9ad8\u6548\u3002
"},{"location":"chapter_hashing/hash_map/#611","title":"6.1.1 \u00a0 \u54c8\u5e0c\u8868\u5e38\u7528\u64cd\u4f5c","text":"\u54c8\u5e0c\u8868\u7684\u5e38\u89c1\u64cd\u4f5c\u5305\u62ec\uff1a\u521d\u59cb\u5316\u3001\u67e5\u8be2\u64cd\u4f5c\u3001\u6dfb\u52a0\u952e\u503c\u5bf9\u548c\u5220\u9664\u952e\u503c\u5bf9\u7b49\uff0c\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig hash_map.py# \u521d\u59cb\u5316\u54c8\u5e0c\u8868\nhmap: dict = {}\n\n# \u6dfb\u52a0\u64cd\u4f5c\n# \u5728\u54c8\u5e0c\u8868\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9 (key, value)\nhmap[12836] = \"\u5c0f\u54c8\"\nhmap[15937] = \"\u5c0f\u5570\"\nhmap[16750] = \"\u5c0f\u7b97\"\nhmap[13276] = \"\u5c0f\u6cd5\"\nhmap[10583] = \"\u5c0f\u9e2d\"\n\n# \u67e5\u8be2\u64cd\u4f5c\n# \u5411\u54c8\u5e0c\u8868\u8f93\u5165\u952e key \uff0c\u5f97\u5230\u503c value\nname: str = hmap[15937]\n\n# \u5220\u9664\u64cd\u4f5c\n# \u5728\u54c8\u5e0c\u8868\u4e2d\u5220\u9664\u952e\u503c\u5bf9 (key, value)\nhmap.pop(10583)\n
hash_map.cpp/* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nunordered_map<int, string> map;\n\n/* \u6dfb\u52a0\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9 (key, value)\nmap[12836] = \"\u5c0f\u54c8\";\nmap[15937] = \"\u5c0f\u5570\";\nmap[16750] = \"\u5c0f\u7b97\";\nmap[13276] = \"\u5c0f\u6cd5\";\nmap[10583] = \"\u5c0f\u9e2d\";\n\n/* \u67e5\u8be2\u64cd\u4f5c */\n// \u5411\u54c8\u5e0c\u8868\u8f93\u5165\u952e key \uff0c\u5f97\u5230\u503c value\nstring name = map[15937];\n\n/* \u5220\u9664\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u5220\u9664\u952e\u503c\u5bf9 (key, value)\nmap.erase(10583);\n
hash_map.java/* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nMap<Integer, String> map = new HashMap<>();\n\n/* \u6dfb\u52a0\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9 (key, value)\nmap.put(12836, \"\u5c0f\u54c8\"); \nmap.put(15937, \"\u5c0f\u5570\"); \nmap.put(16750, \"\u5c0f\u7b97\"); \nmap.put(13276, \"\u5c0f\u6cd5\");\nmap.put(10583, \"\u5c0f\u9e2d\");\n\n/* \u67e5\u8be2\u64cd\u4f5c */\n// \u5411\u54c8\u5e0c\u8868\u8f93\u5165\u952e key \uff0c\u5f97\u5230\u503c value\nString name = map.get(15937);\n\n/* \u5220\u9664\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u5220\u9664\u952e\u503c\u5bf9 (key, value)\nmap.remove(10583);\n
hash_map.cs/* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nDictionary<int, string> map = new() {\n /* \u6dfb\u52a0\u64cd\u4f5c */\n // \u5728\u54c8\u5e0c\u8868\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9 (key, value)\n { 12836, \"\u5c0f\u54c8\" },\n { 15937, \"\u5c0f\u5570\" },\n { 16750, \"\u5c0f\u7b97\" },\n { 13276, \"\u5c0f\u6cd5\" },\n { 10583, \"\u5c0f\u9e2d\" }\n};\n\n/* \u67e5\u8be2\u64cd\u4f5c */\n// \u5411\u54c8\u5e0c\u8868\u8f93\u5165\u952e key \uff0c\u5f97\u5230\u503c value\nstring name = map[15937];\n\n/* \u5220\u9664\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u5220\u9664\u952e\u503c\u5bf9 (key, value)\nmap.Remove(10583);\n
hash_map_test.go/* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nhmap := make(map[int]string)\n\n/* \u6dfb\u52a0\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9 (key, value)\nhmap[12836] = \"\u5c0f\u54c8\"\nhmap[15937] = \"\u5c0f\u5570\"\nhmap[16750] = \"\u5c0f\u7b97\"\nhmap[13276] = \"\u5c0f\u6cd5\"\nhmap[10583] = \"\u5c0f\u9e2d\"\n\n/* \u67e5\u8be2\u64cd\u4f5c */\n// \u5411\u54c8\u5e0c\u8868\u8f93\u5165\u952e key \uff0c\u5f97\u5230\u503c value\nname := hmap[15937]\n\n/* \u5220\u9664\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u5220\u9664\u952e\u503c\u5bf9 (key, value)\ndelete(hmap, 10583)\n
hash_map.swift/* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nvar map: [Int: String] = [:]\n\n/* \u6dfb\u52a0\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9 (key, value)\nmap[12836] = \"\u5c0f\u54c8\"\nmap[15937] = \"\u5c0f\u5570\"\nmap[16750] = \"\u5c0f\u7b97\"\nmap[13276] = \"\u5c0f\u6cd5\"\nmap[10583] = \"\u5c0f\u9e2d\"\n\n/* \u67e5\u8be2\u64cd\u4f5c */\n// \u5411\u54c8\u5e0c\u8868\u8f93\u5165\u952e key \uff0c\u5f97\u5230\u503c value\nlet name = map[15937]!\n\n/* \u5220\u9664\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u5220\u9664\u952e\u503c\u5bf9 (key, value)\nmap.removeValue(forKey: 10583)\n
hash_map.js/* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nconst map = new Map();\n/* \u6dfb\u52a0\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9 (key, value)\nmap.set(12836, '\u5c0f\u54c8');\nmap.set(15937, '\u5c0f\u5570');\nmap.set(16750, '\u5c0f\u7b97');\nmap.set(13276, '\u5c0f\u6cd5');\nmap.set(10583, '\u5c0f\u9e2d');\n\n/* \u67e5\u8be2\u64cd\u4f5c */\n// \u5411\u54c8\u5e0c\u8868\u8f93\u5165\u952e key \uff0c\u5f97\u5230\u503c value\nlet name = map.get(15937);\n\n/* \u5220\u9664\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u5220\u9664\u952e\u503c\u5bf9 (key, value)\nmap.delete(10583);\n
hash_map.ts/* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nconst map = new Map<number, string>();\n/* \u6dfb\u52a0\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9 (key, value)\nmap.set(12836, '\u5c0f\u54c8');\nmap.set(15937, '\u5c0f\u5570');\nmap.set(16750, '\u5c0f\u7b97');\nmap.set(13276, '\u5c0f\u6cd5');\nmap.set(10583, '\u5c0f\u9e2d');\nconsole.info('\\n\u6dfb\u52a0\u5b8c\u6210\u540e\uff0c\u54c8\u5e0c\u8868\u4e3a\\nKey -> Value');\nconsole.info(map);\n\n/* \u67e5\u8be2\u64cd\u4f5c */\n// \u5411\u54c8\u5e0c\u8868\u8f93\u5165\u952e key \uff0c\u5f97\u5230\u503c value\nlet name = map.get(15937);\nconsole.info('\\n\u8f93\u5165\u5b66\u53f7 15937 \uff0c\u67e5\u8be2\u5230\u59d3\u540d ' + name);\n\n/* \u5220\u9664\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u5220\u9664\u952e\u503c\u5bf9 (key, value)\nmap.delete(10583);\nconsole.info('\\n\u5220\u9664 10583 \u540e\uff0c\u54c8\u5e0c\u8868\u4e3a\\nKey -> Value');\nconsole.info(map);\n
hash_map.dart/* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nMap<int, String> map = {};\n\n/* \u6dfb\u52a0\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9 (key, value)\nmap[12836] = \"\u5c0f\u54c8\";\nmap[15937] = \"\u5c0f\u5570\";\nmap[16750] = \"\u5c0f\u7b97\";\nmap[13276] = \"\u5c0f\u6cd5\";\nmap[10583] = \"\u5c0f\u9e2d\";\n\n/* \u67e5\u8be2\u64cd\u4f5c */\n// \u5411\u54c8\u5e0c\u8868\u8f93\u5165\u952e key \uff0c\u5f97\u5230\u503c value\nString name = map[15937];\n\n/* \u5220\u9664\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u5220\u9664\u952e\u503c\u5bf9 (key, value)\nmap.remove(10583);\n
hash_map.rsuse std::collections::HashMap;\n\n/* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nlet mut map: HashMap<i32, String> = HashMap::new();\n\n/* \u6dfb\u52a0\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9 (key, value)\nmap.insert(12836, \"\u5c0f\u54c8\".to_string());\nmap.insert(15937, \"\u5c0f\u5570\".to_string());\nmap.insert(16750, \"\u5c0f\u7b97\".to_string());\nmap.insert(13279, \"\u5c0f\u6cd5\".to_string());\nmap.insert(10583, \"\u5c0f\u9e2d\".to_string());\n\n/* \u67e5\u8be2\u64cd\u4f5c */\n// \u5411\u54c8\u5e0c\u8868\u4e2d\u8f93\u5165\u952e key \uff0c\u5f97\u5230\u503c value\nlet _name: Option<&String> = map.get(&15937);\n\n/* \u5220\u9664\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u5220\u9664\u952e\u503c\u5bf9 (key, value)\nlet _removed_value: Option<String> = map.remove(&10583);\n
hash_map.c// C \u672a\u63d0\u4f9b\u5185\u7f6e\u54c8\u5e0c\u8868\n
hash_map.zig\n
\u54c8\u5e0c\u8868\u6709\u4e09\u79cd\u5e38\u7528\u7684\u904d\u5386\u65b9\u5f0f\uff1a\u904d\u5386\u952e\u503c\u5bf9\u3001\u904d\u5386\u952e\u548c\u904d\u5386\u503c\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig hash_map.py# \u904d\u5386\u54c8\u5e0c\u8868\n# \u904d\u5386\u952e\u503c\u5bf9 key->value\nfor key, value in hmap.items():\n print(key, \"->\", value)\n# \u5355\u72ec\u904d\u5386\u952e key\nfor key in hmap.keys():\n print(key)\n# \u5355\u72ec\u904d\u5386\u503c value\nfor value in hmap.values():\n print(value)\n
hash_map.cpp/* \u904d\u5386\u54c8\u5e0c\u8868 */\n// \u904d\u5386\u952e\u503c\u5bf9 key->value\nfor (auto kv: map) {\n cout << kv.first << \" -> \" << kv.second << endl;\n}\n// \u4f7f\u7528\u8fed\u4ee3\u5668\u904d\u5386 key->value\nfor (auto iter = map.begin(); iter != map.end(); iter++) {\n cout << iter->first << \"->\" << iter->second << endl;\n}\n
hash_map.java/* \u904d\u5386\u54c8\u5e0c\u8868 */\n// \u904d\u5386\u952e\u503c\u5bf9 key->value\nfor (Map.Entry <Integer, String> kv: map.entrySet()) {\n System.out.println(kv.getKey() + \" -> \" + kv.getValue());\n}\n// \u5355\u72ec\u904d\u5386\u952e key\nfor (int key: map.keySet()) {\n System.out.println(key);\n}\n// \u5355\u72ec\u904d\u5386\u503c value\nfor (String val: map.values()) {\n System.out.println(val);\n}\n
hash_map.cs/* \u904d\u5386\u54c8\u5e0c\u8868 */\n// \u904d\u5386\u952e\u503c\u5bf9 Key->Value\nforeach (var kv in map) {\n Console.WriteLine(kv.Key + \" -> \" + kv.Value);\n}\n// \u5355\u72ec\u904d\u5386\u952e key\nforeach (int key in map.Keys) {\n Console.WriteLine(key);\n}\n// \u5355\u72ec\u904d\u5386\u503c value\nforeach (string val in map.Values) {\n Console.WriteLine(val);\n}\n
hash_map_test.go/* \u904d\u5386\u54c8\u5e0c\u8868 */\n// \u904d\u5386\u952e\u503c\u5bf9 key->value\nfor key, value := range hmap {\n fmt.Println(key, \"->\", value)\n}\n// \u5355\u72ec\u904d\u5386\u952e key\nfor key := range hmap {\n fmt.Println(key)\n}\n// \u5355\u72ec\u904d\u5386\u503c value\nfor _, value := range hmap {\n fmt.Println(value)\n}\n
hash_map.swift/* \u904d\u5386\u54c8\u5e0c\u8868 */\n// \u904d\u5386\u952e\u503c\u5bf9 Key->Value\nfor (key, value) in map {\n print(\"\\(key) -> \\(value)\")\n}\n// \u5355\u72ec\u904d\u5386\u952e Key\nfor key in map.keys {\n print(key)\n}\n// \u5355\u72ec\u904d\u5386\u503c Value\nfor value in map.values {\n print(value)\n}\n
hash_map.js/* \u904d\u5386\u54c8\u5e0c\u8868 */\nconsole.info('\\n\u904d\u5386\u952e\u503c\u5bf9 Key->Value');\nfor (const [k, v] of map.entries()) {\n console.info(k + ' -> ' + v);\n}\nconsole.info('\\n\u5355\u72ec\u904d\u5386\u952e Key');\nfor (const k of map.keys()) {\n console.info(k);\n}\nconsole.info('\\n\u5355\u72ec\u904d\u5386\u503c Value');\nfor (const v of map.values()) {\n console.info(v);\n}\n
hash_map.ts/* \u904d\u5386\u54c8\u5e0c\u8868 */\nconsole.info('\\n\u904d\u5386\u952e\u503c\u5bf9 Key->Value');\nfor (const [k, v] of map.entries()) {\n console.info(k + ' -> ' + v);\n}\nconsole.info('\\n\u5355\u72ec\u904d\u5386\u952e Key');\nfor (const k of map.keys()) {\n console.info(k);\n}\nconsole.info('\\n\u5355\u72ec\u904d\u5386\u503c Value');\nfor (const v of map.values()) {\n console.info(v);\n}\n
hash_map.dart/* \u904d\u5386\u54c8\u5e0c\u8868 */\n// \u904d\u5386\u952e\u503c\u5bf9 Key->Value\nmap.forEach((key, value) {\n print('$key -> $value');\n});\n\n// \u5355\u72ec\u904d\u5386\u952e Key\nmap.keys.forEach((key) {\n print(key);\n});\n\n// \u5355\u72ec\u904d\u5386\u503c Value\nmap.values.forEach((value) {\n print(value);\n});\n
hash_map.rs/* \u904d\u5386\u54c8\u5e0c\u8868 */\n// \u904d\u5386\u952e\u503c\u5bf9 Key->Value\nfor (key, value) in &map {\n println!(\"{key} -> {value}\");\n}\n\n// \u5355\u72ec\u904d\u5386\u952e Key\nfor key in map.keys() {\n println!(\"{key}\"); \n}\n\n// \u5355\u72ec\u904d\u5386\u503c Value\nfor value in map.values() {\n println!(\"{value}\");\n}\n
hash_map.c// C \u672a\u63d0\u4f9b\u5185\u7f6e\u54c8\u5e0c\u8868\n
hash_map.zig\n
"},{"location":"chapter_hashing/hash_map/#612","title":"6.1.2 \u00a0 \u54c8\u5e0c\u8868\u7b80\u5355\u5b9e\u73b0","text":"\u6211\u4eec\u5148\u8003\u8651\u6700\u7b80\u5355\u7684\u60c5\u51b5\uff0c\u4ec5\u7528\u4e00\u4e2a\u6570\u7ec4\u6765\u5b9e\u73b0\u54c8\u5e0c\u8868\u3002\u5728\u54c8\u5e0c\u8868\u4e2d\uff0c\u6211\u4eec\u5c06\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u7a7a\u4f4d\u79f0\u4e3a\u300c\u6876 bucket\u300d\uff0c\u6bcf\u4e2a\u6876\u53ef\u5b58\u50a8\u4e00\u4e2a\u952e\u503c\u5bf9\u3002\u56e0\u6b64\uff0c\u67e5\u8be2\u64cd\u4f5c\u5c31\u662f\u627e\u5230 key
\u5bf9\u5e94\u7684\u6876\uff0c\u5e76\u5728\u6876\u4e2d\u83b7\u53d6 value
\u3002
\u90a3\u4e48\uff0c\u5982\u4f55\u57fa\u4e8e key
\u5b9a\u4f4d\u5bf9\u5e94\u7684\u6876\u5462\uff1f\u8fd9\u662f\u901a\u8fc7\u300c\u54c8\u5e0c\u51fd\u6570 hash function\u300d\u5b9e\u73b0\u7684\u3002\u54c8\u5e0c\u51fd\u6570\u7684\u4f5c\u7528\u662f\u5c06\u4e00\u4e2a\u8f83\u5927\u7684\u8f93\u5165\u7a7a\u95f4\u6620\u5c04\u5230\u4e00\u4e2a\u8f83\u5c0f\u7684\u8f93\u51fa\u7a7a\u95f4\u3002\u5728\u54c8\u5e0c\u8868\u4e2d\uff0c\u8f93\u5165\u7a7a\u95f4\u662f\u6240\u6709 key
\uff0c\u8f93\u51fa\u7a7a\u95f4\u662f\u6240\u6709\u6876\uff08\u6570\u7ec4\u7d22\u5f15\uff09\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u8f93\u5165\u4e00\u4e2a key
\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u54c8\u5e0c\u51fd\u6570\u5f97\u5230\u8be5 key
\u5bf9\u5e94\u7684\u952e\u503c\u5bf9\u5728\u6570\u7ec4\u4e2d\u7684\u5b58\u50a8\u4f4d\u7f6e\u3002
\u8f93\u5165\u4e00\u4e2a key
\uff0c\u54c8\u5e0c\u51fd\u6570\u7684\u8ba1\u7b97\u8fc7\u7a0b\u5206\u4e3a\u4ee5\u4e0b\u4e24\u6b65\u3002
hash()
\u8ba1\u7b97\u5f97\u5230\u54c8\u5e0c\u503c\u3002capacity
\u53d6\u6a21\uff0c\u4ece\u800c\u83b7\u53d6\u8be5 key
\u5bf9\u5e94\u7684\u6570\u7ec4\u7d22\u5f15 index
\u3002index = hash(key) % capacity\n
\u968f\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5229\u7528 index
\u5728\u54c8\u5e0c\u8868\u4e2d\u8bbf\u95ee\u5bf9\u5e94\u7684\u6876\uff0c\u4ece\u800c\u83b7\u53d6 value
\u3002
\u8bbe\u6570\u7ec4\u957f\u5ea6 capacity = 100
\u3001\u54c8\u5e0c\u7b97\u6cd5 hash(key) = key
\uff0c\u6613\u5f97\u54c8\u5e0c\u51fd\u6570\u4e3a key % 100
\u3002\u56fe 6-2 \u4ee5 key
\u5b66\u53f7\u548c value
\u59d3\u540d\u4e3a\u4f8b\uff0c\u5c55\u793a\u4e86\u54c8\u5e0c\u51fd\u6570\u7684\u5de5\u4f5c\u539f\u7406\u3002
\u56fe 6-2 \u00a0 \u54c8\u5e0c\u51fd\u6570\u5de5\u4f5c\u539f\u7406
\u4ee5\u4e0b\u4ee3\u7801\u5b9e\u73b0\u4e86\u4e00\u4e2a\u7b80\u5355\u54c8\u5e0c\u8868\u3002\u5176\u4e2d\uff0c\u6211\u4eec\u5c06 key
\u548c value
\u5c01\u88c5\u6210\u4e00\u4e2a\u7c7b Pair
\uff0c\u4ee5\u8868\u793a\u952e\u503c\u5bf9\u3002
class Pair:\n \"\"\"\u952e\u503c\u5bf9\"\"\"\n\n def __init__(self, key: int, val: str):\n self.key = key\n self.val = val\n\nclass ArrayHashMap:\n \"\"\"\u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868\"\"\"\n\n def __init__(self):\n \"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n # \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\n self.buckets: list[Pair | None] = [None] * 100\n\n def hash_func(self, key: int) -> int:\n \"\"\"\u54c8\u5e0c\u51fd\u6570\"\"\"\n index = key % 100\n return index\n\n def get(self, key: int) -> str:\n \"\"\"\u67e5\u8be2\u64cd\u4f5c\"\"\"\n index: int = self.hash_func(key)\n pair: Pair = self.buckets[index]\n if pair is None:\n return None\n return pair.val\n\n def put(self, key: int, val: str):\n \"\"\"\u6dfb\u52a0\u64cd\u4f5c\"\"\"\n pair = Pair(key, val)\n index: int = self.hash_func(key)\n self.buckets[index] = pair\n\n def remove(self, key: int):\n \"\"\"\u5220\u9664\u64cd\u4f5c\"\"\"\n index: int = self.hash_func(key)\n # \u7f6e\u4e3a None \uff0c\u4ee3\u8868\u5220\u9664\n self.buckets[index] = None\n\n def entry_set(self) -> list[Pair]:\n \"\"\"\u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9\"\"\"\n result: list[Pair] = []\n for pair in self.buckets:\n if pair is not None:\n result.append(pair)\n return result\n\n def key_set(self) -> list[int]:\n \"\"\"\u83b7\u53d6\u6240\u6709\u952e\"\"\"\n result = []\n for pair in self.buckets:\n if pair is not None:\n result.append(pair.key)\n return result\n\n def value_set(self) -> list[str]:\n \"\"\"\u83b7\u53d6\u6240\u6709\u503c\"\"\"\n result = []\n for pair in self.buckets:\n if pair is not None:\n result.append(pair.val)\n return result\n\n def print(self):\n \"\"\"\u6253\u5370\u54c8\u5e0c\u8868\"\"\"\n for pair in self.buckets:\n if pair is not None:\n print(pair.key, \"->\", pair.val)\n
array_hash_map.cpp/* \u952e\u503c\u5bf9 */\nstruct Pair {\n public:\n int key;\n string val;\n Pair(int key, string val) {\n this->key = key;\n this->val = val;\n }\n};\n\n/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\nclass ArrayHashMap {\n private:\n vector<Pair *> buckets;\n\n public:\n ArrayHashMap() {\n // \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\n buckets = vector<Pair *>(100);\n }\n\n ~ArrayHashMap() {\n // \u91ca\u653e\u5185\u5b58\n for (const auto &bucket : buckets) {\n delete bucket;\n }\n buckets.clear();\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n int hashFunc(int key) {\n int index = key % 100;\n return index;\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n string get(int key) {\n int index = hashFunc(key);\n Pair *pair = buckets[index];\n if (pair == nullptr)\n return \"\";\n return pair->val;\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n void put(int key, string val) {\n Pair *pair = new Pair(key, val);\n int index = hashFunc(key);\n buckets[index] = pair;\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n void remove(int key) {\n int index = hashFunc(key);\n // \u91ca\u653e\u5185\u5b58\u5e76\u7f6e\u4e3a nullptr\n delete buckets[index];\n buckets[index] = nullptr;\n }\n\n /* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\n vector<Pair *> pairSet() {\n vector<Pair *> pairSet;\n for (Pair *pair : buckets) {\n if (pair != nullptr) {\n pairSet.push_back(pair);\n }\n }\n return pairSet;\n }\n\n /* \u83b7\u53d6\u6240\u6709\u952e */\n vector<int> keySet() {\n vector<int> keySet;\n for (Pair *pair : buckets) {\n if (pair != nullptr) {\n keySet.push_back(pair->key);\n }\n }\n return keySet;\n }\n\n /* \u83b7\u53d6\u6240\u6709\u503c */\n vector<string> valueSet() {\n vector<string> valueSet;\n for (Pair *pair : buckets) {\n if (pair != nullptr) {\n valueSet.push_back(pair->val);\n }\n }\n return valueSet;\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n void print() {\n for (Pair *kv : pairSet()) {\n cout << kv->key << \" -> \" << kv->val << endl;\n }\n }\n};\n
array_hash_map.java/* \u952e\u503c\u5bf9 */\nclass Pair {\n public int key;\n public String val;\n\n public Pair(int key, String val) {\n this.key = key;\n this.val = val;\n }\n}\n\n/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\nclass ArrayHashMap {\n private List<Pair> buckets;\n\n public ArrayHashMap() {\n // \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\n buckets = new ArrayList<>();\n for (int i = 0; i < 100; i++) {\n buckets.add(null);\n }\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n private int hashFunc(int key) {\n int index = key % 100;\n return index;\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n public String get(int key) {\n int index = hashFunc(key);\n Pair pair = buckets.get(index);\n if (pair == null)\n return null;\n return pair.val;\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n public void put(int key, String val) {\n Pair pair = new Pair(key, val);\n int index = hashFunc(key);\n buckets.set(index, pair);\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n public void remove(int key) {\n int index = hashFunc(key);\n // \u7f6e\u4e3a null \uff0c\u4ee3\u8868\u5220\u9664\n buckets.set(index, null);\n }\n\n /* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\n public List<Pair> pairSet() {\n List<Pair> pairSet = new ArrayList<>();\n for (Pair pair : buckets) {\n if (pair != null)\n pairSet.add(pair);\n }\n return pairSet;\n }\n\n /* \u83b7\u53d6\u6240\u6709\u952e */\n public List<Integer> keySet() {\n List<Integer> keySet = new ArrayList<>();\n for (Pair pair : buckets) {\n if (pair != null)\n keySet.add(pair.key);\n }\n return keySet;\n }\n\n /* \u83b7\u53d6\u6240\u6709\u503c */\n public List<String> valueSet() {\n List<String> valueSet = new ArrayList<>();\n for (Pair pair : buckets) {\n if (pair != null)\n valueSet.add(pair.val);\n }\n return valueSet;\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n public void print() {\n for (Pair kv : pairSet()) {\n System.out.println(kv.key + \" -> \" + kv.val);\n }\n }\n}\n
array_hash_map.cs/* \u952e\u503c\u5bf9 int->string */\nclass Pair(int key, string val) {\n public int key = key;\n public string val = val;\n}\n\n/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\nclass ArrayHashMap {\n List<Pair?> buckets;\n public ArrayHashMap() {\n // \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\n buckets = [];\n for (int i = 0; i < 100; i++) {\n buckets.Add(null);\n }\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n int HashFunc(int key) {\n int index = key % 100;\n return index;\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n public string? Get(int key) {\n int index = HashFunc(key);\n Pair? pair = buckets[index];\n if (pair == null) return null;\n return pair.val;\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n public void Put(int key, string val) {\n Pair pair = new(key, val);\n int index = HashFunc(key);\n buckets[index] = pair;\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n public void Remove(int key) {\n int index = HashFunc(key);\n // \u7f6e\u4e3a null \uff0c\u4ee3\u8868\u5220\u9664\n buckets[index] = null;\n }\n\n /* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\n public List<Pair> PairSet() {\n List<Pair> pairSet = [];\n foreach (Pair? pair in buckets) {\n if (pair != null)\n pairSet.Add(pair);\n }\n return pairSet;\n }\n\n /* \u83b7\u53d6\u6240\u6709\u952e */\n public List<int> KeySet() {\n List<int> keySet = [];\n foreach (Pair? pair in buckets) {\n if (pair != null)\n keySet.Add(pair.key);\n }\n return keySet;\n }\n\n /* \u83b7\u53d6\u6240\u6709\u503c */\n public List<string> ValueSet() {\n List<string> valueSet = [];\n foreach (Pair? pair in buckets) {\n if (pair != null)\n valueSet.Add(pair.val);\n }\n return valueSet;\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n public void Print() {\n foreach (Pair kv in PairSet()) {\n Console.WriteLine(kv.key + \" -> \" + kv.val);\n }\n }\n}\n
array_hash_map.go/* \u952e\u503c\u5bf9 */\ntype pair struct {\n key int\n val string\n}\n\n/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\ntype arrayHashMap struct {\n buckets []*pair\n}\n\n/* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nfunc newArrayHashMap() *arrayHashMap {\n // \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\n buckets := make([]*pair, 100)\n return &arrayHashMap{buckets: buckets}\n}\n\n/* \u54c8\u5e0c\u51fd\u6570 */\nfunc (a *arrayHashMap) hashFunc(key int) int {\n index := key % 100\n return index\n}\n\n/* \u67e5\u8be2\u64cd\u4f5c */\nfunc (a *arrayHashMap) get(key int) string {\n index := a.hashFunc(key)\n pair := a.buckets[index]\n if pair == nil {\n return \"Not Found\"\n }\n return pair.val\n}\n\n/* \u6dfb\u52a0\u64cd\u4f5c */\nfunc (a *arrayHashMap) put(key int, val string) {\n pair := &pair{key: key, val: val}\n index := a.hashFunc(key)\n a.buckets[index] = pair\n}\n\n/* \u5220\u9664\u64cd\u4f5c */\nfunc (a *arrayHashMap) remove(key int) {\n index := a.hashFunc(key)\n // \u7f6e\u4e3a nil \uff0c\u4ee3\u8868\u5220\u9664\n a.buckets[index] = nil\n}\n\n/* \u83b7\u53d6\u6240\u6709\u952e\u5bf9 */\nfunc (a *arrayHashMap) pairSet() []*pair {\n var pairs []*pair\n for _, pair := range a.buckets {\n if pair != nil {\n pairs = append(pairs, pair)\n }\n }\n return pairs\n}\n\n/* \u83b7\u53d6\u6240\u6709\u952e */\nfunc (a *arrayHashMap) keySet() []int {\n var keys []int\n for _, pair := range a.buckets {\n if pair != nil {\n keys = append(keys, pair.key)\n }\n }\n return keys\n}\n\n/* \u83b7\u53d6\u6240\u6709\u503c */\nfunc (a *arrayHashMap) valueSet() []string {\n var values []string\n for _, pair := range a.buckets {\n if pair != nil {\n values = append(values, pair.val)\n }\n }\n return values\n}\n\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nfunc (a *arrayHashMap) print() {\n for _, pair := range a.buckets {\n if pair != nil {\n fmt.Println(pair.key, \"->\", pair.val)\n }\n }\n}\n
array_hash_map.swift/* \u952e\u503c\u5bf9 */\nclass Pair: Equatable {\n public var key: Int\n public var val: String\n\n public init(key: Int, val: String) {\n self.key = key\n self.val = val\n }\n\n public static func == (lhs: Pair, rhs: Pair) -> Bool {\n lhs.key == rhs.key && lhs.val == rhs.val\n }\n}\n\n/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\nclass ArrayHashMap {\n private var buckets: [Pair?] = []\n\n init() {\n // \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\n for _ in 0 ..< 100 {\n buckets.append(nil)\n }\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n private func hashFunc(key: Int) -> Int {\n let index = key % 100\n return index\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n func get(key: Int) -> String? {\n let index = hashFunc(key: key)\n let pair = buckets[index]\n return pair?.val\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n func put(key: Int, val: String) {\n let pair = Pair(key: key, val: val)\n let index = hashFunc(key: key)\n buckets[index] = pair\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n func remove(key: Int) {\n let index = hashFunc(key: key)\n // \u7f6e\u4e3a nil \uff0c\u4ee3\u8868\u5220\u9664\n buckets[index] = nil\n }\n\n /* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\n func pairSet() -> [Pair] {\n var pairSet: [Pair] = []\n for pair in buckets {\n if let pair = pair {\n pairSet.append(pair)\n }\n }\n return pairSet\n }\n\n /* \u83b7\u53d6\u6240\u6709\u952e */\n func keySet() -> [Int] {\n var keySet: [Int] = []\n for pair in buckets {\n if let pair = pair {\n keySet.append(pair.key)\n }\n }\n return keySet\n }\n\n /* \u83b7\u53d6\u6240\u6709\u503c */\n func valueSet() -> [String] {\n var valueSet: [String] = []\n for pair in buckets {\n if let pair = pair {\n valueSet.append(pair.val)\n }\n }\n return valueSet\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n func print() {\n for pair in pairSet() {\n Swift.print(\"\\(pair.key) -> \\(pair.val)\")\n }\n }\n}\n
array_hash_map.js/* \u952e\u503c\u5bf9 Number -> String */\nclass Pair {\n constructor(key, val) {\n this.key = key;\n this.val = val;\n }\n}\n\n/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\nclass ArrayHashMap {\n #buckets;\n constructor() {\n // \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\n this.#buckets = new Array(100).fill(null);\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n #hashFunc(key) {\n return key % 100;\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n get(key) {\n let index = this.#hashFunc(key);\n let pair = this.#buckets[index];\n if (pair === null) return null;\n return pair.val;\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n set(key, val) {\n let index = this.#hashFunc(key);\n this.#buckets[index] = new Pair(key, val);\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n delete(key) {\n let index = this.#hashFunc(key);\n // \u7f6e\u4e3a null \uff0c\u4ee3\u8868\u5220\u9664\n this.#buckets[index] = null;\n }\n\n /* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\n entries() {\n let arr = [];\n for (let i = 0; i < this.#buckets.length; i++) {\n if (this.#buckets[i]) {\n arr.push(this.#buckets[i]);\n }\n }\n return arr;\n }\n\n /* \u83b7\u53d6\u6240\u6709\u952e */\n keys() {\n let arr = [];\n for (let i = 0; i < this.#buckets.length; i++) {\n if (this.#buckets[i]) {\n arr.push(this.#buckets[i].key);\n }\n }\n return arr;\n }\n\n /* \u83b7\u53d6\u6240\u6709\u503c */\n values() {\n let arr = [];\n for (let i = 0; i < this.#buckets.length; i++) {\n if (this.#buckets[i]) {\n arr.push(this.#buckets[i].val);\n }\n }\n return arr;\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n print() {\n let pairSet = this.entries();\n for (const pair of pairSet) {\n console.info(`${pair.key} -> ${pair.val}`);\n }\n }\n}\n
array_hash_map.ts/* \u952e\u503c\u5bf9 Number -> String */\nclass Pair {\n public key: number;\n public val: string;\n\n constructor(key: number, val: string) {\n this.key = key;\n this.val = val;\n }\n}\n\n/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\nclass ArrayHashMap {\n private readonly buckets: (Pair | null)[];\n\n constructor() {\n // \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\n this.buckets = new Array(100).fill(null);\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n private hashFunc(key: number): number {\n return key % 100;\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n public get(key: number): string | null {\n let index = this.hashFunc(key);\n let pair = this.buckets[index];\n if (pair === null) return null;\n return pair.val;\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n public set(key: number, val: string) {\n let index = this.hashFunc(key);\n this.buckets[index] = new Pair(key, val);\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n public delete(key: number) {\n let index = this.hashFunc(key);\n // \u7f6e\u4e3a null \uff0c\u4ee3\u8868\u5220\u9664\n this.buckets[index] = null;\n }\n\n /* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\n public entries(): (Pair | null)[] {\n let arr: (Pair | null)[] = [];\n for (let i = 0; i < this.buckets.length; i++) {\n if (this.buckets[i]) {\n arr.push(this.buckets[i]);\n }\n }\n return arr;\n }\n\n /* \u83b7\u53d6\u6240\u6709\u952e */\n public keys(): (number | undefined)[] {\n let arr: (number | undefined)[] = [];\n for (let i = 0; i < this.buckets.length; i++) {\n if (this.buckets[i]) {\n arr.push(this.buckets[i].key);\n }\n }\n return arr;\n }\n\n /* \u83b7\u53d6\u6240\u6709\u503c */\n public values(): (string | undefined)[] {\n let arr: (string | undefined)[] = [];\n for (let i = 0; i < this.buckets.length; i++) {\n if (this.buckets[i]) {\n arr.push(this.buckets[i].val);\n }\n }\n return arr;\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n public print() {\n let pairSet = this.entries();\n for (const pair of pairSet) {\n console.info(`${pair.key} -> ${pair.val}`);\n }\n }\n}\n
array_hash_map.dart/* \u952e\u503c\u5bf9 */\nclass Pair {\n int key;\n String val;\n Pair(this.key, this.val);\n}\n\n/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\nclass ArrayHashMap {\n late List<Pair?> _buckets;\n\n ArrayHashMap() {\n // \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\n _buckets = List.filled(100, null);\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n int _hashFunc(int key) {\n final int index = key % 100;\n return index;\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n String? get(int key) {\n final int index = _hashFunc(key);\n final Pair? pair = _buckets[index];\n if (pair == null) {\n return null;\n }\n return pair.val;\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n void put(int key, String val) {\n final Pair pair = Pair(key, val);\n final int index = _hashFunc(key);\n _buckets[index] = pair;\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n void remove(int key) {\n final int index = _hashFunc(key);\n _buckets[index] = null;\n }\n\n /* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\n List<Pair> pairSet() {\n List<Pair> pairSet = [];\n for (final Pair? pair in _buckets) {\n if (pair != null) {\n pairSet.add(pair);\n }\n }\n return pairSet;\n }\n\n /* \u83b7\u53d6\u6240\u6709\u952e */\n List<int> keySet() {\n List<int> keySet = [];\n for (final Pair? pair in _buckets) {\n if (pair != null) {\n keySet.add(pair.key);\n }\n }\n return keySet;\n }\n\n /* \u83b7\u53d6\u6240\u6709\u503c */\n List<String> values() {\n List<String> valueSet = [];\n for (final Pair? pair in _buckets) {\n if (pair != null) {\n valueSet.add(pair.val);\n }\n }\n return valueSet;\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n void printHashMap() {\n for (final Pair kv in pairSet()) {\n print(\"${kv.key} -> ${kv.val}\");\n }\n }\n}\n
array_hash_map.rs/* \u952e\u503c\u5bf9 */\n#[derive(Debug, Clone, PartialEq)]\npub struct Pair {\n pub key: i32,\n pub val: String,\n}\n\n/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\npub struct ArrayHashMap {\n buckets: Vec<Option<Pair>>\n}\n\nimpl ArrayHashMap {\n pub fn new() -> ArrayHashMap {\n // \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\n Self { buckets: vec![None; 100] }\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n fn hash_func(&self, key: i32) -> usize {\n key as usize % 100\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n pub fn get(&self, key: i32) -> Option<&String> {\n let index = self.hash_func(key);\n self.buckets[index].as_ref().map(|pair| &pair.val)\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n pub fn put(&mut self, key: i32, val: &str) {\n let index = self.hash_func(key);\n self.buckets[index] = Some(Pair {\n key,\n val: val.to_string(),\n });\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n pub fn remove(&mut self, key: i32) {\n let index = self.hash_func(key);\n // \u7f6e\u4e3a None \uff0c\u4ee3\u8868\u5220\u9664\n self.buckets[index] = None;\n }\n\n /* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\n pub fn entry_set(&self) -> Vec<&Pair> {\n self.buckets.iter().filter_map(|pair| pair.as_ref()).collect()\n }\n\n /* \u83b7\u53d6\u6240\u6709\u952e */\n pub fn key_set(&self) -> Vec<&i32> {\n self.buckets.iter().filter_map(|pair| pair.as_ref().map(|pair| &pair.key)).collect()\n }\n\n /* \u83b7\u53d6\u6240\u6709\u503c */\n pub fn value_set(&self) -> Vec<&String> {\n self.buckets.iter().filter_map(|pair| pair.as_ref().map(|pair| &pair.val)).collect()\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n pub fn print(&self) {\n for pair in self.entry_set() {\n println!(\"{} -> {}\", pair.key, pair.val);\n }\n }\n}\n
array_hash_map.c/* \u952e\u503c\u5bf9 int->string */\ntypedef struct {\n int key;\n char *val;\n} Pair;\n\n/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\ntypedef struct {\n Pair *buckets[HASHTABLE_CAPACITY];\n} ArrayHashMap;\n\n/* \u6784\u9020\u51fd\u6570 */\nArrayHashMap *newArrayHashMap() {\n ArrayHashMap *hmap = malloc(sizeof(ArrayHashMap));\n return hmap;\n}\n\n/* \u6790\u6784\u51fd\u6570 */\nvoid delArrayHashMap(ArrayHashMap *hmap) {\n for (int i = 0; i < HASHTABLE_CAPACITY; i++) {\n if (hmap->buckets[i] != NULL) {\n free(hmap->buckets[i]->val);\n free(hmap->buckets[i]);\n }\n }\n free(hmap);\n}\n\n/* \u6dfb\u52a0\u64cd\u4f5c */\nvoid put(ArrayHashMap *hmap, const int key, const char *val) {\n Pair *Pair = malloc(sizeof(Pair));\n Pair->key = key;\n Pair->val = malloc(strlen(val) + 1);\n strcpy(Pair->val, val);\n\n int index = hashFunc(key);\n hmap->buckets[index] = Pair;\n}\n\n/* \u5220\u9664\u64cd\u4f5c */\nvoid removeItem(ArrayHashMap *hmap, const int key) {\n int index = hashFunc(key);\n free(hmap->buckets[index]->val);\n free(hmap->buckets[index]);\n hmap->buckets[index] = NULL;\n}\n\n/* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\nvoid pairSet(ArrayHashMap *hmap, MapSet *set) {\n Pair *entries;\n int i = 0, index = 0;\n int total = 0;\n /* \u7edf\u8ba1\u6709\u6548\u952e\u503c\u5bf9\u6570\u91cf */\n for (i = 0; i < HASHTABLE_CAPACITY; i++) {\n if (hmap->buckets[i] != NULL) {\n total++;\n }\n }\n entries = malloc(sizeof(Pair) * total);\n for (i = 0; i < HASHTABLE_CAPACITY; i++) {\n if (hmap->buckets[i] != NULL) {\n entries[index].key = hmap->buckets[i]->key;\n entries[index].val = malloc(strlen(hmap->buckets[i]->val) + 1);\n strcpy(entries[index].val, hmap->buckets[i]->val);\n index++;\n }\n }\n set->set = entries;\n set->len = total;\n}\n\n/* \u83b7\u53d6\u6240\u6709\u952e */\nvoid keySet(ArrayHashMap *hmap, MapSet *set) {\n int *keys;\n int i = 0, index = 0;\n int total = 0;\n /* \u7edf\u8ba1\u6709\u6548\u952e\u503c\u5bf9\u6570\u91cf */\n for (i = 0; i < HASHTABLE_CAPACITY; i++) {\n if (hmap->buckets[i] != NULL) {\n total++;\n }\n }\n keys = malloc(total * sizeof(int));\n for (i = 0; i < HASHTABLE_CAPACITY; i++) {\n if (hmap->buckets[i] != NULL) {\n keys[index] = hmap->buckets[i]->key;\n index++;\n }\n }\n set->set = keys;\n set->len = total;\n}\n\n/* \u83b7\u53d6\u6240\u6709\u503c */\nvoid valueSet(ArrayHashMap *hmap, MapSet *set) {\n char **vals;\n int i = 0, index = 0;\n int total = 0;\n /* \u7edf\u8ba1\u6709\u6548\u952e\u503c\u5bf9\u6570\u91cf */\n for (i = 0; i < HASHTABLE_CAPACITY; i++) {\n if (hmap->buckets[i] != NULL) {\n total++;\n }\n }\n vals = malloc(total * sizeof(char *));\n for (i = 0; i < HASHTABLE_CAPACITY; i++) {\n if (hmap->buckets[i] != NULL) {\n vals[index] = hmap->buckets[i]->val;\n index++;\n }\n }\n set->set = vals;\n set->len = total;\n}\n\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nvoid print(ArrayHashMap *hmap) {\n int i;\n MapSet set;\n pairSet(hmap, &set);\n Pair *entries = (Pair *)set.set;\n for (i = 0; i < set.len; i++) {\n printf(\"%d -> %s\\n\", entries[i].key, entries[i].val);\n }\n free(set.set);\n}\n
array_hash_map.zig// \u952e\u503c\u5bf9\nconst Pair = struct {\n key: usize = undefined,\n val: []const u8 = undefined,\n\n pub fn init(key: usize, val: []const u8) Pair {\n return Pair {\n .key = key,\n .val = val,\n };\n }\n};\n\n// \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868\nfn ArrayHashMap(comptime T: type) type {\n return struct {\n bucket: ?std.ArrayList(?T) = null,\n mem_allocator: std.mem.Allocator = undefined,\n\n const Self = @This();\n\n // \u6784\u9020\u51fd\u6570\n pub fn init(self: *Self, allocator: std.mem.Allocator) !void {\n self.mem_allocator = allocator;\n // \u521d\u59cb\u5316\u4e00\u4e2a\u957f\u5ea6\u4e3a 100 \u7684\u6876\uff08\u6570\u7ec4\uff09\n self.bucket = std.ArrayList(?T).init(self.mem_allocator);\n var i: i32 = 0;\n while (i < 100) : (i += 1) {\n try self.bucket.?.append(null);\n }\n }\n\n // \u6790\u6784\u51fd\u6570\n pub fn deinit(self: *Self) void {\n if (self.bucket != null) self.bucket.?.deinit();\n }\n\n // \u54c8\u5e0c\u51fd\u6570\n fn hashFunc(key: usize) usize {\n var index = key % 100;\n return index;\n }\n\n // \u67e5\u8be2\u64cd\u4f5c\n pub fn get(self: *Self, key: usize) []const u8 {\n var index = hashFunc(key);\n var pair = self.bucket.?.items[index];\n return pair.?.val;\n }\n\n // \u6dfb\u52a0\u64cd\u4f5c\n pub fn put(self: *Self, key: usize, val: []const u8) !void {\n var pair = Pair.init(key, val);\n var index = hashFunc(key);\n self.bucket.?.items[index] = pair;\n }\n\n // \u5220\u9664\u64cd\u4f5c\n pub fn remove(self: *Self, key: usize) !void {\n var index = hashFunc(key);\n // \u7f6e\u4e3a null \uff0c\u4ee3\u8868\u5220\u9664\n self.bucket.?.items[index] = null;\n } \n\n // \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9\n pub fn pairSet(self: *Self) !std.ArrayList(T) {\n var entry_set = std.ArrayList(T).init(self.mem_allocator);\n for (self.bucket.?.items) |item| {\n if (item == null) continue;\n try entry_set.append(item.?);\n }\n return entry_set;\n } \n\n // \u83b7\u53d6\u6240\u6709\u952e\n pub fn keySet(self: *Self) !std.ArrayList(usize) {\n var key_set = std.ArrayList(usize).init(self.mem_allocator);\n for (self.bucket.?.items) |item| {\n if (item == null) continue;\n try key_set.append(item.?.key);\n }\n return key_set;\n } \n\n // \u83b7\u53d6\u6240\u6709\u503c\n pub fn valueSet(self: *Self) !std.ArrayList([]const u8) {\n var value_set = std.ArrayList([]const u8).init(self.mem_allocator);\n for (self.bucket.?.items) |item| {\n if (item == null) continue;\n try value_set.append(item.?.val);\n }\n return value_set;\n }\n\n // \u6253\u5370\u54c8\u5e0c\u8868\n pub fn print(self: *Self) !void {\n var entry_set = try self.pairSet();\n defer entry_set.deinit();\n for (entry_set.items) |item| {\n std.debug.print(\"{} -> {s}\\n\", .{item.key, item.val});\n }\n }\n };\n}\n
"},{"location":"chapter_hashing/hash_map/#613","title":"6.1.3 \u00a0 \u54c8\u5e0c\u51b2\u7a81\u4e0e\u6269\u5bb9","text":"\u4ece\u672c\u8d28\u4e0a\u770b\uff0c\u54c8\u5e0c\u51fd\u6570\u7684\u4f5c\u7528\u662f\u5c06\u6240\u6709 key
\u6784\u6210\u7684\u8f93\u5165\u7a7a\u95f4\u6620\u5c04\u5230\u6570\u7ec4\u6240\u6709\u7d22\u5f15\u6784\u6210\u7684\u8f93\u51fa\u7a7a\u95f4\uff0c\u800c\u8f93\u5165\u7a7a\u95f4\u5f80\u5f80\u8fdc\u5927\u4e8e\u8f93\u51fa\u7a7a\u95f4\u3002\u56e0\u6b64\uff0c\u7406\u8bba\u4e0a\u4e00\u5b9a\u5b58\u5728\u201c\u591a\u4e2a\u8f93\u5165\u5bf9\u5e94\u76f8\u540c\u8f93\u51fa\u201d\u7684\u60c5\u51b5\u3002
\u5bf9\u4e8e\u4e0a\u8ff0\u793a\u4f8b\u4e2d\u7684\u54c8\u5e0c\u51fd\u6570\uff0c\u5f53\u8f93\u5165\u7684 key
\u540e\u4e24\u4f4d\u76f8\u540c\u65f6\uff0c\u54c8\u5e0c\u51fd\u6570\u7684\u8f93\u51fa\u7ed3\u679c\u4e5f\u76f8\u540c\u3002\u4f8b\u5982\uff0c\u67e5\u8be2\u5b66\u53f7\u4e3a 12836 \u548c 20336 \u7684\u4e24\u4e2a\u5b66\u751f\u65f6\uff0c\u6211\u4eec\u5f97\u5230\uff1a
12836 % 100 = 36\n20336 % 100 = 36\n
\u5982\u56fe 6-3 \u6240\u793a\uff0c\u4e24\u4e2a\u5b66\u53f7\u6307\u5411\u4e86\u540c\u4e00\u4e2a\u59d3\u540d\uff0c\u8fd9\u663e\u7136\u662f\u4e0d\u5bf9\u7684\u3002\u6211\u4eec\u5c06\u8fd9\u79cd\u591a\u4e2a\u8f93\u5165\u5bf9\u5e94\u540c\u4e00\u8f93\u51fa\u7684\u60c5\u51b5\u79f0\u4e3a\u300c\u54c8\u5e0c\u51b2\u7a81 hash collision\u300d\u3002
\u56fe 6-3 \u00a0 \u54c8\u5e0c\u51b2\u7a81\u793a\u4f8b
\u5bb9\u6613\u60f3\u5230\uff0c\u54c8\u5e0c\u8868\u5bb9\u91cf \\(n\\) \u8d8a\u5927\uff0c\u591a\u4e2a key
\u88ab\u5206\u914d\u5230\u540c\u4e00\u4e2a\u6876\u4e2d\u7684\u6982\u7387\u5c31\u8d8a\u4f4e\uff0c\u51b2\u7a81\u5c31\u8d8a\u5c11\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u6269\u5bb9\u54c8\u5e0c\u8868\u6765\u51cf\u5c11\u54c8\u5e0c\u51b2\u7a81\u3002
\u5982\u56fe 6-4 \u6240\u793a\uff0c\u6269\u5bb9\u524d\u952e\u503c\u5bf9 (136, A)
\u548c (236, D)
\u53d1\u751f\u51b2\u7a81\uff0c\u6269\u5bb9\u540e\u51b2\u7a81\u6d88\u5931\u3002
\u56fe 6-4 \u00a0 \u54c8\u5e0c\u8868\u6269\u5bb9
\u7c7b\u4f3c\u4e8e\u6570\u7ec4\u6269\u5bb9\uff0c\u54c8\u5e0c\u8868\u6269\u5bb9\u9700\u5c06\u6240\u6709\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u8fc1\u79fb\u81f3\u65b0\u54c8\u5e0c\u8868\uff0c\u975e\u5e38\u8017\u65f6\uff1b\u5e76\u4e14\u7531\u4e8e\u54c8\u5e0c\u8868\u5bb9\u91cf capacity
\u6539\u53d8\uff0c\u6211\u4eec\u9700\u8981\u901a\u8fc7\u54c8\u5e0c\u51fd\u6570\u6765\u91cd\u65b0\u8ba1\u7b97\u6240\u6709\u952e\u503c\u5bf9\u7684\u5b58\u50a8\u4f4d\u7f6e\uff0c\u8fd9\u8fdb\u4e00\u6b65\u63d0\u9ad8\u4e86\u6269\u5bb9\u8fc7\u7a0b\u7684\u8ba1\u7b97\u5f00\u9500\u3002\u4e3a\u6b64\uff0c\u7f16\u7a0b\u8bed\u8a00\u901a\u5e38\u4f1a\u9884\u7559\u8db3\u591f\u5927\u7684\u54c8\u5e0c\u8868\u5bb9\u91cf\uff0c\u9632\u6b62\u9891\u7e41\u6269\u5bb9\u3002
\u300c\u8d1f\u8f7d\u56e0\u5b50 load factor\u300d\u662f\u54c8\u5e0c\u8868\u7684\u4e00\u4e2a\u91cd\u8981\u6982\u5ff5\uff0c\u5176\u5b9a\u4e49\u4e3a\u54c8\u5e0c\u8868\u7684\u5143\u7d20\u6570\u91cf\u9664\u4ee5\u6876\u6570\u91cf\uff0c\u7528\u4e8e\u8861\u91cf\u54c8\u5e0c\u51b2\u7a81\u7684\u4e25\u91cd\u7a0b\u5ea6\uff0c\u4e5f\u5e38\u4f5c\u4e3a\u54c8\u5e0c\u8868\u6269\u5bb9\u7684\u89e6\u53d1\u6761\u4ef6\u3002\u4f8b\u5982\u5728 Java \u4e2d\uff0c\u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7 \\(0.75\\) \u65f6\uff0c\u7cfb\u7edf\u4f1a\u5c06\u54c8\u5e0c\u8868\u6269\u5bb9\u81f3\u539f\u5148\u7684 \\(2\\) \u500d\u3002
"},{"location":"chapter_hashing/summary/","title":"6.4 \u00a0 \u5c0f\u7ed3","text":""},{"location":"chapter_hashing/summary/#1","title":"1. \u00a0 \u91cd\u70b9\u56de\u987e","text":"key
\uff0c\u54c8\u5e0c\u8868\u80fd\u591f\u5728 \\(O(1)\\) \u65f6\u95f4\u5185\u67e5\u8be2\u5230 value
\uff0c\u6548\u7387\u975e\u5e38\u9ad8\u3002key
\u6620\u5c04\u4e3a\u6570\u7ec4\u7d22\u5f15\uff0c\u4ece\u800c\u8bbf\u95ee\u5bf9\u5e94\u6876\u5e76\u83b7\u53d6 value
\u3002key
\u53ef\u80fd\u5728\u7ecf\u8fc7\u54c8\u5e0c\u51fd\u6570\u540e\u5f97\u5230\u76f8\u540c\u7684\u6570\u7ec4\u7d22\u5f15\uff0c\u5bfc\u81f4\u67e5\u8be2\u7ed3\u679c\u51fa\u9519\uff0c\u8fd9\u79cd\u73b0\u8c61\u88ab\u79f0\u4e3a\u54c8\u5e0c\u51b2\u7a81\u3002HashMap
\u4f7f\u7528\u94fe\u5f0f\u5730\u5740\uff0c\u800c Python \u7684 Dict
\u91c7\u7528\u5f00\u653e\u5bfb\u5740\u3002\u54c8\u5e0c\u8868\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a\u4ec0\u4e48\u4e0d\u662f \\(O(n)\\) \uff1f
\u5f53\u54c8\u5e0c\u51b2\u7a81\u6bd4\u8f83\u4e25\u91cd\u65f6\uff0c\u54c8\u5e0c\u8868\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4f1a\u9000\u5316\u81f3 \\(O(n)\\) \u3002\u5f53\u54c8\u5e0c\u51fd\u6570\u8bbe\u8ba1\u5f97\u6bd4\u8f83\u597d\u3001\u5bb9\u91cf\u8bbe\u7f6e\u6bd4\u8f83\u5408\u7406\u3001\u51b2\u7a81\u6bd4\u8f83\u5e73\u5747\u65f6\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u662f \\(O(1)\\) \u3002\u6211\u4eec\u4f7f\u7528\u7f16\u7a0b\u8bed\u8a00\u5185\u7f6e\u7684\u54c8\u5e0c\u8868\u65f6\uff0c\u901a\u5e38\u8ba4\u4e3a\u65f6\u95f4\u590d\u6742\u5ea6\u662f \\(O(1)\\) \u3002
\u4e3a\u4ec0\u4e48\u4e0d\u4f7f\u7528\u54c8\u5e0c\u51fd\u6570 \\(f(x) = x\\) \u5462\uff1f\u8fd9\u6837\u5c31\u4e0d\u4f1a\u6709\u51b2\u7a81\u4e86
\u5728 \\(f(x) = x\\) \u54c8\u5e0c\u51fd\u6570\u4e0b\uff0c\u6bcf\u4e2a\u5143\u7d20\u5bf9\u5e94\u552f\u4e00\u7684\u6876\u7d22\u5f15\uff0c\u8fd9\u4e0e\u6570\u7ec4\u7b49\u4ef7\u3002\u7136\u800c\uff0c\u8f93\u5165\u7a7a\u95f4\u901a\u5e38\u8fdc\u5927\u4e8e\u8f93\u51fa\u7a7a\u95f4\uff08\u6570\u7ec4\u957f\u5ea6\uff09\uff0c\u56e0\u6b64\u54c8\u5e0c\u51fd\u6570\u7684\u6700\u540e\u4e00\u6b65\u5f80\u5f80\u662f\u5bf9\u6570\u7ec4\u957f\u5ea6\u53d6\u6a21\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u54c8\u5e0c\u8868\u7684\u76ee\u6807\u662f\u5c06\u4e00\u4e2a\u8f83\u5927\u7684\u72b6\u6001\u7a7a\u95f4\u6620\u5c04\u5230\u4e00\u4e2a\u8f83\u5c0f\u7684\u7a7a\u95f4\uff0c\u5e76\u63d0\u4f9b \\(O(1)\\) \u7684\u67e5\u8be2\u6548\u7387\u3002
\u54c8\u5e0c\u8868\u5e95\u5c42\u5b9e\u73b0\u662f\u6570\u7ec4\u3001\u94fe\u8868\u3001\u4e8c\u53c9\u6811\uff0c\u4f46\u4e3a\u4ec0\u4e48\u6548\u7387\u53ef\u4ee5\u6bd4\u5b83\u4eec\u66f4\u9ad8\u5462\uff1f
\u9996\u5148\uff0c\u54c8\u5e0c\u8868\u7684\u65f6\u95f4\u6548\u7387\u53d8\u9ad8\uff0c\u4f46\u7a7a\u95f4\u6548\u7387\u53d8\u4f4e\u4e86\u3002\u54c8\u5e0c\u8868\u6709\u76f8\u5f53\u4e00\u90e8\u5206\u5185\u5b58\u672a\u4f7f\u7528\u3002
\u5176\u6b21\uff0c\u53ea\u662f\u5728\u7279\u5b9a\u4f7f\u7528\u573a\u666f\u4e0b\u65f6\u95f4\u6548\u7387\u53d8\u9ad8\u4e86\u3002\u5982\u679c\u4e00\u4e2a\u529f\u80fd\u80fd\u591f\u5728\u76f8\u540c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e0b\u4f7f\u7528\u6570\u7ec4\u6216\u94fe\u8868\u5b9e\u73b0\uff0c\u90a3\u4e48\u901a\u5e38\u6bd4\u54c8\u5e0c\u8868\u66f4\u5feb\u3002\u8fd9\u662f\u56e0\u4e3a\u54c8\u5e0c\u51fd\u6570\u8ba1\u7b97\u9700\u8981\u5f00\u9500\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u7684\u5e38\u6570\u9879\u66f4\u5927\u3002
\u6700\u540e\uff0c\u54c8\u5e0c\u8868\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u53ef\u80fd\u53d1\u751f\u52a3\u5316\u3002\u4f8b\u5982\u5728\u94fe\u5f0f\u5730\u5740\u4e2d\uff0c\u6211\u4eec\u91c7\u53d6\u5728\u94fe\u8868\u6216\u7ea2\u9ed1\u6811\u4e2d\u6267\u884c\u67e5\u627e\u64cd\u4f5c\uff0c\u4ecd\u7136\u6709\u9000\u5316\u81f3 \\(O(n)\\) \u65f6\u95f4\u7684\u98ce\u9669\u3002
\u591a\u6b21\u54c8\u5e0c\u6709\u4e0d\u80fd\u76f4\u63a5\u5220\u9664\u5143\u7d20\u7684\u7f3a\u9677\u5417\uff1f\u6807\u8bb0\u4e3a\u5df2\u5220\u9664\u7684\u7a7a\u95f4\u8fd8\u80fd\u518d\u6b21\u4f7f\u7528\u5417\uff1f
\u591a\u6b21\u54c8\u5e0c\u662f\u5f00\u653e\u5bfb\u5740\u7684\u4e00\u79cd\uff0c\u5f00\u653e\u5bfb\u5740\u6cd5\u90fd\u6709\u4e0d\u80fd\u76f4\u63a5\u5220\u9664\u5143\u7d20\u7684\u7f3a\u9677\uff0c\u9700\u8981\u901a\u8fc7\u6807\u8bb0\u5220\u9664\u3002\u6807\u8bb0\u4e3a\u5df2\u5220\u9664\u7684\u7a7a\u95f4\u53ef\u4ee5\u518d\u6b21\u4f7f\u7528\u3002\u5f53\u5c06\u65b0\u5143\u7d20\u63d2\u5165\u54c8\u5e0c\u8868\uff0c\u5e76\u4e14\u901a\u8fc7\u54c8\u5e0c\u51fd\u6570\u627e\u5230\u6807\u8bb0\u4e3a\u5df2\u5220\u9664\u7684\u4f4d\u7f6e\u65f6\uff0c\u8be5\u4f4d\u7f6e\u53ef\u4ee5\u88ab\u65b0\u5143\u7d20\u4f7f\u7528\u3002\u8fd9\u6837\u505a\u65e2\u80fd\u4fdd\u6301\u54c8\u5e0c\u8868\u7684\u63a2\u6d4b\u5e8f\u5217\u4e0d\u53d8\uff0c\u53c8\u80fd\u4fdd\u8bc1\u54c8\u5e0c\u8868\u7684\u7a7a\u95f4\u4f7f\u7528\u7387\u3002
\u4e3a\u4ec0\u4e48\u5728\u7ebf\u6027\u63a2\u6d4b\u4e2d\uff0c\u67e5\u627e\u5143\u7d20\u7684\u65f6\u5019\u4f1a\u51fa\u73b0\u54c8\u5e0c\u51b2\u7a81\u5462\uff1f
\u67e5\u627e\u7684\u65f6\u5019\u901a\u8fc7\u54c8\u5e0c\u51fd\u6570\u627e\u5230\u5bf9\u5e94\u7684\u6876\u548c\u952e\u503c\u5bf9\uff0c\u53d1\u73b0 key
\u4e0d\u5339\u914d\uff0c\u8fd9\u5c31\u4ee3\u8868\u6709\u54c8\u5e0c\u51b2\u7a81\u3002\u56e0\u6b64\uff0c\u7ebf\u6027\u63a2\u6d4b\u6cd5\u4f1a\u6839\u636e\u9884\u5148\u8bbe\u5b9a\u7684\u6b65\u957f\u4f9d\u6b21\u5411\u4e0b\u67e5\u627e\uff0c\u76f4\u81f3\u627e\u5230\u6b63\u786e\u7684\u952e\u503c\u5bf9\u6216\u65e0\u6cd5\u627e\u5230\u8df3\u51fa\u4e3a\u6b62\u3002
\u4e3a\u4ec0\u4e48\u54c8\u5e0c\u8868\u6269\u5bb9\u80fd\u591f\u7f13\u89e3\u54c8\u5e0c\u51b2\u7a81\uff1f
\u54c8\u5e0c\u51fd\u6570\u7684\u6700\u540e\u4e00\u6b65\u5f80\u5f80\u662f\u5bf9\u6570\u7ec4\u957f\u5ea6 \\(n\\) \u53d6\u4f59\uff0c\u8ba9\u8f93\u51fa\u503c\u843d\u5728\u6570\u7ec4\u7d22\u5f15\u8303\u56f4\u5185\uff1b\u5728\u6269\u5bb9\u540e\uff0c\u6570\u7ec4\u957f\u5ea6 \\(n\\) \u53d1\u751f\u53d8\u5316\uff0c\u800c key
\u5bf9\u5e94\u7684\u7d22\u5f15\u4e5f\u53ef\u80fd\u53d1\u751f\u53d8\u5316\u3002\u539f\u5148\u843d\u5728\u540c\u4e00\u4e2a\u6876\u7684\u591a\u4e2a key
\uff0c\u5728\u6269\u5bb9\u540e\u53ef\u80fd\u4f1a\u88ab\u5206\u914d\u5230\u591a\u4e2a\u6876\u4e2d\uff0c\u4ece\u800c\u5b9e\u73b0\u54c8\u5e0c\u51b2\u7a81\u7684\u7f13\u89e3\u3002
Abstract
\u5806\u5c31\u50cf\u662f\u5c71\u5cb3\u5cf0\u5ce6\uff0c\u5c42\u53e0\u8d77\u4f0f\u3001\u5f62\u6001\u5404\u5f02\u3002
\u5ea7\u5ea7\u5c71\u5cf0\u9ad8\u4f4e\u9519\u843d\uff0c\u800c\u6700\u9ad8\u7684\u5c71\u5cf0\u603b\u662f\u6700\u5148\u6620\u5165\u773c\u5e18\u3002
"},{"location":"chapter_heap/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u5e0c\u671b\u4f7f\u7528\u4e00\u4e2a\u5217\u8868\u7684\u6240\u6709\u5143\u7d20\u6765\u6784\u5efa\u4e00\u4e2a\u5806\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u88ab\u79f0\u4e3a\u201c\u5efa\u5806\u64cd\u4f5c\u201d\u3002
"},{"location":"chapter_heap/build_heap/#821","title":"8.2.1 \u00a0 \u501f\u52a9\u5165\u5806\u64cd\u4f5c\u5b9e\u73b0","text":"\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e00\u4e2a\u7a7a\u5806\uff0c\u7136\u540e\u904d\u5386\u5217\u8868\uff0c\u4f9d\u6b21\u5bf9\u6bcf\u4e2a\u5143\u7d20\u6267\u884c\u201c\u5165\u5806\u64cd\u4f5c\u201d\uff0c\u5373\u5148\u5c06\u5143\u7d20\u6dfb\u52a0\u81f3\u5806\u7684\u5c3e\u90e8\uff0c\u518d\u5bf9\u8be5\u5143\u7d20\u6267\u884c\u201c\u4ece\u5e95\u81f3\u9876\u201d\u5806\u5316\u3002
\u6bcf\u5f53\u4e00\u4e2a\u5143\u7d20\u5165\u5806\uff0c\u5806\u7684\u957f\u5ea6\u5c31\u52a0\u4e00\u3002\u7531\u4e8e\u8282\u70b9\u662f\u4ece\u9876\u5230\u5e95\u4f9d\u6b21\u88ab\u6dfb\u52a0\u8fdb\u4e8c\u53c9\u6811\u7684\uff0c\u56e0\u6b64\u5806\u662f\u201c\u81ea\u4e0a\u800c\u4e0b\u201d\u6784\u5efa\u7684\u3002
\u8bbe\u5143\u7d20\u6570\u91cf\u4e3a \\(n\\) \uff0c\u6bcf\u4e2a\u5143\u7d20\u7684\u5165\u5806\u64cd\u4f5c\u4f7f\u7528 \\(O(\\log{n})\\) \u65f6\u95f4\uff0c\u56e0\u6b64\u8be5\u5efa\u5806\u65b9\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n \\log n)\\) \u3002
"},{"location":"chapter_heap/build_heap/#822","title":"8.2.2 \u00a0 \u901a\u8fc7\u904d\u5386\u5806\u5316\u5b9e\u73b0","text":"\u5b9e\u9645\u4e0a\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9e\u73b0\u4e00\u79cd\u66f4\u4e3a\u9ad8\u6548\u7684\u5efa\u5806\u65b9\u6cd5\uff0c\u5171\u5206\u4e3a\u4e24\u6b65\u3002
\u6bcf\u5f53\u5806\u5316\u4e00\u4e2a\u8282\u70b9\u540e\uff0c\u4ee5\u8be5\u8282\u70b9\u4e3a\u6839\u8282\u70b9\u7684\u5b50\u6811\u5c31\u5f62\u6210\u4e00\u4e2a\u5408\u6cd5\u7684\u5b50\u5806\u3002\u800c\u7531\u4e8e\u662f\u5012\u5e8f\u904d\u5386\uff0c\u56e0\u6b64\u5806\u662f\u201c\u81ea\u4e0b\u800c\u4e0a\u201d\u6784\u5efa\u7684\u3002
\u4e4b\u6240\u4ee5\u9009\u62e9\u5012\u5e8f\u904d\u5386\uff0c\u662f\u56e0\u4e3a\u8fd9\u6837\u80fd\u591f\u4fdd\u8bc1\u5f53\u524d\u8282\u70b9\u4e4b\u4e0b\u7684\u5b50\u6811\u5df2\u7ecf\u662f\u5408\u6cd5\u7684\u5b50\u5806\uff0c\u8fd9\u6837\u5806\u5316\u5f53\u524d\u8282\u70b9\u624d\u662f\u6709\u6548\u7684\u3002
\u503c\u5f97\u8bf4\u660e\u7684\u662f\uff0c\u53f6\u8282\u70b9\u6ca1\u6709\u5b50\u8282\u70b9\uff0c\u5929\u7136\u5c31\u662f\u5408\u6cd5\u7684\u5b50\u5806\uff0c\u56e0\u6b64\u65e0\u987b\u5806\u5316\u3002\u5982\u4ee5\u4e0b\u4ee3\u7801\u6240\u793a\uff0c\u6700\u540e\u4e00\u4e2a\u975e\u53f6\u8282\u70b9\u662f\u6700\u540e\u4e00\u4e2a\u8282\u70b9\u7684\u7236\u8282\u70b9\uff0c\u6211\u4eec\u4ece\u5b83\u5f00\u59cb\u5012\u5e8f\u904d\u5386\u5e76\u6267\u884c\u5806\u5316\u3002
PythonC++JavaC#GoSwiftJSTSDartRustCZig my_heap.pydef __init__(self, nums: list[int]):\n \"\"\"\u6784\u9020\u65b9\u6cd5\uff0c\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806\"\"\"\n # \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\n self.max_heap = nums\n # \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n for i in range(self.parent(self.size() - 1), -1, -1):\n self.sift_down(i)\n
my_heap.cpp/* \u6784\u9020\u65b9\u6cd5\uff0c\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806 */\nMaxHeap(vector<int> nums) {\n // \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\n maxHeap = nums;\n // \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n for (int i = parent(size() - 1); i >= 0; i--) {\n siftDown(i);\n }\n}\n
my_heap.java/* \u6784\u9020\u65b9\u6cd5\uff0c\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806 */\nMaxHeap(List<Integer> nums) {\n // \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\n maxHeap = new ArrayList<>(nums);\n // \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n for (int i = parent(size() - 1); i >= 0; i--) {\n siftDown(i);\n }\n}\n
my_heap.cs/* \u6784\u9020\u51fd\u6570\uff0c\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806 */\nMaxHeap(IEnumerable<int> nums) {\n // \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\n maxHeap = new List<int>(nums);\n // \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n var size = Parent(this.Size() - 1);\n for (int i = size; i >= 0; i--) {\n SiftDown(i);\n }\n}\n
my_heap.go/* \u6784\u9020\u51fd\u6570\uff0c\u6839\u636e\u5207\u7247\u5efa\u5806 */\nfunc newMaxHeap(nums []any) *maxHeap {\n // \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\n h := &maxHeap{data: nums}\n for i := h.parent(len(h.data) - 1); i >= 0; i-- {\n // \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n h.siftDown(i)\n }\n return h\n}\n
my_heap.swift/* \u6784\u9020\u65b9\u6cd5\uff0c\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806 */\ninit(nums: [Int]) {\n // \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\n maxHeap = nums\n // \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n for i in stride(from: parent(i: size() - 1), through: 0, by: -1) {\n siftDown(i: i)\n }\n}\n
my_heap.js/* \u6784\u9020\u65b9\u6cd5\uff0c\u5efa\u7acb\u7a7a\u5806\u6216\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806 */\nconstructor(nums) {\n // \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\n this.#maxHeap = nums === undefined ? [] : [...nums];\n // \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n for (let i = this.#parent(this.size() - 1); i >= 0; i--) {\n this.#siftDown(i);\n }\n}\n
my_heap.ts/* \u6784\u9020\u65b9\u6cd5\uff0c\u5efa\u7acb\u7a7a\u5806\u6216\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806 */\nconstructor(nums?: number[]) {\n // \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\n this.maxHeap = nums === undefined ? [] : [...nums];\n // \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n for (let i = this.parent(this.size() - 1); i >= 0; i--) {\n this.siftDown(i);\n }\n}\n
my_heap.dart/* \u6784\u9020\u65b9\u6cd5\uff0c\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806 */\nMaxHeap(List<int> nums) {\n // \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\n _maxHeap = nums;\n // \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n for (int i = _parent(size() - 1); i >= 0; i--) {\n siftDown(i);\n }\n}\n
my_heap.rs/* \u6784\u9020\u65b9\u6cd5\uff0c\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806 */\nfn new(nums: Vec<i32>) -> Self {\n // \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\n let mut heap = MaxHeap { max_heap: nums };\n // \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n for i in (0..=Self::parent(heap.size() - 1)).rev() {\n heap.sift_down(i);\n }\n heap\n}\n
my_heap.c/* \u6784\u9020\u51fd\u6570\uff0c\u6839\u636e\u5207\u7247\u5efa\u5806 */\nMaxHeap *newMaxHeap(int nums[], int size) {\n // \u6240\u6709\u5143\u7d20\u5165\u5806\n MaxHeap *maxHeap = (MaxHeap *)malloc(sizeof(MaxHeap));\n maxHeap->size = size;\n memcpy(maxHeap->data, nums, size * sizeof(int));\n for (int i = parent(maxHeap, size - 1); i >= 0; i--) {\n // \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n siftDown(maxHeap, i);\n }\n return maxHeap;\n}\n
my_heap.zig// \u6784\u9020\u65b9\u6cd5\uff0c\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806\nfn init(self: *Self, allocator: std.mem.Allocator, nums: []const T) !void {\n if (self.max_heap != null) return;\n self.max_heap = std.ArrayList(T).init(allocator);\n // \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\n try self.max_heap.?.appendSlice(nums);\n // \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n var i: usize = parent(self.size() - 1) + 1;\n while (i > 0) : (i -= 1) {\n try self.siftDown(i - 1);\n }\n}\n
"},{"location":"chapter_heap/build_heap/#823","title":"8.2.3 \u00a0 \u590d\u6742\u5ea6\u5206\u6790","text":"\u4e0b\u9762\uff0c\u6211\u4eec\u6765\u5c1d\u8bd5\u63a8\u7b97\u7b2c\u4e8c\u79cd\u5efa\u5806\u65b9\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u3002
\u5c06\u4e0a\u8ff0\u4e24\u8005\u76f8\u4e58\uff0c\u53ef\u5f97\u5230\u5efa\u5806\u8fc7\u7a0b\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n \\log n)\\) \u3002\u4f46\u8fd9\u4e2a\u4f30\u7b97\u7ed3\u679c\u5e76\u4e0d\u51c6\u786e\uff0c\u56e0\u4e3a\u6211\u4eec\u6ca1\u6709\u8003\u8651\u5230\u4e8c\u53c9\u6811\u5e95\u5c42\u8282\u70b9\u6570\u91cf\u8fdc\u591a\u4e8e\u9876\u5c42\u8282\u70b9\u7684\u6027\u8d28\u3002
\u63a5\u4e0b\u6765\u6211\u4eec\u6765\u8fdb\u884c\u66f4\u4e3a\u51c6\u786e\u7684\u8ba1\u7b97\u3002\u4e3a\u4e86\u964d\u4f4e\u8ba1\u7b97\u96be\u5ea6\uff0c\u5047\u8bbe\u7ed9\u5b9a\u4e00\u4e2a\u8282\u70b9\u6570\u91cf\u4e3a \\(n\\) \u3001\u9ad8\u5ea6\u4e3a \\(h\\) \u7684\u201c\u5b8c\u7f8e\u4e8c\u53c9\u6811\u201d\uff0c\u8be5\u5047\u8bbe\u4e0d\u4f1a\u5f71\u54cd\u8ba1\u7b97\u7ed3\u679c\u7684\u6b63\u786e\u6027\u3002
\u56fe 8-5 \u00a0 \u5b8c\u7f8e\u4e8c\u53c9\u6811\u7684\u5404\u5c42\u8282\u70b9\u6570\u91cf
\u5982\u56fe 8-5 \u6240\u793a\uff0c\u8282\u70b9\u201c\u4ece\u9876\u81f3\u5e95\u5806\u5316\u201d\u7684\u6700\u5927\u8fed\u4ee3\u6b21\u6570\u7b49\u4e8e\u8be5\u8282\u70b9\u5230\u53f6\u8282\u70b9\u7684\u8ddd\u79bb\uff0c\u800c\u8be5\u8ddd\u79bb\u6b63\u662f\u201c\u8282\u70b9\u9ad8\u5ea6\u201d\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u5bf9\u5404\u5c42\u7684\u201c\u8282\u70b9\u6570\u91cf \\(\\times\\) \u8282\u70b9\u9ad8\u5ea6\u201d\u6c42\u548c\uff0c\u5f97\u5230\u6240\u6709\u8282\u70b9\u7684\u5806\u5316\u8fed\u4ee3\u6b21\u6570\u7684\u603b\u548c\u3002
\\[ T(h) = 2^0h + 2^1(h-1) + 2^2(h-2) + \\dots + 2^{(h-1)}\\times1 \\]\u5316\u7b80\u4e0a\u5f0f\u9700\u8981\u501f\u52a9\u4e2d\u5b66\u7684\u6570\u5217\u77e5\u8bc6\uff0c\u5148\u5bf9 \\(T(h)\\) \u4e58\u4ee5 \\(2\\) \uff0c\u5f97\u5230\uff1a
\\[ \\begin{aligned} T(h) & = 2^0h + 2^1(h-1) + 2^2(h-2) + \\dots + 2^{h-1}\\times1 \\newline 2 T(h) & = 2^1h + 2^2(h-1) + 2^3(h-2) + \\dots + 2^{h}\\times1 \\newline \\end{aligned} \\]\u4f7f\u7528\u9519\u4f4d\u76f8\u51cf\u6cd5\uff0c\u7528\u4e0b\u5f0f \\(2 T(h)\\) \u51cf\u53bb\u4e0a\u5f0f \\(T(h)\\) \uff0c\u53ef\u5f97\uff1a
\\[ 2T(h) - T(h) = T(h) = -2^0h + 2^1 + 2^2 + \\dots + 2^{h-1} + 2^h \\]\u89c2\u5bdf\u4e0a\u5f0f\uff0c\u53d1\u73b0 \\(T(h)\\) \u662f\u4e00\u4e2a\u7b49\u6bd4\u6570\u5217\uff0c\u53ef\u76f4\u63a5\u4f7f\u7528\u6c42\u548c\u516c\u5f0f\uff0c\u5f97\u5230\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a\uff1a
\\[ \\begin{aligned} T(h) & = 2 \\frac{1 - 2^h}{1 - 2} - h \\newline & = 2^{h+1} - h - 2 \\newline & = O(2^h) \\end{aligned} \\]\u8fdb\u4e00\u6b65\u5730\uff0c\u9ad8\u5ea6\u4e3a \\(h\\) \u7684\u5b8c\u7f8e\u4e8c\u53c9\u6811\u7684\u8282\u70b9\u6570\u91cf\u4e3a \\(n = 2^{h+1} - 1\\) \uff0c\u6613\u5f97\u590d\u6742\u5ea6\u4e3a \\(O(2^h) = O(n)\\) \u3002\u4ee5\u4e0a\u63a8\u7b97\u8868\u660e\uff0c\u8f93\u5165\u5217\u8868\u5e76\u5efa\u5806\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \uff0c\u975e\u5e38\u9ad8\u6548\u3002
"},{"location":"chapter_heap/heap/","title":"8.1 \u00a0 \u5806","text":"\u300c\u5806 heap\u300d\u662f\u4e00\u79cd\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u7684\u5b8c\u5168\u4e8c\u53c9\u6811\uff0c\u4e3b\u8981\u53ef\u5206\u4e3a\u4e24\u79cd\u7c7b\u578b\uff0c\u5982\u56fe 8-1 \u6240\u793a\u3002
\u56fe 8-1 \u00a0 \u5c0f\u9876\u5806\u4e0e\u5927\u9876\u5806
\u5806\u4f5c\u4e3a\u5b8c\u5168\u4e8c\u53c9\u6811\u7684\u4e00\u4e2a\u7279\u4f8b\uff0c\u5177\u6709\u4ee5\u4e0b\u7279\u6027\u3002
\u9700\u8981\u6307\u51fa\u7684\u662f\uff0c\u8bb8\u591a\u7f16\u7a0b\u8bed\u8a00\u63d0\u4f9b\u7684\u662f\u300c\u4f18\u5148\u961f\u5217 priority queue\u300d\uff0c\u8fd9\u662f\u4e00\u79cd\u62bd\u8c61\u6570\u636e\u7ed3\u6784\uff0c\u5b9a\u4e49\u4e3a\u5177\u6709\u4f18\u5148\u7ea7\u6392\u5e8f\u7684\u961f\u5217\u3002
\u5b9e\u9645\u4e0a\uff0c\u5806\u901a\u5e38\u7528\u4e8e\u5b9e\u73b0\u4f18\u5148\u961f\u5217\uff0c\u5927\u9876\u5806\u76f8\u5f53\u4e8e\u5143\u7d20\u6309\u4ece\u5927\u5230\u5c0f\u7684\u987a\u5e8f\u51fa\u961f\u7684\u4f18\u5148\u961f\u5217\u3002\u4ece\u4f7f\u7528\u89d2\u5ea6\u6765\u770b\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u201c\u4f18\u5148\u961f\u5217\u201d\u548c\u201c\u5806\u201d\u770b\u4f5c\u7b49\u4ef7\u7684\u6570\u636e\u7ed3\u6784\u3002\u56e0\u6b64\uff0c\u672c\u4e66\u5bf9\u4e24\u8005\u4e0d\u505a\u7279\u522b\u533a\u5206\uff0c\u7edf\u4e00\u79f0\u4f5c\u201c\u5806\u201d\u3002
\u5806\u7684\u5e38\u7528\u64cd\u4f5c\u89c1\u8868 8-1 \uff0c\u65b9\u6cd5\u540d\u9700\u8981\u6839\u636e\u7f16\u7a0b\u8bed\u8a00\u6765\u786e\u5b9a\u3002
\u8868 8-1 \u00a0 \u5806\u7684\u64cd\u4f5c\u6548\u7387
\u65b9\u6cd5\u540d \u63cf\u8ff0 \u65f6\u95f4\u590d\u6742\u5ea6 push() \u5143\u7d20\u5165\u5806 \\(O(\\log n)\\) pop() \u5806\u9876\u5143\u7d20\u51fa\u5806 \\(O(\\log n)\\) peek() \u8bbf\u95ee\u5806\u9876\u5143\u7d20\uff08\u5927 / \u5c0f\u9876\u5806\u5206\u522b\u4e3a\u6700\u5927 / \u5c0f\u503c\uff09 \\(O(1)\\) size() \u83b7\u53d6\u5806\u7684\u5143\u7d20\u6570\u91cf \\(O(1)\\) isEmpty() \u5224\u65ad\u5806\u662f\u5426\u4e3a\u7a7a \\(O(1)\\)\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u7f16\u7a0b\u8bed\u8a00\u63d0\u4f9b\u7684\u5806\u7c7b\uff08\u6216\u4f18\u5148\u961f\u5217\u7c7b\uff09\u3002
\u7c7b\u4f3c\u4e8e\u6392\u5e8f\u7b97\u6cd5\u4e2d\u7684\u201c\u4ece\u5c0f\u5230\u5927\u6392\u5217\u201d\u548c\u201c\u4ece\u5927\u5230\u5c0f\u6392\u5217\u201d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u4e00\u4e2a flag
\u6216\u4fee\u6539 Comparator
\u5b9e\u73b0\u201c\u5c0f\u9876\u5806\u201d\u4e0e\u201c\u5927\u9876\u5806\u201d\u4e4b\u95f4\u7684\u8f6c\u6362\u3002\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a
# \u521d\u59cb\u5316\u5c0f\u9876\u5806\nmin_heap, flag = [], 1\n# \u521d\u59cb\u5316\u5927\u9876\u5806\nmax_heap, flag = [], -1\n\n# Python \u7684 heapq \u6a21\u5757\u9ed8\u8ba4\u5b9e\u73b0\u5c0f\u9876\u5806\n# \u8003\u8651\u5c06\u201c\u5143\u7d20\u53d6\u8d1f\u201d\u540e\u518d\u5165\u5806\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u5c06\u5927\u5c0f\u5173\u7cfb\u98a0\u5012\uff0c\u4ece\u800c\u5b9e\u73b0\u5927\u9876\u5806\n# \u5728\u672c\u793a\u4f8b\u4e2d\uff0cflag = 1 \u65f6\u5bf9\u5e94\u5c0f\u9876\u5806\uff0cflag = -1 \u65f6\u5bf9\u5e94\u5927\u9876\u5806\n\n# \u5143\u7d20\u5165\u5806\nheapq.heappush(max_heap, flag * 1)\nheapq.heappush(max_heap, flag * 3)\nheapq.heappush(max_heap, flag * 2)\nheapq.heappush(max_heap, flag * 5)\nheapq.heappush(max_heap, flag * 4)\n\n# \u83b7\u53d6\u5806\u9876\u5143\u7d20\npeek: int = flag * max_heap[0] # 5\n\n# \u5806\u9876\u5143\u7d20\u51fa\u5806\n# \u51fa\u5806\u5143\u7d20\u4f1a\u5f62\u6210\u4e00\u4e2a\u4ece\u5927\u5230\u5c0f\u7684\u5e8f\u5217\nval = flag * heapq.heappop(max_heap) # 5\nval = flag * heapq.heappop(max_heap) # 4\nval = flag * heapq.heappop(max_heap) # 3\nval = flag * heapq.heappop(max_heap) # 2\nval = flag * heapq.heappop(max_heap) # 1\n\n# \u83b7\u53d6\u5806\u5927\u5c0f\nsize: int = len(max_heap)\n\n# \u5224\u65ad\u5806\u662f\u5426\u4e3a\u7a7a\nis_empty: bool = not max_heap\n\n# \u8f93\u5165\u5217\u8868\u5e76\u5efa\u5806\nmin_heap: list[int] = [1, 3, 2, 5, 4]\nheapq.heapify(min_heap)\n
heap.cpp/* \u521d\u59cb\u5316\u5806 */\n// \u521d\u59cb\u5316\u5c0f\u9876\u5806\npriority_queue<int, vector<int>, greater<int>> minHeap;\n// \u521d\u59cb\u5316\u5927\u9876\u5806\npriority_queue<int, vector<int>, less<int>> maxHeap;\n\n/* \u5143\u7d20\u5165\u5806 */\nmaxHeap.push(1);\nmaxHeap.push(3);\nmaxHeap.push(2);\nmaxHeap.push(5);\nmaxHeap.push(4);\n\n/* \u83b7\u53d6\u5806\u9876\u5143\u7d20 */\nint peek = maxHeap.top(); // 5\n\n/* \u5806\u9876\u5143\u7d20\u51fa\u5806 */\n// \u51fa\u5806\u5143\u7d20\u4f1a\u5f62\u6210\u4e00\u4e2a\u4ece\u5927\u5230\u5c0f\u7684\u5e8f\u5217\nmaxHeap.pop(); // 5\nmaxHeap.pop(); // 4\nmaxHeap.pop(); // 3\nmaxHeap.pop(); // 2\nmaxHeap.pop(); // 1\n\n/* \u83b7\u53d6\u5806\u5927\u5c0f */\nint size = maxHeap.size();\n\n/* \u5224\u65ad\u5806\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty = maxHeap.empty();\n\n/* \u8f93\u5165\u5217\u8868\u5e76\u5efa\u5806 */\nvector<int> input{1, 3, 2, 5, 4};\npriority_queue<int, vector<int>, greater<int>> minHeap(input.begin(), input.end());\n
heap.java/* \u521d\u59cb\u5316\u5806 */\n// \u521d\u59cb\u5316\u5c0f\u9876\u5806\nQueue<Integer> minHeap = new PriorityQueue<>();\n// \u521d\u59cb\u5316\u5927\u9876\u5806\uff08\u4f7f\u7528 lambda \u8868\u8fbe\u5f0f\u4fee\u6539 Comparator \u5373\u53ef\uff09\nQueue<Integer> maxHeap = new PriorityQueue<>((a, b) -> b - a);\n\n/* \u5143\u7d20\u5165\u5806 */\nmaxHeap.offer(1);\nmaxHeap.offer(3);\nmaxHeap.offer(2);\nmaxHeap.offer(5);\nmaxHeap.offer(4);\n\n/* \u83b7\u53d6\u5806\u9876\u5143\u7d20 */\nint peek = maxHeap.peek(); // 5\n\n/* \u5806\u9876\u5143\u7d20\u51fa\u5806 */\n// \u51fa\u5806\u5143\u7d20\u4f1a\u5f62\u6210\u4e00\u4e2a\u4ece\u5927\u5230\u5c0f\u7684\u5e8f\u5217\npeek = maxHeap.poll(); // 5\npeek = maxHeap.poll(); // 4\npeek = maxHeap.poll(); // 3\npeek = maxHeap.poll(); // 2\npeek = maxHeap.poll(); // 1\n\n/* \u83b7\u53d6\u5806\u5927\u5c0f */\nint size = maxHeap.size();\n\n/* \u5224\u65ad\u5806\u662f\u5426\u4e3a\u7a7a */\nboolean isEmpty = maxHeap.isEmpty();\n\n/* \u8f93\u5165\u5217\u8868\u5e76\u5efa\u5806 */\nminHeap = new PriorityQueue<>(Arrays.asList(1, 3, 2, 5, 4));\n
heap.cs/* \u521d\u59cb\u5316\u5806 */\n// \u521d\u59cb\u5316\u5c0f\u9876\u5806\nPriorityQueue<int, int> minHeap = new();\n// \u521d\u59cb\u5316\u5927\u9876\u5806\uff08\u4f7f\u7528 lambda \u8868\u8fbe\u5f0f\u4fee\u6539 Comparator \u5373\u53ef\uff09\nPriorityQueue<int, int> maxHeap = new(Comparer<int>.Create((x, y) => y - x));\n\n/* \u5143\u7d20\u5165\u5806 */\nmaxHeap.Enqueue(1, 1);\nmaxHeap.Enqueue(3, 3);\nmaxHeap.Enqueue(2, 2);\nmaxHeap.Enqueue(5, 5);\nmaxHeap.Enqueue(4, 4);\n\n/* \u83b7\u53d6\u5806\u9876\u5143\u7d20 */\nint peek = maxHeap.Peek();//5\n\n/* \u5806\u9876\u5143\u7d20\u51fa\u5806 */\n// \u51fa\u5806\u5143\u7d20\u4f1a\u5f62\u6210\u4e00\u4e2a\u4ece\u5927\u5230\u5c0f\u7684\u5e8f\u5217\npeek = maxHeap.Dequeue(); // 5\npeek = maxHeap.Dequeue(); // 4\npeek = maxHeap.Dequeue(); // 3\npeek = maxHeap.Dequeue(); // 2\npeek = maxHeap.Dequeue(); // 1\n\n/* \u83b7\u53d6\u5806\u5927\u5c0f */\nint size = maxHeap.Count;\n\n/* \u5224\u65ad\u5806\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty = maxHeap.Count == 0;\n\n/* \u8f93\u5165\u5217\u8868\u5e76\u5efa\u5806 */\nminHeap = new PriorityQueue<int, int>([(1, 1), (3, 3), (2, 2), (5, 5), (4, 4)]);\n
heap.go// Go \u8bed\u8a00\u4e2d\u53ef\u4ee5\u901a\u8fc7\u5b9e\u73b0 heap.Interface \u6765\u6784\u5efa\u6574\u6570\u5927\u9876\u5806\n// \u5b9e\u73b0 heap.Interface \u9700\u8981\u540c\u65f6\u5b9e\u73b0 sort.Interface\ntype intHeap []any\n\n// Push heap.Interface \u7684\u65b9\u6cd5\uff0c\u5b9e\u73b0\u63a8\u5165\u5143\u7d20\u5230\u5806\nfunc (h *intHeap) Push(x any) {\n // Push \u548c Pop \u4f7f\u7528 pointer receiver \u4f5c\u4e3a\u53c2\u6570\n // \u56e0\u4e3a\u5b83\u4eec\u4e0d\u4ec5\u4f1a\u5bf9\u5207\u7247\u7684\u5185\u5bb9\u8fdb\u884c\u8c03\u6574\uff0c\u8fd8\u4f1a\u4fee\u6539\u5207\u7247\u7684\u957f\u5ea6\u3002\n *h = append(*h, x.(int))\n}\n\n// Pop heap.Interface \u7684\u65b9\u6cd5\uff0c\u5b9e\u73b0\u5f39\u51fa\u5806\u9876\u5143\u7d20\nfunc (h *intHeap) Pop() any {\n // \u5f85\u51fa\u5806\u5143\u7d20\u5b58\u653e\u5728\u6700\u540e\n last := (*h)[len(*h)-1]\n *h = (*h)[:len(*h)-1]\n return last\n}\n\n// Len sort.Interface \u7684\u65b9\u6cd5\nfunc (h *intHeap) Len() int {\n return len(*h)\n}\n\n// Less sort.Interface \u7684\u65b9\u6cd5\nfunc (h *intHeap) Less(i, j int) bool {\n // \u5982\u679c\u5b9e\u73b0\u5c0f\u9876\u5806\uff0c\u5219\u9700\u8981\u8c03\u6574\u4e3a\u5c0f\u4e8e\u53f7\n return (*h)[i].(int) > (*h)[j].(int)\n}\n\n// Swap sort.Interface \u7684\u65b9\u6cd5\nfunc (h *intHeap) Swap(i, j int) {\n (*h)[i], (*h)[j] = (*h)[j], (*h)[i]\n}\n\n// Top \u83b7\u53d6\u5806\u9876\u5143\u7d20\nfunc (h *intHeap) Top() any {\n return (*h)[0]\n}\n\n/* Driver Code */\nfunc TestHeap(t *testing.T) {\n /* \u521d\u59cb\u5316\u5806 */\n // \u521d\u59cb\u5316\u5927\u9876\u5806\n maxHeap := &intHeap{}\n heap.Init(maxHeap)\n /* \u5143\u7d20\u5165\u5806 */\n // \u8c03\u7528 heap.Interface \u7684\u65b9\u6cd5\uff0c\u6765\u6dfb\u52a0\u5143\u7d20\n heap.Push(maxHeap, 1)\n heap.Push(maxHeap, 3)\n heap.Push(maxHeap, 2)\n heap.Push(maxHeap, 4)\n heap.Push(maxHeap, 5)\n\n /* \u83b7\u53d6\u5806\u9876\u5143\u7d20 */\n top := maxHeap.Top()\n fmt.Printf(\"\u5806\u9876\u5143\u7d20\u4e3a %d\\n\", top)\n\n /* \u5806\u9876\u5143\u7d20\u51fa\u5806 */\n // \u8c03\u7528 heap.Interface \u7684\u65b9\u6cd5\uff0c\u6765\u79fb\u9664\u5143\u7d20\n heap.Pop(maxHeap) // 5\n heap.Pop(maxHeap) // 4\n heap.Pop(maxHeap) // 3\n heap.Pop(maxHeap) // 2\n heap.Pop(maxHeap) // 1\n\n /* \u83b7\u53d6\u5806\u5927\u5c0f */\n size := len(*maxHeap)\n fmt.Printf(\"\u5806\u5143\u7d20\u6570\u91cf\u4e3a %d\\n\", size)\n\n /* \u5224\u65ad\u5806\u662f\u5426\u4e3a\u7a7a */\n isEmpty := len(*maxHeap) == 0\n fmt.Printf(\"\u5806\u662f\u5426\u4e3a\u7a7a %t\\n\", isEmpty)\n}\n
heap.swift// Swift \u672a\u63d0\u4f9b\u5185\u7f6e Heap \u7c7b\n
heap.js// JavaScript \u672a\u63d0\u4f9b\u5185\u7f6e Heap \u7c7b\n
heap.ts// TypeScript \u672a\u63d0\u4f9b\u5185\u7f6e Heap \u7c7b\n
heap.dart// Dart \u672a\u63d0\u4f9b\u5185\u7f6e Heap \u7c7b\n
heap.rsuse std::collections::BinaryHeap;\nuse std::cmp::Reverse;\n\n/* \u521d\u59cb\u5316\u5806 */\n// \u521d\u59cb\u5316\u5c0f\u9876\u5806\nlet mut min_heap = BinaryHeap::<Reverse<i32>>::new();\n// \u521d\u59cb\u5316\u5927\u9876\u5806\nlet mut max_heap = BinaryHeap::new();\n\n/* \u5143\u7d20\u5165\u5806 */\nmax_heap.push(1);\nmax_heap.push(3);\nmax_heap.push(2);\nmax_heap.push(5);\nmax_heap.push(4);\n\n/* \u83b7\u53d6\u5806\u9876\u5143\u7d20 */\nlet peek = max_heap.peek().unwrap(); // 5\n\n/* \u5806\u9876\u5143\u7d20\u51fa\u5806 */\n// \u51fa\u5806\u5143\u7d20\u4f1a\u5f62\u6210\u4e00\u4e2a\u4ece\u5927\u5230\u5c0f\u7684\u5e8f\u5217\nlet peek = max_heap.pop().unwrap(); // 5\nlet peek = max_heap.pop().unwrap(); // 4\nlet peek = max_heap.pop().unwrap(); // 3\nlet peek = max_heap.pop().unwrap(); // 2\nlet peek = max_heap.pop().unwrap(); // 1\n\n/* \u83b7\u53d6\u5806\u5927\u5c0f */\nlet size = max_heap.len();\n\n/* \u5224\u65ad\u5806\u662f\u5426\u4e3a\u7a7a */\nlet is_empty = max_heap.is_empty();\n\n/* \u8f93\u5165\u5217\u8868\u5e76\u5efa\u5806 */\nlet min_heap = BinaryHeap::from(vec![Reverse(1), Reverse(3), Reverse(2), Reverse(5), Reverse(4)]);\n
heap.c// C \u672a\u63d0\u4f9b\u5185\u7f6e Heap \u7c7b\n
heap.zig\n
"},{"location":"chapter_heap/heap/#812","title":"8.1.2 \u00a0 \u5806\u7684\u5b9e\u73b0","text":"\u4e0b\u6587\u5b9e\u73b0\u7684\u662f\u5927\u9876\u5806\u3002\u82e5\u8981\u5c06\u5176\u8f6c\u6362\u4e3a\u5c0f\u9876\u5806\uff0c\u53ea\u9700\u5c06\u6240\u6709\u5927\u5c0f\u903b\u8f91\u5224\u65ad\u53d6\u9006\uff08\u4f8b\u5982\uff0c\u5c06 \\(\\geq\\) \u66ff\u6362\u4e3a \\(\\leq\\) \uff09\u3002\u611f\u5174\u8da3\u7684\u8bfb\u8005\u53ef\u4ee5\u81ea\u884c\u5b9e\u73b0\u3002
"},{"location":"chapter_heap/heap/#1","title":"1. \u00a0 \u5806\u7684\u5b58\u50a8\u4e0e\u8868\u793a","text":"\u201c\u4e8c\u53c9\u6811\u201d\u7ae0\u8282\u8bb2\u8fc7\uff0c\u5b8c\u5168\u4e8c\u53c9\u6811\u975e\u5e38\u9002\u5408\u7528\u6570\u7ec4\u6765\u8868\u793a\u3002\u7531\u4e8e\u5806\u6b63\u662f\u4e00\u79cd\u5b8c\u5168\u4e8c\u53c9\u6811\uff0c\u56e0\u6b64\u6211\u4eec\u5c06\u91c7\u7528\u6570\u7ec4\u6765\u5b58\u50a8\u5806\u3002
\u5f53\u4f7f\u7528\u6570\u7ec4\u8868\u793a\u4e8c\u53c9\u6811\u65f6\uff0c\u5143\u7d20\u4ee3\u8868\u8282\u70b9\u503c\uff0c\u7d22\u5f15\u4ee3\u8868\u8282\u70b9\u5728\u4e8c\u53c9\u6811\u4e2d\u7684\u4f4d\u7f6e\u3002\u8282\u70b9\u6307\u9488\u901a\u8fc7\u7d22\u5f15\u6620\u5c04\u516c\u5f0f\u6765\u5b9e\u73b0\u3002
\u5982\u56fe 8-2 \u6240\u793a\uff0c\u7ed9\u5b9a\u7d22\u5f15 \\(i\\) \uff0c\u5176\u5de6\u5b50\u8282\u70b9\u7d22\u5f15\u4e3a \\(2i + 1\\) \uff0c\u53f3\u5b50\u8282\u70b9\u7d22\u5f15\u4e3a \\(2i + 2\\) \uff0c\u7236\u8282\u70b9\u7d22\u5f15\u4e3a \\((i - 1) / 2\\)\uff08\u5411\u4e0b\u6574\u9664\uff09\u3002\u5f53\u7d22\u5f15\u8d8a\u754c\u65f6\uff0c\u8868\u793a\u7a7a\u8282\u70b9\u6216\u8282\u70b9\u4e0d\u5b58\u5728\u3002
\u56fe 8-2 \u00a0 \u5806\u7684\u8868\u793a\u4e0e\u5b58\u50a8
\u6211\u4eec\u53ef\u4ee5\u5c06\u7d22\u5f15\u6620\u5c04\u516c\u5f0f\u5c01\u88c5\u6210\u51fd\u6570\uff0c\u65b9\u4fbf\u540e\u7eed\u4f7f\u7528\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig my_heap.pydef left(self, i: int) -> int:\n \"\"\"\u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7d22\u5f15\"\"\"\n return 2 * i + 1\n\ndef right(self, i: int) -> int:\n \"\"\"\u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7d22\u5f15\"\"\"\n return 2 * i + 2\n\ndef parent(self, i: int) -> int:\n \"\"\"\u83b7\u53d6\u7236\u8282\u70b9\u7d22\u5f15\"\"\"\n return (i - 1) // 2 # \u5411\u4e0b\u6574\u9664\n
my_heap.cpp/* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7d22\u5f15 */\nint left(int i) {\n return 2 * i + 1;\n}\n\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7d22\u5f15 */\nint right(int i) {\n return 2 * i + 2;\n}\n\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7d22\u5f15 */\nint parent(int i) {\n return (i - 1) / 2; // \u5411\u4e0b\u6574\u9664\n}\n
my_heap.java/* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7d22\u5f15 */\nint left(int i) {\n return 2 * i + 1;\n}\n\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7d22\u5f15 */\nint right(int i) {\n return 2 * i + 2;\n}\n\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7d22\u5f15 */\nint parent(int i) {\n return (i - 1) / 2; // \u5411\u4e0b\u6574\u9664\n}\n
my_heap.cs/* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7d22\u5f15 */\nint Left(int i) {\n return 2 * i + 1;\n}\n\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7d22\u5f15 */\nint Right(int i) {\n return 2 * i + 2;\n}\n\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7d22\u5f15 */\nint Parent(int i) {\n return (i - 1) / 2; // \u5411\u4e0b\u6574\u9664\n}\n
my_heap.go/* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7d22\u5f15 */\nfunc (h *maxHeap) left(i int) int {\n return 2*i + 1\n}\n\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7d22\u5f15 */\nfunc (h *maxHeap) right(i int) int {\n return 2*i + 2\n}\n\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7d22\u5f15 */\nfunc (h *maxHeap) parent(i int) int {\n // \u5411\u4e0b\u6574\u9664\n return (i - 1) / 2\n}\n
my_heap.swift/* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7d22\u5f15 */\nfunc left(i: Int) -> Int {\n 2 * i + 1\n}\n\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7d22\u5f15 */\nfunc right(i: Int) -> Int {\n 2 * i + 2\n}\n\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7d22\u5f15 */\nfunc parent(i: Int) -> Int {\n (i - 1) / 2 // \u5411\u4e0b\u6574\u9664\n}\n
my_heap.js/* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7d22\u5f15 */\n#left(i) {\n return 2 * i + 1;\n}\n\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7d22\u5f15 */\n#right(i) {\n return 2 * i + 2;\n}\n\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7d22\u5f15 */\n#parent(i) {\n return Math.floor((i - 1) / 2); // \u5411\u4e0b\u6574\u9664\n}\n
my_heap.ts/* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7d22\u5f15 */\nleft(i: number): number {\n return 2 * i + 1;\n}\n\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7d22\u5f15 */\nright(i: number): number {\n return 2 * i + 2;\n}\n\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7d22\u5f15 */\nparent(i: number): number {\n return Math.floor((i - 1) / 2); // \u5411\u4e0b\u6574\u9664\n}\n
my_heap.dart/* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7d22\u5f15 */\nint _left(int i) {\n return 2 * i + 1;\n}\n\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7d22\u5f15 */\nint _right(int i) {\n return 2 * i + 2;\n}\n\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7d22\u5f15 */\nint _parent(int i) {\n return (i - 1) ~/ 2; // \u5411\u4e0b\u6574\u9664\n}\n
my_heap.rs/* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7d22\u5f15 */\nfn left(i: usize) -> usize {\n 2 * i + 1\n}\n\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7d22\u5f15 */\nfn right(i: usize) -> usize {\n 2 * i + 2\n}\n\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7d22\u5f15 */\nfn parent(i: usize) -> usize {\n (i - 1) / 2 // \u5411\u4e0b\u6574\u9664\n}\n
my_heap.c/* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7d22\u5f15 */\nint left(MaxHeap *maxHeap, int i) {\n return 2 * i + 1;\n}\n\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7d22\u5f15 */\nint right(MaxHeap *maxHeap, int i) {\n return 2 * i + 2;\n}\n\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7d22\u5f15 */\nint parent(MaxHeap *maxHeap, int i) {\n return (i - 1) / 2;\n}\n
my_heap.zig// \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7d22\u5f15\nfn left(i: usize) usize {\n return 2 * i + 1;\n}\n\n// \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7d22\u5f15\nfn right(i: usize) usize {\n return 2 * i + 2;\n}\n\n// \u83b7\u53d6\u7236\u8282\u70b9\u7d22\u5f15\nfn parent(i: usize) usize {\n // return (i - 1) / 2; // \u5411\u4e0b\u6574\u9664\n return @divFloor(i - 1, 2);\n}\n
"},{"location":"chapter_heap/heap/#2","title":"2. \u00a0 \u8bbf\u95ee\u5806\u9876\u5143\u7d20","text":"\u5806\u9876\u5143\u7d20\u5373\u4e3a\u4e8c\u53c9\u6811\u7684\u6839\u8282\u70b9\uff0c\u4e5f\u5c31\u662f\u5217\u8868\u7684\u9996\u4e2a\u5143\u7d20\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig my_heap.pydef peek(self) -> int:\n \"\"\"\u8bbf\u95ee\u5806\u9876\u5143\u7d20\"\"\"\n return self.max_heap[0]\n
my_heap.cpp/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nint peek() {\n return maxHeap[0];\n}\n
my_heap.java/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nint peek() {\n return maxHeap.get(0);\n}\n
my_heap.cs/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nint Peek() {\n return maxHeap[0];\n}\n
my_heap.go/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nfunc (h *maxHeap) peek() any {\n return h.data[0]\n}\n
my_heap.swift/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nfunc peek() -> Int {\n maxHeap[0]\n}\n
my_heap.js/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\npeek() {\n return this.#maxHeap[0];\n}\n
my_heap.ts/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\npeek(): number {\n return this.maxHeap[0];\n}\n
my_heap.dart/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nint peek() {\n return _maxHeap[0];\n}\n
my_heap.rs/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nfn peek(&self) -> Option<i32> {\n self.max_heap.first().copied()\n}\n
my_heap.c/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nint peek(MaxHeap *maxHeap) {\n return maxHeap->data[0];\n}\n
my_heap.zig// \u8bbf\u95ee\u5806\u9876\u5143\u7d20\nfn peek(self: *Self) T {\n return self.max_heap.?.items[0];\n} \n
"},{"location":"chapter_heap/heap/#3","title":"3. \u00a0 \u5143\u7d20\u5165\u5806","text":"\u7ed9\u5b9a\u5143\u7d20 val
\uff0c\u6211\u4eec\u9996\u5148\u5c06\u5176\u6dfb\u52a0\u5230\u5806\u5e95\u3002\u6dfb\u52a0\u4e4b\u540e\uff0c\u7531\u4e8e val \u53ef\u80fd\u5927\u4e8e\u5806\u4e2d\u5176\u4ed6\u5143\u7d20\uff0c\u5806\u7684\u6210\u7acb\u6761\u4ef6\u53ef\u80fd\u5df2\u88ab\u7834\u574f\uff0c\u56e0\u6b64\u9700\u8981\u4fee\u590d\u4ece\u63d2\u5165\u8282\u70b9\u5230\u6839\u8282\u70b9\u7684\u8def\u5f84\u4e0a\u7684\u5404\u4e2a\u8282\u70b9\uff0c\u8fd9\u4e2a\u64cd\u4f5c\u88ab\u79f0\u4e3a\u300c\u5806\u5316 heapify\u300d\u3002
\u8003\u8651\u4ece\u5165\u5806\u8282\u70b9\u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u6267\u884c\u5806\u5316\u3002\u5982\u56fe 8-3 \u6240\u793a\uff0c\u6211\u4eec\u6bd4\u8f83\u63d2\u5165\u8282\u70b9\u4e0e\u5176\u7236\u8282\u70b9\u7684\u503c\uff0c\u5982\u679c\u63d2\u5165\u8282\u70b9\u66f4\u5927\uff0c\u5219\u5c06\u5b83\u4eec\u4ea4\u6362\u3002\u7136\u540e\u7ee7\u7eed\u6267\u884c\u6b64\u64cd\u4f5c\uff0c\u4ece\u5e95\u81f3\u9876\u4fee\u590d\u5806\u4e2d\u7684\u5404\u4e2a\u8282\u70b9\uff0c\u76f4\u81f3\u8d8a\u8fc7\u6839\u8282\u70b9\u6216\u9047\u5230\u65e0\u987b\u4ea4\u6362\u7684\u8282\u70b9\u65f6\u7ed3\u675f\u3002
<1><2><3><4><5><6><7><8><9>\u56fe 8-3 \u00a0 \u5143\u7d20\u5165\u5806\u6b65\u9aa4
\u8bbe\u8282\u70b9\u603b\u6570\u4e3a \\(n\\) \uff0c\u5219\u6811\u7684\u9ad8\u5ea6\u4e3a \\(O(\\log n)\\) \u3002\u7531\u6b64\u53ef\u77e5\uff0c\u5806\u5316\u64cd\u4f5c\u7684\u5faa\u73af\u8f6e\u6570\u6700\u591a\u4e3a \\(O(\\log n)\\) \uff0c\u5143\u7d20\u5165\u5806\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(\\log n)\\) \u3002\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig my_heap.pydef push(self, val: int):\n \"\"\"\u5143\u7d20\u5165\u5806\"\"\"\n # \u6dfb\u52a0\u8282\u70b9\n self.max_heap.append(val)\n # \u4ece\u5e95\u81f3\u9876\u5806\u5316\n self.sift_up(self.size() - 1)\n\ndef sift_up(self, i: int):\n \"\"\"\u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316\"\"\"\n while True:\n # \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\n p = self.parent(i)\n # \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u987b\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\n if p < 0 or self.max_heap[i] <= self.max_heap[p]:\n break\n # \u4ea4\u6362\u4e24\u8282\u70b9\n self.swap(i, p)\n # \u5faa\u73af\u5411\u4e0a\u5806\u5316\n i = p\n
my_heap.cpp/* \u5143\u7d20\u5165\u5806 */\nvoid push(int val) {\n // \u6dfb\u52a0\u8282\u70b9\n maxHeap.push_back(val);\n // \u4ece\u5e95\u81f3\u9876\u5806\u5316\n siftUp(size() - 1);\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nvoid siftUp(int i) {\n while (true) {\n // \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\n int p = parent(i);\n // \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u987b\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\n if (p < 0 || maxHeap[i] <= maxHeap[p])\n break;\n // \u4ea4\u6362\u4e24\u8282\u70b9\n swap(maxHeap[i], maxHeap[p]);\n // \u5faa\u73af\u5411\u4e0a\u5806\u5316\n i = p;\n }\n}\n
my_heap.java/* \u5143\u7d20\u5165\u5806 */\nvoid push(int val) {\n // \u6dfb\u52a0\u8282\u70b9\n maxHeap.add(val);\n // \u4ece\u5e95\u81f3\u9876\u5806\u5316\n siftUp(size() - 1);\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nvoid siftUp(int i) {\n while (true) {\n // \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\n int p = parent(i);\n // \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u987b\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\n if (p < 0 || maxHeap.get(i) <= maxHeap.get(p))\n break;\n // \u4ea4\u6362\u4e24\u8282\u70b9\n swap(i, p);\n // \u5faa\u73af\u5411\u4e0a\u5806\u5316\n i = p;\n }\n}\n
my_heap.cs/* \u5143\u7d20\u5165\u5806 */\nvoid Push(int val) {\n // \u6dfb\u52a0\u8282\u70b9\n maxHeap.Add(val);\n // \u4ece\u5e95\u81f3\u9876\u5806\u5316\n SiftUp(Size() - 1);\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nvoid SiftUp(int i) {\n while (true) {\n // \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\n int p = Parent(i);\n // \u82e5\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u987b\u4fee\u590d\u201d\uff0c\u5219\u7ed3\u675f\u5806\u5316\n if (p < 0 || maxHeap[i] <= maxHeap[p])\n break;\n // \u4ea4\u6362\u4e24\u8282\u70b9\n Swap(i, p);\n // \u5faa\u73af\u5411\u4e0a\u5806\u5316\n i = p;\n }\n}\n
my_heap.go/* \u5143\u7d20\u5165\u5806 */\nfunc (h *maxHeap) push(val any) {\n // \u6dfb\u52a0\u8282\u70b9\n h.data = append(h.data, val)\n // \u4ece\u5e95\u81f3\u9876\u5806\u5316\n h.siftUp(len(h.data) - 1)\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nfunc (h *maxHeap) siftUp(i int) {\n for true {\n // \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\n p := h.parent(i)\n // \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u987b\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\n if p < 0 || h.data[i].(int) <= h.data[p].(int) {\n break\n }\n // \u4ea4\u6362\u4e24\u8282\u70b9\n h.swap(i, p)\n // \u5faa\u73af\u5411\u4e0a\u5806\u5316\n i = p\n }\n}\n
my_heap.swift/* \u5143\u7d20\u5165\u5806 */\nfunc push(val: Int) {\n // \u6dfb\u52a0\u8282\u70b9\n maxHeap.append(val)\n // \u4ece\u5e95\u81f3\u9876\u5806\u5316\n siftUp(i: size() - 1)\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nfunc siftUp(i: Int) {\n var i = i\n while true {\n // \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\n let p = parent(i: i)\n // \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u987b\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\n if p < 0 || maxHeap[i] <= maxHeap[p] {\n break\n }\n // \u4ea4\u6362\u4e24\u8282\u70b9\n swap(i: i, j: p)\n // \u5faa\u73af\u5411\u4e0a\u5806\u5316\n i = p\n }\n}\n
my_heap.js/* \u5143\u7d20\u5165\u5806 */\npush(val) {\n // \u6dfb\u52a0\u8282\u70b9\n this.#maxHeap.push(val);\n // \u4ece\u5e95\u81f3\u9876\u5806\u5316\n this.#siftUp(this.size() - 1);\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\n#siftUp(i) {\n while (true) {\n // \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\n const p = this.#parent(i);\n // \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u987b\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\n if (p < 0 || this.#maxHeap[i] <= this.#maxHeap[p]) break;\n // \u4ea4\u6362\u4e24\u8282\u70b9\n this.#swap(i, p);\n // \u5faa\u73af\u5411\u4e0a\u5806\u5316\n i = p;\n }\n}\n
my_heap.ts/* \u5143\u7d20\u5165\u5806 */\npush(val: number): void {\n // \u6dfb\u52a0\u8282\u70b9\n this.maxHeap.push(val);\n // \u4ece\u5e95\u81f3\u9876\u5806\u5316\n this.siftUp(this.size() - 1);\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nsiftUp(i: number): void {\n while (true) {\n // \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\n const p = this.parent(i);\n // \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u987b\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\n if (p < 0 || this.maxHeap[i] <= this.maxHeap[p]) break;\n // \u4ea4\u6362\u4e24\u8282\u70b9\n this.swap(i, p);\n // \u5faa\u73af\u5411\u4e0a\u5806\u5316\n i = p;\n }\n}\n
my_heap.dart/* \u5143\u7d20\u5165\u5806 */\nvoid push(int val) {\n // \u6dfb\u52a0\u8282\u70b9\n _maxHeap.add(val);\n // \u4ece\u5e95\u81f3\u9876\u5806\u5316\n siftUp(size() - 1);\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nvoid siftUp(int i) {\n while (true) {\n // \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\n int p = _parent(i);\n // \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u987b\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\n if (p < 0 || _maxHeap[i] <= _maxHeap[p]) {\n break;\n }\n // \u4ea4\u6362\u4e24\u8282\u70b9\n _swap(i, p);\n // \u5faa\u73af\u5411\u4e0a\u5806\u5316\n i = p;\n }\n}\n
my_heap.rs/* \u5143\u7d20\u5165\u5806 */\nfn push(&mut self, val: i32) {\n // \u6dfb\u52a0\u8282\u70b9\n self.max_heap.push(val);\n // \u4ece\u5e95\u81f3\u9876\u5806\u5316\n self.sift_up(self.size() - 1);\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nfn sift_up(&mut self, mut i: usize) {\n loop {\n // \u8282\u70b9 i \u5df2\u7ecf\u662f\u5806\u9876\u8282\u70b9\u4e86\uff0c\u7ed3\u675f\u5806\u5316\n if i == 0 {\n break;\n }\n // \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\n let p = Self::parent(i);\n // \u5f53\u201c\u8282\u70b9\u65e0\u987b\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\n if self.max_heap[i] <= self.max_heap[p] {\n break;\n }\n // \u4ea4\u6362\u4e24\u8282\u70b9\n self.swap(i, p);\n // \u5faa\u73af\u5411\u4e0a\u5806\u5316\n i = p;\n }\n}\n
my_heap.c/* \u5143\u7d20\u5165\u5806 */\nvoid push(MaxHeap *maxHeap, int val) {\n // \u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u4e0d\u5e94\u8be5\u6dfb\u52a0\u8fd9\u4e48\u591a\u8282\u70b9\n if (maxHeap->size == MAX_SIZE) {\n printf(\"heap is full!\");\n return;\n }\n // \u6dfb\u52a0\u8282\u70b9\n maxHeap->data[maxHeap->size] = val;\n maxHeap->size++;\n\n // \u4ece\u5e95\u81f3\u9876\u5806\u5316\n siftUp(maxHeap, maxHeap->size - 1);\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nvoid siftUp(MaxHeap *maxHeap, int i) {\n while (true) {\n // \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\n int p = parent(maxHeap, i);\n // \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u987b\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\n if (p < 0 || maxHeap->data[i] <= maxHeap->data[p]) {\n break;\n }\n // \u4ea4\u6362\u4e24\u8282\u70b9\n swap(maxHeap, i, p);\n // \u5faa\u73af\u5411\u4e0a\u5806\u5316\n i = p;\n }\n}\n
my_heap.zig// \u5143\u7d20\u5165\u5806\nfn push(self: *Self, val: T) !void {\n // \u6dfb\u52a0\u8282\u70b9\n try self.max_heap.?.append(val);\n // \u4ece\u5e95\u81f3\u9876\u5806\u5316\n try self.siftUp(self.size() - 1);\n} \n\n// \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316\nfn siftUp(self: *Self, i_: usize) !void {\n var i = i_;\n while (true) {\n // \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\n var p = parent(i);\n // \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u987b\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\n if (p < 0 or self.max_heap.?.items[i] <= self.max_heap.?.items[p]) break;\n // \u4ea4\u6362\u4e24\u8282\u70b9\n try self.swap(i, p);\n // \u5faa\u73af\u5411\u4e0a\u5806\u5316\n i = p;\n }\n}\n
"},{"location":"chapter_heap/heap/#4","title":"4. \u00a0 \u5806\u9876\u5143\u7d20\u51fa\u5806","text":"\u5806\u9876\u5143\u7d20\u662f\u4e8c\u53c9\u6811\u7684\u6839\u8282\u70b9\uff0c\u5373\u5217\u8868\u9996\u5143\u7d20\u3002\u5982\u679c\u6211\u4eec\u76f4\u63a5\u4ece\u5217\u8868\u4e2d\u5220\u9664\u9996\u5143\u7d20\uff0c\u90a3\u4e48\u4e8c\u53c9\u6811\u4e2d\u6240\u6709\u8282\u70b9\u7684\u7d22\u5f15\u90fd\u4f1a\u53d1\u751f\u53d8\u5316\uff0c\u8fd9\u5c06\u4f7f\u5f97\u540e\u7eed\u4f7f\u7528\u5806\u5316\u8fdb\u884c\u4fee\u590d\u53d8\u5f97\u56f0\u96be\u3002\u4e3a\u4e86\u5c3d\u91cf\u51cf\u5c11\u5143\u7d20\u7d22\u5f15\u7684\u53d8\u52a8\uff0c\u6211\u4eec\u91c7\u7528\u4ee5\u4e0b\u64cd\u4f5c\u6b65\u9aa4\u3002
\u5982\u56fe 8-4 \u6240\u793a\uff0c\u201c\u4ece\u9876\u81f3\u5e95\u5806\u5316\u201d\u7684\u64cd\u4f5c\u65b9\u5411\u4e0e\u201c\u4ece\u5e95\u81f3\u9876\u5806\u5316\u201d\u76f8\u53cd\uff0c\u6211\u4eec\u5c06\u6839\u8282\u70b9\u7684\u503c\u4e0e\u5176\u4e24\u4e2a\u5b50\u8282\u70b9\u7684\u503c\u8fdb\u884c\u6bd4\u8f83\uff0c\u5c06\u6700\u5927\u7684\u5b50\u8282\u70b9\u4e0e\u6839\u8282\u70b9\u4ea4\u6362\u3002\u7136\u540e\u5faa\u73af\u6267\u884c\u6b64\u64cd\u4f5c\uff0c\u76f4\u5230\u8d8a\u8fc7\u53f6\u8282\u70b9\u6216\u9047\u5230\u65e0\u987b\u4ea4\u6362\u7684\u8282\u70b9\u65f6\u7ed3\u675f\u3002
<1><2><3><4><5><6><7><8><9><10>\u56fe 8-4 \u00a0 \u5806\u9876\u5143\u7d20\u51fa\u5806\u6b65\u9aa4
\u4e0e\u5143\u7d20\u5165\u5806\u64cd\u4f5c\u76f8\u4f3c\uff0c\u5806\u9876\u5143\u7d20\u51fa\u5806\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e5f\u4e3a \\(O(\\log n)\\) \u3002\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig my_heap.pydef pop(self) -> int:\n \"\"\"\u5143\u7d20\u51fa\u5806\"\"\"\n # \u5224\u7a7a\u5904\u7406\n if self.is_empty():\n raise IndexError(\"\u5806\u4e3a\u7a7a\")\n # \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n self.swap(0, self.size() - 1)\n # \u5220\u9664\u8282\u70b9\n val = self.max_heap.pop()\n # \u4ece\u9876\u81f3\u5e95\u5806\u5316\n self.sift_down(0)\n # \u8fd4\u56de\u5806\u9876\u5143\u7d20\n return val\n\ndef sift_down(self, i: int):\n \"\"\"\u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316\"\"\"\n while True:\n # \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n l, r, ma = self.left(i), self.right(i), i\n if l < self.size() and self.max_heap[l] > self.max_heap[ma]:\n ma = l\n if r < self.size() and self.max_heap[r] > self.max_heap[ma]:\n ma = r\n # \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n if ma == i:\n break\n # \u4ea4\u6362\u4e24\u8282\u70b9\n self.swap(i, ma)\n # \u5faa\u73af\u5411\u4e0b\u5806\u5316\n i = ma\n
my_heap.cpp/* \u5143\u7d20\u51fa\u5806 */\nvoid pop() {\n // \u5224\u7a7a\u5904\u7406\n if (isEmpty()) {\n throw out_of_range(\"\u5806\u4e3a\u7a7a\");\n }\n // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n swap(maxHeap[0], maxHeap[size() - 1]);\n // \u5220\u9664\u8282\u70b9\n maxHeap.pop_back();\n // \u4ece\u9876\u81f3\u5e95\u5806\u5316\n siftDown(0);\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nvoid siftDown(int i) {\n while (true) {\n // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n int l = left(i), r = right(i), ma = i;\n if (l < size() && maxHeap[l] > maxHeap[ma])\n ma = l;\n if (r < size() && maxHeap[r] > maxHeap[ma])\n ma = r;\n // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n if (ma == i)\n break;\n swap(maxHeap[i], maxHeap[ma]);\n // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n i = ma;\n }\n}\n
my_heap.java/* \u5143\u7d20\u51fa\u5806 */\nint pop() {\n // \u5224\u7a7a\u5904\u7406\n if (isEmpty())\n throw new IndexOutOfBoundsException();\n // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n swap(0, size() - 1);\n // \u5220\u9664\u8282\u70b9\n int val = maxHeap.remove(size() - 1);\n // \u4ece\u9876\u81f3\u5e95\u5806\u5316\n siftDown(0);\n // \u8fd4\u56de\u5806\u9876\u5143\u7d20\n return val;\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nvoid siftDown(int i) {\n while (true) {\n // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n int l = left(i), r = right(i), ma = i;\n if (l < size() && maxHeap.get(l) > maxHeap.get(ma))\n ma = l;\n if (r < size() && maxHeap.get(r) > maxHeap.get(ma))\n ma = r;\n // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n if (ma == i)\n break;\n // \u4ea4\u6362\u4e24\u8282\u70b9\n swap(i, ma);\n // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n i = ma;\n }\n}\n
my_heap.cs/* \u5143\u7d20\u51fa\u5806 */\nint Pop() {\n // \u5224\u7a7a\u5904\u7406\n if (IsEmpty())\n throw new IndexOutOfRangeException();\n // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n Swap(0, Size() - 1);\n // \u5220\u9664\u8282\u70b9\n int val = maxHeap.Last();\n maxHeap.RemoveAt(Size() - 1);\n // \u4ece\u9876\u81f3\u5e95\u5806\u5316\n SiftDown(0);\n // \u8fd4\u56de\u5806\u9876\u5143\u7d20\n return val;\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nvoid SiftDown(int i) {\n while (true) {\n // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n int l = Left(i), r = Right(i), ma = i;\n if (l < Size() && maxHeap[l] > maxHeap[ma])\n ma = l;\n if (r < Size() && maxHeap[r] > maxHeap[ma])\n ma = r;\n // \u82e5\u201c\u8282\u70b9 i \u6700\u5927\u201d\u6216\u201c\u8d8a\u8fc7\u53f6\u8282\u70b9\u201d\uff0c\u5219\u7ed3\u675f\u5806\u5316\n if (ma == i) break;\n // \u4ea4\u6362\u4e24\u8282\u70b9\n Swap(i, ma);\n // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n i = ma;\n }\n}\n
my_heap.go/* \u5143\u7d20\u51fa\u5806 */\nfunc (h *maxHeap) pop() any {\n // \u5224\u7a7a\u5904\u7406\n if h.isEmpty() {\n fmt.Println(\"error\")\n return nil\n }\n // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n h.swap(0, h.size()-1)\n // \u5220\u9664\u8282\u70b9\n val := h.data[len(h.data)-1]\n h.data = h.data[:len(h.data)-1]\n // \u4ece\u9876\u81f3\u5e95\u5806\u5316\n h.siftDown(0)\n\n // \u8fd4\u56de\u5806\u9876\u5143\u7d20\n return val\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nfunc (h *maxHeap) siftDown(i int) {\n for true {\n // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a max\n l, r, max := h.left(i), h.right(i), i\n if l < h.size() && h.data[l].(int) > h.data[max].(int) {\n max = l\n }\n if r < h.size() && h.data[r].(int) > h.data[max].(int) {\n max = r\n }\n // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n if max == i {\n break\n }\n // \u4ea4\u6362\u4e24\u8282\u70b9\n h.swap(i, max)\n // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n i = max\n }\n}\n
my_heap.swift/* \u5143\u7d20\u51fa\u5806 */\nfunc pop() -> Int {\n // \u5224\u7a7a\u5904\u7406\n if isEmpty() {\n fatalError(\"\u5806\u4e3a\u7a7a\")\n }\n // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n swap(i: 0, j: size() - 1)\n // \u5220\u9664\u8282\u70b9\n let val = maxHeap.remove(at: size() - 1)\n // \u4ece\u9876\u81f3\u5e95\u5806\u5316\n siftDown(i: 0)\n // \u8fd4\u56de\u5806\u9876\u5143\u7d20\n return val\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nfunc siftDown(i: Int) {\n var i = i\n while true {\n // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n let l = left(i: i)\n let r = right(i: i)\n var ma = i\n if l < size(), maxHeap[l] > maxHeap[ma] {\n ma = l\n }\n if r < size(), maxHeap[r] > maxHeap[ma] {\n ma = r\n }\n // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n if ma == i {\n break\n }\n // \u4ea4\u6362\u4e24\u8282\u70b9\n swap(i: i, j: ma)\n // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n i = ma\n }\n}\n
my_heap.js/* \u5143\u7d20\u51fa\u5806 */\npop() {\n // \u5224\u7a7a\u5904\u7406\n if (this.isEmpty()) throw new Error('\u5806\u4e3a\u7a7a');\n // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n this.#swap(0, this.size() - 1);\n // \u5220\u9664\u8282\u70b9\n const val = this.#maxHeap.pop();\n // \u4ece\u9876\u81f3\u5e95\u5806\u5316\n this.#siftDown(0);\n // \u8fd4\u56de\u5806\u9876\u5143\u7d20\n return val;\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\n#siftDown(i) {\n while (true) {\n // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n const l = this.#left(i),\n r = this.#right(i);\n let ma = i;\n if (l < this.size() && this.#maxHeap[l] > this.#maxHeap[ma]) ma = l;\n if (r < this.size() && this.#maxHeap[r] > this.#maxHeap[ma]) ma = r;\n // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n if (ma === i) break;\n // \u4ea4\u6362\u4e24\u8282\u70b9\n this.#swap(i, ma);\n // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n i = ma;\n }\n}\n
my_heap.ts/* \u5143\u7d20\u51fa\u5806 */\npop(): number {\n // \u5224\u7a7a\u5904\u7406\n if (this.isEmpty()) throw new RangeError('Heap is empty.');\n // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n this.swap(0, this.size() - 1);\n // \u5220\u9664\u8282\u70b9\n const val = this.maxHeap.pop();\n // \u4ece\u9876\u81f3\u5e95\u5806\u5316\n this.siftDown(0);\n // \u8fd4\u56de\u5806\u9876\u5143\u7d20\n return val;\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nsiftDown(i: number): void {\n while (true) {\n // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n const l = this.left(i),\n r = this.right(i);\n let ma = i;\n if (l < this.size() && this.maxHeap[l] > this.maxHeap[ma]) ma = l;\n if (r < this.size() && this.maxHeap[r] > this.maxHeap[ma]) ma = r;\n // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n if (ma === i) break;\n // \u4ea4\u6362\u4e24\u8282\u70b9\n this.swap(i, ma);\n // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n i = ma;\n }\n}\n
my_heap.dart/* \u5143\u7d20\u51fa\u5806 */\nint pop() {\n // \u5224\u7a7a\u5904\u7406\n if (isEmpty()) throw Exception('\u5806\u4e3a\u7a7a');\n // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n _swap(0, size() - 1);\n // \u5220\u9664\u8282\u70b9\n int val = _maxHeap.removeLast();\n // \u4ece\u9876\u81f3\u5e95\u5806\u5316\n siftDown(0);\n // \u8fd4\u56de\u5806\u9876\u5143\u7d20\n return val;\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nvoid siftDown(int i) {\n while (true) {\n // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n int l = _left(i);\n int r = _right(i);\n int ma = i;\n if (l < size() && _maxHeap[l] > _maxHeap[ma]) ma = l;\n if (r < size() && _maxHeap[r] > _maxHeap[ma]) ma = r;\n // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n if (ma == i) break;\n // \u4ea4\u6362\u4e24\u8282\u70b9\n _swap(i, ma);\n // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n i = ma;\n }\n}\n
my_heap.rs/* \u5143\u7d20\u51fa\u5806 */\nfn pop(&mut self) -> i32 {\n // \u5224\u7a7a\u5904\u7406\n if self.is_empty() {\n panic!(\"index out of bounds\");\n }\n // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n self.swap(0, self.size() - 1);\n // \u5220\u9664\u8282\u70b9\n let val = self.max_heap.remove(self.size() - 1);\n // \u4ece\u9876\u81f3\u5e95\u5806\u5316\n self.sift_down(0);\n // \u8fd4\u56de\u5806\u9876\u5143\u7d20\n val\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nfn sift_down(&mut self, mut i: usize) {\n loop {\n // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n let (l, r, mut ma) = (Self::left(i), Self::right(i), i);\n if l < self.size() && self.max_heap[l] > self.max_heap[ma] {\n ma = l;\n }\n if r < self.size() && self.max_heap[r] > self.max_heap[ma] {\n ma = r;\n }\n // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n if ma == i {\n break;\n }\n // \u4ea4\u6362\u4e24\u8282\u70b9\n self.swap(i, ma);\n // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n i = ma;\n }\n}\n
my_heap.c/* \u5143\u7d20\u51fa\u5806 */\nint pop(MaxHeap *maxHeap) {\n // \u5224\u7a7a\u5904\u7406\n if (isEmpty(maxHeap)) {\n printf(\"heap is empty!\");\n return INT_MAX;\n }\n // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n swap(maxHeap, 0, size(maxHeap) - 1);\n // \u5220\u9664\u8282\u70b9\n int val = maxHeap->data[maxHeap->size - 1];\n maxHeap->size--;\n // \u4ece\u9876\u81f3\u5e95\u5806\u5316\n siftDown(maxHeap, 0);\n\n // \u8fd4\u56de\u5806\u9876\u5143\u7d20\n return val;\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nvoid siftDown(MaxHeap *maxHeap, int i) {\n while (true) {\n // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a max\n int l = left(maxHeap, i);\n int r = right(maxHeap, i);\n int max = i;\n if (l < size(maxHeap) && maxHeap->data[l] > maxHeap->data[max]) {\n max = l;\n }\n if (r < size(maxHeap) && maxHeap->data[r] > maxHeap->data[max]) {\n max = r;\n }\n // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n if (max == i) {\n break;\n }\n // \u4ea4\u6362\u4e24\u8282\u70b9\n swap(maxHeap, i, max);\n // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n i = max;\n }\n}\n
my_heap.zig// \u5143\u7d20\u51fa\u5806\nfn pop(self: *Self) !T {\n // \u5224\u65ad\u5904\u7406\n if (self.isEmpty()) unreachable;\n // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n try self.swap(0, self.size() - 1);\n // \u5220\u9664\u8282\u70b9\n var val = self.max_heap.?.pop();\n // \u4ece\u9876\u81f3\u5e95\u5806\u5316\n try self.siftDown(0);\n // \u8fd4\u56de\u5806\u9876\u5143\u7d20\n return val;\n} \n\n// \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316\nfn siftDown(self: *Self, i_: usize) !void {\n var i = i_;\n while (true) {\n // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n var l = left(i);\n var r = right(i);\n var ma = i;\n if (l < self.size() and self.max_heap.?.items[l] > self.max_heap.?.items[ma]) ma = l;\n if (r < self.size() and self.max_heap.?.items[r] > self.max_heap.?.items[ma]) ma = r;\n // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n if (ma == i) break;\n // \u4ea4\u6362\u4e24\u8282\u70b9\n try self.swap(i, ma);\n // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n i = ma;\n }\n}\n
"},{"location":"chapter_heap/heap/#813","title":"8.1.3 \u00a0 \u5806\u5e38\u89c1\u5e94\u7528","text":"\u6570\u636e\u7ed3\u6784\u7684\u201c\u5806\u201d\u4e0e\u5185\u5b58\u7ba1\u7406\u7684\u201c\u5806\u201d\u662f\u540c\u4e00\u4e2a\u6982\u5ff5\u5417\uff1f
\u4e24\u8005\u4e0d\u662f\u540c\u4e00\u4e2a\u6982\u5ff5\uff0c\u53ea\u662f\u78b0\u5de7\u90fd\u53eb\u5806\u3002\u8ba1\u7b97\u673a\u7cfb\u7edf\u5185\u5b58\u4e2d\u7684\u5806\u662f\u52a8\u6001\u5185\u5b58\u5206\u914d\u7684\u4e00\u90e8\u5206\uff0c\u7a0b\u5e8f\u5728\u8fd0\u884c\u65f6\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u5b58\u50a8\u6570\u636e\u3002\u7a0b\u5e8f\u53ef\u4ee5\u8bf7\u6c42\u4e00\u5b9a\u91cf\u7684\u5806\u5185\u5b58\uff0c\u7528\u4e8e\u5b58\u50a8\u5982\u5bf9\u8c61\u548c\u6570\u7ec4\u7b49\u590d\u6742\u7ed3\u6784\u3002\u5f53\u8fd9\u4e9b\u6570\u636e\u4e0d\u518d\u9700\u8981\u65f6\uff0c\u7a0b\u5e8f\u9700\u8981\u91ca\u653e\u8fd9\u4e9b\u5185\u5b58\uff0c\u4ee5\u9632\u6b62\u5185\u5b58\u6cc4\u6f0f\u3002\u76f8\u8f83\u4e8e\u6808\u5185\u5b58\uff0c\u5806\u5185\u5b58\u7684\u7ba1\u7406\u548c\u4f7f\u7528\u9700\u8981\u66f4\u8c28\u614e\uff0c\u4f7f\u7528\u4e0d\u5f53\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\u548c\u91ce\u6307\u9488\u7b49\u95ee\u9898\u3002
"},{"location":"chapter_heap/top_k/","title":"8.3 \u00a0 Top-K \u95ee\u9898","text":"Question
\u7ed9\u5b9a\u4e00\u4e2a\u957f\u5ea6\u4e3a \\(n\\) \u7684\u65e0\u5e8f\u6570\u7ec4 nums
\uff0c\u8bf7\u8fd4\u56de\u6570\u7ec4\u4e2d\u524d \\(k\\) \u5927\u7684\u5143\u7d20\u3002
\u5bf9\u4e8e\u8be5\u95ee\u9898\uff0c\u6211\u4eec\u5148\u4ecb\u7ecd\u4e24\u79cd\u601d\u8def\u6bd4\u8f83\u76f4\u63a5\u7684\u89e3\u6cd5\uff0c\u518d\u4ecb\u7ecd\u6548\u7387\u66f4\u9ad8\u7684\u5806\u89e3\u6cd5\u3002
"},{"location":"chapter_heap/top_k/#831","title":"8.3.1 \u00a0 \u65b9\u6cd5\u4e00\uff1a\u904d\u5386\u9009\u62e9","text":"\u6211\u4eec\u53ef\u4ee5\u8fdb\u884c\u56fe 8-6 \u6240\u793a\u7684 \\(k\\) \u8f6e\u904d\u5386\uff0c\u5206\u522b\u5728\u6bcf\u8f6e\u4e2d\u63d0\u53d6\u7b2c \\(1\\)\u3001\\(2\\)\u3001\\(\\dots\\)\u3001\\(k\\) \u5927\u7684\u5143\u7d20\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(nk)\\) \u3002
\u6b64\u65b9\u6cd5\u53ea\u9002\u7528\u4e8e \\(k \\ll n\\) \u7684\u60c5\u51b5\uff0c\u56e0\u4e3a\u5f53 \\(k\\) \u4e0e \\(n\\) \u6bd4\u8f83\u63a5\u8fd1\u65f6\uff0c\u5176\u65f6\u95f4\u590d\u6742\u5ea6\u8d8b\u5411\u4e8e \\(O(n^2)\\) \uff0c\u975e\u5e38\u8017\u65f6\u3002
\u56fe 8-6 \u00a0 \u904d\u5386\u5bfb\u627e\u6700\u5927\u7684 k \u4e2a\u5143\u7d20
Tip
\u5f53 \\(k = n\\) \u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u5f97\u5230\u5b8c\u6574\u7684\u6709\u5e8f\u5e8f\u5217\uff0c\u6b64\u65f6\u7b49\u4ef7\u4e8e\u201c\u9009\u62e9\u6392\u5e8f\u201d\u7b97\u6cd5\u3002
"},{"location":"chapter_heap/top_k/#832","title":"8.3.2 \u00a0 \u65b9\u6cd5\u4e8c\uff1a\u6392\u5e8f","text":"\u5982\u56fe 8-7 \u6240\u793a\uff0c\u6211\u4eec\u53ef\u4ee5\u5148\u5bf9\u6570\u7ec4 nums
\u8fdb\u884c\u6392\u5e8f\uff0c\u518d\u8fd4\u56de\u6700\u53f3\u8fb9\u7684 \\(k\\) \u4e2a\u5143\u7d20\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n \\log n)\\) \u3002
\u663e\u7136\uff0c\u8be5\u65b9\u6cd5\u201c\u8d85\u989d\u201d\u5b8c\u6210\u4efb\u52a1\u4e86\uff0c\u56e0\u4e3a\u6211\u4eec\u53ea\u9700\u627e\u51fa\u6700\u5927\u7684 \\(k\\) \u4e2a\u5143\u7d20\u5373\u53ef\uff0c\u800c\u4e0d\u9700\u8981\u6392\u5e8f\u5176\u4ed6\u5143\u7d20\u3002
\u56fe 8-7 \u00a0 \u6392\u5e8f\u5bfb\u627e\u6700\u5927\u7684 k \u4e2a\u5143\u7d20
"},{"location":"chapter_heap/top_k/#833","title":"8.3.3 \u00a0 \u65b9\u6cd5\u4e09\uff1a\u5806","text":"\u6211\u4eec\u53ef\u4ee5\u57fa\u4e8e\u5806\u66f4\u52a0\u9ad8\u6548\u5730\u89e3\u51b3 Top-K \u95ee\u9898\uff0c\u6d41\u7a0b\u5982\u56fe 8-8 \u6240\u793a\u3002
\u56fe 8-8 \u00a0 \u57fa\u4e8e\u5806\u5bfb\u627e\u6700\u5927\u7684 k \u4e2a\u5143\u7d20
\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig top_k.pydef top_k_heap(nums: list[int], k: int) -> list[int]:\n \"\"\"\u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20\"\"\"\n # \u521d\u59cb\u5316\u5c0f\u9876\u5806\n heap = []\n # \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\n for i in range(k):\n heapq.heappush(heap, nums[i])\n # \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\n for i in range(k, len(nums)):\n # \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\n if nums[i] > heap[0]:\n heapq.heappop(heap)\n heapq.heappush(heap, nums[i])\n return heap\n
top_k.cpp/* \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20 */\npriority_queue<int, vector<int>, greater<int>> topKHeap(vector<int> &nums, int k) {\n // \u521d\u59cb\u5316\u5c0f\u9876\u5806\n priority_queue<int, vector<int>, greater<int>> heap;\n // \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\n for (int i = 0; i < k; i++) {\n heap.push(nums[i]);\n }\n // \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\n for (int i = k; i < nums.size(); i++) {\n // \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\n if (nums[i] > heap.top()) {\n heap.pop();\n heap.push(nums[i]);\n }\n }\n return heap;\n}\n
top_k.java/* \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20 */\nQueue<Integer> topKHeap(int[] nums, int k) {\n // \u521d\u59cb\u5316\u5c0f\u9876\u5806\n Queue<Integer> heap = new PriorityQueue<Integer>();\n // \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\n for (int i = 0; i < k; i++) {\n heap.offer(nums[i]);\n }\n // \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\n for (int i = k; i < nums.length; i++) {\n // \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\n if (nums[i] > heap.peek()) {\n heap.poll();\n heap.offer(nums[i]);\n }\n }\n return heap;\n}\n
top_k.cs/* \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20 */\nPriorityQueue<int, int> TopKHeap(int[] nums, int k) {\n // \u521d\u59cb\u5316\u5c0f\u9876\u5806\n PriorityQueue<int, int> heap = new();\n // \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\n for (int i = 0; i < k; i++) {\n heap.Enqueue(nums[i], nums[i]);\n }\n // \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\n for (int i = k; i < nums.Length; i++) {\n // \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\n if (nums[i] > heap.Peek()) {\n heap.Dequeue();\n heap.Enqueue(nums[i], nums[i]);\n }\n }\n return heap;\n}\n
top_k.go/* \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20 */\nfunc topKHeap(nums []int, k int) *minHeap {\n // \u521d\u59cb\u5316\u5c0f\u9876\u5806\n h := &minHeap{}\n heap.Init(h)\n // \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\n for i := 0; i < k; i++ {\n heap.Push(h, nums[i])\n }\n // \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\n for i := k; i < len(nums); i++ {\n // \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\n if nums[i] > h.Top().(int) {\n heap.Pop(h)\n heap.Push(h, nums[i])\n }\n }\n return h\n}\n
top_k.swift/* \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20 */\nfunc topKHeap(nums: [Int], k: Int) -> [Int] {\n // \u521d\u59cb\u5316\u4e00\u4e2a\u5c0f\u9876\u5806\uff0c\u5e76\u5c06\u524d k \u4e2a\u5143\u7d20\u5efa\u5806\n var heap = Heap(nums.prefix(k))\n // \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\n for i in stride(from: k, to: nums.count, by: 1) {\n // \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\n if nums[i] > heap.min()! {\n _ = heap.removeMin()\n heap.insert(nums[i])\n }\n }\n return heap.unordered\n}\n
top_k.js/* \u5143\u7d20\u5165\u5806 */\nfunction pushMinHeap(maxHeap, val) {\n // \u5143\u7d20\u53d6\u53cd\n maxHeap.push(-val);\n}\n\n/* \u5143\u7d20\u51fa\u5806 */\nfunction popMinHeap(maxHeap) {\n // \u5143\u7d20\u53d6\u53cd\n return -maxHeap.pop();\n}\n\n/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nfunction peekMinHeap(maxHeap) {\n // \u5143\u7d20\u53d6\u53cd\n return -maxHeap.peek();\n}\n\n/* \u53d6\u51fa\u5806\u4e2d\u5143\u7d20 */\nfunction getMinHeap(maxHeap) {\n // \u5143\u7d20\u53d6\u53cd\n return maxHeap.getMaxHeap().map((num) => -num);\n}\n\n/* \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20 */\nfunction topKHeap(nums, k) {\n // \u521d\u59cb\u5316\u5c0f\u9876\u5806\n // \u8bf7\u6ce8\u610f\uff1a\u6211\u4eec\u5c06\u5806\u4e2d\u6240\u6709\u5143\u7d20\u53d6\u53cd\uff0c\u4ece\u800c\u7528\u5927\u9876\u5806\u6765\u6a21\u62df\u5c0f\u9876\u5806\n const maxHeap = new MaxHeap([]);\n // \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\n for (let i = 0; i < k; i++) {\n pushMinHeap(maxHeap, nums[i]);\n }\n // \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\n for (let i = k; i < nums.length; i++) {\n // \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\n if (nums[i] > peekMinHeap(maxHeap)) {\n popMinHeap(maxHeap);\n pushMinHeap(maxHeap, nums[i]);\n }\n }\n // \u8fd4\u56de\u5806\u4e2d\u5143\u7d20\n return getMinHeap(maxHeap);\n}\n
top_k.ts/* \u5143\u7d20\u5165\u5806 */\nfunction pushMinHeap(maxHeap: MaxHeap, val: number): void {\n // \u5143\u7d20\u53d6\u53cd\n maxHeap.push(-val);\n}\n\n/* \u5143\u7d20\u51fa\u5806 */\nfunction popMinHeap(maxHeap: MaxHeap): number {\n // \u5143\u7d20\u53d6\u53cd\n return -maxHeap.pop();\n}\n\n/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nfunction peekMinHeap(maxHeap: MaxHeap): number {\n // \u5143\u7d20\u53d6\u53cd\n return -maxHeap.peek();\n}\n\n/* \u53d6\u51fa\u5806\u4e2d\u5143\u7d20 */\nfunction getMinHeap(maxHeap: MaxHeap): number[] {\n // \u5143\u7d20\u53d6\u53cd\n return maxHeap.getMaxHeap().map((num: number) => -num);\n}\n\n/* \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20 */\nfunction topKHeap(nums: number[], k: number): number[] {\n // \u521d\u59cb\u5316\u5c0f\u9876\u5806\n // \u8bf7\u6ce8\u610f\uff1a\u6211\u4eec\u5c06\u5806\u4e2d\u6240\u6709\u5143\u7d20\u53d6\u53cd\uff0c\u4ece\u800c\u7528\u5927\u9876\u5806\u6765\u6a21\u62df\u5c0f\u9876\u5806\n const maxHeap = new MaxHeap([]);\n // \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\n for (let i = 0; i < k; i++) {\n pushMinHeap(maxHeap, nums[i]);\n }\n // \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\n for (let i = k; i < nums.length; i++) {\n // \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\n if (nums[i] > peekMinHeap(maxHeap)) {\n popMinHeap(maxHeap);\n pushMinHeap(maxHeap, nums[i]);\n }\n }\n // \u8fd4\u56de\u5806\u4e2d\u5143\u7d20\n return getMinHeap(maxHeap);\n}\n
top_k.dart/* \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20 */\nMinHeap topKHeap(List<int> nums, int k) {\n // \u521d\u59cb\u5316\u5c0f\u9876\u5806\uff0c\u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\n MinHeap heap = MinHeap(nums.sublist(0, k));\n // \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\n for (int i = k; i < nums.length; i++) {\n // \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\n if (nums[i] > heap.peek()) {\n heap.pop();\n heap.push(nums[i]);\n }\n }\n return heap;\n}\n
top_k.rs/* \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20 */\nfn top_k_heap(nums: Vec<i32>, k: usize) -> BinaryHeap<Reverse<i32>> {\n // BinaryHeap \u662f\u5927\u9876\u5806\uff0c\u4f7f\u7528 Reverse \u5c06\u5143\u7d20\u53d6\u53cd\uff0c\u4ece\u800c\u5b9e\u73b0\u5c0f\u9876\u5806\n let mut heap = BinaryHeap::<Reverse<i32>>::new();\n // \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\n for &num in nums.iter().take(k) {\n heap.push(Reverse(num));\n }\n // \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\n for &num in nums.iter().skip(k) {\n // \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\n if num > heap.peek().unwrap().0 {\n heap.pop();\n heap.push(Reverse(num));\n }\n }\n heap\n}\n
top_k.c/* \u5143\u7d20\u5165\u5806 */\nvoid pushMinHeap(MaxHeap *maxHeap, int val) {\n // \u5143\u7d20\u53d6\u53cd\n push(maxHeap, -val);\n}\n\n/* \u5143\u7d20\u51fa\u5806 */\nint popMinHeap(MaxHeap *maxHeap) {\n // \u5143\u7d20\u53d6\u53cd\n return -pop(maxHeap);\n}\n\n/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nint peekMinHeap(MaxHeap *maxHeap) {\n // \u5143\u7d20\u53d6\u53cd\n return -peek(maxHeap);\n}\n\n/* \u53d6\u51fa\u5806\u4e2d\u5143\u7d20 */\nint *getMinHeap(MaxHeap *maxHeap) {\n // \u5c06\u5806\u4e2d\u6240\u6709\u5143\u7d20\u53d6\u53cd\u5e76\u5b58\u5165 res \u6570\u7ec4\n int *res = (int *)malloc(maxHeap->size * sizeof(int));\n for (int i = 0; i < maxHeap->size; i++) {\n res[i] = -maxHeap->data[i];\n }\n return res;\n}\n\n/* \u53d6\u51fa\u5806\u4e2d\u5143\u7d20 */\nint *getMinHeap(MaxHeap *maxHeap) {\n // \u5c06\u5806\u4e2d\u6240\u6709\u5143\u7d20\u53d6\u53cd\u5e76\u5b58\u5165 res \u6570\u7ec4\n int *res = (int *)malloc(maxHeap->size * sizeof(int));\n for (int i = 0; i < maxHeap->size; i++) {\n res[i] = -maxHeap->data[i];\n }\n return res;\n}\n\n// \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20\u7684\u51fd\u6570\nint *topKHeap(int *nums, int sizeNums, int k) {\n // \u521d\u59cb\u5316\u5c0f\u9876\u5806\n // \u8bf7\u6ce8\u610f\uff1a\u6211\u4eec\u5c06\u5806\u4e2d\u6240\u6709\u5143\u7d20\u53d6\u53cd\uff0c\u4ece\u800c\u7528\u5927\u9876\u5806\u6765\u6a21\u62df\u5c0f\u9876\u5806\n int *empty = (int *)malloc(0);\n MaxHeap *maxHeap = newMaxHeap(empty, 0);\n // \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\n for (int i = 0; i < k; i++) {\n pushMinHeap(maxHeap, nums[i]);\n }\n // \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\n for (int i = k; i < sizeNums; i++) {\n // \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\n if (nums[i] > peekMinHeap(maxHeap)) {\n popMinHeap(maxHeap);\n pushMinHeap(maxHeap, nums[i]);\n }\n }\n int *res = getMinHeap(maxHeap);\n // \u91ca\u653e\u5185\u5b58\n delMaxHeap(maxHeap);\n return res;\n}\n
top_k.zig[class]{}-[func]{topKHeap}\n
\u603b\u5171\u6267\u884c\u4e86 \\(n\\) \u8f6e\u5165\u5806\u548c\u51fa\u5806\uff0c\u5806\u7684\u6700\u5927\u957f\u5ea6\u4e3a \\(k\\) \uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n \\log k)\\) \u3002\u8be5\u65b9\u6cd5\u7684\u6548\u7387\u5f88\u9ad8\uff0c\u5f53 \\(k\\) \u8f83\u5c0f\u65f6\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u8d8b\u5411 \\(O(n)\\) \uff1b\u5f53 \\(k\\) \u8f83\u5927\u65f6\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e0d\u4f1a\u8d85\u8fc7 \\(O(n \\log n)\\) \u3002
\u53e6\u5916\uff0c\u8be5\u65b9\u6cd5\u9002\u7528\u4e8e\u52a8\u6001\u6570\u636e\u6d41\u7684\u4f7f\u7528\u573a\u666f\u3002\u5728\u4e0d\u65ad\u52a0\u5165\u6570\u636e\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u6301\u7eed\u7ef4\u62a4\u5806\u5185\u7684\u5143\u7d20\uff0c\u4ece\u800c\u5b9e\u73b0\u6700\u5927 \\(k\\) \u4e2a\u5143\u7d20\u7684\u52a8\u6001\u66f4\u65b0\u3002
"},{"location":"chapter_introduction/","title":"\u7b2c 1 \u7ae0 \u00a0 \u521d\u8bc6\u7b97\u6cd5","text":"Abstract
\u4e00\u4f4d\u5c11\u5973\u7fe9\u7fe9\u8d77\u821e\uff0c\u4e0e\u6570\u636e\u4ea4\u7ec7\u5728\u4e00\u8d77\uff0c\u88d9\u6446\u4e0a\u98d8\u626c\u7740\u7b97\u6cd5\u7684\u65cb\u5f8b\u3002
\u5979\u9080\u8bf7\u4f60\u5171\u821e\uff0c\u8bf7\u7d27\u8ddf\u5979\u7684\u6b65\u4f10\uff0c\u8e0f\u5165\u5145\u6ee1\u903b\u8f91\u4e0e\u7f8e\u611f\u7684\u7b97\u6cd5\u4e16\u754c\u3002
"},{"location":"chapter_introduction/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"\u5f53\u6211\u4eec\u542c\u5230\u201c\u7b97\u6cd5\u201d\u8fd9\u4e2a\u8bcd\u65f6\uff0c\u5f88\u81ea\u7136\u5730\u4f1a\u60f3\u5230\u6570\u5b66\u3002\u7136\u800c\u5b9e\u9645\u4e0a\uff0c\u8bb8\u591a\u7b97\u6cd5\u5e76\u4e0d\u6d89\u53ca\u590d\u6742\u6570\u5b66\uff0c\u800c\u662f\u66f4\u591a\u5730\u4f9d\u8d56\u57fa\u672c\u903b\u8f91\uff0c\u8fd9\u4e9b\u903b\u8f91\u5728\u6211\u4eec\u7684\u65e5\u5e38\u751f\u6d3b\u4e2d\u5904\u5904\u53ef\u89c1\u3002
\u5728\u6b63\u5f0f\u63a2\u8ba8\u7b97\u6cd5\u4e4b\u524d\uff0c\u6709\u4e00\u4e2a\u6709\u8da3\u7684\u4e8b\u5b9e\u503c\u5f97\u5206\u4eab\uff1a\u4f60\u5df2\u7ecf\u5728\u4e0d\u77e5\u4e0d\u89c9\u4e2d\u5b66\u4f1a\u4e86\u8bb8\u591a\u7b97\u6cd5\uff0c\u5e76\u4e60\u60ef\u5c06\u5b83\u4eec\u5e94\u7528\u5230\u65e5\u5e38\u751f\u6d3b\u4e2d\u4e86\u3002\u4e0b\u9762\u6211\u5c06\u4e3e\u51e0\u4e2a\u5177\u4f53\u7684\u4f8b\u5b50\u6765\u8bc1\u5b9e\u8fd9\u4e00\u70b9\u3002
\u4f8b\u4e00\uff1a\u67e5\u9605\u5b57\u5178\u3002\u5728\u5b57\u5178\u91cc\uff0c\u6bcf\u4e2a\u6c49\u5b57\u90fd\u5bf9\u5e94\u4e00\u4e2a\u62fc\u97f3\uff0c\u800c\u5b57\u5178\u662f\u6309\u7167\u62fc\u97f3\u5b57\u6bcd\u987a\u5e8f\u6392\u5217\u7684\u3002\u5047\u8bbe\u6211\u4eec\u9700\u8981\u67e5\u627e\u4e00\u4e2a\u62fc\u97f3\u9996\u5b57\u6bcd\u4e3a \\(r\\) \u7684\u5b57\uff0c\u901a\u5e38\u4f1a\u6309\u7167\u56fe 1-1 \u6240\u793a\u7684\u65b9\u5f0f\u5b9e\u73b0\u3002
1.
\u548c \u6b65\u9aa4 2.
\uff0c\u76f4\u81f3\u627e\u5230\u62fc\u97f3\u9996\u5b57\u6bcd\u4e3a \\(r\\) \u7684\u9875\u7801\u4e3a\u6b62\u3002\u56fe 1-1 \u00a0 \u67e5\u5b57\u5178\u6b65\u9aa4
\u67e5\u5b57\u5178\u8fd9\u4e2a\u5c0f\u5b66\u751f\u5fc5\u5907\u6280\u80fd\uff0c\u5b9e\u9645\u4e0a\u5c31\u662f\u8457\u540d\u7684\u201c\u4e8c\u5206\u67e5\u627e\u201d\u7b97\u6cd5\u3002\u4ece\u6570\u636e\u7ed3\u6784\u7684\u89d2\u5ea6\uff0c\u6211\u4eec\u53ef\u4ee5\u628a\u5b57\u5178\u89c6\u4e3a\u4e00\u4e2a\u5df2\u6392\u5e8f\u7684\u201c\u6570\u7ec4\u201d\uff1b\u4ece\u7b97\u6cd5\u7684\u89d2\u5ea6\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u4e0a\u8ff0\u67e5\u5b57\u5178\u7684\u4e00\u7cfb\u5217\u64cd\u4f5c\u770b\u4f5c\u201c\u4e8c\u5206\u67e5\u627e\u201d\u3002
\u4f8b\u4e8c\uff1a\u6574\u7406\u6251\u514b\u3002\u6211\u4eec\u5728\u6253\u724c\u65f6\uff0c\u6bcf\u5c40\u90fd\u9700\u8981\u6574\u7406\u624b\u4e2d\u7684\u6251\u514b\u724c\uff0c\u4f7f\u5176\u4ece\u5c0f\u5230\u5927\u6392\u5217\uff0c\u5b9e\u73b0\u6d41\u7a0b\u5982\u56fe 1-2 \u6240\u793a\u3002
2.
\uff0c\u6bcf\u4e00\u8f6e\u5c06\u4e00\u5f20\u6251\u514b\u724c\u4ece\u65e0\u5e8f\u90e8\u5206\u63d2\u5165\u81f3\u6709\u5e8f\u90e8\u5206\uff0c\u76f4\u81f3\u6240\u6709\u6251\u514b\u724c\u90fd\u6709\u5e8f\u3002\u56fe 1-2 \u00a0 \u6251\u514b\u6392\u5e8f\u6b65\u9aa4
\u4e0a\u8ff0\u6574\u7406\u6251\u514b\u724c\u7684\u65b9\u6cd5\u672c\u8d28\u4e0a\u662f\u201c\u63d2\u5165\u6392\u5e8f\u201d\u7b97\u6cd5\uff0c\u5b83\u5728\u5904\u7406\u5c0f\u578b\u6570\u636e\u96c6\u65f6\u975e\u5e38\u9ad8\u6548\u3002\u8bb8\u591a\u7f16\u7a0b\u8bed\u8a00\u7684\u6392\u5e8f\u5e93\u51fd\u6570\u4e2d\u90fd\u6709\u63d2\u5165\u6392\u5e8f\u7684\u8eab\u5f71\u3002
\u4f8b\u4e09\uff1a\u8d27\u5e01\u627e\u96f6\u3002\u5047\u8bbe\u6211\u4eec\u5728\u8d85\u5e02\u8d2d\u4e70\u4e86 \\(69\\) \u5143\u7684\u5546\u54c1\uff0c\u7ed9\u4e86\u6536\u94f6\u5458 \\(100\\) \u5143\uff0c\u5219\u6536\u94f6\u5458\u9700\u8981\u627e\u6211\u4eec \\(31\\) \u5143\u3002\u4ed6\u4f1a\u5f88\u81ea\u7136\u5730\u5b8c\u6210\u5982\u56fe 1-3 \u6240\u793a\u7684\u601d\u8003\u3002
\u56fe 1-3 \u00a0 \u8d27\u5e01\u627e\u96f6\u8fc7\u7a0b
\u5728\u4ee5\u4e0a\u6b65\u9aa4\u4e2d\uff0c\u6211\u4eec\u6bcf\u4e00\u6b65\u90fd\u91c7\u53d6\u5f53\u524d\u770b\u6765\u6700\u597d\u7684\u9009\u62e9\uff08\u5c3d\u53ef\u80fd\u7528\u5927\u9762\u989d\u7684\u8d27\u5e01\uff09\uff0c\u6700\u7ec8\u5f97\u5230\u4e86\u53ef\u884c\u7684\u627e\u96f6\u65b9\u6848\u3002\u4ece\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7684\u89d2\u5ea6\u770b\uff0c\u8fd9\u79cd\u65b9\u6cd5\u672c\u8d28\u4e0a\u662f\u201c\u8d2a\u5fc3\u201d\u7b97\u6cd5\u3002
\u5c0f\u5230\u70f9\u996a\u4e00\u9053\u83dc\uff0c\u5927\u5230\u661f\u9645\u822a\u884c\uff0c\u51e0\u4e4e\u6240\u6709\u95ee\u9898\u7684\u89e3\u51b3\u90fd\u79bb\u4e0d\u5f00\u7b97\u6cd5\u3002\u8ba1\u7b97\u673a\u7684\u51fa\u73b0\u4f7f\u5f97\u6211\u4eec\u80fd\u591f\u901a\u8fc7\u7f16\u7a0b\u5c06\u6570\u636e\u7ed3\u6784\u5b58\u50a8\u5728\u5185\u5b58\u4e2d\uff0c\u540c\u65f6\u7f16\u5199\u4ee3\u7801\u8c03\u7528 CPU \u548c GPU \u6267\u884c\u7b97\u6cd5\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u6211\u4eec\u5c31\u80fd\u628a\u751f\u6d3b\u4e2d\u7684\u95ee\u9898\u8f6c\u79fb\u5230\u8ba1\u7b97\u673a\u4e0a\uff0c\u4ee5\u66f4\u9ad8\u6548\u7684\u65b9\u5f0f\u89e3\u51b3\u5404\u79cd\u590d\u6742\u95ee\u9898\u3002
Tip
\u5982\u679c\u4f60\u5bf9\u6570\u636e\u7ed3\u6784\u3001\u7b97\u6cd5\u3001\u6570\u7ec4\u548c\u4e8c\u5206\u67e5\u627e\u7b49\u6982\u5ff5\u4ecd\u611f\u5230\u4e00\u77e5\u534a\u89e3\uff0c\u8bf7\u7ee7\u7eed\u5f80\u4e0b\u9605\u8bfb\uff0c\u672c\u4e66\u5c06\u5f15\u5bfc\u4f60\u8fc8\u5165\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7684\u77e5\u8bc6\u6bbf\u5802\u3002
"},{"location":"chapter_introduction/summary/","title":"1.3 \u00a0 \u5c0f\u7ed3","text":"\u300c\u7b97\u6cd5 algorithm\u300d\u662f\u5728\u6709\u9650\u65f6\u95f4\u5185\u89e3\u51b3\u7279\u5b9a\u95ee\u9898\u7684\u4e00\u7ec4\u6307\u4ee4\u6216\u64cd\u4f5c\u6b65\u9aa4\uff0c\u5b83\u5177\u6709\u4ee5\u4e0b\u7279\u6027\u3002
\u300c\u6570\u636e\u7ed3\u6784 data structure\u300d\u662f\u8ba1\u7b97\u673a\u4e2d\u7ec4\u7ec7\u548c\u5b58\u50a8\u6570\u636e\u7684\u65b9\u5f0f\uff0c\u5177\u6709\u4ee5\u4e0b\u8bbe\u8ba1\u76ee\u6807\u3002
\u6570\u636e\u7ed3\u6784\u8bbe\u8ba1\u662f\u4e00\u4e2a\u5145\u6ee1\u6743\u8861\u7684\u8fc7\u7a0b\u3002\u5982\u679c\u60f3\u5728\u67d0\u65b9\u9762\u53d6\u5f97\u63d0\u5347\uff0c\u5f80\u5f80\u9700\u8981\u5728\u53e6\u4e00\u65b9\u9762\u4f5c\u51fa\u59a5\u534f\u3002\u4e0b\u9762\u4e3e\u4e24\u4e2a\u4f8b\u5b50\u3002
\u5982\u56fe 1-4 \u6240\u793a\uff0c\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u9ad8\u5ea6\u76f8\u5173\u3001\u7d27\u5bc6\u7ed3\u5408\uff0c\u5177\u4f53\u8868\u73b0\u5728\u4ee5\u4e0b\u4e09\u4e2a\u65b9\u9762\u3002
\u56fe 1-4 \u00a0 \u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7684\u5173\u7cfb
\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u72b9\u5982\u56fe 1-5 \u6240\u793a\u7684\u62fc\u88c5\u79ef\u6728\u3002\u4e00\u5957\u79ef\u6728\uff0c\u9664\u4e86\u5305\u542b\u8bb8\u591a\u96f6\u4ef6\u4e4b\u5916\uff0c\u8fd8\u9644\u6709\u8be6\u7ec6\u7684\u7ec4\u88c5\u8bf4\u660e\u4e66\u3002\u6211\u4eec\u6309\u7167\u8bf4\u660e\u4e66\u4e00\u6b65\u6b65\u64cd\u4f5c\uff0c\u5c31\u80fd\u7ec4\u88c5\u51fa\u7cbe\u7f8e\u7684\u79ef\u6728\u6a21\u578b\u3002
\u56fe 1-5 \u00a0 \u62fc\u88c5\u79ef\u6728
\u4e24\u8005\u7684\u8be6\u7ec6\u5bf9\u5e94\u5173\u7cfb\u5982\u8868 1-1 \u6240\u793a\u3002
\u8868 1-1 \u00a0 \u5c06\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7c7b\u6bd4\u4e3a\u62fc\u88c5\u79ef\u6728
\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5 \u62fc\u88c5\u79ef\u6728 \u8f93\u5165\u6570\u636e \u672a\u62fc\u88c5\u7684\u79ef\u6728 \u6570\u636e\u7ed3\u6784 \u79ef\u6728\u7ec4\u7ec7\u5f62\u5f0f\uff0c\u5305\u62ec\u5f62\u72b6\u3001\u5927\u5c0f\u3001\u8fde\u63a5\u65b9\u5f0f\u7b49 \u7b97\u6cd5 \u628a\u79ef\u6728\u62fc\u6210\u76ee\u6807\u5f62\u6001\u7684\u4e00\u7cfb\u5217\u64cd\u4f5c\u6b65\u9aa4 \u8f93\u51fa\u6570\u636e \u79ef\u6728\u6a21\u578b\u503c\u5f97\u8bf4\u660e\u7684\u662f\uff0c\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u662f\u72ec\u7acb\u4e8e\u7f16\u7a0b\u8bed\u8a00\u7684\u3002\u6b63\u56e0\u5982\u6b64\uff0c\u672c\u4e66\u5f97\u4ee5\u63d0\u4f9b\u57fa\u4e8e\u591a\u79cd\u7f16\u7a0b\u8bed\u8a00\u7684\u5b9e\u73b0\u3002
\u7ea6\u5b9a\u4fd7\u6210\u7684\u7b80\u79f0
\u5728\u5b9e\u9645\u8ba8\u8bba\u65f6\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u5c06\u201c\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u201d\u7b80\u79f0\u4e3a\u201c\u7b97\u6cd5\u201d\u3002\u6bd4\u5982\u4f17\u6240\u5468\u77e5\u7684 LeetCode \u7b97\u6cd5\u9898\u76ee\uff0c\u5b9e\u9645\u4e0a\u540c\u65f6\u8003\u67e5\u6570\u636e\u7ed3\u6784\u548c\u7b97\u6cd5\u4e24\u65b9\u9762\u7684\u77e5\u8bc6\u3002
"},{"location":"chapter_preface/","title":"\u7b2c 0 \u7ae0 \u00a0 \u524d\u8a00","text":"Abstract
\u7b97\u6cd5\u72b9\u5982\u7f8e\u5999\u7684\u4ea4\u54cd\u4e50\uff0c\u6bcf\u4e00\u884c\u4ee3\u7801\u90fd\u50cf\u97f5\u5f8b\u822c\u6d41\u6dcc\u3002
\u613f\u8fd9\u672c\u4e66\u5728\u4f60\u7684\u8111\u6d77\u4e2d\u8f7b\u8f7b\u54cd\u8d77\uff0c\u7559\u4e0b\u72ec\u7279\u800c\u6df1\u523b\u7684\u65cb\u5f8b\u3002
"},{"location":"chapter_preface/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"\u672c\u9879\u76ee\u65e8\u5728\u521b\u5efa\u4e00\u672c\u5f00\u6e90\u3001\u514d\u8d39\u3001\u65b0\u624b\u53cb\u597d\u7684\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u5165\u95e8\u6559\u7a0b\u3002
\u82e5\u4f60\u662f\u7b97\u6cd5\u521d\u5b66\u8005\uff0c\u4ece\u672a\u63a5\u89e6\u8fc7\u7b97\u6cd5\uff0c\u6216\u8005\u5df2\u7ecf\u6709\u4e00\u4e9b\u5237\u9898\u7ecf\u9a8c\uff0c\u5bf9\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u6709\u6a21\u7cca\u7684\u8ba4\u8bc6\uff0c\u5728\u4f1a\u4e0e\u4e0d\u4f1a\u4e4b\u95f4\u53cd\u590d\u6a2a\u8df3\uff0c\u90a3\u4e48\u672c\u4e66\u6b63\u662f\u4e3a\u4f60\u91cf\u8eab\u5b9a\u5236\u7684\uff01
\u5982\u679c\u4f60\u5df2\u7ecf\u79ef\u7d2f\u4e00\u5b9a\u5237\u9898\u91cf\uff0c\u719f\u6089\u5927\u90e8\u5206\u9898\u578b\uff0c\u90a3\u4e48\u672c\u4e66\u53ef\u52a9\u4f60\u56de\u987e\u4e0e\u68b3\u7406\u7b97\u6cd5\u77e5\u8bc6\u4f53\u7cfb\uff0c\u4ed3\u5e93\u6e90\u4ee3\u7801\u53ef\u4ee5\u5f53\u4f5c\u201c\u5237\u9898\u5de5\u5177\u5e93\u201d\u6216\u201c\u7b97\u6cd5\u5b57\u5178\u201d\u6765\u4f7f\u7528\u3002
\u82e5\u4f60\u662f\u7b97\u6cd5\u5927\u795e\uff0c\u6211\u4eec\u671f\u5f85\u6536\u5230\u4f60\u7684\u5b9d\u8d35\u5efa\u8bae\uff0c\u6216\u8005\u4e00\u8d77\u53c2\u4e0e\u521b\u4f5c\u3002
\u524d\u7f6e\u6761\u4ef6
\u4f60\u9700\u8981\u81f3\u5c11\u5177\u5907\u4efb\u4e00\u8bed\u8a00\u7684\u7f16\u7a0b\u57fa\u7840\uff0c\u80fd\u591f\u9605\u8bfb\u548c\u7f16\u5199\u7b80\u5355\u4ee3\u7801\u3002
"},{"location":"chapter_preface/about_the_book/#012","title":"0.1.2 \u00a0 \u5185\u5bb9\u7ed3\u6784","text":"\u672c\u4e66\u7684\u4e3b\u8981\u5185\u5bb9\u5982\u56fe 0-1 \u6240\u793a\u3002
\u56fe 0-1 \u00a0 \u672c\u4e66\u4e3b\u8981\u5185\u5bb9
"},{"location":"chapter_preface/about_the_book/#013","title":"0.1.3 \u00a0 \u81f4\u8c22","text":"\u5728\u672c\u4e66\u7684\u521b\u4f5c\u8fc7\u7a0b\u4e2d\uff0c\u6211\u5f97\u5230\u4e86\u8bb8\u591a\u4eba\u7684\u5e2e\u52a9\uff0c\u5305\u62ec\u4f46\u4e0d\u9650\u4e8e\uff1a
\u5728\u5199\u4f5c\u8fc7\u7a0b\u4e2d\uff0c\u6211\u9605\u8bfb\u4e86\u8bb8\u591a\u5173\u4e8e\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7684\u6559\u6750\u548c\u6587\u7ae0\u3002\u8fd9\u4e9b\u4f5c\u54c1\u4e3a\u672c\u4e66\u63d0\u4f9b\u4e86\u4f18\u79c0\u7684\u8303\u672c\uff0c\u786e\u4fdd\u4e86\u672c\u4e66\u5185\u5bb9\u7684\u51c6\u786e\u6027\u4e0e\u54c1\u8d28\u3002\u5728\u6b64\u611f\u8c22\u6240\u6709\u8001\u5e08\u548c\u524d\u8f88\u4eec\u7684\u6770\u51fa\u8d21\u732e\uff01
\u672c\u4e66\u5021\u5bfc\u624b\u8111\u5e76\u7528\u7684\u5b66\u4e60\u65b9\u5f0f\uff0c\u5728\u8fd9\u4e00\u70b9\u4e0a\u6df1\u53d7\u300a\u52a8\u624b\u5b66\u6df1\u5ea6\u5b66\u4e60\u300b\u7684\u542f\u53d1\u3002\u5728\u6b64\u5411\u5404\u4f4d\u8bfb\u8005\u5f3a\u70c8\u63a8\u8350\u8fd9\u672c\u4f18\u79c0\u7684\u8457\u4f5c\u3002
\u8877\u5fc3\u611f\u8c22\u6211\u7684\u7236\u6bcd\uff0c\u6b63\u662f\u4f60\u4eec\u4e00\u76f4\u4ee5\u6765\u7684\u652f\u6301\u4e0e\u9f13\u52b1\uff0c\u8ba9\u6211\u6709\u673a\u4f1a\u505a\u8fd9\u4ef6\u5bcc\u6709\u8da3\u5473\u7684\u4e8b\u3002
"},{"location":"chapter_preface/suggestions/","title":"0.2 \u00a0 \u5982\u4f55\u4f7f\u7528\u672c\u4e66","text":"Tip
\u4e3a\u4e86\u83b7\u5f97\u6700\u4f73\u7684\u9605\u8bfb\u4f53\u9a8c\uff0c\u5efa\u8bae\u4f60\u901a\u8bfb\u672c\u8282\u5185\u5bb9\u3002
"},{"location":"chapter_preface/suggestions/#021","title":"0.2.1 \u00a0 \u884c\u6587\u98ce\u683c\u7ea6\u5b9a","text":"*
\u7684\u662f\u9009\u8bfb\u7ae0\u8282\uff0c\u5185\u5bb9\u76f8\u5bf9\u56f0\u96be\u3002\u5982\u679c\u4f60\u7684\u65f6\u95f4\u6709\u9650\uff0c\u53ef\u4ee5\u5148\u8df3\u8fc7\u3002\u300c \u300d
\u62ec\u53f7\u6807\u6ce8\uff0c\u4f8b\u5982 \u300c\u6570\u7ec4 array\u300d
\u3002\u5efa\u8bae\u8bb0\u4f4f\u5b83\u4eec\uff0c\u4ee5\u4fbf\u9605\u8bfb\u6587\u732e\u3002\u201c\u5f15\u53f7\u201d
\u6807\u6ce8\uff0c\u4ee5\u907f\u514d\u6b67\u4e49\u3002\"\"\"\u6807\u9898\u6ce8\u91ca\uff0c\u7528\u4e8e\u6807\u6ce8\u51fd\u6570\u3001\u7c7b\u3001\u6d4b\u8bd5\u6837\u4f8b\u7b49\"\"\"\n\n# \u5185\u5bb9\u6ce8\u91ca\uff0c\u7528\u4e8e\u8be6\u89e3\u4ee3\u7801\n\n\"\"\"\n\u591a\u884c\n\u6ce8\u91ca\n\"\"\"\n
/* \u6807\u9898\u6ce8\u91ca\uff0c\u7528\u4e8e\u6807\u6ce8\u51fd\u6570\u3001\u7c7b\u3001\u6d4b\u8bd5\u6837\u4f8b\u7b49 */\n\n// \u5185\u5bb9\u6ce8\u91ca\uff0c\u7528\u4e8e\u8be6\u89e3\u4ee3\u7801\n\n/**\n * \u591a\u884c\n * \u6ce8\u91ca\n */\n
/* \u6807\u9898\u6ce8\u91ca\uff0c\u7528\u4e8e\u6807\u6ce8\u51fd\u6570\u3001\u7c7b\u3001\u6d4b\u8bd5\u6837\u4f8b\u7b49 */\n\n// \u5185\u5bb9\u6ce8\u91ca\uff0c\u7528\u4e8e\u8be6\u89e3\u4ee3\u7801\n\n/**\n * \u591a\u884c\n * \u6ce8\u91ca\n */\n
/* \u6807\u9898\u6ce8\u91ca\uff0c\u7528\u4e8e\u6807\u6ce8\u51fd\u6570\u3001\u7c7b\u3001\u6d4b\u8bd5\u6837\u4f8b\u7b49 */\n\n// \u5185\u5bb9\u6ce8\u91ca\uff0c\u7528\u4e8e\u8be6\u89e3\u4ee3\u7801\n\n/**\n * \u591a\u884c\n * \u6ce8\u91ca\n */\n
/* \u6807\u9898\u6ce8\u91ca\uff0c\u7528\u4e8e\u6807\u6ce8\u51fd\u6570\u3001\u7c7b\u3001\u6d4b\u8bd5\u6837\u4f8b\u7b49 */\n\n// \u5185\u5bb9\u6ce8\u91ca\uff0c\u7528\u4e8e\u8be6\u89e3\u4ee3\u7801\n\n/**\n * \u591a\u884c\n * \u6ce8\u91ca\n */\n
/* \u6807\u9898\u6ce8\u91ca\uff0c\u7528\u4e8e\u6807\u6ce8\u51fd\u6570\u3001\u7c7b\u3001\u6d4b\u8bd5\u6837\u4f8b\u7b49 */\n\n// \u5185\u5bb9\u6ce8\u91ca\uff0c\u7528\u4e8e\u8be6\u89e3\u4ee3\u7801\n\n/**\n * \u591a\u884c\n * \u6ce8\u91ca\n */\n
/* \u6807\u9898\u6ce8\u91ca\uff0c\u7528\u4e8e\u6807\u6ce8\u51fd\u6570\u3001\u7c7b\u3001\u6d4b\u8bd5\u6837\u4f8b\u7b49 */\n\n// \u5185\u5bb9\u6ce8\u91ca\uff0c\u7528\u4e8e\u8be6\u89e3\u4ee3\u7801\n\n/**\n * \u591a\u884c\n * \u6ce8\u91ca\n */\n
/* \u6807\u9898\u6ce8\u91ca\uff0c\u7528\u4e8e\u6807\u6ce8\u51fd\u6570\u3001\u7c7b\u3001\u6d4b\u8bd5\u6837\u4f8b\u7b49 */\n\n// \u5185\u5bb9\u6ce8\u91ca\uff0c\u7528\u4e8e\u8be6\u89e3\u4ee3\u7801\n\n/**\n * \u591a\u884c\n * \u6ce8\u91ca\n */\n
/* \u6807\u9898\u6ce8\u91ca\uff0c\u7528\u4e8e\u6807\u6ce8\u51fd\u6570\u3001\u7c7b\u3001\u6d4b\u8bd5\u6837\u4f8b\u7b49 */\n\n// \u5185\u5bb9\u6ce8\u91ca\uff0c\u7528\u4e8e\u8be6\u89e3\u4ee3\u7801\n\n/**\n * \u591a\u884c\n * \u6ce8\u91ca\n */\n
/* \u6807\u9898\u6ce8\u91ca\uff0c\u7528\u4e8e\u6807\u6ce8\u51fd\u6570\u3001\u7c7b\u3001\u6d4b\u8bd5\u6837\u4f8b\u7b49 */\n\n// \u5185\u5bb9\u6ce8\u91ca\uff0c\u7528\u4e8e\u8be6\u89e3\u4ee3\u7801\n\n/**\n * \u591a\u884c\n * \u6ce8\u91ca\n */\n
/* \u6807\u9898\u6ce8\u91ca\uff0c\u7528\u4e8e\u6807\u6ce8\u51fd\u6570\u3001\u7c7b\u3001\u6d4b\u8bd5\u6837\u4f8b\u7b49 */\n\n// \u5185\u5bb9\u6ce8\u91ca\uff0c\u7528\u4e8e\u8be6\u89e3\u4ee3\u7801\n\n/**\n * \u591a\u884c\n * \u6ce8\u91ca\n */\n
// \u6807\u9898\u6ce8\u91ca\uff0c\u7528\u4e8e\u6807\u6ce8\u51fd\u6570\u3001\u7c7b\u3001\u6d4b\u8bd5\u6837\u4f8b\u7b49\n\n// \u5185\u5bb9\u6ce8\u91ca\uff0c\u7528\u4e8e\u8be6\u89e3\u4ee3\u7801\n\n// \u591a\u884c\n// \u6ce8\u91ca\n
"},{"location":"chapter_preface/suggestions/#022","title":"0.2.2 \u00a0 \u5728\u52a8\u753b\u56fe\u89e3\u4e2d\u9ad8\u6548\u5b66\u4e60","text":"\u76f8\u8f83\u4e8e\u6587\u5b57\uff0c\u89c6\u9891\u548c\u56fe\u7247\u5177\u6709\u66f4\u9ad8\u7684\u4fe1\u606f\u5bc6\u5ea6\u548c\u7ed3\u6784\u5316\u7a0b\u5ea6\uff0c\u66f4\u6613\u4e8e\u7406\u89e3\u3002\u5728\u672c\u4e66\u4e2d\uff0c\u91cd\u70b9\u548c\u96be\u70b9\u77e5\u8bc6\u5c06\u4e3b\u8981\u901a\u8fc7\u52a8\u753b\u548c\u56fe\u89e3\u5f62\u5f0f\u5c55\u793a\uff0c\u800c\u6587\u5b57\u5219\u4f5c\u4e3a\u52a8\u753b\u548c\u56fe\u7247\u7684\u89e3\u91ca\u4e0e\u8865\u5145\u3002
\u5982\u679c\u4f60\u5728\u9605\u8bfb\u672c\u4e66\u65f6\uff0c\u53d1\u73b0\u67d0\u6bb5\u5185\u5bb9\u63d0\u4f9b\u4e86\u5982\u56fe 0-2 \u6240\u793a\u7684\u52a8\u753b\u6216\u56fe\u89e3\uff0c\u8bf7\u4ee5\u56fe\u4e3a\u4e3b\u3001\u4ee5\u6587\u5b57\u4e3a\u8f85\uff0c\u7efc\u5408\u4e24\u8005\u6765\u7406\u89e3\u5185\u5bb9\u3002
\u56fe 0-2 \u00a0 \u52a8\u753b\u56fe\u89e3\u793a\u4f8b
"},{"location":"chapter_preface/suggestions/#023","title":"0.2.3 \u00a0 \u5728\u4ee3\u7801\u5b9e\u8df5\u4e2d\u52a0\u6df1\u7406\u89e3","text":"\u672c\u4e66\u7684\u914d\u5957\u4ee3\u7801\u6258\u7ba1\u5728 GitHub \u4ed3\u5e93\u3002\u5982\u56fe 0-3 \u6240\u793a\uff0c\u6e90\u4ee3\u7801\u9644\u6709\u6d4b\u8bd5\u6837\u4f8b\uff0c\u53ef\u4e00\u952e\u8fd0\u884c\u3002
\u5982\u679c\u65f6\u95f4\u5141\u8bb8\uff0c\u5efa\u8bae\u4f60\u53c2\u7167\u4ee3\u7801\u81ea\u884c\u6572\u4e00\u904d\u3002\u5982\u679c\u5b66\u4e60\u65f6\u95f4\u6709\u9650\uff0c\u8bf7\u81f3\u5c11\u901a\u8bfb\u5e76\u8fd0\u884c\u6240\u6709\u4ee3\u7801\u3002
\u4e0e\u9605\u8bfb\u4ee3\u7801\u76f8\u6bd4\uff0c\u7f16\u5199\u4ee3\u7801\u7684\u8fc7\u7a0b\u5f80\u5f80\u80fd\u5e26\u6765\u66f4\u591a\u6536\u83b7\u3002\u52a8\u624b\u5b66\uff0c\u624d\u662f\u771f\u7684\u5b66\u3002
\u56fe 0-3 \u00a0 \u8fd0\u884c\u4ee3\u7801\u793a\u4f8b
\u8fd0\u884c\u4ee3\u7801\u7684\u524d\u7f6e\u5de5\u4f5c\u4e3b\u8981\u5206\u4e3a\u4e09\u6b65\u3002
\u7b2c\u4e00\u6b65\uff1a\u5b89\u88c5\u672c\u5730\u7f16\u7a0b\u73af\u5883\u3002\u8bf7\u53c2\u7167\u9644\u5f55\u6240\u793a\u7684\u6559\u7a0b\u8fdb\u884c\u5b89\u88c5\uff0c\u5982\u679c\u5df2\u5b89\u88c5\uff0c\u5219\u53ef\u8df3\u8fc7\u6b64\u6b65\u9aa4\u3002
\u7b2c\u4e8c\u6b65\uff1a\u514b\u9686\u6216\u4e0b\u8f7d\u4ee3\u7801\u4ed3\u5e93\u3002\u524d\u5f80 GitHub \u4ed3\u5e93\u3002
\u5982\u679c\u5df2\u7ecf\u5b89\u88c5 Git \uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u514b\u9686\u672c\u4ed3\u5e93\uff1a
git clone https://github.com/krahets/hello-algo.git\n
\u5f53\u7136\uff0c\u4f60\u4e5f\u53ef\u4ee5\u5728\u56fe 0-4 \u6240\u793a\u7684\u4f4d\u7f6e\uff0c\u70b9\u51fb\u201cDownload ZIP\u201d\u6309\u94ae\u76f4\u63a5\u4e0b\u8f7d\u4ee3\u7801\u538b\u7f29\u5305\uff0c\u7136\u540e\u5728\u672c\u5730\u89e3\u538b\u5373\u53ef\u3002
\u56fe 0-4 \u00a0 \u514b\u9686\u4ed3\u5e93\u4e0e\u4e0b\u8f7d\u4ee3\u7801
\u7b2c\u4e09\u6b65\uff1a\u8fd0\u884c\u6e90\u4ee3\u7801\u3002\u5982\u56fe 0-5 \u6240\u793a\uff0c\u5bf9\u4e8e\u9876\u90e8\u6807\u6709\u6587\u4ef6\u540d\u79f0\u7684\u4ee3\u7801\u5757\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u4ed3\u5e93\u7684 codes
\u6587\u4ef6\u5939\u5185\u627e\u5230\u5bf9\u5e94\u7684\u6e90\u4ee3\u7801\u6587\u4ef6\u3002\u6e90\u4ee3\u7801\u6587\u4ef6\u53ef\u4e00\u952e\u8fd0\u884c\uff0c\u5c06\u5e2e\u52a9\u4f60\u8282\u7701\u4e0d\u5fc5\u8981\u7684\u8c03\u8bd5\u65f6\u95f4\uff0c\u8ba9\u4f60\u80fd\u591f\u4e13\u6ce8\u4e8e\u5b66\u4e60\u5185\u5bb9\u3002
\u56fe 0-5 \u00a0 \u4ee3\u7801\u5757\u4e0e\u5bf9\u5e94\u7684\u6e90\u4ee3\u7801\u6587\u4ef6
"},{"location":"chapter_preface/suggestions/#024","title":"0.2.4 \u00a0 \u5728\u63d0\u95ee\u8ba8\u8bba\u4e2d\u5171\u540c\u6210\u957f","text":"\u5728\u9605\u8bfb\u672c\u4e66\u65f6\uff0c\u8bf7\u4e0d\u8981\u8f7b\u6613\u8df3\u8fc7\u90a3\u4e9b\u6ca1\u5b66\u660e\u767d\u7684\u77e5\u8bc6\u70b9\u3002\u6b22\u8fce\u5728\u8bc4\u8bba\u533a\u63d0\u51fa\u4f60\u7684\u95ee\u9898\uff0c\u6211\u548c\u5c0f\u4f19\u4f34\u4eec\u5c06\u7aed\u8bda\u4e3a\u4f60\u89e3\u7b54\uff0c\u4e00\u822c\u60c5\u51b5\u4e0b\u53ef\u5728\u4e24\u5929\u5185\u56de\u590d\u3002
\u5982\u56fe 0-6 \u6240\u793a\uff0c\u6bcf\u4e2a\u7ae0\u8282\u7684\u5e95\u90e8\u90fd\u914d\u6709\u8bc4\u8bba\u533a\u3002\u5e0c\u671b\u4f60\u80fd\u591a\u5173\u6ce8\u8bc4\u8bba\u533a\u7684\u5185\u5bb9\u3002\u4e00\u65b9\u9762\uff0c\u4f60\u53ef\u4ee5\u4e86\u89e3\u5927\u5bb6\u9047\u5230\u7684\u95ee\u9898\uff0c\u4ece\u800c\u67e5\u6f0f\u8865\u7f3a\uff0c\u6fc0\u53d1\u66f4\u6df1\u5165\u7684\u601d\u8003\u3002\u53e6\u4e00\u65b9\u9762\uff0c\u671f\u5f85\u4f60\u80fd\u6177\u6168\u5730\u56de\u7b54\u5176\u4ed6\u5c0f\u4f19\u4f34\u7684\u95ee\u9898\uff0c\u5206\u4eab\u4f60\u7684\u89c1\u89e3\uff0c\u5e2e\u52a9\u4ed6\u4eba\u8fdb\u6b65\u3002
\u56fe 0-6 \u00a0 \u8bc4\u8bba\u533a\u793a\u4f8b
"},{"location":"chapter_preface/suggestions/#025","title":"0.2.5 \u00a0 \u7b97\u6cd5\u5b66\u4e60\u8def\u7ebf","text":"\u4ece\u603b\u4f53\u4e0a\u770b\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5b66\u4e60\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7684\u8fc7\u7a0b\u5212\u5206\u4e3a\u4e09\u4e2a\u9636\u6bb5\u3002
\u5982\u56fe 0-7 \u6240\u793a\uff0c\u672c\u4e66\u5185\u5bb9\u4e3b\u8981\u6db5\u76d6\u201c\u9636\u6bb5\u4e00\u201d\uff0c\u65e8\u5728\u5e2e\u52a9\u4f60\u66f4\u9ad8\u6548\u5730\u5c55\u5f00\u9636\u6bb5\u4e8c\u548c\u9636\u6bb5\u4e09\u7684\u5b66\u4e60\u3002
\u56fe 0-7 \u00a0 \u7b97\u6cd5\u5b66\u4e60\u8def\u7ebf
"},{"location":"chapter_preface/summary/","title":"0.3 \u00a0 \u5c0f\u7ed3","text":"[1] Thomas H. Cormen, et al. Introduction to Algorithms (3rd Edition).
[2] Aditya Bhargava. Grokking Algorithms: An Illustrated Guide for Programmers and Other Curious People (1st Edition).
[3] \u4e25\u851a\u654f. \u6570\u636e\u7ed3\u6784\uff08C \u8bed\u8a00\u7248\uff09.
[4] \u9093\u4fca\u8f89. \u6570\u636e\u7ed3\u6784\uff08C++ \u8bed\u8a00\u7248\uff0c\u7b2c\u4e09\u7248\uff09.
[5] \u9a6c\u514b \u827e\u4f26 \u7ef4\u65af\u8457\uff0c\u9648\u8d8a\u8bd1. \u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u5206\u6790\uff1aJava\u8bed\u8a00\u63cf\u8ff0\uff08\u7b2c\u4e09\u7248\uff09.
[6] \u7a0b\u6770. \u5927\u8bdd\u6570\u636e\u7ed3\u6784.
[7] \u738b\u4e89. \u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u4e4b\u7f8e.
[8] Gayle Laakmann McDowell. Cracking the Coding Interview: 189 Programming Questions and Solutions (6th Edition).
[9] Aston Zhang, et al. Dive into Deep Learning.
"},{"location":"chapter_searching/","title":"\u7b2c 10 \u7ae0 \u00a0 \u641c\u7d22","text":"Abstract
\u641c\u7d22\u662f\u4e00\u573a\u672a\u77e5\u7684\u5192\u9669\uff0c\u6211\u4eec\u6216\u8bb8\u9700\u8981\u8d70\u904d\u795e\u79d8\u7a7a\u95f4\u7684\u6bcf\u4e2a\u89d2\u843d\uff0c\u53c8\u6216\u8bb8\u53ef\u4ee5\u5feb\u901f\u9501\u5b9a\u76ee\u6807\u3002
\u5728\u8fd9\u573a\u5bfb\u89c5\u4e4b\u65c5\u4e2d\uff0c\u6bcf\u4e00\u6b21\u63a2\u7d22\u90fd\u53ef\u80fd\u5f97\u5230\u4e00\u4e2a\u672a\u66fe\u6599\u60f3\u7684\u7b54\u6848\u3002
"},{"location":"chapter_searching/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"\u300c\u4e8c\u5206\u67e5\u627e binary search\u300d\u662f\u4e00\u79cd\u57fa\u4e8e\u5206\u6cbb\u7b56\u7565\u7684\u9ad8\u6548\u641c\u7d22\u7b97\u6cd5\u3002\u5b83\u5229\u7528\u6570\u636e\u7684\u6709\u5e8f\u6027\uff0c\u6bcf\u8f6e\u7f29\u5c0f\u4e00\u534a\u641c\u7d22\u8303\u56f4\uff0c\u76f4\u81f3\u627e\u5230\u76ee\u6807\u5143\u7d20\u6216\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u4e3a\u6b62\u3002
Question
\u7ed9\u5b9a\u4e00\u4e2a\u957f\u5ea6\u4e3a \\(n\\) \u7684\u6570\u7ec4 nums
\uff0c\u5143\u7d20\u6309\u4ece\u5c0f\u5230\u5927\u7684\u987a\u5e8f\u6392\u5217\u4e14\u4e0d\u91cd\u590d\u3002\u8bf7\u67e5\u627e\u5e76\u8fd4\u56de\u5143\u7d20 target
\u5728\u8be5\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15\u3002\u82e5\u6570\u7ec4\u4e0d\u5305\u542b\u8be5\u5143\u7d20\uff0c\u5219\u8fd4\u56de \\(-1\\) \u3002\u793a\u4f8b\u5982\u56fe 10-1 \u6240\u793a\u3002
\u56fe 10-1 \u00a0 \u4e8c\u5206\u67e5\u627e\u793a\u4f8b\u6570\u636e
\u5982\u56fe 10-2 \u6240\u793a\uff0c\u6211\u4eec\u5148\u521d\u59cb\u5316\u6307\u9488 \\(i = 0\\) \u548c \\(j = n - 1\\) \uff0c\u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u548c\u5c3e\u5143\u7d20\uff0c\u4ee3\u8868\u641c\u7d22\u533a\u95f4 \\([0, n - 1]\\) \u3002\u8bf7\u6ce8\u610f\uff0c\u4e2d\u62ec\u53f7\u8868\u793a\u95ed\u533a\u95f4\uff0c\u5176\u5305\u542b\u8fb9\u754c\u503c\u672c\u8eab\u3002
\u63a5\u4e0b\u6765\uff0c\u5faa\u73af\u6267\u884c\u4ee5\u4e0b\u4e24\u6b65\u3002
nums[m]
\u548c target
\u7684\u5927\u5c0f\u5173\u7cfb\uff0c\u5206\u4e3a\u4ee5\u4e0b\u4e09\u79cd\u60c5\u51b5\u3002nums[m] < target
\u65f6\uff0c\u8bf4\u660e target
\u5728\u533a\u95f4 \\([m + 1, j]\\) \u4e2d\uff0c\u56e0\u6b64\u6267\u884c \\(i = m + 1\\) \u3002nums[m] > target
\u65f6\uff0c\u8bf4\u660e target
\u5728\u533a\u95f4 \\([i, m - 1]\\) \u4e2d\uff0c\u56e0\u6b64\u6267\u884c \\(j = m - 1\\) \u3002nums[m] = target
\u65f6\uff0c\u8bf4\u660e\u627e\u5230 target
\uff0c\u56e0\u6b64\u8fd4\u56de\u7d22\u5f15 \\(m\\) \u3002\u82e5\u6570\u7ec4\u4e0d\u5305\u542b\u76ee\u6807\u5143\u7d20\uff0c\u641c\u7d22\u533a\u95f4\u6700\u7ec8\u4f1a\u7f29\u5c0f\u4e3a\u7a7a\u3002\u6b64\u65f6\u8fd4\u56de \\(-1\\) \u3002
<1><2><3><4><5><6><7>\u56fe 10-2 \u00a0 \u4e8c\u5206\u67e5\u627e\u6d41\u7a0b
\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u7531\u4e8e \\(i\\) \u548c \\(j\\) \u90fd\u662f int
\u7c7b\u578b\uff0c\u56e0\u6b64 \\(i + j\\) \u53ef\u80fd\u4f1a\u8d85\u51fa int
\u7c7b\u578b\u7684\u53d6\u503c\u8303\u56f4\u3002\u4e3a\u4e86\u907f\u514d\u5927\u6570\u8d8a\u754c\uff0c\u6211\u4eec\u901a\u5e38\u91c7\u7528\u516c\u5f0f \\(m = \\lfloor {i + (j - i) / 2} \\rfloor\\) \u6765\u8ba1\u7b97\u4e2d\u70b9\u3002
\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig binary_search.pydef binary_search(nums: list[int], target: int) -> int:\n \"\"\"\u4e8c\u5206\u67e5\u627e\uff08\u53cc\u95ed\u533a\u95f4\uff09\"\"\"\n # \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1] \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\n i, j = 0, len(nums) - 1\n # \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i > j \u65f6\u4e3a\u7a7a\uff09\n while i <= j:\n # \u7406\u8bba\u4e0a Python \u7684\u6570\u5b57\u53ef\u4ee5\u65e0\u9650\u5927\uff08\u53d6\u51b3\u4e8e\u5185\u5b58\u5927\u5c0f\uff09\uff0c\u65e0\u987b\u8003\u8651\u5927\u6570\u8d8a\u754c\u95ee\u9898\n m = (i + j) // 2 # \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n if nums[m] < target:\n i = m + 1 # \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n elif nums[m] > target:\n j = m - 1 # \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n else:\n return m # \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return -1 # \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n
binary_search.cpp/* \u4e8c\u5206\u67e5\u627e\uff08\u53cc\u95ed\u533a\u95f4\uff09 */\nint binarySearch(vector<int> &nums, int target) {\n // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1] \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\n int i = 0, j = nums.size() - 1;\n // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i > j \u65f6\u4e3a\u7a7a\uff09\n while (i <= j) {\n int m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n if (nums[m] < target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n i = m + 1;\n else if (nums[m] > target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n j = m - 1;\n else // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m;\n }\n // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n return -1;\n}\n
binary_search.java/* \u4e8c\u5206\u67e5\u627e\uff08\u53cc\u95ed\u533a\u95f4\uff09 */\nint binarySearch(int[] nums, int target) {\n // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1] \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\n int i = 0, j = nums.length - 1;\n // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i > j \u65f6\u4e3a\u7a7a\uff09\n while (i <= j) {\n int m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n if (nums[m] < target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n i = m + 1;\n else if (nums[m] > target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n j = m - 1;\n else // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m;\n }\n // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n return -1;\n}\n
binary_search.cs/* \u4e8c\u5206\u67e5\u627e\uff08\u53cc\u95ed\u533a\u95f4\uff09 */\nint BinarySearch(int[] nums, int target) {\n // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1] \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\n int i = 0, j = nums.Length - 1;\n // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i > j \u65f6\u4e3a\u7a7a\uff09\n while (i <= j) {\n int m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n if (nums[m] < target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n i = m + 1;\n else if (nums[m] > target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n j = m - 1;\n else // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m;\n }\n // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n return -1;\n}\n
binary_search.go/* \u4e8c\u5206\u67e5\u627e\uff08\u53cc\u95ed\u533a\u95f4\uff09 */\nfunc binarySearch(nums []int, target int) int {\n // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1] \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\n i, j := 0, len(nums)-1\n // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i > j \u65f6\u4e3a\u7a7a\uff09\n for i <= j {\n m := i + (j-i)/2 // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n if nums[m] < target { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n i = m + 1\n } else if nums[m] > target { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n j = m - 1\n } else { // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m\n }\n }\n // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n return -1\n}\n
binary_search.swift/* \u4e8c\u5206\u67e5\u627e\uff08\u53cc\u95ed\u533a\u95f4\uff09 */\nfunc binarySearch(nums: [Int], target: Int) -> Int {\n // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1] \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\n var i = 0\n var j = nums.count - 1\n // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i > j \u65f6\u4e3a\u7a7a\uff09\n while i <= j {\n let m = i + (j - i) / 2 // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n if nums[m] < target { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n i = m + 1\n } else if nums[m] > target { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n j = m - 1\n } else { // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m\n }\n }\n // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n return -1\n}\n
binary_search.js/* \u4e8c\u5206\u67e5\u627e\uff08\u53cc\u95ed\u533a\u95f4\uff09 */\nfunction binarySearch(nums, target) {\n // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1] \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\n let i = 0,\n j = nums.length - 1;\n // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i > j \u65f6\u4e3a\u7a7a\uff09\n while (i <= j) {\n // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m \uff0c\u4f7f\u7528 parseInt() \u5411\u4e0b\u53d6\u6574\n const m = parseInt(i + (j - i) / 2);\n if (nums[m] < target)\n // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n i = m + 1;\n else if (nums[m] > target)\n // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n j = m - 1;\n else return m; // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n }\n // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n return -1;\n}\n
binary_search.ts/* \u4e8c\u5206\u67e5\u627e\uff08\u53cc\u95ed\u533a\u95f4\uff09 */\nfunction binarySearch(nums: number[], target: number): number {\n // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1] \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\n let i = 0,\n j = nums.length - 1;\n // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i > j \u65f6\u4e3a\u7a7a\uff09\n while (i <= j) {\n // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n const m = Math.floor(i + (j - i) / 2);\n if (nums[m] < target) {\n // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n i = m + 1;\n } else if (nums[m] > target) {\n // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n j = m - 1;\n } else {\n // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m;\n }\n }\n return -1; // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n}\n
binary_search.dart/* \u4e8c\u5206\u67e5\u627e\uff08\u53cc\u95ed\u533a\u95f4\uff09 */\nint binarySearch(List<int> nums, int target) {\n // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1] \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\n int i = 0, j = nums.length - 1;\n // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i > j \u65f6\u4e3a\u7a7a\uff09\n while (i <= j) {\n int m = i + (j - i) ~/ 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n if (nums[m] < target) {\n // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n i = m + 1;\n } else if (nums[m] > target) {\n // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n j = m - 1;\n } else {\n // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m;\n }\n }\n // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n return -1;\n}\n
binary_search.rs/* \u4e8c\u5206\u67e5\u627e\uff08\u53cc\u95ed\u533a\u95f4\uff09 */\nfn binary_search(nums: &[i32], target: i32) -> i32 {\n // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1] \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\n let mut i = 0;\n let mut j = nums.len() as i32 - 1;\n // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i > j \u65f6\u4e3a\u7a7a\uff09\n while i <= j {\n let m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n if nums[m as usize] < target { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n i = m + 1;\n } else if nums[m as usize] > target { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n j = m - 1;\n } else { // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m;\n } \n }\n // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n return -1;\n}\n
binary_search.c/* \u4e8c\u5206\u67e5\u627e\uff08\u53cc\u95ed\u533a\u95f4\uff09 */\nint binarySearch(int *nums, int len, int target) {\n // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1] \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\n int i = 0, j = len - 1;\n // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i > j \u65f6\u4e3a\u7a7a\uff09\n while (i <= j) {\n int m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n if (nums[m] < target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n i = m + 1;\n else if (nums[m] > target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n j = m - 1;\n else // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m;\n }\n // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n return -1;\n}\n
binary_search.zig// \u4e8c\u5206\u67e5\u627e\uff08\u53cc\u95ed\u533a\u95f4\uff09\nfn binarySearch(comptime T: type, nums: std.ArrayList(T), target: T) T {\n // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1] \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\n var i: usize = 0;\n var j: usize = nums.items.len - 1;\n // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i > j \u65f6\u4e3a\u7a7a\uff09\n while (i <= j) {\n var m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n if (nums.items[m] < target) { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n i = m + 1;\n } else if (nums.items[m] > target) { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n j = m - 1;\n } else { // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return @intCast(m);\n }\n }\n // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n return -1;\n}\n
\u65f6\u95f4\u590d\u6742\u5ea6 \\(O(\\log n)\\) \uff1a\u5728\u4e8c\u5206\u5faa\u73af\u4e2d\uff0c\u533a\u95f4\u6bcf\u8f6e\u7f29\u5c0f\u4e00\u534a\uff0c\u5faa\u73af\u6b21\u6570\u4e3a \\(\\log_2 n\\) \u3002
\u7a7a\u95f4\u590d\u6742\u5ea6 \\(O(1)\\) \uff1a\u6307\u9488 \\(i\\) \u548c \\(j\\) \u4f7f\u7528\u5e38\u6570\u5927\u5c0f\u7a7a\u95f4\u3002
"},{"location":"chapter_searching/binary_search/#1011","title":"10.1.1 \u00a0 \u533a\u95f4\u8868\u793a\u65b9\u6cd5","text":"\u9664\u4e86\u4e0a\u8ff0\u53cc\u95ed\u533a\u95f4\u5916\uff0c\u5e38\u89c1\u7684\u533a\u95f4\u8868\u793a\u8fd8\u6709\u201c\u5de6\u95ed\u53f3\u5f00\u201d\u533a\u95f4\uff0c\u5b9a\u4e49\u4e3a \\([0, n)\\) \uff0c\u5373\u5de6\u8fb9\u754c\u5305\u542b\u81ea\u8eab\uff0c\u53f3\u8fb9\u754c\u4e0d\u5305\u542b\u81ea\u8eab\u3002\u5728\u8be5\u8868\u793a\u4e0b\uff0c\u533a\u95f4 \\([i, j)\\) \u5728 \\(i = j\\) \u65f6\u4e3a\u7a7a\u3002
\u6211\u4eec\u53ef\u4ee5\u57fa\u4e8e\u8be5\u8868\u793a\u5b9e\u73b0\u5177\u6709\u76f8\u540c\u529f\u80fd\u7684\u4e8c\u5206\u67e5\u627e\u7b97\u6cd5\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig binary_search.pydef binary_search_lcro(nums: list[int], target: int) -> int:\n \"\"\"\u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\u533a\u95f4\uff09\"\"\"\n # \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00\u533a\u95f4 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\n i, j = 0, len(nums)\n # \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i = j \u65f6\u4e3a\u7a7a\uff09\n while i < j:\n m = (i + j) // 2 # \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n if nums[m] < target:\n i = m + 1 # \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\n elif nums[m] > target:\n j = m # \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\n else:\n return m # \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return -1 # \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n
binary_search.cpp/* \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\u533a\u95f4\uff09 */\nint binarySearchLCRO(vector<int> &nums, int target) {\n // \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00\u533a\u95f4 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\n int i = 0, j = nums.size();\n // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i = j \u65f6\u4e3a\u7a7a\uff09\n while (i < j) {\n int m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n if (nums[m] < target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\n i = m + 1;\n else if (nums[m] > target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\n j = m;\n else // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m;\n }\n // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n return -1;\n}\n
binary_search.java/* \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\u533a\u95f4\uff09 */\nint binarySearchLCRO(int[] nums, int target) {\n // \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00\u533a\u95f4 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\n int i = 0, j = nums.length;\n // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i = j \u65f6\u4e3a\u7a7a\uff09\n while (i < j) {\n int m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n if (nums[m] < target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\n i = m + 1;\n else if (nums[m] > target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\n j = m;\n else // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m;\n }\n // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n return -1;\n}\n
binary_search.cs/* \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\u533a\u95f4\uff09 */\nint BinarySearchLCRO(int[] nums, int target) {\n // \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00\u533a\u95f4 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\n int i = 0, j = nums.Length;\n // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i = j \u65f6\u4e3a\u7a7a\uff09\n while (i < j) {\n int m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n if (nums[m] < target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\n i = m + 1;\n else if (nums[m] > target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\n j = m;\n else // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m;\n }\n // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n return -1;\n}\n
binary_search.go/* \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\u533a\u95f4\uff09 */\nfunc binarySearchLCRO(nums []int, target int) int {\n // \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00\u533a\u95f4 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\n i, j := 0, len(nums)\n // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i = j \u65f6\u4e3a\u7a7a\uff09\n for i < j {\n m := i + (j-i)/2 // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n if nums[m] < target { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\n i = m + 1\n } else if nums[m] > target { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\n j = m\n } else { // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m\n }\n }\n // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n return -1\n}\n
binary_search.swift/* \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\u533a\u95f4\uff09 */\nfunc binarySearchLCRO(nums: [Int], target: Int) -> Int {\n // \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00\u533a\u95f4 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\n var i = 0\n var j = nums.count\n // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i = j \u65f6\u4e3a\u7a7a\uff09\n while i < j {\n let m = i + (j - i) / 2 // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n if nums[m] < target { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\n i = m + 1\n } else if nums[m] > target { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\n j = m\n } else { // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m\n }\n }\n // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n return -1\n}\n
binary_search.js/* \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\u533a\u95f4\uff09 */\nfunction binarySearchLCRO(nums, target) {\n // \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00\u533a\u95f4 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\n let i = 0,\n j = nums.length;\n // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i = j \u65f6\u4e3a\u7a7a\uff09\n while (i < j) {\n // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m \uff0c\u4f7f\u7528 parseInt() \u5411\u4e0b\u53d6\u6574\n const m = parseInt(i + (j - i) / 2);\n if (nums[m] < target)\n // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\n i = m + 1;\n else if (nums[m] > target)\n // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\n j = m;\n // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n else return m;\n }\n // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n return -1;\n}\n
binary_search.ts/* \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\u533a\u95f4\uff09 */\nfunction binarySearchLCRO(nums: number[], target: number): number {\n // \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00\u533a\u95f4 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\n let i = 0,\n j = nums.length;\n // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i = j \u65f6\u4e3a\u7a7a\uff09\n while (i < j) {\n // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n const m = Math.floor(i + (j - i) / 2);\n if (nums[m] < target) {\n // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\n i = m + 1;\n } else if (nums[m] > target) {\n // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\n j = m;\n } else {\n // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m;\n }\n }\n return -1; // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n}\n
binary_search.dart/* \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\u533a\u95f4\uff09 */\nint binarySearchLCRO(List<int> nums, int target) {\n // \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00\u533a\u95f4 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\n int i = 0, j = nums.length;\n // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i = j \u65f6\u4e3a\u7a7a\uff09\n while (i < j) {\n int m = i + (j - i) ~/ 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n if (nums[m] < target) {\n // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\n i = m + 1;\n } else if (nums[m] > target) {\n // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\n j = m;\n } else {\n // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m;\n }\n }\n // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n return -1;\n}\n
binary_search.rs/* \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\u533a\u95f4\uff09 */\nfn binary_search_lcro(nums: &[i32], target: i32) -> i32 {\n // \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00\u533a\u95f4 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\n let mut i = 0;\n let mut j = nums.len() as i32;\n // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i = j \u65f6\u4e3a\u7a7a\uff09\n while i < j {\n let m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n if nums[m as usize] < target { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\n i = m + 1;\n } else if nums[m as usize] > target { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\n j = m - 1;\n } else { // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m;\n } \n }\n // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n return -1;\n}\n
binary_search.c/* \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\u533a\u95f4\uff09 */\nint binarySearchLCRO(int *nums, int len, int target) {\n // \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00\u533a\u95f4 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\n int i = 0, j = len;\n // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i = j \u65f6\u4e3a\u7a7a\uff09\n while (i < j) {\n int m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n if (nums[m] < target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\n i = m + 1;\n else if (nums[m] > target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\n j = m;\n else // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m;\n }\n // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n return -1;\n}\n
binary_search.zig// \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\u533a\u95f4\uff09\nfn binarySearchLCRO(comptime T: type, nums: std.ArrayList(T), target: T) T {\n // \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00\u533a\u95f4 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\n var i: usize = 0;\n var j: usize = nums.items.len;\n // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i = j \u65f6\u4e3a\u7a7a\uff09\n while (i <= j) {\n var m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n if (nums.items[m] < target) { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\n i = m + 1;\n } else if (nums.items[m] > target) { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\n j = m;\n } else { // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return @intCast(m);\n }\n }\n // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n return -1;\n}\n
\u5982\u56fe 10-3 \u6240\u793a\uff0c\u5728\u4e24\u79cd\u533a\u95f4\u8868\u793a\u4e0b\uff0c\u4e8c\u5206\u67e5\u627e\u7b97\u6cd5\u7684\u521d\u59cb\u5316\u3001\u5faa\u73af\u6761\u4ef6\u548c\u7f29\u5c0f\u533a\u95f4\u64cd\u4f5c\u7686\u6709\u6240\u4e0d\u540c\u3002
\u7531\u4e8e\u201c\u53cc\u95ed\u533a\u95f4\u201d\u8868\u793a\u4e2d\u7684\u5de6\u53f3\u8fb9\u754c\u90fd\u88ab\u5b9a\u4e49\u4e3a\u95ed\u533a\u95f4\uff0c\u56e0\u6b64\u901a\u8fc7\u6307\u9488 \\(i\\) \u548c\u6307\u9488 \\(j\\) \u7f29\u5c0f\u533a\u95f4\u7684\u64cd\u4f5c\u4e5f\u662f\u5bf9\u79f0\u7684\u3002\u8fd9\u6837\u66f4\u4e0d\u5bb9\u6613\u51fa\u9519\uff0c\u56e0\u6b64\u4e00\u822c\u5efa\u8bae\u91c7\u7528\u201c\u53cc\u95ed\u533a\u95f4\u201d\u7684\u5199\u6cd5\u3002
\u56fe 10-3 \u00a0 \u4e24\u79cd\u533a\u95f4\u5b9a\u4e49
"},{"location":"chapter_searching/binary_search/#1012","title":"10.1.2 \u00a0 \u4f18\u70b9\u4e0e\u5c40\u9650\u6027","text":"\u4e8c\u5206\u67e5\u627e\u5728\u65f6\u95f4\u548c\u7a7a\u95f4\u65b9\u9762\u90fd\u6709\u8f83\u597d\u7684\u6027\u80fd\u3002
\u7136\u800c\uff0c\u4e8c\u5206\u67e5\u627e\u5e76\u975e\u9002\u7528\u4e8e\u6240\u6709\u60c5\u51b5\uff0c\u4e3b\u8981\u6709\u4ee5\u4e0b\u539f\u56e0\u3002
Question
\u7ed9\u5b9a\u4e00\u4e2a\u957f\u5ea6\u4e3a \\(n\\) \u7684\u6709\u5e8f\u6570\u7ec4 nums
\uff0c\u5176\u4e2d\u53ef\u80fd\u5305\u542b\u91cd\u590d\u5143\u7d20\u3002\u8bf7\u8fd4\u56de\u6570\u7ec4\u4e2d\u6700\u5de6\u4e00\u4e2a\u5143\u7d20 target
\u7684\u7d22\u5f15\u3002\u82e5\u6570\u7ec4\u4e2d\u4e0d\u5305\u542b\u8be5\u5143\u7d20\uff0c\u5219\u8fd4\u56de \\(-1\\) \u3002
\u56de\u5fc6\u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\u7684\u65b9\u6cd5\uff0c\u641c\u7d22\u5b8c\u6210\u540e \\(i\\) \u6307\u5411\u6700\u5de6\u4e00\u4e2a target
\uff0c\u56e0\u6b64\u67e5\u627e\u63d2\u5165\u70b9\u672c\u8d28\u4e0a\u662f\u5728\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target
\u7684\u7d22\u5f15\u3002
\u8003\u8651\u901a\u8fc7\u67e5\u627e\u63d2\u5165\u70b9\u7684\u51fd\u6570\u5b9e\u73b0\u67e5\u627e\u5de6\u8fb9\u754c\u3002\u8bf7\u6ce8\u610f\uff0c\u6570\u7ec4\u4e2d\u53ef\u80fd\u4e0d\u5305\u542b target
\uff0c\u8fd9\u79cd\u60c5\u51b5\u53ef\u80fd\u5bfc\u81f4\u4ee5\u4e0b\u4e24\u79cd\u7ed3\u679c\u3002
nums[i]
\u4e0e target
\u4e0d\u76f8\u7b49\u3002\u5f53\u9047\u5230\u4ee5\u4e0a\u4e24\u79cd\u60c5\u51b5\u65f6\uff0c\u76f4\u63a5\u8fd4\u56de \\(-1\\) \u5373\u53ef\u3002\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig binary_search_edge.pydef binary_search_left_edge(nums: list[int], target: int) -> int:\n \"\"\"\u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target\"\"\"\n # \u7b49\u4ef7\u4e8e\u67e5\u627e target \u7684\u63d2\u5165\u70b9\n i = binary_search_insertion(nums, target)\n # \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n if i == len(nums) or nums[i] != target:\n return -1\n # \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\n return i\n
binary_search_edge.cpp/* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target */\nint binarySearchLeftEdge(vector<int> &nums, int target) {\n // \u7b49\u4ef7\u4e8e\u67e5\u627e target \u7684\u63d2\u5165\u70b9\n int i = binarySearchInsertion(nums, target);\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n if (i == nums.size() || nums[i] != target) {\n return -1;\n }\n // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\n return i;\n}\n
binary_search_edge.java/* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target */\nint binarySearchLeftEdge(int[] nums, int target) {\n // \u7b49\u4ef7\u4e8e\u67e5\u627e target \u7684\u63d2\u5165\u70b9\n int i = binary_search_insertion.binarySearchInsertion(nums, target);\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n if (i == nums.length || nums[i] != target) {\n return -1;\n }\n // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\n return i;\n}\n
binary_search_edge.cs/* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target */\nint BinarySearchLeftEdge(int[] nums, int target) {\n // \u7b49\u4ef7\u4e8e\u67e5\u627e target \u7684\u63d2\u5165\u70b9\n int i = binary_search_insertion.BinarySearchInsertion(nums, target);\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n if (i == nums.Length || nums[i] != target) {\n return -1;\n }\n // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\n return i;\n}\n
binary_search_edge.go/* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target */\nfunc binarySearchLeftEdge(nums []int, target int) int {\n // \u7b49\u4ef7\u4e8e\u67e5\u627e target \u7684\u63d2\u5165\u70b9\n i := binarySearchInsertion(nums, target)\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n if i == len(nums) || nums[i] != target {\n return -1\n }\n // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\n return i\n}\n
binary_search_edge.swift/* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target */\nfunc binarySearchLeftEdge(nums: [Int], target: Int) -> Int {\n // \u7b49\u4ef7\u4e8e\u67e5\u627e target \u7684\u63d2\u5165\u70b9\n let i = binarySearchInsertion(nums: nums, target: target)\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n if i == nums.count || nums[i] != target {\n return -1\n }\n // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\n return i\n}\n
binary_search_edge.js/* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target */\nfunction binarySearchLeftEdge(nums, target) {\n // \u7b49\u4ef7\u4e8e\u67e5\u627e target \u7684\u63d2\u5165\u70b9\n const i = binarySearchInsertion(nums, target);\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n if (i === nums.length || nums[i] !== target) {\n return -1;\n }\n // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\n return i;\n}\n
binary_search_edge.ts/* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target */\nfunction binarySearchLeftEdge(nums: Array<number>, target: number): number {\n // \u7b49\u4ef7\u4e8e\u67e5\u627e target \u7684\u63d2\u5165\u70b9\n const i = binarySearchInsertion(nums, target);\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n if (i === nums.length || nums[i] !== target) {\n return -1;\n }\n // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\n return i;\n}\n
binary_search_edge.dart/* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target */\nint binarySearchLeftEdge(List<int> nums, int target) {\n // \u7b49\u4ef7\u4e8e\u67e5\u627e target \u7684\u63d2\u5165\u70b9\n int i = binarySearchInsertion(nums, target);\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n if (i == nums.length || nums[i] != target) {\n return -1;\n }\n // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\n return i;\n}\n
binary_search_edge.rs/* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target */\nfn binary_search_left_edge(nums: &[i32], target: i32) -> i32 {\n // \u7b49\u4ef7\u4e8e\u67e5\u627e target \u7684\u63d2\u5165\u70b9\n let i = binary_search_insertion(nums, target);\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n if i == nums.len() as i32 || nums[i as usize] != target {\n return -1;\n }\n // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\n i\n}\n
binary_search_edge.c/* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target */\nint binarySearchLeftEdge(int *nums, int numSize, int target) {\n // \u7b49\u4ef7\u4e8e\u67e5\u627e target \u7684\u63d2\u5165\u70b9\n int i = binarySearchInsertion(nums, numSize, target);\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n if (i == numSize || nums[i] != target) {\n return -1;\n }\n // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\n return i;\n}\n
binary_search_edge.zig[class]{}-[func]{binarySearchLeftEdge}\n
"},{"location":"chapter_searching/binary_search_edge/#1032","title":"10.3.2 \u00a0 \u67e5\u627e\u53f3\u8fb9\u754c","text":"\u90a3\u4e48\u5982\u4f55\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target
\u5462\uff1f\u6700\u76f4\u63a5\u7684\u65b9\u5f0f\u662f\u4fee\u6539\u4ee3\u7801\uff0c\u66ff\u6362\u5728 nums[m] == target
\u60c5\u51b5\u4e0b\u7684\u6307\u9488\u6536\u7f29\u64cd\u4f5c\u3002\u4ee3\u7801\u5728\u6b64\u7701\u7565\uff0c\u6709\u5174\u8da3\u7684\u8bfb\u8005\u53ef\u4ee5\u81ea\u884c\u5b9e\u73b0\u3002
\u4e0b\u9762\u6211\u4eec\u4ecb\u7ecd\u4e24\u79cd\u66f4\u52a0\u53d6\u5de7\u7684\u65b9\u6cd5\u3002
"},{"location":"chapter_searching/binary_search_edge/#1","title":"1. \u00a0 \u590d\u7528\u67e5\u627e\u5de6\u8fb9\u754c","text":"\u5b9e\u9645\u4e0a\uff0c\u6211\u4eec\u53ef\u4ee5\u5229\u7528\u67e5\u627e\u6700\u5de6\u5143\u7d20\u7684\u51fd\u6570\u6765\u67e5\u627e\u6700\u53f3\u5143\u7d20\uff0c\u5177\u4f53\u65b9\u6cd5\u4e3a\uff1a\u5c06\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target
\u8f6c\u5316\u4e3a\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target + 1
\u3002
\u5982\u56fe 10-7 \u6240\u793a\uff0c\u67e5\u627e\u5b8c\u6210\u540e\uff0c\u6307\u9488 \\(i\\) \u6307\u5411\u6700\u5de6\u4e00\u4e2a target + 1
\uff08\u5982\u679c\u5b58\u5728\uff09\uff0c\u800c \\(j\\) \u6307\u5411\u6700\u53f3\u4e00\u4e2a target
\uff0c\u56e0\u6b64\u8fd4\u56de \\(j\\) \u5373\u53ef\u3002
\u56fe 10-7 \u00a0 \u5c06\u67e5\u627e\u53f3\u8fb9\u754c\u8f6c\u5316\u4e3a\u67e5\u627e\u5de6\u8fb9\u754c
\u8bf7\u6ce8\u610f\uff0c\u8fd4\u56de\u7684\u63d2\u5165\u70b9\u662f \\(i\\) \uff0c\u56e0\u6b64\u9700\u8981\u5c06\u5176\u51cf \\(1\\) \uff0c\u4ece\u800c\u83b7\u5f97 \\(j\\) \uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig binary_search_edge.pydef binary_search_right_edge(nums: list[int], target: int) -> int:\n \"\"\"\u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target\"\"\"\n # \u8f6c\u5316\u4e3a\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target + 1\n i = binary_search_insertion(nums, target + 1)\n # j \u6307\u5411\u6700\u53f3\u4e00\u4e2a target \uff0ci \u6307\u5411\u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\n j = i - 1\n # \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n if j == -1 or nums[j] != target:\n return -1\n # \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\n return j\n
binary_search_edge.cpp/* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target */\nint binarySearchRightEdge(vector<int> &nums, int target) {\n // \u8f6c\u5316\u4e3a\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target + 1\n int i = binarySearchInsertion(nums, target + 1);\n // j \u6307\u5411\u6700\u53f3\u4e00\u4e2a target \uff0ci \u6307\u5411\u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\n int j = i - 1;\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n if (j == -1 || nums[j] != target) {\n return -1;\n }\n // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\n return j;\n}\n
binary_search_edge.java/* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target */\nint binarySearchRightEdge(int[] nums, int target) {\n // \u8f6c\u5316\u4e3a\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target + 1\n int i = binary_search_insertion.binarySearchInsertion(nums, target + 1);\n // j \u6307\u5411\u6700\u53f3\u4e00\u4e2a target \uff0ci \u6307\u5411\u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\n int j = i - 1;\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n if (j == -1 || nums[j] != target) {\n return -1;\n }\n // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\n return j;\n}\n
binary_search_edge.cs/* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target */\nint BinarySearchRightEdge(int[] nums, int target) {\n // \u8f6c\u5316\u4e3a\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target + 1\n int i = binary_search_insertion.BinarySearchInsertion(nums, target + 1);\n // j \u6307\u5411\u6700\u53f3\u4e00\u4e2a target \uff0ci \u6307\u5411\u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\n int j = i - 1;\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n if (j == -1 || nums[j] != target) {\n return -1;\n }\n // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\n return j;\n}\n
binary_search_edge.go/* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target */\nfunc binarySearchRightEdge(nums []int, target int) int {\n // \u8f6c\u5316\u4e3a\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target + 1\n i := binarySearchInsertion(nums, target+1)\n // j \u6307\u5411\u6700\u53f3\u4e00\u4e2a target \uff0ci \u6307\u5411\u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\n j := i - 1\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n if j == -1 || nums[j] != target {\n return -1\n }\n // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\n return j\n}\n
binary_search_edge.swift/* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target */\nfunc binarySearchRightEdge(nums: [Int], target: Int) -> Int {\n // \u8f6c\u5316\u4e3a\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target + 1\n let i = binarySearchInsertion(nums: nums, target: target + 1)\n // j \u6307\u5411\u6700\u53f3\u4e00\u4e2a target \uff0ci \u6307\u5411\u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\n let j = i - 1\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n if j == -1 || nums[j] != target {\n return -1\n }\n // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\n return j\n}\n
binary_search_edge.js/* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target */\nfunction binarySearchRightEdge(nums, target) {\n // \u8f6c\u5316\u4e3a\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target + 1\n const i = binarySearchInsertion(nums, target + 1);\n // j \u6307\u5411\u6700\u53f3\u4e00\u4e2a target \uff0ci \u6307\u5411\u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\n const j = i - 1;\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n if (j === -1 || nums[j] !== target) {\n return -1;\n }\n // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\n return j;\n}\n
binary_search_edge.ts/* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target */\nfunction binarySearchRightEdge(nums: Array<number>, target: number): number {\n // \u8f6c\u5316\u4e3a\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target + 1\n const i = binarySearchInsertion(nums, target + 1);\n // j \u6307\u5411\u6700\u53f3\u4e00\u4e2a target \uff0ci \u6307\u5411\u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\n const j = i - 1;\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n if (j === -1 || nums[j] !== target) {\n return -1;\n }\n // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\n return j;\n}\n
binary_search_edge.dart/* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target */\nint binarySearchRightEdge(List<int> nums, int target) {\n // \u8f6c\u5316\u4e3a\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target + 1\n int i = binarySearchInsertion(nums, target + 1);\n // j \u6307\u5411\u6700\u53f3\u4e00\u4e2a target \uff0ci \u6307\u5411\u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\n int j = i - 1;\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n if (j == -1 || nums[j] != target) {\n return -1;\n }\n // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\n return j;\n}\n
binary_search_edge.rs/* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target */\nfn binary_search_right_edge(nums: &[i32], target: i32) -> i32 {\n // \u8f6c\u5316\u4e3a\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target + 1\n let i = binary_search_insertion(nums, target + 1);\n // j \u6307\u5411\u6700\u53f3\u4e00\u4e2a target \uff0ci \u6307\u5411\u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\n let j = i - 1;\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n if j == -1 || nums[j as usize] != target {\n return -1;\n }\n // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\n j\n}\n
binary_search_edge.c/* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target */\nint binarySearchRightEdge(int *nums, int numSize, int target) {\n // \u8f6c\u5316\u4e3a\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target + 1\n int i = binarySearchInsertion(nums, numSize, target + 1);\n // j \u6307\u5411\u6700\u53f3\u4e00\u4e2a target \uff0ci \u6307\u5411\u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\n int j = i - 1;\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n if (j == -1 || nums[j] != target) {\n return -1;\n }\n // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\n return j;\n}\n
binary_search_edge.zig[class]{}-[func]{binarySearchRightEdge}\n
"},{"location":"chapter_searching/binary_search_edge/#2","title":"2. \u00a0 \u8f6c\u5316\u4e3a\u67e5\u627e\u5143\u7d20","text":"\u6211\u4eec\u77e5\u9053\uff0c\u5f53\u6570\u7ec4\u4e0d\u5305\u542b target
\u65f6\uff0c\u6700\u7ec8 \\(i\\) \u548c \\(j\\) \u4f1a\u5206\u522b\u6307\u5411\u9996\u4e2a\u5927\u4e8e\u3001\u5c0f\u4e8e target
\u7684\u5143\u7d20\u3002
\u56e0\u6b64\uff0c\u5982\u56fe 10-8 \u6240\u793a\uff0c\u6211\u4eec\u53ef\u4ee5\u6784\u9020\u4e00\u4e2a\u6570\u7ec4\u4e2d\u4e0d\u5b58\u5728\u7684\u5143\u7d20\uff0c\u7528\u4e8e\u67e5\u627e\u5de6\u53f3\u8fb9\u754c\u3002
target
\uff1a\u53ef\u4ee5\u8f6c\u5316\u4e3a\u67e5\u627e target - 0.5
\uff0c\u5e76\u8fd4\u56de\u6307\u9488 \\(i\\) \u3002target
\uff1a\u53ef\u4ee5\u8f6c\u5316\u4e3a\u67e5\u627e target + 0.5
\uff0c\u5e76\u8fd4\u56de\u6307\u9488 \\(j\\) \u3002\u56fe 10-8 \u00a0 \u5c06\u67e5\u627e\u8fb9\u754c\u8f6c\u5316\u4e3a\u67e5\u627e\u5143\u7d20
\u4ee3\u7801\u5728\u6b64\u7701\u7565\uff0c\u4ee5\u4e0b\u4e24\u70b9\u503c\u5f97\u6ce8\u610f\u3002
target
\u6539\u4e3a\u6d6e\u70b9\u6570\u7c7b\u578b\u3002\u4e8c\u5206\u67e5\u627e\u4e0d\u4ec5\u53ef\u7528\u4e8e\u641c\u7d22\u76ee\u6807\u5143\u7d20\uff0c\u8fd8\u53ef\u7528\u4e8e\u89e3\u51b3\u8bb8\u591a\u53d8\u79cd\u95ee\u9898\uff0c\u6bd4\u5982\u641c\u7d22\u76ee\u6807\u5143\u7d20\u7684\u63d2\u5165\u4f4d\u7f6e\u3002
"},{"location":"chapter_searching/binary_search_insertion/#1021","title":"10.2.1 \u00a0 \u65e0\u91cd\u590d\u5143\u7d20\u7684\u60c5\u51b5","text":"Question
\u7ed9\u5b9a\u4e00\u4e2a\u957f\u5ea6\u4e3a \\(n\\) \u7684\u6709\u5e8f\u6570\u7ec4 nums
\u548c\u4e00\u4e2a\u5143\u7d20 target
\uff0c\u6570\u7ec4\u4e0d\u5b58\u5728\u91cd\u590d\u5143\u7d20\u3002\u73b0\u5c06 target
\u63d2\u5165\u6570\u7ec4 nums
\u4e2d\uff0c\u5e76\u4fdd\u6301\u5176\u6709\u5e8f\u6027\u3002\u82e5\u6570\u7ec4\u4e2d\u5df2\u5b58\u5728\u5143\u7d20 target
\uff0c\u5219\u63d2\u5165\u5230\u5176\u5de6\u65b9\u3002\u8bf7\u8fd4\u56de\u63d2\u5165\u540e target
\u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15\u3002
\u56fe 10-4 \u00a0 \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\u793a\u4f8b\u6570\u636e
\u5982\u679c\u60f3\u590d\u7528\u4e0a\u4e00\u8282\u7684\u4e8c\u5206\u67e5\u627e\u4ee3\u7801\uff0c\u5219\u9700\u8981\u56de\u7b54\u4ee5\u4e0b\u4e24\u4e2a\u95ee\u9898\u3002
\u95ee\u9898\u4e00\uff1a\u5f53\u6570\u7ec4\u4e2d\u5305\u542b target
\u65f6\uff0c\u63d2\u5165\u70b9\u7684\u7d22\u5f15\u662f\u5426\u662f\u8be5\u5143\u7d20\u7684\u7d22\u5f15\uff1f
\u9898\u76ee\u8981\u6c42\u5c06 target
\u63d2\u5165\u5230\u76f8\u7b49\u5143\u7d20\u7684\u5de6\u8fb9\uff0c\u8fd9\u610f\u5473\u7740\u65b0\u63d2\u5165\u7684 target
\u66ff\u6362\u4e86\u539f\u6765 target
\u7684\u4f4d\u7f6e\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5f53\u6570\u7ec4\u5305\u542b target
\u65f6\uff0c\u63d2\u5165\u70b9\u7684\u7d22\u5f15\u5c31\u662f\u8be5 target
\u7684\u7d22\u5f15\u3002
\u95ee\u9898\u4e8c\uff1a\u5f53\u6570\u7ec4\u4e2d\u4e0d\u5b58\u5728 target
\u65f6\uff0c\u63d2\u5165\u70b9\u662f\u54ea\u4e2a\u5143\u7d20\u7684\u7d22\u5f15\uff1f
\u8fdb\u4e00\u6b65\u601d\u8003\u4e8c\u5206\u67e5\u627e\u8fc7\u7a0b\uff1a\u5f53 nums[m] < target
\u65f6 \\(i\\) \u79fb\u52a8\uff0c\u8fd9\u610f\u5473\u7740\u6307\u9488 \\(i\\) \u5728\u5411\u5927\u4e8e\u7b49\u4e8e target
\u7684\u5143\u7d20\u9760\u8fd1\u3002\u540c\u7406\uff0c\u6307\u9488 \\(j\\) \u59cb\u7ec8\u5728\u5411\u5c0f\u4e8e\u7b49\u4e8e target
\u7684\u5143\u7d20\u9760\u8fd1\u3002
\u56e0\u6b64\u4e8c\u5206\u7ed3\u675f\u65f6\u4e00\u5b9a\u6709\uff1a\\(i\\) \u6307\u5411\u9996\u4e2a\u5927\u4e8e target
\u7684\u5143\u7d20\uff0c\\(j\\) \u6307\u5411\u9996\u4e2a\u5c0f\u4e8e target
\u7684\u5143\u7d20\u3002\u6613\u5f97\u5f53\u6570\u7ec4\u4e0d\u5305\u542b target
\u65f6\uff0c\u63d2\u5165\u7d22\u5f15\u4e3a \\(i\\) \u3002\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a
def binary_search_insertion_simple(nums: list[int], target: int) -> int:\n \"\"\"\u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u65e0\u91cd\u590d\u5143\u7d20\uff09\"\"\"\n i, j = 0, len(nums) - 1 # \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n while i <= j:\n m = (i + j) // 2 # \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n if nums[m] < target:\n i = m + 1 # target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n elif nums[m] > target:\n j = m - 1 # target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n else:\n return m # \u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 m\n # \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 i\n return i\n
binary_search_insertion.cpp/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u65e0\u91cd\u590d\u5143\u7d20\uff09 */\nint binarySearchInsertionSimple(vector<int> &nums, int target) {\n int i = 0, j = nums.size() - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n while (i <= j) {\n int m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n if (nums[m] < target) {\n i = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n } else if (nums[m] > target) {\n j = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n } else {\n return m; // \u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 m\n }\n }\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 i\n return i;\n}\n
binary_search_insertion.java/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u65e0\u91cd\u590d\u5143\u7d20\uff09 */\nint binarySearchInsertionSimple(int[] nums, int target) {\n int i = 0, j = nums.length - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n while (i <= j) {\n int m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n if (nums[m] < target) {\n i = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n } else if (nums[m] > target) {\n j = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n } else {\n return m; // \u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 m\n }\n }\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 i\n return i;\n}\n
binary_search_insertion.cs/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u65e0\u91cd\u590d\u5143\u7d20\uff09 */\nint BinarySearchInsertionSimple(int[] nums, int target) {\n int i = 0, j = nums.Length - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n while (i <= j) {\n int m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n if (nums[m] < target) {\n i = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n } else if (nums[m] > target) {\n j = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n } else {\n return m; // \u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 m\n }\n }\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 i\n return i;\n}\n
binary_search_insertion.go/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u65e0\u91cd\u590d\u5143\u7d20\uff09 */\nfunc binarySearchInsertionSimple(nums []int, target int) int {\n // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n i, j := 0, len(nums)-1\n for i <= j {\n // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n m := i + (j-i)/2\n if nums[m] < target {\n // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n i = m + 1\n } else if nums[m] > target {\n // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n j = m - 1\n } else {\n // \u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 m\n return m\n }\n }\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 i\n return i\n}\n
binary_search_insertion.swift/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u65e0\u91cd\u590d\u5143\u7d20\uff09 */\nfunc binarySearchInsertionSimple(nums: [Int], target: Int) -> Int {\n var i = 0, j = nums.count - 1 // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n while i <= j {\n let m = i + (j - i) / 2 // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n if nums[m] < target {\n i = m + 1 // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n } else if nums[m] > target {\n j = m - 1 // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n } else {\n return m // \u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 m\n }\n }\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 i\n return i\n}\n
binary_search_insertion.js/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u65e0\u91cd\u590d\u5143\u7d20\uff09 */\nfunction binarySearchInsertionSimple(nums, target) {\n let i = 0,\n j = nums.length - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n while (i <= j) {\n const m = Math.floor(i + (j - i) / 2); // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m, \u4f7f\u7528 Math.floor() \u5411\u4e0b\u53d6\u6574\n if (nums[m] < target) {\n i = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n } else if (nums[m] > target) {\n j = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n } else {\n return m; // \u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 m\n }\n }\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 i\n return i;\n}\n
binary_search_insertion.ts/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u65e0\u91cd\u590d\u5143\u7d20\uff09 */\nfunction binarySearchInsertionSimple(\n nums: Array<number>,\n target: number\n): number {\n let i = 0,\n j = nums.length - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n while (i <= j) {\n const m = Math.floor(i + (j - i) / 2); // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m, \u4f7f\u7528 Math.floor() \u5411\u4e0b\u53d6\u6574\n if (nums[m] < target) {\n i = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n } else if (nums[m] > target) {\n j = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n } else {\n return m; // \u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 m\n }\n }\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 i\n return i;\n}\n
binary_search_insertion.dart/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u65e0\u91cd\u590d\u5143\u7d20\uff09 */\nint binarySearchInsertionSimple(List<int> nums, int target) {\n int i = 0, j = nums.length - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n while (i <= j) {\n int m = i + (j - i) ~/ 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n if (nums[m] < target) {\n i = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n } else if (nums[m] > target) {\n j = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n } else {\n return m; // \u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 m\n }\n }\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 i\n return i;\n}\n
binary_search_insertion.rs/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff09 */\npub fn binary_search_insertion(nums: &[i32], target: i32) -> i32 {\n let (mut i, mut j) = (0, nums.len() as i32 - 1); // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n while i <= j {\n let m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n if nums[m as usize] < target {\n i = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n } else if nums[m as usize] > target {\n j = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n } else {\n j = m - 1; // \u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [i, m-1] \u4e2d\n }\n }\n // \u8fd4\u56de\u63d2\u5165\u70b9 i\n i\n}\n
binary_search_insertion.c/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u65e0\u91cd\u590d\u5143\u7d20\uff09 */\nint binarySearchInsertionSimple(int *nums, int numSize, int target) {\n int i = 0, j = numSize - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n while (i <= j) {\n int m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n if (nums[m] < target) {\n i = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n } else if (nums[m] > target) {\n j = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n } else {\n return m; // \u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 m\n }\n }\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 i\n return i;\n}\n
binary_search_insertion.zig[class]{}-[func]{binarySearchInsertionSimple}\n
"},{"location":"chapter_searching/binary_search_insertion/#1022","title":"10.2.2 \u00a0 \u5b58\u5728\u91cd\u590d\u5143\u7d20\u7684\u60c5\u51b5","text":"Question
\u5728\u4e0a\u4e00\u9898\u7684\u57fa\u7840\u4e0a\uff0c\u89c4\u5b9a\u6570\u7ec4\u53ef\u80fd\u5305\u542b\u91cd\u590d\u5143\u7d20\uff0c\u5176\u4f59\u4e0d\u53d8\u3002
\u5047\u8bbe\u6570\u7ec4\u4e2d\u5b58\u5728\u591a\u4e2a target
\uff0c\u5219\u666e\u901a\u4e8c\u5206\u67e5\u627e\u53ea\u80fd\u8fd4\u56de\u5176\u4e2d\u4e00\u4e2a target
\u7684\u7d22\u5f15\uff0c\u800c\u65e0\u6cd5\u786e\u5b9a\u8be5\u5143\u7d20\u7684\u5de6\u8fb9\u548c\u53f3\u8fb9\u8fd8\u6709\u591a\u5c11 target
\u3002
\u9898\u76ee\u8981\u6c42\u5c06\u76ee\u6807\u5143\u7d20\u63d2\u5165\u5230\u6700\u5de6\u8fb9\uff0c\u6240\u4ee5\u6211\u4eec\u9700\u8981\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5de6\u4e00\u4e2a target
\u7684\u7d22\u5f15\u3002\u521d\u6b65\u8003\u8651\u901a\u8fc7\u56fe 10-5 \u6240\u793a\u7684\u6b65\u9aa4\u5b9e\u73b0\u3002
target
\u7684\u7d22\u5f15\uff0c\u8bb0\u4e3a \\(k\\) \u3002target
\u65f6\u8fd4\u56de\u3002\u56fe 10-5 \u00a0 \u7ebf\u6027\u67e5\u627e\u91cd\u590d\u5143\u7d20\u7684\u63d2\u5165\u70b9
\u6b64\u65b9\u6cd5\u867d\u7136\u53ef\u7528\uff0c\u4f46\u5176\u5305\u542b\u7ebf\u6027\u67e5\u627e\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002\u5f53\u6570\u7ec4\u4e2d\u5b58\u5728\u5f88\u591a\u91cd\u590d\u7684 target
\u65f6\uff0c\u8be5\u65b9\u6cd5\u6548\u7387\u5f88\u4f4e\u3002
\u73b0\u8003\u8651\u62d3\u5c55\u4e8c\u5206\u67e5\u627e\u4ee3\u7801\u3002\u5982\u56fe 10-6 \u6240\u793a\uff0c\u6574\u4f53\u6d41\u7a0b\u4fdd\u6301\u4e0d\u53d8\uff0c\u6bcf\u8f6e\u5148\u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 \\(m\\) \uff0c\u518d\u5224\u65ad target
\u548c nums[m]
\u7684\u5927\u5c0f\u5173\u7cfb\uff0c\u5206\u4e3a\u4ee5\u4e0b\u51e0\u79cd\u60c5\u51b5\u3002
nums[m] < target
\u6216 nums[m] > target
\u65f6\uff0c\u8bf4\u660e\u8fd8\u6ca1\u6709\u627e\u5230 target
\uff0c\u56e0\u6b64\u91c7\u7528\u666e\u901a\u4e8c\u5206\u67e5\u627e\u7684\u7f29\u5c0f\u533a\u95f4\u64cd\u4f5c\uff0c\u4ece\u800c\u4f7f\u6307\u9488 \\(i\\) \u548c \\(j\\) \u5411 target
\u9760\u8fd1\u3002nums[m] == target
\u65f6\uff0c\u8bf4\u660e\u5c0f\u4e8e target
\u7684\u5143\u7d20\u5728\u533a\u95f4 \\([i, m - 1]\\) \u4e2d\uff0c\u56e0\u6b64\u91c7\u7528 \\(j = m - 1\\) \u6765\u7f29\u5c0f\u533a\u95f4\uff0c\u4ece\u800c\u4f7f\u6307\u9488 \\(j\\) \u5411\u5c0f\u4e8e target
\u7684\u5143\u7d20\u9760\u8fd1\u3002\u5faa\u73af\u5b8c\u6210\u540e\uff0c\\(i\\) \u6307\u5411\u6700\u5de6\u8fb9\u7684 target
\uff0c\\(j\\) \u6307\u5411\u9996\u4e2a\u5c0f\u4e8e target
\u7684\u5143\u7d20\uff0c\u56e0\u6b64\u7d22\u5f15 \\(i\\) \u5c31\u662f\u63d2\u5165\u70b9\u3002
\u56fe 10-6 \u00a0 \u4e8c\u5206\u67e5\u627e\u91cd\u590d\u5143\u7d20\u7684\u63d2\u5165\u70b9\u7684\u6b65\u9aa4
\u89c2\u5bdf\u4ee5\u4e0b\u4ee3\u7801\uff0c\u5224\u65ad\u5206\u652f nums[m] > target
\u548c nums[m] == target
\u7684\u64cd\u4f5c\u76f8\u540c\uff0c\u56e0\u6b64\u4e24\u8005\u53ef\u4ee5\u5408\u5e76\u3002
\u5373\u4fbf\u5982\u6b64\uff0c\u6211\u4eec\u4ecd\u7136\u53ef\u4ee5\u5c06\u5224\u65ad\u6761\u4ef6\u4fdd\u6301\u5c55\u5f00\uff0c\u56e0\u4e3a\u5176\u903b\u8f91\u66f4\u52a0\u6e05\u6670\u3001\u53ef\u8bfb\u6027\u66f4\u597d\u3002
PythonC++JavaC#GoSwiftJSTSDartRustCZig binary_search_insertion.pydef binary_search_insertion(nums: list[int], target: int) -> int:\n \"\"\"\u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff09\"\"\"\n i, j = 0, len(nums) - 1 # \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n while i <= j:\n m = (i + j) // 2 # \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n if nums[m] < target:\n i = m + 1 # target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n elif nums[m] > target:\n j = m - 1 # target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n else:\n j = m - 1 # \u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [i, m-1] \u4e2d\n # \u8fd4\u56de\u63d2\u5165\u70b9 i\n return i\n
binary_search_insertion.cpp/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff09 */\nint binarySearchInsertion(vector<int> &nums, int target) {\n int i = 0, j = nums.size() - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n while (i <= j) {\n int m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n if (nums[m] < target) {\n i = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n } else if (nums[m] > target) {\n j = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n } else {\n j = m - 1; // \u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [i, m-1] \u4e2d\n }\n }\n // \u8fd4\u56de\u63d2\u5165\u70b9 i\n return i;\n}\n
binary_search_insertion.java/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff09 */\nint binarySearchInsertion(int[] nums, int target) {\n int i = 0, j = nums.length - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n while (i <= j) {\n int m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n if (nums[m] < target) {\n i = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n } else if (nums[m] > target) {\n j = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n } else {\n j = m - 1; // \u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [i, m-1] \u4e2d\n }\n }\n // \u8fd4\u56de\u63d2\u5165\u70b9 i\n return i;\n}\n
binary_search_insertion.cs/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff09 */\nint BinarySearchInsertion(int[] nums, int target) {\n int i = 0, j = nums.Length - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n while (i <= j) {\n int m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n if (nums[m] < target) {\n i = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n } else if (nums[m] > target) {\n j = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n } else {\n j = m - 1; // \u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [i, m-1] \u4e2d\n }\n }\n // \u8fd4\u56de\u63d2\u5165\u70b9 i\n return i;\n}\n
binary_search_insertion.go/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff09 */\nfunc binarySearchInsertion(nums []int, target int) int {\n // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n i, j := 0, len(nums)-1\n for i <= j {\n // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n m := i + (j-i)/2\n if nums[m] < target {\n // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n i = m + 1\n } else if nums[m] > target {\n // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n j = m - 1\n } else {\n // \u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [i, m-1] \u4e2d\n j = m - 1\n }\n }\n // \u8fd4\u56de\u63d2\u5165\u70b9 i\n return i\n}\n
binary_search_insertion.swift/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff09 */\nfunc binarySearchInsertion(nums: [Int], target: Int) -> Int {\n var i = 0, j = nums.count - 1 // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n while i <= j {\n let m = i + (j - i) / 2 // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n if nums[m] < target {\n i = m + 1 // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n } else if nums[m] > target {\n j = m - 1 // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n } else {\n j = m - 1 // \u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [i, m-1] \u4e2d\n }\n }\n // \u8fd4\u56de\u63d2\u5165\u70b9 i\n return i\n}\n
binary_search_insertion.js/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff09 */\nfunction binarySearchInsertion(nums, target) {\n let i = 0,\n j = nums.length - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n while (i <= j) {\n const m = Math.floor(i + (j - i) / 2); // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m, \u4f7f\u7528 Math.floor() \u5411\u4e0b\u53d6\u6574\n if (nums[m] < target) {\n i = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n } else if (nums[m] > target) {\n j = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n } else {\n j = m - 1; // \u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [i, m-1] \u4e2d\n }\n }\n // \u8fd4\u56de\u63d2\u5165\u70b9 i\n return i;\n}\n
binary_search_insertion.ts/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff09 */\nfunction binarySearchInsertion(nums: Array<number>, target: number): number {\n let i = 0,\n j = nums.length - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n while (i <= j) {\n const m = Math.floor(i + (j - i) / 2); // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m, \u4f7f\u7528 Math.floor() \u5411\u4e0b\u53d6\u6574\n if (nums[m] < target) {\n i = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n } else if (nums[m] > target) {\n j = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n } else {\n j = m - 1; // \u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [i, m-1] \u4e2d\n }\n }\n // \u8fd4\u56de\u63d2\u5165\u70b9 i\n return i;\n}\n
binary_search_insertion.dart/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff09 */\nint binarySearchInsertion(List<int> nums, int target) {\n int i = 0, j = nums.length - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n while (i <= j) {\n int m = i + (j - i) ~/ 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n if (nums[m] < target) {\n i = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n } else if (nums[m] > target) {\n j = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n } else {\n j = m - 1; // \u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [i, m-1] \u4e2d\n }\n }\n // \u8fd4\u56de\u63d2\u5165\u70b9 i\n return i;\n}\n
binary_search_insertion.rs/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff09 */\npub fn binary_search_insertion(nums: &[i32], target: i32) -> i32 {\n let (mut i, mut j) = (0, nums.len() as i32 - 1); // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n while i <= j {\n let m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n if nums[m as usize] < target {\n i = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n } else if nums[m as usize] > target {\n j = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n } else {\n j = m - 1; // \u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [i, m-1] \u4e2d\n }\n }\n // \u8fd4\u56de\u63d2\u5165\u70b9 i\n i\n}\n
binary_search_insertion.c/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff09 */\nint binarySearchInsertion(int *nums, int numSize, int target) {\n int i = 0, j = numSize - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n while (i <= j) {\n int m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n if (nums[m] < target) {\n i = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n } else if (nums[m] > target) {\n j = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n } else {\n j = m - 1; // \u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [i, m-1] \u4e2d\n }\n }\n // \u8fd4\u56de\u63d2\u5165\u70b9 i\n return i;\n}\n
binary_search_insertion.zig[class]{}-[func]{binarySearchInsertion}\n
Tip
\u672c\u8282\u7684\u4ee3\u7801\u90fd\u662f\u201c\u53cc\u95ed\u533a\u95f4\u201d\u5199\u6cd5\u3002\u6709\u5174\u8da3\u7684\u8bfb\u8005\u53ef\u4ee5\u81ea\u884c\u5b9e\u73b0\u201c\u5de6\u95ed\u53f3\u5f00\u201d\u5199\u6cd5\u3002
\u603b\u7684\u6765\u770b\uff0c\u4e8c\u5206\u67e5\u627e\u65e0\u975e\u5c31\u662f\u7ed9\u6307\u9488 \\(i\\) \u548c \\(j\\) \u5206\u522b\u8bbe\u5b9a\u641c\u7d22\u76ee\u6807\uff0c\u76ee\u6807\u53ef\u80fd\u662f\u4e00\u4e2a\u5177\u4f53\u7684\u5143\u7d20\uff08\u4f8b\u5982 target
\uff09\uff0c\u4e5f\u53ef\u80fd\u662f\u4e00\u4e2a\u5143\u7d20\u8303\u56f4\uff08\u4f8b\u5982\u5c0f\u4e8e target
\u7684\u5143\u7d20\uff09\u3002
\u5728\u4e0d\u65ad\u7684\u5faa\u73af\u4e8c\u5206\u4e2d\uff0c\u6307\u9488 \\(i\\) \u548c \\(j\\) \u90fd\u9010\u6e10\u903c\u8fd1\u9884\u5148\u8bbe\u5b9a\u7684\u76ee\u6807\u3002\u6700\u7ec8\uff0c\u5b83\u4eec\u6216\u662f\u6210\u529f\u627e\u5230\u7b54\u6848\uff0c\u6216\u662f\u8d8a\u8fc7\u8fb9\u754c\u540e\u505c\u6b62\u3002
"},{"location":"chapter_searching/replace_linear_by_hashing/","title":"10.4 \u00a0 \u54c8\u5e0c\u4f18\u5316\u7b56\u7565","text":"\u5728\u7b97\u6cd5\u9898\u4e2d\uff0c\u6211\u4eec\u5e38\u901a\u8fc7\u5c06\u7ebf\u6027\u67e5\u627e\u66ff\u6362\u4e3a\u54c8\u5e0c\u67e5\u627e\u6765\u964d\u4f4e\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u3002\u6211\u4eec\u501f\u52a9\u4e00\u4e2a\u7b97\u6cd5\u9898\u6765\u52a0\u6df1\u7406\u89e3\u3002
Question
\u7ed9\u5b9a\u4e00\u4e2a\u6574\u6570\u6570\u7ec4 nums
\u548c\u4e00\u4e2a\u76ee\u6807\u5143\u7d20 target
\uff0c\u8bf7\u5728\u6570\u7ec4\u4e2d\u641c\u7d22\u201c\u548c\u201d\u4e3a target
\u7684\u4e24\u4e2a\u5143\u7d20\uff0c\u5e76\u8fd4\u56de\u5b83\u4eec\u7684\u6570\u7ec4\u7d22\u5f15\u3002\u8fd4\u56de\u4efb\u610f\u4e00\u4e2a\u89e3\u5373\u53ef\u3002
\u8003\u8651\u76f4\u63a5\u904d\u5386\u6240\u6709\u53ef\u80fd\u7684\u7ec4\u5408\u3002\u5982\u56fe 10-9 \u6240\u793a\uff0c\u6211\u4eec\u5f00\u542f\u4e00\u4e2a\u4e24\u5c42\u5faa\u73af\uff0c\u5728\u6bcf\u8f6e\u4e2d\u5224\u65ad\u4e24\u4e2a\u6574\u6570\u7684\u548c\u662f\u5426\u4e3a target
\uff0c\u82e5\u662f\uff0c\u5219\u8fd4\u56de\u5b83\u4eec\u7684\u7d22\u5f15\u3002
\u56fe 10-9 \u00a0 \u7ebf\u6027\u67e5\u627e\u6c42\u89e3\u4e24\u6570\u4e4b\u548c
\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig two_sum.pydef two_sum_brute_force(nums: list[int], target: int) -> list[int]:\n \"\"\"\u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e\"\"\"\n # \u4e24\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n^2)\n for i in range(len(nums) - 1):\n for j in range(i + 1, len(nums)):\n if nums[i] + nums[j] == target:\n return [i, j]\n return []\n
two_sum.cpp/* \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e */\nvector<int> twoSumBruteForce(vector<int> &nums, int target) {\n int size = nums.size();\n // \u4e24\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n^2)\n for (int i = 0; i < size - 1; i++) {\n for (int j = i + 1; j < size; j++) {\n if (nums[i] + nums[j] == target)\n return {i, j};\n }\n }\n return {};\n}\n
two_sum.java/* \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e */\nint[] twoSumBruteForce(int[] nums, int target) {\n int size = nums.length;\n // \u4e24\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n^2)\n for (int i = 0; i < size - 1; i++) {\n for (int j = i + 1; j < size; j++) {\n if (nums[i] + nums[j] == target)\n return new int[] { i, j };\n }\n }\n return new int[0];\n}\n
two_sum.cs/* \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e */\nint[] TwoSumBruteForce(int[] nums, int target) {\n int size = nums.Length;\n // \u4e24\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n^2)\n for (int i = 0; i < size - 1; i++) {\n for (int j = i + 1; j < size; j++) {\n if (nums[i] + nums[j] == target)\n return [i, j];\n }\n }\n return [];\n}\n
two_sum.go/* \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e */\nfunc twoSumBruteForce(nums []int, target int) []int {\n size := len(nums)\n // \u4e24\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n^2)\n for i := 0; i < size-1; i++ {\n for j := i + 1; i < size; j++ {\n if nums[i]+nums[j] == target {\n return []int{i, j}\n }\n }\n }\n return nil\n}\n
two_sum.swift/* \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e */\nfunc twoSumBruteForce(nums: [Int], target: Int) -> [Int] {\n // \u4e24\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n^2)\n for i in nums.indices.dropLast() {\n for j in nums.indices.dropFirst(i + 1) {\n if nums[i] + nums[j] == target {\n return [i, j]\n }\n }\n }\n return [0]\n}\n
two_sum.js/* \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e */\nfunction twoSumBruteForce(nums, target) {\n const n = nums.length;\n // \u4e24\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n^2)\n for (let i = 0; i < n; i++) {\n for (let j = i + 1; j < n; j++) {\n if (nums[i] + nums[j] === target) {\n return [i, j];\n }\n }\n }\n return [];\n}\n
two_sum.ts/* \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e */\nfunction twoSumBruteForce(nums: number[], target: number): number[] {\n const n = nums.length;\n // \u4e24\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n^2)\n for (let i = 0; i < n; i++) {\n for (let j = i + 1; j < n; j++) {\n if (nums[i] + nums[j] === target) {\n return [i, j];\n }\n }\n }\n return [];\n}\n
two_sum.dart/* \u65b9\u6cd5\u4e00\uff1a \u66b4\u529b\u679a\u4e3e */\nList<int> twoSumBruteForce(List<int> nums, int target) {\n int size = nums.length;\n // \u4e24\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n^2)\n for (var i = 0; i < size - 1; i++) {\n for (var j = i + 1; j < size; j++) {\n if (nums[i] + nums[j] == target) return [i, j];\n }\n }\n return [0];\n}\n
two_sum.rs/* \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e */\npub fn two_sum_brute_force(nums: &Vec<i32>, target: i32) -> Option<Vec<i32>> {\n let size = nums.len();\n // \u4e24\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n^2)\n for i in 0..size - 1 {\n for j in i + 1..size {\n if nums[i] + nums[j] == target {\n return Some(vec![i as i32, j as i32]);\n }\n }\n }\n None\n}\n
two_sum.c/* \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e */\nint *twoSumBruteForce(int *nums, int numsSize, int target, int *returnSize) {\n for (int i = 0; i < numsSize; ++i) {\n for (int j = i + 1; j < numsSize; ++j) {\n if (nums[i] + nums[j] == target) {\n int *res = malloc(sizeof(int) * 2);\n res[0] = i, res[1] = j;\n *returnSize = 2;\n return res;\n }\n }\n }\n *returnSize = 0;\n return NULL;\n}\n
two_sum.zig// \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e\nfn twoSumBruteForce(nums: []i32, target: i32) ?[2]i32 {\n var size: usize = nums.len;\n var i: usize = 0;\n // \u4e24\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n^2)\n while (i < size - 1) : (i += 1) {\n var j = i + 1;\n while (j < size) : (j += 1) {\n if (nums[i] + nums[j] == target) {\n return [_]i32{@intCast(i), @intCast(j)};\n }\n }\n }\n return null;\n}\n
\u6b64\u65b9\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\) \uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(1)\\) \uff0c\u5728\u5927\u6570\u636e\u91cf\u4e0b\u975e\u5e38\u8017\u65f6\u3002
"},{"location":"chapter_searching/replace_linear_by_hashing/#1042","title":"10.4.2 \u00a0 \u54c8\u5e0c\u67e5\u627e\uff1a\u4ee5\u7a7a\u95f4\u6362\u65f6\u95f4","text":"\u8003\u8651\u501f\u52a9\u4e00\u4e2a\u54c8\u5e0c\u8868\uff0c\u952e\u503c\u5bf9\u5206\u522b\u4e3a\u6570\u7ec4\u5143\u7d20\u548c\u5143\u7d20\u7d22\u5f15\u3002\u5faa\u73af\u904d\u5386\u6570\u7ec4\uff0c\u6bcf\u8f6e\u6267\u884c\u56fe 10-10 \u6240\u793a\u7684\u6b65\u9aa4\u3002
target - nums[i]
\u662f\u5426\u5728\u54c8\u5e0c\u8868\u4e2d\uff0c\u82e5\u662f\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u8fd9\u4e24\u4e2a\u5143\u7d20\u7684\u7d22\u5f15\u3002nums[i]
\u548c\u7d22\u5f15 i
\u6dfb\u52a0\u8fdb\u54c8\u5e0c\u8868\u3002\u56fe 10-10 \u00a0 \u8f85\u52a9\u54c8\u5e0c\u8868\u6c42\u89e3\u4e24\u6570\u4e4b\u548c
\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff0c\u4ec5\u9700\u5355\u5c42\u5faa\u73af\u5373\u53ef\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig two_sum.pydef two_sum_hash_table(nums: list[int], target: int) -> list[int]:\n \"\"\"\u65b9\u6cd5\u4e8c\uff1a\u8f85\u52a9\u54c8\u5e0c\u8868\"\"\"\n # \u8f85\u52a9\u54c8\u5e0c\u8868\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n dic = {}\n # \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n for i in range(len(nums)):\n if target - nums[i] in dic:\n return [dic[target - nums[i]], i]\n dic[nums[i]] = i\n return []\n
two_sum.cpp/* \u65b9\u6cd5\u4e8c\uff1a\u8f85\u52a9\u54c8\u5e0c\u8868 */\nvector<int> twoSumHashTable(vector<int> &nums, int target) {\n int size = nums.size();\n // \u8f85\u52a9\u54c8\u5e0c\u8868\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n unordered_map<int, int> dic;\n // \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n for (int i = 0; i < size; i++) {\n if (dic.find(target - nums[i]) != dic.end()) {\n return {dic[target - nums[i]], i};\n }\n dic.emplace(nums[i], i);\n }\n return {};\n}\n
two_sum.java/* \u65b9\u6cd5\u4e8c\uff1a\u8f85\u52a9\u54c8\u5e0c\u8868 */\nint[] twoSumHashTable(int[] nums, int target) {\n int size = nums.length;\n // \u8f85\u52a9\u54c8\u5e0c\u8868\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n Map<Integer, Integer> dic = new HashMap<>();\n // \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n for (int i = 0; i < size; i++) {\n if (dic.containsKey(target - nums[i])) {\n return new int[] { dic.get(target - nums[i]), i };\n }\n dic.put(nums[i], i);\n }\n return new int[0];\n}\n
two_sum.cs/* \u65b9\u6cd5\u4e8c\uff1a\u8f85\u52a9\u54c8\u5e0c\u8868 */\nint[] TwoSumHashTable(int[] nums, int target) {\n int size = nums.Length;\n // \u8f85\u52a9\u54c8\u5e0c\u8868\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n Dictionary<int, int> dic = [];\n // \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n for (int i = 0; i < size; i++) {\n if (dic.ContainsKey(target - nums[i])) {\n return [dic[target - nums[i]], i];\n }\n dic.Add(nums[i], i);\n }\n return [];\n}\n
two_sum.go/* \u65b9\u6cd5\u4e8c\uff1a\u8f85\u52a9\u54c8\u5e0c\u8868 */\nfunc twoSumHashTable(nums []int, target int) []int {\n // \u8f85\u52a9\u54c8\u5e0c\u8868\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n hashTable := map[int]int{}\n // \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n for idx, val := range nums {\n if preIdx, ok := hashTable[target-val]; ok {\n return []int{preIdx, idx}\n }\n hashTable[val] = idx\n }\n return nil\n}\n
two_sum.swift/* \u65b9\u6cd5\u4e8c\uff1a\u8f85\u52a9\u54c8\u5e0c\u8868 */\nfunc twoSumHashTable(nums: [Int], target: Int) -> [Int] {\n // \u8f85\u52a9\u54c8\u5e0c\u8868\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n var dic: [Int: Int] = [:]\n // \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n for i in nums.indices {\n if let j = dic[target - nums[i]] {\n return [j, i]\n }\n dic[nums[i]] = i\n }\n return [0]\n}\n
two_sum.js/* \u65b9\u6cd5\u4e8c\uff1a\u8f85\u52a9\u54c8\u5e0c\u8868 */\nfunction twoSumHashTable(nums, target) {\n // \u8f85\u52a9\u54c8\u5e0c\u8868\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n let m = {};\n // \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n for (let i = 0; i < nums.length; i++) {\n if (m[target - nums[i]] !== undefined) {\n return [m[target - nums[i]], i];\n } else {\n m[nums[i]] = i;\n }\n }\n return [];\n}\n
two_sum.ts/* \u65b9\u6cd5\u4e8c\uff1a\u8f85\u52a9\u54c8\u5e0c\u8868 */\nfunction twoSumHashTable(nums: number[], target: number): number[] {\n // \u8f85\u52a9\u54c8\u5e0c\u8868\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n let m: Map<number, number> = new Map();\n // \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n for (let i = 0; i < nums.length; i++) {\n let index = m.get(target - nums[i]);\n if (index !== undefined) {\n return [index, i];\n } else {\n m.set(nums[i], i);\n }\n }\n return [];\n}\n
two_sum.dart/* \u65b9\u6cd5\u4e8c\uff1a \u8f85\u52a9\u54c8\u5e0c\u8868 */\nList<int> twoSumHashTable(List<int> nums, int target) {\n int size = nums.length;\n // \u8f85\u52a9\u54c8\u5e0c\u8868\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n Map<int, int> dic = HashMap();\n // \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n for (var i = 0; i < size; i++) {\n if (dic.containsKey(target - nums[i])) {\n return [dic[target - nums[i]]!, i];\n }\n dic.putIfAbsent(nums[i], () => i);\n }\n return [0];\n}\n
two_sum.rs/* \u65b9\u6cd5\u4e8c\uff1a\u8f85\u52a9\u54c8\u5e0c\u8868 */\npub fn two_sum_hash_table(nums: &Vec<i32>, target: i32) -> Option<Vec<i32>> {\n // \u8f85\u52a9\u54c8\u5e0c\u8868\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n let mut dic = HashMap::new();\n // \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n for (i, num) in nums.iter().enumerate() {\n match dic.get(&(target - num)) {\n Some(v) => return Some(vec![*v as i32, i as i32]),\n None => dic.insert(num, i as i32)\n };\n }\n None\n}\n
two_sum.c/* \u54c8\u5e0c\u8868 */\ntypedef struct {\n int key;\n int val;\n UT_hash_handle hh; // \u57fa\u4e8e uthash.h \u5b9e\u73b0\n} HashTable;\n\n/* \u54c8\u5e0c\u8868\u67e5\u8be2 */\nHashTable *find(HashTable *h, int key) {\n HashTable *tmp;\n HASH_FIND_INT(h, &key, tmp);\n return tmp;\n}\n\n/* \u54c8\u5e0c\u8868\u5143\u7d20\u63d2\u5165 */\nvoid insert(HashTable *h, int key, int val) {\n HashTable *t = find(h, key);\n if (t == NULL) {\n HashTable *tmp = malloc(sizeof(HashTable));\n tmp->key = key, tmp->val = val;\n HASH_ADD_INT(h, key, tmp);\n } else {\n t->val = val;\n }\n}\n\n/* \u65b9\u6cd5\u4e8c\uff1a\u8f85\u52a9\u54c8\u5e0c\u8868 */\nint *twoSumHashTable(int *nums, int numsSize, int target, int *returnSize) {\n HashTable *hashtable = NULL;\n for (int i = 0; i < numsSize; i++) {\n HashTable *t = find(hashtable, target - nums[i]);\n if (t != NULL) {\n int *res = malloc(sizeof(int) * 2);\n res[0] = t->val, res[1] = i;\n *returnSize = 2;\n return res;\n }\n insert(hashtable, nums[i], i);\n }\n *returnSize = 0;\n return NULL;\n}\n
two_sum.zig// \u65b9\u6cd5\u4e8c\uff1a\u8f85\u52a9\u54c8\u5e0c\u8868\nfn twoSumHashTable(nums: []i32, target: i32) !?[2]i32 {\n var size: usize = nums.len;\n // \u8f85\u52a9\u54c8\u5e0c\u8868\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n var dic = std.AutoHashMap(i32, i32).init(std.heap.page_allocator);\n defer dic.deinit();\n var i: usize = 0;\n // \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n while (i < size) : (i += 1) {\n if (dic.contains(target - nums[i])) {\n return [_]i32{dic.get(target - nums[i]).?, @intCast(i)};\n }\n try dic.put(nums[i], @intCast(i));\n }\n return null;\n}\n
\u6b64\u65b9\u6cd5\u901a\u8fc7\u54c8\u5e0c\u67e5\u627e\u5c06\u65f6\u95f4\u590d\u6742\u5ea6\u4ece \\(O(n^2)\\) \u964d\u81f3 \\(O(n)\\) \uff0c\u5927\u5e45\u63d0\u5347\u8fd0\u884c\u6548\u7387\u3002
\u7531\u4e8e\u9700\u8981\u7ef4\u62a4\u4e00\u4e2a\u989d\u5916\u7684\u54c8\u5e0c\u8868\uff0c\u56e0\u6b64\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002\u5c3d\u7ba1\u5982\u6b64\uff0c\u8be5\u65b9\u6cd5\u7684\u6574\u4f53\u65f6\u7a7a\u6548\u7387\u66f4\u4e3a\u5747\u8861\uff0c\u56e0\u6b64\u5b83\u662f\u672c\u9898\u7684\u6700\u4f18\u89e3\u6cd5\u3002
"},{"location":"chapter_searching/searching_algorithm_revisited/","title":"10.5 \u00a0 \u91cd\u8bc6\u641c\u7d22\u7b97\u6cd5","text":"\u300c\u641c\u7d22\u7b97\u6cd5 searching algorithm\u300d\u7528\u4e8e\u5728\u6570\u636e\u7ed3\u6784\uff08\u4f8b\u5982\u6570\u7ec4\u3001\u94fe\u8868\u3001\u6811\u6216\u56fe\uff09\u4e2d\u641c\u7d22\u4e00\u4e2a\u6216\u4e00\u7ec4\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u7684\u5143\u7d20\u3002
\u641c\u7d22\u7b97\u6cd5\u53ef\u6839\u636e\u5b9e\u73b0\u601d\u8def\u5206\u4e3a\u4ee5\u4e0b\u4e24\u7c7b\u3002
\u4e0d\u96be\u53d1\u73b0\uff0c\u8fd9\u4e9b\u77e5\u8bc6\u70b9\u90fd\u5df2\u5728\u524d\u9762\u7684\u7ae0\u8282\u4e2d\u4ecb\u7ecd\u8fc7\uff0c\u56e0\u6b64\u641c\u7d22\u7b97\u6cd5\u5bf9\u4e8e\u6211\u4eec\u6765\u8bf4\u5e76\u4e0d\u964c\u751f\u3002\u5728\u672c\u8282\u4e2d\uff0c\u6211\u4eec\u5c06\u4ece\u66f4\u52a0\u7cfb\u7edf\u7684\u89c6\u89d2\u5207\u5165\uff0c\u91cd\u65b0\u5ba1\u89c6\u641c\u7d22\u7b97\u6cd5\u3002
"},{"location":"chapter_searching/searching_algorithm_revisited/#1051","title":"10.5.1 \u00a0 \u66b4\u529b\u641c\u7d22","text":"\u66b4\u529b\u641c\u7d22\u901a\u8fc7\u904d\u5386\u6570\u636e\u7ed3\u6784\u7684\u6bcf\u4e2a\u5143\u7d20\u6765\u5b9a\u4f4d\u76ee\u6807\u5143\u7d20\u3002
\u66b4\u529b\u641c\u7d22\u7684\u4f18\u70b9\u662f\u7b80\u5355\u4e14\u901a\u7528\u6027\u597d\uff0c\u65e0\u987b\u5bf9\u6570\u636e\u505a\u9884\u5904\u7406\u548c\u501f\u52a9\u989d\u5916\u7684\u6570\u636e\u7ed3\u6784\u3002
\u7136\u800c\uff0c\u6b64\u7c7b\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \uff0c\u5176\u4e2d \\(n\\) \u4e3a\u5143\u7d20\u6570\u91cf\uff0c\u56e0\u6b64\u5728\u6570\u636e\u91cf\u8f83\u5927\u7684\u60c5\u51b5\u4e0b\u6027\u80fd\u8f83\u5dee\u3002
"},{"location":"chapter_searching/searching_algorithm_revisited/#1052","title":"10.5.2 \u00a0 \u81ea\u9002\u5e94\u641c\u7d22","text":"\u81ea\u9002\u5e94\u641c\u7d22\u5229\u7528\u6570\u636e\u7684\u7279\u6709\u5c5e\u6027\uff08\u4f8b\u5982\u6709\u5e8f\u6027\uff09\u6765\u4f18\u5316\u641c\u7d22\u8fc7\u7a0b\uff0c\u4ece\u800c\u66f4\u9ad8\u6548\u5730\u5b9a\u4f4d\u76ee\u6807\u5143\u7d20\u3002
\u6b64\u7c7b\u7b97\u6cd5\u7684\u4f18\u70b9\u662f\u6548\u7387\u9ad8\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u53ef\u8fbe\u5230 \\(O(\\log n)\\) \u751a\u81f3 \\(O(1)\\) \u3002
\u7136\u800c\uff0c\u4f7f\u7528\u8fd9\u4e9b\u7b97\u6cd5\u5f80\u5f80\u9700\u8981\u5bf9\u6570\u636e\u8fdb\u884c\u9884\u5904\u7406\u3002\u4f8b\u5982\uff0c\u4e8c\u5206\u67e5\u627e\u9700\u8981\u9884\u5148\u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\uff0c\u54c8\u5e0c\u67e5\u627e\u548c\u6811\u67e5\u627e\u90fd\u9700\u8981\u501f\u52a9\u989d\u5916\u7684\u6570\u636e\u7ed3\u6784\uff0c\u7ef4\u62a4\u8fd9\u4e9b\u6570\u636e\u7ed3\u6784\u4e5f\u9700\u8981\u989d\u5916\u7684\u65f6\u95f4\u548c\u7a7a\u95f4\u5f00\u9500\u3002
Tip
\u81ea\u9002\u5e94\u641c\u7d22\u7b97\u6cd5\u5e38\u88ab\u79f0\u4e3a\u67e5\u627e\u7b97\u6cd5\uff0c\u4e3b\u8981\u7528\u4e8e\u5728\u7279\u5b9a\u6570\u636e\u7ed3\u6784\u4e2d\u5feb\u901f\u68c0\u7d22\u76ee\u6807\u5143\u7d20\u3002
"},{"location":"chapter_searching/searching_algorithm_revisited/#1053","title":"10.5.3 \u00a0 \u641c\u7d22\u65b9\u6cd5\u9009\u53d6","text":"\u7ed9\u5b9a\u5927\u5c0f\u4e3a \\(n\\) \u7684\u4e00\u7ec4\u6570\u636e\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u7ebf\u6027\u641c\u7d22\u3001\u4e8c\u5206\u67e5\u627e\u3001\u6811\u67e5\u627e\u3001\u54c8\u5e0c\u67e5\u627e\u7b49\u591a\u79cd\u65b9\u6cd5\u4ece\u4e2d\u641c\u7d22\u76ee\u6807\u5143\u7d20\u3002\u5404\u4e2a\u65b9\u6cd5\u7684\u5de5\u4f5c\u539f\u7406\u5982\u56fe 10-11 \u6240\u793a\u3002
\u56fe 10-11 \u00a0 \u591a\u79cd\u641c\u7d22\u7b56\u7565
\u4e0a\u8ff0\u51e0\u79cd\u65b9\u6cd5\u7684\u64cd\u4f5c\u6548\u7387\u4e0e\u7279\u6027\u5982\u8868 10-1 \u6240\u793a\u3002
\u8868 10-1 \u00a0 \u67e5\u627e\u7b97\u6cd5\u6548\u7387\u5bf9\u6bd4
\u7ebf\u6027\u641c\u7d22 \u4e8c\u5206\u67e5\u627e \u6811\u67e5\u627e \u54c8\u5e0c\u67e5\u627e \u67e5\u627e\u5143\u7d20 \\(O(n)\\) \\(O(\\log n)\\) \\(O(\\log n)\\) \\(O(1)\\) \u63d2\u5165\u5143\u7d20 \\(O(1)\\) \\(O(n)\\) \\(O(\\log n)\\) \\(O(1)\\) \u5220\u9664\u5143\u7d20 \\(O(n)\\) \\(O(n)\\) \\(O(\\log n)\\) \\(O(1)\\) \u989d\u5916\u7a7a\u95f4 \\(O(1)\\) \\(O(1)\\) \\(O(n)\\) \\(O(n)\\) \u6570\u636e\u9884\u5904\u7406 / \u6392\u5e8f \\(O(n \\log n)\\) \u5efa\u6811 \\(O(n \\log n)\\) \u5efa\u54c8\u5e0c\u8868 \\(O(n)\\) \u6570\u636e\u662f\u5426\u6709\u5e8f \u65e0\u5e8f \u6709\u5e8f \u6709\u5e8f \u65e0\u5e8f\u641c\u7d22\u7b97\u6cd5\u7684\u9009\u62e9\u8fd8\u53d6\u51b3\u4e8e\u6570\u636e\u4f53\u91cf\u3001\u641c\u7d22\u6027\u80fd\u8981\u6c42\u3001\u6570\u636e\u67e5\u8be2\u4e0e\u66f4\u65b0\u9891\u7387\u7b49\u3002
\u7ebf\u6027\u641c\u7d22
\u4e8c\u5206\u67e5\u627e
\u54c8\u5e0c\u67e5\u627e
\u6811\u67e5\u627e
Abstract
\u6392\u5e8f\u72b9\u5982\u4e00\u628a\u5c06\u6df7\u4e71\u53d8\u4e3a\u79e9\u5e8f\u7684\u9b54\u6cd5\u94a5\u5319\uff0c\u4f7f\u6211\u4eec\u80fd\u4ee5\u66f4\u9ad8\u6548\u7684\u65b9\u5f0f\u7406\u89e3\u4e0e\u5904\u7406\u6570\u636e\u3002
\u65e0\u8bba\u662f\u7b80\u5355\u7684\u5347\u5e8f\uff0c\u8fd8\u662f\u590d\u6742\u7684\u5206\u7c7b\u6392\u5217\uff0c\u6392\u5e8f\u90fd\u5411\u6211\u4eec\u5c55\u793a\u4e86\u6570\u636e\u7684\u548c\u8c10\u7f8e\u611f\u3002
"},{"location":"chapter_sorting/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"\u300c\u5192\u6ce1\u6392\u5e8f bubble sort\u300d\u901a\u8fc7\u8fde\u7eed\u5730\u6bd4\u8f83\u4e0e\u4ea4\u6362\u76f8\u90bb\u5143\u7d20\u5b9e\u73b0\u6392\u5e8f\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u5c31\u50cf\u6c14\u6ce1\u4ece\u5e95\u90e8\u5347\u5230\u9876\u90e8\u4e00\u6837\uff0c\u56e0\u6b64\u5f97\u540d\u5192\u6ce1\u6392\u5e8f\u3002
\u5982\u56fe 11-4 \u6240\u793a\uff0c\u5192\u6ce1\u8fc7\u7a0b\u53ef\u4ee5\u5229\u7528\u5143\u7d20\u4ea4\u6362\u64cd\u4f5c\u6765\u6a21\u62df\uff1a\u4ece\u6570\u7ec4\u6700\u5de6\u7aef\u5f00\u59cb\u5411\u53f3\u904d\u5386\uff0c\u4f9d\u6b21\u6bd4\u8f83\u76f8\u90bb\u5143\u7d20\u5927\u5c0f\uff0c\u5982\u679c\u201c\u5de6\u5143\u7d20 > \u53f3\u5143\u7d20\u201d\u5c31\u4ea4\u6362\u4e8c\u8005\u3002\u904d\u5386\u5b8c\u6210\u540e\uff0c\u6700\u5927\u7684\u5143\u7d20\u4f1a\u88ab\u79fb\u52a8\u5230\u6570\u7ec4\u7684\u6700\u53f3\u7aef\u3002
<1><2><3><4><5><6><7>\u56fe 11-4 \u00a0 \u5229\u7528\u5143\u7d20\u4ea4\u6362\u64cd\u4f5c\u6a21\u62df\u5192\u6ce1
"},{"location":"chapter_sorting/bubble_sort/#1131","title":"11.3.1 \u00a0 \u7b97\u6cd5\u6d41\u7a0b","text":"\u8bbe\u6570\u7ec4\u7684\u957f\u5ea6\u4e3a \\(n\\) \uff0c\u5192\u6ce1\u6392\u5e8f\u7684\u6b65\u9aa4\u5982\u56fe 11-5 \u6240\u793a\u3002
\u56fe 11-5 \u00a0 \u5192\u6ce1\u6392\u5e8f\u6d41\u7a0b
\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig bubble_sort.pydef bubble_sort(nums: list[int]):\n \"\"\"\u5192\u6ce1\u6392\u5e8f\"\"\"\n n = len(nums)\n # \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for i in range(n - 1, 0, -1):\n # \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n for j in range(i):\n if nums[j] > nums[j + 1]:\n # \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n nums[j], nums[j + 1] = nums[j + 1], nums[j]\n
bubble_sort.cpp/* \u5192\u6ce1\u6392\u5e8f */\nvoid bubbleSort(vector<int> &nums) {\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for (int i = nums.size() - 1; i > 0; i--) {\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n for (int j = 0; j < i; j++) {\n if (nums[j] > nums[j + 1]) {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n // \u8fd9\u91cc\u4f7f\u7528\u4e86 std::swap() \u51fd\u6570\n swap(nums[j], nums[j + 1]);\n }\n }\n }\n}\n
bubble_sort.java/* \u5192\u6ce1\u6392\u5e8f */\nvoid bubbleSort(int[] nums) {\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for (int i = nums.length - 1; i > 0; i--) {\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n for (int j = 0; j < i; j++) {\n if (nums[j] > nums[j + 1]) {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n int tmp = nums[j];\n nums[j] = nums[j + 1];\n nums[j + 1] = tmp;\n }\n }\n }\n}\n
bubble_sort.cs/* \u5192\u6ce1\u6392\u5e8f */\nvoid BubbleSort(int[] nums) {\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for (int i = nums.Length - 1; i > 0; i--) {\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef \n for (int j = 0; j < i; j++) {\n if (nums[j] > nums[j + 1]) {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n (nums[j + 1], nums[j]) = (nums[j], nums[j + 1]);\n }\n }\n }\n}\n
bubble_sort.go/* \u5192\u6ce1\u6392\u5e8f */\nfunc bubbleSort(nums []int) {\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for i := len(nums) - 1; i > 0; i-- {\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n for j := 0; j < i; j++ {\n if nums[j] > nums[j+1] {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n nums[j], nums[j+1] = nums[j+1], nums[j]\n }\n }\n }\n}\n
bubble_sort.swift/* \u5192\u6ce1\u6392\u5e8f */\nfunc bubbleSort(nums: inout [Int]) {\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for i in stride(from: nums.count - 1, to: 0, by: -1) {\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef \n for j in stride(from: 0, to: i, by: 1) {\n if nums[j] > nums[j + 1] {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n let tmp = nums[j]\n nums[j] = nums[j + 1]\n nums[j + 1] = tmp\n }\n }\n }\n}\n
bubble_sort.js/* \u5192\u6ce1\u6392\u5e8f */\nfunction bubbleSort(nums) {\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for (let i = nums.length - 1; i > 0; i--) {\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n for (let j = 0; j < i; j++) {\n if (nums[j] > nums[j + 1]) {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n let tmp = nums[j];\n nums[j] = nums[j + 1];\n nums[j + 1] = tmp;\n }\n }\n }\n}\n
bubble_sort.ts/* \u5192\u6ce1\u6392\u5e8f */\nfunction bubbleSort(nums: number[]): void {\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for (let i = nums.length - 1; i > 0; i--) {\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n for (let j = 0; j < i; j++) {\n if (nums[j] > nums[j + 1]) {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n let tmp = nums[j];\n nums[j] = nums[j + 1];\n nums[j + 1] = tmp;\n }\n }\n }\n}\n
bubble_sort.dart/* \u5192\u6ce1\u6392\u5e8f */\nvoid bubbleSort(List<int> nums) {\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for (int i = nums.length - 1; i > 0; i--) {\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n for (int j = 0; j < i; j++) {\n if (nums[j] > nums[j + 1]) {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n int tmp = nums[j];\n nums[j] = nums[j + 1];\n nums[j + 1] = tmp;\n }\n }\n }\n}\n
bubble_sort.rs/* \u5192\u6ce1\u6392\u5e8f */\nfn bubble_sort(nums: &mut [i32]) {\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for i in (1..nums.len()).rev() {\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef \n for j in 0..i {\n if nums[j] > nums[j + 1] {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n let tmp = nums[j];\n nums[j] = nums[j + 1];\n nums[j + 1] = tmp;\n }\n }\n }\n}\n
bubble_sort.c/* \u5192\u6ce1\u6392\u5e8f */\nvoid bubbleSort(int nums[], int size) {\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for (int i = size - 1; i > 0; i--) {\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n for (int j = 0; j < size - 1 - i; j++) {\n if (nums[j] > nums[j + 1]) {\n int temp = nums[j];\n nums[j] = nums[j + 1];\n nums[j + 1] = temp;\n }\n }\n }\n}\n
bubble_sort.zig// \u5192\u6ce1\u6392\u5e8f\nfn bubbleSort(nums: []i32) void {\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n var i: usize = nums.len - 1;\n while (i > 0) : (i -= 1) {\n var j: usize = 0;\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef \n while (j < i) : (j += 1) {\n if (nums[j] > nums[j + 1]) {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n var tmp = nums[j];\n nums[j] = nums[j + 1];\n nums[j + 1] = tmp;\n }\n }\n }\n}\n
"},{"location":"chapter_sorting/bubble_sort/#1132","title":"11.3.2 \u00a0 \u6548\u7387\u4f18\u5316","text":"\u6211\u4eec\u53d1\u73b0\uff0c\u5982\u679c\u67d0\u8f6e\u201c\u5192\u6ce1\u201d\u4e2d\u6ca1\u6709\u6267\u884c\u4efb\u4f55\u4ea4\u6362\u64cd\u4f5c\uff0c\u8bf4\u660e\u6570\u7ec4\u5df2\u7ecf\u5b8c\u6210\u6392\u5e8f\uff0c\u53ef\u76f4\u63a5\u8fd4\u56de\u7ed3\u679c\u3002\u56e0\u6b64\uff0c\u53ef\u4ee5\u589e\u52a0\u4e00\u4e2a\u6807\u5fd7\u4f4d flag
\u6765\u76d1\u6d4b\u8fd9\u79cd\u60c5\u51b5\uff0c\u4e00\u65e6\u51fa\u73b0\u5c31\u7acb\u5373\u8fd4\u56de\u3002
\u7ecf\u8fc7\u4f18\u5316\uff0c\u5192\u6ce1\u6392\u5e8f\u7684\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6\u548c\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u4ecd\u4e3a \\(O(n^2)\\) \uff1b\u4f46\u5f53\u8f93\u5165\u6570\u7ec4\u5b8c\u5168\u6709\u5e8f\u65f6\uff0c\u53ef\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 \\(O(n)\\) \u3002
PythonC++JavaC#GoSwiftJSTSDartRustCZig bubble_sort.pydef bubble_sort_with_flag(nums: list[int]):\n \"\"\"\u5192\u6ce1\u6392\u5e8f\uff08\u6807\u5fd7\u4f18\u5316\uff09\"\"\"\n n = len(nums)\n # \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for i in range(n - 1, 0, -1):\n flag = False # \u521d\u59cb\u5316\u6807\u5fd7\u4f4d\n # \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n for j in range(i):\n if nums[j] > nums[j + 1]:\n # \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n nums[j], nums[j + 1] = nums[j + 1], nums[j]\n flag = True # \u8bb0\u5f55\u4ea4\u6362\u5143\u7d20\n if not flag:\n break # \u6b64\u8f6e\u5192\u6ce1\u672a\u4ea4\u6362\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\n
bubble_sort.cpp/* \u5192\u6ce1\u6392\u5e8f\uff08\u6807\u5fd7\u4f18\u5316\uff09*/\nvoid bubbleSortWithFlag(vector<int> &nums) {\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for (int i = nums.size() - 1; i > 0; i--) {\n bool flag = false; // \u521d\u59cb\u5316\u6807\u5fd7\u4f4d\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n for (int j = 0; j < i; j++) {\n if (nums[j] > nums[j + 1]) {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n // \u8fd9\u91cc\u4f7f\u7528\u4e86 std::swap() \u51fd\u6570\n swap(nums[j], nums[j + 1]);\n flag = true; // \u8bb0\u5f55\u4ea4\u6362\u5143\u7d20\n }\n }\n if (!flag)\n break; // \u6b64\u8f6e\u5192\u6ce1\u672a\u4ea4\u6362\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\n }\n}\n
bubble_sort.java/* \u5192\u6ce1\u6392\u5e8f\uff08\u6807\u5fd7\u4f18\u5316\uff09 */\nvoid bubbleSortWithFlag(int[] nums) {\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for (int i = nums.length - 1; i > 0; i--) {\n boolean flag = false; // \u521d\u59cb\u5316\u6807\u5fd7\u4f4d\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n for (int j = 0; j < i; j++) {\n if (nums[j] > nums[j + 1]) {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n int tmp = nums[j];\n nums[j] = nums[j + 1];\n nums[j + 1] = tmp;\n flag = true; // \u8bb0\u5f55\u4ea4\u6362\u5143\u7d20\n }\n }\n if (!flag)\n break; // \u6b64\u8f6e\u5192\u6ce1\u672a\u4ea4\u6362\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\n }\n}\n
bubble_sort.cs/* \u5192\u6ce1\u6392\u5e8f\uff08\u6807\u5fd7\u4f18\u5316\uff09*/\nvoid BubbleSortWithFlag(int[] nums) {\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for (int i = nums.Length - 1; i > 0; i--) {\n bool flag = false; // \u521d\u59cb\u5316\u6807\u5fd7\u4f4d\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef \n for (int j = 0; j < i; j++) {\n if (nums[j] > nums[j + 1]) {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n (nums[j + 1], nums[j]) = (nums[j], nums[j + 1]);\n flag = true; // \u8bb0\u5f55\u4ea4\u6362\u5143\u7d20\n }\n }\n if (!flag) break; // \u6b64\u8f6e\u5192\u6ce1\u672a\u4ea4\u6362\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\n }\n}\n
bubble_sort.go/* \u5192\u6ce1\u6392\u5e8f\uff08\u6807\u5fd7\u4f18\u5316\uff09*/\nfunc bubbleSortWithFlag(nums []int) {\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for i := len(nums) - 1; i > 0; i-- {\n flag := false // \u521d\u59cb\u5316\u6807\u5fd7\u4f4d\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n for j := 0; j < i; j++ {\n if nums[j] > nums[j+1] {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n nums[j], nums[j+1] = nums[j+1], nums[j]\n flag = true // \u8bb0\u5f55\u4ea4\u6362\u5143\u7d20\n }\n }\n if flag == false { // \u6b64\u8f6e\u5192\u6ce1\u672a\u4ea4\u6362\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\n break\n }\n }\n}\n
bubble_sort.swift/* \u5192\u6ce1\u6392\u5e8f\uff08\u6807\u5fd7\u4f18\u5316\uff09*/\nfunc bubbleSortWithFlag(nums: inout [Int]) {\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for i in stride(from: nums.count - 1, to: 0, by: -1) {\n var flag = false // \u521d\u59cb\u5316\u6807\u5fd7\u4f4d\n for j in stride(from: 0, to: i, by: 1) {\n if nums[j] > nums[j + 1] {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n let tmp = nums[j]\n nums[j] = nums[j + 1]\n nums[j + 1] = tmp\n flag = true // \u8bb0\u5f55\u4ea4\u6362\u5143\u7d20\n }\n }\n if !flag { // \u6b64\u8f6e\u5192\u6ce1\u672a\u4ea4\u6362\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\n break\n }\n }\n}\n
bubble_sort.js/* \u5192\u6ce1\u6392\u5e8f\uff08\u6807\u5fd7\u4f18\u5316\uff09*/\nfunction bubbleSortWithFlag(nums) {\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for (let i = nums.length - 1; i > 0; i--) {\n let flag = false; // \u521d\u59cb\u5316\u6807\u5fd7\u4f4d\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n for (let j = 0; j < i; j++) {\n if (nums[j] > nums[j + 1]) {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n let tmp = nums[j];\n nums[j] = nums[j + 1];\n nums[j + 1] = tmp;\n flag = true; // \u8bb0\u5f55\u4ea4\u6362\u5143\u7d20\n }\n }\n if (!flag) break; // \u6b64\u8f6e\u5192\u6ce1\u672a\u4ea4\u6362\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\n }\n}\n
bubble_sort.ts/* \u5192\u6ce1\u6392\u5e8f\uff08\u6807\u5fd7\u4f18\u5316\uff09*/\nfunction bubbleSortWithFlag(nums: number[]): void {\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for (let i = nums.length - 1; i > 0; i--) {\n let flag = false; // \u521d\u59cb\u5316\u6807\u5fd7\u4f4d\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n for (let j = 0; j < i; j++) {\n if (nums[j] > nums[j + 1]) {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n let tmp = nums[j];\n nums[j] = nums[j + 1];\n nums[j + 1] = tmp;\n flag = true; // \u8bb0\u5f55\u4ea4\u6362\u5143\u7d20\n }\n }\n if (!flag) break; // \u6b64\u8f6e\u5192\u6ce1\u672a\u4ea4\u6362\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\n }\n}\n
bubble_sort.dart/* \u5192\u6ce1\u6392\u5e8f\uff08\u6807\u5fd7\u4f18\u5316\uff09*/\nvoid bubbleSortWithFlag(List<int> nums) {\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for (int i = nums.length - 1; i > 0; i--) {\n bool flag = false; // \u521d\u59cb\u5316\u6807\u5fd7\u4f4d\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n for (int j = 0; j < i; j++) {\n if (nums[j] > nums[j + 1]) {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n int tmp = nums[j];\n nums[j] = nums[j + 1];\n nums[j + 1] = tmp;\n flag = true; // \u8bb0\u5f55\u4ea4\u6362\u5143\u7d20\n }\n }\n if (!flag) break; // \u6b64\u8f6e\u5192\u6ce1\u672a\u4ea4\u6362\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\n }\n}\n
bubble_sort.rs/* \u5192\u6ce1\u6392\u5e8f\uff08\u6807\u5fd7\u4f18\u5316\uff09 */\nfn bubble_sort_with_flag(nums: &mut [i32]) {\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for i in (1..nums.len()).rev() {\n let mut flag = false; // \u521d\u59cb\u5316\u6807\u5fd7\u4f4d\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef \n for j in 0..i {\n if nums[j] > nums[j + 1] {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n let tmp = nums[j];\n nums[j] = nums[j + 1];\n nums[j + 1] = tmp;\n flag = true; // \u8bb0\u5f55\u4ea4\u6362\u5143\u7d20\n }\n }\n if !flag {break}; // \u6b64\u8f6e\u5192\u6ce1\u672a\u4ea4\u6362\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\n }\n}\n
bubble_sort.c/* \u5192\u6ce1\u6392\u5e8f\uff08\u6807\u5fd7\u4f18\u5316\uff09*/\nvoid bubbleSortWithFlag(int nums[], int size) {\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for (int i = size - 1; i > 0; i--) {\n bool flag = false;\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n for (int j = 0; j < size - 1 - i; j++) {\n if (nums[j] > nums[j + 1]) {\n int temp = nums[j];\n nums[j] = nums[j + 1];\n nums[j + 1] = temp;\n flag = true;\n }\n }\n if (!flag)\n break;\n }\n}\n
bubble_sort.zig// \u5192\u6ce1\u6392\u5e8f\uff08\u6807\u5fd7\u4f18\u5316\uff09\nfn bubbleSortWithFlag(nums: []i32) void {\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n var i: usize = nums.len - 1;\n while (i > 0) : (i -= 1) {\n var flag = false; // \u521d\u59cb\u5316\u6807\u5fd7\u4f4d\n var j: usize = 0;\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef \n while (j < i) : (j += 1) {\n if (nums[j] > nums[j + 1]) {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n var tmp = nums[j];\n nums[j] = nums[j + 1];\n nums[j + 1] = tmp;\n flag = true;\n }\n }\n if (!flag) break; // \u6b64\u8f6e\u5192\u6ce1\u672a\u4ea4\u6362\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\n }\n}\n
"},{"location":"chapter_sorting/bubble_sort/#1133","title":"11.3.3 \u00a0 \u7b97\u6cd5\u7279\u6027","text":"flag
\u4f18\u5316\u540e\uff0c\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6\u53ef\u8fbe\u5230 \\(O(n)\\) \u3002\u524d\u8ff0\u51e0\u79cd\u6392\u5e8f\u7b97\u6cd5\u90fd\u5c5e\u4e8e\u201c\u57fa\u4e8e\u6bd4\u8f83\u7684\u6392\u5e8f\u7b97\u6cd5\u201d\uff0c\u5b83\u4eec\u901a\u8fc7\u6bd4\u8f83\u5143\u7d20\u95f4\u7684\u5927\u5c0f\u6765\u5b9e\u73b0\u6392\u5e8f\u3002\u6b64\u7c7b\u6392\u5e8f\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u65e0\u6cd5\u8d85\u8d8a \\(O(n \\log n)\\) \u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u63a2\u8ba8\u51e0\u79cd\u201c\u975e\u6bd4\u8f83\u6392\u5e8f\u7b97\u6cd5\u201d\uff0c\u5b83\u4eec\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u53ef\u4ee5\u8fbe\u5230\u7ebf\u6027\u9636\u3002
\u300c\u6876\u6392\u5e8f bucket sort\u300d\u662f\u5206\u6cbb\u7b56\u7565\u7684\u4e00\u4e2a\u5178\u578b\u5e94\u7528\u3002\u5b83\u901a\u8fc7\u8bbe\u7f6e\u4e00\u4e9b\u5177\u6709\u5927\u5c0f\u987a\u5e8f\u7684\u6876\uff0c\u6bcf\u4e2a\u6876\u5bf9\u5e94\u4e00\u4e2a\u6570\u636e\u8303\u56f4\uff0c\u5c06\u6570\u636e\u5e73\u5747\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\uff1b\u7136\u540e\uff0c\u5728\u6bcf\u4e2a\u6876\u5185\u90e8\u5206\u522b\u6267\u884c\u6392\u5e8f\uff1b\u6700\u7ec8\u6309\u7167\u6876\u7684\u987a\u5e8f\u5c06\u6240\u6709\u6570\u636e\u5408\u5e76\u3002
"},{"location":"chapter_sorting/bucket_sort/#1181","title":"11.8.1 \u00a0 \u7b97\u6cd5\u6d41\u7a0b","text":"\u8003\u8651\u4e00\u4e2a\u957f\u5ea6\u4e3a \\(n\\) \u7684\u6570\u7ec4\uff0c\u5176\u5143\u7d20\u662f\u8303\u56f4 \\([0, 1)\\) \u5185\u7684\u6d6e\u70b9\u6570\u3002\u6876\u6392\u5e8f\u7684\u6d41\u7a0b\u5982\u56fe 11-13 \u6240\u793a\u3002
\u56fe 11-13 \u00a0 \u6876\u6392\u5e8f\u7b97\u6cd5\u6d41\u7a0b
\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig bucket_sort.pydef bucket_sort(nums: list[float]):\n \"\"\"\u6876\u6392\u5e8f\"\"\"\n # \u521d\u59cb\u5316 k = n/2 \u4e2a\u6876\uff0c\u9884\u671f\u5411\u6bcf\u4e2a\u6876\u5206\u914d 2 \u4e2a\u5143\u7d20\n k = len(nums) // 2\n buckets = [[] for _ in range(k)]\n # 1. \u5c06\u6570\u7ec4\u5143\u7d20\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\n for num in nums:\n # \u8f93\u5165\u6570\u636e\u8303\u56f4\u4e3a [0, 1)\uff0c\u4f7f\u7528 num * k \u6620\u5c04\u5230\u7d22\u5f15\u8303\u56f4 [0, k-1]\n i = int(num * k)\n # \u5c06 num \u6dfb\u52a0\u8fdb\u6876 i\n buckets[i].append(num)\n # 2. \u5bf9\u5404\u4e2a\u6876\u6267\u884c\u6392\u5e8f\n for bucket in buckets:\n # \u4f7f\u7528\u5185\u7f6e\u6392\u5e8f\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u66ff\u6362\u6210\u5176\u4ed6\u6392\u5e8f\u7b97\u6cd5\n bucket.sort()\n # 3. \u904d\u5386\u6876\u5408\u5e76\u7ed3\u679c\n i = 0\n for bucket in buckets:\n for num in bucket:\n nums[i] = num\n i += 1\n
bucket_sort.cpp/* \u6876\u6392\u5e8f */\nvoid bucketSort(vector<float> &nums) {\n // \u521d\u59cb\u5316 k = n/2 \u4e2a\u6876\uff0c\u9884\u671f\u5411\u6bcf\u4e2a\u6876\u5206\u914d 2 \u4e2a\u5143\u7d20\n int k = nums.size() / 2;\n vector<vector<float>> buckets(k);\n // 1. \u5c06\u6570\u7ec4\u5143\u7d20\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\n for (float num : nums) {\n // \u8f93\u5165\u6570\u636e\u8303\u56f4\u4e3a [0, 1)\uff0c\u4f7f\u7528 num * k \u6620\u5c04\u5230\u7d22\u5f15\u8303\u56f4 [0, k-1]\n int i = num * k;\n // \u5c06 num \u6dfb\u52a0\u8fdb\u6876 bucket_idx\n buckets[i].push_back(num);\n }\n // 2. \u5bf9\u5404\u4e2a\u6876\u6267\u884c\u6392\u5e8f\n for (vector<float> &bucket : buckets) {\n // \u4f7f\u7528\u5185\u7f6e\u6392\u5e8f\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u66ff\u6362\u6210\u5176\u4ed6\u6392\u5e8f\u7b97\u6cd5\n sort(bucket.begin(), bucket.end());\n }\n // 3. \u904d\u5386\u6876\u5408\u5e76\u7ed3\u679c\n int i = 0;\n for (vector<float> &bucket : buckets) {\n for (float num : bucket) {\n nums[i++] = num;\n }\n }\n}\n
bucket_sort.java/* \u6876\u6392\u5e8f */\nvoid bucketSort(float[] nums) {\n // \u521d\u59cb\u5316 k = n/2 \u4e2a\u6876\uff0c\u9884\u671f\u5411\u6bcf\u4e2a\u6876\u5206\u914d 2 \u4e2a\u5143\u7d20\n int k = nums.length / 2;\n List<List<Float>> buckets = new ArrayList<>();\n for (int i = 0; i < k; i++) {\n buckets.add(new ArrayList<>());\n }\n // 1. \u5c06\u6570\u7ec4\u5143\u7d20\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\n for (float num : nums) {\n // \u8f93\u5165\u6570\u636e\u8303\u56f4\u4e3a [0, 1)\uff0c\u4f7f\u7528 num * k \u6620\u5c04\u5230\u7d22\u5f15\u8303\u56f4 [0, k-1]\n int i = (int) (num * k);\n // \u5c06 num \u6dfb\u52a0\u8fdb\u6876 i\n buckets.get(i).add(num);\n }\n // 2. \u5bf9\u5404\u4e2a\u6876\u6267\u884c\u6392\u5e8f\n for (List<Float> bucket : buckets) {\n // \u4f7f\u7528\u5185\u7f6e\u6392\u5e8f\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u66ff\u6362\u6210\u5176\u4ed6\u6392\u5e8f\u7b97\u6cd5\n Collections.sort(bucket);\n }\n // 3. \u904d\u5386\u6876\u5408\u5e76\u7ed3\u679c\n int i = 0;\n for (List<Float> bucket : buckets) {\n for (float num : bucket) {\n nums[i++] = num;\n }\n }\n}\n
bucket_sort.cs/* \u6876\u6392\u5e8f */\nvoid BucketSort(float[] nums) {\n // \u521d\u59cb\u5316 k = n/2 \u4e2a\u6876\uff0c\u9884\u671f\u5411\u6bcf\u4e2a\u6876\u5206\u914d 2 \u4e2a\u5143\u7d20\n int k = nums.Length / 2;\n List<List<float>> buckets = [];\n for (int i = 0; i < k; i++) {\n buckets.Add([]);\n }\n // 1. \u5c06\u6570\u7ec4\u5143\u7d20\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\n foreach (float num in nums) {\n // \u8f93\u5165\u6570\u636e\u8303\u56f4\u4e3a [0, 1)\uff0c\u4f7f\u7528 num * k \u6620\u5c04\u5230\u7d22\u5f15\u8303\u56f4 [0, k-1]\n int i = (int)(num * k);\n // \u5c06 num \u6dfb\u52a0\u8fdb\u6876 i\n buckets[i].Add(num);\n }\n // 2. \u5bf9\u5404\u4e2a\u6876\u6267\u884c\u6392\u5e8f\n foreach (List<float> bucket in buckets) {\n // \u4f7f\u7528\u5185\u7f6e\u6392\u5e8f\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u66ff\u6362\u6210\u5176\u4ed6\u6392\u5e8f\u7b97\u6cd5\n bucket.Sort();\n }\n // 3. \u904d\u5386\u6876\u5408\u5e76\u7ed3\u679c\n int j = 0;\n foreach (List<float> bucket in buckets) {\n foreach (float num in bucket) {\n nums[j++] = num;\n }\n }\n}\n
bucket_sort.go/* \u6876\u6392\u5e8f */\nfunc bucketSort(nums []float64) {\n // \u521d\u59cb\u5316 k = n/2 \u4e2a\u6876\uff0c\u9884\u671f\u5411\u6bcf\u4e2a\u6876\u5206\u914d 2 \u4e2a\u5143\u7d20\n k := len(nums) / 2\n buckets := make([][]float64, k)\n for i := 0; i < k; i++ {\n buckets[i] = make([]float64, 0)\n }\n // 1. \u5c06\u6570\u7ec4\u5143\u7d20\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\n for _, num := range nums {\n // \u8f93\u5165\u6570\u636e\u8303\u56f4\u4e3a [0, 1)\uff0c\u4f7f\u7528 num * k \u6620\u5c04\u5230\u7d22\u5f15\u8303\u56f4 [0, k-1]\n i := int(num * float64(k))\n // \u5c06 num \u6dfb\u52a0\u8fdb\u6876 i\n buckets[i] = append(buckets[i], num)\n }\n // 2. \u5bf9\u5404\u4e2a\u6876\u6267\u884c\u6392\u5e8f\n for i := 0; i < k; i++ {\n // \u4f7f\u7528\u5185\u7f6e\u5207\u7247\u6392\u5e8f\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u66ff\u6362\u6210\u5176\u4ed6\u6392\u5e8f\u7b97\u6cd5\n sort.Float64s(buckets[i])\n }\n // 3. \u904d\u5386\u6876\u5408\u5e76\u7ed3\u679c\n i := 0\n for _, bucket := range buckets {\n for _, num := range bucket {\n nums[i] = num\n i++\n }\n }\n}\n
bucket_sort.swift/* \u6876\u6392\u5e8f */\nfunc bucketSort(nums: inout [Double]) {\n // \u521d\u59cb\u5316 k = n/2 \u4e2a\u6876\uff0c\u9884\u671f\u5411\u6bcf\u4e2a\u6876\u5206\u914d 2 \u4e2a\u5143\u7d20\n let k = nums.count / 2\n var buckets = (0 ..< k).map { _ in [Double]() }\n // 1. \u5c06\u6570\u7ec4\u5143\u7d20\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\n for num in nums {\n // \u8f93\u5165\u6570\u636e\u8303\u56f4\u4e3a [0, 1)\uff0c\u4f7f\u7528 num * k \u6620\u5c04\u5230\u7d22\u5f15\u8303\u56f4 [0, k-1]\n let i = Int(num * Double(k))\n // \u5c06 num \u6dfb\u52a0\u8fdb\u6876 i\n buckets[i].append(num)\n }\n // 2. \u5bf9\u5404\u4e2a\u6876\u6267\u884c\u6392\u5e8f\n for i in buckets.indices {\n // \u4f7f\u7528\u5185\u7f6e\u6392\u5e8f\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u66ff\u6362\u6210\u5176\u4ed6\u6392\u5e8f\u7b97\u6cd5\n buckets[i].sort()\n }\n // 3. \u904d\u5386\u6876\u5408\u5e76\u7ed3\u679c\n var i = nums.startIndex\n for bucket in buckets {\n for num in bucket {\n nums[i] = num\n nums.formIndex(after: &i)\n }\n }\n}\n
bucket_sort.js/* \u6876\u6392\u5e8f */\nfunction bucketSort(nums) {\n // \u521d\u59cb\u5316 k = n/2 \u4e2a\u6876\uff0c\u9884\u671f\u5411\u6bcf\u4e2a\u6876\u5206\u914d 2 \u4e2a\u5143\u7d20\n const k = nums.length / 2;\n const buckets = [];\n for (let i = 0; i < k; i++) {\n buckets.push([]);\n }\n // 1. \u5c06\u6570\u7ec4\u5143\u7d20\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\n for (const num of nums) {\n // \u8f93\u5165\u6570\u636e\u8303\u56f4\u4e3a [0, 1)\uff0c\u4f7f\u7528 num * k \u6620\u5c04\u5230\u7d22\u5f15\u8303\u56f4 [0, k-1]\n const i = Math.floor(num * k);\n // \u5c06 num \u6dfb\u52a0\u8fdb\u6876 i\n buckets[i].push(num);\n }\n // 2. \u5bf9\u5404\u4e2a\u6876\u6267\u884c\u6392\u5e8f\n for (const bucket of buckets) {\n // \u4f7f\u7528\u5185\u7f6e\u6392\u5e8f\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u66ff\u6362\u6210\u5176\u4ed6\u6392\u5e8f\u7b97\u6cd5\n bucket.sort((a, b) => a - b);\n }\n // 3. \u904d\u5386\u6876\u5408\u5e76\u7ed3\u679c\n let i = 0;\n for (const bucket of buckets) {\n for (const num of bucket) {\n nums[i++] = num;\n }\n }\n}\n
bucket_sort.ts/* \u6876\u6392\u5e8f */\nfunction bucketSort(nums: number[]): void {\n // \u521d\u59cb\u5316 k = n/2 \u4e2a\u6876\uff0c\u9884\u671f\u5411\u6bcf\u4e2a\u6876\u5206\u914d 2 \u4e2a\u5143\u7d20\n const k = nums.length / 2;\n const buckets: number[][] = [];\n for (let i = 0; i < k; i++) {\n buckets.push([]);\n }\n // 1. \u5c06\u6570\u7ec4\u5143\u7d20\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\n for (const num of nums) {\n // \u8f93\u5165\u6570\u636e\u8303\u56f4\u4e3a [0, 1)\uff0c\u4f7f\u7528 num * k \u6620\u5c04\u5230\u7d22\u5f15\u8303\u56f4 [0, k-1]\n const i = Math.floor(num * k);\n // \u5c06 num \u6dfb\u52a0\u8fdb\u6876 i\n buckets[i].push(num);\n }\n // 2. \u5bf9\u5404\u4e2a\u6876\u6267\u884c\u6392\u5e8f\n for (const bucket of buckets) {\n // \u4f7f\u7528\u5185\u7f6e\u6392\u5e8f\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u66ff\u6362\u6210\u5176\u4ed6\u6392\u5e8f\u7b97\u6cd5\n bucket.sort((a, b) => a - b);\n }\n // 3. \u904d\u5386\u6876\u5408\u5e76\u7ed3\u679c\n let i = 0;\n for (const bucket of buckets) {\n for (const num of bucket) {\n nums[i++] = num;\n }\n }\n}\n
bucket_sort.dart/* \u6876\u6392\u5e8f */\nvoid bucketSort(List<double> nums) {\n // \u521d\u59cb\u5316 k = n/2 \u4e2a\u6876\uff0c\u9884\u671f\u5411\u6bcf\u4e2a\u6876\u5206\u914d 2 \u4e2a\u5143\u7d20\n int k = nums.length ~/ 2;\n List<List<double>> buckets = List.generate(k, (index) => []);\n\n // 1. \u5c06\u6570\u7ec4\u5143\u7d20\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\n for (double _num in nums) {\n // \u8f93\u5165\u6570\u636e\u8303\u56f4\u4e3a [0, 1)\uff0c\u4f7f\u7528 _num * k \u6620\u5c04\u5230\u7d22\u5f15\u8303\u56f4 [0, k-1]\n int i = (_num * k).toInt();\n // \u5c06 _num \u6dfb\u52a0\u8fdb\u6876 bucket_idx\n buckets[i].add(_num);\n }\n // 2. \u5bf9\u5404\u4e2a\u6876\u6267\u884c\u6392\u5e8f\n for (List<double> bucket in buckets) {\n bucket.sort();\n }\n // 3. \u904d\u5386\u6876\u5408\u5e76\u7ed3\u679c\n int i = 0;\n for (List<double> bucket in buckets) {\n for (double _num in bucket) {\n nums[i++] = _num;\n }\n }\n}\n
bucket_sort.rs/* \u6876\u6392\u5e8f */\nfn bucket_sort(nums: &mut [f64]) {\n // \u521d\u59cb\u5316 k = n/2 \u4e2a\u6876\uff0c\u9884\u671f\u5411\u6bcf\u4e2a\u6876\u5206\u914d 2 \u4e2a\u5143\u7d20\n let k = nums.len() / 2;\n let mut buckets = vec![vec![]; k];\n // 1. \u5c06\u6570\u7ec4\u5143\u7d20\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\n for &mut num in &mut *nums {\n // \u8f93\u5165\u6570\u636e\u8303\u56f4\u4e3a [0, 1)\uff0c\u4f7f\u7528 num * k \u6620\u5c04\u5230\u7d22\u5f15\u8303\u56f4 [0, k-1]\n let i = (num * k as f64) as usize;\n // \u5c06 num \u6dfb\u52a0\u8fdb\u6876 i\n buckets[i].push(num);\n }\n // 2. \u5bf9\u5404\u4e2a\u6876\u6267\u884c\u6392\u5e8f\n for bucket in &mut buckets {\n // \u4f7f\u7528\u5185\u7f6e\u6392\u5e8f\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u66ff\u6362\u6210\u5176\u4ed6\u6392\u5e8f\u7b97\u6cd5\n bucket.sort_by(|a, b| a.partial_cmp(b).unwrap());\n }\n // 3. \u904d\u5386\u6876\u5408\u5e76\u7ed3\u679c\n let mut i = 0;\n for bucket in &mut buckets {\n for &mut num in bucket {\n nums[i] = num;\n i += 1;\n }\n }\n}\n
bucket_sort.c/* \u6876\u6392\u5e8f */\nvoid bucketSort(float nums[], int size) {\n // \u521d\u59cb\u5316 k = n/2 \u4e2a\u6876\uff0c\u9884\u671f\u5411\u6bcf\u4e2a\u6876\u5206\u914d 2 \u4e2a\u5143\u7d20\n int k = size / 2;\n float **buckets = calloc(k, sizeof(float *));\n for (int i = 0; i < k; i++) {\n // \u6bcf\u4e2a\u6876\u6700\u591a\u53ef\u4ee5\u5206\u914d k \u4e2a\u5143\u7d20\n buckets[i] = calloc(ARRAY_SIZE, sizeof(float));\n }\n\n // 1. \u5c06\u6570\u7ec4\u5143\u7d20\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\n for (int i = 0; i < size; i++) {\n // \u8f93\u5165\u6570\u636e\u8303\u56f4\u4e3a [0, 1)\uff0c\u4f7f\u7528 num * k \u6620\u5c04\u5230\u7d22\u5f15\u8303\u56f4 [0, k-1]\n int bucket_idx = nums[i] * k;\n int j = 0;\n // \u5982\u679c\u6876\u4e2d\u6709\u6570\u636e\u4e14\u6570\u636e\u5c0f\u4e8e\u5f53\u524d\u503c nums[i], \u8981\u5c06\u5176\u653e\u5230\u5f53\u524d\u6876\u7684\u540e\u9762\uff0c\u76f8\u5f53\u4e8e cpp \u4e2d\u7684 push_back\n while (buckets[bucket_idx][j] > 0 && buckets[bucket_idx][j] < nums[i]) {\n j++;\n }\n float temp = nums[i];\n while (j < ARRAY_SIZE && buckets[bucket_idx][j] > 0) {\n swap(&temp, &buckets[bucket_idx][j]);\n j++;\n }\n buckets[bucket_idx][j] = temp;\n }\n\n // 2. \u5bf9\u5404\u4e2a\u6876\u6267\u884c\u6392\u5e8f\n for (int i = 0; i < k; i++) {\n qsort(buckets[i], ARRAY_SIZE, sizeof(float), compare_float);\n }\n\n // 3. \u904d\u5386\u6876\u5408\u5e76\u7ed3\u679c\n for (int i = 0, j = 0; j < k; j++) {\n for (int l = 0; l < ARRAY_SIZE; l++) {\n if (buckets[j][l] > 0) {\n nums[i++] = buckets[j][l];\n }\n }\n }\n\n // \u91ca\u653e\u4e0a\u8ff0\u5206\u914d\u7684\u5185\u5b58\n for (int i = 0; i < k; i++) {\n free(buckets[i]);\n }\n free(buckets);\n}\n
bucket_sort.zig[class]{}-[func]{bucketSort}\n
"},{"location":"chapter_sorting/bucket_sort/#1182","title":"11.8.2 \u00a0 \u7b97\u6cd5\u7279\u6027","text":"\u6876\u6392\u5e8f\u9002\u7528\u4e8e\u5904\u7406\u4f53\u91cf\u5f88\u5927\u7684\u6570\u636e\u3002\u4f8b\u5982\uff0c\u8f93\u5165\u6570\u636e\u5305\u542b 100 \u4e07\u4e2a\u5143\u7d20\uff0c\u7531\u4e8e\u7a7a\u95f4\u9650\u5236\uff0c\u7cfb\u7edf\u5185\u5b58\u65e0\u6cd5\u4e00\u6b21\u6027\u52a0\u8f7d\u6240\u6709\u6570\u636e\u3002\u6b64\u65f6\uff0c\u53ef\u4ee5\u5c06\u6570\u636e\u5206\u6210 1000 \u4e2a\u6876\uff0c\u7136\u540e\u5206\u522b\u5bf9\u6bcf\u4e2a\u6876\u8fdb\u884c\u6392\u5e8f\uff0c\u6700\u540e\u5c06\u7ed3\u679c\u5408\u5e76\u3002
\u6876\u6392\u5e8f\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u7406\u8bba\u4e0a\u53ef\u4ee5\u8fbe\u5230 \\(O(n)\\) \uff0c\u5173\u952e\u5728\u4e8e\u5c06\u5143\u7d20\u5747\u5300\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\uff0c\u56e0\u4e3a\u5b9e\u9645\u6570\u636e\u5f80\u5f80\u4e0d\u662f\u5747\u5300\u5206\u5e03\u7684\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u60f3\u8981\u5c06\u6dd8\u5b9d\u4e0a\u7684\u6240\u6709\u5546\u54c1\u6309\u4ef7\u683c\u8303\u56f4\u5e73\u5747\u5206\u914d\u5230 10 \u4e2a\u6876\u4e2d\uff0c\u4f46\u5546\u54c1\u4ef7\u683c\u5206\u5e03\u4e0d\u5747\uff0c\u4f4e\u4e8e 100 \u5143\u7684\u975e\u5e38\u591a\uff0c\u9ad8\u4e8e 1000 \u5143\u7684\u975e\u5e38\u5c11\u3002\u82e5\u5c06\u4ef7\u683c\u533a\u95f4\u5e73\u5747\u5212\u5206\u4e3a 10 \u4e2a\uff0c\u5404\u4e2a\u6876\u4e2d\u7684\u5546\u54c1\u6570\u91cf\u5dee\u8ddd\u4f1a\u975e\u5e38\u5927\u3002
\u4e3a\u5b9e\u73b0\u5e73\u5747\u5206\u914d\uff0c\u6211\u4eec\u53ef\u4ee5\u5148\u8bbe\u5b9a\u4e00\u6761\u5927\u81f4\u7684\u5206\u754c\u7ebf\uff0c\u5c06\u6570\u636e\u7c97\u7565\u5730\u5206\u5230 3 \u4e2a\u6876\u4e2d\u3002\u5206\u914d\u5b8c\u6bd5\u540e\uff0c\u518d\u5c06\u5546\u54c1\u8f83\u591a\u7684\u6876\u7ee7\u7eed\u5212\u5206\u4e3a 3 \u4e2a\u6876\uff0c\u76f4\u81f3\u6240\u6709\u6876\u4e2d\u7684\u5143\u7d20\u6570\u91cf\u5927\u81f4\u76f8\u7b49\u3002
\u5982\u56fe 11-14 \u6240\u793a\uff0c\u8fd9\u79cd\u65b9\u6cd5\u672c\u8d28\u4e0a\u662f\u521b\u5efa\u4e00\u68f5\u9012\u5f52\u6811\uff0c\u76ee\u6807\u662f\u8ba9\u53f6\u8282\u70b9\u7684\u503c\u5c3d\u53ef\u80fd\u5e73\u5747\u3002\u5f53\u7136\uff0c\u4e0d\u4e00\u5b9a\u8981\u6bcf\u8f6e\u5c06\u6570\u636e\u5212\u5206\u4e3a 3 \u4e2a\u6876\uff0c\u5177\u4f53\u5212\u5206\u65b9\u5f0f\u53ef\u6839\u636e\u6570\u636e\u7279\u70b9\u7075\u6d3b\u9009\u62e9\u3002
\u56fe 11-14 \u00a0 \u9012\u5f52\u5212\u5206\u6876
\u5982\u679c\u6211\u4eec\u63d0\u524d\u77e5\u9053\u5546\u54c1\u4ef7\u683c\u7684\u6982\u7387\u5206\u5e03\uff0c\u5219\u53ef\u4ee5\u6839\u636e\u6570\u636e\u6982\u7387\u5206\u5e03\u8bbe\u7f6e\u6bcf\u4e2a\u6876\u7684\u4ef7\u683c\u5206\u754c\u7ebf\u3002\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u6570\u636e\u5206\u5e03\u5e76\u4e0d\u4e00\u5b9a\u9700\u8981\u7279\u610f\u7edf\u8ba1\uff0c\u4e5f\u53ef\u4ee5\u6839\u636e\u6570\u636e\u7279\u70b9\u91c7\u7528\u67d0\u79cd\u6982\u7387\u6a21\u578b\u8fdb\u884c\u8fd1\u4f3c\u3002
\u5982\u56fe 11-15 \u6240\u793a\uff0c\u6211\u4eec\u5047\u8bbe\u5546\u54c1\u4ef7\u683c\u670d\u4ece\u6b63\u6001\u5206\u5e03\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u5408\u7406\u5730\u8bbe\u5b9a\u4ef7\u683c\u533a\u95f4\uff0c\u4ece\u800c\u5c06\u5546\u54c1\u5e73\u5747\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\u3002
\u56fe 11-15 \u00a0 \u6839\u636e\u6982\u7387\u5206\u5e03\u5212\u5206\u6876
"},{"location":"chapter_sorting/counting_sort/","title":"11.9 \u00a0 \u8ba1\u6570\u6392\u5e8f","text":"\u300c\u8ba1\u6570\u6392\u5e8f counting sort\u300d\u901a\u8fc7\u7edf\u8ba1\u5143\u7d20\u6570\u91cf\u6765\u5b9e\u73b0\u6392\u5e8f\uff0c\u901a\u5e38\u5e94\u7528\u4e8e\u6574\u6570\u6570\u7ec4\u3002
"},{"location":"chapter_sorting/counting_sort/#1191","title":"11.9.1 \u00a0 \u7b80\u5355\u5b9e\u73b0","text":"\u5148\u6765\u770b\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\u3002\u7ed9\u5b9a\u4e00\u4e2a\u957f\u5ea6\u4e3a \\(n\\) \u7684\u6570\u7ec4 nums
\uff0c\u5176\u4e2d\u7684\u5143\u7d20\u90fd\u662f\u201c\u975e\u8d1f\u6574\u6570\u201d\uff0c\u8ba1\u6570\u6392\u5e8f\u7684\u6574\u4f53\u6d41\u7a0b\u5982\u56fe 11-16 \u6240\u793a\u3002
counter
\u3002counter
\u7edf\u8ba1 nums
\u4e2d\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\uff0c\u5176\u4e2d counter[num]
\u5bf9\u5e94\u6570\u5b57 num
\u7684\u51fa\u73b0\u6b21\u6570\u3002\u7edf\u8ba1\u65b9\u6cd5\u5f88\u7b80\u5355\uff0c\u53ea\u9700\u904d\u5386 nums
\uff08\u8bbe\u5f53\u524d\u6570\u5b57\u4e3a num
\uff09\uff0c\u6bcf\u8f6e\u5c06 counter[num]
\u589e\u52a0 \\(1\\) \u5373\u53ef\u3002counter
\u7684\u5404\u4e2a\u7d22\u5f15\u5929\u7136\u6709\u5e8f\uff0c\u56e0\u6b64\u76f8\u5f53\u4e8e\u6240\u6709\u6570\u5b57\u5df2\u7ecf\u6392\u5e8f\u597d\u4e86\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u904d\u5386 counter
\uff0c\u6839\u636e\u5404\u6570\u5b57\u51fa\u73b0\u6b21\u6570\u4ece\u5c0f\u5230\u5927\u7684\u987a\u5e8f\u586b\u5165 nums
\u5373\u53ef\u3002\u56fe 11-16 \u00a0 \u8ba1\u6570\u6392\u5e8f\u6d41\u7a0b
\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig counting_sort.pydef counting_sort_naive(nums: list[int]):\n \"\"\"\u8ba1\u6570\u6392\u5e8f\"\"\"\n # \u7b80\u5355\u5b9e\u73b0\uff0c\u65e0\u6cd5\u7528\u4e8e\u6392\u5e8f\u5bf9\u8c61\n # 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n m = 0\n for num in nums:\n m = max(m, num)\n # 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n # counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n counter = [0] * (m + 1)\n for num in nums:\n counter[num] += 1\n # 3. \u904d\u5386 counter \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u539f\u6570\u7ec4 nums\n i = 0\n for num in range(m + 1):\n for _ in range(counter[num]):\n nums[i] = num\n i += 1\n
counting_sort.cpp/* \u8ba1\u6570\u6392\u5e8f */\n// \u7b80\u5355\u5b9e\u73b0\uff0c\u65e0\u6cd5\u7528\u4e8e\u6392\u5e8f\u5bf9\u8c61\nvoid countingSortNaive(vector<int> &nums) {\n // 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n int m = 0;\n for (int num : nums) {\n m = max(m, num);\n }\n // 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n vector<int> counter(m + 1, 0);\n for (int num : nums) {\n counter[num]++;\n }\n // 3. \u904d\u5386 counter \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u539f\u6570\u7ec4 nums\n int i = 0;\n for (int num = 0; num < m + 1; num++) {\n for (int j = 0; j < counter[num]; j++, i++) {\n nums[i] = num;\n }\n }\n}\n
counting_sort.java/* \u8ba1\u6570\u6392\u5e8f */\n// \u7b80\u5355\u5b9e\u73b0\uff0c\u65e0\u6cd5\u7528\u4e8e\u6392\u5e8f\u5bf9\u8c61\nvoid countingSortNaive(int[] nums) {\n // 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n int m = 0;\n for (int num : nums) {\n m = Math.max(m, num);\n }\n // 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n int[] counter = new int[m + 1];\n for (int num : nums) {\n counter[num]++;\n }\n // 3. \u904d\u5386 counter \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u539f\u6570\u7ec4 nums\n int i = 0;\n for (int num = 0; num < m + 1; num++) {\n for (int j = 0; j < counter[num]; j++, i++) {\n nums[i] = num;\n }\n }\n}\n
counting_sort.cs/* \u8ba1\u6570\u6392\u5e8f */\n// \u7b80\u5355\u5b9e\u73b0\uff0c\u65e0\u6cd5\u7528\u4e8e\u6392\u5e8f\u5bf9\u8c61\nvoid CountingSortNaive(int[] nums) {\n // 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n int m = 0;\n foreach (int num in nums) {\n m = Math.Max(m, num);\n }\n // 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n int[] counter = new int[m + 1];\n foreach (int num in nums) {\n counter[num]++;\n }\n // 3. \u904d\u5386 counter \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u539f\u6570\u7ec4 nums\n int i = 0;\n for (int num = 0; num < m + 1; num++) {\n for (int j = 0; j < counter[num]; j++, i++) {\n nums[i] = num;\n }\n }\n}\n
counting_sort.go/* \u8ba1\u6570\u6392\u5e8f */\n// \u7b80\u5355\u5b9e\u73b0\uff0c\u65e0\u6cd5\u7528\u4e8e\u6392\u5e8f\u5bf9\u8c61\nfunc countingSortNaive(nums []int) {\n // 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n m := 0\n for _, num := range nums {\n if num > m {\n m = num\n }\n }\n // 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n counter := make([]int, m+1)\n for _, num := range nums {\n counter[num]++\n }\n // 3. \u904d\u5386 counter \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u539f\u6570\u7ec4 nums\n for i, num := 0, 0; num < m+1; num++ {\n for j := 0; j < counter[num]; j++ {\n nums[i] = num\n i++\n }\n }\n}\n
counting_sort.swift/* \u8ba1\u6570\u6392\u5e8f */\n// \u7b80\u5355\u5b9e\u73b0\uff0c\u65e0\u6cd5\u7528\u4e8e\u6392\u5e8f\u5bf9\u8c61\nfunc countingSortNaive(nums: inout [Int]) {\n // 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n let m = nums.max()!\n // 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n var counter = Array(repeating: 0, count: m + 1)\n for num in nums {\n counter[num] += 1\n }\n // 3. \u904d\u5386 counter \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u539f\u6570\u7ec4 nums\n var i = 0\n for num in stride(from: 0, to: m + 1, by: 1) {\n for _ in stride(from: 0, to: counter[num], by: 1) {\n nums[i] = num\n i += 1\n }\n }\n}\n
counting_sort.js/* \u8ba1\u6570\u6392\u5e8f */\n// \u7b80\u5355\u5b9e\u73b0\uff0c\u65e0\u6cd5\u7528\u4e8e\u6392\u5e8f\u5bf9\u8c61\nfunction countingSortNaive(nums) {\n // 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n let m = 0;\n for (const num of nums) {\n m = Math.max(m, num);\n }\n // 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n const counter = new Array(m + 1).fill(0);\n for (const num of nums) {\n counter[num]++;\n }\n // 3. \u904d\u5386 counter \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u539f\u6570\u7ec4 nums\n let i = 0;\n for (let num = 0; num < m + 1; num++) {\n for (let j = 0; j < counter[num]; j++, i++) {\n nums[i] = num;\n }\n }\n}\n
counting_sort.ts/* \u8ba1\u6570\u6392\u5e8f */\n// \u7b80\u5355\u5b9e\u73b0\uff0c\u65e0\u6cd5\u7528\u4e8e\u6392\u5e8f\u5bf9\u8c61\nfunction countingSortNaive(nums: number[]): void {\n // 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n let m = 0;\n for (const num of nums) {\n m = Math.max(m, num);\n }\n // 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n const counter: number[] = new Array<number>(m + 1).fill(0);\n for (const num of nums) {\n counter[num]++;\n }\n // 3. \u904d\u5386 counter \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u539f\u6570\u7ec4 nums\n let i = 0;\n for (let num = 0; num < m + 1; num++) {\n for (let j = 0; j < counter[num]; j++, i++) {\n nums[i] = num;\n }\n }\n}\n
counting_sort.dart/* \u8ba1\u6570\u6392\u5e8f */\n// \u7b80\u5355\u5b9e\u73b0\uff0c\u65e0\u6cd5\u7528\u4e8e\u6392\u5e8f\u5bf9\u8c61\nvoid countingSortNaive(List<int> nums) {\n // 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n int m = 0;\n for (int _num in nums) {\n m = max(m, _num);\n }\n // 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n // counter[_num] \u4ee3\u8868 _num \u7684\u51fa\u73b0\u6b21\u6570\n List<int> counter = List.filled(m + 1, 0);\n for (int _num in nums) {\n counter[_num]++;\n }\n // 3. \u904d\u5386 counter \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u539f\u6570\u7ec4 nums\n int i = 0;\n for (int _num = 0; _num < m + 1; _num++) {\n for (int j = 0; j < counter[_num]; j++, i++) {\n nums[i] = _num;\n }\n }\n}\n
counting_sort.rs/* \u8ba1\u6570\u6392\u5e8f */\n// \u7b80\u5355\u5b9e\u73b0\uff0c\u65e0\u6cd5\u7528\u4e8e\u6392\u5e8f\u5bf9\u8c61\nfn counting_sort_naive(nums: &mut [i32]) {\n // 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n let m = *nums.into_iter().max().unwrap();\n // 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n let mut counter = vec![0; m as usize + 1];\n for &num in &*nums {\n counter[num as usize] += 1;\n }\n // 3. \u904d\u5386 counter \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u539f\u6570\u7ec4 nums\n let mut i = 0;\n for num in 0..m + 1 {\n for _ in 0..counter[num as usize] {\n nums[i] = num;\n i += 1;\n }\n }\n}\n
counting_sort.c/* \u8ba1\u6570\u6392\u5e8f */\n// \u7b80\u5355\u5b9e\u73b0\uff0c\u65e0\u6cd5\u7528\u4e8e\u6392\u5e8f\u5bf9\u8c61\nvoid countingSortNaive(int nums[], int size) {\n // 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n int m = 0;\n for (int i = 0; i < size; i++) {\n if (nums[i] > m) {\n m = nums[i];\n }\n }\n // 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n int *counter = calloc(m, sizeof(int));\n for (int i = 0; i < size; i++) {\n counter[nums[i]]++;\n }\n // 3. \u904d\u5386 counter \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u539f\u6570\u7ec4 nums\n int i = 0;\n for (int num = 0; num < m + 1; num++) {\n for (int j = 0; j < counter[num]; j++, i++) {\n nums[i] = num;\n }\n }\n // 4. \u91ca\u653e\u5185\u5b58\n free(counter);\n}\n
counting_sort.zig[class]{}-[func]{countingSortNaive}\n
\u8ba1\u6570\u6392\u5e8f\u4e0e\u6876\u6392\u5e8f\u7684\u8054\u7cfb
\u4ece\u6876\u6392\u5e8f\u7684\u89d2\u5ea6\u770b\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u8ba1\u6570\u6392\u5e8f\u4e2d\u7684\u8ba1\u6570\u6570\u7ec4 counter
\u7684\u6bcf\u4e2a\u7d22\u5f15\u89c6\u4e3a\u4e00\u4e2a\u6876\uff0c\u5c06\u7edf\u8ba1\u6570\u91cf\u7684\u8fc7\u7a0b\u770b\u4f5c\u5c06\u5404\u4e2a\u5143\u7d20\u5206\u914d\u5230\u5bf9\u5e94\u7684\u6876\u4e2d\u3002\u672c\u8d28\u4e0a\uff0c\u8ba1\u6570\u6392\u5e8f\u662f\u6876\u6392\u5e8f\u5728\u6574\u578b\u6570\u636e\u4e0b\u7684\u4e00\u4e2a\u7279\u4f8b\u3002
\u7ec6\u5fc3\u7684\u8bfb\u8005\u53ef\u80fd\u53d1\u73b0\u4e86\uff0c\u5982\u679c\u8f93\u5165\u6570\u636e\u662f\u5bf9\u8c61\uff0c\u4e0a\u8ff0\u6b65\u9aa4 3.
\u5c31\u5931\u6548\u4e86\u3002\u5047\u8bbe\u8f93\u5165\u6570\u636e\u662f\u5546\u54c1\u5bf9\u8c61\uff0c\u6211\u4eec\u60f3\u6309\u7167\u5546\u54c1\u4ef7\u683c\uff08\u7c7b\u7684\u6210\u5458\u53d8\u91cf\uff09\u5bf9\u5546\u54c1\u8fdb\u884c\u6392\u5e8f\uff0c\u800c\u4e0a\u8ff0\u7b97\u6cd5\u53ea\u80fd\u7ed9\u51fa\u4ef7\u683c\u7684\u6392\u5e8f\u7ed3\u679c\u3002
\u90a3\u4e48\u5982\u4f55\u624d\u80fd\u5f97\u5230\u539f\u6570\u636e\u7684\u6392\u5e8f\u7ed3\u679c\u5462\uff1f\u6211\u4eec\u9996\u5148\u8ba1\u7b97 counter
\u7684\u201c\u524d\u7f00\u548c\u201d\u3002\u987e\u540d\u601d\u4e49\uff0c\u7d22\u5f15 i
\u5904\u7684\u524d\u7f00\u548c prefix[i]
\u7b49\u4e8e\u6570\u7ec4\u524d i
\u4e2a\u5143\u7d20\u4e4b\u548c\uff1a
\u524d\u7f00\u548c\u5177\u6709\u660e\u786e\u7684\u610f\u4e49\uff0cprefix[num] - 1
\u4ee3\u8868\u5143\u7d20 num
\u5728\u7ed3\u679c\u6570\u7ec4 res
\u4e2d\u6700\u540e\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\u3002\u8fd9\u4e2a\u4fe1\u606f\u975e\u5e38\u5173\u952e\uff0c\u56e0\u4e3a\u5b83\u544a\u8bc9\u6211\u4eec\u5404\u4e2a\u5143\u7d20\u5e94\u8be5\u51fa\u73b0\u5728\u7ed3\u679c\u6570\u7ec4\u7684\u54ea\u4e2a\u4f4d\u7f6e\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5012\u5e8f\u904d\u5386\u539f\u6570\u7ec4 nums
\u7684\u6bcf\u4e2a\u5143\u7d20 num
\uff0c\u5728\u6bcf\u8f6e\u8fed\u4ee3\u4e2d\u6267\u884c\u4ee5\u4e0b\u4e24\u6b65\u3002
num
\u586b\u5165\u6570\u7ec4 res
\u7684\u7d22\u5f15 prefix[num] - 1
\u5904\u3002prefix[num]
\u51cf\u5c0f \\(1\\) \uff0c\u4ece\u800c\u5f97\u5230\u4e0b\u6b21\u653e\u7f6e num
\u7684\u7d22\u5f15\u3002\u904d\u5386\u5b8c\u6210\u540e\uff0c\u6570\u7ec4 res
\u4e2d\u5c31\u662f\u6392\u5e8f\u597d\u7684\u7ed3\u679c\uff0c\u6700\u540e\u4f7f\u7528 res
\u8986\u76d6\u539f\u6570\u7ec4 nums
\u5373\u53ef\u3002\u56fe 11-17 \u5c55\u793a\u4e86\u5b8c\u6574\u7684\u8ba1\u6570\u6392\u5e8f\u6d41\u7a0b\u3002
\u56fe 11-17 \u00a0 \u8ba1\u6570\u6392\u5e8f\u6b65\u9aa4
\u8ba1\u6570\u6392\u5e8f\u7684\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig counting_sort.pydef counting_sort(nums: list[int]):\n \"\"\"\u8ba1\u6570\u6392\u5e8f\"\"\"\n # \u5b8c\u6574\u5b9e\u73b0\uff0c\u53ef\u6392\u5e8f\u5bf9\u8c61\uff0c\u5e76\u4e14\u662f\u7a33\u5b9a\u6392\u5e8f\n # 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n m = max(nums)\n # 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n # counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n counter = [0] * (m + 1)\n for num in nums:\n counter[num] += 1\n # 3. \u6c42 counter \u7684\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u6b21\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u5c3e\u7d22\u5f15\u201d\n # \u5373 counter[num]-1 \u662f num \u5728 res \u4e2d\u6700\u540e\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\n for i in range(m):\n counter[i + 1] += counter[i]\n # 4. \u5012\u5e8f\u904d\u5386 nums \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u7ed3\u679c\u6570\u7ec4 res\n # \u521d\u59cb\u5316\u6570\u7ec4 res \u7528\u4e8e\u8bb0\u5f55\u7ed3\u679c\n n = len(nums)\n res = [0] * n\n for i in range(n - 1, -1, -1):\n num = nums[i]\n res[counter[num] - 1] = num # \u5c06 num \u653e\u7f6e\u5230\u5bf9\u5e94\u7d22\u5f15\u5904\n counter[num] -= 1 # \u4ee4\u524d\u7f00\u548c\u81ea\u51cf 1 \uff0c\u5f97\u5230\u4e0b\u6b21\u653e\u7f6e num \u7684\u7d22\u5f15\n # \u4f7f\u7528\u7ed3\u679c\u6570\u7ec4 res \u8986\u76d6\u539f\u6570\u7ec4 nums\n for i in range(n):\n nums[i] = res[i]\n
counting_sort.cpp/* \u8ba1\u6570\u6392\u5e8f */\n// \u5b8c\u6574\u5b9e\u73b0\uff0c\u53ef\u6392\u5e8f\u5bf9\u8c61\uff0c\u5e76\u4e14\u662f\u7a33\u5b9a\u6392\u5e8f\nvoid countingSort(vector<int> &nums) {\n // 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n int m = 0;\n for (int num : nums) {\n m = max(m, num);\n }\n // 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n vector<int> counter(m + 1, 0);\n for (int num : nums) {\n counter[num]++;\n }\n // 3. \u6c42 counter \u7684\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u6b21\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u5c3e\u7d22\u5f15\u201d\n // \u5373 counter[num]-1 \u662f num \u5728 res \u4e2d\u6700\u540e\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\n for (int i = 0; i < m; i++) {\n counter[i + 1] += counter[i];\n }\n // 4. \u5012\u5e8f\u904d\u5386 nums \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u7ed3\u679c\u6570\u7ec4 res\n // \u521d\u59cb\u5316\u6570\u7ec4 res \u7528\u4e8e\u8bb0\u5f55\u7ed3\u679c\n int n = nums.size();\n vector<int> res(n);\n for (int i = n - 1; i >= 0; i--) {\n int num = nums[i];\n res[counter[num] - 1] = num; // \u5c06 num \u653e\u7f6e\u5230\u5bf9\u5e94\u7d22\u5f15\u5904\n counter[num]--; // \u4ee4\u524d\u7f00\u548c\u81ea\u51cf 1 \uff0c\u5f97\u5230\u4e0b\u6b21\u653e\u7f6e num \u7684\u7d22\u5f15\n }\n // \u4f7f\u7528\u7ed3\u679c\u6570\u7ec4 res \u8986\u76d6\u539f\u6570\u7ec4 nums\n nums = res;\n}\n
counting_sort.java/* \u8ba1\u6570\u6392\u5e8f */\n// \u5b8c\u6574\u5b9e\u73b0\uff0c\u53ef\u6392\u5e8f\u5bf9\u8c61\uff0c\u5e76\u4e14\u662f\u7a33\u5b9a\u6392\u5e8f\nvoid countingSort(int[] nums) {\n // 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n int m = 0;\n for (int num : nums) {\n m = Math.max(m, num);\n }\n // 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n int[] counter = new int[m + 1];\n for (int num : nums) {\n counter[num]++;\n }\n // 3. \u6c42 counter \u7684\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u6b21\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u5c3e\u7d22\u5f15\u201d\n // \u5373 counter[num]-1 \u662f num \u5728 res \u4e2d\u6700\u540e\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\n for (int i = 0; i < m; i++) {\n counter[i + 1] += counter[i];\n }\n // 4. \u5012\u5e8f\u904d\u5386 nums \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u7ed3\u679c\u6570\u7ec4 res\n // \u521d\u59cb\u5316\u6570\u7ec4 res \u7528\u4e8e\u8bb0\u5f55\u7ed3\u679c\n int n = nums.length;\n int[] res = new int[n];\n for (int i = n - 1; i >= 0; i--) {\n int num = nums[i];\n res[counter[num] - 1] = num; // \u5c06 num \u653e\u7f6e\u5230\u5bf9\u5e94\u7d22\u5f15\u5904\n counter[num]--; // \u4ee4\u524d\u7f00\u548c\u81ea\u51cf 1 \uff0c\u5f97\u5230\u4e0b\u6b21\u653e\u7f6e num \u7684\u7d22\u5f15\n }\n // \u4f7f\u7528\u7ed3\u679c\u6570\u7ec4 res \u8986\u76d6\u539f\u6570\u7ec4 nums\n for (int i = 0; i < n; i++) {\n nums[i] = res[i];\n }\n}\n
counting_sort.cs/* \u8ba1\u6570\u6392\u5e8f */\n// \u5b8c\u6574\u5b9e\u73b0\uff0c\u53ef\u6392\u5e8f\u5bf9\u8c61\uff0c\u5e76\u4e14\u662f\u7a33\u5b9a\u6392\u5e8f\nvoid CountingSort(int[] nums) {\n // 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n int m = 0;\n foreach (int num in nums) {\n m = Math.Max(m, num);\n }\n // 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n int[] counter = new int[m + 1];\n foreach (int num in nums) {\n counter[num]++;\n }\n // 3. \u6c42 counter \u7684\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u6b21\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u5c3e\u7d22\u5f15\u201d\n // \u5373 counter[num]-1 \u662f num \u5728 res \u4e2d\u6700\u540e\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\n for (int i = 0; i < m; i++) {\n counter[i + 1] += counter[i];\n }\n // 4. \u5012\u5e8f\u904d\u5386 nums \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u7ed3\u679c\u6570\u7ec4 res\n // \u521d\u59cb\u5316\u6570\u7ec4 res \u7528\u4e8e\u8bb0\u5f55\u7ed3\u679c\n int n = nums.Length;\n int[] res = new int[n];\n for (int i = n - 1; i >= 0; i--) {\n int num = nums[i];\n res[counter[num] - 1] = num; // \u5c06 num \u653e\u7f6e\u5230\u5bf9\u5e94\u7d22\u5f15\u5904\n counter[num]--; // \u4ee4\u524d\u7f00\u548c\u81ea\u51cf 1 \uff0c\u5f97\u5230\u4e0b\u6b21\u653e\u7f6e num \u7684\u7d22\u5f15\n }\n // \u4f7f\u7528\u7ed3\u679c\u6570\u7ec4 res \u8986\u76d6\u539f\u6570\u7ec4 nums\n for (int i = 0; i < n; i++) {\n nums[i] = res[i];\n }\n}\n
counting_sort.go/* \u8ba1\u6570\u6392\u5e8f */\n// \u5b8c\u6574\u5b9e\u73b0\uff0c\u53ef\u6392\u5e8f\u5bf9\u8c61\uff0c\u5e76\u4e14\u662f\u7a33\u5b9a\u6392\u5e8f\nfunc countingSort(nums []int) {\n // 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n m := 0\n for _, num := range nums {\n if num > m {\n m = num\n }\n }\n // 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n counter := make([]int, m+1)\n for _, num := range nums {\n counter[num]++\n }\n // 3. \u6c42 counter \u7684\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u6b21\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u5c3e\u7d22\u5f15\u201d\n // \u5373 counter[num]-1 \u662f num \u5728 res \u4e2d\u6700\u540e\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\n for i := 0; i < m; i++ {\n counter[i+1] += counter[i]\n }\n // 4. \u5012\u5e8f\u904d\u5386 nums \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u7ed3\u679c\u6570\u7ec4 res\n // \u521d\u59cb\u5316\u6570\u7ec4 res \u7528\u4e8e\u8bb0\u5f55\u7ed3\u679c\n n := len(nums)\n res := make([]int, n)\n for i := n - 1; i >= 0; i-- {\n num := nums[i]\n // \u5c06 num \u653e\u7f6e\u5230\u5bf9\u5e94\u7d22\u5f15\u5904\n res[counter[num]-1] = num\n // \u4ee4\u524d\u7f00\u548c\u81ea\u51cf 1 \uff0c\u5f97\u5230\u4e0b\u6b21\u653e\u7f6e num \u7684\u7d22\u5f15\n counter[num]--\n }\n // \u4f7f\u7528\u7ed3\u679c\u6570\u7ec4 res \u8986\u76d6\u539f\u6570\u7ec4 nums\n copy(nums, res)\n}\n
counting_sort.swift/* \u8ba1\u6570\u6392\u5e8f */\n// \u5b8c\u6574\u5b9e\u73b0\uff0c\u53ef\u6392\u5e8f\u5bf9\u8c61\uff0c\u5e76\u4e14\u662f\u7a33\u5b9a\u6392\u5e8f\nfunc countingSort(nums: inout [Int]) {\n // 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n let m = nums.max()!\n // 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n var counter = Array(repeating: 0, count: m + 1)\n for num in nums {\n counter[num] += 1\n }\n // 3. \u6c42 counter \u7684\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u6b21\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u5c3e\u7d22\u5f15\u201d\n // \u5373 counter[num]-1 \u662f num \u5728 res \u4e2d\u6700\u540e\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\n for i in stride(from: 0, to: m, by: 1) {\n counter[i + 1] += counter[i]\n }\n // 4. \u5012\u5e8f\u904d\u5386 nums \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u7ed3\u679c\u6570\u7ec4 res\n // \u521d\u59cb\u5316\u6570\u7ec4 res \u7528\u4e8e\u8bb0\u5f55\u7ed3\u679c\n var res = Array(repeating: 0, count: nums.count)\n for i in stride(from: nums.count - 1, through: 0, by: -1) {\n let num = nums[i]\n res[counter[num] - 1] = num // \u5c06 num \u653e\u7f6e\u5230\u5bf9\u5e94\u7d22\u5f15\u5904\n counter[num] -= 1 // \u4ee4\u524d\u7f00\u548c\u81ea\u51cf 1 \uff0c\u5f97\u5230\u4e0b\u6b21\u653e\u7f6e num \u7684\u7d22\u5f15\n }\n // \u4f7f\u7528\u7ed3\u679c\u6570\u7ec4 res \u8986\u76d6\u539f\u6570\u7ec4 nums\n for i in stride(from: 0, to: nums.count, by: 1) {\n nums[i] = res[i]\n }\n}\n
counting_sort.js/* \u8ba1\u6570\u6392\u5e8f */\n// \u5b8c\u6574\u5b9e\u73b0\uff0c\u53ef\u6392\u5e8f\u5bf9\u8c61\uff0c\u5e76\u4e14\u662f\u7a33\u5b9a\u6392\u5e8f\nfunction countingSort(nums) {\n // 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n let m = 0;\n for (const num of nums) {\n m = Math.max(m, num);\n }\n // 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n const counter = new Array(m + 1).fill(0);\n for (const num of nums) {\n counter[num]++;\n }\n // 3. \u6c42 counter \u7684\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u6b21\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u5c3e\u7d22\u5f15\u201d\n // \u5373 counter[num]-1 \u662f num \u5728 res \u4e2d\u6700\u540e\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\n for (let i = 0; i < m; i++) {\n counter[i + 1] += counter[i];\n }\n // 4. \u5012\u5e8f\u904d\u5386 nums \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u7ed3\u679c\u6570\u7ec4 res\n // \u521d\u59cb\u5316\u6570\u7ec4 res \u7528\u4e8e\u8bb0\u5f55\u7ed3\u679c\n const n = nums.length;\n const res = new Array(n);\n for (let i = n - 1; i >= 0; i--) {\n const num = nums[i];\n res[counter[num] - 1] = num; // \u5c06 num \u653e\u7f6e\u5230\u5bf9\u5e94\u7d22\u5f15\u5904\n counter[num]--; // \u4ee4\u524d\u7f00\u548c\u81ea\u51cf 1 \uff0c\u5f97\u5230\u4e0b\u6b21\u653e\u7f6e num \u7684\u7d22\u5f15\n }\n // \u4f7f\u7528\u7ed3\u679c\u6570\u7ec4 res \u8986\u76d6\u539f\u6570\u7ec4 nums\n for (let i = 0; i < n; i++) {\n nums[i] = res[i];\n }\n}\n
counting_sort.ts/* \u8ba1\u6570\u6392\u5e8f */\n// \u5b8c\u6574\u5b9e\u73b0\uff0c\u53ef\u6392\u5e8f\u5bf9\u8c61\uff0c\u5e76\u4e14\u662f\u7a33\u5b9a\u6392\u5e8f\nfunction countingSort(nums: number[]): void {\n // 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n let m = 0;\n for (const num of nums) {\n m = Math.max(m, num);\n }\n // 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n const counter: number[] = new Array<number>(m + 1).fill(0);\n for (const num of nums) {\n counter[num]++;\n }\n // 3. \u6c42 counter \u7684\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u6b21\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u5c3e\u7d22\u5f15\u201d\n // \u5373 counter[num]-1 \u662f num \u5728 res \u4e2d\u6700\u540e\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\n for (let i = 0; i < m; i++) {\n counter[i + 1] += counter[i];\n }\n // 4. \u5012\u5e8f\u904d\u5386 nums \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u7ed3\u679c\u6570\u7ec4 res\n // \u521d\u59cb\u5316\u6570\u7ec4 res \u7528\u4e8e\u8bb0\u5f55\u7ed3\u679c\n const n = nums.length;\n const res: number[] = new Array<number>(n);\n for (let i = n - 1; i >= 0; i--) {\n const num = nums[i];\n res[counter[num] - 1] = num; // \u5c06 num \u653e\u7f6e\u5230\u5bf9\u5e94\u7d22\u5f15\u5904\n counter[num]--; // \u4ee4\u524d\u7f00\u548c\u81ea\u51cf 1 \uff0c\u5f97\u5230\u4e0b\u6b21\u653e\u7f6e num \u7684\u7d22\u5f15\n }\n // \u4f7f\u7528\u7ed3\u679c\u6570\u7ec4 res \u8986\u76d6\u539f\u6570\u7ec4 nums\n for (let i = 0; i < n; i++) {\n nums[i] = res[i];\n }\n}\n
counting_sort.dart/* \u8ba1\u6570\u6392\u5e8f */\n// \u5b8c\u6574\u5b9e\u73b0\uff0c\u53ef\u6392\u5e8f\u5bf9\u8c61\uff0c\u5e76\u4e14\u662f\u7a33\u5b9a\u6392\u5e8f\nvoid countingSort(List<int> nums) {\n // 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n int m = 0;\n for (int _num in nums) {\n m = max(m, _num);\n }\n // 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n // counter[_num] \u4ee3\u8868 _num \u7684\u51fa\u73b0\u6b21\u6570\n List<int> counter = List.filled(m + 1, 0);\n for (int _num in nums) {\n counter[_num]++;\n }\n // 3. \u6c42 counter \u7684\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u6b21\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u5c3e\u7d22\u5f15\u201d\n // \u5373 counter[_num]-1 \u662f _num \u5728 res \u4e2d\u6700\u540e\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\n for (int i = 0; i < m; i++) {\n counter[i + 1] += counter[i];\n }\n // 4. \u5012\u5e8f\u904d\u5386 nums \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u7ed3\u679c\u6570\u7ec4 res\n // \u521d\u59cb\u5316\u6570\u7ec4 res \u7528\u4e8e\u8bb0\u5f55\u7ed3\u679c\n int n = nums.length;\n List<int> res = List.filled(n, 0);\n for (int i = n - 1; i >= 0; i--) {\n int _num = nums[i];\n res[counter[_num] - 1] = _num; // \u5c06 _num \u653e\u7f6e\u5230\u5bf9\u5e94\u7d22\u5f15\u5904\n counter[_num]--; // \u4ee4\u524d\u7f00\u548c\u81ea\u51cf 1 \uff0c\u5f97\u5230\u4e0b\u6b21\u653e\u7f6e _num \u7684\u7d22\u5f15\n }\n // \u4f7f\u7528\u7ed3\u679c\u6570\u7ec4 res \u8986\u76d6\u539f\u6570\u7ec4 nums\n nums.setAll(0, res);\n}\n
counting_sort.rs/* \u8ba1\u6570\u6392\u5e8f */\n// \u5b8c\u6574\u5b9e\u73b0\uff0c\u53ef\u6392\u5e8f\u5bf9\u8c61\uff0c\u5e76\u4e14\u662f\u7a33\u5b9a\u6392\u5e8f\nfn counting_sort(nums: &mut [i32]) {\n // 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n let m = *nums.into_iter().max().unwrap();\n // 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n let mut counter = vec![0; m as usize + 1];\n for &num in &*nums {\n counter[num as usize] += 1;\n }\n // 3. \u6c42 counter \u7684\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u6b21\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u5c3e\u7d22\u5f15\u201d\n // \u5373 counter[num]-1 \u662f num \u5728 res \u4e2d\u6700\u540e\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\n for i in 0..m as usize {\n counter[i + 1] += counter[i];\n }\n // 4. \u5012\u5e8f\u904d\u5386 nums \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u7ed3\u679c\u6570\u7ec4 res\n // \u521d\u59cb\u5316\u6570\u7ec4 res \u7528\u4e8e\u8bb0\u5f55\u7ed3\u679c\n let n = nums.len();\n let mut res = vec![0; n];\n for i in (0..n).rev() {\n let num = nums[i];\n res[counter[num as usize] - 1] = num; // \u5c06 num \u653e\u7f6e\u5230\u5bf9\u5e94\u7d22\u5f15\u5904\n counter[num as usize] -= 1; // \u4ee4\u524d\u7f00\u548c\u81ea\u51cf 1 \uff0c\u5f97\u5230\u4e0b\u6b21\u653e\u7f6e num \u7684\u7d22\u5f15\n }\n // \u4f7f\u7528\u7ed3\u679c\u6570\u7ec4 res \u8986\u76d6\u539f\u6570\u7ec4 nums\n for i in 0..n {\n nums[i] = res[i];\n }\n}\n
counting_sort.c/* \u8ba1\u6570\u6392\u5e8f */\n// \u5b8c\u6574\u5b9e\u73b0\uff0c\u53ef\u6392\u5e8f\u5bf9\u8c61\uff0c\u5e76\u4e14\u662f\u7a33\u5b9a\u6392\u5e8f\nvoid countingSort(int nums[], int size) {\n // 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n int m = 0;\n for (int i = 0; i < size; i++) {\n if (nums[i] > m) {\n m = nums[i];\n }\n }\n // 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n int *counter = calloc(m, sizeof(int));\n for (int i = 0; i < size; i++) {\n counter[nums[i]]++;\n }\n // 3. \u6c42 counter \u7684\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u6b21\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u5c3e\u7d22\u5f15\u201d\n // \u5373 counter[num]-1 \u662f num \u5728 res \u4e2d\u6700\u540e\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\n for (int i = 0; i < m; i++) {\n counter[i + 1] += counter[i];\n }\n // 4. \u5012\u5e8f\u904d\u5386 nums \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u7ed3\u679c\u6570\u7ec4 res\n // \u521d\u59cb\u5316\u6570\u7ec4 res \u7528\u4e8e\u8bb0\u5f55\u7ed3\u679c\n int *res = malloc(sizeof(int) * size);\n for (int i = size - 1; i >= 0; i--) {\n int num = nums[i];\n res[counter[num] - 1] = num; // \u5c06 num \u653e\u7f6e\u5230\u5bf9\u5e94\u7d22\u5f15\u5904\n counter[num]--; // \u4ee4\u524d\u7f00\u548c\u81ea\u51cf 1 \uff0c\u5f97\u5230\u4e0b\u6b21\u653e\u7f6e num \u7684\u7d22\u5f15\n }\n // \u4f7f\u7528\u7ed3\u679c\u6570\u7ec4 res \u8986\u76d6\u539f\u6570\u7ec4 nums\n memcpy(nums, res, size * sizeof(int));\n // 5. \u91ca\u653e\u5185\u5b58\n free(counter);\n}\n
counting_sort.zig[class]{}-[func]{countingSort}\n
"},{"location":"chapter_sorting/counting_sort/#1193","title":"11.9.3 \u00a0 \u7b97\u6cd5\u7279\u6027","text":"nums
\u548c\u904d\u5386 counter
\uff0c\u90fd\u4f7f\u7528\u7ebf\u6027\u65f6\u95f4\u3002\u4e00\u822c\u60c5\u51b5\u4e0b \\(n \\gg m\\) \uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u8d8b\u4e8e \\(O(n)\\) \u3002res
\u548c counter
\u3002res
\u4e2d\u586b\u5145\u5143\u7d20\u7684\u987a\u5e8f\u662f\u201c\u4ece\u53f3\u5411\u5de6\u201d\u7684\uff0c\u56e0\u6b64\u5012\u5e8f\u904d\u5386 nums
\u53ef\u4ee5\u907f\u514d\u6539\u53d8\u76f8\u7b49\u5143\u7d20\u4e4b\u95f4\u7684\u76f8\u5bf9\u4f4d\u7f6e\uff0c\u4ece\u800c\u5b9e\u73b0\u7a33\u5b9a\u6392\u5e8f\u3002\u5b9e\u9645\u4e0a\uff0c\u6b63\u5e8f\u904d\u5386 nums
\u4e5f\u53ef\u4ee5\u5f97\u5230\u6b63\u786e\u7684\u6392\u5e8f\u7ed3\u679c\uff0c\u4f46\u7ed3\u679c\u662f\u975e\u7a33\u5b9a\u7684\u3002\u770b\u5230\u8fd9\u91cc\uff0c\u4f60\u4e5f\u8bb8\u4f1a\u89c9\u5f97\u8ba1\u6570\u6392\u5e8f\u975e\u5e38\u5de7\u5999\uff0c\u4ec5\u901a\u8fc7\u7edf\u8ba1\u6570\u91cf\u5c31\u53ef\u4ee5\u5b9e\u73b0\u9ad8\u6548\u7684\u6392\u5e8f\u3002\u7136\u800c\uff0c\u4f7f\u7528\u8ba1\u6570\u6392\u5e8f\u7684\u524d\u7f6e\u6761\u4ef6\u76f8\u5bf9\u8f83\u4e3a\u4e25\u683c\u3002
\u8ba1\u6570\u6392\u5e8f\u53ea\u9002\u7528\u4e8e\u975e\u8d1f\u6574\u6570\u3002\u82e5\u60f3\u5c06\u5176\u7528\u4e8e\u5176\u4ed6\u7c7b\u578b\u7684\u6570\u636e\uff0c\u9700\u8981\u786e\u4fdd\u8fd9\u4e9b\u6570\u636e\u53ef\u4ee5\u8f6c\u6362\u4e3a\u975e\u8d1f\u6574\u6570\uff0c\u5e76\u4e14\u5728\u8f6c\u6362\u8fc7\u7a0b\u4e2d\u4e0d\u80fd\u6539\u53d8\u5404\u4e2a\u5143\u7d20\u4e4b\u95f4\u7684\u76f8\u5bf9\u5927\u5c0f\u5173\u7cfb\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u5305\u542b\u8d1f\u6570\u7684\u6574\u6570\u6570\u7ec4\uff0c\u53ef\u4ee5\u5148\u7ed9\u6240\u6709\u6570\u5b57\u52a0\u4e0a\u4e00\u4e2a\u5e38\u6570\uff0c\u5c06\u5168\u90e8\u6570\u5b57\u8f6c\u5316\u4e3a\u6b63\u6570\uff0c\u6392\u5e8f\u5b8c\u6210\u540e\u518d\u8f6c\u6362\u56de\u53bb\u3002
\u8ba1\u6570\u6392\u5e8f\u9002\u7528\u4e8e\u6570\u636e\u91cf\u5927\u4f46\u6570\u636e\u8303\u56f4\u8f83\u5c0f\u7684\u60c5\u51b5\u3002\u6bd4\u5982\uff0c\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d \\(m\\) \u4e0d\u80fd\u592a\u5927\uff0c\u5426\u5219\u4f1a\u5360\u7528\u8fc7\u591a\u7a7a\u95f4\u3002\u800c\u5f53 \\(n \\ll m\\) \u65f6\uff0c\u8ba1\u6570\u6392\u5e8f\u4f7f\u7528 \\(O(m)\\) \u65f6\u95f4\uff0c\u53ef\u80fd\u6bd4 \\(O(n \\log n)\\) \u7684\u6392\u5e8f\u7b97\u6cd5\u8fd8\u8981\u6162\u3002
"},{"location":"chapter_sorting/heap_sort/","title":"11.7 \u00a0 \u5806\u6392\u5e8f","text":"Tip
\u9605\u8bfb\u672c\u8282\u524d\uff0c\u8bf7\u786e\u4fdd\u5df2\u5b66\u5b8c\u201c\u5806\u201c\u7ae0\u8282\u3002
\u300c\u5806\u6392\u5e8f heap sort\u300d\u662f\u4e00\u79cd\u57fa\u4e8e\u5806\u6570\u636e\u7ed3\u6784\u5b9e\u73b0\u7684\u9ad8\u6548\u6392\u5e8f\u7b97\u6cd5\u3002\u6211\u4eec\u53ef\u4ee5\u5229\u7528\u5df2\u7ecf\u5b66\u8fc7\u7684\u201c\u5efa\u5806\u64cd\u4f5c\u201d\u548c\u201c\u5143\u7d20\u51fa\u5806\u64cd\u4f5c\u201d\u5b9e\u73b0\u5806\u6392\u5e8f\u3002
\u4ee5\u4e0a\u65b9\u6cd5\u867d\u7136\u53ef\u884c\uff0c\u4f46\u9700\u8981\u501f\u52a9\u4e00\u4e2a\u989d\u5916\u6570\u7ec4\u6765\u4fdd\u5b58\u5f39\u51fa\u7684\u5143\u7d20\uff0c\u6bd4\u8f83\u6d6a\u8d39\u7a7a\u95f4\u3002\u5728\u5b9e\u9645\u4e2d\uff0c\u6211\u4eec\u901a\u5e38\u4f7f\u7528\u4e00\u79cd\u66f4\u52a0\u4f18\u96c5\u7684\u5b9e\u73b0\u65b9\u5f0f\u3002
"},{"location":"chapter_sorting/heap_sort/#1171","title":"11.7.1 \u00a0 \u7b97\u6cd5\u6d41\u7a0b","text":"\u8bbe\u6570\u7ec4\u7684\u957f\u5ea6\u4e3a \\(n\\) \uff0c\u5806\u6392\u5e8f\u7684\u6d41\u7a0b\u5982\u56fe 11-12 \u6240\u793a\u3002
2.
\u6b65\u548c\u7b2c 3.
\u6b65\u3002\u5faa\u73af \\(n - 1\\) \u8f6e\u540e\uff0c\u5373\u53ef\u5b8c\u6210\u6570\u7ec4\u6392\u5e8f\u3002Tip
\u5b9e\u9645\u4e0a\uff0c\u5143\u7d20\u51fa\u5806\u64cd\u4f5c\u4e2d\u4e5f\u5305\u542b\u7b2c 2.
\u6b65\u548c\u7b2c 3.
\u6b65\uff0c\u53ea\u662f\u591a\u4e86\u4e00\u4e2a\u5f39\u51fa\u5143\u7d20\u7684\u6b65\u9aa4\u3002
\u56fe 11-12 \u00a0 \u5806\u6392\u5e8f\u6b65\u9aa4
\u5728\u4ee3\u7801\u5b9e\u73b0\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u4e0e\u201c\u5806\u201d\u7ae0\u8282\u76f8\u540c\u7684\u4ece\u9876\u81f3\u5e95\u5806\u5316 sift_down()
\u51fd\u6570\u3002\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u7531\u4e8e\u5806\u7684\u957f\u5ea6\u4f1a\u968f\u7740\u63d0\u53d6\u6700\u5927\u5143\u7d20\u800c\u51cf\u5c0f\uff0c\u56e0\u6b64\u6211\u4eec\u9700\u8981\u7ed9 sift_down()
\u51fd\u6570\u6dfb\u52a0\u4e00\u4e2a\u957f\u5ea6\u53c2\u6570 \\(n\\) \uff0c\u7528\u4e8e\u6307\u5b9a\u5806\u7684\u5f53\u524d\u6709\u6548\u957f\u5ea6\u3002\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a
def sift_down(nums: list[int], n: int, i: int):\n \"\"\"\u5806\u7684\u957f\u5ea6\u4e3a n \uff0c\u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316\"\"\"\n while True:\n # \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n l = 2 * i + 1\n r = 2 * i + 2\n ma = i\n if l < n and nums[l] > nums[ma]:\n ma = l\n if r < n and nums[r] > nums[ma]:\n ma = r\n # \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n if ma == i:\n break\n # \u4ea4\u6362\u4e24\u8282\u70b9\n nums[i], nums[ma] = nums[ma], nums[i]\n # \u5faa\u73af\u5411\u4e0b\u5806\u5316\n i = ma\n\ndef heap_sort(nums: list[int]):\n \"\"\"\u5806\u6392\u5e8f\"\"\"\n # \u5efa\u5806\u64cd\u4f5c\uff1a\u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n for i in range(len(nums) // 2 - 1, -1, -1):\n sift_down(nums, len(nums), i)\n # \u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u5faa\u73af n-1 \u8f6e\n for i in range(len(nums) - 1, 0, -1):\n # \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n nums[0], nums[i] = nums[i], nums[0]\n # \u4ee5\u6839\u8282\u70b9\u4e3a\u8d77\u70b9\uff0c\u4ece\u9876\u81f3\u5e95\u8fdb\u884c\u5806\u5316\n sift_down(nums, i, 0)\n
heap_sort.cpp/* \u5806\u7684\u957f\u5ea6\u4e3a n \uff0c\u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nvoid siftDown(vector<int> &nums, int n, int i) {\n while (true) {\n // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n int l = 2 * i + 1;\n int r = 2 * i + 2;\n int ma = i;\n if (l < n && nums[l] > nums[ma])\n ma = l;\n if (r < n && nums[r] > nums[ma])\n ma = r;\n // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n if (ma == i) {\n break;\n }\n // \u4ea4\u6362\u4e24\u8282\u70b9\n swap(nums[i], nums[ma]);\n // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n i = ma;\n }\n}\n\n/* \u5806\u6392\u5e8f */\nvoid heapSort(vector<int> &nums) {\n // \u5efa\u5806\u64cd\u4f5c\uff1a\u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n for (int i = nums.size() / 2 - 1; i >= 0; --i) {\n siftDown(nums, nums.size(), i);\n }\n // \u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u5faa\u73af n-1 \u8f6e\n for (int i = nums.size() - 1; i > 0; --i) {\n // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n swap(nums[0], nums[i]);\n // \u4ee5\u6839\u8282\u70b9\u4e3a\u8d77\u70b9\uff0c\u4ece\u9876\u81f3\u5e95\u8fdb\u884c\u5806\u5316\n siftDown(nums, i, 0);\n }\n}\n
heap_sort.java/* \u5806\u7684\u957f\u5ea6\u4e3a n \uff0c\u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nvoid siftDown(int[] nums, int n, int i) {\n while (true) {\n // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n int l = 2 * i + 1;\n int r = 2 * i + 2;\n int ma = i;\n if (l < n && nums[l] > nums[ma])\n ma = l;\n if (r < n && nums[r] > nums[ma])\n ma = r;\n // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n if (ma == i)\n break;\n // \u4ea4\u6362\u4e24\u8282\u70b9\n int temp = nums[i];\n nums[i] = nums[ma];\n nums[ma] = temp;\n // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n i = ma;\n }\n}\n\n/* \u5806\u6392\u5e8f */\nvoid heapSort(int[] nums) {\n // \u5efa\u5806\u64cd\u4f5c\uff1a\u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n for (int i = nums.length / 2 - 1; i >= 0; i--) {\n siftDown(nums, nums.length, i);\n }\n // \u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u5faa\u73af n-1 \u8f6e\n for (int i = nums.length - 1; i > 0; i--) {\n // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n int tmp = nums[0];\n nums[0] = nums[i];\n nums[i] = tmp;\n // \u4ee5\u6839\u8282\u70b9\u4e3a\u8d77\u70b9\uff0c\u4ece\u9876\u81f3\u5e95\u8fdb\u884c\u5806\u5316\n siftDown(nums, i, 0);\n }\n}\n
heap_sort.cs/* \u5806\u7684\u957f\u5ea6\u4e3a n \uff0c\u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nvoid SiftDown(int[] nums, int n, int i) {\n while (true) {\n // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n int l = 2 * i + 1;\n int r = 2 * i + 2;\n int ma = i;\n if (l < n && nums[l] > nums[ma])\n ma = l;\n if (r < n && nums[r] > nums[ma])\n ma = r;\n // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n if (ma == i)\n break;\n // \u4ea4\u6362\u4e24\u8282\u70b9\n (nums[ma], nums[i]) = (nums[i], nums[ma]);\n // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n i = ma;\n }\n}\n\n/* \u5806\u6392\u5e8f */\nvoid HeapSort(int[] nums) {\n // \u5efa\u5806\u64cd\u4f5c\uff1a\u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n for (int i = nums.Length / 2 - 1; i >= 0; i--) {\n SiftDown(nums, nums.Length, i);\n }\n // \u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u5faa\u73af n-1 \u8f6e\n for (int i = nums.Length - 1; i > 0; i--) {\n // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n (nums[i], nums[0]) = (nums[0], nums[i]);\n // \u4ee5\u6839\u8282\u70b9\u4e3a\u8d77\u70b9\uff0c\u4ece\u9876\u81f3\u5e95\u8fdb\u884c\u5806\u5316\n SiftDown(nums, i, 0);\n }\n}\n
heap_sort.go/* \u5806\u7684\u957f\u5ea6\u4e3a n \uff0c\u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nfunc siftDown(nums *[]int, n, i int) {\n for true {\n // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n l := 2*i + 1\n r := 2*i + 2\n ma := i\n if l < n && (*nums)[l] > (*nums)[ma] {\n ma = l\n }\n if r < n && (*nums)[r] > (*nums)[ma] {\n ma = r\n }\n // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n if ma == i {\n break\n }\n // \u4ea4\u6362\u4e24\u8282\u70b9\n (*nums)[i], (*nums)[ma] = (*nums)[ma], (*nums)[i]\n // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n i = ma\n }\n}\n\n/* \u5806\u6392\u5e8f */\nfunc heapSort(nums *[]int) {\n // \u5efa\u5806\u64cd\u4f5c\uff1a\u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n for i := len(*nums)/2 - 1; i >= 0; i-- {\n siftDown(nums, len(*nums), i)\n }\n // \u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u5faa\u73af n-1 \u8f6e\n for i := len(*nums) - 1; i > 0; i-- {\n // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n (*nums)[0], (*nums)[i] = (*nums)[i], (*nums)[0]\n // \u4ee5\u6839\u8282\u70b9\u4e3a\u8d77\u70b9\uff0c\u4ece\u9876\u81f3\u5e95\u8fdb\u884c\u5806\u5316\n siftDown(nums, i, 0)\n }\n}\n
heap_sort.swift/* \u5806\u7684\u957f\u5ea6\u4e3a n \uff0c\u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nfunc siftDown(nums: inout [Int], n: Int, i: Int) {\n var i = i\n while true {\n // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n let l = 2 * i + 1\n let r = 2 * i + 2\n var ma = i\n if l < n, nums[l] > nums[ma] {\n ma = l\n }\n if r < n, nums[r] > nums[ma] {\n ma = r\n }\n // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n if ma == i {\n break\n }\n // \u4ea4\u6362\u4e24\u8282\u70b9\n nums.swapAt(i, ma)\n // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n i = ma\n }\n}\n\n/* \u5806\u6392\u5e8f */\nfunc heapSort(nums: inout [Int]) {\n // \u5efa\u5806\u64cd\u4f5c\uff1a\u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n for i in stride(from: nums.count / 2 - 1, through: 0, by: -1) {\n siftDown(nums: &nums, n: nums.count, i: i)\n }\n // \u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u5faa\u73af n-1 \u8f6e\n for i in stride(from: nums.count - 1, to: 0, by: -1) {\n // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n nums.swapAt(0, i)\n // \u4ee5\u6839\u8282\u70b9\u4e3a\u8d77\u70b9\uff0c\u4ece\u9876\u81f3\u5e95\u8fdb\u884c\u5806\u5316\n siftDown(nums: &nums, n: i, i: 0)\n }\n}\n
heap_sort.js/* \u5806\u7684\u957f\u5ea6\u4e3a n \uff0c\u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nfunction siftDown(nums, n, i) {\n while (true) {\n // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n let l = 2 * i + 1;\n let r = 2 * i + 2;\n let ma = i;\n if (l < n && nums[l] > nums[ma]) {\n ma = l;\n }\n if (r < n && nums[r] > nums[ma]) {\n ma = r;\n }\n // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n if (ma === i) {\n break;\n }\n // \u4ea4\u6362\u4e24\u8282\u70b9\n [nums[i], nums[ma]] = [nums[ma], nums[i]];\n // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n i = ma;\n }\n}\n\n/* \u5806\u6392\u5e8f */\nfunction heapSort(nums) {\n // \u5efa\u5806\u64cd\u4f5c\uff1a\u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n for (let i = Math.floor(nums.length / 2) - 1; i >= 0; i--) {\n siftDown(nums, nums.length, i);\n }\n // \u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u5faa\u73af n-1 \u8f6e\n for (let i = nums.length - 1; i > 0; i--) {\n // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n [nums[0], nums[i]] = [nums[i], nums[0]];\n // \u4ee5\u6839\u8282\u70b9\u4e3a\u8d77\u70b9\uff0c\u4ece\u9876\u81f3\u5e95\u8fdb\u884c\u5806\u5316\n siftDown(nums, i, 0);\n }\n}\n
heap_sort.ts/* \u5806\u7684\u957f\u5ea6\u4e3a n \uff0c\u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nfunction siftDown(nums: number[], n: number, i: number): void {\n while (true) {\n // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n let l = 2 * i + 1;\n let r = 2 * i + 2;\n let ma = i;\n if (l < n && nums[l] > nums[ma]) {\n ma = l;\n }\n if (r < n && nums[r] > nums[ma]) {\n ma = r;\n }\n // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n if (ma === i) {\n break;\n }\n // \u4ea4\u6362\u4e24\u8282\u70b9\n [nums[i], nums[ma]] = [nums[ma], nums[i]];\n // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n i = ma;\n }\n}\n\n/* \u5806\u6392\u5e8f */\nfunction heapSort(nums: number[]): void {\n // \u5efa\u5806\u64cd\u4f5c\uff1a\u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n for (let i = Math.floor(nums.length / 2) - 1; i >= 0; i--) {\n siftDown(nums, nums.length, i);\n }\n // \u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u5faa\u73af n-1 \u8f6e\n for (let i = nums.length - 1; i > 0; i--) {\n // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n [nums[0], nums[i]] = [nums[i], nums[0]];\n // \u4ee5\u6839\u8282\u70b9\u4e3a\u8d77\u70b9\uff0c\u4ece\u9876\u81f3\u5e95\u8fdb\u884c\u5806\u5316\n siftDown(nums, i, 0);\n }\n}\n
heap_sort.dart/* \u5806\u7684\u957f\u5ea6\u4e3a n \uff0c\u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nvoid siftDown(List<int> nums, int n, int i) {\n while (true) {\n // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n int l = 2 * i + 1;\n int r = 2 * i + 2;\n int ma = i;\n if (l < n && nums[l] > nums[ma]) ma = l;\n if (r < n && nums[r] > nums[ma]) ma = r;\n // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n if (ma == i) break;\n // \u4ea4\u6362\u4e24\u8282\u70b9\n int temp = nums[i];\n nums[i] = nums[ma];\n nums[ma] = temp;\n // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n i = ma;\n }\n}\n\n/* \u5806\u6392\u5e8f */\nvoid heapSort(List<int> nums) {\n // \u5efa\u5806\u64cd\u4f5c\uff1a\u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n for (int i = nums.length ~/ 2 - 1; i >= 0; i--) {\n siftDown(nums, nums.length, i);\n }\n // \u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u5faa\u73af n-1 \u8f6e\n for (int i = nums.length - 1; i > 0; i--) {\n // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n int tmp = nums[0];\n nums[0] = nums[i];\n nums[i] = tmp;\n // \u4ee5\u6839\u8282\u70b9\u4e3a\u8d77\u70b9\uff0c\u4ece\u9876\u81f3\u5e95\u8fdb\u884c\u5806\u5316\n siftDown(nums, i, 0);\n }\n}\n
heap_sort.rs/* \u5806\u7684\u957f\u5ea6\u4e3a n \uff0c\u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nfn sift_down(nums: &mut [i32], n: usize, mut i: usize) {\n loop {\n // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n let l = 2 * i + 1;\n let r = 2 * i + 2;\n let mut ma = i;\n if l < n && nums[l] > nums[ma] {\n ma = l;\n }\n if r < n && nums[r] > nums[ma] {\n ma = r;\n }\n // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n if ma == i {\n break;\n }\n // \u4ea4\u6362\u4e24\u8282\u70b9\n let temp = nums[i];\n nums[i] = nums[ma];\n nums[ma] = temp;\n // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n i = ma;\n }\n}\n\n/* \u5806\u6392\u5e8f */\nfn heap_sort(nums: &mut [i32]) {\n // \u5efa\u5806\u64cd\u4f5c\uff1a\u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n for i in (0..=nums.len() / 2 - 1).rev() {\n sift_down(nums, nums.len(), i);\n }\n // \u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u5faa\u73af n-1 \u8f6e\n for i in (1..=nums.len() - 1).rev() {\n // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n let tmp = nums[0];\n nums[0] = nums[i];\n nums[i] = tmp;\n // \u4ee5\u6839\u8282\u70b9\u4e3a\u8d77\u70b9\uff0c\u4ece\u9876\u81f3\u5e95\u8fdb\u884c\u5806\u5316\n sift_down(nums, i, 0);\n }\n}\n
heap_sort.c/* \u5806\u7684\u957f\u5ea6\u4e3a n \uff0c\u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nvoid siftDown(int nums[], int n, int i) {\n while (1) {\n // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n int l = 2 * i + 1;\n int r = 2 * i + 2;\n int ma = i;\n if (l < n && nums[l] > nums[ma])\n ma = l;\n if (r < n && nums[r] > nums[ma])\n ma = r;\n // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n if (ma == i) {\n break;\n }\n // \u4ea4\u6362\u4e24\u8282\u70b9\n int temp = nums[i];\n nums[i] = nums[ma];\n nums[ma] = temp;\n // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n i = ma;\n }\n}\n\n/* \u5806\u6392\u5e8f */\nvoid heapSort(int nums[], int n) {\n // \u5efa\u5806\u64cd\u4f5c\uff1a\u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n for (int i = n / 2 - 1; i >= 0; --i) {\n siftDown(nums, n, i);\n }\n // \u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u5faa\u73af n-1 \u8f6e\n for (int i = n - 1; i > 0; --i) {\n // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n int tmp = nums[0];\n nums[0] = nums[i];\n nums[i] = tmp;\n // \u4ee5\u6839\u8282\u70b9\u4e3a\u8d77\u70b9\uff0c\u4ece\u9876\u81f3\u5e95\u8fdb\u884c\u5806\u5316\n siftDown(nums, i, 0);\n }\n}\n
heap_sort.zig[class]{}-[func]{siftDown}\n\n[class]{}-[func]{heapSort}\n
"},{"location":"chapter_sorting/heap_sort/#1172","title":"11.7.2 \u00a0 \u7b97\u6cd5\u7279\u6027","text":"\u300c\u63d2\u5165\u6392\u5e8f insertion sort\u300d\u662f\u4e00\u79cd\u7b80\u5355\u7684\u6392\u5e8f\u7b97\u6cd5\uff0c\u5b83\u7684\u5de5\u4f5c\u539f\u7406\u4e0e\u624b\u52a8\u6574\u7406\u4e00\u526f\u724c\u7684\u8fc7\u7a0b\u975e\u5e38\u76f8\u4f3c\u3002
\u5177\u4f53\u6765\u8bf4\uff0c\u6211\u4eec\u5728\u672a\u6392\u5e8f\u533a\u95f4\u9009\u62e9\u4e00\u4e2a\u57fa\u51c6\u5143\u7d20\uff0c\u5c06\u8be5\u5143\u7d20\u4e0e\u5176\u5de6\u4fa7\u5df2\u6392\u5e8f\u533a\u95f4\u7684\u5143\u7d20\u9010\u4e00\u6bd4\u8f83\u5927\u5c0f\uff0c\u5e76\u5c06\u8be5\u5143\u7d20\u63d2\u5165\u5230\u6b63\u786e\u7684\u4f4d\u7f6e\u3002
\u56fe 11-6 \u5c55\u793a\u4e86\u6570\u7ec4\u63d2\u5165\u5143\u7d20\u7684\u64cd\u4f5c\u6d41\u7a0b\u3002\u8bbe\u57fa\u51c6\u5143\u7d20\u4e3a base
\uff0c\u6211\u4eec\u9700\u8981\u5c06\u4ece\u76ee\u6807\u7d22\u5f15\u5230 base
\u4e4b\u95f4\u7684\u6240\u6709\u5143\u7d20\u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\uff0c\u7136\u540e\u5c06 base
\u8d4b\u503c\u7ed9\u76ee\u6807\u7d22\u5f15\u3002
\u56fe 11-6 \u00a0 \u5355\u6b21\u63d2\u5165\u64cd\u4f5c
"},{"location":"chapter_sorting/insertion_sort/#1141","title":"11.4.1 \u00a0 \u7b97\u6cd5\u6d41\u7a0b","text":"\u63d2\u5165\u6392\u5e8f\u7684\u6574\u4f53\u6d41\u7a0b\u5982\u56fe 11-7 \u6240\u793a\u3002
base
\uff0c\u5c06\u5176\u63d2\u5165\u5230\u6b63\u786e\u4f4d\u7f6e\u540e\uff0c\u6570\u7ec4\u7684\u524d 2 \u4e2a\u5143\u7d20\u5df2\u6392\u5e8f\u3002base
\uff0c\u5c06\u5176\u63d2\u5165\u5230\u6b63\u786e\u4f4d\u7f6e\u540e\uff0c\u6570\u7ec4\u7684\u524d 3 \u4e2a\u5143\u7d20\u5df2\u6392\u5e8f\u3002base
\uff0c\u5c06\u5176\u63d2\u5165\u5230\u6b63\u786e\u4f4d\u7f6e\u540e\uff0c\u6240\u6709\u5143\u7d20\u5747\u5df2\u6392\u5e8f\u3002\u56fe 11-7 \u00a0 \u63d2\u5165\u6392\u5e8f\u6d41\u7a0b
\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig insertion_sort.pydef insertion_sort(nums: list[int]):\n \"\"\"\u63d2\u5165\u6392\u5e8f\"\"\"\n # \u5916\u5faa\u73af\uff1a\u5df2\u6392\u5e8f\u533a\u95f4\u4e3a [0, i-1]\n for i in range(1, len(nums)):\n base = nums[i]\n j = i - 1\n # \u5185\u5faa\u73af\uff1a\u5c06 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u533a\u95f4 [0, i-1] \u4e2d\u7684\u6b63\u786e\u4f4d\u7f6e\n while j >= 0 and nums[j] > base:\n nums[j + 1] = nums[j] # \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\n j -= 1\n nums[j + 1] = base # \u5c06 base \u8d4b\u503c\u5230\u6b63\u786e\u4f4d\u7f6e\n
insertion_sort.cpp/* \u63d2\u5165\u6392\u5e8f */\nvoid insertionSort(vector<int> &nums) {\n // \u5916\u5faa\u73af\uff1a\u5df2\u6392\u5e8f\u5143\u7d20\u6570\u91cf\u4e3a 1, 2, ..., n\n for (int i = 1; i < nums.size(); i++) {\n int base = nums[i], j = i - 1;\n // \u5185\u5faa\u73af\uff1a\u5c06 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u90e8\u5206\u7684\u6b63\u786e\u4f4d\u7f6e\n while (j >= 0 && nums[j] > base) {\n nums[j + 1] = nums[j]; // \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\n j--;\n }\n nums[j + 1] = base; // \u5c06 base \u8d4b\u503c\u5230\u6b63\u786e\u4f4d\u7f6e\n }\n}\n
insertion_sort.java/* \u63d2\u5165\u6392\u5e8f */\nvoid insertionSort(int[] nums) {\n // \u5916\u5faa\u73af\uff1a\u5df2\u6392\u5e8f\u5143\u7d20\u6570\u91cf\u4e3a 1, 2, ..., n\n for (int i = 1; i < nums.length; i++) {\n int base = nums[i], j = i - 1;\n // \u5185\u5faa\u73af\uff1a\u5c06 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u90e8\u5206\u7684\u6b63\u786e\u4f4d\u7f6e\n while (j >= 0 && nums[j] > base) {\n nums[j + 1] = nums[j]; // \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\n j--;\n }\n nums[j + 1] = base; // \u5c06 base \u8d4b\u503c\u5230\u6b63\u786e\u4f4d\u7f6e\n }\n}\n
insertion_sort.cs/* \u63d2\u5165\u6392\u5e8f */\nvoid InsertionSort(int[] nums) {\n // \u5916\u5faa\u73af\uff1a\u5df2\u6392\u5e8f\u5143\u7d20\u6570\u91cf\u4e3a 1, 2, ..., n\n for (int i = 1; i < nums.Length; i++) {\n int bas = nums[i], j = i - 1;\n // \u5185\u5faa\u73af\uff1a\u5c06 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u90e8\u5206\u7684\u6b63\u786e\u4f4d\u7f6e\n while (j >= 0 && nums[j] > bas) {\n nums[j + 1] = nums[j]; // \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\n j--;\n }\n nums[j + 1] = bas; // \u5c06 base \u8d4b\u503c\u5230\u6b63\u786e\u4f4d\u7f6e\n }\n}\n
insertion_sort.go/* \u63d2\u5165\u6392\u5e8f */\nfunc insertionSort(nums []int) {\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for i := 1; i < len(nums); i++ {\n base := nums[i]\n j := i - 1\n // \u5185\u5faa\u73af\uff1a\u5c06 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u90e8\u5206\u7684\u6b63\u786e\u4f4d\u7f6e\n for j >= 0 && nums[j] > base {\n nums[j+1] = nums[j] // \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\n j--\n }\n nums[j+1] = base // \u5c06 base \u8d4b\u503c\u5230\u6b63\u786e\u4f4d\u7f6e\n }\n}\n
insertion_sort.swift/* \u63d2\u5165\u6392\u5e8f */\nfunc insertionSort(nums: inout [Int]) {\n // \u5916\u5faa\u73af\uff1a\u5df2\u6392\u5e8f\u5143\u7d20\u6570\u91cf\u4e3a 1, 2, ..., n\n for i in stride(from: 1, to: nums.count, by: 1) {\n let base = nums[i]\n var j = i - 1\n // \u5185\u5faa\u73af\uff1a\u5c06 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u90e8\u5206\u7684\u6b63\u786e\u4f4d\u7f6e\n while j >= 0, nums[j] > base {\n nums[j + 1] = nums[j] // \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\n j -= 1\n }\n nums[j + 1] = base // \u5c06 base \u8d4b\u503c\u5230\u6b63\u786e\u4f4d\u7f6e\n }\n}\n
insertion_sort.js/* \u63d2\u5165\u6392\u5e8f */\nfunction insertionSort(nums) {\n // \u5916\u5faa\u73af\uff1a\u5df2\u6392\u5e8f\u5143\u7d20\u6570\u91cf\u4e3a 1, 2, ..., n\n for (let i = 1; i < nums.length; i++) {\n let base = nums[i],\n j = i - 1;\n // \u5185\u5faa\u73af\uff1a\u5c06 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u90e8\u5206\u7684\u6b63\u786e\u4f4d\u7f6e\n while (j >= 0 && nums[j] > base) {\n nums[j + 1] = nums[j]; // \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\n j--;\n }\n nums[j + 1] = base; // \u5c06 base \u8d4b\u503c\u5230\u6b63\u786e\u4f4d\u7f6e\n }\n}\n
insertion_sort.ts/* \u63d2\u5165\u6392\u5e8f */\nfunction insertionSort(nums: number[]): void {\n // \u5916\u5faa\u73af\uff1a\u5df2\u6392\u5e8f\u5143\u7d20\u6570\u91cf\u4e3a 1, 2, ..., n\n for (let i = 1; i < nums.length; i++) {\n const base = nums[i];\n let j = i - 1;\n // \u5185\u5faa\u73af\uff1a\u5c06 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u90e8\u5206\u7684\u6b63\u786e\u4f4d\u7f6e\n while (j >= 0 && nums[j] > base) {\n nums[j + 1] = nums[j]; // \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\n j--;\n }\n nums[j + 1] = base; // \u5c06 base \u8d4b\u503c\u5230\u6b63\u786e\u4f4d\u7f6e\n }\n}\n
insertion_sort.dart/* \u63d2\u5165\u6392\u5e8f */\nvoid insertionSort(List<int> nums) {\n // \u5916\u5faa\u73af\uff1a\u5df2\u6392\u5e8f\u5143\u7d20\u6570\u91cf\u4e3a 1, 2, ..., n\n for (int i = 1; i < nums.length; i++) {\n int base = nums[i], j = i - 1;\n // \u5185\u5faa\u73af\uff1a\u5c06 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u90e8\u5206\u7684\u6b63\u786e\u4f4d\u7f6e\n while (j >= 0 && nums[j] > base) {\n nums[j + 1] = nums[j]; // \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\n j--;\n }\n nums[j + 1] = base; // \u5c06 base \u8d4b\u503c\u5230\u6b63\u786e\u4f4d\u7f6e\n }\n}\n
insertion_sort.rs/* \u63d2\u5165\u6392\u5e8f */\nfn insertion_sort(nums: &mut [i32]) {\n // \u5916\u5faa\u73af\uff1a\u5df2\u6392\u5e8f\u5143\u7d20\u6570\u91cf\u4e3a 1, 2, ..., n\n for i in 1..nums.len() {\n let (base, mut j) = (nums[i], (i - 1) as i32);\n // \u5185\u5faa\u73af\uff1a\u5c06 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u90e8\u5206\u7684\u6b63\u786e\u4f4d\u7f6e\n while j >= 0 && nums[j as usize] > base {\n nums[(j + 1) as usize] = nums[j as usize]; // \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\n j -= 1;\n }\n nums[(j + 1) as usize] = base; // \u5c06 base \u8d4b\u503c\u5230\u6b63\u786e\u4f4d\u7f6e\n }\n}\n
insertion_sort.c/* \u63d2\u5165\u6392\u5e8f */\nvoid insertionSort(int nums[], int size) {\n // \u5916\u5faa\u73af\uff1a\u5df2\u6392\u5e8f\u5143\u7d20\u6570\u91cf\u4e3a 1, 2, ..., n\n for (int i = 1; i < size; i++) {\n int base = nums[i], j = i - 1;\n // \u5185\u5faa\u73af\uff1a\u5c06 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u90e8\u5206\u7684\u6b63\u786e\u4f4d\u7f6e\n while (j >= 0 && nums[j] > base) {\n // \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\n nums[j + 1] = nums[j];\n j--;\n }\n // \u5c06 base \u8d4b\u503c\u5230\u6b63\u786e\u4f4d\u7f6e\n nums[j + 1] = base;\n }\n}\n
insertion_sort.zig// \u63d2\u5165\u6392\u5e8f\nfn insertionSort(nums: []i32) void {\n // \u5916\u5faa\u73af\uff1a\u5df2\u6392\u5e8f\u5143\u7d20\u6570\u91cf\u4e3a 1, 2, ..., n\n var i: usize = 1;\n while (i < nums.len) : (i += 1) {\n var base = nums[i];\n var j: usize = i;\n // \u5185\u5faa\u73af\uff1a\u5c06 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u90e8\u5206\u7684\u6b63\u786e\u4f4d\u7f6e\n while (j >= 1 and nums[j - 1] > base) : (j -= 1) {\n nums[j] = nums[j - 1]; // \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\n }\n nums[j] = base; // \u5c06 base \u8d4b\u503c\u5230\u6b63\u786e\u4f4d\u7f6e\n }\n}\n
"},{"location":"chapter_sorting/insertion_sort/#1142","title":"11.4.2 \u00a0 \u7b97\u6cd5\u7279\u6027","text":"\u63d2\u5165\u6392\u5e8f\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\) \uff0c\u800c\u6211\u4eec\u5373\u5c06\u5b66\u4e60\u7684\u5feb\u901f\u6392\u5e8f\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n \\log n)\\) \u3002\u5c3d\u7ba1\u63d2\u5165\u6392\u5e8f\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u66f4\u9ad8\uff0c\u4f46\u5728\u6570\u636e\u91cf\u8f83\u5c0f\u7684\u60c5\u51b5\u4e0b\uff0c\u63d2\u5165\u6392\u5e8f\u901a\u5e38\u66f4\u5feb\u3002
\u8fd9\u4e2a\u7ed3\u8bba\u4e0e\u7ebf\u6027\u67e5\u627e\u548c\u4e8c\u5206\u67e5\u627e\u7684\u9002\u7528\u60c5\u51b5\u7684\u7ed3\u8bba\u7c7b\u4f3c\u3002\u5feb\u901f\u6392\u5e8f\u8fd9\u7c7b \\(O(n \\log n)\\) \u7684\u7b97\u6cd5\u5c5e\u4e8e\u57fa\u4e8e\u5206\u6cbb\u7b56\u7565\u7684\u6392\u5e8f\u7b97\u6cd5\uff0c\u5f80\u5f80\u5305\u542b\u66f4\u591a\u5355\u5143\u8ba1\u7b97\u64cd\u4f5c\u3002\u800c\u5728\u6570\u636e\u91cf\u8f83\u5c0f\u65f6\uff0c\\(n^2\\) \u548c \\(n \\log n\\) \u7684\u6570\u503c\u6bd4\u8f83\u63a5\u8fd1\uff0c\u590d\u6742\u5ea6\u4e0d\u5360\u4e3b\u5bfc\u5730\u4f4d\uff1b\u6bcf\u8f6e\u4e2d\u7684\u5355\u5143\u64cd\u4f5c\u6570\u91cf\u8d77\u5230\u51b3\u5b9a\u6027\u4f5c\u7528\u3002
\u5b9e\u9645\u4e0a\uff0c\u8bb8\u591a\u7f16\u7a0b\u8bed\u8a00\uff08\u4f8b\u5982 Java\uff09\u7684\u5185\u7f6e\u6392\u5e8f\u51fd\u6570\u91c7\u7528\u4e86\u63d2\u5165\u6392\u5e8f\uff0c\u5927\u81f4\u601d\u8def\u4e3a\uff1a\u5bf9\u4e8e\u957f\u6570\u7ec4\uff0c\u91c7\u7528\u57fa\u4e8e\u5206\u6cbb\u7b56\u7565\u7684\u6392\u5e8f\u7b97\u6cd5\uff0c\u4f8b\u5982\u5feb\u901f\u6392\u5e8f\uff1b\u5bf9\u4e8e\u77ed\u6570\u7ec4\uff0c\u76f4\u63a5\u4f7f\u7528\u63d2\u5165\u6392\u5e8f\u3002
\u867d\u7136\u5192\u6ce1\u6392\u5e8f\u3001\u9009\u62e9\u6392\u5e8f\u548c\u63d2\u5165\u6392\u5e8f\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u90fd\u4e3a \\(O(n^2)\\) \uff0c\u4f46\u5728\u5b9e\u9645\u60c5\u51b5\u4e2d\uff0c\u63d2\u5165\u6392\u5e8f\u7684\u4f7f\u7528\u9891\u7387\u663e\u8457\u9ad8\u4e8e\u5192\u6ce1\u6392\u5e8f\u548c\u9009\u62e9\u6392\u5e8f\uff0c\u4e3b\u8981\u6709\u4ee5\u4e0b\u539f\u56e0\u3002
\u300c\u5f52\u5e76\u6392\u5e8f merge sort\u300d\u662f\u4e00\u79cd\u57fa\u4e8e\u5206\u6cbb\u7b56\u7565\u7684\u6392\u5e8f\u7b97\u6cd5\uff0c\u5305\u542b\u56fe 11-10 \u6240\u793a\u7684\u201c\u5212\u5206\u201d\u548c\u201c\u5408\u5e76\u201d\u9636\u6bb5\u3002
\u56fe 11-10 \u00a0 \u5f52\u5e76\u6392\u5e8f\u7684\u5212\u5206\u4e0e\u5408\u5e76\u9636\u6bb5
"},{"location":"chapter_sorting/merge_sort/#1161","title":"11.6.1 \u00a0 \u7b97\u6cd5\u6d41\u7a0b","text":"\u5982\u56fe 11-11 \u6240\u793a\uff0c\u201c\u5212\u5206\u9636\u6bb5\u201d\u4ece\u9876\u81f3\u5e95\u9012\u5f52\u5730\u5c06\u6570\u7ec4\u4ece\u4e2d\u70b9\u5207\u5206\u4e3a\u4e24\u4e2a\u5b50\u6570\u7ec4\u3002
mid
\uff0c\u9012\u5f52\u5212\u5206\u5de6\u5b50\u6570\u7ec4\uff08\u533a\u95f4 [left, mid]
\uff09\u548c\u53f3\u5b50\u6570\u7ec4\uff08\u533a\u95f4 [mid + 1, right]
\uff09\u30021.
\uff0c\u76f4\u81f3\u5b50\u6570\u7ec4\u533a\u95f4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u3002\u201c\u5408\u5e76\u9636\u6bb5\u201d\u4ece\u5e95\u81f3\u9876\u5730\u5c06\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u5408\u5e76\u4e3a\u4e00\u4e2a\u6709\u5e8f\u6570\u7ec4\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u4ece\u957f\u5ea6\u4e3a 1 \u7684\u5b50\u6570\u7ec4\u5f00\u59cb\u5408\u5e76\uff0c\u5408\u5e76\u9636\u6bb5\u4e2d\u7684\u6bcf\u4e2a\u5b50\u6570\u7ec4\u90fd\u662f\u6709\u5e8f\u7684\u3002
<1><2><3><4><5><6><7><8><9><10>\u56fe 11-11 \u00a0 \u5f52\u5e76\u6392\u5e8f\u6b65\u9aa4
\u89c2\u5bdf\u53d1\u73b0\uff0c\u5f52\u5e76\u6392\u5e8f\u4e0e\u4e8c\u53c9\u6811\u540e\u5e8f\u904d\u5386\u7684\u9012\u5f52\u987a\u5e8f\u662f\u4e00\u81f4\u7684\u3002
\u5f52\u5e76\u6392\u5e8f\u7684\u5b9e\u73b0\u5982\u4ee5\u4e0b\u4ee3\u7801\u6240\u793a\u3002\u8bf7\u6ce8\u610f\uff0cnums
\u7684\u5f85\u5408\u5e76\u533a\u95f4\u4e3a [left, right]
\uff0c\u800c tmp
\u7684\u5bf9\u5e94\u533a\u95f4\u4e3a [0, right - left]
\u3002
def merge(nums: list[int], left: int, mid: int, right: int):\n \"\"\"\u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\"\"\"\n # \u5de6\u5b50\u6570\u7ec4\u533a\u95f4 [left, mid], \u53f3\u5b50\u6570\u7ec4\u533a\u95f4 [mid+1, right]\n # \u521b\u5efa\u4e00\u4e2a\u4e34\u65f6\u6570\u7ec4 tmp \uff0c\u7528\u4e8e\u5b58\u653e\u5408\u5e76\u540e\u7684\u7ed3\u679c\n tmp = [0] * (right - left + 1)\n # \u521d\u59cb\u5316\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\n i, j, k = left, mid + 1, 0\n # \u5f53\u5de6\u53f3\u5b50\u6570\u7ec4\u90fd\u8fd8\u6709\u5143\u7d20\u65f6\uff0c\u6bd4\u8f83\u5e76\u5c06\u8f83\u5c0f\u7684\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n while i <= mid and j <= right:\n if nums[i] <= nums[j]:\n tmp[k] = nums[i]\n i += 1\n else:\n tmp[k] = nums[j]\n j += 1\n k += 1\n # \u5c06\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u5269\u4f59\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n while i <= mid:\n tmp[k] = nums[i]\n i += 1\n k += 1\n while j <= right:\n tmp[k] = nums[j]\n j += 1\n k += 1\n # \u5c06\u4e34\u65f6\u6570\u7ec4 tmp \u4e2d\u7684\u5143\u7d20\u590d\u5236\u56de\u539f\u6570\u7ec4 nums \u7684\u5bf9\u5e94\u533a\u95f4\n for k in range(0, len(tmp)):\n nums[left + k] = tmp[k]\n\ndef merge_sort(nums: list[int], left: int, right: int):\n \"\"\"\u5f52\u5e76\u6392\u5e8f\"\"\"\n # \u7ec8\u6b62\u6761\u4ef6\n if left >= right:\n return # \u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n # \u5212\u5206\u9636\u6bb5\n mid = (left + right) // 2 # \u8ba1\u7b97\u4e2d\u70b9\n merge_sort(nums, left, mid) # \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\n merge_sort(nums, mid + 1, right) # \u9012\u5f52\u53f3\u5b50\u6570\u7ec4\n # \u5408\u5e76\u9636\u6bb5\n merge(nums, left, mid, right)\n
merge_sort.cpp/* \u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4 */\nvoid merge(vector<int> &nums, int left, int mid, int right) {\n // \u5de6\u5b50\u6570\u7ec4\u533a\u95f4 [left, mid], \u53f3\u5b50\u6570\u7ec4\u533a\u95f4 [mid+1, right]\n // \u521b\u5efa\u4e00\u4e2a\u4e34\u65f6\u6570\u7ec4 tmp \uff0c\u7528\u4e8e\u5b58\u653e\u5408\u5e76\u540e\u7684\u7ed3\u679c\n vector<int> tmp(right - left + 1);\n // \u521d\u59cb\u5316\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\n int i = left, j = mid + 1, k = 0;\n // \u5f53\u5de6\u53f3\u5b50\u6570\u7ec4\u90fd\u8fd8\u6709\u5143\u7d20\u65f6\uff0c\u6bd4\u8f83\u5e76\u5c06\u8f83\u5c0f\u7684\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n while (i <= mid && j <= right) {\n if (nums[i] <= nums[j])\n tmp[k++] = nums[i++];\n else\n tmp[k++] = nums[j++];\n }\n // \u5c06\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u5269\u4f59\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n while (i <= mid) {\n tmp[k++] = nums[i++];\n }\n while (j <= right) {\n tmp[k++] = nums[j++];\n }\n // \u5c06\u4e34\u65f6\u6570\u7ec4 tmp \u4e2d\u7684\u5143\u7d20\u590d\u5236\u56de\u539f\u6570\u7ec4 nums \u7684\u5bf9\u5e94\u533a\u95f4\n for (k = 0; k < tmp.size(); k++) {\n nums[left + k] = tmp[k];\n }\n}\n\n/* \u5f52\u5e76\u6392\u5e8f */\nvoid mergeSort(vector<int> &nums, int left, int right) {\n // \u7ec8\u6b62\u6761\u4ef6\n if (left >= right)\n return; // \u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n // \u5212\u5206\u9636\u6bb5\n int mid = (left + right) / 2; // \u8ba1\u7b97\u4e2d\u70b9\n mergeSort(nums, left, mid); // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\n mergeSort(nums, mid + 1, right); // \u9012\u5f52\u53f3\u5b50\u6570\u7ec4\n // \u5408\u5e76\u9636\u6bb5\n merge(nums, left, mid, right);\n}\n
merge_sort.java/* \u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4 */\nvoid merge(int[] nums, int left, int mid, int right) {\n // \u5de6\u5b50\u6570\u7ec4\u533a\u95f4 [left, mid], \u53f3\u5b50\u6570\u7ec4\u533a\u95f4 [mid+1, right]\n // \u521b\u5efa\u4e00\u4e2a\u4e34\u65f6\u6570\u7ec4 tmp \uff0c\u7528\u4e8e\u5b58\u653e\u5408\u5e76\u540e\u7684\u7ed3\u679c\n int[] tmp = new int[right - left + 1];\n // \u521d\u59cb\u5316\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\n int i = left, j = mid + 1, k = 0;\n // \u5f53\u5de6\u53f3\u5b50\u6570\u7ec4\u90fd\u8fd8\u6709\u5143\u7d20\u65f6\uff0c\u6bd4\u8f83\u5e76\u5c06\u8f83\u5c0f\u7684\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n while (i <= mid && j <= right) {\n if (nums[i] <= nums[j])\n tmp[k++] = nums[i++];\n else\n tmp[k++] = nums[j++];\n }\n // \u5c06\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u5269\u4f59\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n while (i <= mid) {\n tmp[k++] = nums[i++];\n }\n while (j <= right) {\n tmp[k++] = nums[j++];\n }\n // \u5c06\u4e34\u65f6\u6570\u7ec4 tmp \u4e2d\u7684\u5143\u7d20\u590d\u5236\u56de\u539f\u6570\u7ec4 nums \u7684\u5bf9\u5e94\u533a\u95f4\n for (k = 0; k < tmp.length; k++) {\n nums[left + k] = tmp[k];\n }\n}\n\n/* \u5f52\u5e76\u6392\u5e8f */\nvoid mergeSort(int[] nums, int left, int right) {\n // \u7ec8\u6b62\u6761\u4ef6\n if (left >= right)\n return; // \u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n // \u5212\u5206\u9636\u6bb5\n int mid = (left + right) / 2; // \u8ba1\u7b97\u4e2d\u70b9\n mergeSort(nums, left, mid); // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\n mergeSort(nums, mid + 1, right); // \u9012\u5f52\u53f3\u5b50\u6570\u7ec4\n // \u5408\u5e76\u9636\u6bb5\n merge(nums, left, mid, right);\n}\n
merge_sort.cs/* \u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4 */\nvoid Merge(int[] nums, int left, int mid, int right) {\n // \u5de6\u5b50\u6570\u7ec4\u533a\u95f4 [left, mid], \u53f3\u5b50\u6570\u7ec4\u533a\u95f4 [mid+1, right]\n // \u521b\u5efa\u4e00\u4e2a\u4e34\u65f6\u6570\u7ec4 tmp \uff0c\u7528\u4e8e\u5b58\u653e\u5408\u5e76\u540e\u7684\u7ed3\u679c\n int[] tmp = new int[right - left + 1];\n // \u521d\u59cb\u5316\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\n int i = left, j = mid + 1, k = 0;\n // \u5f53\u5de6\u53f3\u5b50\u6570\u7ec4\u90fd\u8fd8\u6709\u5143\u7d20\u65f6\uff0c\u6bd4\u8f83\u5e76\u5c06\u8f83\u5c0f\u7684\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n while (i <= mid && j <= right) {\n if (nums[i] <= nums[j])\n tmp[k++] = nums[i++];\n else\n tmp[k++] = nums[j++];\n }\n // \u5c06\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u5269\u4f59\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n while (i <= mid) {\n tmp[k++] = nums[i++];\n }\n while (j <= right) {\n tmp[k++] = nums[j++];\n }\n // \u5c06\u4e34\u65f6\u6570\u7ec4 tmp \u4e2d\u7684\u5143\u7d20\u590d\u5236\u56de\u539f\u6570\u7ec4 nums \u7684\u5bf9\u5e94\u533a\u95f4\n for (k = 0; k < tmp.Length; ++k) {\n nums[left + k] = tmp[k];\n }\n}\n\n/* \u5f52\u5e76\u6392\u5e8f */\nvoid MergeSort(int[] nums, int left, int right) {\n // \u7ec8\u6b62\u6761\u4ef6\n if (left >= right) return; // \u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n // \u5212\u5206\u9636\u6bb5\n int mid = (left + right) / 2; // \u8ba1\u7b97\u4e2d\u70b9\n MergeSort(nums, left, mid); // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\n MergeSort(nums, mid + 1, right); // \u9012\u5f52\u53f3\u5b50\u6570\u7ec4\n // \u5408\u5e76\u9636\u6bb5\n Merge(nums, left, mid, right);\n}\n
merge_sort.go/* \u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4 */\nfunc merge(nums []int, left, mid, right int) {\n // \u5de6\u5b50\u6570\u7ec4\u533a\u95f4 [left, mid], \u53f3\u5b50\u6570\u7ec4\u533a\u95f4 [mid+1, right]\n // \u521b\u5efa\u4e00\u4e2a\u4e34\u65f6\u6570\u7ec4 tmp \uff0c\u7528\u4e8e\u5b58\u653e\u5408\u5e76\u540e\u7684\u7ed3\u679c\n tmp := make([]int, right-left+1)\n // \u521d\u59cb\u5316\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\n i, j, k := left, mid+1, 0\n // \u5f53\u5de6\u53f3\u5b50\u6570\u7ec4\u90fd\u8fd8\u6709\u5143\u7d20\u65f6\uff0c\u6bd4\u8f83\u5e76\u5c06\u8f83\u5c0f\u7684\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n for i <= mid && j <= right {\n if nums[i] <= nums[j] {\n tmp[k] = nums[i]\n i++\n } else {\n tmp[k] = nums[j]\n j++\n }\n k++\n }\n // \u5c06\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u5269\u4f59\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n for i <= mid {\n tmp[k] = nums[i]\n i++\n k++\n }\n for j <= right {\n tmp[k] = nums[j]\n j++\n k++\n }\n // \u5c06\u4e34\u65f6\u6570\u7ec4 tmp \u4e2d\u7684\u5143\u7d20\u590d\u5236\u56de\u539f\u6570\u7ec4 nums \u7684\u5bf9\u5e94\u533a\u95f4\n for k := 0; k < len(tmp); k++ {\n nums[left+k] = tmp[k]\n }\n}\n\n/* \u5f52\u5e76\u6392\u5e8f */\nfunc mergeSort(nums []int, left, right int) {\n // \u7ec8\u6b62\u6761\u4ef6\n if left >= right {\n return\n }\n // \u5212\u5206\u9636\u6bb5\n mid := (left + right) / 2\n mergeSort(nums, left, mid)\n mergeSort(nums, mid+1, right)\n // \u5408\u5e76\u9636\u6bb5\n merge(nums, left, mid, right)\n}\n
merge_sort.swift/* \u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4 */\nfunc merge(nums: inout [Int], left: Int, mid: Int, right: Int) {\n // \u5de6\u5b50\u6570\u7ec4\u533a\u95f4 [left, mid], \u53f3\u5b50\u6570\u7ec4\u533a\u95f4 [mid+1, right]\n // \u521b\u5efa\u4e00\u4e2a\u4e34\u65f6\u6570\u7ec4 tmp \uff0c\u7528\u4e8e\u5b58\u653e\u5408\u5e76\u540e\u7684\u7ed3\u679c\n var tmp = Array(repeating: 0, count: right - left + 1)\n // \u521d\u59cb\u5316\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\n var i = left, j = mid + 1, k = 0\n // \u5f53\u5de6\u53f3\u5b50\u6570\u7ec4\u90fd\u8fd8\u6709\u5143\u7d20\u65f6\uff0c\u6bd4\u8f83\u5e76\u5c06\u8f83\u5c0f\u7684\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n while i <= mid, j <= right {\n if nums[i] <= nums[j] {\n tmp[k] = nums[i]\n i += 1\n k += 1\n } else {\n tmp[k] = nums[j]\n j += 1\n k += 1\n }\n }\n // \u5c06\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u5269\u4f59\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n while i <= mid {\n tmp[k] = nums[i]\n i += 1\n k += 1\n }\n while j <= right {\n tmp[k] = nums[j]\n j += 1\n k += 1\n }\n // \u5c06\u4e34\u65f6\u6570\u7ec4 tmp \u4e2d\u7684\u5143\u7d20\u590d\u5236\u56de\u539f\u6570\u7ec4 nums \u7684\u5bf9\u5e94\u533a\u95f4\n for k in tmp.indices {\n nums[left + k] = tmp[k]\n }\n}\n\n/* \u5f52\u5e76\u6392\u5e8f */\nfunc mergeSort(nums: inout [Int], left: Int, right: Int) {\n // \u7ec8\u6b62\u6761\u4ef6\n if left >= right { // \u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n return\n }\n // \u5212\u5206\u9636\u6bb5\n let mid = (left + right) / 2 // \u8ba1\u7b97\u4e2d\u70b9\n mergeSort(nums: &nums, left: left, right: mid) // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\n mergeSort(nums: &nums, left: mid + 1, right: right) // \u9012\u5f52\u53f3\u5b50\u6570\u7ec4\n // \u5408\u5e76\u9636\u6bb5\n merge(nums: &nums, left: left, mid: mid, right: right)\n}\n
merge_sort.js/* \u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4 */\nfunction merge(nums, left, mid, right) {\n // \u5de6\u5b50\u6570\u7ec4\u533a\u95f4 [left, mid], \u53f3\u5b50\u6570\u7ec4\u533a\u95f4 [mid+1, right]\n // \u521b\u5efa\u4e00\u4e2a\u4e34\u65f6\u6570\u7ec4 tmp \uff0c\u7528\u4e8e\u5b58\u653e\u5408\u5e76\u540e\u7684\u7ed3\u679c\n const tmp = new Array(right - left + 1);\n // \u521d\u59cb\u5316\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\n let i = left,\n j = mid + 1,\n k = 0;\n // \u5f53\u5de6\u53f3\u5b50\u6570\u7ec4\u90fd\u8fd8\u6709\u5143\u7d20\u65f6\uff0c\u6bd4\u8f83\u5e76\u5c06\u8f83\u5c0f\u7684\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n while (i <= mid && j <= right) {\n if (nums[i] <= nums[j]) {\n tmp[k++] = nums[i++];\n } else {\n tmp[k++] = nums[j++];\n }\n }\n // \u5c06\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u5269\u4f59\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n while (i <= mid) {\n tmp[k++] = nums[i++];\n }\n while (j <= right) {\n tmp[k++] = nums[j++];\n }\n // \u5c06\u4e34\u65f6\u6570\u7ec4 tmp \u4e2d\u7684\u5143\u7d20\u590d\u5236\u56de\u539f\u6570\u7ec4 nums \u7684\u5bf9\u5e94\u533a\u95f4\n for (k = 0; k < tmp.length; k++) {\n nums[left + k] = tmp[k];\n }\n}\n\n/* \u5f52\u5e76\u6392\u5e8f */\nfunction mergeSort(nums, left, right) {\n // \u7ec8\u6b62\u6761\u4ef6\n if (left >= right) return; // \u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n // \u5212\u5206\u9636\u6bb5\n let mid = Math.floor((left + right) / 2); // \u8ba1\u7b97\u4e2d\u70b9\n mergeSort(nums, left, mid); // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\n mergeSort(nums, mid + 1, right); // \u9012\u5f52\u53f3\u5b50\u6570\u7ec4\n // \u5408\u5e76\u9636\u6bb5\n merge(nums, left, mid, right);\n}\n
merge_sort.ts/* \u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4 */\nfunction merge(nums: number[], left: number, mid: number, right: number): void {\n // \u5de6\u5b50\u6570\u7ec4\u533a\u95f4 [left, mid], \u53f3\u5b50\u6570\u7ec4\u533a\u95f4 [mid+1, right]\n // \u521b\u5efa\u4e00\u4e2a\u4e34\u65f6\u6570\u7ec4 tmp \uff0c\u7528\u4e8e\u5b58\u653e\u5408\u5e76\u540e\u7684\u7ed3\u679c\n const tmp = new Array(right - left + 1);\n // \u521d\u59cb\u5316\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\n let i = left,\n j = mid + 1,\n k = 0;\n // \u5f53\u5de6\u53f3\u5b50\u6570\u7ec4\u90fd\u8fd8\u6709\u5143\u7d20\u65f6\uff0c\u6bd4\u8f83\u5e76\u5c06\u8f83\u5c0f\u7684\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n while (i <= mid && j <= right) {\n if (nums[i] <= nums[j]) {\n tmp[k++] = nums[i++];\n } else {\n tmp[k++] = nums[j++];\n }\n }\n // \u5c06\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u5269\u4f59\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n while (i <= mid) {\n tmp[k++] = nums[i++];\n }\n while (j <= right) {\n tmp[k++] = nums[j++];\n }\n // \u5c06\u4e34\u65f6\u6570\u7ec4 tmp \u4e2d\u7684\u5143\u7d20\u590d\u5236\u56de\u539f\u6570\u7ec4 nums \u7684\u5bf9\u5e94\u533a\u95f4\n for (k = 0; k < tmp.length; k++) {\n nums[left + k] = tmp[k];\n }\n}\n\n/* \u5f52\u5e76\u6392\u5e8f */\nfunction mergeSort(nums: number[], left: number, right: number): void {\n // \u7ec8\u6b62\u6761\u4ef6\n if (left >= right) return; // \u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n // \u5212\u5206\u9636\u6bb5\n let mid = Math.floor((left + right) / 2); // \u8ba1\u7b97\u4e2d\u70b9\n mergeSort(nums, left, mid); // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\n mergeSort(nums, mid + 1, right); // \u9012\u5f52\u53f3\u5b50\u6570\u7ec4\n // \u5408\u5e76\u9636\u6bb5\n merge(nums, left, mid, right);\n}\n
merge_sort.dart/* \u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4 */\nvoid merge(List<int> nums, int left, int mid, int right) {\n // \u5de6\u5b50\u6570\u7ec4\u533a\u95f4 [left, mid], \u53f3\u5b50\u6570\u7ec4\u533a\u95f4 [mid+1, right]\n // \u521b\u5efa\u4e00\u4e2a\u4e34\u65f6\u6570\u7ec4 tmp \uff0c\u7528\u4e8e\u5b58\u653e\u5408\u5e76\u540e\u7684\u7ed3\u679c\n List<int> tmp = List.filled(right - left + 1, 0);\n // \u521d\u59cb\u5316\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\n int i = left, j = mid + 1, k = 0;\n // \u5f53\u5de6\u53f3\u5b50\u6570\u7ec4\u90fd\u8fd8\u6709\u5143\u7d20\u65f6\uff0c\u6bd4\u8f83\u5e76\u5c06\u8f83\u5c0f\u7684\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n while (i <= mid && j <= right) {\n if (nums[i] <= nums[j])\n tmp[k++] = nums[i++];\n else\n tmp[k++] = nums[j++];\n }\n // \u5c06\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u5269\u4f59\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n while (i <= mid) {\n tmp[k++] = nums[i++];\n }\n while (j <= right) {\n tmp[k++] = nums[j++];\n }\n // \u5c06\u4e34\u65f6\u6570\u7ec4 tmp \u4e2d\u7684\u5143\u7d20\u590d\u5236\u56de\u539f\u6570\u7ec4 nums \u7684\u5bf9\u5e94\u533a\u95f4\n for (k = 0; k < tmp.length; k++) {\n nums[left + k] = tmp[k];\n }\n}\n\n/* \u5f52\u5e76\u6392\u5e8f */\nvoid mergeSort(List<int> nums, int left, int right) {\n // \u7ec8\u6b62\u6761\u4ef6\n if (left >= right) return; // \u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n // \u5212\u5206\u9636\u6bb5\n int mid = (left + right) ~/ 2; // \u8ba1\u7b97\u4e2d\u70b9\n mergeSort(nums, left, mid); // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\n mergeSort(nums, mid + 1, right); // \u9012\u5f52\u53f3\u5b50\u6570\u7ec4\n // \u5408\u5e76\u9636\u6bb5\n merge(nums, left, mid, right);\n}\n
merge_sort.rs/* \u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4 */\nfn merge(nums: &mut [i32], left: usize, mid: usize, right: usize) {\n // \u5de6\u5b50\u6570\u7ec4\u533a\u95f4 [left, mid], \u53f3\u5b50\u6570\u7ec4\u533a\u95f4 [mid+1, right]\n // \u521b\u5efa\u4e00\u4e2a\u4e34\u65f6\u6570\u7ec4 tmp \uff0c\u7528\u4e8e\u5b58\u653e\u5408\u5e76\u540e\u7684\u7ed3\u679c\n let tmp_size = right - left + 1;\n let mut tmp = vec![0; tmp_size];\n // \u521d\u59cb\u5316\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\n let (mut i, mut j, mut k) = (left, mid + 1, 0);\n // \u5f53\u5de6\u53f3\u5b50\u6570\u7ec4\u90fd\u8fd8\u6709\u5143\u7d20\u65f6\uff0c\u6bd4\u8f83\u5e76\u5c06\u8f83\u5c0f\u7684\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n while i <= mid && j <= right {\n if nums[i] <= nums[j] {\n tmp[k] = nums[j];\n i += 1;\n } else {\n tmp[k] = nums[j];\n j += 1;\n }\n k += 1;\n }\n // \u5c06\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u5269\u4f59\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n while i <= mid {\n tmp[k] = nums[i];\n k += 1;\n i += 1;\n }\n while j <= right {\n tmp[k] = nums[j];\n k += 1;\n j += 1;\n }\n // \u5c06\u4e34\u65f6\u6570\u7ec4 tmp \u4e2d\u7684\u5143\u7d20\u590d\u5236\u56de\u539f\u6570\u7ec4 nums \u7684\u5bf9\u5e94\u533a\u95f4\n for k in 0..tmp_size {\n nums[left + k] = tmp[k];\n }\n}\n\n/* \u5f52\u5e76\u6392\u5e8f */\nfn merge_sort(nums: &mut [i32], left: usize, right: usize) {\n // \u7ec8\u6b62\u6761\u4ef6\n if left >= right { return; } // \u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n // \u5212\u5206\u9636\u6bb5\n let mid = (left + right) / 2; // \u8ba1\u7b97\u4e2d\u70b9\n merge_sort(nums, left, mid); // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\n merge_sort(nums, mid + 1, right); // \u9012\u5f52\u53f3\u5b50\u6570\u7ec4\n // \u5408\u5e76\u9636\u6bb5\n merge(nums, left, mid, right);\n}\n
merge_sort.c/* \u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4 */\nvoid merge(int *nums, int left, int mid, int right) {\n // \u5de6\u5b50\u6570\u7ec4\u533a\u95f4 [left, mid], \u53f3\u5b50\u6570\u7ec4\u533a\u95f4 [mid+1, right]\n // \u521b\u5efa\u4e00\u4e2a\u4e34\u65f6\u6570\u7ec4 tmp \uff0c\u7528\u4e8e\u5b58\u653e\u5408\u5e76\u540e\u7684\u7ed3\u679c\n int tmpSize = right - left + 1;\n int *tmp = (int *)malloc(tmpSize * sizeof(int));\n // \u521d\u59cb\u5316\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\n int i = left, j = mid + 1, k = 0;\n // \u5f53\u5de6\u53f3\u5b50\u6570\u7ec4\u90fd\u8fd8\u6709\u5143\u7d20\u65f6\uff0c\u6bd4\u8f83\u5e76\u5c06\u8f83\u5c0f\u7684\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n while (i <= mid && j <= right) {\n if (nums[i] <= nums[j]) {\n tmp[k++] = nums[i++];\n } else {\n tmp[k++] = nums[j++];\n }\n }\n // \u5c06\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u5269\u4f59\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n while (i <= mid) {\n tmp[k++] = nums[i++];\n }\n while (j <= right) {\n tmp[k++] = nums[j++];\n }\n // \u5c06\u4e34\u65f6\u6570\u7ec4 tmp \u4e2d\u7684\u5143\u7d20\u590d\u5236\u56de\u539f\u6570\u7ec4 nums \u7684\u5bf9\u5e94\u533a\u95f4\n for (k = 0; k < tmpSize; ++k) {\n nums[left + k] = tmp[k];\n }\n // \u91ca\u653e\u5185\u5b58\n free(tmp);\n}\n\n/* \u5f52\u5e76\u6392\u5e8f */\nvoid mergeSort(int *nums, int left, int right) {\n // \u7ec8\u6b62\u6761\u4ef6\n if (left >= right)\n return; // \u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n // \u5212\u5206\u9636\u6bb5\n int mid = (left + right) / 2; // \u8ba1\u7b97\u4e2d\u70b9\n mergeSort(nums, left, mid); // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\n mergeSort(nums, mid + 1, right); // \u9012\u5f52\u53f3\u5b50\u6570\u7ec4\n // \u5408\u5e76\u9636\u6bb5\n merge(nums, left, mid, right);\n}\n
merge_sort.zig// \u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\n// \u5de6\u5b50\u6570\u7ec4\u533a\u95f4 [left, mid]\n// \u53f3\u5b50\u6570\u7ec4\u533a\u95f4 [mid + 1, right]\nfn merge(nums: []i32, left: usize, mid: usize, right: usize) !void {\n // \u521d\u59cb\u5316\u8f85\u52a9\u6570\u7ec4\n var mem_arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);\n defer mem_arena.deinit();\n const mem_allocator = mem_arena.allocator();\n var tmp = try mem_allocator.alloc(i32, right + 1 - left);\n std.mem.copy(i32, tmp, nums[left..right+1]);\n // \u5de6\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15 \n var leftStart = left - left;\n var leftEnd = mid - left;\n // \u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15 \n var rightStart = mid + 1 - left;\n var rightEnd = right - left;\n // i, j \u5206\u522b\u6307\u5411\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\u7684\u9996\u5143\u7d20\n var i = leftStart;\n var j = rightStart;\n // \u901a\u8fc7\u8986\u76d6\u539f\u6570\u7ec4 nums \u6765\u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\n var k = left;\n while (k <= right) : (k += 1) {\n // \u82e5\u201c\u5de6\u5b50\u6570\u7ec4\u5df2\u5168\u90e8\u5408\u5e76\u5b8c\u201d\uff0c\u5219\u9009\u53d6\u53f3\u5b50\u6570\u7ec4\u5143\u7d20\uff0c\u5e76\u4e14 j++\n if (i > leftEnd) {\n nums[k] = tmp[j];\n j += 1;\n // \u5426\u5219\uff0c\u82e5\u201c\u53f3\u5b50\u6570\u7ec4\u5df2\u5168\u90e8\u5408\u5e76\u5b8c\u201d\u6216\u201c\u5de6\u5b50\u6570\u7ec4\u5143\u7d20 <= \u53f3\u5b50\u6570\u7ec4\u5143\u7d20\u201d\uff0c\u5219\u9009\u53d6\u5de6\u5b50\u6570\u7ec4\u5143\u7d20\uff0c\u5e76\u4e14 i++\n } else if (j > rightEnd or tmp[i] <= tmp[j]) {\n nums[k] = tmp[i];\n i += 1;\n // \u5426\u5219\uff0c\u82e5\u201c\u5de6\u53f3\u5b50\u6570\u7ec4\u90fd\u672a\u5168\u90e8\u5408\u5e76\u5b8c\u201d\u4e14\u201c\u5de6\u5b50\u6570\u7ec4\u5143\u7d20 > \u53f3\u5b50\u6570\u7ec4\u5143\u7d20\u201d\uff0c\u5219\u9009\u53d6\u53f3\u5b50\u6570\u7ec4\u5143\u7d20\uff0c\u5e76\u4e14 j++\n } else {\n nums[k] = tmp[j];\n j += 1;\n }\n }\n}\n\n// \u5f52\u5e76\u6392\u5e8f\nfn mergeSort(nums: []i32, left: usize, right: usize) !void {\n // \u7ec8\u6b62\u6761\u4ef6\n if (left >= right) return; // \u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n // \u5212\u5206\u9636\u6bb5\n var mid = (left + right) / 2; // \u8ba1\u7b97\u4e2d\u70b9\n try mergeSort(nums, left, mid); // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\n try mergeSort(nums, mid + 1, right); // \u9012\u5f52\u53f3\u5b50\u6570\u7ec4\n // \u5408\u5e76\u9636\u6bb5\n try merge(nums, left, mid, right);\n}\n
"},{"location":"chapter_sorting/merge_sort/#1162","title":"11.6.2 \u00a0 \u7b97\u6cd5\u7279\u6027","text":"\u5bf9\u4e8e\u94fe\u8868\uff0c\u5f52\u5e76\u6392\u5e8f\u76f8\u8f83\u4e8e\u5176\u4ed6\u6392\u5e8f\u7b97\u6cd5\u5177\u6709\u663e\u8457\u4f18\u52bf\uff0c\u53ef\u4ee5\u5c06\u94fe\u8868\u6392\u5e8f\u4efb\u52a1\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u4f18\u5316\u81f3 \\(O(1)\\) \u3002
\u5177\u4f53\u5b9e\u73b0\u7ec6\u8282\u6bd4\u8f83\u590d\u6742\uff0c\u6709\u5174\u8da3\u7684\u8bfb\u8005\u53ef\u4ee5\u67e5\u9605\u76f8\u5173\u8d44\u6599\u8fdb\u884c\u5b66\u4e60\u3002
"},{"location":"chapter_sorting/quick_sort/","title":"11.5 \u00a0 \u5feb\u901f\u6392\u5e8f","text":"\u300c\u5feb\u901f\u6392\u5e8f quick sort\u300d\u662f\u4e00\u79cd\u57fa\u4e8e\u5206\u6cbb\u7b56\u7565\u7684\u6392\u5e8f\u7b97\u6cd5\uff0c\u8fd0\u884c\u9ad8\u6548\uff0c\u5e94\u7528\u5e7f\u6cdb\u3002
\u5feb\u901f\u6392\u5e8f\u7684\u6838\u5fc3\u64cd\u4f5c\u662f\u201c\u54e8\u5175\u5212\u5206\u201d\uff0c\u5176\u76ee\u6807\u662f\uff1a\u9009\u62e9\u6570\u7ec4\u4e2d\u7684\u67d0\u4e2a\u5143\u7d20\u4f5c\u4e3a\u201c\u57fa\u51c6\u6570\u201d\uff0c\u5c06\u6240\u6709\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\u79fb\u5230\u5176\u5de6\u4fa7\uff0c\u800c\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\u79fb\u5230\u5176\u53f3\u4fa7\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u54e8\u5175\u5212\u5206\u7684\u6d41\u7a0b\u5982\u56fe 11-8 \u6240\u793a\u3002
i
\u548c j
\u5206\u522b\u6307\u5411\u6570\u7ec4\u7684\u4e24\u7aef\u3002i
\uff08j
\uff09\u5206\u522b\u5bfb\u627e\u7b2c\u4e00\u4e2a\u6bd4\u57fa\u51c6\u6570\u5927\uff08\u5c0f\uff09\u7684\u5143\u7d20\uff0c\u7136\u540e\u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\u30022.
\uff0c\u76f4\u5230 i
\u548c j
\u76f8\u9047\u65f6\u505c\u6b62\uff0c\u6700\u540e\u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u4e2a\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\u3002\u56fe 11-8 \u00a0 \u54e8\u5175\u5212\u5206\u6b65\u9aa4
\u54e8\u5175\u5212\u5206\u5b8c\u6210\u540e\uff0c\u539f\u6570\u7ec4\u88ab\u5212\u5206\u6210\u4e09\u90e8\u5206\uff1a\u5de6\u5b50\u6570\u7ec4\u3001\u57fa\u51c6\u6570\u3001\u53f3\u5b50\u6570\u7ec4\uff0c\u4e14\u6ee1\u8db3\u201c\u5de6\u5b50\u6570\u7ec4\u4efb\u610f\u5143\u7d20 \\(\\leq\\) \u57fa\u51c6\u6570 \\(\\leq\\) \u53f3\u5b50\u6570\u7ec4\u4efb\u610f\u5143\u7d20\u201d\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u63a5\u4e0b\u6765\u53ea\u9700\u5bf9\u8fd9\u4e24\u4e2a\u5b50\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u3002
\u5feb\u901f\u6392\u5e8f\u7684\u5206\u6cbb\u7b56\u7565
\u54e8\u5175\u5212\u5206\u7684\u5b9e\u8d28\u662f\u5c06\u4e00\u4e2a\u8f83\u957f\u6570\u7ec4\u7684\u6392\u5e8f\u95ee\u9898\u7b80\u5316\u4e3a\u4e24\u4e2a\u8f83\u77ed\u6570\u7ec4\u7684\u6392\u5e8f\u95ee\u9898\u3002
PythonC++JavaC#GoSwiftJSTSDartRustCZig quick_sort.pydef partition(self, nums: list[int], left: int, right: int) -> int:\n \"\"\"\u54e8\u5175\u5212\u5206\"\"\"\n # \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n i, j = left, right\n while i < j:\n while i < j and nums[j] >= nums[left]:\n j -= 1 # \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n while i < j and nums[i] <= nums[left]:\n i += 1 # \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n # \u5143\u7d20\u4ea4\u6362\n nums[i], nums[j] = nums[j], nums[i]\n # \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n nums[i], nums[left] = nums[left], nums[i]\n return i # \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n
quick_sort.cpp/* \u5143\u7d20\u4ea4\u6362 */\nvoid swap(vector<int> &nums, int i, int j) {\n int tmp = nums[i];\n nums[i] = nums[j];\n nums[j] = tmp;\n}\n\n/* \u54e8\u5175\u5212\u5206 */\nint partition(vector<int> &nums, int left, int right) {\n // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n int i = left, j = right;\n while (i < j) {\n while (i < j && nums[j] >= nums[left])\n j--; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n while (i < j && nums[i] <= nums[left])\n i++; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n swap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n }\n swap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n return i; // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
quick_sort.java/* \u5143\u7d20\u4ea4\u6362 */\nvoid swap(int[] nums, int i, int j) {\n int tmp = nums[i];\n nums[i] = nums[j];\n nums[j] = tmp;\n}\n\n/* \u54e8\u5175\u5212\u5206 */\nint partition(int[] nums, int left, int right) {\n // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n int i = left, j = right;\n while (i < j) {\n while (i < j && nums[j] >= nums[left])\n j--; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n while (i < j && nums[i] <= nums[left])\n i++; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n swap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n }\n swap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n return i; // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
quick_sort.cs/* \u5143\u7d20\u4ea4\u6362 */\nvoid Swap(int[] nums, int i, int j) {\n (nums[j], nums[i]) = (nums[i], nums[j]);\n}\n\n/* \u54e8\u5175\u5212\u5206 */\nint Partition(int[] nums, int left, int right) {\n // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n int i = left, j = right;\n while (i < j) {\n while (i < j && nums[j] >= nums[left])\n j--; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n while (i < j && nums[i] <= nums[left])\n i++; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n Swap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n }\n Swap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n return i; // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
quick_sort.go/* \u54e8\u5175\u5212\u5206 */\nfunc (q *quickSort) partition(nums []int, left, right int) int {\n // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n i, j := left, right\n for i < j {\n for i < j && nums[j] >= nums[left] {\n j-- // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n }\n for i < j && nums[i] <= nums[left] {\n i++ // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n }\n // \u5143\u7d20\u4ea4\u6362\n nums[i], nums[j] = nums[j], nums[i]\n }\n // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n nums[i], nums[left] = nums[left], nums[i]\n return i // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
quick_sort.swift/* \u5143\u7d20\u4ea4\u6362 */\nfunc swap(nums: inout [Int], i: Int, j: Int) {\n let tmp = nums[i]\n nums[i] = nums[j]\n nums[j] = tmp\n}\n\n/* \u54e8\u5175\u5212\u5206 */\nfunc partition(nums: inout [Int], left: Int, right: Int) -> Int {\n // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n var i = left\n var j = right\n while i < j {\n while i < j, nums[j] >= nums[left] {\n j -= 1 // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n }\n while i < j, nums[i] <= nums[left] {\n i += 1 // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n }\n swap(nums: &nums, i: i, j: j) // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n }\n swap(nums: &nums, i: i, j: left) // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n return i // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
quick_sort.js/* \u5143\u7d20\u4ea4\u6362 */\nswap(nums, i, j) {\n let tmp = nums[i];\n nums[i] = nums[j];\n nums[j] = tmp;\n}\n\n/* \u54e8\u5175\u5212\u5206 */\npartition(nums, left, right) {\n // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n let i = left,\n j = right;\n while (i < j) {\n while (i < j && nums[j] >= nums[left]) {\n j -= 1; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n }\n while (i < j && nums[i] <= nums[left]) {\n i += 1; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n }\n // \u5143\u7d20\u4ea4\u6362\n this.swap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n }\n this.swap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n return i; // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
quick_sort.ts/* \u5143\u7d20\u4ea4\u6362 */\nswap(nums: number[], i: number, j: number): void {\n let tmp = nums[i];\n nums[i] = nums[j];\n nums[j] = tmp;\n}\n\n/* \u54e8\u5175\u5212\u5206 */\npartition(nums: number[], left: number, right: number): number {\n // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n let i = left,\n j = right;\n while (i < j) {\n while (i < j && nums[j] >= nums[left]) {\n j -= 1; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n }\n while (i < j && nums[i] <= nums[left]) {\n i += 1; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n }\n // \u5143\u7d20\u4ea4\u6362\n this.swap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n }\n this.swap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n return i; // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
quick_sort.dart/* \u5143\u7d20\u4ea4\u6362 */\nvoid _swap(List<int> nums, int i, int j) {\n int tmp = nums[i];\n nums[i] = nums[j];\n nums[j] = tmp;\n}\n\n/* \u54e8\u5175\u5212\u5206 */\nint _partition(List<int> nums, int left, int right) {\n // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n int i = left, j = right;\n while (i < j) {\n while (i < j && nums[j] >= nums[left]) j--; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n while (i < j && nums[i] <= nums[left]) i++; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n _swap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n }\n _swap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n return i; // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
quick_sort.rs/* \u54e8\u5175\u5212\u5206 */\nfn partition(nums: &mut [i32], left: usize, right: usize) -> usize {\n // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n let (mut i, mut j) = (left, right);\n while i < j {\n while i < j && nums[j] >= nums[left] {\n j -= 1; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n }\n while i < j && nums[i] <= nums[left] {\n i += 1; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n }\n nums.swap(i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n }\n nums.swap(i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n i // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
quick_sort.c/* \u5143\u7d20\u4ea4\u6362 */\nvoid swap(int nums[], int i, int j) {\n int tmp = nums[i];\n nums[i] = nums[j];\n nums[j] = tmp;\n}\n\n/* \u5feb\u901f\u6392\u5e8f\u7c7b */\n// \u5feb\u901f\u6392\u5e8f\u7c7b-\u54e8\u5175\u5212\u5206\nint partition(int nums[], int left, int right) {\n // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n int i = left, j = right;\n while (i < j) {\n while (i < j && nums[j] >= nums[left]) {\n // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n j--;\n }\n while (i < j && nums[i] <= nums[left]) {\n // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n i++;\n }\n // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n swap(nums, i, j);\n }\n // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n swap(nums, i, left);\n // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n return i;\n}\n
quick_sort.zig// \u5143\u7d20\u4ea4\u6362\nfn swap(nums: []i32, i: usize, j: usize) void {\n var tmp = nums[i];\n nums[i] = nums[j];\n nums[j] = tmp;\n}\n\n// \u54e8\u5175\u5212\u5206\nfn partition(nums: []i32, left: usize, right: usize) usize {\n // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n var i = left;\n var j = right;\n while (i < j) {\n while (i < j and nums[j] >= nums[left]) j -= 1; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n while (i < j and nums[i] <= nums[left]) i += 1; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n swap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n }\n swap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n return i; // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
"},{"location":"chapter_sorting/quick_sort/#1151","title":"11.5.1 \u00a0 \u7b97\u6cd5\u6d41\u7a0b","text":"\u5feb\u901f\u6392\u5e8f\u7684\u6574\u4f53\u6d41\u7a0b\u5982\u56fe 11-9 \u6240\u793a\u3002
\u56fe 11-9 \u00a0 \u5feb\u901f\u6392\u5e8f\u6d41\u7a0b
PythonC++JavaC#GoSwiftJSTSDartRustCZig quick_sort.pydef quick_sort(self, nums: list[int], left: int, right: int):\n \"\"\"\u5feb\u901f\u6392\u5e8f\"\"\"\n # \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n if left >= right:\n return\n # \u54e8\u5175\u5212\u5206\n pivot = self.partition(nums, left, right)\n # \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\n self.quick_sort(nums, left, pivot - 1)\n self.quick_sort(nums, pivot + 1, right)\n
quick_sort.cpp/* \u5feb\u901f\u6392\u5e8f */\nvoid quickSort(vector<int> &nums, int left, int right) {\n // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n if (left >= right)\n return;\n // \u54e8\u5175\u5212\u5206\n int pivot = partition(nums, left, right);\n // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\n quickSort(nums, left, pivot - 1);\n quickSort(nums, pivot + 1, right);\n}\n
quick_sort.java/* \u5feb\u901f\u6392\u5e8f */\nvoid quickSort(int[] nums, int left, int right) {\n // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n if (left >= right)\n return;\n // \u54e8\u5175\u5212\u5206\n int pivot = partition(nums, left, right);\n // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\n quickSort(nums, left, pivot - 1);\n quickSort(nums, pivot + 1, right);\n}\n
quick_sort.cs/* \u5feb\u901f\u6392\u5e8f */\nvoid QuickSort(int[] nums, int left, int right) {\n // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n if (left >= right)\n return;\n // \u54e8\u5175\u5212\u5206\n int pivot = Partition(nums, left, right);\n // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\n QuickSort(nums, left, pivot - 1);\n QuickSort(nums, pivot + 1, right);\n}\n
quick_sort.go/* \u5feb\u901f\u6392\u5e8f */\nfunc (q *quickSort) quickSort(nums []int, left, right int) {\n // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n if left >= right {\n return\n }\n // \u54e8\u5175\u5212\u5206\n pivot := q.partition(nums, left, right)\n // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\n q.quickSort(nums, left, pivot-1)\n q.quickSort(nums, pivot+1, right)\n}\n
quick_sort.swift/* \u5feb\u901f\u6392\u5e8f */\nfunc quickSort(nums: inout [Int], left: Int, right: Int) {\n // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n if left >= right {\n return\n }\n // \u54e8\u5175\u5212\u5206\n let pivot = partition(nums: &nums, left: left, right: right)\n // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\n quickSort(nums: &nums, left: left, right: pivot - 1)\n quickSort(nums: &nums, left: pivot + 1, right: right)\n}\n
quick_sort.js/* \u5feb\u901f\u6392\u5e8f */\nquickSort(nums, left, right) {\n // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n if (left >= right) return;\n // \u54e8\u5175\u5212\u5206\n const pivot = this.partition(nums, left, right);\n // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\n this.quickSort(nums, left, pivot - 1);\n this.quickSort(nums, pivot + 1, right);\n}\n
quick_sort.ts/* \u5feb\u901f\u6392\u5e8f */\nquickSort(nums: number[], left: number, right: number): void {\n // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n if (left >= right) {\n return;\n }\n // \u54e8\u5175\u5212\u5206\n const pivot = this.partition(nums, left, right);\n // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\n this.quickSort(nums, left, pivot - 1);\n this.quickSort(nums, pivot + 1, right);\n}\n
quick_sort.dart/* \u5feb\u901f\u6392\u5e8f */\nvoid quickSort(List<int> nums, int left, int right) {\n // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n if (left >= right) return;\n // \u54e8\u5175\u5212\u5206\n int pivot = _partition(nums, left, right);\n // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\n quickSort(nums, left, pivot - 1);\n quickSort(nums, pivot + 1, right);\n}\n
quick_sort.rs/* \u5feb\u901f\u6392\u5e8f */\npub fn quick_sort(left: i32, right: i32, nums: &mut [i32]) {\n // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n if left >= right {\n return;\n }\n // \u54e8\u5175\u5212\u5206\n let pivot = Self::partition(nums, left as usize, right as usize) as i32;\n // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\n Self::quick_sort(left, pivot - 1, nums);\n Self::quick_sort(pivot + 1, right, nums);\n}\n
quick_sort.c/* \u5feb\u901f\u6392\u5e8f\u7c7b */\n// \u5feb\u901f\u6392\u5e8f\u7c7b-\u54e8\u5175\u5212\u5206\nint partition(int nums[], int left, int right) {\n // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n int i = left, j = right;\n while (i < j) {\n while (i < j && nums[j] >= nums[left]) {\n // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n j--;\n }\n while (i < j && nums[i] <= nums[left]) {\n // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n i++;\n }\n // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n swap(nums, i, j);\n }\n // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n swap(nums, i, left);\n // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n return i;\n}\n\n// \u5feb\u901f\u6392\u5e8f\u7c7b-\u5feb\u901f\u6392\u5e8f\nvoid quickSort(int nums[], int left, int right) {\n // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n if (left >= right) {\n return;\n }\n // \u54e8\u5175\u5212\u5206\n int pivot = partition(nums, left, right);\n // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\n quickSort(nums, left, pivot - 1);\n quickSort(nums, pivot + 1, right);\n}\n
quick_sort.zig// \u5feb\u901f\u6392\u5e8f\nfn quickSort(nums: []i32, left: usize, right: usize) void {\n // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n if (left >= right) return;\n // \u54e8\u5175\u5212\u5206\n var pivot = partition(nums, left, right);\n // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\n quickSort(nums, left, pivot - 1);\n quickSort(nums, pivot + 1, right);\n}\n
"},{"location":"chapter_sorting/quick_sort/#1152","title":"11.5.2 \u00a0 \u7b97\u6cd5\u7279\u6027","text":"\u4ece\u540d\u79f0\u4e0a\u5c31\u80fd\u770b\u51fa\uff0c\u5feb\u901f\u6392\u5e8f\u5728\u6548\u7387\u65b9\u9762\u5e94\u8be5\u5177\u6709\u4e00\u5b9a\u7684\u4f18\u52bf\u3002\u5c3d\u7ba1\u5feb\u901f\u6392\u5e8f\u7684\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u4e0e\u201c\u5f52\u5e76\u6392\u5e8f\u201d\u548c\u201c\u5806\u6392\u5e8f\u201d\u76f8\u540c\uff0c\u4f46\u901a\u5e38\u5feb\u901f\u6392\u5e8f\u7684\u6548\u7387\u66f4\u9ad8\uff0c\u4e3b\u8981\u6709\u4ee5\u4e0b\u539f\u56e0\u3002
\u5feb\u901f\u6392\u5e8f\u5728\u67d0\u4e9b\u8f93\u5165\u4e0b\u7684\u65f6\u95f4\u6548\u7387\u53ef\u80fd\u964d\u4f4e\u3002\u4e3e\u4e00\u4e2a\u6781\u7aef\u4f8b\u5b50\uff0c\u5047\u8bbe\u8f93\u5165\u6570\u7ec4\u662f\u5b8c\u5168\u5012\u5e8f\u7684\uff0c\u7531\u4e8e\u6211\u4eec\u9009\u62e9\u6700\u5de6\u7aef\u5143\u7d20\u4f5c\u4e3a\u57fa\u51c6\u6570\uff0c\u90a3\u4e48\u5728\u54e8\u5175\u5212\u5206\u5b8c\u6210\u540e\uff0c\u57fa\u51c6\u6570\u88ab\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u53f3\u7aef\uff0c\u5bfc\u81f4\u5de6\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a \\(n - 1\\)\u3001\u53f3\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a \\(0\\) \u3002\u5982\u6b64\u9012\u5f52\u4e0b\u53bb\uff0c\u6bcf\u8f6e\u54e8\u5175\u5212\u5206\u540e\u7684\u53f3\u5b50\u6570\u7ec4\u957f\u5ea6\u90fd\u4e3a \\(0\\) \uff0c\u5206\u6cbb\u7b56\u7565\u5931\u6548\uff0c\u5feb\u901f\u6392\u5e8f\u9000\u5316\u4e3a\u201c\u5192\u6ce1\u6392\u5e8f\u201d\u3002
\u4e3a\u4e86\u5c3d\u91cf\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\u53d1\u751f\uff0c\u6211\u4eec\u53ef\u4ee5\u4f18\u5316\u54e8\u5175\u5212\u5206\u4e2d\u7684\u57fa\u51c6\u6570\u7684\u9009\u53d6\u7b56\u7565\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u968f\u673a\u9009\u53d6\u4e00\u4e2a\u5143\u7d20\u4f5c\u4e3a\u57fa\u51c6\u6570\u3002\u7136\u800c\uff0c\u5982\u679c\u8fd0\u6c14\u4e0d\u4f73\uff0c\u6bcf\u6b21\u90fd\u9009\u5230\u4e0d\u7406\u60f3\u7684\u57fa\u51c6\u6570\uff0c\u6548\u7387\u4ecd\u7136\u4e0d\u5c3d\u5982\u4eba\u610f\u3002
\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u7f16\u7a0b\u8bed\u8a00\u901a\u5e38\u751f\u6210\u7684\u662f\u201c\u4f2a\u968f\u673a\u6570\u201d\u3002\u5982\u679c\u6211\u4eec\u9488\u5bf9\u4f2a\u968f\u673a\u6570\u5e8f\u5217\u6784\u5efa\u4e00\u4e2a\u7279\u5b9a\u7684\u6d4b\u8bd5\u6837\u4f8b\uff0c\u90a3\u4e48\u5feb\u901f\u6392\u5e8f\u7684\u6548\u7387\u4ecd\u7136\u53ef\u80fd\u52a3\u5316\u3002
\u4e3a\u4e86\u8fdb\u4e00\u6b65\u6539\u8fdb\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u6570\u7ec4\u4e2d\u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\uff08\u901a\u5e38\u4e3a\u6570\u7ec4\u7684\u9996\u3001\u5c3e\u3001\u4e2d\u70b9\u5143\u7d20\uff09\uff0c\u5e76\u5c06\u8fd9\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570\u4f5c\u4e3a\u57fa\u51c6\u6570\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u57fa\u51c6\u6570\u201c\u65e2\u4e0d\u592a\u5c0f\u4e5f\u4e0d\u592a\u5927\u201d\u7684\u6982\u7387\u5c06\u5927\u5e45\u63d0\u5347\u3002\u5f53\u7136\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u9009\u53d6\u66f4\u591a\u5019\u9009\u5143\u7d20\uff0c\u4ee5\u8fdb\u4e00\u6b65\u63d0\u9ad8\u7b97\u6cd5\u7684\u7a33\u5065\u6027\u3002\u91c7\u7528\u8fd9\u79cd\u65b9\u6cd5\u540e\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u52a3\u5316\u81f3 \\(O(n^2)\\) \u7684\u6982\u7387\u5927\u5927\u964d\u4f4e\u3002
\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig quick_sort.pydef median_three(self, nums: list[int], left: int, mid: int, right: int) -> int:\n \"\"\"\u9009\u53d6\u4e09\u4e2a\u5143\u7d20\u7684\u4e2d\u4f4d\u6570\"\"\"\n # \u6b64\u5904\u4f7f\u7528\u5f02\u6216\u8fd0\u7b97\u6765\u7b80\u5316\u4ee3\u7801\n # \u5f02\u6216\u89c4\u5219\u4e3a 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1\n if (nums[left] < nums[mid]) ^ (nums[left] < nums[right]):\n return left\n elif (nums[mid] < nums[left]) ^ (nums[mid] < nums[right]):\n return mid\n return right\n\ndef partition(self, nums: list[int], left: int, right: int) -> int:\n \"\"\"\u54e8\u5175\u5212\u5206\uff08\u4e09\u6570\u53d6\u4e2d\u503c\uff09\"\"\"\n # \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n med = self.median_three(nums, left, (left + right) // 2, right)\n # \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\n nums[left], nums[med] = nums[med], nums[left]\n # \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n i, j = left, right\n while i < j:\n while i < j and nums[j] >= nums[left]:\n j -= 1 # \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n while i < j and nums[i] <= nums[left]:\n i += 1 # \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n # \u5143\u7d20\u4ea4\u6362\n nums[i], nums[j] = nums[j], nums[i]\n # \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n nums[i], nums[left] = nums[left], nums[i]\n return i # \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n
quick_sort.cpp/* \u9009\u53d6\u4e09\u4e2a\u5143\u7d20\u7684\u4e2d\u4f4d\u6570 */\nint medianThree(vector<int> &nums, int left, int mid, int right) {\n // \u6b64\u5904\u4f7f\u7528\u5f02\u6216\u8fd0\u7b97\u6765\u7b80\u5316\u4ee3\u7801\n // \u5f02\u6216\u89c4\u5219\u4e3a 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1\n if ((nums[left] < nums[mid]) ^ (nums[left] < nums[right]))\n return left;\n else if ((nums[mid] < nums[left]) ^ (nums[mid] < nums[right]))\n return mid;\n else\n return right;\n}\n\n/* \u54e8\u5175\u5212\u5206\uff08\u4e09\u6570\u53d6\u4e2d\u503c\uff09 */\nint partition(vector<int> &nums, int left, int right) {\n // \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570\n int med = medianThree(nums, left, (left + right) / 2, right);\n // \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\n swap(nums, left, med);\n // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n int i = left, j = right;\n while (i < j) {\n while (i < j && nums[j] >= nums[left])\n j--; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n while (i < j && nums[i] <= nums[left])\n i++; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n swap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n }\n swap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n return i; // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
quick_sort.java/* \u9009\u53d6\u4e09\u4e2a\u5143\u7d20\u7684\u4e2d\u4f4d\u6570 */\nint medianThree(int[] nums, int left, int mid, int right) {\n // \u6b64\u5904\u4f7f\u7528\u5f02\u6216\u8fd0\u7b97\u6765\u7b80\u5316\u4ee3\u7801\n // \u5f02\u6216\u89c4\u5219\u4e3a 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1\n if ((nums[left] < nums[mid]) ^ (nums[left] < nums[right]))\n return left;\n else if ((nums[mid] < nums[left]) ^ (nums[mid] < nums[right]))\n return mid;\n else\n return right;\n}\n\n/* \u54e8\u5175\u5212\u5206\uff08\u4e09\u6570\u53d6\u4e2d\u503c\uff09 */\nint partition(int[] nums, int left, int right) {\n // \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570\n int med = medianThree(nums, left, (left + right) / 2, right);\n // \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\n swap(nums, left, med);\n // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n int i = left, j = right;\n while (i < j) {\n while (i < j && nums[j] >= nums[left])\n j--; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n while (i < j && nums[i] <= nums[left])\n i++; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n swap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n }\n swap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n return i; // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
quick_sort.cs/* \u9009\u53d6\u4e09\u4e2a\u5143\u7d20\u7684\u4e2d\u4f4d\u6570 */\nint MedianThree(int[] nums, int left, int mid, int right) {\n // \u6b64\u5904\u4f7f\u7528\u5f02\u6216\u8fd0\u7b97\u6765\u7b80\u5316\u4ee3\u7801\n // \u5f02\u6216\u89c4\u5219\u4e3a 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1\n if ((nums[left] < nums[mid]) ^ (nums[left] < nums[right]))\n return left;\n else if ((nums[mid] < nums[left]) ^ (nums[mid] < nums[right]))\n return mid;\n else\n return right;\n}\n\n/* \u54e8\u5175\u5212\u5206\uff08\u4e09\u6570\u53d6\u4e2d\u503c\uff09 */\nint Partition(int[] nums, int left, int right) {\n // \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570\n int med = MedianThree(nums, left, (left + right) / 2, right);\n // \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\n Swap(nums, left, med);\n // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n int i = left, j = right;\n while (i < j) {\n while (i < j && nums[j] >= nums[left])\n j--; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n while (i < j && nums[i] <= nums[left])\n i++; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n Swap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n }\n Swap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n return i; // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
quick_sort.go/* \u9009\u53d6\u4e09\u4e2a\u5143\u7d20\u7684\u4e2d\u4f4d\u6570 */\nfunc (q *quickSortMedian) medianThree(nums []int, left, mid, right int) int {\n // \u6b64\u5904\u4f7f\u7528\u5f02\u6216\u8fd0\u7b97\u6765\u7b80\u5316\u4ee3\u7801\uff08!= \u5728\u8fd9\u91cc\u8d77\u5230\u5f02\u6216\u7684\u4f5c\u7528\uff09\n // \u5f02\u6216\u89c4\u5219\u4e3a 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1\n if (nums[left] < nums[mid]) != (nums[left] < nums[right]) {\n return left\n } else if (nums[mid] < nums[left]) != (nums[mid] < nums[right]) {\n return mid\n }\n return right\n}\n\n/* \u54e8\u5175\u5212\u5206\uff08\u4e09\u6570\u53d6\u4e2d\u503c\uff09*/\nfunc (q *quickSortMedian) partition(nums []int, left, right int) int {\n // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n med := q.medianThree(nums, left, (left+right)/2, right)\n // \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\n nums[left], nums[med] = nums[med], nums[left]\n // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n i, j := left, right\n for i < j {\n for i < j && nums[j] >= nums[left] {\n j-- //\u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n }\n for i < j && nums[i] <= nums[left] {\n i++ //\u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n }\n //\u5143\u7d20\u4ea4\u6362\n nums[i], nums[j] = nums[j], nums[i]\n }\n //\u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n nums[i], nums[left] = nums[left], nums[i]\n return i //\u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
quick_sort.swift/* \u9009\u53d6\u4e09\u4e2a\u5143\u7d20\u7684\u4e2d\u4f4d\u6570 */\nfunc medianThree(nums: [Int], left: Int, mid: Int, right: Int) -> Int {\n if (nums[left] < nums[mid]) != (nums[left] < nums[right]) {\n return left\n } else if (nums[mid] < nums[left]) != (nums[mid] < nums[right]) {\n return mid\n } else {\n return right\n }\n}\n\n/* \u54e8\u5175\u5212\u5206\uff08\u4e09\u6570\u53d6\u4e2d\u503c\uff09 */\nfunc partitionMedian(nums: inout [Int], left: Int, right: Int) -> Int {\n // \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570\n let med = medianThree(nums: nums, left: left, mid: (left + right) / 2, right: right)\n // \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\n swap(nums: &nums, i: left, j: med)\n return partition(nums: &nums, left: left, right: right)\n}\n
quick_sort.js/* \u9009\u53d6\u4e09\u4e2a\u5143\u7d20\u7684\u4e2d\u4f4d\u6570 */\nmedianThree(nums, left, mid, right) {\n // \u6b64\u5904\u4f7f\u7528\u5f02\u6216\u8fd0\u7b97\u6765\u7b80\u5316\u4ee3\u7801\n // \u5f02\u6216\u89c4\u5219\u4e3a 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1\n if ((nums[left] < nums[mid]) ^ (nums[left] < nums[right])) return left;\n else if ((nums[mid] < nums[left]) ^ (nums[mid] < nums[right]))\n return mid;\n else return right;\n}\n\n/* \u54e8\u5175\u5212\u5206\uff08\u4e09\u6570\u53d6\u4e2d\u503c\uff09 */\npartition(nums, left, right) {\n // \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570\n let med = this.medianThree(\n nums,\n left,\n Math.floor((left + right) / 2),\n right\n );\n // \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\n this.swap(nums, left, med);\n // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n let i = left,\n j = right;\n while (i < j) {\n while (i < j && nums[j] >= nums[left]) j--; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n while (i < j && nums[i] <= nums[left]) i++; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n this.swap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n }\n this.swap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n return i; // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
quick_sort.ts/* \u9009\u53d6\u4e09\u4e2a\u5143\u7d20\u7684\u4e2d\u4f4d\u6570 */\nmedianThree(\n nums: number[],\n left: number,\n mid: number,\n right: number\n): number {\n // \u6b64\u5904\u4f7f\u7528\u5f02\u6216\u8fd0\u7b97\u6765\u7b80\u5316\u4ee3\u7801\n // \u5f02\u6216\u89c4\u5219\u4e3a 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1\n if (Number(nums[left] < nums[mid]) ^ Number(nums[left] < nums[right])) {\n return left;\n } else if (\n Number(nums[mid] < nums[left]) ^ Number(nums[mid] < nums[right])\n ) {\n return mid;\n } else {\n return right;\n }\n}\n\n/* \u54e8\u5175\u5212\u5206\uff08\u4e09\u6570\u53d6\u4e2d\u503c\uff09 */\npartition(nums: number[], left: number, right: number): number {\n // \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570\n let med = this.medianThree(\n nums,\n left,\n Math.floor((left + right) / 2),\n right\n );\n // \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\n this.swap(nums, left, med);\n // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n let i = left,\n j = right;\n while (i < j) {\n while (i < j && nums[j] >= nums[left]) {\n j--; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n }\n while (i < j && nums[i] <= nums[left]) {\n i++; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n }\n this.swap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n }\n this.swap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n return i; // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
quick_sort.dart/* \u9009\u53d6\u4e09\u4e2a\u5143\u7d20\u7684\u4e2d\u4f4d\u6570 */\nint _medianThree(List<int> nums, int left, int mid, int right) {\n // \u6b64\u5904\u4f7f\u7528\u5f02\u6216\u8fd0\u7b97\u6765\u7b80\u5316\u4ee3\u7801\n // \u5f02\u6216\u89c4\u5219\u4e3a 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1\n if ((nums[left] < nums[mid]) ^ (nums[left] < nums[right]))\n return left;\n else if ((nums[mid] < nums[left]) ^ (nums[mid] < nums[right]))\n return mid;\n else\n return right;\n}\n\n/* \u54e8\u5175\u5212\u5206\uff08\u4e09\u6570\u53d6\u4e2d\u503c\uff09 */\nint _partition(List<int> nums, int left, int right) {\n // \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570\n int med = _medianThree(nums, left, (left + right) ~/ 2, right);\n // \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\n _swap(nums, left, med);\n // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n int i = left, j = right;\n while (i < j) {\n while (i < j && nums[j] >= nums[left]) j--; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n while (i < j && nums[i] <= nums[left]) i++; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n _swap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n }\n _swap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n return i; // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
quick_sort.rs/* \u9009\u53d6\u4e09\u4e2a\u5143\u7d20\u7684\u4e2d\u4f4d\u6570 */\nfn median_three(nums: &mut [i32], left: usize, mid: usize, right: usize) -> usize {\n // \u6b64\u5904\u4f7f\u7528\u5f02\u6216\u8fd0\u7b97\u6765\u7b80\u5316\u4ee3\u7801\n // \u5f02\u6216\u89c4\u5219\u4e3a 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1\n if (nums[left] < nums[mid]) ^ (nums[left] < nums[right]) {\n return left;\n } else if (nums[mid] < nums[left]) ^ (nums[mid] < nums[right]) {\n return mid;\n } \n right\n}\n\n/* \u54e8\u5175\u5212\u5206\uff08\u4e09\u6570\u53d6\u4e2d\u503c\uff09 */\nfn partition(nums: &mut [i32], left: usize, right: usize) -> usize {\n // \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570\n let med = Self::median_three(nums, left, (left + right) / 2, right);\n // \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\n nums.swap(left, med);\n // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n let (mut i, mut j) = (left, right);\n while i < j {\n while i < j && nums[j] >= nums[left] {\n j -= 1; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n }\n while i < j && nums[i] <= nums[left] {\n i += 1; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n }\n nums.swap(i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n }\n nums.swap(i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n i // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
quick_sort.c/* \u5feb\u901f\u6392\u5e8f\u7c7b\uff08\u4e2d\u4f4d\u57fa\u51c6\u6570\u4f18\u5316\uff09 */\n// \u9009\u53d6\u4e09\u4e2a\u5143\u7d20\u7684\u4e2d\u4f4d\u6570\nint medianThree(int nums[], int left, int mid, int right) {\n // \u6b64\u5904\u4f7f\u7528\u5f02\u6216\u8fd0\u7b97\u6765\u7b80\u5316\u4ee3\u7801\n // \u5f02\u6216\u89c4\u5219\u4e3a 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1\n if ((nums[left] < nums[mid]) ^ (nums[left] < nums[right]))\n return left;\n else if ((nums[mid] < nums[left]) ^ (nums[mid] < nums[right]))\n return mid;\n else\n return right;\n}\n\n/* \u54e8\u5175\u5212\u5206\uff08\u4e09\u6570\u53d6\u4e2d\u503c\uff09 */ \nint partitionMedian(int nums[], int left, int right) {\n // \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570\n int med = medianThree(nums, left, (left + right) / 2, right);\n // \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\n swap(nums, left, med);\n // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n int i = left, j = right;\n while (i < j) {\n while (i < j && nums[j] >= nums[left])\n j--; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n while (i < j && nums[i] <= nums[left])\n i++; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n swap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n }\n swap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n return i; // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
quick_sort.zig// \u9009\u53d6\u4e09\u4e2a\u5143\u7d20\u7684\u4e2d\u4f4d\u6570\nfn medianThree(nums: []i32, left: usize, mid: usize, right: usize) usize {\n // \u6b64\u5904\u4f7f\u7528\u5f02\u6216\u8fd0\u7b97\u6765\u7b80\u5316\u4ee3\u7801\n // \u5f02\u6216\u89c4\u5219\u4e3a 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1\n if ((nums[left] < nums[mid]) != (nums[left] < nums[right])) {\n return left;\n } else if ((nums[mid] < nums[left]) != (nums[mid] < nums[right])) {\n return mid;\n } else {\n return right;\n }\n}\n\n// \u54e8\u5175\u5212\u5206\uff08\u4e09\u6570\u53d6\u4e2d\u503c\uff09\nfn partition(nums: []i32, left: usize, right: usize) usize {\n // \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570\n var med = medianThree(nums, left, (left + right) / 2, right);\n // \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\n swap(nums, left, med);\n // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n var i = left;\n var j = right;\n while (i < j) {\n while (i < j and nums[j] >= nums[left]) j -= 1; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n while (i < j and nums[i] <= nums[left]) i += 1; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n swap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n }\n swap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n return i; // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
"},{"location":"chapter_sorting/quick_sort/#1155","title":"11.5.5 \u00a0 \u5c3e\u9012\u5f52\u4f18\u5316","text":"\u5728\u67d0\u4e9b\u8f93\u5165\u4e0b\uff0c\u5feb\u901f\u6392\u5e8f\u53ef\u80fd\u5360\u7528\u7a7a\u95f4\u8f83\u591a\u3002\u4ee5\u5b8c\u5168\u6709\u5e8f\u7684\u8f93\u5165\u6570\u7ec4\u4e3a\u4f8b\uff0c\u8bbe\u9012\u5f52\u4e2d\u7684\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a \\(m\\) \uff0c\u6bcf\u8f6e\u54e8\u5175\u5212\u5206\u64cd\u4f5c\u90fd\u5c06\u4ea7\u751f\u957f\u5ea6\u4e3a \\(0\\) \u7684\u5de6\u5b50\u6570\u7ec4\u548c\u957f\u5ea6\u4e3a \\(m - 1\\) \u7684\u53f3\u5b50\u6570\u7ec4\uff0c\u8fd9\u610f\u5473\u7740\u6bcf\u4e00\u5c42\u9012\u5f52\u8c03\u7528\u51cf\u5c11\u7684\u95ee\u9898\u89c4\u6a21\u975e\u5e38\u5c0f\uff08\u53ea\u51cf\u5c11\u4e00\u4e2a\u5143\u7d20\uff09\uff0c\u9012\u5f52\u6811\u7684\u9ad8\u5ea6\u4f1a\u8fbe\u5230 \\(n - 1\\) \uff0c\u6b64\u65f6\u9700\u8981\u5360\u7528 \\(O(n)\\) \u5927\u5c0f\u7684\u6808\u5e27\u7a7a\u95f4\u3002
\u4e3a\u4e86\u9632\u6b62\u6808\u5e27\u7a7a\u95f4\u7684\u7d2f\u79ef\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u6bcf\u8f6e\u54e8\u5175\u6392\u5e8f\u5b8c\u6210\u540e\uff0c\u6bd4\u8f83\u4e24\u4e2a\u5b50\u6570\u7ec4\u7684\u957f\u5ea6\uff0c\u4ec5\u5bf9\u8f83\u77ed\u7684\u5b50\u6570\u7ec4\u8fdb\u884c\u9012\u5f52\u3002\u7531\u4e8e\u8f83\u77ed\u5b50\u6570\u7ec4\u7684\u957f\u5ea6\u4e0d\u4f1a\u8d85\u8fc7 \\(n / 2\\) \uff0c\u56e0\u6b64\u8fd9\u79cd\u65b9\u6cd5\u80fd\u786e\u4fdd\u9012\u5f52\u6df1\u5ea6\u4e0d\u8d85\u8fc7 \\(\\log n\\) \uff0c\u4ece\u800c\u5c06\u6700\u5dee\u7a7a\u95f4\u590d\u6742\u5ea6\u4f18\u5316\u81f3 \\(O(\\log n)\\) \u3002\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig quick_sort.pydef quick_sort(self, nums: list[int], left: int, right: int):\n \"\"\"\u5feb\u901f\u6392\u5e8f\uff08\u5c3e\u9012\u5f52\u4f18\u5316\uff09\"\"\"\n # \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\n while left < right:\n # \u54e8\u5175\u5212\u5206\u64cd\u4f5c\n pivot = self.partition(nums, left, right)\n # \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u901f\u6392\u5e8f\n if pivot - left < right - pivot:\n self.quick_sort(nums, left, pivot - 1) # \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\n left = pivot + 1 # \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n else:\n self.quick_sort(nums, pivot + 1, right) # \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\n right = pivot - 1 # \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [left, pivot - 1]\n
quick_sort.cpp/* \u5feb\u901f\u6392\u5e8f\uff08\u5c3e\u9012\u5f52\u4f18\u5316\uff09 */\nvoid quickSort(vector<int> &nums, int left, int right) {\n // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\n while (left < right) {\n // \u54e8\u5175\u5212\u5206\u64cd\u4f5c\n int pivot = partition(nums, left, right);\n // \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u901f\u6392\u5e8f\n if (pivot - left < right - pivot) {\n quickSort(nums, left, pivot - 1); // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\n left = pivot + 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n } else {\n quickSort(nums, pivot + 1, right); // \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\n right = pivot - 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [left, pivot - 1]\n }\n }\n}\n
quick_sort.java/* \u5feb\u901f\u6392\u5e8f\uff08\u5c3e\u9012\u5f52\u4f18\u5316\uff09 */\nvoid quickSort(int[] nums, int left, int right) {\n // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\n while (left < right) {\n // \u54e8\u5175\u5212\u5206\u64cd\u4f5c\n int pivot = partition(nums, left, right);\n // \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u901f\u6392\u5e8f\n if (pivot - left < right - pivot) {\n quickSort(nums, left, pivot - 1); // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\n left = pivot + 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n } else {\n quickSort(nums, pivot + 1, right); // \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\n right = pivot - 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [left, pivot - 1]\n }\n }\n}\n
quick_sort.cs/* \u5feb\u901f\u6392\u5e8f\uff08\u5c3e\u9012\u5f52\u4f18\u5316\uff09 */\nvoid QuickSort(int[] nums, int left, int right) {\n // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\n while (left < right) {\n // \u54e8\u5175\u5212\u5206\u64cd\u4f5c\n int pivot = Partition(nums, left, right);\n // \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u901f\u6392\u5e8f\n if (pivot - left < right - pivot) {\n QuickSort(nums, left, pivot - 1); // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\n left = pivot + 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n } else {\n QuickSort(nums, pivot + 1, right); // \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\n right = pivot - 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [left, pivot - 1]\n }\n }\n}\n
quick_sort.go/* \u5feb\u901f\u6392\u5e8f\uff08\u5c3e\u9012\u5f52\u4f18\u5316\uff09*/\nfunc (q *quickSortTailCall) quickSort(nums []int, left, right int) {\n // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\n for left < right {\n // \u54e8\u5175\u5212\u5206\u64cd\u4f5c\n pivot := q.partition(nums, left, right)\n // \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u901f\u6392\u5e8f\n if pivot-left < right-pivot {\n q.quickSort(nums, left, pivot-1) // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\n left = pivot + 1 // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n } else {\n q.quickSort(nums, pivot+1, right) // \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\n right = pivot - 1 // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [left, pivot - 1]\n }\n }\n}\n
quick_sort.swift/* \u5feb\u901f\u6392\u5e8f\uff08\u5c3e\u9012\u5f52\u4f18\u5316\uff09 */\nfunc quickSortTailCall(nums: inout [Int], left: Int, right: Int) {\n var left = left\n var right = right\n // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\n while left < right {\n // \u54e8\u5175\u5212\u5206\u64cd\u4f5c\n let pivot = partition(nums: &nums, left: left, right: right)\n // \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u901f\u6392\u5e8f\n if (pivot - left) < (right - pivot) {\n quickSortTailCall(nums: &nums, left: left, right: pivot - 1) // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\n left = pivot + 1 // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n } else {\n quickSortTailCall(nums: &nums, left: pivot + 1, right: right) // \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\n right = pivot - 1 // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [left, pivot - 1]\n }\n }\n}\n
quick_sort.js/* \u5feb\u901f\u6392\u5e8f\uff08\u5c3e\u9012\u5f52\u4f18\u5316\uff09 */\nquickSort(nums, left, right) {\n // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\n while (left < right) {\n // \u54e8\u5175\u5212\u5206\u64cd\u4f5c\n let pivot = this.partition(nums, left, right);\n // \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u901f\u6392\u5e8f\n if (pivot - left < right - pivot) {\n this.quickSort(nums, left, pivot - 1); // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\n left = pivot + 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n } else {\n this.quickSort(nums, pivot + 1, right); // \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\n right = pivot - 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [left, pivot - 1]\n }\n }\n}\n
quick_sort.ts/* \u5feb\u901f\u6392\u5e8f\uff08\u5c3e\u9012\u5f52\u4f18\u5316\uff09 */\nquickSort(nums: number[], left: number, right: number): void {\n // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\n while (left < right) {\n // \u54e8\u5175\u5212\u5206\u64cd\u4f5c\n let pivot = this.partition(nums, left, right);\n // \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u901f\u6392\u5e8f\n if (pivot - left < right - pivot) {\n this.quickSort(nums, left, pivot - 1); // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\n left = pivot + 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n } else {\n this.quickSort(nums, pivot + 1, right); // \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\n right = pivot - 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [left, pivot - 1]\n }\n }\n}\n
quick_sort.dart/* \u5feb\u901f\u6392\u5e8f\uff08\u5c3e\u9012\u5f52\u4f18\u5316\uff09 */\nvoid quickSort(List<int> nums, int left, int right) {\n // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\n while (left < right) {\n // \u54e8\u5175\u5212\u5206\u64cd\u4f5c\n int pivot = _partition(nums, left, right);\n // \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u901f\u6392\u5e8f\n if (pivot - left < right - pivot) {\n quickSort(nums, left, pivot - 1); // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\n left = pivot + 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n } else {\n quickSort(nums, pivot + 1, right); // \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\n right = pivot - 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [left, pivot - 1]\n }\n }\n}\n
quick_sort.rs/* \u5feb\u901f\u6392\u5e8f\uff08\u5c3e\u9012\u5f52\u4f18\u5316\uff09 */\npub fn quick_sort(mut left: i32, mut right: i32, nums: &mut [i32]) {\n // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\n while left < right {\n // \u54e8\u5175\u5212\u5206\u64cd\u4f5c\n let pivot = Self::partition(nums, left as usize, right as usize) as i32;\n // \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u901f\u6392\u5e8f\n if pivot - left < right - pivot {\n Self::quick_sort(left, pivot - 1, nums); // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\n left = pivot + 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n } else {\n Self::quick_sort(pivot + 1, right, nums); // \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\n right = pivot - 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [left, pivot - 1]\n }\n }\n}\n
quick_sort.c/* \u5feb\u901f\u6392\u5e8f\u7c7b\uff08\u5c3e\u9012\u5f52\u4f18\u5316\uff09 */\n// \u5feb\u901f\u6392\u5e8f\uff08\u5c3e\u9012\u5f52\u4f18\u5316\uff09\nvoid quickSortTailCall(int nums[], int left, int right) {\n // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\n while (left < right) {\n // \u54e8\u5175\u5212\u5206\u64cd\u4f5c\n int pivot = partition(nums, left, right);\n // \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u901f\u6392\u5e8f\n if (pivot - left < right - pivot) {\n quickSortTailCall(nums, left, pivot - 1); // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\n left = pivot + 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n } else {\n quickSortTailCall(nums, pivot + 1, right); // \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\n right = pivot - 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [left, pivot - 1]\n }\n }\n}\n
quick_sort.zig// \u5feb\u901f\u6392\u5e8f\uff08\u5c3e\u9012\u5f52\u4f18\u5316\uff09\nfn quickSort(nums: []i32, left_: usize, right_: usize) void {\n var left = left_;\n var right = right_;\n // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n while (left < right) {\n // \u54e8\u5175\u5212\u5206\u64cd\u4f5c\n var pivot = partition(nums, left, right);\n // \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u901f\u6392\u5e8f\n if (pivot - left < right - pivot) {\n quickSort(nums, left, pivot - 1); // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\n left = pivot + 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n } else {\n quickSort(nums, pivot + 1, right); // \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\n right = pivot - 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [left, pivot - 1]\n }\n }\n}\n
"},{"location":"chapter_sorting/radix_sort/","title":"11.10 \u00a0 \u57fa\u6570\u6392\u5e8f","text":"\u4e0a\u4e00\u8282\u4ecb\u7ecd\u4e86\u8ba1\u6570\u6392\u5e8f\uff0c\u5b83\u9002\u7528\u4e8e\u6570\u636e\u91cf \\(n\\) \u8f83\u5927\u4f46\u6570\u636e\u8303\u56f4 \\(m\\) \u8f83\u5c0f\u7684\u60c5\u51b5\u3002\u5047\u8bbe\u6211\u4eec\u9700\u8981\u5bf9 \\(n = 10^6\\) \u4e2a\u5b66\u53f7\u8fdb\u884c\u6392\u5e8f\uff0c\u800c\u5b66\u53f7\u662f\u4e00\u4e2a \\(8\\) \u4f4d\u6570\u5b57\uff0c\u8fd9\u610f\u5473\u7740\u6570\u636e\u8303\u56f4 \\(m = 10^8\\) \u975e\u5e38\u5927\uff0c\u4f7f\u7528\u8ba1\u6570\u6392\u5e8f\u9700\u8981\u5206\u914d\u5927\u91cf\u5185\u5b58\u7a7a\u95f4\uff0c\u800c\u57fa\u6570\u6392\u5e8f\u53ef\u4ee5\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\u3002
\u300c\u57fa\u6570\u6392\u5e8f radix sort\u300d\u7684\u6838\u5fc3\u601d\u60f3\u4e0e\u8ba1\u6570\u6392\u5e8f\u4e00\u81f4\uff0c\u4e5f\u901a\u8fc7\u7edf\u8ba1\u4e2a\u6570\u6765\u5b9e\u73b0\u6392\u5e8f\u3002\u5728\u6b64\u57fa\u7840\u4e0a\uff0c\u57fa\u6570\u6392\u5e8f\u5229\u7528\u6570\u5b57\u5404\u4f4d\u4e4b\u95f4\u7684\u9012\u8fdb\u5173\u7cfb\uff0c\u4f9d\u6b21\u5bf9\u6bcf\u4e00\u4f4d\u8fdb\u884c\u6392\u5e8f\uff0c\u4ece\u800c\u5f97\u5230\u6700\u7ec8\u7684\u6392\u5e8f\u7ed3\u679c\u3002
"},{"location":"chapter_sorting/radix_sort/#11101","title":"11.10.1 \u00a0 \u7b97\u6cd5\u6d41\u7a0b","text":"\u4ee5\u5b66\u53f7\u6570\u636e\u4e3a\u4f8b\uff0c\u5047\u8bbe\u6570\u5b57\u7684\u6700\u4f4e\u4f4d\u662f\u7b2c \\(1\\) \u4f4d\uff0c\u6700\u9ad8\u4f4d\u662f\u7b2c \\(8\\) \u4f4d\uff0c\u57fa\u6570\u6392\u5e8f\u7684\u6d41\u7a0b\u5982\u56fe 11-18 \u6240\u793a\u3002
2.
\u7ee7\u7eed\u8fed\u4ee3\uff0c\u76f4\u5230\u6240\u6709\u4f4d\u90fd\u6392\u5e8f\u5b8c\u6210\u540e\u7ed3\u675f\u3002\u56fe 11-18 \u00a0 \u57fa\u6570\u6392\u5e8f\u7b97\u6cd5\u6d41\u7a0b
\u4e0b\u9762\u5256\u6790\u4ee3\u7801\u5b9e\u73b0\u3002\u5bf9\u4e8e\u4e00\u4e2a \\(d\\) \u8fdb\u5236\u7684\u6570\u5b57 \\(x\\) \uff0c\u8981\u83b7\u53d6\u5176\u7b2c \\(k\\) \u4f4d \\(x_k\\) \uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u8ba1\u7b97\u516c\u5f0f\uff1a
\\[ x_k = \\lfloor\\frac{x}{d^{k-1}}\\rfloor \\bmod d \\]\u5176\u4e2d \\(\\lfloor a \\rfloor\\) \u8868\u793a\u5bf9\u6d6e\u70b9\u6570 \\(a\\) \u5411\u4e0b\u53d6\u6574\uff0c\u800c \\(\\bmod \\: d\\) \u8868\u793a\u5bf9 \\(d\\) \u53d6\u4f59\u3002\u5bf9\u4e8e\u5b66\u53f7\u6570\u636e\uff0c\\(d = 10\\) \u4e14 \\(k \\in [1, 8]\\) \u3002
\u6b64\u5916\uff0c\u6211\u4eec\u9700\u8981\u5c0f\u5e45\u6539\u52a8\u8ba1\u6570\u6392\u5e8f\u4ee3\u7801\uff0c\u4f7f\u4e4b\u53ef\u4ee5\u6839\u636e\u6570\u5b57\u7684\u7b2c \\(k\\) \u4f4d\u8fdb\u884c\u6392\u5e8f\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig radix_sort.pydef digit(num: int, exp: int) -> int:\n \"\"\"\u83b7\u53d6\u5143\u7d20 num \u7684\u7b2c k \u4f4d\uff0c\u5176\u4e2d exp = 10^(k-1)\"\"\"\n # \u4f20\u5165 exp \u800c\u975e k \u53ef\u4ee5\u907f\u514d\u5728\u6b64\u91cd\u590d\u6267\u884c\u6602\u8d35\u7684\u6b21\u65b9\u8ba1\u7b97\n return (num // exp) % 10\n\ndef counting_sort_digit(nums: list[int], exp: int):\n \"\"\"\u8ba1\u6570\u6392\u5e8f\uff08\u6839\u636e nums \u7b2c k \u4f4d\u6392\u5e8f\uff09\"\"\"\n # \u5341\u8fdb\u5236\u7684\u4f4d\u8303\u56f4\u4e3a 0~9 \uff0c\u56e0\u6b64\u9700\u8981\u957f\u5ea6\u4e3a 10 \u7684\u6876\u6570\u7ec4\n counter = [0] * 10\n n = len(nums)\n # \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n for i in range(n):\n d = digit(nums[i], exp) # \u83b7\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8bb0\u4e3a d\n counter[d] += 1 # \u7edf\u8ba1\u6570\u5b57 d \u7684\u51fa\u73b0\u6b21\u6570\n # \u6c42\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u4e2a\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u6570\u7ec4\u7d22\u5f15\u201d\n for i in range(1, 10):\n counter[i] += counter[i - 1]\n # \u5012\u5e8f\u904d\u5386\uff0c\u6839\u636e\u6876\u5185\u7edf\u8ba1\u7ed3\u679c\uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165 res\n res = [0] * n\n for i in range(n - 1, -1, -1):\n d = digit(nums[i], exp)\n j = counter[d] - 1 # \u83b7\u53d6 d \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15 j\n res[j] = nums[i] # \u5c06\u5f53\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\n counter[d] -= 1 # \u5c06 d \u7684\u6570\u91cf\u51cf 1\n # \u4f7f\u7528\u7ed3\u679c\u8986\u76d6\u539f\u6570\u7ec4 nums\n for i in range(n):\n nums[i] = res[i]\n\ndef radix_sort(nums: list[int]):\n \"\"\"\u57fa\u6570\u6392\u5e8f\"\"\"\n # \u83b7\u53d6\u6570\u7ec4\u7684\u6700\u5927\u5143\u7d20\uff0c\u7528\u4e8e\u5224\u65ad\u6700\u5927\u4f4d\u6570\n m = max(nums)\n # \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\n exp = 1\n while exp <= m:\n # \u5bf9\u6570\u7ec4\u5143\u7d20\u7684\u7b2c k \u4f4d\u6267\u884c\u8ba1\u6570\u6392\u5e8f\n # k = 1 -> exp = 1\n # k = 2 -> exp = 10\n # \u5373 exp = 10^(k-1)\n counting_sort_digit(nums, exp)\n exp *= 10\n
radix_sort.cpp/* \u83b7\u53d6\u5143\u7d20 num \u7684\u7b2c k \u4f4d\uff0c\u5176\u4e2d exp = 10^(k-1) */\nint digit(int num, int exp) {\n // \u4f20\u5165 exp \u800c\u975e k \u53ef\u4ee5\u907f\u514d\u5728\u6b64\u91cd\u590d\u6267\u884c\u6602\u8d35\u7684\u6b21\u65b9\u8ba1\u7b97\n return (num / exp) % 10;\n}\n\n/* \u8ba1\u6570\u6392\u5e8f\uff08\u6839\u636e nums \u7b2c k \u4f4d\u6392\u5e8f\uff09 */\nvoid countingSortDigit(vector<int> &nums, int exp) {\n // \u5341\u8fdb\u5236\u7684\u4f4d\u8303\u56f4\u4e3a 0~9 \uff0c\u56e0\u6b64\u9700\u8981\u957f\u5ea6\u4e3a 10 \u7684\u6876\u6570\u7ec4\n vector<int> counter(10, 0);\n int n = nums.size();\n // \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n for (int i = 0; i < n; i++) {\n int d = digit(nums[i], exp); // \u83b7\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8bb0\u4e3a d\n counter[d]++; // \u7edf\u8ba1\u6570\u5b57 d \u7684\u51fa\u73b0\u6b21\u6570\n }\n // \u6c42\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u4e2a\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u6570\u7ec4\u7d22\u5f15\u201d\n for (int i = 1; i < 10; i++) {\n counter[i] += counter[i - 1];\n }\n // \u5012\u5e8f\u904d\u5386\uff0c\u6839\u636e\u6876\u5185\u7edf\u8ba1\u7ed3\u679c\uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165 res\n vector<int> res(n, 0);\n for (int i = n - 1; i >= 0; i--) {\n int d = digit(nums[i], exp);\n int j = counter[d] - 1; // \u83b7\u53d6 d \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15 j\n res[j] = nums[i]; // \u5c06\u5f53\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\n counter[d]--; // \u5c06 d \u7684\u6570\u91cf\u51cf 1\n }\n // \u4f7f\u7528\u7ed3\u679c\u8986\u76d6\u539f\u6570\u7ec4 nums\n for (int i = 0; i < n; i++)\n nums[i] = res[i];\n}\n\n/* \u57fa\u6570\u6392\u5e8f */\nvoid radixSort(vector<int> &nums) {\n // \u83b7\u53d6\u6570\u7ec4\u7684\u6700\u5927\u5143\u7d20\uff0c\u7528\u4e8e\u5224\u65ad\u6700\u5927\u4f4d\u6570\n int m = *max_element(nums.begin(), nums.end());\n // \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\n for (int exp = 1; exp <= m; exp *= 10)\n // \u5bf9\u6570\u7ec4\u5143\u7d20\u7684\u7b2c k \u4f4d\u6267\u884c\u8ba1\u6570\u6392\u5e8f\n // k = 1 -> exp = 1\n // k = 2 -> exp = 10\n // \u5373 exp = 10^(k-1)\n countingSortDigit(nums, exp);\n}\n
radix_sort.java/* \u83b7\u53d6\u5143\u7d20 num \u7684\u7b2c k \u4f4d\uff0c\u5176\u4e2d exp = 10^(k-1) */\nint digit(int num, int exp) {\n // \u4f20\u5165 exp \u800c\u975e k \u53ef\u4ee5\u907f\u514d\u5728\u6b64\u91cd\u590d\u6267\u884c\u6602\u8d35\u7684\u6b21\u65b9\u8ba1\u7b97\n return (num / exp) % 10;\n}\n\n/* \u8ba1\u6570\u6392\u5e8f\uff08\u6839\u636e nums \u7b2c k \u4f4d\u6392\u5e8f\uff09 */\nvoid countingSortDigit(int[] nums, int exp) {\n // \u5341\u8fdb\u5236\u7684\u4f4d\u8303\u56f4\u4e3a 0~9 \uff0c\u56e0\u6b64\u9700\u8981\u957f\u5ea6\u4e3a 10 \u7684\u6876\u6570\u7ec4\n int[] counter = new int[10];\n int n = nums.length;\n // \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n for (int i = 0; i < n; i++) {\n int d = digit(nums[i], exp); // \u83b7\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8bb0\u4e3a d\n counter[d]++; // \u7edf\u8ba1\u6570\u5b57 d \u7684\u51fa\u73b0\u6b21\u6570\n }\n // \u6c42\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u4e2a\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u6570\u7ec4\u7d22\u5f15\u201d\n for (int i = 1; i < 10; i++) {\n counter[i] += counter[i - 1];\n }\n // \u5012\u5e8f\u904d\u5386\uff0c\u6839\u636e\u6876\u5185\u7edf\u8ba1\u7ed3\u679c\uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165 res\n int[] res = new int[n];\n for (int i = n - 1; i >= 0; i--) {\n int d = digit(nums[i], exp);\n int j = counter[d] - 1; // \u83b7\u53d6 d \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15 j\n res[j] = nums[i]; // \u5c06\u5f53\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\n counter[d]--; // \u5c06 d \u7684\u6570\u91cf\u51cf 1\n }\n // \u4f7f\u7528\u7ed3\u679c\u8986\u76d6\u539f\u6570\u7ec4 nums\n for (int i = 0; i < n; i++)\n nums[i] = res[i];\n}\n\n/* \u57fa\u6570\u6392\u5e8f */\nvoid radixSort(int[] nums) {\n // \u83b7\u53d6\u6570\u7ec4\u7684\u6700\u5927\u5143\u7d20\uff0c\u7528\u4e8e\u5224\u65ad\u6700\u5927\u4f4d\u6570\n int m = Integer.MIN_VALUE;\n for (int num : nums)\n if (num > m)\n m = num;\n // \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\n for (int exp = 1; exp <= m; exp *= 10)\n // \u5bf9\u6570\u7ec4\u5143\u7d20\u7684\u7b2c k \u4f4d\u6267\u884c\u8ba1\u6570\u6392\u5e8f\n // k = 1 -> exp = 1\n // k = 2 -> exp = 10\n // \u5373 exp = 10^(k-1)\n countingSortDigit(nums, exp);\n}\n
radix_sort.cs/* \u83b7\u53d6\u5143\u7d20 num \u7684\u7b2c k \u4f4d\uff0c\u5176\u4e2d exp = 10^(k-1) */\nint Digit(int num, int exp) {\n // \u4f20\u5165 exp \u800c\u975e k \u53ef\u4ee5\u907f\u514d\u5728\u6b64\u91cd\u590d\u6267\u884c\u6602\u8d35\u7684\u6b21\u65b9\u8ba1\u7b97\n return (num / exp) % 10;\n}\n\n/* \u8ba1\u6570\u6392\u5e8f\uff08\u6839\u636e nums \u7b2c k \u4f4d\u6392\u5e8f\uff09 */\nvoid CountingSortDigit(int[] nums, int exp) {\n // \u5341\u8fdb\u5236\u7684\u4f4d\u8303\u56f4\u4e3a 0~9 \uff0c\u56e0\u6b64\u9700\u8981\u957f\u5ea6\u4e3a 10 \u7684\u6876\u6570\u7ec4\n int[] counter = new int[10];\n int n = nums.Length;\n // \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n for (int i = 0; i < n; i++) {\n int d = Digit(nums[i], exp); // \u83b7\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8bb0\u4e3a d\n counter[d]++; // \u7edf\u8ba1\u6570\u5b57 d \u7684\u51fa\u73b0\u6b21\u6570\n }\n // \u6c42\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u4e2a\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u6570\u7ec4\u7d22\u5f15\u201d\n for (int i = 1; i < 10; i++) {\n counter[i] += counter[i - 1];\n }\n // \u5012\u5e8f\u904d\u5386\uff0c\u6839\u636e\u6876\u5185\u7edf\u8ba1\u7ed3\u679c\uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165 res\n int[] res = new int[n];\n for (int i = n - 1; i >= 0; i--) {\n int d = Digit(nums[i], exp);\n int j = counter[d] - 1; // \u83b7\u53d6 d \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15 j\n res[j] = nums[i]; // \u5c06\u5f53\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\n counter[d]--; // \u5c06 d \u7684\u6570\u91cf\u51cf 1\n }\n // \u4f7f\u7528\u7ed3\u679c\u8986\u76d6\u539f\u6570\u7ec4 nums\n for (int i = 0; i < n; i++) {\n nums[i] = res[i];\n }\n}\n\n/* \u57fa\u6570\u6392\u5e8f */\nvoid RadixSort(int[] nums) {\n // \u83b7\u53d6\u6570\u7ec4\u7684\u6700\u5927\u5143\u7d20\uff0c\u7528\u4e8e\u5224\u65ad\u6700\u5927\u4f4d\u6570\n int m = int.MinValue;\n foreach (int num in nums) {\n if (num > m) m = num;\n }\n // \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\n for (int exp = 1; exp <= m; exp *= 10) {\n // \u5bf9\u6570\u7ec4\u5143\u7d20\u7684\u7b2c k \u4f4d\u6267\u884c\u8ba1\u6570\u6392\u5e8f\n // k = 1 -> exp = 1\n // k = 2 -> exp = 10\n // \u5373 exp = 10^(k-1)\n CountingSortDigit(nums, exp);\n }\n}\n
radix_sort.go/* \u83b7\u53d6\u5143\u7d20 num \u7684\u7b2c k \u4f4d\uff0c\u5176\u4e2d exp = 10^(k-1) */\nfunc digit(num, exp int) int {\n // \u4f20\u5165 exp \u800c\u975e k \u53ef\u4ee5\u907f\u514d\u5728\u6b64\u91cd\u590d\u6267\u884c\u6602\u8d35\u7684\u6b21\u65b9\u8ba1\u7b97\n return (num / exp) % 10\n}\n\n/* \u8ba1\u6570\u6392\u5e8f\uff08\u6839\u636e nums \u7b2c k \u4f4d\u6392\u5e8f\uff09 */\nfunc countingSortDigit(nums []int, exp int) {\n // \u5341\u8fdb\u5236\u7684\u4f4d\u8303\u56f4\u4e3a 0~9 \uff0c\u56e0\u6b64\u9700\u8981\u957f\u5ea6\u4e3a 10 \u7684\u6876\u6570\u7ec4\n counter := make([]int, 10)\n n := len(nums)\n // \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n for i := 0; i < n; i++ {\n d := digit(nums[i], exp) // \u83b7\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8bb0\u4e3a d\n counter[d]++ // \u7edf\u8ba1\u6570\u5b57 d \u7684\u51fa\u73b0\u6b21\u6570\n }\n // \u6c42\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u4e2a\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u6570\u7ec4\u7d22\u5f15\u201d\n for i := 1; i < 10; i++ {\n counter[i] += counter[i-1]\n }\n // \u5012\u5e8f\u904d\u5386\uff0c\u6839\u636e\u6876\u5185\u7edf\u8ba1\u7ed3\u679c\uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165 res\n res := make([]int, n)\n for i := n - 1; i >= 0; i-- {\n d := digit(nums[i], exp)\n j := counter[d] - 1 // \u83b7\u53d6 d \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15 j\n res[j] = nums[i] // \u5c06\u5f53\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\n counter[d]-- // \u5c06 d \u7684\u6570\u91cf\u51cf 1\n }\n // \u4f7f\u7528\u7ed3\u679c\u8986\u76d6\u539f\u6570\u7ec4 nums\n for i := 0; i < n; i++ {\n nums[i] = res[i]\n }\n}\n\n/* \u57fa\u6570\u6392\u5e8f */\nfunc radixSort(nums []int) {\n // \u83b7\u53d6\u6570\u7ec4\u7684\u6700\u5927\u5143\u7d20\uff0c\u7528\u4e8e\u5224\u65ad\u6700\u5927\u4f4d\u6570\n max := math.MinInt\n for _, num := range nums {\n if num > max {\n max = num\n }\n }\n // \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\n for exp := 1; max >= exp; exp *= 10 {\n // \u5bf9\u6570\u7ec4\u5143\u7d20\u7684\u7b2c k \u4f4d\u6267\u884c\u8ba1\u6570\u6392\u5e8f\n // k = 1 -> exp = 1\n // k = 2 -> exp = 10\n // \u5373 exp = 10^(k-1)\n countingSortDigit(nums, exp)\n }\n}\n
radix_sort.swift/* \u83b7\u53d6\u5143\u7d20 num \u7684\u7b2c k \u4f4d\uff0c\u5176\u4e2d exp = 10^(k-1) */\nfunc digit(num: Int, exp: Int) -> Int {\n // \u4f20\u5165 exp \u800c\u975e k \u53ef\u4ee5\u907f\u514d\u5728\u6b64\u91cd\u590d\u6267\u884c\u6602\u8d35\u7684\u6b21\u65b9\u8ba1\u7b97\n (num / exp) % 10\n}\n\n/* \u8ba1\u6570\u6392\u5e8f\uff08\u6839\u636e nums \u7b2c k \u4f4d\u6392\u5e8f\uff09 */\nfunc countingSortDigit(nums: inout [Int], exp: Int) {\n // \u5341\u8fdb\u5236\u7684\u4f4d\u8303\u56f4\u4e3a 0~9 \uff0c\u56e0\u6b64\u9700\u8981\u957f\u5ea6\u4e3a 10 \u7684\u6876\u6570\u7ec4\n var counter = Array(repeating: 0, count: 10)\n let n = nums.count\n // \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n for i in nums.indices {\n let d = digit(num: nums[i], exp: exp) // \u83b7\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8bb0\u4e3a d\n counter[d] += 1 // \u7edf\u8ba1\u6570\u5b57 d \u7684\u51fa\u73b0\u6b21\u6570\n }\n // \u6c42\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u4e2a\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u6570\u7ec4\u7d22\u5f15\u201d\n for i in 1 ..< 10 {\n counter[i] += counter[i - 1]\n }\n // \u5012\u5e8f\u904d\u5386\uff0c\u6839\u636e\u6876\u5185\u7edf\u8ba1\u7ed3\u679c\uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165 res\n var res = Array(repeating: 0, count: n)\n for i in stride(from: n - 1, through: 0, by: -1) {\n let d = digit(num: nums[i], exp: exp)\n let j = counter[d] - 1 // \u83b7\u53d6 d \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15 j\n res[j] = nums[i] // \u5c06\u5f53\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\n counter[d] -= 1 // \u5c06 d \u7684\u6570\u91cf\u51cf 1\n }\n // \u4f7f\u7528\u7ed3\u679c\u8986\u76d6\u539f\u6570\u7ec4 nums\n for i in nums.indices {\n nums[i] = res[i]\n }\n}\n\n/* \u57fa\u6570\u6392\u5e8f */\nfunc radixSort(nums: inout [Int]) {\n // \u83b7\u53d6\u6570\u7ec4\u7684\u6700\u5927\u5143\u7d20\uff0c\u7528\u4e8e\u5224\u65ad\u6700\u5927\u4f4d\u6570\n var m = Int.min\n for num in nums {\n if num > m {\n m = num\n }\n }\n // \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\n for exp in sequence(first: 1, next: { m >= ($0 * 10) ? $0 * 10 : nil }) {\n // \u5bf9\u6570\u7ec4\u5143\u7d20\u7684\u7b2c k \u4f4d\u6267\u884c\u8ba1\u6570\u6392\u5e8f\n // k = 1 -> exp = 1\n // k = 2 -> exp = 10\n // \u5373 exp = 10^(k-1)\n countingSortDigit(nums: &nums, exp: exp)\n }\n}\n
radix_sort.js/* \u83b7\u53d6\u5143\u7d20 num \u7684\u7b2c k \u4f4d\uff0c\u5176\u4e2d exp = 10^(k-1) */\nfunction digit(num, exp) {\n // \u4f20\u5165 exp \u800c\u975e k \u53ef\u4ee5\u907f\u514d\u5728\u6b64\u91cd\u590d\u6267\u884c\u6602\u8d35\u7684\u6b21\u65b9\u8ba1\u7b97\n return Math.floor(num / exp) % 10;\n}\n\n/* \u8ba1\u6570\u6392\u5e8f\uff08\u6839\u636e nums \u7b2c k \u4f4d\u6392\u5e8f\uff09 */\nfunction countingSortDigit(nums, exp) {\n // \u5341\u8fdb\u5236\u7684\u4f4d\u8303\u56f4\u4e3a 0~9 \uff0c\u56e0\u6b64\u9700\u8981\u957f\u5ea6\u4e3a 10 \u7684\u6876\u6570\u7ec4\n const counter = new Array(10).fill(0);\n const n = nums.length;\n // \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n for (let i = 0; i < n; i++) {\n const d = digit(nums[i], exp); // \u83b7\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8bb0\u4e3a d\n counter[d]++; // \u7edf\u8ba1\u6570\u5b57 d \u7684\u51fa\u73b0\u6b21\u6570\n }\n // \u6c42\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u4e2a\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u6570\u7ec4\u7d22\u5f15\u201d\n for (let i = 1; i < 10; i++) {\n counter[i] += counter[i - 1];\n }\n // \u5012\u5e8f\u904d\u5386\uff0c\u6839\u636e\u6876\u5185\u7edf\u8ba1\u7ed3\u679c\uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165 res\n const res = new Array(n).fill(0);\n for (let i = n - 1; i >= 0; i--) {\n const d = digit(nums[i], exp);\n const j = counter[d] - 1; // \u83b7\u53d6 d \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15 j\n res[j] = nums[i]; // \u5c06\u5f53\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\n counter[d]--; // \u5c06 d \u7684\u6570\u91cf\u51cf 1\n }\n // \u4f7f\u7528\u7ed3\u679c\u8986\u76d6\u539f\u6570\u7ec4 nums\n for (let i = 0; i < n; i++) {\n nums[i] = res[i];\n }\n}\n\n/* \u57fa\u6570\u6392\u5e8f */\nfunction radixSort(nums) {\n // \u83b7\u53d6\u6570\u7ec4\u7684\u6700\u5927\u5143\u7d20\uff0c\u7528\u4e8e\u5224\u65ad\u6700\u5927\u4f4d\u6570\n let m = Number.MIN_VALUE;\n for (const num of nums) {\n if (num > m) {\n m = num;\n }\n }\n // \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\n for (let exp = 1; exp <= m; exp *= 10) {\n // \u5bf9\u6570\u7ec4\u5143\u7d20\u7684\u7b2c k \u4f4d\u6267\u884c\u8ba1\u6570\u6392\u5e8f\n // k = 1 -> exp = 1\n // k = 2 -> exp = 10\n // \u5373 exp = 10^(k-1)\n countingSortDigit(nums, exp);\n }\n}\n
radix_sort.ts/* \u83b7\u53d6\u5143\u7d20 num \u7684\u7b2c k \u4f4d\uff0c\u5176\u4e2d exp = 10^(k-1) */\nfunction digit(num: number, exp: number): number {\n // \u4f20\u5165 exp \u800c\u975e k \u53ef\u4ee5\u907f\u514d\u5728\u6b64\u91cd\u590d\u6267\u884c\u6602\u8d35\u7684\u6b21\u65b9\u8ba1\u7b97\n return Math.floor(num / exp) % 10;\n}\n\n/* \u8ba1\u6570\u6392\u5e8f\uff08\u6839\u636e nums \u7b2c k \u4f4d\u6392\u5e8f\uff09 */\nfunction countingSortDigit(nums: number[], exp: number): void {\n // \u5341\u8fdb\u5236\u7684\u4f4d\u8303\u56f4\u4e3a 0~9 \uff0c\u56e0\u6b64\u9700\u8981\u957f\u5ea6\u4e3a 10 \u7684\u6876\u6570\u7ec4\n const counter = new Array(10).fill(0);\n const n = nums.length;\n // \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n for (let i = 0; i < n; i++) {\n const d = digit(nums[i], exp); // \u83b7\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8bb0\u4e3a d\n counter[d]++; // \u7edf\u8ba1\u6570\u5b57 d \u7684\u51fa\u73b0\u6b21\u6570\n }\n // \u6c42\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u4e2a\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u6570\u7ec4\u7d22\u5f15\u201d\n for (let i = 1; i < 10; i++) {\n counter[i] += counter[i - 1];\n }\n // \u5012\u5e8f\u904d\u5386\uff0c\u6839\u636e\u6876\u5185\u7edf\u8ba1\u7ed3\u679c\uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165 res\n const res = new Array(n).fill(0);\n for (let i = n - 1; i >= 0; i--) {\n const d = digit(nums[i], exp);\n const j = counter[d] - 1; // \u83b7\u53d6 d \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15 j\n res[j] = nums[i]; // \u5c06\u5f53\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\n counter[d]--; // \u5c06 d \u7684\u6570\u91cf\u51cf 1\n }\n // \u4f7f\u7528\u7ed3\u679c\u8986\u76d6\u539f\u6570\u7ec4 nums\n for (let i = 0; i < n; i++) {\n nums[i] = res[i];\n }\n}\n\n/* \u57fa\u6570\u6392\u5e8f */\nfunction radixSort(nums: number[]): void {\n // \u83b7\u53d6\u6570\u7ec4\u7684\u6700\u5927\u5143\u7d20\uff0c\u7528\u4e8e\u5224\u65ad\u6700\u5927\u4f4d\u6570\n let m = Number.MIN_VALUE;\n for (const num of nums) {\n if (num > m) {\n m = num;\n }\n }\n // \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\n for (let exp = 1; exp <= m; exp *= 10) {\n // \u5bf9\u6570\u7ec4\u5143\u7d20\u7684\u7b2c k \u4f4d\u6267\u884c\u8ba1\u6570\u6392\u5e8f\n // k = 1 -> exp = 1\n // k = 2 -> exp = 10\n // \u5373 exp = 10^(k-1)\n countingSortDigit(nums, exp);\n }\n}\n
radix_sort.dart/* \u83b7\u53d6\u5143\u7d20 _num \u7684\u7b2c k \u4f4d\uff0c\u5176\u4e2d exp = 10^(k-1) */\nint digit(int _num, int exp) {\n // \u4f20\u5165 exp \u800c\u975e k \u53ef\u4ee5\u907f\u514d\u5728\u6b64\u91cd\u590d\u6267\u884c\u6602\u8d35\u7684\u6b21\u65b9\u8ba1\u7b97\n return (_num ~/ exp) % 10;\n}\n\n/* \u8ba1\u6570\u6392\u5e8f\uff08\u6839\u636e nums \u7b2c k \u4f4d\u6392\u5e8f\uff09 */\nvoid countingSortDigit(List<int> nums, int exp) {\n // \u5341\u8fdb\u5236\u7684\u4f4d\u8303\u56f4\u4e3a 0~9 \uff0c\u56e0\u6b64\u9700\u8981\u957f\u5ea6\u4e3a 10 \u7684\u6876\u6570\u7ec4\n List<int> counter = List<int>.filled(10, 0);\n int n = nums.length;\n // \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n for (int i = 0; i < n; i++) {\n int d = digit(nums[i], exp); // \u83b7\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8bb0\u4e3a d\n counter[d]++; // \u7edf\u8ba1\u6570\u5b57 d \u7684\u51fa\u73b0\u6b21\u6570\n }\n // \u6c42\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u4e2a\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u6570\u7ec4\u7d22\u5f15\u201d\n for (int i = 1; i < 10; i++) {\n counter[i] += counter[i - 1];\n }\n // \u5012\u5e8f\u904d\u5386\uff0c\u6839\u636e\u6876\u5185\u7edf\u8ba1\u7ed3\u679c\uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165 res\n List<int> res = List<int>.filled(n, 0);\n for (int i = n - 1; i >= 0; i--) {\n int d = digit(nums[i], exp);\n int j = counter[d] - 1; // \u83b7\u53d6 d \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15 j\n res[j] = nums[i]; // \u5c06\u5f53\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\n counter[d]--; // \u5c06 d \u7684\u6570\u91cf\u51cf 1\n }\n // \u4f7f\u7528\u7ed3\u679c\u8986\u76d6\u539f\u6570\u7ec4 nums\n for (int i = 0; i < n; i++) nums[i] = res[i];\n}\n\n/* \u57fa\u6570\u6392\u5e8f */\nvoid radixSort(List<int> nums) {\n // \u83b7\u53d6\u6570\u7ec4\u7684\u6700\u5927\u5143\u7d20\uff0c\u7528\u4e8e\u5224\u65ad\u6700\u5927\u4f4d\u6570\n // dart \u4e2d int \u7684\u957f\u5ea6\u662f 64 \u4f4d\u7684\n int m = -1 << 63;\n for (int _num in nums) if (_num > m) m = _num;\n // \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\n for (int exp = 1; exp <= m; exp *= 10)\n // \u5bf9\u6570\u7ec4\u5143\u7d20\u7684\u7b2c k \u4f4d\u6267\u884c\u8ba1\u6570\u6392\u5e8f\n // k = 1 -> exp = 1\n // k = 2 -> exp = 10\n // \u5373 exp = 10^(k-1)\n countingSortDigit(nums, exp);\n}\n
radix_sort.rs/* \u83b7\u53d6\u5143\u7d20 num \u7684\u7b2c k \u4f4d\uff0c\u5176\u4e2d exp = 10^(k-1) */\nfn digit(num: i32, exp: i32) -> usize {\n // \u4f20\u5165 exp \u800c\u975e k \u53ef\u4ee5\u907f\u514d\u5728\u6b64\u91cd\u590d\u6267\u884c\u6602\u8d35\u7684\u6b21\u65b9\u8ba1\u7b97\n return ((num / exp) % 10) as usize;\n}\n\n/* \u8ba1\u6570\u6392\u5e8f\uff08\u6839\u636e nums \u7b2c k \u4f4d\u6392\u5e8f\uff09 */\nfn counting_sort_digit(nums: &mut [i32], exp: i32) {\n // \u5341\u8fdb\u5236\u7684\u4f4d\u8303\u56f4\u4e3a 0~9 \uff0c\u56e0\u6b64\u9700\u8981\u957f\u5ea6\u4e3a 10 \u7684\u6876\u6570\u7ec4\n let mut counter = [0; 10];\n let n = nums.len();\n // \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n for i in 0..n {\n let d = digit(nums[i], exp); // \u83b7\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8bb0\u4e3a d\n counter[d] += 1; // \u7edf\u8ba1\u6570\u5b57 d \u7684\u51fa\u73b0\u6b21\u6570\n }\n // \u6c42\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u4e2a\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u6570\u7ec4\u7d22\u5f15\u201d\n for i in 1..10 {\n counter[i] += counter[i - 1];\n }\n // \u5012\u5e8f\u904d\u5386\uff0c\u6839\u636e\u6876\u5185\u7edf\u8ba1\u7ed3\u679c\uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165 res\n let mut res = vec![0; n];\n for i in (0..n).rev() {\n let d = digit(nums[i], exp);\n let j = counter[d] - 1; // \u83b7\u53d6 d \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15 j\n res[j] = nums[i]; // \u5c06\u5f53\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\n counter[d] -= 1; // \u5c06 d \u7684\u6570\u91cf\u51cf 1\n }\n // \u4f7f\u7528\u7ed3\u679c\u8986\u76d6\u539f\u6570\u7ec4 nums\n for i in 0..n {\n nums[i] = res[i];\n }\n}\n\n/* \u57fa\u6570\u6392\u5e8f */\nfn radix_sort(nums: &mut [i32]) {\n // \u83b7\u53d6\u6570\u7ec4\u7684\u6700\u5927\u5143\u7d20\uff0c\u7528\u4e8e\u5224\u65ad\u6700\u5927\u4f4d\u6570\n let m = *nums.into_iter().max().unwrap();\n // \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\n let mut exp = 1;\n while exp <= m {\n counting_sort_digit(nums, exp);\n exp *= 10;\n }\n}\n
radix_sort.c/* \u83b7\u53d6\u5143\u7d20 num \u7684\u7b2c k \u4f4d\uff0c\u5176\u4e2d exp = 10^(k-1) */\nint digit(int num, int exp) {\n // \u4f20\u5165 exp \u800c\u975e k \u53ef\u4ee5\u907f\u514d\u5728\u6b64\u91cd\u590d\u6267\u884c\u6602\u8d35\u7684\u6b21\u65b9\u8ba1\u7b97\n return (num / exp) % 10;\n}\n\n/* \u8ba1\u6570\u6392\u5e8f\uff08\u6839\u636e nums \u7b2c k \u4f4d\u6392\u5e8f\uff09 */\nvoid countingSortDigit(int nums[], int size, int exp) {\n // \u5341\u8fdb\u5236\u7684\u4f4d\u8303\u56f4\u4e3a 0~9 \uff0c\u56e0\u6b64\u9700\u8981\u957f\u5ea6\u4e3a 10 \u7684\u6876\u6570\u7ec4\n int *counter = (int *)malloc((sizeof(int) * 10));\n // \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n for (int i = 0; i < size; i++) {\n // \u83b7\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8bb0\u4e3a d\n int d = digit(nums[i], exp);\n // \u7edf\u8ba1\u6570\u5b57 d \u7684\u51fa\u73b0\u6b21\u6570\n counter[d]++;\n }\n // \u6c42\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u4e2a\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u6570\u7ec4\u7d22\u5f15\u201d\n for (int i = 1; i < 10; i++) {\n counter[i] += counter[i - 1];\n }\n // \u5012\u5e8f\u904d\u5386\uff0c\u6839\u636e\u6876\u5185\u7edf\u8ba1\u7ed3\u679c\uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165 res\n int *res = (int *)malloc(sizeof(int) * size);\n for (int i = size - 1; i >= 0; i--) {\n int d = digit(nums[i], exp);\n int j = counter[d] - 1; // \u83b7\u53d6 d \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15 j\n res[j] = nums[i]; // \u5c06\u5f53\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\n counter[d]--; // \u5c06 d \u7684\u6570\u91cf\u51cf 1\n }\n // \u4f7f\u7528\u7ed3\u679c\u8986\u76d6\u539f\u6570\u7ec4 nums\n for (int i = 0; i < size; i++) {\n nums[i] = res[i];\n }\n}\n\n/* \u57fa\u6570\u6392\u5e8f */\nvoid radixSort(int nums[], int size) {\n // \u83b7\u53d6\u6570\u7ec4\u7684\u6700\u5927\u5143\u7d20\uff0c\u7528\u4e8e\u5224\u65ad\u6700\u5927\u4f4d\u6570\n int max = INT32_MIN;\n for (size_t i = 0; i < size - 1; i++) {\n if (nums[i] > max) {\n max = nums[i];\n }\n }\n // \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\n for (int exp = 1; max >= exp; exp *= 10)\n // \u5bf9\u6570\u7ec4\u5143\u7d20\u7684\u7b2c k \u4f4d\u6267\u884c\u8ba1\u6570\u6392\u5e8f\n // k = 1 -> exp = 1\n // k = 2 -> exp = 10\n // \u5373 exp = 10^(k-1)\n countingSortDigit(nums, size, exp);\n}\n
radix_sort.zig// \u83b7\u53d6\u5143\u7d20 num \u7684\u7b2c k \u4f4d\uff0c\u5176\u4e2d exp = 10^(k-1)\nfn digit(num: i32, exp: i32) i32 {\n // \u4f20\u5165 exp \u800c\u975e k \u53ef\u4ee5\u907f\u514d\u5728\u6b64\u91cd\u590d\u6267\u884c\u6602\u8d35\u7684\u6b21\u65b9\u8ba1\u7b97\n return @mod(@divFloor(num, exp), 10);\n}\n\n// \u8ba1\u6570\u6392\u5e8f\uff08\u6839\u636e nums \u7b2c k \u4f4d\u6392\u5e8f\uff09\nfn countingSortDigit(nums: []i32, exp: i32) !void {\n // \u5341\u8fdb\u5236\u7684\u4f4d\u8303\u56f4\u4e3a 0~9 \uff0c\u56e0\u6b64\u9700\u8981\u957f\u5ea6\u4e3a 10 \u7684\u6876\u6570\u7ec4\n var mem_arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);\n // defer mem_arena.deinit();\n const mem_allocator = mem_arena.allocator();\n var counter = try mem_allocator.alloc(usize, 10);\n @memset(counter, 0);\n var n = nums.len;\n // \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n for (nums) |num| {\n var d: u32 = @bitCast(digit(num, exp)); // \u83b7\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8bb0\u4e3a d\n counter[d] += 1; // \u7edf\u8ba1\u6570\u5b57 d \u7684\u51fa\u73b0\u6b21\u6570\n }\n // \u6c42\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u4e2a\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u6570\u7ec4\u7d22\u5f15\u201d\n var i: usize = 1;\n while (i < 10) : (i += 1) {\n counter[i] += counter[i - 1];\n }\n // \u5012\u5e8f\u904d\u5386\uff0c\u6839\u636e\u6876\u5185\u7edf\u8ba1\u7ed3\u679c\uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165 res\n var res = try mem_allocator.alloc(i32, n);\n i = n - 1;\n while (i >= 0) : (i -= 1) {\n var d: u32 = @bitCast(digit(nums[i], exp));\n var j = counter[d] - 1; // \u83b7\u53d6 d \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15 j\n res[j] = nums[i]; // \u5c06\u5f53\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\n counter[d] -= 1; // \u5c06 d \u7684\u6570\u91cf\u51cf 1\n if (i == 0) break;\n }\n // \u4f7f\u7528\u7ed3\u679c\u8986\u76d6\u539f\u6570\u7ec4 nums\n i = 0;\n while (i < n) : (i += 1) {\n nums[i] = res[i];\n }\n}\n\n// \u57fa\u6570\u6392\u5e8f\nfn radixSort(nums: []i32) !void {\n // \u83b7\u53d6\u6570\u7ec4\u7684\u6700\u5927\u5143\u7d20\uff0c\u7528\u4e8e\u5224\u65ad\u6700\u5927\u4f4d\u6570\n var m: i32 = std.math.minInt(i32);\n for (nums) |num| {\n if (num > m) m = num;\n }\n // \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\n var exp: i32 = 1;\n while (exp <= m) : (exp *= 10) {\n // \u5bf9\u6570\u7ec4\u5143\u7d20\u7684\u7b2c k \u4f4d\u6267\u884c\u8ba1\u6570\u6392\u5e8f\n // k = 1 -> exp = 1\n // k = 2 -> exp = 10\n // \u5373 exp = 10^(k-1)\n try countingSortDigit(nums, exp); \n }\n} \n
\u4e3a\u4ec0\u4e48\u4ece\u6700\u4f4e\u4f4d\u5f00\u59cb\u6392\u5e8f\uff1f
\u5728\u8fde\u7eed\u7684\u6392\u5e8f\u8f6e\u6b21\u4e2d\uff0c\u540e\u4e00\u8f6e\u6392\u5e8f\u4f1a\u8986\u76d6\u524d\u4e00\u8f6e\u6392\u5e8f\u7684\u7ed3\u679c\u3002\u4e3e\u4f8b\u6765\u8bf4\uff0c\u5982\u679c\u7b2c\u4e00\u8f6e\u6392\u5e8f\u7ed3\u679c \\(a < b\\) \uff0c\u800c\u7b2c\u4e8c\u8f6e\u6392\u5e8f\u7ed3\u679c \\(a > b\\) \uff0c\u90a3\u4e48\u7b2c\u4e8c\u8f6e\u7684\u7ed3\u679c\u5c06\u53d6\u4ee3\u7b2c\u4e00\u8f6e\u7684\u7ed3\u679c\u3002\u7531\u4e8e\u6570\u5b57\u7684\u9ad8\u4f4d\u4f18\u5148\u7ea7\u9ad8\u4e8e\u4f4e\u4f4d\uff0c\u56e0\u6b64\u5e94\u8be5\u5148\u6392\u5e8f\u4f4e\u4f4d\u518d\u6392\u5e8f\u9ad8\u4f4d\u3002
"},{"location":"chapter_sorting/radix_sort/#11102","title":"11.10.2 \u00a0 \u7b97\u6cd5\u7279\u6027","text":"\u76f8\u8f83\u4e8e\u8ba1\u6570\u6392\u5e8f\uff0c\u57fa\u6570\u6392\u5e8f\u9002\u7528\u4e8e\u6570\u503c\u8303\u56f4\u8f83\u5927\u7684\u60c5\u51b5\uff0c\u4f46\u524d\u63d0\u662f\u6570\u636e\u5fc5\u987b\u53ef\u4ee5\u8868\u793a\u4e3a\u56fa\u5b9a\u4f4d\u6570\u7684\u683c\u5f0f\uff0c\u4e14\u4f4d\u6570\u4e0d\u80fd\u8fc7\u5927\u3002\u4f8b\u5982\uff0c\u6d6e\u70b9\u6570\u4e0d\u9002\u5408\u4f7f\u7528\u57fa\u6570\u6392\u5e8f\uff0c\u56e0\u4e3a\u5176\u4f4d\u6570 \\(k\\) \u8fc7\u5927\uff0c\u53ef\u80fd\u5bfc\u81f4\u65f6\u95f4\u590d\u6742\u5ea6 \\(O(nk) \\gg O(n^2)\\) \u3002
res
\u548c counter
\u3002\u300c\u9009\u62e9\u6392\u5e8f selection sort\u300d\u7684\u5de5\u4f5c\u539f\u7406\u975e\u5e38\u7b80\u5355\uff1a\u5f00\u542f\u4e00\u4e2a\u5faa\u73af\uff0c\u6bcf\u8f6e\u4ece\u672a\u6392\u5e8f\u533a\u95f4\u9009\u62e9\u6700\u5c0f\u7684\u5143\u7d20\uff0c\u5c06\u5176\u653e\u5230\u5df2\u6392\u5e8f\u533a\u95f4\u7684\u672b\u5c3e\u3002
\u8bbe\u6570\u7ec4\u7684\u957f\u5ea6\u4e3a \\(n\\) \uff0c\u9009\u62e9\u6392\u5e8f\u7684\u7b97\u6cd5\u6d41\u7a0b\u5982\u56fe 11-2 \u6240\u793a\u3002
\u56fe 11-2 \u00a0 \u9009\u62e9\u6392\u5e8f\u6b65\u9aa4
\u5728\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u7528 \\(k\\) \u6765\u8bb0\u5f55\u672a\u6392\u5e8f\u533a\u95f4\u5185\u7684\u6700\u5c0f\u5143\u7d20\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig selection_sort.pydef selection_sort(nums: list[int]):\n \"\"\"\u9009\u62e9\u6392\u5e8f\"\"\"\n n = len(nums)\n # \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [i, n-1]\n for i in range(n - 1):\n # \u5185\u5faa\u73af\uff1a\u627e\u5230\u672a\u6392\u5e8f\u533a\u95f4\u5185\u7684\u6700\u5c0f\u5143\u7d20\n k = i\n for j in range(i + 1, n):\n if nums[j] < nums[k]:\n k = j # \u8bb0\u5f55\u6700\u5c0f\u5143\u7d20\u7684\u7d22\u5f15\n # \u5c06\u8be5\u6700\u5c0f\u5143\u7d20\u4e0e\u672a\u6392\u5e8f\u533a\u95f4\u7684\u9996\u4e2a\u5143\u7d20\u4ea4\u6362\n nums[i], nums[k] = nums[k], nums[i]\n
selection_sort.cpp/* \u9009\u62e9\u6392\u5e8f */\nvoid selectionSort(vector<int> &nums) {\n int n = nums.size();\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [i, n-1]\n for (int i = 0; i < n - 1; i++) {\n // \u5185\u5faa\u73af\uff1a\u627e\u5230\u672a\u6392\u5e8f\u533a\u95f4\u5185\u7684\u6700\u5c0f\u5143\u7d20\n int k = i;\n for (int j = i + 1; j < n; j++) {\n if (nums[j] < nums[k])\n k = j; // \u8bb0\u5f55\u6700\u5c0f\u5143\u7d20\u7684\u7d22\u5f15\n }\n // \u5c06\u8be5\u6700\u5c0f\u5143\u7d20\u4e0e\u672a\u6392\u5e8f\u533a\u95f4\u7684\u9996\u4e2a\u5143\u7d20\u4ea4\u6362\n swap(nums[i], nums[k]);\n }\n}\n
selection_sort.java/* \u9009\u62e9\u6392\u5e8f */\nvoid selectionSort(int[] nums) {\n int n = nums.length;\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [i, n-1]\n for (int i = 0; i < n - 1; i++) {\n // \u5185\u5faa\u73af\uff1a\u627e\u5230\u672a\u6392\u5e8f\u533a\u95f4\u5185\u7684\u6700\u5c0f\u5143\u7d20\n int k = i;\n for (int j = i + 1; j < n; j++) {\n if (nums[j] < nums[k])\n k = j; // \u8bb0\u5f55\u6700\u5c0f\u5143\u7d20\u7684\u7d22\u5f15\n }\n // \u5c06\u8be5\u6700\u5c0f\u5143\u7d20\u4e0e\u672a\u6392\u5e8f\u533a\u95f4\u7684\u9996\u4e2a\u5143\u7d20\u4ea4\u6362\n int temp = nums[i];\n nums[i] = nums[k];\n nums[k] = temp;\n }\n}\n
selection_sort.cs/* \u9009\u62e9\u6392\u5e8f */\nvoid SelectionSort(int[] nums) {\n int n = nums.Length;\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [i, n-1]\n for (int i = 0; i < n - 1; i++) {\n // \u5185\u5faa\u73af\uff1a\u627e\u5230\u672a\u6392\u5e8f\u533a\u95f4\u5185\u7684\u6700\u5c0f\u5143\u7d20\n int k = i;\n for (int j = i + 1; j < n; j++) {\n if (nums[j] < nums[k])\n k = j; // \u8bb0\u5f55\u6700\u5c0f\u5143\u7d20\u7684\u7d22\u5f15\n }\n // \u5c06\u8be5\u6700\u5c0f\u5143\u7d20\u4e0e\u672a\u6392\u5e8f\u533a\u95f4\u7684\u9996\u4e2a\u5143\u7d20\u4ea4\u6362\n (nums[k], nums[i]) = (nums[i], nums[k]);\n }\n}\n
selection_sort.go/* \u9009\u62e9\u6392\u5e8f */\nfunc selectionSort(nums []int) {\n n := len(nums)\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [i, n-1]\n for i := 0; i < n-1; i++ {\n // \u5185\u5faa\u73af\uff1a\u627e\u5230\u672a\u6392\u5e8f\u533a\u95f4\u5185\u7684\u6700\u5c0f\u5143\u7d20\n k := i\n for j := i + 1; j < n; j++ {\n if nums[j] < nums[k] {\n // \u8bb0\u5f55\u6700\u5c0f\u5143\u7d20\u7684\u7d22\u5f15\n k = j\n }\n }\n // \u5c06\u8be5\u6700\u5c0f\u5143\u7d20\u4e0e\u672a\u6392\u5e8f\u533a\u95f4\u7684\u9996\u4e2a\u5143\u7d20\u4ea4\u6362\n nums[i], nums[k] = nums[k], nums[i]\n\n }\n}\n
selection_sort.swift/* \u9009\u62e9\u6392\u5e8f */\nfunc selectionSort(nums: inout [Int]) {\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [i, n-1]\n for i in nums.indices.dropLast() {\n // \u5185\u5faa\u73af\uff1a\u627e\u5230\u672a\u6392\u5e8f\u533a\u95f4\u5185\u7684\u6700\u5c0f\u5143\u7d20\n var k = i\n for j in nums.indices.dropFirst(i + 1) {\n if nums[j] < nums[k] {\n k = j // \u8bb0\u5f55\u6700\u5c0f\u5143\u7d20\u7684\u7d22\u5f15\n }\n }\n // \u5c06\u8be5\u6700\u5c0f\u5143\u7d20\u4e0e\u672a\u6392\u5e8f\u533a\u95f4\u7684\u9996\u4e2a\u5143\u7d20\u4ea4\u6362\n nums.swapAt(i, k)\n }\n}\n
selection_sort.js/* \u9009\u62e9\u6392\u5e8f */\nfunction selectionSort(nums) {\n let n = nums.length;\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [i, n-1]\n for (let i = 0; i < n - 1; i++) {\n // \u5185\u5faa\u73af\uff1a\u627e\u5230\u672a\u6392\u5e8f\u533a\u95f4\u5185\u7684\u6700\u5c0f\u5143\u7d20\n let k = i;\n for (let j = i + 1; j < n; j++) {\n if (nums[j] < nums[k]) {\n k = j; // \u8bb0\u5f55\u6700\u5c0f\u5143\u7d20\u7684\u7d22\u5f15\n }\n }\n // \u5c06\u8be5\u6700\u5c0f\u5143\u7d20\u4e0e\u672a\u6392\u5e8f\u533a\u95f4\u7684\u9996\u4e2a\u5143\u7d20\u4ea4\u6362\n [nums[i], nums[k]] = [nums[k], nums[i]];\n }\n}\n
selection_sort.ts/* \u9009\u62e9\u6392\u5e8f */\nfunction selectionSort(nums: number[]): void {\n let n = nums.length;\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [i, n-1]\n for (let i = 0; i < n - 1; i++) {\n // \u5185\u5faa\u73af\uff1a\u627e\u5230\u672a\u6392\u5e8f\u533a\u95f4\u5185\u7684\u6700\u5c0f\u5143\u7d20\n let k = i;\n for (let j = i + 1; j < n; j++) {\n if (nums[j] < nums[k]) {\n k = j; // \u8bb0\u5f55\u6700\u5c0f\u5143\u7d20\u7684\u7d22\u5f15\n }\n }\n // \u5c06\u8be5\u6700\u5c0f\u5143\u7d20\u4e0e\u672a\u6392\u5e8f\u533a\u95f4\u7684\u9996\u4e2a\u5143\u7d20\u4ea4\u6362\n [nums[i], nums[k]] = [nums[k], nums[i]];\n }\n}\n
selection_sort.dart/* \u9009\u62e9\u6392\u5e8f */\nvoid selectionSort(List<int> nums) {\n int n = nums.length;\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [i, n-1]\n for (int i = 0; i < n - 1; i++) {\n // \u5185\u5faa\u73af\uff1a\u627e\u5230\u672a\u6392\u5e8f\u533a\u95f4\u5185\u7684\u6700\u5c0f\u5143\u7d20\n int k = i;\n for (int j = i + 1; j < n; j++) {\n if (nums[j] < nums[k]) k = j; // \u8bb0\u5f55\u6700\u5c0f\u5143\u7d20\u7684\u7d22\u5f15\n }\n // \u5c06\u8be5\u6700\u5c0f\u5143\u7d20\u4e0e\u672a\u6392\u5e8f\u533a\u95f4\u7684\u9996\u4e2a\u5143\u7d20\u4ea4\u6362\n int temp = nums[i];\n nums[i] = nums[k];\n nums[k] = temp;\n }\n}\n
selection_sort.rs/* \u9009\u62e9\u6392\u5e8f */\nfn selection_sort(nums: &mut [i32]) {\n let n = nums.len();\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [i, n-1]\n for i in 0..n-1 {\n // \u5185\u5faa\u73af\uff1a\u627e\u5230\u672a\u6392\u5e8f\u533a\u95f4\u5185\u7684\u6700\u5c0f\u5143\u7d20\n let mut k = i;\n for j in i+1..n {\n if nums[j] < nums[k] {\n k = j; // \u8bb0\u5f55\u6700\u5c0f\u5143\u7d20\u7684\u7d22\u5f15\n }\n }\n // \u5c06\u8be5\u6700\u5c0f\u5143\u7d20\u4e0e\u672a\u6392\u5e8f\u533a\u95f4\u7684\u9996\u4e2a\u5143\u7d20\u4ea4\u6362\n nums.swap(i, k);\n }\n}\n
selection_sort.c/* \u9009\u62e9\u6392\u5e8f */\nvoid selectionSort(int nums[], int n) {\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [i, n-1]\n for (int i = 0; i < n - 1; i++) {\n // \u5185\u5faa\u73af\uff1a\u627e\u5230\u672a\u6392\u5e8f\u533a\u95f4\u5185\u7684\u6700\u5c0f\u5143\u7d20\n int k = i;\n for (int j = i + 1; j < n; j++) {\n if (nums[j] < nums[k])\n k = j; // \u8bb0\u5f55\u6700\u5c0f\u5143\u7d20\u7684\u7d22\u5f15\n }\n // \u5c06\u8be5\u6700\u5c0f\u5143\u7d20\u4e0e\u672a\u6392\u5e8f\u533a\u95f4\u7684\u9996\u4e2a\u5143\u7d20\u4ea4\u6362\n int temp = nums[i];\n nums[i] = nums[k];\n nums[k] = temp;\n }\n}\n
selection_sort.zig[class]{}-[func]{selectionSort}\n
"},{"location":"chapter_sorting/selection_sort/#1121","title":"11.2.1 \u00a0 \u7b97\u6cd5\u7279\u6027","text":"nums[i]
\u6709\u53ef\u80fd\u88ab\u4ea4\u6362\u81f3\u4e0e\u5176\u76f8\u7b49\u7684\u5143\u7d20\u7684\u53f3\u8fb9\uff0c\u5bfc\u81f4\u4e24\u8005\u7684\u76f8\u5bf9\u987a\u5e8f\u53d1\u751f\u6539\u53d8\u3002\u56fe 11-3 \u00a0 \u9009\u62e9\u6392\u5e8f\u975e\u7a33\u5b9a\u793a\u4f8b
"},{"location":"chapter_sorting/sorting_algorithm/","title":"11.1 \u00a0 \u6392\u5e8f\u7b97\u6cd5","text":"\u300c\u6392\u5e8f\u7b97\u6cd5 sorting algorithm\u300d\u7528\u4e8e\u5bf9\u4e00\u7ec4\u6570\u636e\u6309\u7167\u7279\u5b9a\u987a\u5e8f\u8fdb\u884c\u6392\u5217\u3002\u6392\u5e8f\u7b97\u6cd5\u6709\u7740\u5e7f\u6cdb\u7684\u5e94\u7528\uff0c\u56e0\u4e3a\u6709\u5e8f\u6570\u636e\u901a\u5e38\u80fd\u591f\u88ab\u66f4\u9ad8\u6548\u5730\u67e5\u627e\u3001\u5206\u6790\u548c\u5904\u7406\u3002
\u5982\u56fe 11-1 \u6240\u793a\uff0c\u6392\u5e8f\u7b97\u6cd5\u4e2d\u7684\u6570\u636e\u7c7b\u578b\u53ef\u4ee5\u662f\u6574\u6570\u3001\u6d6e\u70b9\u6570\u3001\u5b57\u7b26\u6216\u5b57\u7b26\u4e32\u7b49\u3002\u6392\u5e8f\u7684\u5224\u65ad\u89c4\u5219\u53ef\u6839\u636e\u9700\u6c42\u8bbe\u5b9a\uff0c\u5982\u6570\u5b57\u5927\u5c0f\u3001\u5b57\u7b26 ASCII \u7801\u987a\u5e8f\u6216\u81ea\u5b9a\u4e49\u89c4\u5219\u3002
\u56fe 11-1 \u00a0 \u6570\u636e\u7c7b\u578b\u548c\u5224\u65ad\u89c4\u5219\u793a\u4f8b
"},{"location":"chapter_sorting/sorting_algorithm/#1111","title":"11.1.1 \u00a0 \u8bc4\u4ef7\u7ef4\u5ea6","text":"\u8fd0\u884c\u6548\u7387\uff1a\u6211\u4eec\u671f\u671b\u6392\u5e8f\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u5c3d\u91cf\u4f4e\uff0c\u4e14\u603b\u4f53\u64cd\u4f5c\u6570\u91cf\u8f83\u5c11\uff08\u65f6\u95f4\u590d\u6742\u5ea6\u4e2d\u7684\u5e38\u6570\u9879\u53d8\u5c0f\uff09\u3002\u5bf9\u4e8e\u5927\u6570\u636e\u91cf\u7684\u60c5\u51b5\uff0c\u8fd0\u884c\u6548\u7387\u663e\u5f97\u5c24\u4e3a\u91cd\u8981\u3002
\u5c31\u5730\u6027\uff1a\u987e\u540d\u601d\u4e49\uff0c\u300c\u539f\u5730\u6392\u5e8f\u300d\u901a\u8fc7\u5728\u539f\u6570\u7ec4\u4e0a\u76f4\u63a5\u64cd\u4f5c\u5b9e\u73b0\u6392\u5e8f\uff0c\u65e0\u987b\u501f\u52a9\u989d\u5916\u7684\u8f85\u52a9\u6570\u7ec4\uff0c\u4ece\u800c\u8282\u7701\u5185\u5b58\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u539f\u5730\u6392\u5e8f\u7684\u6570\u636e\u642c\u8fd0\u64cd\u4f5c\u8f83\u5c11\uff0c\u8fd0\u884c\u901f\u5ea6\u4e5f\u66f4\u5feb\u3002
\u7a33\u5b9a\u6027\uff1a\u300c\u7a33\u5b9a\u6392\u5e8f\u300d\u5728\u5b8c\u6210\u6392\u5e8f\u540e\uff0c\u76f8\u7b49\u5143\u7d20\u5728\u6570\u7ec4\u4e2d\u7684\u76f8\u5bf9\u987a\u5e8f\u4e0d\u53d1\u751f\u6539\u53d8\u3002
\u7a33\u5b9a\u6392\u5e8f\u662f\u591a\u7ea7\u6392\u5e8f\u573a\u666f\u7684\u5fc5\u8981\u6761\u4ef6\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5b58\u50a8\u5b66\u751f\u4fe1\u606f\u7684\u8868\u683c\uff0c\u7b2c 1 \u5217\u548c\u7b2c 2 \u5217\u5206\u522b\u662f\u59d3\u540d\u548c\u5e74\u9f84\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u300c\u975e\u7a33\u5b9a\u6392\u5e8f\u300d\u53ef\u80fd\u5bfc\u81f4\u8f93\u5165\u6570\u636e\u7684\u6709\u5e8f\u6027\u4e27\u5931\uff1a
# \u8f93\u5165\u6570\u636e\u662f\u6309\u7167\u59d3\u540d\u6392\u5e8f\u597d\u7684\n# (name, age)\n ('A', 19)\n ('B', 18)\n ('C', 21)\n ('D', 19)\n ('E', 23)\n\n# \u5047\u8bbe\u4f7f\u7528\u975e\u7a33\u5b9a\u6392\u5e8f\u7b97\u6cd5\u6309\u5e74\u9f84\u6392\u5e8f\u5217\u8868\uff0c\n# \u7ed3\u679c\u4e2d ('D', 19) \u548c ('A', 19) \u7684\u76f8\u5bf9\u4f4d\u7f6e\u6539\u53d8\uff0c\n# \u8f93\u5165\u6570\u636e\u6309\u59d3\u540d\u6392\u5e8f\u7684\u6027\u8d28\u4e22\u5931\n ('B', 18)\n ('D', 19)\n ('A', 19)\n ('C', 21)\n ('E', 23)\n
\u81ea\u9002\u5e94\u6027\uff1a\u300c\u81ea\u9002\u5e94\u6392\u5e8f\u300d\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4f1a\u53d7\u8f93\u5165\u6570\u636e\u7684\u5f71\u54cd\uff0c\u5373\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6\u3001\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6\u3001\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u5e76\u4e0d\u5b8c\u5168\u76f8\u7b49\u3002
\u81ea\u9002\u5e94\u6027\u9700\u8981\u6839\u636e\u5177\u4f53\u60c5\u51b5\u6765\u8bc4\u4f30\u3002\u5982\u679c\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6\u5dee\u4e8e\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\uff0c\u8bf4\u660e\u6392\u5e8f\u7b97\u6cd5\u5728\u67d0\u4e9b\u6570\u636e\u4e0b\u6027\u80fd\u53ef\u80fd\u52a3\u5316\uff0c\u56e0\u6b64\u88ab\u89c6\u4e3a\u8d1f\u9762\u5c5e\u6027\uff1b\u800c\u5982\u679c\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6\u4f18\u4e8e\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\uff0c\u5219\u88ab\u89c6\u4e3a\u6b63\u9762\u5c5e\u6027\u3002
\u662f\u5426\u57fa\u4e8e\u6bd4\u8f83\uff1a\u300c\u57fa\u4e8e\u6bd4\u8f83\u7684\u6392\u5e8f\u300d\u4f9d\u8d56\u6bd4\u8f83\u8fd0\u7b97\u7b26\uff08\\(<\\)\u3001\\(=\\)\u3001\\(>\\)\uff09\u6765\u5224\u65ad\u5143\u7d20\u7684\u76f8\u5bf9\u987a\u5e8f\uff0c\u4ece\u800c\u6392\u5e8f\u6574\u4e2a\u6570\u7ec4\uff0c\u7406\u8bba\u6700\u4f18\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n \\log n)\\) \u3002\u800c\u300c\u975e\u6bd4\u8f83\u6392\u5e8f\u300d\u4e0d\u4f7f\u7528\u6bd4\u8f83\u8fd0\u7b97\u7b26\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u53ef\u8fbe \\(O(n)\\) \uff0c\u4f46\u5176\u901a\u7528\u6027\u76f8\u5bf9\u8f83\u5dee\u3002
"},{"location":"chapter_sorting/sorting_algorithm/#1112","title":"11.1.2 \u00a0 \u7406\u60f3\u6392\u5e8f\u7b97\u6cd5","text":"\u8fd0\u884c\u5feb\u3001\u539f\u5730\u3001\u7a33\u5b9a\u3001\u6b63\u5411\u81ea\u9002\u5e94\u3001\u901a\u7528\u6027\u597d\u3002\u663e\u7136\uff0c\u8fc4\u4eca\u4e3a\u6b62\u5c1a\u672a\u53d1\u73b0\u517c\u5177\u4ee5\u4e0a\u6240\u6709\u7279\u6027\u7684\u6392\u5e8f\u7b97\u6cd5\u3002\u56e0\u6b64\uff0c\u5728\u9009\u62e9\u6392\u5e8f\u7b97\u6cd5\u65f6\uff0c\u9700\u8981\u6839\u636e\u5177\u4f53\u7684\u6570\u636e\u7279\u70b9\u548c\u95ee\u9898\u9700\u6c42\u6765\u51b3\u5b9a\u3002
\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u5171\u540c\u5b66\u4e60\u5404\u79cd\u6392\u5e8f\u7b97\u6cd5\uff0c\u5e76\u57fa\u4e8e\u4e0a\u8ff0\u8bc4\u4ef7\u7ef4\u5ea6\u5bf9\u5404\u4e2a\u6392\u5e8f\u7b97\u6cd5\u7684\u4f18\u7f3a\u70b9\u8fdb\u884c\u5206\u6790\u3002
"},{"location":"chapter_sorting/summary/","title":"11.11 \u00a0 \u5c0f\u7ed3","text":""},{"location":"chapter_sorting/summary/#1","title":"1. \u00a0 \u91cd\u70b9\u56de\u987e","text":"\u56fe 11-19 \u00a0 \u6392\u5e8f\u7b97\u6cd5\u5bf9\u6bd4
"},{"location":"chapter_sorting/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"\u6392\u5e8f\u7b97\u6cd5\u7a33\u5b9a\u6027\u5728\u4ec0\u4e48\u60c5\u51b5\u4e0b\u662f\u5fc5\u9700\u7684\uff1f
\u5728\u73b0\u5b9e\u4e2d\uff0c\u6211\u4eec\u6709\u53ef\u80fd\u662f\u57fa\u4e8e\u5bf9\u8c61\u7684\u67d0\u4e2a\u5c5e\u6027\u8fdb\u884c\u6392\u5e8f\u3002\u4f8b\u5982\uff0c\u5b66\u751f\u6709\u59d3\u540d\u548c\u8eab\u9ad8\u4e24\u4e2a\u5c5e\u6027\uff0c\u6211\u4eec\u5e0c\u671b\u5b9e\u73b0\u4e00\u4e2a\u591a\u7ea7\u6392\u5e8f\uff1a
\u5148\u6309\u7167\u59d3\u540d\u8fdb\u884c\u6392\u5e8f\uff0c\u5f97\u5230 (A, 180) (B, 185) (C, 170) (D, 170)
\uff1b\u518d\u5bf9\u8eab\u9ad8\u8fdb\u884c\u6392\u5e8f\u3002\u7531\u4e8e\u6392\u5e8f\u7b97\u6cd5\u4e0d\u7a33\u5b9a\uff0c\u56e0\u6b64\u53ef\u80fd\u5f97\u5230 (D, 170) (C, 170) (A, 180) (B, 185)
\u3002
\u53ef\u4ee5\u53d1\u73b0\uff0c\u5b66\u751f D \u548c C \u7684\u4f4d\u7f6e\u53d1\u751f\u4e86\u4ea4\u6362\uff0c\u59d3\u540d\u7684\u6709\u5e8f\u6027\u88ab\u7834\u574f\u4e86\uff0c\u800c\u8fd9\u662f\u6211\u4eec\u4e0d\u5e0c\u671b\u770b\u5230\u7684\u3002
\u54e8\u5175\u5212\u5206\u4e2d\u201c\u4ece\u53f3\u5f80\u5de6\u67e5\u627e\u201d\u4e0e\u201c\u4ece\u5de6\u5f80\u53f3\u67e5\u627e\u201d\u7684\u987a\u5e8f\u53ef\u4ee5\u4ea4\u6362\u5417\uff1f
\u4e0d\u884c\uff0c\u5f53\u6211\u4eec\u4ee5\u6700\u5de6\u7aef\u5143\u7d20\u4e3a\u57fa\u51c6\u6570\u65f6\uff0c\u5fc5\u987b\u5148\u201c\u4ece\u53f3\u5f80\u5de6\u67e5\u627e\u201d\u518d\u201c\u4ece\u5de6\u5f80\u53f3\u67e5\u627e\u201d\u3002\u8fd9\u4e2a\u7ed3\u8bba\u6709\u4e9b\u53cd\u76f4\u89c9\uff0c\u6211\u4eec\u6765\u5256\u6790\u4e00\u4e0b\u539f\u56e0\u3002
\u54e8\u5175\u5212\u5206 partition()
\u7684\u6700\u540e\u4e00\u6b65\u662f\u4ea4\u6362 nums[left]
\u548c nums[i]
\u3002\u5b8c\u6210\u4ea4\u6362\u540e\uff0c\u57fa\u51c6\u6570\u5de6\u8fb9\u7684\u5143\u7d20\u90fd <=
\u57fa\u51c6\u6570\uff0c\u8fd9\u5c31\u8981\u6c42\u6700\u540e\u4e00\u6b65\u4ea4\u6362\u524d nums[left] >= nums[i]
\u5fc5\u987b\u6210\u7acb\u3002\u5047\u8bbe\u6211\u4eec\u5148\u201c\u4ece\u5de6\u5f80\u53f3\u67e5\u627e\u201d\uff0c\u90a3\u4e48\u5982\u679c\u627e\u4e0d\u5230\u6bd4\u57fa\u51c6\u6570\u66f4\u5927\u7684\u5143\u7d20\uff0c\u5219\u4f1a\u5728 i == j
\u65f6\u8df3\u51fa\u5faa\u73af\uff0c\u6b64\u65f6\u53ef\u80fd nums[j] == nums[i] > nums[left]
\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u6b64\u65f6\u6700\u540e\u4e00\u6b65\u4ea4\u6362\u64cd\u4f5c\u4f1a\u628a\u4e00\u4e2a\u6bd4\u57fa\u51c6\u6570\u66f4\u5927\u7684\u5143\u7d20\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\uff0c\u5bfc\u81f4\u54e8\u5175\u5212\u5206\u5931\u8d25\u3002
\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u7ed9\u5b9a\u6570\u7ec4 [0, 0, 0, 0, 1]
\uff0c\u5982\u679c\u5148\u201c\u4ece\u5de6\u5411\u53f3\u67e5\u627e\u201d\uff0c\u54e8\u5175\u5212\u5206\u540e\u6570\u7ec4\u4e3a [1, 0, 0, 0, 0]
\uff0c\u8fd9\u4e2a\u7ed3\u679c\u662f\u4e0d\u6b63\u786e\u7684\u3002
\u518d\u6df1\u5165\u601d\u8003\u4e00\u4e0b\uff0c\u5982\u679c\u6211\u4eec\u9009\u62e9 nums[right]
\u4e3a\u57fa\u51c6\u6570\uff0c\u90a3\u4e48\u6b63\u597d\u53cd\u8fc7\u6765\uff0c\u5fc5\u987b\u5148\u201c\u4ece\u5de6\u5f80\u53f3\u67e5\u627e\u201d\u3002
\u5173\u4e8e\u5c3e\u9012\u5f52\u4f18\u5316\uff0c\u4e3a\u4ec0\u4e48\u9009\u77ed\u7684\u6570\u7ec4\u80fd\u4fdd\u8bc1\u9012\u5f52\u6df1\u5ea6\u4e0d\u8d85\u8fc7 \\(\\log n\\) \uff1f
\u9012\u5f52\u6df1\u5ea6\u5c31\u662f\u5f53\u524d\u672a\u8fd4\u56de\u7684\u9012\u5f52\u65b9\u6cd5\u7684\u6570\u91cf\u3002\u6bcf\u8f6e\u54e8\u5175\u5212\u5206\u6211\u4eec\u5c06\u539f\u6570\u7ec4\u5212\u5206\u4e3a\u4e24\u4e2a\u5b50\u6570\u7ec4\u3002\u5728\u5c3e\u9012\u5f52\u4f18\u5316\u540e\uff0c\u5411\u4e0b\u9012\u5f52\u7684\u5b50\u6570\u7ec4\u957f\u5ea6\u6700\u5927\u4e3a\u539f\u6570\u7ec4\u957f\u5ea6\u7684\u4e00\u534a\u3002\u5047\u8bbe\u6700\u5dee\u60c5\u51b5\uff0c\u4e00\u76f4\u4e3a\u4e00\u534a\u957f\u5ea6\uff0c\u90a3\u4e48\u6700\u7ec8\u7684\u9012\u5f52\u6df1\u5ea6\u5c31\u662f \\(\\log n\\) \u3002
\u56de\u987e\u539f\u59cb\u7684\u5feb\u901f\u6392\u5e8f\uff0c\u6211\u4eec\u6709\u53ef\u80fd\u4f1a\u8fde\u7eed\u5730\u9012\u5f52\u957f\u5ea6\u8f83\u5927\u7684\u6570\u7ec4\uff0c\u6700\u5dee\u60c5\u51b5\u4e0b\u4e3a \\(n\\)\u3001\\(n - 1\\)\u3001\\(\\dots\\)\u3001\\(2\\)\u3001\\(1\\) \uff0c\u9012\u5f52\u6df1\u5ea6\u4e3a \\(n\\) \u3002\u5c3e\u9012\u5f52\u4f18\u5316\u53ef\u4ee5\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\u51fa\u73b0\u3002
\u5f53\u6570\u7ec4\u4e2d\u6240\u6709\u5143\u7d20\u90fd\u76f8\u7b49\u65f6\uff0c\u5feb\u901f\u6392\u5e8f\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u662f \\(O(n^2)\\) \u5417\uff1f\u8be5\u5982\u4f55\u5904\u7406\u8fd9\u79cd\u9000\u5316\u60c5\u51b5\uff1f
\u662f\u7684\u3002\u5bf9\u4e8e\u8fd9\u79cd\u60c5\u51b5\uff0c\u53ef\u4ee5\u8003\u8651\u901a\u8fc7\u54e8\u5175\u5212\u5206\u5c06\u6570\u7ec4\u5212\u5206\u4e3a\u4e09\u4e2a\u90e8\u5206\uff1a\u5c0f\u4e8e\u3001\u7b49\u4e8e\u3001\u5927\u4e8e\u57fa\u51c6\u6570\u3002\u4ec5\u5411\u4e0b\u9012\u5f52\u5c0f\u4e8e\u548c\u5927\u4e8e\u7684\u4e24\u90e8\u5206\u3002\u5728\u8be5\u65b9\u6cd5\u4e0b\uff0c\u8f93\u5165\u5143\u7d20\u5168\u90e8\u76f8\u7b49\u7684\u6570\u7ec4\uff0c\u4ec5\u4e00\u8f6e\u54e8\u5175\u5212\u5206\u5373\u53ef\u5b8c\u6210\u6392\u5e8f\u3002
\u6876\u6392\u5e8f\u7684\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a\u4ec0\u4e48\u662f \\(O(n^2)\\) \uff1f
\u6700\u5dee\u60c5\u51b5\u4e0b\uff0c\u6240\u6709\u5143\u7d20\u88ab\u5206\u81f3\u540c\u4e00\u4e2a\u6876\u4e2d\u3002\u5982\u679c\u6211\u4eec\u91c7\u7528\u4e00\u4e2a \\(O(n^2)\\) \u7b97\u6cd5\u6765\u6392\u5e8f\u8fd9\u4e9b\u5143\u7d20\uff0c\u5219\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\) \u3002
"},{"location":"chapter_stack_and_queue/","title":"\u7b2c 5 \u7ae0 \u00a0 \u6808\u4e0e\u961f\u5217","text":"Abstract
\u6808\u5982\u540c\u53e0\u732b\u732b\uff0c\u800c\u961f\u5217\u5c31\u50cf\u732b\u732b\u6392\u961f\u3002
\u4e24\u8005\u5206\u522b\u4ee3\u8868\u5148\u5165\u540e\u51fa\u548c\u5148\u5165\u5148\u51fa\u7684\u903b\u8f91\u5173\u7cfb\u3002
"},{"location":"chapter_stack_and_queue/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"\u5728\u961f\u5217\u4e2d\uff0c\u6211\u4eec\u4ec5\u80fd\u5220\u9664\u5934\u90e8\u5143\u7d20\u6216\u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20\u3002\u5982\u56fe 5-7 \u6240\u793a\uff0c\u300c\u53cc\u5411\u961f\u5217 double-ended queue\u300d\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7684\u7075\u6d3b\u6027\uff0c\u5141\u8bb8\u5728\u5934\u90e8\u548c\u5c3e\u90e8\u6267\u884c\u5143\u7d20\u7684\u6dfb\u52a0\u6216\u5220\u9664\u64cd\u4f5c\u3002
\u56fe 5-7 \u00a0 \u53cc\u5411\u961f\u5217\u7684\u64cd\u4f5c
"},{"location":"chapter_stack_and_queue/deque/#531","title":"5.3.1 \u00a0 \u53cc\u5411\u961f\u5217\u5e38\u7528\u64cd\u4f5c","text":"\u53cc\u5411\u961f\u5217\u7684\u5e38\u7528\u64cd\u4f5c\u5982\u8868 5-3 \u6240\u793a\uff0c\u5177\u4f53\u7684\u65b9\u6cd5\u540d\u79f0\u9700\u8981\u6839\u636e\u6240\u4f7f\u7528\u7684\u7f16\u7a0b\u8bed\u8a00\u6765\u786e\u5b9a\u3002
\u8868 5-3 \u00a0 \u53cc\u5411\u961f\u5217\u64cd\u4f5c\u6548\u7387
\u65b9\u6cd5\u540d \u63cf\u8ff0 \u65f6\u95f4\u590d\u6742\u5ea6 pushFirst() \u5c06\u5143\u7d20\u6dfb\u52a0\u81f3\u961f\u9996 \\(O(1)\\) pushLast() \u5c06\u5143\u7d20\u6dfb\u52a0\u81f3\u961f\u5c3e \\(O(1)\\) popFirst() \u5220\u9664\u961f\u9996\u5143\u7d20 \\(O(1)\\) popLast() \u5220\u9664\u961f\u5c3e\u5143\u7d20 \\(O(1)\\) peekFirst() \u8bbf\u95ee\u961f\u9996\u5143\u7d20 \\(O(1)\\) peekLast() \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 \\(O(1)\\)\u540c\u6837\u5730\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u7f16\u7a0b\u8bed\u8a00\u4e2d\u5df2\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217\u7c7b\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig deque.pyfrom collections import deque\n\n# \u521d\u59cb\u5316\u53cc\u5411\u961f\u5217\ndeque: deque[int] = deque()\n\n# \u5143\u7d20\u5165\u961f\ndeque.append(2) # \u6dfb\u52a0\u81f3\u961f\u5c3e\ndeque.append(5)\ndeque.append(4)\ndeque.appendleft(3) # \u6dfb\u52a0\u81f3\u961f\u9996\ndeque.appendleft(1)\n\n# \u8bbf\u95ee\u5143\u7d20\nfront: int = deque[0] # \u961f\u9996\u5143\u7d20\nrear: int = deque[-1] # \u961f\u5c3e\u5143\u7d20\n\n# \u5143\u7d20\u51fa\u961f\npop_front: int = deque.popleft() # \u961f\u9996\u5143\u7d20\u51fa\u961f\npop_rear: int = deque.pop() # \u961f\u5c3e\u5143\u7d20\u51fa\u961f\n\n# \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\nsize: int = len(deque)\n\n# \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a\nis_empty: bool = len(deque) == 0\n
deque.cpp/* \u521d\u59cb\u5316\u53cc\u5411\u961f\u5217 */\ndeque<int> deque;\n\n/* \u5143\u7d20\u5165\u961f */\ndeque.push_back(2); // \u6dfb\u52a0\u81f3\u961f\u5c3e\ndeque.push_back(5);\ndeque.push_back(4);\ndeque.push_front(3); // \u6dfb\u52a0\u81f3\u961f\u9996\ndeque.push_front(1);\n\n/* \u8bbf\u95ee\u5143\u7d20 */\nint front = deque.front(); // \u961f\u9996\u5143\u7d20\nint back = deque.back(); // \u961f\u5c3e\u5143\u7d20\n\n/* \u5143\u7d20\u51fa\u961f */\ndeque.pop_front(); // \u961f\u9996\u5143\u7d20\u51fa\u961f\ndeque.pop_back(); // \u961f\u5c3e\u5143\u7d20\u51fa\u961f\n\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nint size = deque.size();\n\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool empty = deque.empty();\n
deque.java/* \u521d\u59cb\u5316\u53cc\u5411\u961f\u5217 */\nDeque<Integer> deque = new LinkedList<>();\n\n/* \u5143\u7d20\u5165\u961f */\ndeque.offerLast(2); // \u6dfb\u52a0\u81f3\u961f\u5c3e\ndeque.offerLast(5);\ndeque.offerLast(4);\ndeque.offerFirst(3); // \u6dfb\u52a0\u81f3\u961f\u9996\ndeque.offerFirst(1);\n\n/* \u8bbf\u95ee\u5143\u7d20 */\nint peekFirst = deque.peekFirst(); // \u961f\u9996\u5143\u7d20\nint peekLast = deque.peekLast(); // \u961f\u5c3e\u5143\u7d20\n\n/* \u5143\u7d20\u51fa\u961f */\nint popFirst = deque.pollFirst(); // \u961f\u9996\u5143\u7d20\u51fa\u961f\nint popLast = deque.pollLast(); // \u961f\u5c3e\u5143\u7d20\u51fa\u961f\n\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nint size = deque.size();\n\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nboolean isEmpty = deque.isEmpty();\n
deque.cs/* \u521d\u59cb\u5316\u53cc\u5411\u961f\u5217 */\n// \u5728 C# \u4e2d\uff0c\u5c06\u94fe\u8868 LinkedList \u770b\u4f5c\u53cc\u5411\u961f\u5217\u6765\u4f7f\u7528\nLinkedList<int> deque = new();\n\n/* \u5143\u7d20\u5165\u961f */\ndeque.AddLast(2); // \u6dfb\u52a0\u81f3\u961f\u5c3e\ndeque.AddLast(5);\ndeque.AddLast(4);\ndeque.AddFirst(3); // \u6dfb\u52a0\u81f3\u961f\u9996\ndeque.AddFirst(1);\n\n/* \u8bbf\u95ee\u5143\u7d20 */\nint peekFirst = deque.First.Value; // \u961f\u9996\u5143\u7d20\nint peekLast = deque.Last.Value; // \u961f\u5c3e\u5143\u7d20\n\n/* \u5143\u7d20\u51fa\u961f */\ndeque.RemoveFirst(); // \u961f\u9996\u5143\u7d20\u51fa\u961f\ndeque.RemoveLast(); // \u961f\u5c3e\u5143\u7d20\u51fa\u961f\n\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nint size = deque.Count;\n\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty = deque.Count == 0;\n
deque_test.go/* \u521d\u59cb\u5316\u53cc\u5411\u961f\u5217 */\n// \u5728 Go \u4e2d\uff0c\u5c06 list \u4f5c\u4e3a\u53cc\u5411\u961f\u5217\u4f7f\u7528\ndeque := list.New()\n\n/* \u5143\u7d20\u5165\u961f */\ndeque.PushBack(2) // \u6dfb\u52a0\u81f3\u961f\u5c3e\ndeque.PushBack(5)\ndeque.PushBack(4)\ndeque.PushFront(3) // \u6dfb\u52a0\u81f3\u961f\u9996\ndeque.PushFront(1)\n\n/* \u8bbf\u95ee\u5143\u7d20 */\nfront := deque.Front() // \u961f\u9996\u5143\u7d20\nrear := deque.Back() // \u961f\u5c3e\u5143\u7d20\n\n/* \u5143\u7d20\u51fa\u961f */\ndeque.Remove(front) // \u961f\u9996\u5143\u7d20\u51fa\u961f\ndeque.Remove(rear) // \u961f\u5c3e\u5143\u7d20\u51fa\u961f\n\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nsize := deque.Len()\n\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nisEmpty := deque.Len() == 0\n
deque.swift/* \u521d\u59cb\u5316\u53cc\u5411\u961f\u5217 */\n// Swift \u6ca1\u6709\u5185\u7f6e\u7684\u53cc\u5411\u961f\u5217\u7c7b\uff0c\u53ef\u4ee5\u628a Array \u5f53\u4f5c\u53cc\u5411\u961f\u5217\u6765\u4f7f\u7528\nvar deque: [Int] = []\n\n/* \u5143\u7d20\u5165\u961f */\ndeque.append(2) // \u6dfb\u52a0\u81f3\u961f\u5c3e\ndeque.append(5)\ndeque.append(4)\ndeque.insert(3, at: 0) // \u6dfb\u52a0\u81f3\u961f\u9996\ndeque.insert(1, at: 0)\n\n/* \u8bbf\u95ee\u5143\u7d20 */\nlet peekFirst = deque.first! // \u961f\u9996\u5143\u7d20\nlet peekLast = deque.last! // \u961f\u5c3e\u5143\u7d20\n\n/* \u5143\u7d20\u51fa\u961f */\n// \u4f7f\u7528 Array \u6a21\u62df\u65f6 popFirst \u7684\u590d\u6742\u5ea6\u4e3a O(n)\nlet popFirst = deque.removeFirst() // \u961f\u9996\u5143\u7d20\u51fa\u961f\nlet popLast = deque.removeLast() // \u961f\u5c3e\u5143\u7d20\u51fa\u961f\n\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nlet size = deque.count\n\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nlet isEmpty = deque.isEmpty\n
deque.js/* \u521d\u59cb\u5316\u53cc\u5411\u961f\u5217 */\n// JavaScript \u6ca1\u6709\u5185\u7f6e\u7684\u53cc\u7aef\u961f\u5217\uff0c\u53ea\u80fd\u628a Array \u5f53\u4f5c\u53cc\u7aef\u961f\u5217\u6765\u4f7f\u7528\nconst deque = [];\n\n/* \u5143\u7d20\u5165\u961f */\ndeque.push(2);\ndeque.push(5);\ndeque.push(4);\n// \u8bf7\u6ce8\u610f\uff0c\u7531\u4e8e\u662f\u6570\u7ec4\uff0cunshift() \u65b9\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\ndeque.unshift(3);\ndeque.unshift(1);\nconsole.log(\"\u53cc\u5411\u961f\u5217 deque = \", deque);\n\n/* \u8bbf\u95ee\u5143\u7d20 */\nconst peekFirst = deque[0];\nconsole.log(\"\u961f\u9996\u5143\u7d20 peekFirst = \" + peekFirst);\nconst peekLast = deque[deque.length - 1];\nconsole.log(\"\u961f\u5c3e\u5143\u7d20 peekLast = \" + peekLast);\n\n/* \u5143\u7d20\u51fa\u961f */\n// \u8bf7\u6ce8\u610f\uff0c\u7531\u4e8e\u662f\u6570\u7ec4\uff0cshift() \u65b9\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\nconst popFront = deque.shift();\nconsole.log(\"\u961f\u9996\u51fa\u961f\u5143\u7d20 popFront = \" + popFront + \"\uff0c\u961f\u9996\u51fa\u961f\u540e deque = \" + deque);\nconst popBack = deque.pop();\nconsole.log(\"\u961f\u5c3e\u51fa\u961f\u5143\u7d20 popBack = \" + popBack + \"\uff0c\u961f\u5c3e\u51fa\u961f\u540e deque = \" + deque);\n\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nconst size = deque.length;\nconsole.log(\"\u53cc\u5411\u961f\u5217\u957f\u5ea6 size = \" + size);\n\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nconst isEmpty = size === 0;\nconsole.log(\"\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a = \" + isEmpty);\n
deque.ts/* \u521d\u59cb\u5316\u53cc\u5411\u961f\u5217 */\n// TypeScript \u6ca1\u6709\u5185\u7f6e\u7684\u53cc\u7aef\u961f\u5217\uff0c\u53ea\u80fd\u628a Array \u5f53\u4f5c\u53cc\u7aef\u961f\u5217\u6765\u4f7f\u7528\nconst deque: number[] = [];\n\n/* \u5143\u7d20\u5165\u961f */\ndeque.push(2);\ndeque.push(5);\ndeque.push(4);\n// \u8bf7\u6ce8\u610f\uff0c\u7531\u4e8e\u662f\u6570\u7ec4\uff0cunshift() \u65b9\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\ndeque.unshift(3);\ndeque.unshift(1);\nconsole.log(\"\u53cc\u5411\u961f\u5217 deque = \", deque);\n\n/* \u8bbf\u95ee\u5143\u7d20 */\nconst peekFirst: number = deque[0];\nconsole.log(\"\u961f\u9996\u5143\u7d20 peekFirst = \" + peekFirst);\nconst peekLast: number = deque[deque.length - 1];\nconsole.log(\"\u961f\u5c3e\u5143\u7d20 peekLast = \" + peekLast);\n\n/* \u5143\u7d20\u51fa\u961f */\n// \u8bf7\u6ce8\u610f\uff0c\u7531\u4e8e\u662f\u6570\u7ec4\uff0cshift() \u65b9\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\nconst popFront: number = deque.shift() as number;\nconsole.log(\"\u961f\u9996\u51fa\u961f\u5143\u7d20 popFront = \" + popFront + \"\uff0c\u961f\u9996\u51fa\u961f\u540e deque = \" + deque);\nconst popBack: number = deque.pop() as number;\nconsole.log(\"\u961f\u5c3e\u51fa\u961f\u5143\u7d20 popBack = \" + popBack + \"\uff0c\u961f\u5c3e\u51fa\u961f\u540e deque = \" + deque);\n\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nconst size: number = deque.length;\nconsole.log(\"\u53cc\u5411\u961f\u5217\u957f\u5ea6 size = \" + size);\n\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nconst isEmpty: boolean = size === 0;\nconsole.log(\"\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a = \" + isEmpty);\n
deque.dart/* \u521d\u59cb\u5316\u53cc\u5411\u961f\u5217 */\n// \u5728 Dart \u4e2d\uff0cQueue \u88ab\u5b9a\u4e49\u4e3a\u53cc\u5411\u961f\u5217\nQueue<int> deque = Queue<int>();\n\n/* \u5143\u7d20\u5165\u961f */\ndeque.addLast(2); // \u6dfb\u52a0\u81f3\u961f\u5c3e\ndeque.addLast(5);\ndeque.addLast(4);\ndeque.addFirst(3); // \u6dfb\u52a0\u81f3\u961f\u9996\ndeque.addFirst(1);\n\n/* \u8bbf\u95ee\u5143\u7d20 */\nint peekFirst = deque.first; // \u961f\u9996\u5143\u7d20\nint peekLast = deque.last; // \u961f\u5c3e\u5143\u7d20\n\n/* \u5143\u7d20\u51fa\u961f */\nint popFirst = deque.removeFirst(); // \u961f\u9996\u5143\u7d20\u51fa\u961f\nint popLast = deque.removeLast(); // \u961f\u5c3e\u5143\u7d20\u51fa\u961f\n\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nint size = deque.length;\n\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty = deque.isEmpty;W\n
deque.rs/* \u521d\u59cb\u5316\u53cc\u5411\u961f\u5217 */\nlet mut deque: VecDeque<u32> = VecDeque::new();\n\n/* \u5143\u7d20\u5165\u961f */\ndeque.push_back(2); // \u6dfb\u52a0\u81f3\u961f\u5c3e\ndeque.push_back(5);\ndeque.push_back(4);\ndeque.push_front(3); // \u6dfb\u52a0\u81f3\u961f\u9996\ndeque.push_front(1);\n\n/* \u8bbf\u95ee\u5143\u7d20 */\nif let Some(front) = deque.front() { // \u961f\u9996\u5143\u7d20\n}\nif let Some(rear) = deque.back() { // \u961f\u5c3e\u5143\u7d20\n}\n\n/* \u5143\u7d20\u51fa\u961f */\nif let Some(pop_front) = deque.pop_front() { // \u961f\u9996\u5143\u7d20\u51fa\u961f\n}\nif let Some(pop_rear) = deque.pop_back() { // \u961f\u5c3e\u5143\u7d20\u51fa\u961f\n}\n\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nlet size = deque.len();\n\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nlet is_empty = deque.is_empty();\n
deque.c// C \u672a\u63d0\u4f9b\u5185\u7f6e\u53cc\u5411\u961f\u5217\n
deque.zig\n
"},{"location":"chapter_stack_and_queue/deque/#532","title":"5.3.2 \u00a0 \u53cc\u5411\u961f\u5217\u5b9e\u73b0 *","text":"\u53cc\u5411\u961f\u5217\u7684\u5b9e\u73b0\u4e0e\u961f\u5217\u7c7b\u4f3c\uff0c\u53ef\u4ee5\u9009\u62e9\u94fe\u8868\u6216\u6570\u7ec4\u4f5c\u4e3a\u5e95\u5c42\u6570\u636e\u7ed3\u6784\u3002
"},{"location":"chapter_stack_and_queue/deque/#1","title":"1. \u00a0 \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u7684\u5b9e\u73b0","text":"\u56de\u987e\u4e0a\u4e00\u8282\u5185\u5bb9\uff0c\u6211\u4eec\u4f7f\u7528\u666e\u901a\u5355\u5411\u94fe\u8868\u6765\u5b9e\u73b0\u961f\u5217\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u65b9\u4fbf\u5730\u5220\u9664\u5934\u8282\u70b9\uff08\u5bf9\u5e94\u51fa\u961f\u64cd\u4f5c\uff09\u548c\u5728\u5c3e\u8282\u70b9\u540e\u6dfb\u52a0\u65b0\u8282\u70b9\uff08\u5bf9\u5e94\u5165\u961f\u64cd\u4f5c\uff09\u3002
\u5bf9\u4e8e\u53cc\u5411\u961f\u5217\u800c\u8a00\uff0c\u5934\u90e8\u548c\u5c3e\u90e8\u90fd\u53ef\u4ee5\u6267\u884c\u5165\u961f\u548c\u51fa\u961f\u64cd\u4f5c\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u53cc\u5411\u961f\u5217\u9700\u8981\u5b9e\u73b0\u53e6\u4e00\u4e2a\u5bf9\u79f0\u65b9\u5411\u7684\u64cd\u4f5c\u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u91c7\u7528\u201c\u53cc\u5411\u94fe\u8868\u201d\u4f5c\u4e3a\u53cc\u5411\u961f\u5217\u7684\u5e95\u5c42\u6570\u636e\u7ed3\u6784\u3002
\u5982\u56fe 5-8 \u6240\u793a\uff0c\u6211\u4eec\u5c06\u53cc\u5411\u94fe\u8868\u7684\u5934\u8282\u70b9\u548c\u5c3e\u8282\u70b9\u89c6\u4e3a\u53cc\u5411\u961f\u5217\u7684\u961f\u9996\u548c\u961f\u5c3e\uff0c\u540c\u65f6\u5b9e\u73b0\u5728\u4e24\u7aef\u6dfb\u52a0\u548c\u5220\u9664\u8282\u70b9\u7684\u529f\u80fd\u3002
LinkedListDequepushLast()pushFirst()popLast()popFirst()\u56fe 5-8 \u00a0 \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u53cc\u5411\u961f\u5217\u7684\u5165\u961f\u51fa\u961f\u64cd\u4f5c
\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig linkedlist_deque.pyclass ListNode:\n \"\"\"\u53cc\u5411\u94fe\u8868\u8282\u70b9\"\"\"\n\n def __init__(self, val: int):\n \"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n self.val: int = val\n self.next: ListNode | None = None # \u540e\u7ee7\u8282\u70b9\u5f15\u7528\n self.prev: ListNode | None = None # \u524d\u9a71\u8282\u70b9\u5f15\u7528\n\nclass LinkedListDeque:\n \"\"\"\u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217\"\"\"\n\n def __init__(self):\n \"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n self._front: ListNode | None = None # \u5934\u8282\u70b9 front\n self._rear: ListNode | None = None # \u5c3e\u8282\u70b9 rear\n self._size: int = 0 # \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\n\n def size(self) -> int:\n \"\"\"\u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\"\"\"\n return self._size\n\n def is_empty(self) -> bool:\n \"\"\"\u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a\"\"\"\n return self.size() == 0\n\n def push(self, num: int, is_front: bool):\n \"\"\"\u5165\u961f\u64cd\u4f5c\"\"\"\n node = ListNode(num)\n # \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n if self.is_empty():\n self._front = self._rear = node\n # \u961f\u9996\u5165\u961f\u64cd\u4f5c\n elif is_front:\n # \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\n self._front.prev = node\n node.next = self._front\n self._front = node # \u66f4\u65b0\u5934\u8282\u70b9\n # \u961f\u5c3e\u5165\u961f\u64cd\u4f5c\n else:\n # \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\n self._rear.next = node\n node.prev = self._rear\n self._rear = node # \u66f4\u65b0\u5c3e\u8282\u70b9\n self._size += 1 # \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n\n def push_first(self, num: int):\n \"\"\"\u961f\u9996\u5165\u961f\"\"\"\n self.push(num, True)\n\n def push_last(self, num: int):\n \"\"\"\u961f\u5c3e\u5165\u961f\"\"\"\n self.push(num, False)\n\n def pop(self, is_front: bool) -> int:\n \"\"\"\u51fa\u961f\u64cd\u4f5c\"\"\"\n if self.is_empty():\n raise IndexError(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\n # \u961f\u9996\u51fa\u961f\u64cd\u4f5c\n if is_front:\n val: int = self._front.val # \u6682\u5b58\u5934\u8282\u70b9\u503c\n # \u5220\u9664\u5934\u8282\u70b9\n fnext: ListNode | None = self._front.next\n if fnext != None:\n fnext.prev = None\n self._front.next = None\n self._front = fnext # \u66f4\u65b0\u5934\u8282\u70b9\n # \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\n else:\n val: int = self._rear.val # \u6682\u5b58\u5c3e\u8282\u70b9\u503c\n # \u5220\u9664\u5c3e\u8282\u70b9\n rprev: ListNode | None = self._rear.prev\n if rprev != None:\n rprev.next = None\n self._rear.prev = None\n self._rear = rprev # \u66f4\u65b0\u5c3e\u8282\u70b9\n self._size -= 1 # \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n return val\n\n def pop_first(self) -> int:\n \"\"\"\u961f\u9996\u51fa\u961f\"\"\"\n return self.pop(True)\n\n def pop_last(self) -> int:\n \"\"\"\u961f\u5c3e\u51fa\u961f\"\"\"\n return self.pop(False)\n\n def peek_first(self) -> int:\n \"\"\"\u8bbf\u95ee\u961f\u9996\u5143\u7d20\"\"\"\n if self.is_empty():\n raise IndexError(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\n return self._front.val\n\n def peek_last(self) -> int:\n \"\"\"\u8bbf\u95ee\u961f\u5c3e\u5143\u7d20\"\"\"\n if self.is_empty():\n raise IndexError(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\n return self._rear.val\n\n def to_array(self) -> list[int]:\n \"\"\"\u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370\"\"\"\n node = self._front\n res = [0] * self.size()\n for i in range(self.size()):\n res[i] = node.val\n node = node.next\n return res\n
linkedlist_deque.cpp/* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\nstruct DoublyListNode {\n int val; // \u8282\u70b9\u503c\n DoublyListNode *next; // \u540e\u7ee7\u8282\u70b9\u6307\u9488\n DoublyListNode *prev; // \u524d\u9a71\u8282\u70b9\u6307\u9488\n DoublyListNode(int val) : val(val), prev(nullptr), next(nullptr) {\n }\n};\n\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass LinkedListDeque {\n private:\n DoublyListNode *front, *rear; // \u5934\u8282\u70b9 front \uff0c\u5c3e\u8282\u70b9 rear\n int queSize = 0; // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\n\n public:\n /* \u6784\u9020\u65b9\u6cd5 */\n LinkedListDeque() : front(nullptr), rear(nullptr) {\n }\n\n /* \u6790\u6784\u65b9\u6cd5 */\n ~LinkedListDeque() {\n // \u904d\u5386\u94fe\u8868\u5220\u9664\u8282\u70b9\uff0c\u91ca\u653e\u5185\u5b58\n DoublyListNode *pre, *cur = front;\n while (cur != nullptr) {\n pre = cur;\n cur = cur->next;\n delete pre;\n }\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n int size() {\n return queSize;\n }\n\n /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n bool isEmpty() {\n return size() == 0;\n }\n\n /* \u5165\u961f\u64cd\u4f5c */\n void push(int num, bool isFront) {\n DoublyListNode *node = new DoublyListNode(num);\n // \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n if (isEmpty())\n front = rear = node;\n // \u961f\u9996\u5165\u961f\u64cd\u4f5c\n else if (isFront) {\n // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\n front->prev = node;\n node->next = front;\n front = node; // \u66f4\u65b0\u5934\u8282\u70b9\n // \u961f\u5c3e\u5165\u961f\u64cd\u4f5c\n } else {\n // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\n rear->next = node;\n node->prev = rear;\n rear = node; // \u66f4\u65b0\u5c3e\u8282\u70b9\n }\n queSize++; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n }\n\n /* \u961f\u9996\u5165\u961f */\n void pushFirst(int num) {\n push(num, true);\n }\n\n /* \u961f\u5c3e\u5165\u961f */\n void pushLast(int num) {\n push(num, false);\n }\n\n /* \u51fa\u961f\u64cd\u4f5c */\n int pop(bool isFront) {\n if (isEmpty())\n throw out_of_range(\"\u961f\u5217\u4e3a\u7a7a\");\n int val;\n // \u961f\u9996\u51fa\u961f\u64cd\u4f5c\n if (isFront) {\n val = front->val; // \u6682\u5b58\u5934\u8282\u70b9\u503c\n // \u5220\u9664\u5934\u8282\u70b9\n DoublyListNode *fNext = front->next;\n if (fNext != nullptr) {\n fNext->prev = nullptr;\n front->next = nullptr;\n delete front;\n }\n front = fNext; // \u66f4\u65b0\u5934\u8282\u70b9\n // \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\n } else {\n val = rear->val; // \u6682\u5b58\u5c3e\u8282\u70b9\u503c\n // \u5220\u9664\u5c3e\u8282\u70b9\n DoublyListNode *rPrev = rear->prev;\n if (rPrev != nullptr) {\n rPrev->next = nullptr;\n rear->prev = nullptr;\n delete rear;\n }\n rear = rPrev; // \u66f4\u65b0\u5c3e\u8282\u70b9\n }\n queSize--; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n return val;\n }\n\n /* \u961f\u9996\u51fa\u961f */\n int popFirst() {\n return pop(true);\n }\n\n /* \u961f\u5c3e\u51fa\u961f */\n int popLast() {\n return pop(false);\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n int peekFirst() {\n if (isEmpty())\n throw out_of_range(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\");\n return front->val;\n }\n\n /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n int peekLast() {\n if (isEmpty())\n throw out_of_range(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\");\n return rear->val;\n }\n\n /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n vector<int> toVector() {\n DoublyListNode *node = front;\n vector<int> res(size());\n for (int i = 0; i < res.size(); i++) {\n res[i] = node->val;\n node = node->next;\n }\n return res;\n }\n};\n
linkedlist_deque.java/* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\nclass ListNode {\n int val; // \u8282\u70b9\u503c\n ListNode next; // \u540e\u7ee7\u8282\u70b9\u5f15\u7528\n ListNode prev; // \u524d\u9a71\u8282\u70b9\u5f15\u7528\n\n ListNode(int val) {\n this.val = val;\n prev = next = null;\n }\n}\n\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass LinkedListDeque {\n private ListNode front, rear; // \u5934\u8282\u70b9 front \uff0c\u5c3e\u8282\u70b9 rear\n private int queSize = 0; // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\n\n public LinkedListDeque() {\n front = rear = null;\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n public int size() {\n return queSize;\n }\n\n /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n public boolean isEmpty() {\n return size() == 0;\n }\n\n /* \u5165\u961f\u64cd\u4f5c */\n private void push(int num, boolean isFront) {\n ListNode node = new ListNode(num);\n // \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n if (isEmpty())\n front = rear = node;\n // \u961f\u9996\u5165\u961f\u64cd\u4f5c\n else if (isFront) {\n // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\n front.prev = node;\n node.next = front;\n front = node; // \u66f4\u65b0\u5934\u8282\u70b9\n // \u961f\u5c3e\u5165\u961f\u64cd\u4f5c\n } else {\n // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\n rear.next = node;\n node.prev = rear;\n rear = node; // \u66f4\u65b0\u5c3e\u8282\u70b9\n }\n queSize++; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n }\n\n /* \u961f\u9996\u5165\u961f */\n public void pushFirst(int num) {\n push(num, true);\n }\n\n /* \u961f\u5c3e\u5165\u961f */\n public void pushLast(int num) {\n push(num, false);\n }\n\n /* \u51fa\u961f\u64cd\u4f5c */\n private int pop(boolean isFront) {\n if (isEmpty())\n throw new IndexOutOfBoundsException();\n int val;\n // \u961f\u9996\u51fa\u961f\u64cd\u4f5c\n if (isFront) {\n val = front.val; // \u6682\u5b58\u5934\u8282\u70b9\u503c\n // \u5220\u9664\u5934\u8282\u70b9\n ListNode fNext = front.next;\n if (fNext != null) {\n fNext.prev = null;\n front.next = null;\n }\n front = fNext; // \u66f4\u65b0\u5934\u8282\u70b9\n // \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\n } else {\n val = rear.val; // \u6682\u5b58\u5c3e\u8282\u70b9\u503c\n // \u5220\u9664\u5c3e\u8282\u70b9\n ListNode rPrev = rear.prev;\n if (rPrev != null) {\n rPrev.next = null;\n rear.prev = null;\n }\n rear = rPrev; // \u66f4\u65b0\u5c3e\u8282\u70b9\n }\n queSize--; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n return val;\n }\n\n /* \u961f\u9996\u51fa\u961f */\n public int popFirst() {\n return pop(true);\n }\n\n /* \u961f\u5c3e\u51fa\u961f */\n public int popLast() {\n return pop(false);\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n public int peekFirst() {\n if (isEmpty())\n throw new IndexOutOfBoundsException();\n return front.val;\n }\n\n /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n public int peekLast() {\n if (isEmpty())\n throw new IndexOutOfBoundsException();\n return rear.val;\n }\n\n /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n public int[] toArray() {\n ListNode node = front;\n int[] res = new int[size()];\n for (int i = 0; i < res.length; i++) {\n res[i] = node.val;\n node = node.next;\n }\n return res;\n }\n}\n
linkedlist_deque.cs/* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\nclass ListNode(int val) {\n public int val = val; // \u8282\u70b9\u503c\n public ListNode? next = null; // \u540e\u7ee7\u8282\u70b9\u5f15\u7528\n public ListNode? prev = null; // \u524d\u9a71\u8282\u70b9\u5f15\u7528\n}\n\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass LinkedListDeque {\n ListNode? front, rear; // \u5934\u8282\u70b9 front, \u5c3e\u8282\u70b9 rear\n int queSize = 0; // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\n\n public LinkedListDeque() {\n front = null;\n rear = null;\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n public int Size() {\n return queSize;\n }\n\n /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n public bool IsEmpty() {\n return Size() == 0;\n }\n\n /* \u5165\u961f\u64cd\u4f5c */\n void Push(int num, bool isFront) {\n ListNode node = new(num);\n // \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n if (IsEmpty()) {\n front = node;\n rear = node;\n }\n // \u961f\u9996\u5165\u961f\u64cd\u4f5c\n else if (isFront) {\n // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\n front!.prev = node;\n node.next = front;\n front = node; // \u66f4\u65b0\u5934\u8282\u70b9 \n }\n // \u961f\u5c3e\u5165\u961f\u64cd\u4f5c\n else {\n // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\n rear!.next = node;\n node.prev = rear;\n rear = node; // \u66f4\u65b0\u5c3e\u8282\u70b9\n }\n\n queSize++; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n }\n\n /* \u961f\u9996\u5165\u961f */\n public void PushFirst(int num) {\n Push(num, true);\n }\n\n /* \u961f\u5c3e\u5165\u961f */\n public void PushLast(int num) {\n Push(num, false);\n }\n\n /* \u51fa\u961f\u64cd\u4f5c */\n int? Pop(bool isFront) {\n if (IsEmpty())\n throw new Exception();\n int? val;\n // \u961f\u9996\u51fa\u961f\u64cd\u4f5c\n if (isFront) {\n val = front?.val; // \u6682\u5b58\u5934\u8282\u70b9\u503c\n // \u5220\u9664\u5934\u8282\u70b9\n ListNode? fNext = front?.next;\n if (fNext != null) {\n fNext.prev = null;\n front!.next = null;\n }\n front = fNext; // \u66f4\u65b0\u5934\u8282\u70b9\n }\n // \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\n else {\n val = rear?.val; // \u6682\u5b58\u5c3e\u8282\u70b9\u503c\n // \u5220\u9664\u5c3e\u8282\u70b9\n ListNode? rPrev = rear?.prev;\n if (rPrev != null) {\n rPrev.next = null;\n rear!.prev = null;\n }\n rear = rPrev; // \u66f4\u65b0\u5c3e\u8282\u70b9\n }\n\n queSize--; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n return val;\n }\n\n /* \u961f\u9996\u51fa\u961f */\n public int? PopFirst() {\n return Pop(true);\n }\n\n /* \u961f\u5c3e\u51fa\u961f */\n public int? PopLast() {\n return Pop(false);\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n public int? PeekFirst() {\n if (IsEmpty())\n throw new Exception();\n return front?.val;\n }\n\n /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n public int? PeekLast() {\n if (IsEmpty())\n throw new Exception();\n return rear?.val;\n }\n\n /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n public int?[] ToArray() {\n ListNode? node = front;\n int?[] res = new int?[Size()];\n for (int i = 0; i < res.Length; i++) {\n res[i] = node?.val;\n node = node?.next;\n }\n\n return res;\n }\n}\n
linkedlist_deque.go/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\ntype linkedListDeque struct {\n // \u4f7f\u7528\u5185\u7f6e\u5305 list\n data *list.List\n}\n\n/* \u521d\u59cb\u5316\u53cc\u7aef\u961f\u5217 */\nfunc newLinkedListDeque() *linkedListDeque {\n return &linkedListDeque{\n data: list.New(),\n }\n}\n\n/* \u961f\u9996\u5143\u7d20\u5165\u961f */\nfunc (s *linkedListDeque) pushFirst(value any) {\n s.data.PushFront(value)\n}\n\n/* \u961f\u5c3e\u5143\u7d20\u5165\u961f */\nfunc (s *linkedListDeque) pushLast(value any) {\n s.data.PushBack(value)\n}\n\n/* \u961f\u9996\u5143\u7d20\u51fa\u961f */\nfunc (s *linkedListDeque) popFirst() any {\n if s.isEmpty() {\n return nil\n }\n e := s.data.Front()\n s.data.Remove(e)\n return e.Value\n}\n\n/* \u961f\u5c3e\u5143\u7d20\u51fa\u961f */\nfunc (s *linkedListDeque) popLast() any {\n if s.isEmpty() {\n return nil\n }\n e := s.data.Back()\n s.data.Remove(e)\n return e.Value\n}\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nfunc (s *linkedListDeque) peekFirst() any {\n if s.isEmpty() {\n return nil\n }\n e := s.data.Front()\n return e.Value\n}\n\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\nfunc (s *linkedListDeque) peekLast() any {\n if s.isEmpty() {\n return nil\n }\n e := s.data.Back()\n return e.Value\n}\n\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nfunc (s *linkedListDeque) size() int {\n return s.data.Len()\n}\n\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nfunc (s *linkedListDeque) isEmpty() bool {\n return s.data.Len() == 0\n}\n\n/* \u83b7\u53d6 List \u7528\u4e8e\u6253\u5370 */\nfunc (s *linkedListDeque) toList() *list.List {\n return s.data\n}\n
linkedlist_deque.swift/* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\nclass ListNode {\n var val: Int // \u8282\u70b9\u503c\n var next: ListNode? // \u540e\u7ee7\u8282\u70b9\u5f15\u7528\n weak var prev: ListNode? // \u524d\u9a71\u8282\u70b9\u5f15\u7528\n\n init(val: Int) {\n self.val = val\n }\n}\n\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass LinkedListDeque {\n private var front: ListNode? // \u5934\u8282\u70b9 front\n private var rear: ListNode? // \u5c3e\u8282\u70b9 rear\n private var queSize: Int // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\n\n init() {\n queSize = 0\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n func size() -> Int {\n queSize\n }\n\n /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n func isEmpty() -> Bool {\n size() == 0\n }\n\n /* \u5165\u961f\u64cd\u4f5c */\n private func push(num: Int, isFront: Bool) {\n let node = ListNode(val: num)\n // \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n if isEmpty() {\n front = node\n rear = node\n }\n // \u961f\u9996\u5165\u961f\u64cd\u4f5c\n else if isFront {\n // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\n front?.prev = node\n node.next = front\n front = node // \u66f4\u65b0\u5934\u8282\u70b9\n }\n // \u961f\u5c3e\u5165\u961f\u64cd\u4f5c\n else {\n // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\n rear?.next = node\n node.prev = rear\n rear = node // \u66f4\u65b0\u5c3e\u8282\u70b9\n }\n queSize += 1 // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n }\n\n /* \u961f\u9996\u5165\u961f */\n func pushFirst(num: Int) {\n push(num: num, isFront: true)\n }\n\n /* \u961f\u5c3e\u5165\u961f */\n func pushLast(num: Int) {\n push(num: num, isFront: false)\n }\n\n /* \u51fa\u961f\u64cd\u4f5c */\n private func pop(isFront: Bool) -> Int {\n if isEmpty() {\n fatalError(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\n }\n let val: Int\n // \u961f\u9996\u51fa\u961f\u64cd\u4f5c\n if isFront {\n val = front!.val // \u6682\u5b58\u5934\u8282\u70b9\u503c\n // \u5220\u9664\u5934\u8282\u70b9\n let fNext = front?.next\n if fNext != nil {\n fNext?.prev = nil\n front?.next = nil\n }\n front = fNext // \u66f4\u65b0\u5934\u8282\u70b9\n }\n // \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\n else {\n val = rear!.val // \u6682\u5b58\u5c3e\u8282\u70b9\u503c\n // \u5220\u9664\u5c3e\u8282\u70b9\n let rPrev = rear?.prev\n if rPrev != nil {\n rPrev?.next = nil\n rear?.prev = nil\n }\n rear = rPrev // \u66f4\u65b0\u5c3e\u8282\u70b9\n }\n queSize -= 1 // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n return val\n }\n\n /* \u961f\u9996\u51fa\u961f */\n func popFirst() -> Int {\n pop(isFront: true)\n }\n\n /* \u961f\u5c3e\u51fa\u961f */\n func popLast() -> Int {\n pop(isFront: false)\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n func peekFirst() -> Int? {\n isEmpty() ? nil : front?.val\n }\n\n /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n func peekLast() -> Int? {\n isEmpty() ? nil : rear?.val\n }\n\n /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n func toArray() -> [Int] {\n var node = front\n var res = Array(repeating: 0, count: size())\n for i in res.indices {\n res[i] = node!.val\n node = node?.next\n }\n return res\n }\n}\n
linkedlist_deque.js/* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\nclass ListNode {\n prev; // \u524d\u9a71\u8282\u70b9\u5f15\u7528 (\u6307\u9488)\n next; // \u540e\u7ee7\u8282\u70b9\u5f15\u7528 (\u6307\u9488)\n val; // \u8282\u70b9\u503c\n\n constructor(val) {\n this.val = val;\n this.next = null;\n this.prev = null;\n }\n}\n\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass LinkedListDeque {\n #front; // \u5934\u8282\u70b9 front\n #rear; // \u5c3e\u8282\u70b9 rear\n #queSize; // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\n\n constructor() {\n this.#front = null;\n this.#rear = null;\n this.#queSize = 0;\n }\n\n /* \u961f\u5c3e\u5165\u961f\u64cd\u4f5c */\n pushLast(val) {\n const node = new ListNode(val);\n // \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n if (this.#queSize === 0) {\n this.#front = node;\n this.#rear = node;\n } else {\n // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\n this.#rear.next = node;\n node.prev = this.#rear;\n this.#rear = node; // \u66f4\u65b0\u5c3e\u8282\u70b9\n }\n this.#queSize++;\n }\n\n /* \u961f\u9996\u5165\u961f\u64cd\u4f5c */\n pushFirst(val) {\n const node = new ListNode(val);\n // \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n if (this.#queSize === 0) {\n this.#front = node;\n this.#rear = node;\n } else {\n // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\n this.#front.prev = node;\n node.next = this.#front;\n this.#front = node; // \u66f4\u65b0\u5934\u8282\u70b9\n }\n this.#queSize++;\n }\n\n /* \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c */\n popLast() {\n if (this.#queSize === 0) {\n return null;\n }\n const value = this.#rear.val; // \u5b58\u50a8\u5c3e\u8282\u70b9\u503c\n // \u5220\u9664\u5c3e\u8282\u70b9\n let temp = this.#rear.prev;\n if (temp !== null) {\n temp.next = null;\n this.#rear.prev = null;\n }\n this.#rear = temp; // \u66f4\u65b0\u5c3e\u8282\u70b9\n this.#queSize--;\n return value;\n }\n\n /* \u961f\u9996\u51fa\u961f\u64cd\u4f5c */\n popFirst() {\n if (this.#queSize === 0) {\n return null;\n }\n const value = this.#front.val; // \u5b58\u50a8\u5c3e\u8282\u70b9\u503c\n // \u5220\u9664\u5934\u8282\u70b9\n let temp = this.#front.next;\n if (temp !== null) {\n temp.prev = null;\n this.#front.next = null;\n }\n this.#front = temp; // \u66f4\u65b0\u5934\u8282\u70b9\n this.#queSize--;\n return value;\n }\n\n /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n peekLast() {\n return this.#queSize === 0 ? null : this.#rear.val;\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n peekFirst() {\n return this.#queSize === 0 ? null : this.#front.val;\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n size() {\n return this.#queSize;\n }\n\n /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n isEmpty() {\n return this.#queSize === 0;\n }\n\n /* \u6253\u5370\u53cc\u5411\u961f\u5217 */\n print() {\n const arr = [];\n let temp = this.#front;\n while (temp !== null) {\n arr.push(temp.val);\n temp = temp.next;\n }\n console.log('[' + arr.join(', ') + ']');\n }\n}\n
linkedlist_deque.ts/* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\nclass ListNode {\n prev: ListNode; // \u524d\u9a71\u8282\u70b9\u5f15\u7528 (\u6307\u9488)\n next: ListNode; // \u540e\u7ee7\u8282\u70b9\u5f15\u7528 (\u6307\u9488)\n val: number; // \u8282\u70b9\u503c\n\n constructor(val: number) {\n this.val = val;\n this.next = null;\n this.prev = null;\n }\n}\n\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass LinkedListDeque {\n private front: ListNode; // \u5934\u8282\u70b9 front\n private rear: ListNode; // \u5c3e\u8282\u70b9 rear\n private queSize: number; // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\n\n constructor() {\n this.front = null;\n this.rear = null;\n this.queSize = 0;\n }\n\n /* \u961f\u5c3e\u5165\u961f\u64cd\u4f5c */\n pushLast(val: number): void {\n const node: ListNode = new ListNode(val);\n // \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n if (this.queSize === 0) {\n this.front = node;\n this.rear = node;\n } else {\n // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\n this.rear.next = node;\n node.prev = this.rear;\n this.rear = node; // \u66f4\u65b0\u5c3e\u8282\u70b9\n }\n this.queSize++;\n }\n\n /* \u961f\u9996\u5165\u961f\u64cd\u4f5c */\n pushFirst(val: number): void {\n const node: ListNode = new ListNode(val);\n // \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n if (this.queSize === 0) {\n this.front = node;\n this.rear = node;\n } else {\n // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\n this.front.prev = node;\n node.next = this.front;\n this.front = node; // \u66f4\u65b0\u5934\u8282\u70b9\n }\n this.queSize++;\n }\n\n /* \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c */\n popLast(): number {\n if (this.queSize === 0) {\n return null;\n }\n const value: number = this.rear.val; // \u5b58\u50a8\u5c3e\u8282\u70b9\u503c\n // \u5220\u9664\u5c3e\u8282\u70b9\n let temp: ListNode = this.rear.prev;\n if (temp !== null) {\n temp.next = null;\n this.rear.prev = null;\n }\n this.rear = temp; // \u66f4\u65b0\u5c3e\u8282\u70b9\n this.queSize--;\n return value;\n }\n\n /* \u961f\u9996\u51fa\u961f\u64cd\u4f5c */\n popFirst(): number {\n if (this.queSize === 0) {\n return null;\n }\n const value: number = this.front.val; // \u5b58\u50a8\u5c3e\u8282\u70b9\u503c\n // \u5220\u9664\u5934\u8282\u70b9\n let temp: ListNode = this.front.next;\n if (temp !== null) {\n temp.prev = null;\n this.front.next = null;\n }\n this.front = temp; // \u66f4\u65b0\u5934\u8282\u70b9\n this.queSize--;\n return value;\n }\n\n /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n peekLast(): number {\n return this.queSize === 0 ? null : this.rear.val;\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n peekFirst(): number {\n return this.queSize === 0 ? null : this.front.val;\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n size(): number {\n return this.queSize;\n }\n\n /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n isEmpty(): boolean {\n return this.queSize === 0;\n }\n\n /* \u6253\u5370\u53cc\u5411\u961f\u5217 */\n print(): void {\n const arr: number[] = [];\n let temp: ListNode = this.front;\n while (temp !== null) {\n arr.push(temp.val);\n temp = temp.next;\n }\n console.log('[' + arr.join(', ') + ']');\n }\n}\n
linkedlist_deque.dart/* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\nclass ListNode {\n int val; // \u8282\u70b9\u503c\n ListNode? next; // \u540e\u7ee7\u8282\u70b9\u5f15\u7528\n ListNode? prev; // \u524d\u9a71\u8282\u70b9\u5f15\u7528\n\n ListNode(this.val, {this.next, this.prev});\n}\n\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u5bf9\u5217 */\nclass LinkedListDeque {\n late ListNode? _front; // \u5934\u8282\u70b9 _front\n late ListNode? _rear; // \u5c3e\u8282\u70b9 _rear\n int _queSize = 0; // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\n\n LinkedListDeque() {\n this._front = null;\n this._rear = null;\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u957f\u5ea6 */\n int size() {\n return this._queSize;\n }\n\n /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n bool isEmpty() {\n return size() == 0;\n }\n\n /* \u5165\u961f\u64cd\u4f5c */\n void push(int _num, bool isFront) {\n final ListNode node = ListNode(_num);\n if (isEmpty()) {\n // \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 _front \u548c _rear \u90fd\u6307\u5411 node\n _front = _rear = node;\n } else if (isFront) {\n // \u961f\u9996\u5165\u961f\u64cd\u4f5c\n // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\n _front!.prev = node;\n node.next = _front;\n _front = node; // \u66f4\u65b0\u5934\u8282\u70b9\n } else {\n // \u961f\u5c3e\u5165\u961f\u64cd\u4f5c\n // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\n _rear!.next = node;\n node.prev = _rear;\n _rear = node; // \u66f4\u65b0\u5c3e\u8282\u70b9\n }\n _queSize++; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n }\n\n /* \u961f\u9996\u5165\u961f */\n void pushFirst(int _num) {\n push(_num, true);\n }\n\n /* \u961f\u5c3e\u5165\u961f */\n void pushLast(int _num) {\n push(_num, false);\n }\n\n /* \u51fa\u961f\u64cd\u4f5c */\n int? pop(bool isFront) {\n // \u82e5\u961f\u5217\u4e3a\u7a7a\uff0c\u76f4\u63a5\u8fd4\u56de null\n if (isEmpty()) {\n return null;\n }\n final int val;\n if (isFront) {\n // \u961f\u9996\u51fa\u961f\u64cd\u4f5c\n val = _front!.val; // \u6682\u5b58\u5934\u8282\u70b9\u503c\n // \u5220\u9664\u5934\u8282\u70b9\n ListNode? fNext = _front!.next;\n if (fNext != null) {\n fNext.prev = null;\n _front!.next = null;\n }\n _front = fNext; // \u66f4\u65b0\u5934\u8282\u70b9\n } else {\n // \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\n val = _rear!.val; // \u6682\u5b58\u5c3e\u8282\u70b9\u503c\n // \u5220\u9664\u5c3e\u8282\u70b9\n ListNode? rPrev = _rear!.prev;\n if (rPrev != null) {\n rPrev.next = null;\n _rear!.prev = null;\n }\n _rear = rPrev; // \u66f4\u65b0\u5c3e\u8282\u70b9\n }\n _queSize--; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n return val;\n }\n\n /* \u961f\u9996\u51fa\u961f */\n int? popFirst() {\n return pop(true);\n }\n\n /* \u961f\u5c3e\u51fa\u961f */\n int? popLast() {\n return pop(false);\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n int? peekFirst() {\n return _front?.val;\n }\n\n /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n int? peekLast() {\n return _rear?.val;\n }\n\n /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n List<int> toArray() {\n ListNode? node = _front;\n final List<int> res = [];\n for (int i = 0; i < _queSize; i++) {\n res.add(node!.val);\n node = node.next;\n }\n return res;\n }\n}\n
linkedlist_deque.rs/* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\npub struct ListNode<T> {\n pub val: T, // \u8282\u70b9\u503c\n pub next: Option<Rc<RefCell<ListNode<T>>>>, // \u540e\u7ee7\u8282\u70b9\u6307\u9488\n pub prev: Option<Rc<RefCell<ListNode<T>>>>, // \u524d\u9a71\u8282\u70b9\u6307\u9488\n}\n\nimpl<T> ListNode<T> {\n pub fn new(val: T) -> Rc<RefCell<ListNode<T>>> {\n Rc::new(RefCell::new(ListNode {\n val,\n next: None,\n prev: None,\n }))\n }\n}\n\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\n#[allow(dead_code)]\npub struct LinkedListDeque<T> {\n front: Option<Rc<RefCell<ListNode<T>>>>, // \u5934\u8282\u70b9 front\n rear: Option<Rc<RefCell<ListNode<T>>>>, // \u5c3e\u8282\u70b9 rear \n que_size: usize, // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\n}\n\nimpl<T: Copy> LinkedListDeque<T> {\n pub fn new() -> Self {\n Self {\n front: None,\n rear: None,\n que_size: 0, \n }\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n pub fn size(&self) -> usize {\n return self.que_size;\n }\n\n /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n pub fn is_empty(&self) -> bool {\n return self.size() == 0;\n }\n\n /* \u5165\u961f\u64cd\u4f5c */\n pub fn push(&mut self, num: T, is_front: bool) {\n let node = ListNode::new(num);\n // \u961f\u9996\u5165\u961f\u64cd\u4f5c\n if is_front {\n match self.front.take() {\n // \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n None => {\n self.rear = Some(node.clone());\n self.front = Some(node);\n }\n // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\n Some(old_front) => {\n old_front.borrow_mut().prev = Some(node.clone());\n node.borrow_mut().next = Some(old_front);\n self.front = Some(node); // \u66f4\u65b0\u5934\u8282\u70b9\n }\n }\n } \n // \u961f\u5c3e\u5165\u961f\u64cd\u4f5c\n else {\n match self.rear.take() {\n // \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n None => {\n self.front = Some(node.clone());\n self.rear = Some(node);\n }\n // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\n Some(old_rear) => {\n old_rear.borrow_mut().next = Some(node.clone());\n node.borrow_mut().prev = Some(old_rear);\n self.rear = Some(node); // \u66f4\u65b0\u5c3e\u8282\u70b9\n }\n }\n }\n self.que_size += 1; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n }\n\n /* \u961f\u9996\u5165\u961f */\n pub fn push_first(&mut self, num: T) {\n self.push(num, true);\n }\n\n /* \u961f\u5c3e\u5165\u961f */\n pub fn push_last(&mut self, num: T) {\n self.push(num, false);\n }\n\n /* \u51fa\u961f\u64cd\u4f5c */\n pub fn pop(&mut self, is_front: bool) -> Option<T> {\n // \u82e5\u961f\u5217\u4e3a\u7a7a\uff0c\u76f4\u63a5\u8fd4\u56de None\n if self.is_empty() { \n return None \n };\n // \u961f\u9996\u51fa\u961f\u64cd\u4f5c\n if is_front {\n self.front.take().map(|old_front| {\n match old_front.borrow_mut().next.take() {\n Some(new_front) => {\n new_front.borrow_mut().prev.take();\n self.front = Some(new_front); // \u66f4\u65b0\u5934\u8282\u70b9\n }\n None => {\n self.rear.take();\n }\n }\n self.que_size -= 1; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n Rc::try_unwrap(old_front).ok().unwrap().into_inner().val\n })\n\n } \n // \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\n else {\n self.rear.take().map(|old_rear| {\n match old_rear.borrow_mut().prev.take() {\n Some(new_rear) => {\n new_rear.borrow_mut().next.take();\n self.rear = Some(new_rear); // \u66f4\u65b0\u5c3e\u8282\u70b9\n }\n None => {\n self.front.take();\n }\n }\n self.que_size -= 1; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n Rc::try_unwrap(old_rear).ok().unwrap().into_inner().val\n })\n }\n }\n\n /* \u961f\u9996\u51fa\u961f */\n pub fn pop_first(&mut self) -> Option<T> {\n return self.pop(true);\n }\n\n /* \u961f\u5c3e\u51fa\u961f */\n pub fn pop_last(&mut self) -> Option<T> {\n return self.pop(false);\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n pub fn peek_first(&self) -> Option<&Rc<RefCell<ListNode<T>>>> {\n self.front.as_ref()\n }\n\n /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n pub fn peek_last(&self) -> Option<&Rc<RefCell<ListNode<T>>>> {\n self.rear.as_ref()\n }\n\n /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n pub fn to_array(&self, head: Option<&Rc<RefCell<ListNode<T>>>>) -> Vec<T> {\n if let Some(node) = head {\n let mut nums = self.to_array(node.borrow().next.as_ref());\n nums.insert(0, node.borrow().val);\n return nums;\n }\n return Vec::new();\n }\n}\n
linkedlist_deque.c/* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\ntypedef struct DoublyListNode {\n int val; // \u8282\u70b9\u503c\n struct DoublyListNode *next; // \u540e\u7ee7\u8282\u70b9\n struct DoublyListNode *prev; // \u524d\u9a71\u8282\u70b9\n} DoublyListNode;\n\n/* \u6784\u9020\u51fd\u6570 */\nDoublyListNode *newDoublyListNode(int num) {\n DoublyListNode *new = (DoublyListNode *)malloc(sizeof(DoublyListNode));\n new->val = num;\n new->next = NULL;\n new->prev = NULL;\n return new;\n}\n\n/* \u6790\u6784\u51fd\u6570 */\nvoid delDoublyListNode(DoublyListNode *node) {\n free(node);\n}\n\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\ntypedef struct {\n DoublyListNode *front, *rear; // \u5934\u8282\u70b9 front \uff0c\u5c3e\u8282\u70b9 rear\n int queSize; // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\n} LinkedListDeque;\n\n/* \u6784\u9020\u51fd\u6570 */\nLinkedListDeque *newLinkedListDeque() {\n LinkedListDeque *deque = (LinkedListDeque *)malloc(sizeof(LinkedListDeque));\n deque->front = NULL;\n deque->rear = NULL;\n deque->queSize = 0;\n return deque;\n}\n\n/* \u6790\u6784\u51fd\u6570 */\nvoid delLinkedListdeque(LinkedListDeque *deque) {\n // \u91ca\u653e\u6240\u6709\u8282\u70b9\n for (int i = 0; i < deque->queSize && deque->front != NULL; i++) {\n DoublyListNode *tmp = deque->front;\n deque->front = deque->front->next;\n free(tmp);\n }\n // \u91ca\u653e deque \u7ed3\u6784\u4f53\n free(deque);\n}\n\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nint size(LinkedListDeque *deque) {\n return deque->queSize;\n}\n\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool empty(LinkedListDeque *deque) {\n return (size(deque) == 0);\n}\n\n/* \u5165\u961f */\nvoid push(LinkedListDeque *deque, int num, bool isFront) {\n DoublyListNode *node = newDoublyListNode(num);\n // \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front \u548c rear \u90fd\u6307\u5411node\n if (empty(deque)) {\n deque->front = deque->rear = node;\n }\n // \u961f\u9996\u5165\u961f\u64cd\u4f5c\n else if (isFront) {\n // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\n deque->front->prev = node;\n node->next = deque->front;\n deque->front = node; // \u66f4\u65b0\u5934\u8282\u70b9\n }\n // \u961f\u5c3e\u5165\u961f\u64cd\u4f5c\n else {\n // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\n deque->rear->next = node;\n node->prev = deque->rear;\n deque->rear = node;\n }\n deque->queSize++; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n}\n\n/* \u961f\u9996\u5165\u961f */\nvoid pushFirst(LinkedListDeque *deque, int num) {\n push(deque, num, true);\n}\n\n/* \u961f\u5c3e\u5165\u961f */\nvoid pushLast(LinkedListDeque *deque, int num) {\n push(deque, num, false);\n}\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint peekFirst(LinkedListDeque *deque) {\n assert(size(deque) && deque->front);\n return deque->front->val;\n}\n\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\nint peekLast(LinkedListDeque *deque) {\n assert(size(deque) && deque->rear);\n return deque->rear->val;\n}\n\n/* \u51fa\u961f */\nint pop(LinkedListDeque *deque, bool isFront) {\n if (empty(deque))\n return -1;\n int val;\n // \u961f\u9996\u51fa\u961f\u64cd\u4f5c\n if (isFront) {\n val = peekFirst(deque); // \u6682\u5b58\u5934\u8282\u70b9\u503c\n DoublyListNode *fNext = deque->front->next;\n if (fNext) {\n fNext->prev = NULL;\n deque->front->next = NULL;\n delDoublyListNode(deque->front);\n }\n deque->front = fNext; // \u66f4\u65b0\u5934\u8282\u70b9\n }\n // \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\n else {\n val = peekLast(deque); // \u6682\u5b58\u5c3e\u8282\u70b9\u503c\n DoublyListNode *rPrev = deque->rear->prev;\n if (rPrev) {\n rPrev->next = NULL;\n deque->rear->prev = NULL;\n delDoublyListNode(deque->rear);\n }\n deque->rear = rPrev; // \u66f4\u65b0\u5c3e\u8282\u70b9\n }\n deque->queSize--; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n return val;\n}\n\n/* \u961f\u9996\u51fa\u961f */\nint popFirst(LinkedListDeque *deque) {\n return pop(deque, true);\n}\n\n/* \u961f\u5c3e\u51fa\u961f */\nint popLast(LinkedListDeque *deque) {\n return pop(deque, false);\n}\n\n/* \u6253\u5370\u961f\u5217 */\nvoid printLinkedListDeque(LinkedListDeque *deque) {\n int *arr = malloc(sizeof(int) * deque->queSize);\n // \u62f7\u8d1d\u94fe\u8868\u4e2d\u7684\u6570\u636e\u5230\u6570\u7ec4\n int i;\n DoublyListNode *node;\n for (i = 0, node = deque->front; i < deque->queSize; i++) {\n arr[i] = node->val;\n node = node->next;\n }\n printArray(arr, deque->queSize);\n free(arr);\n}\n
linkedlist_deque.zig// \u53cc\u5411\u94fe\u8868\u8282\u70b9\nfn ListNode(comptime T: type) type {\n return struct {\n const Self = @This();\n\n val: T = undefined, // \u8282\u70b9\u503c\n next: ?*Self = null, // \u540e\u7ee7\u8282\u70b9\u6307\u9488\n prev: ?*Self = null, // \u524d\u9a71\u8282\u70b9\u6307\u9488\n\n // Initialize a list node with specific value\n pub fn init(self: *Self, x: i32) void {\n self.val = x;\n self.next = null;\n self.prev = null;\n }\n };\n}\n\n// \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217\nfn LinkedListDeque(comptime T: type) type {\n return struct {\n const Self = @This();\n\n front: ?*ListNode(T) = null, // \u5934\u8282\u70b9 front\n rear: ?*ListNode(T) = null, // \u5c3e\u8282\u70b9 rear\n que_size: usize = 0, // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\n mem_arena: ?std.heap.ArenaAllocator = null,\n mem_allocator: std.mem.Allocator = undefined, // \u5185\u5b58\u5206\u914d\u5668\n\n // \u6784\u9020\u51fd\u6570\uff08\u5206\u914d\u5185\u5b58+\u521d\u59cb\u5316\u961f\u5217\uff09\n pub fn init(self: *Self, allocator: std.mem.Allocator) !void {\n if (self.mem_arena == null) {\n self.mem_arena = std.heap.ArenaAllocator.init(allocator);\n self.mem_allocator = self.mem_arena.?.allocator();\n }\n self.front = null;\n self.rear = null;\n self.que_size = 0;\n }\n\n // \u6790\u6784\u51fd\u6570\uff08\u91ca\u653e\u5185\u5b58\uff09\n pub fn deinit(self: *Self) void {\n if (self.mem_arena == null) return;\n self.mem_arena.?.deinit();\n }\n\n // \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\n pub fn size(self: *Self) usize {\n return self.que_size;\n }\n\n // \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a\n pub fn isEmpty(self: *Self) bool {\n return self.size() == 0;\n }\n\n // \u5165\u961f\u64cd\u4f5c\n pub fn push(self: *Self, num: T, is_front: bool) !void {\n var node = try self.mem_allocator.create(ListNode(T));\n node.init(num);\n // \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n if (self.isEmpty()) {\n self.front = node;\n self.rear = node;\n // \u961f\u9996\u5165\u961f\u64cd\u4f5c\n } else if (is_front) {\n // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\n self.front.?.prev = node;\n node.next = self.front;\n self.front = node; // \u66f4\u65b0\u5934\u8282\u70b9\n // \u961f\u5c3e\u5165\u961f\u64cd\u4f5c\n } else {\n // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\n self.rear.?.next = node;\n node.prev = self.rear;\n self.rear = node; // \u66f4\u65b0\u5c3e\u8282\u70b9\n }\n self.que_size += 1; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n } \n\n // \u961f\u9996\u5165\u961f\n pub fn pushFirst(self: *Self, num: T) !void {\n try self.push(num, true);\n } \n\n // \u961f\u5c3e\u5165\u961f\n pub fn pushLast(self: *Self, num: T) !void {\n try self.push(num, false);\n } \n\n // \u51fa\u961f\u64cd\u4f5c\n pub fn pop(self: *Self, is_front: bool) T {\n if (self.isEmpty()) @panic(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\");\n var val: T = undefined;\n // \u961f\u9996\u51fa\u961f\u64cd\u4f5c\n if (is_front) {\n val = self.front.?.val; // \u6682\u5b58\u5934\u8282\u70b9\u503c\n // \u5220\u9664\u5934\u8282\u70b9\n var fNext = self.front.?.next;\n if (fNext != null) {\n fNext.?.prev = null;\n self.front.?.next = null;\n }\n self.front = fNext; // \u66f4\u65b0\u5934\u8282\u70b9\n // \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\n } else {\n val = self.rear.?.val; // \u6682\u5b58\u5c3e\u8282\u70b9\u503c\n // \u5220\u9664\u5c3e\u8282\u70b9\n var rPrev = self.rear.?.prev;\n if (rPrev != null) {\n rPrev.?.next = null;\n self.rear.?.prev = null;\n }\n self.rear = rPrev; // \u66f4\u65b0\u5c3e\u8282\u70b9\n }\n self.que_size -= 1; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n return val;\n } \n\n // \u961f\u9996\u51fa\u961f\n pub fn popFirst(self: *Self) T {\n return self.pop(true);\n } \n\n // \u961f\u5c3e\u51fa\u961f\n pub fn popLast(self: *Self) T {\n return self.pop(false);\n } \n\n // \u8bbf\u95ee\u961f\u9996\u5143\u7d20\n pub fn peekFirst(self: *Self) T {\n if (self.isEmpty()) @panic(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\");\n return self.front.?.val;\n } \n\n // \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20\n pub fn peekLast(self: *Self) T {\n if (self.isEmpty()) @panic(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\");\n return self.rear.?.val;\n }\n\n // \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370\n pub fn toArray(self: *Self) ![]T {\n var node = self.front;\n var res = try self.mem_allocator.alloc(T, self.size());\n @memset(res, @as(T, 0));\n var i: usize = 0;\n while (i < res.len) : (i += 1) {\n res[i] = node.?.val;\n node = node.?.next;\n }\n return res;\n }\n };\n}\n
"},{"location":"chapter_stack_and_queue/deque/#2","title":"2. \u00a0 \u57fa\u4e8e\u6570\u7ec4\u7684\u5b9e\u73b0","text":"\u5982\u56fe 5-9 \u6240\u793a\uff0c\u4e0e\u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u961f\u5217\u7c7b\u4f3c\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u4f7f\u7528\u73af\u5f62\u6570\u7ec4\u6765\u5b9e\u73b0\u53cc\u5411\u961f\u5217\u3002
ArrayDequepushLast()pushFirst()popLast()popFirst()\u56fe 5-9 \u00a0 \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u53cc\u5411\u961f\u5217\u7684\u5165\u961f\u51fa\u961f\u64cd\u4f5c
\u5728\u961f\u5217\u7684\u5b9e\u73b0\u57fa\u7840\u4e0a\uff0c\u4ec5\u9700\u589e\u52a0\u201c\u961f\u9996\u5165\u961f\u201d\u548c\u201c\u961f\u5c3e\u51fa\u961f\u201d\u7684\u65b9\u6cd5\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig array_deque.pyclass ArrayDeque:\n \"\"\"\u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217\"\"\"\n\n def __init__(self, capacity: int):\n \"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n self._nums: list[int] = [0] * capacity\n self._front: int = 0\n self._size: int = 0\n\n def capacity(self) -> int:\n \"\"\"\u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf\"\"\"\n return len(self._nums)\n\n def size(self) -> int:\n \"\"\"\u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\"\"\"\n return self._size\n\n def is_empty(self) -> bool:\n \"\"\"\u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a\"\"\"\n return self._size == 0\n\n def index(self, i: int) -> int:\n \"\"\"\u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15\"\"\"\n # \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n # \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\uff0c\u56de\u5230\u5934\u90e8\n # \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\n return (i + self.capacity()) % self.capacity()\n\n def push_first(self, num: int):\n \"\"\"\u961f\u9996\u5165\u961f\"\"\"\n if self._size == self.capacity():\n print(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\")\n return\n # \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n # \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\uff0c\u5b9e\u73b0 front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\u56de\u5230\u5c3e\u90e8\n self._front = self.index(self._front - 1)\n # \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\n self._nums[self._front] = num\n self._size += 1\n\n def push_last(self, num: int):\n \"\"\"\u961f\u5c3e\u5165\u961f\"\"\"\n if self._size == self.capacity():\n print(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\")\n return\n # \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n rear = self.index(self._front + self._size)\n # \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n self._nums[rear] = num\n self._size += 1\n\n def pop_first(self) -> int:\n \"\"\"\u961f\u9996\u51fa\u961f\"\"\"\n num = self.peek_first()\n # \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n self._front = self.index(self._front + 1)\n self._size -= 1\n return num\n\n def pop_last(self) -> int:\n \"\"\"\u961f\u5c3e\u51fa\u961f\"\"\"\n num = self.peek_last()\n self._size -= 1\n return num\n\n def peek_first(self) -> int:\n \"\"\"\u8bbf\u95ee\u961f\u9996\u5143\u7d20\"\"\"\n if self.is_empty():\n raise IndexError(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\n return self._nums[self._front]\n\n def peek_last(self) -> int:\n \"\"\"\u8bbf\u95ee\u961f\u5c3e\u5143\u7d20\"\"\"\n if self.is_empty():\n raise IndexError(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\n # \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\n last = self.index(self._front + self._size - 1)\n return self._nums[last]\n\n def to_array(self) -> list[int]:\n \"\"\"\u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370\"\"\"\n # \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n res = []\n for i in range(self._size):\n res.append(self._nums[self.index(self._front + i)])\n return res\n
array_deque.cpp/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass ArrayDeque {\n private:\n vector<int> nums; // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n int front; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n int queSize; // \u53cc\u5411\u961f\u5217\u957f\u5ea6\n\n public:\n /* \u6784\u9020\u65b9\u6cd5 */\n ArrayDeque(int capacity) {\n nums.resize(capacity);\n front = queSize = 0;\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\n int capacity() {\n return nums.size();\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n int size() {\n return queSize;\n }\n\n /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n bool isEmpty() {\n return queSize == 0;\n }\n\n /* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\n int index(int i) {\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\uff0c\u56de\u5230\u5934\u90e8\n // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\n return (i + capacity()) % capacity();\n }\n\n /* \u961f\u9996\u5165\u961f */\n void pushFirst(int num) {\n if (queSize == capacity()) {\n cout << \"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\" << endl;\n return;\n }\n // \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\uff0c\u5b9e\u73b0 front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\u56de\u5230\u5c3e\u90e8\n front = index(front - 1);\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\n nums[front] = num;\n queSize++;\n }\n\n /* \u961f\u5c3e\u5165\u961f */\n void pushLast(int num) {\n if (queSize == capacity()) {\n cout << \"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\" << endl;\n return;\n }\n // \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n int rear = index(front + queSize);\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n nums[rear] = num;\n queSize++;\n }\n\n /* \u961f\u9996\u51fa\u961f */\n int popFirst() {\n int num = peekFirst();\n // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n front = index(front + 1);\n queSize--;\n return num;\n }\n\n /* \u961f\u5c3e\u51fa\u961f */\n int popLast() {\n int num = peekLast();\n queSize--;\n return num;\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n int peekFirst() {\n if (isEmpty())\n throw out_of_range(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\");\n return nums[front];\n }\n\n /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n int peekLast() {\n if (isEmpty())\n throw out_of_range(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\");\n // \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\n int last = index(front + queSize - 1);\n return nums[last];\n }\n\n /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n vector<int> toVector() {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n vector<int> res(queSize);\n for (int i = 0, j = front; i < queSize; i++, j++) {\n res[i] = nums[index(j)];\n }\n return res;\n }\n};\n
array_deque.java/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass ArrayDeque {\n private int[] nums; // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n private int front; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n private int queSize; // \u53cc\u5411\u961f\u5217\u957f\u5ea6\n\n /* \u6784\u9020\u65b9\u6cd5 */\n public ArrayDeque(int capacity) {\n this.nums = new int[capacity];\n front = queSize = 0;\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\n public int capacity() {\n return nums.length;\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n public int size() {\n return queSize;\n }\n\n /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n public boolean isEmpty() {\n return queSize == 0;\n }\n\n /* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\n private int index(int i) {\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\uff0c\u56de\u5230\u5934\u90e8\n // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\n return (i + capacity()) % capacity();\n }\n\n /* \u961f\u9996\u5165\u961f */\n public void pushFirst(int num) {\n if (queSize == capacity()) {\n System.out.println(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\");\n return;\n }\n // \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\uff0c\u5b9e\u73b0 front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\u56de\u5230\u5c3e\u90e8\n front = index(front - 1);\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\n nums[front] = num;\n queSize++;\n }\n\n /* \u961f\u5c3e\u5165\u961f */\n public void pushLast(int num) {\n if (queSize == capacity()) {\n System.out.println(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\");\n return;\n }\n // \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n int rear = index(front + queSize);\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n nums[rear] = num;\n queSize++;\n }\n\n /* \u961f\u9996\u51fa\u961f */\n public int popFirst() {\n int num = peekFirst();\n // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n front = index(front + 1);\n queSize--;\n return num;\n }\n\n /* \u961f\u5c3e\u51fa\u961f */\n public int popLast() {\n int num = peekLast();\n queSize--;\n return num;\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n public int peekFirst() {\n if (isEmpty())\n throw new IndexOutOfBoundsException();\n return nums[front];\n }\n\n /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n public int peekLast() {\n if (isEmpty())\n throw new IndexOutOfBoundsException();\n // \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\n int last = index(front + queSize - 1);\n return nums[last];\n }\n\n /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n public int[] toArray() {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n int[] res = new int[queSize];\n for (int i = 0, j = front; i < queSize; i++, j++) {\n res[i] = nums[index(j)];\n }\n return res;\n }\n}\n
array_deque.cs/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass ArrayDeque {\n int[] nums; // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n int front; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n int queSize; // \u53cc\u5411\u961f\u5217\u957f\u5ea6\n\n /* \u6784\u9020\u65b9\u6cd5 */\n public ArrayDeque(int capacity) {\n nums = new int[capacity];\n front = queSize = 0;\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\n int Capacity() {\n return nums.Length;\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n public int Size() {\n return queSize;\n }\n\n /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n public bool IsEmpty() {\n return queSize == 0;\n }\n\n /* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\n int Index(int i) {\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\uff0c\u56de\u5230\u5934\u90e8\n // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\n return (i + Capacity()) % Capacity();\n }\n\n /* \u961f\u9996\u5165\u961f */\n public void PushFirst(int num) {\n if (queSize == Capacity()) {\n Console.WriteLine(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\");\n return;\n }\n // \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\uff0c\u5b9e\u73b0 front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\u56de\u5230\u5c3e\u90e8\n front = Index(front - 1);\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\n nums[front] = num;\n queSize++;\n }\n\n /* \u961f\u5c3e\u5165\u961f */\n public void PushLast(int num) {\n if (queSize == Capacity()) {\n Console.WriteLine(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\");\n return;\n }\n // \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n int rear = Index(front + queSize);\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n nums[rear] = num;\n queSize++;\n }\n\n /* \u961f\u9996\u51fa\u961f */\n public int PopFirst() {\n int num = PeekFirst();\n // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n front = Index(front + 1);\n queSize--;\n return num;\n }\n\n /* \u961f\u5c3e\u51fa\u961f */\n public int PopLast() {\n int num = PeekLast();\n queSize--;\n return num;\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n public int PeekFirst() {\n if (IsEmpty()) {\n throw new InvalidOperationException();\n }\n return nums[front];\n }\n\n /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n public int PeekLast() {\n if (IsEmpty()) {\n throw new InvalidOperationException();\n }\n // \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\n int last = Index(front + queSize - 1);\n return nums[last];\n }\n\n /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n public int[] ToArray() {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n int[] res = new int[queSize];\n for (int i = 0, j = front; i < queSize; i++, j++) {\n res[i] = nums[Index(j)];\n }\n return res;\n }\n}\n
array_deque.go/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\ntype arrayDeque struct {\n nums []int // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n front int // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n queSize int // \u53cc\u5411\u961f\u5217\u957f\u5ea6\n queCapacity int // \u961f\u5217\u5bb9\u91cf\uff08\u5373\u6700\u5927\u5bb9\u7eb3\u5143\u7d20\u6570\u91cf\uff09\n}\n\n/* \u521d\u59cb\u5316\u961f\u5217 */\nfunc newArrayDeque(queCapacity int) *arrayDeque {\n return &arrayDeque{\n nums: make([]int, queCapacity),\n queCapacity: queCapacity,\n front: 0,\n queSize: 0,\n }\n}\n\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nfunc (q *arrayDeque) size() int {\n return q.queSize\n}\n\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nfunc (q *arrayDeque) isEmpty() bool {\n return q.queSize == 0\n}\n\n/* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\nfunc (q *arrayDeque) index(i int) int {\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\uff0c\u56de\u5230\u5934\u90e8\n // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\n return (i + q.queCapacity) % q.queCapacity\n}\n\n/* \u961f\u9996\u5165\u961f */\nfunc (q *arrayDeque) pushFirst(num int) {\n if q.queSize == q.queCapacity {\n fmt.Println(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\")\n return\n }\n // \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\uff0c\u5b9e\u73b0 front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\u56de\u5230\u5c3e\u90e8\n q.front = q.index(q.front - 1)\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\n q.nums[q.front] = num\n q.queSize++\n}\n\n/* \u961f\u5c3e\u5165\u961f */\nfunc (q *arrayDeque) pushLast(num int) {\n if q.queSize == q.queCapacity {\n fmt.Println(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\")\n return\n }\n // \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n rear := q.index(q.front + q.queSize)\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\n q.nums[rear] = num\n q.queSize++\n}\n\n/* \u961f\u9996\u51fa\u961f */\nfunc (q *arrayDeque) popFirst() any {\n num := q.peekFirst()\n // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n q.front = q.index(q.front + 1)\n q.queSize--\n return num\n}\n\n/* \u961f\u5c3e\u51fa\u961f */\nfunc (q *arrayDeque) popLast() any {\n num := q.peekLast()\n q.queSize--\n return num\n}\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nfunc (q *arrayDeque) peekFirst() any {\n if q.isEmpty() {\n return nil\n }\n return q.nums[q.front]\n}\n\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\nfunc (q *arrayDeque) peekLast() any {\n if q.isEmpty() {\n return nil\n }\n // \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\n last := q.index(q.front + q.queSize - 1)\n return q.nums[last]\n}\n\n/* \u83b7\u53d6 Slice \u7528\u4e8e\u6253\u5370 */\nfunc (q *arrayDeque) toSlice() []int {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n res := make([]int, q.queSize)\n for i, j := 0, q.front; i < q.queSize; i++ {\n res[i] = q.nums[q.index(j)]\n j++\n }\n return res\n}\n
array_deque.swift/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass ArrayDeque {\n private var nums: [Int] // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n private var front: Int // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n private var queSize: Int // \u53cc\u5411\u961f\u5217\u957f\u5ea6\n\n /* \u6784\u9020\u65b9\u6cd5 */\n init(capacity: Int) {\n nums = Array(repeating: 0, count: capacity)\n front = 0\n queSize = 0\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\n func capacity() -> Int {\n nums.count\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n func size() -> Int {\n queSize\n }\n\n /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n func isEmpty() -> Bool {\n size() == 0\n }\n\n /* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\n private func index(i: Int) -> Int {\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\uff0c\u56de\u5230\u5934\u90e8\n // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\n (i + capacity()) % capacity()\n }\n\n /* \u961f\u9996\u5165\u961f */\n func pushFirst(num: Int) {\n if size() == capacity() {\n print(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\")\n return\n }\n // \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\uff0c\u5b9e\u73b0 front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\u56de\u5230\u5c3e\u90e8\n front = index(i: front - 1)\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\n nums[front] = num\n queSize += 1\n }\n\n /* \u961f\u5c3e\u5165\u961f */\n func pushLast(num: Int) {\n if size() == capacity() {\n print(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\")\n return\n }\n // \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n let rear = index(i: front + size())\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n nums[rear] = num\n queSize += 1\n }\n\n /* \u961f\u9996\u51fa\u961f */\n func popFirst() -> Int {\n let num = peekFirst()\n // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n front = index(i: front + 1)\n queSize -= 1\n return num\n }\n\n /* \u961f\u5c3e\u51fa\u961f */\n func popLast() -> Int {\n let num = peekLast()\n queSize -= 1\n return num\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n func peekFirst() -> Int {\n if isEmpty() {\n fatalError(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\n }\n return nums[front]\n }\n\n /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n func peekLast() -> Int {\n if isEmpty() {\n fatalError(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\n }\n // \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\n let last = index(i: front + size() - 1)\n return nums[last]\n }\n\n /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n func toArray() -> [Int] {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n var res = Array(repeating: 0, count: size())\n for (i, j) in sequence(first: (0, front), next: { $0 < self.size() - 1 ? ($0 + 1, $1 + 1) : nil }) {\n res[i] = nums[index(i: j)]\n }\n return res\n }\n}\n
array_deque.js/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass ArrayDeque {\n #nums; // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n #front; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n #queSize; // \u53cc\u5411\u961f\u5217\u957f\u5ea6\n\n /* \u6784\u9020\u65b9\u6cd5 */\n constructor(capacity) {\n this.#nums = new Array(capacity);\n this.#front = 0;\n this.#queSize = 0;\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\n capacity() {\n return this.#nums.length;\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n size() {\n return this.#queSize;\n }\n\n /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n isEmpty() {\n return this.#queSize === 0;\n }\n\n /* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\n index(i) {\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\uff0c\u56de\u5230\u5934\u90e8\n // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\n return (i + this.capacity()) % this.capacity();\n }\n\n /* \u961f\u9996\u5165\u961f */\n pushFirst(num) {\n if (this.#queSize === this.capacity()) {\n console.log('\u53cc\u5411\u961f\u5217\u5df2\u6ee1');\n return;\n }\n // \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\uff0c\u5b9e\u73b0 front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\u56de\u5230\u5c3e\u90e8\n this.#front = this.index(this.#front - 1);\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\n this.#nums[this.#front] = num;\n this.#queSize++;\n }\n\n /* \u961f\u5c3e\u5165\u961f */\n pushLast(num) {\n if (this.#queSize === this.capacity()) {\n console.log('\u53cc\u5411\u961f\u5217\u5df2\u6ee1');\n return;\n }\n // \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n const rear = this.index(this.#front + this.#queSize);\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n this.#nums[rear] = num;\n this.#queSize++;\n }\n\n /* \u961f\u9996\u51fa\u961f */\n popFirst() {\n const num = this.peekFirst();\n // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n this.#front = this.index(this.#front + 1);\n this.#queSize--;\n return num;\n }\n\n /* \u961f\u5c3e\u51fa\u961f */\n popLast() {\n const num = this.peekLast();\n this.#queSize--;\n return num;\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n peekFirst() {\n if (this.isEmpty()) throw new Error('The Deque Is Empty.');\n return this.#nums[this.#front];\n }\n\n /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n peekLast() {\n if (this.isEmpty()) throw new Error('The Deque Is Empty.');\n // \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\n const last = this.index(this.#front + this.#queSize - 1);\n return this.#nums[last];\n }\n\n /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n toArray() {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n const res = [];\n for (let i = 0, j = this.#front; i < this.#queSize; i++, j++) {\n res[i] = this.#nums[this.index(j)];\n }\n return res;\n }\n}\n
array_deque.ts/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass ArrayDeque {\n private nums: number[]; // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n private front: number; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n private queSize: number; // \u53cc\u5411\u961f\u5217\u957f\u5ea6\n\n /* \u6784\u9020\u65b9\u6cd5 */\n constructor(capacity: number) {\n this.nums = new Array(capacity);\n this.front = 0;\n this.queSize = 0;\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\n capacity(): number {\n return this.nums.length;\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n size(): number {\n return this.queSize;\n }\n\n /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n isEmpty(): boolean {\n return this.queSize === 0;\n }\n\n /* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\n index(i: number): number {\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\uff0c\u56de\u5230\u5934\u90e8\n // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\n return (i + this.capacity()) % this.capacity();\n }\n\n /* \u961f\u9996\u5165\u961f */\n pushFirst(num: number): void {\n if (this.queSize === this.capacity()) {\n console.log('\u53cc\u5411\u961f\u5217\u5df2\u6ee1');\n return;\n }\n // \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\uff0c\u5b9e\u73b0 front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\u56de\u5230\u5c3e\u90e8\n this.front = this.index(this.front - 1);\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\n this.nums[this.front] = num;\n this.queSize++;\n }\n\n /* \u961f\u5c3e\u5165\u961f */\n pushLast(num: number): void {\n if (this.queSize === this.capacity()) {\n console.log('\u53cc\u5411\u961f\u5217\u5df2\u6ee1');\n return;\n }\n // \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n const rear: number = this.index(this.front + this.queSize);\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n this.nums[rear] = num;\n this.queSize++;\n }\n\n /* \u961f\u9996\u51fa\u961f */\n popFirst(): number {\n const num: number = this.peekFirst();\n // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n this.front = this.index(this.front + 1);\n this.queSize--;\n return num;\n }\n\n /* \u961f\u5c3e\u51fa\u961f */\n popLast(): number {\n const num: number = this.peekLast();\n this.queSize--;\n return num;\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n peekFirst(): number {\n if (this.isEmpty()) throw new Error('The Deque Is Empty.');\n return this.nums[this.front];\n }\n\n /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n peekLast(): number {\n if (this.isEmpty()) throw new Error('The Deque Is Empty.');\n // \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\n const last = this.index(this.front + this.queSize - 1);\n return this.nums[last];\n }\n\n /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n toArray(): number[] {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n const res: number[] = [];\n for (let i = 0, j = this.front; i < this.queSize; i++, j++) {\n res[i] = this.nums[this.index(j)];\n }\n return res;\n }\n}\n
array_deque.dart/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass ArrayDeque {\n late List<int> _nums; // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n late int _front; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n late int _queSize; // \u53cc\u5411\u961f\u5217\u957f\u5ea6\n\n /* \u6784\u9020\u65b9\u6cd5 */\n ArrayDeque(int capacity) {\n this._nums = List.filled(capacity, 0);\n this._front = this._queSize = 0;\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\n int capacity() {\n return _nums.length;\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n int size() {\n return _queSize;\n }\n\n /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n bool isEmpty() {\n return _queSize == 0;\n }\n\n /* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\n int index(int i) {\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\uff0c\u56de\u5230\u5934\u90e8\n // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\n return (i + capacity()) % capacity();\n }\n\n /* \u961f\u9996\u5165\u961f */\n void pushFirst(int _num) {\n if (_queSize == capacity()) {\n throw Exception(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\");\n }\n // \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\uff0c\u5b9e\u73b0 _front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\u56de\u5230\u5c3e\u90e8\n _front = index(_front - 1);\n // \u5c06 _num \u6dfb\u52a0\u81f3\u961f\u9996\n _nums[_front] = _num;\n _queSize++;\n }\n\n /* \u961f\u5c3e\u5165\u961f */\n void pushLast(int _num) {\n if (_queSize == capacity()) {\n throw Exception(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\");\n }\n // \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n int rear = index(_front + _queSize);\n // \u5c06 _num \u6dfb\u52a0\u81f3\u961f\u5c3e\n _nums[rear] = _num;\n _queSize++;\n }\n\n /* \u961f\u9996\u51fa\u961f */\n int popFirst() {\n int _num = peekFirst();\n // \u961f\u9996\u6307\u9488\u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\n _front = index(_front + 1);\n _queSize--;\n return _num;\n }\n\n /* \u961f\u5c3e\u51fa\u961f */\n int popLast() {\n int _num = peekLast();\n _queSize--;\n return _num;\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n int peekFirst() {\n if (isEmpty()) {\n throw Exception(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\");\n }\n return _nums[_front];\n }\n\n /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n int peekLast() {\n if (isEmpty()) {\n throw Exception(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\");\n }\n // \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\n int last = index(_front + _queSize - 1);\n return _nums[last];\n }\n\n /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n List<int> toArray() {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n List<int> res = List.filled(_queSize, 0);\n for (int i = 0, j = _front; i < _queSize; i++, j++) {\n res[i] = _nums[index(j)];\n }\n return res;\n }\n}\n
array_deque.rs/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nstruct ArrayDeque {\n nums: Vec<i32>, // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n front: usize, // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n que_size: usize, // \u53cc\u5411\u961f\u5217\u957f\u5ea6\n}\n\nimpl ArrayDeque {\n /* \u6784\u9020\u65b9\u6cd5 */\n pub fn new(capacity: usize) -> Self {\n Self {\n nums: vec![0; capacity],\n front: 0,\n que_size: 0,\n }\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\n pub fn capacity(&self) -> usize {\n self.nums.len()\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n pub fn size(&self) -> usize {\n self.que_size\n }\n\n /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n pub fn is_empty(&self) -> bool {\n self.que_size == 0\n }\n\n /* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\n fn index(&self, i: i32) -> usize {\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\uff0c\u56de\u5230\u5934\u90e8\n // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\n return ((i + self.capacity() as i32) % self.capacity() as i32) as usize;\n }\n\n /* \u961f\u9996\u5165\u961f */\n pub fn push_first(&mut self, num: i32) {\n if self.que_size == self.capacity() {\n println!(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\");\n return\n }\n // \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\uff0c\u5b9e\u73b0 front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\u56de\u5230\u5c3e\u90e8\n self.front = self.index(self.front as i32 - 1);\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\n self.nums[self.front] = num;\n self.que_size += 1;\n }\n\n /* \u961f\u5c3e\u5165\u961f */\n pub fn push_last(&mut self, num: i32) {\n if self.que_size == self.capacity() {\n println!(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\");\n return\n }\n // \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n let rear = self.index(self.front as i32 + self.que_size as i32);\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n self.nums[rear] = num;\n self.que_size += 1;\n }\n\n /* \u961f\u9996\u51fa\u961f */\n fn pop_first(&mut self) -> i32 {\n let num = self.peek_first();\n // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n self.front = self.index(self.front as i32 + 1);\n self.que_size -= 1;\n num\n }\n\n /* \u961f\u5c3e\u51fa\u961f */\n fn pop_last(&mut self) -> i32 {\n let num = self.peek_last();\n self.que_size -= 1;\n num\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n fn peek_first(&self) -> i32 {\n if self.is_empty() { panic!(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\") };\n self.nums[self.front]\n }\n\n /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n fn peek_last(&self) -> i32 {\n if self.is_empty() { panic!(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\") };\n // \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\n let last = self.index(self.front as i32 + self.que_size as i32 - 1);\n self.nums[last]\n }\n\n /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n fn to_array(&self) -> Vec<i32> {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n let mut res = vec![0; self.que_size];\n let mut j = self.front;\n for i in 0..self.que_size {\n res[i] = self.nums[self.index(j as i32)];\n j += 1;\n }\n res\n }\n}\n
array_deque.c/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\ntypedef struct {\n int *nums; // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n int front; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n int queSize; // \u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e + 1\n int queCapacity; // \u961f\u5217\u5bb9\u91cf\n} ArrayDeque;\n\n/* \u6784\u9020\u51fd\u6570 */\nArrayDeque *newArrayDeque(int capacity) {\n ArrayDeque *deque = (ArrayDeque *)malloc(sizeof(ArrayDeque));\n // \u521d\u59cb\u5316\u6570\u7ec4\n deque->queCapacity = capacity;\n deque->nums = (int *)malloc(sizeof(int) * deque->queCapacity);\n deque->front = deque->queSize = 0;\n return deque;\n}\n\n/* \u6790\u6784\u51fd\u6570 */\nvoid delArrayDeque(ArrayDeque *deque) {\n free(deque->nums);\n free(deque);\n}\n\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\nint capacity(ArrayDeque *deque) {\n return deque->queCapacity;\n}\n\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nint size(ArrayDeque *deque) {\n return deque->queSize;\n}\n\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool empty(ArrayDeque *deque) {\n return deque->queSize == 0;\n}\n\n/* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\nint dequeIndex(ArrayDeque *deque, int i) {\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u56de\u5230\u5934\u90e8\n // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\n return ((i + capacity(deque)) % capacity(deque));\n}\n\n/* \u961f\u9996\u5165\u961f */\nvoid pushFirst(ArrayDeque *deque, int num) {\n if (deque->queSize == capacity(deque)) {\n printf(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\\r\\n\");\n return;\n }\n // \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\uff0c\u5b9e\u73b0 front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u56de\u5230\u5c3e\u90e8\n deque->front = dequeIndex(deque, deque->front - 1);\n // \u5c06 num \u6dfb\u52a0\u5230\u961f\u9996\n deque->nums[deque->front] = num;\n deque->queSize++;\n}\n\n/* \u961f\u5c3e\u5165\u961f */\nvoid pushLast(ArrayDeque *deque, int num) {\n if (deque->queSize == capacity(deque)) {\n printf(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\\r\\n\");\n return;\n }\n // \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n int rear = dequeIndex(deque, deque->front + deque->queSize);\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n deque->nums[rear] = num;\n deque->queSize++;\n}\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint peekFirst(ArrayDeque *deque) {\n // \u8bbf\u95ee\u5f02\u5e38\uff1a\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\n assert(empty(deque) == 0);\n return deque->nums[deque->front];\n}\n\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\nint peekLast(ArrayDeque *deque) {\n // \u8bbf\u95ee\u5f02\u5e38\uff1a\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\n assert(empty(deque) == 0);\n int last = dequeIndex(deque, deque->front + deque->queSize - 1);\n return deque->nums[last];\n}\n\n/* \u961f\u9996\u51fa\u961f */\nint popFirst(ArrayDeque *deque) {\n int num = peekFirst(deque);\n // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n deque->front = dequeIndex(deque, deque->front + 1);\n deque->queSize--;\n return num;\n}\n\n/* \u961f\u5c3e\u51fa\u961f */\nint popLast(ArrayDeque *deque) {\n int num = peekLast(deque);\n deque->queSize--;\n return num;\n}\n
array_deque.zig[class]{ArrayDeque}-[func]{}\n
"},{"location":"chapter_stack_and_queue/deque/#533","title":"5.3.3 \u00a0 \u53cc\u5411\u961f\u5217\u5e94\u7528","text":"\u53cc\u5411\u961f\u5217\u517c\u5177\u6808\u4e0e\u961f\u5217\u7684\u903b\u8f91\uff0c\u56e0\u6b64\u5b83\u53ef\u4ee5\u5b9e\u73b0\u8fd9\u4e24\u8005\u7684\u6240\u6709\u5e94\u7528\u573a\u666f\uff0c\u540c\u65f6\u63d0\u4f9b\u66f4\u9ad8\u7684\u81ea\u7531\u5ea6\u3002
\u6211\u4eec\u77e5\u9053\uff0c\u8f6f\u4ef6\u7684\u201c\u64a4\u9500\u201d\u529f\u80fd\u901a\u5e38\u4f7f\u7528\u6808\u6765\u5b9e\u73b0\uff1a\u7cfb\u7edf\u5c06\u6bcf\u6b21\u66f4\u6539\u64cd\u4f5c push
\u5230\u6808\u4e2d\uff0c\u7136\u540e\u901a\u8fc7 pop
\u5b9e\u73b0\u64a4\u9500\u3002\u7136\u800c\uff0c\u8003\u8651\u5230\u7cfb\u7edf\u8d44\u6e90\u7684\u9650\u5236\uff0c\u8f6f\u4ef6\u901a\u5e38\u4f1a\u9650\u5236\u64a4\u9500\u7684\u6b65\u6570\uff08\u4f8b\u5982\u4ec5\u5141\u8bb8\u4fdd\u5b58 \\(50\\) \u6b65\uff09\u3002\u5f53\u6808\u7684\u957f\u5ea6\u8d85\u8fc7 \\(50\\) \u65f6\uff0c\u8f6f\u4ef6\u9700\u8981\u5728\u6808\u5e95\uff08\u961f\u9996\uff09\u6267\u884c\u5220\u9664\u64cd\u4f5c\u3002\u4f46\u6808\u65e0\u6cd5\u5b9e\u73b0\u8be5\u529f\u80fd\uff0c\u6b64\u65f6\u5c31\u9700\u8981\u4f7f\u7528\u53cc\u5411\u961f\u5217\u6765\u66ff\u4ee3\u6808\u3002\u8bf7\u6ce8\u610f\uff0c\u201c\u64a4\u9500\u201d\u7684\u6838\u5fc3\u903b\u8f91\u4ecd\u7136\u9075\u5faa\u6808\u7684\u5148\u5165\u540e\u51fa\u539f\u5219\uff0c\u53ea\u662f\u53cc\u5411\u961f\u5217\u80fd\u591f\u66f4\u52a0\u7075\u6d3b\u5730\u5b9e\u73b0\u4e00\u4e9b\u989d\u5916\u903b\u8f91\u3002
\u300c\u961f\u5217 queue\u300d\u662f\u4e00\u79cd\u9075\u5faa\u5148\u5165\u5148\u51fa\u89c4\u5219\u7684\u7ebf\u6027\u6570\u636e\u7ed3\u6784\u3002\u987e\u540d\u601d\u4e49\uff0c\u961f\u5217\u6a21\u62df\u4e86\u6392\u961f\u73b0\u8c61\uff0c\u5373\u65b0\u6765\u7684\u4eba\u4e0d\u65ad\u52a0\u5165\u961f\u5217\u5c3e\u90e8\uff0c\u800c\u4f4d\u4e8e\u961f\u5217\u5934\u90e8\u7684\u4eba\u9010\u4e2a\u79bb\u5f00\u3002
\u5982\u56fe 5-4 \u6240\u793a\uff0c\u6211\u4eec\u5c06\u961f\u5217\u5934\u90e8\u79f0\u4e3a\u201c\u961f\u9996\u201d\uff0c\u5c3e\u90e8\u79f0\u4e3a\u201c\u961f\u5c3e\u201d\uff0c\u5c06\u628a\u5143\u7d20\u52a0\u5165\u961f\u5c3e\u7684\u64cd\u4f5c\u79f0\u4e3a\u201c\u5165\u961f\u201d\uff0c\u5220\u9664\u961f\u9996\u5143\u7d20\u7684\u64cd\u4f5c\u79f0\u4e3a\u201c\u51fa\u961f\u201d\u3002
\u56fe 5-4 \u00a0 \u961f\u5217\u7684\u5148\u5165\u5148\u51fa\u89c4\u5219
"},{"location":"chapter_stack_and_queue/queue/#521","title":"5.2.1 \u00a0 \u961f\u5217\u5e38\u7528\u64cd\u4f5c","text":"\u961f\u5217\u7684\u5e38\u89c1\u64cd\u4f5c\u5982\u8868 5-2 \u6240\u793a\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u4e0d\u540c\u7f16\u7a0b\u8bed\u8a00\u7684\u65b9\u6cd5\u540d\u79f0\u53ef\u80fd\u4f1a\u6709\u6240\u4e0d\u540c\u3002\u6211\u4eec\u5728\u6b64\u91c7\u7528\u4e0e\u6808\u76f8\u540c\u7684\u65b9\u6cd5\u547d\u540d\u3002
\u8868 5-2 \u00a0 \u961f\u5217\u64cd\u4f5c\u6548\u7387
\u65b9\u6cd5\u540d \u63cf\u8ff0 \u65f6\u95f4\u590d\u6742\u5ea6 push() \u5143\u7d20\u5165\u961f\uff0c\u5373\u5c06\u5143\u7d20\u6dfb\u52a0\u81f3\u961f\u5c3e \\(O(1)\\) pop() \u961f\u9996\u5143\u7d20\u51fa\u961f \\(O(1)\\) peek() \u8bbf\u95ee\u961f\u9996\u5143\u7d20 \\(O(1)\\)\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u7f16\u7a0b\u8bed\u8a00\u4e2d\u73b0\u6210\u7684\u961f\u5217\u7c7b\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig queue.pyfrom collections import deque\n\n# \u521d\u59cb\u5316\u961f\u5217\n# \u5728 Python \u4e2d\uff0c\u6211\u4eec\u4e00\u822c\u5c06\u53cc\u5411\u961f\u5217\u7c7b deque \u5f53\u4f5c\u961f\u5217\u4f7f\u7528\n# \u867d\u7136 queue.Queue() \u662f\u7eaf\u6b63\u7684\u961f\u5217\u7c7b\uff0c\u4f46\u4e0d\u592a\u597d\u7528\uff0c\u56e0\u6b64\u4e0d\u63a8\u8350\nque: deque[int] = deque()\n\n# \u5143\u7d20\u5165\u961f\nque.append(1)\nque.append(3)\nque.append(2)\nque.append(5)\nque.append(4)\n\n# \u8bbf\u95ee\u961f\u9996\u5143\u7d20\nfront: int = que[0];\n\n# \u5143\u7d20\u51fa\u961f\npop: int = que.popleft()\n\n# \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6\nsize: int = len(que)\n\n# \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a\nis_empty: bool = len(que) == 0\n
queue.cpp/* \u521d\u59cb\u5316\u961f\u5217 */\nqueue<int> queue;\n\n/* \u5143\u7d20\u5165\u961f */\nqueue.push(1);\nqueue.push(3);\nqueue.push(2);\nqueue.push(5);\nqueue.push(4);\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint front = queue.front();\n\n/* \u5143\u7d20\u51fa\u961f */\nqueue.pop();\n\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nint size = queue.size();\n\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool empty = queue.empty();\n
queue.java/* \u521d\u59cb\u5316\u961f\u5217 */\nQueue<Integer> queue = new LinkedList<>();\n\n/* \u5143\u7d20\u5165\u961f */\nqueue.offer(1);\nqueue.offer(3);\nqueue.offer(2);\nqueue.offer(5);\nqueue.offer(4);\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint peek = queue.peek();\n\n/* \u5143\u7d20\u51fa\u961f */\nint pop = queue.poll();\n\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nint size = queue.size();\n\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nboolean isEmpty = queue.isEmpty();\n
queue.cs/* \u521d\u59cb\u5316\u961f\u5217 */\nQueue<int> queue = new();\n\n/* \u5143\u7d20\u5165\u961f */\nqueue.Enqueue(1);\nqueue.Enqueue(3);\nqueue.Enqueue(2);\nqueue.Enqueue(5);\nqueue.Enqueue(4);\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint peek = queue.Peek();\n\n/* \u5143\u7d20\u51fa\u961f */\nint pop = queue.Dequeue();\n\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nint size = queue.Count;\n\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty = queue.Count == 0;\n
queue_test.go/* \u521d\u59cb\u5316\u961f\u5217 */\n// \u5728 Go \u4e2d\uff0c\u5c06 list \u4f5c\u4e3a\u961f\u5217\u6765\u4f7f\u7528\nqueue := list.New()\n\n/* \u5143\u7d20\u5165\u961f */\nqueue.PushBack(1)\nqueue.PushBack(3)\nqueue.PushBack(2)\nqueue.PushBack(5)\nqueue.PushBack(4)\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\npeek := queue.Front()\n\n/* \u5143\u7d20\u51fa\u961f */\npop := queue.Front()\nqueue.Remove(pop)\n\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nsize := queue.Len()\n\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nisEmpty := queue.Len() == 0\n
queue.swift/* \u521d\u59cb\u5316\u961f\u5217 */\n// Swift \u6ca1\u6709\u5185\u7f6e\u7684\u961f\u5217\u7c7b\uff0c\u53ef\u4ee5\u628a Array \u5f53\u4f5c\u961f\u5217\u6765\u4f7f\u7528\nvar queue: [Int] = []\n\n/* \u5143\u7d20\u5165\u961f */\nqueue.append(1)\nqueue.append(3)\nqueue.append(2)\nqueue.append(5)\nqueue.append(4)\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nlet peek = queue.first!\n\n/* \u5143\u7d20\u51fa\u961f */\n// \u7531\u4e8e\u662f\u6570\u7ec4\uff0c\u56e0\u6b64 removeFirst \u7684\u590d\u6742\u5ea6\u4e3a O(n)\nlet pool = queue.removeFirst()\n\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nlet size = queue.count\n\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nlet isEmpty = queue.isEmpty\n
queue.js/* \u521d\u59cb\u5316\u961f\u5217 */\n// JavaScript \u6ca1\u6709\u5185\u7f6e\u7684\u961f\u5217\uff0c\u53ef\u4ee5\u628a Array \u5f53\u4f5c\u961f\u5217\u6765\u4f7f\u7528\nconst queue = [];\n\n/* \u5143\u7d20\u5165\u961f */\nqueue.push(1);\nqueue.push(3);\nqueue.push(2);\nqueue.push(5);\nqueue.push(4);\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nconst peek = queue[0];\n\n/* \u5143\u7d20\u51fa\u961f */\n// \u5e95\u5c42\u662f\u6570\u7ec4\uff0c\u56e0\u6b64 shift() \u65b9\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\nconst pop = queue.shift();\n\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nconst size = queue.length;\n\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nconst empty = queue.length === 0;\n
queue.ts/* \u521d\u59cb\u5316\u961f\u5217 */\n// TypeScript \u6ca1\u6709\u5185\u7f6e\u7684\u961f\u5217\uff0c\u53ef\u4ee5\u628a Array \u5f53\u4f5c\u961f\u5217\u6765\u4f7f\u7528 \nconst queue: number[] = [];\n\n/* \u5143\u7d20\u5165\u961f */\nqueue.push(1);\nqueue.push(3);\nqueue.push(2);\nqueue.push(5);\nqueue.push(4);\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nconst peek = queue[0];\n\n/* \u5143\u7d20\u51fa\u961f */\n// \u5e95\u5c42\u662f\u6570\u7ec4\uff0c\u56e0\u6b64 shift() \u65b9\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\nconst pop = queue.shift();\n\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nconst size = queue.length;\n\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nconst empty = queue.length === 0;\n
queue.dart/* \u521d\u59cb\u5316\u961f\u5217 */\n// \u5728 Dart \u4e2d\uff0c\u961f\u5217\u7c7b Qeque \u662f\u53cc\u5411\u961f\u5217\uff0c\u4e5f\u53ef\u4f5c\u4e3a\u961f\u5217\u4f7f\u7528\nQueue<int> queue = Queue();\n\n/* \u5143\u7d20\u5165\u961f */\nqueue.add(1);\nqueue.add(3);\nqueue.add(2);\nqueue.add(5);\nqueue.add(4);\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint peek = queue.first;\n\n/* \u5143\u7d20\u51fa\u961f */\nint pop = queue.removeFirst();\n\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nint size = queue.length;\n\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty = queue.isEmpty;\n
queue.rs/* \u521d\u59cb\u5316\u53cc\u5411\u961f\u5217 */\n// \u5728 Rust \u4e2d\u4f7f\u7528\u53cc\u5411\u961f\u5217\u4f5c\u4e3a\u666e\u901a\u961f\u5217\u6765\u4f7f\u7528\nlet mut deque: VecDeque<u32> = VecDeque::new();\n\n/* \u5143\u7d20\u5165\u961f */\ndeque.push_back(1);\ndeque.push_back(3);\ndeque.push_back(2);\ndeque.push_back(5);\ndeque.push_back(4);\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nif let Some(front) = deque.front() {\n}\n\n/* \u5143\u7d20\u51fa\u961f */\nif let Some(pop) = deque.pop_front() {\n}\n\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nlet size = deque.len();\n\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nlet is_empty = deque.is_empty();\n
queue.c// C \u672a\u63d0\u4f9b\u5185\u7f6e\u961f\u5217\n
queue.zig\n
"},{"location":"chapter_stack_and_queue/queue/#522","title":"5.2.2 \u00a0 \u961f\u5217\u5b9e\u73b0","text":"\u4e3a\u4e86\u5b9e\u73b0\u961f\u5217\uff0c\u6211\u4eec\u9700\u8981\u4e00\u79cd\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u5728\u4e00\u7aef\u6dfb\u52a0\u5143\u7d20\uff0c\u5e76\u5728\u53e6\u4e00\u7aef\u5220\u9664\u5143\u7d20\u3002\u94fe\u8868\u548c\u6570\u7ec4\u90fd\u7b26\u5408\u8981\u6c42\u3002
"},{"location":"chapter_stack_and_queue/queue/#1","title":"1. \u00a0 \u57fa\u4e8e\u94fe\u8868\u7684\u5b9e\u73b0","text":"\u5982\u56fe 5-5 \u6240\u793a\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u94fe\u8868\u7684\u201c\u5934\u8282\u70b9\u201d\u548c\u201c\u5c3e\u8282\u70b9\u201d\u5206\u522b\u89c6\u4e3a\u201c\u961f\u9996\u201d\u548c\u201c\u961f\u5c3e\u201d\uff0c\u89c4\u5b9a\u961f\u5c3e\u4ec5\u53ef\u6dfb\u52a0\u8282\u70b9\uff0c\u961f\u9996\u4ec5\u53ef\u5220\u9664\u8282\u70b9\u3002
LinkedListQueuepush()pop()\u56fe 5-5 \u00a0 \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u961f\u5217\u7684\u5165\u961f\u51fa\u961f\u64cd\u4f5c
\u4ee5\u4e0b\u662f\u7528\u94fe\u8868\u5b9e\u73b0\u961f\u5217\u7684\u4ee3\u7801\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig linkedlist_queue.pyclass LinkedListQueue:\n \"\"\"\u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217\"\"\"\n\n def __init__(self):\n \"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n self._front: ListNode | None = None # \u5934\u8282\u70b9 front\n self._rear: ListNode | None = None # \u5c3e\u8282\u70b9 rear\n self._size: int = 0\n\n def size(self) -> int:\n \"\"\"\u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6\"\"\"\n return self._size\n\n def is_empty(self) -> bool:\n \"\"\"\u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a\"\"\"\n return not self._front\n\n def push(self, num: int):\n \"\"\"\u5165\u961f\"\"\"\n # \u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\n node = ListNode(num)\n # \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\n if self._front is None:\n self._front = node\n self._rear = node\n # \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\n else:\n self._rear.next = node\n self._rear = node\n self._size += 1\n\n def pop(self) -> int:\n \"\"\"\u51fa\u961f\"\"\"\n num = self.peek()\n # \u5220\u9664\u5934\u8282\u70b9\n self._front = self._front.next\n self._size -= 1\n return num\n\n def peek(self) -> int:\n \"\"\"\u8bbf\u95ee\u961f\u9996\u5143\u7d20\"\"\"\n if self.is_empty():\n raise IndexError(\"\u961f\u5217\u4e3a\u7a7a\")\n return self._front.val\n\n def to_list(self) -> list[int]:\n \"\"\"\u8f6c\u5316\u4e3a\u5217\u8868\u7528\u4e8e\u6253\u5370\"\"\"\n queue = []\n temp = self._front\n while temp:\n queue.append(temp.val)\n temp = temp.next\n return queue\n
linkedlist_queue.cpp/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\nclass LinkedListQueue {\n private:\n ListNode *front, *rear; // \u5934\u8282\u70b9 front \uff0c\u5c3e\u8282\u70b9 rear\n int queSize;\n\n public:\n LinkedListQueue() {\n front = nullptr;\n rear = nullptr;\n queSize = 0;\n }\n\n ~LinkedListQueue() {\n // \u904d\u5386\u94fe\u8868\u5220\u9664\u8282\u70b9\uff0c\u91ca\u653e\u5185\u5b58\n freeMemoryLinkedList(front);\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n int size() {\n return queSize;\n }\n\n /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n bool isEmpty() {\n return queSize == 0;\n }\n\n /* \u5165\u961f */\n void push(int num) {\n // \u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\n ListNode *node = new ListNode(num);\n // \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\n if (front == nullptr) {\n front = node;\n rear = node;\n }\n // \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\n else {\n rear->next = node;\n rear = node;\n }\n queSize++;\n }\n\n /* \u51fa\u961f */\n void pop() {\n int num = peek();\n // \u5220\u9664\u5934\u8282\u70b9\n ListNode *tmp = front;\n front = front->next;\n // \u91ca\u653e\u5185\u5b58\n delete tmp;\n queSize--;\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n int peek() {\n if (size() == 0)\n throw out_of_range(\"\u961f\u5217\u4e3a\u7a7a\");\n return front->val;\n }\n\n /* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Vector \u5e76\u8fd4\u56de */\n vector<int> toVector() {\n ListNode *node = front;\n vector<int> res(size());\n for (int i = 0; i < res.size(); i++) {\n res[i] = node->val;\n node = node->next;\n }\n return res;\n }\n};\n
linkedlist_queue.java/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\nclass LinkedListQueue {\n private ListNode front, rear; // \u5934\u8282\u70b9 front \uff0c\u5c3e\u8282\u70b9 rear\n private int queSize = 0;\n\n public LinkedListQueue() {\n front = null;\n rear = null;\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n public int size() {\n return queSize;\n }\n\n /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n public boolean isEmpty() {\n return size() == 0;\n }\n\n /* \u5165\u961f */\n public void push(int num) {\n // \u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\n ListNode node = new ListNode(num);\n // \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\n if (front == null) {\n front = node;\n rear = node;\n // \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\n } else {\n rear.next = node;\n rear = node;\n }\n queSize++;\n }\n\n /* \u51fa\u961f */\n public int pop() {\n int num = peek();\n // \u5220\u9664\u5934\u8282\u70b9\n front = front.next;\n queSize--;\n return num;\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n public int peek() {\n if (isEmpty())\n throw new IndexOutOfBoundsException();\n return front.val;\n }\n\n /* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n public int[] toArray() {\n ListNode node = front;\n int[] res = new int[size()];\n for (int i = 0; i < res.length; i++) {\n res[i] = node.val;\n node = node.next;\n }\n return res;\n }\n}\n
linkedlist_queue.cs/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\nclass LinkedListQueue {\n ListNode? front, rear; // \u5934\u8282\u70b9 front \uff0c\u5c3e\u8282\u70b9 rear \n int queSize = 0;\n\n public LinkedListQueue() {\n front = null;\n rear = null;\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n public int Size() {\n return queSize;\n }\n\n /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n public bool IsEmpty() {\n return Size() == 0;\n }\n\n /* \u5165\u961f */\n public void Push(int num) {\n // \u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\n ListNode node = new(num);\n // \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\n if (front == null) {\n front = node;\n rear = node;\n // \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\n } else if (rear != null) {\n rear.next = node;\n rear = node;\n }\n queSize++;\n }\n\n /* \u51fa\u961f */\n public int Pop() {\n int num = Peek();\n // \u5220\u9664\u5934\u8282\u70b9\n front = front?.next;\n queSize--;\n return num;\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n public int Peek() {\n if (IsEmpty())\n throw new Exception();\n return front!.val;\n }\n\n /* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n public int[] ToArray() {\n if (front == null)\n return [];\n\n ListNode? node = front;\n int[] res = new int[Size()];\n for (int i = 0; i < res.Length; i++) {\n res[i] = node!.val;\n node = node.next;\n }\n return res;\n }\n}\n
linkedlist_queue.go/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\ntype linkedListQueue struct {\n // \u4f7f\u7528\u5185\u7f6e\u5305 list \u6765\u5b9e\u73b0\u961f\u5217\n data *list.List\n}\n\n/* \u521d\u59cb\u5316\u961f\u5217 */\nfunc newLinkedListQueue() *linkedListQueue {\n return &linkedListQueue{\n data: list.New(),\n }\n}\n\n/* \u5165\u961f */\nfunc (s *linkedListQueue) push(value any) {\n s.data.PushBack(value)\n}\n\n/* \u51fa\u961f */\nfunc (s *linkedListQueue) pop() any {\n if s.isEmpty() {\n return nil\n }\n e := s.data.Front()\n s.data.Remove(e)\n return e.Value\n}\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nfunc (s *linkedListQueue) peek() any {\n if s.isEmpty() {\n return nil\n }\n e := s.data.Front()\n return e.Value\n}\n\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nfunc (s *linkedListQueue) size() int {\n return s.data.Len()\n}\n\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nfunc (s *linkedListQueue) isEmpty() bool {\n return s.data.Len() == 0\n}\n\n/* \u83b7\u53d6 List \u7528\u4e8e\u6253\u5370 */\nfunc (s *linkedListQueue) toList() *list.List {\n return s.data\n}\n
linkedlist_queue.swift/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\nclass LinkedListQueue {\n private var front: ListNode? // \u5934\u8282\u70b9\n private var rear: ListNode? // \u5c3e\u8282\u70b9\n private var _size = 0\n\n init() {}\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n func size() -> Int {\n _size\n }\n\n /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n func isEmpty() -> Bool {\n size() == 0\n }\n\n /* \u5165\u961f */\n func push(num: Int) {\n // \u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\n let node = ListNode(x: num)\n // \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\n if front == nil {\n front = node\n rear = node\n }\n // \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\n else {\n rear?.next = node\n rear = node\n }\n _size += 1\n }\n\n /* \u51fa\u961f */\n @discardableResult\n func pop() -> Int {\n let num = peek()\n // \u5220\u9664\u5934\u8282\u70b9\n front = front?.next\n _size -= 1\n return num\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n func peek() -> Int {\n if isEmpty() {\n fatalError(\"\u961f\u5217\u4e3a\u7a7a\")\n }\n return front!.val\n }\n\n /* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n func toArray() -> [Int] {\n var node = front\n var res = Array(repeating: 0, count: size())\n for i in res.indices {\n res[i] = node!.val\n node = node?.next\n }\n return res\n }\n}\n
linkedlist_queue.js/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\nclass LinkedListQueue {\n #front; // \u5934\u8282\u70b9 #front\n #rear; // \u5c3e\u8282\u70b9 #rear\n #queSize = 0;\n\n constructor() {\n this.#front = null;\n this.#rear = null;\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n get size() {\n return this.#queSize;\n }\n\n /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n isEmpty() {\n return this.size === 0;\n }\n\n /* \u5165\u961f */\n push(num) {\n // \u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\n const node = new ListNode(num);\n // \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\n if (!this.#front) {\n this.#front = node;\n this.#rear = node;\n // \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\n } else {\n this.#rear.next = node;\n this.#rear = node;\n }\n this.#queSize++;\n }\n\n /* \u51fa\u961f */\n pop() {\n const num = this.peek();\n // \u5220\u9664\u5934\u8282\u70b9\n this.#front = this.#front.next;\n this.#queSize--;\n return num;\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n peek() {\n if (this.size === 0) throw new Error('\u961f\u5217\u4e3a\u7a7a');\n return this.#front.val;\n }\n\n /* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n toArray() {\n let node = this.#front;\n const res = new Array(this.size);\n for (let i = 0; i < res.length; i++) {\n res[i] = node.val;\n node = node.next;\n }\n return res;\n }\n}\n
linkedlist_queue.ts/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\nclass LinkedListQueue {\n private front: ListNode | null; // \u5934\u8282\u70b9 front\n private rear: ListNode | null; // \u5c3e\u8282\u70b9 rear\n private queSize: number = 0;\n\n constructor() {\n this.front = null;\n this.rear = null;\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n get size(): number {\n return this.queSize;\n }\n\n /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n isEmpty(): boolean {\n return this.size === 0;\n }\n\n /* \u5165\u961f */\n push(num: number): void {\n // \u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\n const node = new ListNode(num);\n // \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\n if (!this.front) {\n this.front = node;\n this.rear = node;\n // \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\n } else {\n this.rear!.next = node;\n this.rear = node;\n }\n this.queSize++;\n }\n\n /* \u51fa\u961f */\n pop(): number {\n const num = this.peek();\n if (!this.front) throw new Error('\u961f\u5217\u4e3a\u7a7a');\n // \u5220\u9664\u5934\u8282\u70b9\n this.front = this.front.next;\n this.queSize--;\n return num;\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n peek(): number {\n if (this.size === 0) throw new Error('\u961f\u5217\u4e3a\u7a7a');\n return this.front!.val;\n }\n\n /* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n toArray(): number[] {\n let node = this.front;\n const res = new Array<number>(this.size);\n for (let i = 0; i < res.length; i++) {\n res[i] = node!.val;\n node = node!.next;\n }\n return res;\n }\n}\n
linkedlist_queue.dart/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\nclass LinkedListQueue {\n ListNode? _front; // \u5934\u8282\u70b9 _front\n ListNode? _rear; // \u5c3e\u8282\u70b9 _rear\n int _queSize = 0; // \u961f\u5217\u957f\u5ea6\n\n LinkedListQueue() {\n _front = null;\n _rear = null;\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n int size() {\n return _queSize;\n }\n\n /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n bool isEmpty() {\n return _queSize == 0;\n }\n\n /* \u5165\u961f */\n void push(int _num) {\n // \u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 _num\n final node = ListNode(_num);\n // \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\n if (_front == null) {\n _front = node;\n _rear = node;\n } else {\n // \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\n _rear!.next = node;\n _rear = node;\n }\n _queSize++;\n }\n\n /* \u51fa\u961f */\n int pop() {\n final int _num = peek();\n // \u5220\u9664\u5934\u8282\u70b9\n _front = _front!.next;\n _queSize--;\n return _num;\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n int peek() {\n if (_queSize == 0) {\n throw Exception('\u961f\u5217\u4e3a\u7a7a');\n }\n return _front!.val;\n }\n\n /* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n List<int> toArray() {\n ListNode? node = _front;\n final List<int> queue = [];\n while (node != null) {\n queue.add(node.val);\n node = node.next;\n }\n return queue;\n }\n}\n
linkedlist_queue.rs/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\n#[allow(dead_code)]\npub struct LinkedListQueue<T> {\n front: Option<Rc<RefCell<ListNode<T>>>>, // \u5934\u8282\u70b9 front\n rear: Option<Rc<RefCell<ListNode<T>>>>, // \u5c3e\u8282\u70b9 rear \n que_size: usize, // \u961f\u5217\u7684\u957f\u5ea6\n}\n\nimpl<T: Copy> LinkedListQueue<T> {\n pub fn new() -> Self {\n Self {\n front: None,\n rear: None,\n que_size: 0, \n }\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n pub fn size(&self) -> usize {\n return self.que_size;\n }\n\n /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n pub fn is_empty(&self) -> bool {\n return self.size() == 0;\n }\n\n /* \u5165\u961f */\n pub fn push(&mut self, num: T) {\n // \u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\n let new_rear = ListNode::new(num);\n match self.rear.take() {\n // \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\n Some(old_rear) => {\n old_rear.borrow_mut().next = Some(new_rear.clone());\n self.rear = Some(new_rear);\n }\n // \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\n None => {\n self.front = Some(new_rear.clone());\n self.rear = Some(new_rear);\n }\n }\n self.que_size += 1;\n }\n\n /* \u51fa\u961f */\n pub fn pop(&mut self) -> Option<T> {\n self.front.take().map(|old_front| {\n match old_front.borrow_mut().next.take() {\n Some(new_front) => {\n self.front = Some(new_front);\n }\n None => {\n self.rear.take();\n }\n }\n self.que_size -= 1;\n Rc::try_unwrap(old_front).ok().unwrap().into_inner().val\n })\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n pub fn peek(&self) -> Option<&Rc<RefCell<ListNode<T>>>> {\n self.front.as_ref()\n }\n\n /* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n pub fn to_array(&self, head: Option<&Rc<RefCell<ListNode<T>>>>) -> Vec<T> {\n if let Some(node) = head {\n let mut nums = self.to_array(node.borrow().next.as_ref());\n nums.insert(0, node.borrow().val);\n return nums;\n }\n return Vec::new();\n }\n}\n
linkedlist_queue.c/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\ntypedef struct {\n ListNode *front, *rear;\n int queSize;\n} LinkedListQueue;\n\n/* \u6784\u9020\u51fd\u6570 */\nLinkedListQueue *newLinkedListQueue() {\n LinkedListQueue *queue = (LinkedListQueue *)malloc(sizeof(LinkedListQueue));\n queue->front = NULL;\n queue->rear = NULL;\n queue->queSize = 0;\n return queue;\n}\n\n/* \u6790\u6784\u51fd\u6570 */\nvoid delLinkedListQueue(LinkedListQueue *queue) {\n // \u91ca\u653e\u6240\u6709\u8282\u70b9\n for (int i = 0; i < queue->queSize && queue->front != NULL; i++) {\n ListNode *tmp = queue->front;\n queue->front = queue->front->next;\n free(tmp);\n }\n // \u91ca\u653e queue \u7ed3\u6784\u4f53\n free(queue);\n}\n\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nint size(LinkedListQueue *queue) {\n return queue->queSize;\n}\n\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool empty(LinkedListQueue *queue) {\n return (size(queue) == 0);\n}\n\n/* \u5165\u961f */\nvoid push(LinkedListQueue *queue, int num) {\n // \u5c3e\u8282\u70b9\u5904\u6dfb\u52a0 node\n ListNode *node = newListNode(num);\n // \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\n if (queue->front == NULL) {\n queue->front = node;\n queue->rear = node;\n }\n // \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\n else {\n queue->rear->next = node;\n queue->rear = node;\n }\n queue->queSize++;\n}\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint peek(LinkedListQueue *queue) {\n assert(size(queue) && queue->front);\n return queue->front->val;\n}\n\n/* \u51fa\u961f */\nvoid pop(LinkedListQueue *queue) {\n int num = peek(queue);\n ListNode *tmp = queue->front;\n queue->front = queue->front->next;\n free(tmp);\n queue->queSize--;\n}\n\n/* \u6253\u5370\u961f\u5217 */\nvoid printLinkedListQueue(LinkedListQueue *queue) {\n int *arr = malloc(sizeof(int) * queue->queSize);\n // \u62f7\u8d1d\u94fe\u8868\u4e2d\u7684\u6570\u636e\u5230\u6570\u7ec4\n int i;\n ListNode *node;\n for (i = 0, node = queue->front; i < queue->queSize; i++) {\n arr[i] = node->val;\n node = node->next;\n }\n printArray(arr, queue->queSize);\n free(arr);\n}\n
linkedlist_queue.zig// \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217\nfn LinkedListQueue(comptime T: type) type {\n return struct {\n const Self = @This();\n\n front: ?*inc.ListNode(T) = null, // \u5934\u8282\u70b9 front\n rear: ?*inc.ListNode(T) = null, // \u5c3e\u8282\u70b9 rear\n que_size: usize = 0, // \u961f\u5217\u7684\u957f\u5ea6\n mem_arena: ?std.heap.ArenaAllocator = null,\n mem_allocator: std.mem.Allocator = undefined, // \u5185\u5b58\u5206\u914d\u5668\n\n // \u6784\u9020\u51fd\u6570\uff08\u5206\u914d\u5185\u5b58+\u521d\u59cb\u5316\u961f\u5217\uff09\n pub fn init(self: *Self, allocator: std.mem.Allocator) !void {\n if (self.mem_arena == null) {\n self.mem_arena = std.heap.ArenaAllocator.init(allocator);\n self.mem_allocator = self.mem_arena.?.allocator();\n }\n self.front = null;\n self.rear = null;\n self.que_size = 0;\n }\n\n // \u6790\u6784\u51fd\u6570\uff08\u91ca\u653e\u5185\u5b58\uff09\n pub fn deinit(self: *Self) void {\n if (self.mem_arena == null) return;\n self.mem_arena.?.deinit();\n }\n\n // \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6\n pub fn size(self: *Self) usize {\n return self.que_size;\n }\n\n // \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a\n pub fn isEmpty(self: *Self) bool {\n return self.size() == 0;\n }\n\n // \u8bbf\u95ee\u961f\u9996\u5143\u7d20\n pub fn peek(self: *Self) T {\n if (self.size() == 0) @panic(\"\u961f\u5217\u4e3a\u7a7a\");\n return self.front.?.val;\n } \n\n // \u5165\u961f\n pub fn push(self: *Self, num: T) !void {\n // \u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\n var node = try self.mem_allocator.create(inc.ListNode(T));\n node.init(num);\n // \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\n if (self.front == null) {\n self.front = node;\n self.rear = node;\n // \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\n } else {\n self.rear.?.next = node;\n self.rear = node;\n }\n self.que_size += 1;\n } \n\n // \u51fa\u961f\n pub fn pop(self: *Self) T {\n var num = self.peek();\n // \u5220\u9664\u5934\u8282\u70b9\n self.front = self.front.?.next;\n self.que_size -= 1;\n return num;\n } \n\n // \u5c06\u94fe\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4\n pub fn toArray(self: *Self) ![]T {\n var node = self.front;\n var res = try self.mem_allocator.alloc(T, self.size());\n @memset(res, @as(T, 0));\n var i: usize = 0;\n while (i < res.len) : (i += 1) {\n res[i] = node.?.val;\n node = node.?.next;\n }\n return res;\n }\n };\n}\n
"},{"location":"chapter_stack_and_queue/queue/#2","title":"2. \u00a0 \u57fa\u4e8e\u6570\u7ec4\u7684\u5b9e\u73b0","text":"\u5728\u6570\u7ec4\u4e2d\u5220\u9664\u9996\u5143\u7d20\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \uff0c\u8fd9\u4f1a\u5bfc\u81f4\u51fa\u961f\u64cd\u4f5c\u6548\u7387\u8f83\u4f4e\u3002\u7136\u800c\uff0c\u6211\u4eec\u53ef\u4ee5\u91c7\u7528\u4ee5\u4e0b\u5de7\u5999\u65b9\u6cd5\u6765\u907f\u514d\u8fd9\u4e2a\u95ee\u9898\u3002
\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u53d8\u91cf front
\u6307\u5411\u961f\u9996\u5143\u7d20\u7684\u7d22\u5f15\uff0c\u5e76\u7ef4\u62a4\u4e00\u4e2a\u53d8\u91cf size
\u7528\u4e8e\u8bb0\u5f55\u961f\u5217\u957f\u5ea6\u3002\u5b9a\u4e49 rear = front + size
\uff0c\u8fd9\u4e2a\u516c\u5f0f\u8ba1\u7b97\u51fa\u7684 rear
\u6307\u5411\u961f\u5c3e\u5143\u7d20\u4e4b\u540e\u7684\u4e0b\u4e00\u4e2a\u4f4d\u7f6e\u3002
\u57fa\u4e8e\u6b64\u8bbe\u8ba1\uff0c\u6570\u7ec4\u4e2d\u5305\u542b\u5143\u7d20\u7684\u6709\u6548\u533a\u95f4\u4e3a [front, rear - 1]
\uff0c\u5404\u79cd\u64cd\u4f5c\u7684\u5b9e\u73b0\u65b9\u6cd5\u5982\u56fe 5-6 \u6240\u793a\u3002
rear
\u7d22\u5f15\u5904\uff0c\u5e76\u5c06 size
\u589e\u52a0 1 \u3002front
\u589e\u52a0 1 \uff0c\u5e76\u5c06 size
\u51cf\u5c11 1 \u3002\u53ef\u4ee5\u770b\u5230\uff0c\u5165\u961f\u548c\u51fa\u961f\u64cd\u4f5c\u90fd\u53ea\u9700\u8fdb\u884c\u4e00\u6b21\u64cd\u4f5c\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u5747\u4e3a \\(O(1)\\) \u3002
ArrayQueuepush()pop()\u56fe 5-6 \u00a0 \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u961f\u5217\u7684\u5165\u961f\u51fa\u961f\u64cd\u4f5c
\u4f60\u53ef\u80fd\u4f1a\u53d1\u73b0\u4e00\u4e2a\u95ee\u9898\uff1a\u5728\u4e0d\u65ad\u8fdb\u884c\u5165\u961f\u548c\u51fa\u961f\u7684\u8fc7\u7a0b\u4e2d\uff0cfront
\u548c rear
\u90fd\u5728\u5411\u53f3\u79fb\u52a8\uff0c\u5f53\u5b83\u4eec\u5230\u8fbe\u6570\u7ec4\u5c3e\u90e8\u65f6\u5c31\u65e0\u6cd5\u7ee7\u7eed\u79fb\u52a8\u4e86\u3002\u4e3a\u4e86\u89e3\u51b3\u6b64\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u6570\u7ec4\u89c6\u4e3a\u9996\u5c3e\u76f8\u63a5\u7684\u201c\u73af\u5f62\u6570\u7ec4\u201d\u3002
\u5bf9\u4e8e\u73af\u5f62\u6570\u7ec4\uff0c\u6211\u4eec\u9700\u8981\u8ba9 front
\u6216 rear
\u5728\u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u76f4\u63a5\u56de\u5230\u6570\u7ec4\u5934\u90e8\u7ee7\u7eed\u904d\u5386\u3002\u8fd9\u79cd\u5468\u671f\u6027\u89c4\u5f8b\u53ef\u4ee5\u901a\u8fc7\u201c\u53d6\u4f59\u64cd\u4f5c\u201d\u6765\u5b9e\u73b0\uff0c\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a
class ArrayQueue:\n \"\"\"\u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217\"\"\"\n\n def __init__(self, size: int):\n \"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n self._nums: list[int] = [0] * size # \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n self._front: int = 0 # \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n self._size: int = 0 # \u961f\u5217\u957f\u5ea6\n\n def capacity(self) -> int:\n \"\"\"\u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf\"\"\"\n return len(self._nums)\n\n def size(self) -> int:\n \"\"\"\u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6\"\"\"\n return self._size\n\n def is_empty(self) -> bool:\n \"\"\"\u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a\"\"\"\n return self._size == 0\n\n def push(self, num: int):\n \"\"\"\u5165\u961f\"\"\"\n if self._size == self.capacity():\n raise IndexError(\"\u961f\u5217\u5df2\u6ee1\")\n # \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n # \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\uff0c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\n rear: int = (self._front + self._size) % self.capacity()\n # \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n self._nums[rear] = num\n self._size += 1\n\n def pop(self) -> int:\n \"\"\"\u51fa\u961f\"\"\"\n num: int = self.peek()\n # \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\n self._front = (self._front + 1) % self.capacity()\n self._size -= 1\n return num\n\n def peek(self) -> int:\n \"\"\"\u8bbf\u95ee\u961f\u9996\u5143\u7d20\"\"\"\n if self.is_empty():\n raise IndexError(\"\u961f\u5217\u4e3a\u7a7a\")\n return self._nums[self._front]\n\n def to_list(self) -> list[int]:\n \"\"\"\u8fd4\u56de\u5217\u8868\u7528\u4e8e\u6253\u5370\"\"\"\n res = [0] * self.size()\n j: int = self._front\n for i in range(self.size()):\n res[i] = self._nums[(j % self.capacity())]\n j += 1\n return res\n
array_queue.cpp/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nclass ArrayQueue {\n private:\n int *nums; // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n int front; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n int queSize; // \u961f\u5217\u957f\u5ea6\n int queCapacity; // \u961f\u5217\u5bb9\u91cf\n\n public:\n ArrayQueue(int capacity) {\n // \u521d\u59cb\u5316\u6570\u7ec4\n nums = new int[capacity];\n queCapacity = capacity;\n front = queSize = 0;\n }\n\n ~ArrayQueue() {\n delete[] nums;\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\n int capacity() {\n return queCapacity;\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n int size() {\n return queSize;\n }\n\n /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n bool isEmpty() {\n return size() == 0;\n }\n\n /* \u5165\u961f */\n void push(int num) {\n if (queSize == queCapacity) {\n cout << \"\u961f\u5217\u5df2\u6ee1\" << endl;\n return;\n }\n // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\uff0c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\n int rear = (front + queSize) % queCapacity;\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n nums[rear] = num;\n queSize++;\n }\n\n /* \u51fa\u961f */\n void pop() {\n int num = peek();\n // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\n front = (front + 1) % queCapacity;\n queSize--;\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n int peek() {\n if (isEmpty())\n throw out_of_range(\"\u961f\u5217\u4e3a\u7a7a\");\n return nums[front];\n }\n\n /* \u5c06\u6570\u7ec4\u8f6c\u5316\u4e3a Vector \u5e76\u8fd4\u56de */\n vector<int> toVector() {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n vector<int> arr(queSize);\n for (int i = 0, j = front; i < queSize; i++, j++) {\n arr[i] = nums[j % queCapacity];\n }\n return arr;\n }\n};\n
array_queue.java/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nclass ArrayQueue {\n private int[] nums; // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n private int front; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n private int queSize; // \u961f\u5217\u957f\u5ea6\n\n public ArrayQueue(int capacity) {\n nums = new int[capacity];\n front = queSize = 0;\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\n public int capacity() {\n return nums.length;\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n public int size() {\n return queSize;\n }\n\n /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n public boolean isEmpty() {\n return queSize == 0;\n }\n\n /* \u5165\u961f */\n public void push(int num) {\n if (queSize == capacity()) {\n System.out.println(\"\u961f\u5217\u5df2\u6ee1\");\n return;\n }\n // \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\uff0c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\n int rear = (front + queSize) % capacity();\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n nums[rear] = num;\n queSize++;\n }\n\n /* \u51fa\u961f */\n public int pop() {\n int num = peek();\n // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\n front = (front + 1) % capacity();\n queSize--;\n return num;\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n public int peek() {\n if (isEmpty())\n throw new IndexOutOfBoundsException();\n return nums[front];\n }\n\n /* \u8fd4\u56de\u6570\u7ec4 */\n public int[] toArray() {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n int[] res = new int[queSize];\n for (int i = 0, j = front; i < queSize; i++, j++) {\n res[i] = nums[j % capacity()];\n }\n return res;\n }\n}\n
array_queue.cs/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nclass ArrayQueue {\n int[] nums; // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n int front; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n int queSize; // \u961f\u5217\u957f\u5ea6\n\n public ArrayQueue(int capacity) {\n nums = new int[capacity];\n front = queSize = 0;\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\n int Capacity() {\n return nums.Length;\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n public int Size() {\n return queSize;\n }\n\n /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n public bool IsEmpty() {\n return queSize == 0;\n }\n\n /* \u5165\u961f */\n public void Push(int num) {\n if (queSize == Capacity()) {\n Console.WriteLine(\"\u961f\u5217\u5df2\u6ee1\");\n return;\n }\n // \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\uff0c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\n int rear = (front + queSize) % Capacity();\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n nums[rear] = num;\n queSize++;\n }\n\n /* \u51fa\u961f */\n public int Pop() {\n int num = Peek();\n // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\n front = (front + 1) % Capacity();\n queSize--;\n return num;\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n public int Peek() {\n if (IsEmpty())\n throw new Exception();\n return nums[front];\n }\n\n /* \u8fd4\u56de\u6570\u7ec4 */\n public int[] ToArray() {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n int[] res = new int[queSize];\n for (int i = 0, j = front; i < queSize; i++, j++) {\n res[i] = nums[j % this.Capacity()];\n }\n return res;\n }\n}\n
array_queue.go/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\ntype arrayQueue struct {\n nums []int // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n front int // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n queSize int // \u961f\u5217\u957f\u5ea6\n queCapacity int // \u961f\u5217\u5bb9\u91cf\uff08\u5373\u6700\u5927\u5bb9\u7eb3\u5143\u7d20\u6570\u91cf\uff09\n}\n\n/* \u521d\u59cb\u5316\u961f\u5217 */\nfunc newArrayQueue(queCapacity int) *arrayQueue {\n return &arrayQueue{\n nums: make([]int, queCapacity),\n queCapacity: queCapacity,\n front: 0,\n queSize: 0,\n }\n}\n\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nfunc (q *arrayQueue) size() int {\n return q.queSize\n}\n\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nfunc (q *arrayQueue) isEmpty() bool {\n return q.queSize == 0\n}\n\n/* \u5165\u961f */\nfunc (q *arrayQueue) push(num int) {\n // \u5f53 rear == queCapacity \u8868\u793a\u961f\u5217\u5df2\u6ee1\n if q.queSize == q.queCapacity {\n return\n }\n // \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\uff0c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\n rear := (q.front + q.queSize) % q.queCapacity\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n q.nums[rear] = num\n q.queSize++\n}\n\n/* \u51fa\u961f */\nfunc (q *arrayQueue) pop() any {\n num := q.peek()\n // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\n q.front = (q.front + 1) % q.queCapacity\n q.queSize--\n return num\n}\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nfunc (q *arrayQueue) peek() any {\n if q.isEmpty() {\n return nil\n }\n return q.nums[q.front]\n}\n\n/* \u83b7\u53d6 Slice \u7528\u4e8e\u6253\u5370 */\nfunc (q *arrayQueue) toSlice() []int {\n rear := (q.front + q.queSize)\n if rear >= q.queCapacity {\n rear %= q.queCapacity\n return append(q.nums[q.front:], q.nums[:rear]...)\n }\n return q.nums[q.front:rear]\n}\n
array_queue.swift/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nclass ArrayQueue {\n private var nums: [Int] // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n private var front = 0 // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n private var queSize = 0 // \u961f\u5217\u957f\u5ea6\n\n init(capacity: Int) {\n // \u521d\u59cb\u5316\u6570\u7ec4\n nums = Array(repeating: 0, count: capacity)\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\n func capacity() -> Int {\n nums.count\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n func size() -> Int {\n queSize\n }\n\n /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n func isEmpty() -> Bool {\n queSize == 0\n }\n\n /* \u5165\u961f */\n func push(num: Int) {\n if size() == capacity() {\n print(\"\u961f\u5217\u5df2\u6ee1\")\n return\n }\n // \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\uff0c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\n let rear = (front + queSize) % capacity()\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n nums[rear] = num\n queSize += 1\n }\n\n /* \u51fa\u961f */\n @discardableResult\n func pop() -> Int {\n let num = peek()\n // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\n front = (front + 1) % capacity()\n queSize -= 1\n return num\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n func peek() -> Int {\n if isEmpty() {\n fatalError(\"\u961f\u5217\u4e3a\u7a7a\")\n }\n return nums[front]\n }\n\n /* \u8fd4\u56de\u6570\u7ec4 */\n func toArray() -> [Int] {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n var res = Array(repeating: 0, count: queSize)\n for (i, j) in sequence(first: (0, front), next: { $0 < self.queSize - 1 ? ($0 + 1, $1 + 1) : nil }) {\n res[i] = nums[j % capacity()]\n }\n return res\n }\n}\n
array_queue.js/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nclass ArrayQueue {\n #nums; // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n #front = 0; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n #queSize = 0; // \u961f\u5217\u957f\u5ea6\n\n constructor(capacity) {\n this.#nums = new Array(capacity);\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\n get capacity() {\n return this.#nums.length;\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n get size() {\n return this.#queSize;\n }\n\n /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n isEmpty() {\n return this.#queSize === 0;\n }\n\n /* \u5165\u961f */\n push(num) {\n if (this.size === this.capacity) {\n console.log('\u961f\u5217\u5df2\u6ee1');\n return;\n }\n // \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\uff0c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\n const rear = (this.#front + this.size) % this.capacity;\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n this.#nums[rear] = num;\n this.#queSize++;\n }\n\n /* \u51fa\u961f */\n pop() {\n const num = this.peek();\n // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\n this.#front = (this.#front + 1) % this.capacity;\n this.#queSize--;\n return num;\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n peek() {\n if (this.isEmpty()) throw new Error('\u961f\u5217\u4e3a\u7a7a');\n return this.#nums[this.#front];\n }\n\n /* \u8fd4\u56de Array */\n toArray() {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n const arr = new Array(this.size);\n for (let i = 0, j = this.#front; i < this.size; i++, j++) {\n arr[i] = this.#nums[j % this.capacity];\n }\n return arr;\n }\n}\n
array_queue.ts/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nclass ArrayQueue {\n private nums: number[]; // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n private front: number; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n private queSize: number; // \u961f\u5217\u957f\u5ea6\n\n constructor(capacity: number) {\n this.nums = new Array(capacity);\n this.front = this.queSize = 0;\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\n get capacity(): number {\n return this.nums.length;\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n get size(): number {\n return this.queSize;\n }\n\n /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n isEmpty(): boolean {\n return this.queSize === 0;\n }\n\n /* \u5165\u961f */\n push(num: number): void {\n if (this.size === this.capacity) {\n console.log('\u961f\u5217\u5df2\u6ee1');\n return;\n }\n // \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\uff0c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\n const rear = (this.front + this.queSize) % this.capacity;\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n this.nums[rear] = num;\n this.queSize++;\n }\n\n /* \u51fa\u961f */\n pop(): number {\n const num = this.peek();\n // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\n this.front = (this.front + 1) % this.capacity;\n this.queSize--;\n return num;\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n peek(): number {\n if (this.isEmpty()) throw new Error('\u961f\u5217\u4e3a\u7a7a');\n return this.nums[this.front];\n }\n\n /* \u8fd4\u56de Array */\n toArray(): number[] {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n const arr = new Array(this.size);\n for (let i = 0, j = this.front; i < this.size; i++, j++) {\n arr[i] = this.nums[j % this.capacity];\n }\n return arr;\n }\n}\n
array_queue.dart/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nclass ArrayQueue {\n late List<int> _nums; // \u7528\u4e8e\u50a8\u5b58\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n late int _front; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n late int _queSize; // \u961f\u5217\u957f\u5ea6\n\n ArrayQueue(int capacity) {\n _nums = List.filled(capacity, 0);\n _front = _queSize = 0;\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\n int capaCity() {\n return _nums.length;\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n int size() {\n return _queSize;\n }\n\n /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n bool isEmpty() {\n return _queSize == 0;\n }\n\n /* \u5165\u961f */\n void push(int _num) {\n if (_queSize == capaCity()) {\n throw Exception(\"\u961f\u5217\u5df2\u6ee1\");\n }\n // \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\uff0c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\n int rear = (_front + _queSize) % capaCity();\n // \u5c06 _num \u6dfb\u52a0\u81f3\u961f\u5c3e\n _nums[rear] = _num;\n _queSize++;\n }\n\n /* \u51fa\u961f */\n int pop() {\n int _num = peek();\n // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\n _front = (_front + 1) % capaCity();\n _queSize--;\n return _num;\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n int peek() {\n if (isEmpty()) {\n throw Exception(\"\u961f\u5217\u4e3a\u7a7a\");\n }\n return _nums[_front];\n }\n\n /* \u8fd4\u56de Array */\n List<int> toArray() {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n final List<int> res = List.filled(_queSize, 0);\n for (int i = 0, j = _front; i < _queSize; i++, j++) {\n res[i] = _nums[j % capaCity()];\n }\n return res;\n }\n}\n
array_queue.rs/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nstruct ArrayQueue {\n nums: Vec<i32>, // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n front: i32, // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n que_size: i32, // \u961f\u5217\u957f\u5ea6\n que_capacity: i32, // \u961f\u5217\u5bb9\u91cf\n}\n\nimpl ArrayQueue {\n /* \u6784\u9020\u65b9\u6cd5 */\n fn new(capacity: i32) -> ArrayQueue {\n ArrayQueue {\n nums: vec![0; capacity as usize],\n front: 0,\n que_size: 0,\n que_capacity: capacity,\n }\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\n fn capacity(&self) -> i32 {\n self.que_capacity\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n fn size(&self) -> i32 {\n self.que_size\n }\n\n /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n fn is_empty(&self) -> bool {\n self.que_size == 0\n }\n\n /* \u5165\u961f */\n fn push(&mut self, num: i32) {\n if self.que_size == self.capacity() {\n println!(\"\u961f\u5217\u5df2\u6ee1\");\n return;\n }\n // \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\uff0c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\n let rear = (self.front + self.que_size) % self.que_capacity;\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n self.nums[rear as usize] = num;\n self.que_size += 1;\n }\n\n /* \u51fa\u961f */\n fn pop(&mut self) -> i32 {\n let num = self.peek();\n // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\n self.front = (self.front + 1) % self.que_capacity;\n self.que_size -= 1;\n num\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n fn peek(&self) -> i32 {\n if self.is_empty() {\n panic!(\"index out of bounds\");\n }\n self.nums[self.front as usize]\n }\n\n /* \u8fd4\u56de\u6570\u7ec4 */\n fn to_vector(&self) -> Vec<i32> {\n let cap = self.que_capacity;\n let mut j = self.front;\n let mut arr = vec![0; self.que_size as usize];\n for i in 0..self.que_size {\n arr[i as usize] = self.nums[(j % cap) as usize];\n j += 1;\n }\n arr\n }\n}\n
array_queue.c/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\ntypedef struct {\n int *nums; // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n int front; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n int queSize; // \u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e + 1\n int queCapacity; // \u961f\u5217\u5bb9\u91cf\n} ArrayQueue;\n\n/* \u6784\u9020\u51fd\u6570 */\nArrayQueue *newArrayQueue(int capacity) {\n ArrayQueue *queue = (ArrayQueue *)malloc(sizeof(ArrayQueue));\n // \u521d\u59cb\u5316\u6570\u7ec4\n queue->queCapacity = capacity;\n queue->nums = (int *)malloc(sizeof(int) * queue->queCapacity);\n queue->front = queue->queSize = 0;\n return queue;\n}\n\n/* \u6790\u6784\u51fd\u6570 */\nvoid delArrayQueue(ArrayQueue *queue) {\n free(queue->nums);\n free(queue);\n}\n\n/* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\nint capacity(ArrayQueue *queue) {\n return queue->queCapacity;\n}\n\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nint size(ArrayQueue *queue) {\n return queue->queSize;\n}\n\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool empty(ArrayQueue *queue) {\n return queue->queSize == 0;\n}\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint peek(ArrayQueue *queue) {\n assert(size(queue) != 0);\n return queue->nums[queue->front];\n}\n\n/* \u5165\u961f */\nvoid push(ArrayQueue *queue, int num) {\n if (size(queue) == capacity(queue)) {\n printf(\"\u961f\u5217\u5df2\u6ee1\\r\\n\");\n return;\n }\n // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\uff0c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\n int rear = (queue->front + queue->queSize) % queue->queCapacity;\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n queue->nums[rear] = num;\n queue->queSize++;\n}\n\n/* \u51fa\u961f */\nvoid pop(ArrayQueue *queue) {\n int num = peek(queue);\n // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\n queue->front = (queue->front + 1) % queue->queCapacity;\n queue->queSize--;\n}\n
array_queue.zig// \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217\nfn ArrayQueue(comptime T: type) type {\n return struct {\n const Self = @This();\n\n nums: []T = undefined, // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4 \n cap: usize = 0, // \u961f\u5217\u5bb9\u91cf\n front: usize = 0, // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n queSize: usize = 0, // \u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e + 1\n mem_arena: ?std.heap.ArenaAllocator = null,\n mem_allocator: std.mem.Allocator = undefined, // \u5185\u5b58\u5206\u914d\u5668\n\n // \u6784\u9020\u51fd\u6570\uff08\u5206\u914d\u5185\u5b58+\u521d\u59cb\u5316\u6570\u7ec4\uff09\n pub fn init(self: *Self, allocator: std.mem.Allocator, cap: usize) !void {\n if (self.mem_arena == null) {\n self.mem_arena = std.heap.ArenaAllocator.init(allocator);\n self.mem_allocator = self.mem_arena.?.allocator();\n }\n self.cap = cap;\n self.nums = try self.mem_allocator.alloc(T, self.cap);\n @memset(self.nums, @as(T, 0));\n }\n\n // \u6790\u6784\u51fd\u6570\uff08\u91ca\u653e\u5185\u5b58\uff09\n pub fn deinit(self: *Self) void {\n if (self.mem_arena == null) return;\n self.mem_arena.?.deinit();\n }\n\n // \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf\n pub fn capacity(self: *Self) usize {\n return self.cap;\n }\n\n // \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6\n pub fn size(self: *Self) usize {\n return self.queSize;\n }\n\n // \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a\n pub fn isEmpty(self: *Self) bool {\n return self.queSize == 0;\n }\n\n // \u5165\u961f\n pub fn push(self: *Self, num: T) !void {\n if (self.size() == self.capacity()) {\n std.debug.print(\"\u961f\u5217\u5df2\u6ee1\\n\", .{});\n return;\n }\n // \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\uff0c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\n var rear = (self.front + self.queSize) % self.capacity();\n // \u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\n self.nums[rear] = num;\n self.queSize += 1;\n } \n\n // \u51fa\u961f\n pub fn pop(self: *Self) T {\n var num = self.peek();\n // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\n self.front = (self.front + 1) % self.capacity();\n self.queSize -= 1;\n return num;\n } \n\n // \u8bbf\u95ee\u961f\u9996\u5143\u7d20\n pub fn peek(self: *Self) T {\n if (self.isEmpty()) @panic(\"\u961f\u5217\u4e3a\u7a7a\");\n return self.nums[self.front];\n } \n\n // \u8fd4\u56de\u6570\u7ec4\n pub fn toArray(self: *Self) ![]T {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n var res = try self.mem_allocator.alloc(T, self.size());\n @memset(res, @as(T, 0));\n var i: usize = 0;\n var j: usize = self.front;\n while (i < self.size()) : ({ i += 1; j += 1; }) {\n res[i] = self.nums[j % self.capacity()];\n }\n return res;\n }\n };\n}\n
\u4ee5\u4e0a\u5b9e\u73b0\u7684\u961f\u5217\u4ecd\u7136\u5177\u6709\u5c40\u9650\u6027\uff1a\u5176\u957f\u5ea6\u4e0d\u53ef\u53d8\u3002\u7136\u800c\uff0c\u8fd9\u4e2a\u95ee\u9898\u4e0d\u96be\u89e3\u51b3\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u6570\u7ec4\u66ff\u6362\u4e3a\u52a8\u6001\u6570\u7ec4\uff0c\u4ece\u800c\u5f15\u5165\u6269\u5bb9\u673a\u5236\u3002\u6709\u5174\u8da3\u7684\u8bfb\u8005\u53ef\u4ee5\u5c1d\u8bd5\u81ea\u884c\u5b9e\u73b0\u3002
\u4e24\u79cd\u5b9e\u73b0\u7684\u5bf9\u6bd4\u7ed3\u8bba\u4e0e\u6808\u4e00\u81f4\uff0c\u5728\u6b64\u4e0d\u518d\u8d58\u8ff0\u3002
"},{"location":"chapter_stack_and_queue/queue/#523","title":"5.2.3 \u00a0 \u961f\u5217\u5178\u578b\u5e94\u7528","text":"\u300c\u6808 stack\u300d\u662f\u4e00\u79cd\u9075\u5faa\u5148\u5165\u540e\u51fa\u7684\u903b\u8f91\u7684\u7ebf\u6027\u6570\u636e\u7ed3\u6784\u3002
\u6211\u4eec\u53ef\u4ee5\u5c06\u6808\u7c7b\u6bd4\u4e3a\u684c\u9762\u4e0a\u7684\u4e00\u645e\u76d8\u5b50\uff0c\u5982\u679c\u60f3\u53d6\u51fa\u5e95\u90e8\u7684\u76d8\u5b50\uff0c\u5219\u9700\u8981\u5148\u5c06\u4e0a\u9762\u7684\u76d8\u5b50\u4f9d\u6b21\u79fb\u8d70\u3002\u6211\u4eec\u5c06\u76d8\u5b50\u66ff\u6362\u4e3a\u5404\u79cd\u7c7b\u578b\u7684\u5143\u7d20\uff08\u5982\u6574\u6570\u3001\u5b57\u7b26\u3001\u5bf9\u8c61\u7b49\uff09\uff0c\u5c31\u5f97\u5230\u4e86\u6808\u8fd9\u79cd\u6570\u636e\u7ed3\u6784\u3002
\u5982\u56fe 5-1 \u6240\u793a\uff0c\u6211\u4eec\u628a\u5806\u53e0\u5143\u7d20\u7684\u9876\u90e8\u79f0\u4e3a\u201c\u6808\u9876\u201d\uff0c\u5e95\u90e8\u79f0\u4e3a\u201c\u6808\u5e95\u201d\u3002\u5c06\u628a\u5143\u7d20\u6dfb\u52a0\u5230\u6808\u9876\u7684\u64cd\u4f5c\u53eb\u4f5c\u201c\u5165\u6808\u201d\uff0c\u5220\u9664\u6808\u9876\u5143\u7d20\u7684\u64cd\u4f5c\u53eb\u4f5c\u201c\u51fa\u6808\u201d\u3002
\u56fe 5-1 \u00a0 \u6808\u7684\u5148\u5165\u540e\u51fa\u89c4\u5219
"},{"location":"chapter_stack_and_queue/stack/#511","title":"5.1.1 \u00a0 \u6808\u5e38\u7528\u64cd\u4f5c","text":"\u6808\u7684\u5e38\u7528\u64cd\u4f5c\u5982\u8868 5-1 \u6240\u793a\uff0c\u5177\u4f53\u7684\u65b9\u6cd5\u540d\u9700\u8981\u6839\u636e\u6240\u4f7f\u7528\u7684\u7f16\u7a0b\u8bed\u8a00\u6765\u786e\u5b9a\u3002\u5728\u6b64\uff0c\u6211\u4eec\u4ee5\u5e38\u89c1\u7684 push()
\u3001pop()
\u3001peek()
\u547d\u540d\u4e3a\u4f8b\u3002
\u8868 5-1 \u00a0 \u6808\u7684\u64cd\u4f5c\u6548\u7387
\u65b9\u6cd5 \u63cf\u8ff0 \u65f6\u95f4\u590d\u6742\u5ea6 push() \u5143\u7d20\u5165\u6808\uff08\u6dfb\u52a0\u81f3\u6808\u9876\uff09 \\(O(1)\\) pop() \u6808\u9876\u5143\u7d20\u51fa\u6808 \\(O(1)\\) peek() \u8bbf\u95ee\u6808\u9876\u5143\u7d20 \\(O(1)\\)\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u7f16\u7a0b\u8bed\u8a00\u5185\u7f6e\u7684\u6808\u7c7b\u3002\u7136\u800c\uff0c\u67d0\u4e9b\u8bed\u8a00\u53ef\u80fd\u6ca1\u6709\u4e13\u95e8\u63d0\u4f9b\u6808\u7c7b\uff0c\u8fd9\u65f6\u6211\u4eec\u53ef\u4ee5\u5c06\u8be5\u8bed\u8a00\u7684\u201c\u6570\u7ec4\u201d\u6216\u201c\u94fe\u8868\u201d\u5f53\u4f5c\u6808\u6765\u4f7f\u7528\uff0c\u5e76\u5728\u7a0b\u5e8f\u903b\u8f91\u4e0a\u5ffd\u7565\u4e0e\u6808\u65e0\u5173\u7684\u64cd\u4f5c\u3002
PythonC++JavaC#GoSwiftJSTSDartRustCZig stack.py# \u521d\u59cb\u5316\u6808\n# Python \u6ca1\u6709\u5185\u7f6e\u7684\u6808\u7c7b\uff0c\u53ef\u4ee5\u628a List \u5f53\u4f5c\u6808\u6765\u4f7f\u7528 \nstack: list[int] = []\n\n# \u5143\u7d20\u5165\u6808\nstack.append(1)\nstack.append(3)\nstack.append(2)\nstack.append(5)\nstack.append(4)\n\n# \u8bbf\u95ee\u6808\u9876\u5143\u7d20\npeek: int = stack[-1]\n\n# \u5143\u7d20\u51fa\u6808\npop: int = stack.pop()\n\n# \u83b7\u53d6\u6808\u7684\u957f\u5ea6\nsize: int = len(stack)\n\n# \u5224\u65ad\u662f\u5426\u4e3a\u7a7a\nis_empty: bool = len(stack) == 0\n
stack.cpp/* \u521d\u59cb\u5316\u6808 */\nstack<int> stack;\n\n/* \u5143\u7d20\u5165\u6808 */\nstack.push(1);\nstack.push(3);\nstack.push(2);\nstack.push(5);\nstack.push(4);\n\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nint top = stack.top();\n\n/* \u5143\u7d20\u51fa\u6808 */\nstack.pop(); // \u65e0\u8fd4\u56de\u503c\n\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nint size = stack.size();\n\n/* \u5224\u65ad\u662f\u5426\u4e3a\u7a7a */\nbool empty = stack.empty();\n
stack.java/* \u521d\u59cb\u5316\u6808 */\nStack<Integer> stack = new Stack<>();\n\n/* \u5143\u7d20\u5165\u6808 */\nstack.push(1);\nstack.push(3);\nstack.push(2);\nstack.push(5);\nstack.push(4);\n\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nint peek = stack.peek();\n\n/* \u5143\u7d20\u51fa\u6808 */\nint pop = stack.pop();\n\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nint size = stack.size();\n\n/* \u5224\u65ad\u662f\u5426\u4e3a\u7a7a */\nboolean isEmpty = stack.isEmpty();\n
stack.cs/* \u521d\u59cb\u5316\u6808 */\nStack<int> stack = new();\n\n/* \u5143\u7d20\u5165\u6808 */\nstack.Push(1);\nstack.Push(3);\nstack.Push(2);\nstack.Push(5);\nstack.Push(4);\n\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nint peek = stack.Peek();\n\n/* \u5143\u7d20\u51fa\u6808 */\nint pop = stack.Pop();\n\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nint size = stack.Count;\n\n/* \u5224\u65ad\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty = stack.Count == 0;\n
stack_test.go/* \u521d\u59cb\u5316\u6808 */\n// \u5728 Go \u4e2d\uff0c\u63a8\u8350\u5c06 Slice \u5f53\u4f5c\u6808\u6765\u4f7f\u7528\nvar stack []int\n\n/* \u5143\u7d20\u5165\u6808 */\nstack = append(stack, 1)\nstack = append(stack, 3)\nstack = append(stack, 2)\nstack = append(stack, 5)\nstack = append(stack, 4)\n\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\npeek := stack[len(stack)-1]\n\n/* \u5143\u7d20\u51fa\u6808 */\npop := stack[len(stack)-1]\nstack = stack[:len(stack)-1]\n\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nsize := len(stack)\n\n/* \u5224\u65ad\u662f\u5426\u4e3a\u7a7a */\nisEmpty := len(stack) == 0\n
stack.swift/* \u521d\u59cb\u5316\u6808 */\n// Swift \u6ca1\u6709\u5185\u7f6e\u7684\u6808\u7c7b\uff0c\u53ef\u4ee5\u628a Array \u5f53\u4f5c\u6808\u6765\u4f7f\u7528\nvar stack: [Int] = []\n\n/* \u5143\u7d20\u5165\u6808 */\nstack.append(1)\nstack.append(3)\nstack.append(2)\nstack.append(5)\nstack.append(4)\n\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nlet peek = stack.last!\n\n/* \u5143\u7d20\u51fa\u6808 */\nlet pop = stack.removeLast()\n\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nlet size = stack.count\n\n/* \u5224\u65ad\u662f\u5426\u4e3a\u7a7a */\nlet isEmpty = stack.isEmpty\n
stack.js/* \u521d\u59cb\u5316\u6808 */\n// Javascript \u6ca1\u6709\u5185\u7f6e\u7684\u6808\u7c7b\uff0c\u53ef\u4ee5\u628a Array \u5f53\u4f5c\u6808\u6765\u4f7f\u7528 \nconst stack = [];\n\n/* \u5143\u7d20\u5165\u6808 */\nstack.push(1);\nstack.push(3);\nstack.push(2);\nstack.push(5);\nstack.push(4);\n\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nconst peek = stack[stack.length-1];\n\n/* \u5143\u7d20\u51fa\u6808 */\nconst pop = stack.pop();\n\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nconst size = stack.length;\n\n/* \u5224\u65ad\u662f\u5426\u4e3a\u7a7a */\nconst is_empty = stack.length === 0;\n
stack.ts/* \u521d\u59cb\u5316\u6808 */\n// Typescript \u6ca1\u6709\u5185\u7f6e\u7684\u6808\u7c7b\uff0c\u53ef\u4ee5\u628a Array \u5f53\u4f5c\u6808\u6765\u4f7f\u7528 \nconst stack: number[] = [];\n\n/* \u5143\u7d20\u5165\u6808 */\nstack.push(1);\nstack.push(3);\nstack.push(2);\nstack.push(5);\nstack.push(4);\n\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nconst peek = stack[stack.length - 1];\n\n/* \u5143\u7d20\u51fa\u6808 */\nconst pop = stack.pop();\n\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nconst size = stack.length;\n\n/* \u5224\u65ad\u662f\u5426\u4e3a\u7a7a */\nconst is_empty = stack.length === 0;\n
stack.dart/* \u521d\u59cb\u5316\u6808 */\n// Dart \u6ca1\u6709\u5185\u7f6e\u7684\u6808\u7c7b\uff0c\u53ef\u4ee5\u628a List \u5f53\u4f5c\u6808\u6765\u4f7f\u7528\nList<int> stack = [];\n\n/* \u5143\u7d20\u5165\u6808 */\nstack.add(1);\nstack.add(3);\nstack.add(2);\nstack.add(5);\nstack.add(4);\n\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nint peek = stack.last;\n\n/* \u5143\u7d20\u51fa\u6808 */\nint pop = stack.removeLast();\n\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nint size = stack.length;\n\n/* \u5224\u65ad\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty = stack.isEmpty;\n
stack.rs/* \u521d\u59cb\u5316\u6808 */\n// \u628a Vec \u5f53\u4f5c\u6808\u6765\u4f7f\u7528\nlet mut stack: Vec<i32> = Vec::new();\n\n/* \u5143\u7d20\u5165\u6808 */\nstack.push(1);\nstack.push(3);\nstack.push(2);\nstack.push(5);\nstack.push(4);\n\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nlet top = stack.last().unwrap();\n\n/* \u5143\u7d20\u51fa\u6808 */\nlet pop = stack.pop().unwrap();\n\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nlet size = stack.len();\n\n/* \u5224\u65ad\u662f\u5426\u4e3a\u7a7a */\nlet is_empty = stack.is_empty();\n
stack.c// C \u672a\u63d0\u4f9b\u5185\u7f6e\u6808\n
stack.zig\n
"},{"location":"chapter_stack_and_queue/stack/#512","title":"5.1.2 \u00a0 \u6808\u7684\u5b9e\u73b0","text":"\u4e3a\u4e86\u6df1\u5165\u4e86\u89e3\u6808\u7684\u8fd0\u884c\u673a\u5236\uff0c\u6211\u4eec\u6765\u5c1d\u8bd5\u81ea\u5df1\u5b9e\u73b0\u4e00\u4e2a\u6808\u7c7b\u3002
\u6808\u9075\u5faa\u5148\u5165\u540e\u51fa\u7684\u539f\u5219\uff0c\u56e0\u6b64\u6211\u4eec\u53ea\u80fd\u5728\u6808\u9876\u6dfb\u52a0\u6216\u5220\u9664\u5143\u7d20\u3002\u7136\u800c\uff0c\u6570\u7ec4\u548c\u94fe\u8868\u90fd\u53ef\u4ee5\u5728\u4efb\u610f\u4f4d\u7f6e\u6dfb\u52a0\u548c\u5220\u9664\u5143\u7d20\uff0c\u56e0\u6b64\u6808\u53ef\u4ee5\u89c6\u4e3a\u4e00\u79cd\u53d7\u9650\u5236\u7684\u6570\u7ec4\u6216\u94fe\u8868\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u6211\u4eec\u53ef\u4ee5\u201c\u5c4f\u853d\u201d\u6570\u7ec4\u6216\u94fe\u8868\u7684\u90e8\u5206\u65e0\u5173\u64cd\u4f5c\uff0c\u4f7f\u5176\u5bf9\u5916\u8868\u73b0\u7684\u903b\u8f91\u7b26\u5408\u6808\u7684\u7279\u6027\u3002
"},{"location":"chapter_stack_and_queue/stack/#1","title":"1. \u00a0 \u57fa\u4e8e\u94fe\u8868\u7684\u5b9e\u73b0","text":"\u4f7f\u7528\u94fe\u8868\u5b9e\u73b0\u6808\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u94fe\u8868\u7684\u5934\u8282\u70b9\u89c6\u4e3a\u6808\u9876\uff0c\u5c3e\u8282\u70b9\u89c6\u4e3a\u6808\u5e95\u3002
\u5982\u56fe 5-2 \u6240\u793a\uff0c\u5bf9\u4e8e\u5165\u6808\u64cd\u4f5c\uff0c\u6211\u4eec\u53ea\u9700\u5c06\u5143\u7d20\u63d2\u5165\u94fe\u8868\u5934\u90e8\uff0c\u8fd9\u79cd\u8282\u70b9\u63d2\u5165\u65b9\u6cd5\u88ab\u79f0\u4e3a\u201c\u5934\u63d2\u6cd5\u201d\u3002\u800c\u5bf9\u4e8e\u51fa\u6808\u64cd\u4f5c\uff0c\u53ea\u9700\u5c06\u5934\u8282\u70b9\u4ece\u94fe\u8868\u4e2d\u5220\u9664\u5373\u53ef\u3002
LinkedListStackpush()pop()\u56fe 5-2 \u00a0 \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u6808\u7684\u5165\u6808\u51fa\u6808\u64cd\u4f5c
\u4ee5\u4e0b\u662f\u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u6808\u7684\u793a\u4f8b\u4ee3\u7801\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig linkedlist_stack.pyclass LinkedListStack:\n \"\"\"\u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808\"\"\"\n\n def __init__(self):\n \"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n self._peek: ListNode | None = None\n self._size: int = 0\n\n def size(self) -> int:\n \"\"\"\u83b7\u53d6\u6808\u7684\u957f\u5ea6\"\"\"\n return self._size\n\n def is_empty(self) -> bool:\n \"\"\"\u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a\"\"\"\n return not self._peek\n\n def push(self, val: int):\n \"\"\"\u5165\u6808\"\"\"\n node = ListNode(val)\n node.next = self._peek\n self._peek = node\n self._size += 1\n\n def pop(self) -> int:\n \"\"\"\u51fa\u6808\"\"\"\n num = self.peek()\n self._peek = self._peek.next\n self._size -= 1\n return num\n\n def peek(self) -> int:\n \"\"\"\u8bbf\u95ee\u6808\u9876\u5143\u7d20\"\"\"\n if self.is_empty():\n raise IndexError(\"\u6808\u4e3a\u7a7a\")\n return self._peek.val\n\n def to_list(self) -> list[int]:\n \"\"\"\u8f6c\u5316\u4e3a\u5217\u8868\u7528\u4e8e\u6253\u5370\"\"\"\n arr = []\n node = self._peek\n while node:\n arr.append(node.val)\n node = node.next\n arr.reverse()\n return arr\n
linkedlist_stack.cpp/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\nclass LinkedListStack {\n private:\n ListNode *stackTop; // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\n int stkSize; // \u6808\u7684\u957f\u5ea6\n\n public:\n LinkedListStack() {\n stackTop = nullptr;\n stkSize = 0;\n }\n\n ~LinkedListStack() {\n // \u904d\u5386\u94fe\u8868\u5220\u9664\u8282\u70b9\uff0c\u91ca\u653e\u5185\u5b58\n freeMemoryLinkedList(stackTop);\n }\n\n /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n int size() {\n return stkSize;\n }\n\n /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n bool isEmpty() {\n return size() == 0;\n }\n\n /* \u5165\u6808 */\n void push(int num) {\n ListNode *node = new ListNode(num);\n node->next = stackTop;\n stackTop = node;\n stkSize++;\n }\n\n /* \u51fa\u6808 */\n void pop() {\n int num = top();\n ListNode *tmp = stackTop;\n stackTop = stackTop->next;\n // \u91ca\u653e\u5185\u5b58\n delete tmp;\n stkSize--;\n }\n\n /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n int top() {\n if (isEmpty())\n throw out_of_range(\"\u6808\u4e3a\u7a7a\");\n return stackTop->val;\n }\n\n /* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n vector<int> toVector() {\n ListNode *node = stackTop;\n vector<int> res(size());\n for (int i = res.size() - 1; i >= 0; i--) {\n res[i] = node->val;\n node = node->next;\n }\n return res;\n }\n};\n
linkedlist_stack.java/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\nclass LinkedListStack {\n private ListNode stackPeek; // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\n private int stkSize = 0; // \u6808\u7684\u957f\u5ea6\n\n public LinkedListStack() {\n stackPeek = null;\n }\n\n /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n public int size() {\n return stkSize;\n }\n\n /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n public boolean isEmpty() {\n return size() == 0;\n }\n\n /* \u5165\u6808 */\n public void push(int num) {\n ListNode node = new ListNode(num);\n node.next = stackPeek;\n stackPeek = node;\n stkSize++;\n }\n\n /* \u51fa\u6808 */\n public int pop() {\n int num = peek();\n stackPeek = stackPeek.next;\n stkSize--;\n return num;\n }\n\n /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n public int peek() {\n if (isEmpty())\n throw new IndexOutOfBoundsException();\n return stackPeek.val;\n }\n\n /* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n public int[] toArray() {\n ListNode node = stackPeek;\n int[] res = new int[size()];\n for (int i = res.length - 1; i >= 0; i--) {\n res[i] = node.val;\n node = node.next;\n }\n return res;\n }\n}\n
linkedlist_stack.cs/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\nclass LinkedListStack {\n ListNode? stackPeek; // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\n int stkSize = 0; // \u6808\u7684\u957f\u5ea6\n\n public LinkedListStack() {\n stackPeek = null;\n }\n\n /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n public int Size() {\n return stkSize;\n }\n\n /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n public bool IsEmpty() {\n return Size() == 0;\n }\n\n /* \u5165\u6808 */\n public void Push(int num) {\n ListNode node = new(num) {\n next = stackPeek\n };\n stackPeek = node;\n stkSize++;\n }\n\n /* \u51fa\u6808 */\n public int Pop() {\n int num = Peek();\n stackPeek = stackPeek!.next;\n stkSize--;\n return num;\n }\n\n /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n public int Peek() {\n if (IsEmpty())\n throw new Exception();\n return stackPeek!.val;\n }\n\n /* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n public int[] ToArray() {\n if (stackPeek == null)\n return [];\n\n ListNode? node = stackPeek;\n int[] res = new int[Size()];\n for (int i = res.Length - 1; i >= 0; i--) {\n res[i] = node!.val;\n node = node.next;\n }\n return res;\n }\n}\n
linkedlist_stack.go/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\ntype linkedListStack struct {\n // \u4f7f\u7528\u5185\u7f6e\u5305 list \u6765\u5b9e\u73b0\u6808\n data *list.List\n}\n\n/* \u521d\u59cb\u5316\u6808 */\nfunc newLinkedListStack() *linkedListStack {\n return &linkedListStack{\n data: list.New(),\n }\n}\n\n/* \u5165\u6808 */\nfunc (s *linkedListStack) push(value int) {\n s.data.PushBack(value)\n}\n\n/* \u51fa\u6808 */\nfunc (s *linkedListStack) pop() any {\n if s.isEmpty() {\n return nil\n }\n e := s.data.Back()\n s.data.Remove(e)\n return e.Value\n}\n\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nfunc (s *linkedListStack) peek() any {\n if s.isEmpty() {\n return nil\n }\n e := s.data.Back()\n return e.Value\n}\n\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nfunc (s *linkedListStack) size() int {\n return s.data.Len()\n}\n\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\nfunc (s *linkedListStack) isEmpty() bool {\n return s.data.Len() == 0\n}\n\n/* \u83b7\u53d6 List \u7528\u4e8e\u6253\u5370 */\nfunc (s *linkedListStack) toList() *list.List {\n return s.data\n}\n
linkedlist_stack.swift/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\nclass LinkedListStack {\n private var _peek: ListNode? // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\n private var _size = 0 // \u6808\u7684\u957f\u5ea6\n\n init() {}\n\n /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n func size() -> Int {\n _size\n }\n\n /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n func isEmpty() -> Bool {\n size() == 0\n }\n\n /* \u5165\u6808 */\n func push(num: Int) {\n let node = ListNode(x: num)\n node.next = _peek\n _peek = node\n _size += 1\n }\n\n /* \u51fa\u6808 */\n @discardableResult\n func pop() -> Int {\n let num = peek()\n _peek = _peek?.next\n _size -= 1\n return num\n }\n\n /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n func peek() -> Int {\n if isEmpty() {\n fatalError(\"\u6808\u4e3a\u7a7a\")\n }\n return _peek!.val\n }\n\n /* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n func toArray() -> [Int] {\n var node = _peek\n var res = Array(repeating: 0, count: _size)\n for i in sequence(first: res.count - 1, next: { $0 >= 0 + 1 ? $0 - 1 : nil }) {\n res[i] = node!.val\n node = node?.next\n }\n return res\n }\n}\n
linkedlist_stack.js/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\nclass LinkedListStack {\n #stackPeek; // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\n #stkSize = 0; // \u6808\u7684\u957f\u5ea6\n\n constructor() {\n this.#stackPeek = null;\n }\n\n /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n get size() {\n return this.#stkSize;\n }\n\n /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n isEmpty() {\n return this.size === 0;\n }\n\n /* \u5165\u6808 */\n push(num) {\n const node = new ListNode(num);\n node.next = this.#stackPeek;\n this.#stackPeek = node;\n this.#stkSize++;\n }\n\n /* \u51fa\u6808 */\n pop() {\n const num = this.peek();\n this.#stackPeek = this.#stackPeek.next;\n this.#stkSize--;\n return num;\n }\n\n /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n peek() {\n if (!this.#stackPeek) throw new Error('\u6808\u4e3a\u7a7a');\n return this.#stackPeek.val;\n }\n\n /* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n toArray() {\n let node = this.#stackPeek;\n const res = new Array(this.size);\n for (let i = res.length - 1; i >= 0; i--) {\n res[i] = node.val;\n node = node.next;\n }\n return res;\n }\n}\n
linkedlist_stack.ts/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\nclass LinkedListStack {\n private stackPeek: ListNode | null; // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\n private stkSize: number = 0; // \u6808\u7684\u957f\u5ea6\n\n constructor() {\n this.stackPeek = null;\n }\n\n /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n get size(): number {\n return this.stkSize;\n }\n\n /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n isEmpty(): boolean {\n return this.size === 0;\n }\n\n /* \u5165\u6808 */\n push(num: number): void {\n const node = new ListNode(num);\n node.next = this.stackPeek;\n this.stackPeek = node;\n this.stkSize++;\n }\n\n /* \u51fa\u6808 */\n pop(): number {\n const num = this.peek();\n if (!this.stackPeek) throw new Error('\u6808\u4e3a\u7a7a');\n this.stackPeek = this.stackPeek.next;\n this.stkSize--;\n return num;\n }\n\n /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n peek(): number {\n if (!this.stackPeek) throw new Error('\u6808\u4e3a\u7a7a');\n return this.stackPeek.val;\n }\n\n /* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n toArray(): number[] {\n let node = this.stackPeek;\n const res = new Array<number>(this.size);\n for (let i = res.length - 1; i >= 0; i--) {\n res[i] = node!.val;\n node = node!.next;\n }\n return res;\n }\n}\n
linkedlist_stack.dart/* \u57fa\u4e8e\u94fe\u8868\u7c7b\u5b9e\u73b0\u7684\u6808 */\nclass LinkedListStack {\n ListNode? _stackPeek; // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\n int _stkSize = 0; // \u6808\u7684\u957f\u5ea6\n\n LinkedListStack() {\n _stackPeek = null;\n }\n\n /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n int size() {\n return _stkSize;\n }\n\n /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n bool isEmpty() {\n return _stkSize == 0;\n }\n\n /* \u5165\u6808 */\n void push(int _num) {\n final ListNode node = ListNode(_num);\n node.next = _stackPeek;\n _stackPeek = node;\n _stkSize++;\n }\n\n /* \u51fa\u6808 */\n int pop() {\n final int _num = peek();\n _stackPeek = _stackPeek!.next;\n _stkSize--;\n return _num;\n }\n\n /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n int peek() {\n if (_stackPeek == null) {\n throw Exception(\"\u6808\u4e3a\u7a7a\");\n }\n return _stackPeek!.val;\n }\n\n /* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a List \u5e76\u8fd4\u56de */\n List<int> toList() {\n ListNode? node = _stackPeek;\n List<int> list = [];\n while (node != null) {\n list.add(node.val);\n node = node.next;\n }\n list = list.reversed.toList();\n return list;\n }\n}\n
linkedlist_stack.rs/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\n#[allow(dead_code)]\npub struct LinkedListStack<T> {\n stack_peek: Option<Rc<RefCell<ListNode<T>>>>, // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\n stk_size: usize, // \u6808\u7684\u957f\u5ea6\n}\n\nimpl<T: Copy> LinkedListStack<T> {\n pub fn new() -> Self {\n Self {\n stack_peek: None,\n stk_size: 0,\n }\n }\n\n /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n pub fn size(&self) -> usize {\n return self.stk_size;\n }\n\n /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n pub fn is_empty(&self) -> bool {\n return self.size() == 0;\n }\n\n /* \u5165\u6808 */\n pub fn push(&mut self, num: T) {\n let node = ListNode::new(num);\n node.borrow_mut().next = self.stack_peek.take();\n self.stack_peek = Some(node);\n self.stk_size += 1;\n }\n\n /* \u51fa\u6808 */\n pub fn pop(&mut self) -> Option<T> {\n self.stack_peek.take().map(|old_head| {\n match old_head.borrow_mut().next.take() {\n Some(new_head) => {\n self.stack_peek = Some(new_head);\n }\n None => {\n self.stack_peek = None;\n }\n }\n self.stk_size -= 1;\n Rc::try_unwrap(old_head).ok().unwrap().into_inner().val\n })\n }\n\n /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n pub fn peek(&self) -> Option<&Rc<RefCell<ListNode<T>>>> {\n self.stack_peek.as_ref()\n }\n\n /* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n pub fn to_array(&self, head: Option<&Rc<RefCell<ListNode<T>>>>) -> Vec<T> {\n if let Some(node) = head {\n let mut nums = self.to_array(node.borrow().next.as_ref());\n nums.push(node.borrow().val);\n return nums;\n }\n return Vec::new();\n }\n}\n
linkedlist_stack.c/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\ntypedef struct {\n ListNode *top; // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\n int size; // \u6808\u7684\u957f\u5ea6\n} LinkedListStack;\n\n/* \u6784\u9020\u51fd\u6570 */\nLinkedListStack *newLinkedListStack() {\n LinkedListStack *s = malloc(sizeof(LinkedListStack));\n s->top = NULL;\n s->size = 0;\n return s;\n}\n\n/* \u6790\u6784\u51fd\u6570 */\nvoid delLinkedListStack(LinkedListStack *s) {\n while (s->top) {\n ListNode *n = s->top->next;\n free(s->top);\n s->top = n;\n }\n free(s);\n}\n\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nint size(LinkedListStack *s) {\n assert(s);\n return s->size;\n}\n\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty(LinkedListStack *s) {\n assert(s);\n return size(s) == 0;\n}\n\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nint peek(LinkedListStack *s) {\n assert(s);\n assert(size(s) != 0);\n return s->top->val;\n}\n\n/* \u5165\u6808 */\nvoid push(LinkedListStack *s, int num) {\n assert(s);\n ListNode *node = (ListNode *)malloc(sizeof(ListNode));\n node->next = s->top; // \u66f4\u65b0\u65b0\u52a0\u8282\u70b9\u6307\u9488\u57df\n node->val = num; // \u66f4\u65b0\u65b0\u52a0\u8282\u70b9\u6570\u636e\u57df\n s->top = node; // \u66f4\u65b0\u6808\u9876\n s->size++; // \u66f4\u65b0\u6808\u5927\u5c0f\n}\n\n/* \u51fa\u6808 */\nint pop(LinkedListStack *s) {\n if (s->size == 0) {\n printf(\"stack is empty.\\n\");\n return INT_MAX;\n }\n assert(s);\n int val = peek(s);\n ListNode *tmp = s->top;\n s->top = s->top->next;\n // \u91ca\u653e\u5185\u5b58\n free(tmp);\n s->size--;\n return val;\n}\n
linkedlist_stack.zig// \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808\nfn LinkedListStack(comptime T: type) type {\n return struct {\n const Self = @This();\n\n stack_top: ?*inc.ListNode(T) = null, // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\n stk_size: usize = 0, // \u6808\u7684\u957f\u5ea6\n mem_arena: ?std.heap.ArenaAllocator = null,\n mem_allocator: std.mem.Allocator = undefined, // \u5185\u5b58\u5206\u914d\u5668\n\n // \u6784\u9020\u51fd\u6570\uff08\u5206\u914d\u5185\u5b58+\u521d\u59cb\u5316\u6808\uff09\n pub fn init(self: *Self, allocator: std.mem.Allocator) !void {\n if (self.mem_arena == null) {\n self.mem_arena = std.heap.ArenaAllocator.init(allocator);\n self.mem_allocator = self.mem_arena.?.allocator();\n }\n self.stack_top = null;\n self.stk_size = 0;\n }\n\n // \u6790\u6784\u51fd\u6570\uff08\u91ca\u653e\u5185\u5b58\uff09\n pub fn deinit(self: *Self) void {\n if (self.mem_arena == null) return;\n self.mem_arena.?.deinit();\n }\n\n // \u83b7\u53d6\u6808\u7684\u957f\u5ea6\n pub fn size(self: *Self) usize {\n return self.stk_size;\n }\n\n // \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a\n pub fn isEmpty(self: *Self) bool {\n return self.size() == 0;\n }\n\n // \u8bbf\u95ee\u6808\u9876\u5143\u7d20\n pub fn peek(self: *Self) T {\n if (self.size() == 0) @panic(\"\u6808\u4e3a\u7a7a\");\n return self.stack_top.?.val;\n } \n\n // \u5165\u6808\n pub fn push(self: *Self, num: T) !void {\n var node = try self.mem_allocator.create(inc.ListNode(T));\n node.init(num);\n node.next = self.stack_top;\n self.stack_top = node;\n self.stk_size += 1;\n } \n\n // \u51fa\u6808\n pub fn pop(self: *Self) T {\n var num = self.peek();\n self.stack_top = self.stack_top.?.next;\n self.stk_size -= 1;\n return num;\n } \n\n // \u5c06\u6808\u8f6c\u6362\u4e3a\u6570\u7ec4\n pub fn toArray(self: *Self) ![]T {\n var node = self.stack_top;\n var res = try self.mem_allocator.alloc(T, self.size());\n @memset(res, @as(T, 0));\n var i: usize = 0;\n while (i < res.len) : (i += 1) {\n res[res.len - i - 1] = node.?.val;\n node = node.?.next;\n }\n return res;\n }\n };\n}\n
"},{"location":"chapter_stack_and_queue/stack/#2","title":"2. \u00a0 \u57fa\u4e8e\u6570\u7ec4\u7684\u5b9e\u73b0","text":"\u4f7f\u7528\u6570\u7ec4\u5b9e\u73b0\u6808\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u6570\u7ec4\u7684\u5c3e\u90e8\u4f5c\u4e3a\u6808\u9876\u3002\u5982\u56fe 5-3 \u6240\u793a\uff0c\u5165\u6808\u4e0e\u51fa\u6808\u64cd\u4f5c\u5206\u522b\u5bf9\u5e94\u5728\u6570\u7ec4\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20\u4e0e\u5220\u9664\u5143\u7d20\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u90fd\u4e3a \\(O(1)\\) \u3002
ArrayStackpush()pop()\u56fe 5-3 \u00a0 \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u6808\u7684\u5165\u6808\u51fa\u6808\u64cd\u4f5c
\u7531\u4e8e\u5165\u6808\u7684\u5143\u7d20\u53ef\u80fd\u4f1a\u6e90\u6e90\u4e0d\u65ad\u5730\u589e\u52a0\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u52a8\u6001\u6570\u7ec4\uff0c\u8fd9\u6837\u5c31\u65e0\u987b\u81ea\u884c\u5904\u7406\u6570\u7ec4\u6269\u5bb9\u95ee\u9898\u3002\u4ee5\u4e0b\u4e3a\u793a\u4f8b\u4ee3\u7801\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig array_stack.pyclass ArrayStack:\n \"\"\"\u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808\"\"\"\n\n def __init__(self):\n \"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n self._stack: list[int] = []\n\n def size(self) -> int:\n \"\"\"\u83b7\u53d6\u6808\u7684\u957f\u5ea6\"\"\"\n return len(self._stack)\n\n def is_empty(self) -> bool:\n \"\"\"\u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a\"\"\"\n return self._stack == []\n\n def push(self, item: int):\n \"\"\"\u5165\u6808\"\"\"\n self._stack.append(item)\n\n def pop(self) -> int:\n \"\"\"\u51fa\u6808\"\"\"\n if self.is_empty():\n raise IndexError(\"\u6808\u4e3a\u7a7a\")\n return self._stack.pop()\n\n def peek(self) -> int:\n \"\"\"\u8bbf\u95ee\u6808\u9876\u5143\u7d20\"\"\"\n if self.is_empty():\n raise IndexError(\"\u6808\u4e3a\u7a7a\")\n return self._stack[-1]\n\n def to_list(self) -> list[int]:\n \"\"\"\u8fd4\u56de\u5217\u8868\u7528\u4e8e\u6253\u5370\"\"\"\n return self._stack\n
array_stack.cpp/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nclass ArrayStack {\n private:\n vector<int> stack;\n\n public:\n /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n int size() {\n return stack.size();\n }\n\n /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n bool isEmpty() {\n return stack.size() == 0;\n }\n\n /* \u5165\u6808 */\n void push(int num) {\n stack.push_back(num);\n }\n\n /* \u51fa\u6808 */\n void pop() {\n int oldTop = top();\n stack.pop_back();\n }\n\n /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n int top() {\n if (isEmpty())\n throw out_of_range(\"\u6808\u4e3a\u7a7a\");\n return stack.back();\n }\n\n /* \u8fd4\u56de Vector */\n vector<int> toVector() {\n return stack;\n }\n};\n
array_stack.java/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nclass ArrayStack {\n private ArrayList<Integer> stack;\n\n public ArrayStack() {\n // \u521d\u59cb\u5316\u5217\u8868\uff08\u52a8\u6001\u6570\u7ec4\uff09\n stack = new ArrayList<>();\n }\n\n /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n public int size() {\n return stack.size();\n }\n\n /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n public boolean isEmpty() {\n return size() == 0;\n }\n\n /* \u5165\u6808 */\n public void push(int num) {\n stack.add(num);\n }\n\n /* \u51fa\u6808 */\n public int pop() {\n if (isEmpty())\n throw new IndexOutOfBoundsException();\n return stack.remove(size() - 1);\n }\n\n /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n public int peek() {\n if (isEmpty())\n throw new IndexOutOfBoundsException();\n return stack.get(size() - 1);\n }\n\n /* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n public Object[] toArray() {\n return stack.toArray();\n }\n}\n
array_stack.cs/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nclass ArrayStack {\n List<int> stack;\n public ArrayStack() {\n // \u521d\u59cb\u5316\u5217\u8868\uff08\u52a8\u6001\u6570\u7ec4\uff09\n stack = [];\n }\n\n /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n public int Size() {\n return stack.Count;\n }\n\n /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n public bool IsEmpty() {\n return Size() == 0;\n }\n\n /* \u5165\u6808 */\n public void Push(int num) {\n stack.Add(num);\n }\n\n /* \u51fa\u6808 */\n public int Pop() {\n if (IsEmpty())\n throw new Exception();\n var val = Peek();\n stack.RemoveAt(Size() - 1);\n return val;\n }\n\n /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n public int Peek() {\n if (IsEmpty())\n throw new Exception();\n return stack[Size() - 1];\n }\n\n /* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n public int[] ToArray() {\n return [.. stack];\n }\n}\n
array_stack.go/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\ntype arrayStack struct {\n data []int // \u6570\u636e\n}\n\n/* \u521d\u59cb\u5316\u6808 */\nfunc newArrayStack() *arrayStack {\n return &arrayStack{\n // \u8bbe\u7f6e\u6808\u7684\u957f\u5ea6\u4e3a 0\uff0c\u5bb9\u91cf\u4e3a 16\n data: make([]int, 0, 16),\n }\n}\n\n/* \u6808\u7684\u957f\u5ea6 */\nfunc (s *arrayStack) size() int {\n return len(s.data)\n}\n\n/* \u6808\u662f\u5426\u4e3a\u7a7a */\nfunc (s *arrayStack) isEmpty() bool {\n return s.size() == 0\n}\n\n/* \u5165\u6808 */\nfunc (s *arrayStack) push(v int) {\n // \u5207\u7247\u4f1a\u81ea\u52a8\u6269\u5bb9\n s.data = append(s.data, v)\n}\n\n/* \u51fa\u6808 */\nfunc (s *arrayStack) pop() any {\n val := s.peek()\n s.data = s.data[:len(s.data)-1]\n return val\n}\n\n/* \u83b7\u53d6\u6808\u9876\u5143\u7d20 */\nfunc (s *arrayStack) peek() any {\n if s.isEmpty() {\n return nil\n }\n val := s.data[len(s.data)-1]\n return val\n}\n\n/* \u83b7\u53d6 Slice \u7528\u4e8e\u6253\u5370 */\nfunc (s *arrayStack) toSlice() []int {\n return s.data\n}\n
array_stack.swift/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nclass ArrayStack {\n private var stack: [Int]\n\n init() {\n // \u521d\u59cb\u5316\u5217\u8868\uff08\u52a8\u6001\u6570\u7ec4\uff09\n stack = []\n }\n\n /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n func size() -> Int {\n stack.count\n }\n\n /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n func isEmpty() -> Bool {\n stack.isEmpty\n }\n\n /* \u5165\u6808 */\n func push(num: Int) {\n stack.append(num)\n }\n\n /* \u51fa\u6808 */\n @discardableResult\n func pop() -> Int {\n if isEmpty() {\n fatalError(\"\u6808\u4e3a\u7a7a\")\n }\n return stack.removeLast()\n }\n\n /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n func peek() -> Int {\n if isEmpty() {\n fatalError(\"\u6808\u4e3a\u7a7a\")\n }\n return stack.last!\n }\n\n /* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n func toArray() -> [Int] {\n stack\n }\n}\n
array_stack.js/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nclass ArrayStack {\n #stack;\n constructor() {\n this.#stack = [];\n }\n\n /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n get size() {\n return this.#stack.length;\n }\n\n /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n isEmpty() {\n return this.#stack.length === 0;\n }\n\n /* \u5165\u6808 */\n push(num) {\n this.#stack.push(num);\n }\n\n /* \u51fa\u6808 */\n pop() {\n if (this.isEmpty()) throw new Error('\u6808\u4e3a\u7a7a');\n return this.#stack.pop();\n }\n\n /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n top() {\n if (this.isEmpty()) throw new Error('\u6808\u4e3a\u7a7a');\n return this.#stack[this.#stack.length - 1];\n }\n\n /* \u8fd4\u56de Array */\n toArray() {\n return this.#stack;\n }\n}\n
array_stack.ts/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nclass ArrayStack {\n private stack: number[];\n constructor() {\n this.stack = [];\n }\n\n /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n get size(): number {\n return this.stack.length;\n }\n\n /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n isEmpty(): boolean {\n return this.stack.length === 0;\n }\n\n /* \u5165\u6808 */\n push(num: number): void {\n this.stack.push(num);\n }\n\n /* \u51fa\u6808 */\n pop(): number | undefined {\n if (this.isEmpty()) throw new Error('\u6808\u4e3a\u7a7a');\n return this.stack.pop();\n }\n\n /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n top(): number | undefined {\n if (this.isEmpty()) throw new Error('\u6808\u4e3a\u7a7a');\n return this.stack[this.stack.length - 1];\n }\n\n /* \u8fd4\u56de Array */\n toArray() {\n return this.stack;\n }\n}\n
array_stack.dart/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nclass ArrayStack {\n late List<int> _stack;\n ArrayStack() {\n _stack = [];\n }\n\n /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n int size() {\n return _stack.length;\n }\n\n /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n bool isEmpty() {\n return _stack.isEmpty;\n }\n\n /* \u5165\u6808 */\n void push(int _num) {\n _stack.add(_num);\n }\n\n /* \u51fa\u6808 */\n int pop() {\n if (isEmpty()) {\n throw Exception(\"\u6808\u4e3a\u7a7a\");\n }\n return _stack.removeLast();\n }\n\n /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n int peek() {\n if (isEmpty()) {\n throw Exception(\"\u6808\u4e3a\u7a7a\");\n }\n return _stack.last;\n }\n\n /* \u5c06\u6808\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n List<int> toArray() => _stack;\n}\n
array_stack.rs/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nstruct ArrayStack<T> {\n stack: Vec<T>,\n}\n\nimpl<T> ArrayStack<T> {\n /* \u521d\u59cb\u5316\u6808 */\n fn new() -> ArrayStack<T> {\n ArrayStack::<T> { stack: Vec::<T>::new() }\n }\n\n /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n fn size(&self) -> usize {\n self.stack.len()\n }\n\n /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n fn is_empty(&self) -> bool {\n self.size() == 0\n }\n\n /* \u5165\u6808 */\n fn push(&mut self, num: T) {\n self.stack.push(num);\n }\n\n /* \u51fa\u6808 */\n fn pop(&mut self) -> Option<T> {\n match self.stack.pop() {\n Some(num) => Some(num),\n None => None,\n }\n }\n\n /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n fn peek(&self) -> Option<&T> {\n if self.is_empty() { panic!(\"\u6808\u4e3a\u7a7a\") };\n self.stack.last()\n }\n\n /* \u8fd4\u56de &Vec */\n fn to_array(&self) -> &Vec<T> {\n &self.stack\n }\n}\n
array_stack.c/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\ntypedef struct {\n int *data;\n int size;\n} ArrayStack;\n\n/* \u6784\u9020\u51fd\u6570 */\nArrayStack *newArrayStack() {\n ArrayStack *stack = malloc(sizeof(ArrayStack));\n // \u521d\u59cb\u5316\u4e00\u4e2a\u5927\u5bb9\u91cf\uff0c\u907f\u514d\u6269\u5bb9\n stack->data = malloc(sizeof(int) * MAX_SIZE);\n stack->size = 0;\n return stack;\n}\n\n/* \u6790\u6784\u51fd\u6570 */\nvoid delArrayStack(ArrayStack *stack) {\n free(stack->data);\n free(stack);\n}\n\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nint size(ArrayStack *stack) {\n return stack->size;\n}\n\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty(ArrayStack *stack) {\n return stack->size == 0;\n}\n\n/* \u5165\u6808 */\nvoid push(ArrayStack *stack, int num) {\n if (stack->size == MAX_SIZE) {\n printf(\"stack is full.\\n\");\n return;\n }\n stack->data[stack->size] = num;\n stack->size++;\n}\n\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nint peek(ArrayStack *stack) {\n if (stack->size == 0) {\n printf(\"stack is empty.\\n\");\n return INT_MAX;\n }\n return stack->data[stack->size - 1];\n}\n\n/* \u51fa\u6808 */\nint pop(ArrayStack *stack) {\n if (stack->size == 0) {\n printf(\"stack is empty.\\n\");\n return INT_MAX;\n }\n int val = peek(stack);\n stack->size--;\n return val;\n}\n
array_stack.zig// \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808\nfn ArrayStack(comptime T: type) type {\n return struct {\n const Self = @This();\n\n stack: ?std.ArrayList(T) = null, \n\n // \u6784\u9020\u65b9\u6cd5\uff08\u5206\u914d\u5185\u5b58+\u521d\u59cb\u5316\u6808\uff09\n pub fn init(self: *Self, allocator: std.mem.Allocator) void {\n if (self.stack == null) {\n self.stack = std.ArrayList(T).init(allocator);\n }\n }\n\n // \u6790\u6784\u65b9\u6cd5\uff08\u91ca\u653e\u5185\u5b58\uff09\n pub fn deinit(self: *Self) void {\n if (self.stack == null) return;\n self.stack.?.deinit();\n }\n\n // \u83b7\u53d6\u6808\u7684\u957f\u5ea6\n pub fn size(self: *Self) usize {\n return self.stack.?.items.len;\n }\n\n // \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a\n pub fn isEmpty(self: *Self) bool {\n return self.size() == 0;\n }\n\n // \u8bbf\u95ee\u6808\u9876\u5143\u7d20\n pub fn peek(self: *Self) T {\n if (self.isEmpty()) @panic(\"\u6808\u4e3a\u7a7a\");\n return self.stack.?.items[self.size() - 1];\n } \n\n // \u5165\u6808\n pub fn push(self: *Self, num: T) !void {\n try self.stack.?.append(num);\n } \n\n // \u51fa\u6808\n pub fn pop(self: *Self) T {\n var num = self.stack.?.pop();\n return num;\n } \n\n // \u8fd4\u56de ArrayList\n pub fn toList(self: *Self) std.ArrayList(T) {\n return self.stack.?;\n }\n };\n}\n
"},{"location":"chapter_stack_and_queue/stack/#513","title":"5.1.3 \u00a0 \u4e24\u79cd\u5b9e\u73b0\u5bf9\u6bd4","text":"\u652f\u6301\u64cd\u4f5c
\u4e24\u79cd\u5b9e\u73b0\u90fd\u652f\u6301\u6808\u5b9a\u4e49\u4e2d\u7684\u5404\u9879\u64cd\u4f5c\u3002\u6570\u7ec4\u5b9e\u73b0\u989d\u5916\u652f\u6301\u968f\u673a\u8bbf\u95ee\uff0c\u4f46\u8fd9\u5df2\u8d85\u51fa\u4e86\u6808\u7684\u5b9a\u4e49\u8303\u7574\uff0c\u56e0\u6b64\u4e00\u822c\u4e0d\u4f1a\u7528\u5230\u3002
\u65f6\u95f4\u6548\u7387
\u5728\u57fa\u4e8e\u6570\u7ec4\u7684\u5b9e\u73b0\u4e2d\uff0c\u5165\u6808\u548c\u51fa\u6808\u64cd\u4f5c\u90fd\u5728\u9884\u5148\u5206\u914d\u597d\u7684\u8fde\u7eed\u5185\u5b58\u4e2d\u8fdb\u884c\uff0c\u5177\u6709\u5f88\u597d\u7684\u7f13\u5b58\u672c\u5730\u6027\uff0c\u56e0\u6b64\u6548\u7387\u8f83\u9ad8\u3002\u7136\u800c\uff0c\u5982\u679c\u5165\u6808\u65f6\u8d85\u51fa\u6570\u7ec4\u5bb9\u91cf\uff0c\u4f1a\u89e6\u53d1\u6269\u5bb9\u673a\u5236\uff0c\u5bfc\u81f4\u8be5\u6b21\u5165\u6808\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u53d8\u4e3a \\(O(n)\\) \u3002
\u5728\u57fa\u4e8e\u94fe\u8868\u7684\u5b9e\u73b0\u4e2d\uff0c\u94fe\u8868\u7684\u6269\u5bb9\u975e\u5e38\u7075\u6d3b\uff0c\u4e0d\u5b58\u5728\u4e0a\u8ff0\u6570\u7ec4\u6269\u5bb9\u65f6\u6548\u7387\u964d\u4f4e\u7684\u95ee\u9898\u3002\u4f46\u662f\uff0c\u5165\u6808\u64cd\u4f5c\u9700\u8981\u521d\u59cb\u5316\u8282\u70b9\u5bf9\u8c61\u5e76\u4fee\u6539\u6307\u9488\uff0c\u56e0\u6b64\u6548\u7387\u76f8\u5bf9\u8f83\u4f4e\u3002\u4e0d\u8fc7\uff0c\u5982\u679c\u5165\u6808\u5143\u7d20\u672c\u8eab\u5c31\u662f\u8282\u70b9\u5bf9\u8c61\uff0c\u90a3\u4e48\u53ef\u4ee5\u7701\u53bb\u521d\u59cb\u5316\u6b65\u9aa4\uff0c\u4ece\u800c\u63d0\u9ad8\u6548\u7387\u3002
\u7efc\u4e0a\u6240\u8ff0\uff0c\u5f53\u5165\u6808\u4e0e\u51fa\u6808\u64cd\u4f5c\u7684\u5143\u7d20\u662f\u57fa\u672c\u6570\u636e\u7c7b\u578b\u65f6\uff0c\u4f8b\u5982 int
\u6216 double
\uff0c\u6211\u4eec\u53ef\u4ee5\u5f97\u51fa\u4ee5\u4e0b\u7ed3\u8bba\u3002
\u7a7a\u95f4\u6548\u7387
\u5728\u521d\u59cb\u5316\u5217\u8868\u65f6\uff0c\u7cfb\u7edf\u4f1a\u4e3a\u5217\u8868\u5206\u914d\u201c\u521d\u59cb\u5bb9\u91cf\u201d\uff0c\u8be5\u5bb9\u91cf\u53ef\u80fd\u8d85\u51fa\u5b9e\u9645\u9700\u6c42\uff1b\u5e76\u4e14\uff0c\u6269\u5bb9\u673a\u5236\u901a\u5e38\u662f\u6309\u7167\u7279\u5b9a\u500d\u7387\uff08\u4f8b\u5982 2 \u500d\uff09\u8fdb\u884c\u6269\u5bb9\u7684\uff0c\u6269\u5bb9\u540e\u7684\u5bb9\u91cf\u4e5f\u53ef\u80fd\u8d85\u51fa\u5b9e\u9645\u9700\u6c42\u3002\u56e0\u6b64\uff0c\u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808\u53ef\u80fd\u9020\u6210\u4e00\u5b9a\u7684\u7a7a\u95f4\u6d6a\u8d39\u3002
\u7136\u800c\uff0c\u7531\u4e8e\u94fe\u8868\u8282\u70b9\u9700\u8981\u989d\u5916\u5b58\u50a8\u6307\u9488\uff0c\u56e0\u6b64\u94fe\u8868\u8282\u70b9\u5360\u7528\u7684\u7a7a\u95f4\u76f8\u5bf9\u8f83\u5927\u3002
\u7efc\u4e0a\uff0c\u6211\u4eec\u4e0d\u80fd\u7b80\u5355\u5730\u786e\u5b9a\u54ea\u79cd\u5b9e\u73b0\u66f4\u52a0\u8282\u7701\u5185\u5b58\uff0c\u9700\u8981\u9488\u5bf9\u5177\u4f53\u60c5\u51b5\u8fdb\u884c\u5206\u6790\u3002
"},{"location":"chapter_stack_and_queue/stack/#514","title":"5.1.4 \u00a0 \u6808\u5178\u578b\u5e94\u7528","text":"\u6d4f\u89c8\u5668\u7684\u524d\u8fdb\u540e\u9000\u662f\u5426\u662f\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\uff1f
\u6d4f\u89c8\u5668\u7684\u524d\u8fdb\u540e\u9000\u529f\u80fd\u672c\u8d28\u4e0a\u662f\u201c\u6808\u201d\u7684\u4f53\u73b0\u3002\u5f53\u7528\u6237\u8bbf\u95ee\u4e00\u4e2a\u65b0\u9875\u9762\u65f6\uff0c\u8be5\u9875\u9762\u4f1a\u88ab\u6dfb\u52a0\u5230\u6808\u9876\uff1b\u5f53\u7528\u6237\u70b9\u51fb\u540e\u9000\u6309\u94ae\u65f6\uff0c\u8be5\u9875\u9762\u4f1a\u4ece\u6808\u9876\u5f39\u51fa\u3002\u4f7f\u7528\u53cc\u5411\u961f\u5217\u53ef\u4ee5\u65b9\u4fbf\u5730\u5b9e\u73b0\u4e00\u4e9b\u989d\u5916\u64cd\u4f5c\uff0c\u8fd9\u4e2a\u5728\u201c\u53cc\u5411\u961f\u5217\u201d\u7ae0\u8282\u6709\u63d0\u5230\u3002
\u5728\u51fa\u6808\u540e\uff0c\u662f\u5426\u9700\u8981\u91ca\u653e\u51fa\u6808\u8282\u70b9\u7684\u5185\u5b58\uff1f
\u5982\u679c\u540e\u7eed\u4ecd\u9700\u8981\u4f7f\u7528\u5f39\u51fa\u8282\u70b9\uff0c\u5219\u4e0d\u9700\u8981\u91ca\u653e\u5185\u5b58\u3002\u82e5\u4e4b\u540e\u4e0d\u9700\u8981\u7528\u5230\uff0cJava
\u548c Python
\u7b49\u8bed\u8a00\u62e5\u6709\u81ea\u52a8\u5783\u573e\u56de\u6536\u673a\u5236\uff0c\u56e0\u6b64\u4e0d\u9700\u8981\u624b\u52a8\u91ca\u653e\u5185\u5b58\uff1b\u5728 C
\u548c C++
\u4e2d\u9700\u8981\u624b\u52a8\u91ca\u653e\u5185\u5b58\u3002
\u53cc\u5411\u961f\u5217\u50cf\u662f\u4e24\u4e2a\u6808\u62fc\u63a5\u5728\u4e86\u4e00\u8d77\uff0c\u5b83\u7684\u7528\u9014\u662f\u4ec0\u4e48\uff1f
\u53cc\u5411\u961f\u5217\u5c31\u50cf\u662f\u6808\u548c\u961f\u5217\u7684\u7ec4\u5408\uff0c\u6216\u4e24\u4e2a\u6808\u62fc\u5728\u4e86\u4e00\u8d77\u3002\u5b83\u8868\u73b0\u7684\u662f\u6808 + \u961f\u5217\u7684\u903b\u8f91\uff0c\u56e0\u6b64\u53ef\u4ee5\u5b9e\u73b0\u6808\u4e0e\u961f\u5217\u7684\u6240\u6709\u5e94\u7528\uff0c\u5e76\u4e14\u66f4\u52a0\u7075\u6d3b\u3002
\u64a4\u9500\uff08undo\uff09\u548c\u53cd\u64a4\u9500\uff08redo\uff09\u5177\u4f53\u662f\u5982\u4f55\u5b9e\u73b0\u7684\uff1f
\u4f7f\u7528\u4e24\u4e2a\u5806\u6808\uff0c\u6808 A
\u7528\u4e8e\u64a4\u9500\uff0c\u6808 B
\u7528\u4e8e\u53cd\u64a4\u9500\u3002
A
\uff0c\u5e76\u6e05\u7a7a\u6808 B
\u3002A
\u4e2d\u5f39\u51fa\u6700\u8fd1\u7684\u64cd\u4f5c\uff0c\u5e76\u5c06\u5176\u538b\u5165\u6808 B
\u3002B
\u4e2d\u5f39\u51fa\u6700\u8fd1\u7684\u64cd\u4f5c\uff0c\u5e76\u5c06\u5176\u538b\u5165\u6808 A
\u3002Abstract
\u53c2\u5929\u5927\u6811\u5145\u6ee1\u751f\u547d\u529b\uff0c\u6839\u6df1\u53f6\u8302\uff0c\u5206\u679d\u6276\u758f\u3002
\u5b83\u4e3a\u6211\u4eec\u5c55\u73b0\u4e86\u6570\u636e\u5206\u6cbb\u7684\u751f\u52a8\u5f62\u6001\u3002
"},{"location":"chapter_tree/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"\u5728\u94fe\u8868\u8868\u793a\u4e0b\uff0c\u4e8c\u53c9\u6811\u7684\u5b58\u50a8\u5355\u5143\u4e3a\u8282\u70b9 TreeNode
\uff0c\u8282\u70b9\u4e4b\u95f4\u901a\u8fc7\u6307\u9488\u76f8\u8fde\u63a5\u3002\u4e0a\u4e00\u8282\u4ecb\u7ecd\u4e86\u94fe\u8868\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7684\u5404\u9879\u57fa\u672c\u64cd\u4f5c\u3002
\u90a3\u4e48\uff0c\u6211\u4eec\u80fd\u5426\u7528\u6570\u7ec4\u6765\u8868\u793a\u4e8c\u53c9\u6811\u5462\uff1f\u7b54\u6848\u662f\u80af\u5b9a\u7684\u3002
"},{"location":"chapter_tree/array_representation_of_tree/#731","title":"7.3.1 \u00a0 \u8868\u793a\u5b8c\u7f8e\u4e8c\u53c9\u6811","text":"\u5148\u5206\u6790\u4e00\u4e2a\u7b80\u5355\u6848\u4f8b\u3002\u7ed9\u5b9a\u4e00\u68f5\u5b8c\u7f8e\u4e8c\u53c9\u6811\uff0c\u6211\u4eec\u5c06\u6240\u6709\u8282\u70b9\u6309\u7167\u5c42\u5e8f\u904d\u5386\u7684\u987a\u5e8f\u5b58\u50a8\u5728\u4e00\u4e2a\u6570\u7ec4\u4e2d\uff0c\u5219\u6bcf\u4e2a\u8282\u70b9\u90fd\u5bf9\u5e94\u552f\u4e00\u7684\u6570\u7ec4\u7d22\u5f15\u3002
\u6839\u636e\u5c42\u5e8f\u904d\u5386\u7684\u7279\u6027\uff0c\u6211\u4eec\u53ef\u4ee5\u63a8\u5bfc\u51fa\u7236\u8282\u70b9\u7d22\u5f15\u4e0e\u5b50\u8282\u70b9\u7d22\u5f15\u4e4b\u95f4\u7684\u201c\u6620\u5c04\u516c\u5f0f\u201d\uff1a\u82e5\u67d0\u8282\u70b9\u7684\u7d22\u5f15\u4e3a \\(i\\) \uff0c\u5219\u8be5\u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7d22\u5f15\u4e3a \\(2i + 1\\) \uff0c\u53f3\u5b50\u8282\u70b9\u7d22\u5f15\u4e3a \\(2i + 2\\) \u3002\u56fe 7-12 \u5c55\u793a\u4e86\u5404\u4e2a\u8282\u70b9\u7d22\u5f15\u4e4b\u95f4\u7684\u6620\u5c04\u5173\u7cfb\u3002
\u56fe 7-12 \u00a0 \u5b8c\u7f8e\u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a
\u6620\u5c04\u516c\u5f0f\u7684\u89d2\u8272\u76f8\u5f53\u4e8e\u94fe\u8868\u4e2d\u7684\u6307\u9488\u3002\u7ed9\u5b9a\u6570\u7ec4\u4e2d\u7684\u4efb\u610f\u4e00\u4e2a\u8282\u70b9\uff0c\u6211\u4eec\u90fd\u53ef\u4ee5\u901a\u8fc7\u6620\u5c04\u516c\u5f0f\u6765\u8bbf\u95ee\u5b83\u7684\u5de6\uff08\u53f3\uff09\u5b50\u8282\u70b9\u3002
"},{"location":"chapter_tree/array_representation_of_tree/#732","title":"7.3.2 \u00a0 \u8868\u793a\u4efb\u610f\u4e8c\u53c9\u6811","text":"\u5b8c\u7f8e\u4e8c\u53c9\u6811\u662f\u4e00\u4e2a\u7279\u4f8b\uff0c\u5728\u4e8c\u53c9\u6811\u7684\u4e2d\u95f4\u5c42\u901a\u5e38\u5b58\u5728\u8bb8\u591a \\(\\text{None}\\) \u3002\u7531\u4e8e\u5c42\u5e8f\u904d\u5386\u5e8f\u5217\u5e76\u4e0d\u5305\u542b\u8fd9\u4e9b \\(\\text{None}\\) \uff0c\u56e0\u6b64\u6211\u4eec\u65e0\u6cd5\u4ec5\u51ed\u8be5\u5e8f\u5217\u6765\u63a8\u6d4b \\(\\text{None}\\) \u7684\u6570\u91cf\u548c\u5206\u5e03\u4f4d\u7f6e\u3002\u8fd9\u610f\u5473\u7740\u5b58\u5728\u591a\u79cd\u4e8c\u53c9\u6811\u7ed3\u6784\u90fd\u7b26\u5408\u8be5\u5c42\u5e8f\u904d\u5386\u5e8f\u5217\u3002
\u5982\u56fe 7-13 \u6240\u793a\uff0c\u7ed9\u5b9a\u4e00\u68f5\u975e\u5b8c\u7f8e\u4e8c\u53c9\u6811\uff0c\u4e0a\u8ff0\u6570\u7ec4\u8868\u793a\u65b9\u6cd5\u5df2\u7ecf\u5931\u6548\u3002
\u56fe 7-13 \u00a0 \u5c42\u5e8f\u904d\u5386\u5e8f\u5217\u5bf9\u5e94\u591a\u79cd\u4e8c\u53c9\u6811\u53ef\u80fd\u6027
\u4e3a\u4e86\u89e3\u51b3\u6b64\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u8003\u8651\u5728\u5c42\u5e8f\u904d\u5386\u5e8f\u5217\u4e2d\u663e\u5f0f\u5730\u5199\u51fa\u6240\u6709 \\(\\text{None}\\) \u3002\u5982\u56fe 7-14 \u6240\u793a\uff0c\u8fd9\u6837\u5904\u7406\u540e\uff0c\u5c42\u5e8f\u904d\u5386\u5e8f\u5217\u5c31\u53ef\u4ee5\u552f\u4e00\u8868\u793a\u4e8c\u53c9\u6811\u4e86\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig# \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a\n# \u4f7f\u7528 None \u6765\u8868\u793a\u7a7a\u4f4d\ntree = [1, 2, 3, 4, None, 6, 7, 8, 9, None, None, 12, None, None, 15]\n
/* \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a */\n// \u4f7f\u7528 int \u6700\u5927\u503c INT_MAX \u6807\u8bb0\u7a7a\u4f4d\nvector<int> tree = {1, 2, 3, 4, INT_MAX, 6, 7, 8, 9, INT_MAX, INT_MAX, 12, INT_MAX, INT_MAX, 15};\n
/* \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a */\n// \u4f7f\u7528 int \u7684\u5305\u88c5\u7c7b Integer \uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528 null \u6765\u6807\u8bb0\u7a7a\u4f4d\nInteger[] tree = { 1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15 };\n
/* \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a */\n// \u4f7f\u7528 int? \u53ef\u7a7a\u7c7b\u578b \uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528 null \u6765\u6807\u8bb0\u7a7a\u4f4d\nint?[] tree = [1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15];\n
/* \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a */\n// \u4f7f\u7528 any \u7c7b\u578b\u7684\u5207\u7247, \u5c31\u53ef\u4ee5\u4f7f\u7528 nil \u6765\u6807\u8bb0\u7a7a\u4f4d\ntree := []any{1, 2, 3, 4, nil, 6, 7, 8, 9, nil, nil, 12, nil, nil, 15}\n
/* \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a */\n// \u4f7f\u7528 Int? \u53ef\u7a7a\u7c7b\u578b \uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528 nil \u6765\u6807\u8bb0\u7a7a\u4f4d\nlet tree: [Int?] = [1, 2, 3, 4, nil, 6, 7, 8, 9, nil, nil, 12, nil, nil, 15]\n
/* \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a */\n// \u4f7f\u7528 null \u6765\u8868\u793a\u7a7a\u4f4d\nlet tree = [1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15];\n
/* \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a */\n// \u4f7f\u7528 null \u6765\u8868\u793a\u7a7a\u4f4d\nlet tree: (number | null)[] = [1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15];\n
/* \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a */\n// \u4f7f\u7528 int? \u53ef\u7a7a\u7c7b\u578b \uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528 null \u6765\u6807\u8bb0\u7a7a\u4f4d\nList<int?> tree = [1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15];\n
/* \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a */\n// \u4f7f\u7528 None \u6765\u6807\u8bb0\u7a7a\u4f4d\nlet tree = [Some(1), Some(2), Some(3), Some(4), None, Some(6), Some(7), Some(8), Some(9), None, None, Some(12), None, None, Some(15)];\n
/* \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a */\n// \u4f7f\u7528 int \u6700\u5927\u503c\u6807\u8bb0\u7a7a\u4f4d\uff0c\u56e0\u6b64\u8981\u6c42\u8282\u70b9\u503c\u4e0d\u80fd\u4e3a INT_MAX\nint tree[] = {1, 2, 3, 4, INT_MAX, 6, 7, 8, 9, INT_MAX, INT_MAX, 12, INT_MAX, INT_MAX, 15};\n
\n
\u56fe 7-14 \u00a0 \u4efb\u610f\u7c7b\u578b\u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a
\u503c\u5f97\u8bf4\u660e\u7684\u662f\uff0c\u5b8c\u5168\u4e8c\u53c9\u6811\u975e\u5e38\u9002\u5408\u4f7f\u7528\u6570\u7ec4\u6765\u8868\u793a\u3002\u56de\u987e\u5b8c\u5168\u4e8c\u53c9\u6811\u7684\u5b9a\u4e49\uff0c\\(\\text{None}\\) \u53ea\u51fa\u73b0\u5728\u6700\u5e95\u5c42\u4e14\u9760\u53f3\u7684\u4f4d\u7f6e\uff0c\u56e0\u6b64\u6240\u6709 \\(\\text{None}\\) \u4e00\u5b9a\u51fa\u73b0\u5728\u5c42\u5e8f\u904d\u5386\u5e8f\u5217\u7684\u672b\u5c3e\u3002
\u8fd9\u610f\u5473\u7740\u4f7f\u7528\u6570\u7ec4\u8868\u793a\u5b8c\u5168\u4e8c\u53c9\u6811\u65f6\uff0c\u53ef\u4ee5\u7701\u7565\u5b58\u50a8\u6240\u6709 \\(\\text{None}\\) \uff0c\u975e\u5e38\u65b9\u4fbf\u3002\u56fe 7-15 \u7ed9\u51fa\u4e86\u4e00\u4e2a\u4f8b\u5b50\u3002
\u56fe 7-15 \u00a0 \u5b8c\u5168\u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a
\u4ee5\u4e0b\u4ee3\u7801\u5b9e\u73b0\u4e86\u4e00\u68f5\u57fa\u4e8e\u6570\u7ec4\u8868\u793a\u7684\u4e8c\u53c9\u6811\uff0c\u5305\u62ec\u4ee5\u4e0b\u51e0\u79cd\u64cd\u4f5c\u3002
class ArrayBinaryTree:\n \"\"\"\u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b\"\"\"\n\n def __init__(self, arr: list[int | None]):\n \"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n self._tree = list(arr)\n\n def size(self):\n \"\"\"\u8282\u70b9\u6570\u91cf\"\"\"\n return len(self._tree)\n\n def val(self, i: int) -> int:\n \"\"\"\u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c\"\"\"\n # \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de None \uff0c\u4ee3\u8868\u7a7a\u4f4d\n if i < 0 or i >= self.size():\n return None\n return self._tree[i]\n\n def left(self, i: int) -> int | None:\n \"\"\"\u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15\"\"\"\n return 2 * i + 1\n\n def right(self, i: int) -> int | None:\n \"\"\"\u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15\"\"\"\n return 2 * i + 2\n\n def parent(self, i: int) -> int | None:\n \"\"\"\u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15\"\"\"\n return (i - 1) // 2\n\n def level_order(self) -> list[int]:\n \"\"\"\u5c42\u5e8f\u904d\u5386\"\"\"\n self.res = []\n # \u76f4\u63a5\u904d\u5386\u6570\u7ec4\n for i in range(self.size()):\n if self.val(i) is not None:\n self.res.append(self.val(i))\n return self.res\n\n def dfs(self, i: int, order: str):\n \"\"\"\u6df1\u5ea6\u4f18\u5148\u904d\u5386\"\"\"\n if self.val(i) is None:\n return\n # \u524d\u5e8f\u904d\u5386\n if order == \"pre\":\n self.res.append(self.val(i))\n self.dfs(self.left(i), order)\n # \u4e2d\u5e8f\u904d\u5386\n if order == \"in\":\n self.res.append(self.val(i))\n self.dfs(self.right(i), order)\n # \u540e\u5e8f\u904d\u5386\n if order == \"post\":\n self.res.append(self.val(i))\n\n def pre_order(self) -> list[int]:\n \"\"\"\u524d\u5e8f\u904d\u5386\"\"\"\n self.res = []\n self.dfs(0, order=\"pre\")\n return self.res\n\n def in_order(self) -> list[int]:\n \"\"\"\u4e2d\u5e8f\u904d\u5386\"\"\"\n self.res = []\n self.dfs(0, order=\"in\")\n return self.res\n\n def post_order(self) -> list[int]:\n \"\"\"\u540e\u5e8f\u904d\u5386\"\"\"\n self.res = []\n self.dfs(0, order=\"post\")\n return self.res\n
array_binary_tree.cpp/* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\nclass ArrayBinaryTree {\n public:\n /* \u6784\u9020\u65b9\u6cd5 */\n ArrayBinaryTree(vector<int> arr) {\n tree = arr;\n }\n\n /* \u8282\u70b9\u6570\u91cf */\n int size() {\n return tree.size();\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\n int val(int i) {\n // \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de INT_MAX \uff0c\u4ee3\u8868\u7a7a\u4f4d\n if (i < 0 || i >= size())\n return INT_MAX;\n return tree[i];\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n int left(int i) {\n return 2 * i + 1;\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n int right(int i) {\n return 2 * i + 2;\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\n int parent(int i) {\n return (i - 1) / 2;\n }\n\n /* \u5c42\u5e8f\u904d\u5386 */\n vector<int> levelOrder() {\n vector<int> res;\n // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\n for (int i = 0; i < size(); i++) {\n if (val(i) != INT_MAX)\n res.push_back(val(i));\n }\n return res;\n }\n\n /* \u524d\u5e8f\u904d\u5386 */\n vector<int> preOrder() {\n vector<int> res;\n dfs(0, \"pre\", res);\n return res;\n }\n\n /* \u4e2d\u5e8f\u904d\u5386 */\n vector<int> inOrder() {\n vector<int> res;\n dfs(0, \"in\", res);\n return res;\n }\n\n /* \u540e\u5e8f\u904d\u5386 */\n vector<int> postOrder() {\n vector<int> res;\n dfs(0, \"post\", res);\n return res;\n }\n\n private:\n vector<int> tree;\n\n /* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n void dfs(int i, string order, vector<int> &res) {\n // \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\n if (val(i) == INT_MAX)\n return;\n // \u524d\u5e8f\u904d\u5386\n if (order == \"pre\")\n res.push_back(val(i));\n dfs(left(i), order, res);\n // \u4e2d\u5e8f\u904d\u5386\n if (order == \"in\")\n res.push_back(val(i));\n dfs(right(i), order, res);\n // \u540e\u5e8f\u904d\u5386\n if (order == \"post\")\n res.push_back(val(i));\n }\n};\n
array_binary_tree.java/* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\nclass ArrayBinaryTree {\n private List<Integer> tree;\n\n /* \u6784\u9020\u65b9\u6cd5 */\n public ArrayBinaryTree(List<Integer> arr) {\n tree = new ArrayList<>(arr);\n }\n\n /* \u8282\u70b9\u6570\u91cf */\n public int size() {\n return tree.size();\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\n public Integer val(int i) {\n // \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de null \uff0c\u4ee3\u8868\u7a7a\u4f4d\n if (i < 0 || i >= size())\n return null;\n return tree.get(i);\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n public Integer left(int i) {\n return 2 * i + 1;\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n public Integer right(int i) {\n return 2 * i + 2;\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\n public Integer parent(int i) {\n return (i - 1) / 2;\n }\n\n /* \u5c42\u5e8f\u904d\u5386 */\n public List<Integer> levelOrder() {\n List<Integer> res = new ArrayList<>();\n // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\n for (int i = 0; i < size(); i++) {\n if (val(i) != null)\n res.add(val(i));\n }\n return res;\n }\n\n /* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n private void dfs(Integer i, String order, List<Integer> res) {\n // \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\n if (val(i) == null)\n return;\n // \u524d\u5e8f\u904d\u5386\n if (\"pre\".equals(order))\n res.add(val(i));\n dfs(left(i), order, res);\n // \u4e2d\u5e8f\u904d\u5386\n if (\"in\".equals(order))\n res.add(val(i));\n dfs(right(i), order, res);\n // \u540e\u5e8f\u904d\u5386\n if (\"post\".equals(order))\n res.add(val(i));\n }\n\n /* \u524d\u5e8f\u904d\u5386 */\n public List<Integer> preOrder() {\n List<Integer> res = new ArrayList<>();\n dfs(0, \"pre\", res);\n return res;\n }\n\n /* \u4e2d\u5e8f\u904d\u5386 */\n public List<Integer> inOrder() {\n List<Integer> res = new ArrayList<>();\n dfs(0, \"in\", res);\n return res;\n }\n\n /* \u540e\u5e8f\u904d\u5386 */\n public List<Integer> postOrder() {\n List<Integer> res = new ArrayList<>();\n dfs(0, \"post\", res);\n return res;\n }\n}\n
array_binary_tree.cs/* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\nclass ArrayBinaryTree(List<int?> arr) {\n List<int?> tree = new(arr);\n\n /* \u8282\u70b9\u6570\u91cf */\n public int Size() {\n return tree.Count;\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\n public int? Val(int i) {\n // \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de null \uff0c\u4ee3\u8868\u7a7a\u4f4d\n if (i < 0 || i >= Size())\n return null;\n return tree[i];\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n public int Left(int i) {\n return 2 * i + 1;\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n public int Right(int i) {\n return 2 * i + 2;\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\n public int Parent(int i) {\n return (i - 1) / 2;\n }\n\n /* \u5c42\u5e8f\u904d\u5386 */\n public List<int> LevelOrder() {\n List<int> res = [];\n // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\n for (int i = 0; i < Size(); i++) {\n if (Val(i).HasValue)\n res.Add(Val(i)!.Value);\n }\n return res;\n }\n\n /* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n void DFS(int i, string order, List<int> res) {\n // \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\n if (!Val(i).HasValue)\n return;\n // \u524d\u5e8f\u904d\u5386\n if (order == \"pre\")\n res.Add(Val(i)!.Value);\n DFS(Left(i), order, res);\n // \u4e2d\u5e8f\u904d\u5386\n if (order == \"in\")\n res.Add(Val(i)!.Value);\n DFS(Right(i), order, res);\n // \u540e\u5e8f\u904d\u5386\n if (order == \"post\")\n res.Add(Val(i)!.Value);\n }\n\n /* \u524d\u5e8f\u904d\u5386 */\n public List<int> PreOrder() {\n List<int> res = [];\n DFS(0, \"pre\", res);\n return res;\n }\n\n /* \u4e2d\u5e8f\u904d\u5386 */\n public List<int> InOrder() {\n List<int> res = [];\n DFS(0, \"in\", res);\n return res;\n }\n\n /* \u540e\u5e8f\u904d\u5386 */\n public List<int> PostOrder() {\n List<int> res = [];\n DFS(0, \"post\", res);\n return res;\n }\n}\n
array_binary_tree.go/* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\ntype arrayBinaryTree struct {\n tree []any\n}\n\n/* \u6784\u9020\u65b9\u6cd5 */\nfunc newArrayBinaryTree(arr []any) *arrayBinaryTree {\n return &arrayBinaryTree{\n tree: arr,\n }\n}\n\n/* \u8282\u70b9\u6570\u91cf */\nfunc (abt *arrayBinaryTree) size() int {\n return len(abt.tree)\n}\n\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\nfunc (abt *arrayBinaryTree) val(i int) any {\n // \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de null \uff0c\u4ee3\u8868\u7a7a\u4f4d\n if i < 0 || i >= abt.size() {\n return nil\n }\n return abt.tree[i]\n}\n\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nfunc (abt *arrayBinaryTree) left(i int) int {\n return 2*i + 1\n}\n\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nfunc (abt *arrayBinaryTree) right(i int) int {\n return 2*i + 2\n}\n\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\nfunc (abt *arrayBinaryTree) parent(i int) int {\n return (i - 1) / 2\n}\n\n/* \u5c42\u5e8f\u904d\u5386 */\nfunc (abt *arrayBinaryTree) levelOrder() []any {\n var res []any\n // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\n for i := 0; i < abt.size(); i++ {\n if abt.val(i) != nil {\n res = append(res, abt.val(i))\n }\n }\n return res\n}\n\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\nfunc (abt *arrayBinaryTree) dfs(i int, order string, res *[]any) {\n // \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\n if abt.val(i) == nil {\n return\n }\n // \u524d\u5e8f\u904d\u5386\n if order == \"pre\" {\n *res = append(*res, abt.val(i))\n }\n abt.dfs(abt.left(i), order, res)\n // \u4e2d\u5e8f\u904d\u5386\n if order == \"in\" {\n *res = append(*res, abt.val(i))\n }\n abt.dfs(abt.right(i), order, res)\n // \u540e\u5e8f\u904d\u5386\n if order == \"post\" {\n *res = append(*res, abt.val(i))\n }\n}\n\n/* \u524d\u5e8f\u904d\u5386 */\nfunc (abt *arrayBinaryTree) preOrder() []any {\n var res []any\n abt.dfs(0, \"pre\", &res)\n return res\n}\n\n/* \u4e2d\u5e8f\u904d\u5386 */\nfunc (abt *arrayBinaryTree) inOrder() []any {\n var res []any\n abt.dfs(0, \"in\", &res)\n return res\n}\n\n/* \u540e\u5e8f\u904d\u5386 */\nfunc (abt *arrayBinaryTree) postOrder() []any {\n var res []any\n abt.dfs(0, \"post\", &res)\n return res\n}\n
array_binary_tree.swift/* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\nclass ArrayBinaryTree {\n private var tree: [Int?]\n\n /* \u6784\u9020\u65b9\u6cd5 */\n init(arr: [Int?]) {\n tree = arr\n }\n\n /* \u8282\u70b9\u6570\u91cf */\n func size() -> Int {\n tree.count\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\n func val(i: Int) -> Int? {\n // \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de null \uff0c\u4ee3\u8868\u7a7a\u4f4d\n if i < 0 || i >= size() {\n return nil\n }\n return tree[i]\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n func left(i: Int) -> Int {\n 2 * i + 1\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n func right(i: Int) -> Int {\n 2 * i + 2\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\n func parent(i: Int) -> Int {\n (i - 1) / 2\n }\n\n /* \u5c42\u5e8f\u904d\u5386 */\n func levelOrder() -> [Int] {\n var res: [Int] = []\n // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\n for i in stride(from: 0, to: size(), by: 1) {\n if let val = val(i: i) {\n res.append(val)\n }\n }\n return res\n }\n\n /* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n private func dfs(i: Int, order: String, res: inout [Int]) {\n // \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\n guard let val = val(i: i) else {\n return\n }\n // \u524d\u5e8f\u904d\u5386\n if order == \"pre\" {\n res.append(val)\n }\n dfs(i: left(i: i), order: order, res: &res)\n // \u4e2d\u5e8f\u904d\u5386\n if order == \"in\" {\n res.append(val)\n }\n dfs(i: right(i: i), order: order, res: &res)\n // \u540e\u5e8f\u904d\u5386\n if order == \"post\" {\n res.append(val)\n }\n }\n\n /* \u524d\u5e8f\u904d\u5386 */\n func preOrder() -> [Int] {\n var res: [Int] = []\n dfs(i: 0, order: \"pre\", res: &res)\n return res\n }\n\n /* \u4e2d\u5e8f\u904d\u5386 */\n func inOrder() -> [Int] {\n var res: [Int] = []\n dfs(i: 0, order: \"in\", res: &res)\n return res\n }\n\n /* \u540e\u5e8f\u904d\u5386 */\n func postOrder() -> [Int] {\n var res: [Int] = []\n dfs(i: 0, order: \"post\", res: &res)\n return res\n }\n}\n
array_binary_tree.js/* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\nclass ArrayBinaryTree {\n #tree;\n\n /* \u6784\u9020\u65b9\u6cd5 */\n constructor(arr) {\n this.#tree = arr;\n }\n\n /* \u8282\u70b9\u6570\u91cf */\n size() {\n return this.#tree.length;\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\n val(i) {\n // \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de null \uff0c\u4ee3\u8868\u7a7a\u4f4d\n if (i < 0 || i >= this.size()) return null;\n return this.#tree[i];\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n left(i) {\n return 2 * i + 1;\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n right(i) {\n return 2 * i + 2;\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\n parent(i) {\n return Math.floor((i - 1) / 2); // \u5411\u4e0b\u6574\u9664\n }\n\n /* \u5c42\u5e8f\u904d\u5386 */\n levelOrder() {\n let res = [];\n // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\n for (let i = 0; i < this.size(); i++) {\n if (this.val(i) !== null) res.push(this.val(i));\n }\n return res;\n }\n\n /* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n #dfs(i, order, res) {\n // \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\n if (this.val(i) === null) return;\n // \u524d\u5e8f\u904d\u5386\n if (order === 'pre') res.push(this.val(i));\n this.#dfs(this.left(i), order, res);\n // \u4e2d\u5e8f\u904d\u5386\n if (order === 'in') res.push(this.val(i));\n this.#dfs(this.right(i), order, res);\n // \u540e\u5e8f\u904d\u5386\n if (order === 'post') res.push(this.val(i));\n }\n\n /* \u524d\u5e8f\u904d\u5386 */\n preOrder() {\n const res = [];\n this.#dfs(0, 'pre', res);\n return res;\n }\n\n /* \u4e2d\u5e8f\u904d\u5386 */\n inOrder() {\n const res = [];\n this.#dfs(0, 'in', res);\n return res;\n }\n\n /* \u540e\u5e8f\u904d\u5386 */\n postOrder() {\n const res = [];\n this.#dfs(0, 'post', res);\n return res;\n }\n}\n
array_binary_tree.ts/* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\nclass ArrayBinaryTree {\n #tree: (number | null)[];\n\n /* \u6784\u9020\u65b9\u6cd5 */\n constructor(arr: (number | null)[]) {\n this.#tree = arr;\n }\n\n /* \u8282\u70b9\u6570\u91cf */\n size(): number {\n return this.#tree.length;\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\n val(i: number): number | null {\n // \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de null \uff0c\u4ee3\u8868\u7a7a\u4f4d\n if (i < 0 || i >= this.size()) return null;\n return this.#tree[i];\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n left(i: number): number {\n return 2 * i + 1;\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n right(i: number): number {\n return 2 * i + 2;\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\n parent(i: number): number {\n return Math.floor((i - 1) / 2); // \u5411\u4e0b\u6574\u9664\n }\n\n /* \u5c42\u5e8f\u904d\u5386 */\n levelOrder(): number[] {\n let res = [];\n // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\n for (let i = 0; i < this.size(); i++) {\n if (this.val(i) !== null) res.push(this.val(i));\n }\n return res;\n }\n\n /* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n #dfs(i: number, order: Order, res: (number | null)[]): void {\n // \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\n if (this.val(i) === null) return;\n // \u524d\u5e8f\u904d\u5386\n if (order === 'pre') res.push(this.val(i));\n this.#dfs(this.left(i), order, res);\n // \u4e2d\u5e8f\u904d\u5386\n if (order === 'in') res.push(this.val(i));\n this.#dfs(this.right(i), order, res);\n // \u540e\u5e8f\u904d\u5386\n if (order === 'post') res.push(this.val(i));\n }\n\n /* \u524d\u5e8f\u904d\u5386 */\n preOrder(): (number | null)[] {\n const res = [];\n this.#dfs(0, 'pre', res);\n return res;\n }\n\n /* \u4e2d\u5e8f\u904d\u5386 */\n inOrder(): (number | null)[] {\n const res = [];\n this.#dfs(0, 'in', res);\n return res;\n }\n\n /* \u540e\u5e8f\u904d\u5386 */\n postOrder(): (number | null)[] {\n const res = [];\n this.#dfs(0, 'post', res);\n return res;\n }\n}\n
array_binary_tree.dart/* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\nclass ArrayBinaryTree {\n late List<int?> _tree;\n\n /* \u6784\u9020\u65b9\u6cd5 */\n ArrayBinaryTree(this._tree);\n\n /* \u8282\u70b9\u6570\u91cf */\n int size() {\n return _tree.length;\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\n int? val(int i) {\n // \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de null \uff0c\u4ee3\u8868\u7a7a\u4f4d\n if (i < 0 || i >= size()) {\n return null;\n }\n return _tree[i];\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n int? left(int i) {\n return 2 * i + 1;\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n int? right(int i) {\n return 2 * i + 2;\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\n int? parent(int i) {\n return (i - 1) ~/ 2;\n }\n\n /* \u5c42\u5e8f\u904d\u5386 */\n List<int> levelOrder() {\n List<int> res = [];\n for (int i = 0; i < size(); i++) {\n if (val(i) != null) {\n res.add(val(i)!);\n }\n }\n return res;\n }\n\n /* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n void dfs(int i, String order, List<int?> res) {\n // \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\n if (val(i) == null) {\n return;\n }\n // \u524d\u5e8f\u904d\u5386\n if (order == 'pre') {\n res.add(val(i));\n }\n dfs(left(i)!, order, res);\n // \u4e2d\u5e8f\u904d\u5386\n if (order == 'in') {\n res.add(val(i));\n }\n dfs(right(i)!, order, res);\n // \u540e\u5e8f\u904d\u5386\n if (order == 'post') {\n res.add(val(i));\n }\n }\n\n /* \u524d\u5e8f\u904d\u5386 */\n List<int?> preOrder() {\n List<int?> res = [];\n dfs(0, 'pre', res);\n return res;\n }\n\n /* \u4e2d\u5e8f\u904d\u5386 */\n List<int?> inOrder() {\n List<int?> res = [];\n dfs(0, 'in', res);\n return res;\n }\n\n /* \u540e\u5e8f\u904d\u5386 */\n List<int?> postOrder() {\n List<int?> res = [];\n dfs(0, 'post', res);\n return res;\n }\n}\n
array_binary_tree.rs/* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\nstruct ArrayBinaryTree {\n tree: Vec<Option<i32>>,\n}\n\nimpl ArrayBinaryTree {\n /* \u6784\u9020\u65b9\u6cd5 */\n fn new(arr: Vec<Option<i32>>) -> Self {\n Self { tree: arr }\n }\n\n /* \u8282\u70b9\u6570\u91cf */\n fn size(&self) -> i32 {\n self.tree.len() as i32\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\n fn val(&self, i: i32) -> Option<i32> {\n // \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de None \uff0c\u4ee3\u8868\u7a7a\u4f4d\n if i < 0 || i >= self.size() {\n None\n } else {\n self.tree[i as usize]\n }\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n fn left(&self, i: i32) -> i32 {\n 2 * i + 1\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n fn right(&self, i: i32) -> i32 {\n 2 * i + 2\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\n fn parent(&self, i: i32) -> i32 {\n (i - 1) / 2\n }\n\n /* \u5c42\u5e8f\u904d\u5386 */\n fn level_order(&self) -> Vec<i32> {\n let mut res = vec![];\n // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\n for i in 0..self.size() {\n if let Some(val) = self.val(i) {\n res.push(val)\n }\n }\n res\n }\n\n /* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n fn dfs(&self, i: i32, order: &str, res: &mut Vec<i32>) {\n if self.val(i).is_none() {\n return;\n }\n let val = self.val(i).unwrap();\n // \u524d\u5e8f\u904d\u5386\n if order == \"pre\" {\n res.push(val);\n }\n self.dfs(self.left(i), order, res);\n // \u4e2d\u5e8f\u904d\u5386\n if order == \"in\" {\n res.push(val);\n }\n self.dfs(self.right(i), order, res);\n // \u540e\u5e8f\u904d\u5386\n if order == \"post\" {\n res.push(val);\n }\n }\n\n /* \u524d\u5e8f\u904d\u5386 */\n fn pre_order(&self) -> Vec<i32> {\n let mut res = vec![];\n self.dfs(0, \"pre\", &mut res);\n res\n }\n\n /* \u4e2d\u5e8f\u904d\u5386 */\n fn in_order(&self) -> Vec<i32> {\n let mut res = vec![];\n self.dfs(0, \"in\", &mut res);\n res\n }\n\n /* \u540e\u5e8f\u904d\u5386 */\n fn post_order(&self) -> Vec<i32> {\n let mut res = vec![];\n self.dfs(0, \"post\", &mut res);\n res\n }\n}\n
array_binary_tree.c/* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7ed3\u6784\u4f53 */\ntypedef struct {\n int *tree;\n int size;\n} ArrayBinaryTree;\n\n/* \u6784\u9020\u51fd\u6570 */\nArrayBinaryTree *newArrayBinaryTree(int *arr, int arrSize) {\n ArrayBinaryTree *abt = (ArrayBinaryTree *)malloc(sizeof(ArrayBinaryTree));\n abt->tree = malloc(sizeof(int) * arrSize);\n memcpy(abt->tree, arr, sizeof(int) * arrSize);\n abt->size = arrSize;\n return abt;\n}\n\n/* \u6790\u6784\u51fd\u6570 */\nvoid delArrayBinaryTree(ArrayBinaryTree *abt) {\n free(abt->tree);\n free(abt);\n}\n\n/* \u8282\u70b9\u6570\u91cf */\nint size(ArrayBinaryTree *abt) {\n return abt->size;\n}\n\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\nint val(ArrayBinaryTree *abt, int i) {\n // \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de INT_MAX \uff0c\u4ee3\u8868\u7a7a\u4f4d\n if (i < 0 || i >= size(abt))\n return INT_MAX;\n return abt->tree[i];\n}\n\n/* \u5c42\u5e8f\u904d\u5386 */\nint *levelOrder(ArrayBinaryTree *abt, int *returnSize) {\n int *res = (int *)malloc(sizeof(int) * size(abt));\n int index = 0;\n // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\n for (int i = 0; i < size(abt); i++) {\n if (val(abt, i) != INT_MAX)\n res[index++] = val(abt, i);\n }\n *returnSize = index;\n return res;\n}\n\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\nvoid dfs(ArrayBinaryTree *abt, int i, char *order, int *res, int *index) {\n // \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\n if (val(abt, i) == INT_MAX)\n return;\n // \u524d\u5e8f\u904d\u5386\n if (strcmp(order, \"pre\") == 0)\n res[(*index)++] = val(abt, i);\n dfs(abt, left(i), order, res, index);\n // \u4e2d\u5e8f\u904d\u5386\n if (strcmp(order, \"in\") == 0)\n res[(*index)++] = val(abt, i);\n dfs(abt, right(i), order, res, index);\n // \u540e\u5e8f\u904d\u5386\n if (strcmp(order, \"post\") == 0)\n res[(*index)++] = val(abt, i);\n}\n\n/* \u524d\u5e8f\u904d\u5386 */\nint *preOrder(ArrayBinaryTree *abt, int *returnSize) {\n int *res = (int *)malloc(sizeof(int) * size(abt));\n int index = 0;\n dfs(abt, 0, \"pre\", res, &index);\n *returnSize = index;\n return res;\n}\n\n/* \u4e2d\u5e8f\u904d\u5386 */\nint *inOrder(ArrayBinaryTree *abt, int *returnSize) {\n int *res = (int *)malloc(sizeof(int) * size(abt));\n int index = 0;\n dfs(abt, 0, \"in\", res, &index);\n *returnSize = index;\n return res;\n}\n\n/* \u540e\u5e8f\u904d\u5386 */\nint *postOrder(ArrayBinaryTree *abt, int *returnSize) {\n int *res = (int *)malloc(sizeof(int) * size(abt));\n int index = 0;\n dfs(abt, 0, \"post\", res, &index);\n *returnSize = index;\n return res;\n}\n
array_binary_tree.zig[class]{ArrayBinaryTree}-[func]{}\n
"},{"location":"chapter_tree/array_representation_of_tree/#733","title":"7.3.3 \u00a0 \u4f18\u70b9\u4e0e\u5c40\u9650\u6027","text":"\u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a\u4e3b\u8981\u6709\u4ee5\u4e0b\u4f18\u70b9\u3002
\u7136\u800c\uff0c\u6570\u7ec4\u8868\u793a\u4e5f\u5b58\u5728\u4e00\u4e9b\u5c40\u9650\u6027\u3002
\u5728\u201c\u4e8c\u53c9\u641c\u7d22\u6811\u201d\u7ae0\u8282\u4e2d\uff0c\u6211\u4eec\u63d0\u5230\uff0c\u5728\u591a\u6b21\u63d2\u5165\u548c\u5220\u9664\u64cd\u4f5c\u540e\uff0c\u4e8c\u53c9\u641c\u7d22\u6811\u53ef\u80fd\u9000\u5316\u4e3a\u94fe\u8868\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6240\u6709\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u5c06\u4ece \\(O(\\log n)\\) \u6076\u5316\u4e3a \\(O(n)\\) \u3002
\u5982\u56fe 7-24 \u6240\u793a\uff0c\u7ecf\u8fc7\u4e24\u6b21\u5220\u9664\u8282\u70b9\u64cd\u4f5c\uff0c\u8fd9\u68f5\u4e8c\u53c9\u641c\u7d22\u6811\u4fbf\u4f1a\u9000\u5316\u4e3a\u94fe\u8868\u3002
\u56fe 7-24 \u00a0 AVL \u6811\u5728\u5220\u9664\u8282\u70b9\u540e\u53d1\u751f\u9000\u5316
\u518d\u4f8b\u5982\uff0c\u5728\u56fe 7-25 \u6240\u793a\u7684\u5b8c\u7f8e\u4e8c\u53c9\u6811\u4e2d\u63d2\u5165\u4e24\u4e2a\u8282\u70b9\u540e\uff0c\u6811\u5c06\u4e25\u91cd\u5411\u5de6\u503e\u659c\uff0c\u67e5\u627e\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e5f\u968f\u4e4b\u6076\u5316\u3002
\u56fe 7-25 \u00a0 AVL \u6811\u5728\u63d2\u5165\u8282\u70b9\u540e\u53d1\u751f\u9000\u5316
1962 \u5e74 G. M. Adelson-Velsky \u548c E. M. Landis \u5728\u8bba\u6587 \"An algorithm for the organization of information\" \u4e2d\u63d0\u51fa\u4e86\u300cAVL \u6811\u300d\u3002\u8bba\u6587\u4e2d\u8be6\u7ec6\u63cf\u8ff0\u4e86\u4e00\u7cfb\u5217\u64cd\u4f5c\uff0c\u786e\u4fdd\u5728\u6301\u7eed\u6dfb\u52a0\u548c\u5220\u9664\u8282\u70b9\u540e\uff0cAVL \u6811\u4e0d\u4f1a\u9000\u5316\uff0c\u4ece\u800c\u4f7f\u5f97\u5404\u79cd\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4fdd\u6301\u5728 \\(O(\\log n)\\) \u7ea7\u522b\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u5728\u9700\u8981\u9891\u7e41\u8fdb\u884c\u589e\u5220\u67e5\u6539\u64cd\u4f5c\u7684\u573a\u666f\u4e2d\uff0cAVL \u6811\u80fd\u59cb\u7ec8\u4fdd\u6301\u9ad8\u6548\u7684\u6570\u636e\u64cd\u4f5c\u6027\u80fd\uff0c\u5177\u6709\u5f88\u597d\u7684\u5e94\u7528\u4ef7\u503c\u3002
"},{"location":"chapter_tree/avl_tree/#751-avl","title":"7.5.1 \u00a0 AVL \u6811\u5e38\u89c1\u672f\u8bed","text":"AVL \u6811\u65e2\u662f\u4e8c\u53c9\u641c\u7d22\u6811\u4e5f\u662f\u5e73\u8861\u4e8c\u53c9\u6811\uff0c\u540c\u65f6\u6ee1\u8db3\u8fd9\u4e24\u7c7b\u4e8c\u53c9\u6811\u7684\u6240\u6709\u6027\u8d28\uff0c\u56e0\u6b64\u4e5f\u88ab\u79f0\u4e3a\u300c\u5e73\u8861\u4e8c\u53c9\u641c\u7d22\u6811 balanced binary search tree\u300d\u3002
"},{"location":"chapter_tree/avl_tree/#1","title":"1. \u00a0 \u8282\u70b9\u9ad8\u5ea6","text":"\u7531\u4e8e AVL \u6811\u7684\u76f8\u5173\u64cd\u4f5c\u9700\u8981\u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6\uff0c\u56e0\u6b64\u6211\u4eec\u9700\u8981\u4e3a\u8282\u70b9\u7c7b\u6dfb\u52a0 height
\u53d8\u91cf\uff1a
class TreeNode:\n \"\"\"AVL \u6811\u8282\u70b9\u7c7b\"\"\"\n def __init__(self, val: int):\n self.val: int = val # \u8282\u70b9\u503c\n self.height: int = 0 # \u8282\u70b9\u9ad8\u5ea6\n self.left: TreeNode | None = None # \u5de6\u5b50\u8282\u70b9\u5f15\u7528\n self.right: TreeNode | None = None # \u53f3\u5b50\u8282\u70b9\u5f15\u7528\n
/* AVL \u6811\u8282\u70b9\u7c7b */\nstruct TreeNode {\n int val{}; // \u8282\u70b9\u503c\n int height = 0; // \u8282\u70b9\u9ad8\u5ea6\n TreeNode *left{}; // \u5de6\u5b50\u8282\u70b9\n TreeNode *right{}; // \u53f3\u5b50\u8282\u70b9\n TreeNode() = default;\n explicit TreeNode(int x) : val(x){}\n};\n
/* AVL \u6811\u8282\u70b9\u7c7b */\nclass TreeNode {\n public int val; // \u8282\u70b9\u503c\n public int height; // \u8282\u70b9\u9ad8\u5ea6\n public TreeNode left; // \u5de6\u5b50\u8282\u70b9\n public TreeNode right; // \u53f3\u5b50\u8282\u70b9\n public TreeNode(int x) { val = x; }\n}\n
/* AVL \u6811\u8282\u70b9\u7c7b */\nclass TreeNode(int? x) {\n public int? val = x; // \u8282\u70b9\u503c\n public int height; // \u8282\u70b9\u9ad8\u5ea6\n public TreeNode? left; // \u5de6\u5b50\u8282\u70b9\u5f15\u7528\n public TreeNode? right; // \u53f3\u5b50\u8282\u70b9\u5f15\u7528\n}\n
/* AVL \u6811\u8282\u70b9\u7ed3\u6784\u4f53 */\ntype TreeNode struct {\n Val int // \u8282\u70b9\u503c\n Height int // \u8282\u70b9\u9ad8\u5ea6\n Left *TreeNode // \u5de6\u5b50\u8282\u70b9\u5f15\u7528\n Right *TreeNode // \u53f3\u5b50\u8282\u70b9\u5f15\u7528\n}\n
/* AVL \u6811\u8282\u70b9\u7c7b */\nclass TreeNode {\n var val: Int // \u8282\u70b9\u503c\n var height: Int // \u8282\u70b9\u9ad8\u5ea6\n var left: TreeNode? // \u5de6\u5b50\u8282\u70b9\n var right: TreeNode? // \u53f3\u5b50\u8282\u70b9\n\n init(x: Int) {\n val = x\n height = 0\n }\n}\n
/* AVL \u6811\u8282\u70b9\u7c7b */\nclass TreeNode {\n val; // \u8282\u70b9\u503c\n height; //\u8282\u70b9\u9ad8\u5ea6\n left; // \u5de6\u5b50\u8282\u70b9\u6307\u9488\n right; // \u53f3\u5b50\u8282\u70b9\u6307\u9488\n constructor(val, left, right, height) {\n this.val = val === undefined ? 0 : val;\n this.height = height === undefined ? 0 : height;\n this.left = left === undefined ? null : left;\n this.right = right === undefined ? null : right;\n }\n}\n
/* AVL \u6811\u8282\u70b9\u7c7b */\nclass TreeNode {\n val: number; // \u8282\u70b9\u503c\n height: number; // \u8282\u70b9\u9ad8\u5ea6\n left: TreeNode | null; // \u5de6\u5b50\u8282\u70b9\u6307\u9488\n right: TreeNode | null; // \u53f3\u5b50\u8282\u70b9\u6307\u9488\n constructor(val?: number, height?: number, left?: TreeNode | null, right?: TreeNode | null) {\n this.val = val === undefined ? 0 : val;\n this.height = height === undefined ? 0 : height; \n this.left = left === undefined ? null : left; \n this.right = right === undefined ? null : right; \n }\n}\n
/* AVL \u6811\u8282\u70b9\u7c7b */\nclass TreeNode {\n int val; // \u8282\u70b9\u503c\n int height; // \u8282\u70b9\u9ad8\u5ea6\n TreeNode? left; // \u5de6\u5b50\u8282\u70b9\n TreeNode? right; // \u53f3\u5b50\u8282\u70b9\n TreeNode(this.val, [this.height = 0, this.left, this.right]);\n}\n
use std::rc::Rc;\nuse std::cell::RefCell;\n\n/* AVL \u6811\u8282\u70b9\u7ed3\u6784\u4f53 */\nstruct TreeNode {\n val: i32, // \u8282\u70b9\u503c\n height: i32, // \u8282\u70b9\u9ad8\u5ea6\n left: Option<Rc<RefCell<TreeNode>>>, // \u5de6\u5b50\u8282\u70b9\n right: Option<Rc<RefCell<TreeNode>>>, // \u53f3\u5b50\u8282\u70b9\n}\n\nimpl TreeNode {\n /* \u6784\u9020\u65b9\u6cd5 */\n fn new(val: i32) -> Rc<RefCell<Self>> {\n Rc::new(RefCell::new(Self {\n val,\n height: 0,\n left: None,\n right: None\n }))\n }\n}\n
/* AVL \u6811\u8282\u70b9\u7ed3\u6784\u4f53 */\nTreeNode struct TreeNode {\n int val;\n int height;\n struct TreeNode *left;\n struct TreeNode *right;\n} TreeNode;\n\n/* \u6784\u9020\u51fd\u6570 */\nTreeNode *newTreeNode(int val) {\n TreeNode *node;\n\n node = (TreeNode *)malloc(sizeof(TreeNode));\n node->val = val;\n node->height = 0;\n node->left = NULL;\n node->right = NULL;\n return node;\n}\n
\n
\u201c\u8282\u70b9\u9ad8\u5ea6\u201d\u662f\u6307\u4ece\u8be5\u8282\u70b9\u5230\u5176\u6700\u8fdc\u53f6\u8282\u70b9\u7684\u8ddd\u79bb\uff0c\u5373\u6240\u7ecf\u8fc7\u7684\u201c\u8fb9\u201d\u7684\u6570\u91cf\u3002\u9700\u8981\u7279\u522b\u6ce8\u610f\u7684\u662f\uff0c\u53f6\u8282\u70b9\u7684\u9ad8\u5ea6\u4e3a \\(0\\) \uff0c\u800c\u7a7a\u8282\u70b9\u7684\u9ad8\u5ea6\u4e3a \\(-1\\) \u3002\u6211\u4eec\u5c06\u521b\u5efa\u4e24\u4e2a\u5de5\u5177\u51fd\u6570\uff0c\u5206\u522b\u7528\u4e8e\u83b7\u53d6\u548c\u66f4\u65b0\u8282\u70b9\u7684\u9ad8\u5ea6\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig avl_tree.pydef height(self, node: TreeNode | None) -> int:\n \"\"\"\u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6\"\"\"\n # \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\n if node is not None:\n return node.height\n return -1\n\ndef update_height(self, node: TreeNode | None):\n \"\"\"\u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\"\"\"\n # \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\n node.height = max([self.height(node.left), self.height(node.right)]) + 1\n
avl_tree.cpp/* \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6 */\nint height(TreeNode *node) {\n // \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\n return node == nullptr ? -1 : node->height;\n}\n\n/* \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6 */\nvoid updateHeight(TreeNode *node) {\n // \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\n node->height = max(height(node->left), height(node->right)) + 1;\n}\n
avl_tree.java/* \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6 */\nint height(TreeNode node) {\n // \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\n return node == null ? -1 : node.height;\n}\n\n/* \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6 */\nvoid updateHeight(TreeNode node) {\n // \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\n node.height = Math.max(height(node.left), height(node.right)) + 1;\n}\n
avl_tree.cs/* \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6 */\nint Height(TreeNode? node) {\n // \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\n return node == null ? -1 : node.height;\n}\n\n/* \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6 */\nvoid UpdateHeight(TreeNode node) {\n // \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\n node.height = Math.Max(Height(node.left), Height(node.right)) + 1;\n}\n
avl_tree.go/* \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6 */\nfunc (t *aVLTree) height(node *TreeNode) int {\n // \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\n if node != nil {\n return node.Height\n }\n return -1\n}\n\n/* \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6 */\nfunc (t *aVLTree) updateHeight(node *TreeNode) {\n lh := t.height(node.Left)\n rh := t.height(node.Right)\n // \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\n if lh > rh {\n node.Height = lh + 1\n } else {\n node.Height = rh + 1\n }\n}\n
avl_tree.swift/* \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6 */\nfunc height(node: TreeNode?) -> Int {\n // \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\n node == nil ? -1 : node!.height\n}\n\n/* \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6 */\nfunc updateHeight(node: TreeNode?) {\n // \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\n node?.height = max(height(node: node?.left), height(node: node?.right)) + 1\n}\n
avl_tree.js/* \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6 */\nheight(node) {\n // \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\n return node === null ? -1 : node.height;\n}\n\n/* \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6 */\n#updateHeight(node) {\n // \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\n node.height =\n Math.max(this.height(node.left), this.height(node.right)) + 1;\n}\n
avl_tree.ts/* \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6 */\nheight(node: TreeNode): number {\n // \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\n return node === null ? -1 : node.height;\n}\n\n/* \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6 */\nupdateHeight(node: TreeNode): void {\n // \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\n node.height =\n Math.max(this.height(node.left), this.height(node.right)) + 1;\n}\n
avl_tree.dart/* \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6 */\nint height(TreeNode? node) {\n // \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\n return node == null ? -1 : node.height;\n}\n\n/* \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6 */\nvoid updateHeight(TreeNode? node) {\n // \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\n node!.height = max(height(node.left), height(node.right)) + 1;\n}\n
avl_tree.rs/* \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6 */\nfn height(node: OptionTreeNodeRc) -> i32 {\n // \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\n match node {\n Some(node) => node.borrow().height,\n None => -1,\n }\n}\n\n/* \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6 */\nfn update_height(node: OptionTreeNodeRc) {\n if let Some(node) = node {\n let left = node.borrow().left.clone();\n let right = node.borrow().right.clone();\n // \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\n node.borrow_mut().height = std::cmp::max(Self::height(left), Self::height(right)) + 1;\n }\n}\n
avl_tree.c/* \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6 */\nint height(TreeNode *node) {\n // \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\n if (node != NULL) {\n return node->height;\n }\n return -1;\n}\n\n/* \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6 */\nvoid updateHeight(TreeNode *node) {\n int lh = height(node->left);\n int rh = height(node->right);\n // \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\n if (lh > rh) {\n node->height = lh + 1;\n } else {\n node->height = rh + 1;\n }\n}\n
avl_tree.zig// \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6\nfn height(self: *Self, node: ?*inc.TreeNode(T)) i32 {\n _ = self;\n // \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\n return if (node == null) -1 else node.?.height;\n}\n\n// \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nfn updateHeight(self: *Self, node: ?*inc.TreeNode(T)) void {\n // \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\n node.?.height = @max(self.height(node.?.left), self.height(node.?.right)) + 1;\n}\n
"},{"location":"chapter_tree/avl_tree/#2","title":"2. \u00a0 \u8282\u70b9\u5e73\u8861\u56e0\u5b50","text":"\u8282\u70b9\u7684\u300c\u5e73\u8861\u56e0\u5b50 balance factor\u300d\u5b9a\u4e49\u4e3a\u8282\u70b9\u5de6\u5b50\u6811\u7684\u9ad8\u5ea6\u51cf\u53bb\u53f3\u5b50\u6811\u7684\u9ad8\u5ea6\uff0c\u540c\u65f6\u89c4\u5b9a\u7a7a\u8282\u70b9\u7684\u5e73\u8861\u56e0\u5b50\u4e3a \\(0\\) \u3002\u6211\u4eec\u540c\u6837\u5c06\u83b7\u53d6\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u7684\u529f\u80fd\u5c01\u88c5\u6210\u51fd\u6570\uff0c\u65b9\u4fbf\u540e\u7eed\u4f7f\u7528\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig avl_tree.pydef balance_factor(self, node: TreeNode | None) -> int:\n \"\"\"\u83b7\u53d6\u5e73\u8861\u56e0\u5b50\"\"\"\n # \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\n if node is None:\n return 0\n # \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\n return self.height(node.left) - self.height(node.right)\n
avl_tree.cpp/* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nint balanceFactor(TreeNode *node) {\n // \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\n if (node == nullptr)\n return 0;\n // \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\n return height(node->left) - height(node->right);\n}\n
avl_tree.java/* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nint balanceFactor(TreeNode node) {\n // \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\n if (node == null)\n return 0;\n // \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\n return height(node.left) - height(node.right);\n}\n
avl_tree.cs/* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nint BalanceFactor(TreeNode? node) {\n // \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\n if (node == null) return 0;\n // \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\n return Height(node.left) - Height(node.right);\n}\n
avl_tree.go/* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nfunc (t *aVLTree) balanceFactor(node *TreeNode) int {\n // \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\n if node == nil {\n return 0\n }\n // \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\n return t.height(node.Left) - t.height(node.Right)\n}\n
avl_tree.swift/* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nfunc balanceFactor(node: TreeNode?) -> Int {\n // \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\n guard let node = node else { return 0 }\n // \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\n return height(node: node.left) - height(node: node.right)\n}\n
avl_tree.js/* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nbalanceFactor(node) {\n // \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\n if (node === null) return 0;\n // \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\n return this.height(node.left) - this.height(node.right);\n}\n
avl_tree.ts/* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nbalanceFactor(node: TreeNode): number {\n // \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\n if (node === null) return 0;\n // \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\n return this.height(node.left) - this.height(node.right);\n}\n
avl_tree.dart/* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nint balanceFactor(TreeNode? node) {\n // \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\n if (node == null) return 0;\n // \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\n return height(node.left) - height(node.right);\n}\n
avl_tree.rs/* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nfn balance_factor(node: OptionTreeNodeRc) -> i32 {\n match node {\n // \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\n None => 0,\n // \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\n Some(node) => {\n Self::height(node.borrow().left.clone()) - Self::height(node.borrow().right.clone())\n }\n }\n}\n
avl_tree.c/* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nint balanceFactor(TreeNode *node) {\n // \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\n if (node == NULL) {\n return 0;\n }\n // \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\n return height(node->left) - height(node->right);\n}\n
avl_tree.zig// \u83b7\u53d6\u5e73\u8861\u56e0\u5b50\nfn balanceFactor(self: *Self, node: ?*inc.TreeNode(T)) i32 {\n // \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\n if (node == null) return 0;\n // \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\n return self.height(node.?.left) - self.height(node.?.right);\n}\n
Note
\u8bbe\u5e73\u8861\u56e0\u5b50\u4e3a \\(f\\) \uff0c\u5219\u4e00\u68f5 AVL \u6811\u7684\u4efb\u610f\u8282\u70b9\u7684\u5e73\u8861\u56e0\u5b50\u7686\u6ee1\u8db3 \\(-1 \\le f \\le 1\\) \u3002
"},{"location":"chapter_tree/avl_tree/#752-avl","title":"7.5.2 \u00a0 AVL \u6811\u65cb\u8f6c","text":"AVL \u6811\u7684\u7279\u70b9\u5728\u4e8e\u201c\u65cb\u8f6c\u201d\u64cd\u4f5c\uff0c\u5b83\u80fd\u591f\u5728\u4e0d\u5f71\u54cd\u4e8c\u53c9\u6811\u7684\u4e2d\u5e8f\u904d\u5386\u5e8f\u5217\u7684\u524d\u63d0\u4e0b\uff0c\u4f7f\u5931\u8861\u8282\u70b9\u91cd\u65b0\u6062\u590d\u5e73\u8861\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u65cb\u8f6c\u64cd\u4f5c\u65e2\u80fd\u4fdd\u6301\u201c\u4e8c\u53c9\u641c\u7d22\u6811\u201d\u7684\u6027\u8d28\uff0c\u4e5f\u80fd\u4f7f\u6811\u91cd\u65b0\u53d8\u4e3a\u201c\u5e73\u8861\u4e8c\u53c9\u6811\u201d\u3002
\u6211\u4eec\u5c06\u5e73\u8861\u56e0\u5b50\u7edd\u5bf9\u503c \\(> 1\\) \u7684\u8282\u70b9\u79f0\u4e3a\u201c\u5931\u8861\u8282\u70b9\u201d\u3002\u6839\u636e\u8282\u70b9\u5931\u8861\u60c5\u51b5\u7684\u4e0d\u540c\uff0c\u65cb\u8f6c\u64cd\u4f5c\u5206\u4e3a\u56db\u79cd\uff1a\u53f3\u65cb\u3001\u5de6\u65cb\u3001\u5148\u53f3\u65cb\u540e\u5de6\u65cb\u3001\u5148\u5de6\u65cb\u540e\u53f3\u65cb\u3002\u4e0b\u9762\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65cb\u8f6c\u64cd\u4f5c\u3002
"},{"location":"chapter_tree/avl_tree/#1_1","title":"1. \u00a0 \u53f3\u65cb","text":"\u5982\u56fe 7-26 \u6240\u793a\uff0c\u8282\u70b9\u4e0b\u65b9\u4e3a\u5e73\u8861\u56e0\u5b50\u3002\u4ece\u5e95\u81f3\u9876\u770b\uff0c\u4e8c\u53c9\u6811\u4e2d\u9996\u4e2a\u5931\u8861\u8282\u70b9\u662f\u201c\u8282\u70b9 3\u201d\u3002\u6211\u4eec\u5173\u6ce8\u4ee5\u8be5\u5931\u8861\u8282\u70b9\u4e3a\u6839\u8282\u70b9\u7684\u5b50\u6811\uff0c\u5c06\u8be5\u8282\u70b9\u8bb0\u4e3a node
\uff0c\u5176\u5de6\u5b50\u8282\u70b9\u8bb0\u4e3a child
\uff0c\u6267\u884c\u201c\u53f3\u65cb\u201d\u64cd\u4f5c\u3002\u5b8c\u6210\u53f3\u65cb\u540e\uff0c\u5b50\u6811\u6062\u590d\u5e73\u8861\uff0c\u5e76\u4e14\u4ecd\u7136\u4fdd\u6301\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u6027\u8d28\u3002
\u56fe 7-26 \u00a0 \u53f3\u65cb\u64cd\u4f5c\u6b65\u9aa4
\u5982\u56fe 7-27 \u6240\u793a\uff0c\u5f53\u8282\u70b9 child
\u6709\u53f3\u5b50\u8282\u70b9\uff08\u8bb0\u4e3a grandChild
\uff09\u65f6\uff0c\u9700\u8981\u5728\u53f3\u65cb\u4e2d\u6dfb\u52a0\u4e00\u6b65\uff1a\u5c06 grandChild
\u4f5c\u4e3a node
\u7684\u5de6\u5b50\u8282\u70b9\u3002
\u56fe 7-27 \u00a0 \u6709 grandChild \u7684\u53f3\u65cb\u64cd\u4f5c
\u201c\u5411\u53f3\u65cb\u8f6c\u201d\u662f\u4e00\u79cd\u5f62\u8c61\u5316\u7684\u8bf4\u6cd5\uff0c\u5b9e\u9645\u4e0a\u9700\u8981\u901a\u8fc7\u4fee\u6539\u8282\u70b9\u6307\u9488\u6765\u5b9e\u73b0\uff0c\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig avl_tree.pydef right_rotate(self, node: TreeNode | None) -> TreeNode | None:\n \"\"\"\u53f3\u65cb\u64cd\u4f5c\"\"\"\n child = node.left\n grand_child = child.right\n # \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\n child.right = node\n node.left = grand_child\n # \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n self.update_height(node)\n self.update_height(child)\n # \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child\n
avl_tree.cpp/* \u53f3\u65cb\u64cd\u4f5c */\nTreeNode *rightRotate(TreeNode *node) {\n TreeNode *child = node->left;\n TreeNode *grandChild = child->right;\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\n child->right = node;\n node->left = grandChild;\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n updateHeight(node);\n updateHeight(child);\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child;\n}\n
avl_tree.java/* \u53f3\u65cb\u64cd\u4f5c */\nTreeNode rightRotate(TreeNode node) {\n TreeNode child = node.left;\n TreeNode grandChild = child.right;\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\n child.right = node;\n node.left = grandChild;\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n updateHeight(node);\n updateHeight(child);\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child;\n}\n
avl_tree.cs/* \u53f3\u65cb\u64cd\u4f5c */\nTreeNode? RightRotate(TreeNode? node) {\n TreeNode? child = node?.left;\n TreeNode? grandChild = child?.right;\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\n child.right = node;\n node.left = grandChild;\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n UpdateHeight(node);\n UpdateHeight(child);\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child;\n}\n
avl_tree.go/* \u53f3\u65cb\u64cd\u4f5c */\nfunc (t *aVLTree) rightRotate(node *TreeNode) *TreeNode {\n child := node.Left\n grandChild := child.Right\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\n child.Right = node\n node.Left = grandChild\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n t.updateHeight(node)\n t.updateHeight(child)\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child\n}\n
avl_tree.swift/* \u53f3\u65cb\u64cd\u4f5c */\nfunc rightRotate(node: TreeNode?) -> TreeNode? {\n let child = node?.left\n let grandChild = child?.right\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\n child?.right = node\n node?.left = grandChild\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n updateHeight(node: node)\n updateHeight(node: child)\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child\n}\n
avl_tree.js/* \u53f3\u65cb\u64cd\u4f5c */\n#rightRotate(node) {\n const child = node.left;\n const grandChild = child.right;\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\n child.right = node;\n node.left = grandChild;\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n this.#updateHeight(node);\n this.#updateHeight(child);\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child;\n}\n
avl_tree.ts/* \u53f3\u65cb\u64cd\u4f5c */\nrightRotate(node: TreeNode): TreeNode {\n const child = node.left;\n const grandChild = child.right;\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\n child.right = node;\n node.left = grandChild;\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n this.updateHeight(node);\n this.updateHeight(child);\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child;\n}\n
avl_tree.dart/* \u53f3\u65cb\u64cd\u4f5c */\nTreeNode? rightRotate(TreeNode? node) {\n TreeNode? child = node!.left;\n TreeNode? grandChild = child!.right;\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\n child.right = node;\n node.left = grandChild;\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n updateHeight(node);\n updateHeight(child);\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child;\n}\n
avl_tree.rs/* \u53f3\u65cb\u64cd\u4f5c */\nfn right_rotate(node: OptionTreeNodeRc) -> OptionTreeNodeRc {\n match node {\n Some(node) => {\n let child = node.borrow().left.clone().unwrap();\n let grand_child = child.borrow().right.clone();\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\n child.borrow_mut().right = Some(node.clone());\n node.borrow_mut().left = grand_child;\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n Self::update_height(Some(node));\n Self::update_height(Some(child.clone()));\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n Some(child)\n }\n None => None,\n }\n}\n
avl_tree.c/* \u53f3\u65cb\u64cd\u4f5c */\nTreeNode *rightRotate(TreeNode *node) {\n TreeNode *child, *grandChild;\n child = node->left;\n grandChild = child->right;\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\n child->right = node;\n node->left = grandChild;\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n updateHeight(node);\n updateHeight(child);\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child;\n}\n
avl_tree.zig// \u53f3\u65cb\u64cd\u4f5c\nfn rightRotate(self: *Self, node: ?*inc.TreeNode(T)) ?*inc.TreeNode(T) {\n var child = node.?.left;\n var grandChild = child.?.right;\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\n child.?.right = node;\n node.?.left = grandChild;\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n self.updateHeight(node);\n self.updateHeight(child);\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child;\n}\n
"},{"location":"chapter_tree/avl_tree/#2_1","title":"2. \u00a0 \u5de6\u65cb","text":"\u76f8\u5e94\u5730\uff0c\u5982\u679c\u8003\u8651\u4e0a\u8ff0\u5931\u8861\u4e8c\u53c9\u6811\u7684\u201c\u955c\u50cf\u201d\uff0c\u5219\u9700\u8981\u6267\u884c\u56fe 7-28 \u6240\u793a\u7684\u201c\u5de6\u65cb\u201d\u64cd\u4f5c\u3002
\u56fe 7-28 \u00a0 \u5de6\u65cb\u64cd\u4f5c
\u540c\u7406\uff0c\u5982\u56fe 7-29 \u6240\u793a\uff0c\u5f53\u8282\u70b9 child
\u6709\u5de6\u5b50\u8282\u70b9\uff08\u8bb0\u4e3a grandChild
\uff09\u65f6\uff0c\u9700\u8981\u5728\u5de6\u65cb\u4e2d\u6dfb\u52a0\u4e00\u6b65\uff1a\u5c06 grandChild
\u4f5c\u4e3a node
\u7684\u53f3\u5b50\u8282\u70b9\u3002
\u56fe 7-29 \u00a0 \u6709 grandChild \u7684\u5de6\u65cb\u64cd\u4f5c
\u53ef\u4ee5\u89c2\u5bdf\u5230\uff0c\u53f3\u65cb\u548c\u5de6\u65cb\u64cd\u4f5c\u5728\u903b\u8f91\u4e0a\u662f\u955c\u50cf\u5bf9\u79f0\u7684\uff0c\u5b83\u4eec\u5206\u522b\u89e3\u51b3\u7684\u4e24\u79cd\u5931\u8861\u60c5\u51b5\u4e5f\u662f\u5bf9\u79f0\u7684\u3002\u57fa\u4e8e\u5bf9\u79f0\u6027\uff0c\u6211\u4eec\u53ea\u9700\u5c06\u53f3\u65cb\u7684\u5b9e\u73b0\u4ee3\u7801\u4e2d\u7684\u6240\u6709\u7684 left
\u66ff\u6362\u4e3a right
\uff0c\u5c06\u6240\u6709\u7684 right
\u66ff\u6362\u4e3a left
\uff0c\u5373\u53ef\u5f97\u5230\u5de6\u65cb\u7684\u5b9e\u73b0\u4ee3\u7801\uff1a
def left_rotate(self, node: TreeNode | None) -> TreeNode | None:\n \"\"\"\u5de6\u65cb\u64cd\u4f5c\"\"\"\n child = node.right\n grand_child = child.left\n # \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\n child.left = node\n node.right = grand_child\n # \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n self.update_height(node)\n self.update_height(child)\n # \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child\n
avl_tree.cpp/* \u5de6\u65cb\u64cd\u4f5c */\nTreeNode *leftRotate(TreeNode *node) {\n TreeNode *child = node->right;\n TreeNode *grandChild = child->left;\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\n child->left = node;\n node->right = grandChild;\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n updateHeight(node);\n updateHeight(child);\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child;\n}\n
avl_tree.java/* \u5de6\u65cb\u64cd\u4f5c */\nTreeNode leftRotate(TreeNode node) {\n TreeNode child = node.right;\n TreeNode grandChild = child.left;\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\n child.left = node;\n node.right = grandChild;\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n updateHeight(node);\n updateHeight(child);\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child;\n}\n
avl_tree.cs/* \u5de6\u65cb\u64cd\u4f5c */\nTreeNode? LeftRotate(TreeNode? node) {\n TreeNode? child = node?.right;\n TreeNode? grandChild = child?.left;\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\n child.left = node;\n node.right = grandChild;\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n UpdateHeight(node);\n UpdateHeight(child);\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child;\n}\n
avl_tree.go/* \u5de6\u65cb\u64cd\u4f5c */\nfunc (t *aVLTree) leftRotate(node *TreeNode) *TreeNode {\n child := node.Right\n grandChild := child.Left\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\n child.Left = node\n node.Right = grandChild\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n t.updateHeight(node)\n t.updateHeight(child)\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child\n}\n
avl_tree.swift/* \u5de6\u65cb\u64cd\u4f5c */\nfunc leftRotate(node: TreeNode?) -> TreeNode? {\n let child = node?.right\n let grandChild = child?.left\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\n child?.left = node\n node?.right = grandChild\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n updateHeight(node: node)\n updateHeight(node: child)\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child\n}\n
avl_tree.js/* \u5de6\u65cb\u64cd\u4f5c */\n#leftRotate(node) {\n const child = node.right;\n const grandChild = child.left;\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\n child.left = node;\n node.right = grandChild;\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n this.#updateHeight(node);\n this.#updateHeight(child);\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child;\n}\n
avl_tree.ts/* \u5de6\u65cb\u64cd\u4f5c */\nleftRotate(node: TreeNode): TreeNode {\n const child = node.right;\n const grandChild = child.left;\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\n child.left = node;\n node.right = grandChild;\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n this.updateHeight(node);\n this.updateHeight(child);\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child;\n}\n
avl_tree.dart/* \u5de6\u65cb\u64cd\u4f5c */\nTreeNode? leftRotate(TreeNode? node) {\n TreeNode? child = node!.right;\n TreeNode? grandChild = child!.left;\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\n child.left = node;\n node.right = grandChild;\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n updateHeight(node);\n updateHeight(child);\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child;\n}\n
avl_tree.rs/* \u5de6\u65cb\u64cd\u4f5c */\nfn left_rotate(node: OptionTreeNodeRc) -> OptionTreeNodeRc {\n match node {\n Some(node) => {\n let child = node.borrow().right.clone().unwrap();\n let grand_child = child.borrow().left.clone();\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\n child.borrow_mut().left = Some(node.clone());\n node.borrow_mut().right = grand_child;\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n Self::update_height(Some(node));\n Self::update_height(Some(child.clone()));\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n Some(child)\n }\n None => None,\n }\n}\n
avl_tree.c/* \u5de6\u65cb\u64cd\u4f5c */\nTreeNode *leftRotate(TreeNode *node) {\n TreeNode *child, *grandChild;\n child = node->right;\n grandChild = child->left;\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\n child->left = node;\n node->right = grandChild;\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n updateHeight(node);\n updateHeight(child);\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child;\n}\n
avl_tree.zig// \u5de6\u65cb\u64cd\u4f5c\nfn leftRotate(self: *Self, node: ?*inc.TreeNode(T)) ?*inc.TreeNode(T) {\n var child = node.?.right;\n var grandChild = child.?.left;\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\n child.?.left = node;\n node.?.right = grandChild;\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n self.updateHeight(node);\n self.updateHeight(child);\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child;\n}\n
"},{"location":"chapter_tree/avl_tree/#3","title":"3. \u00a0 \u5148\u5de6\u65cb\u540e\u53f3\u65cb","text":"\u5bf9\u4e8e\u56fe 7-30 \u4e2d\u7684\u5931\u8861\u8282\u70b9 3 \uff0c\u4ec5\u4f7f\u7528\u5de6\u65cb\u6216\u53f3\u65cb\u90fd\u65e0\u6cd5\u4f7f\u5b50\u6811\u6062\u590d\u5e73\u8861\u3002\u6b64\u65f6\u9700\u8981\u5148\u5bf9 child
\u6267\u884c\u201c\u5de6\u65cb\u201d\uff0c\u518d\u5bf9 node
\u6267\u884c\u201c\u53f3\u65cb\u201d\u3002
\u56fe 7-30 \u00a0 \u5148\u5de6\u65cb\u540e\u53f3\u65cb
"},{"location":"chapter_tree/avl_tree/#4","title":"4. \u00a0 \u5148\u53f3\u65cb\u540e\u5de6\u65cb","text":"\u5982\u56fe 7-31 \u6240\u793a\uff0c\u5bf9\u4e8e\u4e0a\u8ff0\u5931\u8861\u4e8c\u53c9\u6811\u7684\u955c\u50cf\u60c5\u51b5\uff0c\u9700\u8981\u5148\u5bf9 child
\u6267\u884c\u201c\u53f3\u65cb\u201d\uff0c\u518d\u5bf9 node
\u6267\u884c\u201c\u5de6\u65cb\u201d\u3002
\u56fe 7-31 \u00a0 \u5148\u53f3\u65cb\u540e\u5de6\u65cb
"},{"location":"chapter_tree/avl_tree/#5","title":"5. \u00a0 \u65cb\u8f6c\u7684\u9009\u62e9","text":"\u56fe 7-32 \u5c55\u793a\u7684\u56db\u79cd\u5931\u8861\u60c5\u51b5\u4e0e\u4e0a\u8ff0\u6848\u4f8b\u9010\u4e2a\u5bf9\u5e94\uff0c\u5206\u522b\u9700\u8981\u91c7\u7528\u53f3\u65cb\u3001\u5148\u5de6\u65cb\u540e\u53f3\u65cb\u3001\u5148\u53f3\u65cb\u540e\u5de6\u65cb\u3001\u5de6\u65cb\u7684\u64cd\u4f5c\u3002
\u56fe 7-32 \u00a0 AVL \u6811\u7684\u56db\u79cd\u65cb\u8f6c\u60c5\u51b5
\u5982\u4e0b\u8868\u6240\u793a\uff0c\u6211\u4eec\u901a\u8fc7\u5224\u65ad\u5931\u8861\u8282\u70b9\u7684\u5e73\u8861\u56e0\u5b50\u4ee5\u53ca\u8f83\u9ad8\u4e00\u4fa7\u5b50\u8282\u70b9\u7684\u5e73\u8861\u56e0\u5b50\u7684\u6b63\u8d1f\u53f7\uff0c\u6765\u786e\u5b9a\u5931\u8861\u8282\u70b9\u5c5e\u4e8e\u56fe 7-32 \u4e2d\u7684\u54ea\u79cd\u60c5\u51b5\u3002
\u8868 7-3 \u00a0 \u56db\u79cd\u65cb\u8f6c\u60c5\u51b5\u7684\u9009\u62e9\u6761\u4ef6
\u5931\u8861\u8282\u70b9\u7684\u5e73\u8861\u56e0\u5b50 \u5b50\u8282\u70b9\u7684\u5e73\u8861\u56e0\u5b50 \u5e94\u91c7\u7528\u7684\u65cb\u8f6c\u65b9\u6cd5 \\(> 1\\) \uff08\u5de6\u504f\u6811\uff09 \\(\\geq 0\\) \u53f3\u65cb \\(> 1\\) \uff08\u5de6\u504f\u6811\uff09 \\(<0\\) \u5148\u5de6\u65cb\u540e\u53f3\u65cb \\(< -1\\) \uff08\u53f3\u504f\u6811\uff09 \\(\\leq 0\\) \u5de6\u65cb \\(< -1\\) \uff08\u53f3\u504f\u6811\uff09 \\(>0\\) \u5148\u53f3\u65cb\u540e\u5de6\u65cb\u4e3a\u4e86\u4fbf\u4e8e\u4f7f\u7528\uff0c\u6211\u4eec\u5c06\u65cb\u8f6c\u64cd\u4f5c\u5c01\u88c5\u6210\u4e00\u4e2a\u51fd\u6570\u3002\u6709\u4e86\u8fd9\u4e2a\u51fd\u6570\uff0c\u6211\u4eec\u5c31\u80fd\u5bf9\u5404\u79cd\u5931\u8861\u60c5\u51b5\u8fdb\u884c\u65cb\u8f6c\uff0c\u4f7f\u5931\u8861\u8282\u70b9\u91cd\u65b0\u6062\u590d\u5e73\u8861\u3002\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig avl_tree.pydef rotate(self, node: TreeNode | None) -> TreeNode | None:\n \"\"\"\u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861\"\"\"\n # \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\n balance_factor = self.balance_factor(node)\n # \u5de6\u504f\u6811\n if balance_factor > 1:\n if self.balance_factor(node.left) >= 0:\n # \u53f3\u65cb\n return self.right_rotate(node)\n else:\n # \u5148\u5de6\u65cb\u540e\u53f3\u65cb\n node.left = self.left_rotate(node.left)\n return self.right_rotate(node)\n # \u53f3\u504f\u6811\n elif balance_factor < -1:\n if self.balance_factor(node.right) <= 0:\n # \u5de6\u65cb\n return self.left_rotate(node)\n else:\n # \u5148\u53f3\u65cb\u540e\u5de6\u65cb\n node.right = self.right_rotate(node.right)\n return self.left_rotate(node)\n # \u5e73\u8861\u6811\uff0c\u65e0\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\n return node\n
avl_tree.cpp/* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nTreeNode *rotate(TreeNode *node) {\n // \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\n int _balanceFactor = balanceFactor(node);\n // \u5de6\u504f\u6811\n if (_balanceFactor > 1) {\n if (balanceFactor(node->left) >= 0) {\n // \u53f3\u65cb\n return rightRotate(node);\n } else {\n // \u5148\u5de6\u65cb\u540e\u53f3\u65cb\n node->left = leftRotate(node->left);\n return rightRotate(node);\n }\n }\n // \u53f3\u504f\u6811\n if (_balanceFactor < -1) {\n if (balanceFactor(node->right) <= 0) {\n // \u5de6\u65cb\n return leftRotate(node);\n } else {\n // \u5148\u53f3\u65cb\u540e\u5de6\u65cb\n node->right = rightRotate(node->right);\n return leftRotate(node);\n }\n }\n // \u5e73\u8861\u6811\uff0c\u65e0\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\n return node;\n}\n
avl_tree.java/* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nTreeNode rotate(TreeNode node) {\n // \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\n int balanceFactor = balanceFactor(node);\n // \u5de6\u504f\u6811\n if (balanceFactor > 1) {\n if (balanceFactor(node.left) >= 0) {\n // \u53f3\u65cb\n return rightRotate(node);\n } else {\n // \u5148\u5de6\u65cb\u540e\u53f3\u65cb\n node.left = leftRotate(node.left);\n return rightRotate(node);\n }\n }\n // \u53f3\u504f\u6811\n if (balanceFactor < -1) {\n if (balanceFactor(node.right) <= 0) {\n // \u5de6\u65cb\n return leftRotate(node);\n } else {\n // \u5148\u53f3\u65cb\u540e\u5de6\u65cb\n node.right = rightRotate(node.right);\n return leftRotate(node);\n }\n }\n // \u5e73\u8861\u6811\uff0c\u65e0\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\n return node;\n}\n
avl_tree.cs/* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nTreeNode? Rotate(TreeNode? node) {\n // \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\n int balanceFactorInt = BalanceFactor(node);\n // \u5de6\u504f\u6811\n if (balanceFactorInt > 1) {\n if (BalanceFactor(node?.left) >= 0) {\n // \u53f3\u65cb\n return RightRotate(node);\n } else {\n // \u5148\u5de6\u65cb\u540e\u53f3\u65cb\n node!.left = LeftRotate(node!.left);\n return RightRotate(node);\n }\n }\n // \u53f3\u504f\u6811\n if (balanceFactorInt < -1) {\n if (BalanceFactor(node?.right) <= 0) {\n // \u5de6\u65cb\n return LeftRotate(node);\n } else {\n // \u5148\u53f3\u65cb\u540e\u5de6\u65cb\n node!.right = RightRotate(node!.right);\n return LeftRotate(node);\n }\n }\n // \u5e73\u8861\u6811\uff0c\u65e0\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\n return node;\n}\n
avl_tree.go/* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nfunc (t *aVLTree) rotate(node *TreeNode) *TreeNode {\n // \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\n // Go \u63a8\u8350\u77ed\u53d8\u91cf\uff0c\u8fd9\u91cc bf \u6307\u4ee3 t.balanceFactor\n bf := t.balanceFactor(node)\n // \u5de6\u504f\u6811\n if bf > 1 {\n if t.balanceFactor(node.Left) >= 0 {\n // \u53f3\u65cb\n return t.rightRotate(node)\n } else {\n // \u5148\u5de6\u65cb\u540e\u53f3\u65cb\n node.Left = t.leftRotate(node.Left)\n return t.rightRotate(node)\n }\n }\n // \u53f3\u504f\u6811\n if bf < -1 {\n if t.balanceFactor(node.Right) <= 0 {\n // \u5de6\u65cb\n return t.leftRotate(node)\n } else {\n // \u5148\u53f3\u65cb\u540e\u5de6\u65cb\n node.Right = t.rightRotate(node.Right)\n return t.leftRotate(node)\n }\n }\n // \u5e73\u8861\u6811\uff0c\u65e0\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\n return node\n}\n
avl_tree.swift/* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nfunc rotate(node: TreeNode?) -> TreeNode? {\n // \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\n let balanceFactor = balanceFactor(node: node)\n // \u5de6\u504f\u6811\n if balanceFactor > 1 {\n if self.balanceFactor(node: node?.left) >= 0 {\n // \u53f3\u65cb\n return rightRotate(node: node)\n } else {\n // \u5148\u5de6\u65cb\u540e\u53f3\u65cb\n node?.left = leftRotate(node: node?.left)\n return rightRotate(node: node)\n }\n }\n // \u53f3\u504f\u6811\n if balanceFactor < -1 {\n if self.balanceFactor(node: node?.right) <= 0 {\n // \u5de6\u65cb\n return leftRotate(node: node)\n } else {\n // \u5148\u53f3\u65cb\u540e\u5de6\u65cb\n node?.right = rightRotate(node: node?.right)\n return leftRotate(node: node)\n }\n }\n // \u5e73\u8861\u6811\uff0c\u65e0\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\n return node\n}\n
avl_tree.js/* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n#rotate(node) {\n // \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\n const balanceFactor = this.balanceFactor(node);\n // \u5de6\u504f\u6811\n if (balanceFactor > 1) {\n if (this.balanceFactor(node.left) >= 0) {\n // \u53f3\u65cb\n return this.#rightRotate(node);\n } else {\n // \u5148\u5de6\u65cb\u540e\u53f3\u65cb\n node.left = this.#leftRotate(node.left);\n return this.#rightRotate(node);\n }\n }\n // \u53f3\u504f\u6811\n if (balanceFactor < -1) {\n if (this.balanceFactor(node.right) <= 0) {\n // \u5de6\u65cb\n return this.#leftRotate(node);\n } else {\n // \u5148\u53f3\u65cb\u540e\u5de6\u65cb\n node.right = this.#rightRotate(node.right);\n return this.#leftRotate(node);\n }\n }\n // \u5e73\u8861\u6811\uff0c\u65e0\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\n return node;\n}\n
avl_tree.ts/* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nrotate(node: TreeNode): TreeNode {\n // \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\n const balanceFactor = this.balanceFactor(node);\n // \u5de6\u504f\u6811\n if (balanceFactor > 1) {\n if (this.balanceFactor(node.left) >= 0) {\n // \u53f3\u65cb\n return this.rightRotate(node);\n } else {\n // \u5148\u5de6\u65cb\u540e\u53f3\u65cb\n node.left = this.leftRotate(node.left);\n return this.rightRotate(node);\n }\n }\n // \u53f3\u504f\u6811\n if (balanceFactor < -1) {\n if (this.balanceFactor(node.right) <= 0) {\n // \u5de6\u65cb\n return this.leftRotate(node);\n } else {\n // \u5148\u53f3\u65cb\u540e\u5de6\u65cb\n node.right = this.rightRotate(node.right);\n return this.leftRotate(node);\n }\n }\n // \u5e73\u8861\u6811\uff0c\u65e0\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\n return node;\n}\n
avl_tree.dart/* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nTreeNode? rotate(TreeNode? node) {\n // \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\n int factor = balanceFactor(node);\n // \u5de6\u504f\u6811\n if (factor > 1) {\n if (balanceFactor(node!.left) >= 0) {\n // \u53f3\u65cb\n return rightRotate(node);\n } else {\n // \u5148\u5de6\u65cb\u540e\u53f3\u65cb\n node.left = leftRotate(node.left);\n return rightRotate(node);\n }\n }\n // \u53f3\u504f\u6811\n if (factor < -1) {\n if (balanceFactor(node!.right) <= 0) {\n // \u5de6\u65cb\n return leftRotate(node);\n } else {\n // \u5148\u53f3\u65cb\u540e\u5de6\u65cb\n node.right = rightRotate(node.right);\n return leftRotate(node);\n }\n }\n // \u5e73\u8861\u6811\uff0c\u65e0\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\n return node;\n}\n
avl_tree.rs/* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nfn rotate(node: OptionTreeNodeRc) -> OptionTreeNodeRc {\n // \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\n let balance_factor = Self::balance_factor(node.clone());\n // \u5de6\u504f\u6811\n if balance_factor > 1 {\n let node = node.unwrap();\n if Self::balance_factor(node.borrow().left.clone()) >= 0 {\n // \u53f3\u65cb\n Self::right_rotate(Some(node))\n } else {\n // \u5148\u5de6\u65cb\u540e\u53f3\u65cb\n let left = node.borrow().left.clone();\n node.borrow_mut().left = Self::left_rotate(left);\n Self::right_rotate(Some(node))\n }\n }\n // \u53f3\u504f\u6811\n else if balance_factor < -1 {\n let node = node.unwrap();\n if Self::balance_factor(node.borrow().right.clone()) <= 0 {\n // \u5de6\u65cb\n Self::left_rotate(Some(node))\n } else {\n // \u5148\u53f3\u65cb\u540e\u5de6\u65cb\n let right = node.borrow().right.clone();\n node.borrow_mut().right = Self::right_rotate(right);\n Self::left_rotate(Some(node))\n }\n } else {\n // \u5e73\u8861\u6811\uff0c\u65e0\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\n node\n }\n}\n
avl_tree.c/* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nTreeNode *rotate(TreeNode *node) {\n // \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\n int bf = balanceFactor(node);\n // \u5de6\u504f\u6811\n if (bf > 1) {\n if (balanceFactor(node->left) >= 0) {\n // \u53f3\u65cb\n return rightRotate(node);\n } else {\n // \u5148\u5de6\u65cb\u540e\u53f3\u65cb\n node->left = leftRotate(node->left);\n return rightRotate(node);\n }\n }\n // \u53f3\u504f\u6811\n if (bf < -1) {\n if (balanceFactor(node->right) <= 0) {\n // \u5de6\u65cb\n return leftRotate(node);\n } else {\n // \u5148\u53f3\u65cb\u540e\u5de6\u65cb\n node->right = rightRotate(node->right);\n return leftRotate(node);\n }\n }\n // \u5e73\u8861\u6811\uff0c\u65e0\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\n return node;\n}\n
avl_tree.zig// \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861\nfn rotate(self: *Self, node: ?*inc.TreeNode(T)) ?*inc.TreeNode(T) {\n // \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\n var balance_factor = self.balanceFactor(node);\n // \u5de6\u504f\u6811\n if (balance_factor > 1) {\n if (self.balanceFactor(node.?.left) >= 0) {\n // \u53f3\u65cb\n return self.rightRotate(node);\n } else {\n // \u5148\u5de6\u65cb\u540e\u53f3\u65cb\n node.?.left = self.leftRotate(node.?.left);\n return self.rightRotate(node);\n }\n }\n // \u53f3\u504f\u6811\n if (balance_factor < -1) {\n if (self.balanceFactor(node.?.right) <= 0) {\n // \u5de6\u65cb\n return self.leftRotate(node);\n } else {\n // \u5148\u53f3\u65cb\u540e\u5de6\u65cb\n node.?.right = self.rightRotate(node.?.right);\n return self.leftRotate(node);\n }\n }\n // \u5e73\u8861\u6811\uff0c\u65e0\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\n return node;\n}\n
"},{"location":"chapter_tree/avl_tree/#753-avl","title":"7.5.3 \u00a0 AVL \u6811\u5e38\u7528\u64cd\u4f5c","text":""},{"location":"chapter_tree/avl_tree/#1_2","title":"1. \u00a0 \u63d2\u5165\u8282\u70b9","text":"AVL \u6811\u7684\u8282\u70b9\u63d2\u5165\u64cd\u4f5c\u4e0e\u4e8c\u53c9\u641c\u7d22\u6811\u5728\u4e3b\u4f53\u4e0a\u7c7b\u4f3c\u3002\u552f\u4e00\u7684\u533a\u522b\u5728\u4e8e\uff0c\u5728 AVL \u6811\u4e2d\u63d2\u5165\u8282\u70b9\u540e\uff0c\u4ece\u8be5\u8282\u70b9\u5230\u6839\u8282\u70b9\u7684\u8def\u5f84\u4e0a\u53ef\u80fd\u4f1a\u51fa\u73b0\u4e00\u7cfb\u5217\u5931\u8861\u8282\u70b9\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u9700\u8981\u4ece\u8fd9\u4e2a\u8282\u70b9\u5f00\u59cb\uff0c\u81ea\u5e95\u5411\u4e0a\u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u6240\u6709\u5931\u8861\u8282\u70b9\u6062\u590d\u5e73\u8861\u3002\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig avl_tree.pydef insert(self, val):\n \"\"\"\u63d2\u5165\u8282\u70b9\"\"\"\n self._root = self.insert_helper(self._root, val)\n\ndef insert_helper(self, node: TreeNode | None, val: int) -> TreeNode:\n \"\"\"\u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09\"\"\"\n if node is None:\n return TreeNode(val)\n # 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\uff0c\u5e76\u63d2\u5165\u8282\u70b9\n if val < node.val:\n node.left = self.insert_helper(node.left, val)\n elif val > node.val:\n node.right = self.insert_helper(node.right, val)\n else:\n # \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n return node\n # \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n self.update_height(node)\n # 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861\n return self.rotate(node)\n
avl_tree.cpp/* \u63d2\u5165\u8282\u70b9 */\nvoid insert(int val) {\n root = insertHelper(root, val);\n}\n\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nTreeNode *insertHelper(TreeNode *node, int val) {\n if (node == nullptr)\n return new TreeNode(val);\n /* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\uff0c\u5e76\u63d2\u5165\u8282\u70b9 */\n if (val < node->val)\n node->left = insertHelper(node->left, val);\n else if (val > node->val)\n node->right = insertHelper(node->right, val);\n else\n return node; // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n updateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n node = rotate(node);\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n return node;\n}\n
avl_tree.java/* \u63d2\u5165\u8282\u70b9 */\nvoid insert(int val) {\n root = insertHelper(root, val);\n}\n\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nTreeNode insertHelper(TreeNode node, int val) {\n if (node == null)\n return new TreeNode(val);\n /* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\uff0c\u5e76\u63d2\u5165\u8282\u70b9 */\n if (val < node.val)\n node.left = insertHelper(node.left, val);\n else if (val > node.val)\n node.right = insertHelper(node.right, val);\n else\n return node; // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n updateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n node = rotate(node);\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n return node;\n}\n
avl_tree.cs/* \u63d2\u5165\u8282\u70b9 */\nvoid Insert(int val) {\n root = InsertHelper(root, val);\n}\n\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nTreeNode? InsertHelper(TreeNode? node, int val) {\n if (node == null) return new TreeNode(val);\n /* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\uff0c\u5e76\u63d2\u5165\u8282\u70b9 */\n if (val < node.val)\n node.left = InsertHelper(node.left, val);\n else if (val > node.val)\n node.right = InsertHelper(node.right, val);\n else\n return node; // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n UpdateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n node = Rotate(node);\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n return node;\n}\n
avl_tree.go/* \u63d2\u5165\u8282\u70b9 */\nfunc (t *aVLTree) insert(val int) {\n t.root = t.insertHelper(t.root, val)\n}\n\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u51fd\u6570\uff09 */\nfunc (t *aVLTree) insertHelper(node *TreeNode, val int) *TreeNode {\n if node == nil {\n return NewTreeNode(val)\n }\n /* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\uff0c\u5e76\u63d2\u5165\u8282\u70b9 */\n if val < node.Val.(int) {\n node.Left = t.insertHelper(node.Left, val)\n } else if val > node.Val.(int) {\n node.Right = t.insertHelper(node.Right, val)\n } else {\n // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n return node\n }\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n t.updateHeight(node)\n /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n node = t.rotate(node)\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n return node\n}\n
avl_tree.swift/* \u63d2\u5165\u8282\u70b9 */\nfunc insert(val: Int) {\n root = insertHelper(node: root, val: val)\n}\n\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nfunc insertHelper(node: TreeNode?, val: Int) -> TreeNode? {\n var node = node\n if node == nil {\n return TreeNode(x: val)\n }\n /* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\uff0c\u5e76\u63d2\u5165\u8282\u70b9 */\n if val < node!.val {\n node?.left = insertHelper(node: node?.left, val: val)\n } else if val > node!.val {\n node?.right = insertHelper(node: node?.right, val: val)\n } else {\n return node // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n }\n updateHeight(node: node) // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n node = rotate(node: node)\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n return node\n}\n
avl_tree.js/* \u63d2\u5165\u8282\u70b9 */\ninsert(val) {\n this.root = this.#insertHelper(this.root, val);\n}\n\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\n#insertHelper(node, val) {\n if (node === null) return new TreeNode(val);\n /* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\uff0c\u5e76\u63d2\u5165\u8282\u70b9 */\n if (val < node.val) node.left = this.#insertHelper(node.left, val);\n else if (val > node.val)\n node.right = this.#insertHelper(node.right, val);\n else return node; // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n this.#updateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n node = this.#rotate(node);\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n return node;\n}\n
avl_tree.ts/* \u63d2\u5165\u8282\u70b9 */\ninsert(val: number): void {\n this.root = this.insertHelper(this.root, val);\n}\n\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\ninsertHelper(node: TreeNode, val: number): TreeNode {\n if (node === null) return new TreeNode(val);\n /* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\uff0c\u5e76\u63d2\u5165\u8282\u70b9 */\n if (val < node.val) {\n node.left = this.insertHelper(node.left, val);\n } else if (val > node.val) {\n node.right = this.insertHelper(node.right, val);\n } else {\n return node; // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n }\n this.updateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n node = this.rotate(node);\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n return node;\n}\n
avl_tree.dart/* \u63d2\u5165\u8282\u70b9 */\nvoid insert(int val) {\n root = insertHelper(root, val);\n}\n\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nTreeNode? insertHelper(TreeNode? node, int val) {\n if (node == null) return TreeNode(val);\n /* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\uff0c\u5e76\u63d2\u5165\u8282\u70b9 */\n if (val < node.val)\n node.left = insertHelper(node.left, val);\n else if (val > node.val)\n node.right = insertHelper(node.right, val);\n else\n return node; // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n updateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n node = rotate(node);\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n return node;\n}\n
avl_tree.rs/* \u63d2\u5165\u8282\u70b9 */\nfn insert(&mut self, val: i32) {\n self.root = Self::insert_helper(self.root.clone(), val);\n}\n\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nfn insert_helper(node: OptionTreeNodeRc, val: i32) -> OptionTreeNodeRc {\n match node {\n Some(mut node) => {\n /* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\uff0c\u5e76\u63d2\u5165\u8282\u70b9 */\n match {\n let node_val = node.borrow().val;\n node_val\n }\n .cmp(&val)\n {\n Ordering::Greater => {\n let left = node.borrow().left.clone();\n node.borrow_mut().left = Self::insert_helper(left, val);\n }\n Ordering::Less => {\n let right = node.borrow().right.clone();\n node.borrow_mut().right = Self::insert_helper(right, val);\n }\n Ordering::Equal => {\n return Some(node); // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n }\n }\n Self::update_height(Some(node.clone())); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n node = Self::rotate(Some(node)).unwrap();\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n Some(node)\n }\n None => Some(TreeNode::new(val)),\n }\n}\n
avl_tree.c/* \u63d2\u5165\u8282\u70b9 */\nvoid insert(AVLTree *tree, int val) {\n tree->root = insertHelper(tree->root, val);\n}\n\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u51fd\u6570\uff09 */\nTreeNode *insertHelper(TreeNode *node, int val) {\n if (node == NULL) {\n return newTreeNode(val);\n }\n /* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\uff0c\u5e76\u63d2\u5165\u8282\u70b9 */\n if (val < node->val) {\n node->left = insertHelper(node->left, val);\n } else if (val > node->val) {\n node->right = insertHelper(node->right, val);\n } else {\n // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n return node;\n }\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n updateHeight(node);\n /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n node = rotate(node);\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n return node;\n}\n
avl_tree.zig// \u63d2\u5165\u8282\u70b9\nfn insert(self: *Self, val: T) !void {\n self.root = (try self.insertHelper(self.root, val)).?;\n}\n\n// \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09\nfn insertHelper(self: *Self, node_: ?*inc.TreeNode(T), val: T) !?*inc.TreeNode(T) {\n var node = node_;\n if (node == null) {\n var tmp_node = try self.mem_allocator.create(inc.TreeNode(T));\n tmp_node.init(val);\n return tmp_node;\n }\n // 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\uff0c\u5e76\u63d2\u5165\u8282\u70b9\n if (val < node.?.val) {\n node.?.left = try self.insertHelper(node.?.left, val);\n } else if (val > node.?.val) {\n node.?.right = try self.insertHelper(node.?.right, val);\n } else {\n return node; // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n }\n self.updateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n // 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861\n node = self.rotate(node);\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n return node;\n}\n
"},{"location":"chapter_tree/avl_tree/#2_2","title":"2. \u00a0 \u5220\u9664\u8282\u70b9","text":"\u7c7b\u4f3c\u5730\uff0c\u5728\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u5220\u9664\u8282\u70b9\u65b9\u6cd5\u7684\u57fa\u7840\u4e0a\uff0c\u9700\u8981\u4ece\u5e95\u81f3\u9876\u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u6240\u6709\u5931\u8861\u8282\u70b9\u6062\u590d\u5e73\u8861\u3002\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig avl_tree.pydef remove(self, val: int):\n \"\"\"\u5220\u9664\u8282\u70b9\"\"\"\n self._root = self.remove_helper(self._root, val)\n\ndef remove_helper(self, node: TreeNode | None, val: int) -> TreeNode | None:\n \"\"\"\u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09\"\"\"\n if node is None:\n return None\n # 1. \u67e5\u627e\u8282\u70b9\uff0c\u5e76\u5220\u9664\u4e4b\n if val < node.val:\n node.left = self.remove_helper(node.left, val)\n elif val > node.val:\n node.right = self.remove_helper(node.right, val)\n else:\n if node.left is None or node.right is None:\n child = node.left or node.right\n # \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\n if child is None:\n return None\n # \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\n else:\n node = child\n else:\n # \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\n temp = node.right\n while temp.left is not None:\n temp = temp.left\n node.right = self.remove_helper(node.right, temp.val)\n node.val = temp.val\n # \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n self.update_height(node)\n # 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861\n return self.rotate(node)\n
avl_tree.cpp/* \u5220\u9664\u8282\u70b9 */\nvoid remove(int val) {\n root = removeHelper(root, val);\n}\n\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nTreeNode *removeHelper(TreeNode *node, int val) {\n if (node == nullptr)\n return nullptr;\n /* 1. \u67e5\u627e\u8282\u70b9\uff0c\u5e76\u5220\u9664\u4e4b */\n if (val < node->val)\n node->left = removeHelper(node->left, val);\n else if (val > node->val)\n node->right = removeHelper(node->right, val);\n else {\n if (node->left == nullptr || node->right == nullptr) {\n TreeNode *child = node->left != nullptr ? node->left : node->right;\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\n if (child == nullptr) {\n delete node;\n return nullptr;\n }\n // \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\n else {\n delete node;\n node = child;\n }\n } else {\n // \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\n TreeNode *temp = node->right;\n while (temp->left != nullptr) {\n temp = temp->left;\n }\n int tempVal = temp->val;\n node->right = removeHelper(node->right, temp->val);\n node->val = tempVal;\n }\n }\n updateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n node = rotate(node);\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n return node;\n}\n
avl_tree.java/* \u5220\u9664\u8282\u70b9 */\nvoid remove(int val) {\n root = removeHelper(root, val);\n}\n\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nTreeNode removeHelper(TreeNode node, int val) {\n if (node == null)\n return null;\n /* 1. \u67e5\u627e\u8282\u70b9\uff0c\u5e76\u5220\u9664\u4e4b */\n if (val < node.val)\n node.left = removeHelper(node.left, val);\n else if (val > node.val)\n node.right = removeHelper(node.right, val);\n else {\n if (node.left == null || node.right == null) {\n TreeNode child = node.left != null ? node.left : node.right;\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\n if (child == null)\n return null;\n // \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\n else\n node = child;\n } else {\n // \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\n TreeNode temp = node.right;\n while (temp.left != null) {\n temp = temp.left;\n }\n node.right = removeHelper(node.right, temp.val);\n node.val = temp.val;\n }\n }\n updateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n node = rotate(node);\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n return node;\n}\n
avl_tree.cs/* \u5220\u9664\u8282\u70b9 */\nvoid Remove(int val) {\n root = RemoveHelper(root, val);\n}\n\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nTreeNode? RemoveHelper(TreeNode? node, int val) {\n if (node == null) return null;\n /* 1. \u67e5\u627e\u8282\u70b9\uff0c\u5e76\u5220\u9664\u4e4b */\n if (val < node.val)\n node.left = RemoveHelper(node.left, val);\n else if (val > node.val)\n node.right = RemoveHelper(node.right, val);\n else {\n if (node.left == null || node.right == null) {\n TreeNode? child = node.left ?? node.right;\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\n if (child == null)\n return null;\n // \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\n else\n node = child;\n } else {\n // \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\n TreeNode? temp = node.right;\n while (temp.left != null) {\n temp = temp.left;\n }\n node.right = RemoveHelper(node.right, temp.val!.Value);\n node.val = temp.val;\n }\n }\n UpdateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n node = Rotate(node);\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n return node;\n}\n
avl_tree.go/* \u5220\u9664\u8282\u70b9 */\nfunc (t *aVLTree) remove(val int) {\n t.root = t.removeHelper(t.root, val)\n}\n\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u51fd\u6570\uff09 */\nfunc (t *aVLTree) removeHelper(node *TreeNode, val int) *TreeNode {\n if node == nil {\n return nil\n }\n /* 1. \u67e5\u627e\u8282\u70b9\uff0c\u5e76\u5220\u9664\u4e4b */\n if val < node.Val.(int) {\n node.Left = t.removeHelper(node.Left, val)\n } else if val > node.Val.(int) {\n node.Right = t.removeHelper(node.Right, val)\n } else {\n if node.Left == nil || node.Right == nil {\n child := node.Left\n if node.Right != nil {\n child = node.Right\n }\n if child == nil {\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\n return nil\n } else {\n // \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\n node = child\n }\n } else {\n // \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\n temp := node.Right\n for temp.Left != nil {\n temp = temp.Left\n }\n node.Right = t.removeHelper(node.Right, temp.Val.(int))\n node.Val = temp.Val\n }\n }\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n t.updateHeight(node)\n /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n node = t.rotate(node)\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n return node\n}\n
avl_tree.swift/* \u5220\u9664\u8282\u70b9 */\nfunc remove(val: Int) {\n root = removeHelper(node: root, val: val)\n}\n\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nfunc removeHelper(node: TreeNode?, val: Int) -> TreeNode? {\n var node = node\n if node == nil {\n return nil\n }\n /* 1. \u67e5\u627e\u8282\u70b9\uff0c\u5e76\u5220\u9664\u4e4b */\n if val < node!.val {\n node?.left = removeHelper(node: node?.left, val: val)\n } else if val > node!.val {\n node?.right = removeHelper(node: node?.right, val: val)\n } else {\n if node?.left == nil || node?.right == nil {\n let child = node?.left != nil ? node?.left : node?.right\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\n if child == nil {\n return nil\n }\n // \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\n else {\n node = child\n }\n } else {\n // \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\n var temp = node?.right\n while temp?.left != nil {\n temp = temp?.left\n }\n node?.right = removeHelper(node: node?.right, val: temp!.val)\n node?.val = temp!.val\n }\n }\n updateHeight(node: node) // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n node = rotate(node: node)\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n return node\n}\n
avl_tree.js/* \u5220\u9664\u8282\u70b9 */\nremove(val) {\n this.root = this.#removeHelper(this.root, val);\n}\n\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\n#removeHelper(node, val) {\n if (node === null) return null;\n /* 1. \u67e5\u627e\u8282\u70b9\uff0c\u5e76\u5220\u9664\u4e4b */\n if (val < node.val) node.left = this.#removeHelper(node.left, val);\n else if (val > node.val)\n node.right = this.#removeHelper(node.right, val);\n else {\n if (node.left === null || node.right === null) {\n const child = node.left !== null ? node.left : node.right;\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\n if (child === null) return null;\n // \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\n else node = child;\n } else {\n // \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\n let temp = node.right;\n while (temp.left !== null) {\n temp = temp.left;\n }\n node.right = this.#removeHelper(node.right, temp.val);\n node.val = temp.val;\n }\n }\n this.#updateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n node = this.#rotate(node);\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n return node;\n}\n
avl_tree.ts/* \u5220\u9664\u8282\u70b9 */\nremove(val: number): void {\n this.root = this.removeHelper(this.root, val);\n}\n\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nremoveHelper(node: TreeNode, val: number): TreeNode {\n if (node === null) return null;\n /* 1. \u67e5\u627e\u8282\u70b9\uff0c\u5e76\u5220\u9664\u4e4b */\n if (val < node.val) {\n node.left = this.removeHelper(node.left, val);\n } else if (val > node.val) {\n node.right = this.removeHelper(node.right, val);\n } else {\n if (node.left === null || node.right === null) {\n const child = node.left !== null ? node.left : node.right;\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\n if (child === null) {\n return null;\n } else {\n // \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\n node = child;\n }\n } else {\n // \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\n let temp = node.right;\n while (temp.left !== null) {\n temp = temp.left;\n }\n node.right = this.removeHelper(node.right, temp.val);\n node.val = temp.val;\n }\n }\n this.updateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n node = this.rotate(node);\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n return node;\n}\n
avl_tree.dart/* \u5220\u9664\u8282\u70b9 */\nvoid remove(int val) {\n root = removeHelper(root, val);\n}\n\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nTreeNode? removeHelper(TreeNode? node, int val) {\n if (node == null) return null;\n /* 1. \u67e5\u627e\u8282\u70b9\uff0c\u5e76\u5220\u9664\u4e4b */\n if (val < node.val)\n node.left = removeHelper(node.left, val);\n else if (val > node.val)\n node.right = removeHelper(node.right, val);\n else {\n if (node.left == null || node.right == null) {\n TreeNode? child = node.left ?? node.right;\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\n if (child == null)\n return null;\n // \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\n else\n node = child;\n } else {\n // \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\n TreeNode? temp = node.right;\n while (temp!.left != null) {\n temp = temp.left;\n }\n node.right = removeHelper(node.right, temp.val);\n node.val = temp.val;\n }\n }\n updateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n node = rotate(node);\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n return node;\n}\n
avl_tree.rs/* \u5220\u9664\u8282\u70b9 */\nfn remove(&self, val: i32) {\n Self::remove_helper(self.root.clone(), val);\n}\n\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nfn remove_helper(node: OptionTreeNodeRc, val: i32) -> OptionTreeNodeRc {\n match node {\n Some(mut node) => {\n /* 1. \u67e5\u627e\u8282\u70b9\uff0c\u5e76\u5220\u9664\u4e4b */\n if val < node.borrow().val {\n let left = node.borrow().left.clone();\n node.borrow_mut().left = Self::remove_helper(left, val);\n } else if val > node.borrow().val {\n let right = node.borrow().right.clone();\n node.borrow_mut().right = Self::remove_helper(right, val);\n } else if node.borrow().left.is_none() || node.borrow().right.is_none() {\n let child = if node.borrow().left.is_some() {\n node.borrow().left.clone()\n } else {\n node.borrow().right.clone()\n };\n match child {\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\n None => {\n return None;\n }\n // \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\n Some(child) => node = child,\n }\n } else {\n // \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\n let mut temp = node.borrow().right.clone().unwrap();\n loop {\n let temp_left = temp.borrow().left.clone();\n if temp_left.is_none() {\n break;\n }\n temp = temp_left.unwrap();\n }\n let right = node.borrow().right.clone();\n node.borrow_mut().right = Self::remove_helper(right, temp.borrow().val);\n node.borrow_mut().val = temp.borrow().val;\n }\n Self::update_height(Some(node.clone())); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n node = Self::rotate(Some(node)).unwrap();\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n Some(node)\n }\n None => None,\n }\n}\n
avl_tree.c/* \u5220\u9664\u8282\u70b9 */\n// \u7531\u4e8e\u5f15\u5165\u4e86 stdio.h \uff0c\u6b64\u5904\u65e0\u6cd5\u4f7f\u7528 remove \u5173\u952e\u8bcd\nvoid removeItem(AVLTree *tree, int val) {\n TreeNode *root = removeHelper(tree->root, val);\n}\n\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u51fd\u6570\uff09 */\nTreeNode *removeHelper(TreeNode *node, int val) {\n TreeNode *child, *grandChild;\n if (node == NULL) {\n return NULL;\n }\n /* 1. \u67e5\u627e\u8282\u70b9\uff0c\u5e76\u5220\u9664\u4e4b */\n if (val < node->val) {\n node->left = removeHelper(node->left, val);\n } else if (val > node->val) {\n node->right = removeHelper(node->right, val);\n } else {\n if (node->left == NULL || node->right == NULL) {\n child = node->left;\n if (node->right != NULL) {\n child = node->right;\n }\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\n if (child == NULL) {\n return NULL;\n } else {\n // \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\n node = child;\n }\n } else {\n // \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\n TreeNode *temp = node->right;\n while (temp->left != NULL) {\n temp = temp->left;\n }\n int tempVal = temp->val;\n node->right = removeHelper(node->right, temp->val);\n node->val = tempVal;\n }\n }\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n updateHeight(node);\n /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n node = rotate(node);\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n return node;\n}\n
avl_tree.zig// \u5220\u9664\u8282\u70b9\nfn remove(self: *Self, val: T) void {\n self.root = self.removeHelper(self.root, val).?;\n}\n\n// \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09\nfn removeHelper(self: *Self, node_: ?*inc.TreeNode(T), val: T) ?*inc.TreeNode(T) {\n var node = node_;\n if (node == null) return null;\n // 1. \u67e5\u627e\u8282\u70b9\uff0c\u5e76\u5220\u9664\u4e4b\n if (val < node.?.val) {\n node.?.left = self.removeHelper(node.?.left, val);\n } else if (val > node.?.val) {\n node.?.right = self.removeHelper(node.?.right, val);\n } else {\n if (node.?.left == null or node.?.right == null) {\n var child = if (node.?.left != null) node.?.left else node.?.right;\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\n if (child == null) {\n return null;\n // \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\n } else {\n node = child;\n }\n } else {\n // \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\n var temp = node.?.right;\n while (temp.?.left != null) {\n temp = temp.?.left;\n }\n node.?.right = self.removeHelper(node.?.right, temp.?.val);\n node.?.val = temp.?.val;\n }\n }\n self.updateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n // 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861\n node = self.rotate(node);\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n return node;\n}\n
"},{"location":"chapter_tree/avl_tree/#3_1","title":"3. \u00a0 \u67e5\u627e\u8282\u70b9","text":"AVL \u6811\u7684\u8282\u70b9\u67e5\u627e\u64cd\u4f5c\u4e0e\u4e8c\u53c9\u641c\u7d22\u6811\u4e00\u81f4\uff0c\u5728\u6b64\u4e0d\u518d\u8d58\u8ff0\u3002
"},{"location":"chapter_tree/avl_tree/#754-avl","title":"7.5.4 \u00a0 AVL \u6811\u5178\u578b\u5e94\u7528","text":"\u5982\u56fe 7-16 \u6240\u793a\uff0c\u300c\u4e8c\u53c9\u641c\u7d22\u6811 binary search tree\u300d\u6ee1\u8db3\u4ee5\u4e0b\u6761\u4ef6\u3002
1.
\u3002\u56fe 7-16 \u00a0 \u4e8c\u53c9\u641c\u7d22\u6811
"},{"location":"chapter_tree/binary_search_tree/#741","title":"7.4.1 \u00a0 \u4e8c\u53c9\u641c\u7d22\u6811\u7684\u64cd\u4f5c","text":"\u6211\u4eec\u5c06\u4e8c\u53c9\u641c\u7d22\u6811\u5c01\u88c5\u4e3a\u4e00\u4e2a\u7c7b BinarySearchTree
\uff0c\u5e76\u58f0\u660e\u4e00\u4e2a\u6210\u5458\u53d8\u91cf root
\uff0c\u6307\u5411\u6811\u7684\u6839\u8282\u70b9\u3002
\u7ed9\u5b9a\u76ee\u6807\u8282\u70b9\u503c num
\uff0c\u53ef\u4ee5\u6839\u636e\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u6027\u8d28\u6765\u67e5\u627e\u3002\u5982\u56fe 7-17 \u6240\u793a\uff0c\u6211\u4eec\u58f0\u660e\u4e00\u4e2a\u8282\u70b9 cur
\uff0c\u4ece\u4e8c\u53c9\u6811\u7684\u6839\u8282\u70b9 root
\u51fa\u53d1\uff0c\u5faa\u73af\u6bd4\u8f83\u8282\u70b9\u503c cur.val
\u548c num
\u4e4b\u95f4\u7684\u5927\u5c0f\u5173\u7cfb\u3002
cur.val < num
\uff0c\u8bf4\u660e\u76ee\u6807\u8282\u70b9\u5728 cur
\u7684\u53f3\u5b50\u6811\u4e2d\uff0c\u56e0\u6b64\u6267\u884c cur = cur.right
\u3002cur.val > num
\uff0c\u8bf4\u660e\u76ee\u6807\u8282\u70b9\u5728 cur
\u7684\u5de6\u5b50\u6811\u4e2d\uff0c\u56e0\u6b64\u6267\u884c cur = cur.left
\u3002cur.val = num
\uff0c\u8bf4\u660e\u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\u5e76\u8fd4\u56de\u8be5\u8282\u70b9\u3002\u56fe 7-17 \u00a0 \u4e8c\u53c9\u641c\u7d22\u6811\u67e5\u627e\u8282\u70b9\u793a\u4f8b
\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u67e5\u627e\u64cd\u4f5c\u4e0e\u4e8c\u5206\u67e5\u627e\u7b97\u6cd5\u7684\u5de5\u4f5c\u539f\u7406\u4e00\u81f4\uff0c\u90fd\u662f\u6bcf\u8f6e\u6392\u9664\u4e00\u534a\u60c5\u51b5\u3002\u5faa\u73af\u6b21\u6570\u6700\u591a\u4e3a\u4e8c\u53c9\u6811\u7684\u9ad8\u5ea6\uff0c\u5f53\u4e8c\u53c9\u6811\u5e73\u8861\u65f6\uff0c\u4f7f\u7528 \\(O(\\log n)\\) \u65f6\u95f4\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig binary_search_tree.pydef search(self, num: int) -> TreeNode | None:\n \"\"\"\u67e5\u627e\u8282\u70b9\"\"\"\n cur = self._root\n # \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while cur is not None:\n # \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if cur.val < num:\n cur = cur.right\n # \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n elif cur.val > num:\n cur = cur.left\n # \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n else:\n break\n return cur\n
binary_search_tree.cpp/* \u67e5\u627e\u8282\u70b9 */\nTreeNode *search(int num) {\n TreeNode *cur = root;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur != nullptr) {\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur->val < num)\n cur = cur->right;\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else if (cur->val > num)\n cur = cur->left;\n // \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n else\n break;\n }\n // \u8fd4\u56de\u76ee\u6807\u8282\u70b9\n return cur;\n}\n
binary_search_tree.java/* \u67e5\u627e\u8282\u70b9 */\nTreeNode search(int num) {\n TreeNode cur = root;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur != null) {\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur.val < num)\n cur = cur.right;\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else if (cur.val > num)\n cur = cur.left;\n // \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n else\n break;\n }\n // \u8fd4\u56de\u76ee\u6807\u8282\u70b9\n return cur;\n}\n
binary_search_tree.cs/* \u67e5\u627e\u8282\u70b9 */\nTreeNode? Search(int num) {\n TreeNode? cur = root;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur != null) {\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur.val < num) cur =\n cur.right;\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else if (cur.val > num)\n cur = cur.left;\n // \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n else\n break;\n }\n // \u8fd4\u56de\u76ee\u6807\u8282\u70b9\n return cur;\n}\n
binary_search_tree.go/* \u67e5\u627e\u8282\u70b9 */\nfunc (bst *binarySearchTree) search(num int) *TreeNode {\n node := bst.root\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n for node != nil {\n if node.Val.(int) < num {\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n node = node.Right\n } else if node.Val.(int) > num {\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n node = node.Left\n } else {\n // \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n break\n }\n }\n // \u8fd4\u56de\u76ee\u6807\u8282\u70b9\n return node\n}\n
binary_search_tree.swift/* \u67e5\u627e\u8282\u70b9 */\nfunc search(num: Int) -> TreeNode? {\n var cur = root\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while cur != nil {\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if cur!.val < num {\n cur = cur?.right\n }\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else if cur!.val > num {\n cur = cur?.left\n }\n // \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n else {\n break\n }\n }\n // \u8fd4\u56de\u76ee\u6807\u8282\u70b9\n return cur\n}\n
binary_search_tree.js/* \u67e5\u627e\u8282\u70b9 */\nsearch(num) {\n let cur = this.root;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur !== null) {\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur.val < num) cur = cur.right;\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else if (cur.val > num) cur = cur.left;\n // \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n else break;\n }\n // \u8fd4\u56de\u76ee\u6807\u8282\u70b9\n return cur;\n}\n
binary_search_tree.ts/* \u67e5\u627e\u8282\u70b9 */\nsearch(num: number): TreeNode | null {\n let cur = this.root;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur !== null) {\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur.val < num) cur = cur.right;\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else if (cur.val > num) cur = cur.left;\n // \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n else break;\n }\n // \u8fd4\u56de\u76ee\u6807\u8282\u70b9\n return cur;\n}\n
binary_search_tree.dart/* \u67e5\u627e\u8282\u70b9 */\nTreeNode? search(int _num) {\n TreeNode? cur = _root;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur != null) {\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur.val < _num)\n cur = cur.right;\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else if (cur.val > _num)\n cur = cur.left;\n // \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n else\n break;\n }\n // \u8fd4\u56de\u76ee\u6807\u8282\u70b9\n return cur;\n}\n
binary_search_tree.rs/* \u67e5\u627e\u8282\u70b9 */\npub fn search(&self, num: i32) -> Option<TreeNodeRc> {\n let mut cur = self.root.clone();\n\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while let Some(node) = cur.clone() {\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if node.borrow().val < num {\n cur = node.borrow().right.clone();\n }\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else if node.borrow().val > num {\n cur = node.borrow().left.clone();\n }\n // \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n else {\n break;\n }\n }\n // \u8fd4\u56de\u76ee\u6807\u8282\u70b9\n cur\n}\n
binary_search_tree.c/* \u67e5\u627e\u8282\u70b9 */\nTreeNode *search(BinarySearchTree *bst, int num) {\n TreeNode *cur = bst->root;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur != NULL) {\n if (cur->val < num) {\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n cur = cur->right;\n } else if (cur->val > num) {\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n cur = cur->left;\n } else {\n // \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n break;\n }\n }\n // \u8fd4\u56de\u76ee\u6807\u8282\u70b9\n return cur;\n}\n
binary_search_tree.zig// \u67e5\u627e\u8282\u70b9\nfn search(self: *Self, num: T) ?*inc.TreeNode(T) {\n var cur = self.root;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur != null) {\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur.?.val < num) {\n cur = cur.?.right;\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n } else if (cur.?.val > num) {\n cur = cur.?.left;\n // \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n } else {\n break;\n }\n }\n // \u8fd4\u56de\u76ee\u6807\u8282\u70b9\n return cur;\n}\n
"},{"location":"chapter_tree/binary_search_tree/#2","title":"2. \u00a0 \u63d2\u5165\u8282\u70b9","text":"\u7ed9\u5b9a\u4e00\u4e2a\u5f85\u63d2\u5165\u5143\u7d20 num
\uff0c\u4e3a\u4e86\u4fdd\u6301\u4e8c\u53c9\u641c\u7d22\u6811\u201c\u5de6\u5b50\u6811 < \u6839\u8282\u70b9 < \u53f3\u5b50\u6811\u201d\u7684\u6027\u8d28\uff0c\u63d2\u5165\u64cd\u4f5c\u6d41\u7a0b\u5982\u56fe 7-18 \u6240\u793a\u3002
num
\u7684\u5927\u5c0f\u5173\u7cfb\u5faa\u73af\u5411\u4e0b\u641c\u7d22\uff0c\u76f4\u5230\u8d8a\u8fc7\u53f6\u8282\u70b9\uff08\u904d\u5386\u81f3 \\(\\text{None}\\) \uff09\u65f6\u8df3\u51fa\u5faa\u73af\u3002num
\uff0c\u5c06\u8be5\u8282\u70b9\u7f6e\u4e8e \\(\\text{None}\\) \u7684\u4f4d\u7f6e\u3002\u56fe 7-18 \u00a0 \u5728\u4e8c\u53c9\u641c\u7d22\u6811\u4e2d\u63d2\u5165\u8282\u70b9
\u5728\u4ee3\u7801\u5b9e\u73b0\u4e2d\uff0c\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u4e24\u70b9\u3002
pre
\u4fdd\u5b58\u4e0a\u4e00\u8f6e\u5faa\u73af\u7684\u8282\u70b9\u3002\u8fd9\u6837\u5728\u904d\u5386\u81f3 \\(\\text{None}\\) \u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u83b7\u53d6\u5230\u5176\u7236\u8282\u70b9\uff0c\u4ece\u800c\u5b8c\u6210\u8282\u70b9\u63d2\u5165\u64cd\u4f5c\u3002def insert(self, num: int):\n \"\"\"\u63d2\u5165\u8282\u70b9\"\"\"\n # \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\n if self._root is None:\n self._root = TreeNode(num)\n return\n # \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n cur, pre = self._root, None\n while cur is not None:\n # \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\n if cur.val == num:\n return\n pre = cur\n # \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if cur.val < num:\n cur = cur.right\n # \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else:\n cur = cur.left\n # \u63d2\u5165\u8282\u70b9\n node = TreeNode(num)\n if pre.val < num:\n pre.right = node\n else:\n pre.left = node\n
binary_search_tree.cpp/* \u63d2\u5165\u8282\u70b9 */\nvoid insert(int num) {\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\n if (root == nullptr) {\n root = new TreeNode(num);\n return;\n }\n TreeNode *cur = root, *pre = nullptr;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur != nullptr) {\n // \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\n if (cur->val == num)\n return;\n pre = cur;\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur->val < num)\n cur = cur->right;\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else\n cur = cur->left;\n }\n // \u63d2\u5165\u8282\u70b9\n TreeNode *node = new TreeNode(num);\n if (pre->val < num)\n pre->right = node;\n else\n pre->left = node;\n}\n
binary_search_tree.java/* \u63d2\u5165\u8282\u70b9 */\nvoid insert(int num) {\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\n if (root == null) {\n root = new TreeNode(num);\n return;\n }\n TreeNode cur = root, pre = null;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur != null) {\n // \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\n if (cur.val == num)\n return;\n pre = cur;\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur.val < num)\n cur = cur.right;\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else\n cur = cur.left;\n }\n // \u63d2\u5165\u8282\u70b9\n TreeNode node = new TreeNode(num);\n if (pre.val < num)\n pre.right = node;\n else\n pre.left = node;\n}\n
binary_search_tree.cs/* \u63d2\u5165\u8282\u70b9 */\nvoid Insert(int num) {\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\n if (root == null) {\n root = new TreeNode(num);\n return;\n }\n TreeNode? cur = root, pre = null;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur != null) {\n // \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\n if (cur.val == num)\n return;\n pre = cur;\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur.val < num)\n cur = cur.right;\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else\n cur = cur.left;\n }\n\n // \u63d2\u5165\u8282\u70b9\n TreeNode node = new(num);\n if (pre != null) {\n if (pre.val < num)\n pre.right = node;\n else\n pre.left = node;\n }\n}\n
binary_search_tree.go/* \u63d2\u5165\u8282\u70b9 */\nfunc (bst *binarySearchTree) insert(num int) {\n cur := bst.root\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\n if cur == nil {\n bst.root = NewTreeNode(num)\n return\n }\n // \u5f85\u63d2\u5165\u8282\u70b9\u4e4b\u524d\u7684\u8282\u70b9\u4f4d\u7f6e\n var pre *TreeNode = nil\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n for cur != nil {\n if cur.Val == num {\n return\n }\n pre = cur\n if cur.Val.(int) < num {\n cur = cur.Right\n } else {\n cur = cur.Left\n }\n }\n // \u63d2\u5165\u8282\u70b9\n node := NewTreeNode(num)\n if pre.Val.(int) < num {\n pre.Right = node\n } else {\n pre.Left = node\n }\n}\n
binary_search_tree.swift/* \u63d2\u5165\u8282\u70b9 */\nfunc insert(num: Int) {\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\n if root == nil {\n root = TreeNode(x: num)\n return\n }\n var cur = root\n var pre: TreeNode?\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while cur != nil {\n // \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\n if cur!.val == num {\n return\n }\n pre = cur\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if cur!.val < num {\n cur = cur?.right\n }\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else {\n cur = cur?.left\n }\n }\n // \u63d2\u5165\u8282\u70b9\n let node = TreeNode(x: num)\n if pre!.val < num {\n pre?.right = node\n } else {\n pre?.left = node\n }\n}\n
binary_search_tree.js/* \u63d2\u5165\u8282\u70b9 */\ninsert(num) {\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\n if (this.root === null) {\n this.root = new TreeNode(num);\n return;\n }\n let cur = this.root,\n pre = null;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur !== null) {\n // \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\n if (cur.val === num) return;\n pre = cur;\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur.val < num) cur = cur.right;\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else cur = cur.left;\n }\n // \u63d2\u5165\u8282\u70b9\n const node = new TreeNode(num);\n if (pre.val < num) pre.right = node;\n else pre.left = node;\n}\n
binary_search_tree.ts/* \u63d2\u5165\u8282\u70b9 */\ninsert(num: number): void {\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\n if (this.root === null) {\n this.root = new TreeNode(num);\n return;\n }\n let cur: TreeNode | null = this.root,\n pre: TreeNode | null = null;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur !== null) {\n // \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\n if (cur.val === num) return;\n pre = cur;\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur.val < num) cur = cur.right;\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else cur = cur.left;\n }\n // \u63d2\u5165\u8282\u70b9\n const node = new TreeNode(num);\n if (pre!.val < num) pre!.right = node;\n else pre!.left = node;\n}\n
binary_search_tree.dart/* \u63d2\u5165\u8282\u70b9 */\nvoid insert(int _num) {\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\n if (_root == null) {\n _root = TreeNode(_num);\n return;\n }\n TreeNode? cur = _root;\n TreeNode? pre = null;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur != null) {\n // \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\n if (cur.val == _num) return;\n pre = cur;\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur.val < _num)\n cur = cur.right;\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else\n cur = cur.left;\n }\n // \u63d2\u5165\u8282\u70b9\n TreeNode? node = TreeNode(_num);\n if (pre!.val < _num)\n pre.right = node;\n else\n pre.left = node;\n}\n
binary_search_tree.rs/* \u63d2\u5165\u8282\u70b9 */\npub fn insert(&mut self, num: i32) {\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\n if self.root.is_none() {\n self.root = Some(TreeNode::new(num));\n return;\n }\n let mut cur = self.root.clone();\n let mut pre = None;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while let Some(node) = cur.clone() {\n // \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\n if node.borrow().val == num {\n return;\n }\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n pre = cur.clone();\n if node.borrow().val < num {\n cur = node.borrow().right.clone();\n }\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else {\n cur = node.borrow().left.clone();\n }\n }\n // \u63d2\u5165\u8282\u70b9\n let node = TreeNode::new(num);\n let pre = pre.unwrap();\n if pre.borrow().val < num {\n pre.borrow_mut().right = Some(Rc::clone(&node));\n } else {\n pre.borrow_mut().left = Some(Rc::clone(&node));\n }\n}\n
binary_search_tree.c/* \u63d2\u5165\u8282\u70b9 */\nvoid insert(BinarySearchTree *bst, int num) {\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\n if (bst->root == NULL) {\n bst->root = newTreeNode(num);\n return;\n }\n TreeNode *cur = bst->root, *pre = NULL;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur != NULL) {\n // \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\n if (cur->val == num) {\n return;\n }\n pre = cur;\n if (cur->val < num) {\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n cur = cur->right;\n } else {\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n cur = cur->left;\n }\n }\n // \u63d2\u5165\u8282\u70b9\n TreeNode *node = newTreeNode(num);\n if (pre->val < num) {\n pre->right = node;\n } else {\n pre->left = node;\n }\n}\n
binary_search_tree.zig// \u63d2\u5165\u8282\u70b9\nfn insert(self: *Self, num: T) !void {\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\n if (self.root == null) {\n self.root = try self.mem_allocator.create(inc.TreeNode(T));\n return;\n }\n var cur = self.root;\n var pre: ?*inc.TreeNode(T) = null;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur != null) {\n // \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\n if (cur.?.val == num) return;\n pre = cur;\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur.?.val < num) {\n cur = cur.?.right;\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n } else {\n cur = cur.?.left;\n }\n }\n // \u63d2\u5165\u8282\u70b9\n var node = try self.mem_allocator.create(inc.TreeNode(T));\n node.init(num);\n if (pre.?.val < num) {\n pre.?.right = node;\n } else {\n pre.?.left = node;\n }\n}\n
\u4e0e\u67e5\u627e\u8282\u70b9\u76f8\u540c\uff0c\u63d2\u5165\u8282\u70b9\u4f7f\u7528 \\(O(\\log n)\\) \u65f6\u95f4\u3002
"},{"location":"chapter_tree/binary_search_tree/#3","title":"3. \u00a0 \u5220\u9664\u8282\u70b9","text":"\u5148\u5728\u4e8c\u53c9\u6811\u4e2d\u67e5\u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u518d\u5c06\u5176\u5220\u9664\u3002
\u4e0e\u63d2\u5165\u8282\u70b9\u7c7b\u4f3c\uff0c\u6211\u4eec\u9700\u8981\u4fdd\u8bc1\u5728\u5220\u9664\u64cd\u4f5c\u5b8c\u6210\u540e\uff0c\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u201c\u5de6\u5b50\u6811 < \u6839\u8282\u70b9 < \u53f3\u5b50\u6811\u201d\u7684\u6027\u8d28\u4ecd\u7136\u6ee1\u8db3\u3002
\u56e0\u6b64\uff0c\u6211\u4eec\u6839\u636e\u76ee\u6807\u8282\u70b9\u7684\u5b50\u8282\u70b9\u6570\u91cf\uff0c\u5206 0\u30011 \u548c 2 \u4e09\u79cd\u60c5\u51b5\uff0c\u6267\u884c\u5bf9\u5e94\u7684\u5220\u9664\u8282\u70b9\u64cd\u4f5c\u3002
\u5982\u56fe 7-19 \u6240\u793a\uff0c\u5f53\u5f85\u5220\u9664\u8282\u70b9\u7684\u5ea6\u4e3a \\(0\\) \u65f6\uff0c\u8868\u793a\u8be5\u8282\u70b9\u662f\u53f6\u8282\u70b9\uff0c\u53ef\u4ee5\u76f4\u63a5\u5220\u9664\u3002
\u56fe 7-19 \u00a0 \u5728\u4e8c\u53c9\u641c\u7d22\u6811\u4e2d\u5220\u9664\u8282\u70b9\uff08\u5ea6\u4e3a 0 \uff09
\u5982\u56fe 7-20 \u6240\u793a\uff0c\u5f53\u5f85\u5220\u9664\u8282\u70b9\u7684\u5ea6\u4e3a \\(1\\) \u65f6\uff0c\u5c06\u5f85\u5220\u9664\u8282\u70b9\u66ff\u6362\u4e3a\u5176\u5b50\u8282\u70b9\u5373\u53ef\u3002
\u56fe 7-20 \u00a0 \u5728\u4e8c\u53c9\u641c\u7d22\u6811\u4e2d\u5220\u9664\u8282\u70b9\uff08\u5ea6\u4e3a 1 \uff09
\u5f53\u5f85\u5220\u9664\u8282\u70b9\u7684\u5ea6\u4e3a \\(2\\) \u65f6\uff0c\u6211\u4eec\u65e0\u6cd5\u76f4\u63a5\u5220\u9664\u5b83\uff0c\u800c\u9700\u8981\u4f7f\u7528\u4e00\u4e2a\u8282\u70b9\u66ff\u6362\u8be5\u8282\u70b9\u3002\u7531\u4e8e\u8981\u4fdd\u6301\u4e8c\u53c9\u641c\u7d22\u6811\u201c\u5de6\u5b50\u6811 \\(<\\) \u6839\u8282\u70b9 \\(<\\) \u53f3\u5b50\u6811\u201d\u7684\u6027\u8d28\uff0c\u56e0\u6b64\u8fd9\u4e2a\u8282\u70b9\u53ef\u4ee5\u662f\u53f3\u5b50\u6811\u7684\u6700\u5c0f\u8282\u70b9\u6216\u5de6\u5b50\u6811\u7684\u6700\u5927\u8282\u70b9\u3002
\u5047\u8bbe\u6211\u4eec\u9009\u62e9\u53f3\u5b50\u6811\u7684\u6700\u5c0f\u8282\u70b9\uff08\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\uff09\uff0c\u5219\u5220\u9664\u64cd\u4f5c\u6d41\u7a0b\u5982\u56fe 7-21 \u6240\u793a\u3002
tmp
\u3002tmp
\u7684\u503c\u8986\u76d6\u5f85\u5220\u9664\u8282\u70b9\u7684\u503c\uff0c\u5e76\u5728\u6811\u4e2d\u9012\u5f52\u5220\u9664\u8282\u70b9 tmp
\u3002\u56fe 7-21 \u00a0 \u5728\u4e8c\u53c9\u641c\u7d22\u6811\u4e2d\u5220\u9664\u8282\u70b9\uff08\u5ea6\u4e3a 2 \uff09
\u5220\u9664\u8282\u70b9\u64cd\u4f5c\u540c\u6837\u4f7f\u7528 \\(O(\\log n)\\) \u65f6\u95f4\uff0c\u5176\u4e2d\u67e5\u627e\u5f85\u5220\u9664\u8282\u70b9\u9700\u8981 \\(O(\\log n)\\) \u65f6\u95f4\uff0c\u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u540e\u7ee7\u8282\u70b9\u9700\u8981 \\(O(\\log n)\\) \u65f6\u95f4\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig binary_search_tree.pydef remove(self, num: int):\n \"\"\"\u5220\u9664\u8282\u70b9\"\"\"\n # \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n if self._root is None:\n return\n # \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n cur, pre = self._root, None\n while cur is not None:\n # \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n if cur.val == num:\n break\n pre = cur\n # \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if cur.val < num:\n cur = cur.right\n # \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else:\n cur = cur.left\n # \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if cur is None:\n return\n\n # \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\n if cur.left is None or cur.right is None:\n # \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = null / \u8be5\u5b50\u8282\u70b9\n child = cur.left or cur.right\n # \u5220\u9664\u8282\u70b9 cur\n if cur != self._root:\n if pre.left == cur:\n pre.left = child\n else:\n pre.right = child\n else:\n # \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\n self._root = child\n # \u5b50\u8282\u70b9\u6570\u91cf = 2\n else:\n # \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\n tmp: TreeNode = cur.right\n while tmp.left is not None:\n tmp = tmp.left\n # \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\n self.remove(tmp.val)\n # \u7528 tmp \u8986\u76d6 cur\n cur.val = tmp.val\n
binary_search_tree.cpp/* \u5220\u9664\u8282\u70b9 */\nvoid remove(int num) {\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n if (root == nullptr)\n return;\n TreeNode *cur = root, *pre = nullptr;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur != nullptr) {\n // \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n if (cur->val == num)\n break;\n pre = cur;\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur->val < num)\n cur = cur->right;\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else\n cur = cur->left;\n }\n // \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if (cur == nullptr)\n return;\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\n if (cur->left == nullptr || cur->right == nullptr) {\n // \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = nullptr / \u8be5\u5b50\u8282\u70b9\n TreeNode *child = cur->left != nullptr ? cur->left : cur->right;\n // \u5220\u9664\u8282\u70b9 cur\n if (cur != root) {\n if (pre->left == cur)\n pre->left = child;\n else\n pre->right = child;\n } else {\n // \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\n root = child;\n }\n // \u91ca\u653e\u5185\u5b58\n delete cur;\n }\n // \u5b50\u8282\u70b9\u6570\u91cf = 2\n else {\n // \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\n TreeNode *tmp = cur->right;\n while (tmp->left != nullptr) {\n tmp = tmp->left;\n }\n int tmpVal = tmp->val;\n // \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\n remove(tmp->val);\n // \u7528 tmp \u8986\u76d6 cur\n cur->val = tmpVal;\n }\n}\n
binary_search_tree.java/* \u5220\u9664\u8282\u70b9 */\nvoid remove(int num) {\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n if (root == null)\n return;\n TreeNode cur = root, pre = null;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur != null) {\n // \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n if (cur.val == num)\n break;\n pre = cur;\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur.val < num)\n cur = cur.right;\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else\n cur = cur.left;\n }\n // \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if (cur == null)\n return;\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\n if (cur.left == null || cur.right == null) {\n // \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = null / \u8be5\u5b50\u8282\u70b9\n TreeNode child = cur.left != null ? cur.left : cur.right;\n // \u5220\u9664\u8282\u70b9 cur\n if (cur != root) {\n if (pre.left == cur)\n pre.left = child;\n else\n pre.right = child;\n } else {\n // \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\n root = child;\n }\n }\n // \u5b50\u8282\u70b9\u6570\u91cf = 2\n else {\n // \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\n TreeNode tmp = cur.right;\n while (tmp.left != null) {\n tmp = tmp.left;\n }\n // \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\n remove(tmp.val);\n // \u7528 tmp \u8986\u76d6 cur\n cur.val = tmp.val;\n }\n}\n
binary_search_tree.cs/* \u5220\u9664\u8282\u70b9 */\nvoid Remove(int num) {\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n if (root == null)\n return;\n TreeNode? cur = root, pre = null;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur != null) {\n // \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n if (cur.val == num)\n break;\n pre = cur;\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur.val < num)\n cur = cur.right;\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else\n cur = cur.left;\n }\n // \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if (cur == null)\n return;\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\n if (cur.left == null || cur.right == null) {\n // \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = null / \u8be5\u5b50\u8282\u70b9\n TreeNode? child = cur.left ?? cur.right;\n // \u5220\u9664\u8282\u70b9 cur\n if (cur != root) {\n if (pre!.left == cur)\n pre.left = child;\n else\n pre.right = child;\n } else {\n // \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\n root = child;\n }\n }\n // \u5b50\u8282\u70b9\u6570\u91cf = 2\n else {\n // \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\n TreeNode? tmp = cur.right;\n while (tmp.left != null) {\n tmp = tmp.left;\n }\n // \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\n Remove(tmp.val!.Value);\n // \u7528 tmp \u8986\u76d6 cur\n cur.val = tmp.val;\n }\n}\n
binary_search_tree.go/* \u5220\u9664\u8282\u70b9 */\nfunc (bst *binarySearchTree) remove(num int) {\n cur := bst.root\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n if cur == nil {\n return\n }\n // \u5f85\u5220\u9664\u8282\u70b9\u4e4b\u524d\u7684\u8282\u70b9\u4f4d\u7f6e\n var pre *TreeNode = nil\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n for cur != nil {\n if cur.Val == num {\n break\n }\n pre = cur\n if cur.Val.(int) < num {\n // \u5f85\u5220\u9664\u8282\u70b9\u5728\u53f3\u5b50\u6811\u4e2d\n cur = cur.Right\n } else {\n // \u5f85\u5220\u9664\u8282\u70b9\u5728\u5de6\u5b50\u6811\u4e2d\n cur = cur.Left\n }\n }\n // \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if cur == nil {\n return\n }\n // \u5b50\u8282\u70b9\u6570\u4e3a 0 \u6216 1\n if cur.Left == nil || cur.Right == nil {\n var child *TreeNode = nil\n // \u53d6\u51fa\u5f85\u5220\u9664\u8282\u70b9\u7684\u5b50\u8282\u70b9\n if cur.Left != nil {\n child = cur.Left\n } else {\n child = cur.Right\n }\n // \u5220\u9664\u8282\u70b9 cur\n if cur != bst.root {\n if pre.Left == cur {\n pre.Left = child\n } else {\n pre.Right = child\n }\n } else {\n // \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\n bst.root = child\n }\n // \u5b50\u8282\u70b9\u6570\u4e3a 2\n } else {\n // \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d\u5f85\u5220\u9664\u8282\u70b9 cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\n tmp := cur.Right\n for tmp.Left != nil {\n tmp = tmp.Left\n }\n // \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\n bst.remove(tmp.Val.(int))\n // \u7528 tmp \u8986\u76d6 cur\n cur.Val = tmp.Val\n }\n}\n
binary_search_tree.swift/* \u5220\u9664\u8282\u70b9 */\nfunc remove(num: Int) {\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n if root == nil {\n return\n }\n var cur = root\n var pre: TreeNode?\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while cur != nil {\n // \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n if cur!.val == num {\n break\n }\n pre = cur\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if cur!.val < num {\n cur = cur?.right\n }\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else {\n cur = cur?.left\n }\n }\n // \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if cur == nil {\n return\n }\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\n if cur?.left == nil || cur?.right == nil {\n // \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = null / \u8be5\u5b50\u8282\u70b9\n let child = cur?.left != nil ? cur?.left : cur?.right\n // \u5220\u9664\u8282\u70b9 cur\n if cur !== root {\n if pre?.left === cur {\n pre?.left = child\n } else {\n pre?.right = child\n }\n } else {\n // \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\n root = child\n }\n }\n // \u5b50\u8282\u70b9\u6570\u91cf = 2\n else {\n // \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\n var tmp = cur?.right\n while tmp?.left != nil {\n tmp = tmp?.left\n }\n // \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\n remove(num: tmp!.val)\n // \u7528 tmp \u8986\u76d6 cur\n cur?.val = tmp!.val\n }\n}\n
binary_search_tree.js/* \u5220\u9664\u8282\u70b9 */\nremove(num) {\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n if (this.root === null) return;\n let cur = this.root,\n pre = null;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur !== null) {\n // \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n if (cur.val === num) break;\n pre = cur;\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur.val < num) cur = cur.right;\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else cur = cur.left;\n }\n // \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if (cur === null) return;\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\n if (cur.left === null || cur.right === null) {\n // \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = null / \u8be5\u5b50\u8282\u70b9\n const child = cur.left !== null ? cur.left : cur.right;\n // \u5220\u9664\u8282\u70b9 cur\n if (cur !== this.root) {\n if (pre.left === cur) pre.left = child;\n else pre.right = child;\n } else {\n // \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\n this.root = child;\n }\n }\n // \u5b50\u8282\u70b9\u6570\u91cf = 2\n else {\n // \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\n let tmp = cur.right;\n while (tmp.left !== null) {\n tmp = tmp.left;\n }\n // \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\n this.remove(tmp.val);\n // \u7528 tmp \u8986\u76d6 cur\n cur.val = tmp.val;\n }\n}\n
binary_search_tree.ts/* \u5220\u9664\u8282\u70b9 */\nremove(num: number): void {\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n if (this.root === null) return;\n let cur: TreeNode | null = this.root,\n pre: TreeNode | null = null;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur !== null) {\n // \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n if (cur.val === num) break;\n pre = cur;\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur.val < num) cur = cur.right;\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else cur = cur.left;\n }\n // \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if (cur === null) return;\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\n if (cur.left === null || cur.right === null) {\n // \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = null / \u8be5\u5b50\u8282\u70b9\n const child: TreeNode | null =\n cur.left !== null ? cur.left : cur.right;\n // \u5220\u9664\u8282\u70b9 cur\n if (cur !== this.root) {\n if (pre!.left === cur) pre!.left = child;\n else pre!.right = child;\n } else {\n // \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\n this.root = child;\n }\n }\n // \u5b50\u8282\u70b9\u6570\u91cf = 2\n else {\n // \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\n let tmp: TreeNode | null = cur.right;\n while (tmp!.left !== null) {\n tmp = tmp!.left;\n }\n // \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\n this.remove(tmp!.val);\n // \u7528 tmp \u8986\u76d6 cur\n cur.val = tmp!.val;\n }\n}\n
binary_search_tree.dart/* \u5220\u9664\u8282\u70b9 */\nvoid remove(int _num) {\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n if (_root == null) return;\n TreeNode? cur = _root;\n TreeNode? pre = null;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur != null) {\n // \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n if (cur.val == _num) break;\n pre = cur;\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur.val < _num)\n cur = cur.right;\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else\n cur = cur.left;\n }\n // \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\n if (cur == null) return;\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\n if (cur.left == null || cur.right == null) {\n // \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = null / \u8be5\u5b50\u8282\u70b9\n TreeNode? child = cur.left ?? cur.right;\n // \u5220\u9664\u8282\u70b9 cur\n if (cur != _root) {\n if (pre!.left == cur)\n pre.left = child;\n else\n pre.right = child;\n } else {\n // \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\n _root = child;\n }\n } else {\n // \u5b50\u8282\u70b9\u6570\u91cf = 2\n // \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\n TreeNode? tmp = cur.right;\n while (tmp!.left != null) {\n tmp = tmp.left;\n }\n // \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\n remove(tmp.val);\n // \u7528 tmp \u8986\u76d6 cur\n cur.val = tmp.val;\n }\n}\n
binary_search_tree.rs/* \u5220\u9664\u8282\u70b9 */\npub fn remove(&mut self, num: i32) {\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n if self.root.is_none() { \n return; \n }\n let mut cur = self.root.clone();\n let mut pre = None;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while let Some(node) = cur.clone() {\n // \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n if node.borrow().val == num {\n break;\n }\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n pre = cur.clone();\n if node.borrow().val < num {\n cur = node.borrow().right.clone();\n }\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else {\n cur = node.borrow().left.clone();\n }\n }\n // \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if cur.is_none() {\n return;\n }\n let cur = cur.unwrap();\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\n if cur.borrow().left.is_none() || cur.borrow().right.is_none() {\n // \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = nullptr / \u8be5\u5b50\u8282\u70b9\n let child = cur.borrow().left.clone().or_else(|| cur.borrow().right.clone());\n let pre = pre.unwrap();\n let left = pre.borrow().left.clone().unwrap();\n // \u5220\u9664\u8282\u70b9 cur\n if !Rc::ptr_eq(&cur, self.root.as_ref().unwrap()) {\n if Rc::ptr_eq(&left, &cur) {\n pre.borrow_mut().left = child;\n } else {\n pre.borrow_mut().right = child;\n }\n } else {\n // \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\n self.root = child;\n }\n }\n // \u5b50\u8282\u70b9\u6570\u91cf = 2\n else {\n // \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\n let mut tmp = cur.borrow().right.clone();\n while let Some(node) = tmp.clone() {\n if node.borrow().left.is_some() {\n tmp = node.borrow().left.clone();\n } else {\n break;\n }\n }\n let tmpval = tmp.unwrap().borrow().val;\n // \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\n self.remove(tmpval);\n // \u7528 tmp \u8986\u76d6 cur\n cur.borrow_mut().val = tmpval;\n }\n}\n
binary_search_tree.c/* \u5220\u9664\u8282\u70b9 */\n// \u7531\u4e8e\u5f15\u5165\u4e86 stdio.h \uff0c\u6b64\u5904\u65e0\u6cd5\u4f7f\u7528 remove \u5173\u952e\u8bcd\nvoid removeItem(BinarySearchTree *bst, int num) {\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n if (bst->root == NULL)\n return;\n TreeNode *cur = bst->root, *pre = NULL;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur != NULL) {\n // \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n if (cur->val == num)\n break;\n pre = cur;\n if (cur->val < num) {\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 root \u7684\u53f3\u5b50\u6811\u4e2d\n cur = cur->right;\n } else {\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 root \u7684\u5de6\u5b50\u6811\u4e2d\n cur = cur->left;\n }\n }\n // \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if (cur == NULL)\n return;\n // \u5224\u65ad\u5f85\u5220\u9664\u8282\u70b9\u662f\u5426\u5b58\u5728\u5b50\u8282\u70b9\n if (cur->left == NULL || cur->right == NULL) {\n /* \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1 */\n // \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = nullptr / \u8be5\u5b50\u8282\u70b9\n TreeNode *child = cur->left != NULL ? cur->left : cur->right;\n // \u5220\u9664\u8282\u70b9 cur\n if (pre->left == cur) {\n pre->left = child;\n } else {\n pre->right = child;\n }\n } else {\n /* \u5b50\u8282\u70b9\u6570\u91cf = 2 */\n // \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\n TreeNode *tmp = cur->right;\n while (tmp->left != NULL) {\n tmp = tmp->left;\n }\n int tmpVal = tmp->val;\n // \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\n removeItem(bst, tmp->val);\n // \u7528 tmp \u8986\u76d6 cur\n cur->val = tmpVal;\n }\n}\n
binary_search_tree.zig// \u5220\u9664\u8282\u70b9\nfn remove(self: *Self, num: T) void {\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n if (self.root == null) return;\n var cur = self.root;\n var pre: ?*inc.TreeNode(T) = null;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur != null) {\n // \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n if (cur.?.val == num) break;\n pre = cur;\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur.?.val < num) {\n cur = cur.?.right;\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n } else {\n cur = cur.?.left;\n }\n }\n // \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if (cur == null) return;\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\n if (cur.?.left == null or cur.?.right == null) {\n // \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = null / \u8be5\u5b50\u8282\u70b9\n var child = if (cur.?.left != null) cur.?.left else cur.?.right;\n // \u5220\u9664\u8282\u70b9 cur\n if (pre.?.left == cur) {\n pre.?.left = child;\n } else {\n pre.?.right = child;\n }\n // \u5b50\u8282\u70b9\u6570\u91cf = 2\n } else {\n // \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\n var tmp = cur.?.right;\n while (tmp.?.left != null) {\n tmp = tmp.?.left;\n }\n var tmp_val = tmp.?.val;\n // \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\n self.remove(tmp.?.val);\n // \u7528 tmp \u8986\u76d6 cur\n cur.?.val = tmp_val;\n }\n}\n
"},{"location":"chapter_tree/binary_search_tree/#4","title":"4. \u00a0 \u4e2d\u5e8f\u904d\u5386\u6709\u5e8f","text":"\u5982\u56fe 7-22 \u6240\u793a\uff0c\u4e8c\u53c9\u6811\u7684\u4e2d\u5e8f\u904d\u5386\u9075\u5faa\u201c\u5de6 \\(\\rightarrow\\) \u6839 \\(\\rightarrow\\) \u53f3\u201d\u7684\u904d\u5386\u987a\u5e8f\uff0c\u800c\u4e8c\u53c9\u641c\u7d22\u6811\u6ee1\u8db3\u201c\u5de6\u5b50\u8282\u70b9 \\(<\\) \u6839\u8282\u70b9 \\(<\\) \u53f3\u5b50\u8282\u70b9\u201d\u7684\u5927\u5c0f\u5173\u7cfb\u3002
\u8fd9\u610f\u5473\u7740\u5728\u4e8c\u53c9\u641c\u7d22\u6811\u4e2d\u8fdb\u884c\u4e2d\u5e8f\u904d\u5386\u65f6\uff0c\u603b\u662f\u4f1a\u4f18\u5148\u904d\u5386\u4e0b\u4e00\u4e2a\u6700\u5c0f\u8282\u70b9\uff0c\u4ece\u800c\u5f97\u51fa\u4e00\u4e2a\u91cd\u8981\u6027\u8d28\uff1a\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u4e2d\u5e8f\u904d\u5386\u5e8f\u5217\u662f\u5347\u5e8f\u7684\u3002
\u5229\u7528\u4e2d\u5e8f\u904d\u5386\u5347\u5e8f\u7684\u6027\u8d28\uff0c\u6211\u4eec\u5728\u4e8c\u53c9\u641c\u7d22\u6811\u4e2d\u83b7\u53d6\u6709\u5e8f\u6570\u636e\u4ec5\u9700 \\(O(n)\\) \u65f6\u95f4\uff0c\u65e0\u987b\u8fdb\u884c\u989d\u5916\u7684\u6392\u5e8f\u64cd\u4f5c\uff0c\u975e\u5e38\u9ad8\u6548\u3002
\u56fe 7-22 \u00a0 \u4e8c\u53c9\u641c\u7d22\u6811\u7684\u4e2d\u5e8f\u904d\u5386\u5e8f\u5217
"},{"location":"chapter_tree/binary_search_tree/#742","title":"7.4.2 \u00a0 \u4e8c\u53c9\u641c\u7d22\u6811\u7684\u6548\u7387","text":"\u7ed9\u5b9a\u4e00\u7ec4\u6570\u636e\uff0c\u6211\u4eec\u8003\u8651\u4f7f\u7528\u6570\u7ec4\u6216\u4e8c\u53c9\u641c\u7d22\u6811\u5b58\u50a8\u3002\u89c2\u5bdf\u8868 7-2 \uff0c\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u5404\u9879\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u90fd\u662f\u5bf9\u6570\u9636\uff0c\u5177\u6709\u7a33\u5b9a\u4e14\u9ad8\u6548\u7684\u6027\u80fd\u3002\u53ea\u6709\u5728\u9ad8\u9891\u6dfb\u52a0\u3001\u4f4e\u9891\u67e5\u627e\u5220\u9664\u6570\u636e\u7684\u573a\u666f\u4e0b\uff0c\u6570\u7ec4\u6bd4\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u6548\u7387\u66f4\u9ad8\u3002
\u8868 7-2 \u00a0 \u6570\u7ec4\u4e0e\u641c\u7d22\u6811\u7684\u6548\u7387\u5bf9\u6bd4
\u65e0\u5e8f\u6570\u7ec4 \u4e8c\u53c9\u641c\u7d22\u6811 \u67e5\u627e\u5143\u7d20 \\(O(n)\\) \\(O(\\log n)\\) \u63d2\u5165\u5143\u7d20 \\(O(1)\\) \\(O(\\log n)\\) \u5220\u9664\u5143\u7d20 \\(O(n)\\) \\(O(\\log n)\\)\u5728\u7406\u60f3\u60c5\u51b5\u4e0b\uff0c\u4e8c\u53c9\u641c\u7d22\u6811\u662f\u201c\u5e73\u8861\u201d\u7684\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u5728 \\(\\log n\\) \u8f6e\u5faa\u73af\u5185\u67e5\u627e\u4efb\u610f\u8282\u70b9\u3002
\u7136\u800c\uff0c\u5982\u679c\u6211\u4eec\u5728\u4e8c\u53c9\u641c\u7d22\u6811\u4e2d\u4e0d\u65ad\u5730\u63d2\u5165\u548c\u5220\u9664\u8282\u70b9\uff0c\u53ef\u80fd\u5bfc\u81f4\u4e8c\u53c9\u6811\u9000\u5316\u4e3a\u56fe 7-23 \u6240\u793a\u7684\u94fe\u8868\uff0c\u8fd9\u65f6\u5404\u79cd\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e5f\u4f1a\u9000\u5316\u4e3a \\(O(n)\\) \u3002
\u56fe 7-23 \u00a0 \u4e8c\u53c9\u641c\u7d22\u6811\u9000\u5316
"},{"location":"chapter_tree/binary_search_tree/#743","title":"7.4.3 \u00a0 \u4e8c\u53c9\u641c\u7d22\u6811\u5e38\u89c1\u5e94\u7528","text":"\u300c\u4e8c\u53c9\u6811 binary tree\u300d\u662f\u4e00\u79cd\u975e\u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff0c\u4ee3\u8868\u201c\u7956\u5148\u201d\u4e0e\u201c\u540e\u4ee3\u201d\u4e4b\u95f4\u7684\u6d3e\u751f\u5173\u7cfb\uff0c\u4f53\u73b0\u4e86\u201c\u4e00\u5206\u4e3a\u4e8c\u201d\u7684\u5206\u6cbb\u903b\u8f91\u3002\u4e0e\u94fe\u8868\u7c7b\u4f3c\uff0c\u4e8c\u53c9\u6811\u7684\u57fa\u672c\u5355\u5143\u662f\u8282\u70b9\uff0c\u6bcf\u4e2a\u8282\u70b9\u5305\u542b\u503c\u3001\u5de6\u5b50\u8282\u70b9\u5f15\u7528\u548c\u53f3\u5b50\u8282\u70b9\u5f15\u7528\u3002
PythonC++JavaC#GoSwiftJSTSDartRustCZigclass TreeNode:\n \"\"\"\u4e8c\u53c9\u6811\u8282\u70b9\u7c7b\"\"\"\n def __init__(self, val: int):\n self.val: int = val # \u8282\u70b9\u503c\n self.left: TreeNode | None = None # \u5de6\u5b50\u8282\u70b9\u5f15\u7528\n self.right: TreeNode | None = None # \u53f3\u5b50\u8282\u70b9\u5f15\u7528\n
/* \u4e8c\u53c9\u6811\u8282\u70b9\u7ed3\u6784\u4f53 */\nstruct TreeNode {\n int val; // \u8282\u70b9\u503c\n TreeNode *left; // \u5de6\u5b50\u8282\u70b9\u6307\u9488\n TreeNode *right; // \u53f3\u5b50\u8282\u70b9\u6307\u9488\n TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n};\n
/* \u4e8c\u53c9\u6811\u8282\u70b9\u7c7b */\nclass TreeNode {\n int val; // \u8282\u70b9\u503c\n TreeNode left; // \u5de6\u5b50\u8282\u70b9\u5f15\u7528\n TreeNode right; // \u53f3\u5b50\u8282\u70b9\u5f15\u7528\n TreeNode(int x) { val = x; }\n}\n
/* \u4e8c\u53c9\u6811\u8282\u70b9\u7c7b */\nclass TreeNode(int? x) {\n public int? val = x; // \u8282\u70b9\u503c\n public TreeNode? left; // \u5de6\u5b50\u8282\u70b9\u5f15\u7528\n public TreeNode? right; // \u53f3\u5b50\u8282\u70b9\u5f15\u7528\n}\n
/* \u4e8c\u53c9\u6811\u8282\u70b9\u7ed3\u6784\u4f53 */\ntype TreeNode struct {\n Val int\n Left *TreeNode\n Right *TreeNode\n}\n/* \u6784\u9020\u65b9\u6cd5 */\nfunc NewTreeNode(v int) *TreeNode {\n return &TreeNode{\n Left: nil, // \u5de6\u5b50\u8282\u70b9\u6307\u9488\n Right: nil, // \u53f3\u5b50\u8282\u70b9\u6307\u9488\n Val: v, // \u8282\u70b9\u503c\n }\n}\n
/* \u4e8c\u53c9\u6811\u8282\u70b9\u7c7b */\nclass TreeNode {\n var val: Int // \u8282\u70b9\u503c\n var left: TreeNode? // \u5de6\u5b50\u8282\u70b9\u5f15\u7528\n var right: TreeNode? // \u53f3\u5b50\u8282\u70b9\u5f15\u7528\n\n init(x: Int) {\n val = x\n }\n}\n
/* \u4e8c\u53c9\u6811\u8282\u70b9\u7c7b */\nclass TreeNode {\n val; // \u8282\u70b9\u503c\n left; // \u5de6\u5b50\u8282\u70b9\u6307\u9488\n right; // \u53f3\u5b50\u8282\u70b9\u6307\u9488\n constructor(val, left, right) {\n this.val = val === undefined ? 0 : val;\n this.left = left === undefined ? null : left;\n this.right = right === undefined ? null : right;\n }\n}\n
/* \u4e8c\u53c9\u6811\u8282\u70b9\u7c7b */\nclass TreeNode {\n val: number;\n left: TreeNode | null;\n right: TreeNode | null;\n\n constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {\n this.val = val === undefined ? 0 : val; // \u8282\u70b9\u503c\n this.left = left === undefined ? null : left; // \u5de6\u5b50\u8282\u70b9\u5f15\u7528\n this.right = right === undefined ? null : right; // \u53f3\u5b50\u8282\u70b9\u5f15\u7528\n }\n}\n
/* \u4e8c\u53c9\u6811\u8282\u70b9\u7c7b */\nclass TreeNode {\n int val; // \u8282\u70b9\u503c\n TreeNode? left; // \u5de6\u5b50\u8282\u70b9\u5f15\u7528\n TreeNode? right; // \u53f3\u5b50\u8282\u70b9\u5f15\u7528\n TreeNode(this.val, [this.left, this.right]);\n}\n
use std::rc::Rc;\nuse std::cell::RefCell;\n\n/* \u4e8c\u53c9\u6811\u8282\u70b9\u7ed3\u6784\u4f53 */\nstruct TreeNode {\n val: i32, // \u8282\u70b9\u503c\n left: Option<Rc<RefCell<TreeNode>>>, // \u5de6\u5b50\u8282\u70b9\u5f15\u7528\n right: Option<Rc<RefCell<TreeNode>>>, // \u53f3\u5b50\u8282\u70b9\u5f15\u7528\n}\n\nimpl TreeNode {\n /* \u6784\u9020\u65b9\u6cd5 */\n fn new(val: i32) -> Rc<RefCell<Self>> {\n Rc::new(RefCell::new(Self {\n val,\n left: None,\n right: None\n }))\n }\n}\n
/* \u4e8c\u53c9\u6811\u8282\u70b9\u7ed3\u6784\u4f53 */\ntypedef struct TreeNode {\n int val; // \u8282\u70b9\u503c\n int height; // \u8282\u70b9\u9ad8\u5ea6\n struct TreeNode *left; // \u5de6\u5b50\u8282\u70b9\u6307\u9488\n struct TreeNode *right; // \u53f3\u5b50\u8282\u70b9\u6307\u9488\n} TreeNode;\n\n/* \u6784\u9020\u51fd\u6570 */\nTreeNode *newTreeNode(int val) {\n TreeNode *node;\n\n node = (TreeNode *)malloc(sizeof(TreeNode));\n node->val = val;\n node->height = 0;\n node->left = NULL;\n node->right = NULL;\n return node;\n}\n
\n
\u6bcf\u4e2a\u8282\u70b9\u90fd\u6709\u4e24\u4e2a\u5f15\u7528\uff08\u6307\u9488\uff09\uff0c\u5206\u522b\u6307\u5411\u300c\u5de6\u5b50\u8282\u70b9 left-child node\u300d\u548c\u300c\u53f3\u5b50\u8282\u70b9 right-child node\u300d\uff0c\u8be5\u8282\u70b9\u88ab\u79f0\u4e3a\u8fd9\u4e24\u4e2a\u5b50\u8282\u70b9\u7684\u300c\u7236\u8282\u70b9 parent node\u300d\u3002\u5f53\u7ed9\u5b9a\u4e00\u4e2a\u4e8c\u53c9\u6811\u7684\u8282\u70b9\u65f6\uff0c\u6211\u4eec\u5c06\u8be5\u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u53ca\u5176\u4ee5\u4e0b\u8282\u70b9\u5f62\u6210\u7684\u6811\u79f0\u4e3a\u8be5\u8282\u70b9\u7684\u300c\u5de6\u5b50\u6811 left subtree\u300d\uff0c\u540c\u7406\u53ef\u5f97\u300c\u53f3\u5b50\u6811 right subtree\u300d\u3002
\u5728\u4e8c\u53c9\u6811\u4e2d\uff0c\u9664\u53f6\u8282\u70b9\u5916\uff0c\u5176\u4ed6\u6240\u6709\u8282\u70b9\u90fd\u5305\u542b\u5b50\u8282\u70b9\u548c\u975e\u7a7a\u5b50\u6811\u3002\u5982\u56fe 7-1 \u6240\u793a\uff0c\u5982\u679c\u5c06\u201c\u8282\u70b9 2\u201d\u89c6\u4e3a\u7236\u8282\u70b9\uff0c\u5219\u5176\u5de6\u5b50\u8282\u70b9\u548c\u53f3\u5b50\u8282\u70b9\u5206\u522b\u662f\u201c\u8282\u70b9 4\u201d\u548c\u201c\u8282\u70b9 5\u201d\uff0c\u5de6\u5b50\u6811\u662f\u201c\u8282\u70b9 4 \u53ca\u5176\u4ee5\u4e0b\u8282\u70b9\u5f62\u6210\u7684\u6811\u201d\uff0c\u53f3\u5b50\u6811\u662f\u201c\u8282\u70b9 5 \u53ca\u5176\u4ee5\u4e0b\u8282\u70b9\u5f62\u6210\u7684\u6811\u201d\u3002
\u56fe 7-1 \u00a0 \u7236\u8282\u70b9\u3001\u5b50\u8282\u70b9\u3001\u5b50\u6811
"},{"location":"chapter_tree/binary_tree/#711","title":"7.1.1 \u00a0 \u4e8c\u53c9\u6811\u5e38\u89c1\u672f\u8bed","text":"\u4e8c\u53c9\u6811\u7684\u5e38\u7528\u672f\u8bed\u5982\u56fe 7-2 \u6240\u793a\u3002
\u56fe 7-2 \u00a0 \u4e8c\u53c9\u6811\u7684\u5e38\u7528\u672f\u8bed
Tip
\u8bf7\u6ce8\u610f\uff0c\u6211\u4eec\u901a\u5e38\u5c06\u201c\u9ad8\u5ea6\u201d\u548c\u201c\u6df1\u5ea6\u201d\u5b9a\u4e49\u4e3a\u201c\u7ecf\u8fc7\u7684\u8fb9\u7684\u6570\u91cf\u201d\uff0c\u4f46\u6709\u4e9b\u9898\u76ee\u6216\u6559\u6750\u53ef\u80fd\u4f1a\u5c06\u5176\u5b9a\u4e49\u4e3a\u201c\u7ecf\u8fc7\u7684\u8282\u70b9\u7684\u6570\u91cf\u201d\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u9ad8\u5ea6\u548c\u6df1\u5ea6\u90fd\u9700\u8981\u52a0 1 \u3002
"},{"location":"chapter_tree/binary_tree/#712","title":"7.1.2 \u00a0 \u4e8c\u53c9\u6811\u57fa\u672c\u64cd\u4f5c","text":""},{"location":"chapter_tree/binary_tree/#1","title":"1. \u00a0 \u521d\u59cb\u5316\u4e8c\u53c9\u6811","text":"\u4e0e\u94fe\u8868\u7c7b\u4f3c\uff0c\u9996\u5148\u521d\u59cb\u5316\u8282\u70b9\uff0c\u7136\u540e\u6784\u5efa\u5f15\u7528\uff08\u6307\u9488\uff09\u3002
PythonC++JavaC#GoSwiftJSTSDartRustCZig binary_tree.py# \u521d\u59cb\u5316\u4e8c\u53c9\u6811\n# \u521d\u59cb\u5316\u8282\u70b9\nn1 = TreeNode(val=1)\nn2 = TreeNode(val=2)\nn3 = TreeNode(val=3)\nn4 = TreeNode(val=4)\nn5 = TreeNode(val=5)\n# \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\uff08\u6307\u9488\uff09\nn1.left = n2\nn1.right = n3\nn2.left = n4\nn2.right = n5\n
binary_tree.cpp/* \u521d\u59cb\u5316\u4e8c\u53c9\u6811 */\n// \u521d\u59cb\u5316\u8282\u70b9\nTreeNode* n1 = new TreeNode(1);\nTreeNode* n2 = new TreeNode(2);\nTreeNode* n3 = new TreeNode(3);\nTreeNode* n4 = new TreeNode(4);\nTreeNode* n5 = new TreeNode(5);\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\uff08\u6307\u9488\uff09\nn1->left = n2;\nn1->right = n3;\nn2->left = n4;\nn2->right = n5;\n
binary_tree.java// \u521d\u59cb\u5316\u8282\u70b9\nTreeNode n1 = new TreeNode(1);\nTreeNode n2 = new TreeNode(2);\nTreeNode n3 = new TreeNode(3);\nTreeNode n4 = new TreeNode(4);\nTreeNode n5 = new TreeNode(5);\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\uff08\u6307\u9488\uff09\nn1.left = n2;\nn1.right = n3;\nn2.left = n4;\nn2.right = n5;\n
binary_tree.cs/* \u521d\u59cb\u5316\u4e8c\u53c9\u6811 */\n// \u521d\u59cb\u5316\u8282\u70b9\nTreeNode n1 = new(1);\nTreeNode n2 = new(2);\nTreeNode n3 = new(3);\nTreeNode n4 = new(4);\nTreeNode n5 = new(5);\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\uff08\u6307\u9488\uff09\nn1.left = n2;\nn1.right = n3;\nn2.left = n4;\nn2.right = n5;\n
binary_tree.go/* \u521d\u59cb\u5316\u4e8c\u53c9\u6811 */\n// \u521d\u59cb\u5316\u8282\u70b9\nn1 := NewTreeNode(1)\nn2 := NewTreeNode(2)\nn3 := NewTreeNode(3)\nn4 := NewTreeNode(4)\nn5 := NewTreeNode(5)\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\uff08\u6307\u9488\uff09\nn1.Left = n2\nn1.Right = n3\nn2.Left = n4\nn2.Right = n5\n
binary_tree.swift// \u521d\u59cb\u5316\u8282\u70b9\nlet n1 = TreeNode(x: 1)\nlet n2 = TreeNode(x: 2)\nlet n3 = TreeNode(x: 3)\nlet n4 = TreeNode(x: 4)\nlet n5 = TreeNode(x: 5)\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\uff08\u6307\u9488\uff09\nn1.left = n2\nn1.right = n3\nn2.left = n4\nn2.right = n5\n
binary_tree.js/* \u521d\u59cb\u5316\u4e8c\u53c9\u6811 */\n// \u521d\u59cb\u5316\u8282\u70b9\nlet n1 = new TreeNode(1),\n n2 = new TreeNode(2),\n n3 = new TreeNode(3),\n n4 = new TreeNode(4),\n n5 = new TreeNode(5);\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\uff08\u6307\u9488\uff09\nn1.left = n2;\nn1.right = n3;\nn2.left = n4;\nn2.right = n5;\n
binary_tree.ts/* \u521d\u59cb\u5316\u4e8c\u53c9\u6811 */\n// \u521d\u59cb\u5316\u8282\u70b9\nlet n1 = new TreeNode(1),\n n2 = new TreeNode(2),\n n3 = new TreeNode(3),\n n4 = new TreeNode(4),\n n5 = new TreeNode(5);\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\uff08\u6307\u9488\uff09\nn1.left = n2;\nn1.right = n3;\nn2.left = n4;\nn2.right = n5;\n
binary_tree.dart/* \u521d\u59cb\u5316\u4e8c\u53c9\u6811 */\n// \u521d\u59cb\u5316\u8282\u70b9\nTreeNode n1 = new TreeNode(1);\nTreeNode n2 = new TreeNode(2);\nTreeNode n3 = new TreeNode(3);\nTreeNode n4 = new TreeNode(4);\nTreeNode n5 = new TreeNode(5);\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\uff08\u6307\u9488\uff09\nn1.left = n2;\nn1.right = n3;\nn2.left = n4;\nn2.right = n5;\n
binary_tree.rs// \u521d\u59cb\u5316\u8282\u70b9\nlet n1 = TreeNode::new(1);\nlet n2 = TreeNode::new(2);\nlet n3 = TreeNode::new(3);\nlet n4 = TreeNode::new(4);\nlet n5 = TreeNode::new(5);\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\uff08\u6307\u9488\uff09\nn1.borrow_mut().left = Some(n2.clone());\nn1.borrow_mut().right = Some(n3);\nn2.borrow_mut().left = Some(n4);\nn2.borrow_mut().right = Some(n5);\n
binary_tree.c/* \u521d\u59cb\u5316\u4e8c\u53c9\u6811 */\n// \u521d\u59cb\u5316\u8282\u70b9\nTreeNode *n1 = newTreeNode(1);\nTreeNode *n2 = newTreeNode(2);\nTreeNode *n3 = newTreeNode(3);\nTreeNode *n4 = newTreeNode(4);\nTreeNode *n5 = newTreeNode(5);\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\uff08\u6307\u9488\uff09\nn1->left = n2;\nn1->right = n3;\nn2->left = n4;\nn2->right = n5;\n
binary_tree.zig\n
"},{"location":"chapter_tree/binary_tree/#2","title":"2. \u00a0 \u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9","text":"\u4e0e\u94fe\u8868\u7c7b\u4f3c\uff0c\u5728\u4e8c\u53c9\u6811\u4e2d\u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9\u53ef\u4ee5\u901a\u8fc7\u4fee\u6539\u6307\u9488\u6765\u5b9e\u73b0\u3002\u56fe 7-3 \u7ed9\u51fa\u4e86\u4e00\u4e2a\u793a\u4f8b\u3002
\u56fe 7-3 \u00a0 \u5728\u4e8c\u53c9\u6811\u4e2d\u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9
PythonC++JavaC#GoSwiftJSTSDartRustCZig binary_tree.py# \u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9\np = TreeNode(0)\n# \u5728 n1 -> n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\nn1.left = p\np.left = n2\n# \u5220\u9664\u8282\u70b9 P\nn1.left = n2\n
binary_tree.cpp/* \u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9 */\nTreeNode* P = new TreeNode(0);\n// \u5728 n1 -> n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\nn1->left = P;\nP->left = n2;\n// \u5220\u9664\u8282\u70b9 P\nn1->left = n2;\n
binary_tree.javaTreeNode P = new TreeNode(0);\n// \u5728 n1 -> n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\nn1.left = P;\nP.left = n2;\n// \u5220\u9664\u8282\u70b9 P\nn1.left = n2;\n
binary_tree.cs/* \u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9 */\nTreeNode P = new(0);\n// \u5728 n1 -> n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\nn1.left = P;\nP.left = n2;\n// \u5220\u9664\u8282\u70b9 P\nn1.left = n2;\n
binary_tree.go/* \u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9 */\n// \u5728 n1 -> n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\np := NewTreeNode(0)\nn1.Left = p\np.Left = n2\n// \u5220\u9664\u8282\u70b9 P\nn1.Left = n2\n
binary_tree.swiftlet P = TreeNode(x: 0)\n// \u5728 n1 -> n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\nn1.left = P\nP.left = n2\n// \u5220\u9664\u8282\u70b9 P\nn1.left = n2\n
binary_tree.js/* \u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9 */\nlet P = new TreeNode(0);\n// \u5728 n1 -> n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\nn1.left = P;\nP.left = n2;\n// \u5220\u9664\u8282\u70b9 P\nn1.left = n2;\n
binary_tree.ts/* \u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9 */\nconst P = new TreeNode(0);\n// \u5728 n1 -> n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\nn1.left = P;\nP.left = n2;\n// \u5220\u9664\u8282\u70b9 P\nn1.left = n2;\n
binary_tree.dart/* \u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9 */\nTreeNode P = new TreeNode(0);\n// \u5728 n1 -> n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\nn1.left = P;\nP.left = n2;\n// \u5220\u9664\u8282\u70b9 P\nn1.left = n2;\n
binary_tree.rslet p = TreeNode::new(0);\n// \u5728 n1 -> n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\nn1.borrow_mut().left = Some(p.clone());\np.borrow_mut().left = Some(n2.clone());\n// \u5220\u9664\u8282\u70b9 p\nn1.borrow_mut().left = Some(n2);\n
binary_tree.c/* \u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9 */\nTreeNode *P = newTreeNode(0);\n// \u5728 n1 -> n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\nn1->left = P;\nP->left = n2;\n// \u5220\u9664\u8282\u70b9 P\nn1->left = n2;\n
binary_tree.zig\n
Note
\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u63d2\u5165\u8282\u70b9\u53ef\u80fd\u4f1a\u6539\u53d8\u4e8c\u53c9\u6811\u7684\u539f\u6709\u903b\u8f91\u7ed3\u6784\uff0c\u800c\u5220\u9664\u8282\u70b9\u901a\u5e38\u610f\u5473\u7740\u5220\u9664\u8be5\u8282\u70b9\u53ca\u5176\u6240\u6709\u5b50\u6811\u3002\u56e0\u6b64\uff0c\u5728\u4e8c\u53c9\u6811\u4e2d\uff0c\u63d2\u5165\u4e0e\u5220\u9664\u901a\u5e38\u662f\u7531\u4e00\u5957\u64cd\u4f5c\u914d\u5408\u5b8c\u6210\u7684\uff0c\u4ee5\u5b9e\u73b0\u6709\u5b9e\u9645\u610f\u4e49\u7684\u64cd\u4f5c\u3002
"},{"location":"chapter_tree/binary_tree/#713","title":"7.1.3 \u00a0 \u5e38\u89c1\u4e8c\u53c9\u6811\u7c7b\u578b","text":""},{"location":"chapter_tree/binary_tree/#1_1","title":"1. \u00a0 \u5b8c\u7f8e\u4e8c\u53c9\u6811","text":"\u5982\u56fe 7-4 \u6240\u793a\uff0c\u300c\u5b8c\u7f8e\u4e8c\u53c9\u6811 perfect binary tree\u300d\u6240\u6709\u5c42\u7684\u8282\u70b9\u90fd\u88ab\u5b8c\u5168\u586b\u6ee1\u3002\u5728\u5b8c\u7f8e\u4e8c\u53c9\u6811\u4e2d\uff0c\u53f6\u8282\u70b9\u7684\u5ea6\u4e3a \\(0\\) \uff0c\u5176\u4f59\u6240\u6709\u8282\u70b9\u7684\u5ea6\u90fd\u4e3a \\(2\\) \uff1b\u82e5\u6811\u7684\u9ad8\u5ea6\u4e3a \\(h\\) \uff0c\u5219\u8282\u70b9\u603b\u6570\u4e3a \\(2^{h+1} - 1\\) \uff0c\u5448\u73b0\u6807\u51c6\u7684\u6307\u6570\u7ea7\u5173\u7cfb\uff0c\u53cd\u6620\u4e86\u81ea\u7136\u754c\u4e2d\u5e38\u89c1\u7684\u7ec6\u80de\u5206\u88c2\u73b0\u8c61\u3002
Tip
\u8bf7\u6ce8\u610f\uff0c\u5728\u4e2d\u6587\u793e\u533a\u4e2d\uff0c\u5b8c\u7f8e\u4e8c\u53c9\u6811\u5e38\u88ab\u79f0\u4e3a\u300c\u6ee1\u4e8c\u53c9\u6811\u300d\u3002
\u56fe 7-4 \u00a0 \u5b8c\u7f8e\u4e8c\u53c9\u6811
"},{"location":"chapter_tree/binary_tree/#2_1","title":"2. \u00a0 \u5b8c\u5168\u4e8c\u53c9\u6811","text":"\u5982\u56fe 7-5 \u6240\u793a\uff0c\u300c\u5b8c\u5168\u4e8c\u53c9\u6811 complete binary tree\u300d\u53ea\u6709\u6700\u5e95\u5c42\u7684\u8282\u70b9\u672a\u88ab\u586b\u6ee1\uff0c\u4e14\u6700\u5e95\u5c42\u8282\u70b9\u5c3d\u91cf\u9760\u5de6\u586b\u5145\u3002
\u56fe 7-5 \u00a0 \u5b8c\u5168\u4e8c\u53c9\u6811
"},{"location":"chapter_tree/binary_tree/#3","title":"3. \u00a0 \u5b8c\u6ee1\u4e8c\u53c9\u6811","text":"\u5982\u56fe 7-6 \u6240\u793a\uff0c\u300c\u5b8c\u6ee1\u4e8c\u53c9\u6811 full binary tree\u300d\u9664\u4e86\u53f6\u8282\u70b9\u4e4b\u5916\uff0c\u5176\u4f59\u6240\u6709\u8282\u70b9\u90fd\u6709\u4e24\u4e2a\u5b50\u8282\u70b9\u3002
\u56fe 7-6 \u00a0 \u5b8c\u6ee1\u4e8c\u53c9\u6811
"},{"location":"chapter_tree/binary_tree/#4","title":"4. \u00a0 \u5e73\u8861\u4e8c\u53c9\u6811","text":"\u5982\u56fe 7-7 \u6240\u793a\uff0c\u300c\u5e73\u8861\u4e8c\u53c9\u6811 balanced binary tree\u300d\u4e2d\u4efb\u610f\u8282\u70b9\u7684\u5de6\u5b50\u6811\u548c\u53f3\u5b50\u6811\u7684\u9ad8\u5ea6\u4e4b\u5dee\u7684\u7edd\u5bf9\u503c\u4e0d\u8d85\u8fc7 1 \u3002
\u56fe 7-7 \u00a0 \u5e73\u8861\u4e8c\u53c9\u6811
"},{"location":"chapter_tree/binary_tree/#714","title":"7.1.4 \u00a0 \u4e8c\u53c9\u6811\u7684\u9000\u5316","text":"\u56fe 7-8 \u5c55\u793a\u4e86\u4e8c\u53c9\u6811\u7684\u7406\u60f3\u7ed3\u6784\u4e0e\u9000\u5316\u7ed3\u6784\u3002\u5f53\u4e8c\u53c9\u6811\u7684\u6bcf\u5c42\u8282\u70b9\u90fd\u88ab\u586b\u6ee1\u65f6\uff0c\u8fbe\u5230\u201c\u5b8c\u7f8e\u4e8c\u53c9\u6811\u201d\uff1b\u800c\u5f53\u6240\u6709\u8282\u70b9\u90fd\u504f\u5411\u4e00\u4fa7\u65f6\uff0c\u4e8c\u53c9\u6811\u9000\u5316\u4e3a\u201c\u94fe\u8868\u201d\u3002
\u56fe 7-8 \u00a0 \u4e8c\u53c9\u6811\u7684\u6700\u4f73\u7ed3\u6784\u4e0e\u6700\u5dee\u7ed3\u6784
\u5982\u8868 7-1 \u6240\u793a\uff0c\u5728\u6700\u4f73\u7ed3\u6784\u548c\u6700\u5dee\u7ed3\u6784\u4e0b\uff0c\u4e8c\u53c9\u6811\u7684\u53f6\u8282\u70b9\u6570\u91cf\u3001\u8282\u70b9\u603b\u6570\u3001\u9ad8\u5ea6\u7b49\u8fbe\u5230\u6781\u5927\u503c\u6216\u6781\u5c0f\u503c\u3002
\u8868 7-1 \u00a0 \u4e8c\u53c9\u6811\u7684\u6700\u4f73\u7ed3\u6784\u4e0e\u6700\u5dee\u7ed3\u6784
\u5b8c\u7f8e\u4e8c\u53c9\u6811 \u94fe\u8868 \u7b2c \\(i\\) \u5c42\u7684\u8282\u70b9\u6570\u91cf \\(2^{i-1}\\) \\(1\\) \u9ad8\u5ea6\u4e3a \\(h\\) \u7684\u6811\u7684\u53f6\u8282\u70b9\u6570\u91cf \\(2^h\\) \\(1\\) \u9ad8\u5ea6\u4e3a \\(h\\) \u7684\u6811\u7684\u8282\u70b9\u603b\u6570 \\(2^{h+1} - 1\\) \\(h + 1\\) \u8282\u70b9\u603b\u6570\u4e3a \\(n\\) \u7684\u6811\u7684\u9ad8\u5ea6 \\(\\log_2 (n+1) - 1\\) \\(n - 1\\)"},{"location":"chapter_tree/binary_tree_traversal/","title":"7.2 \u00a0 \u4e8c\u53c9\u6811\u904d\u5386","text":"\u4ece\u7269\u7406\u7ed3\u6784\u7684\u89d2\u5ea6\u6765\u770b\uff0c\u6811\u662f\u4e00\u79cd\u57fa\u4e8e\u94fe\u8868\u7684\u6570\u636e\u7ed3\u6784\uff0c\u56e0\u6b64\u5176\u904d\u5386\u65b9\u5f0f\u662f\u901a\u8fc7\u6307\u9488\u9010\u4e2a\u8bbf\u95ee\u8282\u70b9\u3002\u7136\u800c\uff0c\u6811\u662f\u4e00\u79cd\u975e\u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff0c\u8fd9\u4f7f\u5f97\u904d\u5386\u6811\u6bd4\u904d\u5386\u94fe\u8868\u66f4\u52a0\u590d\u6742\uff0c\u9700\u8981\u501f\u52a9\u641c\u7d22\u7b97\u6cd5\u6765\u5b9e\u73b0\u3002
\u4e8c\u53c9\u6811\u5e38\u89c1\u7684\u904d\u5386\u65b9\u5f0f\u5305\u62ec\u5c42\u5e8f\u904d\u5386\u3001\u524d\u5e8f\u904d\u5386\u3001\u4e2d\u5e8f\u904d\u5386\u548c\u540e\u5e8f\u904d\u5386\u7b49\u3002
"},{"location":"chapter_tree/binary_tree_traversal/#721","title":"7.2.1 \u00a0 \u5c42\u5e8f\u904d\u5386","text":"\u5982\u56fe 7-9 \u6240\u793a\uff0c\u300c\u5c42\u5e8f\u904d\u5386 level-order traversal\u300d\u4ece\u9876\u90e8\u5230\u5e95\u90e8\u9010\u5c42\u904d\u5386\u4e8c\u53c9\u6811\uff0c\u5e76\u5728\u6bcf\u4e00\u5c42\u6309\u7167\u4ece\u5de6\u5230\u53f3\u7684\u987a\u5e8f\u8bbf\u95ee\u8282\u70b9\u3002
\u5c42\u5e8f\u904d\u5386\u672c\u8d28\u4e0a\u5c5e\u4e8e\u300c\u5e7f\u5ea6\u4f18\u5148\u904d\u5386 breadth-first traversal, BFS\u300d\uff0c\u5b83\u4f53\u73b0\u4e86\u4e00\u79cd\u201c\u4e00\u5708\u4e00\u5708\u5411\u5916\u6269\u5c55\u201d\u7684\u9010\u5c42\u904d\u5386\u65b9\u5f0f\u3002
\u56fe 7-9 \u00a0 \u4e8c\u53c9\u6811\u7684\u5c42\u5e8f\u904d\u5386
"},{"location":"chapter_tree/binary_tree_traversal/#1","title":"1. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"\u5e7f\u5ea6\u4f18\u5148\u904d\u5386\u901a\u5e38\u501f\u52a9\u201c\u961f\u5217\u201d\u6765\u5b9e\u73b0\u3002\u961f\u5217\u9075\u5faa\u201c\u5148\u8fdb\u5148\u51fa\u201d\u7684\u89c4\u5219\uff0c\u800c\u5e7f\u5ea6\u4f18\u5148\u904d\u5386\u5219\u9075\u5faa\u201c\u9010\u5c42\u63a8\u8fdb\u201d\u7684\u89c4\u5219\uff0c\u4e24\u8005\u80cc\u540e\u7684\u601d\u60f3\u662f\u4e00\u81f4\u7684\u3002\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig binary_tree_bfs.pydef level_order(root: TreeNode | None) -> list[int]:\n \"\"\"\u5c42\u5e8f\u904d\u5386\"\"\"\n # \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u8282\u70b9\n queue: deque[TreeNode] = deque()\n queue.append(root)\n # \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\n res = []\n while queue:\n node: TreeNode = queue.popleft() # \u961f\u5217\u51fa\u961f\n res.append(node.val) # \u4fdd\u5b58\u8282\u70b9\u503c\n if node.left is not None:\n queue.append(node.left) # \u5de6\u5b50\u8282\u70b9\u5165\u961f\n if node.right is not None:\n queue.append(node.right) # \u53f3\u5b50\u8282\u70b9\u5165\u961f\n return res\n
binary_tree_bfs.cpp/* \u5c42\u5e8f\u904d\u5386 */\nvector<int> levelOrder(TreeNode *root) {\n // \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u8282\u70b9\n queue<TreeNode *> queue;\n queue.push(root);\n // \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\n vector<int> vec;\n while (!queue.empty()) {\n TreeNode *node = queue.front();\n queue.pop(); // \u961f\u5217\u51fa\u961f\n vec.push_back(node->val); // \u4fdd\u5b58\u8282\u70b9\u503c\n if (node->left != nullptr)\n queue.push(node->left); // \u5de6\u5b50\u8282\u70b9\u5165\u961f\n if (node->right != nullptr)\n queue.push(node->right); // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n }\n return vec;\n}\n
binary_tree_bfs.java/* \u5c42\u5e8f\u904d\u5386 */\nList<Integer> levelOrder(TreeNode root) {\n // \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u8282\u70b9\n Queue<TreeNode> queue = new LinkedList<>();\n queue.add(root);\n // \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\n List<Integer> list = new ArrayList<>();\n while (!queue.isEmpty()) {\n TreeNode node = queue.poll(); // \u961f\u5217\u51fa\u961f\n list.add(node.val); // \u4fdd\u5b58\u8282\u70b9\u503c\n if (node.left != null)\n queue.offer(node.left); // \u5de6\u5b50\u8282\u70b9\u5165\u961f\n if (node.right != null)\n queue.offer(node.right); // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n }\n return list;\n}\n
binary_tree_bfs.cs/* \u5c42\u5e8f\u904d\u5386 */\nList<int> LevelOrder(TreeNode root) {\n // \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u8282\u70b9\n Queue<TreeNode> queue = new();\n queue.Enqueue(root);\n // \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\n List<int> list = [];\n while (queue.Count != 0) {\n TreeNode node = queue.Dequeue(); // \u961f\u5217\u51fa\u961f\n list.Add(node.val!.Value); // \u4fdd\u5b58\u8282\u70b9\u503c\n if (node.left != null)\n queue.Enqueue(node.left); // \u5de6\u5b50\u8282\u70b9\u5165\u961f\n if (node.right != null)\n queue.Enqueue(node.right); // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n }\n return list;\n}\n
binary_tree_bfs.go/* \u5c42\u5e8f\u904d\u5386 */\nfunc levelOrder(root *TreeNode) []any {\n // \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u8282\u70b9\n queue := list.New()\n queue.PushBack(root)\n // \u521d\u59cb\u5316\u4e00\u4e2a\u5207\u7247\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\n nums := make([]any, 0)\n for queue.Len() > 0 {\n // \u961f\u5217\u51fa\u961f\n node := queue.Remove(queue.Front()).(*TreeNode)\n // \u4fdd\u5b58\u8282\u70b9\u503c\n nums = append(nums, node.Val)\n if node.Left != nil {\n // \u5de6\u5b50\u8282\u70b9\u5165\u961f\n queue.PushBack(node.Left)\n }\n if node.Right != nil {\n // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n queue.PushBack(node.Right)\n }\n }\n return nums\n}\n
binary_tree_bfs.swift/* \u5c42\u5e8f\u904d\u5386 */\nfunc levelOrder(root: TreeNode) -> [Int] {\n // \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u8282\u70b9\n var queue: [TreeNode] = [root]\n // \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\n var list: [Int] = []\n while !queue.isEmpty {\n let node = queue.removeFirst() // \u961f\u5217\u51fa\u961f\n list.append(node.val) // \u4fdd\u5b58\u8282\u70b9\u503c\n if let left = node.left {\n queue.append(left) // \u5de6\u5b50\u8282\u70b9\u5165\u961f\n }\n if let right = node.right {\n queue.append(right) // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n }\n }\n return list\n}\n
binary_tree_bfs.js/* \u5c42\u5e8f\u904d\u5386 */\nfunction levelOrder(root) {\n // \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u8282\u70b9\n const queue = [root];\n // \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\n const list = [];\n while (queue.length) {\n let node = queue.shift(); // \u961f\u5217\u51fa\u961f\n list.push(node.val); // \u4fdd\u5b58\u8282\u70b9\u503c\n if (node.left) queue.push(node.left); // \u5de6\u5b50\u8282\u70b9\u5165\u961f\n if (node.right) queue.push(node.right); // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n }\n return list;\n}\n
binary_tree_bfs.ts/* \u5c42\u5e8f\u904d\u5386 */\nfunction levelOrder(root: TreeNode | null): number[] {\n // \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u8282\u70b9\n const queue = [root];\n // \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\n const list: number[] = [];\n while (queue.length) {\n let node = queue.shift() as TreeNode; // \u961f\u5217\u51fa\u961f\n list.push(node.val); // \u4fdd\u5b58\u8282\u70b9\u503c\n if (node.left) {\n queue.push(node.left); // \u5de6\u5b50\u8282\u70b9\u5165\u961f\n }\n if (node.right) {\n queue.push(node.right); // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n }\n }\n return list;\n}\n
binary_tree_bfs.dart/* \u5c42\u5e8f\u904d\u5386 */\nList<int> levelOrder(TreeNode? root) {\n // \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u8282\u70b9\n Queue<TreeNode?> queue = Queue();\n queue.add(root);\n // \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\n List<int> res = [];\n while (queue.isNotEmpty) {\n TreeNode? node = queue.removeFirst(); // \u961f\u5217\u51fa\u961f\n res.add(node!.val); // \u4fdd\u5b58\u8282\u70b9\u503c\n if (node.left != null) queue.add(node.left); // \u5de6\u5b50\u8282\u70b9\u5165\u961f\n if (node.right != null) queue.add(node.right); // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n }\n return res;\n}\n
binary_tree_bfs.rs/* \u5c42\u5e8f\u904d\u5386 */\nfn level_order(root: &Rc<RefCell<TreeNode>>) -> Vec<i32> {\n // \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u8282\u70b9\n let mut que = VecDeque::new();\n que.push_back(Rc::clone(&root));\n // \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\n let mut vec = Vec::new();\n\n while let Some(node) = que.pop_front() { // \u961f\u5217\u51fa\u961f\n vec.push(node.borrow().val); // \u4fdd\u5b58\u8282\u70b9\u503c\n if let Some(left) = node.borrow().left.as_ref() {\n que.push_back(Rc::clone(left)); // \u5de6\u5b50\u8282\u70b9\u5165\u961f\n }\n if let Some(right) = node.borrow().right.as_ref() {\n que.push_back(Rc::clone(right)); // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n };\n }\n vec\n}\n
binary_tree_bfs.c/* \u5c42\u5e8f\u904d\u5386 */\nint *levelOrder(TreeNode *root, int *size) {\n /* \u8f85\u52a9\u961f\u5217 */\n int front, rear;\n int index, *arr;\n TreeNode *node;\n TreeNode **queue;\n\n /* \u8f85\u52a9\u961f\u5217 */\n queue = (TreeNode **)malloc(sizeof(TreeNode *) * MAX_SIZE);\n // \u961f\u5217\u6307\u9488\n front = 0, rear = 0;\n // \u52a0\u5165\u6839\u8282\u70b9\n queue[rear++] = root;\n // \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\n /* \u8f85\u52a9\u6570\u7ec4 */\n arr = (int *)malloc(sizeof(int) * MAX_SIZE);\n // \u6570\u7ec4\u6307\u9488\n index = 0;\n while (front < rear) {\n // \u961f\u5217\u51fa\u961f\n node = queue[front++];\n // \u4fdd\u5b58\u8282\u70b9\u503c\n arr[index++] = node->val;\n if (node->left != NULL) {\n // \u5de6\u5b50\u8282\u70b9\u5165\u961f\n queue[rear++] = node->left;\n }\n if (node->right != NULL) {\n // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n queue[rear++] = node->right;\n }\n }\n // \u66f4\u65b0\u6570\u7ec4\u957f\u5ea6\u7684\u503c\n *size = index;\n arr = realloc(arr, sizeof(int) * (*size));\n\n // \u91ca\u653e\u8f85\u52a9\u6570\u7ec4\u7a7a\u95f4\n free(queue);\n return arr;\n}\n
binary_tree_bfs.zig// \u5c42\u5e8f\u904d\u5386\nfn levelOrder(comptime T: type, mem_allocator: std.mem.Allocator, root: *inc.TreeNode(T)) !std.ArrayList(T) {\n // \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u8282\u70b9\n const L = std.TailQueue(*inc.TreeNode(T));\n var queue = L{};\n var root_node = try mem_allocator.create(L.Node);\n root_node.data = root;\n queue.append(root_node); \n // \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\n var list = std.ArrayList(T).init(std.heap.page_allocator);\n while (queue.len > 0) {\n var queue_node = queue.popFirst().?; // \u961f\u5217\u51fa\u961f\n var node = queue_node.data;\n try list.append(node.val); // \u4fdd\u5b58\u8282\u70b9\u503c\n if (node.left != null) {\n var tmp_node = try mem_allocator.create(L.Node);\n tmp_node.data = node.left.?;\n queue.append(tmp_node); // \u5de6\u5b50\u8282\u70b9\u5165\u961f\n }\n if (node.right != null) {\n var tmp_node = try mem_allocator.create(L.Node);\n tmp_node.data = node.right.?;\n queue.append(tmp_node); // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n } \n }\n return list;\n}\n
"},{"location":"chapter_tree/binary_tree_traversal/#2","title":"2. \u00a0 \u590d\u6742\u5ea6\u5206\u6790","text":"\u76f8\u5e94\u5730\uff0c\u524d\u5e8f\u3001\u4e2d\u5e8f\u548c\u540e\u5e8f\u904d\u5386\u90fd\u5c5e\u4e8e\u300c\u6df1\u5ea6\u4f18\u5148\u904d\u5386 depth-first traversal, DFS\u300d\uff0c\u5b83\u4f53\u73b0\u4e86\u4e00\u79cd\u201c\u5148\u8d70\u5230\u5c3d\u5934\uff0c\u518d\u56de\u6eaf\u7ee7\u7eed\u201d\u7684\u904d\u5386\u65b9\u5f0f\u3002
\u56fe 7-10 \u5c55\u793a\u4e86\u5bf9\u4e8c\u53c9\u6811\u8fdb\u884c\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u7684\u5de5\u4f5c\u539f\u7406\u3002\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u5c31\u50cf\u662f\u7ed5\u7740\u6574\u68f5\u4e8c\u53c9\u6811\u7684\u5916\u56f4\u201c\u8d70\u201d\u4e00\u5708\uff0c\u5728\u6bcf\u4e2a\u8282\u70b9\u90fd\u4f1a\u9047\u5230\u4e09\u4e2a\u4f4d\u7f6e\uff0c\u5206\u522b\u5bf9\u5e94\u524d\u5e8f\u904d\u5386\u3001\u4e2d\u5e8f\u904d\u5386\u548c\u540e\u5e8f\u904d\u5386\u3002
\u56fe 7-10 \u00a0 \u4e8c\u53c9\u641c\u7d22\u6811\u7684\u524d\u5e8f\u3001\u4e2d\u5e8f\u3001\u540e\u5e8f\u904d\u5386
"},{"location":"chapter_tree/binary_tree_traversal/#1_1","title":"1. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u901a\u5e38\u57fa\u4e8e\u9012\u5f52\u5b9e\u73b0\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig binary_tree_dfs.pydef pre_order(root: TreeNode | None):\n \"\"\"\u524d\u5e8f\u904d\u5386\"\"\"\n if root is None:\n return\n # \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\n res.append(root.val)\n pre_order(root=root.left)\n pre_order(root=root.right)\n\ndef in_order(root: TreeNode | None):\n \"\"\"\u4e2d\u5e8f\u904d\u5386\"\"\"\n if root is None:\n return\n # \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\n in_order(root=root.left)\n res.append(root.val)\n in_order(root=root.right)\n\ndef post_order(root: TreeNode | None):\n \"\"\"\u540e\u5e8f\u904d\u5386\"\"\"\n if root is None:\n return\n # \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\n post_order(root=root.left)\n post_order(root=root.right)\n res.append(root.val)\n
binary_tree_dfs.cpp/* \u524d\u5e8f\u904d\u5386 */\nvoid preOrder(TreeNode *root) {\n if (root == nullptr)\n return;\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\n vec.push_back(root->val);\n preOrder(root->left);\n preOrder(root->right);\n}\n\n/* \u4e2d\u5e8f\u904d\u5386 */\nvoid inOrder(TreeNode *root) {\n if (root == nullptr)\n return;\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\n inOrder(root->left);\n vec.push_back(root->val);\n inOrder(root->right);\n}\n\n/* \u540e\u5e8f\u904d\u5386 */\nvoid postOrder(TreeNode *root) {\n if (root == nullptr)\n return;\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\n postOrder(root->left);\n postOrder(root->right);\n vec.push_back(root->val);\n}\n
binary_tree_dfs.java/* \u524d\u5e8f\u904d\u5386 */\nvoid preOrder(TreeNode root) {\n if (root == null)\n return;\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\n list.add(root.val);\n preOrder(root.left);\n preOrder(root.right);\n}\n\n/* \u4e2d\u5e8f\u904d\u5386 */\nvoid inOrder(TreeNode root) {\n if (root == null)\n return;\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\n inOrder(root.left);\n list.add(root.val);\n inOrder(root.right);\n}\n\n/* \u540e\u5e8f\u904d\u5386 */\nvoid postOrder(TreeNode root) {\n if (root == null)\n return;\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\n postOrder(root.left);\n postOrder(root.right);\n list.add(root.val);\n}\n
binary_tree_dfs.cs/* \u524d\u5e8f\u904d\u5386 */\nvoid PreOrder(TreeNode? root) {\n if (root == null) return;\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\n list.Add(root.val!.Value);\n PreOrder(root.left);\n PreOrder(root.right);\n}\n\n/* \u4e2d\u5e8f\u904d\u5386 */\nvoid InOrder(TreeNode? root) {\n if (root == null) return;\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\n InOrder(root.left);\n list.Add(root.val!.Value);\n InOrder(root.right);\n}\n\n/* \u540e\u5e8f\u904d\u5386 */\nvoid PostOrder(TreeNode? root) {\n if (root == null) return;\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\n PostOrder(root.left);\n PostOrder(root.right);\n list.Add(root.val!.Value);\n}\n
binary_tree_dfs.go/* \u524d\u5e8f\u904d\u5386 */\nfunc preOrder(node *TreeNode) {\n if node == nil {\n return\n }\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\n nums = append(nums, node.Val)\n preOrder(node.Left)\n preOrder(node.Right)\n}\n\n/* \u4e2d\u5e8f\u904d\u5386 */\nfunc inOrder(node *TreeNode) {\n if node == nil {\n return\n }\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\n inOrder(node.Left)\n nums = append(nums, node.Val)\n inOrder(node.Right)\n}\n\n/* \u540e\u5e8f\u904d\u5386 */\nfunc postOrder(node *TreeNode) {\n if node == nil {\n return\n }\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\n postOrder(node.Left)\n postOrder(node.Right)\n nums = append(nums, node.Val)\n}\n
binary_tree_dfs.swift/* \u524d\u5e8f\u904d\u5386 */\nfunc preOrder(root: TreeNode?) {\n guard let root = root else {\n return\n }\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\n list.append(root.val)\n preOrder(root: root.left)\n preOrder(root: root.right)\n}\n\n/* \u4e2d\u5e8f\u904d\u5386 */\nfunc inOrder(root: TreeNode?) {\n guard let root = root else {\n return\n }\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\n inOrder(root: root.left)\n list.append(root.val)\n inOrder(root: root.right)\n}\n\n/* \u540e\u5e8f\u904d\u5386 */\nfunc postOrder(root: TreeNode?) {\n guard let root = root else {\n return\n }\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\n postOrder(root: root.left)\n postOrder(root: root.right)\n list.append(root.val)\n}\n
binary_tree_dfs.js/* \u524d\u5e8f\u904d\u5386 */\nfunction preOrder(root) {\n if (root === null) return;\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\n list.push(root.val);\n preOrder(root.left);\n preOrder(root.right);\n}\n\n/* \u4e2d\u5e8f\u904d\u5386 */\nfunction inOrder(root) {\n if (root === null) return;\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\n inOrder(root.left);\n list.push(root.val);\n inOrder(root.right);\n}\n\n/* \u540e\u5e8f\u904d\u5386 */\nfunction postOrder(root) {\n if (root === null) return;\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\n postOrder(root.left);\n postOrder(root.right);\n list.push(root.val);\n}\n
binary_tree_dfs.ts/* \u524d\u5e8f\u904d\u5386 */\nfunction preOrder(root: TreeNode | null): void {\n if (root === null) {\n return;\n }\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\n list.push(root.val);\n preOrder(root.left);\n preOrder(root.right);\n}\n\n/* \u4e2d\u5e8f\u904d\u5386 */\nfunction inOrder(root: TreeNode | null): void {\n if (root === null) {\n return;\n }\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\n inOrder(root.left);\n list.push(root.val);\n inOrder(root.right);\n}\n\n/* \u540e\u5e8f\u904d\u5386 */\nfunction postOrder(root: TreeNode | null): void {\n if (root === null) {\n return;\n }\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\n postOrder(root.left);\n postOrder(root.right);\n list.push(root.val);\n}\n
binary_tree_dfs.dart/* \u524d\u5e8f\u904d\u5386 */\nvoid preOrder(TreeNode? node) {\n if (node == null) return;\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\n list.add(node.val);\n preOrder(node.left);\n preOrder(node.right);\n}\n\n/* \u4e2d\u5e8f\u904d\u5386 */\nvoid inOrder(TreeNode? node) {\n if (node == null) return;\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\n inOrder(node.left);\n list.add(node.val);\n inOrder(node.right);\n}\n\n/* \u540e\u5e8f\u904d\u5386 */\nvoid postOrder(TreeNode? node) {\n if (node == null) return;\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\n postOrder(node.left);\n postOrder(node.right);\n list.add(node.val);\n}\n
binary_tree_dfs.rs/* \u524d\u5e8f\u904d\u5386 */\nfn pre_order(root: Option<&Rc<RefCell<TreeNode>>>) -> Vec<i32> {\n let mut result = vec![];\n\n if let Some(node) = root {\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\n result.push(node.borrow().val);\n result.append(&mut pre_order(node.borrow().left.as_ref()));\n result.append(&mut pre_order(node.borrow().right.as_ref()));\n }\n result\n}\n\n/* \u4e2d\u5e8f\u904d\u5386 */\nfn in_order(root: Option<&Rc<RefCell<TreeNode>>>) -> Vec<i32> {\n let mut result = vec![];\n\n if let Some(node) = root {\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\n result.append(&mut in_order(node.borrow().left.as_ref()));\n result.push(node.borrow().val);\n result.append(&mut in_order(node.borrow().right.as_ref()));\n }\n result\n}\n\n/* \u540e\u5e8f\u904d\u5386 */\nfn post_order(root: Option<&Rc<RefCell<TreeNode>>>) -> Vec<i32> {\n let mut result = vec![];\n\n if let Some(node) = root {\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\n result.append(&mut post_order(node.borrow().left.as_ref()));\n result.append(&mut post_order(node.borrow().right.as_ref()));\n result.push(node.borrow().val);\n }\n result\n}\n
binary_tree_dfs.c/* \u524d\u5e8f\u904d\u5386 */\nvoid preOrder(TreeNode *root, int *size) {\n if (root == NULL)\n return;\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\n arr[(*size)++] = root->val;\n preOrder(root->left, size);\n preOrder(root->right, size);\n}\n\n/* \u4e2d\u5e8f\u904d\u5386 */\nvoid inOrder(TreeNode *root, int *size) {\n if (root == NULL)\n return;\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\n inOrder(root->left, size);\n arr[(*size)++] = root->val;\n inOrder(root->right, size);\n}\n\n/* \u540e\u5e8f\u904d\u5386 */\nvoid postOrder(TreeNode *root, int *size) {\n if (root == NULL)\n return;\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\n postOrder(root->left, size);\n postOrder(root->right, size);\n arr[(*size)++] = root->val;\n}\n
binary_tree_dfs.zig// \u524d\u5e8f\u904d\u5386\nfn preOrder(comptime T: type, root: ?*inc.TreeNode(T)) !void {\n if (root == null) return;\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\n try list.append(root.?.val);\n try preOrder(T, root.?.left);\n try preOrder(T, root.?.right);\n}\n\n// \u4e2d\u5e8f\u904d\u5386\nfn inOrder(comptime T: type, root: ?*inc.TreeNode(T)) !void {\n if (root == null) return;\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\n try inOrder(T, root.?.left);\n try list.append(root.?.val);\n try inOrder(T, root.?.right);\n}\n\n// \u540e\u5e8f\u904d\u5386\nfn postOrder(comptime T: type, root: ?*inc.TreeNode(T)) !void {\n if (root == null) return;\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\n try postOrder(T, root.?.left);\n try postOrder(T, root.?.right);\n try list.append(root.?.val);\n}\n
Tip
\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u4e5f\u53ef\u4ee5\u57fa\u4e8e\u8fed\u4ee3\u5b9e\u73b0\uff0c\u6709\u5174\u8da3\u7684\u8bfb\u8005\u53ef\u4ee5\u81ea\u884c\u7814\u7a76\u3002
\u56fe 7-11 \u5c55\u793a\u4e86\u524d\u5e8f\u904d\u5386\u4e8c\u53c9\u6811\u7684\u9012\u5f52\u8fc7\u7a0b\uff0c\u5176\u53ef\u5206\u4e3a\u201c\u9012\u201d\u548c\u201c\u5f52\u201d\u4e24\u4e2a\u9006\u5411\u7684\u90e8\u5206\u3002
\u56fe 7-11 \u00a0 \u524d\u5e8f\u904d\u5386\u7684\u9012\u5f52\u8fc7\u7a0b
"},{"location":"chapter_tree/binary_tree_traversal/#2_1","title":"2. \u00a0 \u590d\u6742\u5ea6\u5206\u6790","text":"\u5bf9\u4e8e\u53ea\u6709\u4e00\u4e2a\u8282\u70b9\u7684\u4e8c\u53c9\u6811\uff0c\u6811\u7684\u9ad8\u5ea6\u548c\u6839\u8282\u70b9\u7684\u6df1\u5ea6\u90fd\u662f \\(0\\) \u5417\uff1f
\u662f\u7684\uff0c\u56e0\u4e3a\u9ad8\u5ea6\u548c\u6df1\u5ea6\u901a\u5e38\u5b9a\u4e49\u4e3a\u201c\u7ecf\u8fc7\u7684\u8fb9\u7684\u6570\u91cf\u201d\u3002
\u4e8c\u53c9\u6811\u4e2d\u7684\u63d2\u5165\u4e0e\u5220\u9664\u4e00\u822c\u7531\u4e00\u5957\u64cd\u4f5c\u914d\u5408\u5b8c\u6210\uff0c\u8fd9\u91cc\u7684\u201c\u4e00\u5957\u64cd\u4f5c\u201d\u6307\u4ec0\u4e48\u5462\uff1f\u53ef\u4ee5\u7406\u89e3\u4e3a\u8d44\u6e90\u7684\u5b50\u8282\u70b9\u7684\u8d44\u6e90\u91ca\u653e\u5417\uff1f
\u62ff\u4e8c\u53c9\u641c\u7d22\u6811\u6765\u4e3e\u4f8b\uff0c\u5220\u9664\u8282\u70b9\u64cd\u4f5c\u8981\u5206\u4e09\u79cd\u60c5\u51b5\u5904\u7406\uff0c\u5176\u4e2d\u6bcf\u79cd\u60c5\u51b5\u90fd\u9700\u8981\u8fdb\u884c\u591a\u4e2a\u6b65\u9aa4\u7684\u8282\u70b9\u64cd\u4f5c\u3002
\u4e3a\u4ec0\u4e48 DFS \u904d\u5386\u4e8c\u53c9\u6811\u6709\u524d\u3001\u4e2d\u3001\u540e\u4e09\u79cd\u987a\u5e8f\uff0c\u5206\u522b\u6709\u4ec0\u4e48\u7528\u5462\uff1f
\u4e0e\u987a\u5e8f\u548c\u9006\u5e8f\u904d\u5386\u6570\u7ec4\u7c7b\u4f3c\uff0c\u524d\u5e8f\u3001\u4e2d\u5e8f\u3001\u540e\u5e8f\u904d\u5386\u662f\u4e09\u79cd\u4e8c\u53c9\u6811\u904d\u5386\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5b83\u4eec\u5f97\u5230\u4e00\u4e2a\u7279\u5b9a\u987a\u5e8f\u7684\u904d\u5386\u7ed3\u679c\u3002\u4f8b\u5982\u5728\u4e8c\u53c9\u641c\u7d22\u6811\u4e2d\uff0c\u7531\u4e8e\u8282\u70b9\u5927\u5c0f\u6ee1\u8db3 \u5de6\u5b50\u8282\u70b9\u503c < \u6839\u8282\u70b9\u503c < \u53f3\u5b50\u8282\u70b9\u503c
\uff0c\u56e0\u6b64\u6211\u4eec\u53ea\u8981\u6309\u7167 \u5de6 $\\rightarrow$ \u6839 $\\rightarrow$ \u53f3
\u7684\u4f18\u5148\u7ea7\u904d\u5386\u6811\uff0c\u5c31\u53ef\u4ee5\u83b7\u5f97\u6709\u5e8f\u7684\u8282\u70b9\u5e8f\u5217\u3002
\u53f3\u65cb\u64cd\u4f5c\u662f\u5904\u7406\u5931\u8861\u8282\u70b9 node
\u3001child
\u3001grand_child
\u4e4b\u95f4\u7684\u5173\u7cfb\uff0c\u90a3 node
\u7684\u7236\u8282\u70b9\u548c node
\u539f\u6765\u7684\u8fde\u63a5\u4e0d\u9700\u8981\u7ef4\u62a4\u5417\uff1f\u53f3\u65cb\u64cd\u4f5c\u540e\u5c82\u4e0d\u662f\u65ad\u6389\u4e86\uff1f
\u6211\u4eec\u9700\u8981\u4ece\u9012\u5f52\u7684\u89c6\u89d2\u6765\u770b\u8fd9\u4e2a\u95ee\u9898\u3002\u53f3\u65cb\u64cd\u4f5c right_rotate(root)
\u4f20\u5165\u7684\u662f\u5b50\u6811\u7684\u6839\u8282\u70b9\uff0c\u6700\u7ec8 return child
\u8fd4\u56de\u65cb\u8f6c\u4e4b\u540e\u7684\u5b50\u6811\u7684\u6839\u8282\u70b9\u3002\u5b50\u6811\u7684\u6839\u8282\u70b9\u548c\u5176\u7236\u8282\u70b9\u7684\u8fde\u63a5\u662f\u5728\u8be5\u51fd\u6570\u8fd4\u56de\u540e\u5b8c\u6210\u7684\uff0c\u4e0d\u5c5e\u4e8e\u53f3\u65cb\u64cd\u4f5c\u7684\u7ef4\u62a4\u8303\u56f4\u3002
\u5728 C++ \u4e2d\uff0c\u51fd\u6570\u88ab\u5212\u5206\u5230 private
\u548c public
\u4e2d\uff0c\u8fd9\u65b9\u9762\u6709\u4ec0\u4e48\u8003\u91cf\u5417\uff1f\u4e3a\u4ec0\u4e48\u8981\u5c06 height()
\u51fd\u6570\u548c updateHeight()
\u51fd\u6570\u5206\u522b\u653e\u5728 public
\u548c private
\u4e2d\u5462\uff1f
\u4e3b\u8981\u770b\u65b9\u6cd5\u7684\u4f7f\u7528\u8303\u56f4\uff0c\u5982\u679c\u65b9\u6cd5\u53ea\u5728\u7c7b\u5185\u90e8\u4f7f\u7528\uff0c\u90a3\u4e48\u5c31\u8bbe\u8ba1\u4e3a private
\u3002\u4f8b\u5982\uff0c\u7528\u6237\u5355\u72ec\u8c03\u7528 updateHeight()
\u662f\u6ca1\u6709\u610f\u4e49\u7684\uff0c\u5b83\u53ea\u662f\u63d2\u5165\u3001\u5220\u9664\u64cd\u4f5c\u4e2d\u7684\u4e00\u6b65\u3002\u800c height()
\u662f\u8bbf\u95ee\u8282\u70b9\u9ad8\u5ea6\uff0c\u7c7b\u4f3c\u4e8e vector.size()
\uff0c\u56e0\u6b64\u8bbe\u7f6e\u6210 public
\u4ee5\u4fbf\u4f7f\u7528\u3002
\u5982\u4f55\u4ece\u4e00\u7ec4\u8f93\u5165\u6570\u636e\u6784\u5efa\u4e00\u68f5\u4e8c\u53c9\u641c\u7d22\u6811\uff1f\u6839\u8282\u70b9\u7684\u9009\u62e9\u662f\u4e0d\u662f\u5f88\u91cd\u8981\uff1f
\u662f\u7684\uff0c\u6784\u5efa\u6811\u7684\u65b9\u6cd5\u5df2\u5728\u4e8c\u53c9\u641c\u7d22\u6811\u4ee3\u7801\u4e2d\u7684 build_tree()
\u65b9\u6cd5\u4e2d\u7ed9\u51fa\u3002\u81f3\u4e8e\u6839\u8282\u70b9\u7684\u9009\u62e9\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u5c06\u8f93\u5165\u6570\u636e\u6392\u5e8f\uff0c\u7136\u540e\u5c06\u4e2d\u70b9\u5143\u7d20\u4f5c\u4e3a\u6839\u8282\u70b9\uff0c\u518d\u9012\u5f52\u5730\u6784\u5efa\u5de6\u53f3\u5b50\u6811\u3002\u8fd9\u6837\u505a\u53ef\u4ee5\u6700\u5927\u7a0b\u5ea6\u4fdd\u8bc1\u6811\u7684\u5e73\u8861\u6027\u3002
\u5728 Java \u4e2d\uff0c\u5b57\u7b26\u4e32\u5bf9\u6bd4\u662f\u5426\u4e00\u5b9a\u8981\u7528 equals()
\u65b9\u6cd5\uff1f
\u5728 Java \u4e2d\uff0c\u5bf9\u4e8e\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff0c==
\u7528\u4e8e\u5bf9\u6bd4\u4e24\u4e2a\u53d8\u91cf\u7684\u503c\u662f\u5426\u76f8\u7b49\u3002\u5bf9\u4e8e\u5f15\u7528\u7c7b\u578b\uff0c\u4e24\u79cd\u7b26\u53f7\u7684\u5de5\u4f5c\u539f\u7406\u662f\u4e0d\u540c\u7684\u3002
==
\uff1a\u7528\u6765\u6bd4\u8f83\u4e24\u4e2a\u53d8\u91cf\u662f\u5426\u6307\u5411\u540c\u4e00\u4e2a\u5bf9\u8c61\uff0c\u5373\u5b83\u4eec\u5728\u5185\u5b58\u4e2d\u7684\u4f4d\u7f6e\u662f\u5426\u76f8\u540c\u3002equals()
\uff1a\u7528\u6765\u5bf9\u6bd4\u4e24\u4e2a\u5bf9\u8c61\u7684\u503c\u662f\u5426\u76f8\u7b49\u3002\u56e0\u6b64\uff0c\u5982\u679c\u8981\u5bf9\u6bd4\u503c\uff0c\u6211\u4eec\u5e94\u8be5\u4f7f\u7528 equals()
\u3002\u7136\u800c\uff0c\u901a\u8fc7 String a = \"hi\"; String b = \"hi\";
\u521d\u59cb\u5316\u7684\u5b57\u7b26\u4e32\u90fd\u5b58\u50a8\u5728\u5b57\u7b26\u4e32\u5e38\u91cf\u6c60\u4e2d\uff0c\u5b83\u4eec\u6307\u5411\u540c\u4e00\u4e2a\u5bf9\u8c61\uff0c\u56e0\u6b64\u4e5f\u53ef\u4ee5\u7528 a == b
\u6765\u6bd4\u8f83\u4e24\u4e2a\u5b57\u7b26\u4e32\u7684\u5185\u5bb9\u3002
\u5e7f\u5ea6\u4f18\u5148\u904d\u5386\u5230\u6700\u5e95\u5c42\u4e4b\u524d\uff0c\u961f\u5217\u4e2d\u7684\u8282\u70b9\u6570\u91cf\u662f \\(2^h\\) \u5417\uff1f
\u662f\u7684\uff0c\u4f8b\u5982\u9ad8\u5ea6 \\(h = 2\\) \u7684\u6ee1\u4e8c\u53c9\u6811\uff0c\u5176\u8282\u70b9\u603b\u6570 \\(n = 7\\) \uff0c\u5219\u5e95\u5c42\u8282\u70b9\u6570\u91cf \\(4 = 2^h = (n + 1) / 2\\) \u3002
"}]} \ No newline at end of file +{"config":{"lang":["en"],"separator":"[\\s\\u200b\\u3000\\-\u3001\u3002\uff0c\uff0e\uff1f\uff01\uff1b]+","pipeline":["stemmer"]},"docs":[{"location":"","title":"Home","text":"\u300a Hello \u7b97\u6cd5 \u300b\u52a8\u753b\u56fe\u89e3\u3001\u4e00\u952e\u8fd0\u884c\u7684\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u6559\u7a0b
\u5f00\u59cb\u9605\u8bfb \u4e0b\u8f7d\u4ee3\u7801 \u4e0b\u8f7d PDF
\u63a8\u8350\u8bed
Quote
\u201c\u4e00\u672c\u901a\u4fd7\u6613\u61c2\u7684\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u5165\u95e8\u4e66\uff0c\u5f15\u5bfc\u8bfb\u8005\u624b\u8111\u5e76\u7528\u5730\u5b66\u4e60\uff0c\u5f3a\u70c8\u63a8\u8350\u7b97\u6cd5\u521d\u5b66\u8005\u9605\u8bfb\u3002\u201d
\u2014\u2014 \u9093\u4fca\u8f89\uff0c\u6e05\u534e\u5927\u5b66\u8ba1\u7b97\u673a\u7cfb\u6559\u6388
Quote
\u201c\u5982\u679c\u6211\u5f53\u5e74\u5b66\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7684\u65f6\u5019\u6709\u300aHello \u7b97\u6cd5\u300b\uff0c\u5b66\u8d77\u6765\u5e94\u8be5\u4f1a\u7b80\u5355 10 \u500d\uff01\u201d
\u2014\u2014 \u674e\u6c90\uff0c\u4e9a\u9a6c\u900a\u8d44\u6df1\u9996\u5e2d\u79d1\u5b66\u5bb6
\u52a8\u753b\u56fe\u89e3\u5185\u5bb9\u6e05\u6670\u6613\u61c2\u5b66\u4e60\u66f2\u7ebf\u5e73\u6ed1
\"A picture is worth a thousand words.\"
\u201c\u4e00\u56fe\u80dc\u5343\u8a00\u201d
\u4e00\u952e\u8fd0\u884c\u5341\u4f59\u79cd\u7f16\u7a0b\u8bed\u8a00\u4ee3\u7801\u53ef\u76f4\u63a5\u8fd0\u884c
\"Talk is cheap. Show me the code.\"
\u201c\u5c11\u5439\u725b\uff0c\u770b\u4ee3\u7801\u201d
\u4e92\u52a9\u5b66\u4e60\u6b22\u8fce\u8ba8\u8bba\u4e0e\u63d0\u95ee\u8bfb\u8005\u95f4\u643a\u624b\u5171\u8fdb
\"\u8ffd\u98ce\u8d76\u6708\u83ab\u505c\u7559\"
\u201c\u5e73\u829c\u5c3d\u5904\u662f\u6625\u5c71\u201d
\u5e8f\u4e24\u5e74\u524d\uff0c\u6211\u5728\u529b\u6263\u4e0a\u5206\u4eab\u4e86\u201c\u5251\u6307 Offer\u201d\u7cfb\u5217\u9898\u89e3\uff0c\u53d7\u5230\u4e86\u8bb8\u591a\u540c\u5b66\u7684\u559c\u7231\u548c\u652f\u6301\u3002\u5728\u4e0e\u8bfb\u8005\u4ea4\u6d41\u671f\u95f4\uff0c\u6211\u6700\u5e38\u6536\u5230\u7684\u4e00\u4e2a\u95ee\u9898\u662f\u201c\u5982\u4f55\u5165\u95e8\u7b97\u6cd5\u201d\u3002\u9010\u6e10\u5730\uff0c\u6211\u5bf9\u8fd9\u4e2a\u95ee\u9898\u4ea7\u751f\u4e86\u6d53\u539a\u7684\u5174\u8da3\u3002
\u4e24\u773c\u4e00\u62b9\u9ed1\u5730\u5237\u9898\u4f3c\u4e4e\u662f\u6700\u53d7\u6b22\u8fce\u7684\u65b9\u6cd5\uff0c\u7b80\u5355\u76f4\u63a5\u4e14\u6709\u6548\u3002\u7136\u800c\u5237\u9898\u5c31\u5982\u540c\u73a9\u201c\u626b\u96f7\u201d\u6e38\u620f\uff0c\u81ea\u5b66\u80fd\u529b\u5f3a\u7684\u540c\u5b66\u80fd\u591f\u987a\u5229\u5c06\u5730\u96f7\u9010\u4e2a\u6392\u6389\uff0c\u800c\u57fa\u7840\u4e0d\u8db3\u7684\u540c\u5b66\u5f88\u53ef\u80fd\u88ab\u70b8\u7684\u6ee1\u5934\u662f\u5305\uff0c\u5e76\u5728\u632b\u6298\u4e2d\u6b65\u6b65\u9000\u7f29\u3002\u901a\u8bfb\u6559\u6750\u4e5f\u662f\u4e00\u79cd\u5e38\u89c1\u505a\u6cd5\uff0c\u4f46\u5bf9\u4e8e\u9762\u5411\u6c42\u804c\u7684\u540c\u5b66\u6765\u8bf4\uff0c\u6bd5\u4e1a\u5b63\u3001\u6295\u9012\u7b80\u5386\u3001\u51c6\u5907\u7b14\u8bd5\u9762\u8bd5\u5df2\u7ecf\u6d88\u8017\u4e86\u5927\u90e8\u5206\u7cbe\u529b\uff0c\u5543\u539a\u91cd\u7684\u4e66\u5f80\u5f80\u53d8\u6210\u4e86\u4e00\u9879\u8270\u5de8\u7684\u6311\u6218\u3002
\u5982\u679c\u4f60\u4e5f\u9762\u4e34\u7c7b\u4f3c\u7684\u56f0\u6270\uff0c\u90a3\u4e48\u5f88\u5e78\u8fd0\u8fd9\u672c\u4e66\u627e\u5230\u4e86\u4f60\u3002\u672c\u4e66\u662f\u6211\u5bf9\u8fd9\u4e2a\u95ee\u9898\u7ed9\u51fa\u7684\u7b54\u6848\uff0c\u5373\u4f7f\u4e0d\u662f\u6700\u4f18\u89e3\uff0c\u4e5f\u81f3\u5c11\u662f\u4e00\u6b21\u79ef\u6781\u7684\u5c1d\u8bd5\u3002\u672c\u4e66\u867d\u7136\u4e0d\u8db3\u4ee5\u8ba9\u4f60\u76f4\u63a5\u62ff\u5230 Offer \uff0c\u4f46\u4f1a\u5f15\u5bfc\u4f60\u63a2\u7d22\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7684\u201c\u77e5\u8bc6\u5730\u56fe\u201d\uff0c\u5e26\u4f60\u4e86\u89e3\u4e0d\u540c\u201c\u5730\u96f7\u201d\u7684\u5f62\u72b6\u3001\u5927\u5c0f\u548c\u5206\u5e03\u4f4d\u7f6e\uff0c\u8ba9\u4f60\u638c\u63e1\u5404\u79cd\u201c\u6392\u96f7\u65b9\u6cd5\u201d\u3002\u6709\u4e86\u8fd9\u4e9b\u672c\u9886\uff0c\u76f8\u4fe1\u4f60\u53ef\u4ee5\u66f4\u52a0\u81ea\u5982\u5730\u5237\u9898\u548c\u9605\u8bfb\u6587\u732e\uff0c\u9010\u6b65\u6784\u5efa\u8d77\u5b8c\u6574\u7684\u77e5\u8bc6\u4f53\u7cfb\u3002
\u6211\u6df1\u6df1\u8d5e\u540c\u8d39\u66fc\u6559\u6388\u6240\u8a00\uff1a\u201cKnowledge isn't free. You have to pay attention.\u201d\u4ece\u8fd9\u4e2a\u610f\u4e49\u4e0a\u770b\uff0c\u8fd9\u672c\u4e66\u5e76\u975e\u5b8c\u5168\u201c\u514d\u8d39\u201d\u3002\u4e3a\u4e86\u4e0d\u8f9c\u8d1f\u4f60\u4e3a\u672c\u4e66\u6240\u4ed8\u51fa\u7684\u5b9d\u8d35\u201c\u6ce8\u610f\u529b\u201d\uff0c\u6211\u4f1a\u5c3d\u6211\u6240\u80fd\uff0c\u6295\u5165\u6700\u5927\u7684\u201c\u6ce8\u610f\u529b\u201d\u6765\u5b8c\u6210\u8fd9\u672c\u4e66\u7684\u521b\u4f5c\u3002
\u4f5c\u8005\u7b80\u4ecb\u9773\u5b87\u680b (Krahets)\uff0c\u5927\u5382\u9ad8\u7ea7\u7b97\u6cd5\u5de5\u7a0b\u5e08\uff0c\u4e0a\u6d77\u4ea4\u901a\u5927\u5b66\u7855\u58eb\u3002\u529b\u6263\uff08LeetCode\uff09\u5168\u7f51\u9605\u8bfb\u91cf\u6700\u9ad8\u535a\u4e3b\uff0c\u53d1\u8868\u7684\u300a\u56fe\u89e3\u7b97\u6cd5\u6570\u636e\u7ed3\u6784\u300b\u5df2\u88ab\u8ba2\u9605 30 \u4e07\u672c\u3002
\u81f4\u8c22\u672c\u4e66\u5728\u5f00\u6e90\u793e\u533a\u4f17\u591a\u8d21\u732e\u8005\u7684\u5171\u540c\u52aa\u529b\u4e0b\u4e0d\u65ad\u5b8c\u5584\u3002\u611f\u8c22\u6bcf\u4e00\u4f4d\u6295\u5165\u65f6\u95f4\u4e0e\u7cbe\u529b\u7684\u64b0\u7a3f\u4eba\uff0c\u4ed6\u4eec\u662f\uff08\u6309\u7167 GitHub \u81ea\u52a8\u751f\u6210\u7684\u987a\u5e8f\u6392\u5217\uff09\uff1a
\u672c\u4e66\u7684\u4ee3\u7801\u5ba1\u9605\u5de5\u4f5c\u7531 Gonglja\u3001gvenusleo\u3001hpstory\u3001justin\u2010tse\u3001krahets\u3001night-cruise\u3001nuomi1\u3001Reanon \u548c sjinzh \u5b8c\u6210\uff08\u6309\u7167\u9996\u5b57\u6bcd\u987a\u5e8f\u6392\u5217\uff09\u3002\u611f\u8c22\u4ed6\u4eec\u4ed8\u51fa\u7684\u65f6\u95f4\u4e0e\u7cbe\u529b\uff0c\u6b63\u662f\u4ed6\u4eec\u786e\u4fdd\u4e86\u5404\u8bed\u8a00\u4ee3\u7801\u7684\u89c4\u8303\u4e0e\u7edf\u4e00\u3002
GongljaC, C++ gvenusleoDart hpstoryC# justin-tseJS, TS krahetsJava, Python night-cruiseRust nuomi1Swift ReanonGo, C sjinzhRust, Zig"},{"location":"chapter_appendix/","title":"\u7b2c 16 \u7ae0 \u00a0 \u9644\u5f55","text":""},{"location":"chapter_appendix/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"\u7531\u4e8e\u7b14\u8005\u80fd\u529b\u6709\u9650\uff0c\u4e66\u4e2d\u96be\u514d\u5b58\u5728\u4e00\u4e9b\u9057\u6f0f\u548c\u9519\u8bef\uff0c\u8bf7\u60a8\u8c05\u89e3\u3002\u5982\u679c\u60a8\u53d1\u73b0\u4e86\u7b14\u8bef\u3001\u94fe\u63a5\u5931\u6548\u3001\u5185\u5bb9\u7f3a\u5931\u3001\u6587\u5b57\u6b67\u4e49\u3001\u89e3\u91ca\u4e0d\u6e05\u6670\u6216\u884c\u6587\u7ed3\u6784\u4e0d\u5408\u7406\u7b49\u95ee\u9898\uff0c\u8bf7\u534f\u52a9\u6211\u4eec\u8fdb\u884c\u4fee\u6b63\uff0c\u4ee5\u7ed9\u8bfb\u8005\u63d0\u4f9b\u66f4\u4f18\u8d28\u7684\u5b66\u4e60\u8d44\u6e90\u3002
\u6240\u6709\u64b0\u7a3f\u4eba\u7684 GitHub ID \u5c06\u5728\u672c\u4e66\u4ed3\u5e93\u3001\u7f51\u9875\u7248\u548c PDF \u7248\u7684\u4e3b\u9875\u4e0a\u8fdb\u884c\u5c55\u793a\uff0c\u4ee5\u611f\u8c22\u4ed6\u4eec\u5bf9\u5f00\u6e90\u793e\u533a\u7684\u65e0\u79c1\u5949\u732e\u3002
\u5f00\u6e90\u7684\u9b45\u529b
\u7eb8\u8d28\u56fe\u4e66\u7684\u4e24\u6b21\u5370\u5237\u7684\u95f4\u9694\u65f6\u95f4\u5f80\u5f80\u8f83\u4e45\uff0c\u5185\u5bb9\u66f4\u65b0\u975e\u5e38\u4e0d\u65b9\u4fbf\u3002
\u800c\u5728\u672c\u5f00\u6e90\u4e66\u4e2d\uff0c\u5185\u5bb9\u66f4\u8fed\u7684\u65f6\u95f4\u88ab\u7f29\u77ed\u81f3\u6570\u65e5\u751a\u81f3\u51e0\u4e2a\u5c0f\u65f6\u3002
"},{"location":"chapter_appendix/contribution/#1","title":"1. \u00a0 \u5185\u5bb9\u5fae\u8c03","text":"\u5982\u56fe 16-3 \u6240\u793a\uff0c\u6bcf\u4e2a\u9875\u9762\u7684\u53f3\u4e0a\u89d2\u90fd\u6709\u201c\u7f16\u8f91\u56fe\u6807\u201d\u3002\u60a8\u53ef\u4ee5\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u4fee\u6539\u6587\u672c\u6216\u4ee3\u7801\u3002
\u56fe 16-3 \u00a0 \u9875\u9762\u7f16\u8f91\u6309\u952e
\u56fe\u7247\u65e0\u6cd5\u76f4\u63a5\u4fee\u6539\uff0c\u9700\u8981\u901a\u8fc7\u65b0\u5efa Issue \u6216\u8bc4\u8bba\u7559\u8a00\u6765\u63cf\u8ff0\u95ee\u9898\uff0c\u6211\u4eec\u4f1a\u5c3d\u5feb\u91cd\u65b0\u7ed8\u5236\u5e76\u66ff\u6362\u56fe\u7247\u3002
"},{"location":"chapter_appendix/contribution/#2","title":"2. \u00a0 \u5185\u5bb9\u521b\u4f5c","text":"\u5982\u679c\u60a8\u6709\u5174\u8da3\u53c2\u4e0e\u6b64\u5f00\u6e90\u9879\u76ee\uff0c\u5305\u62ec\u5c06\u4ee3\u7801\u7ffb\u8bd1\u6210\u5176\u4ed6\u7f16\u7a0b\u8bed\u8a00\u3001\u6269\u5c55\u6587\u7ae0\u5185\u5bb9\u7b49\uff0c\u90a3\u4e48\u9700\u8981\u5b9e\u65bd\u4ee5\u4e0b Pull Request \u5de5\u4f5c\u6d41\u7a0b\u3002
git clone
\u547d\u4ee4\u5c06\u4ed3\u5e93\u514b\u9686\u81f3\u672c\u5730\u3002\u5728 hello-algo
\u6839\u76ee\u5f55\u4e0b\uff0c\u6267\u884c\u4ee5\u4e0b Docker \u811a\u672c\uff0c\u5373\u53ef\u5728 http://localhost:8000
\u8bbf\u95ee\u672c\u9879\u76ee\uff1a
docker-compose up -d\n
\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5373\u53ef\u5220\u9664\u90e8\u7f72\uff1a
docker-compose down\n
"},{"location":"chapter_appendix/installation/","title":"16.1 \u00a0 \u7f16\u7a0b\u73af\u5883\u5b89\u88c5","text":""},{"location":"chapter_appendix/installation/#1611-ide","title":"16.1.1 \u00a0 \u5b89\u88c5 IDE","text":"\u63a8\u8350\u4f7f\u7528\u5f00\u6e90\u3001\u8f7b\u91cf\u7684 VSCode \u4f5c\u4e3a\u672c\u5730\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09\u3002\u8bbf\u95ee VSCode \u5b98\u7f51\uff0c\u6839\u636e\u64cd\u4f5c\u7cfb\u7edf\u9009\u62e9\u76f8\u5e94\u7248\u672c\u7684 VSCode \u8fdb\u884c\u4e0b\u8f7d\u548c\u5b89\u88c5\u3002
\u56fe 16-1 \u00a0 \u4ece\u5b98\u7f51\u4e0b\u8f7d VSCode
VSCode \u62e5\u6709\u5f3a\u5927\u7684\u6269\u5c55\u5305\u751f\u6001\u7cfb\u7edf\uff0c\u652f\u6301\u5927\u591a\u6570\u7f16\u7a0b\u8bed\u8a00\u7684\u8fd0\u884c\u548c\u8c03\u8bd5\u3002\u4ee5 Python \u4e3a\u4f8b\uff0c\u5b89\u88c5\u201cPython Extension Pack\u201d\u6269\u5c55\u5305\u4e4b\u540e\uff0c\u5373\u53ef\u8fdb\u884c Python \u4ee3\u7801\u8c03\u8bd5\u3002\u5b89\u88c5\u6b65\u9aa4\u5982\u56fe 16-2 \u6240\u793a\u3002
\u56fe 16-2 \u00a0 \u5b89\u88c5 VSCode \u6269\u5c55\u5305
"},{"location":"chapter_appendix/installation/#1612","title":"16.1.2 \u00a0 \u5b89\u88c5\u8bed\u8a00\u73af\u5883","text":""},{"location":"chapter_appendix/installation/#1-python","title":"1. \u00a0 Python \u73af\u5883","text":"python
\uff0c\u5b89\u88c5 Python Extension Pack \u3002pip install black
\uff0c\u5b89\u88c5\u4ee3\u7801\u683c\u5f0f\u5316\u5de5\u5177\u3002c++
\uff0c\u5b89\u88c5 C/C++ Extension Pack \u3002Clang_format_fallback Style
\u4ee3\u7801\u683c\u5f0f\u5316\u9009\u9879\uff0c\u8bbe\u7f6e\u4e3a { BasedOnStyle: Microsoft, BreakBeforeBraces: Attach }
\u3002java
\uff0c\u5b89\u88c5 Extension Pack for Java \u3002C# Dev Kit
\uff0c\u5b89\u88c5 C# Dev Kit \uff08\u914d\u7f6e\u6559\u7a0b\uff09\u3002go
\uff0c\u5b89\u88c5 Go \u3002Ctrl + Shift + P
\u547c\u51fa\u547d\u4ee4\u680f\uff0c\u8f93\u5165 go \uff0c\u9009\u62e9 Go: Install/Update Tools
\uff0c\u5168\u90e8\u52fe\u9009\u5e76\u5b89\u88c5\u5373\u53ef\u3002swift
\uff0c\u5b89\u88c5 Swift for Visual Studio Code \u3002javascript
\uff0c\u5b89\u88c5 JavaScript (ES6) code snippets \u3002Prettier
\uff0c\u5b89\u88c5\u4ee3\u7801\u683c\u5f0f\u5316\u5de5\u5177\u3002dart
\uff0c\u5b89\u88c5 Dart \u3002rust
\uff0c\u5b89\u88c5 rust-analyzer \u3002\u8868 16-1 \u5217\u51fa\u4e86\u4e66\u4e2d\u51fa\u73b0\u7684\u91cd\u8981\u672f\u8bed\u3002\u5efa\u8bae\u8bfb\u8005\u540c\u65f6\u8bb0\u4f4f\u5b83\u4eec\u7684\u4e2d\u82f1\u6587\u53eb\u6cd5\uff0c\u4ee5\u4fbf\u9605\u8bfb\u82f1\u6587\u6587\u732e\u3002
\u8868 16-1 \u00a0 \u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7684\u91cd\u8981\u540d\u8bcd
\u4e2d\u6587 English \u4e2d\u6587 English \u7b97\u6cd5 algorithm \u5c42\u5e8f\u904d\u5386 level-order traversal \u6570\u636e\u7ed3\u6784 data structure \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 breadth-first traversal \u6e10\u8fd1\u590d\u6742\u5ea6\u5206\u6790 asymptotic complexity analysis \u6df1\u5ea6\u4f18\u5148\u904d\u5386 depth-first traversal \u65f6\u95f4\u590d\u6742\u5ea6 time complexity \u4e8c\u53c9\u641c\u7d22\u6811 binary search tree \u7a7a\u95f4\u590d\u6742\u5ea6 space complexity \u5e73\u8861\u4e8c\u53c9\u641c\u7d22\u6811 balanced binary search tree \u8fed\u4ee3 iteration \u5e73\u8861\u56e0\u5b50 balance factor \u9012\u5f52 recursion \u5806 heap \u5c3e\u9012\u5f52 tail recursion \u5927\u9876\u5806 max heap \u9012\u5f52\u6811 recursion tree \u5c0f\u9876\u5806 min heap \u5927 big- \u4f18\u5148\u961f\u5217 priority queue \u8bb0\u53f7 notation \u6e10\u8fd1\u4e0a\u754c asymptotic upper bound \u5806\u5316 heapify \u539f\u7801 sign-magnitude \u56fe graph \u53cd\u7801 1\u2019s complement \u9876\u70b9 vertex \u8865\u7801 2\u2019s complement \u65e0\u5411\u56fe undirected graph \u6570\u7ec4 array \u6709\u5411\u56fe directed graph \u7d22\u5f15 index \u8fde\u901a\u56fe connected graph \u94fe\u8868 linked list \u975e\u8fde\u901a\u56fe disconnected graph \u94fe\u8868\u8282\u70b9 linked list node, list node \u6709\u6743\u56fe weighted graph \u5217\u8868 list \u90bb\u63a5 adjacency \u52a8\u6001\u6570\u7ec4 dynamic array \u8def\u5f84 path \u786c\u76d8 hard disk \u5165\u5ea6 in-degree \u5185\u5b58 random-access memory (RAM) \u51fa\u5ea6 out-degree \u7f13\u5b58 cache memory \u90bb\u63a5\u77e9\u9635 adjacency matrix \u7f13\u5b58\u672a\u547d\u4e2d cache miss \u90bb\u63a5\u8868 adjacency list \u7f13\u5b58\u547d\u4e2d\u7387 cache hit rate \u5e7f\u5ea6\u4f18\u5148\u641c\u7d22 breadth-first search \u6808 stack \u6df1\u5ea6\u4f18\u5148\u641c\u7d22 depth-first search \u961f\u5217 queue \u4e8c\u5206\u67e5\u627e binary search \u53cc\u5411\u961f\u5217 double-ended queue \u641c\u7d22\u7b97\u6cd5 searching algorithm \u54c8\u5e0c\u8868 hash table \u6392\u5e8f\u7b97\u6cd5 sorting algorithm \u6876 bucket \u9009\u62e9\u6392\u5e8f selection sort \u54c8\u5e0c\u51fd\u6570 hash function \u5192\u6ce1\u6392\u5e8f bubble sort \u54c8\u5e0c\u51b2\u7a81 hash collision \u63d2\u5165\u6392\u5e8f insertion sort \u8d1f\u8f7d\u56e0\u5b50 load factor \u5feb\u901f\u6392\u5e8f quick sort \u94fe\u5f0f\u5730\u5740 separate chaining \u5f52\u5e76\u6392\u5e8f merge sort \u5f00\u653e\u5bfb\u5740 open addressing \u5806\u6392\u5e8f heap sort \u7ebf\u6027\u63a2\u6d4b linear probing \u6876\u6392\u5e8f bucket sort \u61d2\u5220\u9664 lazy deletion \u8ba1\u6570\u6392\u5e8f counting sort \u4e8c\u53c9\u6811 binary tree \u57fa\u6570\u6392\u5e8f radix sort \u6811\u8282\u70b9 tree node \u5206\u6cbb divide and conquer \u5de6\u5b50\u8282\u70b9 left-child node \u6c49\u8bfa\u5854\u95ee\u9898 hanota problem \u53f3\u5b50\u8282\u70b9 right-child node \u56de\u6eaf\u7b97\u6cd5 backtracking algorithm \u7236\u8282\u70b9 parent node \u7ea6\u675f constraint \u5de6\u5b50\u6811 left subtree \u89e3 solution \u53f3\u5b50\u6811 right subtree \u72b6\u6001 state \u6839\u8282\u70b9 root node \u526a\u679d pruning \u53f6\u8282\u70b9 leaf node \u5168\u6392\u5217\u95ee\u9898 permutations problem \u8fb9 edge \u5b50\u96c6\u548c\u95ee\u9898 subset-sum problem \u5c42 level N \u7687\u540e\u95ee\u9898 N-queens problem \u5ea6 degree \u52a8\u6001\u89c4\u5212 dynamic programming \u9ad8\u5ea6 height \u521d\u59cb\u72b6\u6001 initial state \u6df1\u5ea6 depth \u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b state-trasition equation \u5b8c\u7f8e\u4e8c\u53c9\u6811 perfect binary tree \u80cc\u5305\u95ee\u9898 knapsack problem \u5b8c\u5168\u4e8c\u53c9\u6811 complete binary tree \u7f16\u8f91\u8ddd\u79bb\u95ee\u9898 edit distance problem \u5b8c\u6ee1\u4e8c\u53c9\u6811 full binary tree \u8d2a\u5fc3\u7b97\u6cd5 greedy algorithm \u5e73\u8861\u4e8c\u53c9\u6811 balanced binary tree AVL \u6811 AVL tree \u7ea2\u9ed1\u6811 red-black tree"},{"location":"chapter_array_and_linkedlist/","title":"\u7b2c 4 \u7ae0 \u00a0 \u6570\u7ec4\u4e0e\u94fe\u8868","text":"Abstract
\u6570\u636e\u7ed3\u6784\u7684\u4e16\u754c\u5982\u540c\u4e00\u5835\u539a\u5b9e\u7684\u7816\u5899\u3002
\u6570\u7ec4\u7684\u7816\u5757\u6574\u9f50\u6392\u5217\uff0c\u9010\u4e2a\u7d27\u8d34\u3002\u94fe\u8868\u7684\u7816\u5757\u5206\u6563\u5404\u5904\uff0c\u8fde\u63a5\u7684\u85e4\u8513\u81ea\u7531\u5730\u7a7f\u68ad\u4e8e\u7816\u7f1d\u4e4b\u95f4\u3002
"},{"location":"chapter_array_and_linkedlist/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"\u300c\u6570\u7ec4 array\u300d\u662f\u4e00\u79cd\u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff0c\u5176\u5c06\u76f8\u540c\u7c7b\u578b\u7684\u5143\u7d20\u5b58\u50a8\u5728\u8fde\u7eed\u7684\u5185\u5b58\u7a7a\u95f4\u4e2d\u3002\u6211\u4eec\u5c06\u5143\u7d20\u5728\u6570\u7ec4\u4e2d\u7684\u4f4d\u7f6e\u79f0\u4e3a\u8be5\u5143\u7d20\u7684\u300c\u7d22\u5f15 index\u300d\u3002\u56fe 4-1 \u5c55\u793a\u4e86\u6570\u7ec4\u7684\u4e3b\u8981\u672f\u8bed\u548c\u6982\u5ff5\u3002
\u56fe 4-1 \u00a0 \u6570\u7ec4\u5b9a\u4e49\u4e0e\u5b58\u50a8\u65b9\u5f0f
"},{"location":"chapter_array_and_linkedlist/array/#411","title":"4.1.1 \u00a0 \u6570\u7ec4\u5e38\u7528\u64cd\u4f5c","text":""},{"location":"chapter_array_and_linkedlist/array/#1","title":"1. \u00a0 \u521d\u59cb\u5316\u6570\u7ec4","text":"\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u9009\u7528\u6570\u7ec4\u7684\u4e24\u79cd\u521d\u59cb\u5316\u65b9\u5f0f\uff1a\u65e0\u521d\u59cb\u503c\u3001\u7ed9\u5b9a\u521d\u59cb\u503c\u3002\u5728\u672a\u6307\u5b9a\u521d\u59cb\u503c\u7684\u60c5\u51b5\u4e0b\uff0c\u5927\u591a\u6570\u7f16\u7a0b\u8bed\u8a00\u4f1a\u5c06\u6570\u7ec4\u5143\u7d20\u521d\u59cb\u5316\u4e3a \\(0\\) \uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig array.py# \u521d\u59cb\u5316\u6570\u7ec4\narr: list[int] = [0] * 5 # [ 0, 0, 0, 0, 0 ]\nnums: list[int] = [1, 3, 2, 5, 4] \n
array.cpp/* \u521d\u59cb\u5316\u6570\u7ec4 */\n// \u5b58\u50a8\u5728\u6808\u4e0a\nint arr[5];\nint nums[5] = { 1, 3, 2, 5, 4 };\n// \u5b58\u50a8\u5728\u5806\u4e0a\uff08\u9700\u8981\u624b\u52a8\u91ca\u653e\u7a7a\u95f4\uff09\nint* arr1 = new int[5];\nint* nums1 = new int[5] { 1, 3, 2, 5, 4 };\n
array.java/* \u521d\u59cb\u5316\u6570\u7ec4 */\nint[] arr = new int[5]; // { 0, 0, 0, 0, 0 }\nint[] nums = { 1, 3, 2, 5, 4 };\n
array.cs/* \u521d\u59cb\u5316\u6570\u7ec4 */\nint[] arr = new int[5]; // { 0, 0, 0, 0, 0 }\nint[] nums = [1, 3, 2, 5, 4];\n
array.go/* \u521d\u59cb\u5316\u6570\u7ec4 */\nvar arr [5]int\n// \u5728 Go \u4e2d\uff0c\u6307\u5b9a\u957f\u5ea6\u65f6\uff08[5]int\uff09\u4e3a\u6570\u7ec4\uff0c\u4e0d\u6307\u5b9a\u957f\u5ea6\u65f6\uff08[]int\uff09\u4e3a\u5207\u7247\n// \u7531\u4e8e Go \u7684\u6570\u7ec4\u88ab\u8bbe\u8ba1\u4e3a\u5728\u7f16\u8bd1\u671f\u786e\u5b9a\u957f\u5ea6\uff0c\u56e0\u6b64\u53ea\u80fd\u4f7f\u7528\u5e38\u91cf\u6765\u6307\u5b9a\u957f\u5ea6\n// \u4e3a\u4e86\u65b9\u4fbf\u5b9e\u73b0\u6269\u5bb9 extend() \u65b9\u6cd5\uff0c\u4ee5\u4e0b\u5c06\u5207\u7247\uff08Slice\uff09\u770b\u4f5c\u6570\u7ec4\uff08Array\uff09\nnums := []int{1, 3, 2, 5, 4}\n
array.swift/* \u521d\u59cb\u5316\u6570\u7ec4 */\nlet arr = Array(repeating: 0, count: 5) // [0, 0, 0, 0, 0]\nlet nums = [1, 3, 2, 5, 4]\n
array.js/* \u521d\u59cb\u5316\u6570\u7ec4 */\nvar arr = new Array(5).fill(0);\nvar nums = [1, 3, 2, 5, 4];\n
array.ts/* \u521d\u59cb\u5316\u6570\u7ec4 */\nlet arr: number[] = new Array(5).fill(0);\nlet nums: number[] = [1, 3, 2, 5, 4];\n
array.dart/* \u521d\u59cb\u5316\u6570\u7ec4 */\nList<int> arr = List.filled(5, 0); // [0, 0, 0, 0, 0]\nList<int> nums = [1, 3, 2, 5, 4];\n
array.rs/* \u521d\u59cb\u5316\u6570\u7ec4 */\nlet arr: Vec<i32> = vec![0; 5]; // [0, 0, 0, 0, 0]\nlet nums: Vec<i32> = vec![1, 3, 2, 5, 4];\n
array.c/* \u521d\u59cb\u5316\u6570\u7ec4 */\nint arr[5] = { 0 }; // { 0, 0, 0, 0, 0 }\nint nums[5] = { 1, 3, 2, 5, 4 };\n
array.zig// \u521d\u59cb\u5316\u6570\u7ec4\nvar arr = [_]i32{0} ** 5; // { 0, 0, 0, 0, 0 }\nvar nums = [_]i32{ 1, 3, 2, 5, 4 };\n
"},{"location":"chapter_array_and_linkedlist/array/#2","title":"2. \u00a0 \u8bbf\u95ee\u5143\u7d20","text":"\u6570\u7ec4\u5143\u7d20\u88ab\u5b58\u50a8\u5728\u8fde\u7eed\u7684\u5185\u5b58\u7a7a\u95f4\u4e2d\uff0c\u8fd9\u610f\u5473\u7740\u8ba1\u7b97\u6570\u7ec4\u5143\u7d20\u7684\u5185\u5b58\u5730\u5740\u975e\u5e38\u5bb9\u6613\u3002\u7ed9\u5b9a\u6570\u7ec4\u5185\u5b58\u5730\u5740\uff08\u9996\u5143\u7d20\u5185\u5b58\u5730\u5740\uff09\u548c\u67d0\u4e2a\u5143\u7d20\u7684\u7d22\u5f15\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u56fe 4-2 \u6240\u793a\u7684\u516c\u5f0f\u8ba1\u7b97\u5f97\u5230\u8be5\u5143\u7d20\u7684\u5185\u5b58\u5730\u5740\uff0c\u4ece\u800c\u76f4\u63a5\u8bbf\u95ee\u8be5\u5143\u7d20\u3002
\u56fe 4-2 \u00a0 \u6570\u7ec4\u5143\u7d20\u7684\u5185\u5b58\u5730\u5740\u8ba1\u7b97
\u89c2\u5bdf\u56fe 4-2 \uff0c\u6211\u4eec\u53d1\u73b0\u6570\u7ec4\u9996\u4e2a\u5143\u7d20\u7684\u7d22\u5f15\u4e3a \\(0\\) \uff0c\u8fd9\u4f3c\u4e4e\u6709\u4e9b\u53cd\u76f4\u89c9\uff0c\u56e0\u4e3a\u4ece \\(1\\) \u5f00\u59cb\u8ba1\u6570\u4f1a\u66f4\u81ea\u7136\u3002\u4f46\u4ece\u5730\u5740\u8ba1\u7b97\u516c\u5f0f\u7684\u89d2\u5ea6\u770b\uff0c\u7d22\u5f15\u672c\u8d28\u4e0a\u662f\u5185\u5b58\u5730\u5740\u7684\u504f\u79fb\u91cf\u3002\u9996\u4e2a\u5143\u7d20\u7684\u5730\u5740\u504f\u79fb\u91cf\u662f \\(0\\) \uff0c\u56e0\u6b64\u5b83\u7684\u7d22\u5f15\u4e3a \\(0\\) \u662f\u5408\u7406\u7684\u3002
\u5728\u6570\u7ec4\u4e2d\u8bbf\u95ee\u5143\u7d20\u975e\u5e38\u9ad8\u6548\uff0c\u6211\u4eec\u53ef\u4ee5\u5728 \\(O(1)\\) \u65f6\u95f4\u5185\u968f\u673a\u8bbf\u95ee\u6570\u7ec4\u4e2d\u7684\u4efb\u610f\u4e00\u4e2a\u5143\u7d20\u3002
PythonC++JavaC#GoSwiftJSTSDartRustCZig array.pydef random_access(nums: list[int]) -> int:\n \"\"\"\u968f\u673a\u8bbf\u95ee\u5143\u7d20\"\"\"\n # \u5728\u533a\u95f4 [0, len(nums)-1] \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\n random_index = random.randint(0, len(nums) - 1)\n # \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\n random_num = nums[random_index]\n return random_num\n
array.cpp/* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nint randomAccess(int *nums, int size) {\n // \u5728\u533a\u95f4 [0, size) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\n int randomIndex = rand() % size;\n // \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\n int randomNum = nums[randomIndex];\n return randomNum;\n}\n
array.java/* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nint randomAccess(int[] nums) {\n // \u5728\u533a\u95f4 [0, nums.length) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\n int randomIndex = ThreadLocalRandom.current().nextInt(0, nums.length);\n // \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\n int randomNum = nums[randomIndex];\n return randomNum;\n}\n
array.cs/* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nint RandomAccess(int[] nums) {\n Random random = new();\n // \u5728\u533a\u95f4 [0, nums.Length) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\n int randomIndex = random.Next(nums.Length);\n // \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\n int randomNum = nums[randomIndex];\n return randomNum;\n}\n
array.go/* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nfunc randomAccess(nums []int) (randomNum int) {\n // \u5728\u533a\u95f4 [0, nums.length) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\n randomIndex := rand.Intn(len(nums))\n // \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\n randomNum = nums[randomIndex]\n return\n}\n
array.swift/* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nfunc randomAccess(nums: [Int]) -> Int {\n // \u5728\u533a\u95f4 [0, nums.count) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\n let randomIndex = nums.indices.randomElement()!\n // \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\n let randomNum = nums[randomIndex]\n return randomNum\n}\n
array.js/* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nfunction randomAccess(nums) {\n // \u5728\u533a\u95f4 [0, nums.length) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\n const random_index = Math.floor(Math.random() * nums.length);\n // \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\n const random_num = nums[random_index];\n return random_num;\n}\n
array.ts/* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nfunction randomAccess(nums: number[]): number {\n // \u5728\u533a\u95f4 [0, nums.length) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\n const random_index = Math.floor(Math.random() * nums.length);\n // \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\n const random_num = nums[random_index];\n return random_num;\n}\n
array.dart/* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nint randomAccess(List<int> nums) {\n // \u5728\u533a\u95f4 [0, nums.length) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\n int randomIndex = Random().nextInt(nums.length);\n // \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\n int randomNum = nums[randomIndex];\n return randomNum;\n}\n
array.rs/* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nfn random_access(nums: &[i32]) -> i32 {\n // \u5728\u533a\u95f4 [0, nums.len()) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\n let random_index = rand::thread_rng().gen_range(0..nums.len());\n // \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\n let random_num = nums[random_index];\n random_num\n}\n
array.c/* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nint randomAccess(int *nums, int size) {\n // \u5728\u533a\u95f4 [0, size) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\n int randomIndex = rand() % size;\n // \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\n int randomNum = nums[randomIndex];\n return randomNum;\n}\n
array.zig// \u968f\u673a\u8bbf\u95ee\u5143\u7d20\nfn randomAccess(nums: []i32) i32 {\n // \u5728\u533a\u95f4 [0, nums.len) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6574\u6570\n var randomIndex = std.crypto.random.intRangeLessThan(usize, 0, nums.len);\n // \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\n var randomNum = nums[randomIndex];\n return randomNum;\n}\n
"},{"location":"chapter_array_and_linkedlist/array/#3","title":"3. \u00a0 \u63d2\u5165\u5143\u7d20","text":"\u6570\u7ec4\u5143\u7d20\u5728\u5185\u5b58\u4e2d\u662f\u201c\u7d27\u6328\u7740\u7684\u201d\uff0c\u5b83\u4eec\u4e4b\u95f4\u6ca1\u6709\u7a7a\u95f4\u518d\u5b58\u653e\u4efb\u4f55\u6570\u636e\u3002\u5982\u56fe 4-3 \u6240\u793a\uff0c\u5982\u679c\u60f3\u5728\u6570\u7ec4\u4e2d\u95f4\u63d2\u5165\u4e00\u4e2a\u5143\u7d20\uff0c\u5219\u9700\u8981\u5c06\u8be5\u5143\u7d20\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u4e4b\u540e\u518d\u628a\u5143\u7d20\u8d4b\u503c\u7ed9\u8be5\u7d22\u5f15\u3002
\u56fe 4-3 \u00a0 \u6570\u7ec4\u63d2\u5165\u5143\u7d20\u793a\u4f8b
\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u7531\u4e8e\u6570\u7ec4\u7684\u957f\u5ea6\u662f\u56fa\u5b9a\u7684\uff0c\u56e0\u6b64\u63d2\u5165\u4e00\u4e2a\u5143\u7d20\u5fc5\u5b9a\u4f1a\u5bfc\u81f4\u6570\u7ec4\u5c3e\u90e8\u5143\u7d20\u201c\u4e22\u5931\u201d\u3002\u6211\u4eec\u5c06\u8fd9\u4e2a\u95ee\u9898\u7684\u89e3\u51b3\u65b9\u6848\u7559\u5728\u201c\u5217\u8868\u201d\u7ae0\u8282\u4e2d\u8ba8\u8bba\u3002
PythonC++JavaC#GoSwiftJSTSDartRustCZig array.pydef insert(nums: list[int], num: int, index: int):\n \"\"\"\u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num\"\"\"\n # \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for i in range(len(nums) - 1, index, -1):\n nums[i] = nums[i - 1]\n # \u5c06 num \u8d4b\u7ed9 index \u5904\u7684\u5143\u7d20\n nums[index] = num\n
array.cpp/* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num */\nvoid insert(int *nums, int size, int num, int index) {\n // \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for (int i = size - 1; i > index; i--) {\n nums[i] = nums[i - 1];\n }\n // \u5c06 num \u8d4b\u7ed9 index \u5904\u7684\u5143\u7d20\n nums[index] = num;\n}\n
array.java/* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num */\nvoid insert(int[] nums, int num, int index) {\n // \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for (int i = nums.length - 1; i > index; i--) {\n nums[i] = nums[i - 1];\n }\n // \u5c06 num \u8d4b\u7ed9 index \u5904\u7684\u5143\u7d20\n nums[index] = num;\n}\n
array.cs/* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num */\nvoid Insert(int[] nums, int num, int index) {\n // \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for (int i = nums.Length - 1; i > index; i--) {\n nums[i] = nums[i - 1];\n }\n // \u5c06 num \u8d4b\u7ed9 index \u5904\u7684\u5143\u7d20\n nums[index] = num;\n}\n
array.go/* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num */\nfunc insert(nums []int, num int, index int) {\n // \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for i := len(nums) - 1; i > index; i-- {\n nums[i] = nums[i-1]\n }\n // \u5c06 num \u8d4b\u7ed9 index \u5904\u7684\u5143\u7d20\n nums[index] = num\n}\n
array.swift/* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num */\nfunc insert(nums: inout [Int], num: Int, index: Int) {\n // \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for i in nums.indices.dropFirst(index).reversed() {\n nums[i] = nums[i - 1]\n }\n // \u5c06 num \u8d4b\u7ed9 index \u5904\u7684\u5143\u7d20\n nums[index] = num\n}\n
array.js/* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num */\nfunction insert(nums, num, index) {\n // \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for (let i = nums.length - 1; i > index; i--) {\n nums[i] = nums[i - 1];\n }\n // \u5c06 num \u8d4b\u7ed9 index \u5904\u7684\u5143\u7d20\n nums[index] = num;\n}\n
array.ts/* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num */\nfunction insert(nums: number[], num: number, index: number): void {\n // \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for (let i = nums.length - 1; i > index; i--) {\n nums[i] = nums[i - 1];\n }\n // \u5c06 num \u8d4b\u7ed9 index \u5904\u7684\u5143\u7d20\n nums[index] = num;\n}\n
array.dart/* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 _num */\nvoid insert(List<int> nums, int _num, int index) {\n // \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for (var i = nums.length - 1; i > index; i--) {\n nums[i] = nums[i - 1];\n }\n // \u5c06 _num \u8d4b\u7ed9 index \u5904\u5143\u7d20\n nums[index] = _num;\n}\n
array.rs/* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num */\nfn insert(nums: &mut Vec<i32>, num: i32, index: usize) {\n // \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for i in (index + 1..nums.len()).rev() {\n nums[i] = nums[i - 1];\n }\n // \u5c06 num \u8d4b\u7ed9 index \u5904\u7684\u5143\u7d20\n nums[index] = num;\n}\n
array.c/* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num */\nvoid insert(int *nums, int size, int num, int index) {\n // \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for (int i = size - 1; i > index; i--) {\n nums[i] = nums[i - 1];\n }\n // \u5c06 num \u8d4b\u7ed9 index \u5904\u7684\u5143\u7d20\n nums[index] = num;\n}\n
array.zig// \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num\nfn insert(nums: []i32, num: i32, index: usize) void {\n // \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n var i = nums.len - 1;\n while (i > index) : (i -= 1) {\n nums[i] = nums[i - 1];\n }\n // \u5c06 num \u8d4b\u7ed9 index \u5904\u7684\u5143\u7d20\n nums[index] = num;\n}\n
"},{"location":"chapter_array_and_linkedlist/array/#4","title":"4. \u00a0 \u5220\u9664\u5143\u7d20","text":"\u540c\u7406\uff0c\u5982\u56fe 4-4 \u6240\u793a\uff0c\u82e5\u60f3\u5220\u9664\u7d22\u5f15 \\(i\\) \u5904\u7684\u5143\u7d20\uff0c\u5219\u9700\u8981\u628a\u7d22\u5f15 \\(i\\) \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\u3002
\u56fe 4-4 \u00a0 \u6570\u7ec4\u5220\u9664\u5143\u7d20\u793a\u4f8b
\u8bf7\u6ce8\u610f\uff0c\u5220\u9664\u5143\u7d20\u5b8c\u6210\u540e\uff0c\u539f\u5148\u672b\u5c3e\u7684\u5143\u7d20\u53d8\u5f97\u201c\u65e0\u610f\u4e49\u201d\u4e86\uff0c\u6240\u4ee5\u6211\u4eec\u65e0\u987b\u7279\u610f\u53bb\u4fee\u6539\u5b83\u3002
PythonC++JavaC#GoSwiftJSTSDartRustCZig array.pydef remove(nums: list[int], index: int):\n \"\"\"\u5220\u9664\u7d22\u5f15 index \u5904\u7684\u5143\u7d20\"\"\"\n # \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for i in range(index, len(nums) - 1):\n nums[i] = nums[i + 1]\n
array.cpp/* \u5220\u9664\u7d22\u5f15 index \u5904\u7684\u5143\u7d20 */\nvoid remove(int *nums, int size, int index) {\n // \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for (int i = index; i < size - 1; i++) {\n nums[i] = nums[i + 1];\n }\n}\n
array.java/* \u5220\u9664\u7d22\u5f15 index \u5904\u7684\u5143\u7d20 */\nvoid remove(int[] nums, int index) {\n // \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for (int i = index; i < nums.length - 1; i++) {\n nums[i] = nums[i + 1];\n }\n}\n
array.cs/* \u5220\u9664\u7d22\u5f15 index \u5904\u7684\u5143\u7d20 */\nvoid Remove(int[] nums, int index) {\n // \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for (int i = index; i < nums.Length - 1; i++) {\n nums[i] = nums[i + 1];\n }\n}\n
array.go/* \u5220\u9664\u7d22\u5f15 index \u5904\u7684\u5143\u7d20 */\nfunc remove(nums []int, index int) {\n // \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for i := index; i < len(nums)-1; i++ {\n nums[i] = nums[i+1]\n }\n}\n
array.swift/* \u5220\u9664\u7d22\u5f15 index \u5904\u7684\u5143\u7d20 */\nfunc remove(nums: inout [Int], index: Int) {\n // \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for i in nums.indices.dropFirst(index).dropLast() {\n nums[i] = nums[i + 1]\n }\n}\n
array.js/* \u5220\u9664\u7d22\u5f15 index \u5904\u7684\u5143\u7d20 */\nfunction remove(nums, index) {\n // \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for (let i = index; i < nums.length - 1; i++) {\n nums[i] = nums[i + 1];\n }\n}\n
array.ts/* \u5220\u9664\u7d22\u5f15 index \u5904\u7684\u5143\u7d20 */\nfunction remove(nums: number[], index: number): void {\n // \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for (let i = index; i < nums.length - 1; i++) {\n nums[i] = nums[i + 1];\n }\n}\n
array.dart/* \u5220\u9664\u7d22\u5f15 index \u5904\u7684\u5143\u7d20 */\nvoid remove(List<int> nums, int index) {\n // \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for (var i = index; i < nums.length - 1; i++) {\n nums[i] = nums[i + 1];\n }\n}\n
array.rs/* \u5220\u9664\u7d22\u5f15 index \u5904\u7684\u5143\u7d20 */\nfn remove(nums: &mut Vec<i32>, index: usize) {\n // \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for i in index..nums.len() - 1 {\n nums[i] = nums[i + 1];\n }\n}\n
array.c/* \u5220\u9664\u7d22\u5f15 index \u5904\u7684\u5143\u7d20 */\n// \u6ce8\u610f\uff1astdio.h \u5360\u7528\u4e86 remove \u5173\u952e\u8bcd\nvoid removeItem(int *nums, int size, int index) {\n // \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for (int i = index; i < size - 1; i++) {\n nums[i] = nums[i + 1];\n }\n}\n
array.zig// \u5220\u9664\u7d22\u5f15 index \u5904\u7684\u5143\u7d20\nfn remove(nums: []i32, index: usize) void {\n // \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n var i = index;\n while (i < nums.len - 1) : (i += 1) {\n nums[i] = nums[i + 1];\n }\n}\n
\u603b\u7684\u6765\u770b\uff0c\u6570\u7ec4\u7684\u63d2\u5165\u4e0e\u5220\u9664\u64cd\u4f5c\u6709\u4ee5\u4e0b\u7f3a\u70b9\u3002
\u5728\u5927\u591a\u6570\u7f16\u7a0b\u8bed\u8a00\u4e2d\uff0c\u6211\u4eec\u65e2\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\uff0c\u4e5f\u53ef\u4ee5\u76f4\u63a5\u904d\u5386\u83b7\u53d6\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig array.pydef traverse(nums: list[int]):\n \"\"\"\u904d\u5386\u6570\u7ec4\"\"\"\n count = 0\n # \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\n for i in range(len(nums)):\n count += nums[i]\n # \u76f4\u63a5\u904d\u5386\u6570\u7ec4\u5143\u7d20\n for num in nums:\n count += num\n # \u540c\u65f6\u904d\u5386\u6570\u636e\u7d22\u5f15\u548c\u5143\u7d20\n for i, num in enumerate(nums):\n count += nums[i]\n count += num\n
array.cpp/* \u904d\u5386\u6570\u7ec4 */\nvoid traverse(int *nums, int size) {\n int count = 0;\n // \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\n for (int i = 0; i < size; i++) {\n count += nums[i];\n }\n}\n
array.java/* \u904d\u5386\u6570\u7ec4 */\nvoid traverse(int[] nums) {\n int count = 0;\n // \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\n for (int i = 0; i < nums.length; i++) {\n count += nums[i];\n }\n // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\u5143\u7d20\n for (int num : nums) {\n count += num;\n }\n}\n
array.cs/* \u904d\u5386\u6570\u7ec4 */\nvoid Traverse(int[] nums) {\n int count = 0;\n // \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\n for (int i = 0; i < nums.Length; i++) {\n count += nums[i];\n }\n // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\u5143\u7d20\n foreach (int num in nums) {\n count += num;\n }\n}\n
array.go/* \u904d\u5386\u6570\u7ec4 */\nfunc traverse(nums []int) {\n count := 0\n // \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\n for i := 0; i < len(nums); i++ {\n count += nums[i]\n }\n count = 0\n // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\u5143\u7d20\n for _, num := range nums {\n count += num\n }\n // \u540c\u65f6\u904d\u5386\u6570\u636e\u7d22\u5f15\u548c\u5143\u7d20\n for i, num := range nums {\n count += nums[i]\n count += num\n }\n}\n
array.swift/* \u904d\u5386\u6570\u7ec4 */\nfunc traverse(nums: [Int]) {\n var count = 0\n // \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\n for i in nums.indices {\n count += nums[i]\n }\n // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\u5143\u7d20\n for num in nums {\n count += num\n }\n}\n
array.js/* \u904d\u5386\u6570\u7ec4 */\nfunction traverse(nums) {\n let count = 0;\n // \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\n for (let i = 0; i < nums.length; i++) {\n count += nums[i];\n }\n // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\u5143\u7d20\n for (const num of nums) {\n count += num;\n }\n}\n
array.ts/* \u904d\u5386\u6570\u7ec4 */\nfunction traverse(nums: number[]): void {\n let count = 0;\n // \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\n for (let i = 0; i < nums.length; i++) {\n count += nums[i];\n }\n // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\u5143\u7d20\n for (const num of nums) {\n count += num;\n }\n}\n
array.dart/* \u904d\u5386\u6570\u7ec4\u5143\u7d20 */\nvoid traverse(List<int> nums) {\n int count = 0;\n // \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\n for (var i = 0; i < nums.length; i++) {\n count += nums[i];\n }\n // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\u5143\u7d20\n for (int _num in nums) {\n count += _num;\n }\n // \u901a\u8fc7 forEach \u65b9\u6cd5\u904d\u5386\u6570\u7ec4\n nums.forEach((_num) {\n count += _num;\n });\n}\n
array.rs/* \u904d\u5386\u6570\u7ec4 */\nfn traverse(nums: &[i32]) {\n let mut _count = 0;\n // \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\n for i in 0..nums.len() {\n _count += nums[i];\n }\n // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\u5143\u7d20\n for num in nums {\n _count += num;\n }\n}\n
array.c/* \u904d\u5386\u6570\u7ec4 */\nvoid traverse(int *nums, int size) {\n int count = 0;\n // \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\n for (int i = 0; i < size; i++) {\n count += nums[i];\n }\n}\n
array.zig// \u904d\u5386\u6570\u7ec4\nfn traverse(nums: []i32) void {\n var count: i32 = 0;\n // \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\n var i: i32 = 0;\n while (i < nums.len) : (i += 1) {\n count += nums[i];\n }\n count = 0;\n // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\u5143\u7d20\n for (nums) |num| {\n count += num;\n }\n}\n
"},{"location":"chapter_array_and_linkedlist/array/#6","title":"6. \u00a0 \u67e5\u627e\u5143\u7d20","text":"\u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20\u9700\u8981\u904d\u5386\u6570\u7ec4\uff0c\u6bcf\u8f6e\u5224\u65ad\u5143\u7d20\u503c\u662f\u5426\u5339\u914d\uff0c\u82e5\u5339\u914d\u5219\u8f93\u51fa\u5bf9\u5e94\u7d22\u5f15\u3002
\u56e0\u4e3a\u6570\u7ec4\u662f\u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff0c\u6240\u4ee5\u4e0a\u8ff0\u67e5\u627e\u64cd\u4f5c\u88ab\u79f0\u4e3a\u201c\u7ebf\u6027\u67e5\u627e\u201d\u3002
PythonC++JavaC#GoSwiftJSTSDartRustCZig array.pydef find(nums: list[int], target: int) -> int:\n \"\"\"\u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20\"\"\"\n for i in range(len(nums)):\n if nums[i] == target:\n return i\n return -1\n
array.cpp/* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nint find(int *nums, int size, int target) {\n for (int i = 0; i < size; i++) {\n if (nums[i] == target)\n return i;\n }\n return -1;\n}\n
array.java/* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nint find(int[] nums, int target) {\n for (int i = 0; i < nums.length; i++) {\n if (nums[i] == target)\n return i;\n }\n return -1;\n}\n
array.cs/* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nint Find(int[] nums, int target) {\n for (int i = 0; i < nums.Length; i++) {\n if (nums[i] == target)\n return i;\n }\n return -1;\n}\n
array.go/* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nfunc find(nums []int, target int) (index int) {\n index = -1\n for i := 0; i < len(nums); i++ {\n if nums[i] == target {\n index = i\n break\n }\n }\n return\n}\n
array.swift/* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nfunc find(nums: [Int], target: Int) -> Int {\n for i in nums.indices {\n if nums[i] == target {\n return i\n }\n }\n return -1\n}\n
array.js/* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nfunction find(nums, target) {\n for (let i = 0; i < nums.length; i++) {\n if (nums[i] === target) return i;\n }\n return -1;\n}\n
array.ts/* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nfunction find(nums: number[], target: number): number {\n for (let i = 0; i < nums.length; i++) {\n if (nums[i] === target) {\n return i;\n }\n }\n return -1;\n}\n
array.dart/* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nint find(List<int> nums, int target) {\n for (var i = 0; i < nums.length; i++) {\n if (nums[i] == target) return i;\n }\n return -1;\n}\n
array.rs/* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nfn find(nums: &[i32], target: i32) -> Option<usize> {\n for i in 0..nums.len() {\n if nums[i] == target {\n return Some(i);\n }\n }\n None\n}\n
array.c/* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nint find(int *nums, int size, int target) {\n for (int i = 0; i < size; i++) {\n if (nums[i] == target)\n return i;\n }\n return -1;\n}\n
array.zig// \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20\nfn find(nums: []i32, target: i32) i32 {\n for (nums, 0..) |num, i| {\n if (num == target) return @intCast(i);\n }\n return -1;\n}\n
"},{"location":"chapter_array_and_linkedlist/array/#7","title":"7. \u00a0 \u6269\u5bb9\u6570\u7ec4","text":"\u5728\u590d\u6742\u7684\u7cfb\u7edf\u73af\u5883\u4e2d\uff0c\u7a0b\u5e8f\u96be\u4ee5\u4fdd\u8bc1\u6570\u7ec4\u4e4b\u540e\u7684\u5185\u5b58\u7a7a\u95f4\u662f\u53ef\u7528\u7684\uff0c\u4ece\u800c\u65e0\u6cd5\u5b89\u5168\u5730\u6269\u5c55\u6570\u7ec4\u5bb9\u91cf\u3002\u56e0\u6b64\u5728\u5927\u591a\u6570\u7f16\u7a0b\u8bed\u8a00\u4e2d\uff0c\u6570\u7ec4\u7684\u957f\u5ea6\u662f\u4e0d\u53ef\u53d8\u7684\u3002
\u5982\u679c\u6211\u4eec\u5e0c\u671b\u6269\u5bb9\u6570\u7ec4\uff0c\u5219\u9700\u91cd\u65b0\u5efa\u7acb\u4e00\u4e2a\u66f4\u5927\u7684\u6570\u7ec4\uff0c\u7136\u540e\u628a\u539f\u6570\u7ec4\u5143\u7d20\u4f9d\u6b21\u590d\u5236\u5230\u65b0\u6570\u7ec4\u3002\u8fd9\u662f\u4e00\u4e2a \\(O(n)\\) \u7684\u64cd\u4f5c\uff0c\u5728\u6570\u7ec4\u5f88\u5927\u7684\u60c5\u51b5\u4e0b\u975e\u5e38\u8017\u65f6\u3002\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig array.pydef extend(nums: list[int], enlarge: int) -> list[int]:\n \"\"\"\u6269\u5c55\u6570\u7ec4\u957f\u5ea6\"\"\"\n # \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\n res = [0] * (len(nums) + enlarge)\n # \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\n for i in range(len(nums)):\n res[i] = nums[i]\n # \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\n return res\n
array.cpp/* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\nint *extend(int *nums, int size, int enlarge) {\n // \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\n int *res = new int[size + enlarge];\n // \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\n for (int i = 0; i < size; i++) {\n res[i] = nums[i];\n }\n // \u91ca\u653e\u5185\u5b58\n delete[] nums;\n // \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\n return res;\n}\n
array.java/* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\nint[] extend(int[] nums, int enlarge) {\n // \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\n int[] res = new int[nums.length + enlarge];\n // \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\n for (int i = 0; i < nums.length; i++) {\n res[i] = nums[i];\n }\n // \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\n return res;\n}\n
array.cs/* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\nint[] Extend(int[] nums, int enlarge) {\n // \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\n int[] res = new int[nums.Length + enlarge];\n // \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\n for (int i = 0; i < nums.Length; i++) {\n res[i] = nums[i];\n }\n // \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\n return res;\n}\n
array.go/* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\nfunc extend(nums []int, enlarge int) []int {\n // \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\n res := make([]int, len(nums)+enlarge)\n // \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\n for i, num := range nums {\n res[i] = num\n }\n // \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\n return res\n}\n
array.swift/* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\nfunc extend(nums: [Int], enlarge: Int) -> [Int] {\n // \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\n var res = Array(repeating: 0, count: nums.count + enlarge)\n // \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\n for i in nums.indices {\n res[i] = nums[i]\n }\n // \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\n return res\n}\n
array.js/* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\n// \u8bf7\u6ce8\u610f\uff0cJavaScript \u7684 Array \u662f\u52a8\u6001\u6570\u7ec4\uff0c\u53ef\u4ee5\u76f4\u63a5\u6269\u5c55\n// \u4e3a\u4e86\u65b9\u4fbf\u5b66\u4e60\uff0c\u672c\u51fd\u6570\u5c06 Array \u770b\u4f5c\u957f\u5ea6\u4e0d\u53ef\u53d8\u7684\u6570\u7ec4\nfunction extend(nums, enlarge) {\n // \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\n const res = new Array(nums.length + enlarge).fill(0);\n // \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\n for (let i = 0; i < nums.length; i++) {\n res[i] = nums[i];\n }\n // \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\n return res;\n}\n
array.ts/* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\n// \u8bf7\u6ce8\u610f\uff0cTypeScript \u7684 Array \u662f\u52a8\u6001\u6570\u7ec4\uff0c\u53ef\u4ee5\u76f4\u63a5\u6269\u5c55\n// \u4e3a\u4e86\u65b9\u4fbf\u5b66\u4e60\uff0c\u672c\u51fd\u6570\u5c06 Array \u770b\u4f5c\u957f\u5ea6\u4e0d\u53ef\u53d8\u7684\u6570\u7ec4\nfunction extend(nums: number[], enlarge: number): number[] {\n // \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\n const res = new Array(nums.length + enlarge).fill(0);\n // \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\n for (let i = 0; i < nums.length; i++) {\n res[i] = nums[i];\n }\n // \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\n return res;\n}\n
array.dart/* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\nList<int> extend(List<int> nums, int enlarge) {\n // \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\n List<int> res = List.filled(nums.length + enlarge, 0);\n // \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\n for (var i = 0; i < nums.length; i++) {\n res[i] = nums[i];\n }\n // \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\n return res;\n}\n
array.rs/* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\nfn extend(nums: Vec<i32>, enlarge: usize) -> Vec<i32> {\n // \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\n let mut res: Vec<i32> = vec![0; nums.len() + enlarge];\n // \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\n for i in 0..nums.len() {\n res[i] = nums[i];\n }\n // \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\n res\n}\n
array.c/* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\nint *extend(int *nums, int size, int enlarge) {\n // \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\n int *res = (int *)malloc(sizeof(int) * (size + enlarge));\n // \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\n for (int i = 0; i < size; i++) {\n res[i] = nums[i];\n }\n // \u521d\u59cb\u5316\u6269\u5c55\u540e\u7684\u7a7a\u95f4\n for (int i = size; i < size + enlarge; i++) {\n res[i] = 0;\n }\n // \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\n return res;\n}\n
array.zig// \u6269\u5c55\u6570\u7ec4\u957f\u5ea6\nfn extend(mem_allocator: std.mem.Allocator, nums: []i32, enlarge: usize) ![]i32 {\n // \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\n var res = try mem_allocator.alloc(i32, nums.len + enlarge);\n @memset(res, 0);\n // \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\n std.mem.copy(i32, res, nums);\n // \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\n return res;\n}\n
"},{"location":"chapter_array_and_linkedlist/array/#412","title":"4.1.2 \u00a0 \u6570\u7ec4\u7684\u4f18\u70b9\u4e0e\u5c40\u9650\u6027","text":"\u6570\u7ec4\u5b58\u50a8\u5728\u8fde\u7eed\u7684\u5185\u5b58\u7a7a\u95f4\u5185\uff0c\u4e14\u5143\u7d20\u7c7b\u578b\u76f8\u540c\u3002\u8fd9\u79cd\u505a\u6cd5\u5305\u542b\u4e30\u5bcc\u7684\u5148\u9a8c\u4fe1\u606f\uff0c\u7cfb\u7edf\u53ef\u4ee5\u5229\u7528\u8fd9\u4e9b\u4fe1\u606f\u6765\u4f18\u5316\u6570\u636e\u7ed3\u6784\u7684\u64cd\u4f5c\u6548\u7387\u3002
\u8fde\u7eed\u7a7a\u95f4\u5b58\u50a8\u662f\u4e00\u628a\u53cc\u5203\u5251\uff0c\u5176\u5b58\u5728\u4ee5\u4e0b\u5c40\u9650\u6027\u3002
\u6570\u7ec4\u662f\u4e00\u79cd\u57fa\u7840\u4e14\u5e38\u89c1\u7684\u6570\u636e\u7ed3\u6784\uff0c\u65e2\u9891\u7e41\u5e94\u7528\u5728\u5404\u7c7b\u7b97\u6cd5\u4e4b\u4e2d\uff0c\u4e5f\u53ef\u7528\u4e8e\u5b9e\u73b0\u5404\u79cd\u590d\u6742\u6570\u636e\u7ed3\u6784\u3002
\u5185\u5b58\u7a7a\u95f4\u662f\u6240\u6709\u7a0b\u5e8f\u7684\u516c\u5171\u8d44\u6e90\uff0c\u5728\u4e00\u4e2a\u590d\u6742\u7684\u7cfb\u7edf\u8fd0\u884c\u73af\u5883\u4e0b\uff0c\u7a7a\u95f2\u7684\u5185\u5b58\u7a7a\u95f4\u53ef\u80fd\u6563\u843d\u5728\u5185\u5b58\u5404\u5904\u3002\u6211\u4eec\u77e5\u9053\uff0c\u5b58\u50a8\u6570\u7ec4\u7684\u5185\u5b58\u7a7a\u95f4\u5fc5\u987b\u662f\u8fde\u7eed\u7684\uff0c\u800c\u5f53\u6570\u7ec4\u975e\u5e38\u5927\u65f6\uff0c\u5185\u5b58\u53ef\u80fd\u65e0\u6cd5\u63d0\u4f9b\u5982\u6b64\u5927\u7684\u8fde\u7eed\u7a7a\u95f4\u3002\u6b64\u65f6\u94fe\u8868\u7684\u7075\u6d3b\u6027\u4f18\u52bf\u5c31\u4f53\u73b0\u51fa\u6765\u4e86\u3002
\u300c\u94fe\u8868 linked list\u300d\u662f\u4e00\u79cd\u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff0c\u5176\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u90fd\u662f\u4e00\u4e2a\u8282\u70b9\u5bf9\u8c61\uff0c\u5404\u4e2a\u8282\u70b9\u901a\u8fc7\u201c\u5f15\u7528\u201d\u76f8\u8fde\u63a5\u3002\u5f15\u7528\u8bb0\u5f55\u4e86\u4e0b\u4e00\u4e2a\u8282\u70b9\u7684\u5185\u5b58\u5730\u5740\uff0c\u901a\u8fc7\u5b83\u53ef\u4ee5\u4ece\u5f53\u524d\u8282\u70b9\u8bbf\u95ee\u5230\u4e0b\u4e00\u4e2a\u8282\u70b9\u3002
\u94fe\u8868\u7684\u8bbe\u8ba1\u4f7f\u5f97\u5404\u4e2a\u8282\u70b9\u53ef\u4ee5\u5206\u6563\u5b58\u50a8\u5728\u5185\u5b58\u5404\u5904\uff0c\u5b83\u4eec\u7684\u5185\u5b58\u5730\u5740\u65e0\u987b\u8fde\u7eed\u3002
\u56fe 4-5 \u00a0 \u94fe\u8868\u5b9a\u4e49\u4e0e\u5b58\u50a8\u65b9\u5f0f
\u89c2\u5bdf\u56fe 4-5 \uff0c\u94fe\u8868\u7684\u7ec4\u6210\u5355\u4f4d\u662f\u300c\u8282\u70b9 node\u300d\u5bf9\u8c61\u3002\u6bcf\u4e2a\u8282\u70b9\u90fd\u5305\u542b\u4e24\u9879\u6570\u636e\uff1a\u8282\u70b9\u7684\u201c\u503c\u201d\u548c\u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u201c\u5f15\u7528\u201d\u3002
\u5982\u4ee5\u4e0b\u4ee3\u7801\u6240\u793a\uff0c\u94fe\u8868\u8282\u70b9 ListNode
\u9664\u4e86\u5305\u542b\u503c\uff0c\u8fd8\u9700\u989d\u5916\u4fdd\u5b58\u4e00\u4e2a\u5f15\u7528\uff08\u6307\u9488\uff09\u3002\u56e0\u6b64\u5728\u76f8\u540c\u6570\u636e\u91cf\u4e0b\uff0c\u94fe\u8868\u6bd4\u6570\u7ec4\u5360\u7528\u66f4\u591a\u7684\u5185\u5b58\u7a7a\u95f4\u3002
class ListNode:\n \"\"\"\u94fe\u8868\u8282\u70b9\u7c7b\"\"\"\n def __init__(self, val: int):\n self.val: int = val # \u8282\u70b9\u503c\n self.next: ListNode | None = None # \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u5f15\u7528\n
/* \u94fe\u8868\u8282\u70b9\u7ed3\u6784\u4f53 */\nstruct ListNode {\n int val; // \u8282\u70b9\u503c\n ListNode *next; // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u6307\u9488\n ListNode(int x) : val(x), next(nullptr) {} // \u6784\u9020\u51fd\u6570\n};\n
/* \u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode {\n int val; // \u8282\u70b9\u503c\n ListNode next; // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u5f15\u7528\n ListNode(int x) { val = x; } // \u6784\u9020\u51fd\u6570\n}\n
/* \u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode(int x) { //\u6784\u9020\u51fd\u6570\n int val = x; // \u8282\u70b9\u503c\n ListNode? next; // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u5f15\u7528\n}\n
/* \u94fe\u8868\u8282\u70b9\u7ed3\u6784\u4f53 */\ntype ListNode struct {\n Val int // \u8282\u70b9\u503c\n Next *ListNode // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u6307\u9488\n}\n\n// NewListNode \u6784\u9020\u51fd\u6570\uff0c\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u94fe\u8868\nfunc NewListNode(val int) *ListNode {\n return &ListNode{\n Val: val,\n Next: nil,\n }\n}\n
/* \u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode {\n var val: Int // \u8282\u70b9\u503c\n var next: ListNode? // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u5f15\u7528\n\n init(x: Int) { // \u6784\u9020\u51fd\u6570\n val = x\n }\n}\n
/* \u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode {\n constructor(val, next) {\n this.val = (val === undefined ? 0 : val); // \u8282\u70b9\u503c\n this.next = (next === undefined ? null : next); // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u5f15\u7528\n }\n}\n
/* \u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode {\n val: number;\n next: ListNode | null;\n constructor(val?: number, next?: ListNode | null) {\n this.val = val === undefined ? 0 : val; // \u8282\u70b9\u503c\n this.next = next === undefined ? null : next; // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u5f15\u7528\n }\n}\n
/* \u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode {\n int val; // \u8282\u70b9\u503c\n ListNode? next; // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u5f15\u7528\n ListNode(this.val, [this.next]); // \u6784\u9020\u51fd\u6570\n}\n
use std::rc::Rc;\nuse std::cell::RefCell;\n/* \u94fe\u8868\u8282\u70b9\u7c7b */\n#[derive(Debug)]\nstruct ListNode {\n val: i32, // \u8282\u70b9\u503c\n next: Option<Rc<RefCell<ListNode>>>, // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u6307\u9488\n}\n
/* \u94fe\u8868\u8282\u70b9\u7ed3\u6784\u4f53 */\ntypedef struct ListNode {\n int val; // \u8282\u70b9\u503c\n struct ListNode *next; // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u6307\u9488\n} ListNode;\n\n/* \u6784\u9020\u51fd\u6570 */\nListNode *newListNode(int val) {\n ListNode *node;\n node = (ListNode *) malloc(sizeof(ListNode));\n node->val = val;\n node->next = NULL;\n return node;\n}\n
// \u94fe\u8868\u8282\u70b9\u7c7b\npub fn ListNode(comptime T: type) type {\n return struct {\n const Self = @This();\n\n val: T = 0, // \u8282\u70b9\u503c\n next: ?*Self = null, // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u6307\u9488\n\n // \u6784\u9020\u51fd\u6570\n pub fn init(self: *Self, x: i32) void {\n self.val = x;\n self.next = null;\n }\n };\n}\n
"},{"location":"chapter_array_and_linkedlist/linked_list/#421","title":"4.2.1 \u00a0 \u94fe\u8868\u5e38\u7528\u64cd\u4f5c","text":""},{"location":"chapter_array_and_linkedlist/linked_list/#1","title":"1. \u00a0 \u521d\u59cb\u5316\u94fe\u8868","text":"\u5efa\u7acb\u94fe\u8868\u5206\u4e3a\u4e24\u6b65\uff0c\u7b2c\u4e00\u6b65\u662f\u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\u5bf9\u8c61\uff0c\u7b2c\u4e8c\u6b65\u662f\u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\u5173\u7cfb\u3002\u521d\u59cb\u5316\u5b8c\u6210\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u4ece\u94fe\u8868\u7684\u5934\u8282\u70b9\u51fa\u53d1\uff0c\u901a\u8fc7\u5f15\u7528\u6307\u5411 next
\u4f9d\u6b21\u8bbf\u95ee\u6240\u6709\u8282\u70b9\u3002
# \u521d\u59cb\u5316\u94fe\u8868 1 -> 3 -> 2 -> 5 -> 4\n# \u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\nn0 = ListNode(1)\nn1 = ListNode(3)\nn2 = ListNode(2)\nn3 = ListNode(5)\nn4 = ListNode(4)\n# \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\nn0.next = n1\nn1.next = n2\nn2.next = n3\nn3.next = n4\n
linked_list.cpp/* \u521d\u59cb\u5316\u94fe\u8868 1 -> 3 -> 2 -> 5 -> 4 */\n// \u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\nListNode* n0 = new ListNode(1);\nListNode* n1 = new ListNode(3);\nListNode* n2 = new ListNode(2);\nListNode* n3 = new ListNode(5);\nListNode* n4 = new ListNode(4);\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\nn0->next = n1;\nn1->next = n2;\nn2->next = n3;\nn3->next = n4;\n
linked_list.java/* \u521d\u59cb\u5316\u94fe\u8868 1 -> 3 -> 2 -> 5 -> 4 */\n// \u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\nListNode n0 = new ListNode(1);\nListNode n1 = new ListNode(3);\nListNode n2 = new ListNode(2);\nListNode n3 = new ListNode(5);\nListNode n4 = new ListNode(4);\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\nn0.next = n1;\nn1.next = n2;\nn2.next = n3;\nn3.next = n4;\n
linked_list.cs/* \u521d\u59cb\u5316\u94fe\u8868 1 -> 3 -> 2 -> 5 -> 4 */\n// \u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\nListNode n0 = new(1);\nListNode n1 = new(3);\nListNode n2 = new(2);\nListNode n3 = new(5);\nListNode n4 = new(4);\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\nn0.next = n1;\nn1.next = n2;\nn2.next = n3;\nn3.next = n4;\n
linked_list.go/* \u521d\u59cb\u5316\u94fe\u8868 1 -> 3 -> 2 -> 5 -> 4 */\n// \u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\nn0 := NewListNode(1)\nn1 := NewListNode(3)\nn2 := NewListNode(2)\nn3 := NewListNode(5)\nn4 := NewListNode(4)\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\nn0.Next = n1\nn1.Next = n2\nn2.Next = n3\nn3.Next = n4\n
linked_list.swift/* \u521d\u59cb\u5316\u94fe\u8868 1 -> 3 -> 2 -> 5 -> 4 */\n// \u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\nlet n0 = ListNode(x: 1)\nlet n1 = ListNode(x: 3)\nlet n2 = ListNode(x: 2)\nlet n3 = ListNode(x: 5)\nlet n4 = ListNode(x: 4)\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\nn0.next = n1\nn1.next = n2\nn2.next = n3\nn3.next = n4\n
linked_list.js/* \u521d\u59cb\u5316\u94fe\u8868 1 -> 3 -> 2 -> 5 -> 4 */\n// \u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\nconst n0 = new ListNode(1);\nconst n1 = new ListNode(3);\nconst n2 = new ListNode(2);\nconst n3 = new ListNode(5);\nconst n4 = new ListNode(4);\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\nn0.next = n1;\nn1.next = n2;\nn2.next = n3;\nn3.next = n4;\n
linked_list.ts/* \u521d\u59cb\u5316\u94fe\u8868 1 -> 3 -> 2 -> 5 -> 4 */\n// \u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\nconst n0 = new ListNode(1);\nconst n1 = new ListNode(3);\nconst n2 = new ListNode(2);\nconst n3 = new ListNode(5);\nconst n4 = new ListNode(4);\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\nn0.next = n1;\nn1.next = n2;\nn2.next = n3;\nn3.next = n4;\n
linked_list.dart/* \u521d\u59cb\u5316\u94fe\u8868 1 -> 3 -> 2 -> 5 -> 4 */\\\n// \u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\nListNode n0 = ListNode(1);\nListNode n1 = ListNode(3);\nListNode n2 = ListNode(2);\nListNode n3 = ListNode(5);\nListNode n4 = ListNode(4);\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\nn0.next = n1;\nn1.next = n2;\nn2.next = n3;\nn3.next = n4;\n
linked_list.rs/* \u521d\u59cb\u5316\u94fe\u8868 1 -> 3 -> 2 -> 5 -> 4 */\n// \u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\nlet n0 = Rc::new(RefCell::new(ListNode { val: 1, next: None }));\nlet n1 = Rc::new(RefCell::new(ListNode { val: 3, next: None }));\nlet n2 = Rc::new(RefCell::new(ListNode { val: 2, next: None }));\nlet n3 = Rc::new(RefCell::new(ListNode { val: 5, next: None }));\nlet n4 = Rc::new(RefCell::new(ListNode { val: 4, next: None }));\n\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\nn0.borrow_mut().next = Some(n1.clone());\nn1.borrow_mut().next = Some(n2.clone());\nn2.borrow_mut().next = Some(n3.clone());\nn3.borrow_mut().next = Some(n4.clone());\n
linked_list.c/* \u521d\u59cb\u5316\u94fe\u8868 1 -> 3 -> 2 -> 5 -> 4 */\n// \u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\nListNode* n0 = newListNode(1);\nListNode* n1 = newListNode(3);\nListNode* n2 = newListNode(2);\nListNode* n3 = newListNode(5);\nListNode* n4 = newListNode(4);\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\nn0->next = n1;\nn1->next = n2;\nn2->next = n3;\nn3->next = n4;\n
linked_list.zig// \u521d\u59cb\u5316\u94fe\u8868\n// \u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\nvar n0 = inc.ListNode(i32){.val = 1};\nvar n1 = inc.ListNode(i32){.val = 3};\nvar n2 = inc.ListNode(i32){.val = 2};\nvar n3 = inc.ListNode(i32){.val = 5};\nvar n4 = inc.ListNode(i32){.val = 4};\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\nn0.next = &n1;\nn1.next = &n2;\nn2.next = &n3;\nn3.next = &n4;\n
\u6570\u7ec4\u6574\u4f53\u662f\u4e00\u4e2a\u53d8\u91cf\uff0c\u6bd4\u5982\u6570\u7ec4 nums
\u5305\u542b\u5143\u7d20 nums[0]
\u548c nums[1]
\u7b49\uff0c\u800c\u94fe\u8868\u662f\u7531\u591a\u4e2a\u72ec\u7acb\u7684\u8282\u70b9\u5bf9\u8c61\u7ec4\u6210\u7684\u3002\u6211\u4eec\u901a\u5e38\u5c06\u5934\u8282\u70b9\u5f53\u4f5c\u94fe\u8868\u7684\u4ee3\u79f0\uff0c\u6bd4\u5982\u4ee5\u4e0a\u4ee3\u7801\u4e2d\u7684\u94fe\u8868\u53ef\u8bb0\u4f5c\u94fe\u8868 n0
\u3002
\u5728\u94fe\u8868\u4e2d\u63d2\u5165\u8282\u70b9\u975e\u5e38\u5bb9\u6613\u3002\u5982\u56fe 4-6 \u6240\u793a\uff0c\u5047\u8bbe\u6211\u4eec\u60f3\u5728\u76f8\u90bb\u7684\u4e24\u4e2a\u8282\u70b9 n0
\u548c n1
\u4e4b\u95f4\u63d2\u5165\u4e00\u4e2a\u65b0\u8282\u70b9 P
\uff0c\u5219\u53ea\u9700\u6539\u53d8\u4e24\u4e2a\u8282\u70b9\u5f15\u7528\uff08\u6307\u9488\uff09\u5373\u53ef\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(1)\\) \u3002
\u76f8\u6bd4\u4e4b\u4e0b\uff0c\u5728\u6570\u7ec4\u4e2d\u63d2\u5165\u5143\u7d20\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \uff0c\u5728\u5927\u6570\u636e\u91cf\u4e0b\u7684\u6548\u7387\u8f83\u4f4e\u3002
\u56fe 4-6 \u00a0 \u94fe\u8868\u63d2\u5165\u8282\u70b9\u793a\u4f8b
PythonC++JavaC#GoSwiftJSTSDartRustCZig linked_list.pydef insert(n0: ListNode, P: ListNode):\n \"\"\"\u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P\"\"\"\n n1 = n0.next\n P.next = n1\n n0.next = P\n
linked_list.cpp/* \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P */\nvoid insert(ListNode *n0, ListNode *P) {\n ListNode *n1 = n0->next;\n P->next = n1;\n n0->next = P;\n}\n
linked_list.java/* \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P */\nvoid insert(ListNode n0, ListNode P) {\n ListNode n1 = n0.next;\n P.next = n1;\n n0.next = P;\n}\n
linked_list.cs/* \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P */\nvoid Insert(ListNode n0, ListNode P) {\n ListNode? n1 = n0.next;\n P.next = n1;\n n0.next = P;\n}\n
linked_list.go/* \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P */\nfunc insertNode(n0 *ListNode, P *ListNode) {\n n1 := n0.Next\n P.Next = n1\n n0.Next = P\n}\n
linked_list.swift/* \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P */\nfunc insert(n0: ListNode, P: ListNode) {\n let n1 = n0.next\n P.next = n1\n n0.next = P\n}\n
linked_list.js/* \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P */\nfunction insert(n0, P) {\n const n1 = n0.next;\n P.next = n1;\n n0.next = P;\n}\n
linked_list.ts/* \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P */\nfunction insert(n0: ListNode, P: ListNode): void {\n const n1 = n0.next;\n P.next = n1;\n n0.next = P;\n}\n
linked_list.dart/* \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P */\nvoid insert(ListNode n0, ListNode P) {\n ListNode? n1 = n0.next;\n P.next = n1;\n n0.next = P;\n}\n
linked_list.rs/* \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P */\n#[allow(non_snake_case)]\npub fn insert<T>(n0: &Rc<RefCell<ListNode<T>>>, P: Rc<RefCell<ListNode<T>>>) {\n let n1 = n0.borrow_mut().next.take();\n P.borrow_mut().next = n1;\n n0.borrow_mut().next = Some(P);\n}\n
linked_list.c/* \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P */\nvoid insert(ListNode *n0, ListNode *P) {\n ListNode *n1 = n0->next;\n P->next = n1;\n n0->next = P;\n}\n
linked_list.zig// \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P\nfn insert(n0: ?*inc.ListNode(i32), P: ?*inc.ListNode(i32)) void {\n var n1 = n0.?.next;\n P.?.next = n1;\n n0.?.next = P;\n}\n
"},{"location":"chapter_array_and_linkedlist/linked_list/#3","title":"3. \u00a0 \u5220\u9664\u8282\u70b9","text":"\u5982\u56fe 4-7 \u6240\u793a\uff0c\u5728\u94fe\u8868\u4e2d\u5220\u9664\u8282\u70b9\u4e5f\u975e\u5e38\u65b9\u4fbf\uff0c\u53ea\u9700\u6539\u53d8\u4e00\u4e2a\u8282\u70b9\u7684\u5f15\u7528\uff08\u6307\u9488\uff09\u5373\u53ef\u3002
\u8bf7\u6ce8\u610f\uff0c\u5c3d\u7ba1\u5728\u5220\u9664\u64cd\u4f5c\u5b8c\u6210\u540e\u8282\u70b9 P
\u4ecd\u7136\u6307\u5411 n1
\uff0c\u4f46\u5b9e\u9645\u4e0a\u904d\u5386\u6b64\u94fe\u8868\u5df2\u7ecf\u65e0\u6cd5\u8bbf\u95ee\u5230 P
\uff0c\u8fd9\u610f\u5473\u7740 P
\u5df2\u7ecf\u4e0d\u518d\u5c5e\u4e8e\u8be5\u94fe\u8868\u4e86\u3002
\u56fe 4-7 \u00a0 \u94fe\u8868\u5220\u9664\u8282\u70b9
PythonC++JavaC#GoSwiftJSTSDartRustCZig linked_list.pydef remove(n0: ListNode):\n \"\"\"\u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9\"\"\"\n if not n0.next:\n return\n # n0 -> P -> n1\n P = n0.next\n n1 = P.next\n n0.next = n1\n
linked_list.cpp/* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\nvoid remove(ListNode *n0) {\n if (n0->next == nullptr)\n return;\n // n0 -> P -> n1\n ListNode *P = n0->next;\n ListNode *n1 = P->next;\n n0->next = n1;\n // \u91ca\u653e\u5185\u5b58\n delete P;\n}\n
linked_list.java/* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\nvoid remove(ListNode n0) {\n if (n0.next == null)\n return;\n // n0 -> P -> n1\n ListNode P = n0.next;\n ListNode n1 = P.next;\n n0.next = n1;\n}\n
linked_list.cs/* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\nvoid Remove(ListNode n0) {\n if (n0.next == null)\n return;\n // n0 -> P -> n1\n ListNode P = n0.next;\n ListNode? n1 = P.next;\n n0.next = n1;\n}\n
linked_list.go/* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\nfunc removeItem(n0 *ListNode) {\n if n0.Next == nil {\n return\n }\n // n0 -> P -> n1\n P := n0.Next\n n1 := P.Next\n n0.Next = n1\n}\n
linked_list.swift/* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\nfunc remove(n0: ListNode) {\n if n0.next == nil {\n return\n }\n // n0 -> P -> n1\n let P = n0.next\n let n1 = P?.next\n n0.next = n1\n P?.next = nil\n}\n
linked_list.js/* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\nfunction remove(n0) {\n if (!n0.next) return;\n // n0 -> P -> n1\n const P = n0.next;\n const n1 = P.next;\n n0.next = n1;\n}\n
linked_list.ts/* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\nfunction remove(n0: ListNode): void {\n if (!n0.next) {\n return;\n }\n // n0 -> P -> n1\n const P = n0.next;\n const n1 = P.next;\n n0.next = n1;\n}\n
linked_list.dart/* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\nvoid remove(ListNode n0) {\n if (n0.next == null) return;\n // n0 -> P -> n1\n ListNode P = n0.next!;\n ListNode? n1 = P.next;\n n0.next = n1;\n}\n
linked_list.rs/* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\n#[allow(non_snake_case)]\npub fn remove<T>(n0: &Rc<RefCell<ListNode<T>>>) {\n if n0.borrow().next.is_none() {return};\n // n0 -> P -> n1\n let P = n0.borrow_mut().next.take();\n if let Some(node) = P {\n let n1 = node.borrow_mut().next.take();\n n0.borrow_mut().next = n1;\n }\n}\n
linked_list.c/* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\n// \u6ce8\u610f\uff1astdio.h \u5360\u7528\u4e86 remove \u5173\u952e\u8bcd\nvoid removeItem(ListNode *n0) {\n if (!n0->next)\n return;\n // n0 -> P -> n1\n ListNode *P = n0->next;\n ListNode *n1 = P->next;\n n0->next = n1;\n // \u91ca\u653e\u5185\u5b58\n free(P);\n}\n
linked_list.zig// \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9\nfn remove(n0: ?*inc.ListNode(i32)) void {\n if (n0.?.next == null) return;\n // n0 -> P -> n1\n var P = n0.?.next;\n var n1 = P.?.next;\n n0.?.next = n1;\n}\n
"},{"location":"chapter_array_and_linkedlist/linked_list/#4","title":"4. \u00a0 \u8bbf\u95ee\u8282\u70b9","text":"\u5728\u94fe\u8868\u4e2d\u8bbf\u95ee\u8282\u70b9\u7684\u6548\u7387\u8f83\u4f4e\u3002\u5982\u4e0a\u4e00\u8282\u6240\u8ff0\uff0c\u6211\u4eec\u53ef\u4ee5\u5728 \\(O(1)\\) \u65f6\u95f4\u4e0b\u8bbf\u95ee\u6570\u7ec4\u4e2d\u7684\u4efb\u610f\u5143\u7d20\u3002\u94fe\u8868\u5219\u4e0d\u7136\uff0c\u7a0b\u5e8f\u9700\u8981\u4ece\u5934\u8282\u70b9\u51fa\u53d1\uff0c\u9010\u4e2a\u5411\u540e\u904d\u5386\uff0c\u76f4\u81f3\u627e\u5230\u76ee\u6807\u8282\u70b9\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u8bbf\u95ee\u94fe\u8868\u7684\u7b2c \\(i\\) \u4e2a\u8282\u70b9\u9700\u8981\u5faa\u73af \\(i - 1\\) \u8f6e\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002
PythonC++JavaC#GoSwiftJSTSDartRustCZig linked_list.pydef access(head: ListNode, index: int) -> ListNode | None:\n \"\"\"\u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9\"\"\"\n for _ in range(index):\n if not head:\n return None\n head = head.next\n return head\n
linked_list.cpp/* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\nListNode *access(ListNode *head, int index) {\n for (int i = 0; i < index; i++) {\n if (head == nullptr)\n return nullptr;\n head = head->next;\n }\n return head;\n}\n
linked_list.java/* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\nListNode access(ListNode head, int index) {\n for (int i = 0; i < index; i++) {\n if (head == null)\n return null;\n head = head.next;\n }\n return head;\n}\n
linked_list.cs/* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\nListNode? Access(ListNode? head, int index) {\n for (int i = 0; i < index; i++) {\n if (head == null)\n return null;\n head = head.next;\n }\n return head;\n}\n
linked_list.go/* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\nfunc access(head *ListNode, index int) *ListNode {\n for i := 0; i < index; i++ {\n if head == nil {\n return nil\n }\n head = head.Next\n }\n return head\n}\n
linked_list.swift/* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\nfunc access(head: ListNode, index: Int) -> ListNode? {\n var head: ListNode? = head\n for _ in 0 ..< index {\n if head == nil {\n return nil\n }\n head = head?.next\n }\n return head\n}\n
linked_list.js/* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\nfunction access(head, index) {\n for (let i = 0; i < index; i++) {\n if (!head) {\n return null;\n }\n head = head.next;\n }\n return head;\n}\n
linked_list.ts/* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\nfunction access(head: ListNode | null, index: number): ListNode | null {\n for (let i = 0; i < index; i++) {\n if (!head) {\n return null;\n }\n head = head.next;\n }\n return head;\n}\n
linked_list.dart/* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\nListNode? access(ListNode? head, int index) {\n for (var i = 0; i < index; i++) {\n if (head == null) return null;\n head = head.next;\n }\n return head;\n}\n
linked_list.rs/* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\npub fn access<T>(head: Rc<RefCell<ListNode<T>>>, index: i32) -> Rc<RefCell<ListNode<T>>> {\n if index <= 0 {return head};\n if let Some(node) = &head.borrow_mut().next {\n return access(node.clone(), index - 1);\n }\n return head;\n}\n
linked_list.c/* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\nListNode *access(ListNode *head, int index) {\n while (head && head->next && index) {\n head = head->next;\n index--;\n }\n return head;\n}\n
linked_list.zig// \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9\nfn access(node: ?*inc.ListNode(i32), index: i32) ?*inc.ListNode(i32) {\n var head = node;\n var i: i32 = 0;\n while (i < index) : (i += 1) {\n head = head.?.next;\n if (head == null) return null;\n }\n return head;\n}\n
"},{"location":"chapter_array_and_linkedlist/linked_list/#5","title":"5. \u00a0 \u67e5\u627e\u8282\u70b9","text":"\u904d\u5386\u94fe\u8868\uff0c\u67e5\u627e\u5176\u4e2d\u503c\u4e3a target
\u7684\u8282\u70b9\uff0c\u8f93\u51fa\u8be5\u8282\u70b9\u5728\u94fe\u8868\u4e2d\u7684\u7d22\u5f15\u3002\u6b64\u8fc7\u7a0b\u4e5f\u5c5e\u4e8e\u7ebf\u6027\u67e5\u627e\u3002\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a
def find(head: ListNode, target: int) -> int:\n \"\"\"\u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9\"\"\"\n index = 0\n while head:\n if head.val == target:\n return index\n head = head.next\n index += 1\n return -1\n
linked_list.cpp/* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 */\nint find(ListNode *head, int target) {\n int index = 0;\n while (head != nullptr) {\n if (head->val == target)\n return index;\n head = head->next;\n index++;\n }\n return -1;\n}\n
linked_list.java/* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 */\nint find(ListNode head, int target) {\n int index = 0;\n while (head != null) {\n if (head.val == target)\n return index;\n head = head.next;\n index++;\n }\n return -1;\n}\n
linked_list.cs/* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 */\nint Find(ListNode? head, int target) {\n int index = 0;\n while (head != null) {\n if (head.val == target)\n return index;\n head = head.next;\n index++;\n }\n return -1;\n}\n
linked_list.go/* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 */\nfunc findNode(head *ListNode, target int) int {\n index := 0\n for head != nil {\n if head.Val == target {\n return index\n }\n head = head.Next\n index++\n }\n return -1\n}\n
linked_list.swift/* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 */\nfunc find(head: ListNode, target: Int) -> Int {\n var head: ListNode? = head\n var index = 0\n while head != nil {\n if head?.val == target {\n return index\n }\n head = head?.next\n index += 1\n }\n return -1\n}\n
linked_list.js/* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 */\nfunction find(head, target) {\n let index = 0;\n while (head !== null) {\n if (head.val === target) {\n return index;\n }\n head = head.next;\n index += 1;\n }\n return -1;\n}\n
linked_list.ts/* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 */\nfunction find(head: ListNode | null, target: number): number {\n let index = 0;\n while (head !== null) {\n if (head.val === target) {\n return index;\n }\n head = head.next;\n index += 1;\n }\n return -1;\n}\n
linked_list.dart/* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 */\nint find(ListNode? head, int target) {\n int index = 0;\n while (head != null) {\n if (head.val == target) {\n return index;\n }\n head = head.next;\n index++;\n }\n return -1;\n}\n
linked_list.rs/* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 */\npub fn find<T: PartialEq>(head: Rc<RefCell<ListNode<T>>>, target: T, index: i32) -> i32 {\n if head.borrow().val == target {return index};\n if let Some(node) = &head.borrow_mut().next {\n return find(node.clone(), target, index + 1);\n }\n return -1;\n}\n
linked_list.c/* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 */\nint find(ListNode *head, int target) {\n int index = 0;\n while (head) {\n if (head->val == target)\n return index;\n head = head->next;\n index++;\n }\n return -1;\n}\n
linked_list.zig// \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9\nfn find(node: ?*inc.ListNode(i32), target: i32) i32 {\n var head = node;\n var index: i32 = 0;\n while (head != null) {\n if (head.?.val == target) return index;\n head = head.?.next;\n index += 1;\n }\n return -1;\n}\n
"},{"location":"chapter_array_and_linkedlist/linked_list/#422-vs","title":"4.2.2 \u00a0 \u6570\u7ec4 vs. \u94fe\u8868","text":"\u8868 4-1 \u603b\u7ed3\u4e86\u6570\u7ec4\u548c\u94fe\u8868\u7684\u5404\u9879\u7279\u70b9\u5e76\u5bf9\u6bd4\u4e86\u64cd\u4f5c\u6548\u7387\u3002\u7531\u4e8e\u5b83\u4eec\u91c7\u7528\u4e24\u79cd\u76f8\u53cd\u7684\u5b58\u50a8\u7b56\u7565\uff0c\u56e0\u6b64\u5404\u79cd\u6027\u8d28\u548c\u64cd\u4f5c\u6548\u7387\u4e5f\u5448\u73b0\u5bf9\u7acb\u7684\u7279\u70b9\u3002
\u8868 4-1 \u00a0 \u6570\u7ec4\u4e0e\u94fe\u8868\u7684\u6548\u7387\u5bf9\u6bd4
\u6570\u7ec4 \u94fe\u8868 \u5b58\u50a8\u65b9\u5f0f \u8fde\u7eed\u5185\u5b58\u7a7a\u95f4 \u5206\u6563\u5185\u5b58\u7a7a\u95f4 \u5bb9\u91cf\u6269\u5c55 \u957f\u5ea6\u4e0d\u53ef\u53d8 \u53ef\u7075\u6d3b\u6269\u5c55 \u5185\u5b58\u6548\u7387 \u5143\u7d20\u5360\u7528\u5185\u5b58\u5c11\u3001\u4f46\u53ef\u80fd\u6d6a\u8d39\u7a7a\u95f4 \u5143\u7d20\u5360\u7528\u5185\u5b58\u591a \u8bbf\u95ee\u5143\u7d20 \\(O(1)\\) \\(O(n)\\) \u6dfb\u52a0\u5143\u7d20 \\(O(n)\\) \\(O(1)\\) \u5220\u9664\u5143\u7d20 \\(O(n)\\) \\(O(1)\\)"},{"location":"chapter_array_and_linkedlist/linked_list/#423","title":"4.2.3 \u00a0 \u5e38\u89c1\u94fe\u8868\u7c7b\u578b","text":"\u5982\u56fe 4-8 \u6240\u793a\uff0c\u5e38\u89c1\u7684\u94fe\u8868\u7c7b\u578b\u5305\u62ec\u4e09\u79cd\u3002
class ListNode:\n \"\"\"\u53cc\u5411\u94fe\u8868\u8282\u70b9\u7c7b\"\"\"\n def __init__(self, val: int):\n self.val: int = val # \u8282\u70b9\u503c\n self.next: ListNode | None = None # \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u5f15\u7528\n self.prev: ListNode | None = None # \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u5f15\u7528\n
/* \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7ed3\u6784\u4f53 */\nstruct ListNode {\n int val; // \u8282\u70b9\u503c\n ListNode *next; // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u6307\u9488\n ListNode *prev; // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u6307\u9488\n ListNode(int x) : val(x), next(nullptr), prev(nullptr) {} // \u6784\u9020\u51fd\u6570\n};\n
/* \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode {\n int val; // \u8282\u70b9\u503c\n ListNode next; // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u5f15\u7528\n ListNode prev; // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u5f15\u7528\n ListNode(int x) { val = x; } // \u6784\u9020\u51fd\u6570\n}\n
/* \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode(int x) { // \u6784\u9020\u51fd\u6570\n int val = x; // \u8282\u70b9\u503c\n ListNode next; // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u5f15\u7528\n ListNode prev; // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u5f15\u7528\n}\n
/* \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7ed3\u6784\u4f53 */\ntype DoublyListNode struct {\n Val int // \u8282\u70b9\u503c\n Next *DoublyListNode // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u6307\u9488\n Prev *DoublyListNode // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u6307\u9488\n}\n\n// NewDoublyListNode \u521d\u59cb\u5316\nfunc NewDoublyListNode(val int) *DoublyListNode {\n return &DoublyListNode{\n Val: val,\n Next: nil,\n Prev: nil,\n }\n}\n
/* \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode {\n var val: Int // \u8282\u70b9\u503c\n var next: ListNode? // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u5f15\u7528\n var prev: ListNode? // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u5f15\u7528\n\n init(x: Int) { // \u6784\u9020\u51fd\u6570\n val = x\n }\n}\n
/* \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode {\n constructor(val, next, prev) {\n this.val = val === undefined ? 0 : val; // \u8282\u70b9\u503c\n this.next = next === undefined ? null : next; // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u5f15\u7528\n this.prev = prev === undefined ? null : prev; // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u5f15\u7528\n }\n}\n
/* \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode {\n val: number;\n next: ListNode | null;\n prev: ListNode | null;\n constructor(val?: number, next?: ListNode | null, prev?: ListNode | null) {\n this.val = val === undefined ? 0 : val; // \u8282\u70b9\u503c\n this.next = next === undefined ? null : next; // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u5f15\u7528\n this.prev = prev === undefined ? null : prev; // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u5f15\u7528\n }\n}\n
/* \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode {\n int val; // \u8282\u70b9\u503c\n ListNode next; // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u5f15\u7528\n ListNode prev; // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u5f15\u7528\n ListNode(this.val, [this.next, this.prev]); // \u6784\u9020\u51fd\u6570\n}\n
use std::rc::Rc;\nuse std::cell::RefCell;\n\n/* \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7c7b\u578b */\n#[derive(Debug)]\nstruct ListNode {\n val: i32, // \u8282\u70b9\u503c\n next: Option<Rc<RefCell<ListNode>>>, // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u6307\u9488\n prev: Option<Rc<RefCell<ListNode>>>, // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u6307\u9488\n}\n\n/* \u6784\u9020\u51fd\u6570 */\nimpl ListNode {\n fn new(val: i32) -> Self {\n ListNode {\n val,\n next: None,\n prev: None,\n }\n }\n}\n
/* \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7ed3\u6784\u4f53 */\ntypedef struct ListNode {\n int val; // \u8282\u70b9\u503c\n struct ListNode *next; // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u6307\u9488\n struct ListNode *prev; // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u6307\u9488\n} ListNode;\n\n/* \u6784\u9020\u51fd\u6570 */\nListNode *newListNode(int val) {\n ListNode *node;\n node = (ListNode *) malloc(sizeof(ListNode));\n node->val = val;\n node->next = NULL;\n node->prev = NULL;\n return node;\n}\n
// \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7c7b\npub fn ListNode(comptime T: type) type {\n return struct {\n const Self = @This();\n\n val: T = 0, // \u8282\u70b9\u503c\n next: ?*Self = null, // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u6307\u9488\n prev: ?*Self = null, // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u6307\u9488\n\n // \u6784\u9020\u51fd\u6570\n pub fn init(self: *Self, x: i32) void {\n self.val = x;\n self.next = null;\n self.prev = null;\n }\n };\n}\n
\u56fe 4-8 \u00a0 \u5e38\u89c1\u94fe\u8868\u79cd\u7c7b
"},{"location":"chapter_array_and_linkedlist/linked_list/#424","title":"4.2.4 \u00a0 \u94fe\u8868\u5178\u578b\u5e94\u7528","text":"\u5355\u5411\u94fe\u8868\u901a\u5e38\u7528\u4e8e\u5b9e\u73b0\u6808\u3001\u961f\u5217\u3001\u54c8\u5e0c\u8868\u548c\u56fe\u7b49\u6570\u636e\u7ed3\u6784\u3002
\u53cc\u5411\u94fe\u8868\u5e38\u7528\u4e8e\u9700\u8981\u5feb\u901f\u67e5\u627e\u524d\u4e00\u4e2a\u548c\u540e\u4e00\u4e2a\u5143\u7d20\u7684\u573a\u666f\u3002
\u73af\u5f62\u94fe\u8868\u5e38\u7528\u4e8e\u9700\u8981\u5468\u671f\u6027\u64cd\u4f5c\u7684\u573a\u666f\uff0c\u6bd4\u5982\u64cd\u4f5c\u7cfb\u7edf\u7684\u8d44\u6e90\u8c03\u5ea6\u3002
\u300c\u5217\u8868 list\u300d\u662f\u4e00\u4e2a\u62bd\u8c61\u7684\u6570\u636e\u7ed3\u6784\u6982\u5ff5\uff0c\u5b83\u8868\u793a\u5143\u7d20\u7684\u6709\u5e8f\u96c6\u5408\uff0c\u652f\u6301\u5143\u7d20\u8bbf\u95ee\u3001\u4fee\u6539\u3001\u6dfb\u52a0\u3001\u5220\u9664\u548c\u904d\u5386\u7b49\u64cd\u4f5c\uff0c\u65e0\u987b\u4f7f\u7528\u8005\u8003\u8651\u5bb9\u91cf\u9650\u5236\u7684\u95ee\u9898\u3002\u5217\u8868\u53ef\u4ee5\u57fa\u4e8e\u94fe\u8868\u6216\u6570\u7ec4\u5b9e\u73b0\u3002
\u5f53\u4f7f\u7528\u6570\u7ec4\u5b9e\u73b0\u5217\u8868\u65f6\uff0c\u957f\u5ea6\u4e0d\u53ef\u53d8\u7684\u6027\u8d28\u4f1a\u5bfc\u81f4\u5217\u8868\u7684\u5b9e\u7528\u6027\u964d\u4f4e\u3002\u8fd9\u662f\u56e0\u4e3a\u6211\u4eec\u901a\u5e38\u65e0\u6cd5\u4e8b\u5148\u786e\u5b9a\u9700\u8981\u5b58\u50a8\u591a\u5c11\u6570\u636e\uff0c\u4ece\u800c\u96be\u4ee5\u9009\u62e9\u5408\u9002\u7684\u5217\u8868\u957f\u5ea6\u3002\u82e5\u957f\u5ea6\u8fc7\u5c0f\uff0c\u5219\u5f88\u53ef\u80fd\u65e0\u6cd5\u6ee1\u8db3\u4f7f\u7528\u9700\u6c42\uff1b\u82e5\u957f\u5ea6\u8fc7\u5927\uff0c\u5219\u4f1a\u9020\u6210\u5185\u5b58\u7a7a\u95f4\u6d6a\u8d39\u3002
\u4e3a\u89e3\u51b3\u6b64\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u300c\u52a8\u6001\u6570\u7ec4 dynamic array\u300d\u6765\u5b9e\u73b0\u5217\u8868\u3002\u5b83\u7ee7\u627f\u4e86\u6570\u7ec4\u7684\u5404\u9879\u4f18\u70b9\uff0c\u5e76\u4e14\u53ef\u4ee5\u5728\u7a0b\u5e8f\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u8fdb\u884c\u52a8\u6001\u6269\u5bb9\u3002
\u5b9e\u9645\u4e0a\uff0c\u8bb8\u591a\u7f16\u7a0b\u8bed\u8a00\u4e2d\u7684\u6807\u51c6\u5e93\u63d0\u4f9b\u7684\u5217\u8868\u662f\u57fa\u4e8e\u52a8\u6001\u6570\u7ec4\u5b9e\u73b0\u7684\uff0c\u4f8b\u5982 Python \u4e2d\u7684 list
\u3001Java \u4e2d\u7684 ArrayList
\u3001C++ \u4e2d\u7684 vector
\u548c C# \u4e2d\u7684 List
\u7b49\u3002\u5728\u63a5\u4e0b\u6765\u7684\u8ba8\u8bba\u4e2d\uff0c\u6211\u4eec\u5c06\u628a\u201c\u5217\u8868\u201d\u548c\u201c\u52a8\u6001\u6570\u7ec4\u201d\u89c6\u4e3a\u7b49\u540c\u7684\u6982\u5ff5\u3002
\u6211\u4eec\u901a\u5e38\u4f7f\u7528\u201c\u65e0\u521d\u59cb\u503c\u201d\u548c\u201c\u6709\u521d\u59cb\u503c\u201d\u8fd9\u4e24\u79cd\u521d\u59cb\u5316\u65b9\u6cd5\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig list.py# \u521d\u59cb\u5316\u5217\u8868\n# \u65e0\u521d\u59cb\u503c\nnums1: list[int] = []\n# \u6709\u521d\u59cb\u503c\nnums: list[int] = [1, 3, 2, 5, 4]\n
list.cpp/* \u521d\u59cb\u5316\u5217\u8868 */\n// \u9700\u6ce8\u610f\uff0cC++ \u4e2d vector \u5373\u662f\u672c\u6587\u63cf\u8ff0\u7684 nums\n// \u65e0\u521d\u59cb\u503c\nvector<int> nums1;\n// \u6709\u521d\u59cb\u503c\nvector<int> nums = { 1, 3, 2, 5, 4 };\n
list.java/* \u521d\u59cb\u5316\u5217\u8868 */\n// \u65e0\u521d\u59cb\u503c\nList<Integer> nums1 = new ArrayList<>();\n// \u6709\u521d\u59cb\u503c\uff08\u6ce8\u610f\u6570\u7ec4\u7684\u5143\u7d20\u7c7b\u578b\u9700\u4e3a int[] \u7684\u5305\u88c5\u7c7b Integer[]\uff09\nInteger[] numbers = new Integer[] { 1, 3, 2, 5, 4 };\nList<Integer> nums = new ArrayList<>(Arrays.asList(numbers));\n
list.cs/* \u521d\u59cb\u5316\u5217\u8868 */\n// \u65e0\u521d\u59cb\u503c\nList<int> nums1 = [];\n// \u6709\u521d\u59cb\u503c\nint[] numbers = [1, 3, 2, 5, 4];\nList<int> nums = [.. numbers];\n
list_test.go/* \u521d\u59cb\u5316\u5217\u8868 */\n// \u65e0\u521d\u59cb\u503c\nnums1 := []int{}\n// \u6709\u521d\u59cb\u503c\nnums := []int{1, 3, 2, 5, 4}\n
list.swift/* \u521d\u59cb\u5316\u5217\u8868 */\n// \u65e0\u521d\u59cb\u503c\nlet nums1: [Int] = []\n// \u6709\u521d\u59cb\u503c\nvar nums = [1, 3, 2, 5, 4]\n
list.js/* \u521d\u59cb\u5316\u5217\u8868 */\n// \u65e0\u521d\u59cb\u503c\nconst nums1 = [];\n// \u6709\u521d\u59cb\u503c\nconst nums = [1, 3, 2, 5, 4];\n
list.ts/* \u521d\u59cb\u5316\u5217\u8868 */\n// \u65e0\u521d\u59cb\u503c\nconst nums1: number[] = [];\n// \u6709\u521d\u59cb\u503c\nconst nums: number[] = [1, 3, 2, 5, 4];\n
list.dart/* \u521d\u59cb\u5316\u5217\u8868 */\n// \u65e0\u521d\u59cb\u503c\nList<int> nums1 = [];\n// \u6709\u521d\u59cb\u503c\nList<int> nums = [1, 3, 2, 5, 4];\n
list.rs/* \u521d\u59cb\u5316\u5217\u8868 */\n// \u65e0\u521d\u59cb\u503c\nlet nums1: Vec<i32> = Vec::new();\n// \u6709\u521d\u59cb\u503c\nlet nums: Vec<i32> = vec![1, 3, 2, 5, 4];\n
list.c// C \u672a\u63d0\u4f9b\u5185\u7f6e\u52a8\u6001\u6570\u7ec4\n
list.zig// \u521d\u59cb\u5316\u5217\u8868\nvar nums = std.ArrayList(i32).init(std.heap.page_allocator);\ndefer nums.deinit();\ntry nums.appendSlice(&[_]i32{ 1, 3, 2, 5, 4 });\n
"},{"location":"chapter_array_and_linkedlist/list/#2","title":"2. \u00a0 \u8bbf\u95ee\u5143\u7d20","text":"\u5217\u8868\u672c\u8d28\u4e0a\u662f\u6570\u7ec4\uff0c\u56e0\u6b64\u53ef\u4ee5\u5728 \\(O(1)\\) \u65f6\u95f4\u5185\u8bbf\u95ee\u548c\u66f4\u65b0\u5143\u7d20\uff0c\u6548\u7387\u5f88\u9ad8\u3002
PythonC++JavaC#GoSwiftJSTSDartRustCZig list.py# \u8bbf\u95ee\u5143\u7d20\nnum: int = nums[1] # \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n\n# \u66f4\u65b0\u5143\u7d20\nnums[1] = 0 # \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n
list.cpp/* \u8bbf\u95ee\u5143\u7d20 */\nint num = nums[1]; // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n\n/* \u66f4\u65b0\u5143\u7d20 */\nnums[1] = 0; // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n
list.java/* \u8bbf\u95ee\u5143\u7d20 */\nint num = nums.get(1); // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n\n/* \u66f4\u65b0\u5143\u7d20 */\nnums.set(1, 0); // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n
list.cs/* \u8bbf\u95ee\u5143\u7d20 */\nint num = nums[1]; // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n\n/* \u66f4\u65b0\u5143\u7d20 */\nnums[1] = 0; // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n
list_test.go/* \u8bbf\u95ee\u5143\u7d20 */\nnum := nums[1] // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n\n/* \u66f4\u65b0\u5143\u7d20 */\nnums[1] = 0 // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n
list.swift/* \u8bbf\u95ee\u5143\u7d20 */\nlet num = nums[1] // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n\n/* \u66f4\u65b0\u5143\u7d20 */\nnums[1] = 0 // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n
list.js/* \u8bbf\u95ee\u5143\u7d20 */\nconst num = nums[1]; // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n\n/* \u66f4\u65b0\u5143\u7d20 */\nnums[1] = 0; // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n
list.ts/* \u8bbf\u95ee\u5143\u7d20 */\nconst num: number = nums[1]; // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n\n/* \u66f4\u65b0\u5143\u7d20 */\nnums[1] = 0; // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n
list.dart/* \u8bbf\u95ee\u5143\u7d20 */\nint num = nums[1]; // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n\n/* \u66f4\u65b0\u5143\u7d20 */\nnums[1] = 0; // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n
list.rs/* \u8bbf\u95ee\u5143\u7d20 */\nlet num: i32 = nums[1]; // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n/* \u66f4\u65b0\u5143\u7d20 */\nnums[1] = 0; // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n
list.c// C \u672a\u63d0\u4f9b\u5185\u7f6e\u52a8\u6001\u6570\u7ec4\n
list.zig// \u8bbf\u95ee\u5143\u7d20\nvar num = nums.items[1]; // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n\n// \u66f4\u65b0\u5143\u7d20\nnums.items[1] = 0; // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0 \n
"},{"location":"chapter_array_and_linkedlist/list/#3","title":"3. \u00a0 \u63d2\u5165\u4e0e\u5220\u9664\u5143\u7d20","text":"\u76f8\u8f83\u4e8e\u6570\u7ec4\uff0c\u5217\u8868\u53ef\u4ee5\u81ea\u7531\u5730\u6dfb\u52a0\u4e0e\u5220\u9664\u5143\u7d20\u3002\u5728\u5217\u8868\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(1)\\) \uff0c\u4f46\u63d2\u5165\u548c\u5220\u9664\u5143\u7d20\u7684\u6548\u7387\u4ecd\u4e0e\u6570\u7ec4\u76f8\u540c\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002
PythonC++JavaC#GoSwiftJSTSDartRustCZig list.py# \u6e05\u7a7a\u5217\u8868\nnums.clear()\n\n# \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20\nnums.append(1)\nnums.append(3)\nnums.append(2)\nnums.append(5)\nnums.append(4)\n\n# \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20\nnums.insert(3, 6) # \u5728\u7d22\u5f15 3 \u5904\u63d2\u5165\u6570\u5b57 6\n\n# \u5220\u9664\u5143\u7d20\nnums.pop(3) # \u5220\u9664\u7d22\u5f15 3 \u5904\u7684\u5143\u7d20\n
list.cpp/* \u6e05\u7a7a\u5217\u8868 */\nnums.clear();\n\n/* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nnums.push_back(1);\nnums.push_back(3);\nnums.push_back(2);\nnums.push_back(5);\nnums.push_back(4);\n\n/* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nnums.insert(nums.begin() + 3, 6); // \u5728\u7d22\u5f15 3 \u5904\u63d2\u5165\u6570\u5b57 6\n\n/* \u5220\u9664\u5143\u7d20 */\nnums.erase(nums.begin() + 3); // \u5220\u9664\u7d22\u5f15 3 \u5904\u7684\u5143\u7d20\n
list.java/* \u6e05\u7a7a\u5217\u8868 */\nnums.clear();\n\n/* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nnums.add(1);\nnums.add(3);\nnums.add(2);\nnums.add(5);\nnums.add(4);\n\n/* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nnums.add(3, 6); // \u5728\u7d22\u5f15 3 \u5904\u63d2\u5165\u6570\u5b57 6\n\n/* \u5220\u9664\u5143\u7d20 */\nnums.remove(3); // \u5220\u9664\u7d22\u5f15 3 \u5904\u7684\u5143\u7d20\n
list.cs/* \u6e05\u7a7a\u5217\u8868 */\nnums.Clear();\n\n/* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nnums.Add(1);\nnums.Add(3);\nnums.Add(2);\nnums.Add(5);\nnums.Add(4);\n\n/* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nnums.Insert(3, 6);\n\n/* \u5220\u9664\u5143\u7d20 */\nnums.RemoveAt(3);\n
list_test.go/* \u6e05\u7a7a\u5217\u8868 */\nnums = nil\n\n/* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nnums = append(nums, 1)\nnums = append(nums, 3)\nnums = append(nums, 2)\nnums = append(nums, 5)\nnums = append(nums, 4)\n\n/* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nnums = append(nums[:3], append([]int{6}, nums[3:]...)...) // \u5728\u7d22\u5f15 3 \u5904\u63d2\u5165\u6570\u5b57 6\n\n/* \u5220\u9664\u5143\u7d20 */\nnums = append(nums[:3], nums[4:]...) // \u5220\u9664\u7d22\u5f15 3 \u5904\u7684\u5143\u7d20\n
list.swift/* \u6e05\u7a7a\u5217\u8868 */\nnums.removeAll()\n\n/* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nnums.append(1)\nnums.append(3)\nnums.append(2)\nnums.append(5)\nnums.append(4)\n\n/* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nnums.insert(6, at: 3) // \u5728\u7d22\u5f15 3 \u5904\u63d2\u5165\u6570\u5b57 6\n\n/* \u5220\u9664\u5143\u7d20 */\nnums.remove(at: 3) // \u5220\u9664\u7d22\u5f15 3 \u5904\u7684\u5143\u7d20\n
list.js/* \u6e05\u7a7a\u5217\u8868 */\nnums.length = 0;\n\n/* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nnums.push(1);\nnums.push(3);\nnums.push(2);\nnums.push(5);\nnums.push(4);\n\n/* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nnums.splice(3, 0, 6);\n\n/* \u5220\u9664\u5143\u7d20 */\nnums.splice(3, 1);\n
list.ts/* \u6e05\u7a7a\u5217\u8868 */\nnums.length = 0;\n\n/* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nnums.push(1);\nnums.push(3);\nnums.push(2);\nnums.push(5);\nnums.push(4);\n\n/* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nnums.splice(3, 0, 6);\n\n/* \u5220\u9664\u5143\u7d20 */\nnums.splice(3, 1);\n
list.dart/* \u6e05\u7a7a\u5217\u8868 */\nnums.clear();\n\n/* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nnums.add(1);\nnums.add(3);\nnums.add(2);\nnums.add(5);\nnums.add(4);\n\n/* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nnums.insert(3, 6); // \u5728\u7d22\u5f15 3 \u5904\u63d2\u5165\u6570\u5b57 6\n\n/* \u5220\u9664\u5143\u7d20 */\nnums.removeAt(3); // \u5220\u9664\u7d22\u5f15 3 \u5904\u7684\u5143\u7d20\n
list.rs/* \u6e05\u7a7a\u5217\u8868 */\nnums.clear();\n\n/* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nnums.push(1);\nnums.push(3);\nnums.push(2);\nnums.push(5);\nnums.push(4);\n\n/* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nnums.insert(3, 6); // \u5728\u7d22\u5f15 3 \u5904\u63d2\u5165\u6570\u5b57 6\n\n/* \u5220\u9664\u5143\u7d20 */\nnums.remove(3); // \u5220\u9664\u7d22\u5f15 3 \u5904\u7684\u5143\u7d20\n
list.c// C \u672a\u63d0\u4f9b\u5185\u7f6e\u52a8\u6001\u6570\u7ec4\n
list.zig// \u6e05\u7a7a\u5217\u8868\nnums.clearRetainingCapacity();\n\n// \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20\ntry nums.append(1);\ntry nums.append(3);\ntry nums.append(2);\ntry nums.append(5);\ntry nums.append(4);\n\n// \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20\ntry nums.insert(3, 6); // \u5728\u7d22\u5f15 3 \u5904\u63d2\u5165\u6570\u5b57 6\n\n// \u5220\u9664\u5143\u7d20\n_ = nums.orderedRemove(3); // \u5220\u9664\u7d22\u5f15 3 \u5904\u7684\u5143\u7d20\n
"},{"location":"chapter_array_and_linkedlist/list/#4","title":"4. \u00a0 \u904d\u5386\u5217\u8868","text":"\u4e0e\u6570\u7ec4\u4e00\u6837\uff0c\u5217\u8868\u53ef\u4ee5\u6839\u636e\u7d22\u5f15\u904d\u5386\uff0c\u4e5f\u53ef\u4ee5\u76f4\u63a5\u904d\u5386\u5404\u5143\u7d20\u3002
PythonC++JavaC#GoSwiftJSTSDartRustCZig list.py# \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868\ncount = 0\nfor i in range(len(nums)):\n count += nums[i]\n\n# \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20\nfor num in nums:\n count += num\n
list.cpp/* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\nint count = 0;\nfor (int i = 0; i < nums.size(); i++) {\n count += nums[i];\n}\n\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\ncount = 0;\nfor (int num : nums) {\n count += num;\n}\n
list.java/* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\nint count = 0;\nfor (int i = 0; i < nums.size(); i++) {\n count += nums.get(i);\n}\n\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\nfor (int num : nums) {\n count += num;\n}\n
list.cs/* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\nint count = 0;\nfor (int i = 0; i < nums.Count; i++) {\n count += nums[i];\n}\n\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\ncount = 0;\nforeach (int num in nums) {\n count += num;\n}\n
list_test.go/* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\ncount := 0\nfor i := 0; i < len(nums); i++ {\n count += nums[i]\n}\n\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\ncount = 0\nfor _, num := range nums {\n count += num\n}\n
list.swift/* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\nvar count = 0\nfor i in nums.indices {\n count += nums[i]\n}\n\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\ncount = 0\nfor num in nums {\n count += num\n}\n
list.js/* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\nlet count = 0;\nfor (let i = 0; i < nums.length; i++) {\n count += nums[i];\n}\n\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\ncount = 0;\nfor (const num of nums) {\n count += num;\n}\n
list.ts/* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\nlet count = 0;\nfor (let i = 0; i < nums.length; i++) {\n count += nums[i];\n}\n\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\ncount = 0;\nfor (const num of nums) {\n count += num;\n}\n
list.dart/* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\nint count = 0;\nfor (var i = 0; i < nums.length; i++) {\n count += nums[i];\n}\n\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\ncount = 0;\nfor (var num in nums) {\n count += num;\n}\n
list.rs// \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868\nlet mut _count = 0;\nfor i in 0..nums.len() {\n _count += nums[i];\n}\n\n// \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20\n_count = 0;\nfor num in &nums {\n _count += num;\n}\n
list.c// C \u672a\u63d0\u4f9b\u5185\u7f6e\u52a8\u6001\u6570\u7ec4\n
list.zig// \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868\nvar count: i32 = 0;\nvar i: i32 = 0;\nwhile (i < nums.items.len) : (i += 1) {\n count += nums[i];\n}\n\n// \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20\ncount = 0;\nfor (nums.items) |num| {\n count += num;\n}\n
"},{"location":"chapter_array_and_linkedlist/list/#5","title":"5. \u00a0 \u62fc\u63a5\u5217\u8868","text":"\u7ed9\u5b9a\u4e00\u4e2a\u65b0\u5217\u8868 nums1
\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5176\u62fc\u63a5\u5230\u539f\u5217\u8868\u7684\u5c3e\u90e8\u3002
# \u62fc\u63a5\u4e24\u4e2a\u5217\u8868\nnums1: list[int] = [6, 8, 7, 10, 9]\nnums += nums1 # \u5c06\u5217\u8868 nums1 \u62fc\u63a5\u5230 nums \u4e4b\u540e\n
list.cpp/* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nvector<int> nums1 = { 6, 8, 7, 10, 9 };\n// \u5c06\u5217\u8868 nums1 \u62fc\u63a5\u5230 nums \u4e4b\u540e\nnums.insert(nums.end(), nums1.begin(), nums1.end());\n
list.java/* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nList<Integer> nums1 = new ArrayList<>(Arrays.asList(new Integer[] { 6, 8, 7, 10, 9 }));\nnums.addAll(nums1); // \u5c06\u5217\u8868 nums1 \u62fc\u63a5\u5230 nums \u4e4b\u540e\n
list.cs/* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nList<int> nums1 = [6, 8, 7, 10, 9];\nnums.AddRange(nums1); // \u5c06\u5217\u8868 nums1 \u62fc\u63a5\u5230 nums \u4e4b\u540e\n
list_test.go/* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nnums1 := []int{6, 8, 7, 10, 9}\nnums = append(nums, nums1...) // \u5c06\u5217\u8868 nums1 \u62fc\u63a5\u5230 nums \u4e4b\u540e\n
list.swift/* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nlet nums1 = [6, 8, 7, 10, 9]\nnums.append(contentsOf: nums1) // \u5c06\u5217\u8868 nums1 \u62fc\u63a5\u5230 nums \u4e4b\u540e\n
list.js/* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nconst nums1 = [6, 8, 7, 10, 9];\nnums.push(...nums1); // \u5c06\u5217\u8868 nums1 \u62fc\u63a5\u5230 nums \u4e4b\u540e\n
list.ts/* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nconst nums1: number[] = [6, 8, 7, 10, 9];\nnums.push(...nums1); // \u5c06\u5217\u8868 nums1 \u62fc\u63a5\u5230 nums \u4e4b\u540e\n
list.dart/* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nList<int> nums1 = [6, 8, 7, 10, 9];\nnums.addAll(nums1); // \u5c06\u5217\u8868 nums1 \u62fc\u63a5\u5230 nums \u4e4b\u540e\n
list.rs/* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nlet nums1: Vec<i32> = vec![6, 8, 7, 10, 9];\nnums.extend(nums1);\n
list.c// C \u672a\u63d0\u4f9b\u5185\u7f6e\u52a8\u6001\u6570\u7ec4\n
list.zig// \u62fc\u63a5\u4e24\u4e2a\u5217\u8868\nvar nums1 = std.ArrayList(i32).init(std.heap.page_allocator);\ndefer nums1.deinit();\ntry nums1.appendSlice(&[_]i32{ 6, 8, 7, 10, 9 });\ntry nums.insertSlice(nums.items.len, nums1.items); // \u5c06\u5217\u8868 nums1 \u62fc\u63a5\u5230 nums \u4e4b\u540e\n
"},{"location":"chapter_array_and_linkedlist/list/#6","title":"6. \u00a0 \u6392\u5e8f\u5217\u8868","text":"\u5b8c\u6210\u5217\u8868\u6392\u5e8f\u540e\uff0c\u6211\u4eec\u4fbf\u53ef\u4ee5\u4f7f\u7528\u5728\u6570\u7ec4\u7c7b\u7b97\u6cd5\u9898\u4e2d\u7ecf\u5e38\u8003\u67e5\u7684\u201c\u4e8c\u5206\u67e5\u627e\u201d\u548c\u201c\u53cc\u6307\u9488\u201d\u7b97\u6cd5\u3002
PythonC++JavaC#GoSwiftJSTSDartRustCZig list.py# \u6392\u5e8f\u5217\u8868\nnums.sort() # \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n
list.cpp/* \u6392\u5e8f\u5217\u8868 */\nsort(nums.begin(), nums.end()); // \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n
list.java/* \u6392\u5e8f\u5217\u8868 */\nCollections.sort(nums); // \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n
list.cs/* \u6392\u5e8f\u5217\u8868 */\nnums.Sort(); // \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n
list_test.go/* \u6392\u5e8f\u5217\u8868 */\nsort.Ints(nums) // \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n
list.swift/* \u6392\u5e8f\u5217\u8868 */\nnums.sort() // \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n
list.js/* \u6392\u5e8f\u5217\u8868 */ \nnums.sort((a, b) => a - b); // \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n
list.ts/* \u6392\u5e8f\u5217\u8868 */\nnums.sort((a, b) => a - b); // \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n
list.dart/* \u6392\u5e8f\u5217\u8868 */\nnums.sort(); // \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n
list.rs/* \u6392\u5e8f\u5217\u8868 */\nnums.sort(); // \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n
list.c// C \u672a\u63d0\u4f9b\u5185\u7f6e\u52a8\u6001\u6570\u7ec4\n
list.zig// \u6392\u5e8f\u5217\u8868\nstd.sort.sort(i32, nums.items, {}, comptime std.sort.asc(i32));\n
"},{"location":"chapter_array_and_linkedlist/list/#432","title":"4.3.2 \u00a0 \u5217\u8868\u5b9e\u73b0","text":"\u8bb8\u591a\u7f16\u7a0b\u8bed\u8a00\u5185\u7f6e\u4e86\u5217\u8868\uff0c\u4f8b\u5982 Java\u3001C++\u3001Python \u7b49\u3002\u5b83\u4eec\u7684\u5b9e\u73b0\u6bd4\u8f83\u590d\u6742\uff0c\u5404\u4e2a\u53c2\u6570\u7684\u8bbe\u5b9a\u4e5f\u975e\u5e38\u8003\u7a76\uff0c\u4f8b\u5982\u521d\u59cb\u5bb9\u91cf\u3001\u6269\u5bb9\u500d\u6570\u7b49\u3002\u611f\u5174\u8da3\u7684\u8bfb\u8005\u53ef\u4ee5\u67e5\u9605\u6e90\u7801\u8fdb\u884c\u5b66\u4e60\u3002
\u4e3a\u4e86\u52a0\u6df1\u5bf9\u5217\u8868\u5de5\u4f5c\u539f\u7406\u7684\u7406\u89e3\uff0c\u6211\u4eec\u5c1d\u8bd5\u5b9e\u73b0\u4e00\u4e2a\u7b80\u6613\u7248\u5217\u8868\uff0c\u5305\u62ec\u4ee5\u4e0b\u4e09\u4e2a\u91cd\u70b9\u8bbe\u8ba1\u3002
size
\uff0c\u7528\u4e8e\u8bb0\u5f55\u5217\u8868\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff0c\u5e76\u968f\u7740\u5143\u7d20\u63d2\u5165\u548c\u5220\u9664\u5b9e\u65f6\u66f4\u65b0\u3002\u6839\u636e\u6b64\u53d8\u91cf\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9a\u4f4d\u5217\u8868\u5c3e\u90e8\uff0c\u4ee5\u53ca\u5224\u65ad\u662f\u5426\u9700\u8981\u6269\u5bb9\u3002class MyList:\n \"\"\"\u5217\u8868\u7c7b\"\"\"\n\n def __init__(self):\n \"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n self._capacity: int = 10 # \u5217\u8868\u5bb9\u91cf\n self._arr: list[int] = [0] * self._capacity # \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\n self._size: int = 0 # \u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\n self._extend_ratio: int = 2 # \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n\n def size(self) -> int:\n \"\"\"\u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\"\"\"\n return self._size\n\n def capacity(self) -> int:\n \"\"\"\u83b7\u53d6\u5217\u8868\u5bb9\u91cf\"\"\"\n return self._capacity\n\n def get(self, index: int) -> int:\n \"\"\"\u8bbf\u95ee\u5143\u7d20\"\"\"\n # \u7d22\u5f15\u5982\u679c\u8d8a\u754c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\n if index < 0 or index >= self._size:\n raise IndexError(\"\u7d22\u5f15\u8d8a\u754c\")\n return self._arr[index]\n\n def set(self, num: int, index: int):\n \"\"\"\u66f4\u65b0\u5143\u7d20\"\"\"\n if index < 0 or index >= self._size:\n raise IndexError(\"\u7d22\u5f15\u8d8a\u754c\")\n self._arr[index] = num\n\n def add(self, num: int):\n \"\"\"\u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20\"\"\"\n # \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if self.size() == self.capacity():\n self.extend_capacity()\n self._arr[self._size] = num\n self._size += 1\n\n def insert(self, num: int, index: int):\n \"\"\"\u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20\"\"\"\n if index < 0 or index >= self._size:\n raise IndexError(\"\u7d22\u5f15\u8d8a\u754c\")\n # \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if self._size == self.capacity():\n self.extend_capacity()\n # \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for j in range(self._size - 1, index - 1, -1):\n self._arr[j + 1] = self._arr[j]\n self._arr[index] = num\n # \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n self._size += 1\n\n def remove(self, index: int) -> int:\n \"\"\"\u5220\u9664\u5143\u7d20\"\"\"\n if index < 0 or index >= self._size:\n raise IndexError(\"\u7d22\u5f15\u8d8a\u754c\")\n num = self._arr[index]\n # \u7d22\u5f15 i \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for j in range(index, self._size - 1):\n self._arr[j] = self._arr[j + 1]\n # \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n self._size -= 1\n # \u8fd4\u56de\u88ab\u5220\u9664\u5143\u7d20\n return num\n\n def extend_capacity(self):\n \"\"\"\u5217\u8868\u6269\u5bb9\"\"\"\n # \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a\u539f\u6570\u7ec4 __extend_ratio \u500d\u7684\u65b0\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u62f7\u8d1d\u5230\u65b0\u6570\u7ec4\n self._arr = self._arr + [0] * self.capacity() * (self._extend_ratio - 1)\n # \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\n self._capacity = len(self._arr)\n\n def to_array(self) -> list[int]:\n \"\"\"\u8fd4\u56de\u6709\u6548\u957f\u5ea6\u7684\u5217\u8868\"\"\"\n return self._arr[: self._size]\n
my_list.cpp/* \u5217\u8868\u7c7b */\nclass MyList {\n private:\n int *arr; // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\n int arrCapacity = 10; // \u5217\u8868\u5bb9\u91cf\n int arrSize = 0; // \u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\n int extendRatio = 2; // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n\n public:\n /* \u6784\u9020\u65b9\u6cd5 */\n MyList() {\n arr = new int[arrCapacity];\n }\n\n /* \u6790\u6784\u65b9\u6cd5 */\n ~MyList() {\n delete[] arr;\n }\n\n /* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09*/\n int size() {\n return arrSize;\n }\n\n /* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\n int capacity() {\n return arrCapacity;\n }\n\n /* \u8bbf\u95ee\u5143\u7d20 */\n int get(int index) {\n // \u7d22\u5f15\u5982\u679c\u8d8a\u754c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\n if (index < 0 || index >= size())\n throw out_of_range(\"\u7d22\u5f15\u8d8a\u754c\");\n return arr[index];\n }\n\n /* \u66f4\u65b0\u5143\u7d20 */\n void set(int index, int num) {\n if (index < 0 || index >= size())\n throw out_of_range(\"\u7d22\u5f15\u8d8a\u754c\");\n arr[index] = num;\n }\n\n /* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\n void add(int num) {\n // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if (size() == capacity())\n extendCapacity();\n arr[size()] = num;\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n arrSize++;\n }\n\n /* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\n void insert(int index, int num) {\n if (index < 0 || index >= size())\n throw out_of_range(\"\u7d22\u5f15\u8d8a\u754c\");\n // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if (size() == capacity())\n extendCapacity();\n // \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for (int j = size() - 1; j >= index; j--) {\n arr[j + 1] = arr[j];\n }\n arr[index] = num;\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n arrSize++;\n }\n\n /* \u5220\u9664\u5143\u7d20 */\n int remove(int index) {\n if (index < 0 || index >= size())\n throw out_of_range(\"\u7d22\u5f15\u8d8a\u754c\");\n int num = arr[index];\n // \u7d22\u5f15 i \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for (int j = index; j < size() - 1; j++) {\n arr[j] = arr[j + 1];\n }\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n arrSize--;\n // \u8fd4\u56de\u88ab\u5220\u9664\u5143\u7d20\n return num;\n }\n\n /* \u5217\u8868\u6269\u5bb9 */\n void extendCapacity() {\n // \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a\u539f\u6570\u7ec4 extendRatio \u500d\u7684\u65b0\u6570\u7ec4\n int newCapacity = capacity() * extendRatio;\n int *tmp = arr;\n arr = new int[newCapacity];\n // \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\n for (int i = 0; i < size(); i++) {\n arr[i] = tmp[i];\n }\n // \u91ca\u653e\u5185\u5b58\n delete[] tmp;\n arrCapacity = newCapacity;\n }\n\n /* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a Vector \u7528\u4e8e\u6253\u5370 */\n vector<int> toVector() {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n vector<int> vec(size());\n for (int i = 0; i < size(); i++) {\n vec[i] = arr[i];\n }\n return vec;\n }\n};\n
my_list.java/* \u5217\u8868\u7c7b */\nclass MyList {\n private int[] arr; // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\n private int capacity = 10; // \u5217\u8868\u5bb9\u91cf\n private int size = 0; // \u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\n private int extendRatio = 2; // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n\n /* \u6784\u9020\u65b9\u6cd5 */\n public MyList() {\n arr = new int[capacity];\n }\n\n /* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09 */\n public int size() {\n return size;\n }\n\n /* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\n public int capacity() {\n return capacity;\n }\n\n /* \u8bbf\u95ee\u5143\u7d20 */\n public int get(int index) {\n // \u7d22\u5f15\u5982\u679c\u8d8a\u754c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\n if (index < 0 || index >= size)\n throw new IndexOutOfBoundsException(\"\u7d22\u5f15\u8d8a\u754c\");\n return arr[index];\n }\n\n /* \u66f4\u65b0\u5143\u7d20 */\n public void set(int index, int num) {\n if (index < 0 || index >= size)\n throw new IndexOutOfBoundsException(\"\u7d22\u5f15\u8d8a\u754c\");\n arr[index] = num;\n }\n\n /* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\n public void add(int num) {\n // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if (size == capacity())\n extendCapacity();\n arr[size] = num;\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n size++;\n }\n\n /* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\n public void insert(int index, int num) {\n if (index < 0 || index >= size)\n throw new IndexOutOfBoundsException(\"\u7d22\u5f15\u8d8a\u754c\");\n // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if (size == capacity())\n extendCapacity();\n // \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for (int j = size - 1; j >= index; j--) {\n arr[j + 1] = arr[j];\n }\n arr[index] = num;\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n size++;\n }\n\n /* \u5220\u9664\u5143\u7d20 */\n public int remove(int index) {\n if (index < 0 || index >= size)\n throw new IndexOutOfBoundsException(\"\u7d22\u5f15\u8d8a\u754c\");\n int num = arr[index];\n // \u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for (int j = index; j < size - 1; j++) {\n arr[j] = arr[j + 1];\n }\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n size--;\n // \u8fd4\u56de\u88ab\u5220\u9664\u5143\u7d20\n return num;\n }\n\n /* \u5217\u8868\u6269\u5bb9 */\n public void extendCapacity() {\n // \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a\u539f\u6570\u7ec4 extendRatio \u500d\u7684\u65b0\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u62f7\u8d1d\u5230\u65b0\u6570\u7ec4\n arr = Arrays.copyOf(arr, capacity() * extendRatio);\n // \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\n capacity = arr.length;\n }\n\n /* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4 */\n public int[] toArray() {\n int size = size();\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n int[] arr = new int[size];\n for (int i = 0; i < size; i++) {\n arr[i] = get(i);\n }\n return arr;\n }\n}\n
my_list.cs/* \u5217\u8868\u7c7b */\nclass MyList {\n private int[] arr; // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\n private int arrCapacity = 10; // \u5217\u8868\u5bb9\u91cf\n private int arrSize = 0; // \u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\n private readonly int extendRatio = 2; // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n\n /* \u6784\u9020\u65b9\u6cd5 */\n public MyList() {\n arr = new int[arrCapacity];\n }\n\n /* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09*/\n public int Size() {\n return arrSize;\n }\n\n /* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\n public int Capacity() {\n return arrCapacity;\n }\n\n /* \u8bbf\u95ee\u5143\u7d20 */\n public int Get(int index) {\n // \u7d22\u5f15\u5982\u679c\u8d8a\u754c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\n if (index < 0 || index >= arrSize)\n throw new IndexOutOfRangeException(\"\u7d22\u5f15\u8d8a\u754c\");\n return arr[index];\n }\n\n /* \u66f4\u65b0\u5143\u7d20 */\n public void Set(int index, int num) {\n if (index < 0 || index >= arrSize)\n throw new IndexOutOfRangeException(\"\u7d22\u5f15\u8d8a\u754c\");\n arr[index] = num;\n }\n\n /* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\n public void Add(int num) {\n // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if (arrSize == arrCapacity)\n ExtendCapacity();\n arr[arrSize] = num;\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n arrSize++;\n }\n\n /* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\n public void Insert(int index, int num) {\n if (index < 0 || index >= arrSize)\n throw new IndexOutOfRangeException(\"\u7d22\u5f15\u8d8a\u754c\");\n // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if (arrSize == arrCapacity)\n ExtendCapacity();\n // \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for (int j = arrSize - 1; j >= index; j--) {\n arr[j + 1] = arr[j];\n }\n arr[index] = num;\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n arrSize++;\n }\n\n /* \u5220\u9664\u5143\u7d20 */\n public int Remove(int index) {\n if (index < 0 || index >= arrSize)\n throw new IndexOutOfRangeException(\"\u7d22\u5f15\u8d8a\u754c\");\n int num = arr[index];\n // \u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for (int j = index; j < arrSize - 1; j++) {\n arr[j] = arr[j + 1];\n }\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n arrSize--;\n // \u8fd4\u56de\u88ab\u5220\u9664\u5143\u7d20\n return num;\n }\n\n /* \u5217\u8868\u6269\u5bb9 */\n public void ExtendCapacity() {\n // \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a arrCapacity * extendRatio \u7684\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u62f7\u8d1d\u5230\u65b0\u6570\u7ec4\n Array.Resize(ref arr, arrCapacity * extendRatio);\n // \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\n arrCapacity = arr.Length;\n }\n\n /* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4 */\n public int[] ToArray() {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n int[] arr = new int[arrSize];\n for (int i = 0; i < arrSize; i++) {\n arr[i] = Get(i);\n }\n return arr;\n }\n}\n
my_list.go/* \u5217\u8868\u7c7b */\ntype myList struct {\n arrCapacity int\n arr []int\n arrSize int\n extendRatio int\n}\n\n/* \u6784\u9020\u51fd\u6570 */\nfunc newMyList() *myList {\n return &myList{\n arrCapacity: 10, // \u5217\u8868\u5bb9\u91cf\n arr: make([]int, 10), // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\n arrSize: 0, // \u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\n extendRatio: 2, // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n }\n}\n\n/* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09 */\nfunc (l *myList) size() int {\n return l.arrSize\n}\n\n/* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\nfunc (l *myList) capacity() int {\n return l.arrCapacity\n}\n\n/* \u8bbf\u95ee\u5143\u7d20 */\nfunc (l *myList) get(index int) int {\n // \u7d22\u5f15\u5982\u679c\u8d8a\u754c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\n if index < 0 || index >= l.arrSize {\n panic(\"\u7d22\u5f15\u8d8a\u754c\")\n }\n return l.arr[index]\n}\n\n/* \u66f4\u65b0\u5143\u7d20 */\nfunc (l *myList) set(num, index int) {\n if index < 0 || index >= l.arrSize {\n panic(\"\u7d22\u5f15\u8d8a\u754c\")\n }\n l.arr[index] = num\n}\n\n/* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nfunc (l *myList) add(num int) {\n // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if l.arrSize == l.arrCapacity {\n l.extendCapacity()\n }\n l.arr[l.arrSize] = num\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n l.arrSize++\n}\n\n/* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nfunc (l *myList) insert(num, index int) {\n if index < 0 || index >= l.arrSize {\n panic(\"\u7d22\u5f15\u8d8a\u754c\")\n }\n // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if l.arrSize == l.arrCapacity {\n l.extendCapacity()\n }\n // \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for j := l.arrSize - 1; j >= index; j-- {\n l.arr[j+1] = l.arr[j]\n }\n l.arr[index] = num\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n l.arrSize++\n}\n\n/* \u5220\u9664\u5143\u7d20 */\nfunc (l *myList) remove(index int) int {\n if index < 0 || index >= l.arrSize {\n panic(\"\u7d22\u5f15\u8d8a\u754c\")\n }\n num := l.arr[index]\n // \u7d22\u5f15 i \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for j := index; j < l.arrSize-1; j++ {\n l.arr[j] = l.arr[j+1]\n }\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n l.arrSize--\n // \u8fd4\u56de\u88ab\u5220\u9664\u5143\u7d20\n return num\n}\n\n/* \u5217\u8868\u6269\u5bb9 */\nfunc (l *myList) extendCapacity() {\n // \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a\u539f\u6570\u7ec4 extendRatio \u500d\u7684\u65b0\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u62f7\u8d1d\u5230\u65b0\u6570\u7ec4\n l.arr = append(l.arr, make([]int, l.arrCapacity*(l.extendRatio-1))...)\n // \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\n l.arrCapacity = len(l.arr)\n}\n\n/* \u8fd4\u56de\u6709\u6548\u957f\u5ea6\u7684\u5217\u8868 */\nfunc (l *myList) toArray() []int {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n return l.arr[:l.arrSize]\n}\n
my_list.swift/* \u5217\u8868\u7c7b */\nclass MyList {\n private var arr: [Int] // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\n private var _capacity = 10 // \u5217\u8868\u5bb9\u91cf\n private var _size = 0 // \u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\n private let extendRatio = 2 // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n\n /* \u6784\u9020\u65b9\u6cd5 */\n init() {\n arr = Array(repeating: 0, count: _capacity)\n }\n\n /* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09*/\n func size() -> Int {\n _size\n }\n\n /* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\n func capacity() -> Int {\n _capacity\n }\n\n /* \u8bbf\u95ee\u5143\u7d20 */\n func get(index: Int) -> Int {\n // \u7d22\u5f15\u5982\u679c\u8d8a\u754c\u5219\u629b\u51fa\u9519\u8bef\uff0c\u4e0b\u540c\n if index < 0 || index >= _size {\n fatalError(\"\u7d22\u5f15\u8d8a\u754c\")\n }\n return arr[index]\n }\n\n /* \u66f4\u65b0\u5143\u7d20 */\n func set(index: Int, num: Int) {\n if index < 0 || index >= _size {\n fatalError(\"\u7d22\u5f15\u8d8a\u754c\")\n }\n arr[index] = num\n }\n\n /* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\n func add(num: Int) {\n // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if _size == _capacity {\n extendCapacity()\n }\n arr[_size] = num\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n _size += 1\n }\n\n /* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\n func insert(index: Int, num: Int) {\n if index < 0 || index >= _size {\n fatalError(\"\u7d22\u5f15\u8d8a\u754c\")\n }\n // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if _size == _capacity {\n extendCapacity()\n }\n // \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for j in sequence(first: _size - 1, next: { $0 >= index + 1 ? $0 - 1 : nil }) {\n arr[j + 1] = arr[j]\n }\n arr[index] = num\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n _size += 1\n }\n\n /* \u5220\u9664\u5143\u7d20 */\n @discardableResult\n func remove(index: Int) -> Int {\n if index < 0 || index >= _size {\n fatalError(\"\u7d22\u5f15\u8d8a\u754c\")\n }\n let num = arr[index]\n // \u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for j in index ..< (_size - 1) {\n arr[j] = arr[j + 1]\n }\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n _size -= 1\n // \u8fd4\u56de\u88ab\u5220\u9664\u5143\u7d20\n return num\n }\n\n /* \u5217\u8868\u6269\u5bb9 */\n func extendCapacity() {\n // \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a\u539f\u6570\u7ec4 extendRatio \u500d\u7684\u65b0\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u62f7\u8d1d\u5230\u65b0\u6570\u7ec4\n arr = arr + Array(repeating: 0, count: _capacity * (extendRatio - 1))\n // \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\n _capacity = arr.count\n }\n\n /* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4 */\n func toArray() -> [Int] {\n var arr = Array(repeating: 0, count: _size)\n for i in 0 ..< _size {\n arr[i] = get(index: i)\n }\n return arr\n }\n}\n
my_list.js/* \u5217\u8868\u7c7b */\nclass MyList {\n #arr = new Array(); // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\n #capacity = 10; // \u5217\u8868\u5bb9\u91cf\n #size = 0; // \u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\n #extendRatio = 2; // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n\n /* \u6784\u9020\u65b9\u6cd5 */\n constructor() {\n this.#arr = new Array(this.#capacity);\n }\n\n /* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09*/\n size() {\n return this.#size;\n }\n\n /* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\n capacity() {\n return this.#capacity;\n }\n\n /* \u8bbf\u95ee\u5143\u7d20 */\n get(index) {\n // \u7d22\u5f15\u5982\u679c\u8d8a\u754c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\n if (index < 0 || index >= this.#size) throw new Error('\u7d22\u5f15\u8d8a\u754c');\n return this.#arr[index];\n }\n\n /* \u66f4\u65b0\u5143\u7d20 */\n set(index, num) {\n if (index < 0 || index >= this.#size) throw new Error('\u7d22\u5f15\u8d8a\u754c');\n this.#arr[index] = num;\n }\n\n /* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\n add(num) {\n // \u5982\u679c\u957f\u5ea6\u7b49\u4e8e\u5bb9\u91cf\uff0c\u5219\u9700\u8981\u6269\u5bb9\n if (this.#size === this.#capacity) {\n this.extendCapacity();\n }\n // \u5c06\u65b0\u5143\u7d20\u6dfb\u52a0\u5230\u5217\u8868\u5c3e\u90e8\n this.#arr[this.#size] = num;\n this.#size++;\n }\n\n /* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\n insert(index, num) {\n if (index < 0 || index >= this.#size) throw new Error('\u7d22\u5f15\u8d8a\u754c');\n // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if (this.#size === this.#capacity) {\n this.extendCapacity();\n }\n // \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for (let j = this.#size - 1; j >= index; j--) {\n this.#arr[j + 1] = this.#arr[j];\n }\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n this.#arr[index] = num;\n this.#size++;\n }\n\n /* \u5220\u9664\u5143\u7d20 */\n remove(index) {\n if (index < 0 || index >= this.#size) throw new Error('\u7d22\u5f15\u8d8a\u754c');\n let num = this.#arr[index];\n // \u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for (let j = index; j < this.#size - 1; j++) {\n this.#arr[j] = this.#arr[j + 1];\n }\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n this.#size--;\n // \u8fd4\u56de\u88ab\u5220\u9664\u5143\u7d20\n return num;\n }\n\n /* \u5217\u8868\u6269\u5bb9 */\n extendCapacity() {\n // \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a\u539f\u6570\u7ec4 extendRatio \u500d\u7684\u65b0\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u62f7\u8d1d\u5230\u65b0\u6570\u7ec4\n this.#arr = this.#arr.concat(\n new Array(this.capacity() * (this.#extendRatio - 1))\n );\n // \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\n this.#capacity = this.#arr.length;\n }\n\n /* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4 */\n toArray() {\n let size = this.size();\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n const arr = new Array(size);\n for (let i = 0; i < size; i++) {\n arr[i] = this.get(i);\n }\n return arr;\n }\n}\n
my_list.ts/* \u5217\u8868\u7c7b */\nclass MyList {\n private arr: Array<number>; // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\n private _capacity: number = 10; // \u5217\u8868\u5bb9\u91cf\n private _size: number = 0; // \u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\n private extendRatio: number = 2; // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n\n /* \u6784\u9020\u65b9\u6cd5 */\n constructor() {\n this.arr = new Array(this._capacity);\n }\n\n /* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09*/\n public size(): number {\n return this._size;\n }\n\n /* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\n public capacity(): number {\n return this._capacity;\n }\n\n /* \u8bbf\u95ee\u5143\u7d20 */\n public get(index: number): number {\n // \u7d22\u5f15\u5982\u679c\u8d8a\u754c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\n if (index < 0 || index >= this._size) throw new Error('\u7d22\u5f15\u8d8a\u754c');\n return this.arr[index];\n }\n\n /* \u66f4\u65b0\u5143\u7d20 */\n public set(index: number, num: number): void {\n if (index < 0 || index >= this._size) throw new Error('\u7d22\u5f15\u8d8a\u754c');\n this.arr[index] = num;\n }\n\n /* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\n public add(num: number): void {\n // \u5982\u679c\u957f\u5ea6\u7b49\u4e8e\u5bb9\u91cf\uff0c\u5219\u9700\u8981\u6269\u5bb9\n if (this._size === this._capacity) this.extendCapacity();\n // \u5c06\u65b0\u5143\u7d20\u6dfb\u52a0\u5230\u5217\u8868\u5c3e\u90e8\n this.arr[this._size] = num;\n this._size++;\n }\n\n /* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\n public insert(index: number, num: number): void {\n if (index < 0 || index >= this._size) throw new Error('\u7d22\u5f15\u8d8a\u754c');\n // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if (this._size === this._capacity) {\n this.extendCapacity();\n }\n // \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for (let j = this._size - 1; j >= index; j--) {\n this.arr[j + 1] = this.arr[j];\n }\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n this.arr[index] = num;\n this._size++;\n }\n\n /* \u5220\u9664\u5143\u7d20 */\n public remove(index: number): number {\n if (index < 0 || index >= this._size) throw new Error('\u7d22\u5f15\u8d8a\u754c');\n let num = this.arr[index];\n // \u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for (let j = index; j < this._size - 1; j++) {\n this.arr[j] = this.arr[j + 1];\n }\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n this._size--;\n // \u8fd4\u56de\u88ab\u5220\u9664\u5143\u7d20\n return num;\n }\n\n /* \u5217\u8868\u6269\u5bb9 */\n public extendCapacity(): void {\n // \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a size \u7684\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u62f7\u8d1d\u5230\u65b0\u6570\u7ec4\n this.arr = this.arr.concat(\n new Array(this.capacity() * (this.extendRatio - 1))\n );\n // \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\n this._capacity = this.arr.length;\n }\n\n /* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4 */\n public toArray(): number[] {\n let size = this.size();\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n const arr = new Array(size);\n for (let i = 0; i < size; i++) {\n arr[i] = this.get(i);\n }\n return arr;\n }\n}\n
my_list.dart/* \u5217\u8868\u7c7b */\nclass MyList {\n late List<int> _arr; // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\n int _capacity = 10; // \u5217\u8868\u5bb9\u91cf\n int _size = 0; // \u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\n int _extendRatio = 2; // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n\n /* \u6784\u9020\u65b9\u6cd5 */\n MyList() {\n _arr = List.filled(_capacity, 0);\n }\n\n /* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09*/\n int size() => _size;\n\n /* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\n int capacity() => _capacity;\n\n /* \u8bbf\u95ee\u5143\u7d20 */\n int get(int index) {\n if (index >= _size) throw RangeError('\u7d22\u5f15\u8d8a\u754c');\n return _arr[index];\n }\n\n /* \u66f4\u65b0\u5143\u7d20 */\n void set(int index, int _num) {\n if (index >= _size) throw RangeError('\u7d22\u5f15\u8d8a\u754c');\n _arr[index] = _num;\n }\n\n /* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\n void add(int _num) {\n // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if (_size == _capacity) extendCapacity();\n _arr[_size] = _num;\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n _size++;\n }\n\n /* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\n void insert(int index, int _num) {\n if (index >= _size) throw RangeError('\u7d22\u5f15\u8d8a\u754c');\n // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if (_size == _capacity) extendCapacity();\n // \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for (var j = _size - 1; j >= index; j--) {\n _arr[j + 1] = _arr[j];\n }\n _arr[index] = _num;\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n _size++;\n }\n\n /* \u5220\u9664\u5143\u7d20 */\n int remove(int index) {\n if (index >= _size) throw RangeError('\u7d22\u5f15\u8d8a\u754c');\n int _num = _arr[index];\n // \u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for (var j = index; j < _size - 1; j++) {\n _arr[j] = _arr[j + 1];\n }\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n _size--;\n // \u8fd4\u56de\u88ab\u5220\u9664\u5143\u7d20\n return _num;\n }\n\n /* \u5217\u8868\u6269\u5bb9 */\n void extendCapacity() {\n // \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a\u539f\u6570\u7ec4 _extendRatio \u500d\u7684\u65b0\u6570\u7ec4\n final _newNums = List.filled(_capacity * _extendRatio, 0);\n // \u5c06\u539f\u6570\u7ec4\u62f7\u8d1d\u5230\u65b0\u6570\u7ec4\n List.copyRange(_newNums, 0, _arr);\n // \u66f4\u65b0 _arr \u7684\u5f15\u7528\n _arr = _newNums;\n // \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\n _capacity = _arr.length;\n }\n\n /* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4 */\n List<int> toArray() {\n List<int> arr = [];\n for (var i = 0; i < _size; i++) {\n arr.add(get(i));\n }\n return arr;\n }\n}\n
my_list.rs/* \u5217\u8868\u7c7b */\n#[allow(dead_code)]\nstruct MyList {\n arr: Vec<i32>, // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\n capacity: usize, // \u5217\u8868\u5bb9\u91cf\n size: usize, // \u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\n extend_ratio: usize, // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n}\n\n#[allow(unused,unused_comparisons)]\nimpl MyList {\n /* \u6784\u9020\u65b9\u6cd5 */\n pub fn new(capacity: usize) -> Self {\n let mut vec = Vec::new(); \n vec.resize(capacity, 0);\n Self {\n arr: vec,\n capacity,\n size: 0,\n extend_ratio: 2,\n }\n }\n\n /* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09*/\n pub fn size(&self) -> usize {\n return self.size;\n }\n\n /* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\n pub fn capacity(&self) -> usize {\n return self.capacity;\n }\n\n /* \u8bbf\u95ee\u5143\u7d20 */\n pub fn get(&self, index: usize) -> i32 {\n // \u7d22\u5f15\u5982\u679c\u8d8a\u754c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\n if index >= self.size {panic!(\"\u7d22\u5f15\u8d8a\u754c\")};\n return self.arr[index];\n }\n\n /* \u66f4\u65b0\u5143\u7d20 */\n pub fn set(&mut self, index: usize, num: i32) {\n if index >= self.size {panic!(\"\u7d22\u5f15\u8d8a\u754c\")};\n self.arr[index] = num;\n }\n\n /* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\n pub fn add(&mut self, num: i32) {\n // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if self.size == self.capacity() {\n self.extend_capacity();\n }\n self.arr[self.size] = num;\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n self.size += 1;\n }\n\n /* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\n pub fn insert(&mut self, index: usize, num: i32) {\n if index >= self.size() {panic!(\"\u7d22\u5f15\u8d8a\u754c\")};\n // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if self.size == self.capacity() {\n self.extend_capacity();\n }\n // \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for j in (index..self.size).rev() {\n self.arr[j + 1] = self.arr[j];\n }\n self.arr[index] = num;\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n self.size += 1;\n }\n\n /* \u5220\u9664\u5143\u7d20 */\n pub fn remove(&mut self, index: usize) -> i32 {\n if index >= self.size() {panic!(\"\u7d22\u5f15\u8d8a\u754c\")};\n let num = self.arr[index];\n // \u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for j in (index..self.size - 1) {\n self.arr[j] = self.arr[j + 1];\n }\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n self.size -= 1;\n // \u8fd4\u56de\u88ab\u5220\u9664\u5143\u7d20\n return num;\n }\n\n /* \u5217\u8868\u6269\u5bb9 */\n pub fn extend_capacity(&mut self) {\n // \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a\u539f\u6570\u7ec4 extend_ratio \u500d\u7684\u65b0\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u62f7\u8d1d\u5230\u65b0\u6570\u7ec4\n let new_capacity = self.capacity * self.extend_ratio;\n self.arr.resize(new_capacity, 0);\n // \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\n self.capacity = new_capacity;\n }\n\n /* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4 */\n pub fn to_array(&mut self) -> Vec<i32> {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n let mut arr = Vec::new();\n for i in 0..self.size {\n arr.push(self.get(i));\n }\n arr\n }\n}\n
my_list.c/* \u5217\u8868\u7c7b */\ntypedef struct {\n int *arr; // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\n int capacity; // \u5217\u8868\u5bb9\u91cf\n int size; // \u5217\u8868\u5927\u5c0f\n int extendRatio; // \u5217\u8868\u6bcf\u6b21\u6269\u5bb9\u7684\u500d\u6570\n} MyList;\n\n/* \u6784\u9020\u51fd\u6570 */\nMyList *newMyList() {\n MyList *nums = malloc(sizeof(MyList));\n nums->capacity = 10;\n nums->arr = malloc(sizeof(int) * nums->capacity);\n nums->size = 0;\n nums->extendRatio = 2;\n return nums;\n}\n\n/* \u6790\u6784\u51fd\u6570 */\nvoid delMyList(MyList *nums) {\n free(nums->arr);\n free(nums);\n}\n\n/* \u83b7\u53d6\u5217\u8868\u957f\u5ea6 */\nint size(MyList *nums) {\n return nums->size;\n}\n\n/* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\nint capacity(MyList *nums) {\n return nums->capacity;\n}\n\n/* \u8bbf\u95ee\u5143\u7d20 */\nint get(MyList *nums, int index) {\n assert(index >= 0 && index < nums->size);\n return nums->arr[index];\n}\n\n/* \u66f4\u65b0\u5143\u7d20 */\nvoid set(MyList *nums, int index, int num) {\n assert(index >= 0 && index < nums->size);\n nums->arr[index] = num;\n}\n\n/* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nvoid add(MyList *nums, int num) {\n if (size(nums) == capacity(nums)) {\n extendCapacity(nums); // \u6269\u5bb9\n }\n nums->arr[size(nums)] = num;\n nums->size++;\n}\n\n/* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nvoid insert(MyList *nums, int index, int num) {\n assert(index >= 0 && index < size(nums));\n // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if (size(nums) == capacity(nums)) {\n extendCapacity(nums); // \u6269\u5bb9\n }\n for (int i = size(nums); i > index; --i) {\n nums->arr[i] = nums->arr[i - 1];\n }\n nums->arr[index] = num;\n nums->size++;\n}\n\n/* \u5220\u9664\u5143\u7d20 */\n// \u6ce8\u610f\uff1astdio.h \u5360\u7528\u4e86 remove \u5173\u952e\u8bcd\nint removeItem(MyList *nums, int index) {\n assert(index >= 0 && index < size(nums));\n int num = nums->arr[index];\n for (int i = index; i < size(nums) - 1; i++) {\n nums->arr[i] = nums->arr[i + 1];\n }\n nums->size--;\n return num;\n}\n\n/* \u5217\u8868\u6269\u5bb9 */\nvoid extendCapacity(MyList *nums) {\n // \u5148\u5206\u914d\u7a7a\u95f4\n int newCapacity = capacity(nums) * nums->extendRatio;\n int *extend = (int *)malloc(sizeof(int) * newCapacity);\n int *temp = nums->arr;\n\n // \u62f7\u8d1d\u65e7\u6570\u636e\u5230\u65b0\u6570\u636e\n for (int i = 0; i < size(nums); i++)\n extend[i] = nums->arr[i];\n\n // \u91ca\u653e\u65e7\u6570\u636e\n free(temp);\n\n // \u66f4\u65b0\u65b0\u6570\u636e\n nums->arr = extend;\n nums->capacity = newCapacity;\n}\n\n/* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a Array \u7528\u4e8e\u6253\u5370 */\nint *toArray(MyList *nums) {\n return nums->arr;\n}\n
my_list.zig// \u5217\u8868\u7c7b\nfn MyList(comptime T: type) type {\n return struct {\n const Self = @This();\n\n arr: []T = undefined, // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\n arrCapacity: usize = 10, // \u5217\u8868\u5bb9\u91cf\n numSize: usize = 0, // \u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\n extendRatio: usize = 2, // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n mem_arena: ?std.heap.ArenaAllocator = null,\n mem_allocator: std.mem.Allocator = undefined, // \u5185\u5b58\u5206\u914d\u5668\n\n // \u6784\u9020\u51fd\u6570\uff08\u5206\u914d\u5185\u5b58+\u521d\u59cb\u5316\u5217\u8868\uff09\n pub fn init(self: *Self, allocator: std.mem.Allocator) !void {\n if (self.mem_arena == null) {\n self.mem_arena = std.heap.ArenaAllocator.init(allocator);\n self.mem_allocator = self.mem_arena.?.allocator();\n }\n self.arr = try self.mem_allocator.alloc(T, self.arrCapacity);\n @memset(self.arr, @as(T, 0));\n }\n\n // \u6790\u6784\u51fd\u6570\uff08\u91ca\u653e\u5185\u5b58\uff09\n pub fn deinit(self: *Self) void {\n if (self.mem_arena == null) return;\n self.mem_arena.?.deinit();\n }\n\n // \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\n pub fn size(self: *Self) usize {\n return self.numSize;\n }\n\n // \u83b7\u53d6\u5217\u8868\u5bb9\u91cf\n pub fn capacity(self: *Self) usize {\n return self.arrCapacity;\n }\n\n // \u8bbf\u95ee\u5143\u7d20\n pub fn get(self: *Self, index: usize) T {\n // \u7d22\u5f15\u5982\u679c\u8d8a\u754c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\n if (index < 0 or index >= self.size()) @panic(\"\u7d22\u5f15\u8d8a\u754c\");\n return self.arr[index];\n } \n\n // \u66f4\u65b0\u5143\u7d20\n pub fn set(self: *Self, index: usize, num: T) void {\n // \u7d22\u5f15\u5982\u679c\u8d8a\u754c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\n if (index < 0 or index >= self.size()) @panic(\"\u7d22\u5f15\u8d8a\u754c\");\n self.arr[index] = num;\n } \n\n // \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20\n pub fn add(self: *Self, num: T) !void {\n // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if (self.size() == self.capacity()) try self.extendCapacity();\n self.arr[self.size()] = num;\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n self.numSize += 1;\n } \n\n // \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20\n pub fn insert(self: *Self, index: usize, num: T) !void {\n if (index < 0 or index >= self.size()) @panic(\"\u7d22\u5f15\u8d8a\u754c\");\n // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if (self.size() == self.capacity()) try self.extendCapacity();\n // \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n var j = self.size() - 1;\n while (j >= index) : (j -= 1) {\n self.arr[j + 1] = self.arr[j];\n }\n self.arr[index] = num;\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n self.numSize += 1;\n }\n\n // \u5220\u9664\u5143\u7d20\n pub fn remove(self: *Self, index: usize) T {\n if (index < 0 or index >= self.size()) @panic(\"\u7d22\u5f15\u8d8a\u754c\");\n var num = self.arr[index];\n // \u7d22\u5f15 i \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n var j = index;\n while (j < self.size() - 1) : (j += 1) {\n self.arr[j] = self.arr[j + 1];\n }\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n self.numSize -= 1;\n // \u8fd4\u56de\u88ab\u5220\u9664\u5143\u7d20\n return num;\n }\n\n // \u5217\u8868\u6269\u5bb9\n pub fn extendCapacity(self: *Self) !void {\n // \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a size * extendRatio \u7684\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u62f7\u8d1d\u5230\u65b0\u6570\u7ec4\n var newCapacity = self.capacity() * self.extendRatio;\n var extend = try self.mem_allocator.alloc(T, newCapacity);\n @memset(extend, @as(T, 0));\n // \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\n std.mem.copy(T, extend, self.arr);\n self.arr = extend;\n // \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\n self.arrCapacity = newCapacity;\n }\n\n // \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4\n pub fn toArray(self: *Self) ![]T {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n var arr = try self.mem_allocator.alloc(T, self.size());\n @memset(arr, @as(T, 0));\n for (arr, 0..) |*num, i| {\n num.* = self.get(i);\n }\n return arr;\n }\n };\n}\n
"},{"location":"chapter_array_and_linkedlist/ram_and_cache/","title":"4.4 \u00a0 \u5185\u5b58\u4e0e\u7f13\u5b58 *","text":"\u5728\u672c\u7ae0\u7684\u524d\u4e24\u8282\u4e2d\uff0c\u6211\u4eec\u63a2\u8ba8\u4e86\u6570\u7ec4\u548c\u94fe\u8868\u8fd9\u4e24\u79cd\u57fa\u7840\u4e14\u91cd\u8981\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5b83\u4eec\u5206\u522b\u4ee3\u8868\u4e86\u201c\u8fde\u7eed\u5b58\u50a8\u201d\u548c\u201c\u5206\u6563\u5b58\u50a8\u201d\u4e24\u79cd\u7269\u7406\u7ed3\u6784\u3002
\u5b9e\u9645\u4e0a\uff0c\u7269\u7406\u7ed3\u6784\u5728\u5f88\u5927\u7a0b\u5ea6\u4e0a\u51b3\u5b9a\u4e86\u7a0b\u5e8f\u5bf9\u5185\u5b58\u548c\u7f13\u5b58\u7684\u4f7f\u7528\u6548\u7387\uff0c\u8fdb\u800c\u5f71\u54cd\u7b97\u6cd5\u7a0b\u5e8f\u7684\u6574\u4f53\u6027\u80fd\u3002
"},{"location":"chapter_array_and_linkedlist/ram_and_cache/#441","title":"4.4.1 \u00a0 \u8ba1\u7b97\u673a\u5b58\u50a8\u8bbe\u5907","text":"\u8ba1\u7b97\u673a\u4e2d\u5305\u62ec\u4e09\u79cd\u7c7b\u578b\u7684\u5b58\u50a8\u8bbe\u5907\uff1a\u300c\u786c\u76d8 hard disk\u300d\u3001\u300c\u5185\u5b58 random-access memory, RAM\u300d\u3001\u300c\u7f13\u5b58 cache memory\u300d\u3002\u8868 4-2 \u5c55\u793a\u4e86\u5b83\u4eec\u5728\u8ba1\u7b97\u673a\u7cfb\u7edf\u4e2d\u7684\u4e0d\u540c\u89d2\u8272\u548c\u6027\u80fd\u7279\u70b9\u3002
\u8868 4-2 \u00a0 \u8ba1\u7b97\u673a\u7684\u5b58\u50a8\u8bbe\u5907
\u786c\u76d8 \u5185\u5b58 \u7f13\u5b58 \u7528\u9014 \u957f\u671f\u5b58\u50a8\u6570\u636e\uff0c\u5305\u62ec\u64cd\u4f5c\u7cfb\u7edf\u3001\u7a0b\u5e8f\u3001\u6587\u4ef6\u7b49 \u4e34\u65f6\u5b58\u50a8\u5f53\u524d\u8fd0\u884c\u7684\u7a0b\u5e8f\u548c\u6b63\u5728\u5904\u7406\u7684\u6570\u636e \u5b58\u50a8\u7ecf\u5e38\u8bbf\u95ee\u7684\u6570\u636e\u548c\u6307\u4ee4\uff0c\u51cf\u5c11 CPU \u8bbf\u95ee\u5185\u5b58\u7684\u6b21\u6570 \u6613\u5931\u6027 \u65ad\u7535\u540e\u6570\u636e\u4e0d\u4f1a\u4e22\u5931 \u65ad\u7535\u540e\u6570\u636e\u4f1a\u4e22\u5931 \u65ad\u7535\u540e\u6570\u636e\u4f1a\u4e22\u5931 \u5bb9\u91cf \u8f83\u5927\uff0cTB \u7ea7\u522b \u8f83\u5c0f\uff0cGB \u7ea7\u522b \u975e\u5e38\u5c0f\uff0cMB \u7ea7\u522b \u901f\u5ea6 \u8f83\u6162\uff0c\u51e0\u767e\u5230\u51e0\u5343 MB/s \u8f83\u5feb\uff0c\u51e0\u5341 GB/s \u975e\u5e38\u5feb\uff0c\u51e0\u5341\u5230\u51e0\u767e GB/s \u4ef7\u683c \u8f83\u4fbf\u5b9c\uff0c\u51e0\u6bdb\u5230\u51e0\u5143 / GB \u8f83\u8d35\uff0c\u51e0\u5341\u5230\u51e0\u767e\u5143 / GB \u975e\u5e38\u8d35\uff0c\u968f CPU \u6253\u5305\u8ba1\u4ef7\u6211\u4eec\u53ef\u4ee5\u5c06\u8ba1\u7b97\u673a\u5b58\u50a8\u7cfb\u7edf\u60f3\u8c61\u4e3a\u56fe 4-9 \u6240\u793a\u7684\u91d1\u5b57\u5854\u7ed3\u6784\u3002\u8d8a\u9760\u8fd1\u91d1\u5b57\u5854\u9876\u7aef\u7684\u5b58\u50a8\u8bbe\u5907\u7684\u901f\u5ea6\u8d8a\u5feb\u3001\u5bb9\u91cf\u8d8a\u5c0f\u3001\u6210\u672c\u8d8a\u9ad8\u3002\u8fd9\u79cd\u591a\u5c42\u7ea7\u7684\u8bbe\u8ba1\u5e76\u975e\u5076\u7136\uff0c\u800c\u662f\u8ba1\u7b97\u673a\u79d1\u5b66\u5bb6\u548c\u5de5\u7a0b\u5e08\u4eec\u7ecf\u8fc7\u6df1\u601d\u719f\u8651\u7684\u7ed3\u679c\u3002
\u56fe 4-9 \u00a0 \u8ba1\u7b97\u673a\u5b58\u50a8\u7cfb\u7edf
Note
\u8ba1\u7b97\u673a\u7684\u5b58\u50a8\u5c42\u6b21\u7ed3\u6784\u4f53\u73b0\u4e86\u901f\u5ea6\u3001\u5bb9\u91cf\u548c\u6210\u672c\u4e09\u8005\u4e4b\u95f4\u7684\u7cbe\u5999\u5e73\u8861\u3002\u5b9e\u9645\u4e0a\uff0c\u8fd9\u79cd\u6743\u8861\u666e\u904d\u5b58\u5728\u4e8e\u6240\u6709\u5de5\u4e1a\u9886\u57df\uff0c\u5b83\u8981\u6c42\u6211\u4eec\u5728\u4e0d\u540c\u7684\u4f18\u52bf\u548c\u9650\u5236\u4e4b\u95f4\u627e\u5230\u6700\u4f73\u5e73\u8861\u70b9\u3002
\u603b\u7684\u6765\u8bf4\uff0c\u786c\u76d8\u7528\u4e8e\u957f\u671f\u5b58\u50a8\u5927\u91cf\u6570\u636e\uff0c\u5185\u5b58\u7528\u4e8e\u4e34\u65f6\u5b58\u50a8\u7a0b\u5e8f\u8fd0\u884c\u4e2d\u6b63\u5728\u5904\u7406\u7684\u6570\u636e\uff0c\u800c\u7f13\u5b58\u5219\u7528\u4e8e\u5b58\u50a8\u7ecf\u5e38\u8bbf\u95ee\u7684\u6570\u636e\u548c\u6307\u4ee4\uff0c\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u8fd0\u884c\u6548\u7387\u3002\u4e09\u8005\u5171\u540c\u534f\u4f5c\uff0c\u786e\u4fdd\u8ba1\u7b97\u673a\u7cfb\u7edf\u9ad8\u6548\u8fd0\u884c\u3002
\u5982\u56fe 4-10 \u6240\u793a\uff0c\u5728\u7a0b\u5e8f\u8fd0\u884c\u65f6\uff0c\u6570\u636e\u4f1a\u4ece\u786c\u76d8\u4e2d\u88ab\u8bfb\u53d6\u5230\u5185\u5b58\u4e2d\uff0c\u4f9b CPU \u8ba1\u7b97\u4f7f\u7528\u3002\u7f13\u5b58\u53ef\u4ee5\u770b\u4f5c CPU \u7684\u4e00\u90e8\u5206\uff0c\u5b83\u901a\u8fc7\u667a\u80fd\u5730\u4ece\u5185\u5b58\u52a0\u8f7d\u6570\u636e\uff0c\u7ed9 CPU \u63d0\u4f9b\u9ad8\u901f\u7684\u6570\u636e\u8bfb\u53d6\uff0c\u4ece\u800c\u663e\u8457\u63d0\u5347\u7a0b\u5e8f\u7684\u6267\u884c\u6548\u7387\uff0c\u51cf\u5c11\u5bf9\u8f83\u6162\u7684\u5185\u5b58\u7684\u4f9d\u8d56\u3002
\u56fe 4-10 \u00a0 \u786c\u76d8\u3001\u5185\u5b58\u548c\u7f13\u5b58\u4e4b\u95f4\u7684\u6570\u636e\u6d41\u901a
"},{"location":"chapter_array_and_linkedlist/ram_and_cache/#442","title":"4.4.2 \u00a0 \u6570\u636e\u7ed3\u6784\u7684\u5185\u5b58\u6548\u7387","text":"\u5728\u5185\u5b58\u7a7a\u95f4\u5229\u7528\u65b9\u9762\uff0c\u6570\u7ec4\u548c\u94fe\u8868\u5404\u81ea\u5177\u6709\u4f18\u52bf\u548c\u5c40\u9650\u6027\u3002
\u4e00\u65b9\u9762\uff0c\u5185\u5b58\u662f\u6709\u9650\u7684\uff0c\u4e14\u540c\u4e00\u5757\u5185\u5b58\u4e0d\u80fd\u88ab\u591a\u4e2a\u7a0b\u5e8f\u5171\u4eab\uff0c\u56e0\u6b64\u6211\u4eec\u5e0c\u671b\u6570\u636e\u7ed3\u6784\u80fd\u591f\u5c3d\u53ef\u80fd\u9ad8\u6548\u5730\u5229\u7528\u7a7a\u95f4\u3002\u6570\u7ec4\u7684\u5143\u7d20\u7d27\u5bc6\u6392\u5217\uff0c\u4e0d\u9700\u8981\u989d\u5916\u7684\u7a7a\u95f4\u6765\u5b58\u50a8\u94fe\u8868\u8282\u70b9\u95f4\u7684\u5f15\u7528\uff08\u6307\u9488\uff09\uff0c\u56e0\u6b64\u7a7a\u95f4\u6548\u7387\u66f4\u9ad8\u3002\u7136\u800c\uff0c\u6570\u7ec4\u9700\u8981\u4e00\u6b21\u6027\u5206\u914d\u8db3\u591f\u7684\u8fde\u7eed\u5185\u5b58\u7a7a\u95f4\uff0c\u8fd9\u53ef\u80fd\u5bfc\u81f4\u5185\u5b58\u6d6a\u8d39\uff0c\u6570\u7ec4\u6269\u5bb9\u4e5f\u9700\u8981\u989d\u5916\u7684\u65f6\u95f4\u548c\u7a7a\u95f4\u6210\u672c\u3002\u76f8\u6bd4\u4e4b\u4e0b\uff0c\u94fe\u8868\u4ee5\u201c\u8282\u70b9\u201d\u4e3a\u5355\u4f4d\u8fdb\u884c\u52a8\u6001\u5185\u5b58\u5206\u914d\u548c\u56de\u6536\uff0c\u63d0\u4f9b\u4e86\u66f4\u5927\u7684\u7075\u6d3b\u6027\u3002
\u53e6\u4e00\u65b9\u9762\uff0c\u5728\u7a0b\u5e8f\u8fd0\u884c\u65f6\uff0c\u968f\u7740\u53cd\u590d\u7533\u8bf7\u4e0e\u91ca\u653e\u5185\u5b58\uff0c\u7a7a\u95f2\u5185\u5b58\u7684\u788e\u7247\u5316\u7a0b\u5ea6\u4f1a\u8d8a\u6765\u8d8a\u9ad8\uff0c\u4ece\u800c\u5bfc\u81f4\u5185\u5b58\u7684\u5229\u7528\u6548\u7387\u964d\u4f4e\u3002\u6570\u7ec4\u7531\u4e8e\u5176\u8fde\u7eed\u7684\u5b58\u50a8\u65b9\u5f0f\uff0c\u76f8\u5bf9\u4e0d\u5bb9\u6613\u5bfc\u81f4\u5185\u5b58\u788e\u7247\u5316\u3002\u76f8\u53cd\uff0c\u94fe\u8868\u7684\u5143\u7d20\u662f\u5206\u6563\u5b58\u50a8\u7684\uff0c\u5728\u9891\u7e41\u7684\u63d2\u5165\u4e0e\u5220\u9664\u64cd\u4f5c\u4e2d\uff0c\u66f4\u5bb9\u6613\u5bfc\u81f4\u5185\u5b58\u788e\u7247\u5316\u3002
"},{"location":"chapter_array_and_linkedlist/ram_and_cache/#443","title":"4.4.3 \u00a0 \u6570\u636e\u7ed3\u6784\u7684\u7f13\u5b58\u6548\u7387","text":"\u7f13\u5b58\u867d\u7136\u5728\u7a7a\u95f4\u5bb9\u91cf\u4e0a\u8fdc\u5c0f\u4e8e\u5185\u5b58\uff0c\u4f46\u5b83\u6bd4\u5185\u5b58\u5feb\u5f97\u591a\uff0c\u5728\u7a0b\u5e8f\u6267\u884c\u901f\u5ea6\u4e0a\u8d77\u7740\u81f3\u5173\u91cd\u8981\u7684\u4f5c\u7528\u3002\u7531\u4e8e\u7f13\u5b58\u7684\u5bb9\u91cf\u6709\u9650\uff0c\u53ea\u80fd\u5b58\u50a8\u4e00\u5c0f\u90e8\u5206\u9891\u7e41\u8bbf\u95ee\u7684\u6570\u636e\uff0c\u56e0\u6b64\u5f53 CPU \u5c1d\u8bd5\u8bbf\u95ee\u7684\u6570\u636e\u4e0d\u5728\u7f13\u5b58\u4e2d\u65f6\uff0c\u5c31\u4f1a\u53d1\u751f\u300c\u7f13\u5b58\u672a\u547d\u4e2d cache miss\u300d\uff0c\u6b64\u65f6 CPU \u4e0d\u5f97\u4e0d\u4ece\u901f\u5ea6\u8f83\u6162\u7684\u5185\u5b58\u4e2d\u52a0\u8f7d\u6240\u9700\u6570\u636e\u3002
\u663e\u7136\uff0c\u201c\u7f13\u5b58\u672a\u547d\u4e2d\u201d\u8d8a\u5c11\uff0cCPU \u8bfb\u5199\u6570\u636e\u7684\u6548\u7387\u5c31\u8d8a\u9ad8\uff0c\u7a0b\u5e8f\u6027\u80fd\u4e5f\u5c31\u8d8a\u597d\u3002\u6211\u4eec\u5c06 CPU \u4ece\u7f13\u5b58\u4e2d\u6210\u529f\u83b7\u53d6\u6570\u636e\u7684\u6bd4\u4f8b\u79f0\u4e3a\u300c\u7f13\u5b58\u547d\u4e2d\u7387 cache hit rate\u300d\uff0c\u8fd9\u4e2a\u6307\u6807\u901a\u5e38\u7528\u6765\u8861\u91cf\u7f13\u5b58\u6548\u7387\u3002
\u4e3a\u4e86\u5c3d\u53ef\u80fd\u8fbe\u5230\u66f4\u9ad8\u7684\u6548\u7387\uff0c\u7f13\u5b58\u4f1a\u91c7\u53d6\u4ee5\u4e0b\u6570\u636e\u52a0\u8f7d\u673a\u5236\u3002
\u5b9e\u9645\u4e0a\uff0c\u6570\u7ec4\u548c\u94fe\u8868\u5bf9\u7f13\u5b58\u7684\u5229\u7528\u6548\u7387\u662f\u4e0d\u540c\u7684\uff0c\u4e3b\u8981\u4f53\u73b0\u5728\u4ee5\u4e0b\u51e0\u4e2a\u65b9\u9762\u3002
\u603b\u4f53\u800c\u8a00\uff0c\u6570\u7ec4\u5177\u6709\u66f4\u9ad8\u7684\u7f13\u5b58\u547d\u4e2d\u7387\uff0c\u56e0\u6b64\u5b83\u5728\u64cd\u4f5c\u6548\u7387\u4e0a\u901a\u5e38\u4f18\u4e8e\u94fe\u8868\u3002\u8fd9\u4f7f\u5f97\u5728\u89e3\u51b3\u7b97\u6cd5\u95ee\u9898\u65f6\uff0c\u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6570\u636e\u7ed3\u6784\u5f80\u5f80\u66f4\u53d7\u6b22\u8fce\u3002
\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u9ad8\u7f13\u5b58\u6548\u7387\u5e76\u4e0d\u610f\u5473\u7740\u6570\u7ec4\u5728\u6240\u6709\u60c5\u51b5\u4e0b\u90fd\u4f18\u4e8e\u94fe\u8868\u3002\u5b9e\u9645\u5e94\u7528\u4e2d\u9009\u62e9\u54ea\u79cd\u6570\u636e\u7ed3\u6784\uff0c\u5e94\u6839\u636e\u5177\u4f53\u9700\u6c42\u6765\u51b3\u5b9a\u3002\u4f8b\u5982\uff0c\u6570\u7ec4\u548c\u94fe\u8868\u90fd\u53ef\u4ee5\u5b9e\u73b0\u201c\u6808\u201d\u6570\u636e\u7ed3\u6784\uff08\u4e0b\u4e00\u7ae0\u4f1a\u8be6\u7ec6\u4ecb\u7ecd\uff09\uff0c\u4f46\u5b83\u4eec\u9002\u7528\u4e8e\u4e0d\u540c\u573a\u666f\u3002
\u6570\u7ec4\u5b58\u50a8\u5728\u6808\u4e0a\u548c\u5b58\u50a8\u5728\u5806\u4e0a\uff0c\u5bf9\u65f6\u95f4\u6548\u7387\u548c\u7a7a\u95f4\u6548\u7387\u662f\u5426\u6709\u5f71\u54cd\uff1f
\u5b58\u50a8\u5728\u6808\u4e0a\u548c\u5806\u4e0a\u7684\u6570\u7ec4\u90fd\u88ab\u5b58\u50a8\u5728\u8fde\u7eed\u5185\u5b58\u7a7a\u95f4\u5185\uff0c\u6570\u636e\u64cd\u4f5c\u6548\u7387\u57fa\u672c\u4e00\u81f4\u3002\u7136\u800c\uff0c\u6808\u548c\u5806\u5177\u6709\u5404\u81ea\u7684\u7279\u70b9\uff0c\u4ece\u800c\u5bfc\u81f4\u4ee5\u4e0b\u4e0d\u540c\u70b9\u3002
\u4e3a\u4ec0\u4e48\u6570\u7ec4\u8981\u6c42\u76f8\u540c\u7c7b\u578b\u7684\u5143\u7d20\uff0c\u800c\u5728\u94fe\u8868\u4e2d\u5374\u6ca1\u6709\u5f3a\u8c03\u540c\u7c7b\u578b\u5462\uff1f
\u94fe\u8868\u7531\u8282\u70b9\u7ec4\u6210\uff0c\u8282\u70b9\u4e4b\u95f4\u901a\u8fc7\u5f15\u7528\uff08\u6307\u9488\uff09\u8fde\u63a5\uff0c\u5404\u4e2a\u8282\u70b9\u53ef\u4ee5\u5b58\u50a8\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\uff0c\u4f8b\u5982 int\u3001double\u3001string\u3001object \u7b49\u3002
\u76f8\u5bf9\u5730\uff0c\u6570\u7ec4\u5143\u7d20\u5219\u5fc5\u987b\u662f\u76f8\u540c\u7c7b\u578b\u7684\uff0c\u8fd9\u6837\u624d\u80fd\u901a\u8fc7\u8ba1\u7b97\u504f\u79fb\u91cf\u6765\u83b7\u53d6\u5bf9\u5e94\u5143\u7d20\u4f4d\u7f6e\u3002\u4f8b\u5982\uff0c\u6570\u7ec4\u540c\u65f6\u5305\u542b int \u548c long \u4e24\u79cd\u7c7b\u578b\uff0c\u5355\u4e2a\u5143\u7d20\u5206\u522b\u5360\u7528 4 bytes \u548c 8 bytes \uff0c\u6b64\u65f6\u5c31\u4e0d\u80fd\u7528\u4ee5\u4e0b\u516c\u5f0f\u8ba1\u7b97\u504f\u79fb\u91cf\u4e86\uff0c\u56e0\u4e3a\u6570\u7ec4\u4e2d\u5305\u542b\u4e86\u4e24\u79cd\u957f\u5ea6\u7684\u5143\u7d20\u3002
# \u5143\u7d20\u5185\u5b58\u5730\u5740 = \u6570\u7ec4\u5185\u5b58\u5730\u5740 + \u5143\u7d20\u957f\u5ea6 * \u5143\u7d20\u7d22\u5f15\n
\u5220\u9664\u8282\u70b9\u540e\uff0c\u662f\u5426\u9700\u8981\u628a P.next
\u8bbe\u4e3a \\(\\text{None}\\) \u5462\uff1f
\u4e0d\u4fee\u6539 P.next
\u4e5f\u53ef\u4ee5\u3002\u4ece\u8be5\u94fe\u8868\u7684\u89d2\u5ea6\u770b\uff0c\u4ece\u5934\u8282\u70b9\u904d\u5386\u5230\u5c3e\u8282\u70b9\u5df2\u7ecf\u4e0d\u4f1a\u9047\u5230 P
\u4e86\u3002\u8fd9\u610f\u5473\u7740\u8282\u70b9 P
\u5df2\u7ecf\u4ece\u94fe\u8868\u4e2d\u5220\u9664\u4e86\uff0c\u6b64\u65f6\u8282\u70b9 P
\u6307\u5411\u54ea\u91cc\u90fd\u4e0d\u4f1a\u5bf9\u8be5\u94fe\u8868\u4ea7\u751f\u5f71\u54cd\u3002
\u4ece\u5783\u573e\u56de\u6536\u7684\u89d2\u5ea6\u770b\uff0c\u5bf9\u4e8e Java\u3001Python\u3001Go \u7b49\u62e5\u6709\u81ea\u52a8\u5783\u573e\u56de\u6536\u673a\u5236\u7684\u8bed\u8a00\u6765\u8bf4\uff0c\u8282\u70b9 P
\u662f\u5426\u88ab\u56de\u6536\u53d6\u51b3\u4e8e\u662f\u5426\u4ecd\u5b58\u5728\u6307\u5411\u5b83\u7684\u5f15\u7528\uff0c\u800c\u4e0d\u662f P.next
\u7684\u503c\u3002\u5728 C \u548c C++ \u7b49\u8bed\u8a00\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u624b\u52a8\u91ca\u653e\u8282\u70b9\u5185\u5b58\u3002
\u5728\u94fe\u8868\u4e2d\u63d2\u5165\u548c\u5220\u9664\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u662f \\(O(1)\\) \u3002\u4f46\u662f\u589e\u5220\u4e4b\u524d\u90fd\u9700\u8981 \\(O(n)\\) \u7684\u65f6\u95f4\u67e5\u627e\u5143\u7d20\uff0c\u90a3\u4e3a\u4ec0\u4e48\u65f6\u95f4\u590d\u6742\u5ea6\u4e0d\u662f \\(O(n)\\) \u5462\uff1f
\u5982\u679c\u662f\u5148\u67e5\u627e\u5143\u7d20\u3001\u518d\u5220\u9664\u5143\u7d20\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u786e\u5b9e\u662f \\(O(n)\\) \u3002\u7136\u800c\uff0c\u94fe\u8868\u7684 \\(O(1)\\) \u589e\u5220\u7684\u4f18\u52bf\u53ef\u4ee5\u5728\u5176\u4ed6\u5e94\u7528\u4e0a\u5f97\u5230\u4f53\u73b0\u3002\u4f8b\u5982\uff0c\u53cc\u5411\u961f\u5217\u9002\u5408\u4f7f\u7528\u94fe\u8868\u5b9e\u73b0\uff0c\u6211\u4eec\u7ef4\u62a4\u4e00\u4e2a\u6307\u9488\u53d8\u91cf\u59cb\u7ec8\u6307\u5411\u5934\u8282\u70b9\u3001\u5c3e\u8282\u70b9\uff0c\u6bcf\u6b21\u63d2\u5165\u4e0e\u5220\u9664\u64cd\u4f5c\u90fd\u662f \\(O(1)\\) \u3002
\u56fe\u201c\u94fe\u8868\u5b9a\u4e49\u4e0e\u5b58\u50a8\u65b9\u5f0f\u201d\u4e2d\uff0c\u6d45\u84dd\u8272\u7684\u5b58\u50a8\u8282\u70b9\u6307\u9488\u662f\u5360\u7528\u4e00\u5757\u5185\u5b58\u5730\u5740\u5417\uff1f\u8fd8\u662f\u548c\u8282\u70b9\u503c\u5404\u5360\u4e00\u534a\u5462\uff1f
\u8be5\u793a\u610f\u56fe\u53ea\u662f\u5b9a\u6027\u8868\u793a\uff0c\u5b9a\u91cf\u8868\u793a\u9700\u8981\u6839\u636e\u5177\u4f53\u60c5\u51b5\u8fdb\u884c\u5206\u6790\u3002
\u5728\u5217\u8868\u672b\u5c3e\u6dfb\u52a0\u5143\u7d20\u662f\u5426\u65f6\u65f6\u523b\u523b\u90fd\u4e3a \\(O(1)\\) \uff1f
\u5982\u679c\u6dfb\u52a0\u5143\u7d20\u65f6\u8d85\u51fa\u5217\u8868\u957f\u5ea6\uff0c\u5219\u9700\u8981\u5148\u6269\u5bb9\u5217\u8868\u518d\u6dfb\u52a0\u3002\u7cfb\u7edf\u4f1a\u7533\u8bf7\u4e00\u5757\u65b0\u7684\u5185\u5b58\uff0c\u5e76\u5c06\u539f\u5217\u8868\u7684\u6240\u6709\u5143\u7d20\u642c\u8fd0\u8fc7\u53bb\uff0c\u8fd9\u65f6\u5019\u65f6\u95f4\u590d\u6742\u5ea6\u5c31\u4f1a\u662f \\(O(n)\\) \u3002
\u201c\u5217\u8868\u7684\u51fa\u73b0\u6781\u5927\u5730\u63d0\u9ad8\u4e86\u6570\u7ec4\u7684\u5b9e\u7528\u6027\uff0c\u4f46\u53ef\u80fd\u5bfc\u81f4\u90e8\u5206\u5185\u5b58\u7a7a\u95f4\u6d6a\u8d39\u201d\uff0c\u8fd9\u91cc\u7684\u7a7a\u95f4\u6d6a\u8d39\u662f\u6307\u989d\u5916\u589e\u52a0\u7684\u53d8\u91cf\u5982\u5bb9\u91cf\u3001\u957f\u5ea6\u3001\u6269\u5bb9\u500d\u6570\u6240\u5360\u7684\u5185\u5b58\u5417\uff1f
\u8fd9\u91cc\u7684\u7a7a\u95f4\u6d6a\u8d39\u4e3b\u8981\u6709\u4e24\u65b9\u9762\u542b\u4e49\uff1a\u4e00\u65b9\u9762\uff0c\u5217\u8868\u90fd\u4f1a\u8bbe\u5b9a\u4e00\u4e2a\u521d\u59cb\u957f\u5ea6\uff0c\u6211\u4eec\u4e0d\u4e00\u5b9a\u9700\u8981\u7528\u8fd9\u4e48\u591a\uff1b\u53e6\u4e00\u65b9\u9762\uff0c\u4e3a\u4e86\u9632\u6b62\u9891\u7e41\u6269\u5bb9\uff0c\u6269\u5bb9\u4e00\u822c\u4f1a\u4e58\u4ee5\u4e00\u4e2a\u7cfb\u6570\uff0c\u6bd4\u5982 \\(\\times 1.5\\) \u3002\u8fd9\u6837\u4e00\u6765\uff0c\u4e5f\u4f1a\u51fa\u73b0\u5f88\u591a\u7a7a\u4f4d\uff0c\u6211\u4eec\u901a\u5e38\u4e0d\u80fd\u5b8c\u5168\u586b\u6ee1\u5b83\u4eec\u3002
\u5728 Python \u4e2d\u521d\u59cb\u5316 n = [1, 2, 3]
\u540e\uff0c\u8fd9 3 \u4e2a\u5143\u7d20\u7684\u5730\u5740\u662f\u76f8\u8fde\u7684\uff0c\u4f46\u662f\u521d\u59cb\u5316 m = [2, 1, 3]
\u4f1a\u53d1\u73b0\u5b83\u4eec\u6bcf\u4e2a\u5143\u7d20\u7684 id \u5e76\u4e0d\u662f\u8fde\u7eed\u7684\uff0c\u800c\u662f\u5206\u522b\u8ddf n
\u4e2d\u7684\u76f8\u540c\u3002\u8fd9\u4e9b\u5143\u7d20\u7684\u5730\u5740\u4e0d\u8fde\u7eed\uff0c\u90a3\u4e48 m
\u8fd8\u662f\u6570\u7ec4\u5417\uff1f
\u5047\u5982\u628a\u5217\u8868\u5143\u7d20\u6362\u6210\u94fe\u8868\u8282\u70b9 n = [n1, n2, n3, n4, n5]
\uff0c\u901a\u5e38\u60c5\u51b5\u4e0b\u8fd9 5 \u4e2a\u8282\u70b9\u5bf9\u8c61\u4e5f\u5206\u6563\u5b58\u50a8\u5728\u5185\u5b58\u5404\u5904\u3002\u7136\u800c\uff0c\u7ed9\u5b9a\u4e00\u4e2a\u5217\u8868\u7d22\u5f15\uff0c\u6211\u4eec\u4ecd\u7136\u53ef\u4ee5\u5728 \\(O(1)\\) \u65f6\u95f4\u5185\u83b7\u53d6\u8282\u70b9\u5185\u5b58\u5730\u5740\uff0c\u4ece\u800c\u8bbf\u95ee\u5230\u5bf9\u5e94\u7684\u8282\u70b9\u3002\u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u4e2d\u5b58\u50a8\u7684\u662f\u8282\u70b9\u7684\u5f15\u7528\uff0c\u800c\u975e\u8282\u70b9\u672c\u8eab\u3002
\u4e0e\u8bb8\u591a\u8bed\u8a00\u4e0d\u540c\uff0cPython \u4e2d\u7684\u6570\u5b57\u4e5f\u88ab\u5305\u88c5\u4e3a\u5bf9\u8c61\uff0c\u5217\u8868\u4e2d\u5b58\u50a8\u7684\u4e0d\u662f\u6570\u5b57\u672c\u8eab\uff0c\u800c\u662f\u5bf9\u6570\u5b57\u7684\u5f15\u7528\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u4f1a\u53d1\u73b0\u4e24\u4e2a\u6570\u7ec4\u4e2d\u7684\u76f8\u540c\u6570\u5b57\u62e5\u6709\u540c\u4e00\u4e2a id \uff0c\u5e76\u4e14\u8fd9\u4e9b\u6570\u5b57\u7684\u5185\u5b58\u5730\u5740\u65e0\u987b\u8fde\u7eed\u3002
C++ STL \u91cc\u9762\u7684 std::list
\u5df2\u7ecf\u5b9e\u73b0\u4e86\u53cc\u5411\u94fe\u8868\uff0c\u4f46\u597d\u50cf\u4e00\u4e9b\u7b97\u6cd5\u4e66\u4e0a\u4e0d\u600e\u4e48\u76f4\u63a5\u4f7f\u7528\u5b83\uff0c\u662f\u4e0d\u662f\u56e0\u4e3a\u6709\u4ec0\u4e48\u5c40\u9650\u6027\u5462\uff1f
\u4e00\u65b9\u9762\uff0c\u6211\u4eec\u5f80\u5f80\u66f4\u9752\u7750\u4f7f\u7528\u6570\u7ec4\u5b9e\u73b0\u7b97\u6cd5\uff0c\u800c\u53ea\u5728\u5fc5\u8981\u65f6\u624d\u4f7f\u7528\u94fe\u8868\uff0c\u4e3b\u8981\u6709\u4e24\u4e2a\u539f\u56e0\u3002
std::list
\u901a\u5e38\u6bd4 std::vector
\u66f4\u5360\u7528\u7a7a\u95f4\u3002std::list
\u5bf9\u7f13\u5b58\u7684\u5229\u7528\u7387\u8f83\u4f4e\u3002\u4e00\u822c\u60c5\u51b5\u4e0b\uff0cstd::vector
\u7684\u6027\u80fd\u4f1a\u66f4\u597d\u3002\u53e6\u4e00\u65b9\u9762\uff0c\u5fc5\u8981\u4f7f\u7528\u94fe\u8868\u7684\u60c5\u51b5\u4e3b\u8981\u662f\u4e8c\u53c9\u6811\u548c\u56fe\u3002\u6808\u548c\u961f\u5217\u5f80\u5f80\u4f1a\u4f7f\u7528\u7f16\u7a0b\u8bed\u8a00\u63d0\u4f9b\u7684 stack
\u548c queue
\uff0c\u800c\u975e\u94fe\u8868\u3002
\u521d\u59cb\u5316\u5217\u8868 res = [0] * self.size()
\u64cd\u4f5c\uff0c\u4f1a\u5bfc\u81f4 res
\u7684\u6bcf\u4e2a\u5143\u7d20\u5f15\u7528\u76f8\u540c\u7684\u5730\u5740\u5417\uff1f
\u4e0d\u4f1a\u3002\u4f46\u4e8c\u7ef4\u6570\u7ec4\u4f1a\u6709\u8fd9\u4e2a\u95ee\u9898\uff0c\u4f8b\u5982\u521d\u59cb\u5316\u4e8c\u7ef4\u5217\u8868 res = [[0] * self.size()]
\uff0c\u5219\u591a\u6b21\u5f15\u7528\u4e86\u540c\u4e00\u4e2a\u5217\u8868 [0]
\u3002
\u5728\u5220\u9664\u8282\u70b9\u4e2d\uff0c\u9700\u8981\u65ad\u5f00\u8be5\u8282\u70b9\u4e0e\u5176\u540e\u7ee7\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\u6307\u5411\u5417\uff1f
\u4ece\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\uff08\u505a\u9898\uff09\u7684\u89d2\u5ea6\u770b\uff0c\u4e0d\u65ad\u5f00\u6ca1\u6709\u5173\u7cfb\uff0c\u53ea\u8981\u4fdd\u8bc1\u7a0b\u5e8f\u7684\u903b\u8f91\u662f\u6b63\u786e\u7684\u5c31\u884c\u3002\u4ece\u6807\u51c6\u5e93\u7684\u89d2\u5ea6\u770b\uff0c\u65ad\u5f00\u66f4\u52a0\u5b89\u5168\u3001\u903b\u8f91\u66f4\u52a0\u6e05\u6670\u3002\u5982\u679c\u4e0d\u65ad\u5f00\uff0c\u5047\u8bbe\u88ab\u5220\u9664\u8282\u70b9\u672a\u88ab\u6b63\u5e38\u56de\u6536\uff0c\u90a3\u4e48\u5b83\u4f1a\u5f71\u54cd\u540e\u7ee7\u8282\u70b9\u7684\u5185\u5b58\u56de\u6536\u3002
"},{"location":"chapter_backtracking/","title":"\u7b2c 13 \u7ae0 \u00a0 \u56de\u6eaf","text":"Abstract
\u6211\u4eec\u5982\u540c\u8ff7\u5bab\u4e2d\u7684\u63a2\u7d22\u8005\uff0c\u5728\u524d\u8fdb\u7684\u9053\u8def\u4e0a\u53ef\u80fd\u4f1a\u9047\u5230\u56f0\u96be\u3002
\u56de\u6eaf\u7684\u529b\u91cf\u8ba9\u6211\u4eec\u80fd\u591f\u91cd\u65b0\u5f00\u59cb\uff0c\u4e0d\u65ad\u5c1d\u8bd5\uff0c\u6700\u7ec8\u627e\u5230\u901a\u5f80\u5149\u660e\u7684\u51fa\u53e3\u3002
"},{"location":"chapter_backtracking/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"\u300c\u56de\u6eaf\u7b97\u6cd5 backtracking algorithm\u300d\u662f\u4e00\u79cd\u901a\u8fc7\u7a77\u4e3e\u6765\u89e3\u51b3\u95ee\u9898\u7684\u65b9\u6cd5\uff0c\u5b83\u7684\u6838\u5fc3\u601d\u60f3\u662f\u4ece\u4e00\u4e2a\u521d\u59cb\u72b6\u6001\u51fa\u53d1\uff0c\u66b4\u529b\u641c\u7d22\u6240\u6709\u53ef\u80fd\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u5f53\u9047\u5230\u6b63\u786e\u7684\u89e3\u5219\u5c06\u5176\u8bb0\u5f55\uff0c\u76f4\u5230\u627e\u5230\u89e3\u6216\u8005\u5c1d\u8bd5\u4e86\u6240\u6709\u53ef\u80fd\u7684\u9009\u62e9\u90fd\u65e0\u6cd5\u627e\u5230\u89e3\u4e3a\u6b62\u3002
\u56de\u6eaf\u7b97\u6cd5\u901a\u5e38\u91c7\u7528\u201c\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u201d\u6765\u904d\u5386\u89e3\u7a7a\u95f4\u3002\u5728\u201c\u4e8c\u53c9\u6811\u201d\u7ae0\u8282\u4e2d\uff0c\u6211\u4eec\u63d0\u5230\u524d\u5e8f\u3001\u4e2d\u5e8f\u548c\u540e\u5e8f\u904d\u5386\u90fd\u5c5e\u4e8e\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5229\u7528\u524d\u5e8f\u904d\u5386\u6784\u9020\u4e00\u4e2a\u56de\u6eaf\u95ee\u9898\uff0c\u9010\u6b65\u4e86\u89e3\u56de\u6eaf\u7b97\u6cd5\u7684\u5de5\u4f5c\u539f\u7406\u3002
\u4f8b\u9898\u4e00
\u7ed9\u5b9a\u4e00\u68f5\u4e8c\u53c9\u6811\uff0c\u641c\u7d22\u5e76\u8bb0\u5f55\u6240\u6709\u503c\u4e3a \\(7\\) \u7684\u8282\u70b9\uff0c\u8bf7\u8fd4\u56de\u8282\u70b9\u5217\u8868\u3002
\u5bf9\u4e8e\u6b64\u9898\uff0c\u6211\u4eec\u524d\u5e8f\u904d\u5386\u8fd9\u68f5\u6811\uff0c\u5e76\u5224\u65ad\u5f53\u524d\u8282\u70b9\u7684\u503c\u662f\u5426\u4e3a \\(7\\) \uff0c\u82e5\u662f\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u7684\u503c\u52a0\u5165\u7ed3\u679c\u5217\u8868 res
\u4e4b\u4e2d\u3002\u76f8\u5173\u8fc7\u7a0b\u5b9e\u73b0\u5982\u56fe 13-1 \u548c\u4ee5\u4e0b\u4ee3\u7801\u6240\u793a\uff1a
def pre_order(root: TreeNode):\n \"\"\"\u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e00\"\"\"\n if root is None:\n return\n if root.val == 7:\n # \u8bb0\u5f55\u89e3\n res.append(root)\n pre_order(root.left)\n pre_order(root.right)\n
preorder_traversal_i_compact.cpp/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e00 */\nvoid preOrder(TreeNode *root) {\n if (root == nullptr) {\n return;\n }\n if (root->val == 7) {\n // \u8bb0\u5f55\u89e3\n res.push_back(root);\n }\n preOrder(root->left);\n preOrder(root->right);\n}\n
preorder_traversal_i_compact.java/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e00 */\nvoid preOrder(TreeNode root) {\n if (root == null) {\n return;\n }\n if (root.val == 7) {\n // \u8bb0\u5f55\u89e3\n res.add(root);\n }\n preOrder(root.left);\n preOrder(root.right);\n}\n
preorder_traversal_i_compact.cs/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e00 */\nvoid PreOrder(TreeNode? root) {\n if (root == null) {\n return;\n }\n if (root.val == 7) {\n // \u8bb0\u5f55\u89e3\n res.Add(root);\n }\n PreOrder(root.left);\n PreOrder(root.right);\n}\n
preorder_traversal_i_compact.go/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e00 */\nfunc preOrderI(root *TreeNode, res *[]*TreeNode) {\n if root == nil {\n return\n }\n if (root.Val).(int) == 7 {\n // \u8bb0\u5f55\u89e3\n *res = append(*res, root)\n }\n preOrderI(root.Left, res)\n preOrderI(root.Right, res)\n}\n
preorder_traversal_i_compact.swift/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e00 */\nfunc preOrder(root: TreeNode?) {\n guard let root = root else {\n return\n }\n if root.val == 7 {\n // \u8bb0\u5f55\u89e3\n res.append(root)\n }\n preOrder(root: root.left)\n preOrder(root: root.right)\n}\n
preorder_traversal_i_compact.js/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e00 */\nfunction preOrder(root, res) {\n if (root === null) {\n return;\n }\n if (root.val === 7) {\n // \u8bb0\u5f55\u89e3\n res.push(root);\n }\n preOrder(root.left, res);\n preOrder(root.right, res);\n}\n
preorder_traversal_i_compact.ts/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e00 */\nfunction preOrder(root: TreeNode | null, res: TreeNode[]): void {\n if (root === null) {\n return;\n }\n if (root.val === 7) {\n // \u8bb0\u5f55\u89e3\n res.push(root);\n }\n preOrder(root.left, res);\n preOrder(root.right, res);\n}\n
preorder_traversal_i_compact.dart/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e00 */\nvoid preOrder(TreeNode? root, List<TreeNode> res) {\n if (root == null) {\n return;\n }\n if (root.val == 7) {\n // \u8bb0\u5f55\u89e3\n res.add(root);\n }\n preOrder(root.left, res);\n preOrder(root.right, res);\n}\n
preorder_traversal_i_compact.rs/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e00 */\nfn pre_order(res: &mut Vec<Rc<RefCell<TreeNode>>>, root: Option<Rc<RefCell<TreeNode>>>) {\n if root.is_none() {\n return;\n }\n if let Some(node) = root {\n if node.borrow().val == 7 {\n // \u8bb0\u5f55\u89e3\n res.push(node.clone());\n }\n pre_order(res, node.borrow().left.clone());\n pre_order(res, node.borrow().right.clone());\n }\n}\n
preorder_traversal_i_compact.c/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e00 */\nvoid preOrder(TreeNode *root) {\n if (root == NULL) {\n return;\n }\n if (root->val == 7) {\n // \u8bb0\u5f55\u89e3\n res[resSize++] = root;\n }\n preOrder(root->left);\n preOrder(root->right);\n}\n
preorder_traversal_i_compact.zig[class]{}-[func]{preOrder}\n
\u56fe 13-1 \u00a0 \u5728\u524d\u5e8f\u904d\u5386\u4e2d\u641c\u7d22\u8282\u70b9
"},{"location":"chapter_backtracking/backtracking_algorithm/#1311","title":"13.1.1 \u00a0 \u5c1d\u8bd5\u4e0e\u56de\u9000","text":"\u4e4b\u6240\u4ee5\u79f0\u4e4b\u4e3a\u56de\u6eaf\u7b97\u6cd5\uff0c\u662f\u56e0\u4e3a\u8be5\u7b97\u6cd5\u5728\u641c\u7d22\u89e3\u7a7a\u95f4\u65f6\u4f1a\u91c7\u7528\u201c\u5c1d\u8bd5\u201d\u4e0e\u201c\u56de\u9000\u201d\u7684\u7b56\u7565\u3002\u5f53\u7b97\u6cd5\u5728\u641c\u7d22\u8fc7\u7a0b\u4e2d\u9047\u5230\u67d0\u4e2a\u72b6\u6001\u65e0\u6cd5\u7ee7\u7eed\u524d\u8fdb\u6216\u65e0\u6cd5\u5f97\u5230\u6ee1\u8db3\u6761\u4ef6\u7684\u89e3\u65f6\uff0c\u5b83\u4f1a\u64a4\u9500\u4e0a\u4e00\u6b65\u7684\u9009\u62e9\uff0c\u9000\u56de\u5230\u4e4b\u524d\u7684\u72b6\u6001\uff0c\u5e76\u5c1d\u8bd5\u5176\u4ed6\u53ef\u80fd\u7684\u9009\u62e9\u3002
\u5bf9\u4e8e\u4f8b\u9898\u4e00\uff0c\u8bbf\u95ee\u6bcf\u4e2a\u8282\u70b9\u90fd\u4ee3\u8868\u4e00\u6b21\u201c\u5c1d\u8bd5\u201d\uff0c\u800c\u8d8a\u8fc7\u53f6\u8282\u70b9\u6216\u8fd4\u56de\u7236\u8282\u70b9\u7684 return
\u5219\u8868\u793a\u201c\u56de\u9000\u201d\u3002
\u503c\u5f97\u8bf4\u660e\u7684\u662f\uff0c\u56de\u9000\u5e76\u4e0d\u4ec5\u4ec5\u5305\u62ec\u51fd\u6570\u8fd4\u56de\u3002\u4e3a\u89e3\u91ca\u8fd9\u4e00\u70b9\uff0c\u6211\u4eec\u5bf9\u4f8b\u9898\u4e00\u7a0d\u4f5c\u62d3\u5c55\u3002
\u4f8b\u9898\u4e8c
\u5728\u4e8c\u53c9\u6811\u4e2d\u641c\u7d22\u6240\u6709\u503c\u4e3a \\(7\\) \u7684\u8282\u70b9\uff0c\u8bf7\u8fd4\u56de\u6839\u8282\u70b9\u5230\u8fd9\u4e9b\u8282\u70b9\u7684\u8def\u5f84\u3002
\u5728\u4f8b\u9898\u4e00\u4ee3\u7801\u7684\u57fa\u7840\u4e0a\uff0c\u6211\u4eec\u9700\u8981\u501f\u52a9\u4e00\u4e2a\u5217\u8868 path
\u8bb0\u5f55\u8bbf\u95ee\u8fc7\u7684\u8282\u70b9\u8def\u5f84\u3002\u5f53\u8bbf\u95ee\u5230\u503c\u4e3a \\(7\\) \u7684\u8282\u70b9\u65f6\uff0c\u5219\u590d\u5236 path
\u5e76\u6dfb\u52a0\u8fdb\u7ed3\u679c\u5217\u8868 res
\u3002\u904d\u5386\u5b8c\u6210\u540e\uff0cres
\u4e2d\u4fdd\u5b58\u7684\u5c31\u662f\u6240\u6709\u7684\u89e3\u3002\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a
def pre_order(root: TreeNode):\n \"\"\"\u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e8c\"\"\"\n if root is None:\n return\n # \u5c1d\u8bd5\n path.append(root)\n if root.val == 7:\n # \u8bb0\u5f55\u89e3\n res.append(list(path))\n pre_order(root.left)\n pre_order(root.right)\n # \u56de\u9000\n path.pop()\n
preorder_traversal_ii_compact.cpp/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e8c */\nvoid preOrder(TreeNode *root) {\n if (root == nullptr) {\n return;\n }\n // \u5c1d\u8bd5\n path.push_back(root);\n if (root->val == 7) {\n // \u8bb0\u5f55\u89e3\n res.push_back(path);\n }\n preOrder(root->left);\n preOrder(root->right);\n // \u56de\u9000\n path.pop_back();\n}\n
preorder_traversal_ii_compact.java/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e8c */\nvoid preOrder(TreeNode root) {\n if (root == null) {\n return;\n }\n // \u5c1d\u8bd5\n path.add(root);\n if (root.val == 7) {\n // \u8bb0\u5f55\u89e3\n res.add(new ArrayList<>(path));\n }\n preOrder(root.left);\n preOrder(root.right);\n // \u56de\u9000\n path.remove(path.size() - 1);\n}\n
preorder_traversal_ii_compact.cs/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e8c */\nvoid PreOrder(TreeNode? root) {\n if (root == null) {\n return;\n }\n // \u5c1d\u8bd5\n path.Add(root);\n if (root.val == 7) {\n // \u8bb0\u5f55\u89e3\n res.Add(new List<TreeNode>(path));\n }\n PreOrder(root.left);\n PreOrder(root.right);\n // \u56de\u9000\n path.RemoveAt(path.Count - 1);\n}\n
preorder_traversal_ii_compact.go/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e8c */\nfunc preOrderII(root *TreeNode, res *[][]*TreeNode, path *[]*TreeNode) {\n if root == nil {\n return\n }\n // \u5c1d\u8bd5\n *path = append(*path, root)\n if root.Val.(int) == 7 {\n // \u8bb0\u5f55\u89e3\n *res = append(*res, *path)\n }\n preOrderII(root.Left, res, path)\n preOrderII(root.Right, res, path)\n // \u56de\u9000\n *path = (*path)[:len(*path)-1]\n}\n
preorder_traversal_ii_compact.swift/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e8c */\nfunc preOrder(root: TreeNode?) {\n guard let root = root else {\n return\n }\n // \u5c1d\u8bd5\n path.append(root)\n if root.val == 7 {\n // \u8bb0\u5f55\u89e3\n res.append(path)\n }\n preOrder(root: root.left)\n preOrder(root: root.right)\n // \u56de\u9000\n path.removeLast()\n}\n
preorder_traversal_ii_compact.js/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e8c */\nfunction preOrder(root, path, res) {\n if (root === null) {\n return;\n }\n // \u5c1d\u8bd5\n path.push(root);\n if (root.val === 7) {\n // \u8bb0\u5f55\u89e3\n res.push([...path]);\n }\n preOrder(root.left, path, res);\n preOrder(root.right, path, res);\n // \u56de\u9000\n path.pop();\n}\n
preorder_traversal_ii_compact.ts/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e8c */\nfunction preOrder(\n root: TreeNode | null,\n path: TreeNode[],\n res: TreeNode[][]\n): void {\n if (root === null) {\n return;\n }\n // \u5c1d\u8bd5\n path.push(root);\n if (root.val === 7) {\n // \u8bb0\u5f55\u89e3\n res.push([...path]);\n }\n preOrder(root.left, path, res);\n preOrder(root.right, path, res);\n // \u56de\u9000\n path.pop();\n}\n
preorder_traversal_ii_compact.dart/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e8c */\nvoid preOrder(\n TreeNode? root,\n List<TreeNode> path,\n List<List<TreeNode>> res,\n) {\n if (root == null) {\n return;\n }\n\n // \u5c1d\u8bd5\n path.add(root);\n if (root.val == 7) {\n // \u8bb0\u5f55\u89e3\n res.add(List.from(path));\n }\n preOrder(root.left, path, res);\n preOrder(root.right, path, res);\n // \u56de\u9000\n path.removeLast();\n}\n
preorder_traversal_ii_compact.rs/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e8c */\nfn pre_order(res: &mut Vec<Vec<Rc<RefCell<TreeNode>>>>, path: &mut Vec<Rc<RefCell<TreeNode>>>, root: Option<Rc<RefCell<TreeNode>>>) {\n if root.is_none() {\n return;\n }\n if let Some(node) = root {\n // \u5c1d\u8bd5\n path.push(node.clone());\n if node.borrow().val == 7 {\n // \u8bb0\u5f55\u89e3\n res.push(path.clone());\n }\n pre_order(res, path, node.borrow().left.clone());\n pre_order(res, path, node.borrow().right.clone());\n // \u56de\u9000\n path.remove(path.len() - 1);\n }\n}\n
preorder_traversal_ii_compact.c/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e8c */\nvoid preOrder(TreeNode *root) {\n if (root == NULL) {\n return;\n }\n // \u5c1d\u8bd5\n path[pathSize++] = root;\n if (root->val == 7) {\n // \u8bb0\u5f55\u89e3\n for (int i = 0; i < pathSize; ++i) {\n res[resSize][i] = path[i];\n }\n resSize++;\n }\n preOrder(root->left);\n preOrder(root->right);\n // \u56de\u9000\n pathSize--;\n}\n
preorder_traversal_ii_compact.zig[class]{}-[func]{preOrder}\n
\u5728\u6bcf\u6b21\u201c\u5c1d\u8bd5\u201d\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u5c06\u5f53\u524d\u8282\u70b9\u6dfb\u52a0\u8fdb path
\u6765\u8bb0\u5f55\u8def\u5f84\uff1b\u800c\u5728\u201c\u56de\u9000\u201d\u524d\uff0c\u6211\u4eec\u9700\u8981\u5c06\u8be5\u8282\u70b9\u4ece path
\u4e2d\u5f39\u51fa\uff0c\u4ee5\u6062\u590d\u672c\u6b21\u5c1d\u8bd5\u4e4b\u524d\u7684\u72b6\u6001\u3002
\u89c2\u5bdf\u56fe 13-2 \u6240\u793a\u7684\u8fc7\u7a0b\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5c1d\u8bd5\u548c\u56de\u9000\u7406\u89e3\u4e3a\u201c\u524d\u8fdb\u201d\u4e0e\u201c\u64a4\u9500\u201d\uff0c\u4e24\u4e2a\u64cd\u4f5c\u4e92\u4e3a\u9006\u5411\u3002
<1><2><3><4><5><6><7><8><9><10><11>\u56fe 13-2 \u00a0 \u5c1d\u8bd5\u4e0e\u56de\u9000
"},{"location":"chapter_backtracking/backtracking_algorithm/#1312","title":"13.1.2 \u00a0 \u526a\u679d","text":"\u590d\u6742\u7684\u56de\u6eaf\u95ee\u9898\u901a\u5e38\u5305\u542b\u4e00\u4e2a\u6216\u591a\u4e2a\u7ea6\u675f\u6761\u4ef6\uff0c\u7ea6\u675f\u6761\u4ef6\u901a\u5e38\u53ef\u7528\u4e8e\u201c\u526a\u679d\u201d\u3002
\u4f8b\u9898\u4e09
\u5728\u4e8c\u53c9\u6811\u4e2d\u641c\u7d22\u6240\u6709\u503c\u4e3a \\(7\\) \u7684\u8282\u70b9\uff0c\u8bf7\u8fd4\u56de\u6839\u8282\u70b9\u5230\u8fd9\u4e9b\u8282\u70b9\u7684\u8def\u5f84\uff0c\u5e76\u8981\u6c42\u8def\u5f84\u4e2d\u4e0d\u5305\u542b\u503c\u4e3a \\(3\\) \u7684\u8282\u70b9\u3002
\u4e3a\u4e86\u6ee1\u8db3\u4ee5\u4e0a\u7ea6\u675f\u6761\u4ef6\uff0c\u6211\u4eec\u9700\u8981\u6dfb\u52a0\u526a\u679d\u64cd\u4f5c\uff1a\u5728\u641c\u7d22\u8fc7\u7a0b\u4e2d\uff0c\u82e5\u9047\u5230\u503c\u4e3a \\(3\\) \u7684\u8282\u70b9\uff0c\u5219\u63d0\u524d\u8fd4\u56de\uff0c\u4e0d\u518d\u7ee7\u7eed\u641c\u7d22\u3002\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig preorder_traversal_iii_compact.pydef pre_order(root: TreeNode):\n \"\"\"\u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e09\"\"\"\n # \u526a\u679d\n if root is None or root.val == 3:\n return\n # \u5c1d\u8bd5\n path.append(root)\n if root.val == 7:\n # \u8bb0\u5f55\u89e3\n res.append(list(path))\n pre_order(root.left)\n pre_order(root.right)\n # \u56de\u9000\n path.pop()\n
preorder_traversal_iii_compact.cpp/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e09 */\nvoid preOrder(TreeNode *root) {\n // \u526a\u679d\n if (root == nullptr || root->val == 3) {\n return;\n }\n // \u5c1d\u8bd5\n path.push_back(root);\n if (root->val == 7) {\n // \u8bb0\u5f55\u89e3\n res.push_back(path);\n }\n preOrder(root->left);\n preOrder(root->right);\n // \u56de\u9000\n path.pop_back();\n}\n
preorder_traversal_iii_compact.java/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e09 */\nvoid preOrder(TreeNode root) {\n // \u526a\u679d\n if (root == null || root.val == 3) {\n return;\n }\n // \u5c1d\u8bd5\n path.add(root);\n if (root.val == 7) {\n // \u8bb0\u5f55\u89e3\n res.add(new ArrayList<>(path));\n }\n preOrder(root.left);\n preOrder(root.right);\n // \u56de\u9000\n path.remove(path.size() - 1);\n}\n
preorder_traversal_iii_compact.cs/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e09 */\nvoid PreOrder(TreeNode? root) {\n // \u526a\u679d\n if (root == null || root.val == 3) {\n return;\n }\n // \u5c1d\u8bd5\n path.Add(root);\n if (root.val == 7) {\n // \u8bb0\u5f55\u89e3\n res.Add(new List<TreeNode>(path));\n }\n PreOrder(root.left);\n PreOrder(root.right);\n // \u56de\u9000\n path.RemoveAt(path.Count - 1);\n}\n
preorder_traversal_iii_compact.go/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e09 */\nfunc preOrderIII(root *TreeNode, res *[][]*TreeNode, path *[]*TreeNode) {\n // \u526a\u679d\n if root == nil || root.Val == 3 {\n return\n }\n // \u5c1d\u8bd5\n *path = append(*path, root)\n if root.Val.(int) == 7 {\n // \u8bb0\u5f55\u89e3\n *res = append(*res, *path)\n }\n preOrderIII(root.Left, res, path)\n preOrderIII(root.Right, res, path)\n // \u56de\u9000\n *path = (*path)[:len(*path)-1]\n}\n
preorder_traversal_iii_compact.swift/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e09 */\nfunc preOrder(root: TreeNode?) {\n // \u526a\u679d\n guard let root = root, root.val != 3 else {\n return\n }\n // \u5c1d\u8bd5\n path.append(root)\n if root.val == 7 {\n // \u8bb0\u5f55\u89e3\n res.append(path)\n }\n preOrder(root: root.left)\n preOrder(root: root.right)\n // \u56de\u9000\n path.removeLast()\n}\n
preorder_traversal_iii_compact.js/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e09 */\nfunction preOrder(root, path, res) {\n // \u526a\u679d\n if (root === null || root.val === 3) {\n return;\n }\n // \u5c1d\u8bd5\n path.push(root);\n if (root.val === 7) {\n // \u8bb0\u5f55\u89e3\n res.push([...path]);\n }\n preOrder(root.left, path, res);\n preOrder(root.right, path, res);\n // \u56de\u9000\n path.pop();\n}\n
preorder_traversal_iii_compact.ts/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e09 */\nfunction preOrder(\n root: TreeNode | null,\n path: TreeNode[],\n res: TreeNode[][]\n): void {\n // \u526a\u679d\n if (root === null || root.val === 3) {\n return;\n }\n // \u5c1d\u8bd5\n path.push(root);\n if (root.val === 7) {\n // \u8bb0\u5f55\u89e3\n res.push([...path]);\n }\n preOrder(root.left, path, res);\n preOrder(root.right, path, res);\n // \u56de\u9000\n path.pop();\n}\n
preorder_traversal_iii_compact.dart/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e09 */\nvoid preOrder(\n TreeNode? root,\n List<TreeNode> path,\n List<List<TreeNode>> res,\n) {\n if (root == null || root.val == 3) {\n return;\n }\n\n // \u5c1d\u8bd5\n path.add(root);\n if (root.val == 7) {\n // \u8bb0\u5f55\u89e3\n res.add(List.from(path));\n }\n preOrder(root.left, path, res);\n preOrder(root.right, path, res);\n // \u56de\u9000\n path.removeLast();\n}\n
preorder_traversal_iii_compact.rs/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e09 */\nfn pre_order(res: &mut Vec<Vec<Rc<RefCell<TreeNode>>>>, path: &mut Vec<Rc<RefCell<TreeNode>>>, root: Option<Rc<RefCell<TreeNode>>>) {\n // \u526a\u679d\n if root.is_none() || root.as_ref().unwrap().borrow().val == 3 {\n return;\n }\n if let Some(node) = root {\n // \u5c1d\u8bd5\n path.push(node.clone());\n if node.borrow().val == 7 {\n // \u8bb0\u5f55\u89e3\n res.push(path.clone());\n }\n pre_order(res, path, node.borrow().left.clone());\n pre_order(res, path, node.borrow().right.clone());\n // \u56de\u9000\n path.remove(path.len() - 1);\n }\n}\n
preorder_traversal_iii_compact.c/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e09 */\nvoid preOrder(TreeNode *root) {\n // \u526a\u679d\n if (root == NULL || root->val == 3) {\n return;\n }\n // \u5c1d\u8bd5\n path[pathSize++] = root;\n if (root->val == 7) {\n // \u8bb0\u5f55\u89e3\n for (int i = 0; i < pathSize; i++) {\n res[resSize][i] = path[i];\n }\n resSize++;\n }\n preOrder(root->left);\n preOrder(root->right);\n // \u56de\u9000\n pathSize--;\n}\n
preorder_traversal_iii_compact.zig[class]{}-[func]{preOrder}\n
\u201c\u526a\u679d\u201d\u662f\u4e00\u4e2a\u975e\u5e38\u5f62\u8c61\u7684\u540d\u8bcd\u3002\u5982\u56fe 13-3 \u6240\u793a\uff0c\u5728\u641c\u7d22\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u201c\u526a\u6389\u201d\u4e86\u4e0d\u6ee1\u8db3\u7ea6\u675f\u6761\u4ef6\u7684\u641c\u7d22\u5206\u652f\uff0c\u907f\u514d\u8bb8\u591a\u65e0\u610f\u4e49\u7684\u5c1d\u8bd5\uff0c\u4ece\u800c\u63d0\u9ad8\u4e86\u641c\u7d22\u6548\u7387\u3002
\u56fe 13-3 \u00a0 \u6839\u636e\u7ea6\u675f\u6761\u4ef6\u526a\u679d
"},{"location":"chapter_backtracking/backtracking_algorithm/#1313","title":"13.1.3 \u00a0 \u6846\u67b6\u4ee3\u7801","text":"\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c1d\u8bd5\u5c06\u56de\u6eaf\u7684\u201c\u5c1d\u8bd5\u3001\u56de\u9000\u3001\u526a\u679d\u201d\u7684\u4e3b\u4f53\u6846\u67b6\u63d0\u70bc\u51fa\u6765\uff0c\u63d0\u5347\u4ee3\u7801\u7684\u901a\u7528\u6027\u3002
\u5728\u4ee5\u4e0b\u6846\u67b6\u4ee3\u7801\u4e2d\uff0cstate
\u8868\u793a\u95ee\u9898\u7684\u5f53\u524d\u72b6\u6001\uff0cchoices
\u8868\u793a\u5f53\u524d\u72b6\u6001\u4e0b\u53ef\u4ee5\u505a\u51fa\u7684\u9009\u62e9\uff1a
def backtrack(state: State, choices: list[choice], res: list[state]):\n \"\"\"\u56de\u6eaf\u7b97\u6cd5\u6846\u67b6\"\"\"\n # \u5224\u65ad\u662f\u5426\u4e3a\u89e3\n if is_solution(state):\n # \u8bb0\u5f55\u89e3\n record_solution(state, res)\n # \u4e0d\u518d\u7ee7\u7eed\u641c\u7d22\n return\n # \u904d\u5386\u6240\u6709\u9009\u62e9\n for choice in choices:\n # \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\n if is_valid(state, choice):\n # \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n make_choice(state, choice)\n backtrack(state, choices, res)\n # \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n undo_choice(state, choice)\n
/* \u56de\u6eaf\u7b97\u6cd5\u6846\u67b6 */\nvoid backtrack(State *state, vector<Choice *> &choices, vector<State *> &res) {\n // \u5224\u65ad\u662f\u5426\u4e3a\u89e3\n if (isSolution(state)) {\n // \u8bb0\u5f55\u89e3\n recordSolution(state, res);\n // \u4e0d\u518d\u7ee7\u7eed\u641c\u7d22\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for (Choice choice : choices) {\n // \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\n if (isValid(state, choice)) {\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n makeChoice(state, choice);\n backtrack(state, choices, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n undoChoice(state, choice);\n }\n }\n}\n
/* \u56de\u6eaf\u7b97\u6cd5\u6846\u67b6 */\nvoid backtrack(State state, List<Choice> choices, List<State> res) {\n // \u5224\u65ad\u662f\u5426\u4e3a\u89e3\n if (isSolution(state)) {\n // \u8bb0\u5f55\u89e3\n recordSolution(state, res);\n // \u4e0d\u518d\u7ee7\u7eed\u641c\u7d22\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for (Choice choice : choices) {\n // \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\n if (isValid(state, choice)) {\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n makeChoice(state, choice);\n backtrack(state, choices, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n undoChoice(state, choice);\n }\n }\n}\n
/* \u56de\u6eaf\u7b97\u6cd5\u6846\u67b6 */\nvoid Backtrack(State state, List<Choice> choices, List<State> res) {\n // \u5224\u65ad\u662f\u5426\u4e3a\u89e3\n if (IsSolution(state)) {\n // \u8bb0\u5f55\u89e3\n RecordSolution(state, res);\n // \u4e0d\u518d\u7ee7\u7eed\u641c\u7d22\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n foreach (Choice choice in choices) {\n // \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\n if (IsValid(state, choice)) {\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n MakeChoice(state, choice);\n Backtrack(state, choices, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n UndoChoice(state, choice);\n }\n }\n}\n
/* \u56de\u6eaf\u7b97\u6cd5\u6846\u67b6 */\nfunc backtrack(state *State, choices []Choice, res *[]State) {\n // \u5224\u65ad\u662f\u5426\u4e3a\u89e3\n if isSolution(state) {\n // \u8bb0\u5f55\u89e3\n recordSolution(state, res)\n // \u4e0d\u518d\u7ee7\u7eed\u641c\u7d22\n return\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for _, choice := range choices {\n // \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\n if isValid(state, choice) {\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n makeChoice(state, choice)\n backtrack(state, choices, res)\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n undoChoice(state, choice)\n }\n }\n}\n
/* \u56de\u6eaf\u7b97\u6cd5\u6846\u67b6 */\nfunc backtrack(state: inout State, choices: [Choice], res: inout [State]) {\n // \u5224\u65ad\u662f\u5426\u4e3a\u89e3\n if isSolution(state: state) {\n // \u8bb0\u5f55\u89e3\n recordSolution(state: state, res: &res)\n // \u4e0d\u518d\u7ee7\u7eed\u641c\u7d22\n return\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for choice in choices {\n // \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\n if isValid(state: state, choice: choice) {\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n makeChoice(state: &state, choice: choice)\n backtrack(state: &state, choices: choices, res: &res)\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n undoChoice(state: &state, choice: choice)\n }\n }\n}\n
/* \u56de\u6eaf\u7b97\u6cd5\u6846\u67b6 */\nfunction backtrack(state, choices, res) {\n // \u5224\u65ad\u662f\u5426\u4e3a\u89e3\n if (isSolution(state)) {\n // \u8bb0\u5f55\u89e3\n recordSolution(state, res);\n // \u4e0d\u518d\u7ee7\u7eed\u641c\u7d22\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for (let choice of choices) {\n // \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\n if (isValid(state, choice)) {\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n makeChoice(state, choice);\n backtrack(state, choices, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n undoChoice(state, choice);\n }\n }\n}\n
/* \u56de\u6eaf\u7b97\u6cd5\u6846\u67b6 */\nfunction backtrack(state: State, choices: Choice[], res: State[]): void {\n // \u5224\u65ad\u662f\u5426\u4e3a\u89e3\n if (isSolution(state)) {\n // \u8bb0\u5f55\u89e3\n recordSolution(state, res);\n // \u4e0d\u518d\u7ee7\u7eed\u641c\u7d22\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for (let choice of choices) {\n // \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\n if (isValid(state, choice)) {\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n makeChoice(state, choice);\n backtrack(state, choices, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n undoChoice(state, choice);\n }\n }\n}\n
/* \u56de\u6eaf\u7b97\u6cd5\u6846\u67b6 */\nvoid backtrack(State state, List<Choice>, List<State> res) {\n // \u5224\u65ad\u662f\u5426\u4e3a\u89e3\n if (isSolution(state)) {\n // \u8bb0\u5f55\u89e3\n recordSolution(state, res);\n // \u4e0d\u518d\u7ee7\u7eed\u641c\u7d22\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for (Choice choice in choices) {\n // \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\n if (isValid(state, choice)) {\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n makeChoice(state, choice);\n backtrack(state, choices, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n undoChoice(state, choice);\n }\n }\n}\n
/* \u56de\u6eaf\u7b97\u6cd5\u6846\u67b6 */\nfn backtrack(state: &mut State, choices: &Vec<Choice>, res: &mut Vec<State>) {\n // \u5224\u65ad\u662f\u5426\u4e3a\u89e3\n if is_solution(state) {\n // \u8bb0\u5f55\u89e3\n record_solution(state, res);\n // \u4e0d\u518d\u7ee7\u7eed\u641c\u7d22\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for choice in choices {\n // \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\n if is_valid(state, choice) {\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n make_choice(state, choice);\n backtrack(state, choices, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n undo_choice(state, choice);\n }\n }\n}\n
/* \u56de\u6eaf\u7b97\u6cd5\u6846\u67b6 */\nvoid backtrack(State *state, Choice *choices, int numChoices, State *res, int numRes) {\n // \u5224\u65ad\u662f\u5426\u4e3a\u89e3\n if (isSolution(state)) {\n // \u8bb0\u5f55\u89e3\n recordSolution(state, res, numRes);\n // \u4e0d\u518d\u7ee7\u7eed\u641c\u7d22\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for (int i = 0; i < numChoices; i++) {\n // \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\n if (isValid(state, &choices[i])) {\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n makeChoice(state, &choices[i]);\n backtrack(state, choices, numChoices, res, numRes);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n undoChoice(state, &choices[i]);\n }\n }\n}\n
\n
\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u57fa\u4e8e\u6846\u67b6\u4ee3\u7801\u6765\u89e3\u51b3\u4f8b\u9898\u4e09\u3002\u72b6\u6001 state
\u4e3a\u8282\u70b9\u904d\u5386\u8def\u5f84\uff0c\u9009\u62e9 choices
\u4e3a\u5f53\u524d\u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u548c\u53f3\u5b50\u8282\u70b9\uff0c\u7ed3\u679c res
\u662f\u8def\u5f84\u5217\u8868\uff1a
def is_solution(state: list[TreeNode]) -> bool:\n \"\"\"\u5224\u65ad\u5f53\u524d\u72b6\u6001\u662f\u5426\u4e3a\u89e3\"\"\"\n return state and state[-1].val == 7\n\ndef record_solution(state: list[TreeNode], res: list[list[TreeNode]]):\n \"\"\"\u8bb0\u5f55\u89e3\"\"\"\n res.append(list(state))\n\ndef is_valid(state: list[TreeNode], choice: TreeNode) -> bool:\n \"\"\"\u5224\u65ad\u5728\u5f53\u524d\u72b6\u6001\u4e0b\uff0c\u8be5\u9009\u62e9\u662f\u5426\u5408\u6cd5\"\"\"\n return choice is not None and choice.val != 3\n\ndef make_choice(state: list[TreeNode], choice: TreeNode):\n \"\"\"\u66f4\u65b0\u72b6\u6001\"\"\"\n state.append(choice)\n\ndef undo_choice(state: list[TreeNode], choice: TreeNode):\n \"\"\"\u6062\u590d\u72b6\u6001\"\"\"\n state.pop()\n\ndef backtrack(\n state: list[TreeNode], choices: list[TreeNode], res: list[list[TreeNode]]\n):\n \"\"\"\u56de\u6eaf\u7b97\u6cd5\uff1a\u4f8b\u9898\u4e09\"\"\"\n # \u68c0\u67e5\u662f\u5426\u4e3a\u89e3\n if is_solution(state):\n # \u8bb0\u5f55\u89e3\n record_solution(state, res)\n # \u904d\u5386\u6240\u6709\u9009\u62e9\n for choice in choices:\n # \u526a\u679d\uff1a\u68c0\u67e5\u9009\u62e9\u662f\u5426\u5408\u6cd5\n if is_valid(state, choice):\n # \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n make_choice(state, choice)\n # \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state, [choice.left, choice.right], res)\n # \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n undo_choice(state, choice)\n
preorder_traversal_iii_template.cpp/* \u5224\u65ad\u5f53\u524d\u72b6\u6001\u662f\u5426\u4e3a\u89e3 */\nbool isSolution(vector<TreeNode *> &state) {\n return !state.empty() && state.back()->val == 7;\n}\n\n/* \u8bb0\u5f55\u89e3 */\nvoid recordSolution(vector<TreeNode *> &state, vector<vector<TreeNode *>> &res) {\n res.push_back(state);\n}\n\n/* \u5224\u65ad\u5728\u5f53\u524d\u72b6\u6001\u4e0b\uff0c\u8be5\u9009\u62e9\u662f\u5426\u5408\u6cd5 */\nbool isValid(vector<TreeNode *> &state, TreeNode *choice) {\n return choice != nullptr && choice->val != 3;\n}\n\n/* \u66f4\u65b0\u72b6\u6001 */\nvoid makeChoice(vector<TreeNode *> &state, TreeNode *choice) {\n state.push_back(choice);\n}\n\n/* \u6062\u590d\u72b6\u6001 */\nvoid undoChoice(vector<TreeNode *> &state, TreeNode *choice) {\n state.pop_back();\n}\n\n/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u4f8b\u9898\u4e09 */\nvoid backtrack(vector<TreeNode *> &state, vector<TreeNode *> &choices, vector<vector<TreeNode *>> &res) {\n // \u68c0\u67e5\u662f\u5426\u4e3a\u89e3\n if (isSolution(state)) {\n // \u8bb0\u5f55\u89e3\n recordSolution(state, res);\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for (TreeNode *choice : choices) {\n // \u526a\u679d\uff1a\u68c0\u67e5\u9009\u62e9\u662f\u5426\u5408\u6cd5\n if (isValid(state, choice)) {\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n makeChoice(state, choice);\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n vector<TreeNode *> nextChoices{choice->left, choice->right};\n backtrack(state, nextChoices, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n undoChoice(state, choice);\n }\n }\n}\n
preorder_traversal_iii_template.java/* \u5224\u65ad\u5f53\u524d\u72b6\u6001\u662f\u5426\u4e3a\u89e3 */\nboolean isSolution(List<TreeNode> state) {\n return !state.isEmpty() && state.get(state.size() - 1).val == 7;\n}\n\n/* \u8bb0\u5f55\u89e3 */\nvoid recordSolution(List<TreeNode> state, List<List<TreeNode>> res) {\n res.add(new ArrayList<>(state));\n}\n\n/* \u5224\u65ad\u5728\u5f53\u524d\u72b6\u6001\u4e0b\uff0c\u8be5\u9009\u62e9\u662f\u5426\u5408\u6cd5 */\nboolean isValid(List<TreeNode> state, TreeNode choice) {\n return choice != null && choice.val != 3;\n}\n\n/* \u66f4\u65b0\u72b6\u6001 */\nvoid makeChoice(List<TreeNode> state, TreeNode choice) {\n state.add(choice);\n}\n\n/* \u6062\u590d\u72b6\u6001 */\nvoid undoChoice(List<TreeNode> state, TreeNode choice) {\n state.remove(state.size() - 1);\n}\n\n/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u4f8b\u9898\u4e09 */\nvoid backtrack(List<TreeNode> state, List<TreeNode> choices, List<List<TreeNode>> res) {\n // \u68c0\u67e5\u662f\u5426\u4e3a\u89e3\n if (isSolution(state)) {\n // \u8bb0\u5f55\u89e3\n recordSolution(state, res);\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for (TreeNode choice : choices) {\n // \u526a\u679d\uff1a\u68c0\u67e5\u9009\u62e9\u662f\u5426\u5408\u6cd5\n if (isValid(state, choice)) {\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n makeChoice(state, choice);\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state, Arrays.asList(choice.left, choice.right), res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n undoChoice(state, choice);\n }\n }\n}\n
preorder_traversal_iii_template.cs/* \u5224\u65ad\u5f53\u524d\u72b6\u6001\u662f\u5426\u4e3a\u89e3 */\nbool IsSolution(List<TreeNode> state) {\n return state.Count != 0 && state[^1].val == 7;\n}\n\n/* \u8bb0\u5f55\u89e3 */\nvoid RecordSolution(List<TreeNode> state, List<List<TreeNode>> res) {\n res.Add(new List<TreeNode>(state));\n}\n\n/* \u5224\u65ad\u5728\u5f53\u524d\u72b6\u6001\u4e0b\uff0c\u8be5\u9009\u62e9\u662f\u5426\u5408\u6cd5 */\nbool IsValid(List<TreeNode> state, TreeNode choice) {\n return choice != null && choice.val != 3;\n}\n\n/* \u66f4\u65b0\u72b6\u6001 */\nvoid MakeChoice(List<TreeNode> state, TreeNode choice) {\n state.Add(choice);\n}\n\n/* \u6062\u590d\u72b6\u6001 */\nvoid UndoChoice(List<TreeNode> state, TreeNode choice) {\n state.RemoveAt(state.Count - 1);\n}\n\n/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u4f8b\u9898\u4e09 */\nvoid Backtrack(List<TreeNode> state, List<TreeNode> choices, List<List<TreeNode>> res) {\n // \u68c0\u67e5\u662f\u5426\u4e3a\u89e3\n if (IsSolution(state)) {\n // \u8bb0\u5f55\u89e3\n RecordSolution(state, res);\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n foreach (TreeNode choice in choices) {\n // \u526a\u679d\uff1a\u68c0\u67e5\u9009\u62e9\u662f\u5426\u5408\u6cd5\n if (IsValid(state, choice)) {\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n MakeChoice(state, choice);\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n Backtrack(state, [choice.left!, choice.right!], res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n UndoChoice(state, choice);\n }\n }\n}\n
preorder_traversal_iii_template.go/* \u5224\u65ad\u5f53\u524d\u72b6\u6001\u662f\u5426\u4e3a\u89e3 */\nfunc isSolution(state *[]*TreeNode) bool {\n return len(*state) != 0 && (*state)[len(*state)-1].Val == 7\n}\n\n/* \u8bb0\u5f55\u89e3 */\nfunc recordSolution(state *[]*TreeNode, res *[][]*TreeNode) {\n *res = append(*res, *state)\n}\n\n/* \u5224\u65ad\u5728\u5f53\u524d\u72b6\u6001\u4e0b\uff0c\u8be5\u9009\u62e9\u662f\u5426\u5408\u6cd5 */\nfunc isValid(state *[]*TreeNode, choice *TreeNode) bool {\n return choice != nil && choice.Val != 3\n}\n\n/* \u66f4\u65b0\u72b6\u6001 */\nfunc makeChoice(state *[]*TreeNode, choice *TreeNode) {\n *state = append(*state, choice)\n}\n\n/* \u6062\u590d\u72b6\u6001 */\nfunc undoChoice(state *[]*TreeNode, choice *TreeNode) {\n *state = (*state)[:len(*state)-1]\n}\n\n/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u4f8b\u9898\u4e09 */\nfunc backtrackIII(state *[]*TreeNode, choices *[]*TreeNode, res *[][]*TreeNode) {\n // \u68c0\u67e5\u662f\u5426\u4e3a\u89e3\n if isSolution(state) {\n // \u8bb0\u5f55\u89e3\n recordSolution(state, res)\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for _, choice := range *choices {\n // \u526a\u679d\uff1a\u68c0\u67e5\u9009\u62e9\u662f\u5426\u5408\u6cd5\n if isValid(state, choice) {\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n makeChoice(state, choice)\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n temp := make([]*TreeNode, 0)\n temp = append(temp, choice.Left, choice.Right)\n backtrackIII(state, &temp, res)\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n undoChoice(state, choice)\n }\n }\n}\n
preorder_traversal_iii_template.swift/* \u5224\u65ad\u5f53\u524d\u72b6\u6001\u662f\u5426\u4e3a\u89e3 */\nfunc isSolution(state: [TreeNode]) -> Bool {\n !state.isEmpty && state.last!.val == 7\n}\n\n/* \u8bb0\u5f55\u89e3 */\nfunc recordSolution(state: [TreeNode], res: inout [[TreeNode]]) {\n res.append(state)\n}\n\n/* \u5224\u65ad\u5728\u5f53\u524d\u72b6\u6001\u4e0b\uff0c\u8be5\u9009\u62e9\u662f\u5426\u5408\u6cd5 */\nfunc isValid(state: [TreeNode], choice: TreeNode?) -> Bool {\n choice != nil && choice!.val != 3\n}\n\n/* \u66f4\u65b0\u72b6\u6001 */\nfunc makeChoice(state: inout [TreeNode], choice: TreeNode) {\n state.append(choice)\n}\n\n/* \u6062\u590d\u72b6\u6001 */\nfunc undoChoice(state: inout [TreeNode], choice: TreeNode) {\n state.removeLast()\n}\n\n/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u4f8b\u9898\u4e09 */\nfunc backtrack(state: inout [TreeNode], choices: [TreeNode], res: inout [[TreeNode]]) {\n // \u68c0\u67e5\u662f\u5426\u4e3a\u89e3\n if isSolution(state: state) {\n recordSolution(state: state, res: &res)\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for choice in choices {\n // \u526a\u679d\uff1a\u68c0\u67e5\u9009\u62e9\u662f\u5426\u5408\u6cd5\n if isValid(state: state, choice: choice) {\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n makeChoice(state: &state, choice: choice)\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state: &state, choices: [choice.left, choice.right].compactMap { $0 }, res: &res)\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n undoChoice(state: &state, choice: choice)\n }\n }\n}\n
preorder_traversal_iii_template.js/* \u5224\u65ad\u5f53\u524d\u72b6\u6001\u662f\u5426\u4e3a\u89e3 */\nfunction isSolution(state) {\n return state && state[state.length - 1]?.val === 7;\n}\n\n/* \u8bb0\u5f55\u89e3 */\nfunction recordSolution(state, res) {\n res.push([...state]);\n}\n\n/* \u5224\u65ad\u5728\u5f53\u524d\u72b6\u6001\u4e0b\uff0c\u8be5\u9009\u62e9\u662f\u5426\u5408\u6cd5 */\nfunction isValid(state, choice) {\n return choice !== null && choice.val !== 3;\n}\n\n/* \u66f4\u65b0\u72b6\u6001 */\nfunction makeChoice(state, choice) {\n state.push(choice);\n}\n\n/* \u6062\u590d\u72b6\u6001 */\nfunction undoChoice(state) {\n state.pop();\n}\n\n/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u4f8b\u9898\u4e09 */\nfunction backtrack(state, choices, res) {\n // \u68c0\u67e5\u662f\u5426\u4e3a\u89e3\n if (isSolution(state)) {\n // \u8bb0\u5f55\u89e3\n recordSolution(state, res);\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for (const choice of choices) {\n // \u526a\u679d\uff1a\u68c0\u67e5\u9009\u62e9\u662f\u5426\u5408\u6cd5\n if (isValid(state, choice)) {\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n makeChoice(state, choice);\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state, [choice.left, choice.right], res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n undoChoice(state);\n }\n }\n}\n
preorder_traversal_iii_template.ts/* \u5224\u65ad\u5f53\u524d\u72b6\u6001\u662f\u5426\u4e3a\u89e3 */\nfunction isSolution(state: TreeNode[]): boolean {\n return state && state[state.length - 1]?.val === 7;\n}\n\n/* \u8bb0\u5f55\u89e3 */\nfunction recordSolution(state: TreeNode[], res: TreeNode[][]): void {\n res.push([...state]);\n}\n\n/* \u5224\u65ad\u5728\u5f53\u524d\u72b6\u6001\u4e0b\uff0c\u8be5\u9009\u62e9\u662f\u5426\u5408\u6cd5 */\nfunction isValid(state: TreeNode[], choice: TreeNode): boolean {\n return choice !== null && choice.val !== 3;\n}\n\n/* \u66f4\u65b0\u72b6\u6001 */\nfunction makeChoice(state: TreeNode[], choice: TreeNode): void {\n state.push(choice);\n}\n\n/* \u6062\u590d\u72b6\u6001 */\nfunction undoChoice(state: TreeNode[]): void {\n state.pop();\n}\n\n/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u4f8b\u9898\u4e09 */\nfunction backtrack(\n state: TreeNode[],\n choices: TreeNode[],\n res: TreeNode[][]\n): void {\n // \u68c0\u67e5\u662f\u5426\u4e3a\u89e3\n if (isSolution(state)) {\n // \u8bb0\u5f55\u89e3\n recordSolution(state, res);\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for (const choice of choices) {\n // \u526a\u679d\uff1a\u68c0\u67e5\u9009\u62e9\u662f\u5426\u5408\u6cd5\n if (isValid(state, choice)) {\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n makeChoice(state, choice);\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state, [choice.left, choice.right], res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n undoChoice(state);\n }\n }\n}\n
preorder_traversal_iii_template.dart/* \u5224\u65ad\u5f53\u524d\u72b6\u6001\u662f\u5426\u4e3a\u89e3 */\nbool isSolution(List<TreeNode> state) {\n return state.isNotEmpty && state.last.val == 7;\n}\n\n/* \u8bb0\u5f55\u89e3 */\nvoid recordSolution(List<TreeNode> state, List<List<TreeNode>> res) {\n res.add(List.from(state));\n}\n\n/* \u5224\u65ad\u5728\u5f53\u524d\u72b6\u6001\u4e0b\uff0c\u8be5\u9009\u62e9\u662f\u5426\u5408\u6cd5 */\nbool isValid(List<TreeNode> state, TreeNode? choice) {\n return choice != null && choice.val != 3;\n}\n\n/* \u66f4\u65b0\u72b6\u6001 */\nvoid makeChoice(List<TreeNode> state, TreeNode? choice) {\n state.add(choice!);\n}\n\n/* \u6062\u590d\u72b6\u6001 */\nvoid undoChoice(List<TreeNode> state, TreeNode? choice) {\n state.removeLast();\n}\n\n/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u4f8b\u9898\u4e09 */\nvoid backtrack(\n List<TreeNode> state,\n List<TreeNode?> choices,\n List<List<TreeNode>> res,\n) {\n // \u68c0\u67e5\u662f\u5426\u4e3a\u89e3\n if (isSolution(state)) {\n // \u8bb0\u5f55\u89e3\n recordSolution(state, res);\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for (TreeNode? choice in choices) {\n // \u526a\u679d\uff1a\u68c0\u67e5\u9009\u62e9\u662f\u5426\u5408\u6cd5\n if (isValid(state, choice)) {\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n makeChoice(state, choice);\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state, [choice!.left, choice.right], res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n undoChoice(state, choice);\n }\n }\n}\n
preorder_traversal_iii_template.rs/* \u5224\u65ad\u5f53\u524d\u72b6\u6001\u662f\u5426\u4e3a\u89e3 */\nfn is_solution(state: &mut Vec<Rc<RefCell<TreeNode>>>) -> bool {\n return !state.is_empty() && state.get(state.len() - 1).unwrap().borrow().val == 7;\n}\n\n/* \u8bb0\u5f55\u89e3 */\nfn record_solution(state: &mut Vec<Rc<RefCell<TreeNode>>>, res: &mut Vec<Vec<Rc<RefCell<TreeNode>>>>) {\n res.push(state.clone());\n}\n\n/* \u5224\u65ad\u5728\u5f53\u524d\u72b6\u6001\u4e0b\uff0c\u8be5\u9009\u62e9\u662f\u5426\u5408\u6cd5 */\nfn is_valid(_: &mut Vec<Rc<RefCell<TreeNode>>>, choice: Rc<RefCell<TreeNode>>) -> bool {\n return choice.borrow().val != 3;\n}\n\n/* \u66f4\u65b0\u72b6\u6001 */\nfn make_choice(state: &mut Vec<Rc<RefCell<TreeNode>>>, choice: Rc<RefCell<TreeNode>>) {\n state.push(choice);\n}\n\n/* \u6062\u590d\u72b6\u6001 */\nfn undo_choice(state: &mut Vec<Rc<RefCell<TreeNode>>>, _: Rc<RefCell<TreeNode>>) {\n state.remove(state.len() - 1);\n}\n\n/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u4f8b\u9898\u4e09 */\nfn backtrack(state: &mut Vec<Rc<RefCell<TreeNode>>>, choices: &mut Vec<Rc<RefCell<TreeNode>>>, res: &mut Vec<Vec<Rc<RefCell<TreeNode>>>>) {\n // \u68c0\u67e5\u662f\u5426\u4e3a\u89e3\n if is_solution(state) {\n // \u8bb0\u5f55\u89e3\n record_solution(state, res);\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for choice in choices {\n // \u526a\u679d\uff1a\u68c0\u67e5\u9009\u62e9\u662f\u5426\u5408\u6cd5\n if is_valid(state, choice.clone()) {\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n make_choice(state, choice.clone());\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state, &mut vec![choice.borrow().left.clone().unwrap(), choice.borrow().right.clone().unwrap()], res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n undo_choice(state, choice.clone());\n }\n }\n}\n
preorder_traversal_iii_template.c/* \u5224\u65ad\u5f53\u524d\u72b6\u6001\u662f\u5426\u4e3a\u89e3 */\nbool isSolution(void) {\n return pathSize > 0 && path[pathSize - 1]->val == 7;\n}\n\n/* \u8bb0\u5f55\u89e3 */\nvoid recordSolution(void) {\n for (int i = 0; i < pathSize; i++) {\n res[resSize][i] = path[i];\n }\n resSize++;\n}\n\n/* \u5224\u65ad\u5728\u5f53\u524d\u72b6\u6001\u4e0b\uff0c\u8be5\u9009\u62e9\u662f\u5426\u5408\u6cd5 */\nbool isValid(TreeNode *choice) {\n return choice != NULL && choice->val != 3;\n}\n\n/* \u66f4\u65b0\u72b6\u6001 */\nvoid makeChoice(TreeNode *choice) {\n path[pathSize++] = choice;\n}\n\n/* \u6062\u590d\u72b6\u6001 */\nvoid undoChoice(void) {\n pathSize--;\n}\n\n/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u4f8b\u9898\u4e09 */\nvoid backtrack(TreeNode *choices[2]) {\n // \u68c0\u67e5\u662f\u5426\u4e3a\u89e3\n if (isSolution()) {\n // \u8bb0\u5f55\u89e3\n recordSolution();\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for (int i = 0; i < 2; i++) {\n TreeNode *choice = choices[i];\n // \u526a\u679d\uff1a\u68c0\u67e5\u9009\u62e9\u662f\u5426\u5408\u6cd5\n if (isValid(choice)) {\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n makeChoice(choice);\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n TreeNode *nextChoices[2] = {choice->left, choice->right};\n backtrack(nextChoices);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n undoChoice();\n }\n }\n}\n
preorder_traversal_iii_template.zig[class]{}-[func]{isSolution}\n\n[class]{}-[func]{recordSolution}\n\n[class]{}-[func]{isValid}\n\n[class]{}-[func]{makeChoice}\n\n[class]{}-[func]{undoChoice}\n\n[class]{}-[func]{backtrack}\n
\u6839\u636e\u9898\u610f\uff0c\u6211\u4eec\u5728\u627e\u5230\u503c\u4e3a \\(7\\) \u7684\u8282\u70b9\u540e\u5e94\u8be5\u7ee7\u7eed\u641c\u7d22\uff0c\u56e0\u6b64\u9700\u8981\u5c06\u8bb0\u5f55\u89e3\u4e4b\u540e\u7684 return
\u8bed\u53e5\u5220\u9664\u3002\u56fe 13-4 \u5bf9\u6bd4\u4e86\u4fdd\u7559\u6216\u5220\u9664 return
\u8bed\u53e5\u7684\u641c\u7d22\u8fc7\u7a0b\u3002
\u56fe 13-4 \u00a0 \u4fdd\u7559\u4e0e\u5220\u9664 return \u7684\u641c\u7d22\u8fc7\u7a0b\u5bf9\u6bd4
\u76f8\u6bd4\u57fa\u4e8e\u524d\u5e8f\u904d\u5386\u7684\u4ee3\u7801\u5b9e\u73b0\uff0c\u57fa\u4e8e\u56de\u6eaf\u7b97\u6cd5\u6846\u67b6\u7684\u4ee3\u7801\u5b9e\u73b0\u867d\u7136\u663e\u5f97\u5570\u5506\uff0c\u4f46\u901a\u7528\u6027\u66f4\u597d\u3002\u5b9e\u9645\u4e0a\uff0c\u8bb8\u591a\u56de\u6eaf\u95ee\u9898\u53ef\u4ee5\u5728\u8be5\u6846\u67b6\u4e0b\u89e3\u51b3\u3002\u6211\u4eec\u53ea\u9700\u6839\u636e\u5177\u4f53\u95ee\u9898\u6765\u5b9a\u4e49 state
\u548c choices
\uff0c\u5e76\u5b9e\u73b0\u6846\u67b6\u4e2d\u7684\u5404\u4e2a\u65b9\u6cd5\u5373\u53ef\u3002
\u4e3a\u4e86\u66f4\u6e05\u6670\u5730\u5206\u6790\u7b97\u6cd5\u95ee\u9898\uff0c\u6211\u4eec\u603b\u7ed3\u4e00\u4e0b\u56de\u6eaf\u7b97\u6cd5\u4e2d\u5e38\u7528\u672f\u8bed\u7684\u542b\u4e49\uff0c\u5e76\u5bf9\u7167\u4f8b\u9898\u4e09\u7ed9\u51fa\u5bf9\u5e94\u793a\u4f8b\uff0c\u5982\u8868 13-1 \u6240\u793a\u3002
\u8868 13-1 \u00a0 \u5e38\u89c1\u7684\u56de\u6eaf\u7b97\u6cd5\u672f\u8bed
\u540d\u8bcd \u5b9a\u4e49 \u4f8b\u9898\u4e09 \u89e3\uff08solution\uff09 \u89e3\u662f\u6ee1\u8db3\u95ee\u9898\u7279\u5b9a\u6761\u4ef6\u7684\u7b54\u6848\uff0c\u53ef\u80fd\u6709\u4e00\u4e2a\u6216\u591a\u4e2a \u6839\u8282\u70b9\u5230\u8282\u70b9 \\(7\\) \u7684\u6ee1\u8db3\u7ea6\u675f\u6761\u4ef6\u7684\u6240\u6709\u8def\u5f84 \u7ea6\u675f\u6761\u4ef6\uff08constraint\uff09 \u7ea6\u675f\u6761\u4ef6\u662f\u95ee\u9898\u4e2d\u9650\u5236\u89e3\u7684\u53ef\u884c\u6027\u7684\u6761\u4ef6\uff0c\u901a\u5e38\u7528\u4e8e\u526a\u679d \u8def\u5f84\u4e2d\u4e0d\u5305\u542b\u8282\u70b9 \\(3\\) \u72b6\u6001\uff08state\uff09 \u72b6\u6001\u8868\u793a\u95ee\u9898\u5728\u67d0\u4e00\u65f6\u523b\u7684\u60c5\u51b5\uff0c\u5305\u62ec\u5df2\u7ecf\u505a\u51fa\u7684\u9009\u62e9 \u5f53\u524d\u5df2\u8bbf\u95ee\u7684\u8282\u70b9\u8def\u5f84\uff0c\u5373path
\u8282\u70b9\u5217\u8868 \u5c1d\u8bd5\uff08attempt\uff09 \u5c1d\u8bd5\u662f\u6839\u636e\u53ef\u7528\u9009\u62e9\u6765\u63a2\u7d22\u89e3\u7a7a\u95f4\u7684\u8fc7\u7a0b\uff0c\u5305\u62ec\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\uff0c\u68c0\u67e5\u662f\u5426\u4e3a\u89e3 \u9012\u5f52\u8bbf\u95ee\u5de6\uff08\u53f3\uff09\u5b50\u8282\u70b9\uff0c\u5c06\u8282\u70b9\u6dfb\u52a0\u8fdb path
\uff0c\u5224\u65ad\u8282\u70b9\u7684\u503c\u662f\u5426\u4e3a \\(7\\) \u56de\u9000\uff08backtracking\uff09 \u56de\u9000\u6307\u9047\u5230\u4e0d\u6ee1\u8db3\u7ea6\u675f\u6761\u4ef6\u7684\u72b6\u6001\u65f6\uff0c\u64a4\u9500\u524d\u9762\u505a\u51fa\u7684\u9009\u62e9\uff0c\u56de\u5230\u4e0a\u4e00\u4e2a\u72b6\u6001 \u5f53\u8d8a\u8fc7\u53f6\u8282\u70b9\u3001\u7ed3\u675f\u8282\u70b9\u8bbf\u95ee\u3001\u9047\u5230\u503c\u4e3a \\(3\\) \u7684\u8282\u70b9\u65f6\u7ec8\u6b62\u641c\u7d22\uff0c\u51fd\u6570\u8fd4\u56de \u526a\u679d\uff08pruning\uff09 \u526a\u679d\u662f\u6839\u636e\u95ee\u9898\u7279\u6027\u548c\u7ea6\u675f\u6761\u4ef6\u907f\u514d\u65e0\u610f\u4e49\u7684\u641c\u7d22\u8def\u5f84\u7684\u65b9\u6cd5\uff0c\u53ef\u63d0\u9ad8\u641c\u7d22\u6548\u7387 \u5f53\u9047\u5230\u503c\u4e3a \\(3\\) \u7684\u8282\u70b9\u65f6\uff0c\u5219\u4e0d\u518d\u7ee7\u7eed\u641c\u7d22 Tip
\u95ee\u9898\u3001\u89e3\u3001\u72b6\u6001\u7b49\u6982\u5ff5\u662f\u901a\u7528\u7684\uff0c\u5728\u5206\u6cbb\u3001\u56de\u6eaf\u3001\u52a8\u6001\u89c4\u5212\u3001\u8d2a\u5fc3\u7b49\u7b97\u6cd5\u4e2d\u90fd\u6709\u6d89\u53ca\u3002
"},{"location":"chapter_backtracking/backtracking_algorithm/#1315","title":"13.1.5 \u00a0 \u4f18\u70b9\u4e0e\u5c40\u9650\u6027","text":"\u56de\u6eaf\u7b97\u6cd5\u672c\u8d28\u4e0a\u662f\u4e00\u79cd\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u7b97\u6cd5\uff0c\u5b83\u5c1d\u8bd5\u6240\u6709\u53ef\u80fd\u7684\u89e3\u51b3\u65b9\u6848\u76f4\u5230\u627e\u5230\u6ee1\u8db3\u6761\u4ef6\u7684\u89e3\u3002\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u5728\u4e8e\u80fd\u591f\u627e\u5230\u6240\u6709\u53ef\u80fd\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u800c\u4e14\u5728\u5408\u7406\u7684\u526a\u679d\u64cd\u4f5c\u4e0b\uff0c\u5177\u6709\u5f88\u9ad8\u7684\u6548\u7387\u3002
\u7136\u800c\uff0c\u5728\u5904\u7406\u5927\u89c4\u6a21\u6216\u8005\u590d\u6742\u95ee\u9898\u65f6\uff0c\u56de\u6eaf\u7b97\u6cd5\u7684\u8fd0\u884c\u6548\u7387\u53ef\u80fd\u96be\u4ee5\u63a5\u53d7\u3002
\u5373\u4fbf\u5982\u6b64\uff0c\u56de\u6eaf\u7b97\u6cd5\u4ecd\u7136\u662f\u67d0\u4e9b\u641c\u7d22\u95ee\u9898\u548c\u7ea6\u675f\u6ee1\u8db3\u95ee\u9898\u7684\u6700\u4f73\u89e3\u51b3\u65b9\u6848\u3002\u5bf9\u4e8e\u8fd9\u4e9b\u95ee\u9898\uff0c\u7531\u4e8e\u65e0\u6cd5\u9884\u6d4b\u54ea\u4e9b\u9009\u62e9\u53ef\u751f\u6210\u6709\u6548\u7684\u89e3\uff0c\u56e0\u6b64\u6211\u4eec\u5fc5\u987b\u5bf9\u6240\u6709\u53ef\u80fd\u7684\u9009\u62e9\u8fdb\u884c\u904d\u5386\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5173\u952e\u662f\u5982\u4f55\u4f18\u5316\u6548\u7387\uff0c\u5e38\u89c1\u7684\u6548\u7387\u4f18\u5316\u65b9\u6cd5\u6709\u4e24\u79cd\u3002
\u56de\u6eaf\u7b97\u6cd5\u53ef\u7528\u4e8e\u89e3\u51b3\u8bb8\u591a\u641c\u7d22\u95ee\u9898\u3001\u7ea6\u675f\u6ee1\u8db3\u95ee\u9898\u548c\u7ec4\u5408\u4f18\u5316\u95ee\u9898\u3002
\u641c\u7d22\u95ee\u9898\uff1a\u8fd9\u7c7b\u95ee\u9898\u7684\u76ee\u6807\u662f\u627e\u5230\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u7684\u89e3\u51b3\u65b9\u6848\u3002
\u7ea6\u675f\u6ee1\u8db3\u95ee\u9898\uff1a\u8fd9\u7c7b\u95ee\u9898\u7684\u76ee\u6807\u662f\u627e\u5230\u6ee1\u8db3\u6240\u6709\u7ea6\u675f\u6761\u4ef6\u7684\u89e3\u3002
\u7ec4\u5408\u4f18\u5316\u95ee\u9898\uff1a\u8fd9\u7c7b\u95ee\u9898\u7684\u76ee\u6807\u662f\u5728\u4e00\u4e2a\u7ec4\u5408\u7a7a\u95f4\u4e2d\u627e\u5230\u6ee1\u8db3\u67d0\u4e9b\u6761\u4ef6\u7684\u6700\u4f18\u89e3\u3002
\u8bf7\u6ce8\u610f\uff0c\u5bf9\u4e8e\u8bb8\u591a\u7ec4\u5408\u4f18\u5316\u95ee\u9898\uff0c\u56de\u6eaf\u4e0d\u662f\u6700\u4f18\u89e3\u51b3\u65b9\u6848\u3002
Question
\u6839\u636e\u56fd\u9645\u8c61\u68cb\u7684\u89c4\u5219\uff0c\u7687\u540e\u53ef\u4ee5\u653b\u51fb\u4e0e\u540c\u5904\u4e00\u884c\u3001\u4e00\u5217\u6216\u4e00\u6761\u659c\u7ebf\u4e0a\u7684\u68cb\u5b50\u3002\u7ed9\u5b9a \\(n\\) \u4e2a\u7687\u540e\u548c\u4e00\u4e2a \\(n \\times n\\) \u5927\u5c0f\u7684\u68cb\u76d8\uff0c\u5bfb\u627e\u4f7f\u5f97\u6240\u6709\u7687\u540e\u4e4b\u95f4\u65e0\u6cd5\u76f8\u4e92\u653b\u51fb\u7684\u6446\u653e\u65b9\u6848\u3002
\u5982\u56fe 13-15 \u6240\u793a\uff0c\u5f53 \\(n = 4\\) \u65f6\uff0c\u5171\u53ef\u4ee5\u627e\u5230\u4e24\u4e2a\u89e3\u3002\u4ece\u56de\u6eaf\u7b97\u6cd5\u7684\u89d2\u5ea6\u770b\uff0c\\(n \\times n\\) \u5927\u5c0f\u7684\u68cb\u76d8\u5171\u6709 \\(n^2\\) \u4e2a\u683c\u5b50\uff0c\u7ed9\u51fa\u4e86\u6240\u6709\u7684\u9009\u62e9 choices
\u3002\u5728\u9010\u4e2a\u653e\u7f6e\u7687\u540e\u7684\u8fc7\u7a0b\u4e2d\uff0c\u68cb\u76d8\u72b6\u6001\u5728\u4e0d\u65ad\u5730\u53d8\u5316\uff0c\u6bcf\u4e2a\u65f6\u523b\u7684\u68cb\u76d8\u5c31\u662f\u72b6\u6001 state
\u3002
\u56fe 13-15 \u00a0 4 \u7687\u540e\u95ee\u9898\u7684\u89e3
\u56fe 13-16 \u5c55\u793a\u4e86\u672c\u9898\u7684\u4e09\u4e2a\u7ea6\u675f\u6761\u4ef6\uff1a\u591a\u4e2a\u7687\u540e\u4e0d\u80fd\u5728\u540c\u4e00\u884c\u3001\u540c\u4e00\u5217\u3001\u540c\u4e00\u5bf9\u89d2\u7ebf\u3002\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u5bf9\u89d2\u7ebf\u5206\u4e3a\u4e3b\u5bf9\u89d2\u7ebf \\
\u548c\u6b21\u5bf9\u89d2\u7ebf /
\u4e24\u79cd\u3002
\u56fe 13-16 \u00a0 n \u7687\u540e\u95ee\u9898\u7684\u7ea6\u675f\u6761\u4ef6
"},{"location":"chapter_backtracking/n_queens_problem/#1","title":"1. \u00a0 \u9010\u884c\u653e\u7f6e\u7b56\u7565","text":"\u7687\u540e\u7684\u6570\u91cf\u548c\u68cb\u76d8\u7684\u884c\u6570\u90fd\u4e3a \\(n\\) \uff0c\u56e0\u6b64\u6211\u4eec\u5bb9\u6613\u5f97\u5230\u4e00\u4e2a\u63a8\u8bba\uff1a\u68cb\u76d8\u6bcf\u884c\u90fd\u5141\u8bb8\u4e14\u53ea\u5141\u8bb8\u653e\u7f6e\u4e00\u4e2a\u7687\u540e\u3002
\u4e5f\u5c31\u662f\u8bf4\uff0c\u6211\u4eec\u53ef\u4ee5\u91c7\u53d6\u9010\u884c\u653e\u7f6e\u7b56\u7565\uff1a\u4ece\u7b2c\u4e00\u884c\u5f00\u59cb\uff0c\u5728\u6bcf\u884c\u653e\u7f6e\u4e00\u4e2a\u7687\u540e\uff0c\u76f4\u81f3\u6700\u540e\u4e00\u884c\u7ed3\u675f\u3002
\u56fe 13-17 \u6240\u793a\u4e3a \\(4\\) \u7687\u540e\u95ee\u9898\u7684\u9010\u884c\u653e\u7f6e\u8fc7\u7a0b\u3002\u53d7\u753b\u5e45\u9650\u5236\uff0c\u56fe 13-17 \u4ec5\u5c55\u5f00\u4e86\u7b2c\u4e00\u884c\u7684\u5176\u4e2d\u4e00\u4e2a\u641c\u7d22\u5206\u652f\uff0c\u5e76\u4e14\u5c06\u4e0d\u6ee1\u8db3\u5217\u7ea6\u675f\u548c\u5bf9\u89d2\u7ebf\u7ea6\u675f\u7684\u65b9\u6848\u90fd\u8fdb\u884c\u4e86\u526a\u679d\u3002
\u56fe 13-17 \u00a0 \u9010\u884c\u653e\u7f6e\u7b56\u7565
\u4ece\u672c\u8d28\u4e0a\u770b\uff0c\u9010\u884c\u653e\u7f6e\u7b56\u7565\u8d77\u5230\u4e86\u526a\u679d\u7684\u4f5c\u7528\uff0c\u5b83\u907f\u514d\u4e86\u540c\u4e00\u884c\u51fa\u73b0\u591a\u4e2a\u7687\u540e\u7684\u6240\u6709\u641c\u7d22\u5206\u652f\u3002
"},{"location":"chapter_backtracking/n_queens_problem/#2","title":"2. \u00a0 \u5217\u4e0e\u5bf9\u89d2\u7ebf\u526a\u679d","text":"\u4e3a\u4e86\u6ee1\u8db3\u5217\u7ea6\u675f\uff0c\u6211\u4eec\u53ef\u4ee5\u5229\u7528\u4e00\u4e2a\u957f\u5ea6\u4e3a \\(n\\) \u7684\u5e03\u5c14\u578b\u6570\u7ec4 cols
\u8bb0\u5f55\u6bcf\u4e00\u5217\u662f\u5426\u6709\u7687\u540e\u3002\u5728\u6bcf\u6b21\u51b3\u5b9a\u653e\u7f6e\u524d\uff0c\u6211\u4eec\u901a\u8fc7 cols
\u5c06\u5df2\u6709\u7687\u540e\u7684\u5217\u8fdb\u884c\u526a\u679d\uff0c\u5e76\u5728\u56de\u6eaf\u4e2d\u52a8\u6001\u66f4\u65b0 cols
\u7684\u72b6\u6001\u3002
\u90a3\u4e48\uff0c\u5982\u4f55\u5904\u7406\u5bf9\u89d2\u7ebf\u7ea6\u675f\u5462\uff1f\u8bbe\u68cb\u76d8\u4e2d\u67d0\u4e2a\u683c\u5b50\u7684\u884c\u5217\u7d22\u5f15\u4e3a \\((row, col)\\) \uff0c\u9009\u5b9a\u77e9\u9635\u4e2d\u7684\u67d0\u6761\u4e3b\u5bf9\u89d2\u7ebf\uff0c\u6211\u4eec\u53d1\u73b0\u8be5\u5bf9\u89d2\u7ebf\u4e0a\u6240\u6709\u683c\u5b50\u7684\u884c\u7d22\u5f15\u51cf\u5217\u7d22\u5f15\u90fd\u76f8\u7b49\uff0c\u5373\u5bf9\u89d2\u7ebf\u4e0a\u6240\u6709\u683c\u5b50\u7684 \\(row - col\\) \u4e3a\u6052\u5b9a\u503c\u3002
\u4e5f\u5c31\u662f\u8bf4\uff0c\u5982\u679c\u4e24\u4e2a\u683c\u5b50\u6ee1\u8db3 \\(row_1 - col_1 = row_2 - col_2\\) \uff0c\u5219\u5b83\u4eec\u4e00\u5b9a\u5904\u5728\u540c\u4e00\u6761\u4e3b\u5bf9\u89d2\u7ebf\u4e0a\u3002\u5229\u7528\u8be5\u89c4\u5f8b\uff0c\u6211\u4eec\u53ef\u4ee5\u501f\u52a9\u56fe 13-18 \u6240\u793a\u7684\u6570\u7ec4 diags1
\u8bb0\u5f55\u6bcf\u6761\u4e3b\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\u3002
\u540c\u7406\uff0c\u6b21\u5bf9\u89d2\u7ebf\u4e0a\u7684\u6240\u6709\u683c\u5b50\u7684 \\(row + col\\) \u662f\u6052\u5b9a\u503c\u3002\u6211\u4eec\u540c\u6837\u4e5f\u53ef\u4ee5\u501f\u52a9\u6570\u7ec4 diags2
\u6765\u5904\u7406\u6b21\u5bf9\u89d2\u7ebf\u7ea6\u675f\u3002
\u56fe 13-18 \u00a0 \u5904\u7406\u5217\u7ea6\u675f\u548c\u5bf9\u89d2\u7ebf\u7ea6\u675f
"},{"location":"chapter_backtracking/n_queens_problem/#3","title":"3. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"\u8bf7\u6ce8\u610f\uff0c\\(n\\) \u7ef4\u65b9\u9635\u4e2d \\(row - col\\) \u7684\u8303\u56f4\u662f \\([-n + 1, n - 1]\\) \uff0c\\(row + col\\) \u7684\u8303\u56f4\u662f \\([0, 2n - 2]\\) \uff0c\u6240\u4ee5\u4e3b\u5bf9\u89d2\u7ebf\u548c\u6b21\u5bf9\u89d2\u7ebf\u7684\u6570\u91cf\u90fd\u4e3a \\(2n - 1\\) \uff0c\u5373\u6570\u7ec4 diags1
\u548c diags2
\u7684\u957f\u5ea6\u90fd\u4e3a \\(2n - 1\\) \u3002
def backtrack(\n row: int,\n n: int,\n state: list[list[str]],\n res: list[list[list[str]]],\n cols: list[bool],\n diags1: list[bool],\n diags2: list[bool],\n):\n \"\"\"\u56de\u6eaf\u7b97\u6cd5\uff1aN \u7687\u540e\"\"\"\n # \u5f53\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u65f6\uff0c\u8bb0\u5f55\u89e3\n if row == n:\n res.append([list(row) for row in state])\n return\n # \u904d\u5386\u6240\u6709\u5217\n for col in range(n):\n # \u8ba1\u7b97\u8be5\u683c\u5b50\u5bf9\u5e94\u7684\u4e3b\u5bf9\u89d2\u7ebf\u548c\u526f\u5bf9\u89d2\u7ebf\n diag1 = row - col + n - 1\n diag2 = row + col\n # \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8be5\u683c\u5b50\u6240\u5728\u5217\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u526f\u5bf9\u89d2\u7ebf\u4e0a\u5b58\u5728\u7687\u540e\n if not cols[col] and not diags1[diag1] and not diags2[diag2]:\n # \u5c1d\u8bd5\uff1a\u5c06\u7687\u540e\u653e\u7f6e\u5728\u8be5\u683c\u5b50\n state[row][col] = \"Q\"\n cols[col] = diags1[diag1] = diags2[diag2] = True\n # \u653e\u7f6e\u4e0b\u4e00\u884c\n backtrack(row + 1, n, state, res, cols, diags1, diags2)\n # \u56de\u9000\uff1a\u5c06\u8be5\u683c\u5b50\u6062\u590d\u4e3a\u7a7a\u4f4d\n state[row][col] = \"#\"\n cols[col] = diags1[diag1] = diags2[diag2] = False\n\ndef n_queens(n: int) -> list[list[list[str]]]:\n \"\"\"\u6c42\u89e3 N \u7687\u540e\"\"\"\n # \u521d\u59cb\u5316 n*n \u5927\u5c0f\u7684\u68cb\u76d8\uff0c\u5176\u4e2d 'Q' \u4ee3\u8868\u7687\u540e\uff0c'#' \u4ee3\u8868\u7a7a\u4f4d\n state = [[\"#\" for _ in range(n)] for _ in range(n)]\n cols = [False] * n # \u8bb0\u5f55\u5217\u662f\u5426\u6709\u7687\u540e\n diags1 = [False] * (2 * n - 1) # \u8bb0\u5f55\u4e3b\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n diags2 = [False] * (2 * n - 1) # \u8bb0\u5f55\u526f\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n res = []\n backtrack(0, n, state, res, cols, diags1, diags2)\n\n return res\n
n_queens.cpp/* \u56de\u6eaf\u7b97\u6cd5\uff1aN \u7687\u540e */\nvoid backtrack(int row, int n, vector<vector<string>> &state, vector<vector<vector<string>>> &res, vector<bool> &cols,\n vector<bool> &diags1, vector<bool> &diags2) {\n // \u5f53\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u65f6\uff0c\u8bb0\u5f55\u89e3\n if (row == n) {\n res.push_back(state);\n return;\n }\n // \u904d\u5386\u6240\u6709\u5217\n for (int col = 0; col < n; col++) {\n // \u8ba1\u7b97\u8be5\u683c\u5b50\u5bf9\u5e94\u7684\u4e3b\u5bf9\u89d2\u7ebf\u548c\u526f\u5bf9\u89d2\u7ebf\n int diag1 = row - col + n - 1;\n int diag2 = row + col;\n // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8be5\u683c\u5b50\u6240\u5728\u5217\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u526f\u5bf9\u89d2\u7ebf\u4e0a\u5b58\u5728\u7687\u540e\n if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {\n // \u5c1d\u8bd5\uff1a\u5c06\u7687\u540e\u653e\u7f6e\u5728\u8be5\u683c\u5b50\n state[row][col] = \"Q\";\n cols[col] = diags1[diag1] = diags2[diag2] = true;\n // \u653e\u7f6e\u4e0b\u4e00\u884c\n backtrack(row + 1, n, state, res, cols, diags1, diags2);\n // \u56de\u9000\uff1a\u5c06\u8be5\u683c\u5b50\u6062\u590d\u4e3a\u7a7a\u4f4d\n state[row][col] = \"#\";\n cols[col] = diags1[diag1] = diags2[diag2] = false;\n }\n }\n}\n\n/* \u6c42\u89e3 N \u7687\u540e */\nvector<vector<vector<string>>> nQueens(int n) {\n // \u521d\u59cb\u5316 n*n \u5927\u5c0f\u7684\u68cb\u76d8\uff0c\u5176\u4e2d 'Q' \u4ee3\u8868\u7687\u540e\uff0c'#' \u4ee3\u8868\u7a7a\u4f4d\n vector<vector<string>> state(n, vector<string>(n, \"#\"));\n vector<bool> cols(n, false); // \u8bb0\u5f55\u5217\u662f\u5426\u6709\u7687\u540e\n vector<bool> diags1(2 * n - 1, false); // \u8bb0\u5f55\u4e3b\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n vector<bool> diags2(2 * n - 1, false); // \u8bb0\u5f55\u526f\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n vector<vector<vector<string>>> res;\n\n backtrack(0, n, state, res, cols, diags1, diags2);\n\n return res;\n}\n
n_queens.java/* \u56de\u6eaf\u7b97\u6cd5\uff1aN \u7687\u540e */\nvoid backtrack(int row, int n, List<List<String>> state, List<List<List<String>>> res,\n boolean[] cols, boolean[] diags1, boolean[] diags2) {\n // \u5f53\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u65f6\uff0c\u8bb0\u5f55\u89e3\n if (row == n) {\n List<List<String>> copyState = new ArrayList<>();\n for (List<String> sRow : state) {\n copyState.add(new ArrayList<>(sRow));\n }\n res.add(copyState);\n return;\n }\n // \u904d\u5386\u6240\u6709\u5217\n for (int col = 0; col < n; col++) {\n // \u8ba1\u7b97\u8be5\u683c\u5b50\u5bf9\u5e94\u7684\u4e3b\u5bf9\u89d2\u7ebf\u548c\u526f\u5bf9\u89d2\u7ebf\n int diag1 = row - col + n - 1;\n int diag2 = row + col;\n // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8be5\u683c\u5b50\u6240\u5728\u5217\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u526f\u5bf9\u89d2\u7ebf\u4e0a\u5b58\u5728\u7687\u540e\n if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {\n // \u5c1d\u8bd5\uff1a\u5c06\u7687\u540e\u653e\u7f6e\u5728\u8be5\u683c\u5b50\n state.get(row).set(col, \"Q\");\n cols[col] = diags1[diag1] = diags2[diag2] = true;\n // \u653e\u7f6e\u4e0b\u4e00\u884c\n backtrack(row + 1, n, state, res, cols, diags1, diags2);\n // \u56de\u9000\uff1a\u5c06\u8be5\u683c\u5b50\u6062\u590d\u4e3a\u7a7a\u4f4d\n state.get(row).set(col, \"#\");\n cols[col] = diags1[diag1] = diags2[diag2] = false;\n }\n }\n}\n\n/* \u6c42\u89e3 N \u7687\u540e */\nList<List<List<String>>> nQueens(int n) {\n // \u521d\u59cb\u5316 n*n \u5927\u5c0f\u7684\u68cb\u76d8\uff0c\u5176\u4e2d 'Q' \u4ee3\u8868\u7687\u540e\uff0c'#' \u4ee3\u8868\u7a7a\u4f4d\n List<List<String>> state = new ArrayList<>();\n for (int i = 0; i < n; i++) {\n List<String> row = new ArrayList<>();\n for (int j = 0; j < n; j++) {\n row.add(\"#\");\n }\n state.add(row);\n }\n boolean[] cols = new boolean[n]; // \u8bb0\u5f55\u5217\u662f\u5426\u6709\u7687\u540e\n boolean[] diags1 = new boolean[2 * n - 1]; // \u8bb0\u5f55\u4e3b\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n boolean[] diags2 = new boolean[2 * n - 1]; // \u8bb0\u5f55\u526f\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n List<List<List<String>>> res = new ArrayList<>();\n\n backtrack(0, n, state, res, cols, diags1, diags2);\n\n return res;\n}\n
n_queens.cs/* \u56de\u6eaf\u7b97\u6cd5\uff1aN \u7687\u540e */\nvoid Backtrack(int row, int n, List<List<string>> state, List<List<List<string>>> res,\n bool[] cols, bool[] diags1, bool[] diags2) {\n // \u5f53\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u65f6\uff0c\u8bb0\u5f55\u89e3\n if (row == n) {\n List<List<string>> copyState = [];\n foreach (List<string> sRow in state) {\n copyState.Add(new List<string>(sRow));\n }\n res.Add(copyState);\n return;\n }\n // \u904d\u5386\u6240\u6709\u5217\n for (int col = 0; col < n; col++) {\n // \u8ba1\u7b97\u8be5\u683c\u5b50\u5bf9\u5e94\u7684\u4e3b\u5bf9\u89d2\u7ebf\u548c\u526f\u5bf9\u89d2\u7ebf\n int diag1 = row - col + n - 1;\n int diag2 = row + col;\n // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8be5\u683c\u5b50\u6240\u5728\u5217\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u526f\u5bf9\u89d2\u7ebf\u4e0a\u5b58\u5728\u7687\u540e\n if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {\n // \u5c1d\u8bd5\uff1a\u5c06\u7687\u540e\u653e\u7f6e\u5728\u8be5\u683c\u5b50\n state[row][col] = \"Q\";\n cols[col] = diags1[diag1] = diags2[diag2] = true;\n // \u653e\u7f6e\u4e0b\u4e00\u884c\n Backtrack(row + 1, n, state, res, cols, diags1, diags2);\n // \u56de\u9000\uff1a\u5c06\u8be5\u683c\u5b50\u6062\u590d\u4e3a\u7a7a\u4f4d\n state[row][col] = \"#\";\n cols[col] = diags1[diag1] = diags2[diag2] = false;\n }\n }\n}\n\n/* \u6c42\u89e3 N \u7687\u540e */\nList<List<List<string>>> NQueens(int n) {\n // \u521d\u59cb\u5316 n*n \u5927\u5c0f\u7684\u68cb\u76d8\uff0c\u5176\u4e2d 'Q' \u4ee3\u8868\u7687\u540e\uff0c'#' \u4ee3\u8868\u7a7a\u4f4d\n List<List<string>> state = [];\n for (int i = 0; i < n; i++) {\n List<string> row = [];\n for (int j = 0; j < n; j++) {\n row.Add(\"#\");\n }\n state.Add(row);\n }\n bool[] cols = new bool[n]; // \u8bb0\u5f55\u5217\u662f\u5426\u6709\u7687\u540e\n bool[] diags1 = new bool[2 * n - 1]; // \u8bb0\u5f55\u4e3b\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n bool[] diags2 = new bool[2 * n - 1]; // \u8bb0\u5f55\u526f\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n List<List<List<string>>> res = [];\n\n Backtrack(0, n, state, res, cols, diags1, diags2);\n\n return res;\n}\n
n_queens.go/* \u56de\u6eaf\u7b97\u6cd5\uff1aN \u7687\u540e */\nfunc backtrack(row, n int, state *[][]string, res *[][][]string, cols, diags1, diags2 *[]bool) {\n // \u5f53\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u65f6\uff0c\u8bb0\u5f55\u89e3\n if row == n {\n newState := make([][]string, len(*state))\n for i, _ := range newState {\n newState[i] = make([]string, len((*state)[0]))\n copy(newState[i], (*state)[i])\n\n }\n *res = append(*res, newState)\n }\n // \u904d\u5386\u6240\u6709\u5217\n for col := 0; col < n; col++ {\n // \u8ba1\u7b97\u8be5\u683c\u5b50\u5bf9\u5e94\u7684\u4e3b\u5bf9\u89d2\u7ebf\u548c\u526f\u5bf9\u89d2\u7ebf\n diag1 := row - col + n - 1\n diag2 := row + col\n // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8be5\u683c\u5b50\u6240\u5728\u5217\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u526f\u5bf9\u89d2\u7ebf\u4e0a\u5b58\u5728\u7687\u540e\n if !(*cols)[col] && !(*diags1)[diag1] && !(*diags2)[diag2] {\n // \u5c1d\u8bd5\uff1a\u5c06\u7687\u540e\u653e\u7f6e\u5728\u8be5\u683c\u5b50\n (*state)[row][col] = \"Q\"\n (*cols)[col], (*diags1)[diag1], (*diags2)[diag2] = true, true, true\n // \u653e\u7f6e\u4e0b\u4e00\u884c\n backtrack(row+1, n, state, res, cols, diags1, diags2)\n // \u56de\u9000\uff1a\u5c06\u8be5\u683c\u5b50\u6062\u590d\u4e3a\u7a7a\u4f4d\n (*state)[row][col] = \"#\"\n (*cols)[col], (*diags1)[diag1], (*diags2)[diag2] = false, false, false\n }\n }\n}\n\n/* \u56de\u6eaf\u7b97\u6cd5\uff1aN \u7687\u540e */\nfunc backtrack(row, n int, state *[][]string, res *[][][]string, cols, diags1, diags2 *[]bool) {\n // \u5f53\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u65f6\uff0c\u8bb0\u5f55\u89e3\n if row == n {\n newState := make([][]string, len(*state))\n for i, _ := range newState {\n newState[i] = make([]string, len((*state)[0]))\n copy(newState[i], (*state)[i])\n\n }\n *res = append(*res, newState)\n }\n // \u904d\u5386\u6240\u6709\u5217\n for col := 0; col < n; col++ {\n // \u8ba1\u7b97\u8be5\u683c\u5b50\u5bf9\u5e94\u7684\u4e3b\u5bf9\u89d2\u7ebf\u548c\u526f\u5bf9\u89d2\u7ebf\n diag1 := row - col + n - 1\n diag2 := row + col\n // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8be5\u683c\u5b50\u6240\u5728\u5217\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u526f\u5bf9\u89d2\u7ebf\u4e0a\u5b58\u5728\u7687\u540e\n if !(*cols)[col] && !(*diags1)[diag1] && !(*diags2)[diag2] {\n // \u5c1d\u8bd5\uff1a\u5c06\u7687\u540e\u653e\u7f6e\u5728\u8be5\u683c\u5b50\n (*state)[row][col] = \"Q\"\n (*cols)[col], (*diags1)[diag1], (*diags2)[diag2] = true, true, true\n // \u653e\u7f6e\u4e0b\u4e00\u884c\n backtrack(row+1, n, state, res, cols, diags1, diags2)\n // \u56de\u9000\uff1a\u5c06\u8be5\u683c\u5b50\u6062\u590d\u4e3a\u7a7a\u4f4d\n (*state)[row][col] = \"#\"\n (*cols)[col], (*diags1)[diag1], (*diags2)[diag2] = false, false, false\n }\n }\n}\n\nfunc nQueens(n int) [][][]string {\n // \u521d\u59cb\u5316 n*n \u5927\u5c0f\u7684\u68cb\u76d8\uff0c\u5176\u4e2d 'Q' \u4ee3\u8868\u7687\u540e\uff0c'#' \u4ee3\u8868\u7a7a\u4f4d\n state := make([][]string, n)\n for i := 0; i < n; i++ {\n row := make([]string, n)\n for i := 0; i < n; i++ {\n row[i] = \"#\"\n }\n state[i] = row\n }\n // \u8bb0\u5f55\u5217\u662f\u5426\u6709\u7687\u540e\n cols := make([]bool, n)\n diags1 := make([]bool, 2*n-1)\n diags2 := make([]bool, 2*n-1)\n res := make([][][]string, 0)\n backtrack(0, n, &state, &res, &cols, &diags1, &diags2)\n return res\n}\n
n_queens.swift/* \u56de\u6eaf\u7b97\u6cd5\uff1aN \u7687\u540e */\nfunc backtrack(row: Int, n: Int, state: inout [[String]], res: inout [[[String]]], cols: inout [Bool], diags1: inout [Bool], diags2: inout [Bool]) {\n // \u5f53\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u65f6\uff0c\u8bb0\u5f55\u89e3\n if row == n {\n res.append(state)\n return\n }\n // \u904d\u5386\u6240\u6709\u5217\n for col in 0 ..< n {\n // \u8ba1\u7b97\u8be5\u683c\u5b50\u5bf9\u5e94\u7684\u4e3b\u5bf9\u89d2\u7ebf\u548c\u526f\u5bf9\u89d2\u7ebf\n let diag1 = row - col + n - 1\n let diag2 = row + col\n // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8be5\u683c\u5b50\u6240\u5728\u5217\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u526f\u5bf9\u89d2\u7ebf\u4e0a\u5b58\u5728\u7687\u540e\n if !cols[col] && !diags1[diag1] && !diags2[diag2] {\n // \u5c1d\u8bd5\uff1a\u5c06\u7687\u540e\u653e\u7f6e\u5728\u8be5\u683c\u5b50\n state[row][col] = \"Q\"\n cols[col] = true\n diags1[diag1] = true\n diags2[diag2] = true\n // \u653e\u7f6e\u4e0b\u4e00\u884c\n backtrack(row: row + 1, n: n, state: &state, res: &res, cols: &cols, diags1: &diags1, diags2: &diags2)\n // \u56de\u9000\uff1a\u5c06\u8be5\u683c\u5b50\u6062\u590d\u4e3a\u7a7a\u4f4d\n state[row][col] = \"#\"\n cols[col] = false\n diags1[diag1] = false\n diags2[diag2] = false\n }\n }\n}\n\n/* \u6c42\u89e3 N \u7687\u540e */\nfunc nQueens(n: Int) -> [[[String]]] {\n // \u521d\u59cb\u5316 n*n \u5927\u5c0f\u7684\u68cb\u76d8\uff0c\u5176\u4e2d 'Q' \u4ee3\u8868\u7687\u540e\uff0c'#' \u4ee3\u8868\u7a7a\u4f4d\n var state = Array(repeating: Array(repeating: \"#\", count: n), count: n)\n var cols = Array(repeating: false, count: n) // \u8bb0\u5f55\u5217\u662f\u5426\u6709\u7687\u540e\n var diags1 = Array(repeating: false, count: 2 * n - 1) // \u8bb0\u5f55\u4e3b\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n var diags2 = Array(repeating: false, count: 2 * n - 1) // \u8bb0\u5f55\u526f\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n var res: [[[String]]] = []\n\n backtrack(row: 0, n: n, state: &state, res: &res, cols: &cols, diags1: &diags1, diags2: &diags2)\n\n return res\n}\n
n_queens.js/* \u56de\u6eaf\u7b97\u6cd5\uff1aN \u7687\u540e */\nfunction backtrack(row, n, state, res, cols, diags1, diags2) {\n // \u5f53\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u65f6\uff0c\u8bb0\u5f55\u89e3\n if (row === n) {\n res.push(state.map((row) => row.slice()));\n return;\n }\n // \u904d\u5386\u6240\u6709\u5217\n for (let col = 0; col < n; col++) {\n // \u8ba1\u7b97\u8be5\u683c\u5b50\u5bf9\u5e94\u7684\u4e3b\u5bf9\u89d2\u7ebf\u548c\u526f\u5bf9\u89d2\u7ebf\n const diag1 = row - col + n - 1;\n const diag2 = row + col;\n // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8be5\u683c\u5b50\u6240\u5728\u5217\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u526f\u5bf9\u89d2\u7ebf\u4e0a\u5b58\u5728\u7687\u540e\n if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {\n // \u5c1d\u8bd5\uff1a\u5c06\u7687\u540e\u653e\u7f6e\u5728\u8be5\u683c\u5b50\n state[row][col] = 'Q';\n cols[col] = diags1[diag1] = diags2[diag2] = true;\n // \u653e\u7f6e\u4e0b\u4e00\u884c\n backtrack(row + 1, n, state, res, cols, diags1, diags2);\n // \u56de\u9000\uff1a\u5c06\u8be5\u683c\u5b50\u6062\u590d\u4e3a\u7a7a\u4f4d\n state[row][col] = '#';\n cols[col] = diags1[diag1] = diags2[diag2] = false;\n }\n }\n}\n\n/* \u6c42\u89e3 N \u7687\u540e */\nfunction nQueens(n) {\n // \u521d\u59cb\u5316 n*n \u5927\u5c0f\u7684\u68cb\u76d8\uff0c\u5176\u4e2d 'Q' \u4ee3\u8868\u7687\u540e\uff0c'#' \u4ee3\u8868\u7a7a\u4f4d\n const state = Array.from({ length: n }, () => Array(n).fill('#'));\n const cols = Array(n).fill(false); // \u8bb0\u5f55\u5217\u662f\u5426\u6709\u7687\u540e\n const diags1 = Array(2 * n - 1).fill(false); // \u8bb0\u5f55\u4e3b\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n const diags2 = Array(2 * n - 1).fill(false); // \u8bb0\u5f55\u526f\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n const res = [];\n\n backtrack(0, n, state, res, cols, diags1, diags2);\n return res;\n}\n
n_queens.ts/* \u56de\u6eaf\u7b97\u6cd5\uff1aN \u7687\u540e */\nfunction backtrack(\n row: number,\n n: number,\n state: string[][],\n res: string[][][],\n cols: boolean[],\n diags1: boolean[],\n diags2: boolean[]\n): void {\n // \u5f53\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u65f6\uff0c\u8bb0\u5f55\u89e3\n if (row === n) {\n res.push(state.map((row) => row.slice()));\n return;\n }\n // \u904d\u5386\u6240\u6709\u5217\n for (let col = 0; col < n; col++) {\n // \u8ba1\u7b97\u8be5\u683c\u5b50\u5bf9\u5e94\u7684\u4e3b\u5bf9\u89d2\u7ebf\u548c\u526f\u5bf9\u89d2\u7ebf\n const diag1 = row - col + n - 1;\n const diag2 = row + col;\n // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8be5\u683c\u5b50\u6240\u5728\u5217\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u526f\u5bf9\u89d2\u7ebf\u4e0a\u5b58\u5728\u7687\u540e\n if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {\n // \u5c1d\u8bd5\uff1a\u5c06\u7687\u540e\u653e\u7f6e\u5728\u8be5\u683c\u5b50\n state[row][col] = 'Q';\n cols[col] = diags1[diag1] = diags2[diag2] = true;\n // \u653e\u7f6e\u4e0b\u4e00\u884c\n backtrack(row + 1, n, state, res, cols, diags1, diags2);\n // \u56de\u9000\uff1a\u5c06\u8be5\u683c\u5b50\u6062\u590d\u4e3a\u7a7a\u4f4d\n state[row][col] = '#';\n cols[col] = diags1[diag1] = diags2[diag2] = false;\n }\n }\n}\n\n/* \u6c42\u89e3 N \u7687\u540e */\nfunction nQueens(n: number): string[][][] {\n // \u521d\u59cb\u5316 n*n \u5927\u5c0f\u7684\u68cb\u76d8\uff0c\u5176\u4e2d 'Q' \u4ee3\u8868\u7687\u540e\uff0c'#' \u4ee3\u8868\u7a7a\u4f4d\n const state = Array.from({ length: n }, () => Array(n).fill('#'));\n const cols = Array(n).fill(false); // \u8bb0\u5f55\u5217\u662f\u5426\u6709\u7687\u540e\n const diags1 = Array(2 * n - 1).fill(false); // \u8bb0\u5f55\u4e3b\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n const diags2 = Array(2 * n - 1).fill(false); // \u8bb0\u5f55\u526f\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n const res: string[][][] = [];\n\n backtrack(0, n, state, res, cols, diags1, diags2);\n return res;\n}\n
n_queens.dart/* \u56de\u6eaf\u7b97\u6cd5\uff1aN \u7687\u540e */\nvoid backtrack(\n int row,\n int n,\n List<List<String>> state,\n List<List<List<String>>> res,\n List<bool> cols,\n List<bool> diags1,\n List<bool> diags2,\n) {\n // \u5f53\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u65f6\uff0c\u8bb0\u5f55\u89e3\n if (row == n) {\n List<List<String>> copyState = [];\n for (List<String> sRow in state) {\n copyState.add(List.from(sRow));\n }\n res.add(copyState);\n return;\n }\n // \u904d\u5386\u6240\u6709\u5217\n for (int col = 0; col < n; col++) {\n // \u8ba1\u7b97\u8be5\u683c\u5b50\u5bf9\u5e94\u7684\u4e3b\u5bf9\u89d2\u7ebf\u548c\u526f\u5bf9\u89d2\u7ebf\n int diag1 = row - col + n - 1;\n int diag2 = row + col;\n // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8be5\u683c\u5b50\u6240\u5728\u5217\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u526f\u5bf9\u89d2\u7ebf\u4e0a\u5b58\u5728\u7687\u540e\n if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {\n // \u5c1d\u8bd5\uff1a\u5c06\u7687\u540e\u653e\u7f6e\u5728\u8be5\u683c\u5b50\n state[row][col] = \"Q\";\n cols[col] = true;\n diags1[diag1] = true;\n diags2[diag2] = true;\n // \u653e\u7f6e\u4e0b\u4e00\u884c\n backtrack(row + 1, n, state, res, cols, diags1, diags2);\n // \u56de\u9000\uff1a\u5c06\u8be5\u683c\u5b50\u6062\u590d\u4e3a\u7a7a\u4f4d\n state[row][col] = \"#\";\n cols[col] = false;\n diags1[diag1] = false;\n diags2[diag2] = false;\n }\n }\n}\n\n/* \u6c42\u89e3 N \u7687\u540e */\nList<List<List<String>>> nQueens(int n) {\n // \u521d\u59cb\u5316 n*n \u5927\u5c0f\u7684\u68cb\u76d8\uff0c\u5176\u4e2d 'Q' \u4ee3\u8868\u7687\u540e\uff0c'#' \u4ee3\u8868\u7a7a\u4f4d\n List<List<String>> state = List.generate(n, (index) => List.filled(n, \"#\"));\n List<bool> cols = List.filled(n, false); // \u8bb0\u5f55\u5217\u662f\u5426\u6709\u7687\u540e\n List<bool> diags1 = List.filled(2 * n - 1, false); // \u8bb0\u5f55\u4e3b\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n List<bool> diags2 = List.filled(2 * n - 1, false); // \u8bb0\u5f55\u526f\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n List<List<List<String>>> res = [];\n\n backtrack(0, n, state, res, cols, diags1, diags2);\n\n return res;\n}\n
n_queens.rs/* \u56de\u6eaf\u7b97\u6cd5\uff1aN \u7687\u540e */\nfn backtrack(row: usize, n: usize, state: &mut Vec<Vec<String>>, res: &mut Vec<Vec<Vec<String>>>,\n cols: &mut [bool], diags1: &mut [bool], diags2: &mut [bool]) {\n // \u5f53\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u65f6\uff0c\u8bb0\u5f55\u89e3\n if row == n {\n let mut copy_state: Vec<Vec<String>> = Vec::new();\n for s_row in state.clone() {\n copy_state.push(s_row);\n }\n res.push(copy_state);\n return;\n }\n // \u904d\u5386\u6240\u6709\u5217\n for col in 0..n {\n // \u8ba1\u7b97\u8be5\u683c\u5b50\u5bf9\u5e94\u7684\u4e3b\u5bf9\u89d2\u7ebf\u548c\u526f\u5bf9\u89d2\u7ebf\n let diag1 = row + n - 1 - col;\n let diag2 = row + col;\n // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8be5\u683c\u5b50\u6240\u5728\u5217\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u526f\u5bf9\u89d2\u7ebf\u4e0a\u5b58\u5728\u7687\u540e\n if !cols[col] && !diags1[diag1] && !diags2[diag2] {\n // \u5c1d\u8bd5\uff1a\u5c06\u7687\u540e\u653e\u7f6e\u5728\u8be5\u683c\u5b50\n state.get_mut(row).unwrap()[col] = \"Q\".into();\n (cols[col], diags1[diag1], diags2[diag2]) = (true, true, true);\n // \u653e\u7f6e\u4e0b\u4e00\u884c\n backtrack(row + 1, n, state, res, cols, diags1, diags2);\n // \u56de\u9000\uff1a\u5c06\u8be5\u683c\u5b50\u6062\u590d\u4e3a\u7a7a\u4f4d\n state.get_mut(row).unwrap()[col] = \"#\".into();\n (cols[col], diags1[diag1], diags2[diag2]) = (false, false, false);\n }\n }\n}\n\n/* \u6c42\u89e3 N \u7687\u540e */\nfn n_queens(n: usize) -> Vec<Vec<Vec<String>>> {\n // \u521d\u59cb\u5316 n*n \u5927\u5c0f\u7684\u68cb\u76d8\uff0c\u5176\u4e2d 'Q' \u4ee3\u8868\u7687\u540e\uff0c'#' \u4ee3\u8868\u7a7a\u4f4d\n let mut state: Vec<Vec<String>> = Vec::new();\n for _ in 0..n {\n let mut row: Vec<String> = Vec::new();\n for _ in 0..n {\n row.push(\"#\".into());\n }\n state.push(row);\n }\n let mut cols = vec![false; n]; // \u8bb0\u5f55\u5217\u662f\u5426\u6709\u7687\u540e\n let mut diags1 = vec![false; 2 * n - 1]; // \u8bb0\u5f55\u4e3b\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n let mut diags2 = vec![false; 2 * n - 1]; // \u8bb0\u5f55\u526f\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n let mut res: Vec<Vec<Vec<String>>> = Vec::new();\n\n backtrack(0, n, &mut state, &mut res, &mut cols, &mut diags1, &mut diags2);\n\n res\n}\n
n_queens.c/* \u56de\u6eaf\u7b97\u6cd5\uff1aN \u7687\u540e */\nvoid backtrack(int row, int n, char state[MAX_SIZE][MAX_SIZE], char ***res, int *resSize, bool cols[MAX_SIZE],\n bool diags1[2 * MAX_SIZE - 1], bool diags2[2 * MAX_SIZE - 1]) {\n // \u5f53\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u65f6\uff0c\u8bb0\u5f55\u89e3\n if (row == n) {\n res[*resSize] = (char **)malloc(sizeof(char *) * n);\n for (int i = 0; i < n; ++i) {\n res[*resSize][i] = (char *)malloc(sizeof(char) * (n + 1));\n strcpy(res[*resSize][i], state[i]);\n }\n (*resSize)++;\n return;\n }\n // \u904d\u5386\u6240\u6709\u5217\n for (int col = 0; col < n; col++) {\n // \u8ba1\u7b97\u8be5\u683c\u5b50\u5bf9\u5e94\u7684\u4e3b\u5bf9\u89d2\u7ebf\u548c\u526f\u5bf9\u89d2\u7ebf\n int diag1 = row - col + n - 1;\n int diag2 = row + col;\n // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8be5\u683c\u5b50\u6240\u5728\u5217\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u526f\u5bf9\u89d2\u7ebf\u4e0a\u5b58\u5728\u7687\u540e\n if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {\n // \u5c1d\u8bd5\uff1a\u5c06\u7687\u540e\u653e\u7f6e\u5728\u8be5\u683c\u5b50\n state[row][col] = 'Q';\n cols[col] = diags1[diag1] = diags2[diag2] = true;\n // \u653e\u7f6e\u4e0b\u4e00\u884c\n backtrack(row + 1, n, state, res, resSize, cols, diags1, diags2);\n // \u56de\u9000\uff1a\u5c06\u8be5\u683c\u5b50\u6062\u590d\u4e3a\u7a7a\u4f4d\n state[row][col] = '#';\n cols[col] = diags1[diag1] = diags2[diag2] = false;\n }\n }\n}\n\n/* \u6c42\u89e3 N \u7687\u540e */\nchar ***nQueens(int n, int *returnSize) {\n char state[MAX_SIZE][MAX_SIZE];\n // \u521d\u59cb\u5316 n*n \u5927\u5c0f\u7684\u68cb\u76d8\uff0c\u5176\u4e2d 'Q' \u4ee3\u8868\u7687\u540e\uff0c'#' \u4ee3\u8868\u7a7a\u4f4d\n for (int i = 0; i < n; ++i) {\n for (int j = 0; j < n; ++j) {\n state[i][j] = '#';\n }\n state[i][n] = '\\0';\n }\n bool cols[MAX_SIZE] = {false}; // \u8bb0\u5f55\u5217\u662f\u5426\u6709\u7687\u540e\n bool diags1[2 * MAX_SIZE - 1] = {false}; // \u8bb0\u5f55\u4e3b\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n bool diags2[2 * MAX_SIZE - 1] = {false}; // \u8bb0\u5f55\u526f\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\n\n char ***res = (char ***)malloc(sizeof(char **) * MAX_SIZE);\n *returnSize = 0;\n backtrack(0, n, state, res, returnSize, cols, diags1, diags2);\n return res;\n}\n
n_queens.zig[class]{}-[func]{backtrack}\n\n[class]{}-[func]{nQueens}\n
\u9010\u884c\u653e\u7f6e \\(n\\) \u6b21\uff0c\u8003\u8651\u5217\u7ea6\u675f\uff0c\u5219\u4ece\u7b2c\u4e00\u884c\u5230\u6700\u540e\u4e00\u884c\u5206\u522b\u6709 \\(n\\)\u3001\\(n-1\\)\u3001\\(\\dots\\)\u3001\\(2\\)\u3001\\(1\\) \u4e2a\u9009\u62e9\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n!)\\) \u3002\u5b9e\u9645\u4e0a\uff0c\u6839\u636e\u5bf9\u89d2\u7ebf\u7ea6\u675f\u7684\u526a\u679d\u4e5f\u80fd\u591f\u5927\u5e45\u7f29\u5c0f\u641c\u7d22\u7a7a\u95f4\uff0c\u56e0\u800c\u641c\u7d22\u6548\u7387\u5f80\u5f80\u4f18\u4e8e\u4ee5\u4e0a\u65f6\u95f4\u590d\u6742\u5ea6\u3002
\u6570\u7ec4 state
\u4f7f\u7528 \\(O(n^2)\\) \u7a7a\u95f4\uff0c\u6570\u7ec4 cols
\u3001diags1
\u548c diags2
\u7686\u4f7f\u7528 \\(O(n)\\) \u7a7a\u95f4\u3002\u6700\u5927\u9012\u5f52\u6df1\u5ea6\u4e3a \\(n\\) \uff0c\u4f7f\u7528 \\(O(n)\\) \u6808\u5e27\u7a7a\u95f4\u3002\u56e0\u6b64\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\) \u3002
\u5168\u6392\u5217\u95ee\u9898\u662f\u56de\u6eaf\u7b97\u6cd5\u7684\u4e00\u4e2a\u5178\u578b\u5e94\u7528\u3002\u5b83\u7684\u5b9a\u4e49\u662f\u5728\u7ed9\u5b9a\u4e00\u4e2a\u96c6\u5408\uff08\u5982\u4e00\u4e2a\u6570\u7ec4\u6216\u5b57\u7b26\u4e32\uff09\u7684\u60c5\u51b5\u4e0b\uff0c\u627e\u51fa\u5176\u4e2d\u5143\u7d20\u7684\u6240\u6709\u53ef\u80fd\u7684\u6392\u5217\u3002
\u8868 13-2 \u5217\u4e3e\u4e86\u51e0\u4e2a\u793a\u4f8b\u6570\u636e\uff0c\u5305\u62ec\u8f93\u5165\u6570\u7ec4\u548c\u5bf9\u5e94\u7684\u6240\u6709\u6392\u5217\u3002
\u8868 13-2 \u00a0 \u5168\u6392\u5217\u793a\u4f8b
\u8f93\u5165\u6570\u7ec4 \u6240\u6709\u6392\u5217 \\([1]\\) \\([1]\\) \\([1, 2]\\) \\([1, 2], [2, 1]\\) \\([1, 2, 3]\\) \\([1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]\\)"},{"location":"chapter_backtracking/permutations_problem/#1321","title":"13.2.1 \u00a0 \u65e0\u76f8\u7b49\u5143\u7d20\u7684\u60c5\u51b5","text":"Question
\u8f93\u5165\u4e00\u4e2a\u6574\u6570\u6570\u7ec4\uff0c\u5176\u4e2d\u4e0d\u5305\u542b\u91cd\u590d\u5143\u7d20\uff0c\u8fd4\u56de\u6240\u6709\u53ef\u80fd\u7684\u6392\u5217\u3002
\u4ece\u56de\u6eaf\u7b97\u6cd5\u7684\u89d2\u5ea6\u770b\uff0c\u6211\u4eec\u53ef\u4ee5\u628a\u751f\u6210\u6392\u5217\u7684\u8fc7\u7a0b\u60f3\u8c61\u6210\u4e00\u7cfb\u5217\u9009\u62e9\u7684\u7ed3\u679c\u3002\u5047\u8bbe\u8f93\u5165\u6570\u7ec4\u4e3a \\([1, 2, 3]\\) \uff0c\u5982\u679c\u6211\u4eec\u5148\u9009\u62e9 \\(1\\) \uff0c\u518d\u9009\u62e9 \\(3\\) \uff0c\u6700\u540e\u9009\u62e9 \\(2\\) \uff0c\u5219\u83b7\u5f97\u6392\u5217 \\([1, 3, 2]\\) \u3002\u56de\u9000\u8868\u793a\u64a4\u9500\u4e00\u4e2a\u9009\u62e9\uff0c\u4e4b\u540e\u7ee7\u7eed\u5c1d\u8bd5\u5176\u4ed6\u9009\u62e9\u3002
\u4ece\u56de\u6eaf\u4ee3\u7801\u7684\u89d2\u5ea6\u770b\uff0c\u5019\u9009\u96c6\u5408 choices
\u662f\u8f93\u5165\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\uff0c\u72b6\u6001 state
\u662f\u76f4\u81f3\u76ee\u524d\u5df2\u88ab\u9009\u62e9\u7684\u5143\u7d20\u3002\u8bf7\u6ce8\u610f\uff0c\u6bcf\u4e2a\u5143\u7d20\u53ea\u5141\u8bb8\u88ab\u9009\u62e9\u4e00\u6b21\uff0c\u56e0\u6b64 state
\u4e2d\u7684\u6240\u6709\u5143\u7d20\u90fd\u5e94\u8be5\u662f\u552f\u4e00\u7684\u3002
\u5982\u56fe 13-5 \u6240\u793a\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u641c\u7d22\u8fc7\u7a0b\u5c55\u5f00\u6210\u4e00\u68f5\u9012\u5f52\u6811\uff0c\u6811\u4e2d\u7684\u6bcf\u4e2a\u8282\u70b9\u4ee3\u8868\u5f53\u524d\u72b6\u6001 state
\u3002\u4ece\u6839\u8282\u70b9\u5f00\u59cb\uff0c\u7ecf\u8fc7\u4e09\u8f6e\u9009\u62e9\u540e\u5230\u8fbe\u53f6\u8282\u70b9\uff0c\u6bcf\u4e2a\u53f6\u8282\u70b9\u90fd\u5bf9\u5e94\u4e00\u4e2a\u6392\u5217\u3002
\u56fe 13-5 \u00a0 \u5168\u6392\u5217\u7684\u9012\u5f52\u6811
"},{"location":"chapter_backtracking/permutations_problem/#1","title":"1. \u00a0 \u91cd\u590d\u9009\u62e9\u526a\u679d","text":"\u4e3a\u4e86\u5b9e\u73b0\u6bcf\u4e2a\u5143\u7d20\u53ea\u88ab\u9009\u62e9\u4e00\u6b21\uff0c\u6211\u4eec\u8003\u8651\u5f15\u5165\u4e00\u4e2a\u5e03\u5c14\u578b\u6570\u7ec4 selected
\uff0c\u5176\u4e2d selected[i]
\u8868\u793a choices[i]
\u662f\u5426\u5df2\u88ab\u9009\u62e9\uff0c\u5e76\u57fa\u4e8e\u5b83\u5b9e\u73b0\u4ee5\u4e0b\u526a\u679d\u64cd\u4f5c\u3002
choice[i]
\u540e\uff0c\u6211\u4eec\u5c31\u5c06 selected[i]
\u8d4b\u503c\u4e3a \\(\\text{True}\\) \uff0c\u4ee3\u8868\u5b83\u5df2\u88ab\u9009\u62e9\u3002choices
\u65f6\uff0c\u8df3\u8fc7\u6240\u6709\u5df2\u88ab\u9009\u62e9\u7684\u8282\u70b9\uff0c\u5373\u526a\u679d\u3002\u5982\u56fe 13-6 \u6240\u793a\uff0c\u5047\u8bbe\u6211\u4eec\u7b2c\u4e00\u8f6e\u9009\u62e9 1 \uff0c\u7b2c\u4e8c\u8f6e\u9009\u62e9 3 \uff0c\u7b2c\u4e09\u8f6e\u9009\u62e9 2 \uff0c\u5219\u9700\u8981\u5728\u7b2c\u4e8c\u8f6e\u526a\u6389\u5143\u7d20 1 \u7684\u5206\u652f\uff0c\u5728\u7b2c\u4e09\u8f6e\u526a\u6389\u5143\u7d20 1 \u548c\u5143\u7d20 3 \u7684\u5206\u652f\u3002
\u56fe 13-6 \u00a0 \u5168\u6392\u5217\u526a\u679d\u793a\u4f8b
\u89c2\u5bdf\u56fe 13-6 \u53d1\u73b0\uff0c\u8be5\u526a\u679d\u64cd\u4f5c\u5c06\u641c\u7d22\u7a7a\u95f4\u5927\u5c0f\u4ece \\(O(n^n)\\) \u51cf\u5c0f\u81f3 \\(O(n!)\\) \u3002
"},{"location":"chapter_backtracking/permutations_problem/#2","title":"2. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"\u60f3\u6e05\u695a\u4ee5\u4e0a\u4fe1\u606f\u4e4b\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5728\u6846\u67b6\u4ee3\u7801\u4e2d\u505a\u201c\u5b8c\u5f62\u586b\u7a7a\u201d\u4e86\u3002\u4e3a\u4e86\u7f29\u77ed\u6574\u4f53\u4ee3\u7801\uff0c\u6211\u4eec\u4e0d\u5355\u72ec\u5b9e\u73b0\u6846\u67b6\u4ee3\u7801\u4e2d\u7684\u5404\u4e2a\u51fd\u6570\uff0c\u800c\u662f\u5c06\u5b83\u4eec\u5c55\u5f00\u5728 backtrack()
\u51fd\u6570\u4e2d\uff1a
def backtrack(\n state: list[int], choices: list[int], selected: list[bool], res: list[list[int]]\n):\n \"\"\"\u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 I\"\"\"\n # \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n if len(state) == len(choices):\n res.append(list(state))\n return\n # \u904d\u5386\u6240\u6709\u9009\u62e9\n for i, choice in enumerate(choices):\n # \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20\n if not selected[i]:\n # \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n selected[i] = True\n state.append(choice)\n # \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state, choices, selected, res)\n # \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n selected[i] = False\n state.pop()\n\ndef permutations_i(nums: list[int]) -> list[list[int]]:\n \"\"\"\u5168\u6392\u5217 I\"\"\"\n res = []\n backtrack(state=[], choices=nums, selected=[False] * len(nums), res=res)\n return res\n
permutations_i.cpp/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 I */\nvoid backtrack(vector<int> &state, const vector<int> &choices, vector<bool> &selected, vector<vector<int>> &res) {\n // \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n if (state.size() == choices.size()) {\n res.push_back(state);\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for (int i = 0; i < choices.size(); i++) {\n int choice = choices[i];\n // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20\n if (!selected[i]) {\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n selected[i] = true;\n state.push_back(choice);\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state, choices, selected, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n selected[i] = false;\n state.pop_back();\n }\n }\n}\n\n/* \u5168\u6392\u5217 I */\nvector<vector<int>> permutationsI(vector<int> nums) {\n vector<int> state;\n vector<bool> selected(nums.size(), false);\n vector<vector<int>> res;\n backtrack(state, nums, selected, res);\n return res;\n}\n
permutations_i.java/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 I */\nvoid backtrack(List<Integer> state, int[] choices, boolean[] selected, List<List<Integer>> res) {\n // \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n if (state.size() == choices.length) {\n res.add(new ArrayList<Integer>(state));\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for (int i = 0; i < choices.length; i++) {\n int choice = choices[i];\n // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20\n if (!selected[i]) {\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n selected[i] = true;\n state.add(choice);\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state, choices, selected, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n selected[i] = false;\n state.remove(state.size() - 1);\n }\n }\n}\n\n/* \u5168\u6392\u5217 I */\nList<List<Integer>> permutationsI(int[] nums) {\n List<List<Integer>> res = new ArrayList<List<Integer>>();\n backtrack(new ArrayList<Integer>(), nums, new boolean[nums.length], res);\n return res;\n}\n
permutations_i.cs/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 I */\nvoid Backtrack(List<int> state, int[] choices, bool[] selected, List<List<int>> res) {\n // \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n if (state.Count == choices.Length) {\n res.Add(new List<int>(state));\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for (int i = 0; i < choices.Length; i++) {\n int choice = choices[i];\n // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20\n if (!selected[i]) {\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n selected[i] = true;\n state.Add(choice);\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n Backtrack(state, choices, selected, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n selected[i] = false;\n state.RemoveAt(state.Count - 1);\n }\n }\n}\n\n/* \u5168\u6392\u5217 I */\nList<List<int>> PermutationsI(int[] nums) {\n List<List<int>> res = [];\n Backtrack([], nums, new bool[nums.Length], res);\n return res;\n}\n
permutations_i.go/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 I */\nfunc backtrackI(state *[]int, choices *[]int, selected *[]bool, res *[][]int) {\n // \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n if len(*state) == len(*choices) {\n newState := append([]int{}, *state...)\n *res = append(*res, newState)\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for i := 0; i < len(*choices); i++ {\n choice := (*choices)[i]\n // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20\n if !(*selected)[i] {\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n (*selected)[i] = true\n *state = append(*state, choice)\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrackI(state, choices, selected, res)\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n (*selected)[i] = false\n *state = (*state)[:len(*state)-1]\n }\n }\n}\n\n/* \u5168\u6392\u5217 I */\nfunc permutationsI(nums []int) [][]int {\n res := make([][]int, 0)\n state := make([]int, 0)\n selected := make([]bool, len(nums))\n backtrackI(&state, &nums, &selected, &res)\n return res\n}\n
permutations_i.swift/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 I */\nfunc backtrack(state: inout [Int], choices: [Int], selected: inout [Bool], res: inout [[Int]]) {\n // \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n if state.count == choices.count {\n res.append(state)\n return\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for (i, choice) in choices.enumerated() {\n // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20\n if !selected[i] {\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n selected[i] = true\n state.append(choice)\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state: &state, choices: choices, selected: &selected, res: &res)\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n selected[i] = false\n state.removeLast()\n }\n }\n}\n\n/* \u5168\u6392\u5217 I */\nfunc permutationsI(nums: [Int]) -> [[Int]] {\n var state: [Int] = []\n var selected = Array(repeating: false, count: nums.count)\n var res: [[Int]] = []\n backtrack(state: &state, choices: nums, selected: &selected, res: &res)\n return res\n}\n
permutations_i.js/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 I */\nfunction backtrack(state, choices, selected, res) {\n // \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n if (state.length === choices.length) {\n res.push([...state]);\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n choices.forEach((choice, i) => {\n // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20\n if (!selected[i]) {\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n selected[i] = true;\n state.push(choice);\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state, choices, selected, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n selected[i] = false;\n state.pop();\n }\n });\n}\n\n/* \u5168\u6392\u5217 I */\nfunction permutationsI(nums) {\n const res = [];\n backtrack([], nums, Array(nums.length).fill(false), res);\n return res;\n}\n
permutations_i.ts/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 I */\nfunction backtrack(\n state: number[],\n choices: number[],\n selected: boolean[],\n res: number[][]\n): void {\n // \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n if (state.length === choices.length) {\n res.push([...state]);\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n choices.forEach((choice, i) => {\n // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20\n if (!selected[i]) {\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n selected[i] = true;\n state.push(choice);\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state, choices, selected, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n selected[i] = false;\n state.pop();\n }\n });\n}\n\n/* \u5168\u6392\u5217 I */\nfunction permutationsI(nums: number[]): number[][] {\n const res: number[][] = [];\n backtrack([], nums, Array(nums.length).fill(false), res);\n return res;\n}\n
permutations_i.dart/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 I */\nvoid backtrack(\n List<int> state,\n List<int> choices,\n List<bool> selected,\n List<List<int>> res,\n) {\n // \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n if (state.length == choices.length) {\n res.add(List.from(state));\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for (int i = 0; i < choices.length; i++) {\n int choice = choices[i];\n // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20\n if (!selected[i]) {\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n selected[i] = true;\n state.add(choice);\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state, choices, selected, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n selected[i] = false;\n state.removeLast();\n }\n }\n}\n\n/* \u5168\u6392\u5217 I */\nList<List<int>> permutationsI(List<int> nums) {\n List<List<int>> res = [];\n backtrack([], nums, List.filled(nums.length, false), res);\n return res;\n}\n
permutations_i.rs/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 I */\nfn backtrack(mut state: Vec<i32>, choices: &[i32], selected: &mut [bool], res: &mut Vec<Vec<i32>>) {\n // \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n if state.len() == choices.len() {\n res.push(state);\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for i in 0..choices.len() {\n let choice = choices[i];\n // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20\n if !selected[i] {\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n selected[i] = true;\n state.push(choice);\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state.clone(), choices, selected, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n selected[i] = false;\n state.remove(state.len() - 1);\n }\n }\n}\n\n/* \u5168\u6392\u5217 I */\nfn permutations_i(nums: &mut [i32]) -> Vec<Vec<i32>> {\n let mut res = Vec::new(); // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n backtrack(Vec::new(), nums, &mut vec![false; nums.len()], &mut res);\n res\n}\n
permutations_i.c/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 I */\nvoid backtrack(int *state, int stateSize, int *choices, int choicesSize, bool *selected, int **res, int *resSize) {\n // \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n if (stateSize == choicesSize) {\n res[*resSize] = (int *)malloc(choicesSize * sizeof(int));\n for (int i = 0; i < choicesSize; i++) {\n res[*resSize][i] = state[i];\n }\n (*resSize)++;\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for (int i = 0; i < choicesSize; i++) {\n int choice = choices[i];\n // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20\n if (!selected[i]) {\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n selected[i] = true;\n state[stateSize] = choice;\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state, stateSize + 1, choices, choicesSize, selected, res, resSize);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n selected[i] = false;\n }\n }\n}\n\n/* \u5168\u6392\u5217 I */\nint **permutationsI(int *nums, int numsSize, int *returnSize) {\n int *state = (int *)malloc(numsSize * sizeof(int));\n bool *selected = (bool *)malloc(numsSize * sizeof(bool));\n for (int i = 0; i < numsSize; i++) {\n selected[i] = false;\n }\n int **res = (int **)malloc(MAX_SIZE * sizeof(int *));\n *returnSize = 0;\n\n backtrack(state, 0, nums, numsSize, selected, res, returnSize);\n\n free(state);\n free(selected);\n\n return res;\n}\n
permutations_i.zig[class]{}-[func]{backtrack}\n\n[class]{}-[func]{permutationsI}\n
"},{"location":"chapter_backtracking/permutations_problem/#1322","title":"13.2.2 \u00a0 \u8003\u8651\u76f8\u7b49\u5143\u7d20\u7684\u60c5\u51b5","text":"Question
\u8f93\u5165\u4e00\u4e2a\u6574\u6570\u6570\u7ec4\uff0c\u6570\u7ec4\u4e2d\u53ef\u80fd\u5305\u542b\u91cd\u590d\u5143\u7d20\uff0c\u8fd4\u56de\u6240\u6709\u4e0d\u91cd\u590d\u7684\u6392\u5217\u3002
\u5047\u8bbe\u8f93\u5165\u6570\u7ec4\u4e3a \\([1, 1, 2]\\) \u3002\u4e3a\u4e86\u65b9\u4fbf\u533a\u5206\u4e24\u4e2a\u91cd\u590d\u5143\u7d20 \\(1\\) \uff0c\u6211\u4eec\u5c06\u7b2c\u4e8c\u4e2a \\(1\\) \u8bb0\u4e3a \\(\\hat{1}\\) \u3002
\u5982\u56fe 13-7 \u6240\u793a\uff0c\u4e0a\u8ff0\u65b9\u6cd5\u751f\u6210\u7684\u6392\u5217\u6709\u4e00\u534a\u662f\u91cd\u590d\u7684\u3002
\u56fe 13-7 \u00a0 \u91cd\u590d\u6392\u5217
\u90a3\u4e48\u5982\u4f55\u53bb\u9664\u91cd\u590d\u7684\u6392\u5217\u5462\uff1f\u6700\u76f4\u63a5\u5730\uff0c\u8003\u8651\u501f\u52a9\u4e00\u4e2a\u54c8\u5e0c\u8868\uff0c\u76f4\u63a5\u5bf9\u6392\u5217\u7ed3\u679c\u8fdb\u884c\u53bb\u91cd\u3002\u7136\u800c\u8fd9\u6837\u505a\u4e0d\u591f\u4f18\u96c5\uff0c\u56e0\u4e3a\u751f\u6210\u91cd\u590d\u6392\u5217\u7684\u641c\u7d22\u5206\u652f\u6ca1\u6709\u5fc5\u8981\uff0c\u5e94\u5f53\u63d0\u524d\u8bc6\u522b\u5e76\u526a\u679d\uff0c\u8fd9\u6837\u53ef\u4ee5\u8fdb\u4e00\u6b65\u63d0\u5347\u7b97\u6cd5\u6548\u7387\u3002
"},{"location":"chapter_backtracking/permutations_problem/#1_1","title":"1. \u00a0 \u76f8\u7b49\u5143\u7d20\u526a\u679d","text":"\u89c2\u5bdf\u56fe 13-8 \uff0c\u5728\u7b2c\u4e00\u8f6e\u4e2d\uff0c\u9009\u62e9 \\(1\\) \u6216\u9009\u62e9 \\(\\hat{1}\\) \u662f\u7b49\u4ef7\u7684\uff0c\u5728\u8fd9\u4e24\u4e2a\u9009\u62e9\u4e4b\u4e0b\u751f\u6210\u7684\u6240\u6709\u6392\u5217\u90fd\u662f\u91cd\u590d\u7684\u3002\u56e0\u6b64\u5e94\u8be5\u628a \\(\\hat{1}\\) \u526a\u679d\u3002
\u540c\u7406\uff0c\u5728\u7b2c\u4e00\u8f6e\u9009\u62e9 \\(2\\) \u4e4b\u540e\uff0c\u7b2c\u4e8c\u8f6e\u9009\u62e9\u4e2d\u7684 \\(1\\) \u548c \\(\\hat{1}\\) \u4e5f\u4f1a\u4ea7\u751f\u91cd\u590d\u5206\u652f\uff0c\u56e0\u6b64\u4e5f\u5e94\u5c06\u7b2c\u4e8c\u8f6e\u7684 \\(\\hat{1}\\) \u526a\u679d\u3002
\u4ece\u672c\u8d28\u4e0a\u770b\uff0c\u6211\u4eec\u7684\u76ee\u6807\u662f\u5728\u67d0\u4e00\u8f6e\u9009\u62e9\u4e2d\uff0c\u4fdd\u8bc1\u591a\u4e2a\u76f8\u7b49\u7684\u5143\u7d20\u4ec5\u88ab\u9009\u62e9\u4e00\u6b21\u3002
\u56fe 13-8 \u00a0 \u91cd\u590d\u6392\u5217\u526a\u679d
"},{"location":"chapter_backtracking/permutations_problem/#2_1","title":"2. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"\u5728\u4e0a\u4e00\u9898\u7684\u4ee3\u7801\u7684\u57fa\u7840\u4e0a\uff0c\u6211\u4eec\u8003\u8651\u5728\u6bcf\u4e00\u8f6e\u9009\u62e9\u4e2d\u5f00\u542f\u4e00\u4e2a\u54c8\u5e0c\u8868 duplicated
\uff0c\u7528\u4e8e\u8bb0\u5f55\u8be5\u8f6e\u4e2d\u5df2\u7ecf\u5c1d\u8bd5\u8fc7\u7684\u5143\u7d20\uff0c\u5e76\u5c06\u91cd\u590d\u5143\u7d20\u526a\u679d\uff1a
def backtrack(\n state: list[int], choices: list[int], selected: list[bool], res: list[list[int]]\n):\n \"\"\"\u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 II\"\"\"\n # \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n if len(state) == len(choices):\n res.append(list(state))\n return\n # \u904d\u5386\u6240\u6709\u9009\u62e9\n duplicated = set[int]()\n for i, choice in enumerate(choices):\n # \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20 \u4e14 \u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u76f8\u7b49\u5143\u7d20\n if not selected[i] and choice not in duplicated:\n # \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n duplicated.add(choice) # \u8bb0\u5f55\u9009\u62e9\u8fc7\u7684\u5143\u7d20\u503c\n selected[i] = True\n state.append(choice)\n # \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state, choices, selected, res)\n # \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n selected[i] = False\n state.pop()\n\ndef permutations_ii(nums: list[int]) -> list[list[int]]:\n \"\"\"\u5168\u6392\u5217 II\"\"\"\n res = []\n backtrack(state=[], choices=nums, selected=[False] * len(nums), res=res)\n return res\n
permutations_ii.cpp/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 II */\nvoid backtrack(vector<int> &state, const vector<int> &choices, vector<bool> &selected, vector<vector<int>> &res) {\n // \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n if (state.size() == choices.size()) {\n res.push_back(state);\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n unordered_set<int> duplicated;\n for (int i = 0; i < choices.size(); i++) {\n int choice = choices[i];\n // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20 \u4e14 \u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u76f8\u7b49\u5143\u7d20\n if (!selected[i] && duplicated.find(choice) == duplicated.end()) {\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n duplicated.emplace(choice); // \u8bb0\u5f55\u9009\u62e9\u8fc7\u7684\u5143\u7d20\u503c\n selected[i] = true;\n state.push_back(choice);\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state, choices, selected, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n selected[i] = false;\n state.pop_back();\n }\n }\n}\n\n/* \u5168\u6392\u5217 II */\nvector<vector<int>> permutationsII(vector<int> nums) {\n vector<int> state;\n vector<bool> selected(nums.size(), false);\n vector<vector<int>> res;\n backtrack(state, nums, selected, res);\n return res;\n}\n
permutations_ii.java/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 II */\nvoid backtrack(List<Integer> state, int[] choices, boolean[] selected, List<List<Integer>> res) {\n // \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n if (state.size() == choices.length) {\n res.add(new ArrayList<Integer>(state));\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n Set<Integer> duplicated = new HashSet<Integer>();\n for (int i = 0; i < choices.length; i++) {\n int choice = choices[i];\n // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20 \u4e14 \u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u76f8\u7b49\u5143\u7d20\n if (!selected[i] && !duplicated.contains(choice)) {\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n duplicated.add(choice); // \u8bb0\u5f55\u9009\u62e9\u8fc7\u7684\u5143\u7d20\u503c\n selected[i] = true;\n state.add(choice);\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state, choices, selected, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n selected[i] = false;\n state.remove(state.size() - 1);\n }\n }\n}\n\n/* \u5168\u6392\u5217 II */\nList<List<Integer>> permutationsII(int[] nums) {\n List<List<Integer>> res = new ArrayList<List<Integer>>();\n backtrack(new ArrayList<Integer>(), nums, new boolean[nums.length], res);\n return res;\n}\n
permutations_ii.cs/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 II */\nvoid Backtrack(List<int> state, int[] choices, bool[] selected, List<List<int>> res) {\n // \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n if (state.Count == choices.Length) {\n res.Add(new List<int>(state));\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n HashSet<int> duplicated = [];\n for (int i = 0; i < choices.Length; i++) {\n int choice = choices[i];\n // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20 \u4e14 \u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u76f8\u7b49\u5143\u7d20\n if (!selected[i] && !duplicated.Contains(choice)) {\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n duplicated.Add(choice); // \u8bb0\u5f55\u9009\u62e9\u8fc7\u7684\u5143\u7d20\u503c\n selected[i] = true;\n state.Add(choice);\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n Backtrack(state, choices, selected, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n selected[i] = false;\n state.RemoveAt(state.Count - 1);\n }\n }\n}\n\n/* \u5168\u6392\u5217 II */\nList<List<int>> PermutationsII(int[] nums) {\n List<List<int>> res = [];\n Backtrack([], nums, new bool[nums.Length], res);\n return res;\n}\n
permutations_ii.go/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 II */\nfunc backtrackII(state *[]int, choices *[]int, selected *[]bool, res *[][]int) {\n // \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n if len(*state) == len(*choices) {\n newState := append([]int{}, *state...)\n *res = append(*res, newState)\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n duplicated := make(map[int]struct{}, 0)\n for i := 0; i < len(*choices); i++ {\n choice := (*choices)[i]\n // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20 \u4e14 \u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u76f8\u7b49\u5143\u7d20\n if _, ok := duplicated[choice]; !ok && !(*selected)[i] {\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n // \u8bb0\u5f55\u9009\u62e9\u8fc7\u7684\u5143\u7d20\u503c\n duplicated[choice] = struct{}{}\n (*selected)[i] = true\n *state = append(*state, choice)\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrackI(state, choices, selected, res)\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n (*selected)[i] = false\n *state = (*state)[:len(*state)-1]\n }\n }\n}\n\n/* \u5168\u6392\u5217 II */\nfunc permutationsII(nums []int) [][]int {\n res := make([][]int, 0)\n state := make([]int, 0)\n selected := make([]bool, len(nums))\n backtrackII(&state, &nums, &selected, &res)\n return res\n}\n
permutations_ii.swift/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 II */\nfunc backtrack(state: inout [Int], choices: [Int], selected: inout [Bool], res: inout [[Int]]) {\n // \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n if state.count == choices.count {\n res.append(state)\n return\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n var duplicated: Set<Int> = []\n for (i, choice) in choices.enumerated() {\n // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20 \u4e14 \u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u76f8\u7b49\u5143\u7d20\n if !selected[i], !duplicated.contains(choice) {\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n duplicated.insert(choice) // \u8bb0\u5f55\u9009\u62e9\u8fc7\u7684\u5143\u7d20\u503c\n selected[i] = true\n state.append(choice)\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state: &state, choices: choices, selected: &selected, res: &res)\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n selected[i] = false\n state.removeLast()\n }\n }\n}\n\n/* \u5168\u6392\u5217 II */\nfunc permutationsII(nums: [Int]) -> [[Int]] {\n var state: [Int] = []\n var selected = Array(repeating: false, count: nums.count)\n var res: [[Int]] = []\n backtrack(state: &state, choices: nums, selected: &selected, res: &res)\n return res\n}\n
permutations_ii.js/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 II */\nfunction backtrack(state, choices, selected, res) {\n // \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n if (state.length === choices.length) {\n res.push([...state]);\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n const duplicated = new Set();\n choices.forEach((choice, i) => {\n // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20 \u4e14 \u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u76f8\u7b49\u5143\u7d20\n if (!selected[i] && !duplicated.has(choice)) {\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n duplicated.add(choice); // \u8bb0\u5f55\u9009\u62e9\u8fc7\u7684\u5143\u7d20\u503c\n selected[i] = true;\n state.push(choice);\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state, choices, selected, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n selected[i] = false;\n state.pop();\n }\n });\n}\n\n/* \u5168\u6392\u5217 II */\nfunction permutationsII(nums) {\n const res = [];\n backtrack([], nums, Array(nums.length).fill(false), res);\n return res;\n}\n
permutations_ii.ts/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 II */\nfunction backtrack(\n state: number[],\n choices: number[],\n selected: boolean[],\n res: number[][]\n): void {\n // \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n if (state.length === choices.length) {\n res.push([...state]);\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n const duplicated = new Set();\n choices.forEach((choice, i) => {\n // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20 \u4e14 \u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u76f8\u7b49\u5143\u7d20\n if (!selected[i] && !duplicated.has(choice)) {\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n duplicated.add(choice); // \u8bb0\u5f55\u9009\u62e9\u8fc7\u7684\u5143\u7d20\u503c\n selected[i] = true;\n state.push(choice);\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state, choices, selected, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n selected[i] = false;\n state.pop();\n }\n });\n}\n\n/* \u5168\u6392\u5217 II */\nfunction permutationsII(nums: number[]): number[][] {\n const res: number[][] = [];\n backtrack([], nums, Array(nums.length).fill(false), res);\n return res;\n}\n
permutations_ii.dart/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 II */\nvoid backtrack(\n List<int> state,\n List<int> choices,\n List<bool> selected,\n List<List<int>> res,\n) {\n // \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n if (state.length == choices.length) {\n res.add(List.from(state));\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n Set<int> duplicated = {};\n for (int i = 0; i < choices.length; i++) {\n int choice = choices[i];\n // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20 \u4e14 \u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u76f8\u7b49\u5143\u7d20\n if (!selected[i] && !duplicated.contains(choice)) {\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n duplicated.add(choice); // \u8bb0\u5f55\u9009\u62e9\u8fc7\u7684\u5143\u7d20\u503c\n selected[i] = true;\n state.add(choice);\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state, choices, selected, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n selected[i] = false;\n state.removeLast();\n }\n }\n}\n\n/* \u5168\u6392\u5217 II */\nList<List<int>> permutationsII(List<int> nums) {\n List<List<int>> res = [];\n backtrack([], nums, List.filled(nums.length, false), res);\n return res;\n}\n
permutations_ii.rs/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 II */\nfn backtrack(mut state: Vec<i32>, choices: &[i32], selected: &mut [bool], res: &mut Vec<Vec<i32>>) {\n // \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n if state.len() == choices.len() {\n res.push(state);\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n let mut duplicated = HashSet::<i32>::new();\n for i in 0..choices.len() {\n let choice = choices[i];\n // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20 \u4e14 \u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u76f8\u7b49\u5143\u7d20\n if !selected[i] && !duplicated.contains(&choice) {\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n duplicated.insert(choice); // \u8bb0\u5f55\u9009\u62e9\u8fc7\u7684\u5143\u7d20\u503c\n selected[i] = true;\n state.push(choice);\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state.clone(), choices, selected, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n selected[i] = false;\n state.remove(state.len() - 1);\n }\n }\n}\n\n/* \u5168\u6392\u5217 II */\nfn permutations_ii(nums: &mut [i32]) -> Vec<Vec<i32>> {\n let mut res = Vec::new();\n backtrack(Vec::new(), nums, &mut vec![false; nums.len()], &mut res);\n res\n}\n
permutations_ii.c/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 II */\nvoid backtrack(int *state, int stateSize, int *choices, int choicesSize, bool *selected, int **res, int *resSize) {\n // \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\n if (stateSize == choicesSize) {\n res[*resSize] = (int *)malloc(choicesSize * sizeof(int));\n for (int i = 0; i < choicesSize; i++) {\n res[*resSize][i] = state[i];\n }\n (*resSize)++;\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n bool duplicated[MAX_SIZE] = {false};\n for (int i = 0; i < choicesSize; i++) {\n int choice = choices[i];\n // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20 \u4e14 \u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u76f8\u7b49\u5143\u7d20\n if (!selected[i] && !duplicated[choice]) {\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n duplicated[choice] = true; // \u8bb0\u5f55\u9009\u62e9\u8fc7\u7684\u5143\u7d20\u503c\n selected[i] = true;\n state[stateSize] = choice;\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state, stateSize + 1, choices, choicesSize, selected, res, resSize);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n selected[i] = false;\n }\n }\n}\n\n/* \u5168\u6392\u5217 II */\nint **permutationsII(int *nums, int numsSize, int *returnSize) {\n int *state = (int *)malloc(numsSize * sizeof(int));\n bool *selected = (bool *)malloc(numsSize * sizeof(bool));\n for (int i = 0; i < numsSize; i++) {\n selected[i] = false;\n }\n int **res = (int **)malloc(MAX_SIZE * sizeof(int *));\n *returnSize = 0;\n\n backtrack(state, 0, nums, numsSize, selected, res, returnSize);\n\n free(state);\n free(selected);\n\n return res;\n}\n
permutations_ii.zig[class]{}-[func]{backtrack}\n\n[class]{}-[func]{permutationsII}\n
\u5047\u8bbe\u5143\u7d20\u4e24\u4e24\u4e4b\u95f4\u4e92\u4e0d\u76f8\u540c\uff0c\u5219 \\(n\\) \u4e2a\u5143\u7d20\u5171\u6709 \\(n!\\) \u79cd\u6392\u5217\uff08\u9636\u4e58\uff09\uff1b\u5728\u8bb0\u5f55\u7ed3\u679c\u65f6\uff0c\u9700\u8981\u590d\u5236\u957f\u5ea6\u4e3a \\(n\\) \u7684\u5217\u8868\uff0c\u4f7f\u7528 \\(O(n)\\) \u65f6\u95f4\u3002\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n!n)\\) \u3002
\u6700\u5927\u9012\u5f52\u6df1\u5ea6\u4e3a \\(n\\) \uff0c\u4f7f\u7528 \\(O(n)\\) \u6808\u5e27\u7a7a\u95f4\u3002selected
\u4f7f\u7528 \\(O(n)\\) \u7a7a\u95f4\u3002\u540c\u4e00\u65f6\u523b\u6700\u591a\u5171\u6709 \\(n\\) \u4e2a duplicated
\uff0c\u4f7f\u7528 \\(O(n^2)\\) \u7a7a\u95f4\u3002\u56e0\u6b64\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\) \u3002
\u8bf7\u6ce8\u610f\uff0c\u867d\u7136 selected
\u548c duplicated
\u90fd\u7528\u4e8e\u526a\u679d\uff0c\u4f46\u4e24\u8005\u7684\u76ee\u6807\u4e0d\u540c\u3002
selected
\u3002\u5b83\u8bb0\u5f55\u7684\u662f\u5f53\u524d\u72b6\u6001\u4e2d\u5305\u542b\u54ea\u4e9b\u5143\u7d20\uff0c\u5176\u4f5c\u7528\u662f\u9632\u6b62 choices
\u4e2d\u7684\u4efb\u4e00\u5143\u7d20\u5728 state
\u4e2d\u91cd\u590d\u51fa\u73b0\u3002backtrack
\u51fd\u6570\uff09\u90fd\u5305\u542b\u4e00\u4e2a duplicated
\u3002\u5b83\u8bb0\u5f55\u7684\u662f\u5728\u672c\u8f6e\u904d\u5386\uff08for
\u5faa\u73af\uff09\u4e2d\u54ea\u4e9b\u5143\u7d20\u5df2\u88ab\u9009\u62e9\u8fc7\uff0c\u5176\u4f5c\u7528\u662f\u4fdd\u8bc1\u76f8\u7b49\u7684\u5143\u7d20\u53ea\u88ab\u9009\u62e9\u4e00\u6b21\u3002\u56fe 13-9 \u5c55\u793a\u4e86\u4e24\u4e2a\u526a\u679d\u6761\u4ef6\u7684\u751f\u6548\u8303\u56f4\u3002\u6ce8\u610f\uff0c\u6811\u4e2d\u7684\u6bcf\u4e2a\u8282\u70b9\u4ee3\u8868\u4e00\u4e2a\u9009\u62e9\uff0c\u4ece\u6839\u8282\u70b9\u5230\u53f6\u8282\u70b9\u7684\u8def\u5f84\u4e0a\u7684\u5404\u4e2a\u8282\u70b9\u6784\u6210\u4e00\u4e2a\u6392\u5217\u3002
\u56fe 13-9 \u00a0 \u4e24\u79cd\u526a\u679d\u6761\u4ef6\u7684\u4f5c\u7528\u8303\u56f4
"},{"location":"chapter_backtracking/subset_sum_problem/","title":"13.3 \u00a0 \u5b50\u96c6\u548c\u95ee\u9898","text":""},{"location":"chapter_backtracking/subset_sum_problem/#1331","title":"13.3.1 \u00a0 \u65e0\u91cd\u590d\u5143\u7d20\u7684\u60c5\u51b5","text":"Question
\u7ed9\u5b9a\u4e00\u4e2a\u6b63\u6574\u6570\u6570\u7ec4 nums
\u548c\u4e00\u4e2a\u76ee\u6807\u6b63\u6574\u6570 target
\uff0c\u8bf7\u627e\u51fa\u6240\u6709\u53ef\u80fd\u7684\u7ec4\u5408\uff0c\u4f7f\u5f97\u7ec4\u5408\u4e2d\u7684\u5143\u7d20\u548c\u7b49\u4e8e target
\u3002\u7ed9\u5b9a\u6570\u7ec4\u65e0\u91cd\u590d\u5143\u7d20\uff0c\u6bcf\u4e2a\u5143\u7d20\u53ef\u4ee5\u88ab\u9009\u53d6\u591a\u6b21\u3002\u8bf7\u4ee5\u5217\u8868\u5f62\u5f0f\u8fd4\u56de\u8fd9\u4e9b\u7ec4\u5408\uff0c\u5217\u8868\u4e2d\u4e0d\u5e94\u5305\u542b\u91cd\u590d\u7ec4\u5408\u3002
\u4f8b\u5982\uff0c\u8f93\u5165\u96c6\u5408 \\(\\{3, 4, 5\\}\\) \u548c\u76ee\u6807\u6574\u6570 \\(9\\) \uff0c\u89e3\u4e3a \\(\\{3, 3, 3\\}, \\{4, 5\\}\\) \u3002\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u4e24\u70b9\u3002
\u7c7b\u4f3c\u4e8e\u5168\u6392\u5217\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u628a\u5b50\u96c6\u7684\u751f\u6210\u8fc7\u7a0b\u60f3\u8c61\u6210\u4e00\u7cfb\u5217\u9009\u62e9\u7684\u7ed3\u679c\uff0c\u5e76\u5728\u9009\u62e9\u8fc7\u7a0b\u4e2d\u5b9e\u65f6\u66f4\u65b0\u201c\u5143\u7d20\u548c\u201d\uff0c\u5f53\u5143\u7d20\u548c\u7b49\u4e8e target
\u65f6\uff0c\u5c31\u5c06\u5b50\u96c6\u8bb0\u5f55\u81f3\u7ed3\u679c\u5217\u8868\u3002
\u800c\u4e0e\u5168\u6392\u5217\u95ee\u9898\u4e0d\u540c\u7684\u662f\uff0c\u672c\u9898\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u53ef\u4ee5\u88ab\u65e0\u9650\u6b21\u9009\u53d6\uff0c\u56e0\u6b64\u65e0\u987b\u501f\u52a9 selected
\u5e03\u5c14\u5217\u8868\u6765\u8bb0\u5f55\u5143\u7d20\u662f\u5426\u5df2\u88ab\u9009\u62e9\u3002\u6211\u4eec\u53ef\u4ee5\u5bf9\u5168\u6392\u5217\u4ee3\u7801\u8fdb\u884c\u5c0f\u5e45\u4fee\u6539\uff0c\u521d\u6b65\u5f97\u5230\u89e3\u9898\u4ee3\u7801\uff1a
def backtrack(\n state: list[int],\n target: int,\n total: int,\n choices: list[int],\n res: list[list[int]],\n):\n \"\"\"\u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I\"\"\"\n # \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n if total == target:\n res.append(list(state))\n return\n # \u904d\u5386\u6240\u6709\u9009\u62e9\n for i in range(len(choices)):\n # \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u8df3\u8fc7\u8be5\u9009\u62e9\n if total + choices[i] > target:\n continue\n # \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\n state.append(choices[i])\n # \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state, target, total + choices[i], choices, res)\n # \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n state.pop()\n\ndef subset_sum_i_naive(nums: list[int], target: int) -> list[list[int]]:\n \"\"\"\u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u590d\u5b50\u96c6\uff09\"\"\"\n state = [] # \u72b6\u6001\uff08\u5b50\u96c6\uff09\n total = 0 # \u5b50\u96c6\u548c\n res = [] # \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n backtrack(state, target, total, nums, res)\n return res\n
subset_sum_i_naive.cpp/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nvoid backtrack(vector<int> &state, int target, int total, vector<int> &choices, vector<vector<int>> &res) {\n // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n if (total == target) {\n res.push_back(state);\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for (size_t i = 0; i < choices.size(); i++) {\n // \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u8df3\u8fc7\u8be5\u9009\u62e9\n if (total + choices[i] > target) {\n continue;\n }\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\n state.push_back(choices[i]);\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state, target, total + choices[i], choices, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n state.pop_back();\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u590d\u5b50\u96c6\uff09 */\nvector<vector<int>> subsetSumINaive(vector<int> &nums, int target) {\n vector<int> state; // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n int total = 0; // \u5b50\u96c6\u548c\n vector<vector<int>> res; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n backtrack(state, target, total, nums, res);\n return res;\n}\n
subset_sum_i_naive.java/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nvoid backtrack(List<Integer> state, int target, int total, int[] choices, List<List<Integer>> res) {\n // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n if (total == target) {\n res.add(new ArrayList<>(state));\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for (int i = 0; i < choices.length; i++) {\n // \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u8df3\u8fc7\u8be5\u9009\u62e9\n if (total + choices[i] > target) {\n continue;\n }\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\n state.add(choices[i]);\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state, target, total + choices[i], choices, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n state.remove(state.size() - 1);\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u590d\u5b50\u96c6\uff09 */\nList<List<Integer>> subsetSumINaive(int[] nums, int target) {\n List<Integer> state = new ArrayList<>(); // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n int total = 0; // \u5b50\u96c6\u548c\n List<List<Integer>> res = new ArrayList<>(); // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n backtrack(state, target, total, nums, res);\n return res;\n}\n
subset_sum_i_naive.cs/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nvoid Backtrack(List<int> state, int target, int total, int[] choices, List<List<int>> res) {\n // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n if (total == target) {\n res.Add(new List<int>(state));\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for (int i = 0; i < choices.Length; i++) {\n // \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u8df3\u8fc7\u8be5\u9009\u62e9\n if (total + choices[i] > target) {\n continue;\n }\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\n state.Add(choices[i]);\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n Backtrack(state, target, total + choices[i], choices, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n state.RemoveAt(state.Count - 1);\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u590d\u5b50\u96c6\uff09 */\nList<List<int>> SubsetSumINaive(int[] nums, int target) {\n List<int> state = []; // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n int total = 0; // \u5b50\u96c6\u548c\n List<List<int>> res = []; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n Backtrack(state, target, total, nums, res);\n return res;\n}\n
subset_sum_i_naive.go/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nfunc backtrackSubsetSumINaive(total, target int, state, choices *[]int, res *[][]int) {\n // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n if target == total {\n newState := append([]int{}, *state...)\n *res = append(*res, newState)\n return\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for i := 0; i < len(*choices); i++ {\n // \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u8df3\u8fc7\u8be5\u9009\u62e9\n if total+(*choices)[i] > target {\n continue\n }\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\n *state = append(*state, (*choices)[i])\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrackSubsetSumINaive(total+(*choices)[i], target, state, choices, res)\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n *state = (*state)[:len(*state)-1]\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u590d\u5b50\u96c6\uff09 */\nfunc subsetSumINaive(nums []int, target int) [][]int {\n state := make([]int, 0) // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n total := 0 // \u5b50\u96c6\u548c\n res := make([][]int, 0) // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n backtrackSubsetSumINaive(total, target, &state, &nums, &res)\n return res\n}\n
subset_sum_i_naive.swift/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nfunc backtrack(state: inout [Int], target: Int, total: Int, choices: [Int], res: inout [[Int]]) {\n // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n if total == target {\n res.append(state)\n return\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for i in stride(from: 0, to: choices.count, by: 1) {\n // \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u8df3\u8fc7\u8be5\u9009\u62e9\n if total + choices[i] > target {\n continue\n }\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\n state.append(choices[i])\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state: &state, target: target, total: total + choices[i], choices: choices, res: &res)\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n state.removeLast()\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u590d\u5b50\u96c6\uff09 */\nfunc subsetSumINaive(nums: [Int], target: Int) -> [[Int]] {\n var state: [Int] = [] // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n let total = 0 // \u5b50\u96c6\u548c\n var res: [[Int]] = [] // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n backtrack(state: &state, target: target, total: total, choices: nums, res: &res)\n return res\n}\n
subset_sum_i_naive.js/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nfunction backtrack(state, target, total, choices, res) {\n // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n if (total === target) {\n res.push([...state]);\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for (let i = 0; i < choices.length; i++) {\n // \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u8df3\u8fc7\u8be5\u9009\u62e9\n if (total + choices[i] > target) {\n continue;\n }\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\n state.push(choices[i]);\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state, target, total + choices[i], choices, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n state.pop();\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u590d\u5b50\u96c6\uff09 */\nfunction subsetSumINaive(nums, target) {\n const state = []; // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n const total = 0; // \u5b50\u96c6\u548c\n const res = []; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n backtrack(state, target, total, nums, res);\n return res;\n}\n
subset_sum_i_naive.ts/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nfunction backtrack(\n state: number[],\n target: number,\n total: number,\n choices: number[],\n res: number[][]\n): void {\n // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n if (total === target) {\n res.push([...state]);\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for (let i = 0; i < choices.length; i++) {\n // \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u8df3\u8fc7\u8be5\u9009\u62e9\n if (total + choices[i] > target) {\n continue;\n }\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\n state.push(choices[i]);\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state, target, total + choices[i], choices, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n state.pop();\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u590d\u5b50\u96c6\uff09 */\nfunction subsetSumINaive(nums: number[], target: number): number[][] {\n const state = []; // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n const total = 0; // \u5b50\u96c6\u548c\n const res = []; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n backtrack(state, target, total, nums, res);\n return res;\n}\n
subset_sum_i_naive.dart/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nvoid backtrack(\n List<int> state,\n int target,\n int total,\n List<int> choices,\n List<List<int>> res,\n) {\n // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n if (total == target) {\n res.add(List.from(state));\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for (int i = 0; i < choices.length; i++) {\n // \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u8df3\u8fc7\u8be5\u9009\u62e9\n if (total + choices[i] > target) {\n continue;\n }\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\n state.add(choices[i]);\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state, target, total + choices[i], choices, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n state.removeLast();\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u590d\u5b50\u96c6\uff09 */\nList<List<int>> subsetSumINaive(List<int> nums, int target) {\n List<int> state = []; // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n int total = 0; // \u5143\u7d20\u548c\n List<List<int>> res = []; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n backtrack(state, target, total, nums, res);\n return res;\n}\n
subset_sum_i_naive.rs/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nfn backtrack(mut state: Vec<i32>, target: i32, total: i32, choices: &[i32], res: &mut Vec<Vec<i32>>) {\n // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n if total == target {\n res.push(state);\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for i in 0..choices.len() {\n // \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u8df3\u8fc7\u8be5\u9009\u62e9\n if total + choices[i] > target {\n continue;\n }\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\n state.push(choices[i]);\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state.clone(), target, total + choices[i], choices, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n state.pop();\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u590d\u5b50\u96c6\uff09 */\nfn subset_sum_i_naive(nums: &[i32], target: i32) -> Vec<Vec<i32>> {\n let state = Vec::new(); // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n let total = 0; // \u5b50\u96c6\u548c\n let mut res = Vec::new(); // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n backtrack(state, target, total, nums, &mut res);\n res\n}\n
subset_sum_i_naive.c/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nvoid backtrack(int target, int total, int *choices, int choicesSize) {\n // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n if (total == target) {\n for (int i = 0; i < stateSize; i++) {\n res[resSize][i] = state[i];\n }\n resColSizes[resSize++] = stateSize;\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for (int i = 0; i < choicesSize; i++) {\n // \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u8df3\u8fc7\u8be5\u9009\u62e9\n if (total + choices[i] > target) {\n continue;\n }\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\n state[stateSize++] = choices[i];\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(target, total + choices[i], choices, choicesSize);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n stateSize--;\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u590d\u5b50\u96c6\uff09 */\nvoid subsetSumINaive(int *nums, int numsSize, int target) {\n resSize = 0; // \u521d\u59cb\u5316\u89e3\u7684\u6570\u91cf\u4e3a0\n backtrack(target, 0, nums, numsSize);\n}\n
subset_sum_i_naive.zig[class]{}-[func]{backtrack}\n\n[class]{}-[func]{subsetSumINaive}\n
\u5411\u4ee5\u4e0a\u4ee3\u7801\u8f93\u5165\u6570\u7ec4 \\([3, 4, 5]\\) \u548c\u76ee\u6807\u5143\u7d20 \\(9\\) \uff0c\u8f93\u51fa\u7ed3\u679c\u4e3a \\([3, 3, 3], [4, 5], [5, 4]\\) \u3002\u867d\u7136\u6210\u529f\u627e\u51fa\u4e86\u6240\u6709\u548c\u4e3a \\(9\\) \u7684\u5b50\u96c6\uff0c\u4f46\u5176\u4e2d\u5b58\u5728\u91cd\u590d\u7684\u5b50\u96c6 \\([4, 5]\\) \u548c \\([5, 4]\\) \u3002
\u8fd9\u662f\u56e0\u4e3a\u641c\u7d22\u8fc7\u7a0b\u662f\u533a\u5206\u9009\u62e9\u987a\u5e8f\u7684\uff0c\u7136\u800c\u5b50\u96c6\u4e0d\u533a\u5206\u9009\u62e9\u987a\u5e8f\u3002\u5982\u56fe 13-10 \u6240\u793a\uff0c\u5148\u9009 \\(4\\) \u540e\u9009 \\(5\\) \u4e0e\u5148\u9009 \\(5\\) \u540e\u9009 \\(4\\) \u662f\u4e0d\u540c\u7684\u5206\u652f\uff0c\u4f46\u5bf9\u5e94\u540c\u4e00\u4e2a\u5b50\u96c6\u3002
\u56fe 13-10 \u00a0 \u5b50\u96c6\u641c\u7d22\u4e0e\u8d8a\u754c\u526a\u679d
\u4e3a\u4e86\u53bb\u9664\u91cd\u590d\u5b50\u96c6\uff0c\u4e00\u79cd\u76f4\u63a5\u7684\u601d\u8def\u662f\u5bf9\u7ed3\u679c\u5217\u8868\u8fdb\u884c\u53bb\u91cd\u3002\u4f46\u8fd9\u4e2a\u65b9\u6cd5\u6548\u7387\u5f88\u4f4e\uff0c\u6709\u4e24\u65b9\u9762\u539f\u56e0\u3002
target
\u8f83\u5927\u65f6\uff0c\u641c\u7d22\u8fc7\u7a0b\u4f1a\u4ea7\u751f\u5927\u91cf\u7684\u91cd\u590d\u5b50\u96c6\u3002\u6211\u4eec\u8003\u8651\u5728\u641c\u7d22\u8fc7\u7a0b\u4e2d\u901a\u8fc7\u526a\u679d\u8fdb\u884c\u53bb\u91cd\u3002\u89c2\u5bdf\u56fe 13-11 \uff0c\u91cd\u590d\u5b50\u96c6\u662f\u5728\u4ee5\u4e0d\u540c\u987a\u5e8f\u9009\u62e9\u6570\u7ec4\u5143\u7d20\u65f6\u4ea7\u751f\u7684\uff0c\u4f8b\u5982\u4ee5\u4e0b\u60c5\u51b5\u3002
1.
\u6b65\u4e2d\u751f\u6210\u7684\u5b50\u96c6\u5b8c\u5168\u91cd\u590d\u3002\u5728\u641c\u7d22\u8fc7\u7a0b\u4e2d\uff0c\u6bcf\u4e00\u5c42\u7684\u9009\u62e9\u90fd\u662f\u4ece\u5de6\u5230\u53f3\u88ab\u9010\u4e2a\u5c1d\u8bd5\u7684\uff0c\u56e0\u6b64\u8d8a\u9760\u53f3\u7684\u5206\u652f\u88ab\u526a\u6389\u7684\u8d8a\u591a\u3002
1.
\u6b65\u548c\u7b2c 2.
\u6b65\u4e2d\u63cf\u8ff0\u7684\u5b50\u96c6\u5b8c\u5168\u91cd\u590d\u3002\u56fe 13-11 \u00a0 \u4e0d\u540c\u9009\u62e9\u987a\u5e8f\u5bfc\u81f4\u7684\u91cd\u590d\u5b50\u96c6
\u603b\u7ed3\u6765\u770b\uff0c\u7ed9\u5b9a\u8f93\u5165\u6570\u7ec4 \\([x_1, x_2, \\dots, x_n]\\) \uff0c\u8bbe\u641c\u7d22\u8fc7\u7a0b\u4e2d\u7684\u9009\u62e9\u5e8f\u5217\u4e3a \\([x_{i_1}, x_{i_2}, \\dots, x_{i_m}]\\) \uff0c\u5219\u8be5\u9009\u62e9\u5e8f\u5217\u9700\u8981\u6ee1\u8db3 \\(i_1 \\leq i_2 \\leq \\dots \\leq i_m\\) \uff0c\u4e0d\u6ee1\u8db3\u8be5\u6761\u4ef6\u7684\u9009\u62e9\u5e8f\u5217\u90fd\u4f1a\u9020\u6210\u91cd\u590d\uff0c\u5e94\u5f53\u526a\u679d\u3002
"},{"location":"chapter_backtracking/subset_sum_problem/#3","title":"3. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"\u4e3a\u5b9e\u73b0\u8be5\u526a\u679d\uff0c\u6211\u4eec\u521d\u59cb\u5316\u53d8\u91cf start
\uff0c\u7528\u4e8e\u6307\u793a\u904d\u5386\u8d77\u59cb\u70b9\u3002\u5f53\u505a\u51fa\u9009\u62e9 \\(x_{i}\\) \u540e\uff0c\u8bbe\u5b9a\u4e0b\u4e00\u8f6e\u4ece\u7d22\u5f15 \\(i\\) \u5f00\u59cb\u904d\u5386\u3002\u8fd9\u6837\u505a\u5c31\u53ef\u4ee5\u8ba9\u9009\u62e9\u5e8f\u5217\u6ee1\u8db3 \\(i_1 \\leq i_2 \\leq \\dots \\leq i_m\\) \uff0c\u4ece\u800c\u4fdd\u8bc1\u5b50\u96c6\u552f\u4e00\u3002
\u9664\u6b64\u4e4b\u5916\uff0c\u6211\u4eec\u8fd8\u5bf9\u4ee3\u7801\u8fdb\u884c\u4e86\u4ee5\u4e0b\u4e24\u9879\u4f18\u5316\u3002
nums
\u6392\u5e8f\u3002\u5728\u904d\u5386\u6240\u6709\u9009\u62e9\u65f6\uff0c\u5f53\u5b50\u96c6\u548c\u8d85\u8fc7 target
\u65f6\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\uff0c\u56e0\u4e3a\u540e\u8fb9\u7684\u5143\u7d20\u66f4\u5927\uff0c\u5176\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target
\u3002total
\uff0c\u901a\u8fc7\u5728 target
\u4e0a\u6267\u884c\u51cf\u6cd5\u6765\u7edf\u8ba1\u5143\u7d20\u548c\uff0c\u5f53 target
\u7b49\u4e8e \\(0\\) \u65f6\u8bb0\u5f55\u89e3\u3002def backtrack(\n state: list[int], target: int, choices: list[int], start: int, res: list[list[int]]\n):\n \"\"\"\u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I\"\"\"\n # \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n if target == 0:\n res.append(list(state))\n return\n # \u904d\u5386\u6240\u6709\u9009\u62e9\n # \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n for i in range(start, len(choices)):\n # \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n # \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n if target - choices[i] < 0:\n break\n # \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n state.append(choices[i])\n # \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state, target - choices[i], choices, i, res)\n # \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n state.pop()\n\ndef subset_sum_i(nums: list[int], target: int) -> list[list[int]]:\n \"\"\"\u6c42\u89e3\u5b50\u96c6\u548c I\"\"\"\n state = [] # \u72b6\u6001\uff08\u5b50\u96c6\uff09\n nums.sort() # \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n start = 0 # \u904d\u5386\u8d77\u59cb\u70b9\n res = [] # \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n backtrack(state, target, nums, start, res)\n return res\n
subset_sum_i.cpp/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nvoid backtrack(vector<int> &state, int target, vector<int> &choices, int start, vector<vector<int>> &res) {\n // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n if (target == 0) {\n res.push_back(state);\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n // \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n for (int i = start; i < choices.size(); i++) {\n // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n // \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n if (target - choices[i] < 0) {\n break;\n }\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n state.push_back(choices[i]);\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state, target - choices[i], choices, i, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n state.pop_back();\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I */\nvector<vector<int>> subsetSumI(vector<int> &nums, int target) {\n vector<int> state; // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n sort(nums.begin(), nums.end()); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n int start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\n vector<vector<int>> res; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n backtrack(state, target, nums, start, res);\n return res;\n}\n
subset_sum_i.java/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nvoid backtrack(List<Integer> state, int target, int[] choices, int start, List<List<Integer>> res) {\n // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n if (target == 0) {\n res.add(new ArrayList<>(state));\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n // \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n for (int i = start; i < choices.length; i++) {\n // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n // \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n if (target - choices[i] < 0) {\n break;\n }\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n state.add(choices[i]);\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state, target - choices[i], choices, i, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n state.remove(state.size() - 1);\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I */\nList<List<Integer>> subsetSumI(int[] nums, int target) {\n List<Integer> state = new ArrayList<>(); // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n Arrays.sort(nums); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n int start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\n List<List<Integer>> res = new ArrayList<>(); // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n backtrack(state, target, nums, start, res);\n return res;\n}\n
subset_sum_i.cs/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nvoid Backtrack(List<int> state, int target, int[] choices, int start, List<List<int>> res) {\n // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n if (target == 0) {\n res.Add(new List<int>(state));\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n // \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n for (int i = start; i < choices.Length; i++) {\n // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n // \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n if (target - choices[i] < 0) {\n break;\n }\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n state.Add(choices[i]);\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n Backtrack(state, target - choices[i], choices, i, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n state.RemoveAt(state.Count - 1);\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I */\nList<List<int>> SubsetSumI(int[] nums, int target) {\n List<int> state = []; // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n Array.Sort(nums); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n int start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\n List<List<int>> res = []; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n Backtrack(state, target, nums, start, res);\n return res;\n}\n
subset_sum_i.go/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nfunc backtrackSubsetSumI(start, target int, state, choices *[]int, res *[][]int) {\n // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n if target == 0 {\n newState := append([]int{}, *state...)\n *res = append(*res, newState)\n return\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n // \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n for i := start; i < len(*choices); i++ {\n // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n // \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n if target-(*choices)[i] < 0 {\n break\n }\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n *state = append(*state, (*choices)[i])\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrackSubsetSumI(i, target-(*choices)[i], state, choices, res)\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n *state = (*state)[:len(*state)-1]\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I */\nfunc subsetSumI(nums []int, target int) [][]int {\n state := make([]int, 0) // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n sort.Ints(nums) // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n start := 0 // \u904d\u5386\u8d77\u59cb\u70b9\n res := make([][]int, 0) // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n backtrackSubsetSumI(start, target, &state, &nums, &res)\n return res\n}\n
subset_sum_i.swift/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nfunc backtrack(state: inout [Int], target: Int, choices: [Int], start: Int, res: inout [[Int]]) {\n // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n if target == 0 {\n res.append(state)\n return\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n // \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n for i in stride(from: start, to: choices.count, by: 1) {\n // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n // \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n if target - choices[i] < 0 {\n break\n }\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n state.append(choices[i])\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state: &state, target: target - choices[i], choices: choices, start: i, res: &res)\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n state.removeLast()\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I */\nfunc subsetSumI(nums: [Int], target: Int) -> [[Int]] {\n var state: [Int] = [] // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n let nums = nums.sorted() // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n let start = 0 // \u904d\u5386\u8d77\u59cb\u70b9\n var res: [[Int]] = [] // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n backtrack(state: &state, target: target, choices: nums, start: start, res: &res)\n return res\n}\n
subset_sum_i.js/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nfunction backtrack(state, target, choices, start, res) {\n // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n if (target === 0) {\n res.push([...state]);\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n // \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n for (let i = start; i < choices.length; i++) {\n // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n // \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n if (target - choices[i] < 0) {\n break;\n }\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n state.push(choices[i]);\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state, target - choices[i], choices, i, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n state.pop();\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I */\nfunction subsetSumI(nums, target) {\n const state = []; // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n nums.sort((a, b) => a - b); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n const start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\n const res = []; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n backtrack(state, target, nums, start, res);\n return res;\n}\n
subset_sum_i.ts/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nfunction backtrack(\n state: number[],\n target: number,\n choices: number[],\n start: number,\n res: number[][]\n): void {\n // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n if (target === 0) {\n res.push([...state]);\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n // \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n for (let i = start; i < choices.length; i++) {\n // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n // \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n if (target - choices[i] < 0) {\n break;\n }\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n state.push(choices[i]);\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state, target - choices[i], choices, i, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n state.pop();\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I */\nfunction subsetSumI(nums: number[], target: number): number[][] {\n const state = []; // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n nums.sort((a, b) => a - b); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n const start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\n const res = []; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n backtrack(state, target, nums, start, res);\n return res;\n}\n
subset_sum_i.dart/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nvoid backtrack(\n List<int> state,\n int target,\n List<int> choices,\n int start,\n List<List<int>> res,\n) {\n // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n if (target == 0) {\n res.add(List.from(state));\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n // \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n for (int i = start; i < choices.length; i++) {\n // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n // \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n if (target - choices[i] < 0) {\n break;\n }\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n state.add(choices[i]);\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state, target - choices[i], choices, i, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n state.removeLast();\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I */\nList<List<int>> subsetSumI(List<int> nums, int target) {\n List<int> state = []; // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n nums.sort(); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n int start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\n List<List<int>> res = []; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n backtrack(state, target, nums, start, res);\n return res;\n}\n
subset_sum_i.rs/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nfn backtrack(mut state: Vec<i32>, target: i32, choices: &[i32], start: usize, res: &mut Vec<Vec<i32>>) {\n // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n if target == 0 {\n res.push(state);\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n // \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n for i in start..choices.len() {\n // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n // \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n if target - choices[i] < 0 {\n break;\n }\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n state.push(choices[i]);\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state.clone(), target - choices[i], choices, i, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n state.pop();\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I */\nfn subset_sum_i(nums: &mut [i32], target: i32) -> Vec<Vec<i32>> {\n let state = Vec::new(); // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n nums.sort(); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n let start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\n let mut res = Vec::new(); // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n backtrack(state, target, nums, start, &mut res);\n res\n}\n
subset_sum_i.c/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nvoid backtrack(int target, int *choices, int choicesSize, int start) {\n // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n if (target == 0) {\n for (int i = 0; i < stateSize; ++i) {\n res[resSize][i] = state[i];\n }\n resColSizes[resSize++] = stateSize;\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n // \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n for (int i = start; i < choicesSize; i++) {\n // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n // \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n if (target - choices[i] < 0) {\n break;\n }\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n state[stateSize] = choices[i];\n stateSize++;\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(target - choices[i], choices, choicesSize, i);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n stateSize--;\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I */\nvoid subsetSumI(int *nums, int numsSize, int target) {\n qsort(nums, numsSize, sizeof(int), cmp); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n int start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\n backtrack(target, nums, numsSize, start);\n}\n
subset_sum_i.zig[class]{}-[func]{backtrack}\n\n[class]{}-[func]{subsetSumI}\n
\u56fe 13-12 \u6240\u793a\u4e3a\u5c06\u6570\u7ec4 \\([3, 4, 5]\\) \u548c\u76ee\u6807\u5143\u7d20 \\(9\\) \u8f93\u5165\u4ee5\u4e0a\u4ee3\u7801\u540e\u7684\u6574\u4f53\u56de\u6eaf\u8fc7\u7a0b\u3002
\u56fe 13-12 \u00a0 \u5b50\u96c6\u548c I \u56de\u6eaf\u8fc7\u7a0b
"},{"location":"chapter_backtracking/subset_sum_problem/#1332","title":"13.3.2 \u00a0 \u8003\u8651\u91cd\u590d\u5143\u7d20\u7684\u60c5\u51b5","text":"Question
\u7ed9\u5b9a\u4e00\u4e2a\u6b63\u6574\u6570\u6570\u7ec4 nums
\u548c\u4e00\u4e2a\u76ee\u6807\u6b63\u6574\u6570 target
\uff0c\u8bf7\u627e\u51fa\u6240\u6709\u53ef\u80fd\u7684\u7ec4\u5408\uff0c\u4f7f\u5f97\u7ec4\u5408\u4e2d\u7684\u5143\u7d20\u548c\u7b49\u4e8e target
\u3002\u7ed9\u5b9a\u6570\u7ec4\u53ef\u80fd\u5305\u542b\u91cd\u590d\u5143\u7d20\uff0c\u6bcf\u4e2a\u5143\u7d20\u53ea\u53ef\u88ab\u9009\u62e9\u4e00\u6b21\u3002\u8bf7\u4ee5\u5217\u8868\u5f62\u5f0f\u8fd4\u56de\u8fd9\u4e9b\u7ec4\u5408\uff0c\u5217\u8868\u4e2d\u4e0d\u5e94\u5305\u542b\u91cd\u590d\u7ec4\u5408\u3002
\u76f8\u6bd4\u4e8e\u4e0a\u9898\uff0c\u672c\u9898\u7684\u8f93\u5165\u6570\u7ec4\u53ef\u80fd\u5305\u542b\u91cd\u590d\u5143\u7d20\uff0c\u8fd9\u5f15\u5165\u4e86\u65b0\u7684\u95ee\u9898\u3002\u4f8b\u5982\uff0c\u7ed9\u5b9a\u6570\u7ec4 \\([4, \\hat{4}, 5]\\) \u548c\u76ee\u6807\u5143\u7d20 \\(9\\) \uff0c\u5219\u73b0\u6709\u4ee3\u7801\u7684\u8f93\u51fa\u7ed3\u679c\u4e3a \\([4, 5], [\\hat{4}, 5]\\) \uff0c\u51fa\u73b0\u4e86\u91cd\u590d\u5b50\u96c6\u3002
\u9020\u6210\u8fd9\u79cd\u91cd\u590d\u7684\u539f\u56e0\u662f\u76f8\u7b49\u5143\u7d20\u5728\u67d0\u8f6e\u4e2d\u88ab\u591a\u6b21\u9009\u62e9\u3002\u5728\u56fe 13-13 \u4e2d\uff0c\u7b2c\u4e00\u8f6e\u5171\u6709\u4e09\u4e2a\u9009\u62e9\uff0c\u5176\u4e2d\u4e24\u4e2a\u90fd\u4e3a \\(4\\) \uff0c\u4f1a\u4ea7\u751f\u4e24\u4e2a\u91cd\u590d\u7684\u641c\u7d22\u5206\u652f\uff0c\u4ece\u800c\u8f93\u51fa\u91cd\u590d\u5b50\u96c6\uff1b\u540c\u7406\uff0c\u7b2c\u4e8c\u8f6e\u7684\u4e24\u4e2a \\(4\\) \u4e5f\u4f1a\u4ea7\u751f\u91cd\u590d\u5b50\u96c6\u3002
\u56fe 13-13 \u00a0 \u76f8\u7b49\u5143\u7d20\u5bfc\u81f4\u7684\u91cd\u590d\u5b50\u96c6
"},{"location":"chapter_backtracking/subset_sum_problem/#1_1","title":"1. \u00a0 \u76f8\u7b49\u5143\u7d20\u526a\u679d","text":"\u4e3a\u89e3\u51b3\u6b64\u95ee\u9898\uff0c\u6211\u4eec\u9700\u8981\u9650\u5236\u76f8\u7b49\u5143\u7d20\u5728\u6bcf\u4e00\u8f6e\u4e2d\u53ea\u80fd\u88ab\u9009\u62e9\u4e00\u6b21\u3002\u5b9e\u73b0\u65b9\u5f0f\u6bd4\u8f83\u5de7\u5999\uff1a\u7531\u4e8e\u6570\u7ec4\u662f\u5df2\u6392\u5e8f\u7684\uff0c\u56e0\u6b64\u76f8\u7b49\u5143\u7d20\u90fd\u662f\u76f8\u90bb\u7684\u3002\u8fd9\u610f\u5473\u7740\u5728\u67d0\u8f6e\u9009\u62e9\u4e2d\uff0c\u82e5\u5f53\u524d\u5143\u7d20\u4e0e\u5176\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u5219\u8bf4\u660e\u5b83\u5df2\u7ecf\u88ab\u9009\u62e9\u8fc7\uff0c\u56e0\u6b64\u76f4\u63a5\u8df3\u8fc7\u5f53\u524d\u5143\u7d20\u3002
\u4e0e\u6b64\u540c\u65f6\uff0c\u672c\u9898\u89c4\u5b9a\u6bcf\u4e2a\u6570\u7ec4\u5143\u7d20\u53ea\u80fd\u88ab\u9009\u62e9\u4e00\u6b21\u3002\u5e78\u8fd0\u7684\u662f\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u5229\u7528\u53d8\u91cf start
\u6765\u6ee1\u8db3\u8be5\u7ea6\u675f\uff1a\u5f53\u505a\u51fa\u9009\u62e9 \\(x_{i}\\) \u540e\uff0c\u8bbe\u5b9a\u4e0b\u4e00\u8f6e\u4ece\u7d22\u5f15 \\(i + 1\\) \u5f00\u59cb\u5411\u540e\u904d\u5386\u3002\u8fd9\u6837\u65e2\u80fd\u53bb\u9664\u91cd\u590d\u5b50\u96c6\uff0c\u4e5f\u80fd\u907f\u514d\u91cd\u590d\u9009\u62e9\u5143\u7d20\u3002
def backtrack(\n state: list[int], target: int, choices: list[int], start: int, res: list[list[int]]\n):\n \"\"\"\u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II\"\"\"\n # \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n if target == 0:\n res.append(list(state))\n return\n # \u904d\u5386\u6240\u6709\u9009\u62e9\n # \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n # \u526a\u679d\u4e09\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\n for i in range(start, len(choices)):\n # \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n # \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n if target - choices[i] < 0:\n break\n # \u526a\u679d\u56db\uff1a\u5982\u679c\u8be5\u5143\u7d20\u4e0e\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u8bf4\u660e\u8be5\u641c\u7d22\u5206\u652f\u91cd\u590d\uff0c\u76f4\u63a5\u8df3\u8fc7\n if i > start and choices[i] == choices[i - 1]:\n continue\n # \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n state.append(choices[i])\n # \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state, target - choices[i], choices, i + 1, res)\n # \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n state.pop()\n\ndef subset_sum_ii(nums: list[int], target: int) -> list[list[int]]:\n \"\"\"\u6c42\u89e3\u5b50\u96c6\u548c II\"\"\"\n state = [] # \u72b6\u6001\uff08\u5b50\u96c6\uff09\n nums.sort() # \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n start = 0 # \u904d\u5386\u8d77\u59cb\u70b9\n res = [] # \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n backtrack(state, target, nums, start, res)\n return res\n
subset_sum_ii.cpp/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II */\nvoid backtrack(vector<int> &state, int target, vector<int> &choices, int start, vector<vector<int>> &res) {\n // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n if (target == 0) {\n res.push_back(state);\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n // \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n // \u526a\u679d\u4e09\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\n for (int i = start; i < choices.size(); i++) {\n // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n // \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n if (target - choices[i] < 0) {\n break;\n }\n // \u526a\u679d\u56db\uff1a\u5982\u679c\u8be5\u5143\u7d20\u4e0e\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u8bf4\u660e\u8be5\u641c\u7d22\u5206\u652f\u91cd\u590d\uff0c\u76f4\u63a5\u8df3\u8fc7\n if (i > start && choices[i] == choices[i - 1]) {\n continue;\n }\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n state.push_back(choices[i]);\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state, target - choices[i], choices, i + 1, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n state.pop_back();\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c II */\nvector<vector<int>> subsetSumII(vector<int> &nums, int target) {\n vector<int> state; // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n sort(nums.begin(), nums.end()); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n int start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\n vector<vector<int>> res; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n backtrack(state, target, nums, start, res);\n return res;\n}\n
subset_sum_ii.java/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II */\nvoid backtrack(List<Integer> state, int target, int[] choices, int start, List<List<Integer>> res) {\n // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n if (target == 0) {\n res.add(new ArrayList<>(state));\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n // \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n // \u526a\u679d\u4e09\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\n for (int i = start; i < choices.length; i++) {\n // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n // \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n if (target - choices[i] < 0) {\n break;\n }\n // \u526a\u679d\u56db\uff1a\u5982\u679c\u8be5\u5143\u7d20\u4e0e\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u8bf4\u660e\u8be5\u641c\u7d22\u5206\u652f\u91cd\u590d\uff0c\u76f4\u63a5\u8df3\u8fc7\n if (i > start && choices[i] == choices[i - 1]) {\n continue;\n }\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n state.add(choices[i]);\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state, target - choices[i], choices, i + 1, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n state.remove(state.size() - 1);\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c II */\nList<List<Integer>> subsetSumII(int[] nums, int target) {\n List<Integer> state = new ArrayList<>(); // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n Arrays.sort(nums); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n int start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\n List<List<Integer>> res = new ArrayList<>(); // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n backtrack(state, target, nums, start, res);\n return res;\n}\n
subset_sum_ii.cs/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II */\nvoid Backtrack(List<int> state, int target, int[] choices, int start, List<List<int>> res) {\n // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n if (target == 0) {\n res.Add(new List<int>(state));\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n // \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n // \u526a\u679d\u4e09\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\n for (int i = start; i < choices.Length; i++) {\n // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n // \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n if (target - choices[i] < 0) {\n break;\n }\n // \u526a\u679d\u56db\uff1a\u5982\u679c\u8be5\u5143\u7d20\u4e0e\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u8bf4\u660e\u8be5\u641c\u7d22\u5206\u652f\u91cd\u590d\uff0c\u76f4\u63a5\u8df3\u8fc7\n if (i > start && choices[i] == choices[i - 1]) {\n continue;\n }\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n state.Add(choices[i]);\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n Backtrack(state, target - choices[i], choices, i + 1, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n state.RemoveAt(state.Count - 1);\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c II */\nList<List<int>> SubsetSumII(int[] nums, int target) {\n List<int> state = []; // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n Array.Sort(nums); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n int start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\n List<List<int>> res = []; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n Backtrack(state, target, nums, start, res);\n return res;\n}\n
subset_sum_ii.go/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II */\nfunc backtrackSubsetSumII(start, target int, state, choices *[]int, res *[][]int) {\n // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n if target == 0 {\n newState := append([]int{}, *state...)\n *res = append(*res, newState)\n return\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n // \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n // \u526a\u679d\u4e09\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\n for i := start; i < len(*choices); i++ {\n // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n // \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n if target-(*choices)[i] < 0 {\n break\n }\n // \u526a\u679d\u56db\uff1a\u5982\u679c\u8be5\u5143\u7d20\u4e0e\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u8bf4\u660e\u8be5\u641c\u7d22\u5206\u652f\u91cd\u590d\uff0c\u76f4\u63a5\u8df3\u8fc7\n if i > start && (*choices)[i] == (*choices)[i-1] {\n continue\n }\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n *state = append(*state, (*choices)[i])\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrackSubsetSumII(i+1, target-(*choices)[i], state, choices, res)\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n *state = (*state)[:len(*state)-1]\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c II */\nfunc subsetSumII(nums []int, target int) [][]int {\n state := make([]int, 0) // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n sort.Ints(nums) // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n start := 0 // \u904d\u5386\u8d77\u59cb\u70b9\n res := make([][]int, 0) // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n backtrackSubsetSumII(start, target, &state, &nums, &res)\n return res\n}\n
subset_sum_ii.swift/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II */\nfunc backtrack(state: inout [Int], target: Int, choices: [Int], start: Int, res: inout [[Int]]) {\n // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n if target == 0 {\n res.append(state)\n return\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n // \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n // \u526a\u679d\u4e09\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\n for i in stride(from: start, to: choices.count, by: 1) {\n // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n // \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n if target - choices[i] < 0 {\n break\n }\n // \u526a\u679d\u56db\uff1a\u5982\u679c\u8be5\u5143\u7d20\u4e0e\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u8bf4\u660e\u8be5\u641c\u7d22\u5206\u652f\u91cd\u590d\uff0c\u76f4\u63a5\u8df3\u8fc7\n if i > start, choices[i] == choices[i - 1] {\n continue\n }\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n state.append(choices[i])\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state: &state, target: target - choices[i], choices: choices, start: i + 1, res: &res)\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n state.removeLast()\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c II */\nfunc subsetSumII(nums: [Int], target: Int) -> [[Int]] {\n var state: [Int] = [] // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n let nums = nums.sorted() // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n let start = 0 // \u904d\u5386\u8d77\u59cb\u70b9\n var res: [[Int]] = [] // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n backtrack(state: &state, target: target, choices: nums, start: start, res: &res)\n return res\n}\n
subset_sum_ii.js/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II */\nfunction backtrack(state, target, choices, start, res) {\n // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n if (target === 0) {\n res.push([...state]);\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n // \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n // \u526a\u679d\u4e09\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\n for (let i = start; i < choices.length; i++) {\n // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n // \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n if (target - choices[i] < 0) {\n break;\n }\n // \u526a\u679d\u56db\uff1a\u5982\u679c\u8be5\u5143\u7d20\u4e0e\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u8bf4\u660e\u8be5\u641c\u7d22\u5206\u652f\u91cd\u590d\uff0c\u76f4\u63a5\u8df3\u8fc7\n if (i > start && choices[i] === choices[i - 1]) {\n continue;\n }\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n state.push(choices[i]);\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state, target - choices[i], choices, i + 1, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n state.pop();\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c II */\nfunction subsetSumII(nums, target) {\n const state = []; // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n nums.sort((a, b) => a - b); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n const start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\n const res = []; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n backtrack(state, target, nums, start, res);\n return res;\n}\n
subset_sum_ii.ts/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II */\nfunction backtrack(\n state: number[],\n target: number,\n choices: number[],\n start: number,\n res: number[][]\n): void {\n // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n if (target === 0) {\n res.push([...state]);\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n // \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n // \u526a\u679d\u4e09\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\n for (let i = start; i < choices.length; i++) {\n // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n // \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n if (target - choices[i] < 0) {\n break;\n }\n // \u526a\u679d\u56db\uff1a\u5982\u679c\u8be5\u5143\u7d20\u4e0e\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u8bf4\u660e\u8be5\u641c\u7d22\u5206\u652f\u91cd\u590d\uff0c\u76f4\u63a5\u8df3\u8fc7\n if (i > start && choices[i] === choices[i - 1]) {\n continue;\n }\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n state.push(choices[i]);\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state, target - choices[i], choices, i + 1, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n state.pop();\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c II */\nfunction subsetSumII(nums: number[], target: number): number[][] {\n const state = []; // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n nums.sort((a, b) => a - b); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n const start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\n const res = []; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n backtrack(state, target, nums, start, res);\n return res;\n}\n
subset_sum_ii.dart/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II */\nvoid backtrack(\n List<int> state,\n int target,\n List<int> choices,\n int start,\n List<List<int>> res,\n) {\n // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n if (target == 0) {\n res.add(List.from(state));\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n // \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n // \u526a\u679d\u4e09\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\n for (int i = start; i < choices.length; i++) {\n // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n // \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n if (target - choices[i] < 0) {\n break;\n }\n // \u526a\u679d\u56db\uff1a\u5982\u679c\u8be5\u5143\u7d20\u4e0e\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u8bf4\u660e\u8be5\u641c\u7d22\u5206\u652f\u91cd\u590d\uff0c\u76f4\u63a5\u8df3\u8fc7\n if (i > start && choices[i] == choices[i - 1]) {\n continue;\n }\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n state.add(choices[i]);\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state, target - choices[i], choices, i + 1, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n state.removeLast();\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c II */\nList<List<int>> subsetSumII(List<int> nums, int target) {\n List<int> state = []; // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n nums.sort(); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n int start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\n List<List<int>> res = []; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n backtrack(state, target, nums, start, res);\n return res;\n}\n
subset_sum_ii.rs/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II */\nfn backtrack(mut state: Vec<i32>, target: i32, choices: &[i32], start: usize, res: &mut Vec<Vec<i32>>) {\n // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n if target == 0 {\n res.push(state);\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n // \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n // \u526a\u679d\u4e09\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\n for i in start..choices.len() {\n // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n // \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\n if target - choices[i] < 0 {\n break;\n }\n // \u526a\u679d\u56db\uff1a\u5982\u679c\u8be5\u5143\u7d20\u4e0e\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u8bf4\u660e\u8be5\u641c\u7d22\u5206\u652f\u91cd\u590d\uff0c\u76f4\u63a5\u8df3\u8fc7\n if i > start && choices[i] == choices[i - 1] {\n continue;\n }\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n state.push(choices[i]);\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(state.clone(), target - choices[i], choices, i, res);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n state.pop();\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c II */\nfn subset_sum_ii(nums: &mut [i32], target: i32) -> Vec<Vec<i32>> {\n let state = Vec::new(); // \u72b6\u6001\uff08\u5b50\u96c6\uff09\n nums.sort(); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n let start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\n let mut res = Vec::new(); // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\n backtrack(state, target, nums, start, &mut res);\n res\n}\n
subset_sum_ii.c/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II */\nvoid backtrack(int target, int *choices, int choicesSize, int start) {\n // \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\n if (target == 0) {\n for (int i = 0; i < stateSize; i++) {\n res[resSize][i] = state[i];\n }\n resColSizes[resSize++] = stateSize;\n return;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n // \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n // \u526a\u679d\u4e09\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\n for (int i = start; i < choicesSize; i++) {\n // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\n if (target - choices[i] < 0) {\n continue;\n }\n // \u526a\u679d\u56db\uff1a\u5982\u679c\u8be5\u5143\u7d20\u4e0e\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u8bf4\u660e\u8be5\u641c\u7d22\u5206\u652f\u91cd\u590d\uff0c\u76f4\u63a5\u8df3\u8fc7\n if (i > start && choices[i] == choices[i - 1]) {\n continue;\n }\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n state[stateSize] = choices[i];\n stateSize++;\n // \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\n backtrack(target - choices[i], choices, choicesSize, i + 1);\n // \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n stateSize--;\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c II */\nvoid subsetSumII(int *nums, int numsSize, int target) {\n // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\n qsort(nums, numsSize, sizeof(int), cmp);\n // \u5f00\u59cb\u56de\u6eaf\n backtrack(target, nums, numsSize, 0);\n}\n
subset_sum_ii.zig[class]{}-[func]{backtrack}\n\n[class]{}-[func]{subsetSumII}\n
\u56fe 13-14 \u5c55\u793a\u4e86\u6570\u7ec4 \\([4, 4, 5]\\) \u548c\u76ee\u6807\u5143\u7d20 \\(9\\) \u7684\u56de\u6eaf\u8fc7\u7a0b\uff0c\u5171\u5305\u542b\u56db\u79cd\u526a\u679d\u64cd\u4f5c\u3002\u8bf7\u4f60\u5c06\u56fe\u793a\u4e0e\u4ee3\u7801\u6ce8\u91ca\u76f8\u7ed3\u5408\uff0c\u7406\u89e3\u6574\u4e2a\u641c\u7d22\u8fc7\u7a0b\uff0c\u4ee5\u53ca\u6bcf\u79cd\u526a\u679d\u64cd\u4f5c\u662f\u5982\u4f55\u5de5\u4f5c\u7684\u3002
\u56fe 13-14 \u00a0 \u5b50\u96c6\u548c II \u56de\u6eaf\u8fc7\u7a0b
"},{"location":"chapter_backtracking/summary/","title":"13.5 \u00a0 \u5c0f\u7ed3","text":""},{"location":"chapter_backtracking/summary/#1","title":"1. \u00a0 \u91cd\u70b9\u56de\u987e","text":"\u600e\u4e48\u7406\u89e3\u56de\u6eaf\u548c\u9012\u5f52\u7684\u5173\u7cfb\uff1f
\u603b\u7684\u6765\u770b\uff0c\u56de\u6eaf\u662f\u4e00\u79cd\u201c\u7b97\u6cd5\u7b56\u7565\u201d\uff0c\u800c\u9012\u5f52\u66f4\u50cf\u662f\u4e00\u4e2a\u201c\u5de5\u5177\u201d\u3002
Abstract
\u590d\u6742\u5ea6\u5206\u6790\u72b9\u5982\u6d69\u701a\u7684\u7b97\u6cd5\u5b87\u5b99\u4e2d\u7684\u65f6\u7a7a\u5411\u5bfc\u3002
\u5b83\u5e26\u9886\u6211\u4eec\u5728\u65f6\u95f4\u4e0e\u7a7a\u95f4\u8fd9\u4e24\u4e2a\u7ef4\u5ea6\u4e0a\u6df1\u5165\u63a2\u7d22\uff0c\u5bfb\u627e\u66f4\u4f18\u96c5\u7684\u89e3\u51b3\u65b9\u6848\u3002
"},{"location":"chapter_computational_complexity/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"\u5728\u7b97\u6cd5\u4e2d\uff0c\u91cd\u590d\u6267\u884c\u67d0\u4e2a\u4efb\u52a1\u662f\u5f88\u5e38\u89c1\u7684\uff0c\u5b83\u4e0e\u590d\u6742\u5ea6\u5206\u6790\u606f\u606f\u76f8\u5173\u3002\u56e0\u6b64\uff0c\u5728\u4ecb\u7ecd\u65f6\u95f4\u590d\u6742\u5ea6\u548c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e4b\u524d\uff0c\u6211\u4eec\u5148\u6765\u4e86\u89e3\u5982\u4f55\u5728\u7a0b\u5e8f\u4e2d\u5b9e\u73b0\u91cd\u590d\u6267\u884c\u4efb\u52a1\uff0c\u5373\u4e24\u79cd\u57fa\u672c\u7684\u7a0b\u5e8f\u63a7\u5236\u7ed3\u6784\uff1a\u8fed\u4ee3\u3001\u9012\u5f52\u3002
"},{"location":"chapter_computational_complexity/iteration_and_recursion/#221","title":"2.2.1 \u00a0 \u8fed\u4ee3","text":"\u300c\u8fed\u4ee3 iteration\u300d\u662f\u4e00\u79cd\u91cd\u590d\u6267\u884c\u67d0\u4e2a\u4efb\u52a1\u7684\u63a7\u5236\u7ed3\u6784\u3002\u5728\u8fed\u4ee3\u4e2d\uff0c\u7a0b\u5e8f\u4f1a\u5728\u6ee1\u8db3\u4e00\u5b9a\u7684\u6761\u4ef6\u4e0b\u91cd\u590d\u6267\u884c\u67d0\u6bb5\u4ee3\u7801\uff0c\u76f4\u5230\u8fd9\u4e2a\u6761\u4ef6\u4e0d\u518d\u6ee1\u8db3\u3002
"},{"location":"chapter_computational_complexity/iteration_and_recursion/#1-for","title":"1. \u00a0 for \u5faa\u73af","text":"for
\u5faa\u73af\u662f\u6700\u5e38\u89c1\u7684\u8fed\u4ee3\u5f62\u5f0f\u4e4b\u4e00\uff0c\u9002\u5408\u5728\u9884\u5148\u77e5\u9053\u8fed\u4ee3\u6b21\u6570\u65f6\u4f7f\u7528\u3002
\u4ee5\u4e0b\u51fd\u6570\u57fa\u4e8e for
\u5faa\u73af\u5b9e\u73b0\u4e86\u6c42\u548c \\(1 + 2 + \\dots + n\\) \uff0c\u6c42\u548c\u7ed3\u679c\u4f7f\u7528\u53d8\u91cf res
\u8bb0\u5f55\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0cPython \u4e2d range(a, b)
\u5bf9\u5e94\u7684\u533a\u95f4\u662f\u201c\u5de6\u95ed\u53f3\u5f00\u201d\u7684\uff0c\u5bf9\u5e94\u7684\u904d\u5386\u8303\u56f4\u4e3a \\(a, a + 1, \\dots, b-1\\) \uff1a
def for_loop(n: int) -> int:\n \"\"\"for \u5faa\u73af\"\"\"\n res = 0\n # \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n for i in range(1, n + 1):\n res += i\n return res\n
iteration.cpp/* for \u5faa\u73af */\nint forLoop(int n) {\n int res = 0;\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n for (int i = 1; i <= n; ++i) {\n res += i;\n }\n return res;\n}\n
iteration.java/* for \u5faa\u73af */\nint forLoop(int n) {\n int res = 0;\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n for (int i = 1; i <= n; i++) {\n res += i;\n }\n return res;\n}\n
iteration.cs/* for \u5faa\u73af */\nint ForLoop(int n) {\n int res = 0;\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n for (int i = 1; i <= n; i++) {\n res += i;\n }\n return res;\n}\n
iteration.go/* for \u5faa\u73af */\nfunc forLoop(n int) int {\n res := 0\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n for i := 1; i <= n; i++ {\n res += i\n }\n return res\n}\n
iteration.swift/* for \u5faa\u73af */\nfunc forLoop(n: Int) -> Int {\n var res = 0\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n for i in 1 ... n {\n res += i\n }\n return res\n}\n
iteration.js/* for \u5faa\u73af */\nfunction forLoop(n) {\n let res = 0;\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n for (let i = 1; i <= n; i++) {\n res += i;\n }\n return res;\n}\n
iteration.ts/* for \u5faa\u73af */\nfunction forLoop(n: number): number {\n let res = 0;\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n for (let i = 1; i <= n; i++) {\n res += i;\n }\n return res;\n}\n
iteration.dart/* for \u5faa\u73af */\nint forLoop(int n) {\n int res = 0;\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n for (int i = 1; i <= n; i++) {\n res += i;\n }\n return res;\n}\n
iteration.rs/* for \u5faa\u73af */\nfn for_loop(n: i32) -> i32 {\n let mut res = 0;\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n for i in 1..=n {\n res += i;\n }\n res\n} \n
iteration.c/* for \u5faa\u73af */\nint forLoop(int n) {\n int res = 0;\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n for (int i = 1; i <= n; i++) {\n res += i;\n }\n return res;\n}\n
iteration.zig// for \u5faa\u73af\nfn forLoop(n: usize) i32 {\n var res: i32 = 0;\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n for (1..n+1) |i| {\n res = res + @as(i32, @intCast(i));\n }\n return res;\n} \n
\u56fe 2-1 \u662f\u8be5\u6c42\u548c\u51fd\u6570\u7684\u6d41\u7a0b\u6846\u56fe\u3002
\u56fe 2-1 \u00a0 \u6c42\u548c\u51fd\u6570\u7684\u6d41\u7a0b\u6846\u56fe
\u6b64\u6c42\u548c\u51fd\u6570\u7684\u64cd\u4f5c\u6570\u91cf\u4e0e\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u6210\u6b63\u6bd4\uff0c\u6216\u8005\u8bf4\u6210\u201c\u7ebf\u6027\u5173\u7cfb\u201d\u3002\u5b9e\u9645\u4e0a\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u63cf\u8ff0\u7684\u5c31\u662f\u8fd9\u4e2a\u201c\u7ebf\u6027\u5173\u7cfb\u201d\u3002\u76f8\u5173\u5185\u5bb9\u5c06\u4f1a\u5728\u4e0b\u4e00\u8282\u4e2d\u8be6\u7ec6\u4ecb\u7ecd\u3002
"},{"location":"chapter_computational_complexity/iteration_and_recursion/#2-while","title":"2. \u00a0 while \u5faa\u73af","text":"\u4e0e for
\u5faa\u73af\u7c7b\u4f3c\uff0cwhile
\u5faa\u73af\u4e5f\u662f\u4e00\u79cd\u5b9e\u73b0\u8fed\u4ee3\u7684\u65b9\u6cd5\u3002\u5728 while
\u5faa\u73af\u4e2d\uff0c\u7a0b\u5e8f\u6bcf\u8f6e\u90fd\u4f1a\u5148\u68c0\u67e5\u6761\u4ef6\uff0c\u5982\u679c\u6761\u4ef6\u4e3a\u771f\uff0c\u5219\u7ee7\u7eed\u6267\u884c\uff0c\u5426\u5219\u5c31\u7ed3\u675f\u5faa\u73af\u3002
\u4e0b\u9762\u6211\u4eec\u7528 while
\u5faa\u73af\u6765\u5b9e\u73b0\u6c42\u548c \\(1 + 2 + \\dots + n\\) \uff1a
def while_loop(n: int) -> int:\n \"\"\"while \u5faa\u73af\"\"\"\n res = 0\n i = 1 # \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n # \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n while i <= n:\n res += i\n i += 1 # \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n return res\n
iteration.cpp/* while \u5faa\u73af */\nint whileLoop(int n) {\n int res = 0;\n int i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n while (i <= n) {\n res += i;\n i++; // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n }\n return res;\n}\n
iteration.java/* while \u5faa\u73af */\nint whileLoop(int n) {\n int res = 0;\n int i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n while (i <= n) {\n res += i;\n i++; // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n }\n return res;\n}\n
iteration.cs/* while \u5faa\u73af */\nint WhileLoop(int n) {\n int res = 0;\n int i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n while (i <= n) {\n res += i;\n i += 1; // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n }\n return res;\n}\n
iteration.go/* while \u5faa\u73af */\nfunc whileLoop(n int) int {\n res := 0\n // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n i := 1\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n for i <= n {\n res += i\n // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n i++\n }\n return res\n}\n
iteration.swift/* while \u5faa\u73af */\nfunc whileLoop(n: Int) -> Int {\n var res = 0\n var i = 1 // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n while i <= n {\n res += i\n i += 1 // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n }\n return res\n}\n
iteration.js/* while \u5faa\u73af */\nfunction whileLoop(n) {\n let res = 0;\n let i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n while (i <= n) {\n res += i;\n i++; // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n }\n return res;\n}\n
iteration.ts/* while \u5faa\u73af */\nfunction whileLoop(n: number): number {\n let res = 0;\n let i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n while (i <= n) {\n res += i;\n i++; // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n }\n return res;\n}\n
iteration.dart/* while \u5faa\u73af */\nint whileLoop(int n) {\n int res = 0;\n int i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n while (i <= n) {\n res += i;\n i++; // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n }\n return res;\n}\n
iteration.rs/* while \u5faa\u73af */\nfn while_loop(n: i32) -> i32 {\n let mut res = 0;\n let mut i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n while i <= n {\n res += i;\n i += 1; // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n }\n res\n}\n
iteration.c/* while \u5faa\u73af */\nint whileLoop(int n) {\n int res = 0;\n int i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n while (i <= n) {\n res += i;\n i++; // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n }\n return res;\n}\n
iteration.zig// while \u5faa\u73af\nfn whileLoop(n: i32) i32 {\n var res: i32 = 0;\n var i: i32 = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n while (i <= n) {\n res += @intCast(i);\n i += 1;\n }\n return res;\n}\n
while
\u5faa\u73af\u6bd4 for
\u5faa\u73af\u7684\u81ea\u7531\u5ea6\u66f4\u9ad8\u3002\u5728 while
\u5faa\u73af\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u81ea\u7531\u5730\u8bbe\u8ba1\u6761\u4ef6\u53d8\u91cf\u7684\u521d\u59cb\u5316\u548c\u66f4\u65b0\u6b65\u9aa4\u3002
\u4f8b\u5982\u5728\u4ee5\u4e0b\u4ee3\u7801\u4e2d\uff0c\u6761\u4ef6\u53d8\u91cf \\(i\\) \u6bcf\u8f6e\u8fdb\u884c\u4e24\u6b21\u66f4\u65b0\uff0c\u8fd9\u79cd\u60c5\u51b5\u5c31\u4e0d\u592a\u65b9\u4fbf\u7528 for
\u5faa\u73af\u5b9e\u73b0\uff1a
def while_loop_ii(n: int) -> int:\n \"\"\"while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09\"\"\"\n res = 0\n i = 1 # \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n # \u5faa\u73af\u6c42\u548c 1, 4, 10, ...\n while i <= n:\n res += i\n # \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n i += 1\n i *= 2\n return res\n
iteration.cpp/* while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09 */\nint whileLoopII(int n) {\n int res = 0;\n int i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n // \u5faa\u73af\u6c42\u548c 1, 4, 10, ...\n while (i <= n) {\n res += i;\n // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n i++;\n i *= 2;\n }\n return res;\n}\n
iteration.java/* while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09 */\nint whileLoopII(int n) {\n int res = 0;\n int i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n // \u5faa\u73af\u6c42\u548c 1, 4, 10, ...\n while (i <= n) {\n res += i;\n // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n i++;\n i *= 2;\n }\n return res;\n}\n
iteration.cs/* while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09 */\nint WhileLoopII(int n) {\n int res = 0;\n int i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n // \u5faa\u73af\u6c42\u548c 1, 2, 4, 5...\n while (i <= n) {\n res += i;\n // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n i += 1; \n i *= 2;\n }\n return res;\n}\n
iteration.go/* while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09 */\nfunc whileLoopII(n int) int {\n res := 0\n // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n i := 1\n // \u5faa\u73af\u6c42\u548c 1, 4, 10, ...\n for i <= n {\n res += i\n // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n i++\n i *= 2\n }\n return res\n}\n
iteration.swift/* while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09 */\nfunc whileLoopII(n: Int) -> Int {\n var res = 0\n var i = 1 // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n // \u5faa\u73af\u6c42\u548c 1, 4, 10, ...\n while i <= n {\n res += i\n // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n i += 1\n i *= 2\n }\n return res\n}\n
iteration.js/* while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09 */\nfunction whileLoopII(n) {\n let res = 0;\n let i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n // \u5faa\u73af\u6c42\u548c 1, 4, 10, ...\n while (i <= n) {\n res += i;\n // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n i++;\n i *= 2;\n }\n return res;\n}\n
iteration.ts/* while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09 */\nfunction whileLoopII(n: number): number {\n let res = 0;\n let i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n // \u5faa\u73af\u6c42\u548c 1, 4, 10, ...\n while (i <= n) {\n res += i;\n // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n i++;\n i *= 2;\n }\n return res;\n}\n
iteration.dart/* while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09 */\nint whileLoopII(int n) {\n int res = 0;\n int i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n // \u5faa\u73af\u6c42\u548c 1, 4, 10, ...\n while (i <= n) {\n res += i;\n // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n i++;\n i *= 2;\n }\n return res;\n}\n
iteration.rs/* while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09 */\nfn while_loop_ii(n: i32) -> i32 {\n let mut res = 0;\n let mut i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n // \u5faa\u73af\u6c42\u548c 1, 4, 10, ...\n while i <= n {\n res += i;\n // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n i += 1;\n i *= 2;\n }\n res\n}\n
iteration.c/* while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09 */\nint whileLoopII(int n) {\n int res = 0;\n int i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n // \u5faa\u73af\u6c42\u548c 1, 4, 10, ...\n while (i <= n) {\n res += i;\n // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n i++;\n i *= 2;\n }\n return res;\n}\n
iteration.zig// while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09\nfn whileLoopII(n: i32) i32 {\n var res: i32 = 0;\n var i: i32 = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n // \u5faa\u73af\u6c42\u548c 1, 4, 10, ...\n while (i <= n) {\n res += @intCast(i);\n // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n i += 1;\n i *= 2;\n }\n return res;\n}\n
\u603b\u7684\u6765\u8bf4\uff0cfor
\u5faa\u73af\u7684\u4ee3\u7801\u66f4\u52a0\u7d27\u51d1\uff0cwhile
\u5faa\u73af\u66f4\u52a0\u7075\u6d3b\uff0c\u4e24\u8005\u90fd\u53ef\u4ee5\u5b9e\u73b0\u8fed\u4ee3\u7ed3\u6784\u3002\u9009\u62e9\u4f7f\u7528\u54ea\u4e00\u4e2a\u5e94\u8be5\u6839\u636e\u7279\u5b9a\u95ee\u9898\u7684\u9700\u6c42\u6765\u51b3\u5b9a\u3002
\u6211\u4eec\u53ef\u4ee5\u5728\u4e00\u4e2a\u5faa\u73af\u7ed3\u6784\u5185\u5d4c\u5957\u53e6\u4e00\u4e2a\u5faa\u73af\u7ed3\u6784\uff0c\u4e0b\u9762\u4ee5 for
\u5faa\u73af\u4e3a\u4f8b\uff1a
def nested_for_loop(n: int) -> str:\n \"\"\"\u53cc\u5c42 for \u5faa\u73af\"\"\"\n res = \"\"\n # \u5faa\u73af i = 1, 2, ..., n-1, n\n for i in range(1, n + 1):\n # \u5faa\u73af j = 1, 2, ..., n-1, n\n for j in range(1, n + 1):\n res += f\"({i}, {j}), \"\n return res\n
iteration.cpp/* \u53cc\u5c42 for \u5faa\u73af */\nstring nestedForLoop(int n) {\n ostringstream res;\n // \u5faa\u73af i = 1, 2, ..., n-1, n\n for (int i = 1; i <= n; ++i) {\n // \u5faa\u73af j = 1, 2, ..., n-1, n\n for (int j = 1; j <= n; ++j) {\n res << \"(\" << i << \", \" << j << \"), \";\n }\n }\n return res.str();\n}\n
iteration.java/* \u53cc\u5c42 for \u5faa\u73af */\nString nestedForLoop(int n) {\n StringBuilder res = new StringBuilder();\n // \u5faa\u73af i = 1, 2, ..., n-1, n\n for (int i = 1; i <= n; i++) {\n // \u5faa\u73af j = 1, 2, ..., n-1, n\n for (int j = 1; j <= n; j++) {\n res.append(\"(\" + i + \", \" + j + \"), \");\n }\n }\n return res.toString();\n}\n
iteration.cs/* \u53cc\u5c42 for \u5faa\u73af */\nstring NestedForLoop(int n) {\n StringBuilder res = new();\n // \u5faa\u73af i = 1, 2, ..., n-1, n\n for (int i = 1; i <= n; i++) {\n // \u5faa\u73af j = 1, 2, ..., n-1, n\n for (int j = 1; j <= n; j++) {\n res.Append($\"({i}, {j}), \");\n }\n }\n return res.ToString();\n}\n
iteration.go/* \u53cc\u5c42 for \u5faa\u73af */\nfunc nestedForLoop(n int) string {\n res := \"\"\n // \u5faa\u73af i = 1, 2, ..., n-1, n\n for i := 1; i <= n; i++ {\n for j := 1; j <= n; j++ {\n // \u5faa\u73af j = 1, 2, ..., n-1, n\n res += fmt.Sprintf(\"(%d, %d), \", i, j)\n }\n }\n return res\n}\n
iteration.swift/* \u53cc\u5c42 for \u5faa\u73af */\nfunc nestedForLoop(n: Int) -> String {\n var res = \"\"\n // \u5faa\u73af i = 1, 2, ..., n-1, n\n for i in 1 ... n {\n // \u5faa\u73af j = 1, 2, ..., n-1, n\n for j in 1 ... n {\n res.append(\"(\\(i), \\(j)), \")\n }\n }\n return res\n}\n
iteration.js/* \u53cc\u5c42 for \u5faa\u73af */\nfunction nestedForLoop(n) {\n let res = '';\n // \u5faa\u73af i = 1, 2, ..., n-1, n\n for (let i = 1; i <= n; i++) {\n // \u5faa\u73af j = 1, 2, ..., n-1, n\n for (let j = 1; j <= n; j++) {\n res += `(${i}, ${j}), `;\n }\n }\n return res;\n}\n
iteration.ts/* \u53cc\u5c42 for \u5faa\u73af */\nfunction nestedForLoop(n: number): string {\n let res = '';\n // \u5faa\u73af i = 1, 2, ..., n-1, n\n for (let i = 1; i <= n; i++) {\n // \u5faa\u73af j = 1, 2, ..., n-1, n\n for (let j = 1; j <= n; j++) {\n res += `(${i}, ${j}), `;\n }\n }\n return res;\n}\n
iteration.dart/* \u53cc\u5c42 for \u5faa\u73af */\nString nestedForLoop(int n) {\n String res = \"\";\n // \u5faa\u73af i = 1, 2, ..., n-1, n\n for (int i = 1; i <= n; i++) {\n // \u5faa\u73af j = 1, 2, ..., n-1, n\n for (int j = 1; j <= n; j++) {\n res += \"($i, $j), \";\n }\n }\n return res;\n}\n
iteration.rs/* \u53cc\u5c42 for \u5faa\u73af */\nfn nested_for_loop(n: i32) -> String {\n let mut res = vec![];\n // \u5faa\u73af i = 1, 2, ..., n-1, n\n for i in 1..=n {\n // \u5faa\u73af j = 1, 2, ..., n-1, n\n for j in 1..=n {\n res.push(format!(\"({}, {}), \", i, j));\n }\n }\n res.join(\"\")\n}\n
iteration.c/* \u53cc\u5c42 for \u5faa\u73af */\nchar *nestedForLoop(int n) {\n // n * n \u4e3a\u5bf9\u5e94\u70b9\u6570\u91cf\uff0c\"(i, j), \" \u5bf9\u5e94\u5b57\u7b26\u4e32\u957f\u6700\u5927\u4e3a 6+10*2\uff0c\u52a0\u4e0a\u6700\u540e\u4e00\u4e2a\u7a7a\u5b57\u7b26 \\0 \u7684\u989d\u5916\u7a7a\u95f4\n int size = n * n * 26 + 1;\n char *res = malloc(size * sizeof(char));\n // \u5faa\u73af i = 1, 2, ..., n-1, n\n for (int i = 1; i <= n; i++) {\n // \u5faa\u73af j = 1, 2, ..., n-1, n\n for (int j = 1; j <= n; j++) {\n char tmp[26];\n snprintf(tmp, sizeof(tmp), \"(%d, %d), \", i, j);\n strncat(res, tmp, size - strlen(res) - 1);\n }\n }\n return res;\n}\n
iteration.zig// \u53cc\u5c42 for \u5faa\u73af\nfn nestedForLoop(allocator: Allocator, n: usize) ![]const u8 {\n var res = std.ArrayList(u8).init(allocator);\n defer res.deinit();\n var buffer: [20]u8 = undefined;\n // \u5faa\u73af i = 1, 2, ..., n-1, n\n for (1..n+1) |i| {\n // \u5faa\u73af j = 1, 2, ..., n-1, n\n for (1..n+1) |j| {\n var _str = try std.fmt.bufPrint(&buffer, \"({d}, {d}), \", .{i, j});\n try res.appendSlice(_str);\n }\n }\n return res.toOwnedSlice();\n}\n
\u56fe 2-2 \u662f\u8be5\u5d4c\u5957\u5faa\u73af\u7684\u6d41\u7a0b\u6846\u56fe\u3002
\u56fe 2-2 \u00a0 \u5d4c\u5957\u5faa\u73af\u7684\u6d41\u7a0b\u6846\u56fe
\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u51fd\u6570\u7684\u64cd\u4f5c\u6570\u91cf\u4e0e \\(n^2\\) \u6210\u6b63\u6bd4\uff0c\u6216\u8005\u8bf4\u7b97\u6cd5\u8fd0\u884c\u65f6\u95f4\u548c\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u6210\u201c\u5e73\u65b9\u5173\u7cfb\u201d\u3002
\u6211\u4eec\u53ef\u4ee5\u7ee7\u7eed\u6dfb\u52a0\u5d4c\u5957\u5faa\u73af\uff0c\u6bcf\u4e00\u6b21\u5d4c\u5957\u90fd\u662f\u4e00\u6b21\u201c\u5347\u7ef4\u201d\uff0c\u5c06\u4f1a\u4f7f\u65f6\u95f4\u590d\u6742\u5ea6\u63d0\u9ad8\u81f3\u201c\u7acb\u65b9\u5173\u7cfb\u201d\u201c\u56db\u6b21\u65b9\u5173\u7cfb\u201d\uff0c\u4ee5\u6b64\u7c7b\u63a8\u3002
"},{"location":"chapter_computational_complexity/iteration_and_recursion/#222","title":"2.2.2 \u00a0 \u9012\u5f52","text":"\u300c\u9012\u5f52 recursion\u300d\u662f\u4e00\u79cd\u7b97\u6cd5\u7b56\u7565\uff0c\u901a\u8fc7\u51fd\u6570\u8c03\u7528\u81ea\u8eab\u6765\u89e3\u51b3\u95ee\u9898\u3002\u5b83\u4e3b\u8981\u5305\u542b\u4e24\u4e2a\u9636\u6bb5\u3002
\u800c\u4ece\u5b9e\u73b0\u7684\u89d2\u5ea6\u770b\uff0c\u9012\u5f52\u4ee3\u7801\u4e3b\u8981\u5305\u542b\u4e09\u4e2a\u8981\u7d20\u3002
\u89c2\u5bdf\u4ee5\u4e0b\u4ee3\u7801\uff0c\u6211\u4eec\u53ea\u9700\u8c03\u7528\u51fd\u6570 recur(n)
\uff0c\u5c31\u53ef\u4ee5\u5b8c\u6210 \\(1 + 2 + \\dots + n\\) \u7684\u8ba1\u7b97\uff1a
def recur(n: int) -> int:\n \"\"\"\u9012\u5f52\"\"\"\n # \u7ec8\u6b62\u6761\u4ef6\n if n == 1:\n return 1\n # \u9012\uff1a\u9012\u5f52\u8c03\u7528\n res = recur(n - 1)\n # \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n return n + res\n
recursion.cpp/* \u9012\u5f52 */\nint recur(int n) {\n // \u7ec8\u6b62\u6761\u4ef6\n if (n == 1)\n return 1;\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n int res = recur(n - 1);\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n return n + res;\n}\n
recursion.java/* \u9012\u5f52 */\nint recur(int n) {\n // \u7ec8\u6b62\u6761\u4ef6\n if (n == 1)\n return 1;\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n int res = recur(n - 1);\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n return n + res;\n}\n
recursion.cs/* \u9012\u5f52 */\nint Recur(int n) {\n // \u7ec8\u6b62\u6761\u4ef6\n if (n == 1)\n return 1;\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n int res = Recur(n - 1);\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n return n + res;\n}\n
recursion.go/* \u9012\u5f52 */\nfunc recur(n int) int {\n // \u7ec8\u6b62\u6761\u4ef6\n if n == 1 {\n return 1\n }\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n res := recur(n - 1)\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n return n + res\n}\n
recursion.swift/* \u9012\u5f52 */\nfunc recur(n: Int) -> Int {\n // \u7ec8\u6b62\u6761\u4ef6\n if n == 1 {\n return 1\n }\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n let res = recur(n: n - 1)\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n return n + res\n}\n
recursion.js/* \u9012\u5f52 */\nfunction recur(n) {\n // \u7ec8\u6b62\u6761\u4ef6\n if (n === 1) return 1;\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n const res = recur(n - 1);\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n return n + res;\n}\n
recursion.ts/* \u9012\u5f52 */\nfunction recur(n: number): number {\n // \u7ec8\u6b62\u6761\u4ef6\n if (n === 1) return 1;\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n const res = recur(n - 1);\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n return n + res;\n}\n
recursion.dart/* \u9012\u5f52 */\nint recur(int n) {\n // \u7ec8\u6b62\u6761\u4ef6\n if (n == 1) return 1;\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n int res = recur(n - 1);\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n return n + res;\n}\n
recursion.rs/* \u9012\u5f52 */\nfn recur(n: i32) -> i32 {\n // \u7ec8\u6b62\u6761\u4ef6\n if n == 1 {\n return 1;\n }\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n let res = recur(n - 1);\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n n + res\n}\n
recursion.c/* \u9012\u5f52 */\nint recur(int n) {\n // \u7ec8\u6b62\u6761\u4ef6\n if (n == 1)\n return 1;\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n int res = recur(n - 1);\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n return n + res;\n}\n
recursion.zig// \u9012\u5f52\u51fd\u6570\nfn recur(n: i32) i32 {\n // \u7ec8\u6b62\u6761\u4ef6\n if (n == 1) {\n return 1;\n }\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n var res: i32 = recur(n - 1);\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n return n + res;\n}\n
\u56fe 2-3 \u5c55\u793a\u4e86\u8be5\u51fd\u6570\u7684\u9012\u5f52\u8fc7\u7a0b\u3002
\u56fe 2-3 \u00a0 \u6c42\u548c\u51fd\u6570\u7684\u9012\u5f52\u8fc7\u7a0b
\u867d\u7136\u4ece\u8ba1\u7b97\u89d2\u5ea6\u770b\uff0c\u8fed\u4ee3\u4e0e\u9012\u5f52\u53ef\u4ee5\u5f97\u5230\u76f8\u540c\u7684\u7ed3\u679c\uff0c\u4f46\u5b83\u4eec\u4ee3\u8868\u4e86\u4e24\u79cd\u5b8c\u5168\u4e0d\u540c\u7684\u601d\u8003\u548c\u89e3\u51b3\u95ee\u9898\u7684\u8303\u5f0f\u3002
\u4ee5\u4e0a\u8ff0\u6c42\u548c\u51fd\u6570\u4e3a\u4f8b\uff0c\u8bbe\u95ee\u9898 \\(f(n) = 1 + 2 + \\dots + n\\) \u3002
\u9012\u5f52\u51fd\u6570\u6bcf\u6b21\u8c03\u7528\u81ea\u8eab\u65f6\uff0c\u7cfb\u7edf\u90fd\u4f1a\u4e3a\u65b0\u5f00\u542f\u7684\u51fd\u6570\u5206\u914d\u5185\u5b58\uff0c\u4ee5\u5b58\u50a8\u5c40\u90e8\u53d8\u91cf\u3001\u8c03\u7528\u5730\u5740\u548c\u5176\u4ed6\u4fe1\u606f\u7b49\u3002\u8fd9\u5c06\u5bfc\u81f4\u4e24\u65b9\u9762\u7684\u7ed3\u679c\u3002
\u5982\u56fe 2-4 \u6240\u793a\uff0c\u5728\u89e6\u53d1\u7ec8\u6b62\u6761\u4ef6\u524d\uff0c\u540c\u65f6\u5b58\u5728 \\(n\\) \u4e2a\u672a\u8fd4\u56de\u7684\u9012\u5f52\u51fd\u6570\uff0c\u9012\u5f52\u6df1\u5ea6\u4e3a \\(n\\) \u3002
\u56fe 2-4 \u00a0 \u9012\u5f52\u8c03\u7528\u6df1\u5ea6
\u5728\u5b9e\u9645\u4e2d\uff0c\u7f16\u7a0b\u8bed\u8a00\u5141\u8bb8\u7684\u9012\u5f52\u6df1\u5ea6\u901a\u5e38\u662f\u6709\u9650\u7684\uff0c\u8fc7\u6df1\u7684\u9012\u5f52\u53ef\u80fd\u5bfc\u81f4\u6808\u6ea2\u51fa\u9519\u8bef\u3002
"},{"location":"chapter_computational_complexity/iteration_and_recursion/#2","title":"2. \u00a0 \u5c3e\u9012\u5f52","text":"\u6709\u8da3\u7684\u662f\uff0c\u5982\u679c\u51fd\u6570\u5728\u8fd4\u56de\u524d\u7684\u6700\u540e\u4e00\u6b65\u624d\u8fdb\u884c\u9012\u5f52\u8c03\u7528\uff0c\u5219\u8be5\u51fd\u6570\u53ef\u4ee5\u88ab\u7f16\u8bd1\u5668\u6216\u89e3\u91ca\u5668\u4f18\u5316\uff0c\u4f7f\u5176\u5728\u7a7a\u95f4\u6548\u7387\u4e0a\u4e0e\u8fed\u4ee3\u76f8\u5f53\u3002\u8fd9\u79cd\u60c5\u51b5\u88ab\u79f0\u4e3a\u300c\u5c3e\u9012\u5f52 tail recursion\u300d\u3002
\u4ee5\u8ba1\u7b97 \\(1 + 2 + \\dots + n\\) \u4e3a\u4f8b\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u7ed3\u679c\u53d8\u91cf res
\u8bbe\u4e3a\u51fd\u6570\u53c2\u6570\uff0c\u4ece\u800c\u5b9e\u73b0\u5c3e\u9012\u5f52\uff1a
def tail_recur(n, res):\n \"\"\"\u5c3e\u9012\u5f52\"\"\"\n # \u7ec8\u6b62\u6761\u4ef6\n if n == 0:\n return res\n # \u5c3e\u9012\u5f52\u8c03\u7528\n return tail_recur(n - 1, res + n)\n
recursion.cpp/* \u5c3e\u9012\u5f52 */\nint tailRecur(int n, int res) {\n // \u7ec8\u6b62\u6761\u4ef6\n if (n == 0)\n return res;\n // \u5c3e\u9012\u5f52\u8c03\u7528\n return tailRecur(n - 1, res + n);\n}\n
recursion.java/* \u5c3e\u9012\u5f52 */\nint tailRecur(int n, int res) {\n // \u7ec8\u6b62\u6761\u4ef6\n if (n == 0)\n return res;\n // \u5c3e\u9012\u5f52\u8c03\u7528\n return tailRecur(n - 1, res + n);\n}\n
recursion.cs/* \u5c3e\u9012\u5f52 */\nint TailRecur(int n, int res) {\n // \u7ec8\u6b62\u6761\u4ef6\n if (n == 0)\n return res;\n // \u5c3e\u9012\u5f52\u8c03\u7528\n return TailRecur(n - 1, res + n);\n}\n
recursion.go/* \u5c3e\u9012\u5f52 */\nfunc tailRecur(n int, res int) int {\n // \u7ec8\u6b62\u6761\u4ef6\n if n == 0 {\n return res\n }\n // \u5c3e\u9012\u5f52\u8c03\u7528\n return tailRecur(n-1, res+n)\n}\n
recursion.swift/* \u5c3e\u9012\u5f52 */\nfunc tailRecur(n: Int, res: Int) -> Int {\n // \u7ec8\u6b62\u6761\u4ef6\n if n == 0 {\n return res\n }\n // \u5c3e\u9012\u5f52\u8c03\u7528\n return tailRecur(n: n - 1, res: res + n)\n}\n
recursion.js/* \u5c3e\u9012\u5f52 */\nfunction tailRecur(n, res) {\n // \u7ec8\u6b62\u6761\u4ef6\n if (n === 0) return res;\n // \u5c3e\u9012\u5f52\u8c03\u7528\n return tailRecur(n - 1, res + n);\n}\n
recursion.ts/* \u5c3e\u9012\u5f52 */\nfunction tailRecur(n: number, res: number): number {\n // \u7ec8\u6b62\u6761\u4ef6\n if (n === 0) return res;\n // \u5c3e\u9012\u5f52\u8c03\u7528\n return tailRecur(n - 1, res + n);\n}\n
recursion.dart/* \u5c3e\u9012\u5f52 */\nint tailRecur(int n, int res) {\n // \u7ec8\u6b62\u6761\u4ef6\n if (n == 0) return res;\n // \u5c3e\u9012\u5f52\u8c03\u7528\n return tailRecur(n - 1, res + n);\n}\n
recursion.rs/* \u5c3e\u9012\u5f52 */\nfn tail_recur(n: i32, res: i32) -> i32 {\n // \u7ec8\u6b62\u6761\u4ef6\n if n == 0 {\n return res;\n }\n // \u5c3e\u9012\u5f52\u8c03\u7528\n tail_recur(n - 1, res + n)\n}\n
recursion.c/* \u5c3e\u9012\u5f52 */\nint tailRecur(int n, int res) {\n // \u7ec8\u6b62\u6761\u4ef6\n if (n == 0)\n return res;\n // \u5c3e\u9012\u5f52\u8c03\u7528\n return tailRecur(n - 1, res + n);\n}\n
recursion.zig// \u5c3e\u9012\u5f52\u51fd\u6570\nfn tailRecur(n: i32, res: i32) i32 {\n // \u7ec8\u6b62\u6761\u4ef6\n if (n == 0) {\n return res;\n }\n // \u5c3e\u9012\u5f52\u8c03\u7528\n return tailRecur(n - 1, res + n);\n}\n
\u5c3e\u9012\u5f52\u7684\u6267\u884c\u8fc7\u7a0b\u5982\u56fe 2-5 \u6240\u793a\u3002\u5bf9\u6bd4\u666e\u901a\u9012\u5f52\u548c\u5c3e\u9012\u5f52\uff0c\u4e24\u8005\u7684\u6c42\u548c\u64cd\u4f5c\u7684\u6267\u884c\u70b9\u662f\u4e0d\u540c\u7684\u3002
\u56fe 2-5 \u00a0 \u5c3e\u9012\u5f52\u8fc7\u7a0b
Tip
\u8bf7\u6ce8\u610f\uff0c\u8bb8\u591a\u7f16\u8bd1\u5668\u6216\u89e3\u91ca\u5668\u5e76\u4e0d\u652f\u6301\u5c3e\u9012\u5f52\u4f18\u5316\u3002\u4f8b\u5982\uff0cPython \u9ed8\u8ba4\u4e0d\u652f\u6301\u5c3e\u9012\u5f52\u4f18\u5316\uff0c\u56e0\u6b64\u5373\u4f7f\u51fd\u6570\u662f\u5c3e\u9012\u5f52\u5f62\u5f0f\uff0c\u4ecd\u7136\u53ef\u80fd\u4f1a\u9047\u5230\u6808\u6ea2\u51fa\u95ee\u9898\u3002
"},{"location":"chapter_computational_complexity/iteration_and_recursion/#3_1","title":"3. \u00a0 \u9012\u5f52\u6811","text":"\u5f53\u5904\u7406\u4e0e\u201c\u5206\u6cbb\u201d\u76f8\u5173\u7684\u7b97\u6cd5\u95ee\u9898\u65f6\uff0c\u9012\u5f52\u5f80\u5f80\u6bd4\u8fed\u4ee3\u7684\u601d\u8def\u66f4\u52a0\u76f4\u89c2\u3001\u4ee3\u7801\u66f4\u52a0\u6613\u8bfb\u3002\u4ee5\u201c\u6590\u6ce2\u90a3\u5951\u6570\u5217\u201d\u4e3a\u4f8b\u3002
Question
\u7ed9\u5b9a\u4e00\u4e2a\u6590\u6ce2\u90a3\u5951\u6570\u5217 \\(0, 1, 1, 2, 3, 5, 8, 13, \\dots\\) \uff0c\u6c42\u8be5\u6570\u5217\u7684\u7b2c \\(n\\) \u4e2a\u6570\u5b57\u3002
\u8bbe\u6590\u6ce2\u90a3\u5951\u6570\u5217\u7684\u7b2c \\(n\\) \u4e2a\u6570\u5b57\u4e3a \\(f(n)\\) \uff0c\u6613\u5f97\u4e24\u4e2a\u7ed3\u8bba\u3002
\u6309\u7167\u9012\u63a8\u5173\u7cfb\u8fdb\u884c\u9012\u5f52\u8c03\u7528\uff0c\u5c06\u524d\u4e24\u4e2a\u6570\u5b57\u4f5c\u4e3a\u7ec8\u6b62\u6761\u4ef6\uff0c\u4fbf\u53ef\u5199\u51fa\u9012\u5f52\u4ee3\u7801\u3002\u8c03\u7528 fib(n)
\u5373\u53ef\u5f97\u5230\u6590\u6ce2\u90a3\u5951\u6570\u5217\u7684\u7b2c \\(n\\) \u4e2a\u6570\u5b57\uff1a
def fib(n: int) -> int:\n \"\"\"\u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52\"\"\"\n # \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\n if n == 1 or n == 2:\n return n - 1\n # \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\n res = fib(n - 1) + fib(n - 2)\n # \u8fd4\u56de\u7ed3\u679c f(n)\n return res\n
recursion.cpp/* \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 */\nint fib(int n) {\n // \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\n if (n == 1 || n == 2)\n return n - 1;\n // \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\n int res = fib(n - 1) + fib(n - 2);\n // \u8fd4\u56de\u7ed3\u679c f(n)\n return res;\n}\n
recursion.java/* \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 */\nint fib(int n) {\n // \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\n if (n == 1 || n == 2)\n return n - 1;\n // \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\n int res = fib(n - 1) + fib(n - 2);\n // \u8fd4\u56de\u7ed3\u679c f(n)\n return res;\n}\n
recursion.cs/* \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 */\nint Fib(int n) {\n // \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\n if (n == 1 || n == 2)\n return n - 1;\n // \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\n int res = Fib(n - 1) + Fib(n - 2);\n // \u8fd4\u56de\u7ed3\u679c f(n)\n return res;\n}\n
recursion.go/* \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 */\nfunc fib(n int) int {\n // \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\n if n == 1 || n == 2 {\n return n - 1\n }\n // \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\n res := fib(n-1) + fib(n-2)\n // \u8fd4\u56de\u7ed3\u679c f(n)\n return res\n}\n
recursion.swift/* \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 */\nfunc fib(n: Int) -> Int {\n // \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\n if n == 1 || n == 2 {\n return n - 1\n }\n // \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\n let res = fib(n: n - 1) + fib(n: n - 2)\n // \u8fd4\u56de\u7ed3\u679c f(n)\n return res\n}\n
recursion.js/* \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 */\nfunction fib(n) {\n // \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\n if (n === 1 || n === 2) return n - 1;\n // \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\n const res = fib(n - 1) + fib(n - 2);\n // \u8fd4\u56de\u7ed3\u679c f(n)\n return res;\n}\n
recursion.ts/* \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 */\nfunction fib(n: number): number {\n // \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\n if (n === 1 || n === 2) return n - 1;\n // \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\n const res = fib(n - 1) + fib(n - 2);\n // \u8fd4\u56de\u7ed3\u679c f(n)\n return res;\n}\n
recursion.dart/* \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 */\nint fib(int n) {\n // \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\n if (n == 1 || n == 2) return n - 1;\n // \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\n int res = fib(n - 1) + fib(n - 2);\n // \u8fd4\u56de\u7ed3\u679c f(n)\n return res;\n}\n
recursion.rs/* \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 */\nfn fib(n: i32) -> i32 {\n // \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\n if n == 1 || n == 2 {\n return n - 1;\n }\n // \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\n let res = fib(n - 1) + fib(n - 2);\n // \u8fd4\u56de\u7ed3\u679c\n res\n}\n
recursion.c/* \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 */\nint fib(int n) {\n // \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\n if (n == 1 || n == 2)\n return n - 1;\n // \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\n int res = fib(n - 1) + fib(n - 2);\n // \u8fd4\u56de\u7ed3\u679c f(n)\n return res;\n}\n
recursion.zig// \u6590\u6ce2\u90a3\u5951\u6570\u5217\nfn fib(n: i32) i32 {\n // \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\n if (n == 1 or n == 2) {\n return n - 1;\n }\n // \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\n var res: i32 = fib(n - 1) + fib(n - 2);\n // \u8fd4\u56de\u7ed3\u679c f(n)\n return res;\n}\n
\u89c2\u5bdf\u4ee5\u4e0a\u4ee3\u7801\uff0c\u6211\u4eec\u5728\u51fd\u6570\u5185\u9012\u5f52\u8c03\u7528\u4e86\u4e24\u4e2a\u51fd\u6570\uff0c\u8fd9\u610f\u5473\u7740\u4ece\u4e00\u4e2a\u8c03\u7528\u4ea7\u751f\u4e86\u4e24\u4e2a\u8c03\u7528\u5206\u652f\u3002\u5982\u56fe 2-6 \u6240\u793a\uff0c\u8fd9\u6837\u4e0d\u65ad\u9012\u5f52\u8c03\u7528\u4e0b\u53bb\uff0c\u6700\u7ec8\u5c06\u4ea7\u751f\u4e00\u68f5\u5c42\u6570\u4e3a \\(n\\) \u7684\u300c\u9012\u5f52\u6811 recursion tree\u300d\u3002
\u56fe 2-6 \u00a0 \u6590\u6ce2\u90a3\u5951\u6570\u5217\u7684\u9012\u5f52\u6811
\u4ece\u672c\u8d28\u4e0a\u770b\uff0c\u9012\u5f52\u4f53\u73b0\u4e86\u201c\u5c06\u95ee\u9898\u5206\u89e3\u4e3a\u66f4\u5c0f\u5b50\u95ee\u9898\u201d\u7684\u601d\u7ef4\u8303\u5f0f\uff0c\u8fd9\u79cd\u5206\u6cbb\u7b56\u7565\u81f3\u5173\u91cd\u8981\u3002
\u603b\u7ed3\u4ee5\u4e0a\u5185\u5bb9\uff0c\u5982\u8868 2-1 \u6240\u793a\uff0c\u8fed\u4ee3\u548c\u9012\u5f52\u5728\u5b9e\u73b0\u3001\u6027\u80fd\u548c\u9002\u7528\u6027\u4e0a\u6709\u6240\u4e0d\u540c\u3002
\u8868 2-1 \u00a0 \u8fed\u4ee3\u4e0e\u9012\u5f52\u7279\u70b9\u5bf9\u6bd4
\u8fed\u4ee3 \u9012\u5f52 \u5b9e\u73b0\u65b9\u5f0f \u5faa\u73af\u7ed3\u6784 \u51fd\u6570\u8c03\u7528\u81ea\u8eab \u65f6\u95f4\u6548\u7387 \u6548\u7387\u901a\u5e38\u8f83\u9ad8\uff0c\u65e0\u51fd\u6570\u8c03\u7528\u5f00\u9500 \u6bcf\u6b21\u51fd\u6570\u8c03\u7528\u90fd\u4f1a\u4ea7\u751f\u5f00\u9500 \u5185\u5b58\u4f7f\u7528 \u901a\u5e38\u4f7f\u7528\u56fa\u5b9a\u5927\u5c0f\u7684\u5185\u5b58\u7a7a\u95f4 \u7d2f\u79ef\u51fd\u6570\u8c03\u7528\u53ef\u80fd\u4f7f\u7528\u5927\u91cf\u7684\u6808\u5e27\u7a7a\u95f4 \u9002\u7528\u95ee\u9898 \u9002\u7528\u4e8e\u7b80\u5355\u5faa\u73af\u4efb\u52a1\uff0c\u4ee3\u7801\u76f4\u89c2\u3001\u53ef\u8bfb\u6027\u597d \u9002\u7528\u4e8e\u5b50\u95ee\u9898\u5206\u89e3\uff0c\u5982\u6811\u3001\u56fe\u3001\u5206\u6cbb\u3001\u56de\u6eaf\u7b49\uff0c\u4ee3\u7801\u7ed3\u6784\u7b80\u6d01\u3001\u6e05\u6670Tip
\u5982\u679c\u611f\u89c9\u4ee5\u4e0b\u5185\u5bb9\u7406\u89e3\u56f0\u96be\uff0c\u53ef\u4ee5\u5728\u8bfb\u5b8c\u201c\u6808\u201d\u7ae0\u8282\u540e\u518d\u6765\u590d\u4e60\u3002
\u90a3\u4e48\uff0c\u8fed\u4ee3\u548c\u9012\u5f52\u5177\u6709\u4ec0\u4e48\u5185\u5728\u8054\u7cfb\u5462\uff1f\u4ee5\u4e0a\u8ff0\u9012\u5f52\u51fd\u6570\u4e3a\u4f8b\uff0c\u6c42\u548c\u64cd\u4f5c\u5728\u9012\u5f52\u7684\u201c\u5f52\u201d\u9636\u6bb5\u8fdb\u884c\u3002\u8fd9\u610f\u5473\u7740\u6700\u521d\u88ab\u8c03\u7528\u7684\u51fd\u6570\u5b9e\u9645\u4e0a\u662f\u6700\u540e\u5b8c\u6210\u5176\u6c42\u548c\u64cd\u4f5c\u7684\uff0c\u8fd9\u79cd\u5de5\u4f5c\u673a\u5236\u4e0e\u6808\u7684\u201c\u5148\u5165\u540e\u51fa\u201d\u539f\u5219\u5f02\u66f2\u540c\u5de5\u3002
\u4e8b\u5b9e\u4e0a\uff0c\u201c\u8c03\u7528\u6808\u201d\u548c\u201c\u6808\u5e27\u7a7a\u95f4\u201d\u8fd9\u7c7b\u9012\u5f52\u672f\u8bed\u5df2\u7ecf\u6697\u793a\u4e86\u9012\u5f52\u4e0e\u6808\u4e4b\u95f4\u7684\u5bc6\u5207\u5173\u7cfb\u3002
\u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u8c03\u7528\u6808\u7684\u884c\u4e3a\uff0c\u4ece\u800c\u5c06\u9012\u5f52\u8f6c\u5316\u4e3a\u8fed\u4ee3\u5f62\u5f0f\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig recursion.pydef for_loop_recur(n: int) -> int:\n \"\"\"\u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52\"\"\"\n # \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\n stack = []\n res = 0\n # \u9012\uff1a\u9012\u5f52\u8c03\u7528\n for i in range(n, 0, -1):\n # \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\n stack.append(i)\n # \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n while stack:\n # \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\n res += stack.pop()\n # res = 1+2+3+...+n\n return res\n
recursion.cpp/* \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 */\nint forLoopRecur(int n) {\n // \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\n stack<int> stack;\n int res = 0;\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n for (int i = n; i > 0; i--) {\n // \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\n stack.push(i);\n }\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n while (!stack.empty()) {\n // \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\n res += stack.top();\n stack.pop();\n }\n // res = 1+2+3+...+n\n return res;\n}\n
recursion.java/* \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 */\nint forLoopRecur(int n) {\n // \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\n Stack<Integer> stack = new Stack<>();\n int res = 0;\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n for (int i = n; i > 0; i--) {\n // \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\n stack.push(i);\n }\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n while (!stack.isEmpty()) {\n // \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\n res += stack.pop();\n }\n // res = 1+2+3+...+n\n return res;\n}\n
recursion.cs/* \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 */\nint ForLoopRecur(int n) {\n // \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\n Stack<int> stack = new();\n int res = 0;\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n for (int i = n; i > 0; i--) {\n // \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\n stack.Push(i);\n }\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n while (stack.Count > 0) {\n // \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\n res += stack.Pop();\n }\n // res = 1+2+3+...+n\n return res;\n}\n
recursion.go/* \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 */\nfunc forLoopRecur(n int) int {\n // \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\n stack := list.New()\n res := 0\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n for i := n; i > 0; i-- {\n // \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\n stack.PushBack(i)\n }\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n for stack.Len() != 0 {\n // \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\n res += stack.Back().Value.(int)\n stack.Remove(stack.Back())\n }\n // res = 1+2+3+...+n\n return res\n}\n
recursion.swift/* \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 */\nfunc forLoopRecur(n: Int) -> Int {\n // \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\n var stack: [Int] = []\n var res = 0\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n for i in stride(from: n, to: 0, by: -1) {\n // \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\n stack.append(i)\n }\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n while !stack.isEmpty {\n // \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\n res += stack.removeLast()\n }\n // res = 1+2+3+...+n\n return res\n}\n
recursion.js/* \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 */\nfunction forLoopRecur(n) {\n // \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\n const stack = [];\n let res = 0;\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n for (let i = 1; i <= n; i++) {\n // \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\n stack.push(i);\n }\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n while (stack.length) { \n // \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\n res += stack.pop();\n }\n // res = 1+2+3+...+n\n return res;\n}\n
recursion.ts/* \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 */\nfunction forLoopRecur(n: number): number {\n // \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808 \n const stack: number[] = [];\n let res: number = 0;\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n for (let i = 1; i <= n; i++) {\n // \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\n stack.push(i);\n }\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n while (stack.length) { \n // \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\n res += stack.pop();\n }\n // res = 1+2+3+...+n\n return res;\n}\n
recursion.dart/* \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 */\nint forLoopRecur(int n) {\n // \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\n List<int> stack = [];\n int res = 0;\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n for (int i = n; i > 0; i--) {\n // \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\n stack.add(i);\n }\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n while (!stack.isEmpty) {\n // \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\n res += stack.removeLast();\n }\n // res = 1+2+3+...+n\n return res;\n}\n
recursion.rs/* \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 */\nfn for_loop_recur(n: i32) -> i32 {\n // \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\n let mut stack = Vec::new();\n let mut res = 0;\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n for i in (1..=n).rev() {\n // \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\n stack.push(i);\n }\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n while !stack.is_empty() {\n // \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\n res += stack.pop().unwrap();\n }\n // res = 1+2+3+...+n\n res\n}\n
recursion.c/* \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 */\nint forLoopRecur(int n) {\n int stack[1000]; // \u501f\u52a9\u4e00\u4e2a\u5927\u6570\u7ec4\u6765\u6a21\u62df\u6808\n int top = -1; // \u6808\u9876\u7d22\u5f15\n int res = 0;\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n for (int i = n; i > 0; i--) {\n // \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\n stack[1 + top++] = i;\n }\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n while (top >= 0) {\n // \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\n res += stack[top--];\n }\n // res = 1+2+3+...+n\n return res;\n}\n
recursion.zig// \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52\nfn forLoopRecur(comptime n: i32) i32 {\n // \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\n var stack: [n]i32 = undefined;\n var res: i32 = 0;\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n var i: usize = n;\n while (i > 0) {\n stack[i - 1] = @intCast(i);\n i -= 1;\n }\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n var index: usize = n;\n while (index > 0) {\n index -= 1;\n res += stack[index];\n }\n // res = 1+2+3+...+n\n return res;\n}\n
\u89c2\u5bdf\u4ee5\u4e0a\u4ee3\u7801\uff0c\u5f53\u9012\u5f52\u8f6c\u5316\u4e3a\u8fed\u4ee3\u540e\uff0c\u4ee3\u7801\u53d8\u5f97\u66f4\u52a0\u590d\u6742\u4e86\u3002\u5c3d\u7ba1\u8fed\u4ee3\u548c\u9012\u5f52\u5728\u5f88\u591a\u60c5\u51b5\u4e0b\u53ef\u4ee5\u4e92\u76f8\u8f6c\u5316\uff0c\u4f46\u4e0d\u4e00\u5b9a\u503c\u5f97\u8fd9\u6837\u505a\uff0c\u6709\u4ee5\u4e0b\u4e24\u70b9\u539f\u56e0\u3002
\u603b\u4e4b\uff0c\u9009\u62e9\u8fed\u4ee3\u8fd8\u662f\u9012\u5f52\u53d6\u51b3\u4e8e\u7279\u5b9a\u95ee\u9898\u7684\u6027\u8d28\u3002\u5728\u7f16\u7a0b\u5b9e\u8df5\u4e2d\uff0c\u6743\u8861\u4e24\u8005\u7684\u4f18\u52a3\u5e76\u6839\u636e\u60c5\u5883\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u81f3\u5173\u91cd\u8981\u3002
"},{"location":"chapter_computational_complexity/performance_evaluation/","title":"2.1 \u00a0 \u7b97\u6cd5\u6548\u7387\u8bc4\u4f30","text":"\u5728\u7b97\u6cd5\u8bbe\u8ba1\u4e2d\uff0c\u6211\u4eec\u5148\u540e\u8ffd\u6c42\u4ee5\u4e0b\u4e24\u4e2a\u5c42\u9762\u7684\u76ee\u6807\u3002
\u4e5f\u5c31\u662f\u8bf4\uff0c\u5728\u80fd\u591f\u89e3\u51b3\u95ee\u9898\u7684\u524d\u63d0\u4e0b\uff0c\u7b97\u6cd5\u6548\u7387\u5df2\u6210\u4e3a\u8861\u91cf\u7b97\u6cd5\u4f18\u52a3\u7684\u4e3b\u8981\u8bc4\u4ef7\u6307\u6807\uff0c\u5b83\u5305\u62ec\u4ee5\u4e0b\u4e24\u4e2a\u7ef4\u5ea6\u3002
\u7b80\u800c\u8a00\u4e4b\uff0c\u6211\u4eec\u7684\u76ee\u6807\u662f\u8bbe\u8ba1\u201c\u65e2\u5feb\u53c8\u7701\u201d\u7684\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u3002\u800c\u6709\u6548\u5730\u8bc4\u4f30\u7b97\u6cd5\u6548\u7387\u81f3\u5173\u91cd\u8981\uff0c\u56e0\u4e3a\u53ea\u6709\u8fd9\u6837\u6211\u4eec\u624d\u80fd\u5c06\u5404\u79cd\u7b97\u6cd5\u8fdb\u884c\u5bf9\u6bd4\uff0c\u8fdb\u800c\u6307\u5bfc\u7b97\u6cd5\u8bbe\u8ba1\u4e0e\u4f18\u5316\u8fc7\u7a0b\u3002
\u6548\u7387\u8bc4\u4f30\u65b9\u6cd5\u4e3b\u8981\u5206\u4e3a\u4e24\u79cd\uff1a\u5b9e\u9645\u6d4b\u8bd5\u3001\u7406\u8bba\u4f30\u7b97\u3002
"},{"location":"chapter_computational_complexity/performance_evaluation/#211","title":"2.1.1 \u00a0 \u5b9e\u9645\u6d4b\u8bd5","text":"\u5047\u8bbe\u6211\u4eec\u73b0\u5728\u6709\u7b97\u6cd5 A
\u548c\u7b97\u6cd5 B
\uff0c\u5b83\u4eec\u90fd\u80fd\u89e3\u51b3\u540c\u4e00\u95ee\u9898\uff0c\u73b0\u5728\u9700\u8981\u5bf9\u6bd4\u8fd9\u4e24\u4e2a\u7b97\u6cd5\u7684\u6548\u7387\u3002\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u662f\u627e\u4e00\u53f0\u8ba1\u7b97\u673a\uff0c\u8fd0\u884c\u8fd9\u4e24\u4e2a\u7b97\u6cd5\uff0c\u5e76\u76d1\u63a7\u8bb0\u5f55\u5b83\u4eec\u7684\u8fd0\u884c\u65f6\u95f4\u548c\u5185\u5b58\u5360\u7528\u60c5\u51b5\u3002\u8fd9\u79cd\u8bc4\u4f30\u65b9\u5f0f\u80fd\u591f\u53cd\u6620\u771f\u5b9e\u60c5\u51b5\uff0c\u4f46\u4e5f\u5b58\u5728\u8f83\u5927\u7684\u5c40\u9650\u6027\u3002
\u4e00\u65b9\u9762\uff0c\u96be\u4ee5\u6392\u9664\u6d4b\u8bd5\u73af\u5883\u7684\u5e72\u6270\u56e0\u7d20\u3002\u786c\u4ef6\u914d\u7f6e\u4f1a\u5f71\u54cd\u7b97\u6cd5\u7684\u6027\u80fd\u3002\u6bd4\u5982\u5728\u67d0\u53f0\u8ba1\u7b97\u673a\u4e2d\uff0c\u7b97\u6cd5 A
\u7684\u8fd0\u884c\u65f6\u95f4\u6bd4\u7b97\u6cd5 B
\u77ed\uff1b\u4f46\u5728\u53e6\u4e00\u53f0\u914d\u7f6e\u4e0d\u540c\u7684\u8ba1\u7b97\u673a\u4e2d\uff0c\u53ef\u80fd\u5f97\u5230\u76f8\u53cd\u7684\u6d4b\u8bd5\u7ed3\u679c\u3002\u8fd9\u610f\u5473\u7740\u6211\u4eec\u9700\u8981\u5728\u5404\u79cd\u673a\u5668\u4e0a\u8fdb\u884c\u6d4b\u8bd5\uff0c\u7edf\u8ba1\u5e73\u5747\u6548\u7387\uff0c\u800c\u8fd9\u662f\u4e0d\u73b0\u5b9e\u7684\u3002
\u53e6\u4e00\u65b9\u9762\uff0c\u5c55\u5f00\u5b8c\u6574\u6d4b\u8bd5\u975e\u5e38\u8017\u8d39\u8d44\u6e90\u3002\u968f\u7740\u8f93\u5165\u6570\u636e\u91cf\u7684\u53d8\u5316\uff0c\u7b97\u6cd5\u4f1a\u8868\u73b0\u51fa\u4e0d\u540c\u7684\u6548\u7387\u3002\u4f8b\u5982\uff0c\u5728\u8f93\u5165\u6570\u636e\u91cf\u8f83\u5c0f\u65f6\uff0c\u7b97\u6cd5 A
\u7684\u8fd0\u884c\u65f6\u95f4\u6bd4\u7b97\u6cd5 B
\u77ed\uff1b\u800c\u5728\u8f93\u5165\u6570\u636e\u91cf\u8f83\u5927\u65f6\uff0c\u6d4b\u8bd5\u7ed3\u679c\u53ef\u80fd\u6070\u6070\u76f8\u53cd\u3002\u56e0\u6b64\uff0c\u4e3a\u4e86\u5f97\u5230\u6709\u8bf4\u670d\u529b\u7684\u7ed3\u8bba\uff0c\u6211\u4eec\u9700\u8981\u6d4b\u8bd5\u5404\u79cd\u89c4\u6a21\u7684\u8f93\u5165\u6570\u636e\uff0c\u800c\u8fd9\u9700\u8981\u8017\u8d39\u5927\u91cf\u7684\u8ba1\u7b97\u8d44\u6e90\u3002
\u7531\u4e8e\u5b9e\u9645\u6d4b\u8bd5\u5177\u6709\u8f83\u5927\u7684\u5c40\u9650\u6027\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u8003\u8651\u4ec5\u901a\u8fc7\u4e00\u4e9b\u8ba1\u7b97\u6765\u8bc4\u4f30\u7b97\u6cd5\u7684\u6548\u7387\u3002\u8fd9\u79cd\u4f30\u7b97\u65b9\u6cd5\u88ab\u79f0\u4e3a\u300c\u6e10\u8fd1\u590d\u6742\u5ea6\u5206\u6790 asymptotic complexity analysis\u300d\uff0c\u7b80\u79f0\u300c\u590d\u6742\u5ea6\u5206\u6790\u300d\u3002
\u590d\u6742\u5ea6\u5206\u6790\u80fd\u591f\u4f53\u73b0\u7b97\u6cd5\u8fd0\u884c\u6240\u9700\u7684\u65f6\u95f4\u548c\u7a7a\u95f4\u8d44\u6e90\u4e0e\u8f93\u5165\u6570\u636e\u5927\u5c0f\u4e4b\u95f4\u7684\u5173\u7cfb\u3002\u5b83\u63cf\u8ff0\u4e86\u968f\u7740\u8f93\u5165\u6570\u636e\u5927\u5c0f\u7684\u589e\u52a0\uff0c\u7b97\u6cd5\u6267\u884c\u6240\u9700\u65f6\u95f4\u548c\u7a7a\u95f4\u7684\u589e\u957f\u8d8b\u52bf\u3002\u8fd9\u4e2a\u5b9a\u4e49\u6709\u4e9b\u62d7\u53e3\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5176\u5206\u4e3a\u4e09\u4e2a\u91cd\u70b9\u6765\u7406\u89e3\u3002
\u590d\u6742\u5ea6\u5206\u6790\u514b\u670d\u4e86\u5b9e\u9645\u6d4b\u8bd5\u65b9\u6cd5\u7684\u5f0a\u7aef\uff0c\u4f53\u73b0\u5728\u4ee5\u4e0b\u4e24\u4e2a\u65b9\u9762\u3002
Tip
\u5982\u679c\u4f60\u4ecd\u5bf9\u590d\u6742\u5ea6\u7684\u6982\u5ff5\u611f\u5230\u56f0\u60d1\uff0c\u65e0\u987b\u62c5\u5fc3\uff0c\u6211\u4eec\u4f1a\u5728\u540e\u7eed\u7ae0\u8282\u4e2d\u8be6\u7ec6\u4ecb\u7ecd\u3002
\u590d\u6742\u5ea6\u5206\u6790\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u4e00\u628a\u8bc4\u4f30\u7b97\u6cd5\u6548\u7387\u7684\u201c\u6807\u5c3a\u201d\uff0c\u4f7f\u6211\u4eec\u53ef\u4ee5\u8861\u91cf\u6267\u884c\u67d0\u4e2a\u7b97\u6cd5\u6240\u9700\u7684\u65f6\u95f4\u548c\u7a7a\u95f4\u8d44\u6e90\uff0c\u5bf9\u6bd4\u4e0d\u540c\u7b97\u6cd5\u4e4b\u95f4\u7684\u6548\u7387\u3002
\u590d\u6742\u5ea6\u662f\u4e2a\u6570\u5b66\u6982\u5ff5\uff0c\u5bf9\u4e8e\u521d\u5b66\u8005\u53ef\u80fd\u6bd4\u8f83\u62bd\u8c61\uff0c\u5b66\u4e60\u96be\u5ea6\u76f8\u5bf9\u8f83\u9ad8\u3002\u4ece\u8fd9\u4e2a\u89d2\u5ea6\u770b\uff0c\u590d\u6742\u5ea6\u5206\u6790\u53ef\u80fd\u4e0d\u592a\u9002\u5408\u4f5c\u4e3a\u6700\u5148\u4ecb\u7ecd\u7684\u5185\u5bb9\u3002\u7136\u800c\uff0c\u5f53\u6211\u4eec\u8ba8\u8bba\u67d0\u4e2a\u6570\u636e\u7ed3\u6784\u6216\u7b97\u6cd5\u7684\u7279\u70b9\u65f6\uff0c\u96be\u4ee5\u907f\u514d\u8981\u5206\u6790\u5176\u8fd0\u884c\u901f\u5ea6\u548c\u7a7a\u95f4\u4f7f\u7528\u60c5\u51b5\u3002
\u7efc\u4e0a\u6240\u8ff0\uff0c\u5efa\u8bae\u4f60\u5728\u6df1\u5165\u5b66\u4e60\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u4e4b\u524d\uff0c\u5148\u5bf9\u590d\u6742\u5ea6\u5206\u6790\u5efa\u7acb\u521d\u6b65\u7684\u4e86\u89e3\uff0c\u4ee5\u4fbf\u80fd\u591f\u5b8c\u6210\u7b80\u5355\u7b97\u6cd5\u7684\u590d\u6742\u5ea6\u5206\u6790\u3002
"},{"location":"chapter_computational_complexity/space_complexity/","title":"2.4 \u00a0 \u7a7a\u95f4\u590d\u6742\u5ea6","text":"\u300c\u7a7a\u95f4\u590d\u6742\u5ea6 space complexity\u300d\u7528\u4e8e\u8861\u91cf\u7b97\u6cd5\u5360\u7528\u5185\u5b58\u7a7a\u95f4\u968f\u7740\u6570\u636e\u91cf\u53d8\u5927\u65f6\u7684\u589e\u957f\u8d8b\u52bf\u3002\u8fd9\u4e2a\u6982\u5ff5\u4e0e\u65f6\u95f4\u590d\u6742\u5ea6\u975e\u5e38\u7c7b\u4f3c\uff0c\u53ea\u9700\u5c06\u201c\u8fd0\u884c\u65f6\u95f4\u201d\u66ff\u6362\u4e3a\u201c\u5360\u7528\u5185\u5b58\u7a7a\u95f4\u201d\u3002
"},{"location":"chapter_computational_complexity/space_complexity/#241","title":"2.4.1 \u00a0 \u7b97\u6cd5\u76f8\u5173\u7a7a\u95f4","text":"\u7b97\u6cd5\u5728\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u4f7f\u7528\u7684\u5185\u5b58\u7a7a\u95f4\u4e3b\u8981\u5305\u62ec\u4ee5\u4e0b\u51e0\u79cd\u3002
\u4e00\u822c\u60c5\u51b5\u4e0b\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u7684\u7edf\u8ba1\u8303\u56f4\u662f\u201c\u6682\u5b58\u7a7a\u95f4\u201d\u52a0\u4e0a\u201c\u8f93\u51fa\u7a7a\u95f4\u201d\u3002
\u6682\u5b58\u7a7a\u95f4\u53ef\u4ee5\u8fdb\u4e00\u6b65\u5212\u5206\u4e3a\u4e09\u4e2a\u90e8\u5206\u3002
\u5728\u5206\u6790\u4e00\u6bb5\u7a0b\u5e8f\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u65f6\uff0c\u6211\u4eec\u901a\u5e38\u7edf\u8ba1\u6682\u5b58\u6570\u636e\u3001\u6808\u5e27\u7a7a\u95f4\u548c\u8f93\u51fa\u6570\u636e\u4e09\u90e8\u5206\uff0c\u5982\u56fe 2-15 \u6240\u793a\u3002
\u56fe 2-15 \u00a0 \u7b97\u6cd5\u4f7f\u7528\u7684\u76f8\u5173\u7a7a\u95f4
\u76f8\u5173\u4ee3\u7801\u5982\u4e0b\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZigclass Node:\n \"\"\"\u7c7b\"\"\"\n def __init__(self, x: int):\n self.val: int = x # \u8282\u70b9\u503c\n self.next: Node | None = None # \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u5f15\u7528\n\ndef function() -> int:\n \"\"\"\u51fd\u6570\"\"\"\n # \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c...\n return 0\n\ndef algorithm(n) -> int: # \u8f93\u5165\u6570\u636e\n A = 0 # \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff0c\u4e00\u822c\u7528\u5927\u5199\u5b57\u6bcd\u8868\u793a\uff09\n b = 0 # \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\n node = Node(0) # \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\n c = function() # \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\n return A + b + c # \u8f93\u51fa\u6570\u636e\n
/* \u7ed3\u6784\u4f53 */\nstruct Node {\n int val;\n Node *next;\n Node(int x) : val(x), next(nullptr) {}\n};\n\n/* \u51fd\u6570 */\nint func() {\n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c...\n return 0;\n}\n\nint algorithm(int n) { // \u8f93\u5165\u6570\u636e\n const int a = 0; // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\n int b = 0; // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\n Node* node = new Node(0); // \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\n int c = func(); // \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\n return a + b + c; // \u8f93\u51fa\u6570\u636e\n}\n
/* \u7c7b */\nclass Node {\n int val;\n Node next;\n Node(int x) { val = x; }\n}\n\n/* \u51fd\u6570 */\nint function() {\n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c...\n return 0;\n}\n\nint algorithm(int n) { // \u8f93\u5165\u6570\u636e\n final int a = 0; // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\n int b = 0; // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\n Node node = new Node(0); // \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\n int c = function(); // \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\n return a + b + c; // \u8f93\u51fa\u6570\u636e\n}\n
/* \u7c7b */\nclass Node(int x) {\n int val = x;\n Node next;\n}\n\n/* \u51fd\u6570 */\nint Function() {\n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c...\n return 0;\n}\n\nint Algorithm(int n) { // \u8f93\u5165\u6570\u636e\n const int a = 0; // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\n int b = 0; // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\n Node node = new(0); // \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\n int c = Function(); // \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\n return a + b + c; // \u8f93\u51fa\u6570\u636e\n}\n
/* \u7ed3\u6784\u4f53 */\ntype node struct {\n val int\n next *node\n}\n\n/* \u521b\u5efa node \u7ed3\u6784\u4f53 */\nfunc newNode(val int) *node {\n return &node{val: val}\n}\n\n/* \u51fd\u6570 */\nfunc function() int {\n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c...\n return 0\n}\n\nfunc algorithm(n int) int { // \u8f93\u5165\u6570\u636e\n const a = 0 // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\n b := 0 // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\n newNode(0) // \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\n c := function() // \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\n return a + b + c // \u8f93\u51fa\u6570\u636e\n}\n
/* \u7c7b */\nclass Node {\n var val: Int\n var next: Node?\n\n init(x: Int) {\n val = x\n }\n}\n\n/* \u51fd\u6570 */\nfunc function() -> Int {\n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c...\n return 0\n}\n\nfunc algorithm(n: Int) -> Int { // \u8f93\u5165\u6570\u636e\n let a = 0 // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\n var b = 0 // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\n let node = Node(x: 0) // \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\n let c = function() // \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\n return a + b + c // \u8f93\u51fa\u6570\u636e\n}\n
/* \u7c7b */\nclass Node {\n val;\n next;\n constructor(val) {\n this.val = val === undefined ? 0 : val; // \u8282\u70b9\u503c\n this.next = null; // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u5f15\u7528\n }\n}\n\n/* \u51fd\u6570 */\nfunction constFunc() {\n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0;\n}\n\nfunction algorithm(n) { // \u8f93\u5165\u6570\u636e\n const a = 0; // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\n let b = 0; // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\n const node = new Node(0); // \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\n const c = constFunc(); // \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\n return a + b + c; // \u8f93\u51fa\u6570\u636e\n}\n
/* \u7c7b */\nclass Node {\n val: number;\n next: Node | null;\n constructor(val?: number) {\n this.val = val === undefined ? 0 : val; // \u8282\u70b9\u503c\n this.next = null; // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u5f15\u7528\n }\n}\n\n/* \u51fd\u6570 */\nfunction constFunc(): number {\n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0;\n}\n\nfunction algorithm(n: number): number { // \u8f93\u5165\u6570\u636e\n const a = 0; // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\n let b = 0; // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\n const node = new Node(0); // \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\n const c = constFunc(); // \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\n return a + b + c; // \u8f93\u51fa\u6570\u636e\n}\n
/* \u7c7b */\nclass Node {\n int val;\n Node next;\n Node(this.val, [this.next]);\n}\n\n/* \u51fd\u6570 */\nint function() {\n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c...\n return 0;\n}\n\nint algorithm(int n) { // \u8f93\u5165\u6570\u636e\n const int a = 0; // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\n int b = 0; // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\n Node node = Node(0); // \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\n int c = function(); // \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\n return a + b + c; // \u8f93\u51fa\u6570\u636e\n}\n
use std::rc::Rc;\nuse std::cell::RefCell;\n\n/* \u7ed3\u6784\u4f53 */\nstruct Node {\n val: i32,\n next: Option<Rc<RefCell<Node>>>,\n}\n\n/* \u521b\u5efa Node \u7ed3\u6784\u4f53 */\nimpl Node {\n fn new(val: i32) -> Self {\n Self { val: val, next: None }\n }\n}\n\n/* \u51fd\u6570 */\nfn function() -> i32 { \n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c...\n return 0;\n}\n\nfn algorithm(n: i32) -> i32 { // \u8f93\u5165\u6570\u636e\n const a: i32 = 0; // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\n let mut b = 0; // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\n let node = Node::new(0); // \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\n let c = function(); // \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\n return a + b + c; // \u8f93\u51fa\u6570\u636e\n}\n
/* \u51fd\u6570 */\nint func() {\n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c...\n return 0;\n}\n\nint algorithm(int n) { // \u8f93\u5165\u6570\u636e\n const int a = 0; // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\n int b = 0; // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\n int c = func(); // \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\n return a + b + c; // \u8f93\u51fa\u6570\u636e\n}\n
\n
"},{"location":"chapter_computational_complexity/space_complexity/#242","title":"2.4.2 \u00a0 \u63a8\u7b97\u65b9\u6cd5","text":"\u7a7a\u95f4\u590d\u6742\u5ea6\u7684\u63a8\u7b97\u65b9\u6cd5\u4e0e\u65f6\u95f4\u590d\u6742\u5ea6\u5927\u81f4\u76f8\u540c\uff0c\u53ea\u9700\u5c06\u7edf\u8ba1\u5bf9\u8c61\u4ece\u201c\u64cd\u4f5c\u6570\u91cf\u201d\u8f6c\u4e3a\u201c\u4f7f\u7528\u7a7a\u95f4\u5927\u5c0f\u201d\u3002
\u800c\u4e0e\u65f6\u95f4\u590d\u6742\u5ea6\u4e0d\u540c\u7684\u662f\uff0c\u6211\u4eec\u901a\u5e38\u53ea\u5173\u6ce8\u6700\u5dee\u7a7a\u95f4\u590d\u6742\u5ea6\u3002\u8fd9\u662f\u56e0\u4e3a\u5185\u5b58\u7a7a\u95f4\u662f\u4e00\u9879\u786c\u6027\u8981\u6c42\uff0c\u6211\u4eec\u5fc5\u987b\u786e\u4fdd\u5728\u6240\u6709\u8f93\u5165\u6570\u636e\u4e0b\u90fd\u6709\u8db3\u591f\u7684\u5185\u5b58\u7a7a\u95f4\u9884\u7559\u3002
\u89c2\u5bdf\u4ee5\u4e0b\u4ee3\u7801\uff0c\u6700\u5dee\u7a7a\u95f4\u590d\u6742\u5ea6\u4e2d\u7684\u201c\u6700\u5dee\u201d\u6709\u4e24\u5c42\u542b\u4e49\u3002
nums
\u5360\u7528 \\(O(n)\\) \u7a7a\u95f4\uff0c\u56e0\u6b64\u6700\u5dee\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002nums
\u65f6\uff0c\u7a0b\u5e8f\u5360\u7528 \\(O(n)\\) \u7a7a\u95f4\uff0c\u56e0\u6b64\u6700\u5dee\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002def algorithm(n: int):\n a = 0 # O(1)\n b = [0] * 10000 # O(1)\n if n > 10:\n nums = [0] * n # O(n)\n
void algorithm(int n) {\n int a = 0; // O(1)\n vector<int> b(10000); // O(1)\n if (n > 10)\n vector<int> nums(n); // O(n)\n}\n
void algorithm(int n) {\n int a = 0; // O(1)\n int[] b = new int[10000]; // O(1)\n if (n > 10)\n int[] nums = new int[n]; // O(n)\n}\n
void Algorithm(int n) {\n int a = 0; // O(1)\n int[] b = new int[10000]; // O(1)\n if (n > 10) {\n int[] nums = new int[n]; // O(n)\n }\n}\n
func algorithm(n int) {\n a := 0 // O(1)\n b := make([]int, 10000) // O(1)\n var nums []int\n if n > 10 {\n nums := make([]int, n) // O(n)\n }\n fmt.Println(a, b, nums)\n}\n
func algorithm(n: Int) {\n let a = 0 // O(1)\n let b = Array(repeating: 0, count: 10000) // O(1)\n if n > 10 {\n let nums = Array(repeating: 0, count: n) // O(n)\n }\n}\n
function algorithm(n) {\n const a = 0; // O(1)\n const b = new Array(10000); // O(1)\n if (n > 10) {\n const nums = new Array(n); // O(n)\n }\n}\n
function algorithm(n: number): void {\n const a = 0; // O(1)\n const b = new Array(10000); // O(1)\n if (n > 10) {\n const nums = new Array(n); // O(n)\n }\n}\n
void algorithm(int n) {\n int a = 0; // O(1)\n List<int> b = List.filled(10000, 0); // O(1)\n if (n > 10) {\n List<int> nums = List.filled(n, 0); // O(n)\n }\n}\n
fn algorithm(n: i32) {\n let a = 0; // O(1)\n let b = [0; 10000]; // O(1)\n if n > 10 {\n let nums = vec![0; n as usize]; // O(n)\n }\n}\n
void algorithm(int n) {\n int a = 0; // O(1)\n int b[10000]; // O(1)\n if (n > 10)\n int nums[n] = {0}; // O(n)\n}\n
\n
\u5728\u9012\u5f52\u51fd\u6570\u4e2d\uff0c\u9700\u8981\u6ce8\u610f\u7edf\u8ba1\u6808\u5e27\u7a7a\u95f4\u3002\u89c2\u5bdf\u4ee5\u4e0b\u4ee3\u7801\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZigdef function() -> int:\n # \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0\n\ndef loop(n: int):\n \"\"\"\u5faa\u73af O(1)\"\"\"\n for _ in range(n):\n function()\n\ndef recur(n: int) -> int:\n \"\"\"\u9012\u5f52 O(n)\"\"\"\n if n == 1: return\n return recur(n - 1)\n
int func() {\n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0;\n}\n/* \u5faa\u73af O(1) */\nvoid loop(int n) {\n for (int i = 0; i < n; i++) {\n func();\n }\n}\n/* \u9012\u5f52 O(n) */\nvoid recur(int n) {\n if (n == 1) return;\n return recur(n - 1);\n}\n
int function() {\n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0;\n}\n/* \u5faa\u73af O(1) */\nvoid loop(int n) {\n for (int i = 0; i < n; i++) {\n function();\n }\n}\n/* \u9012\u5f52 O(n) */\nvoid recur(int n) {\n if (n == 1) return;\n return recur(n - 1);\n}\n
int Function() {\n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0;\n}\n/* \u5faa\u73af O(1) */\nvoid Loop(int n) {\n for (int i = 0; i < n; i++) {\n Function();\n }\n}\n/* \u9012\u5f52 O(n) */\nint Recur(int n) {\n if (n == 1) return 1;\n return Recur(n - 1);\n}\n
func function() int {\n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0\n}\n\n/* \u5faa\u73af O(1) */\nfunc loop(n int) {\n for i := 0; i < n; i++ {\n function()\n }\n}\n\n/* \u9012\u5f52 O(n) */\nfunc recur(n int) {\n if n == 1 {\n return\n }\n recur(n - 1)\n}\n
@discardableResult\nfunc function() -> Int {\n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0\n}\n\n/* \u5faa\u73af O(1) */\nfunc loop(n: Int) {\n for _ in 0 ..< n {\n function()\n }\n}\n\n/* \u9012\u5f52 O(n) */\nfunc recur(n: Int) {\n if n == 1 {\n return\n }\n recur(n: n - 1)\n}\n
function constFunc() {\n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0;\n}\n/* \u5faa\u73af O(1) */\nfunction loop(n) {\n for (let i = 0; i < n; i++) {\n constFunc();\n }\n}\n/* \u9012\u5f52 O(n) */\nfunction recur(n) {\n if (n === 1) return;\n return recur(n - 1);\n}\n
function constFunc(): number {\n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0;\n}\n/* \u5faa\u73af O(1) */\nfunction loop(n: number): void {\n for (let i = 0; i < n; i++) {\n constFunc();\n }\n}\n/* \u9012\u5f52 O(n) */\nfunction recur(n: number): void {\n if (n === 1) return;\n return recur(n - 1);\n}\n
int function() {\n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0;\n}\n/* \u5faa\u73af O(1) */\nvoid loop(int n) {\n for (int i = 0; i < n; i++) {\n function();\n }\n}\n/* \u9012\u5f52 O(n) */\nvoid recur(int n) {\n if (n == 1) return;\n return recur(n - 1);\n}\n
fn function() -> i32 {\n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0;\n}\n/* \u5faa\u73af O(1) */\nfn loop(n: i32) {\n for i in 0..n {\n function();\n }\n}\n/* \u9012\u5f52 O(n) */\nvoid recur(n: i32) {\n if n == 1 {\n return;\n }\n recur(n - 1);\n}\n
int func() {\n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0;\n}\n/* \u5faa\u73af O(1) */\nvoid loop(int n) {\n for (int i = 0; i < n; i++) {\n func();\n }\n}\n/* \u9012\u5f52 O(n) */\nvoid recur(int n) {\n if (n == 1) return;\n return recur(n - 1);\n}\n
\n
\u51fd\u6570 loop()
\u548c recur()
\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u90fd\u4e3a \\(O(n)\\) \uff0c\u4f46\u7a7a\u95f4\u590d\u6742\u5ea6\u4e0d\u540c\u3002
loop()
\u5728\u5faa\u73af\u4e2d\u8c03\u7528\u4e86 \\(n\\) \u6b21 function()
\uff0c\u6bcf\u8f6e\u4e2d\u7684 function()
\u90fd\u8fd4\u56de\u5e76\u91ca\u653e\u4e86\u6808\u5e27\u7a7a\u95f4\uff0c\u56e0\u6b64\u7a7a\u95f4\u590d\u6742\u5ea6\u4ecd\u4e3a \\(O(1)\\) \u3002recur()
\u5728\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u4f1a\u540c\u65f6\u5b58\u5728 \\(n\\) \u4e2a\u672a\u8fd4\u56de\u7684 recur()
\uff0c\u4ece\u800c\u5360\u7528 \\(O(n)\\) \u7684\u6808\u5e27\u7a7a\u95f4\u3002\u8bbe\u8f93\u5165\u6570\u636e\u5927\u5c0f\u4e3a \\(n\\) \uff0c\u56fe 2-16 \u5c55\u793a\u4e86\u5e38\u89c1\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u7c7b\u578b\uff08\u4ece\u4f4e\u5230\u9ad8\u6392\u5217\uff09\u3002
\\[ \\begin{aligned} O(1) < O(\\log n) < O(n) < O(n^2) < O(2^n) \\newline \\text{\u5e38\u6570\u9636} < \\text{\u5bf9\u6570\u9636} < \\text{\u7ebf\u6027\u9636} < \\text{\u5e73\u65b9\u9636} < \\text{\u6307\u6570\u9636} \\end{aligned} \\]\u56fe 2-16 \u00a0 \u5e38\u89c1\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u7c7b\u578b
"},{"location":"chapter_computational_complexity/space_complexity/#1-o1","title":"1. \u00a0 \u5e38\u6570\u9636 \\(O(1)\\)","text":"\u5e38\u6570\u9636\u5e38\u89c1\u4e8e\u6570\u91cf\u4e0e\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u65e0\u5173\u7684\u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u3002
\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5728\u5faa\u73af\u4e2d\u521d\u59cb\u5316\u53d8\u91cf\u6216\u8c03\u7528\u51fd\u6570\u800c\u5360\u7528\u7684\u5185\u5b58\uff0c\u5728\u8fdb\u5165\u4e0b\u4e00\u5faa\u73af\u540e\u5c31\u4f1a\u88ab\u91ca\u653e\uff0c\u56e0\u6b64\u4e0d\u4f1a\u7d2f\u79ef\u5360\u7528\u7a7a\u95f4\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4ecd\u4e3a \\(O(1)\\) \uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig space_complexity.pydef function() -> int:\n \"\"\"\u51fd\u6570\"\"\"\n # \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0\n\ndef constant(n: int):\n \"\"\"\u5e38\u6570\u9636\"\"\"\n # \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\n a = 0\n nums = [0] * 10000\n node = ListNode(0)\n # \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\n for _ in range(n):\n c = 0\n # \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\n for _ in range(n):\n function()\n
space_complexity.cpp/* \u51fd\u6570 */\nint func() {\n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0;\n}\n\n/* \u5e38\u6570\u9636 */\nvoid constant(int n) {\n // \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\n const int a = 0;\n int b = 0;\n vector<int> nums(10000);\n ListNode node(0);\n // \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\n for (int i = 0; i < n; i++) {\n int c = 0;\n }\n // \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\n for (int i = 0; i < n; i++) {\n func();\n }\n}\n
space_complexity.java/* \u51fd\u6570 */\nint function() {\n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0;\n}\n\n/* \u5e38\u6570\u9636 */\nvoid constant(int n) {\n // \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\n final int a = 0;\n int b = 0;\n int[] nums = new int[10000];\n ListNode node = new ListNode(0);\n // \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\n for (int i = 0; i < n; i++) {\n int c = 0;\n }\n // \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\n for (int i = 0; i < n; i++) {\n function();\n }\n}\n
space_complexity.cs/* \u51fd\u6570 */\nint Function() {\n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0;\n}\n\n/* \u5e38\u6570\u9636 */\nvoid Constant(int n) {\n // \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\n int a = 0;\n int b = 0;\n int[] nums = new int[10000];\n ListNode node = new(0);\n // \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\n for (int i = 0; i < n; i++) {\n int c = 0;\n }\n // \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\n for (int i = 0; i < n; i++) {\n Function();\n }\n}\n
space_complexity.go/* \u51fd\u6570 */\nfunc function() int {\n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c...\n return 0\n}\n\n/* \u5e38\u6570\u9636 */\nfunc spaceConstant(n int) {\n // \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\n const a = 0\n b := 0\n nums := make([]int, 10000)\n ListNode := newNode(0)\n // \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\n var c int\n for i := 0; i < n; i++ {\n c = 0\n }\n // \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\n for i := 0; i < n; i++ {\n function()\n }\n fmt.Println(a, b, nums, c, ListNode)\n}\n
space_complexity.swift/* \u51fd\u6570 */\n@discardableResult\nfunc function() -> Int {\n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0\n}\n\n/* \u5e38\u6570\u9636 */\nfunc constant(n: Int) {\n // \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\n let a = 0\n var b = 0\n let nums = Array(repeating: 0, count: 10000)\n let node = ListNode(x: 0)\n // \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\n for _ in 0 ..< n {\n let c = 0\n }\n // \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\n for _ in 0 ..< n {\n function()\n }\n}\n
space_complexity.js/* \u51fd\u6570 */\nfunction constFunc() {\n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0;\n}\n\n/* \u5e38\u6570\u9636 */\nfunction constant(n) {\n // \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\n const a = 0;\n const b = 0;\n const nums = new Array(10000);\n const node = new ListNode(0);\n // \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\n for (let i = 0; i < n; i++) {\n const c = 0;\n }\n // \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\n for (let i = 0; i < n; i++) {\n constFunc();\n }\n}\n
space_complexity.ts/* \u51fd\u6570 */\nfunction constFunc(): number {\n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0;\n}\n\n/* \u5e38\u6570\u9636 */\nfunction constant(n: number): void {\n // \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\n const a = 0;\n const b = 0;\n const nums = new Array(10000);\n const node = new ListNode(0);\n // \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\n for (let i = 0; i < n; i++) {\n const c = 0;\n }\n // \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\n for (let i = 0; i < n; i++) {\n constFunc();\n }\n}\n
space_complexity.dart/* \u51fd\u6570 */\nint function() {\n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0;\n}\n\n/* \u5e38\u6570\u9636 */\nvoid constant(int n) {\n // \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\n final int a = 0;\n int b = 0;\n List<int> nums = List.filled(10000, 0);\n ListNode node = ListNode(0);\n // \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\n for (var i = 0; i < n; i++) {\n int c = 0;\n }\n // \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\n for (var i = 0; i < n; i++) {\n function();\n }\n}\n
space_complexity.rs/* \u51fd\u6570 */\nfn function() ->i32 {\n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0;\n}\n\n/* \u5e38\u6570\u9636 */\n#[allow(unused)]\nfn constant(n: i32) {\n // \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\n const A: i32 = 0;\n let b = 0;\n let nums = vec![0; 10000];\n let node = ListNode::new(0);\n // \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\n for i in 0..n {\n let c = 0;\n }\n // \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\n for i in 0..n {\n function();\n }\n}\n
space_complexity.c/* \u51fd\u6570 */\nint func() {\n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0;\n}\n\n/* \u5e38\u6570\u9636 */\nvoid constant(int n) {\n // \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\n const int a = 0;\n int b = 0;\n int nums[1000];\n ListNode *node = newListNode(0);\n free(node);\n // \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\n for (int i = 0; i < n; i++) {\n int c = 0;\n }\n // \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\n for (int i = 0; i < n; i++) {\n func();\n }\n}\n
space_complexity.zig// \u51fd\u6570\nfn function() i32 {\n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0;\n}\n\n// \u5e38\u6570\u9636\nfn constant(n: i32) void {\n // \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\n const a: i32 = 0;\n var b: i32 = 0;\n var nums = [_]i32{0}**10000;\n var node = inc.ListNode(i32){.val = 0};\n var i: i32 = 0;\n // \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\n while (i < n) : (i += 1) {\n var c: i32 = 0;\n _ = c;\n }\n // \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\n i = 0;\n while (i < n) : (i += 1) {\n _ = function();\n }\n _ = a;\n _ = b;\n _ = nums;\n _ = node;\n}\n
"},{"location":"chapter_computational_complexity/space_complexity/#2-on","title":"2. \u00a0 \u7ebf\u6027\u9636 \\(O(n)\\)","text":"\u7ebf\u6027\u9636\u5e38\u89c1\u4e8e\u5143\u7d20\u6570\u91cf\u4e0e \\(n\\) \u6210\u6b63\u6bd4\u7684\u6570\u7ec4\u3001\u94fe\u8868\u3001\u6808\u3001\u961f\u5217\u7b49\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig space_complexity.pydef linear(n: int):\n \"\"\"\u7ebf\u6027\u9636\"\"\"\n # \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n nums = [0] * n\n # \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n hmap = dict[int, str]()\n for i in range(n):\n hmap[i] = str(i)\n
space_complexity.cpp/* \u7ebf\u6027\u9636 */\nvoid linear(int n) {\n // \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\n vector<int> nums(n);\n // \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n vector<ListNode> nodes;\n for (int i = 0; i < n; i++) {\n nodes.push_back(ListNode(i));\n }\n // \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n unordered_map<int, string> map;\n for (int i = 0; i < n; i++) {\n map[i] = to_string(i);\n }\n}\n
space_complexity.java/* \u7ebf\u6027\u9636 */\nvoid linear(int n) {\n // \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\n int[] nums = new int[n];\n // \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n List<ListNode> nodes = new ArrayList<>();\n for (int i = 0; i < n; i++) {\n nodes.add(new ListNode(i));\n }\n // \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n Map<Integer, String> map = new HashMap<>();\n for (int i = 0; i < n; i++) {\n map.put(i, String.valueOf(i));\n }\n}\n
space_complexity.cs/* \u7ebf\u6027\u9636 */\nvoid Linear(int n) {\n // \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\n int[] nums = new int[n];\n // \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n List<ListNode> nodes = [];\n for (int i = 0; i < n; i++) {\n nodes.Add(new ListNode(i));\n }\n // \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n Dictionary<int, string> map = [];\n for (int i = 0; i < n; i++) {\n map.Add(i, i.ToString());\n }\n}\n
space_complexity.go/* \u7ebf\u6027\u9636 */\nfunc spaceLinear(n int) {\n // \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\n _ = make([]int, n)\n // \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n var nodes []*node\n for i := 0; i < n; i++ {\n nodes = append(nodes, newNode(i))\n }\n // \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n m := make(map[int]string, n)\n for i := 0; i < n; i++ {\n m[i] = strconv.Itoa(i)\n }\n}\n
space_complexity.swift/* \u7ebf\u6027\u9636 */\nfunc linear(n: Int) {\n // \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\n let nums = Array(repeating: 0, count: n)\n // \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n let nodes = (0 ..< n).map { ListNode(x: $0) }\n // \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n let map = Dictionary(uniqueKeysWithValues: (0 ..< n).map { ($0, \"\\($0)\") })\n}\n
space_complexity.js/* \u7ebf\u6027\u9636 */\nfunction linear(n) {\n // \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\n const nums = new Array(n);\n // \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n const nodes = [];\n for (let i = 0; i < n; i++) {\n nodes.push(new ListNode(i));\n }\n // \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n const map = new Map();\n for (let i = 0; i < n; i++) {\n map.set(i, i.toString());\n }\n}\n
space_complexity.ts/* \u7ebf\u6027\u9636 */\nfunction linear(n: number): void {\n // \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\n const nums = new Array(n);\n // \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n const nodes: ListNode[] = [];\n for (let i = 0; i < n; i++) {\n nodes.push(new ListNode(i));\n }\n // \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n const map = new Map();\n for (let i = 0; i < n; i++) {\n map.set(i, i.toString());\n }\n}\n
space_complexity.dart/* \u7ebf\u6027\u9636 */\nvoid linear(int n) {\n // \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\n List<int> nums = List.filled(n, 0);\n // \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n List<ListNode> nodes = [];\n for (var i = 0; i < n; i++) {\n nodes.add(ListNode(i));\n }\n // \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n Map<int, String> map = HashMap();\n for (var i = 0; i < n; i++) {\n map.putIfAbsent(i, () => i.toString());\n }\n}\n
space_complexity.rs/* \u7ebf\u6027\u9636 */\n#[allow(unused)]\nfn linear(n: i32) {\n // \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\n let mut nums = vec![0; n as usize];\n // \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n let mut nodes = Vec::new();\n for i in 0..n {\n nodes.push(ListNode::new(i))\n }\n // \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n let mut map = HashMap::new();\n for i in 0..n {\n map.insert(i, i.to_string());\n }\n}\n
space_complexity.c/* \u54c8\u5e0c\u8868 */\ntypedef struct {\n int key;\n int val;\n UT_hash_handle hh; // \u57fa\u4e8e uthash.h \u5b9e\u73b0\n} HashTable;\n\n/* \u7ebf\u6027\u9636 */\nvoid linear(int n) {\n // \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\n int *nums = malloc(sizeof(int) * n);\n free(nums);\n\n // \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n ListNode **nodes = malloc(sizeof(ListNode *) * n);\n for (int i = 0; i < n; i++) {\n nodes[i] = newListNode(i);\n }\n // \u5185\u5b58\u91ca\u653e\n for (int i = 0; i < n; i++) {\n free(nodes[i]);\n }\n free(nodes);\n\n // \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n HashTable *h = NULL;\n for (int i = 0; i < n; i++) {\n HashTable *tmp = malloc(sizeof(HashTable));\n tmp->key = i;\n tmp->val = i;\n HASH_ADD_INT(h, key, tmp);\n }\n\n // \u5185\u5b58\u91ca\u653e\n HashTable *curr, *tmp;\n HASH_ITER(hh, h, curr, tmp) {\n HASH_DEL(h, curr);\n free(curr);\n }\n}\n
space_complexity.zig// \u7ebf\u6027\u9636\nfn linear(comptime n: i32) !void {\n // \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\n var nums = [_]i32{0}**n;\n // \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n var nodes = std.ArrayList(i32).init(std.heap.page_allocator);\n defer nodes.deinit();\n var i: i32 = 0;\n while (i < n) : (i += 1) {\n try nodes.append(i);\n }\n // \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n var map = std.AutoArrayHashMap(i32, []const u8).init(std.heap.page_allocator);\n defer map.deinit();\n var j: i32 = 0;\n while (j < n) : (j += 1) {\n const string = try std.fmt.allocPrint(std.heap.page_allocator, \"{d}\", .{j});\n defer std.heap.page_allocator.free(string);\n try map.put(i, string);\n }\n _ = nums;\n}\n
\u5982\u56fe 2-17 \u6240\u793a\uff0c\u6b64\u51fd\u6570\u7684\u9012\u5f52\u6df1\u5ea6\u4e3a \\(n\\) \uff0c\u5373\u540c\u65f6\u5b58\u5728 \\(n\\) \u4e2a\u672a\u8fd4\u56de\u7684 linear_recur()
\u51fd\u6570\uff0c\u4f7f\u7528 \\(O(n)\\) \u5927\u5c0f\u7684\u6808\u5e27\u7a7a\u95f4\uff1a
def linear_recur(n: int):\n \"\"\"\u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\"\"\"\n print(\"\u9012\u5f52 n =\", n)\n if n == 1:\n return\n linear_recur(n - 1)\n
space_complexity.cpp/* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nvoid linearRecur(int n) {\n cout << \"\u9012\u5f52 n = \" << n << endl;\n if (n == 1)\n return;\n linearRecur(n - 1);\n}\n
space_complexity.java/* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nvoid linearRecur(int n) {\n System.out.println(\"\u9012\u5f52 n = \" + n);\n if (n == 1)\n return;\n linearRecur(n - 1);\n}\n
space_complexity.cs/* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nvoid LinearRecur(int n) {\n Console.WriteLine(\"\u9012\u5f52 n = \" + n);\n if (n == 1) return;\n LinearRecur(n - 1);\n}\n
space_complexity.go/* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunc spaceLinearRecur(n int) {\n fmt.Println(\"\u9012\u5f52 n =\", n)\n if n == 1 {\n return\n }\n spaceLinearRecur(n - 1)\n}\n
space_complexity.swift/* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunc linearRecur(n: Int) {\n print(\"\u9012\u5f52 n = \\(n)\")\n if n == 1 {\n return\n }\n linearRecur(n: n - 1)\n}\n
space_complexity.js/* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction linearRecur(n) {\n console.log(`\u9012\u5f52 n = ${n}`);\n if (n === 1) return;\n linearRecur(n - 1);\n}\n
space_complexity.ts/* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction linearRecur(n: number): void {\n console.log(`\u9012\u5f52 n = ${n}`);\n if (n === 1) return;\n linearRecur(n - 1);\n}\n
space_complexity.dart/* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nvoid linearRecur(int n) {\n print('\u9012\u5f52 n = $n');\n if (n == 1) return;\n linearRecur(n - 1);\n}\n
space_complexity.rs/* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfn linear_recur(n: i32) {\n println!(\"\u9012\u5f52 n = {}\", n);\n if n == 1 {return};\n linear_recur(n - 1);\n}\n
space_complexity.c/* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nvoid linearRecur(int n) {\n printf(\"\u9012\u5f52 n = %d\\r\\n\", n);\n if (n == 1)\n return;\n linearRecur(n - 1);\n}\n
space_complexity.zig// \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\nfn linearRecur(comptime n: i32) void {\n std.debug.print(\"\u9012\u5f52 n = {}\\n\", .{n});\n if (n == 1) return;\n linearRecur(n - 1);\n}\n
\u56fe 2-17 \u00a0 \u9012\u5f52\u51fd\u6570\u4ea7\u751f\u7684\u7ebf\u6027\u9636\u7a7a\u95f4\u590d\u6742\u5ea6
"},{"location":"chapter_computational_complexity/space_complexity/#3-on2","title":"3. \u00a0 \u5e73\u65b9\u9636 \\(O(n^2)\\)","text":"\u5e73\u65b9\u9636\u5e38\u89c1\u4e8e\u77e9\u9635\u548c\u56fe\uff0c\u5143\u7d20\u6570\u91cf\u4e0e \\(n\\) \u6210\u5e73\u65b9\u5173\u7cfb\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig space_complexity.pydef quadratic(n: int):\n \"\"\"\u5e73\u65b9\u9636\"\"\"\n # \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\n num_matrix = [[0] * n for _ in range(n)]\n
space_complexity.cpp/* \u5e73\u65b9\u9636 */\nvoid quadratic(int n) {\n // \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\n vector<vector<int>> numMatrix;\n for (int i = 0; i < n; i++) {\n vector<int> tmp;\n for (int j = 0; j < n; j++) {\n tmp.push_back(0);\n }\n numMatrix.push_back(tmp);\n }\n}\n
space_complexity.java/* \u5e73\u65b9\u9636 */\nvoid quadratic(int n) {\n // \u77e9\u9635\u5360\u7528 O(n^2) \u7a7a\u95f4\n int[][] numMatrix = new int[n][n];\n // \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\n List<List<Integer>> numList = new ArrayList<>();\n for (int i = 0; i < n; i++) {\n List<Integer> tmp = new ArrayList<>();\n for (int j = 0; j < n; j++) {\n tmp.add(0);\n }\n numList.add(tmp);\n }\n}\n
space_complexity.cs/* \u5e73\u65b9\u9636 */\nvoid Quadratic(int n) {\n // \u77e9\u9635\u5360\u7528 O(n^2) \u7a7a\u95f4\n int[,] numMatrix = new int[n, n];\n // \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\n List<List<int>> numList = [];\n for (int i = 0; i < n; i++) {\n List<int> tmp = [];\n for (int j = 0; j < n; j++) {\n tmp.Add(0);\n }\n numList.Add(tmp);\n }\n}\n
space_complexity.go/* \u5e73\u65b9\u9636 */\nfunc spaceQuadratic(n int) {\n // \u77e9\u9635\u5360\u7528 O(n^2) \u7a7a\u95f4\n numMatrix := make([][]int, n)\n for i := 0; i < n; i++ {\n numMatrix[i] = make([]int, n)\n }\n}\n
space_complexity.swift/* \u5e73\u65b9\u9636 */\nfunc quadratic(n: Int) {\n // \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\n let numList = Array(repeating: Array(repeating: 0, count: n), count: n)\n}\n
space_complexity.js/* \u5e73\u65b9\u9636 */\nfunction quadratic(n) {\n // \u77e9\u9635\u5360\u7528 O(n^2) \u7a7a\u95f4\n const numMatrix = Array(n)\n .fill(null)\n .map(() => Array(n).fill(null));\n // \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\n const numList = [];\n for (let i = 0; i < n; i++) {\n const tmp = [];\n for (let j = 0; j < n; j++) {\n tmp.push(0);\n }\n numList.push(tmp);\n }\n}\n
space_complexity.ts/* \u5e73\u65b9\u9636 */\nfunction quadratic(n: number): void {\n // \u77e9\u9635\u5360\u7528 O(n^2) \u7a7a\u95f4\n const numMatrix = Array(n)\n .fill(null)\n .map(() => Array(n).fill(null));\n // \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\n const numList = [];\n for (let i = 0; i < n; i++) {\n const tmp = [];\n for (let j = 0; j < n; j++) {\n tmp.push(0);\n }\n numList.push(tmp);\n }\n}\n
space_complexity.dart/* \u5e73\u65b9\u9636 */\nvoid quadratic(int n) {\n // \u77e9\u9635\u5360\u7528 O(n^2) \u7a7a\u95f4\n List<List<int>> numMatrix = List.generate(n, (_) => List.filled(n, 0));\n // \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\n List<List<int>> numList = [];\n for (var i = 0; i < n; i++) {\n List<int> tmp = [];\n for (int j = 0; j < n; j++) {\n tmp.add(0);\n }\n numList.add(tmp);\n }\n}\n
space_complexity.rs/* \u5e73\u65b9\u9636 */\n#[allow(unused)]\nfn quadratic(n: i32) {\n // \u77e9\u9635\u5360\u7528 O(n^2) \u7a7a\u95f4\n let num_matrix = vec![vec![0; n as usize]; n as usize];\n // \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\n let mut num_list = Vec::new();\n for i in 0..n {\n let mut tmp = Vec::new();\n for j in 0..n {\n tmp.push(0);\n }\n num_list.push(tmp);\n }\n}\n
space_complexity.c/* \u5e73\u65b9\u9636 */\nvoid quadratic(int n) {\n // \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\n int **numMatrix = malloc(sizeof(int *) * n);\n for (int i = 0; i < n; i++) {\n int *tmp = malloc(sizeof(int) * n);\n for (int j = 0; j < n; j++) {\n tmp[j] = 0;\n }\n numMatrix[i] = tmp;\n }\n\n // \u5185\u5b58\u91ca\u653e\n for (int i = 0; i < n; i++) {\n free(numMatrix[i]);\n }\n free(numMatrix);\n}\n
space_complexity.zig// \u5e73\u65b9\u9636\nfn quadratic(n: i32) !void {\n // \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\n var nodes = std.ArrayList(std.ArrayList(i32)).init(std.heap.page_allocator);\n defer nodes.deinit();\n var i: i32 = 0;\n while (i < n) : (i += 1) {\n var tmp = std.ArrayList(i32).init(std.heap.page_allocator);\n defer tmp.deinit();\n var j: i32 = 0;\n while (j < n) : (j += 1) {\n try tmp.append(0);\n }\n try nodes.append(tmp);\n }\n}\n
\u5982\u56fe 2-18 \u6240\u793a\uff0c\u8be5\u51fd\u6570\u7684\u9012\u5f52\u6df1\u5ea6\u4e3a \\(n\\) \uff0c\u5728\u6bcf\u4e2a\u9012\u5f52\u51fd\u6570\u4e2d\u90fd\u521d\u59cb\u5316\u4e86\u4e00\u4e2a\u6570\u7ec4\uff0c\u957f\u5ea6\u5206\u522b\u4e3a \\(n\\)\u3001\\(n-1\\)\u3001\\(\\dots\\)\u3001\\(2\\)\u3001\\(1\\) \uff0c\u5e73\u5747\u957f\u5ea6\u4e3a \\(n / 2\\) \uff0c\u56e0\u6b64\u603b\u4f53\u5360\u7528 \\(O(n^2)\\) \u7a7a\u95f4\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig space_complexity.pydef quadratic_recur(n: int) -> int:\n \"\"\"\u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\"\"\"\n if n <= 0:\n return 0\n # \u6570\u7ec4 nums \u957f\u5ea6\u4e3a n, n-1, ..., 2, 1\n nums = [0] * n\n return quadratic_recur(n - 1)\n
space_complexity.cpp/* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint quadraticRecur(int n) {\n if (n <= 0)\n return 0;\n vector<int> nums(n);\n cout << \"\u9012\u5f52 n = \" << n << \" \u4e2d\u7684 nums \u957f\u5ea6 = \" << nums.size() << endl;\n return quadraticRecur(n - 1);\n}\n
space_complexity.java/* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint quadraticRecur(int n) {\n if (n <= 0)\n return 0;\n // \u6570\u7ec4 nums \u957f\u5ea6\u4e3a n, n-1, ..., 2, 1\n int[] nums = new int[n];\n System.out.println(\"\u9012\u5f52 n = \" + n + \" \u4e2d\u7684 nums \u957f\u5ea6 = \" + nums.length);\n return quadraticRecur(n - 1);\n}\n
space_complexity.cs/* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint QuadraticRecur(int n) {\n if (n <= 0) return 0;\n int[] nums = new int[n];\n Console.WriteLine(\"\u9012\u5f52 n = \" + n + \" \u4e2d\u7684 nums \u957f\u5ea6 = \" + nums.Length);\n return QuadraticRecur(n - 1);\n}\n
space_complexity.go/* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunc spaceQuadraticRecur(n int) int {\n if n <= 0 {\n return 0\n }\n nums := make([]int, n)\n fmt.Printf(\"\u9012\u5f52 n = %d \u4e2d\u7684 nums \u957f\u5ea6 = %d \\n\", n, len(nums))\n return spaceQuadraticRecur(n - 1)\n}\n
space_complexity.swift/* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\n@discardableResult\nfunc quadraticRecur(n: Int) -> Int {\n if n <= 0 {\n return 0\n }\n // \u6570\u7ec4 nums \u957f\u5ea6\u4e3a n, n-1, ..., 2, 1\n let nums = Array(repeating: 0, count: n)\n print(\"\u9012\u5f52 n = \\(n) \u4e2d\u7684 nums \u957f\u5ea6 = \\(nums.count)\")\n return quadraticRecur(n: n - 1)\n}\n
space_complexity.js/* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction quadraticRecur(n) {\n if (n <= 0) return 0;\n const nums = new Array(n);\n console.log(`\u9012\u5f52 n = ${n} \u4e2d\u7684 nums \u957f\u5ea6 = ${nums.length}`);\n return quadraticRecur(n - 1);\n}\n
space_complexity.ts/* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction quadraticRecur(n: number): number {\n if (n <= 0) return 0;\n const nums = new Array(n);\n console.log(`\u9012\u5f52 n = ${n} \u4e2d\u7684 nums \u957f\u5ea6 = ${nums.length}`);\n return quadraticRecur(n - 1);\n}\n
space_complexity.dart/* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint quadraticRecur(int n) {\n if (n <= 0) return 0;\n List<int> nums = List.filled(n, 0);\n print('\u9012\u5f52 n = $n \u4e2d\u7684 nums \u957f\u5ea6 = ${nums.length}');\n return quadraticRecur(n - 1);\n}\n
space_complexity.rs/* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfn quadratic_recur(n: i32) -> i32 {\n if n <= 0 {return 0};\n // \u6570\u7ec4 nums \u957f\u5ea6\u4e3a n, n-1, ..., 2, 1\n let nums = vec![0; n as usize];\n println!(\"\u9012\u5f52 n = {} \u4e2d\u7684 nums \u957f\u5ea6 = {}\", n, nums.len());\n return quadratic_recur(n - 1);\n}\n
space_complexity.c/* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint quadraticRecur(int n) {\n if (n <= 0)\n return 0;\n int *nums = malloc(sizeof(int) * n);\n printf(\"\u9012\u5f52 n = %d \u4e2d\u7684 nums \u957f\u5ea6 = %d\\r\\n\", n, n);\n int res = quadraticRecur(n - 1);\n free(nums);\n return res;\n}\n
space_complexity.zig// \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\nfn quadraticRecur(comptime n: i32) i32 {\n if (n <= 0) return 0;\n var nums = [_]i32{0}**n;\n std.debug.print(\"\u9012\u5f52 n = {} \u4e2d\u7684 nums \u957f\u5ea6 = {}\\n\", .{n, nums.len});\n return quadraticRecur(n - 1);\n}\n
\u56fe 2-18 \u00a0 \u9012\u5f52\u51fd\u6570\u4ea7\u751f\u7684\u5e73\u65b9\u9636\u7a7a\u95f4\u590d\u6742\u5ea6
"},{"location":"chapter_computational_complexity/space_complexity/#4-o2n","title":"4. \u00a0 \u6307\u6570\u9636 \\(O(2^n)\\)","text":"\u6307\u6570\u9636\u5e38\u89c1\u4e8e\u4e8c\u53c9\u6811\u3002\u89c2\u5bdf\u56fe 2-19 \uff0c\u5c42\u6570\u4e3a \\(n\\) \u7684\u201c\u6ee1\u4e8c\u53c9\u6811\u201d\u7684\u8282\u70b9\u6570\u91cf\u4e3a \\(2^n - 1\\) \uff0c\u5360\u7528 \\(O(2^n)\\) \u7a7a\u95f4\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig space_complexity.pydef build_tree(n: int) -> TreeNode | None:\n \"\"\"\u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09\"\"\"\n if n == 0:\n return None\n root = TreeNode(0)\n root.left = build_tree(n - 1)\n root.right = build_tree(n - 1)\n return root\n
space_complexity.cpp/* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nTreeNode *buildTree(int n) {\n if (n == 0)\n return nullptr;\n TreeNode *root = new TreeNode(0);\n root->left = buildTree(n - 1);\n root->right = buildTree(n - 1);\n return root;\n}\n
space_complexity.java/* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nTreeNode buildTree(int n) {\n if (n == 0)\n return null;\n TreeNode root = new TreeNode(0);\n root.left = buildTree(n - 1);\n root.right = buildTree(n - 1);\n return root;\n}\n
space_complexity.cs/* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nTreeNode? BuildTree(int n) {\n if (n == 0) return null;\n TreeNode root = new(0) {\n left = BuildTree(n - 1),\n right = BuildTree(n - 1)\n };\n return root;\n}\n
space_complexity.go/* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nfunc buildTree(n int) *treeNode {\n if n == 0 {\n return nil\n }\n root := newTreeNode(0)\n root.left = buildTree(n - 1)\n root.right = buildTree(n - 1)\n return root\n}\n
space_complexity.swift/* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nfunc buildTree(n: Int) -> TreeNode? {\n if n == 0 {\n return nil\n }\n let root = TreeNode(x: 0)\n root.left = buildTree(n: n - 1)\n root.right = buildTree(n: n - 1)\n return root\n}\n
space_complexity.js/* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nfunction buildTree(n) {\n if (n === 0) return null;\n const root = new TreeNode(0);\n root.left = buildTree(n - 1);\n root.right = buildTree(n - 1);\n return root;\n}\n
space_complexity.ts/* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nfunction buildTree(n: number): TreeNode | null {\n if (n === 0) return null;\n const root = new TreeNode(0);\n root.left = buildTree(n - 1);\n root.right = buildTree(n - 1);\n return root;\n}\n
space_complexity.dart/* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nTreeNode? buildTree(int n) {\n if (n == 0) return null;\n TreeNode root = TreeNode(0);\n root.left = buildTree(n - 1);\n root.right = buildTree(n - 1);\n return root;\n}\n
space_complexity.rs/* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nfn build_tree(n: i32) -> Option<Rc<RefCell<TreeNode>>> {\n if n == 0 {return None};\n let root = TreeNode::new(0);\n root.borrow_mut().left = build_tree(n - 1);\n root.borrow_mut().right = build_tree(n - 1);\n return Some(root);\n}\n
space_complexity.c/* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nTreeNode *buildTree(int n) {\n if (n == 0)\n return NULL;\n TreeNode *root = newTreeNode(0);\n root->left = buildTree(n - 1);\n root->right = buildTree(n - 1);\n return root;\n}\n
space_complexity.zig// \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09\nfn buildTree(mem_allocator: std.mem.Allocator, n: i32) !?*inc.TreeNode(i32) {\n if (n == 0) return null;\n const root = try mem_allocator.create(inc.TreeNode(i32));\n root.init(0);\n root.left = try buildTree(mem_allocator, n - 1);\n root.right = try buildTree(mem_allocator, n - 1);\n return root;\n}\n
\u56fe 2-19 \u00a0 \u6ee1\u4e8c\u53c9\u6811\u4ea7\u751f\u7684\u6307\u6570\u9636\u7a7a\u95f4\u590d\u6742\u5ea6
"},{"location":"chapter_computational_complexity/space_complexity/#5-olog-n","title":"5. \u00a0 \u5bf9\u6570\u9636 \\(O(\\log n)\\)","text":"\u5bf9\u6570\u9636\u5e38\u89c1\u4e8e\u5206\u6cbb\u7b97\u6cd5\u3002\u4f8b\u5982\u5f52\u5e76\u6392\u5e8f\uff0c\u8f93\u5165\u957f\u5ea6\u4e3a \\(n\\) \u7684\u6570\u7ec4\uff0c\u6bcf\u8f6e\u9012\u5f52\u5c06\u6570\u7ec4\u4ece\u4e2d\u70b9\u5904\u5212\u5206\u4e3a\u4e24\u534a\uff0c\u5f62\u6210\u9ad8\u5ea6\u4e3a \\(\\log n\\) \u7684\u9012\u5f52\u6811\uff0c\u4f7f\u7528 \\(O(\\log n)\\) \u6808\u5e27\u7a7a\u95f4\u3002
\u518d\u4f8b\u5982\u5c06\u6570\u5b57\u8f6c\u5316\u4e3a\u5b57\u7b26\u4e32\uff0c\u8f93\u5165\u4e00\u4e2a\u6b63\u6574\u6570 \\(n\\) \uff0c\u5b83\u7684\u4f4d\u6570\u4e3a \\(\\log_{10} n + 1\\) \uff0c\u5373\u5bf9\u5e94\u5b57\u7b26\u4e32\u957f\u5ea6\u4e3a \\(\\log_{10} n + 1\\) \uff0c\u56e0\u6b64\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(\\log_{10} n + 1) = O(\\log n)\\) \u3002
"},{"location":"chapter_computational_complexity/space_complexity/#244","title":"2.4.4 \u00a0 \u6743\u8861\u65f6\u95f4\u4e0e\u7a7a\u95f4","text":"\u7406\u60f3\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u5e0c\u671b\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u548c\u7a7a\u95f4\u590d\u6742\u5ea6\u90fd\u80fd\u8fbe\u5230\u6700\u4f18\u3002\u7136\u800c\u5728\u5b9e\u9645\u60c5\u51b5\u4e2d\uff0c\u540c\u65f6\u4f18\u5316\u65f6\u95f4\u590d\u6742\u5ea6\u548c\u7a7a\u95f4\u590d\u6742\u5ea6\u901a\u5e38\u975e\u5e38\u56f0\u96be\u3002
\u964d\u4f4e\u65f6\u95f4\u590d\u6742\u5ea6\u901a\u5e38\u9700\u8981\u4ee5\u63d0\u5347\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a\u4ee3\u4ef7\uff0c\u53cd\u4e4b\u4ea6\u7136\u3002\u6211\u4eec\u5c06\u727a\u7272\u5185\u5b58\u7a7a\u95f4\u6765\u63d0\u5347\u7b97\u6cd5\u8fd0\u884c\u901f\u5ea6\u7684\u601d\u8def\u79f0\u4e3a\u201c\u4ee5\u7a7a\u95f4\u6362\u65f6\u95f4\u201d\uff1b\u53cd\u4e4b\uff0c\u5219\u79f0\u4e3a\u201c\u4ee5\u65f6\u95f4\u6362\u7a7a\u95f4\u201d\u3002
\u9009\u62e9\u54ea\u79cd\u601d\u8def\u53d6\u51b3\u4e8e\u6211\u4eec\u66f4\u770b\u91cd\u54ea\u4e2a\u65b9\u9762\u3002\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u65f6\u95f4\u6bd4\u7a7a\u95f4\u66f4\u5b9d\u8d35\uff0c\u56e0\u6b64\u201c\u4ee5\u7a7a\u95f4\u6362\u65f6\u95f4\u201d\u901a\u5e38\u662f\u66f4\u5e38\u7528\u7684\u7b56\u7565\u3002\u5f53\u7136\uff0c\u5728\u6570\u636e\u91cf\u5f88\u5927\u7684\u60c5\u51b5\u4e0b\uff0c\u63a7\u5236\u7a7a\u95f4\u590d\u6742\u5ea6\u4e5f\u975e\u5e38\u91cd\u8981\u3002
"},{"location":"chapter_computational_complexity/summary/","title":"2.5 \u00a0 \u5c0f\u7ed3","text":""},{"location":"chapter_computational_complexity/summary/#1","title":"1. \u00a0 \u91cd\u70b9\u56de\u987e","text":"\u7b97\u6cd5\u6548\u7387\u8bc4\u4f30
\u65f6\u95f4\u590d\u6742\u5ea6
\u7a7a\u95f4\u590d\u6742\u5ea6
\u5c3e\u9012\u5f52\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u662f \\(O(1)\\) \u5417\uff1f
\u7406\u8bba\u4e0a\uff0c\u5c3e\u9012\u5f52\u51fd\u6570\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u53ef\u4ee5\u4f18\u5316\u81f3 \\(O(1)\\) \u3002\u4e0d\u8fc7\u7edd\u5927\u591a\u6570\u7f16\u7a0b\u8bed\u8a00\uff08\u4f8b\u5982 Java\u3001Python\u3001C++\u3001Go\u3001C# \u7b49\uff09\u4e0d\u652f\u6301\u81ea\u52a8\u4f18\u5316\u5c3e\u9012\u5f52\uff0c\u56e0\u6b64\u901a\u5e38\u8ba4\u4e3a\u7a7a\u95f4\u590d\u6742\u5ea6\u662f \\(O(n)\\) \u3002
\u51fd\u6570\u548c\u65b9\u6cd5\u8fd9\u4e24\u4e2a\u672f\u8bed\u7684\u533a\u522b\u662f\u4ec0\u4e48\uff1f
\u300c\u51fd\u6570 function\u300d\u53ef\u4ee5\u88ab\u72ec\u7acb\u6267\u884c\uff0c\u6240\u6709\u53c2\u6570\u90fd\u4ee5\u663e\u5f0f\u4f20\u9012\u3002\u300c\u65b9\u6cd5 method\u300d\u4e0e\u4e00\u4e2a\u5bf9\u8c61\u5173\u8054\uff0c\u88ab\u9690\u5f0f\u4f20\u9012\u7ed9\u8c03\u7528\u5b83\u7684\u5bf9\u8c61\uff0c\u80fd\u591f\u5bf9\u7c7b\u7684\u5b9e\u4f8b\u4e2d\u5305\u542b\u7684\u6570\u636e\u8fdb\u884c\u64cd\u4f5c\u3002
\u4e0b\u9762\u4ee5\u51e0\u79cd\u5e38\u89c1\u7684\u7f16\u7a0b\u8bed\u8a00\u4e3a\u4f8b\u6765\u8bf4\u660e\u3002
\u56fe\u89e3\u201c\u5e38\u89c1\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u7c7b\u578b\u201d\u53cd\u6620\u7684\u662f\u5426\u662f\u5360\u7528\u7a7a\u95f4\u7684\u7edd\u5bf9\u5927\u5c0f\uff1f
\u4e0d\u662f\uff0c\u8be5\u56fe\u5c55\u793a\u7684\u662f\u7a7a\u95f4\u590d\u6742\u5ea6\uff0c\u5176\u53cd\u6620\u7684\u662f\u589e\u957f\u8d8b\u52bf\uff0c\u800c\u4e0d\u662f\u5360\u7528\u7a7a\u95f4\u7684\u7edd\u5bf9\u5927\u5c0f\u3002
\u5047\u8bbe\u53d6 \\(n = 8\\) \uff0c\u4f60\u53ef\u80fd\u4f1a\u53d1\u73b0\u6bcf\u6761\u66f2\u7ebf\u7684\u503c\u4e0e\u51fd\u6570\u5bf9\u5e94\u4e0d\u4e0a\u3002\u8fd9\u662f\u56e0\u4e3a\u6bcf\u6761\u66f2\u7ebf\u90fd\u5305\u542b\u4e00\u4e2a\u5e38\u6570\u9879\uff0c\u7528\u4e8e\u5c06\u53d6\u503c\u8303\u56f4\u538b\u7f29\u5230\u4e00\u4e2a\u89c6\u89c9\u8212\u9002\u7684\u8303\u56f4\u5185\u3002
\u5728\u5b9e\u9645\u4e2d\uff0c\u56e0\u4e3a\u6211\u4eec\u901a\u5e38\u4e0d\u77e5\u9053\u6bcf\u4e2a\u65b9\u6cd5\u7684\u201c\u5e38\u6570\u9879\u201d\u590d\u6742\u5ea6\u662f\u591a\u5c11\uff0c\u6240\u4ee5\u4e00\u822c\u65e0\u6cd5\u4ec5\u51ed\u590d\u6742\u5ea6\u6765\u9009\u62e9 \\(n = 8\\) \u4e4b\u4e0b\u7684\u6700\u4f18\u89e3\u6cd5\u3002\u4f46\u5bf9\u4e8e \\(n = 8^5\\) \u5c31\u5f88\u597d\u9009\u4e86\uff0c\u8fd9\u65f6\u589e\u957f\u8d8b\u52bf\u5df2\u7ecf\u5360\u4e3b\u5bfc\u4e86\u3002
"},{"location":"chapter_computational_complexity/time_complexity/","title":"2.3 \u00a0 \u65f6\u95f4\u590d\u6742\u5ea6","text":"\u8fd0\u884c\u65f6\u95f4\u53ef\u4ee5\u76f4\u89c2\u4e14\u51c6\u786e\u5730\u53cd\u6620\u7b97\u6cd5\u7684\u6548\u7387\u3002\u5982\u679c\u6211\u4eec\u60f3\u51c6\u786e\u9884\u4f30\u4e00\u6bb5\u4ee3\u7801\u7684\u8fd0\u884c\u65f6\u95f4\uff0c\u5e94\u8be5\u5982\u4f55\u64cd\u4f5c\u5462\uff1f
+
\u9700\u8981 1 ns \uff0c\u4e58\u6cd5\u64cd\u4f5c *
\u9700\u8981 10 ns \uff0c\u6253\u5370\u64cd\u4f5c print()
\u9700\u8981 5 ns \u7b49\u3002\u4f8b\u5982\u5728\u4ee5\u4e0b\u4ee3\u7801\u4e2d\uff0c\u8f93\u5165\u6570\u636e\u5927\u5c0f\u4e3a \\(n\\) \uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig# \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\ndef algorithm(n: int):\n a = 2 # 1 ns\n a = a + 1 # 1 ns\n a = a * 2 # 10 ns\n # \u5faa\u73af n \u6b21\n for _ in range(n): # 1 ns\n print(0) # 5 ns\n
// \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\nvoid algorithm(int n) {\n int a = 2; // 1 ns\n a = a + 1; // 1 ns\n a = a * 2; // 10 ns\n // \u5faa\u73af n \u6b21\n for (int i = 0; i < n; i++) { // 1 ns \uff0c\u6bcf\u8f6e\u90fd\u8981\u6267\u884c i++\n cout << 0 << endl; // 5 ns\n }\n}\n
// \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\nvoid algorithm(int n) {\n int a = 2; // 1 ns\n a = a + 1; // 1 ns\n a = a * 2; // 10 ns\n // \u5faa\u73af n \u6b21\n for (int i = 0; i < n; i++) { // 1 ns \uff0c\u6bcf\u8f6e\u90fd\u8981\u6267\u884c i++\n System.out.println(0); // 5 ns\n }\n}\n
// \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\nvoid Algorithm(int n) {\n int a = 2; // 1 ns\n a = a + 1; // 1 ns\n a = a * 2; // 10 ns\n // \u5faa\u73af n \u6b21\n for (int i = 0; i < n; i++) { // 1 ns \uff0c\u6bcf\u8f6e\u90fd\u8981\u6267\u884c i++\n Console.WriteLine(0); // 5 ns\n }\n}\n
// \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\nfunc algorithm(n int) {\n a := 2 // 1 ns\n a = a + 1 // 1 ns\n a = a * 2 // 10 ns\n // \u5faa\u73af n \u6b21\n for i := 0; i < n; i++ { // 1 ns\n fmt.Println(a) // 5 ns\n }\n}\n
// \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\nfunc algorithm(n: Int) {\n var a = 2 // 1 ns\n a = a + 1 // 1 ns\n a = a * 2 // 10 ns\n // \u5faa\u73af n \u6b21\n for _ in 0 ..< n { // 1 ns\n print(0) // 5 ns\n }\n}\n
// \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\nfunction algorithm(n) {\n var a = 2; // 1 ns\n a = a + 1; // 1 ns\n a = a * 2; // 10 ns\n // \u5faa\u73af n \u6b21\n for(let i = 0; i < n; i++) { // 1 ns \uff0c\u6bcf\u8f6e\u90fd\u8981\u6267\u884c i++\n console.log(0); // 5 ns\n }\n}\n
// \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\nfunction algorithm(n: number): void {\n var a: number = 2; // 1 ns\n a = a + 1; // 1 ns\n a = a * 2; // 10 ns\n // \u5faa\u73af n \u6b21\n for(let i = 0; i < n; i++) { // 1 ns \uff0c\u6bcf\u8f6e\u90fd\u8981\u6267\u884c i++\n console.log(0); // 5 ns\n }\n}\n
// \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\nvoid algorithm(int n) {\n int a = 2; // 1 ns\n a = a + 1; // 1 ns\n a = a * 2; // 10 ns\n // \u5faa\u73af n \u6b21\n for (int i = 0; i < n; i++) { // 1 ns \uff0c\u6bcf\u8f6e\u90fd\u8981\u6267\u884c i++\n print(0); // 5 ns\n }\n}\n
// \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\nfn algorithm(n: i32) {\n let mut a = 2; // 1 ns\n a = a + 1; // 1 ns\n a = a * 2; // 10 ns\n // \u5faa\u73af n \u6b21\n for _ in 0..n { // 1 ns \uff0c\u6bcf\u8f6e\u90fd\u8981\u6267\u884c i++\n println!(\"{}\", 0); // 5 ns\n }\n}\n
// \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\nvoid algorithm(int n) {\n int a = 2; // 1 ns\n a = a + 1; // 1 ns\n a = a * 2; // 10 ns\n // \u5faa\u73af n \u6b21\n for (int i = 0; i < n; i++) { // 1 ns \uff0c\u6bcf\u8f6e\u90fd\u8981\u6267\u884c i++\n printf(\"%d\", 0); // 5 ns\n }\n}\n
// \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\nfn algorithm(n: usize) void {\n var a: i32 = 2; // 1 ns\n a += 1; // 1 ns\n a *= 2; // 10 ns\n // \u5faa\u73af n \u6b21\n for (0..n) |_| { // 1 ns\n std.debug.print(\"{}\\n\", .{0}); // 5 ns\n }\n}\n
\u6839\u636e\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5f97\u5230\u7b97\u6cd5\u7684\u8fd0\u884c\u65f6\u95f4\u4e3a \\((6n + 12)\\) ns \uff1a
\\[ 1 + 1 + 10 + (1 + 5) \\times n = 6n + 12 \\]\u4f46\u5b9e\u9645\u4e0a\uff0c\u7edf\u8ba1\u7b97\u6cd5\u7684\u8fd0\u884c\u65f6\u95f4\u65e2\u4e0d\u5408\u7406\u4e5f\u4e0d\u73b0\u5b9e\u3002\u9996\u5148\uff0c\u6211\u4eec\u4e0d\u5e0c\u671b\u5c06\u9884\u4f30\u65f6\u95f4\u548c\u8fd0\u884c\u5e73\u53f0\u7ed1\u5b9a\uff0c\u56e0\u4e3a\u7b97\u6cd5\u9700\u8981\u5728\u5404\u79cd\u4e0d\u540c\u7684\u5e73\u53f0\u4e0a\u8fd0\u884c\u3002\u5176\u6b21\uff0c\u6211\u4eec\u5f88\u96be\u83b7\u77e5\u6bcf\u79cd\u64cd\u4f5c\u7684\u8fd0\u884c\u65f6\u95f4\uff0c\u8fd9\u7ed9\u9884\u4f30\u8fc7\u7a0b\u5e26\u6765\u4e86\u6781\u5927\u7684\u96be\u5ea6\u3002
"},{"location":"chapter_computational_complexity/time_complexity/#231","title":"2.3.1 \u00a0 \u7edf\u8ba1\u65f6\u95f4\u589e\u957f\u8d8b\u52bf","text":"\u65f6\u95f4\u590d\u6742\u5ea6\u5206\u6790\u7edf\u8ba1\u7684\u4e0d\u662f\u7b97\u6cd5\u8fd0\u884c\u65f6\u95f4\uff0c\u800c\u662f\u7b97\u6cd5\u8fd0\u884c\u65f6\u95f4\u968f\u7740\u6570\u636e\u91cf\u53d8\u5927\u65f6\u7684\u589e\u957f\u8d8b\u52bf\u3002
\u201c\u65f6\u95f4\u589e\u957f\u8d8b\u52bf\u201d\u8fd9\u4e2a\u6982\u5ff5\u6bd4\u8f83\u62bd\u8c61\uff0c\u6211\u4eec\u901a\u8fc7\u4e00\u4e2a\u4f8b\u5b50\u6765\u52a0\u4ee5\u7406\u89e3\u3002\u5047\u8bbe\u8f93\u5165\u6570\u636e\u5927\u5c0f\u4e3a \\(n\\) \uff0c\u7ed9\u5b9a\u4e09\u4e2a\u7b97\u6cd5 A
\u3001B
\u548c C
\uff1a
# \u7b97\u6cd5 A \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\ndef algorithm_A(n: int):\n print(0)\n# \u7b97\u6cd5 B \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\ndef algorithm_B(n: int):\n for _ in range(n):\n print(0)\n# \u7b97\u6cd5 C \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\ndef algorithm_C(n: int):\n for _ in range(1000000):\n print(0)\n
// \u7b97\u6cd5 A \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithm_A(int n) {\n cout << 0 << endl;\n}\n// \u7b97\u6cd5 B \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nvoid algorithm_B(int n) {\n for (int i = 0; i < n; i++) {\n cout << 0 << endl;\n }\n}\n// \u7b97\u6cd5 C \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithm_C(int n) {\n for (int i = 0; i < 1000000; i++) {\n cout << 0 << endl;\n }\n}\n
// \u7b97\u6cd5 A \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithm_A(int n) {\n System.out.println(0);\n}\n// \u7b97\u6cd5 B \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nvoid algorithm_B(int n) {\n for (int i = 0; i < n; i++) {\n System.out.println(0);\n }\n}\n// \u7b97\u6cd5 C \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithm_C(int n) {\n for (int i = 0; i < 1000000; i++) {\n System.out.println(0);\n }\n}\n
// \u7b97\u6cd5 A \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid AlgorithmA(int n) {\n Console.WriteLine(0);\n}\n// \u7b97\u6cd5 B \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nvoid AlgorithmB(int n) {\n for (int i = 0; i < n; i++) {\n Console.WriteLine(0);\n }\n}\n// \u7b97\u6cd5 C \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid AlgorithmC(int n) {\n for (int i = 0; i < 1000000; i++) {\n Console.WriteLine(0);\n }\n}\n
// \u7b97\u6cd5 A \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfunc algorithm_A(n int) {\n fmt.Println(0)\n}\n// \u7b97\u6cd5 B \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nfunc algorithm_B(n int) {\n for i := 0; i < n; i++ {\n fmt.Println(0)\n }\n}\n// \u7b97\u6cd5 C \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfunc algorithm_C(n int) {\n for i := 0; i < 1000000; i++ {\n fmt.Println(0)\n }\n}\n
// \u7b97\u6cd5 A \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfunc algorithmA(n: Int) {\n print(0)\n}\n\n// \u7b97\u6cd5 B \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nfunc algorithmB(n: Int) {\n for _ in 0 ..< n {\n print(0)\n }\n}\n\n// \u7b97\u6cd5 C \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfunc algorithmC(n: Int) {\n for _ in 0 ..< 1000000 {\n print(0)\n }\n}\n
// \u7b97\u6cd5 A \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfunction algorithm_A(n) {\n console.log(0);\n}\n// \u7b97\u6cd5 B \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nfunction algorithm_B(n) {\n for (let i = 0; i < n; i++) {\n console.log(0);\n }\n}\n// \u7b97\u6cd5 C \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfunction algorithm_C(n) {\n for (let i = 0; i < 1000000; i++) {\n console.log(0);\n }\n}\n
// \u7b97\u6cd5 A \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfunction algorithm_A(n: number): void {\n console.log(0);\n}\n// \u7b97\u6cd5 B \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nfunction algorithm_B(n: number): void {\n for (let i = 0; i < n; i++) {\n console.log(0);\n }\n}\n// \u7b97\u6cd5 C \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfunction algorithm_C(n: number): void {\n for (let i = 0; i < 1000000; i++) {\n console.log(0);\n }\n}\n
// \u7b97\u6cd5 A \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithmA(int n) {\n print(0);\n}\n// \u7b97\u6cd5 B \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nvoid algorithmB(int n) {\n for (int i = 0; i < n; i++) {\n print(0);\n }\n}\n// \u7b97\u6cd5 C \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithmC(int n) {\n for (int i = 0; i < 1000000; i++) {\n print(0);\n }\n}\n
// \u7b97\u6cd5 A \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfn algorithm_A(n: i32) {\n println!(\"{}\", 0);\n}\n// \u7b97\u6cd5 B \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nfn algorithm_B(n: i32) {\n for _ in 0..n {\n println!(\"{}\", 0);\n }\n}\n// \u7b97\u6cd5 C \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfn algorithm_C(n: i32) {\n for _ in 0..1000000 {\n println!(\"{}\", 0);\n }\n}\n
// \u7b97\u6cd5 A \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithm_A(int n) {\n printf(\"%d\", 0);\n}\n// \u7b97\u6cd5 B \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nvoid algorithm_B(int n) {\n for (int i = 0; i < n; i++) {\n printf(\"%d\", 0);\n }\n}\n// \u7b97\u6cd5 C \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithm_C(int n) {\n for (int i = 0; i < 1000000; i++) {\n printf(\"%d\", 0);\n }\n}\n
// \u7b97\u6cd5 A \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfn algorithm_A(n: usize) void {\n _ = n;\n std.debug.print(\"{}\\n\", .{0});\n}\n// \u7b97\u6cd5 B \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nfn algorithm_B(n: i32) void {\n for (0..n) |_| {\n std.debug.print(\"{}\\n\", .{0});\n }\n}\n// \u7b97\u6cd5 C \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfn algorithm_C(n: i32) void {\n _ = n;\n for (0..1000000) |_| { \n std.debug.print(\"{}\\n\", .{0});\n }\n}\n
\u56fe 2-7 \u5c55\u793a\u4e86\u4ee5\u4e0a\u4e09\u4e2a\u7b97\u6cd5\u51fd\u6570\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u3002
A
\u53ea\u6709 \\(1\\) \u4e2a\u6253\u5370\u64cd\u4f5c\uff0c\u7b97\u6cd5\u8fd0\u884c\u65f6\u95f4\u4e0d\u968f\u7740 \\(n\\) \u589e\u5927\u800c\u589e\u957f\u3002\u6211\u4eec\u79f0\u6b64\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a\u201c\u5e38\u6570\u9636\u201d\u3002B
\u4e2d\u7684\u6253\u5370\u64cd\u4f5c\u9700\u8981\u5faa\u73af \\(n\\) \u6b21\uff0c\u7b97\u6cd5\u8fd0\u884c\u65f6\u95f4\u968f\u7740 \\(n\\) \u589e\u5927\u5448\u7ebf\u6027\u589e\u957f\u3002\u6b64\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u88ab\u79f0\u4e3a\u201c\u7ebf\u6027\u9636\u201d\u3002C
\u4e2d\u7684\u6253\u5370\u64cd\u4f5c\u9700\u8981\u5faa\u73af \\(1000000\\) \u6b21\uff0c\u867d\u7136\u8fd0\u884c\u65f6\u95f4\u5f88\u957f\uff0c\u4f46\u5b83\u4e0e\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u65e0\u5173\u3002\u56e0\u6b64 C
\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u548c A
\u76f8\u540c\uff0c\u4ecd\u4e3a\u201c\u5e38\u6570\u9636\u201d\u3002\u56fe 2-7 \u00a0 \u7b97\u6cd5 A\u3001B \u548c C \u7684\u65f6\u95f4\u589e\u957f\u8d8b\u52bf
\u76f8\u8f83\u4e8e\u76f4\u63a5\u7edf\u8ba1\u7b97\u6cd5\u7684\u8fd0\u884c\u65f6\u95f4\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u5206\u6790\u6709\u54ea\u4e9b\u7279\u70b9\u5462\uff1f
B
\u7684\u8fd0\u884c\u65f6\u95f4\u5448\u7ebf\u6027\u589e\u957f\uff0c\u5728 \\(n > 1\\) \u65f6\u6bd4\u7b97\u6cd5 A
\u66f4\u6162\uff0c\u5728 \\(n > 1000000\\) \u65f6\u6bd4\u7b97\u6cd5 C
\u66f4\u6162\u3002\u4e8b\u5b9e\u4e0a\uff0c\u53ea\u8981\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u8db3\u591f\u5927\uff0c\u590d\u6742\u5ea6\u4e3a\u201c\u5e38\u6570\u9636\u201d\u7684\u7b97\u6cd5\u4e00\u5b9a\u4f18\u4e8e\u201c\u7ebf\u6027\u9636\u201d\u7684\u7b97\u6cd5\uff0c\u8fd9\u6b63\u662f\u65f6\u95f4\u589e\u957f\u8d8b\u52bf\u7684\u542b\u4e49\u3002A
\u548c C
\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u76f8\u540c\uff0c\u4f46\u5b9e\u9645\u8fd0\u884c\u65f6\u95f4\u5dee\u522b\u5f88\u5927\u3002\u540c\u6837\uff0c\u5c3d\u7ba1\u7b97\u6cd5 B
\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u6bd4 C
\u9ad8\uff0c\u4f46\u5728\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u8f83\u5c0f\u65f6\uff0c\u7b97\u6cd5 B
\u660e\u663e\u4f18\u4e8e\u7b97\u6cd5 C
\u3002\u5728\u8fd9\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u5f88\u96be\u4ec5\u51ed\u65f6\u95f4\u590d\u6742\u5ea6\u5224\u65ad\u7b97\u6cd5\u6548\u7387\u7684\u9ad8\u4f4e\u3002\u5f53\u7136\uff0c\u5c3d\u7ba1\u5b58\u5728\u4e0a\u8ff0\u95ee\u9898\uff0c\u590d\u6742\u5ea6\u5206\u6790\u4ecd\u7136\u662f\u8bc4\u5224\u7b97\u6cd5\u6548\u7387\u6700\u6709\u6548\u4e14\u5e38\u7528\u7684\u65b9\u6cd5\u3002\u7ed9\u5b9a\u4e00\u4e2a\u8f93\u5165\u5927\u5c0f\u4e3a \\(n\\) \u7684\u51fd\u6570\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZigdef algorithm(n: int):\n a = 1 # +1\n a = a + 1 # +1\n a = a * 2 # +1\n # \u5faa\u73af n \u6b21\n for i in range(n): # +1\n print(0) # +1\n
void algorithm(int n) {\n int a = 1; // +1\n a = a + 1; // +1\n a = a * 2; // +1\n // \u5faa\u73af n \u6b21\n for (int i = 0; i < n; i++) { // +1\uff08\u6bcf\u8f6e\u90fd\u6267\u884c i ++\uff09\n cout << 0 << endl; // +1\n }\n}\n
void algorithm(int n) {\n int a = 1; // +1\n a = a + 1; // +1\n a = a * 2; // +1\n // \u5faa\u73af n \u6b21\n for (int i = 0; i < n; i++) { // +1\uff08\u6bcf\u8f6e\u90fd\u6267\u884c i ++\uff09\n System.out.println(0); // +1\n }\n}\n
void Algorithm(int n) {\n int a = 1; // +1\n a = a + 1; // +1\n a = a * 2; // +1\n // \u5faa\u73af n \u6b21\n for (int i = 0; i < n; i++) { // +1\uff08\u6bcf\u8f6e\u90fd\u6267\u884c i ++\uff09\n Console.WriteLine(0); // +1\n }\n}\n
func algorithm(n int) {\n a := 1 // +1\n a = a + 1 // +1\n a = a * 2 // +1\n // \u5faa\u73af n \u6b21\n for i := 0; i < n; i++ { // +1\n fmt.Println(a) // +1\n }\n}\n
func algorithm(n: Int) {\n var a = 1 // +1\n a = a + 1 // +1\n a = a * 2 // +1\n // \u5faa\u73af n \u6b21\n for _ in 0 ..< n { // +1\n print(0) // +1\n }\n}\n
function algorithm(n) {\n var a = 1; // +1\n a += 1; // +1\n a *= 2; // +1\n // \u5faa\u73af n \u6b21\n for(let i = 0; i < n; i++){ // +1\uff08\u6bcf\u8f6e\u90fd\u6267\u884c i ++\uff09\n console.log(0); // +1\n }\n}\n
function algorithm(n: number): void{\n var a: number = 1; // +1\n a += 1; // +1\n a *= 2; // +1\n // \u5faa\u73af n \u6b21\n for(let i = 0; i < n; i++){ // +1\uff08\u6bcf\u8f6e\u90fd\u6267\u884c i ++\uff09\n console.log(0); // +1\n }\n}\n
void algorithm(int n) {\n int a = 1; // +1\n a = a + 1; // +1\n a = a * 2; // +1\n // \u5faa\u73af n \u6b21\n for (int i = 0; i < n; i++) { // +1\uff08\u6bcf\u8f6e\u90fd\u6267\u884c i ++\uff09\n print(0); // +1\n }\n}\n
fn algorithm(n: i32) {\n let mut a = 1; // +1\n a = a + 1; // +1\n a = a * 2; // +1\n\n // \u5faa\u73af n \u6b21\n for _ in 0..n { // +1\uff08\u6bcf\u8f6e\u90fd\u6267\u884c i ++\uff09\n println!(\"{}\", 0); // +1\n }\n}\n
void algorithm(int n) {\n int a = 1; // +1\n a = a + 1; // +1\n a = a * 2; // +1\n // \u5faa\u73af n \u6b21\n for (int i = 0; i < n; i++) { // +1\uff08\u6bcf\u8f6e\u90fd\u6267\u884c i ++\uff09\n printf(\"%d\", 0); // +1\n }\n} \n
fn algorithm(n: usize) void {\n var a: i32 = 1; // +1\n a += 1; // +1\n a *= 2; // +1\n // \u5faa\u73af n \u6b21\n for (0..n) |_| { // +1\uff08\u6bcf\u8f6e\u90fd\u6267\u884c i ++\uff09\n std.debug.print(\"{}\\n\", .{0}); // +1\n }\n}\n
\u8bbe\u7b97\u6cd5\u7684\u64cd\u4f5c\u6570\u91cf\u662f\u4e00\u4e2a\u5173\u4e8e\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u7684\u51fd\u6570\uff0c\u8bb0\u4e3a \\(T(n)\\) \uff0c\u5219\u4ee5\u4e0a\u51fd\u6570\u7684\u64cd\u4f5c\u6570\u91cf\u4e3a\uff1a
\\[ T(n) = 3 + 2n \\]\\(T(n)\\) \u662f\u4e00\u6b21\u51fd\u6570\uff0c\u8bf4\u660e\u5176\u8fd0\u884c\u65f6\u95f4\u7684\u589e\u957f\u8d8b\u52bf\u662f\u7ebf\u6027\u7684\uff0c\u56e0\u6b64\u5b83\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u662f\u7ebf\u6027\u9636\u3002
\u6211\u4eec\u5c06\u7ebf\u6027\u9636\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u8bb0\u4e3a \\(O(n)\\) \uff0c\u8fd9\u4e2a\u6570\u5b66\u7b26\u53f7\u79f0\u4e3a\u300c\u5927 \\(O\\) \u8bb0\u53f7 big-\\(O\\) notation\u300d\uff0c\u8868\u793a\u51fd\u6570 \\(T(n)\\) \u7684\u300c\u6e10\u8fd1\u4e0a\u754c asymptotic upper bound\u300d\u3002
\u65f6\u95f4\u590d\u6742\u5ea6\u5206\u6790\u672c\u8d28\u4e0a\u662f\u8ba1\u7b97\u201c\u64cd\u4f5c\u6570\u91cf \\(T(n)\\)\u201d\u7684\u6e10\u8fd1\u4e0a\u754c\uff0c\u5b83\u5177\u6709\u660e\u786e\u7684\u6570\u5b66\u5b9a\u4e49\u3002
\u51fd\u6570\u6e10\u8fd1\u4e0a\u754c
\u82e5\u5b58\u5728\u6b63\u5b9e\u6570 \\(c\\) \u548c\u5b9e\u6570 \\(n_0\\) \uff0c\u4f7f\u5f97\u5bf9\u4e8e\u6240\u6709\u7684 \\(n > n_0\\) \uff0c\u5747\u6709 \\(T(n) \\leq c \\cdot f(n)\\) \uff0c\u5219\u53ef\u8ba4\u4e3a \\(f(n)\\) \u7ed9\u51fa\u4e86 \\(T(n)\\) \u7684\u4e00\u4e2a\u6e10\u8fd1\u4e0a\u754c\uff0c\u8bb0\u4e3a \\(T(n) = O(f(n))\\) \u3002
\u5982\u56fe 2-8 \u6240\u793a\uff0c\u8ba1\u7b97\u6e10\u8fd1\u4e0a\u754c\u5c31\u662f\u5bfb\u627e\u4e00\u4e2a\u51fd\u6570 \\(f(n)\\) \uff0c\u4f7f\u5f97\u5f53 \\(n\\) \u8d8b\u5411\u4e8e\u65e0\u7a77\u5927\u65f6\uff0c\\(T(n)\\) \u548c \\(f(n)\\) \u5904\u4e8e\u76f8\u540c\u7684\u589e\u957f\u7ea7\u522b\uff0c\u4ec5\u76f8\u5dee\u4e00\u4e2a\u5e38\u6570\u9879 \\(c\\) \u7684\u500d\u6570\u3002
\u56fe 2-8 \u00a0 \u51fd\u6570\u7684\u6e10\u8fd1\u4e0a\u754c
"},{"location":"chapter_computational_complexity/time_complexity/#233","title":"2.3.3 \u00a0 \u63a8\u7b97\u65b9\u6cd5","text":"\u6e10\u8fd1\u4e0a\u754c\u7684\u6570\u5b66\u5473\u513f\u6709\u70b9\u91cd\uff0c\u5982\u679c\u4f60\u611f\u89c9\u6ca1\u6709\u5b8c\u5168\u7406\u89e3\uff0c\u4e5f\u65e0\u987b\u62c5\u5fc3\u3002\u6211\u4eec\u53ef\u4ee5\u5148\u638c\u63e1\u63a8\u7b97\u65b9\u6cd5\uff0c\u5728\u4e0d\u65ad\u7684\u5b9e\u8df5\u4e2d\uff0c\u5c31\u53ef\u4ee5\u9010\u6e10\u9886\u609f\u5176\u6570\u5b66\u610f\u4e49\u3002
\u6839\u636e\u5b9a\u4e49\uff0c\u786e\u5b9a \\(f(n)\\) \u4e4b\u540e\uff0c\u6211\u4eec\u4fbf\u53ef\u5f97\u5230\u65f6\u95f4\u590d\u6742\u5ea6 \\(O(f(n))\\) \u3002\u90a3\u4e48\u5982\u4f55\u786e\u5b9a\u6e10\u8fd1\u4e0a\u754c \\(f(n)\\) \u5462\uff1f\u603b\u4f53\u5206\u4e3a\u4e24\u6b65\uff1a\u9996\u5148\u7edf\u8ba1\u64cd\u4f5c\u6570\u91cf\uff0c\u7136\u540e\u5224\u65ad\u6e10\u8fd1\u4e0a\u754c\u3002
"},{"location":"chapter_computational_complexity/time_complexity/#1","title":"1. \u00a0 \u7b2c\u4e00\u6b65\uff1a\u7edf\u8ba1\u64cd\u4f5c\u6570\u91cf","text":"\u9488\u5bf9\u4ee3\u7801\uff0c\u9010\u884c\u4ece\u4e0a\u5230\u4e0b\u8ba1\u7b97\u5373\u53ef\u3002\u7136\u800c\uff0c\u7531\u4e8e\u4e0a\u8ff0 \\(c \\cdot f(n)\\) \u4e2d\u7684\u5e38\u6570\u9879 \\(c\\) \u53ef\u4ee5\u53d6\u4efb\u610f\u5927\u5c0f\uff0c\u56e0\u6b64\u64cd\u4f5c\u6570\u91cf \\(T(n)\\) \u4e2d\u7684\u5404\u79cd\u7cfb\u6570\u3001\u5e38\u6570\u9879\u90fd\u53ef\u4ee5\u5ffd\u7565\u3002\u6839\u636e\u6b64\u539f\u5219\uff0c\u53ef\u4ee5\u603b\u7ed3\u51fa\u4ee5\u4e0b\u8ba1\u6570\u7b80\u5316\u6280\u5de7\u3002
1.
\u70b9\u548c\u7b2c 2.
\u70b9\u7684\u6280\u5de7\u3002\u7ed9\u5b9a\u4e00\u4e2a\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u7528\u4e0a\u8ff0\u6280\u5de7\u6765\u7edf\u8ba1\u64cd\u4f5c\u6570\u91cf\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZigdef algorithm(n: int):\n a = 1 # +0\uff08\u6280\u5de7 1\uff09\n a = a + n # +0\uff08\u6280\u5de7 1\uff09\n # +n\uff08\u6280\u5de7 2\uff09\n for i in range(5 * n + 1):\n print(0)\n # +n*n\uff08\u6280\u5de7 3\uff09\n for i in range(2 * n):\n for j in range(n + 1):\n print(0)\n
void algorithm(int n) {\n int a = 1; // +0\uff08\u6280\u5de7 1\uff09\n a = a + n; // +0\uff08\u6280\u5de7 1\uff09\n // +n\uff08\u6280\u5de7 2\uff09\n for (int i = 0; i < 5 * n + 1; i++) {\n cout << 0 << endl;\n }\n // +n*n\uff08\u6280\u5de7 3\uff09\n for (int i = 0; i < 2 * n; i++) {\n for (int j = 0; j < n + 1; j++) {\n cout << 0 << endl;\n }\n }\n}\n
void algorithm(int n) {\n int a = 1; // +0\uff08\u6280\u5de7 1\uff09\n a = a + n; // +0\uff08\u6280\u5de7 1\uff09\n // +n\uff08\u6280\u5de7 2\uff09\n for (int i = 0; i < 5 * n + 1; i++) {\n System.out.println(0);\n }\n // +n*n\uff08\u6280\u5de7 3\uff09\n for (int i = 0; i < 2 * n; i++) {\n for (int j = 0; j < n + 1; j++) {\n System.out.println(0);\n }\n }\n}\n
void Algorithm(int n) {\n int a = 1; // +0\uff08\u6280\u5de7 1\uff09\n a = a + n; // +0\uff08\u6280\u5de7 1\uff09\n // +n\uff08\u6280\u5de7 2\uff09\n for (int i = 0; i < 5 * n + 1; i++) {\n Console.WriteLine(0);\n }\n // +n*n\uff08\u6280\u5de7 3\uff09\n for (int i = 0; i < 2 * n; i++) {\n for (int j = 0; j < n + 1; j++) {\n Console.WriteLine(0);\n }\n }\n}\n
func algorithm(n int) {\n a := 1 // +0\uff08\u6280\u5de7 1\uff09\n a = a + n // +0\uff08\u6280\u5de7 1\uff09\n // +n\uff08\u6280\u5de7 2\uff09\n for i := 0; i < 5 * n + 1; i++ {\n fmt.Println(0)\n }\n // +n*n\uff08\u6280\u5de7 3\uff09\n for i := 0; i < 2 * n; i++ {\n for j := 0; j < n + 1; j++ {\n fmt.Println(0)\n }\n }\n}\n
func algorithm(n: Int) {\n var a = 1 // +0\uff08\u6280\u5de7 1\uff09\n a = a + n // +0\uff08\u6280\u5de7 1\uff09\n // +n\uff08\u6280\u5de7 2\uff09\n for _ in 0 ..< (5 * n + 1) {\n print(0)\n }\n // +n*n\uff08\u6280\u5de7 3\uff09\n for _ in 0 ..< (2 * n) {\n for _ in 0 ..< (n + 1) {\n print(0)\n }\n }\n}\n
function algorithm(n) {\n let a = 1; // +0\uff08\u6280\u5de7 1\uff09\n a = a + n; // +0\uff08\u6280\u5de7 1\uff09\n // +n\uff08\u6280\u5de7 2\uff09\n for (let i = 0; i < 5 * n + 1; i++) {\n console.log(0);\n }\n // +n*n\uff08\u6280\u5de7 3\uff09\n for (let i = 0; i < 2 * n; i++) {\n for (let j = 0; j < n + 1; j++) {\n console.log(0);\n }\n }\n}\n
function algorithm(n: number): void {\n let a = 1; // +0\uff08\u6280\u5de7 1\uff09\n a = a + n; // +0\uff08\u6280\u5de7 1\uff09\n // +n\uff08\u6280\u5de7 2\uff09\n for (let i = 0; i < 5 * n + 1; i++) {\n console.log(0);\n }\n // +n*n\uff08\u6280\u5de7 3\uff09\n for (let i = 0; i < 2 * n; i++) {\n for (let j = 0; j < n + 1; j++) {\n console.log(0);\n }\n }\n}\n
void algorithm(int n) {\n int a = 1; // +0\uff08\u6280\u5de7 1\uff09\n a = a + n; // +0\uff08\u6280\u5de7 1\uff09\n // +n\uff08\u6280\u5de7 2\uff09\n for (int i = 0; i < 5 * n + 1; i++) {\n print(0);\n }\n // +n*n\uff08\u6280\u5de7 3\uff09\n for (int i = 0; i < 2 * n; i++) {\n for (int j = 0; j < n + 1; j++) {\n print(0);\n }\n }\n}\n
fn algorithm(n: i32) {\n let mut a = 1; // +0\uff08\u6280\u5de7 1\uff09\n a = a + n; // +0\uff08\u6280\u5de7 1\uff09\n\n // +n\uff08\u6280\u5de7 2\uff09\n for i in 0..(5 * n + 1) {\n println!(\"{}\", 0);\n }\n\n // +n*n\uff08\u6280\u5de7 3\uff09\n for i in 0..(2 * n) {\n for j in 0..(n + 1) {\n println!(\"{}\", 0);\n }\n }\n}\n
void algorithm(int n) {\n int a = 1; // +0\uff08\u6280\u5de7 1\uff09\n a = a + n; // +0\uff08\u6280\u5de7 1\uff09\n // +n\uff08\u6280\u5de7 2\uff09\n for (int i = 0; i < 5 * n + 1; i++) {\n printf(\"%d\", 0);\n }\n // +n*n\uff08\u6280\u5de7 3\uff09\n for (int i = 0; i < 2 * n; i++) {\n for (int j = 0; j < n + 1; j++) {\n printf(\"%d\", 0);\n }\n }\n}\n
fn algorithm(n: usize) void {\n var a: i32 = 1; // +0\uff08\u6280\u5de7 1\uff09\n a = a + @as(i32, @intCast(n)); // +0\uff08\u6280\u5de7 1\uff09\n\n // +n\uff08\u6280\u5de7 2\uff09\n for(0..(5 * n + 1)) |_| {\n std.debug.print(\"{}\\n\", .{0}); \n }\n\n // +n*n\uff08\u6280\u5de7 3\uff09\n for(0..(2 * n)) |_| {\n for(0..(n + 1)) |_| {\n std.debug.print(\"{}\\n\", .{0}); \n }\n }\n}\n
\u4ee5\u4e0b\u516c\u5f0f\u5c55\u793a\u4e86\u4f7f\u7528\u4e0a\u8ff0\u6280\u5de7\u524d\u540e\u7684\u7edf\u8ba1\u7ed3\u679c\uff0c\u4e24\u8005\u63a8\u7b97\u51fa\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u90fd\u4e3a \\(O(n^2)\\) \u3002
\\[ \\begin{aligned} T(n) & = 2n(n + 1) + (5n + 1) + 2 & \\text{\u5b8c\u6574\u7edf\u8ba1 (-.-|||)} \\newline & = 2n^2 + 7n + 3 \\newline T(n) & = n^2 + n & \\text{\u5077\u61d2\u7edf\u8ba1 (o.O)} \\end{aligned} \\]"},{"location":"chapter_computational_complexity/time_complexity/#2","title":"2. \u00a0 \u7b2c\u4e8c\u6b65\uff1a\u5224\u65ad\u6e10\u8fd1\u4e0a\u754c","text":"\u65f6\u95f4\u590d\u6742\u5ea6\u7531 \\(T(n)\\) \u4e2d\u6700\u9ad8\u9636\u7684\u9879\u6765\u51b3\u5b9a\u3002\u8fd9\u662f\u56e0\u4e3a\u5728 \\(n\\) \u8d8b\u4e8e\u65e0\u7a77\u5927\u65f6\uff0c\u6700\u9ad8\u9636\u7684\u9879\u5c06\u53d1\u6325\u4e3b\u5bfc\u4f5c\u7528\uff0c\u5176\u4ed6\u9879\u7684\u5f71\u54cd\u90fd\u53ef\u4ee5\u5ffd\u7565\u3002
\u8868 2-2 \u5c55\u793a\u4e86\u4e00\u4e9b\u4f8b\u5b50\uff0c\u5176\u4e2d\u4e00\u4e9b\u5938\u5f20\u7684\u503c\u662f\u4e3a\u4e86\u5f3a\u8c03\u201c\u7cfb\u6570\u65e0\u6cd5\u64bc\u52a8\u9636\u6570\u201d\u8fd9\u4e00\u7ed3\u8bba\u3002\u5f53 \\(n\\) \u8d8b\u4e8e\u65e0\u7a77\u5927\u65f6\uff0c\u8fd9\u4e9b\u5e38\u6570\u53d8\u5f97\u65e0\u8db3\u8f7b\u91cd\u3002
\u8868 2-2 \u00a0 \u4e0d\u540c\u64cd\u4f5c\u6570\u91cf\u5bf9\u5e94\u7684\u65f6\u95f4\u590d\u6742\u5ea6
\u64cd\u4f5c\u6570\u91cf \\(T(n)\\) \u65f6\u95f4\u590d\u6742\u5ea6 \\(O(f(n))\\) \\(100000\\) \\(O(1)\\) \\(3n + 2\\) \\(O(n)\\) \\(2n^2 + 3n + 2\\) \\(O(n^2)\\) \\(n^3 + 10000n^2\\) \\(O(n^3)\\) \\(2^n + 10000n^{10000}\\) \\(O(2^n)\\)"},{"location":"chapter_computational_complexity/time_complexity/#234","title":"2.3.4 \u00a0 \u5e38\u89c1\u7c7b\u578b","text":"\u8bbe\u8f93\u5165\u6570\u636e\u5927\u5c0f\u4e3a \\(n\\) \uff0c\u5e38\u89c1\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u7c7b\u578b\u5982\u56fe 2-9 \u6240\u793a\uff08\u6309\u7167\u4ece\u4f4e\u5230\u9ad8\u7684\u987a\u5e8f\u6392\u5217\uff09\u3002
\\[ \\begin{aligned} O(1) < O(\\log n) < O(n) < O(n \\log n) < O(n^2) < O(2^n) < O(n!) \\newline \\text{\u5e38\u6570\u9636} < \\text{\u5bf9\u6570\u9636} < \\text{\u7ebf\u6027\u9636} < \\text{\u7ebf\u6027\u5bf9\u6570\u9636} < \\text{\u5e73\u65b9\u9636} < \\text{\u6307\u6570\u9636} < \\text{\u9636\u4e58\u9636} \\end{aligned} \\]\u56fe 2-9 \u00a0 \u5e38\u89c1\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u7c7b\u578b
"},{"location":"chapter_computational_complexity/time_complexity/#1-o1","title":"1. \u00a0 \u5e38\u6570\u9636 \\(O(1)\\)","text":"\u5e38\u6570\u9636\u7684\u64cd\u4f5c\u6570\u91cf\u4e0e\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u65e0\u5173\uff0c\u5373\u4e0d\u968f\u7740 \\(n\\) \u7684\u53d8\u5316\u800c\u53d8\u5316\u3002
\u5728\u4ee5\u4e0b\u51fd\u6570\u4e2d\uff0c\u5c3d\u7ba1\u64cd\u4f5c\u6570\u91cf size
\u53ef\u80fd\u5f88\u5927\uff0c\u4f46\u7531\u4e8e\u5176\u4e0e\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u65e0\u5173\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4ecd\u4e3a \\(O(1)\\) \uff1a
def constant(n: int) -> int:\n \"\"\"\u5e38\u6570\u9636\"\"\"\n count = 0\n size = 100000\n for _ in range(size):\n count += 1\n return count\n
time_complexity.cpp/* \u5e38\u6570\u9636 */\nint constant(int n) {\n int count = 0;\n int size = 100000;\n for (int i = 0; i < size; i++)\n count++;\n return count;\n}\n
time_complexity.java/* \u5e38\u6570\u9636 */\nint constant(int n) {\n int count = 0;\n int size = 100000;\n for (int i = 0; i < size; i++)\n count++;\n return count;\n}\n
time_complexity.cs/* \u5e38\u6570\u9636 */\nint Constant(int n) {\n int count = 0;\n int size = 100000;\n for (int i = 0; i < size; i++)\n count++;\n return count;\n}\n
time_complexity.go/* \u5e38\u6570\u9636 */\nfunc constant(n int) int {\n count := 0\n size := 100000\n for i := 0; i < size; i++ {\n count++\n }\n return count\n}\n
time_complexity.swift/* \u5e38\u6570\u9636 */\nfunc constant(n: Int) -> Int {\n var count = 0\n let size = 100_000\n for _ in 0 ..< size {\n count += 1\n }\n return count\n}\n
time_complexity.js/* \u5e38\u6570\u9636 */\nfunction constant(n) {\n let count = 0;\n const size = 100000;\n for (let i = 0; i < size; i++) count++;\n return count;\n}\n
time_complexity.ts/* \u5e38\u6570\u9636 */\nfunction constant(n: number): number {\n let count = 0;\n const size = 100000;\n for (let i = 0; i < size; i++) count++;\n return count;\n}\n
time_complexity.dart/* \u5e38\u6570\u9636 */\nint constant(int n) {\n int count = 0;\n int size = 100000;\n for (var i = 0; i < size; i++) {\n count++;\n }\n return count;\n}\n
time_complexity.rs/* \u5e38\u6570\u9636 */\nfn constant(n: i32) -> i32 {\n _ = n;\n let mut count = 0;\n let size = 100_000;\n for _ in 0..size {\n count += 1;\n }\n count\n}\n
time_complexity.c/* \u5e38\u6570\u9636 */\nint constant(int n) {\n int count = 0;\n int size = 100000;\n int i = 0;\n for (int i = 0; i < size; i++) {\n count++;\n }\n return count;\n}\n
time_complexity.zig// \u5e38\u6570\u9636\nfn constant(n: i32) i32 {\n _ = n;\n var count: i32 = 0;\n const size: i32 = 100_000;\n var i: i32 = 0;\n while(i<size) : (i += 1) {\n count += 1;\n }\n return count;\n}\n
"},{"location":"chapter_computational_complexity/time_complexity/#2-on","title":"2. \u00a0 \u7ebf\u6027\u9636 \\(O(n)\\)","text":"\u7ebf\u6027\u9636\u7684\u64cd\u4f5c\u6570\u91cf\u76f8\u5bf9\u4e8e\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u4ee5\u7ebf\u6027\u7ea7\u522b\u589e\u957f\u3002\u7ebf\u6027\u9636\u901a\u5e38\u51fa\u73b0\u5728\u5355\u5c42\u5faa\u73af\u4e2d\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig time_complexity.pydef linear(n: int) -> int:\n \"\"\"\u7ebf\u6027\u9636\"\"\"\n count = 0\n for _ in range(n):\n count += 1\n return count\n
time_complexity.cpp/* \u7ebf\u6027\u9636 */\nint linear(int n) {\n int count = 0;\n for (int i = 0; i < n; i++)\n count++;\n return count;\n}\n
time_complexity.java/* \u7ebf\u6027\u9636 */\nint linear(int n) {\n int count = 0;\n for (int i = 0; i < n; i++)\n count++;\n return count;\n}\n
time_complexity.cs/* \u7ebf\u6027\u9636 */\nint Linear(int n) {\n int count = 0;\n for (int i = 0; i < n; i++)\n count++;\n return count;\n}\n
time_complexity.go/* \u7ebf\u6027\u9636 */\nfunc linear(n int) int {\n count := 0\n for i := 0; i < n; i++ {\n count++\n }\n return count\n}\n
time_complexity.swift/* \u7ebf\u6027\u9636 */\nfunc linear(n: Int) -> Int {\n var count = 0\n for _ in 0 ..< n {\n count += 1\n }\n return count\n}\n
time_complexity.js/* \u7ebf\u6027\u9636 */\nfunction linear(n) {\n let count = 0;\n for (let i = 0; i < n; i++) count++;\n return count;\n}\n
time_complexity.ts/* \u7ebf\u6027\u9636 */\nfunction linear(n: number): number {\n let count = 0;\n for (let i = 0; i < n; i++) count++;\n return count;\n}\n
time_complexity.dart/* \u7ebf\u6027\u9636 */\nint linear(int n) {\n int count = 0;\n for (var i = 0; i < n; i++) {\n count++;\n }\n return count;\n}\n
time_complexity.rs/* \u7ebf\u6027\u9636 */\nfn linear(n: i32) -> i32 {\n let mut count = 0;\n for _ in 0..n {\n count += 1;\n }\n count\n}\n
time_complexity.c/* \u7ebf\u6027\u9636 */\nint linear(int n) {\n int count = 0;\n for (int i = 0; i < n; i++) {\n count++;\n }\n return count;\n}\n
time_complexity.zig// \u7ebf\u6027\u9636\nfn linear(n: i32) i32 {\n var count: i32 = 0;\n var i: i32 = 0;\n while (i < n) : (i += 1) {\n count += 1;\n }\n return count;\n}\n
\u904d\u5386\u6570\u7ec4\u548c\u904d\u5386\u94fe\u8868\u7b49\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u5747\u4e3a \\(O(n)\\) \uff0c\u5176\u4e2d \\(n\\) \u4e3a\u6570\u7ec4\u6216\u94fe\u8868\u7684\u957f\u5ea6\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig time_complexity.pydef array_traversal(nums: list[int]) -> int:\n \"\"\"\u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09\"\"\"\n count = 0\n # \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\n for num in nums:\n count += 1\n return count\n
time_complexity.cpp/* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nint arrayTraversal(vector<int> &nums) {\n int count = 0;\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\n for (int num : nums) {\n count++;\n }\n return count;\n}\n
time_complexity.java/* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nint arrayTraversal(int[] nums) {\n int count = 0;\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\n for (int num : nums) {\n count++;\n }\n return count;\n}\n
time_complexity.cs/* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nint ArrayTraversal(int[] nums) {\n int count = 0;\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\n foreach (int num in nums) {\n count++;\n }\n return count;\n}\n
time_complexity.go/* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nfunc arrayTraversal(nums []int) int {\n count := 0\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\n for range nums {\n count++\n }\n return count\n}\n
time_complexity.swift/* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nfunc arrayTraversal(nums: [Int]) -> Int {\n var count = 0\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\n for _ in nums {\n count += 1\n }\n return count\n}\n
time_complexity.js/* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nfunction arrayTraversal(nums) {\n let count = 0;\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\n for (let i = 0; i < nums.length; i++) {\n count++;\n }\n return count;\n}\n
time_complexity.ts/* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nfunction arrayTraversal(nums: number[]): number {\n let count = 0;\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\n for (let i = 0; i < nums.length; i++) {\n count++;\n }\n return count;\n}\n
time_complexity.dart/* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nint arrayTraversal(List<int> nums) {\n int count = 0;\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\n for (var _num in nums) {\n count++;\n }\n return count;\n}\n
time_complexity.rs/* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nfn array_traversal(nums: &[i32]) -> i32 {\n let mut count = 0;\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\n for _ in nums {\n count += 1;\n }\n count\n}\n
time_complexity.c/* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nint arrayTraversal(int *nums, int n) {\n int count = 0;\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\n for (int i = 0; i < n; i++) {\n count++;\n }\n return count;\n}\n
time_complexity.zig// \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09\nfn arrayTraversal(nums: []i32) i32 {\n var count: i32 = 0;\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\n for (nums) |_| {\n count += 1;\n }\n return count;\n}\n
\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u9700\u6839\u636e\u8f93\u5165\u6570\u636e\u7684\u7c7b\u578b\u6765\u5177\u4f53\u786e\u5b9a\u3002\u6bd4\u5982\u5728\u7b2c\u4e00\u4e2a\u793a\u4f8b\u4e2d\uff0c\u53d8\u91cf \\(n\\) \u4e3a\u8f93\u5165\u6570\u636e\u5927\u5c0f\uff1b\u5728\u7b2c\u4e8c\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6570\u7ec4\u957f\u5ea6 \\(n\\) \u4e3a\u6570\u636e\u5927\u5c0f\u3002
"},{"location":"chapter_computational_complexity/time_complexity/#3-on2","title":"3. \u00a0 \u5e73\u65b9\u9636 \\(O(n^2)\\)","text":"\u5e73\u65b9\u9636\u7684\u64cd\u4f5c\u6570\u91cf\u76f8\u5bf9\u4e8e\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u4ee5\u5e73\u65b9\u7ea7\u522b\u589e\u957f\u3002\u5e73\u65b9\u9636\u901a\u5e38\u51fa\u73b0\u5728\u5d4c\u5957\u5faa\u73af\u4e2d\uff0c\u5916\u5c42\u5faa\u73af\u548c\u5185\u5c42\u5faa\u73af\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u90fd\u4e3a \\(O(n)\\) \uff0c\u56e0\u6b64\u603b\u4f53\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\) \uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig time_complexity.pydef quadratic(n: int) -> int:\n \"\"\"\u5e73\u65b9\u9636\"\"\"\n count = 0\n # \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u5e73\u65b9\u5173\u7cfb\n for i in range(n):\n for j in range(n):\n count += 1\n return count\n
time_complexity.cpp/* \u5e73\u65b9\u9636 */\nint quadratic(int n) {\n int count = 0;\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u5e73\u65b9\u5173\u7cfb\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n count++;\n }\n }\n return count;\n}\n
time_complexity.java/* \u5e73\u65b9\u9636 */\nint quadratic(int n) {\n int count = 0;\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u5e73\u65b9\u5173\u7cfb\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n count++;\n }\n }\n return count;\n}\n
time_complexity.cs/* \u5e73\u65b9\u9636 */\nint Quadratic(int n) {\n int count = 0;\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u5e73\u65b9\u5173\u7cfb\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n count++;\n }\n }\n return count;\n}\n
time_complexity.go/* \u5e73\u65b9\u9636 */\nfunc quadratic(n int) int {\n count := 0\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u5e73\u65b9\u5173\u7cfb\n for i := 0; i < n; i++ {\n for j := 0; j < n; j++ {\n count++\n }\n }\n return count\n}\n
time_complexity.swift/* \u5e73\u65b9\u9636 */\nfunc quadratic(n: Int) -> Int {\n var count = 0\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u5e73\u65b9\u5173\u7cfb\n for _ in 0 ..< n {\n for _ in 0 ..< n {\n count += 1\n }\n }\n return count\n}\n
time_complexity.js/* \u5e73\u65b9\u9636 */\nfunction quadratic(n) {\n let count = 0;\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u5e73\u65b9\u5173\u7cfb\n for (let i = 0; i < n; i++) {\n for (let j = 0; j < n; j++) {\n count++;\n }\n }\n return count;\n}\n
time_complexity.ts/* \u5e73\u65b9\u9636 */\nfunction quadratic(n: number): number {\n let count = 0;\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u5e73\u65b9\u5173\u7cfb\n for (let i = 0; i < n; i++) {\n for (let j = 0; j < n; j++) {\n count++;\n }\n }\n return count;\n}\n
time_complexity.dart/* \u5e73\u65b9\u9636 */\nint quadratic(int n) {\n int count = 0;\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u5e73\u65b9\u5173\u7cfb\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n count++;\n }\n }\n return count;\n}\n
time_complexity.rs/* \u5e73\u65b9\u9636 */\nfn quadratic(n: i32) -> i32 {\n let mut count = 0;\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u5e73\u65b9\u5173\u7cfb\n for _ in 0..n {\n for _ in 0..n {\n count += 1;\n }\n }\n count\n}\n
time_complexity.c/* \u5e73\u65b9\u9636 */\nint quadratic(int n) {\n int count = 0;\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u5e73\u65b9\u5173\u7cfb\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n count++;\n }\n }\n return count;\n}\n
time_complexity.zig// \u5e73\u65b9\u9636\nfn quadratic(n: i32) i32 {\n var count: i32 = 0;\n var i: i32 = 0;\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u5e73\u65b9\u5173\u7cfb\n while (i < n) : (i += 1) {\n var j: i32 = 0;\n while (j < n) : (j += 1) {\n count += 1;\n }\n }\n return count;\n}\n
\u56fe 2-10 \u5bf9\u6bd4\u4e86\u5e38\u6570\u9636\u3001\u7ebf\u6027\u9636\u548c\u5e73\u65b9\u9636\u4e09\u79cd\u65f6\u95f4\u590d\u6742\u5ea6\u3002
\u56fe 2-10 \u00a0 \u5e38\u6570\u9636\u3001\u7ebf\u6027\u9636\u548c\u5e73\u65b9\u9636\u7684\u65f6\u95f4\u590d\u6742\u5ea6
\u4ee5\u5192\u6ce1\u6392\u5e8f\u4e3a\u4f8b\uff0c\u5916\u5c42\u5faa\u73af\u6267\u884c \\(n - 1\\) \u6b21\uff0c\u5185\u5c42\u5faa\u73af\u6267\u884c \\(n-1\\)\u3001\\(n-2\\)\u3001\\(\\dots\\)\u3001\\(2\\)\u3001\\(1\\) \u6b21\uff0c\u5e73\u5747\u4e3a \\(n / 2\\) \u6b21\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O((n - 1) n / 2) = O(n^2)\\) \uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig time_complexity.pydef bubble_sort(nums: list[int]) -> int:\n \"\"\"\u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09\"\"\"\n count = 0 # \u8ba1\u6570\u5668\n # \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for i in range(len(nums) - 1, 0, -1):\n # \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n for j in range(i):\n if nums[j] > nums[j + 1]:\n # \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n tmp: int = nums[j]\n nums[j] = nums[j + 1]\n nums[j + 1] = tmp\n count += 3 # \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n return count\n
time_complexity.cpp/* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nint bubbleSort(vector<int> &nums) {\n int count = 0; // \u8ba1\u6570\u5668\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for (int i = nums.size() - 1; i > 0; i--) {\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n for (int j = 0; j < i; j++) {\n if (nums[j] > nums[j + 1]) {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n int tmp = nums[j];\n nums[j] = nums[j + 1];\n nums[j + 1] = tmp;\n count += 3; // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n }\n }\n }\n return count;\n}\n
time_complexity.java/* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nint bubbleSort(int[] nums) {\n int count = 0; // \u8ba1\u6570\u5668\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for (int i = nums.length - 1; i > 0; i--) {\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n for (int j = 0; j < i; j++) {\n if (nums[j] > nums[j + 1]) {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n int tmp = nums[j];\n nums[j] = nums[j + 1];\n nums[j + 1] = tmp;\n count += 3; // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n }\n }\n }\n return count;\n}\n
time_complexity.cs/* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nint BubbleSort(int[] nums) {\n int count = 0; // \u8ba1\u6570\u5668\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for (int i = nums.Length - 1; i > 0; i--) {\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef \n for (int j = 0; j < i; j++) {\n if (nums[j] > nums[j + 1]) {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n (nums[j + 1], nums[j]) = (nums[j], nums[j + 1]);\n count += 3; // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n }\n }\n }\n return count;\n}\n
time_complexity.go/* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nfunc bubbleSort(nums []int) int {\n count := 0 // \u8ba1\u6570\u5668\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for i := len(nums) - 1; i > 0; i-- {\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n for j := 0; j < i; j++ {\n if nums[j] > nums[j+1] {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n tmp := nums[j]\n nums[j] = nums[j+1]\n nums[j+1] = tmp\n count += 3 // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n }\n }\n }\n return count\n}\n
time_complexity.swift/* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nfunc bubbleSort(nums: inout [Int]) -> Int {\n var count = 0 // \u8ba1\u6570\u5668\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for i in stride(from: nums.count - 1, to: 0, by: -1) {\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef \n for j in 0 ..< i {\n if nums[j] > nums[j + 1] {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n let tmp = nums[j]\n nums[j] = nums[j + 1]\n nums[j + 1] = tmp\n count += 3 // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n }\n }\n }\n return count\n}\n
time_complexity.js/* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nfunction bubbleSort(nums) {\n let count = 0; // \u8ba1\u6570\u5668\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for (let i = nums.length - 1; i > 0; i--) {\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n for (let j = 0; j < i; j++) {\n if (nums[j] > nums[j + 1]) {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n let tmp = nums[j];\n nums[j] = nums[j + 1];\n nums[j + 1] = tmp;\n count += 3; // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n }\n }\n }\n return count;\n}\n
time_complexity.ts/* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nfunction bubbleSort(nums: number[]): number {\n let count = 0; // \u8ba1\u6570\u5668\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for (let i = nums.length - 1; i > 0; i--) {\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n for (let j = 0; j < i; j++) {\n if (nums[j] > nums[j + 1]) {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n let tmp = nums[j];\n nums[j] = nums[j + 1];\n nums[j + 1] = tmp;\n count += 3; // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n }\n }\n }\n return count;\n}\n
time_complexity.dart/* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nint bubbleSort(List<int> nums) {\n int count = 0; // \u8ba1\u6570\u5668\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for (var i = nums.length - 1; i > 0; i--) {\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n for (var j = 0; j < i; j++) {\n if (nums[j] > nums[j + 1]) {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n int tmp = nums[j];\n nums[j] = nums[j + 1];\n nums[j + 1] = tmp;\n count += 3; // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n }\n }\n }\n return count;\n}\n
time_complexity.rs/* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nfn bubble_sort(nums: &mut [i32]) -> i32 {\n let mut count = 0; // \u8ba1\u6570\u5668\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for i in (1..nums.len()).rev() {\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef \n for j in 0..i {\n if nums[j] > nums[j + 1] {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n let tmp = nums[j];\n nums[j] = nums[j + 1];\n nums[j + 1] = tmp;\n count += 3; // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n }\n }\n }\n count\n}\n
time_complexity.c/* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nint bubbleSort(int *nums, int n) {\n int count = 0; // \u8ba1\u6570\u5668\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for (int i = n - 1; i > 0; i--) {\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n for (int j = 0; j < i; j++) {\n if (nums[j] > nums[j + 1]) {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n int tmp = nums[j];\n nums[j] = nums[j + 1];\n nums[j + 1] = tmp;\n count += 3; // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n }\n }\n }\n return count;\n}\n
time_complexity.zig// \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09\nfn bubbleSort(nums: []i32) i32 {\n var count: i32 = 0; // \u8ba1\u6570\u5668 \n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n var i: i32 = @as(i32, @intCast(nums.len)) - 1;\n while (i > 0) : (i -= 1) {\n var j: usize = 0;\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef \n while (j < i) : (j += 1) {\n if (nums[j] > nums[j + 1]) {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n var tmp = nums[j];\n nums[j] = nums[j + 1];\n nums[j + 1] = tmp;\n count += 3; // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n }\n }\n }\n return count;\n}\n
"},{"location":"chapter_computational_complexity/time_complexity/#4-o2n","title":"4. \u00a0 \u6307\u6570\u9636 \\(O(2^n)\\)","text":"\u751f\u7269\u5b66\u7684\u201c\u7ec6\u80de\u5206\u88c2\u201d\u662f\u6307\u6570\u9636\u589e\u957f\u7684\u5178\u578b\u4f8b\u5b50\uff1a\u521d\u59cb\u72b6\u6001\u4e3a \\(1\\) \u4e2a\u7ec6\u80de\uff0c\u5206\u88c2\u4e00\u8f6e\u540e\u53d8\u4e3a \\(2\\) \u4e2a\uff0c\u5206\u88c2\u4e24\u8f6e\u540e\u53d8\u4e3a \\(4\\) \u4e2a\uff0c\u4ee5\u6b64\u7c7b\u63a8\uff0c\u5206\u88c2 \\(n\\) \u8f6e\u540e\u6709 \\(2^n\\) \u4e2a\u7ec6\u80de\u3002
\u56fe 2-11 \u548c\u4ee5\u4e0b\u4ee3\u7801\u6a21\u62df\u4e86\u7ec6\u80de\u5206\u88c2\u7684\u8fc7\u7a0b\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(2^n)\\) \u3002
PythonC++JavaC#GoSwiftJSTSDartRustCZig time_complexity.pydef exponential(n: int) -> int:\n \"\"\"\u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09\"\"\"\n count = 0\n base = 1\n # \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\n for _ in range(n):\n for _ in range(base):\n count += 1\n base *= 2\n # count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n return count\n
time_complexity.cpp/* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint exponential(int n) {\n int count = 0, base = 1;\n // \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < base; j++) {\n count++;\n }\n base *= 2;\n }\n // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n return count;\n}\n
time_complexity.java/* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint exponential(int n) {\n int count = 0, base = 1;\n // \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < base; j++) {\n count++;\n }\n base *= 2;\n }\n // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n return count;\n}\n
time_complexity.cs/* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint Exponential(int n) {\n int count = 0, bas = 1;\n // \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < bas; j++) {\n count++;\n }\n bas *= 2;\n }\n // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n return count;\n}\n
time_complexity.go/* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09*/\nfunc exponential(n int) int {\n count, base := 0, 1\n // \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\n for i := 0; i < n; i++ {\n for j := 0; j < base; j++ {\n count++\n }\n base *= 2\n }\n // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n return count\n}\n
time_complexity.swift/* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfunc exponential(n: Int) -> Int {\n var count = 0\n var base = 1\n // \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\n for _ in 0 ..< n {\n for _ in 0 ..< base {\n count += 1\n }\n base *= 2\n }\n // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n return count\n}\n
time_complexity.js/* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfunction exponential(n) {\n let count = 0,\n base = 1;\n // \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\n for (let i = 0; i < n; i++) {\n for (let j = 0; j < base; j++) {\n count++;\n }\n base *= 2;\n }\n // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n return count;\n}\n
time_complexity.ts/* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfunction exponential(n: number): number {\n let count = 0,\n base = 1;\n // \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\n for (let i = 0; i < n; i++) {\n for (let j = 0; j < base; j++) {\n count++;\n }\n base *= 2;\n }\n // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n return count;\n}\n
time_complexity.dart/* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint exponential(int n) {\n int count = 0, base = 1;\n // \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\n for (var i = 0; i < n; i++) {\n for (var j = 0; j < base; j++) {\n count++;\n }\n base *= 2;\n }\n // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n return count;\n}\n
time_complexity.rs/* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfn exponential(n: i32) -> i32 {\n let mut count = 0;\n let mut base = 1;\n // \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\n for _ in 0..n {\n for _ in 0..base {\n count += 1\n }\n base *= 2;\n }\n // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n count\n}\n
time_complexity.c/* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint exponential(int n) {\n int count = 0;\n int bas = 1;\n // \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < bas; j++) {\n count++;\n }\n bas *= 2;\n }\n // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n return count;\n}\n
time_complexity.zig// \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09\nfn exponential(n: i32) i32 {\n var count: i32 = 0;\n var bas: i32 = 1;\n var i: i32 = 0;\n // \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\n while (i < n) : (i += 1) {\n var j: i32 = 0;\n while (j < bas) : (j += 1) {\n count += 1;\n }\n bas *= 2;\n }\n // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n return count;\n}\n
\u56fe 2-11 \u00a0 \u6307\u6570\u9636\u7684\u65f6\u95f4\u590d\u6742\u5ea6
\u5728\u5b9e\u9645\u7b97\u6cd5\u4e2d\uff0c\u6307\u6570\u9636\u5e38\u51fa\u73b0\u4e8e\u9012\u5f52\u51fd\u6570\u4e2d\u3002\u4f8b\u5982\u5728\u4ee5\u4e0b\u4ee3\u7801\u4e2d\uff0c\u5176\u9012\u5f52\u5730\u4e00\u5206\u4e3a\u4e8c\uff0c\u7ecf\u8fc7 \\(n\\) \u6b21\u5206\u88c2\u540e\u505c\u6b62\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig time_complexity.pydef exp_recur(n: int) -> int:\n \"\"\"\u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\"\"\"\n if n == 1:\n return 1\n return exp_recur(n - 1) + exp_recur(n - 1) + 1\n
time_complexity.cpp/* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint expRecur(int n) {\n if (n == 1)\n return 1;\n return expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n
time_complexity.java/* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint expRecur(int n) {\n if (n == 1)\n return 1;\n return expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n
time_complexity.cs/* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint ExpRecur(int n) {\n if (n == 1) return 1;\n return ExpRecur(n - 1) + ExpRecur(n - 1) + 1;\n}\n
time_complexity.go/* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09*/\nfunc expRecur(n int) int {\n if n == 1 {\n return 1\n }\n return expRecur(n-1) + expRecur(n-1) + 1\n}\n
time_complexity.swift/* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunc expRecur(n: Int) -> Int {\n if n == 1 {\n return 1\n }\n return expRecur(n: n - 1) + expRecur(n: n - 1) + 1\n}\n
time_complexity.js/* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction expRecur(n) {\n if (n === 1) return 1;\n return expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n
time_complexity.ts/* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction expRecur(n: number): number {\n if (n === 1) return 1;\n return expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n
time_complexity.dart/* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint expRecur(int n) {\n if (n == 1) return 1;\n return expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n
time_complexity.rs/* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfn exp_recur(n: i32) -> i32 {\n if n == 1 {\n return 1;\n }\n exp_recur(n - 1) + exp_recur(n - 1) + 1\n}\n
time_complexity.c/* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint expRecur(int n) {\n if (n == 1)\n return 1;\n return expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n
time_complexity.zig// \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\nfn expRecur(n: i32) i32 {\n if (n == 1) return 1;\n return expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n
\u6307\u6570\u9636\u589e\u957f\u975e\u5e38\u8fc5\u901f\uff0c\u5728\u7a77\u4e3e\u6cd5\uff08\u66b4\u529b\u641c\u7d22\u3001\u56de\u6eaf\u7b49\uff09\u4e2d\u6bd4\u8f83\u5e38\u89c1\u3002\u5bf9\u4e8e\u6570\u636e\u89c4\u6a21\u8f83\u5927\u7684\u95ee\u9898\uff0c\u6307\u6570\u9636\u662f\u4e0d\u53ef\u63a5\u53d7\u7684\uff0c\u901a\u5e38\u9700\u8981\u4f7f\u7528\u52a8\u6001\u89c4\u5212\u6216\u8d2a\u5fc3\u7b97\u6cd5\u7b49\u6765\u89e3\u51b3\u3002
"},{"location":"chapter_computational_complexity/time_complexity/#5-olog-n","title":"5. \u00a0 \u5bf9\u6570\u9636 \\(O(\\log n)\\)","text":"\u4e0e\u6307\u6570\u9636\u76f8\u53cd\uff0c\u5bf9\u6570\u9636\u53cd\u6620\u4e86\u201c\u6bcf\u8f6e\u7f29\u51cf\u5230\u4e00\u534a\u201d\u7684\u60c5\u51b5\u3002\u8bbe\u8f93\u5165\u6570\u636e\u5927\u5c0f\u4e3a \\(n\\) \uff0c\u7531\u4e8e\u6bcf\u8f6e\u7f29\u51cf\u5230\u4e00\u534a\uff0c\u56e0\u6b64\u5faa\u73af\u6b21\u6570\u662f \\(\\log_2 n\\) \uff0c\u5373 \\(2^n\\) \u7684\u53cd\u51fd\u6570\u3002
\u56fe 2-12 \u548c\u4ee5\u4e0b\u4ee3\u7801\u6a21\u62df\u4e86\u201c\u6bcf\u8f6e\u7f29\u51cf\u5230\u4e00\u534a\u201d\u7684\u8fc7\u7a0b\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(\\log_2 n)\\) \uff0c\u7b80\u8bb0\u4e3a \\(O(\\log n)\\) \uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig time_complexity.pydef logarithmic(n: float) -> int:\n \"\"\"\u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09\"\"\"\n count = 0\n while n > 1:\n n = n / 2\n count += 1\n return count\n
time_complexity.cpp/* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint logarithmic(float n) {\n int count = 0;\n while (n > 1) {\n n = n / 2;\n count++;\n }\n return count;\n}\n
time_complexity.java/* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint logarithmic(float n) {\n int count = 0;\n while (n > 1) {\n n = n / 2;\n count++;\n }\n return count;\n}\n
time_complexity.cs/* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint Logarithmic(float n) {\n int count = 0;\n while (n > 1) {\n n /= 2;\n count++;\n }\n return count;\n}\n
time_complexity.go/* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09*/\nfunc logarithmic(n float64) int {\n count := 0\n for n > 1 {\n n = n / 2\n count++\n }\n return count\n}\n
time_complexity.swift/* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfunc logarithmic(n: Double) -> Int {\n var count = 0\n var n = n\n while n > 1 {\n n = n / 2\n count += 1\n }\n return count\n}\n
time_complexity.js/* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfunction logarithmic(n) {\n let count = 0;\n while (n > 1) {\n n = n / 2;\n count++;\n }\n return count;\n}\n
time_complexity.ts/* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfunction logarithmic(n: number): number {\n let count = 0;\n while (n > 1) {\n n = n / 2;\n count++;\n }\n return count;\n}\n
time_complexity.dart/* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint logarithmic(num n) {\n int count = 0;\n while (n > 1) {\n n = n / 2;\n count++;\n }\n return count;\n}\n
time_complexity.rs/* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfn logarithmic(mut n: f32) -> i32 {\n let mut count = 0;\n while n > 1.0 {\n n = n / 2.0;\n count += 1;\n }\n count\n}\n
time_complexity.c/* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint logarithmic(float n) {\n int count = 0;\n while (n > 1) {\n n = n / 2;\n count++;\n }\n return count;\n}\n
time_complexity.zig// \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09\nfn logarithmic(n: f32) i32 {\n var count: i32 = 0;\n var n_var = n;\n while (n_var > 1)\n {\n n_var = n_var / 2;\n count +=1;\n }\n return count;\n}\n
\u56fe 2-12 \u00a0 \u5bf9\u6570\u9636\u7684\u65f6\u95f4\u590d\u6742\u5ea6
\u4e0e\u6307\u6570\u9636\u7c7b\u4f3c\uff0c\u5bf9\u6570\u9636\u4e5f\u5e38\u51fa\u73b0\u4e8e\u9012\u5f52\u51fd\u6570\u4e2d\u3002\u4ee5\u4e0b\u4ee3\u7801\u5f62\u6210\u4e86\u4e00\u68f5\u9ad8\u5ea6\u4e3a \\(\\log_2 n\\) \u7684\u9012\u5f52\u6811\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig time_complexity.pydef log_recur(n: float) -> int:\n \"\"\"\u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\"\"\"\n if n <= 1:\n return 0\n return log_recur(n / 2) + 1\n
time_complexity.cpp/* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint logRecur(float n) {\n if (n <= 1)\n return 0;\n return logRecur(n / 2) + 1;\n}\n
time_complexity.java/* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint logRecur(float n) {\n if (n <= 1)\n return 0;\n return logRecur(n / 2) + 1;\n}\n
time_complexity.cs/* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint LogRecur(float n) {\n if (n <= 1) return 0;\n return LogRecur(n / 2) + 1;\n}\n
time_complexity.go/* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09*/\nfunc logRecur(n float64) int {\n if n <= 1 {\n return 0\n }\n return logRecur(n/2) + 1\n}\n
time_complexity.swift/* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunc logRecur(n: Double) -> Int {\n if n <= 1 {\n return 0\n }\n return logRecur(n: n / 2) + 1\n}\n
time_complexity.js/* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction logRecur(n) {\n if (n <= 1) return 0;\n return logRecur(n / 2) + 1;\n}\n
time_complexity.ts/* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction logRecur(n: number): number {\n if (n <= 1) return 0;\n return logRecur(n / 2) + 1;\n}\n
time_complexity.dart/* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint logRecur(num n) {\n if (n <= 1) return 0;\n return logRecur(n / 2) + 1;\n}\n
time_complexity.rs/* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfn log_recur(n: f32) -> i32 {\n if n <= 1.0 {\n return 0;\n }\n log_recur(n / 2.0) + 1\n}\n
time_complexity.c/* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint logRecur(float n) {\n if (n <= 1)\n return 0;\n return logRecur(n / 2) + 1;\n}\n
time_complexity.zig// \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\nfn logRecur(n: f32) i32 {\n if (n <= 1) return 0;\n return logRecur(n / 2) + 1;\n}\n
\u5bf9\u6570\u9636\u5e38\u51fa\u73b0\u4e8e\u57fa\u4e8e\u5206\u6cbb\u7b56\u7565\u7684\u7b97\u6cd5\u4e2d\uff0c\u4f53\u73b0\u4e86\u201c\u4e00\u5206\u4e3a\u591a\u201d\u548c\u201c\u5316\u7e41\u4e3a\u7b80\u201d\u7684\u7b97\u6cd5\u601d\u60f3\u3002\u5b83\u589e\u957f\u7f13\u6162\uff0c\u662f\u4ec5\u6b21\u4e8e\u5e38\u6570\u9636\u7684\u7406\u60f3\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u3002
\\(O(\\log n)\\) \u7684\u5e95\u6570\u662f\u591a\u5c11\uff1f
\u51c6\u786e\u6765\u8bf4\uff0c\u201c\u4e00\u5206\u4e3a \\(m\\)\u201d\u5bf9\u5e94\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u662f \\(O(\\log_m n)\\) \u3002\u800c\u901a\u8fc7\u5bf9\u6570\u6362\u5e95\u516c\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u5f97\u5230\u5177\u6709\u4e0d\u540c\u5e95\u6570\u3001\u76f8\u7b49\u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a
\\[ O(\\log_m n) = O(\\log_k n / \\log_k m) = O(\\log_k n) \\]\u4e5f\u5c31\u662f\u8bf4\uff0c\u5e95\u6570 \\(m\\) \u53ef\u4ee5\u5728\u4e0d\u5f71\u54cd\u590d\u6742\u5ea6\u7684\u524d\u63d0\u4e0b\u8f6c\u6362\u3002\u56e0\u6b64\u6211\u4eec\u901a\u5e38\u4f1a\u7701\u7565\u5e95\u6570 \\(m\\) \uff0c\u5c06\u5bf9\u6570\u9636\u76f4\u63a5\u8bb0\u4e3a \\(O(\\log n)\\) \u3002
"},{"location":"chapter_computational_complexity/time_complexity/#6-on-log-n","title":"6. \u00a0 \u7ebf\u6027\u5bf9\u6570\u9636 \\(O(n \\log n)\\)","text":"\u7ebf\u6027\u5bf9\u6570\u9636\u5e38\u51fa\u73b0\u4e8e\u5d4c\u5957\u5faa\u73af\u4e2d\uff0c\u4e24\u5c42\u5faa\u73af\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u5206\u522b\u4e3a \\(O(\\log n)\\) \u548c \\(O(n)\\) \u3002\u76f8\u5173\u4ee3\u7801\u5982\u4e0b\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig time_complexity.pydef linear_log_recur(n: float) -> int:\n \"\"\"\u7ebf\u6027\u5bf9\u6570\u9636\"\"\"\n if n <= 1:\n return 1\n count: int = linear_log_recur(n // 2) + linear_log_recur(n // 2)\n for _ in range(n):\n count += 1\n return count\n
time_complexity.cpp/* \u7ebf\u6027\u5bf9\u6570\u9636 */\nint linearLogRecur(float n) {\n if (n <= 1)\n return 1;\n int count = linearLogRecur(n / 2) + linearLogRecur(n / 2);\n for (int i = 0; i < n; i++) {\n count++;\n }\n return count;\n}\n
time_complexity.java/* \u7ebf\u6027\u5bf9\u6570\u9636 */\nint linearLogRecur(float n) {\n if (n <= 1)\n return 1;\n int count = linearLogRecur(n / 2) + linearLogRecur(n / 2);\n for (int i = 0; i < n; i++) {\n count++;\n }\n return count;\n}\n
time_complexity.cs/* \u7ebf\u6027\u5bf9\u6570\u9636 */\nint LinearLogRecur(float n) {\n if (n <= 1) return 1;\n int count = LinearLogRecur(n / 2) + LinearLogRecur(n / 2);\n for (int i = 0; i < n; i++) {\n count++;\n }\n return count;\n}\n
time_complexity.go/* \u7ebf\u6027\u5bf9\u6570\u9636 */\nfunc linearLogRecur(n float64) int {\n if n <= 1 {\n return 1\n }\n count := linearLogRecur(n/2) + linearLogRecur(n/2)\n for i := 0.0; i < n; i++ {\n count++\n }\n return count\n}\n
time_complexity.swift/* \u7ebf\u6027\u5bf9\u6570\u9636 */\nfunc linearLogRecur(n: Double) -> Int {\n if n <= 1 {\n return 1\n }\n var count = linearLogRecur(n: n / 2) + linearLogRecur(n: n / 2)\n for _ in stride(from: 0, to: n, by: 1) {\n count += 1\n }\n return count\n}\n
time_complexity.js/* \u7ebf\u6027\u5bf9\u6570\u9636 */\nfunction linearLogRecur(n) {\n if (n <= 1) return 1;\n let count = linearLogRecur(n / 2) + linearLogRecur(n / 2);\n for (let i = 0; i < n; i++) {\n count++;\n }\n return count;\n}\n
time_complexity.ts/* \u7ebf\u6027\u5bf9\u6570\u9636 */\nfunction linearLogRecur(n: number): number {\n if (n <= 1) return 1;\n let count = linearLogRecur(n / 2) + linearLogRecur(n / 2);\n for (let i = 0; i < n; i++) {\n count++;\n }\n return count;\n}\n
time_complexity.dart/* \u7ebf\u6027\u5bf9\u6570\u9636 */\nint linearLogRecur(num n) {\n if (n <= 1) return 1;\n int count = linearLogRecur(n / 2) + linearLogRecur(n / 2);\n for (var i = 0; i < n; i++) {\n count++;\n }\n return count;\n}\n
time_complexity.rs/* \u7ebf\u6027\u5bf9\u6570\u9636 */\nfn linear_log_recur(n: f32) -> i32 {\n if n <= 1.0 {\n return 1;\n }\n let mut count = linear_log_recur(n / 2.0) + linear_log_recur(n / 2.0);\n for _ in 0 ..n as i32 {\n count += 1;\n }\n return count\n}\n
time_complexity.c/* \u7ebf\u6027\u5bf9\u6570\u9636 */\nint linearLogRecur(float n) {\n if (n <= 1)\n return 1;\n int count = linearLogRecur(n / 2) + linearLogRecur(n / 2);\n for (int i = 0; i < n; i++) {\n count++;\n }\n return count;\n}\n
time_complexity.zig// \u7ebf\u6027\u5bf9\u6570\u9636\nfn linearLogRecur(n: f32) i32 {\n if (n <= 1) return 1;\n var count: i32 = linearLogRecur(n / 2) + linearLogRecur(n / 2);\n var i: f32 = 0;\n while (i < n) : (i += 1) {\n count += 1;\n }\n return count;\n}\n
\u56fe 2-13 \u5c55\u793a\u4e86\u7ebf\u6027\u5bf9\u6570\u9636\u7684\u751f\u6210\u65b9\u5f0f\u3002\u4e8c\u53c9\u6811\u7684\u6bcf\u4e00\u5c42\u7684\u64cd\u4f5c\u603b\u6570\u90fd\u4e3a \\(n\\) \uff0c\u6811\u5171\u6709 \\(\\log_2 n + 1\\) \u5c42\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n \\log n)\\) \u3002
\u56fe 2-13 \u00a0 \u7ebf\u6027\u5bf9\u6570\u9636\u7684\u65f6\u95f4\u590d\u6742\u5ea6
\u4e3b\u6d41\u6392\u5e8f\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u901a\u5e38\u4e3a \\(O(n \\log n)\\) \uff0c\u4f8b\u5982\u5feb\u901f\u6392\u5e8f\u3001\u5f52\u5e76\u6392\u5e8f\u3001\u5806\u6392\u5e8f\u7b49\u3002
"},{"location":"chapter_computational_complexity/time_complexity/#7-on","title":"7. \u00a0 \u9636\u4e58\u9636 \\(O(n!)\\)","text":"\u9636\u4e58\u9636\u5bf9\u5e94\u6570\u5b66\u4e0a\u7684\u201c\u5168\u6392\u5217\u201d\u95ee\u9898\u3002\u7ed9\u5b9a \\(n\\) \u4e2a\u4e92\u4e0d\u91cd\u590d\u7684\u5143\u7d20\uff0c\u6c42\u5176\u6240\u6709\u53ef\u80fd\u7684\u6392\u5217\u65b9\u6848\uff0c\u65b9\u6848\u6570\u91cf\u4e3a\uff1a
\\[ n! = n \\times (n - 1) \\times (n - 2) \\times \\dots \\times 2 \\times 1 \\]\u9636\u4e58\u901a\u5e38\u4f7f\u7528\u9012\u5f52\u5b9e\u73b0\u3002\u5982\u56fe 2-14 \u548c\u4ee5\u4e0b\u4ee3\u7801\u6240\u793a\uff0c\u7b2c\u4e00\u5c42\u5206\u88c2\u51fa \\(n\\) \u4e2a\uff0c\u7b2c\u4e8c\u5c42\u5206\u88c2\u51fa \\(n - 1\\) \u4e2a\uff0c\u4ee5\u6b64\u7c7b\u63a8\uff0c\u76f4\u81f3\u7b2c \\(n\\) \u5c42\u65f6\u505c\u6b62\u5206\u88c2\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig time_complexity.pydef factorial_recur(n: int) -> int:\n \"\"\"\u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\"\"\"\n if n == 0:\n return 1\n count = 0\n # \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\n for _ in range(n):\n count += factorial_recur(n - 1)\n return count\n
time_complexity.cpp/* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint factorialRecur(int n) {\n if (n == 0)\n return 1;\n int count = 0;\n // \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\n for (int i = 0; i < n; i++) {\n count += factorialRecur(n - 1);\n }\n return count;\n}\n
time_complexity.java/* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint factorialRecur(int n) {\n if (n == 0)\n return 1;\n int count = 0;\n // \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\n for (int i = 0; i < n; i++) {\n count += factorialRecur(n - 1);\n }\n return count;\n}\n
time_complexity.cs/* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint FactorialRecur(int n) {\n if (n == 0) return 1;\n int count = 0;\n // \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\n for (int i = 0; i < n; i++) {\n count += FactorialRecur(n - 1);\n }\n return count;\n}\n
time_complexity.go/* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunc factorialRecur(n int) int {\n if n == 0 {\n return 1\n }\n count := 0\n // \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\n for i := 0; i < n; i++ {\n count += factorialRecur(n - 1)\n }\n return count\n}\n
time_complexity.swift/* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunc factorialRecur(n: Int) -> Int {\n if n == 0 {\n return 1\n }\n var count = 0\n // \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\n for _ in 0 ..< n {\n count += factorialRecur(n: n - 1)\n }\n return count\n}\n
time_complexity.js/* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction factorialRecur(n) {\n if (n === 0) return 1;\n let count = 0;\n // \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\n for (let i = 0; i < n; i++) {\n count += factorialRecur(n - 1);\n }\n return count;\n}\n
time_complexity.ts/* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction factorialRecur(n: number): number {\n if (n === 0) return 1;\n let count = 0;\n // \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\n for (let i = 0; i < n; i++) {\n count += factorialRecur(n - 1);\n }\n return count;\n}\n
time_complexity.dart/* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint factorialRecur(int n) {\n if (n == 0) return 1;\n int count = 0;\n // \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\n for (var i = 0; i < n; i++) {\n count += factorialRecur(n - 1);\n }\n return count;\n}\n
time_complexity.rs/* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfn factorial_recur(n: i32) -> i32 {\n if n == 0 {\n return 1;\n }\n let mut count = 0;\n // \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\n for _ in 0..n {\n count += factorial_recur(n - 1);\n }\n count\n}\n
time_complexity.c/* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint factorialRecur(int n) {\n if (n == 0)\n return 1;\n int count = 0;\n for (int i = 0; i < n; i++) {\n count += factorialRecur(n - 1);\n }\n return count;\n}\n
time_complexity.zig// \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\nfn factorialRecur(n: i32) i32 {\n if (n == 0) return 1;\n var count: i32 = 0;\n var i: i32 = 0;\n // \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\n while (i < n) : (i += 1) {\n count += factorialRecur(n - 1);\n }\n return count;\n}\n
\u56fe 2-14 \u00a0 \u9636\u4e58\u9636\u7684\u65f6\u95f4\u590d\u6742\u5ea6
\u8bf7\u6ce8\u610f\uff0c\u56e0\u4e3a\u5f53 \\(n \\geq 4\\) \u65f6\u6052\u6709 \\(n! > 2^n\\) \uff0c\u6240\u4ee5\u9636\u4e58\u9636\u6bd4\u6307\u6570\u9636\u589e\u957f\u5f97\u66f4\u5feb\uff0c\u5728 \\(n\\) \u8f83\u5927\u65f6\u4e5f\u662f\u4e0d\u53ef\u63a5\u53d7\u7684\u3002
"},{"location":"chapter_computational_complexity/time_complexity/#235","title":"2.3.5 \u00a0 \u6700\u5dee\u3001\u6700\u4f73\u3001\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6","text":"\u7b97\u6cd5\u7684\u65f6\u95f4\u6548\u7387\u5f80\u5f80\u4e0d\u662f\u56fa\u5b9a\u7684\uff0c\u800c\u662f\u4e0e\u8f93\u5165\u6570\u636e\u7684\u5206\u5e03\u6709\u5173\u3002\u5047\u8bbe\u8f93\u5165\u4e00\u4e2a\u957f\u5ea6\u4e3a \\(n\\) \u7684\u6570\u7ec4 nums
\uff0c\u5176\u4e2d nums
\u7531\u4ece \\(1\\) \u81f3 \\(n\\) \u7684\u6570\u5b57\u7ec4\u6210\uff0c\u6bcf\u4e2a\u6570\u5b57\u53ea\u51fa\u73b0\u4e00\u6b21\uff1b\u4f46\u5143\u7d20\u987a\u5e8f\u662f\u968f\u673a\u6253\u4e71\u7684\uff0c\u4efb\u52a1\u76ee\u6807\u662f\u8fd4\u56de\u5143\u7d20 \\(1\\) \u7684\u7d22\u5f15\u3002\u6211\u4eec\u53ef\u4ee5\u5f97\u51fa\u4ee5\u4e0b\u7ed3\u8bba\u3002
nums = [?, ?, ..., 1]
\uff0c\u5373\u5f53\u672b\u5c3e\u5143\u7d20\u662f \\(1\\) \u65f6\uff0c\u9700\u8981\u5b8c\u6574\u904d\u5386\u6570\u7ec4\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 \\(O(n)\\) \u3002nums = [1, ?, ?, ...]
\uff0c\u5373\u5f53\u9996\u4e2a\u5143\u7d20\u4e3a \\(1\\) \u65f6\uff0c\u65e0\u8bba\u6570\u7ec4\u591a\u957f\u90fd\u4e0d\u9700\u8981\u7ee7\u7eed\u904d\u5386\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 \\(\\Omega(1)\\) \u3002\u201c\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6\u201d\u5bf9\u5e94\u51fd\u6570\u6e10\u8fd1\u4e0a\u754c\uff0c\u4f7f\u7528\u5927 \\(O\\) \u8bb0\u53f7\u8868\u793a\u3002\u76f8\u5e94\u5730\uff0c\u201c\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6\u201d\u5bf9\u5e94\u51fd\u6570\u6e10\u8fd1\u4e0b\u754c\uff0c\u7528 \\(\\Omega\\) \u8bb0\u53f7\u8868\u793a\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig worst_best_time_complexity.pydef random_numbers(n: int) -> list[int]:\n \"\"\"\u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a: 1, 2, ..., n \uff0c\u987a\u5e8f\u88ab\u6253\u4e71\"\"\"\n # \u751f\u6210\u6570\u7ec4 nums =: 1, 2, 3, ..., n\n nums = [i for i in range(1, n + 1)]\n # \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\n random.shuffle(nums)\n return nums\n\ndef find_one(nums: list[int]) -> int:\n \"\"\"\u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15\"\"\"\n for i in range(len(nums)):\n # \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n # \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\n if nums[i] == 1:\n return i\n return -1\n
worst_best_time_complexity.cpp/* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nvector<int> randomNumbers(int n) {\n vector<int> nums(n);\n // \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\n for (int i = 0; i < n; i++) {\n nums[i] = i + 1;\n }\n // \u4f7f\u7528\u7cfb\u7edf\u65f6\u95f4\u751f\u6210\u968f\u673a\u79cd\u5b50\n unsigned seed = chrono::system_clock::now().time_since_epoch().count();\n // \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\n shuffle(nums.begin(), nums.end(), default_random_engine(seed));\n return nums;\n}\n\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nint findOne(vector<int> &nums) {\n for (int i = 0; i < nums.size(); i++) {\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\n if (nums[i] == 1)\n return i;\n }\n return -1;\n}\n
worst_best_time_complexity.java/* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nint[] randomNumbers(int n) {\n Integer[] nums = new Integer[n];\n // \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\n for (int i = 0; i < n; i++) {\n nums[i] = i + 1;\n }\n // \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\n Collections.shuffle(Arrays.asList(nums));\n // Integer[] -> int[]\n int[] res = new int[n];\n for (int i = 0; i < n; i++) {\n res[i] = nums[i];\n }\n return res;\n}\n\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nint findOne(int[] nums) {\n for (int i = 0; i < nums.length; i++) {\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\n if (nums[i] == 1)\n return i;\n }\n return -1;\n}\n
worst_best_time_complexity.cs/* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nint[] RandomNumbers(int n) {\n int[] nums = new int[n];\n // \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\n for (int i = 0; i < n; i++) {\n nums[i] = i + 1;\n }\n\n // \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\n for (int i = 0; i < nums.Length; i++) {\n int index = new Random().Next(i, nums.Length);\n (nums[i], nums[index]) = (nums[index], nums[i]);\n }\n return nums;\n}\n\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nint FindOne(int[] nums) {\n for (int i = 0; i < nums.Length; i++) {\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\n if (nums[i] == 1)\n return i;\n }\n return -1;\n}\n
worst_best_time_complexity.go/* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nfunc randomNumbers(n int) []int {\n nums := make([]int, n)\n // \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\n for i := 0; i < n; i++ {\n nums[i] = i + 1\n }\n // \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\n rand.Shuffle(len(nums), func(i, j int) {\n nums[i], nums[j] = nums[j], nums[i]\n })\n return nums\n}\n\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nfunc findOne(nums []int) int {\n for i := 0; i < len(nums); i++ {\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\n if nums[i] == 1 {\n return i\n }\n }\n return -1\n}\n
worst_best_time_complexity.swift/* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nfunc randomNumbers(n: Int) -> [Int] {\n // \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\n var nums = Array(1 ... n)\n // \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\n nums.shuffle()\n return nums\n}\n\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nfunc findOne(nums: [Int]) -> Int {\n for i in nums.indices {\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\n if nums[i] == 1 {\n return i\n }\n }\n return -1\n}\n
worst_best_time_complexity.js/* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nfunction randomNumbers(n) {\n const nums = Array(n);\n // \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\n for (let i = 0; i < n; i++) {\n nums[i] = i + 1;\n }\n // \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\n for (let i = 0; i < n; i++) {\n const r = Math.floor(Math.random() * (i + 1));\n const temp = nums[i];\n nums[i] = nums[r];\n nums[r] = temp;\n }\n return nums;\n}\n\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nfunction findOne(nums) {\n for (let i = 0; i < nums.length; i++) {\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\n if (nums[i] === 1) {\n return i;\n }\n }\n return -1;\n}\n
worst_best_time_complexity.ts/* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nfunction randomNumbers(n: number): number[] {\n const nums = Array(n);\n // \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\n for (let i = 0; i < n; i++) {\n nums[i] = i + 1;\n }\n // \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\n for (let i = 0; i < n; i++) {\n const r = Math.floor(Math.random() * (i + 1));\n const temp = nums[i];\n nums[i] = nums[r];\n nums[r] = temp;\n }\n return nums;\n}\n\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nfunction findOne(nums: number[]): number {\n for (let i = 0; i < nums.length; i++) {\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\n if (nums[i] === 1) {\n return i;\n }\n }\n return -1;\n}\n
worst_best_time_complexity.dart/* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nList<int> randomNumbers(int n) {\n final nums = List.filled(n, 0);\n // \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\n for (var i = 0; i < n; i++) {\n nums[i] = i + 1;\n }\n // \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\n nums.shuffle();\n\n return nums;\n}\n\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nint findOne(List<int> nums) {\n for (var i = 0; i < nums.length; i++) {\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\n if (nums[i] == 1) return i;\n }\n\n return -1;\n}\n
worst_best_time_complexity.rs/* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nfn random_numbers(n: i32) -> Vec<i32> {\n // \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\n let mut nums = (1..=n).collect::<Vec<i32>>();\n // \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\n nums.shuffle(&mut thread_rng());\n nums\n}\n\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nfn find_one(nums: &[i32]) -> Option<usize> {\n for i in 0..nums.len() {\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\n if nums[i] == 1 {\n return Some(i);\n }\n }\n None\n}\n
worst_best_time_complexity.c/* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nint *randomNumbers(int n) {\n // \u5206\u914d\u5806\u533a\u5185\u5b58\uff08\u521b\u5efa\u4e00\u7ef4\u53ef\u53d8\u957f\u6570\u7ec4\uff1a\u6570\u7ec4\u4e2d\u5143\u7d20\u6570\u91cf\u4e3a n \uff0c\u5143\u7d20\u7c7b\u578b\u4e3a int \uff09\n int *nums = (int *)malloc(n * sizeof(int));\n // \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\n for (int i = 0; i < n; i++) {\n nums[i] = i + 1;\n }\n // \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\n for (int i = n - 1; i > 0; i--) {\n int j = rand() % (i + 1);\n int temp = nums[i];\n nums[i] = nums[j];\n nums[j] = temp;\n }\n return nums;\n}\n\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nint findOne(int *nums, int n) {\n for (int i = 0; i < n; i++) {\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\n if (nums[i] == 1)\n return i;\n }\n return -1;\n}\n
worst_best_time_complexity.zig// \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71\nfn randomNumbers(comptime n: usize) [n]i32 {\n var nums: [n]i32 = undefined;\n // \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\n for (&nums, 0..) |*num, i| {\n num.* = @as(i32, @intCast(i)) + 1;\n }\n // \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\n const rand = std.crypto.random;\n rand.shuffle(i32, &nums);\n return nums;\n}\n\n// \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15\nfn findOne(nums: []i32) i32 {\n for (nums, 0..) |num, i| {\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\n if (num == 1) return @intCast(i);\n }\n return -1;\n}\n
\u503c\u5f97\u8bf4\u660e\u7684\u662f\uff0c\u6211\u4eec\u5728\u5b9e\u9645\u4e2d\u5f88\u5c11\u4f7f\u7528\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6\uff0c\u56e0\u4e3a\u901a\u5e38\u53ea\u6709\u5728\u5f88\u5c0f\u6982\u7387\u4e0b\u624d\u80fd\u8fbe\u5230\uff0c\u53ef\u80fd\u4f1a\u5e26\u6765\u4e00\u5b9a\u7684\u8bef\u5bfc\u6027\u3002\u800c\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6\u66f4\u4e3a\u5b9e\u7528\uff0c\u56e0\u4e3a\u5b83\u7ed9\u51fa\u4e86\u4e00\u4e2a\u6548\u7387\u5b89\u5168\u503c\uff0c\u8ba9\u6211\u4eec\u53ef\u4ee5\u653e\u5fc3\u5730\u4f7f\u7528\u7b97\u6cd5\u3002
\u4ece\u4e0a\u8ff0\u793a\u4f8b\u53ef\u4ee5\u770b\u51fa\uff0c\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6\u548c\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6\u53ea\u51fa\u73b0\u4e8e\u201c\u7279\u6b8a\u7684\u6570\u636e\u5206\u5e03\u201d\uff0c\u8fd9\u4e9b\u60c5\u51b5\u7684\u51fa\u73b0\u6982\u7387\u53ef\u80fd\u5f88\u5c0f\uff0c\u5e76\u4e0d\u80fd\u771f\u5b9e\u5730\u53cd\u6620\u7b97\u6cd5\u8fd0\u884c\u6548\u7387\u3002\u76f8\u6bd4\u4e4b\u4e0b\uff0c\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u53ef\u4ee5\u4f53\u73b0\u7b97\u6cd5\u5728\u968f\u673a\u8f93\u5165\u6570\u636e\u4e0b\u7684\u8fd0\u884c\u6548\u7387\uff0c\u7528 \\(\\Theta\\) \u8bb0\u53f7\u6765\u8868\u793a\u3002
\u5bf9\u4e8e\u90e8\u5206\u7b97\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u7b80\u5355\u5730\u63a8\u7b97\u51fa\u968f\u673a\u6570\u636e\u5206\u5e03\u4e0b\u7684\u5e73\u5747\u60c5\u51b5\u3002\u6bd4\u5982\u4e0a\u8ff0\u793a\u4f8b\uff0c\u7531\u4e8e\u8f93\u5165\u6570\u7ec4\u662f\u88ab\u6253\u4e71\u7684\uff0c\u56e0\u6b64\u5143\u7d20 \\(1\\) \u51fa\u73b0\u5728\u4efb\u610f\u7d22\u5f15\u7684\u6982\u7387\u90fd\u662f\u76f8\u7b49\u7684\uff0c\u90a3\u4e48\u7b97\u6cd5\u7684\u5e73\u5747\u5faa\u73af\u6b21\u6570\u5c31\u662f\u6570\u7ec4\u957f\u5ea6\u7684\u4e00\u534a \\(n / 2\\) \uff0c\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(\\Theta(n / 2) = \\Theta(n)\\) \u3002
\u4f46\u5bf9\u4e8e\u8f83\u4e3a\u590d\u6742\u7684\u7b97\u6cd5\uff0c\u8ba1\u7b97\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u5f80\u5f80\u6bd4\u8f83\u56f0\u96be\uff0c\u56e0\u4e3a\u5f88\u96be\u5206\u6790\u51fa\u5728\u6570\u636e\u5206\u5e03\u4e0b\u7684\u6574\u4f53\u6570\u5b66\u671f\u671b\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u901a\u5e38\u4f7f\u7528\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6\u4f5c\u4e3a\u7b97\u6cd5\u6548\u7387\u7684\u8bc4\u5224\u6807\u51c6\u3002
\u4e3a\u4ec0\u4e48\u5f88\u5c11\u770b\u5230 \\(\\Theta\\) \u7b26\u53f7\uff1f
\u53ef\u80fd\u7531\u4e8e \\(O\\) \u7b26\u53f7\u8fc7\u4e8e\u6717\u6717\u4e0a\u53e3\uff0c\u56e0\u6b64\u6211\u4eec\u5e38\u5e38\u4f7f\u7528\u5b83\u6765\u8868\u793a\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u3002\u4f46\u4ece\u4e25\u683c\u610f\u4e49\u4e0a\u8bb2\uff0c\u8fd9\u79cd\u505a\u6cd5\u5e76\u4e0d\u89c4\u8303\u3002\u5728\u672c\u4e66\u548c\u5176\u4ed6\u8d44\u6599\u4e2d\uff0c\u82e5\u9047\u5230\u7c7b\u4f3c\u201c\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6 \\(O(n)\\)\u201d\u7684\u8868\u8ff0\uff0c\u8bf7\u5c06\u5176\u76f4\u63a5\u7406\u89e3\u4e3a \\(\\Theta(n)\\) \u3002
"},{"location":"chapter_data_structure/","title":"\u7b2c 3 \u7ae0 \u00a0 \u6570\u636e\u7ed3\u6784","text":"Abstract
\u6570\u636e\u7ed3\u6784\u5982\u540c\u4e00\u526f\u7a33\u56fa\u800c\u591a\u6837\u7684\u6846\u67b6\u3002
\u5b83\u4e3a\u6570\u636e\u7684\u6709\u5e8f\u7ec4\u7ec7\u63d0\u4f9b\u4e86\u84dd\u56fe\uff0c\u7b97\u6cd5\u5f97\u4ee5\u5728\u6b64\u57fa\u7840\u4e0a\u751f\u52a8\u8d77\u6765\u3002
"},{"location":"chapter_data_structure/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"\u5f53\u8c08\u53ca\u8ba1\u7b97\u673a\u4e2d\u7684\u6570\u636e\u65f6\uff0c\u6211\u4eec\u4f1a\u60f3\u5230\u6587\u672c\u3001\u56fe\u7247\u3001\u89c6\u9891\u3001\u8bed\u97f3\u30013D \u6a21\u578b\u7b49\u5404\u79cd\u5f62\u5f0f\u3002\u5c3d\u7ba1\u8fd9\u4e9b\u6570\u636e\u7684\u7ec4\u7ec7\u5f62\u5f0f\u5404\u5f02\uff0c\u4f46\u5b83\u4eec\u90fd\u7531\u5404\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6784\u6210\u3002
\u57fa\u672c\u6570\u636e\u7c7b\u578b\u662f CPU \u53ef\u4ee5\u76f4\u63a5\u8fdb\u884c\u8fd0\u7b97\u7684\u7c7b\u578b\uff0c\u5728\u7b97\u6cd5\u4e2d\u76f4\u63a5\u88ab\u4f7f\u7528\uff0c\u4e3b\u8981\u5305\u62ec\u4ee5\u4e0b\u51e0\u79cd\u3002
byte
\u3001short
\u3001int
\u3001long
\u3002float
\u3001double
\uff0c\u7528\u4e8e\u8868\u793a\u5c0f\u6570\u3002char
\uff0c\u7528\u4e8e\u8868\u793a\u5404\u79cd\u8bed\u8a00\u7684\u5b57\u6bcd\u3001\u6807\u70b9\u7b26\u53f7\u751a\u81f3\u8868\u60c5\u7b26\u53f7\u7b49\u3002bool
\uff0c\u7528\u4e8e\u8868\u793a\u201c\u662f\u201d\u4e0e\u201c\u5426\u201d\u5224\u65ad\u3002\u57fa\u672c\u6570\u636e\u7c7b\u578b\u4ee5\u4e8c\u8fdb\u5236\u7684\u5f62\u5f0f\u5b58\u50a8\u5728\u8ba1\u7b97\u673a\u4e2d\u3002\u4e00\u4e2a\u4e8c\u8fdb\u5236\u4f4d\u5373\u4e3a \\(1\\) \u6bd4\u7279\u3002\u5728\u7edd\u5927\u591a\u6570\u73b0\u4ee3\u64cd\u4f5c\u7cfb\u7edf\u4e2d\uff0c\\(1\\) \u5b57\u8282\uff08byte\uff09\u7531 \\(8\\) \u6bd4\u7279\uff08bit\uff09\u7ec4\u6210\u3002
\u57fa\u672c\u6570\u636e\u7c7b\u578b\u7684\u53d6\u503c\u8303\u56f4\u53d6\u51b3\u4e8e\u5176\u5360\u7528\u7684\u7a7a\u95f4\u5927\u5c0f\u3002\u4e0b\u9762\u4ee5 Java \u4e3a\u4f8b\u3002
byte
\u5360\u7528 \\(1\\) byte = \\(8\\) bits \uff0c\u53ef\u4ee5\u8868\u793a \\(2^{8}\\) \u4e2a\u6570\u5b57\u3002int
\u5360\u7528 \\(4\\) bytes = \\(32\\) bits \uff0c\u53ef\u4ee5\u8868\u793a \\(2^{32}\\) \u4e2a\u6570\u5b57\u3002\u8868 3-1 \u5217\u4e3e\u4e86 Java \u4e2d\u5404\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u7684\u5360\u7528\u7a7a\u95f4\u3001\u53d6\u503c\u8303\u56f4\u548c\u9ed8\u8ba4\u503c\u3002\u6b64\u8868\u683c\u65e0\u987b\u6b7b\u8bb0\u786c\u80cc\uff0c\u5927\u81f4\u7406\u89e3\u5373\u53ef\uff0c\u9700\u8981\u65f6\u53ef\u4ee5\u901a\u8fc7\u67e5\u8868\u6765\u56de\u5fc6\u3002
\u8868 3-1 \u00a0 \u57fa\u672c\u6570\u636e\u7c7b\u578b\u7684\u5360\u7528\u7a7a\u95f4\u548c\u53d6\u503c\u8303\u56f4
\u7c7b\u578b \u7b26\u53f7 \u5360\u7528\u7a7a\u95f4 \u6700\u5c0f\u503c \u6700\u5927\u503c \u9ed8\u8ba4\u503c \u6574\u6570byte
1 byte \\(-2^7\\) (\\(-128\\)) \\(2^7 - 1\\) (\\(127\\)) \\(0\\) short
2 bytes \\(-2^{15}\\) \\(2^{15} - 1\\) \\(0\\) int
4 bytes \\(-2^{31}\\) \\(2^{31} - 1\\) \\(0\\) long
8 bytes \\(-2^{63}\\) \\(2^{63} - 1\\) \\(0\\) \u6d6e\u70b9\u6570 float
4 bytes \\(1.175 \\times 10^{-38}\\) \\(3.403 \\times 10^{38}\\) \\(0.0\\text{f}\\) double
8 bytes \\(2.225 \\times 10^{-308}\\) \\(1.798 \\times 10^{308}\\) \\(0.0\\) \u5b57\u7b26 char
2 bytes \\(0\\) \\(2^{16} - 1\\) \\(0\\) \u5e03\u5c14 bool
1 byte \\(\\text{false}\\) \\(\\text{true}\\) \\(\\text{false}\\) \u8bf7\u6ce8\u610f\uff0c\u8868 3-1 \u9488\u5bf9\u7684\u662f Java \u7684\u57fa\u672c\u6570\u636e\u7c7b\u578b\u7684\u60c5\u51b5\u3002\u6bcf\u79cd\u7f16\u7a0b\u8bed\u8a00\u90fd\u6709\u5404\u81ea\u7684\u6570\u636e\u7c7b\u578b\u5b9a\u4e49\uff0c\u5b83\u4eec\u7684\u5360\u7528\u7a7a\u95f4\u3001\u53d6\u503c\u8303\u56f4\u548c\u9ed8\u8ba4\u503c\u53ef\u80fd\u4f1a\u6709\u6240\u4e0d\u540c\u3002
int
\u53ef\u4ee5\u662f\u4efb\u610f\u5927\u5c0f\uff0c\u53ea\u53d7\u9650\u4e8e\u53ef\u7528\u5185\u5b58\uff1b\u6d6e\u70b9\u6570 float
\u662f\u53cc\u7cbe\u5ea6 64 \u4f4d\uff1b\u6ca1\u6709 char
\u7c7b\u578b\uff0c\u5355\u4e2a\u5b57\u7b26\u5b9e\u9645\u4e0a\u662f\u957f\u5ea6\u4e3a 1 \u7684\u5b57\u7b26\u4e32 str
\u3002char
\u7684\u5927\u5c0f\u5728 C \u548c C++ \u4e2d\u4e3a 1 \u5b57\u8282\uff0c\u5728\u5927\u591a\u6570\u7f16\u7a0b\u8bed\u8a00\u4e2d\u53d6\u51b3\u4e8e\u7279\u5b9a\u7684\u5b57\u7b26\u7f16\u7801\u65b9\u6cd5\uff0c\u8be6\u89c1\u201c\u5b57\u7b26\u7f16\u7801\u201d\u7ae0\u8282\u3002\u90a3\u4e48\uff0c\u57fa\u672c\u6570\u636e\u7c7b\u578b\u4e0e\u6570\u636e\u7ed3\u6784\u4e4b\u95f4\u6709\u4ec0\u4e48\u8054\u7cfb\u5462\uff1f\u6211\u4eec\u77e5\u9053\uff0c\u6570\u636e\u7ed3\u6784\u662f\u5728\u8ba1\u7b97\u673a\u4e2d\u7ec4\u7ec7\u4e0e\u5b58\u50a8\u6570\u636e\u7684\u65b9\u5f0f\u3002\u8fd9\u53e5\u8bdd\u7684\u4e3b\u8bed\u662f\u201c\u7ed3\u6784\u201d\u800c\u975e\u201c\u6570\u636e\u201d\u3002
\u5982\u679c\u60f3\u8868\u793a\u201c\u4e00\u6392\u6570\u5b57\u201d\uff0c\u6211\u4eec\u81ea\u7136\u4f1a\u60f3\u5230\u4f7f\u7528\u6570\u7ec4\u3002\u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u7684\u7ebf\u6027\u7ed3\u6784\u53ef\u4ee5\u8868\u793a\u6570\u5b57\u7684\u76f8\u90bb\u5173\u7cfb\u548c\u987a\u5e8f\u5173\u7cfb\uff0c\u4f46\u81f3\u4e8e\u5b58\u50a8\u7684\u5185\u5bb9\u662f\u6574\u6570 int
\u3001\u5c0f\u6570 float
\u6216\u662f\u5b57\u7b26 char
\uff0c\u5219\u4e0e\u201c\u6570\u636e\u7ed3\u6784\u201d\u65e0\u5173\u3002
\u6362\u53e5\u8bdd\u8bf4\uff0c\u57fa\u672c\u6570\u636e\u7c7b\u578b\u63d0\u4f9b\u4e86\u6570\u636e\u7684\u201c\u5185\u5bb9\u7c7b\u578b\u201d\uff0c\u800c\u6570\u636e\u7ed3\u6784\u63d0\u4f9b\u4e86\u6570\u636e\u7684\u201c\u7ec4\u7ec7\u65b9\u5f0f\u201d\u3002\u4f8b\u5982\u4ee5\u4e0b\u4ee3\u7801\uff0c\u6211\u4eec\u7528\u76f8\u540c\u7684\u6570\u636e\u7ed3\u6784\uff08\u6570\u7ec4\uff09\u6765\u5b58\u50a8\u4e0e\u8868\u793a\u4e0d\u540c\u7684\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff0c\u5305\u62ec int
\u3001float
\u3001char
\u3001bool
\u7b49\u3002
# \u4f7f\u7528\u591a\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6765\u521d\u59cb\u5316\u6570\u7ec4\nnumbers: list[int] = [0] * 5\ndecimals: list[float] = [0.0] * 5\n# Python \u7684\u5b57\u7b26\u5b9e\u9645\u4e0a\u662f\u957f\u5ea6\u4e3a 1 \u7684\u5b57\u7b26\u4e32\ncharacters: list[str] = ['0'] * 5\nbools: list[bool] = [False] * 5\n# Python \u7684\u5217\u8868\u53ef\u4ee5\u81ea\u7531\u5b58\u50a8\u5404\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u548c\u5bf9\u8c61\u5f15\u7528\ndata = [0, 0.0, 'a', False, ListNode(0)]\n
// \u4f7f\u7528\u591a\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6765\u521d\u59cb\u5316\u6570\u7ec4\nint numbers[5];\nfloat decimals[5];\nchar characters[5];\nbool bools[5];\n
// \u4f7f\u7528\u591a\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6765\u521d\u59cb\u5316\u6570\u7ec4\nint[] numbers = new int[5];\nfloat[] decimals = new float[5];\nchar[] characters = new char[5];\nboolean[] bools = new boolean[5];\n
// \u4f7f\u7528\u591a\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6765\u521d\u59cb\u5316\u6570\u7ec4\nint[] numbers = new int[5];\nfloat[] decimals = new float[5];\nchar[] characters = new char[5];\nbool[] bools = new bool[5];\n
// \u4f7f\u7528\u591a\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6765\u521d\u59cb\u5316\u6570\u7ec4\nvar numbers = [5]int{}\nvar decimals = [5]float64{}\nvar characters = [5]byte{}\nvar bools = [5]bool{}\n
// \u4f7f\u7528\u591a\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6765\u521d\u59cb\u5316\u6570\u7ec4\nlet numbers = Array(repeating: Int(), count: 5)\nlet decimals = Array(repeating: Double(), count: 5)\nlet characters = Array(repeating: Character(\"a\"), count: 5)\nlet bools = Array(repeating: Bool(), count: 5)\n
// JavaScript \u7684\u6570\u7ec4\u53ef\u4ee5\u81ea\u7531\u5b58\u50a8\u5404\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u548c\u5bf9\u8c61\nconst array = [0, 0.0, 'a', false];\n
// \u4f7f\u7528\u591a\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6765\u521d\u59cb\u5316\u6570\u7ec4\nconst numbers: number[] = [];\nconst characters: string[] = [];\nconst bools: boolean[] = [];\n
// \u4f7f\u7528\u591a\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6765\u521d\u59cb\u5316\u6570\u7ec4\nList<int> numbers = List.filled(5, 0);\nList<double> decimals = List.filled(5, 0.0);\nList<String> characters = List.filled(5, 'a');\nList<bool> bools = List.filled(5, false);\n
// \u4f7f\u7528\u591a\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6765\u521d\u59cb\u5316\u6570\u7ec4\nlet numbers: Vec<i32> = vec![0; 5];\nlet decimals: Vec<f32> = vec![0.0, 5];\nlet characters: Vec<char> = vec!['0'; 5];\nlet bools: Vec<bool> = vec![false; 5];\n
// \u4f7f\u7528\u591a\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6765\u521d\u59cb\u5316\u6570\u7ec4\nint numbers[10];\nfloat decimals[10];\nchar characters[10];\nbool bools[10];\n
\n
"},{"location":"chapter_data_structure/character_encoding/","title":"3.4 \u00a0 \u5b57\u7b26\u7f16\u7801 *","text":"\u5728\u8ba1\u7b97\u673a\u4e2d\uff0c\u6240\u6709\u6570\u636e\u90fd\u662f\u4ee5\u4e8c\u8fdb\u5236\u6570\u7684\u5f62\u5f0f\u5b58\u50a8\u7684\uff0c\u5b57\u7b26 char
\u4e5f\u4e0d\u4f8b\u5916\u3002\u4e3a\u4e86\u8868\u793a\u5b57\u7b26\uff0c\u6211\u4eec\u9700\u8981\u5efa\u7acb\u4e00\u5957\u201c\u5b57\u7b26\u96c6\u201d\uff0c\u89c4\u5b9a\u6bcf\u4e2a\u5b57\u7b26\u548c\u4e8c\u8fdb\u5236\u6570\u4e4b\u95f4\u7684\u4e00\u4e00\u5bf9\u5e94\u5173\u7cfb\u3002\u6709\u4e86\u5b57\u7b26\u96c6\u4e4b\u540e\uff0c\u8ba1\u7b97\u673a\u5c31\u53ef\u4ee5\u901a\u8fc7\u67e5\u8868\u5b8c\u6210\u4e8c\u8fdb\u5236\u6570\u5230\u5b57\u7b26\u7684\u8f6c\u6362\u3002
\u300cASCII \u7801\u300d\u662f\u6700\u65e9\u51fa\u73b0\u7684\u5b57\u7b26\u96c6\uff0c\u5176\u5168\u79f0\u4e3a American Standard Code for Information Interchange\uff08\u7f8e\u56fd\u6807\u51c6\u4fe1\u606f\u4ea4\u6362\u4ee3\u7801\uff09\u3002\u5b83\u4f7f\u7528 7 \u4f4d\u4e8c\u8fdb\u5236\u6570\uff08\u4e00\u4e2a\u5b57\u8282\u7684\u4f4e 7 \u4f4d\uff09\u8868\u793a\u4e00\u4e2a\u5b57\u7b26\uff0c\u6700\u591a\u80fd\u591f\u8868\u793a 128 \u4e2a\u4e0d\u540c\u7684\u5b57\u7b26\u3002\u5982\u56fe 3-6 \u6240\u793a\uff0cASCII \u7801\u5305\u62ec\u82f1\u6587\u5b57\u6bcd\u7684\u5927\u5c0f\u5199\u3001\u6570\u5b57 0 ~ 9\u3001\u4e00\u4e9b\u6807\u70b9\u7b26\u53f7\uff0c\u4ee5\u53ca\u4e00\u4e9b\u63a7\u5236\u5b57\u7b26\uff08\u5982\u6362\u884c\u7b26\u548c\u5236\u8868\u7b26\uff09\u3002
\u56fe 3-6 \u00a0 ASCII \u7801
\u7136\u800c\uff0cASCII \u7801\u4ec5\u80fd\u591f\u8868\u793a\u82f1\u6587\u3002\u968f\u7740\u8ba1\u7b97\u673a\u7684\u5168\u7403\u5316\uff0c\u8bde\u751f\u4e86\u4e00\u79cd\u80fd\u591f\u8868\u793a\u66f4\u591a\u8bed\u8a00\u7684\u5b57\u7b26\u96c6\u300cEASCII\u300d\u3002\u5b83\u5728 ASCII \u7684 7 \u4f4d\u57fa\u7840\u4e0a\u6269\u5c55\u5230 8 \u4f4d\uff0c\u80fd\u591f\u8868\u793a 256 \u4e2a\u4e0d\u540c\u7684\u5b57\u7b26\u3002
\u5728\u4e16\u754c\u8303\u56f4\u5185\uff0c\u9646\u7eed\u51fa\u73b0\u4e86\u4e00\u6279\u9002\u7528\u4e8e\u4e0d\u540c\u5730\u533a\u7684 EASCII \u5b57\u7b26\u96c6\u3002\u8fd9\u4e9b\u5b57\u7b26\u96c6\u7684\u524d 128 \u4e2a\u5b57\u7b26\u7edf\u4e00\u4e3a ASCII \u7801\uff0c\u540e 128 \u4e2a\u5b57\u7b26\u5b9a\u4e49\u4e0d\u540c\uff0c\u4ee5\u9002\u5e94\u4e0d\u540c\u8bed\u8a00\u7684\u9700\u6c42\u3002
"},{"location":"chapter_data_structure/character_encoding/#342-gbk","title":"3.4.2 \u00a0 GBK \u5b57\u7b26\u96c6","text":"\u540e\u6765\u4eba\u4eec\u53d1\u73b0\uff0cEASCII \u7801\u4ecd\u7136\u65e0\u6cd5\u6ee1\u8db3\u8bb8\u591a\u8bed\u8a00\u7684\u5b57\u7b26\u6570\u91cf\u8981\u6c42\u3002\u6bd4\u5982\u6c49\u5b57\u6709\u8fd1\u5341\u4e07\u4e2a\uff0c\u5149\u65e5\u5e38\u4f7f\u7528\u7684\u5c31\u6709\u51e0\u5343\u4e2a\u3002\u4e2d\u56fd\u56fd\u5bb6\u6807\u51c6\u603b\u5c40\u4e8e 1980 \u5e74\u53d1\u5e03\u4e86\u300cGB2312\u300d\u5b57\u7b26\u96c6\uff0c\u5176\u6536\u5f55\u4e86 6763 \u4e2a\u6c49\u5b57\uff0c\u57fa\u672c\u6ee1\u8db3\u4e86\u6c49\u5b57\u7684\u8ba1\u7b97\u673a\u5904\u7406\u9700\u8981\u3002
\u7136\u800c\uff0cGB2312 \u65e0\u6cd5\u5904\u7406\u90e8\u5206\u7f55\u89c1\u5b57\u548c\u7e41\u4f53\u5b57\u3002\u300cGBK\u300d\u5b57\u7b26\u96c6\u662f\u5728 GB2312 \u7684\u57fa\u7840\u4e0a\u6269\u5c55\u5f97\u5230\u7684\uff0c\u5b83\u5171\u6536\u5f55\u4e86 21886 \u4e2a\u6c49\u5b57\u3002\u5728 GBK \u7684\u7f16\u7801\u65b9\u6848\u4e2d\uff0cASCII \u5b57\u7b26\u4f7f\u7528\u4e00\u4e2a\u5b57\u8282\u8868\u793a\uff0c\u6c49\u5b57\u4f7f\u7528\u4e24\u4e2a\u5b57\u8282\u8868\u793a\u3002
"},{"location":"chapter_data_structure/character_encoding/#343-unicode","title":"3.4.3 \u00a0 Unicode \u5b57\u7b26\u96c6","text":"\u968f\u7740\u8ba1\u7b97\u673a\u6280\u672f\u7684\u84ec\u52c3\u53d1\u5c55\uff0c\u5b57\u7b26\u96c6\u4e0e\u7f16\u7801\u6807\u51c6\u767e\u82b1\u9f50\u653e\uff0c\u800c\u8fd9\u5e26\u6765\u4e86\u8bb8\u591a\u95ee\u9898\u3002\u4e00\u65b9\u9762\uff0c\u8fd9\u4e9b\u5b57\u7b26\u96c6\u4e00\u822c\u53ea\u5b9a\u4e49\u4e86\u7279\u5b9a\u8bed\u8a00\u7684\u5b57\u7b26\uff0c\u65e0\u6cd5\u5728\u591a\u8bed\u8a00\u73af\u5883\u4e0b\u6b63\u5e38\u5de5\u4f5c\u3002\u53e6\u4e00\u65b9\u9762\uff0c\u540c\u4e00\u79cd\u8bed\u8a00\u5b58\u5728\u591a\u79cd\u5b57\u7b26\u96c6\u6807\u51c6\uff0c\u5982\u679c\u4e24\u53f0\u8ba1\u7b97\u673a\u4f7f\u7528\u7684\u662f\u4e0d\u540c\u7684\u7f16\u7801\u6807\u51c6\uff0c\u5219\u5728\u4fe1\u606f\u4f20\u9012\u65f6\u5c31\u4f1a\u51fa\u73b0\u4e71\u7801\u3002
\u90a3\u4e2a\u65f6\u4ee3\u7684\u7814\u7a76\u4eba\u5458\u5c31\u5728\u60f3\uff1a\u5982\u679c\u63a8\u51fa\u4e00\u4e2a\u8db3\u591f\u5b8c\u6574\u7684\u5b57\u7b26\u96c6\uff0c\u5c06\u4e16\u754c\u8303\u56f4\u5185\u7684\u6240\u6709\u8bed\u8a00\u548c\u7b26\u53f7\u90fd\u6536\u5f55\u5176\u4e2d\uff0c\u4e0d\u5c31\u53ef\u4ee5\u89e3\u51b3\u8de8\u8bed\u8a00\u73af\u5883\u548c\u4e71\u7801\u95ee\u9898\u4e86\u5417\uff1f\u5728\u8fd9\u79cd\u60f3\u6cd5\u7684\u9a71\u52a8\u4e0b\uff0c\u4e00\u4e2a\u5927\u800c\u5168\u7684\u5b57\u7b26\u96c6 Unicode \u5e94\u8fd0\u800c\u751f\u3002
\u300cUnicode\u300d\u7684\u4e2d\u6587\u540d\u79f0\u4e3a\u201c\u7edf\u4e00\u7801\u201d\uff0c\u7406\u8bba\u4e0a\u80fd\u5bb9\u7eb3 100 \u591a\u4e07\u4e2a\u5b57\u7b26\u3002\u5b83\u81f4\u529b\u4e8e\u5c06\u5168\u7403\u8303\u56f4\u5185\u7684\u5b57\u7b26\u7eb3\u5165\u7edf\u4e00\u7684\u5b57\u7b26\u96c6\u4e4b\u4e2d\uff0c\u63d0\u4f9b\u4e00\u79cd\u901a\u7528\u7684\u5b57\u7b26\u96c6\u6765\u5904\u7406\u548c\u663e\u793a\u5404\u79cd\u8bed\u8a00\u6587\u5b57\uff0c\u51cf\u5c11\u56e0\u4e3a\u7f16\u7801\u6807\u51c6\u4e0d\u540c\u800c\u4ea7\u751f\u7684\u4e71\u7801\u95ee\u9898\u3002
\u81ea 1991 \u5e74\u53d1\u5e03\u4ee5\u6765\uff0cUnicode \u4e0d\u65ad\u6269\u5145\u65b0\u7684\u8bed\u8a00\u4e0e\u5b57\u7b26\u3002\u622a\u81f3 2022 \u5e74 9 \u6708\uff0cUnicode \u5df2\u7ecf\u5305\u542b 149186 \u4e2a\u5b57\u7b26\uff0c\u5305\u62ec\u5404\u79cd\u8bed\u8a00\u7684\u5b57\u7b26\u3001\u7b26\u53f7\u751a\u81f3\u8868\u60c5\u7b26\u53f7\u7b49\u3002\u5728\u5e9e\u5927\u7684 Unicode \u5b57\u7b26\u96c6\u4e2d\uff0c\u5e38\u7528\u7684\u5b57\u7b26\u5360\u7528 2 \u5b57\u8282\uff0c\u6709\u4e9b\u751f\u50fb\u7684\u5b57\u7b26\u5360\u7528 3 \u5b57\u8282\u751a\u81f3 4 \u5b57\u8282\u3002
Unicode \u662f\u4e00\u79cd\u901a\u7528\u5b57\u7b26\u96c6\uff0c\u672c\u8d28\u4e0a\u662f\u7ed9\u6bcf\u4e2a\u5b57\u7b26\u5206\u914d\u4e00\u4e2a\u7f16\u53f7\uff08\u79f0\u4e3a\u201c\u7801\u70b9\u201d\uff09\uff0c\u4f46\u5b83\u5e76\u6ca1\u6709\u89c4\u5b9a\u5728\u8ba1\u7b97\u673a\u4e2d\u5982\u4f55\u5b58\u50a8\u8fd9\u4e9b\u5b57\u7b26\u7801\u70b9\u3002\u6211\u4eec\u4e0d\u7981\u4f1a\u95ee\uff1a\u5f53\u591a\u79cd\u957f\u5ea6\u7684 Unicode \u7801\u70b9\u540c\u65f6\u51fa\u73b0\u5728\u4e00\u4e2a\u6587\u672c\u4e2d\u65f6\uff0c\u7cfb\u7edf\u5982\u4f55\u89e3\u6790\u5b57\u7b26\uff1f\u4f8b\u5982\u7ed9\u5b9a\u4e00\u4e2a\u957f\u5ea6\u4e3a 2 \u5b57\u8282\u7684\u7f16\u7801\uff0c\u7cfb\u7edf\u5982\u4f55\u786e\u8ba4\u5b83\u662f\u4e00\u4e2a 2 \u5b57\u8282\u7684\u5b57\u7b26\u8fd8\u662f\u4e24\u4e2a 1 \u5b57\u8282\u7684\u5b57\u7b26\uff1f
\u5bf9\u4e8e\u4ee5\u4e0a\u95ee\u9898\uff0c\u4e00\u79cd\u76f4\u63a5\u7684\u89e3\u51b3\u65b9\u6848\u662f\u5c06\u6240\u6709\u5b57\u7b26\u5b58\u50a8\u4e3a\u7b49\u957f\u7684\u7f16\u7801\u3002\u5982\u56fe 3-7 \u6240\u793a\uff0c\u201cHello\u201d\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u5360\u7528 1 \u5b57\u8282\uff0c\u201c\u7b97\u6cd5\u201d\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u5360\u7528 2 \u5b57\u8282\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u9ad8\u4f4d\u586b 0 \u5c06\u201cHello \u7b97\u6cd5\u201d\u4e2d\u7684\u6240\u6709\u5b57\u7b26\u90fd\u7f16\u7801\u4e3a 2 \u5b57\u8282\u957f\u5ea6\u3002\u8fd9\u6837\u7cfb\u7edf\u5c31\u53ef\u4ee5\u6bcf\u9694 2 \u5b57\u8282\u89e3\u6790\u4e00\u4e2a\u5b57\u7b26\uff0c\u6062\u590d\u8fd9\u4e2a\u77ed\u8bed\u7684\u5185\u5bb9\u4e86\u3002
\u56fe 3-7 \u00a0 Unicode \u7f16\u7801\u793a\u4f8b
\u7136\u800c ASCII \u7801\u5df2\u7ecf\u5411\u6211\u4eec\u8bc1\u660e\uff0c\u7f16\u7801\u82f1\u6587\u53ea\u9700 1 \u5b57\u8282\u3002\u82e5\u91c7\u7528\u4e0a\u8ff0\u65b9\u6848\uff0c\u82f1\u6587\u6587\u672c\u5360\u7528\u7a7a\u95f4\u7684\u5927\u5c0f\u5c06\u4f1a\u662f ASCII \u7f16\u7801\u4e0b\u7684\u4e24\u500d\uff0c\u975e\u5e38\u6d6a\u8d39\u5185\u5b58\u7a7a\u95f4\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u9700\u8981\u4e00\u79cd\u66f4\u52a0\u9ad8\u6548\u7684 Unicode \u7f16\u7801\u65b9\u6cd5\u3002
"},{"location":"chapter_data_structure/character_encoding/#344-utf-8","title":"3.4.4 \u00a0 UTF-8 \u7f16\u7801","text":"\u76ee\u524d\uff0cUTF-8 \u5df2\u6210\u4e3a\u56fd\u9645\u4e0a\u4f7f\u7528\u6700\u5e7f\u6cdb\u7684 Unicode \u7f16\u7801\u65b9\u6cd5\u3002\u5b83\u662f\u4e00\u79cd\u53ef\u53d8\u957f\u5ea6\u7684\u7f16\u7801\uff0c\u4f7f\u7528 1 \u5230 4 \u5b57\u8282\u6765\u8868\u793a\u4e00\u4e2a\u5b57\u7b26\uff0c\u6839\u636e\u5b57\u7b26\u7684\u590d\u6742\u6027\u800c\u53d8\u3002ASCII \u5b57\u7b26\u53ea\u9700 1 \u5b57\u8282\uff0c\u62c9\u4e01\u5b57\u6bcd\u548c\u5e0c\u814a\u5b57\u6bcd\u9700\u8981 2 \u5b57\u8282\uff0c\u5e38\u7528\u7684\u4e2d\u6587\u5b57\u7b26\u9700\u8981 3 \u5b57\u8282\uff0c\u5176\u4ed6\u7684\u4e00\u4e9b\u751f\u50fb\u5b57\u7b26\u9700\u8981 4 \u5b57\u8282\u3002
UTF-8 \u7684\u7f16\u7801\u89c4\u5219\u5e76\u4e0d\u590d\u6742\uff0c\u5206\u4e3a\u4ee5\u4e0b\u4e24\u79cd\u60c5\u51b5\u3002
\u56fe 3-8 \u5c55\u793a\u4e86\u201cHello\u7b97\u6cd5\u201d\u5bf9\u5e94\u7684 UTF-8 \u7f16\u7801\u3002\u89c2\u5bdf\u53d1\u73b0\uff0c\u7531\u4e8e\u6700\u9ad8 \\(n\\) \u4f4d\u90fd\u8bbe\u7f6e\u4e3a \\(1\\) \uff0c\u56e0\u6b64\u7cfb\u7edf\u53ef\u4ee5\u901a\u8fc7\u8bfb\u53d6\u6700\u9ad8\u4f4d \\(1\\) \u7684\u4e2a\u6570\u6765\u89e3\u6790\u51fa\u5b57\u7b26\u7684\u957f\u5ea6\u4e3a \\(n\\) \u3002
\u4f46\u4e3a\u4ec0\u4e48\u8981\u5c06\u5176\u4f59\u6240\u6709\u5b57\u8282\u7684\u9ad8 2 \u4f4d\u90fd\u8bbe\u7f6e\u4e3a \\(10\\) \u5462\uff1f\u5b9e\u9645\u4e0a\uff0c\u8fd9\u4e2a \\(10\\) \u80fd\u591f\u8d77\u5230\u6821\u9a8c\u7b26\u7684\u4f5c\u7528\u3002\u5047\u8bbe\u7cfb\u7edf\u4ece\u4e00\u4e2a\u9519\u8bef\u7684\u5b57\u8282\u5f00\u59cb\u89e3\u6790\u6587\u672c\uff0c\u5b57\u8282\u5934\u90e8\u7684 \\(10\\) \u80fd\u591f\u5e2e\u52a9\u7cfb\u7edf\u5feb\u901f\u5224\u65ad\u51fa\u5f02\u5e38\u3002
\u4e4b\u6240\u4ee5\u5c06 \\(10\\) \u5f53\u4f5c\u6821\u9a8c\u7b26\uff0c\u662f\u56e0\u4e3a\u5728 UTF-8 \u7f16\u7801\u89c4\u5219\u4e0b\uff0c\u4e0d\u53ef\u80fd\u6709\u5b57\u7b26\u7684\u6700\u9ad8\u4e24\u4f4d\u662f \\(10\\) \u3002\u8fd9\u4e2a\u7ed3\u8bba\u53ef\u4ee5\u7528\u53cd\u8bc1\u6cd5\u6765\u8bc1\u660e\uff1a\u5047\u8bbe\u4e00\u4e2a\u5b57\u7b26\u7684\u6700\u9ad8\u4e24\u4f4d\u662f \\(10\\) \uff0c\u8bf4\u660e\u8be5\u5b57\u7b26\u7684\u957f\u5ea6\u4e3a \\(1\\) \uff0c\u5bf9\u5e94 ASCII \u7801\u3002\u800c ASCII \u7801\u7684\u6700\u9ad8\u4f4d\u5e94\u8be5\u662f \\(0\\) \uff0c\u4e0e\u5047\u8bbe\u77db\u76fe\u3002
\u56fe 3-8 \u00a0 UTF-8 \u7f16\u7801\u793a\u4f8b
\u9664\u4e86 UTF-8 \u4e4b\u5916\uff0c\u5e38\u89c1\u7684\u7f16\u7801\u65b9\u5f0f\u8fd8\u5305\u62ec\u4ee5\u4e0b\u4e24\u79cd\u3002
\u4ece\u5b58\u50a8\u7a7a\u95f4\u5360\u7528\u7684\u89d2\u5ea6\u770b\uff0c\u4f7f\u7528 UTF-8 \u8868\u793a\u82f1\u6587\u5b57\u7b26\u975e\u5e38\u9ad8\u6548\uff0c\u56e0\u4e3a\u5b83\u4ec5\u9700 1 \u5b57\u8282\uff1b\u4f7f\u7528 UTF-16 \u7f16\u7801\u67d0\u4e9b\u975e\u82f1\u6587\u5b57\u7b26\uff08\u4f8b\u5982\u4e2d\u6587\uff09\u4f1a\u66f4\u52a0\u9ad8\u6548\uff0c\u56e0\u4e3a\u5b83\u4ec5\u9700 2 \u5b57\u8282\uff0c\u800c UTF-8 \u53ef\u80fd\u9700\u8981 3 \u5b57\u8282\u3002
\u4ece\u517c\u5bb9\u6027\u7684\u89d2\u5ea6\u770b\uff0cUTF-8 \u7684\u901a\u7528\u6027\u6700\u4f73\uff0c\u8bb8\u591a\u5de5\u5177\u548c\u5e93\u4f18\u5148\u652f\u6301 UTF-8 \u3002
"},{"location":"chapter_data_structure/character_encoding/#345","title":"3.4.5 \u00a0 \u7f16\u7a0b\u8bed\u8a00\u7684\u5b57\u7b26\u7f16\u7801","text":"\u5bf9\u4e8e\u4ee5\u5f80\u7684\u5927\u591a\u6570\u7f16\u7a0b\u8bed\u8a00\uff0c\u7a0b\u5e8f\u8fd0\u884c\u4e2d\u7684\u5b57\u7b26\u4e32\u90fd\u91c7\u7528 UTF-16 \u6216 UTF-32 \u8fd9\u7c7b\u7b49\u957f\u7684\u7f16\u7801\u3002\u5728\u7b49\u957f\u7f16\u7801\u4e0b\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u770b\u4f5c\u6570\u7ec4\u6765\u5904\u7406\uff0c\u8fd9\u79cd\u505a\u6cd5\u5177\u6709\u4ee5\u4e0b\u4f18\u70b9\u3002
\u5b9e\u9645\u4e0a\uff0c\u7f16\u7a0b\u8bed\u8a00\u7684\u5b57\u7b26\u7f16\u7801\u65b9\u6848\u8bbe\u8ba1\u662f\u4e00\u4e2a\u5f88\u6709\u8da3\u7684\u8bdd\u9898\uff0c\u6d89\u53ca\u8bb8\u591a\u56e0\u7d20\u3002
String
\u7c7b\u578b\u4f7f\u7528 UTF-16 \u7f16\u7801\uff0c\u6bcf\u4e2a\u5b57\u7b26\u5360\u7528 2 \u5b57\u8282\u3002\u8fd9\u662f\u56e0\u4e3a Java \u8bed\u8a00\u8bbe\u8ba1\u4e4b\u521d\uff0c\u4eba\u4eec\u8ba4\u4e3a 16 \u4f4d\u8db3\u4ee5\u8868\u793a\u6240\u6709\u53ef\u80fd\u7684\u5b57\u7b26\u3002\u7136\u800c\uff0c\u8fd9\u662f\u4e00\u4e2a\u4e0d\u6b63\u786e\u7684\u5224\u65ad\u3002\u540e\u6765 Unicode \u89c4\u8303\u6269\u5c55\u5230\u4e86\u8d85\u8fc7 16 \u4f4d\uff0c\u6240\u4ee5 Java \u4e2d\u7684\u5b57\u7b26\u73b0\u5728\u53ef\u80fd\u7531\u4e00\u5bf9 16 \u4f4d\u7684\u503c\uff08\u79f0\u4e3a\u201c\u4ee3\u7406\u5bf9\u201d\uff09\u8868\u793a\u3002\u7531\u4e8e\u4ee5\u4e0a\u7f16\u7a0b\u8bed\u8a00\u5bf9\u5b57\u7b26\u6570\u91cf\u7684\u4f4e\u4f30\uff0c\u5b83\u4eec\u4e0d\u5f97\u4e0d\u91c7\u53d6\u201c\u4ee3\u7406\u5bf9\u201d\u7684\u65b9\u5f0f\u6765\u8868\u793a\u8d85\u8fc7 16 \u4f4d\u957f\u5ea6\u7684 Unicode \u5b57\u7b26\u3002\u8fd9\u662f\u4e00\u4e2a\u4e0d\u5f97\u5df2\u4e3a\u4e4b\u7684\u65e0\u5948\u4e4b\u4e3e\u3002\u4e00\u65b9\u9762\uff0c\u5305\u542b\u4ee3\u7406\u5bf9\u7684\u5b57\u7b26\u4e32\u4e2d\uff0c\u4e00\u4e2a\u5b57\u7b26\u53ef\u80fd\u5360\u7528 2 \u5b57\u8282\u6216 4 \u5b57\u8282\uff0c\u4ece\u800c\u4e27\u5931\u4e86\u7b49\u957f\u7f16\u7801\u7684\u4f18\u52bf\u3002\u53e6\u4e00\u65b9\u9762\uff0c\u5904\u7406\u4ee3\u7406\u5bf9\u9700\u8981\u589e\u52a0\u989d\u5916\u4ee3\u7801\uff0c\u8fd9\u63d0\u9ad8\u4e86\u7f16\u7a0b\u7684\u590d\u6742\u6027\u548c\u8c03\u8bd5\u96be\u5ea6\u3002
\u51fa\u4e8e\u4ee5\u4e0a\u539f\u56e0\uff0c\u90e8\u5206\u7f16\u7a0b\u8bed\u8a00\u63d0\u51fa\u4e86\u4e00\u4e9b\u4e0d\u540c\u7684\u7f16\u7801\u65b9\u6848\u3002
str
\u4f7f\u7528 Unicode \u7f16\u7801\uff0c\u5e76\u91c7\u7528\u4e00\u79cd\u7075\u6d3b\u7684\u5b57\u7b26\u4e32\u8868\u793a\uff0c\u5b58\u50a8\u7684\u5b57\u7b26\u957f\u5ea6\u53d6\u51b3\u4e8e\u5b57\u7b26\u4e32\u4e2d\u6700\u5927\u7684 Unicode \u7801\u70b9\u3002\u82e5\u5b57\u7b26\u4e32\u4e2d\u5168\u90e8\u662f ASCII \u5b57\u7b26\uff0c\u5219\u6bcf\u4e2a\u5b57\u7b26\u5360\u7528 1 \u5b57\u8282\uff1b\u5982\u679c\u6709\u5b57\u7b26\u8d85\u51fa\u4e86 ASCII \u8303\u56f4\uff0c\u4f46\u5168\u90e8\u5728\u57fa\u672c\u591a\u8bed\u8a00\u5e73\u9762\uff08BMP\uff09\u5185\uff0c\u5219\u6bcf\u4e2a\u5b57\u7b26\u5360\u7528 2 \u5b57\u8282\uff1b\u5982\u679c\u6709\u8d85\u51fa BMP \u7684\u5b57\u7b26\uff0c\u5219\u6bcf\u4e2a\u5b57\u7b26\u5360\u7528 4 \u5b57\u8282\u3002string
\u7c7b\u578b\u5728\u5185\u90e8\u4f7f\u7528 UTF-8 \u7f16\u7801\u3002Go \u8bed\u8a00\u8fd8\u63d0\u4f9b\u4e86 rune
\u7c7b\u578b\uff0c\u5b83\u7528\u4e8e\u8868\u793a\u5355\u4e2a Unicode \u7801\u70b9\u3002char
\u7c7b\u578b\uff0c\u7528\u4e8e\u8868\u793a\u5355\u4e2a Unicode \u7801\u70b9\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u4ee5\u4e0a\u8ba8\u8bba\u7684\u90fd\u662f\u5b57\u7b26\u4e32\u5728\u7f16\u7a0b\u8bed\u8a00\u4e2d\u7684\u5b58\u50a8\u65b9\u5f0f\uff0c\u8fd9\u548c\u5b57\u7b26\u4e32\u5982\u4f55\u5728\u6587\u4ef6\u4e2d\u5b58\u50a8\u6216\u5728\u7f51\u7edc\u4e2d\u4f20\u8f93\u662f\u4e0d\u540c\u7684\u95ee\u9898\u3002\u5728\u6587\u4ef6\u5b58\u50a8\u6216\u7f51\u7edc\u4f20\u8f93\u4e2d\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u5c06\u5b57\u7b26\u4e32\u7f16\u7801\u4e3a UTF-8 \u683c\u5f0f\uff0c\u4ee5\u8fbe\u5230\u6700\u4f18\u7684\u517c\u5bb9\u6027\u548c\u7a7a\u95f4\u6548\u7387\u3002
"},{"location":"chapter_data_structure/classification_of_data_structure/","title":"3.1 \u00a0 \u6570\u636e\u7ed3\u6784\u5206\u7c7b","text":"\u5e38\u89c1\u7684\u6570\u636e\u7ed3\u6784\u5305\u62ec\u6570\u7ec4\u3001\u94fe\u8868\u3001\u6808\u3001\u961f\u5217\u3001\u54c8\u5e0c\u8868\u3001\u6811\u3001\u5806\u3001\u56fe\uff0c\u5b83\u4eec\u53ef\u4ee5\u4ece\u201c\u903b\u8f91\u7ed3\u6784\u201d\u548c\u201c\u7269\u7406\u7ed3\u6784\u201d\u4e24\u4e2a\u7ef4\u5ea6\u8fdb\u884c\u5206\u7c7b\u3002
"},{"location":"chapter_data_structure/classification_of_data_structure/#311","title":"3.1.1 \u00a0 \u903b\u8f91\u7ed3\u6784\uff1a\u7ebf\u6027\u4e0e\u975e\u7ebf\u6027","text":"\u903b\u8f91\u7ed3\u6784\u63ed\u793a\u4e86\u6570\u636e\u5143\u7d20\u4e4b\u95f4\u7684\u903b\u8f91\u5173\u7cfb\u3002\u5728\u6570\u7ec4\u548c\u94fe\u8868\u4e2d\uff0c\u6570\u636e\u6309\u7167\u4e00\u5b9a\u987a\u5e8f\u6392\u5217\uff0c\u4f53\u73b0\u4e86\u6570\u636e\u4e4b\u95f4\u7684\u7ebf\u6027\u5173\u7cfb\uff1b\u800c\u5728\u6811\u4e2d\uff0c\u6570\u636e\u4ece\u9876\u90e8\u5411\u4e0b\u6309\u5c42\u6b21\u6392\u5217\uff0c\u8868\u73b0\u51fa\u201c\u7956\u5148\u201d\u4e0e\u201c\u540e\u4ee3\u201d\u4e4b\u95f4\u7684\u6d3e\u751f\u5173\u7cfb\uff1b\u56fe\u5219\u7531\u8282\u70b9\u548c\u8fb9\u6784\u6210\uff0c\u53cd\u6620\u4e86\u590d\u6742\u7684\u7f51\u7edc\u5173\u7cfb\u3002
\u5982\u56fe 3-1 \u6240\u793a\uff0c\u903b\u8f91\u7ed3\u6784\u53ef\u5206\u4e3a\u201c\u7ebf\u6027\u201d\u548c\u201c\u975e\u7ebf\u6027\u201d\u4e24\u5927\u7c7b\u3002\u7ebf\u6027\u7ed3\u6784\u6bd4\u8f83\u76f4\u89c2\uff0c\u6307\u6570\u636e\u5728\u903b\u8f91\u5173\u7cfb\u4e0a\u5448\u7ebf\u6027\u6392\u5217\uff1b\u975e\u7ebf\u6027\u7ed3\u6784\u5219\u76f8\u53cd\uff0c\u5448\u975e\u7ebf\u6027\u6392\u5217\u3002
\u56fe 3-1 \u00a0 \u7ebf\u6027\u6570\u636e\u7ed3\u6784\u4e0e\u975e\u7ebf\u6027\u6570\u636e\u7ed3\u6784
\u975e\u7ebf\u6027\u6570\u636e\u7ed3\u6784\u53ef\u4ee5\u8fdb\u4e00\u6b65\u5212\u5206\u4e3a\u6811\u5f62\u7ed3\u6784\u548c\u7f51\u72b6\u7ed3\u6784\u3002
\u5f53\u7b97\u6cd5\u7a0b\u5e8f\u8fd0\u884c\u65f6\uff0c\u6b63\u5728\u5904\u7406\u7684\u6570\u636e\u4e3b\u8981\u5b58\u50a8\u5728\u5185\u5b58\u4e2d\u3002\u56fe 3-2 \u5c55\u793a\u4e86\u4e00\u4e2a\u8ba1\u7b97\u673a\u5185\u5b58\u6761\uff0c\u5176\u4e2d\u6bcf\u4e2a\u9ed1\u8272\u65b9\u5757\u90fd\u5305\u542b\u4e00\u5757\u5185\u5b58\u7a7a\u95f4\u3002\u6211\u4eec\u53ef\u4ee5\u5c06\u5185\u5b58\u60f3\u8c61\u6210\u4e00\u4e2a\u5de8\u5927\u7684 Excel \u8868\u683c\uff0c\u5176\u4e2d\u6bcf\u4e2a\u5355\u5143\u683c\u90fd\u53ef\u4ee5\u5b58\u50a8\u4e00\u5b9a\u5927\u5c0f\u7684\u6570\u636e\u3002
\u7cfb\u7edf\u901a\u8fc7\u5185\u5b58\u5730\u5740\u6765\u8bbf\u95ee\u76ee\u6807\u4f4d\u7f6e\u7684\u6570\u636e\u3002\u5982\u56fe 3-2 \u6240\u793a\uff0c\u8ba1\u7b97\u673a\u6839\u636e\u7279\u5b9a\u89c4\u5219\u4e3a\u8868\u683c\u4e2d\u7684\u6bcf\u4e2a\u5355\u5143\u683c\u5206\u914d\u7f16\u53f7\uff0c\u786e\u4fdd\u6bcf\u4e2a\u5185\u5b58\u7a7a\u95f4\u90fd\u6709\u552f\u4e00\u7684\u5185\u5b58\u5730\u5740\u3002\u6709\u4e86\u8fd9\u4e9b\u5730\u5740\uff0c\u7a0b\u5e8f\u4fbf\u53ef\u4ee5\u8bbf\u95ee\u5185\u5b58\u4e2d\u7684\u6570\u636e\u3002
\u56fe 3-2 \u00a0 \u5185\u5b58\u6761\u3001\u5185\u5b58\u7a7a\u95f4\u3001\u5185\u5b58\u5730\u5740
Tip
\u503c\u5f97\u8bf4\u660e\u7684\u662f\uff0c\u5c06\u5185\u5b58\u6bd4\u4f5c Excel \u8868\u683c\u662f\u4e00\u4e2a\u7b80\u5316\u7684\u7c7b\u6bd4\uff0c\u5b9e\u9645\u5185\u5b58\u7684\u5de5\u4f5c\u673a\u5236\u6bd4\u8f83\u590d\u6742\uff0c\u6d89\u53ca\u5730\u5740\u7a7a\u95f4\u3001\u5185\u5b58\u7ba1\u7406\u3001\u7f13\u5b58\u673a\u5236\u3001\u865a\u62df\u5185\u5b58\u548c\u7269\u7406\u5185\u5b58\u7b49\u6982\u5ff5\u3002
\u5185\u5b58\u662f\u6240\u6709\u7a0b\u5e8f\u7684\u5171\u4eab\u8d44\u6e90\uff0c\u5f53\u67d0\u5757\u5185\u5b58\u88ab\u67d0\u4e2a\u7a0b\u5e8f\u5360\u7528\u65f6\uff0c\u5219\u65e0\u6cd5\u88ab\u5176\u4ed6\u7a0b\u5e8f\u540c\u65f6\u4f7f\u7528\u4e86\u3002\u56e0\u6b64\u5728\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7684\u8bbe\u8ba1\u4e2d\uff0c\u5185\u5b58\u8d44\u6e90\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u8003\u8651\u56e0\u7d20\u3002\u6bd4\u5982\uff0c\u7b97\u6cd5\u6240\u5360\u7528\u7684\u5185\u5b58\u5cf0\u503c\u4e0d\u5e94\u8d85\u8fc7\u7cfb\u7edf\u5269\u4f59\u7a7a\u95f2\u5185\u5b58\uff1b\u5982\u679c\u7f3a\u5c11\u8fde\u7eed\u5927\u5757\u7684\u5185\u5b58\u7a7a\u95f4\uff0c\u90a3\u4e48\u6240\u9009\u7528\u7684\u6570\u636e\u7ed3\u6784\u5fc5\u987b\u80fd\u591f\u5b58\u50a8\u5728\u5206\u6563\u7684\u5185\u5b58\u7a7a\u95f4\u5185\u3002
\u5982\u56fe 3-3 \u6240\u793a\uff0c\u7269\u7406\u7ed3\u6784\u53cd\u6620\u4e86\u6570\u636e\u5728\u8ba1\u7b97\u673a\u5185\u5b58\u4e2d\u7684\u5b58\u50a8\u65b9\u5f0f\uff0c\u53ef\u5206\u4e3a\u8fde\u7eed\u7a7a\u95f4\u5b58\u50a8\uff08\u6570\u7ec4\uff09\u548c\u5206\u6563\u7a7a\u95f4\u5b58\u50a8\uff08\u94fe\u8868\uff09\u3002\u7269\u7406\u7ed3\u6784\u4ece\u5e95\u5c42\u51b3\u5b9a\u4e86\u6570\u636e\u7684\u8bbf\u95ee\u3001\u66f4\u65b0\u3001\u589e\u5220\u7b49\u64cd\u4f5c\u65b9\u6cd5\uff0c\u4e24\u79cd\u7269\u7406\u7ed3\u6784\u5728\u65f6\u95f4\u6548\u7387\u548c\u7a7a\u95f4\u6548\u7387\u65b9\u9762\u5448\u73b0\u51fa\u4e92\u8865\u7684\u7279\u70b9\u3002
\u56fe 3-3 \u00a0 \u8fde\u7eed\u7a7a\u95f4\u5b58\u50a8\u4e0e\u5206\u6563\u7a7a\u95f4\u5b58\u50a8
\u503c\u5f97\u8bf4\u660e\u7684\u662f\uff0c\u6240\u6709\u6570\u636e\u7ed3\u6784\u90fd\u662f\u57fa\u4e8e\u6570\u7ec4\u3001\u94fe\u8868\u6216\u4e8c\u8005\u7684\u7ec4\u5408\u5b9e\u73b0\u7684\u3002\u4f8b\u5982\uff0c\u6808\u548c\u961f\u5217\u65e2\u53ef\u4ee5\u4f7f\u7528\u6570\u7ec4\u5b9e\u73b0\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528\u94fe\u8868\u5b9e\u73b0\uff1b\u800c\u54c8\u5e0c\u8868\u7684\u5b9e\u73b0\u53ef\u80fd\u540c\u65f6\u5305\u542b\u6570\u7ec4\u548c\u94fe\u8868\u3002
\u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6570\u636e\u7ed3\u6784\u4e5f\u79f0\u201c\u9759\u6001\u6570\u636e\u7ed3\u6784\u201d\uff0c\u8fd9\u610f\u5473\u7740\u6b64\u7c7b\u6570\u636e\u7ed3\u6784\u5728\u521d\u59cb\u5316\u540e\u957f\u5ea6\u4e0d\u53ef\u53d8\u3002\u76f8\u5bf9\u5e94\u5730\uff0c\u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6570\u636e\u7ed3\u6784\u79f0\u201c\u52a8\u6001\u6570\u636e\u7ed3\u6784\u201d\uff0c\u8fd9\u7c7b\u6570\u636e\u7ed3\u6784\u5728\u521d\u59cb\u5316\u540e\uff0c\u4ecd\u53ef\u4ee5\u5728\u7a0b\u5e8f\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u5bf9\u5176\u957f\u5ea6\u8fdb\u884c\u8c03\u6574\u3002
Tip
\u5982\u679c\u4f60\u611f\u89c9\u7269\u7406\u7ed3\u6784\u7406\u89e3\u8d77\u6765\u6709\u56f0\u96be\uff0c\u5efa\u8bae\u5148\u9605\u8bfb\u4e0b\u4e00\u7ae0\uff0c\u7136\u540e\u518d\u56de\u987e\u672c\u8282\u5185\u5bb9\u3002
"},{"location":"chapter_data_structure/number_encoding/","title":"3.3 \u00a0 \u6570\u5b57\u7f16\u7801 *","text":"Note
\u5728\u672c\u4e66\u4e2d\uff0c\u6807\u9898\u5e26\u6709 * \u7b26\u53f7\u7684\u662f\u9009\u8bfb\u7ae0\u8282\u3002\u5982\u679c\u4f60\u65f6\u95f4\u6709\u9650\u6216\u611f\u5230\u7406\u89e3\u56f0\u96be\uff0c\u53ef\u4ee5\u5148\u8df3\u8fc7\uff0c\u7b49\u5b66\u5b8c\u5fc5\u8bfb\u7ae0\u8282\u540e\u518d\u5355\u72ec\u653b\u514b\u3002
"},{"location":"chapter_data_structure/number_encoding/#331","title":"3.3.1 \u00a0 \u6574\u6570\u7f16\u7801","text":"\u5728\u4e0a\u4e00\u8282\u7684\u8868\u683c\u4e2d\u6211\u4eec\u53d1\u73b0\uff0c\u6240\u6709\u6574\u6570\u7c7b\u578b\u80fd\u591f\u8868\u793a\u7684\u8d1f\u6570\u90fd\u6bd4\u6b63\u6570\u591a\u4e00\u4e2a\uff0c\u4f8b\u5982 byte
\u7684\u53d6\u503c\u8303\u56f4\u662f \\([-128, 127]\\) \u3002\u8fd9\u4e2a\u73b0\u8c61\u6bd4\u8f83\u53cd\u76f4\u89c9\uff0c\u5b83\u7684\u5185\u5728\u539f\u56e0\u6d89\u53ca\u539f\u7801\u3001\u53cd\u7801\u3001\u8865\u7801\u7684\u76f8\u5173\u77e5\u8bc6\u3002
\u9996\u5148\u9700\u8981\u6307\u51fa\uff0c\u6570\u5b57\u662f\u4ee5\u201c\u8865\u7801\u201d\u7684\u5f62\u5f0f\u5b58\u50a8\u5728\u8ba1\u7b97\u673a\u4e2d\u7684\u3002\u5728\u5206\u6790\u8fd9\u6837\u505a\u7684\u539f\u56e0\u4e4b\u524d\uff0c\u9996\u5148\u7ed9\u51fa\u4e09\u8005\u7684\u5b9a\u4e49\u3002
\u56fe 3-4 \u5c55\u793a\u4e86\u539f\u7801\u3001\u53cd\u7801\u548c\u8865\u7801\u4e4b\u95f4\u7684\u8f6c\u6362\u65b9\u6cd5\u3002
\u56fe 3-4 \u00a0 \u539f\u7801\u3001\u53cd\u7801\u4e0e\u8865\u7801\u4e4b\u95f4\u7684\u76f8\u4e92\u8f6c\u6362
\u300c\u539f\u7801 sign-magnitude\u300d\u867d\u7136\u6700\u76f4\u89c2\uff0c\u4f46\u5b58\u5728\u4e00\u4e9b\u5c40\u9650\u6027\u3002\u4e00\u65b9\u9762\uff0c\u8d1f\u6570\u7684\u539f\u7801\u4e0d\u80fd\u76f4\u63a5\u7528\u4e8e\u8fd0\u7b97\u3002\u4f8b\u5982\u5728\u539f\u7801\u4e0b\u8ba1\u7b97 \\(1 + (-2)\\) \uff0c\u5f97\u5230\u7684\u7ed3\u679c\u662f \\(-3\\) \uff0c\u8fd9\u663e\u7136\u662f\u4e0d\u5bf9\u7684\u3002
\\[ \\begin{aligned} & 1 + (-2) \\newline & \\rightarrow 0000 \\; 0001 + 1000 \\; 0010 \\newline & = 1000 \\; 0011 \\newline & \\rightarrow -3 \\end{aligned} \\]\u4e3a\u4e86\u89e3\u51b3\u6b64\u95ee\u9898\uff0c\u8ba1\u7b97\u673a\u5f15\u5165\u4e86\u300c\u53cd\u7801 1's complement\u300d\u3002\u5982\u679c\u6211\u4eec\u5148\u5c06\u539f\u7801\u8f6c\u6362\u4e3a\u53cd\u7801\uff0c\u5e76\u5728\u53cd\u7801\u4e0b\u8ba1\u7b97 \\(1 + (-2)\\) \uff0c\u6700\u540e\u5c06\u7ed3\u679c\u4ece\u53cd\u7801\u8f6c\u6362\u56de\u539f\u7801\uff0c\u5219\u53ef\u5f97\u5230\u6b63\u786e\u7ed3\u679c \\(-1\\) \u3002
\\[ \\begin{aligned} & 1 + (-2) \\newline & \\rightarrow 0000 \\; 0001 \\; \\text{(\u539f\u7801)} + 1000 \\; 0010 \\; \\text{(\u539f\u7801)} \\newline & = 0000 \\; 0001 \\; \\text{(\u53cd\u7801)} + 1111 \\; 1101 \\; \\text{(\u53cd\u7801)} \\newline & = 1111 \\; 1110 \\; \\text{(\u53cd\u7801)} \\newline & = 1000 \\; 0001 \\; \\text{(\u539f\u7801)} \\newline & \\rightarrow -1 \\end{aligned} \\]\u53e6\u4e00\u65b9\u9762\uff0c\u6570\u5b57\u96f6\u7684\u539f\u7801\u6709 \\(+0\\) \u548c \\(-0\\) \u4e24\u79cd\u8868\u793a\u65b9\u5f0f\u3002\u8fd9\u610f\u5473\u7740\u6570\u5b57\u96f6\u5bf9\u5e94\u4e24\u4e2a\u4e0d\u540c\u7684\u4e8c\u8fdb\u5236\u7f16\u7801\uff0c\u8fd9\u53ef\u80fd\u4f1a\u5e26\u6765\u6b67\u4e49\u3002\u6bd4\u5982\u5728\u6761\u4ef6\u5224\u65ad\u4e2d\uff0c\u5982\u679c\u6ca1\u6709\u533a\u5206\u6b63\u96f6\u548c\u8d1f\u96f6\uff0c\u5219\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5224\u65ad\u7ed3\u679c\u51fa\u9519\u3002\u800c\u5982\u679c\u6211\u4eec\u60f3\u5904\u7406\u6b63\u96f6\u548c\u8d1f\u96f6\u6b67\u4e49\uff0c\u5219\u9700\u8981\u5f15\u5165\u989d\u5916\u7684\u5224\u65ad\u64cd\u4f5c\uff0c\u8fd9\u53ef\u80fd\u4f1a\u964d\u4f4e\u8ba1\u7b97\u673a\u7684\u8fd0\u7b97\u6548\u7387\u3002
\\[ \\begin{aligned} +0 & \\rightarrow 0000 \\; 0000 \\newline -0 & \\rightarrow 1000 \\; 0000 \\end{aligned} \\]\u4e0e\u539f\u7801\u4e00\u6837\uff0c\u53cd\u7801\u4e5f\u5b58\u5728\u6b63\u8d1f\u96f6\u6b67\u4e49\u95ee\u9898\uff0c\u56e0\u6b64\u8ba1\u7b97\u673a\u8fdb\u4e00\u6b65\u5f15\u5165\u4e86\u300c\u8865\u7801 2's complement\u300d\u3002\u6211\u4eec\u5148\u6765\u89c2\u5bdf\u4e00\u4e0b\u8d1f\u96f6\u7684\u539f\u7801\u3001\u53cd\u7801\u3001\u8865\u7801\u7684\u8f6c\u6362\u8fc7\u7a0b\uff1a
\\[ \\begin{aligned} -0 \\rightarrow \\; & 1000 \\; 0000 \\; \\text{(\u539f\u7801)} \\newline = \\; & 1111 \\; 1111 \\; \\text{(\u53cd\u7801)} \\newline = 1 \\; & 0000 \\; 0000 \\; \\text{(\u8865\u7801)} \\newline \\end{aligned} \\]\u5728\u8d1f\u96f6\u7684\u53cd\u7801\u57fa\u7840\u4e0a\u52a0 \\(1\\) \u4f1a\u4ea7\u751f\u8fdb\u4f4d\uff0c\u4f46 byte
\u7c7b\u578b\u7684\u957f\u5ea6\u53ea\u6709 8 \u4f4d\uff0c\u56e0\u6b64\u6ea2\u51fa\u5230\u7b2c 9 \u4f4d\u7684 \\(1\\) \u4f1a\u88ab\u820d\u5f03\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u8d1f\u96f6\u7684\u8865\u7801\u4e3a \\(0000 \\; 0000\\) \uff0c\u4e0e\u6b63\u96f6\u7684\u8865\u7801\u76f8\u540c\u3002\u8fd9\u610f\u5473\u7740\u5728\u8865\u7801\u8868\u793a\u4e2d\u53ea\u5b58\u5728\u4e00\u4e2a\u96f6\uff0c\u6b63\u8d1f\u96f6\u6b67\u4e49\u4ece\u800c\u5f97\u5230\u89e3\u51b3\u3002
\u8fd8\u5269\u6700\u540e\u4e00\u4e2a\u7591\u60d1\uff1abyte
\u7c7b\u578b\u7684\u53d6\u503c\u8303\u56f4\u662f \\([-128, 127]\\) \uff0c\u591a\u51fa\u6765\u7684\u4e00\u4e2a\u8d1f\u6570 \\(-128\\) \u662f\u5982\u4f55\u5f97\u5230\u7684\u5462\uff1f\u6211\u4eec\u6ce8\u610f\u5230\uff0c\u533a\u95f4 \\([-127, +127]\\) \u5185\u7684\u6240\u6709\u6574\u6570\u90fd\u6709\u5bf9\u5e94\u7684\u539f\u7801\u3001\u53cd\u7801\u548c\u8865\u7801\uff0c\u5e76\u4e14\u539f\u7801\u548c\u8865\u7801\u4e4b\u95f4\u53ef\u4ee5\u4e92\u76f8\u8f6c\u6362\u3002
\u7136\u800c\uff0c\u8865\u7801 \\(1000 \\; 0000\\) \u662f\u4e00\u4e2a\u4f8b\u5916\uff0c\u5b83\u5e76\u6ca1\u6709\u5bf9\u5e94\u7684\u539f\u7801\u3002\u6839\u636e\u8f6c\u6362\u65b9\u6cd5\uff0c\u6211\u4eec\u5f97\u5230\u8be5\u8865\u7801\u7684\u539f\u7801\u4e3a \\(0000 \\; 0000\\) \u3002\u8fd9\u663e\u7136\u662f\u77db\u76fe\u7684\uff0c\u56e0\u4e3a\u8be5\u539f\u7801\u8868\u793a\u6570\u5b57 \\(0\\) \uff0c\u5b83\u7684\u8865\u7801\u5e94\u8be5\u662f\u81ea\u8eab\u3002\u8ba1\u7b97\u673a\u89c4\u5b9a\u8fd9\u4e2a\u7279\u6b8a\u7684\u8865\u7801 \\(1000 \\; 0000\\) \u4ee3\u8868 \\(-128\\) \u3002\u5b9e\u9645\u4e0a\uff0c\\((-1) + (-127)\\) \u5728\u8865\u7801\u4e0b\u7684\u8ba1\u7b97\u7ed3\u679c\u5c31\u662f \\(-128\\) \u3002
\\[ \\begin{aligned} & (-127) + (-1) \\newline & \\rightarrow 1111 \\; 1111 \\; \\text{(\u539f\u7801)} + 1000 \\; 0001 \\; \\text{(\u539f\u7801)} \\newline & = 1000 \\; 0000 \\; \\text{(\u53cd\u7801)} + 1111 \\; 1110 \\; \\text{(\u53cd\u7801)} \\newline & = 1000 \\; 0001 \\; \\text{(\u8865\u7801)} + 1111 \\; 1111 \\; \\text{(\u8865\u7801)} \\newline & = 1000 \\; 0000 \\; \\text{(\u8865\u7801)} \\newline & \\rightarrow -128 \\end{aligned} \\]\u4f60\u53ef\u80fd\u5df2\u7ecf\u53d1\u73b0\u4e86\uff0c\u4e0a\u8ff0\u6240\u6709\u8ba1\u7b97\u90fd\u662f\u52a0\u6cd5\u8fd0\u7b97\u3002\u8fd9\u6697\u793a\u7740\u4e00\u4e2a\u91cd\u8981\u4e8b\u5b9e\uff1a\u8ba1\u7b97\u673a\u5185\u90e8\u7684\u786c\u4ef6\u7535\u8def\u4e3b\u8981\u662f\u57fa\u4e8e\u52a0\u6cd5\u8fd0\u7b97\u8bbe\u8ba1\u7684\u3002\u8fd9\u662f\u56e0\u4e3a\u52a0\u6cd5\u8fd0\u7b97\u76f8\u5bf9\u4e8e\u5176\u4ed6\u8fd0\u7b97\uff08\u6bd4\u5982\u4e58\u6cd5\u3001\u9664\u6cd5\u548c\u51cf\u6cd5\uff09\u6765\u8bf4\uff0c\u786c\u4ef6\u5b9e\u73b0\u8d77\u6765\u66f4\u7b80\u5355\uff0c\u66f4\u5bb9\u6613\u8fdb\u884c\u5e76\u884c\u5316\u5904\u7406\uff0c\u8fd0\u7b97\u901f\u5ea6\u66f4\u5feb\u3002
\u8bf7\u6ce8\u610f\uff0c\u8fd9\u5e76\u4e0d\u610f\u5473\u7740\u8ba1\u7b97\u673a\u53ea\u80fd\u505a\u52a0\u6cd5\u3002\u901a\u8fc7\u5c06\u52a0\u6cd5\u4e0e\u4e00\u4e9b\u57fa\u672c\u903b\u8f91\u8fd0\u7b97\u7ed3\u5408\uff0c\u8ba1\u7b97\u673a\u80fd\u591f\u5b9e\u73b0\u5404\u79cd\u5176\u4ed6\u7684\u6570\u5b66\u8fd0\u7b97\u3002\u4f8b\u5982\uff0c\u8ba1\u7b97\u51cf\u6cd5 \\(a - b\\) \u53ef\u4ee5\u8f6c\u6362\u4e3a\u8ba1\u7b97\u52a0\u6cd5 \\(a + (-b)\\) \uff1b\u8ba1\u7b97\u4e58\u6cd5\u548c\u9664\u6cd5\u53ef\u4ee5\u8f6c\u6362\u4e3a\u8ba1\u7b97\u591a\u6b21\u52a0\u6cd5\u6216\u51cf\u6cd5\u3002
\u73b0\u5728\u6211\u4eec\u53ef\u4ee5\u603b\u7ed3\u51fa\u8ba1\u7b97\u673a\u4f7f\u7528\u8865\u7801\u7684\u539f\u56e0\uff1a\u57fa\u4e8e\u8865\u7801\u8868\u793a\uff0c\u8ba1\u7b97\u673a\u53ef\u4ee5\u7528\u540c\u6837\u7684\u7535\u8def\u548c\u64cd\u4f5c\u6765\u5904\u7406\u6b63\u6570\u548c\u8d1f\u6570\u7684\u52a0\u6cd5\uff0c\u4e0d\u9700\u8981\u8bbe\u8ba1\u7279\u6b8a\u7684\u786c\u4ef6\u7535\u8def\u6765\u5904\u7406\u51cf\u6cd5\uff0c\u5e76\u4e14\u65e0\u987b\u7279\u522b\u5904\u7406\u6b63\u8d1f\u96f6\u7684\u6b67\u4e49\u95ee\u9898\u3002\u8fd9\u5927\u5927\u7b80\u5316\u4e86\u786c\u4ef6\u8bbe\u8ba1\uff0c\u63d0\u9ad8\u4e86\u8fd0\u7b97\u6548\u7387\u3002
\u8865\u7801\u7684\u8bbe\u8ba1\u975e\u5e38\u7cbe\u5999\uff0c\u56e0\u7bc7\u5e45\u5173\u7cfb\u6211\u4eec\u5c31\u5148\u4ecb\u7ecd\u5230\u8fd9\u91cc\uff0c\u5efa\u8bae\u6709\u5174\u8da3\u7684\u8bfb\u8005\u8fdb\u4e00\u6b65\u6df1\u5165\u4e86\u89e3\u3002
"},{"location":"chapter_data_structure/number_encoding/#332","title":"3.3.2 \u00a0 \u6d6e\u70b9\u6570\u7f16\u7801","text":"\u7ec6\u5fc3\u7684\u4f60\u53ef\u80fd\u4f1a\u53d1\u73b0\uff1aint
\u548c float
\u957f\u5ea6\u76f8\u540c\uff0c\u90fd\u662f 4 bytes \uff0c\u4f46\u4e3a\u4ec0\u4e48 float
\u7684\u53d6\u503c\u8303\u56f4\u8fdc\u5927\u4e8e int
\uff1f\u8fd9\u975e\u5e38\u53cd\u76f4\u89c9\uff0c\u56e0\u4e3a\u6309\u7406\u8bf4 float
\u9700\u8981\u8868\u793a\u5c0f\u6570\uff0c\u53d6\u503c\u8303\u56f4\u5e94\u8be5\u53d8\u5c0f\u624d\u5bf9\u3002
\u5b9e\u9645\u4e0a\uff0c\u8fd9\u662f\u56e0\u4e3a\u6d6e\u70b9\u6570 float
\u91c7\u7528\u4e86\u4e0d\u540c\u7684\u8868\u793a\u65b9\u5f0f\u3002\u8bb0\u4e00\u4e2a 32-bit \u957f\u5ea6\u7684\u4e8c\u8fdb\u5236\u6570\u4e3a\uff1a
\u6839\u636e IEEE 754 \u6807\u51c6\uff0c32-bit \u957f\u5ea6\u7684 float
\u7531\u4ee5\u4e0b\u4e09\u4e2a\u90e8\u5206\u6784\u6210\u3002
\u4e8c\u8fdb\u5236\u6570 float
\u5bf9\u5e94\u503c\u7684\u8ba1\u7b97\u65b9\u6cd5\u4e3a\uff1a
\u8f6c\u5316\u5230\u5341\u8fdb\u5236\u4e0b\u7684\u8ba1\u7b97\u516c\u5f0f\u4e3a\uff1a
\\[ \\text {val}=(-1)^{\\mathrm{S}} \\times 2^{\\mathrm{E} -127} \\times (1 + \\mathrm{N}) \\]\u5176\u4e2d\u5404\u9879\u7684\u53d6\u503c\u8303\u56f4\u4e3a\uff1a
\\[ \\begin{aligned} \\mathrm{S} \\in & \\{ 0, 1\\}, \\quad \\mathrm{E} \\in \\{ 1, 2, \\dots, 254 \\} \\newline (1 + \\mathrm{N}) = & (1 + \\sum_{i=1}^{23} b_{23-i} 2^{-i}) \\subset [1, 2 - 2^{-23}] \\end{aligned} \\]\u56fe 3-5 \u00a0 IEEE 754 \u6807\u51c6\u4e0b\u7684 float \u7684\u8ba1\u7b97\u793a\u4f8b
\u89c2\u5bdf\u56fe 3-5 \uff0c\u7ed9\u5b9a\u4e00\u4e2a\u793a\u4f8b\u6570\u636e \\(\\mathrm{S} = 0\\) \uff0c \\(\\mathrm{E} = 124\\) \uff0c\\(\\mathrm{N} = 2^{-2} + 2^{-3} = 0.375\\) \uff0c\u5219\u6709\uff1a
\\[ \\text { val } = (-1)^0 \\times 2^{124 - 127} \\times (1 + 0.375) = 0.171875 \\]\u73b0\u5728\u6211\u4eec\u53ef\u4ee5\u56de\u7b54\u6700\u521d\u7684\u95ee\u9898\uff1afloat
\u7684\u8868\u793a\u65b9\u5f0f\u5305\u542b\u6307\u6570\u4f4d\uff0c\u5bfc\u81f4\u5176\u53d6\u503c\u8303\u56f4\u8fdc\u5927\u4e8e int
\u3002\u6839\u636e\u4ee5\u4e0a\u8ba1\u7b97\uff0cfloat
\u53ef\u8868\u793a\u7684\u6700\u5927\u6b63\u6570\u4e3a \\(2^{254 - 127} \\times (2 - 2^{-23}) \\approx 3.4 \\times 10^{38}\\) \uff0c\u5207\u6362\u7b26\u53f7\u4f4d\u4fbf\u53ef\u5f97\u5230\u6700\u5c0f\u8d1f\u6570\u3002
\u5c3d\u7ba1\u6d6e\u70b9\u6570 float
\u6269\u5c55\u4e86\u53d6\u503c\u8303\u56f4\uff0c\u4f46\u5176\u526f\u4f5c\u7528\u662f\u727a\u7272\u4e86\u7cbe\u5ea6\u3002\u6574\u6570\u7c7b\u578b int
\u5c06\u5168\u90e8 32 \u4f4d\u7528\u4e8e\u8868\u793a\u6570\u5b57\uff0c\u6570\u5b57\u662f\u5747\u5300\u5206\u5e03\u7684\uff1b\u800c\u7531\u4e8e\u6307\u6570\u4f4d\u7684\u5b58\u5728\uff0c\u6d6e\u70b9\u6570 float
\u7684\u6570\u503c\u8d8a\u5927\uff0c\u76f8\u90bb\u4e24\u4e2a\u6570\u5b57\u4e4b\u95f4\u7684\u5dee\u503c\u5c31\u4f1a\u8d8b\u5411\u8d8a\u5927\u3002
\u5982\u8868 3-2 \u6240\u793a\uff0c\u6307\u6570\u4f4d \\(E = 0\\) \u548c \\(E = 255\\) \u5177\u6709\u7279\u6b8a\u542b\u4e49\uff0c\u7528\u4e8e\u8868\u793a\u96f6\u3001\u65e0\u7a77\u5927\u3001\\(\\mathrm{NaN}\\) \u7b49\u3002
\u8868 3-2 \u00a0 \u6307\u6570\u4f4d\u542b\u4e49
\u6307\u6570\u4f4d E \u5206\u6570\u4f4d \\(\\mathrm{N} = 0\\) \u5206\u6570\u4f4d \\(\\mathrm{N} \\ne 0\\) \u8ba1\u7b97\u516c\u5f0f \\(0\\) \\(\\pm 0\\) \u6b21\u6b63\u89c4\u6570 \\((-1)^{\\mathrm{S}} \\times 2^{-126} \\times (0.\\mathrm{N})\\) \\(1, 2, \\dots, 254\\) \u6b63\u89c4\u6570 \u6b63\u89c4\u6570 \\((-1)^{\\mathrm{S}} \\times 2^{(\\mathrm{E} -127)} \\times (1.\\mathrm{N})\\) \\(255\\) \\(\\pm \\infty\\) \\(\\mathrm{NaN}\\)\u503c\u5f97\u8bf4\u660e\u7684\u662f\uff0c\u6b21\u6b63\u89c4\u6570\u663e\u8457\u63d0\u5347\u4e86\u6d6e\u70b9\u6570\u7684\u7cbe\u5ea6\u3002\u6700\u5c0f\u6b63\u6b63\u89c4\u6570\u4e3a \\(2^{-126}\\) \uff0c\u6700\u5c0f\u6b63\u6b21\u6b63\u89c4\u6570\u4e3a \\(2^{-126} \\times 2^{-23}\\) \u3002
\u53cc\u7cbe\u5ea6 double
\u4e5f\u91c7\u7528\u7c7b\u4f3c\u4e8e float
\u7684\u8868\u793a\u65b9\u6cd5\uff0c\u5728\u6b64\u4e0d\u505a\u8d58\u8ff0\u3002
byte
\u3001short
\u3001int
\u3001long
\uff0c\u6d6e\u70b9\u6570 float
\u3001double
\uff0c\u5b57\u7b26 char
\u548c\u5e03\u5c14 boolean
\u3002\u5b83\u4eec\u7684\u53d6\u503c\u8303\u56f4\u53d6\u51b3\u4e8e\u5360\u7528\u7a7a\u95f4\u5927\u5c0f\u548c\u8868\u793a\u65b9\u5f0f\u3002\u4e3a\u4ec0\u4e48\u54c8\u5e0c\u8868\u540c\u65f6\u5305\u542b\u7ebf\u6027\u6570\u636e\u7ed3\u6784\u548c\u975e\u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff1f
\u54c8\u5e0c\u8868\u5e95\u5c42\u662f\u6570\u7ec4\uff0c\u800c\u4e3a\u4e86\u89e3\u51b3\u54c8\u5e0c\u51b2\u7a81\uff0c\u6211\u4eec\u53ef\u80fd\u4f1a\u4f7f\u7528\u201c\u94fe\u5f0f\u5730\u5740\u201d\uff08\u540e\u7eed\u201c\u54c8\u5e0c\u51b2\u7a81\u201d\u7ae0\u8282\u4f1a\u8bb2\uff09\uff1a\u6570\u7ec4\u4e2d\u6bcf\u4e2a\u6876\u6307\u5411\u4e00\u4e2a\u94fe\u8868\uff0c\u5f53\u94fe\u8868\u957f\u5ea6\u8d85\u8fc7\u4e00\u5b9a\u9608\u503c\u65f6\uff0c\u53c8\u53ef\u80fd\u88ab\u8f6c\u5316\u4e3a\u6811\uff08\u901a\u5e38\u4e3a\u7ea2\u9ed1\u6811\uff09\u3002 \u4ece\u5b58\u50a8\u7684\u89d2\u5ea6\u6765\u770b\uff0c\u54c8\u5e0c\u8868\u7684\u5e95\u5c42\u662f\u6570\u7ec4\uff0c\u5176\u4e2d\u6bcf\u4e00\u4e2a\u6876\u69fd\u4f4d\u53ef\u80fd\u5305\u542b\u4e00\u4e2a\u503c\uff0c\u4e5f\u53ef\u80fd\u5305\u542b\u4e00\u4e2a\u94fe\u8868\u6216\u4e00\u68f5\u6811\u3002\u56e0\u6b64\uff0c\u54c8\u5e0c\u8868\u53ef\u80fd\u540c\u65f6\u5305\u542b\u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff08\u6570\u7ec4\u3001\u94fe\u8868\uff09\u548c\u975e\u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff08\u6811\uff09\u3002
char
\u7c7b\u578b\u7684\u957f\u5ea6\u662f 1 byte \u5417\uff1f
char
\u7c7b\u578b\u7684\u957f\u5ea6\u7531\u7f16\u7a0b\u8bed\u8a00\u91c7\u7528\u7684\u7f16\u7801\u65b9\u6cd5\u51b3\u5b9a\u3002\u4f8b\u5982\uff0cJava\u3001JavaScript\u3001TypeScript\u3001C# \u90fd\u91c7\u7528 UTF-16 \u7f16\u7801\uff08\u4fdd\u5b58 Unicode \u7801\u70b9\uff09\uff0c\u56e0\u6b64 char \u7c7b\u578b\u7684\u957f\u5ea6\u4e3a 2 bytes\u3002
\u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6570\u636e\u7ed3\u6784\u4e5f\u79f0\u201c\u9759\u6001\u6570\u636e\u7ed3\u6784\u201d \u662f\u5426\u6709\u6b67\u4e49\uff1f\u56e0\u4e3a\u6808\u4e5f\u53ef\u4ee5\u8fdb\u884c\u51fa\u6808\u548c\u5165\u6808\u7b49\u64cd\u4f5c\uff0c\u8fd9\u4e9b\u64cd\u4f5c\u90fd\u662f\u201c\u52a8\u6001\u201d\u7684\u3002
\u6808\u786e\u5b9e\u53ef\u4ee5\u5b9e\u73b0\u52a8\u6001\u7684\u6570\u636e\u64cd\u4f5c\uff0c\u4f46\u6570\u636e\u7ed3\u6784\u4ecd\u7136\u662f\u201c\u9759\u6001\u201d\uff08\u957f\u5ea6\u4e0d\u53ef\u53d8\uff09\u7684\u3002\u5c3d\u7ba1\u57fa\u4e8e\u6570\u7ec4\u7684\u6570\u636e\u7ed3\u6784\u53ef\u4ee5\u52a8\u6001\u5730\u6dfb\u52a0\u6216\u5220\u9664\u5143\u7d20\uff0c\u4f46\u5b83\u4eec\u7684\u5bb9\u91cf\u662f\u56fa\u5b9a\u7684\u3002\u5982\u679c\u6570\u636e\u91cf\u8d85\u51fa\u4e86\u9884\u5206\u914d\u7684\u5927\u5c0f\uff0c\u5c31\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u66f4\u5927\u7684\u6570\u7ec4\uff0c\u5e76\u5c06\u65e7\u6570\u7ec4\u7684\u5185\u5bb9\u590d\u5236\u5230\u65b0\u6570\u7ec4\u4e2d\u3002
\u5728\u6784\u5efa\u6808\uff08\u961f\u5217\uff09\u7684\u65f6\u5019\uff0c\u672a\u6307\u5b9a\u5b83\u7684\u5927\u5c0f\uff0c\u4e3a\u4ec0\u4e48\u5b83\u4eec\u662f\u201c\u9759\u6001\u6570\u636e\u7ed3\u6784\u201d\u5462\uff1f
\u5728\u9ad8\u7ea7\u7f16\u7a0b\u8bed\u8a00\u4e2d\uff0c\u6211\u4eec\u65e0\u987b\u4eba\u5de5\u6307\u5b9a\u6808\uff08\u961f\u5217\uff09\u7684\u521d\u59cb\u5bb9\u91cf\uff0c\u8fd9\u4e2a\u5de5\u4f5c\u7531\u7c7b\u5185\u90e8\u81ea\u52a8\u5b8c\u6210\u3002\u4f8b\u5982\uff0cJava \u7684 ArrayList \u7684\u521d\u59cb\u5bb9\u91cf\u901a\u5e38\u4e3a 10\u3002\u53e6\u5916\uff0c\u6269\u5bb9\u64cd\u4f5c\u4e5f\u662f\u81ea\u52a8\u5b9e\u73b0\u7684\u3002\u8be6\u89c1\u540e\u7eed\u7684\u201c\u5217\u8868\u201d\u7ae0\u8282\u3002
"},{"location":"chapter_divide_and_conquer/","title":"\u7b2c 12 \u7ae0 \u00a0 \u5206\u6cbb","text":"Abstract
\u96be\u9898\u88ab\u9010\u5c42\u62c6\u89e3\uff0c\u6bcf\u4e00\u6b21\u7684\u62c6\u89e3\u90fd\u4f7f\u5b83\u53d8\u5f97\u66f4\u4e3a\u7b80\u5355\u3002
\u5206\u800c\u6cbb\u4e4b\u63ed\u793a\u4e86\u4e00\u4e2a\u91cd\u8981\u7684\u4e8b\u5b9e\uff1a\u4ece\u7b80\u5355\u505a\u8d77\uff0c\u4e00\u5207\u90fd\u4e0d\u518d\u590d\u6742\u3002
"},{"location":"chapter_divide_and_conquer/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"\u6211\u4eec\u5df2\u7ecf\u5b66\u8fc7\uff0c\u641c\u7d22\u7b97\u6cd5\u5206\u4e3a\u4e24\u5927\u7c7b\u3002
\u5b9e\u9645\u4e0a\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(\\log n)\\) \u7684\u641c\u7d22\u7b97\u6cd5\u901a\u5e38\u662f\u57fa\u4e8e\u5206\u6cbb\u7b56\u7565\u5b9e\u73b0\u7684\uff0c\u4f8b\u5982\u4e8c\u5206\u67e5\u627e\u548c\u6811\u3002
\u4e8c\u5206\u67e5\u627e\u7684\u5206\u6cbb\u7b56\u7565\u5982\u4e0b\u6240\u793a\u3002
\u5206\u6cbb\u80fd\u591f\u63d0\u5347\u641c\u7d22\u6548\u7387\uff0c\u672c\u8d28\u4e0a\u662f\u56e0\u4e3a\u66b4\u529b\u641c\u7d22\u6bcf\u8f6e\u53ea\u80fd\u6392\u9664\u4e00\u4e2a\u9009\u9879\uff0c\u800c\u5206\u6cbb\u641c\u7d22\u6bcf\u8f6e\u53ef\u4ee5\u6392\u9664\u4e00\u534a\u9009\u9879\u3002
"},{"location":"chapter_divide_and_conquer/binary_search_recur/#1","title":"1. \u00a0 \u57fa\u4e8e\u5206\u6cbb\u5b9e\u73b0\u4e8c\u5206\u67e5\u627e","text":"\u5728\u4e4b\u524d\u7684\u7ae0\u8282\u4e2d\uff0c\u4e8c\u5206\u67e5\u627e\u662f\u57fa\u4e8e\u9012\u63a8\uff08\u8fed\u4ee3\uff09\u5b9e\u73b0\u7684\u3002\u73b0\u5728\u6211\u4eec\u57fa\u4e8e\u5206\u6cbb\uff08\u9012\u5f52\uff09\u6765\u5b9e\u73b0\u5b83\u3002
Question
\u7ed9\u5b9a\u4e00\u4e2a\u957f\u5ea6\u4e3a \\(n\\) \u7684\u6709\u5e8f\u6570\u7ec4 nums
\uff0c\u5176\u4e2d\u6240\u6709\u5143\u7d20\u90fd\u662f\u552f\u4e00\u7684\uff0c\u8bf7\u67e5\u627e\u5143\u7d20 target
\u3002
\u4ece\u5206\u6cbb\u89d2\u5ea6\uff0c\u6211\u4eec\u5c06\u641c\u7d22\u533a\u95f4 \\([i, j]\\) \u5bf9\u5e94\u7684\u5b50\u95ee\u9898\u8bb0\u4e3a \\(f(i, j)\\) \u3002
\u4ee5\u539f\u95ee\u9898 \\(f(0, n-1)\\) \u4e3a\u8d77\u59cb\u70b9\uff0c\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u8fdb\u884c\u4e8c\u5206\u67e5\u627e\u3002
1.
\u6b65\u548c\u7b2c 2.
\u6b65\uff0c\u76f4\u81f3\u627e\u5230 target
\u6216\u533a\u95f4\u4e3a\u7a7a\u65f6\u8fd4\u56de\u3002\u56fe 12-4 \u5c55\u793a\u4e86\u5728\u6570\u7ec4\u4e2d\u4e8c\u5206\u67e5\u627e\u5143\u7d20 \\(6\\) \u7684\u5206\u6cbb\u8fc7\u7a0b\u3002
\u56fe 12-4 \u00a0 \u4e8c\u5206\u67e5\u627e\u7684\u5206\u6cbb\u8fc7\u7a0b
\u5728\u5b9e\u73b0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u58f0\u660e\u4e00\u4e2a\u9012\u5f52\u51fd\u6570 dfs()
\u6765\u6c42\u89e3\u95ee\u9898 \\(f(i, j)\\) \uff1a
def dfs(nums: list[int], target: int, i: int, j: int) -> int:\n \"\"\"\u4e8c\u5206\u67e5\u627e\uff1a\u95ee\u9898 f(i, j)\"\"\"\n # \u82e5\u533a\u95f4\u4e3a\u7a7a\uff0c\u4ee3\u8868\u65e0\u76ee\u6807\u5143\u7d20\uff0c\u5219\u8fd4\u56de -1\n if i > j:\n return -1\n # \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n m = (i + j) // 2\n if nums[m] < target:\n # \u9012\u5f52\u5b50\u95ee\u9898 f(m+1, j)\n return dfs(nums, target, m + 1, j)\n elif nums[m] > target:\n # \u9012\u5f52\u5b50\u95ee\u9898 f(i, m-1)\n return dfs(nums, target, i, m - 1)\n else:\n # \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m\n\ndef binary_search(nums: list[int], target: int) -> int:\n \"\"\"\u4e8c\u5206\u67e5\u627e\"\"\"\n n = len(nums)\n # \u6c42\u89e3\u95ee\u9898 f(0, n-1)\n return dfs(nums, target, 0, n - 1)\n
binary_search_recur.cpp/* \u4e8c\u5206\u67e5\u627e\uff1a\u95ee\u9898 f(i, j) */\nint dfs(vector<int> &nums, int target, int i, int j) {\n // \u82e5\u533a\u95f4\u4e3a\u7a7a\uff0c\u4ee3\u8868\u65e0\u76ee\u6807\u5143\u7d20\uff0c\u5219\u8fd4\u56de -1\n if (i > j) {\n return -1;\n }\n // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n int m = (i + j) / 2;\n if (nums[m] < target) {\n // \u9012\u5f52\u5b50\u95ee\u9898 f(m+1, j)\n return dfs(nums, target, m + 1, j);\n } else if (nums[m] > target) {\n // \u9012\u5f52\u5b50\u95ee\u9898 f(i, m-1)\n return dfs(nums, target, i, m - 1);\n } else {\n // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m;\n }\n}\n\n/* \u4e8c\u5206\u67e5\u627e */\nint binarySearch(vector<int> &nums, int target) {\n int n = nums.size();\n // \u6c42\u89e3\u95ee\u9898 f(0, n-1)\n return dfs(nums, target, 0, n - 1);\n}\n
binary_search_recur.java/* \u4e8c\u5206\u67e5\u627e\uff1a\u95ee\u9898 f(i, j) */\nint dfs(int[] nums, int target, int i, int j) {\n // \u82e5\u533a\u95f4\u4e3a\u7a7a\uff0c\u4ee3\u8868\u65e0\u76ee\u6807\u5143\u7d20\uff0c\u5219\u8fd4\u56de -1\n if (i > j) {\n return -1;\n }\n // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n int m = (i + j) / 2;\n if (nums[m] < target) {\n // \u9012\u5f52\u5b50\u95ee\u9898 f(m+1, j)\n return dfs(nums, target, m + 1, j);\n } else if (nums[m] > target) {\n // \u9012\u5f52\u5b50\u95ee\u9898 f(i, m-1)\n return dfs(nums, target, i, m - 1);\n } else {\n // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m;\n }\n}\n\n/* \u4e8c\u5206\u67e5\u627e */\nint binarySearch(int[] nums, int target) {\n int n = nums.length;\n // \u6c42\u89e3\u95ee\u9898 f(0, n-1)\n return dfs(nums, target, 0, n - 1);\n}\n
binary_search_recur.cs/* \u4e8c\u5206\u67e5\u627e\uff1a\u95ee\u9898 f(i, j) */\nint DFS(int[] nums, int target, int i, int j) {\n // \u82e5\u533a\u95f4\u4e3a\u7a7a\uff0c\u4ee3\u8868\u65e0\u76ee\u6807\u5143\u7d20\uff0c\u5219\u8fd4\u56de -1\n if (i > j) {\n return -1;\n }\n // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n int m = (i + j) / 2;\n if (nums[m] < target) {\n // \u9012\u5f52\u5b50\u95ee\u9898 f(m+1, j)\n return DFS(nums, target, m + 1, j);\n } else if (nums[m] > target) {\n // \u9012\u5f52\u5b50\u95ee\u9898 f(i, m-1)\n return DFS(nums, target, i, m - 1);\n } else {\n // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m;\n }\n}\n\n/* \u4e8c\u5206\u67e5\u627e */\nint BinarySearch(int[] nums, int target) {\n int n = nums.Length;\n // \u6c42\u89e3\u95ee\u9898 f(0, n-1)\n return DFS(nums, target, 0, n - 1);\n}\n
binary_search_recur.go/* \u4e8c\u5206\u67e5\u627e\uff1a\u95ee\u9898 f(i, j) */\nfunc dfs(nums []int, target, i, j int) int {\n // \u5982\u679c\u533a\u95f4\u4e3a\u7a7a\uff0c\u4ee3\u8868\u6ca1\u6709\u76ee\u6807\u5143\u7d20\uff0c\u5219\u8fd4\u56de -1\n if i > j {\n return -1\n }\n // \u8ba1\u7b97\u7d22\u5f15\u4e2d\u70b9\n m := i + ((j - i) >> 1)\n //\u5224\u65ad\u4e2d\u70b9\u4e0e\u76ee\u6807\u5143\u7d20\u5927\u5c0f\n if nums[m] < target {\n // \u5c0f\u4e8e\u5219\u9012\u5f52\u53f3\u534a\u6570\u7ec4\n // \u9012\u5f52\u5b50\u95ee\u9898 f(m+1, j)\n return dfs(nums, target, m+1, j)\n } else if nums[m] > target {\n // \u5c0f\u4e8e\u5219\u9012\u5f52\u5de6\u534a\u6570\u7ec4\n // \u9012\u5f52\u5b50\u95ee\u9898 f(i, m-1)\n return dfs(nums, target, i, m-1)\n } else {\n // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m\n }\n}\n\n/* \u4e8c\u5206\u67e5\u627e */\nfunc binarySearch(nums []int, target int) int {\n n := len(nums)\n return dfs(nums, target, 0, n-1)\n}\n
binary_search_recur.swift/* \u4e8c\u5206\u67e5\u627e\uff1a\u95ee\u9898 f(i, j) */\nfunc dfs(nums: [Int], target: Int, i: Int, j: Int) -> Int {\n // \u82e5\u533a\u95f4\u4e3a\u7a7a\uff0c\u4ee3\u8868\u65e0\u76ee\u6807\u5143\u7d20\uff0c\u5219\u8fd4\u56de -1\n if i > j {\n return -1\n }\n // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n let m = (i + j) / 2\n if nums[m] < target {\n // \u9012\u5f52\u5b50\u95ee\u9898 f(m+1, j)\n return dfs(nums: nums, target: target, i: m + 1, j: j)\n } else if nums[m] > target {\n // \u9012\u5f52\u5b50\u95ee\u9898 f(i, m-1)\n return dfs(nums: nums, target: target, i: i, j: m - 1)\n } else {\n // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m\n }\n}\n\n/* \u4e8c\u5206\u67e5\u627e */\nfunc binarySearch(nums: [Int], target: Int) -> Int {\n let n = nums.count\n // \u6c42\u89e3\u95ee\u9898 f(0, n-1)\n return dfs(nums: nums, target: target, i: 0, j: n - 1)\n}\n
binary_search_recur.js/* \u4e8c\u5206\u67e5\u627e\uff1a\u95ee\u9898 f(i, j) */\nfunction dfs(nums, target, i, j) {\n // \u82e5\u533a\u95f4\u4e3a\u7a7a\uff0c\u4ee3\u8868\u65e0\u76ee\u6807\u5143\u7d20\uff0c\u5219\u8fd4\u56de -1\n if (i > j) {\n return -1;\n }\n // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n const m = i + ((j - i) >> 1);\n if (nums[m] < target) {\n // \u9012\u5f52\u5b50\u95ee\u9898 f(m+1, j)\n return dfs(nums, target, m + 1, j);\n } else if (nums[m] > target) {\n // \u9012\u5f52\u5b50\u95ee\u9898 f(i, m-1)\n return dfs(nums, target, i, m - 1);\n } else {\n // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m;\n }\n}\n\n/* \u4e8c\u5206\u67e5\u627e */\nfunction binarySearch(nums, target) {\n const n = nums.length;\n // \u6c42\u89e3\u95ee\u9898 f(0, n-1)\n return dfs(nums, target, 0, n - 1);\n}\n
binary_search_recur.ts/* \u4e8c\u5206\u67e5\u627e\uff1a\u95ee\u9898 f(i, j) */\nfunction dfs(nums: number[], target: number, i: number, j: number): number {\n // \u82e5\u533a\u95f4\u4e3a\u7a7a\uff0c\u4ee3\u8868\u65e0\u76ee\u6807\u5143\u7d20\uff0c\u5219\u8fd4\u56de -1\n if (i > j) {\n return -1;\n }\n // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n const m = i + ((j - i) >> 1);\n if (nums[m] < target) {\n // \u9012\u5f52\u5b50\u95ee\u9898 f(m+1, j)\n return dfs(nums, target, m + 1, j);\n } else if (nums[m] > target) {\n // \u9012\u5f52\u5b50\u95ee\u9898 f(i, m-1)\n return dfs(nums, target, i, m - 1);\n } else {\n // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m;\n }\n}\n\n/* \u4e8c\u5206\u67e5\u627e */\nfunction binarySearch(nums: number[], target: number): number {\n const n = nums.length;\n // \u6c42\u89e3\u95ee\u9898 f(0, n-1)\n return dfs(nums, target, 0, n - 1);\n}\n
binary_search_recur.dart/* \u4e8c\u5206\u67e5\u627e\uff1a\u95ee\u9898 f(i, j) */\nint dfs(List<int> nums, int target, int i, int j) {\n // \u82e5\u533a\u95f4\u4e3a\u7a7a\uff0c\u4ee3\u8868\u65e0\u76ee\u6807\u5143\u7d20\uff0c\u5219\u8fd4\u56de -1\n if (i > j) {\n return -1;\n }\n // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n int m = (i + j) ~/ 2;\n if (nums[m] < target) {\n // \u9012\u5f52\u5b50\u95ee\u9898 f(m+1, j)\n return dfs(nums, target, m + 1, j);\n } else if (nums[m] > target) {\n // \u9012\u5f52\u5b50\u95ee\u9898 f(i, m-1)\n return dfs(nums, target, i, m - 1);\n } else {\n // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m;\n }\n}\n\n/* \u4e8c\u5206\u67e5\u627e */\nint binarySearch(List<int> nums, int target) {\n int n = nums.length;\n // \u6c42\u89e3\u95ee\u9898 f(0, n-1)\n return dfs(nums, target, 0, n - 1);\n}\n
binary_search_recur.rs/* \u4e8c\u5206\u67e5\u627e\uff1a\u95ee\u9898 f(i, j) */\nfn dfs(nums: &[i32], target: i32, i: i32, j: i32) -> i32 {\n // \u82e5\u533a\u95f4\u4e3a\u7a7a\uff0c\u4ee3\u8868\u65e0\u76ee\u6807\u5143\u7d20\uff0c\u5219\u8fd4\u56de -1\n if i > j { return -1; }\n let m: i32 = (i + j) / 2;\n if nums[m as usize] < target {\n // \u9012\u5f52\u5b50\u95ee\u9898 f(m+1, j)\n return dfs(nums, target, m + 1, j);\n } else if nums[m as usize] > target {\n // \u9012\u5f52\u5b50\u95ee\u9898 f(i, m-1)\n return dfs(nums, target, i, m - 1);\n } else {\n // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m;\n }\n}\n\n/* \u4e8c\u5206\u67e5\u627e */\nfn binary_search(nums: &[i32], target: i32) -> i32 {\n let n = nums.len() as i32;\n // \u6c42\u89e3\u95ee\u9898 f(0, n-1)\n dfs(nums, target, 0, n - 1)\n}\n
binary_search_recur.c/* \u4e8c\u5206\u67e5\u627e\uff1a\u95ee\u9898 f(i, j) */\nint dfs(int nums[], int target, int i, int j) {\n // \u82e5\u533a\u95f4\u4e3a\u7a7a\uff0c\u4ee3\u8868\u65e0\u76ee\u6807\u5143\u7d20\uff0c\u5219\u8fd4\u56de -1\n if (i > j) {\n return -1;\n }\n // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n int m = (i + j) / 2;\n if (nums[m] < target) {\n // \u9012\u5f52\u5b50\u95ee\u9898 f(m+1, j)\n return dfs(nums, target, m + 1, j);\n } else if (nums[m] > target) {\n // \u9012\u5f52\u5b50\u95ee\u9898 f(i, m-1)\n return dfs(nums, target, i, m - 1);\n } else {\n // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m;\n }\n}\n\n/* \u4e8c\u5206\u67e5\u627e */\nint binarySearch(int nums[], int target, int numsSize) {\n int n = numsSize;\n // \u6c42\u89e3\u95ee\u9898 f(0, n-1)\n return dfs(nums, target, 0, n - 1);\n}\n
binary_search_recur.zig[class]{}-[func]{dfs}\n\n[class]{}-[func]{binarySearch}\n
"},{"location":"chapter_divide_and_conquer/build_binary_tree_problem/","title":"12.3 \u00a0 \u6784\u5efa\u4e8c\u53c9\u6811\u95ee\u9898","text":"Question
\u7ed9\u5b9a\u4e00\u68f5\u4e8c\u53c9\u6811\u7684\u524d\u5e8f\u904d\u5386 preorder
\u548c\u4e2d\u5e8f\u904d\u5386 inorder
\uff0c\u8bf7\u4ece\u4e2d\u6784\u5efa\u4e8c\u53c9\u6811\uff0c\u8fd4\u56de\u4e8c\u53c9\u6811\u7684\u6839\u8282\u70b9\u3002\u5047\u8bbe\u4e8c\u53c9\u6811\u4e2d\u6ca1\u6709\u503c\u91cd\u590d\u7684\u8282\u70b9\u3002
\u56fe 12-5 \u00a0 \u6784\u5efa\u4e8c\u53c9\u6811\u7684\u793a\u4f8b\u6570\u636e
"},{"location":"chapter_divide_and_conquer/build_binary_tree_problem/#1","title":"1. \u00a0 \u5224\u65ad\u662f\u5426\u4e3a\u5206\u6cbb\u95ee\u9898","text":"\u539f\u95ee\u9898\u5b9a\u4e49\u4e3a\u4ece preorder
\u548c inorder
\u6784\u5efa\u4e8c\u53c9\u6811\uff0c\u662f\u4e00\u4e2a\u5178\u578b\u7684\u5206\u6cbb\u95ee\u9898\u3002
\u6839\u636e\u4ee5\u4e0a\u5206\u6790\uff0c\u8fd9\u9053\u9898\u53ef\u4ee5\u4f7f\u7528\u5206\u6cbb\u6765\u6c42\u89e3\uff0c\u4f46\u5982\u4f55\u901a\u8fc7\u524d\u5e8f\u904d\u5386 preorder
\u548c\u4e2d\u5e8f\u904d\u5386 inorder
\u6765\u5212\u5206\u5de6\u5b50\u6811\u548c\u53f3\u5b50\u6811\u5462\uff1f
\u6839\u636e\u5b9a\u4e49\uff0cpreorder
\u548c inorder
\u90fd\u53ef\u4ee5\u5212\u5206\u4e3a\u4e09\u4e2a\u90e8\u5206\u3002
[ \u6839\u8282\u70b9 | \u5de6\u5b50\u6811 | \u53f3\u5b50\u6811 ]
\uff0c\u4f8b\u5982\u56fe 12-5 \u7684\u6811\u5bf9\u5e94 [ 3 | 9 | 2 1 7 ]
\u3002[ \u5de6\u5b50\u6811 | \u6839\u8282\u70b9 \uff5c \u53f3\u5b50\u6811 ]
\uff0c\u4f8b\u5982\u56fe 12-5 \u7684\u6811\u5bf9\u5e94 [ 9 | 3 | 1 2 7 ]
\u3002\u4ee5\u4e0a\u56fe\u6570\u636e\u4e3a\u4f8b\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u56fe 12-6 \u6240\u793a\u7684\u6b65\u9aa4\u5f97\u5230\u5212\u5206\u7ed3\u679c\u3002
inorder
\u4e2d\u7684\u7d22\u5f15\uff0c\u5229\u7528\u8be5\u7d22\u5f15\u53ef\u5c06 inorder
\u5212\u5206\u4e3a [ 9 | 3 \uff5c 1 2 7 ]
\u3002inorder
\u5212\u5206\u7ed3\u679c\uff0c\u6613\u5f97\u5de6\u5b50\u6811\u548c\u53f3\u5b50\u6811\u7684\u8282\u70b9\u6570\u91cf\u5206\u522b\u4e3a 1 \u548c 3 \uff0c\u4ece\u800c\u53ef\u5c06 preorder
\u5212\u5206\u4e3a [ 3 | 9 | 2 1 7 ]
\u3002\u56fe 12-6 \u00a0 \u5728\u524d\u5e8f\u904d\u5386\u548c\u4e2d\u5e8f\u904d\u5386\u4e2d\u5212\u5206\u5b50\u6811
"},{"location":"chapter_divide_and_conquer/build_binary_tree_problem/#3","title":"3. \u00a0 \u57fa\u4e8e\u53d8\u91cf\u63cf\u8ff0\u5b50\u6811\u533a\u95f4","text":"\u6839\u636e\u4ee5\u4e0a\u5212\u5206\u65b9\u6cd5\uff0c\u6211\u4eec\u5df2\u7ecf\u5f97\u5230\u6839\u8282\u70b9\u3001\u5de6\u5b50\u6811\u3001\u53f3\u5b50\u6811\u5728 preorder
\u548c inorder
\u4e2d\u7684\u7d22\u5f15\u533a\u95f4\u3002\u800c\u4e3a\u4e86\u63cf\u8ff0\u8fd9\u4e9b\u7d22\u5f15\u533a\u95f4\uff0c\u6211\u4eec\u9700\u8981\u501f\u52a9\u51e0\u4e2a\u6307\u9488\u53d8\u91cf\u3002
preorder
\u4e2d\u7684\u7d22\u5f15\u8bb0\u4e3a \\(i\\) \u3002inorder
\u4e2d\u7684\u7d22\u5f15\u8bb0\u4e3a \\(m\\) \u3002inorder
\u4e2d\u7684\u7d22\u5f15\u533a\u95f4\u8bb0\u4e3a \\([l, r]\\) \u3002\u5982\u8868 12-1 \u6240\u793a\uff0c\u901a\u8fc7\u4ee5\u4e0a\u53d8\u91cf\u5373\u53ef\u8868\u793a\u6839\u8282\u70b9\u5728 preorder
\u4e2d\u7684\u7d22\u5f15\uff0c\u4ee5\u53ca\u5b50\u6811\u5728 inorder
\u4e2d\u7684\u7d22\u5f15\u533a\u95f4\u3002
\u8868 12-1 \u00a0 \u6839\u8282\u70b9\u548c\u5b50\u6811\u5728\u524d\u5e8f\u904d\u5386\u548c\u4e2d\u5e8f\u904d\u5386\u4e2d\u7684\u7d22\u5f15
\u6839\u8282\u70b9\u5728preorder
\u4e2d\u7684\u7d22\u5f15 \u5b50\u6811\u5728 inorder
\u4e2d\u7684\u7d22\u5f15\u533a\u95f4 \u5f53\u524d\u6811 \\(i\\) \\([l, r]\\) \u5de6\u5b50\u6811 \\(i + 1\\) \\([l, m-1]\\) \u53f3\u5b50\u6811 \\(i + 1 + (m - l)\\) \\([m+1, r]\\) \u8bf7\u6ce8\u610f\uff0c\u53f3\u5b50\u6811\u6839\u8282\u70b9\u7d22\u5f15\u4e2d\u7684 \\((m-l)\\) \u7684\u542b\u4e49\u662f\u201c\u5de6\u5b50\u6811\u7684\u8282\u70b9\u6570\u91cf\u201d\uff0c\u5efa\u8bae\u914d\u5408\u56fe 12-7 \u7406\u89e3\u3002
\u56fe 12-7 \u00a0 \u6839\u8282\u70b9\u548c\u5de6\u53f3\u5b50\u6811\u7684\u7d22\u5f15\u533a\u95f4\u8868\u793a
"},{"location":"chapter_divide_and_conquer/build_binary_tree_problem/#4","title":"4. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"\u4e3a\u4e86\u63d0\u5347\u67e5\u8be2 \\(m\\) \u7684\u6548\u7387\uff0c\u6211\u4eec\u501f\u52a9\u4e00\u4e2a\u54c8\u5e0c\u8868 hmap
\u6765\u5b58\u50a8\u6570\u7ec4 inorder
\u4e2d\u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\uff1a
def dfs(\n preorder: list[int],\n inorder_map: dict[int, int],\n i: int,\n l: int,\n r: int,\n) -> TreeNode | None:\n \"\"\"\u6784\u5efa\u4e8c\u53c9\u6811\uff1a\u5206\u6cbb\"\"\"\n # \u5b50\u6811\u533a\u95f4\u4e3a\u7a7a\u65f6\u7ec8\u6b62\n if r - l < 0:\n return None\n # \u521d\u59cb\u5316\u6839\u8282\u70b9\n root = TreeNode(preorder[i])\n # \u67e5\u8be2 m \uff0c\u4ece\u800c\u5212\u5206\u5de6\u53f3\u5b50\u6811\n m = inorder_map[preorder[i]]\n # \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u5de6\u5b50\u6811\n root.left = dfs(preorder, inorder_map, i + 1, l, m - 1)\n # \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u53f3\u5b50\u6811\n root.right = dfs(preorder, inorder_map, i + 1 + m - l, m + 1, r)\n # \u8fd4\u56de\u6839\u8282\u70b9\n return root\n\ndef build_tree(preorder: list[int], inorder: list[int]) -> TreeNode | None:\n \"\"\"\u6784\u5efa\u4e8c\u53c9\u6811\"\"\"\n # \u521d\u59cb\u5316\u54c8\u5e0c\u8868\uff0c\u5b58\u50a8 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\n inorder_map = {val: i for i, val in enumerate(inorder)}\n root = dfs(preorder, inorder_map, 0, 0, len(inorder) - 1)\n return root\n
build_tree.cpp/* \u6784\u5efa\u4e8c\u53c9\u6811\uff1a\u5206\u6cbb */\nTreeNode *dfs(vector<int> &preorder, unordered_map<int, int> &inorderMap, int i, int l, int r) {\n // \u5b50\u6811\u533a\u95f4\u4e3a\u7a7a\u65f6\u7ec8\u6b62\n if (r - l < 0)\n return NULL;\n // \u521d\u59cb\u5316\u6839\u8282\u70b9\n TreeNode *root = new TreeNode(preorder[i]);\n // \u67e5\u8be2 m \uff0c\u4ece\u800c\u5212\u5206\u5de6\u53f3\u5b50\u6811\n int m = inorderMap[preorder[i]];\n // \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u5de6\u5b50\u6811\n root->left = dfs(preorder, inorderMap, i + 1, l, m - 1);\n // \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u53f3\u5b50\u6811\n root->right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r);\n // \u8fd4\u56de\u6839\u8282\u70b9\n return root;\n}\n\n/* \u6784\u5efa\u4e8c\u53c9\u6811 */\nTreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {\n // \u521d\u59cb\u5316\u54c8\u5e0c\u8868\uff0c\u5b58\u50a8 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\n unordered_map<int, int> inorderMap;\n for (int i = 0; i < inorder.size(); i++) {\n inorderMap[inorder[i]] = i;\n }\n TreeNode *root = dfs(preorder, inorderMap, 0, 0, inorder.size() - 1);\n return root;\n}\n
build_tree.java/* \u6784\u5efa\u4e8c\u53c9\u6811\uff1a\u5206\u6cbb */\nTreeNode dfs(int[] preorder, Map<Integer, Integer> inorderMap, int i, int l, int r) {\n // \u5b50\u6811\u533a\u95f4\u4e3a\u7a7a\u65f6\u7ec8\u6b62\n if (r - l < 0)\n return null;\n // \u521d\u59cb\u5316\u6839\u8282\u70b9\n TreeNode root = new TreeNode(preorder[i]);\n // \u67e5\u8be2 m \uff0c\u4ece\u800c\u5212\u5206\u5de6\u53f3\u5b50\u6811\n int m = inorderMap.get(preorder[i]);\n // \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u5de6\u5b50\u6811\n root.left = dfs(preorder, inorderMap, i + 1, l, m - 1);\n // \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u53f3\u5b50\u6811\n root.right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r);\n // \u8fd4\u56de\u6839\u8282\u70b9\n return root;\n}\n\n/* \u6784\u5efa\u4e8c\u53c9\u6811 */\nTreeNode buildTree(int[] preorder, int[] inorder) {\n // \u521d\u59cb\u5316\u54c8\u5e0c\u8868\uff0c\u5b58\u50a8 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\n Map<Integer, Integer> inorderMap = new HashMap<>();\n for (int i = 0; i < inorder.length; i++) {\n inorderMap.put(inorder[i], i);\n }\n TreeNode root = dfs(preorder, inorderMap, 0, 0, inorder.length - 1);\n return root;\n}\n
build_tree.cs/* \u6784\u5efa\u4e8c\u53c9\u6811\uff1a\u5206\u6cbb */\nTreeNode? DFS(int[] preorder, Dictionary<int, int> inorderMap, int i, int l, int r) {\n // \u5b50\u6811\u533a\u95f4\u4e3a\u7a7a\u65f6\u7ec8\u6b62\n if (r - l < 0)\n return null;\n // \u521d\u59cb\u5316\u6839\u8282\u70b9\n TreeNode root = new(preorder[i]);\n // \u67e5\u8be2 m \uff0c\u4ece\u800c\u5212\u5206\u5de6\u53f3\u5b50\u6811\n int m = inorderMap[preorder[i]];\n // \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u5de6\u5b50\u6811\n root.left = DFS(preorder, inorderMap, i + 1, l, m - 1);\n // \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u53f3\u5b50\u6811\n root.right = DFS(preorder, inorderMap, i + 1 + m - l, m + 1, r);\n // \u8fd4\u56de\u6839\u8282\u70b9\n return root;\n}\n\n/* \u6784\u5efa\u4e8c\u53c9\u6811 */\nTreeNode? BuildTree(int[] preorder, int[] inorder) {\n // \u521d\u59cb\u5316\u54c8\u5e0c\u8868\uff0c\u5b58\u50a8 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\n Dictionary<int, int> inorderMap = [];\n for (int i = 0; i < inorder.Length; i++) {\n inorderMap.TryAdd(inorder[i], i);\n }\n TreeNode? root = DFS(preorder, inorderMap, 0, 0, inorder.Length - 1);\n return root;\n}\n
build_tree.go/* \u6784\u5efa\u4e8c\u53c9\u6811\uff1a\u5206\u6cbb */\nfunc dfsBuildTree(preorder []int, inorderMap map[int]int, i, l, r int) *TreeNode {\n // \u5b50\u6811\u533a\u95f4\u4e3a\u7a7a\u65f6\u7ec8\u6b62\n if r-l < 0 {\n return nil\n }\n // \u521d\u59cb\u5316\u6839\u8282\u70b9\n root := NewTreeNode(preorder[i])\n // \u67e5\u8be2 m \uff0c\u4ece\u800c\u5212\u5206\u5de6\u53f3\u5b50\u6811\n m := inorderMap[preorder[i]]\n // \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u5de6\u5b50\u6811\n root.Left = dfsBuildTree(preorder, inorderMap, i+1, l, m-1)\n // \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u53f3\u5b50\u6811\n root.Right = dfsBuildTree(preorder, inorderMap, i+1+m-l, m+1, r)\n // \u8fd4\u56de\u6839\u8282\u70b9\n return root\n}\n\n/* \u6784\u5efa\u4e8c\u53c9\u6811 */\nfunc buildTree(preorder, inorder []int) *TreeNode {\n // \u521d\u59cb\u5316\u54c8\u5e0c\u8868\uff0c\u5b58\u50a8 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\n inorderMap := make(map[int]int, len(inorder))\n for i := 0; i < len(inorder); i++ {\n inorderMap[inorder[i]] = i\n }\n\n root := dfsBuildTree(preorder, inorderMap, 0, 0, len(inorder)-1)\n return root\n}\n
build_tree.swift/* \u6784\u5efa\u4e8c\u53c9\u6811\uff1a\u5206\u6cbb */\nfunc dfs(preorder: [Int], inorderMap: [Int: Int], i: Int, l: Int, r: Int) -> TreeNode? {\n // \u5b50\u6811\u533a\u95f4\u4e3a\u7a7a\u65f6\u7ec8\u6b62\n if r - l < 0 {\n return nil\n }\n // \u521d\u59cb\u5316\u6839\u8282\u70b9\n let root = TreeNode(x: preorder[i])\n // \u67e5\u8be2 m \uff0c\u4ece\u800c\u5212\u5206\u5de6\u53f3\u5b50\u6811\n let m = inorderMap[preorder[i]]!\n // \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u5de6\u5b50\u6811\n root.left = dfs(preorder: preorder, inorderMap: inorderMap, i: i + 1, l: l, r: m - 1)\n // \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u53f3\u5b50\u6811\n root.right = dfs(preorder: preorder, inorderMap: inorderMap, i: i + 1 + m - l, l: m + 1, r: r)\n // \u8fd4\u56de\u6839\u8282\u70b9\n return root\n}\n\n/* \u6784\u5efa\u4e8c\u53c9\u6811 */\nfunc buildTree(preorder: [Int], inorder: [Int]) -> TreeNode? {\n // \u521d\u59cb\u5316\u54c8\u5e0c\u8868\uff0c\u5b58\u50a8 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\n let inorderMap = inorder.enumerated().reduce(into: [:]) { $0[$1.element] = $1.offset }\n return dfs(preorder: preorder, inorderMap: inorderMap, i: 0, l: 0, r: inorder.count - 1)\n}\n
build_tree.js/* \u6784\u5efa\u4e8c\u53c9\u6811\uff1a\u5206\u6cbb */\nfunction dfs(preorder, inorderMap, i, l, r) {\n // \u5b50\u6811\u533a\u95f4\u4e3a\u7a7a\u65f6\u7ec8\u6b62\n if (r - l < 0) return null;\n // \u521d\u59cb\u5316\u6839\u8282\u70b9\n const root = new TreeNode(preorder[i]);\n // \u67e5\u8be2 m \uff0c\u4ece\u800c\u5212\u5206\u5de6\u53f3\u5b50\u6811\n const m = inorderMap.get(preorder[i]);\n // \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u5de6\u5b50\u6811\n root.left = dfs(preorder, inorderMap, i + 1, l, m - 1);\n // \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u53f3\u5b50\u6811\n root.right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r);\n // \u8fd4\u56de\u6839\u8282\u70b9\n return root;\n}\n\n/* \u6784\u5efa\u4e8c\u53c9\u6811 */\nfunction buildTree(preorder, inorder) {\n // \u521d\u59cb\u5316\u54c8\u5e0c\u8868\uff0c\u5b58\u50a8 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\n let inorderMap = new Map();\n for (let i = 0; i < inorder.length; i++) {\n inorderMap.set(inorder[i], i);\n }\n const root = dfs(preorder, inorderMap, 0, 0, inorder.length - 1);\n return root;\n}\n
build_tree.ts/* \u6784\u5efa\u4e8c\u53c9\u6811\uff1a\u5206\u6cbb */\nfunction dfs(\n preorder: number[],\n inorderMap: Map<number, number>,\n i: number,\n l: number,\n r: number\n): TreeNode | null {\n // \u5b50\u6811\u533a\u95f4\u4e3a\u7a7a\u65f6\u7ec8\u6b62\n if (r - l < 0) return null;\n // \u521d\u59cb\u5316\u6839\u8282\u70b9\n const root: TreeNode = new TreeNode(preorder[i]);\n // \u67e5\u8be2 m \uff0c\u4ece\u800c\u5212\u5206\u5de6\u53f3\u5b50\u6811\n const m = inorderMap.get(preorder[i]);\n // \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u5de6\u5b50\u6811\n root.left = dfs(preorder, inorderMap, i + 1, l, m - 1);\n // \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u53f3\u5b50\u6811\n root.right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r);\n // \u8fd4\u56de\u6839\u8282\u70b9\n return root;\n}\n\n/* \u6784\u5efa\u4e8c\u53c9\u6811 */\nfunction buildTree(preorder: number[], inorder: number[]): TreeNode | null {\n // \u521d\u59cb\u5316\u54c8\u5e0c\u8868\uff0c\u5b58\u50a8 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\n let inorderMap = new Map<number, number>();\n for (let i = 0; i < inorder.length; i++) {\n inorderMap.set(inorder[i], i);\n }\n const root = dfs(preorder, inorderMap, 0, 0, inorder.length - 1);\n return root;\n}\n
build_tree.dart/* \u6784\u5efa\u4e8c\u53c9\u6811\uff1a\u5206\u6cbb */\nTreeNode? dfs(\n List<int> preorder,\n Map<int, int> inorderMap,\n int i,\n int l,\n int r,\n) {\n // \u5b50\u6811\u533a\u95f4\u4e3a\u7a7a\u65f6\u7ec8\u6b62\n if (r - l < 0) {\n return null;\n }\n // \u521d\u59cb\u5316\u6839\u8282\u70b9\n TreeNode? root = TreeNode(preorder[i]);\n // \u67e5\u8be2 m \uff0c\u4ece\u800c\u5212\u5206\u5de6\u53f3\u5b50\u6811\n int m = inorderMap[preorder[i]]!;\n // \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u5de6\u5b50\u6811\n root.left = dfs(preorder, inorderMap, i + 1, l, m - 1);\n // \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u53f3\u5b50\u6811\n root.right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r);\n // \u8fd4\u56de\u6839\u8282\u70b9\n return root;\n}\n\n/* \u6784\u5efa\u4e8c\u53c9\u6811 */\nTreeNode? buildTree(List<int> preorder, List<int> inorder) {\n // \u521d\u59cb\u5316\u54c8\u5e0c\u8868\uff0c\u5b58\u50a8 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\n Map<int, int> inorderMap = {};\n for (int i = 0; i < inorder.length; i++) {\n inorderMap[inorder[i]] = i;\n }\n TreeNode? root = dfs(preorder, inorderMap, 0, 0, inorder.length - 1);\n return root;\n}\n
build_tree.rs/* \u6784\u5efa\u4e8c\u53c9\u6811\uff1a\u5206\u6cbb */\nfn dfs(preorder: &[i32], inorder_map: &HashMap<i32, i32>, i: i32, l: i32, r: i32) -> Option<Rc<RefCell<TreeNode>>> {\n // \u5b50\u6811\u533a\u95f4\u4e3a\u7a7a\u65f6\u7ec8\u6b62\n if r - l < 0 { return None; }\n // \u521d\u59cb\u5316\u6839\u8282\u70b9\n let root = TreeNode::new(preorder[i as usize]);\n // \u67e5\u8be2 m \uff0c\u4ece\u800c\u5212\u5206\u5de6\u53f3\u5b50\u6811\n let m = inorder_map.get(&preorder[i as usize]).unwrap();\n // \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u5de6\u5b50\u6811\n root.borrow_mut().left = dfs(preorder, inorder_map, i + 1, l, m - 1);\n // \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u53f3\u5b50\u6811\n root.borrow_mut().right = dfs(preorder, inorder_map, i + 1 + m - l, m + 1, r);\n // \u8fd4\u56de\u6839\u8282\u70b9\n Some(root)\n}\n\n/* \u6784\u5efa\u4e8c\u53c9\u6811 */\nfn build_tree(preorder: &[i32], inorder: &[i32]) -> Option<Rc<RefCell<TreeNode>>> {\n // \u521d\u59cb\u5316\u54c8\u5e0c\u8868\uff0c\u5b58\u50a8 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\n let mut inorder_map: HashMap<i32, i32> = HashMap::new();\n for i in 0..inorder.len() {\n inorder_map.insert(inorder[i], i as i32);\n }\n let root = dfs(preorder, &inorder_map, 0, 0, inorder.len() as i32 - 1);\n root\n}\n
build_tree.c/* \u6784\u5efa\u4e8c\u53c9\u6811\uff1a\u5206\u6cbb */\nTreeNode *dfs(int *preorder, int *inorderMap, int i, int l, int r, int size) {\n // \u5b50\u6811\u533a\u95f4\u4e3a\u7a7a\u65f6\u7ec8\u6b62\n if (r - l < 0)\n return NULL;\n // \u521d\u59cb\u5316\u6839\u8282\u70b9\n TreeNode *root = (TreeNode *)malloc(sizeof(TreeNode));\n root->val = preorder[i];\n root->left = NULL;\n root->right = NULL;\n // \u67e5\u8be2 m \uff0c\u4ece\u800c\u5212\u5206\u5de6\u53f3\u5b50\u6811\n int m = inorderMap[preorder[i]];\n // \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u5de6\u5b50\u6811\n root->left = dfs(preorder, inorderMap, i + 1, l, m - 1, size);\n // \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u53f3\u5b50\u6811\n root->right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r, size);\n // \u8fd4\u56de\u6839\u8282\u70b9\n return root;\n}\n\n/* \u6784\u5efa\u4e8c\u53c9\u6811 */\nTreeNode *buildTree(int *preorder, int preorderSize, int *inorder, int inorderSize) {\n // \u521d\u59cb\u5316\u54c8\u5e0c\u8868\uff0c\u5b58\u50a8 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\n int *inorderMap = (int *)malloc(sizeof(int) * MAX_SIZE);\n for (int i = 0; i < inorderSize; i++) {\n inorderMap[inorder[i]] = i;\n }\n TreeNode *root = dfs(preorder, inorderMap, 0, 0, inorderSize - 1, inorderSize);\n free(inorderMap);\n return root;\n}\n
build_tree.zig[class]{}-[func]{dfs}\n\n[class]{}-[func]{buildTree}\n
\u56fe 12-8 \u5c55\u793a\u4e86\u6784\u5efa\u4e8c\u53c9\u6811\u7684\u9012\u5f52\u8fc7\u7a0b\uff0c\u5404\u4e2a\u8282\u70b9\u662f\u5728\u5411\u4e0b\u201c\u9012\u201d\u7684\u8fc7\u7a0b\u4e2d\u5efa\u7acb\u7684\uff0c\u800c\u5404\u6761\u8fb9\uff08\u5f15\u7528\uff09\u662f\u5728\u5411\u4e0a\u201c\u5f52\u201d\u7684\u8fc7\u7a0b\u4e2d\u5efa\u7acb\u7684\u3002
<1><2><3><4><5><6><7><8><9>\u56fe 12-8 \u00a0 \u6784\u5efa\u4e8c\u53c9\u6811\u7684\u9012\u5f52\u8fc7\u7a0b
\u6bcf\u4e2a\u9012\u5f52\u51fd\u6570\u5185\u7684\u524d\u5e8f\u904d\u5386 preorder
\u548c\u4e2d\u5e8f\u904d\u5386 inorder
\u7684\u5212\u5206\u7ed3\u679c\u5982\u56fe 12-9 \u6240\u793a\u3002
\u56fe 12-9 \u00a0 \u6bcf\u4e2a\u9012\u5f52\u51fd\u6570\u4e2d\u7684\u5212\u5206\u7ed3\u679c
\u8bbe\u6811\u7684\u8282\u70b9\u6570\u91cf\u4e3a \\(n\\) \uff0c\u521d\u59cb\u5316\u6bcf\u4e00\u4e2a\u8282\u70b9\uff08\u6267\u884c\u4e00\u4e2a\u9012\u5f52\u51fd\u6570 dfs()
\uff09\u4f7f\u7528 \\(O(1)\\) \u65f6\u95f4\u3002\u56e0\u6b64\u603b\u4f53\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002
\u54c8\u5e0c\u8868\u5b58\u50a8 inorder
\u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002\u5728\u6700\u5dee\u60c5\u51b5\u4e0b\uff0c\u5373\u4e8c\u53c9\u6811\u9000\u5316\u4e3a\u94fe\u8868\u65f6\uff0c\u9012\u5f52\u6df1\u5ea6\u8fbe\u5230 \\(n\\) \uff0c\u4f7f\u7528 \\(O(n)\\) \u7684\u6808\u5e27\u7a7a\u95f4\u3002\u56e0\u6b64\u603b\u4f53\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002
\u300c\u5206\u6cbb divide and conquer\u300d\uff0c\u5168\u79f0\u5206\u800c\u6cbb\u4e4b\uff0c\u662f\u4e00\u79cd\u975e\u5e38\u91cd\u8981\u4e14\u5e38\u89c1\u7684\u7b97\u6cd5\u7b56\u7565\u3002\u5206\u6cbb\u901a\u5e38\u57fa\u4e8e\u9012\u5f52\u5b9e\u73b0\uff0c\u5305\u62ec\u201c\u5206\u201d\u548c\u201c\u6cbb\u201d\u4e24\u4e2a\u6b65\u9aa4\u3002
\u5982\u56fe 12-1 \u6240\u793a\uff0c\u201c\u5f52\u5e76\u6392\u5e8f\u201d\u662f\u5206\u6cbb\u7b56\u7565\u7684\u5178\u578b\u5e94\u7528\u4e4b\u4e00\u3002
\u56fe 12-1 \u00a0 \u5f52\u5e76\u6392\u5e8f\u7684\u5206\u6cbb\u7b56\u7565
"},{"location":"chapter_divide_and_conquer/divide_and_conquer/#1211","title":"12.1.1 \u00a0 \u5982\u4f55\u5224\u65ad\u5206\u6cbb\u95ee\u9898","text":"\u4e00\u4e2a\u95ee\u9898\u662f\u5426\u9002\u5408\u4f7f\u7528\u5206\u6cbb\u89e3\u51b3\uff0c\u901a\u5e38\u53ef\u4ee5\u53c2\u8003\u4ee5\u4e0b\u51e0\u4e2a\u5224\u65ad\u4f9d\u636e\u3002
\u663e\u7136\uff0c\u5f52\u5e76\u6392\u5e8f\u6ee1\u8db3\u4ee5\u4e0a\u4e09\u6761\u5224\u65ad\u4f9d\u636e\u3002
\u5206\u6cbb\u4e0d\u4ec5\u53ef\u4ee5\u6709\u6548\u5730\u89e3\u51b3\u7b97\u6cd5\u95ee\u9898\uff0c\u5f80\u5f80\u8fd8\u53ef\u4ee5\u63d0\u5347\u7b97\u6cd5\u6548\u7387\u3002\u5728\u6392\u5e8f\u7b97\u6cd5\u4e2d\uff0c\u5feb\u901f\u6392\u5e8f\u3001\u5f52\u5e76\u6392\u5e8f\u3001\u5806\u6392\u5e8f\u76f8\u8f83\u4e8e\u9009\u62e9\u3001\u5192\u6ce1\u3001\u63d2\u5165\u6392\u5e8f\u66f4\u5feb\uff0c\u5c31\u662f\u56e0\u4e3a\u5b83\u4eec\u5e94\u7528\u4e86\u5206\u6cbb\u7b56\u7565\u3002
\u90a3\u4e48\uff0c\u6211\u4eec\u4e0d\u7981\u53d1\u95ee\uff1a\u4e3a\u4ec0\u4e48\u5206\u6cbb\u53ef\u4ee5\u63d0\u5347\u7b97\u6cd5\u6548\u7387\uff0c\u5176\u5e95\u5c42\u903b\u8f91\u662f\u4ec0\u4e48\uff1f\u6362\u53e5\u8bdd\u8bf4\uff0c\u5c06\u5927\u95ee\u9898\u5206\u89e3\u4e3a\u591a\u4e2a\u5b50\u95ee\u9898\u3001\u89e3\u51b3\u5b50\u95ee\u9898\u3001\u5c06\u5b50\u95ee\u9898\u7684\u89e3\u5408\u5e76\u4e3a\u539f\u95ee\u9898\u7684\u89e3\uff0c\u8fd9\u51e0\u6b65\u7684\u6548\u7387\u4e3a\u4ec0\u4e48\u6bd4\u76f4\u63a5\u89e3\u51b3\u539f\u95ee\u9898\u7684\u6548\u7387\u66f4\u9ad8\uff1f\u8fd9\u4e2a\u95ee\u9898\u53ef\u4ee5\u4ece\u64cd\u4f5c\u6570\u91cf\u548c\u5e76\u884c\u8ba1\u7b97\u4e24\u65b9\u9762\u6765\u8ba8\u8bba\u3002
"},{"location":"chapter_divide_and_conquer/divide_and_conquer/#1","title":"1. \u00a0 \u64cd\u4f5c\u6570\u91cf\u4f18\u5316","text":"\u4ee5\u201c\u5192\u6ce1\u6392\u5e8f\u201d\u4e3a\u4f8b\uff0c\u5176\u5904\u7406\u4e00\u4e2a\u957f\u5ea6\u4e3a \\(n\\) \u7684\u6570\u7ec4\u9700\u8981 \\(O(n^2)\\) \u65f6\u95f4\u3002\u5047\u8bbe\u6211\u4eec\u6309\u7167\u56fe 12-2 \u6240\u793a\u7684\u65b9\u5f0f\uff0c\u5c06\u6570\u7ec4\u4ece\u4e2d\u70b9\u5904\u5206\u4e3a\u4e24\u4e2a\u5b50\u6570\u7ec4\uff0c\u5219\u5212\u5206\u9700\u8981 \\(O(n)\\) \u65f6\u95f4\uff0c\u6392\u5e8f\u6bcf\u4e2a\u5b50\u6570\u7ec4\u9700\u8981 \\(O((n / 2)^2)\\) \u65f6\u95f4\uff0c\u5408\u5e76\u4e24\u4e2a\u5b50\u6570\u7ec4\u9700\u8981 \\(O(n)\\) \u65f6\u95f4\uff0c\u603b\u4f53\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a\uff1a
\\[ O(n + (\\frac{n}{2})^2 \\times 2 + n) = O(\\frac{n^2}{2} + 2n) \\]\u56fe 12-2 \u00a0 \u5212\u5206\u6570\u7ec4\u524d\u540e\u7684\u5192\u6ce1\u6392\u5e8f
\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u8ba1\u7b97\u4ee5\u4e0b\u4e0d\u7b49\u5f0f\uff0c\u5176\u5de6\u8fb9\u548c\u53f3\u8fb9\u5206\u522b\u4e3a\u5212\u5206\u524d\u548c\u5212\u5206\u540e\u7684\u64cd\u4f5c\u603b\u6570\uff1a
\\[ \\begin{aligned} n^2 & > \\frac{n^2}{2} + 2n \\newline n^2 - \\frac{n^2}{2} - 2n & > 0 \\newline n(n - 4) & > 0 \\end{aligned} \\]\u8fd9\u610f\u5473\u7740\u5f53 \\(n > 4\\) \u65f6\uff0c\u5212\u5206\u540e\u7684\u64cd\u4f5c\u6570\u91cf\u66f4\u5c11\uff0c\u6392\u5e8f\u6548\u7387\u5e94\u8be5\u66f4\u9ad8\u3002\u8bf7\u6ce8\u610f\uff0c\u5212\u5206\u540e\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4ecd\u7136\u662f\u5e73\u65b9\u9636 \\(O(n^2)\\) \uff0c\u53ea\u662f\u590d\u6742\u5ea6\u4e2d\u7684\u5e38\u6570\u9879\u53d8\u5c0f\u4e86\u3002
\u8fdb\u4e00\u6b65\u60f3\uff0c\u5982\u679c\u6211\u4eec\u628a\u5b50\u6570\u7ec4\u4e0d\u65ad\u5730\u518d\u4ece\u4e2d\u70b9\u5904\u5212\u5206\u4e3a\u4e24\u4e2a\u5b50\u6570\u7ec4\uff0c\u76f4\u81f3\u5b50\u6570\u7ec4\u53ea\u5269\u4e00\u4e2a\u5143\u7d20\u65f6\u505c\u6b62\u5212\u5206\u5462\uff1f\u8fd9\u79cd\u601d\u8def\u5b9e\u9645\u4e0a\u5c31\u662f\u201c\u5f52\u5e76\u6392\u5e8f\u201d\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n \\log n)\\) \u3002
\u518d\u601d\u8003\uff0c\u5982\u679c\u6211\u4eec\u591a\u8bbe\u7f6e\u51e0\u4e2a\u5212\u5206\u70b9\uff0c\u5c06\u539f\u6570\u7ec4\u5e73\u5747\u5212\u5206\u4e3a \\(k\\) \u4e2a\u5b50\u6570\u7ec4\u5462\uff1f\u8fd9\u79cd\u60c5\u51b5\u4e0e\u201c\u6876\u6392\u5e8f\u201d\u975e\u5e38\u7c7b\u4f3c\uff0c\u5b83\u975e\u5e38\u9002\u5408\u6392\u5e8f\u6d77\u91cf\u6570\u636e\uff0c\u7406\u8bba\u4e0a\u65f6\u95f4\u590d\u6742\u5ea6\u53ef\u4ee5\u8fbe\u5230 \\(O(n + k)\\) \u3002
"},{"location":"chapter_divide_and_conquer/divide_and_conquer/#2","title":"2. \u00a0 \u5e76\u884c\u8ba1\u7b97\u4f18\u5316","text":"\u6211\u4eec\u77e5\u9053\uff0c\u5206\u6cbb\u751f\u6210\u7684\u5b50\u95ee\u9898\u662f\u76f8\u4e92\u72ec\u7acb\u7684\uff0c\u56e0\u6b64\u901a\u5e38\u53ef\u4ee5\u5e76\u884c\u89e3\u51b3\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5206\u6cbb\u4e0d\u4ec5\u53ef\u4ee5\u964d\u4f4e\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff0c\u8fd8\u6709\u5229\u4e8e\u64cd\u4f5c\u7cfb\u7edf\u7684\u5e76\u884c\u4f18\u5316\u3002
\u5e76\u884c\u4f18\u5316\u5728\u591a\u6838\u6216\u591a\u5904\u7406\u5668\u7684\u73af\u5883\u4e2d\u5c24\u5176\u6709\u6548\uff0c\u56e0\u4e3a\u7cfb\u7edf\u53ef\u4ee5\u540c\u65f6\u5904\u7406\u591a\u4e2a\u5b50\u95ee\u9898\uff0c\u66f4\u52a0\u5145\u5206\u5730\u5229\u7528\u8ba1\u7b97\u8d44\u6e90\uff0c\u4ece\u800c\u663e\u8457\u51cf\u5c11\u603b\u4f53\u7684\u8fd0\u884c\u65f6\u95f4\u3002
\u6bd4\u5982\u5728\u56fe 12-3 \u6240\u793a\u7684\u201c\u6876\u6392\u5e8f\u201d\u4e2d\uff0c\u6211\u4eec\u5c06\u6d77\u91cf\u7684\u6570\u636e\u5e73\u5747\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\uff0c\u5219\u53ef\u6240\u6709\u6876\u7684\u6392\u5e8f\u4efb\u52a1\u5206\u6563\u5230\u5404\u4e2a\u8ba1\u7b97\u5355\u5143\uff0c\u5b8c\u6210\u540e\u518d\u5408\u5e76\u7ed3\u679c\u3002
\u56fe 12-3 \u00a0 \u6876\u6392\u5e8f\u7684\u5e76\u884c\u8ba1\u7b97
"},{"location":"chapter_divide_and_conquer/divide_and_conquer/#1213","title":"12.1.3 \u00a0 \u5206\u6cbb\u5e38\u89c1\u5e94\u7528","text":"\u4e00\u65b9\u9762\uff0c\u5206\u6cbb\u53ef\u4ee5\u7528\u6765\u89e3\u51b3\u8bb8\u591a\u7ecf\u5178\u7b97\u6cd5\u95ee\u9898\u3002
\u53e6\u4e00\u65b9\u9762\uff0c\u5206\u6cbb\u5728\u7b97\u6cd5\u548c\u6570\u636e\u7ed3\u6784\u7684\u8bbe\u8ba1\u4e2d\u5e94\u7528\u975e\u5e38\u5e7f\u6cdb\u3002
\u53ef\u4ee5\u770b\u51fa\uff0c\u5206\u6cbb\u662f\u4e00\u79cd\u201c\u6da6\u7269\u7ec6\u65e0\u58f0\u201d\u7684\u7b97\u6cd5\u601d\u60f3\uff0c\u9690\u542b\u5728\u5404\u79cd\u7b97\u6cd5\u4e0e\u6570\u636e\u7ed3\u6784\u4e4b\u4e2d\u3002
"},{"location":"chapter_divide_and_conquer/hanota_problem/","title":"12.4 \u00a0 \u6c49\u8bfa\u5854\u95ee\u9898","text":"\u5728\u5f52\u5e76\u6392\u5e8f\u548c\u6784\u5efa\u4e8c\u53c9\u6811\u4e2d\uff0c\u6211\u4eec\u90fd\u662f\u5c06\u539f\u95ee\u9898\u5206\u89e3\u4e3a\u4e24\u4e2a\u89c4\u6a21\u4e3a\u539f\u95ee\u9898\u4e00\u534a\u7684\u5b50\u95ee\u9898\u3002\u7136\u800c\u5bf9\u4e8e\u6c49\u8bfa\u5854\u95ee\u9898\uff0c\u6211\u4eec\u91c7\u7528\u4e0d\u540c\u7684\u5206\u89e3\u7b56\u7565\u3002
Question
\u7ed9\u5b9a\u4e09\u6839\u67f1\u5b50\uff0c\u8bb0\u4e3a A
\u3001B
\u548c C
\u3002\u8d77\u59cb\u72b6\u6001\u4e0b\uff0c\u67f1\u5b50 A
\u4e0a\u5957\u7740 \\(n\\) \u4e2a\u5706\u76d8\uff0c\u5b83\u4eec\u4ece\u4e0a\u5230\u4e0b\u6309\u7167\u4ece\u5c0f\u5230\u5927\u7684\u987a\u5e8f\u6392\u5217\u3002\u6211\u4eec\u7684\u4efb\u52a1\u662f\u8981\u628a\u8fd9 \\(n\\) \u4e2a\u5706\u76d8\u79fb\u5230\u67f1\u5b50 C
\u4e0a\uff0c\u5e76\u4fdd\u6301\u5b83\u4eec\u7684\u539f\u6709\u987a\u5e8f\u4e0d\u53d8\uff08\u5982\u56fe 12-10 \u6240\u793a\uff09\u3002\u5728\u79fb\u52a8\u5706\u76d8\u7684\u8fc7\u7a0b\u4e2d\uff0c\u9700\u8981\u9075\u5b88\u4ee5\u4e0b\u89c4\u5219\u3002
\u56fe 12-10 \u00a0 \u6c49\u8bfa\u5854\u95ee\u9898\u793a\u4f8b
\u6211\u4eec\u5c06\u89c4\u6a21\u4e3a \\(i\\) \u7684\u6c49\u8bfa\u5854\u95ee\u9898\u8bb0\u4f5c \\(f(i)\\) \u3002\u4f8b\u5982 \\(f(3)\\) \u4ee3\u8868\u5c06 \\(3\\) \u4e2a\u5706\u76d8\u4ece A
\u79fb\u52a8\u81f3 C
\u7684\u6c49\u8bfa\u5854\u95ee\u9898\u3002
\u5982\u56fe 12-11 \u6240\u793a\uff0c\u5bf9\u4e8e\u95ee\u9898 \\(f(1)\\) \uff0c\u5373\u5f53\u53ea\u6709\u4e00\u4e2a\u5706\u76d8\u65f6\uff0c\u6211\u4eec\u5c06\u5b83\u76f4\u63a5\u4ece A
\u79fb\u52a8\u81f3 C
\u5373\u53ef\u3002
\u56fe 12-11 \u00a0 \u89c4\u6a21\u4e3a 1 \u95ee\u9898\u7684\u89e3
\u5982\u56fe 12-12 \u6240\u793a\uff0c\u5bf9\u4e8e\u95ee\u9898 \\(f(2)\\) \uff0c\u5373\u5f53\u6709\u4e24\u4e2a\u5706\u76d8\u65f6\uff0c\u7531\u4e8e\u8981\u65f6\u523b\u6ee1\u8db3\u5c0f\u5706\u76d8\u5728\u5927\u5706\u76d8\u4e4b\u4e0a\uff0c\u56e0\u6b64\u9700\u8981\u501f\u52a9 B
\u6765\u5b8c\u6210\u79fb\u52a8\u3002
A
\u79fb\u81f3 B
\u3002A
\u79fb\u81f3 C
\u3002B
\u79fb\u81f3 C
\u3002\u56fe 12-12 \u00a0 \u89c4\u6a21\u4e3a 2 \u95ee\u9898\u7684\u89e3
\u89e3\u51b3\u95ee\u9898 \\(f(2)\\) \u7684\u8fc7\u7a0b\u53ef\u603b\u7ed3\u4e3a\uff1a\u5c06\u4e24\u4e2a\u5706\u76d8\u501f\u52a9 B
\u4ece A
\u79fb\u81f3 C
\u3002\u5176\u4e2d\uff0cC
\u79f0\u4e3a\u76ee\u6807\u67f1\u3001B
\u79f0\u4e3a\u7f13\u51b2\u67f1\u3002
\u5bf9\u4e8e\u95ee\u9898 \\(f(3)\\) \uff0c\u5373\u5f53\u6709\u4e09\u4e2a\u5706\u76d8\u65f6\uff0c\u60c5\u51b5\u53d8\u5f97\u7a0d\u5fae\u590d\u6742\u4e86\u4e00\u4e9b\u3002
\u56e0\u4e3a\u5df2\u77e5 \\(f(1)\\) \u548c \\(f(2)\\) \u7684\u89e3\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ece\u5206\u6cbb\u89d2\u5ea6\u601d\u8003\uff0c\u5c06 A
\u9876\u90e8\u7684\u4e24\u4e2a\u5706\u76d8\u770b\u4f5c\u4e00\u4e2a\u6574\u4f53\uff0c\u6267\u884c\u56fe 12-13 \u6240\u793a\u7684\u6b65\u9aa4\u3002\u8fd9\u6837\u4e09\u4e2a\u5706\u76d8\u5c31\u88ab\u987a\u5229\u5730\u4ece A
\u79fb\u81f3 C
\u4e86\u3002
B
\u4e3a\u76ee\u6807\u67f1\u3001C
\u4e3a\u7f13\u51b2\u67f1\uff0c\u5c06\u4e24\u4e2a\u5706\u76d8\u4ece A
\u79fb\u81f3 B
\u3002A
\u4e2d\u5269\u4f59\u7684\u4e00\u4e2a\u5706\u76d8\u4ece A
\u76f4\u63a5\u79fb\u52a8\u81f3 C
\u3002C
\u4e3a\u76ee\u6807\u67f1\u3001A
\u4e3a\u7f13\u51b2\u67f1\uff0c\u5c06\u4e24\u4e2a\u5706\u76d8\u4ece B
\u79fb\u81f3 C
\u3002\u56fe 12-13 \u00a0 \u89c4\u6a21\u4e3a 3 \u95ee\u9898\u7684\u89e3
\u4ece\u672c\u8d28\u4e0a\u770b\uff0c\u6211\u4eec\u5c06\u95ee\u9898 \\(f(3)\\) \u5212\u5206\u4e3a\u4e24\u4e2a\u5b50\u95ee\u9898 \\(f(2)\\) \u548c\u5b50\u95ee\u9898 \\(f(1)\\) \u3002\u6309\u987a\u5e8f\u89e3\u51b3\u8fd9\u4e09\u4e2a\u5b50\u95ee\u9898\u4e4b\u540e\uff0c\u539f\u95ee\u9898\u968f\u4e4b\u5f97\u5230\u89e3\u51b3\u3002\u8fd9\u8bf4\u660e\u5b50\u95ee\u9898\u662f\u72ec\u7acb\u7684\uff0c\u800c\u4e14\u89e3\u53ef\u4ee5\u5408\u5e76\u3002
\u81f3\u6b64\uff0c\u6211\u4eec\u53ef\u603b\u7ed3\u51fa\u56fe 12-14 \u6240\u793a\u7684\u89e3\u51b3\u6c49\u8bfa\u5854\u95ee\u9898\u7684\u5206\u6cbb\u7b56\u7565\uff1a\u5c06\u539f\u95ee\u9898 \\(f(n)\\) \u5212\u5206\u4e3a\u4e24\u4e2a\u5b50\u95ee\u9898 \\(f(n-1)\\) \u548c\u4e00\u4e2a\u5b50\u95ee\u9898 \\(f(1)\\) \uff0c\u5e76\u6309\u7167\u4ee5\u4e0b\u987a\u5e8f\u89e3\u51b3\u8fd9\u4e09\u4e2a\u5b50\u95ee\u9898\u3002
C
\u4ece A
\u79fb\u81f3 B
\u3002A
\u76f4\u63a5\u79fb\u81f3 C
\u3002A
\u4ece B
\u79fb\u81f3 C
\u3002\u5bf9\u4e8e\u8fd9\u4e24\u4e2a\u5b50\u95ee\u9898 \\(f(n-1)\\) \uff0c\u53ef\u4ee5\u901a\u8fc7\u76f8\u540c\u7684\u65b9\u5f0f\u8fdb\u884c\u9012\u5f52\u5212\u5206\uff0c\u76f4\u81f3\u8fbe\u5230\u6700\u5c0f\u5b50\u95ee\u9898 \\(f(1)\\) \u3002\u800c \\(f(1)\\) \u7684\u89e3\u662f\u5df2\u77e5\u7684\uff0c\u53ea\u9700\u4e00\u6b21\u79fb\u52a8\u64cd\u4f5c\u5373\u53ef\u3002
\u56fe 12-14 \u00a0 \u89e3\u51b3\u6c49\u8bfa\u5854\u95ee\u9898\u7684\u5206\u6cbb\u7b56\u7565
"},{"location":"chapter_divide_and_conquer/hanota_problem/#3","title":"3. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"\u5728\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u58f0\u660e\u4e00\u4e2a\u9012\u5f52\u51fd\u6570 dfs(i, src, buf, tar)
\uff0c\u5b83\u7684\u4f5c\u7528\u662f\u5c06\u67f1 src
\u9876\u90e8\u7684 \\(i\\) \u4e2a\u5706\u76d8\u501f\u52a9\u7f13\u51b2\u67f1 buf
\u79fb\u52a8\u81f3\u76ee\u6807\u67f1 tar
\uff1a
def move(src: list[int], tar: list[int]):\n \"\"\"\u79fb\u52a8\u4e00\u4e2a\u5706\u76d8\"\"\"\n # \u4ece src \u9876\u90e8\u62ff\u51fa\u4e00\u4e2a\u5706\u76d8\n pan = src.pop()\n # \u5c06\u5706\u76d8\u653e\u5165 tar \u9876\u90e8\n tar.append(pan)\n\ndef dfs(i: int, src: list[int], buf: list[int], tar: list[int]):\n \"\"\"\u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 f(i)\"\"\"\n # \u82e5 src \u53ea\u5269\u4e0b\u4e00\u4e2a\u5706\u76d8\uff0c\u5219\u76f4\u63a5\u5c06\u5176\u79fb\u5230 tar\n if i == 1:\n move(src, tar)\n return\n # \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 src \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 tar \u79fb\u5230 buf\n dfs(i - 1, src, tar, buf)\n # \u5b50\u95ee\u9898 f(1) \uff1a\u5c06 src \u5269\u4f59\u4e00\u4e2a\u5706\u76d8\u79fb\u5230 tar\n move(src, tar)\n # \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 buf \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 src \u79fb\u5230 tar\n dfs(i - 1, buf, src, tar)\n\ndef solve_hanota(A: list[int], B: list[int], C: list[int]):\n \"\"\"\u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898\"\"\"\n n = len(A)\n # \u5c06 A \u9876\u90e8 n \u4e2a\u5706\u76d8\u501f\u52a9 B \u79fb\u5230 C\n dfs(n, A, B, C)\n
hanota.cpp/* \u79fb\u52a8\u4e00\u4e2a\u5706\u76d8 */\nvoid move(vector<int> &src, vector<int> &tar) {\n // \u4ece src \u9876\u90e8\u62ff\u51fa\u4e00\u4e2a\u5706\u76d8\n int pan = src.back();\n src.pop_back();\n // \u5c06\u5706\u76d8\u653e\u5165 tar \u9876\u90e8\n tar.push_back(pan);\n}\n\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 f(i) */\nvoid dfs(int i, vector<int> &src, vector<int> &buf, vector<int> &tar) {\n // \u82e5 src \u53ea\u5269\u4e0b\u4e00\u4e2a\u5706\u76d8\uff0c\u5219\u76f4\u63a5\u5c06\u5176\u79fb\u5230 tar\n if (i == 1) {\n move(src, tar);\n return;\n }\n // \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 src \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 tar \u79fb\u5230 buf\n dfs(i - 1, src, tar, buf);\n // \u5b50\u95ee\u9898 f(1) \uff1a\u5c06 src \u5269\u4f59\u4e00\u4e2a\u5706\u76d8\u79fb\u5230 tar\n move(src, tar);\n // \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 buf \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 src \u79fb\u5230 tar\n dfs(i - 1, buf, src, tar);\n}\n\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 */\nvoid solveHanota(vector<int> &A, vector<int> &B, vector<int> &C) {\n int n = A.size();\n // \u5c06 A \u9876\u90e8 n \u4e2a\u5706\u76d8\u501f\u52a9 B \u79fb\u5230 C\n dfs(n, A, B, C);\n}\n
hanota.java/* \u79fb\u52a8\u4e00\u4e2a\u5706\u76d8 */\nvoid move(List<Integer> src, List<Integer> tar) {\n // \u4ece src \u9876\u90e8\u62ff\u51fa\u4e00\u4e2a\u5706\u76d8\n Integer pan = src.remove(src.size() - 1);\n // \u5c06\u5706\u76d8\u653e\u5165 tar \u9876\u90e8\n tar.add(pan);\n}\n\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 f(i) */\nvoid dfs(int i, List<Integer> src, List<Integer> buf, List<Integer> tar) {\n // \u82e5 src \u53ea\u5269\u4e0b\u4e00\u4e2a\u5706\u76d8\uff0c\u5219\u76f4\u63a5\u5c06\u5176\u79fb\u5230 tar\n if (i == 1) {\n move(src, tar);\n return;\n }\n // \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 src \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 tar \u79fb\u5230 buf\n dfs(i - 1, src, tar, buf);\n // \u5b50\u95ee\u9898 f(1) \uff1a\u5c06 src \u5269\u4f59\u4e00\u4e2a\u5706\u76d8\u79fb\u5230 tar\n move(src, tar);\n // \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 buf \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 src \u79fb\u5230 tar\n dfs(i - 1, buf, src, tar);\n}\n\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 */\nvoid solveHanota(List<Integer> A, List<Integer> B, List<Integer> C) {\n int n = A.size();\n // \u5c06 A \u9876\u90e8 n \u4e2a\u5706\u76d8\u501f\u52a9 B \u79fb\u5230 C\n dfs(n, A, B, C);\n}\n
hanota.cs/* \u79fb\u52a8\u4e00\u4e2a\u5706\u76d8 */\nvoid Move(List<int> src, List<int> tar) {\n // \u4ece src \u9876\u90e8\u62ff\u51fa\u4e00\u4e2a\u5706\u76d8\n int pan = src[^1];\n src.RemoveAt(src.Count - 1);\n // \u5c06\u5706\u76d8\u653e\u5165 tar \u9876\u90e8\n tar.Add(pan);\n}\n\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 f(i) */\nvoid DFS(int i, List<int> src, List<int> buf, List<int> tar) {\n // \u82e5 src \u53ea\u5269\u4e0b\u4e00\u4e2a\u5706\u76d8\uff0c\u5219\u76f4\u63a5\u5c06\u5176\u79fb\u5230 tar\n if (i == 1) {\n Move(src, tar);\n return;\n }\n // \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 src \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 tar \u79fb\u5230 buf\n DFS(i - 1, src, tar, buf);\n // \u5b50\u95ee\u9898 f(1) \uff1a\u5c06 src \u5269\u4f59\u4e00\u4e2a\u5706\u76d8\u79fb\u5230 tar\n Move(src, tar);\n // \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 buf \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 src \u79fb\u5230 tar\n DFS(i - 1, buf, src, tar);\n}\n\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 */\nvoid SolveHanota(List<int> A, List<int> B, List<int> C) {\n int n = A.Count;\n // \u5c06 A \u9876\u90e8 n \u4e2a\u5706\u76d8\u501f\u52a9 B \u79fb\u5230 C\n DFS(n, A, B, C);\n}\n
hanota.go/* \u79fb\u52a8\u4e00\u4e2a\u5706\u76d8 */\nfunc move(src, tar *list.List) {\n // \u4ece src \u9876\u90e8\u62ff\u51fa\u4e00\u4e2a\u5706\u76d8\n pan := src.Back()\n // \u5c06\u5706\u76d8\u653e\u5165 tar \u9876\u90e8\n tar.PushBack(pan.Value)\n // \u79fb\u9664 src \u9876\u90e8\u5706\u76d8\n src.Remove(pan)\n}\n\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 f(i) */\nfunc dfsHanota(i int, src, buf, tar *list.List) {\n // \u82e5 src \u53ea\u5269\u4e0b\u4e00\u4e2a\u5706\u76d8\uff0c\u5219\u76f4\u63a5\u5c06\u5176\u79fb\u5230 tar\n if i == 1 {\n move(src, tar)\n return\n }\n // \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 src \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 tar \u79fb\u5230 buf\n dfsHanota(i-1, src, tar, buf)\n // \u5b50\u95ee\u9898 f(1) \uff1a\u5c06 src \u5269\u4f59\u4e00\u4e2a\u5706\u76d8\u79fb\u5230 tar\n move(src, tar)\n // \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 buf \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 src \u79fb\u5230 tar\n dfsHanota(i-1, buf, src, tar)\n}\n\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 */\nfunc solveHanota(A, B, C *list.List) {\n n := A.Len()\n // \u5c06 A \u9876\u90e8 n \u4e2a\u5706\u76d8\u501f\u52a9 B \u79fb\u5230 C\n dfsHanota(n, A, B, C)\n}\n
hanota.swift/* \u79fb\u52a8\u4e00\u4e2a\u5706\u76d8 */\nfunc move(src: inout [Int], tar: inout [Int]) {\n // \u4ece src \u9876\u90e8\u62ff\u51fa\u4e00\u4e2a\u5706\u76d8\n let pan = src.popLast()!\n // \u5c06\u5706\u76d8\u653e\u5165 tar \u9876\u90e8\n tar.append(pan)\n}\n\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 f(i) */\nfunc dfs(i: Int, src: inout [Int], buf: inout [Int], tar: inout [Int]) {\n // \u82e5 src \u53ea\u5269\u4e0b\u4e00\u4e2a\u5706\u76d8\uff0c\u5219\u76f4\u63a5\u5c06\u5176\u79fb\u5230 tar\n if i == 1 {\n move(src: &src, tar: &tar)\n return\n }\n // \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 src \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 tar \u79fb\u5230 buf\n dfs(i: i - 1, src: &src, buf: &tar, tar: &buf)\n // \u5b50\u95ee\u9898 f(1) \uff1a\u5c06 src \u5269\u4f59\u4e00\u4e2a\u5706\u76d8\u79fb\u5230 tar\n move(src: &src, tar: &tar)\n // \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 buf \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 src \u79fb\u5230 tar\n dfs(i: i - 1, src: &buf, buf: &src, tar: &tar)\n}\n\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 */\nfunc solveHanota(A: inout [Int], B: inout [Int], C: inout [Int]) {\n let n = A.count\n // \u5217\u8868\u5c3e\u90e8\u662f\u67f1\u5b50\u9876\u90e8\n // \u5c06 src \u9876\u90e8 n \u4e2a\u5706\u76d8\u501f\u52a9 B \u79fb\u5230 C\n dfs(i: n, src: &A, buf: &B, tar: &C)\n}\n
hanota.js/* \u79fb\u52a8\u4e00\u4e2a\u5706\u76d8 */\nfunction move(src, tar) {\n // \u4ece src \u9876\u90e8\u62ff\u51fa\u4e00\u4e2a\u5706\u76d8\n const pan = src.pop();\n // \u5c06\u5706\u76d8\u653e\u5165 tar \u9876\u90e8\n tar.push(pan);\n}\n\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 f(i) */\nfunction dfs(i, src, buf, tar) {\n // \u82e5 src \u53ea\u5269\u4e0b\u4e00\u4e2a\u5706\u76d8\uff0c\u5219\u76f4\u63a5\u5c06\u5176\u79fb\u5230 tar\n if (i === 1) {\n move(src, tar);\n return;\n }\n // \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 src \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 tar \u79fb\u5230 buf\n dfs(i - 1, src, tar, buf);\n // \u5b50\u95ee\u9898 f(1) \uff1a\u5c06 src \u5269\u4f59\u4e00\u4e2a\u5706\u76d8\u79fb\u5230 tar\n move(src, tar);\n // \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 buf \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 src \u79fb\u5230 tar\n dfs(i - 1, buf, src, tar);\n}\n\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 */\nfunction solveHanota(A, B, C) {\n const n = A.length;\n // \u5c06 A \u9876\u90e8 n \u4e2a\u5706\u76d8\u501f\u52a9 B \u79fb\u5230 C\n dfs(n, A, B, C);\n}\n
hanota.ts/* \u79fb\u52a8\u4e00\u4e2a\u5706\u76d8 */\nfunction move(src: number[], tar: number[]): void {\n // \u4ece src \u9876\u90e8\u62ff\u51fa\u4e00\u4e2a\u5706\u76d8\n const pan = src.pop();\n // \u5c06\u5706\u76d8\u653e\u5165 tar \u9876\u90e8\n tar.push(pan);\n}\n\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 f(i) */\nfunction dfs(i: number, src: number[], buf: number[], tar: number[]): void {\n // \u82e5 src \u53ea\u5269\u4e0b\u4e00\u4e2a\u5706\u76d8\uff0c\u5219\u76f4\u63a5\u5c06\u5176\u79fb\u5230 tar\n if (i === 1) {\n move(src, tar);\n return;\n }\n // \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 src \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 tar \u79fb\u5230 buf\n dfs(i - 1, src, tar, buf);\n // \u5b50\u95ee\u9898 f(1) \uff1a\u5c06 src \u5269\u4f59\u4e00\u4e2a\u5706\u76d8\u79fb\u5230 tar\n move(src, tar);\n // \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 buf \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 src \u79fb\u5230 tar\n dfs(i - 1, buf, src, tar);\n}\n\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 */\nfunction solveHanota(A: number[], B: number[], C: number[]): void {\n const n = A.length;\n // \u5c06 A \u9876\u90e8 n \u4e2a\u5706\u76d8\u501f\u52a9 B \u79fb\u5230 C\n dfs(n, A, B, C);\n}\n
hanota.dart/* \u79fb\u52a8\u4e00\u4e2a\u5706\u76d8 */\nvoid move(List<int> src, List<int> tar) {\n // \u4ece src \u9876\u90e8\u62ff\u51fa\u4e00\u4e2a\u5706\u76d8\n int pan = src.removeLast();\n // \u5c06\u5706\u76d8\u653e\u5165 tar \u9876\u90e8\n tar.add(pan);\n}\n\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 f(i) */\nvoid dfs(int i, List<int> src, List<int> buf, List<int> tar) {\n // \u82e5 src \u53ea\u5269\u4e0b\u4e00\u4e2a\u5706\u76d8\uff0c\u5219\u76f4\u63a5\u5c06\u5176\u79fb\u5230 tar\n if (i == 1) {\n move(src, tar);\n return;\n }\n // \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 src \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 tar \u79fb\u5230 buf\n dfs(i - 1, src, tar, buf);\n // \u5b50\u95ee\u9898 f(1) \uff1a\u5c06 src \u5269\u4f59\u4e00\u4e2a\u5706\u76d8\u79fb\u5230 tar\n move(src, tar);\n // \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 buf \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 src \u79fb\u5230 tar\n dfs(i - 1, buf, src, tar);\n}\n\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 */\nvoid solveHanota(List<int> A, List<int> B, List<int> C) {\n int n = A.length;\n // \u5c06 A \u9876\u90e8 n \u4e2a\u5706\u76d8\u501f\u52a9 B \u79fb\u5230 C\n dfs(n, A, B, C);\n}\n
hanota.rs/* \u79fb\u52a8\u4e00\u4e2a\u5706\u76d8 */\nfn move_pan(src: &mut Vec<i32>, tar: &mut Vec<i32>) {\n // \u4ece src \u9876\u90e8\u62ff\u51fa\u4e00\u4e2a\u5706\u76d8\n let pan = src.remove(src.len() - 1);\n // \u5c06\u5706\u76d8\u653e\u5165 tar \u9876\u90e8\n tar.push(pan);\n}\n\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 f(i) */\nfn dfs(i: i32, src: &mut Vec<i32>, buf: &mut Vec<i32>, tar: &mut Vec<i32>) {\n // \u82e5 src \u53ea\u5269\u4e0b\u4e00\u4e2a\u5706\u76d8\uff0c\u5219\u76f4\u63a5\u5c06\u5176\u79fb\u5230 tar\n if i == 1 {\n move_pan(src, tar);\n return;\n }\n // \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 src \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 tar \u79fb\u5230 buf\n dfs(i - 1, src, tar, buf);\n // \u5b50\u95ee\u9898 f(1) \uff1a\u5c06 src \u5269\u4f59\u4e00\u4e2a\u5706\u76d8\u79fb\u5230 tar\n move_pan(src, tar);\n // \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 buf \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 src \u79fb\u5230 tar\n dfs(i - 1, buf, src, tar);\n}\n\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 */\nfn solve_hanota(A: &mut Vec<i32>, B: &mut Vec<i32>, C: &mut Vec<i32>) {\n let n = A.len() as i32;\n // \u5c06 A \u9876\u90e8 n \u4e2a\u5706\u76d8\u501f\u52a9 B \u79fb\u5230 C\n dfs(n, A, B, C);\n}\n
hanota.c/* \u79fb\u52a8\u4e00\u4e2a\u5706\u76d8 */\nvoid move(int *src, int *srcSize, int *tar, int *tarSize) {\n // \u4ece src \u9876\u90e8\u62ff\u51fa\u4e00\u4e2a\u5706\u76d8\n int pan = src[*srcSize - 1];\n src[*srcSize - 1] = 0;\n (*srcSize)--;\n // \u5c06\u5706\u76d8\u653e\u5165 tar \u9876\u90e8\n tar[*tarSize] = pan;\n (*tarSize)++;\n}\n\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 f(i) */\nvoid dfs(int i, int *src, int *srcSize, int *buf, int *bufSize, int *tar, int *tarSize) {\n // \u82e5 src \u53ea\u5269\u4e0b\u4e00\u4e2a\u5706\u76d8\uff0c\u5219\u76f4\u63a5\u5c06\u5176\u79fb\u5230 tar\n if (i == 1) {\n move(src, srcSize, tar, tarSize);\n return;\n }\n // \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 src \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 tar \u79fb\u5230 buf\n dfs(i - 1, src, srcSize, tar, tarSize, buf, bufSize);\n // \u5b50\u95ee\u9898 f(1) \uff1a\u5c06 src \u5269\u4f59\u4e00\u4e2a\u5706\u76d8\u79fb\u5230 tar\n move(src, srcSize, tar, tarSize);\n // \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 buf \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 src \u79fb\u5230 tar\n dfs(i - 1, buf, bufSize, src, srcSize, tar, tarSize);\n}\n\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\u95ee\u9898 */\nvoid solveHanota(int *A, int *ASize, int *B, int *BSize, int *C, int *CSize) {\n // \u5c06 A \u9876\u90e8 n \u4e2a\u5706\u76d8\u501f\u52a9 B \u79fb\u5230 C\n dfs(*ASize, A, ASize, B, BSize, C, CSize);\n}\n
hanota.zig[class]{}-[func]{move}\n\n[class]{}-[func]{dfs}\n\n[class]{}-[func]{solveHanota}\n
\u5982\u56fe 12-15 \u6240\u793a\uff0c\u6c49\u8bfa\u5854\u95ee\u9898\u5f62\u6210\u4e00\u68f5\u9ad8\u5ea6\u4e3a \\(n\\) \u7684\u9012\u5f52\u6811\uff0c\u6bcf\u4e2a\u8282\u70b9\u4ee3\u8868\u4e00\u4e2a\u5b50\u95ee\u9898\uff0c\u5bf9\u5e94\u4e00\u4e2a\u5f00\u542f\u7684 dfs()
\u51fd\u6570\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(2^n)\\) \uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002
\u56fe 12-15 \u00a0 \u6c49\u8bfa\u5854\u95ee\u9898\u7684\u9012\u5f52\u6811
Quote
\u6c49\u8bfa\u5854\u95ee\u9898\u6e90\u81ea\u4e00\u4e2a\u53e4\u8001\u7684\u4f20\u8bf4\u3002\u5728\u53e4\u5370\u5ea6\u7684\u4e00\u4e2a\u5bfa\u5e99\u91cc\uff0c\u50e7\u4fa3\u4eec\u6709\u4e09\u6839\u9ad8\u5927\u7684\u94bb\u77f3\u67f1\u5b50\uff0c\u4ee5\u53ca \\(64\\) \u4e2a\u5927\u5c0f\u4e0d\u4e00\u7684\u91d1\u5706\u76d8\u3002\u50e7\u4fa3\u4eec\u4e0d\u65ad\u5730\u79fb\u52a8\u5706\u76d8\uff0c\u4ed6\u4eec\u76f8\u4fe1\u5728\u6700\u540e\u4e00\u4e2a\u5706\u76d8\u88ab\u6b63\u786e\u653e\u7f6e\u7684\u90a3\u4e00\u523b\uff0c\u8fd9\u4e2a\u4e16\u754c\u5c31\u4f1a\u7ed3\u675f\u3002
\u7136\u800c\uff0c\u5373\u4f7f\u50e7\u4fa3\u4eec\u6bcf\u79d2\u949f\u79fb\u52a8\u4e00\u6b21\uff0c\u603b\u5171\u9700\u8981\u5927\u7ea6 \\(2^{64} \\approx 1.84\u00d710^{19}\\) \u79d2\uff0c\u5408\u7ea6 \\(5850\\) \u4ebf\u5e74\uff0c\u8fdc\u8fdc\u8d85\u8fc7\u4e86\u73b0\u5728\u5bf9\u5b87\u5b99\u5e74\u9f84\u7684\u4f30\u8ba1\u3002\u6240\u4ee5\uff0c\u5018\u82e5\u8fd9\u4e2a\u4f20\u8bf4\u662f\u771f\u7684\uff0c\u6211\u4eec\u5e94\u8be5\u4e0d\u9700\u8981\u62c5\u5fc3\u4e16\u754c\u672b\u65e5\u7684\u5230\u6765\u3002
"},{"location":"chapter_divide_and_conquer/summary/","title":"12.5 \u00a0 \u5c0f\u7ed3","text":"Abstract
\u5c0f\u6eaa\u6c47\u5165\u6cb3\u6d41\uff0c\u6c5f\u6cb3\u6c47\u5165\u5927\u6d77\u3002
\u52a8\u6001\u89c4\u5212\u5c06\u5c0f\u95ee\u9898\u7684\u89e3\u6c47\u96c6\u6210\u5927\u95ee\u9898\u7684\u7b54\u6848\uff0c\u4e00\u6b65\u6b65\u5f15\u9886\u6211\u4eec\u8d70\u5411\u89e3\u51b3\u95ee\u9898\u7684\u5f7c\u5cb8\u3002
"},{"location":"chapter_dynamic_programming/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"\u5728\u4e0a\u4e00\u8282\u4e2d\uff0c\u6211\u4eec\u5b66\u4e60\u4e86\u52a8\u6001\u89c4\u5212\u662f\u5982\u4f55\u901a\u8fc7\u5b50\u95ee\u9898\u5206\u89e3\u6765\u6c42\u89e3\u539f\u95ee\u9898\u7684\u3002\u5b9e\u9645\u4e0a\uff0c\u5b50\u95ee\u9898\u5206\u89e3\u662f\u4e00\u79cd\u901a\u7528\u7684\u7b97\u6cd5\u601d\u8def\uff0c\u5728\u5206\u6cbb\u3001\u52a8\u6001\u89c4\u5212\u3001\u56de\u6eaf\u4e2d\u7684\u4fa7\u91cd\u70b9\u4e0d\u540c\u3002
\u5b9e\u9645\u4e0a\uff0c\u52a8\u6001\u89c4\u5212\u5e38\u7528\u6765\u6c42\u89e3\u6700\u4f18\u5316\u95ee\u9898\uff0c\u5b83\u4eec\u4e0d\u4ec5\u5305\u542b\u91cd\u53e0\u5b50\u95ee\u9898\uff0c\u8fd8\u5177\u6709\u53e6\u5916\u4e24\u5927\u7279\u6027\uff1a\u6700\u4f18\u5b50\u7ed3\u6784\u3001\u65e0\u540e\u6548\u6027\u3002
"},{"location":"chapter_dynamic_programming/dp_problem_features/#1421","title":"14.2.1 \u00a0 \u6700\u4f18\u5b50\u7ed3\u6784","text":"\u6211\u4eec\u5bf9\u722c\u697c\u68af\u95ee\u9898\u7a0d\u4f5c\u6539\u52a8\uff0c\u4f7f\u4e4b\u66f4\u52a0\u9002\u5408\u5c55\u793a\u6700\u4f18\u5b50\u7ed3\u6784\u6982\u5ff5\u3002
\u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7
\u7ed9\u5b9a\u4e00\u4e2a\u697c\u68af\uff0c\u4f60\u6bcf\u6b65\u53ef\u4ee5\u4e0a \\(1\\) \u9636\u6216\u8005 \\(2\\) \u9636\uff0c\u6bcf\u4e00\u9636\u697c\u68af\u4e0a\u90fd\u8d34\u6709\u4e00\u4e2a\u975e\u8d1f\u6574\u6570\uff0c\u8868\u793a\u4f60\u5728\u8be5\u53f0\u9636\u6240\u9700\u8981\u4ed8\u51fa\u7684\u4ee3\u4ef7\u3002\u7ed9\u5b9a\u4e00\u4e2a\u975e\u8d1f\u6574\u6570\u6570\u7ec4 \\(cost\\) \uff0c\u5176\u4e2d \\(cost[i]\\) \u8868\u793a\u5728\u7b2c \\(i\\) \u4e2a\u53f0\u9636\u9700\u8981\u4ed8\u51fa\u7684\u4ee3\u4ef7\uff0c\\(cost[0]\\) \u4e3a\u5730\u9762\uff08\u8d77\u59cb\u70b9\uff09\u3002\u8bf7\u8ba1\u7b97\u6700\u5c11\u9700\u8981\u4ed8\u51fa\u591a\u5c11\u4ee3\u4ef7\u624d\u80fd\u5230\u8fbe\u9876\u90e8\uff1f
\u5982\u56fe 14-6 \u6240\u793a\uff0c\u82e5\u7b2c \\(1\\)\u3001\\(2\\)\u3001\\(3\\) \u9636\u7684\u4ee3\u4ef7\u5206\u522b\u4e3a \\(1\\)\u3001\\(10\\)\u3001\\(1\\) \uff0c\u5219\u4ece\u5730\u9762\u722c\u5230\u7b2c \\(3\\) \u9636\u7684\u6700\u5c0f\u4ee3\u4ef7\u4e3a \\(2\\) \u3002
\u56fe 14-6 \u00a0 \u722c\u5230\u7b2c 3 \u9636\u7684\u6700\u5c0f\u4ee3\u4ef7
\u8bbe \\(dp[i]\\) \u4e3a\u722c\u5230\u7b2c \\(i\\) \u9636\u7d2f\u8ba1\u4ed8\u51fa\u7684\u4ee3\u4ef7\uff0c\u7531\u4e8e\u7b2c \\(i\\) \u9636\u53ea\u53ef\u80fd\u4ece \\(i - 1\\) \u9636\u6216 \\(i - 2\\) \u9636\u8d70\u6765\uff0c\u56e0\u6b64 \\(dp[i]\\) \u53ea\u53ef\u80fd\u7b49\u4e8e \\(dp[i - 1] + cost[i]\\) \u6216 \\(dp[i - 2] + cost[i]\\) \u3002\u4e3a\u4e86\u5c3d\u53ef\u80fd\u51cf\u5c11\u4ee3\u4ef7\uff0c\u6211\u4eec\u5e94\u8be5\u9009\u62e9\u4e24\u8005\u4e2d\u8f83\u5c0f\u7684\u90a3\u4e00\u4e2a\uff1a
\\[ dp[i] = \\min(dp[i-1], dp[i-2]) + cost[i] \\]\u8fd9\u4fbf\u53ef\u4ee5\u5f15\u51fa\u6700\u4f18\u5b50\u7ed3\u6784\u7684\u542b\u4e49\uff1a\u539f\u95ee\u9898\u7684\u6700\u4f18\u89e3\u662f\u4ece\u5b50\u95ee\u9898\u7684\u6700\u4f18\u89e3\u6784\u5efa\u5f97\u6765\u7684\u3002
\u672c\u9898\u663e\u7136\u5177\u6709\u6700\u4f18\u5b50\u7ed3\u6784\uff1a\u6211\u4eec\u4ece\u4e24\u4e2a\u5b50\u95ee\u9898\u6700\u4f18\u89e3 \\(dp[i-1]\\) \u548c \\(dp[i-2]\\) \u4e2d\u6311\u9009\u51fa\u8f83\u4f18\u7684\u90a3\u4e00\u4e2a\uff0c\u5e76\u7528\u5b83\u6784\u5efa\u51fa\u539f\u95ee\u9898 \\(dp[i]\\) \u7684\u6700\u4f18\u89e3\u3002
\u90a3\u4e48\uff0c\u4e0a\u4e00\u8282\u7684\u722c\u697c\u68af\u9898\u76ee\u6709\u6ca1\u6709\u6700\u4f18\u5b50\u7ed3\u6784\u5462\uff1f\u5b83\u7684\u76ee\u6807\u662f\u6c42\u89e3\u65b9\u6848\u6570\u91cf\uff0c\u770b\u4f3c\u662f\u4e00\u4e2a\u8ba1\u6570\u95ee\u9898\uff0c\u4f46\u5982\u679c\u6362\u4e00\u79cd\u95ee\u6cd5\uff1a\u201c\u6c42\u89e3\u6700\u5927\u65b9\u6848\u6570\u91cf\u201d\u3002\u6211\u4eec\u610f\u5916\u5730\u53d1\u73b0\uff0c\u867d\u7136\u9898\u76ee\u4fee\u6539\u524d\u540e\u662f\u7b49\u4ef7\u7684\uff0c\u4f46\u6700\u4f18\u5b50\u7ed3\u6784\u6d6e\u73b0\u51fa\u6765\u4e86\uff1a\u7b2c \\(n\\) \u9636\u6700\u5927\u65b9\u6848\u6570\u91cf\u7b49\u4e8e\u7b2c \\(n-1\\) \u9636\u548c\u7b2c \\(n-2\\) \u9636\u6700\u5927\u65b9\u6848\u6570\u91cf\u4e4b\u548c\u3002\u6240\u4ee5\u8bf4\uff0c\u6700\u4f18\u5b50\u7ed3\u6784\u7684\u89e3\u91ca\u65b9\u5f0f\u6bd4\u8f83\u7075\u6d3b\uff0c\u5728\u4e0d\u540c\u95ee\u9898\u4e2d\u4f1a\u6709\u4e0d\u540c\u7684\u542b\u4e49\u3002
\u6839\u636e\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\uff0c\u4ee5\u53ca\u521d\u59cb\u72b6\u6001 \\(dp[1] = cost[1]\\) \u548c \\(dp[2] = cost[2]\\) \uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5f97\u5230\u52a8\u6001\u89c4\u5212\u4ee3\u7801\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig min_cost_climbing_stairs_dp.pydef min_cost_climbing_stairs_dp(cost: list[int]) -> int:\n \"\"\"\u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212\"\"\"\n n = len(cost) - 1\n if n == 1 or n == 2:\n return cost[n]\n # \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n dp = [0] * (n + 1)\n # \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n dp[1], dp[2] = cost[1], cost[2]\n # \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n for i in range(3, n + 1):\n dp[i] = min(dp[i - 1], dp[i - 2]) + cost[i]\n return dp[n]\n
min_cost_climbing_stairs_dp.cpp/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212 */\nint minCostClimbingStairsDP(vector<int> &cost) {\n int n = cost.size() - 1;\n if (n == 1 || n == 2)\n return cost[n];\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n vector<int> dp(n + 1);\n // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n dp[1] = cost[1];\n dp[2] = cost[2];\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n for (int i = 3; i <= n; i++) {\n dp[i] = min(dp[i - 1], dp[i - 2]) + cost[i];\n }\n return dp[n];\n}\n
min_cost_climbing_stairs_dp.java/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212 */\nint minCostClimbingStairsDP(int[] cost) {\n int n = cost.length - 1;\n if (n == 1 || n == 2)\n return cost[n];\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n int[] dp = new int[n + 1];\n // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n dp[1] = cost[1];\n dp[2] = cost[2];\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n for (int i = 3; i <= n; i++) {\n dp[i] = Math.min(dp[i - 1], dp[i - 2]) + cost[i];\n }\n return dp[n];\n}\n
min_cost_climbing_stairs_dp.cs/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212 */\nint MinCostClimbingStairsDP(int[] cost) {\n int n = cost.Length - 1;\n if (n == 1 || n == 2)\n return cost[n];\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n int[] dp = new int[n + 1];\n // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n dp[1] = cost[1];\n dp[2] = cost[2];\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n for (int i = 3; i <= n; i++) {\n dp[i] = Math.Min(dp[i - 1], dp[i - 2]) + cost[i];\n }\n return dp[n];\n}\n
min_cost_climbing_stairs_dp.go/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc minCostClimbingStairsDP(cost []int) int {\n n := len(cost) - 1\n if n == 1 || n == 2 {\n return cost[n]\n }\n min := func(a, b int) int {\n if a < b {\n return a\n }\n return b\n }\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n dp := make([]int, n+1)\n // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n dp[1] = cost[1]\n dp[2] = cost[2]\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n for i := 3; i <= n; i++ {\n dp[i] = min(dp[i-1], dp[i-2]) + cost[i]\n }\n return dp[n]\n}\n
min_cost_climbing_stairs_dp.swift/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc minCostClimbingStairsDP(cost: [Int]) -> Int {\n let n = cost.count - 1\n if n == 1 || n == 2 {\n return cost[n]\n }\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n var dp = Array(repeating: 0, count: n + 1)\n // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n dp[1] = cost[1]\n dp[2] = cost[2]\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n for i in stride(from: 3, through: n, by: 1) {\n dp[i] = min(dp[i - 1], dp[i - 2]) + cost[i]\n }\n return dp[n]\n}\n
min_cost_climbing_stairs_dp.js/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction minCostClimbingStairsDP(cost) {\n const n = cost.length - 1;\n if (n === 1 || n === 2) {\n return cost[n];\n }\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n const dp = new Array(n + 1);\n // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n dp[1] = cost[1];\n dp[2] = cost[2];\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n for (let i = 3; i <= n; i++) {\n dp[i] = Math.min(dp[i - 1], dp[i - 2]) + cost[i];\n }\n return dp[n];\n}\n
min_cost_climbing_stairs_dp.ts/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction minCostClimbingStairsDP(cost: Array<number>): number {\n const n = cost.length - 1;\n if (n === 1 || n === 2) {\n return cost[n];\n }\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n const dp = new Array(n + 1);\n // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n dp[1] = cost[1];\n dp[2] = cost[2];\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n for (let i = 3; i <= n; i++) {\n dp[i] = Math.min(dp[i - 1], dp[i - 2]) + cost[i];\n }\n return dp[n];\n}\n
min_cost_climbing_stairs_dp.dart/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212 */\nint minCostClimbingStairsDP(List<int> cost) {\n int n = cost.length - 1;\n if (n == 1 || n == 2) return cost[n];\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n List<int> dp = List.filled(n + 1, 0);\n // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n dp[1] = cost[1];\n dp[2] = cost[2];\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n for (int i = 3; i <= n; i++) {\n dp[i] = min(dp[i - 1], dp[i - 2]) + cost[i];\n }\n return dp[n];\n}\n
min_cost_climbing_stairs_dp.rs/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212 */\nfn min_cost_climbing_stairs_dp(cost: &[i32]) -> i32 {\n let n = cost.len() - 1;\n if n == 1 || n == 2 { return cost[n]; }\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n let mut dp = vec![-1; n + 1];\n // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n dp[1] = cost[1];\n dp[2] = cost[2];\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n for i in 3..=n {\n dp[i] = cmp::min(dp[i - 1], dp[i - 2]) + cost[i];\n }\n dp[n]\n}\n
min_cost_climbing_stairs_dp.c/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212 */\nint minCostClimbingStairsDP(int cost[], int costSize) {\n int n = costSize - 1;\n if (n == 1 || n == 2)\n return cost[n];\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n int *dp = calloc(n + 1, sizeof(int));\n // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n dp[1] = cost[1];\n dp[2] = cost[2];\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n for (int i = 3; i <= n; i++) {\n dp[i] = myMin(dp[i - 1], dp[i - 2]) + cost[i];\n }\n int res = dp[n];\n // \u91ca\u653e\u5185\u5b58\n free(dp);\n return res;\n}\n
min_cost_climbing_stairs_dp.zig// \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212\nfn minCostClimbingStairsDP(comptime cost: []i32) i32 {\n comptime var n = cost.len - 1;\n if (n == 1 or n == 2) {\n return cost[n];\n }\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n var dp = [_]i32{-1} ** (n + 1);\n // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n dp[1] = cost[1];\n dp[2] = cost[2];\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n for (3..n + 1) |i| {\n dp[i] = @min(dp[i - 1], dp[i - 2]) + cost[i];\n }\n return dp[n];\n}\n
\u56fe 14-7 \u5c55\u793a\u4e86\u4ee5\u4e0a\u4ee3\u7801\u7684\u52a8\u6001\u89c4\u5212\u8fc7\u7a0b\u3002
\u56fe 14-7 \u00a0 \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\u7684\u52a8\u6001\u89c4\u5212\u8fc7\u7a0b
\u672c\u9898\u4e5f\u53ef\u4ee5\u8fdb\u884c\u7a7a\u95f4\u4f18\u5316\uff0c\u5c06\u4e00\u7ef4\u538b\u7f29\u81f3\u96f6\u7ef4\uff0c\u4f7f\u5f97\u7a7a\u95f4\u590d\u6742\u5ea6\u4ece \\(O(n)\\) \u964d\u81f3 \\(O(1)\\) \uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig min_cost_climbing_stairs_dp.pydef min_cost_climbing_stairs_dp_comp(cost: list[int]) -> int:\n \"\"\"\u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\"\"\"\n n = len(cost) - 1\n if n == 1 or n == 2:\n return cost[n]\n a, b = cost[1], cost[2]\n for i in range(3, n + 1):\n a, b = b, min(a, b) + cost[i]\n return b\n
min_cost_climbing_stairs_dp.cpp/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint minCostClimbingStairsDPComp(vector<int> &cost) {\n int n = cost.size() - 1;\n if (n == 1 || n == 2)\n return cost[n];\n int a = cost[1], b = cost[2];\n for (int i = 3; i <= n; i++) {\n int tmp = b;\n b = min(a, tmp) + cost[i];\n a = tmp;\n }\n return b;\n}\n
min_cost_climbing_stairs_dp.java/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint minCostClimbingStairsDPComp(int[] cost) {\n int n = cost.length - 1;\n if (n == 1 || n == 2)\n return cost[n];\n int a = cost[1], b = cost[2];\n for (int i = 3; i <= n; i++) {\n int tmp = b;\n b = Math.min(a, tmp) + cost[i];\n a = tmp;\n }\n return b;\n}\n
min_cost_climbing_stairs_dp.cs/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint MinCostClimbingStairsDPComp(int[] cost) {\n int n = cost.Length - 1;\n if (n == 1 || n == 2)\n return cost[n];\n int a = cost[1], b = cost[2];\n for (int i = 3; i <= n; i++) {\n int tmp = b;\n b = Math.Min(a, tmp) + cost[i];\n a = tmp;\n }\n return b;\n}\n
min_cost_climbing_stairs_dp.go/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc minCostClimbingStairsDPComp(cost []int) int {\n n := len(cost) - 1\n if n == 1 || n == 2 {\n return cost[n]\n }\n min := func(a, b int) int {\n if a < b {\n return a\n }\n return b\n }\n // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n a, b := cost[1], cost[2]\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n for i := 3; i <= n; i++ {\n tmp := b\n b = min(a, tmp) + cost[i]\n a = tmp\n }\n return b\n}\n
min_cost_climbing_stairs_dp.swift/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc minCostClimbingStairsDPComp(cost: [Int]) -> Int {\n let n = cost.count - 1\n if n == 1 || n == 2 {\n return cost[n]\n }\n var (a, b) = (cost[1], cost[2])\n for i in stride(from: 3, through: n, by: 1) {\n (a, b) = (b, min(a, b) + cost[i])\n }\n return b\n}\n
min_cost_climbing_stairs_dp.js/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction minCostClimbingStairsDPComp(cost) {\n const n = cost.length - 1;\n if (n === 1 || n === 2) {\n return cost[n];\n }\n let a = cost[1],\n b = cost[2];\n for (let i = 3; i <= n; i++) {\n const tmp = b;\n b = Math.min(a, tmp) + cost[i];\n a = tmp;\n }\n return b;\n}\n
min_cost_climbing_stairs_dp.ts/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction minCostClimbingStairsDPComp(cost: Array<number>): number {\n const n = cost.length - 1;\n if (n === 1 || n === 2) {\n return cost[n];\n }\n let a = cost[1],\n b = cost[2];\n for (let i = 3; i <= n; i++) {\n const tmp = b;\n b = Math.min(a, tmp) + cost[i];\n a = tmp;\n }\n return b;\n}\n
min_cost_climbing_stairs_dp.dart/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint minCostClimbingStairsDPComp(List<int> cost) {\n int n = cost.length - 1;\n if (n == 1 || n == 2) return cost[n];\n int a = cost[1], b = cost[2];\n for (int i = 3; i <= n; i++) {\n int tmp = b;\n b = min(a, tmp) + cost[i];\n a = tmp;\n }\n return b;\n}\n
min_cost_climbing_stairs_dp.rs/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfn min_cost_climbing_stairs_dp_comp(cost: &[i32]) -> i32 {\n let n = cost.len() - 1;\n if n == 1 || n == 2 { return cost[n] };\n let (mut a, mut b) = (cost[1], cost[2]);\n for i in 3..=n {\n let tmp = b;\n b = cmp::min(a, tmp) + cost[i];\n a = tmp;\n }\n b\n}\n
min_cost_climbing_stairs_dp.c/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint minCostClimbingStairsDPComp(int cost[], int costSize) {\n int n = costSize - 1;\n if (n == 1 || n == 2)\n return cost[n];\n int a = cost[1], b = cost[2];\n for (int i = 3; i <= n; i++) {\n int tmp = b;\n b = myMin(a, tmp) + cost[i];\n a = tmp;\n }\n return b;\n}\n
min_cost_climbing_stairs_dp.zig// \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\nfn minCostClimbingStairsDPComp(cost: []i32) i32 {\n var n = cost.len - 1;\n if (n == 1 or n == 2) {\n return cost[n];\n }\n var a = cost[1];\n var b = cost[2];\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n for (3..n + 1) |i| {\n var tmp = b;\n b = @min(a, tmp) + cost[i];\n a = tmp;\n }\n return b;\n}\n
"},{"location":"chapter_dynamic_programming/dp_problem_features/#1422","title":"14.2.2 \u00a0 \u65e0\u540e\u6548\u6027","text":"\u65e0\u540e\u6548\u6027\u662f\u52a8\u6001\u89c4\u5212\u80fd\u591f\u6709\u6548\u89e3\u51b3\u95ee\u9898\u7684\u91cd\u8981\u7279\u6027\u4e4b\u4e00\uff0c\u5176\u5b9a\u4e49\u4e3a\uff1a\u7ed9\u5b9a\u4e00\u4e2a\u786e\u5b9a\u7684\u72b6\u6001\uff0c\u5b83\u7684\u672a\u6765\u53d1\u5c55\u53ea\u4e0e\u5f53\u524d\u72b6\u6001\u6709\u5173\uff0c\u800c\u4e0e\u8fc7\u53bb\u7ecf\u5386\u7684\u6240\u6709\u72b6\u6001\u65e0\u5173\u3002
\u4ee5\u722c\u697c\u68af\u95ee\u9898\u4e3a\u4f8b\uff0c\u7ed9\u5b9a\u72b6\u6001 \\(i\\) \uff0c\u5b83\u4f1a\u53d1\u5c55\u51fa\u72b6\u6001 \\(i+1\\) \u548c\u72b6\u6001 \\(i+2\\) \uff0c\u5206\u522b\u5bf9\u5e94\u8df3 \\(1\\) \u6b65\u548c\u8df3 \\(2\\) \u6b65\u3002\u5728\u505a\u51fa\u8fd9\u4e24\u79cd\u9009\u62e9\u65f6\uff0c\u6211\u4eec\u65e0\u987b\u8003\u8651\u72b6\u6001 \\(i\\) \u4e4b\u524d\u7684\u72b6\u6001\uff0c\u5b83\u4eec\u5bf9\u72b6\u6001 \\(i\\) \u7684\u672a\u6765\u6ca1\u6709\u5f71\u54cd\u3002
\u7136\u800c\uff0c\u5982\u679c\u6211\u4eec\u7ed9\u722c\u697c\u68af\u95ee\u9898\u6dfb\u52a0\u4e00\u4e2a\u7ea6\u675f\uff0c\u60c5\u51b5\u5c31\u4e0d\u4e00\u6837\u4e86\u3002
\u5e26\u7ea6\u675f\u722c\u697c\u68af
\u7ed9\u5b9a\u4e00\u4e2a\u5171\u6709 \\(n\\) \u9636\u7684\u697c\u68af\uff0c\u4f60\u6bcf\u6b65\u53ef\u4ee5\u4e0a \\(1\\) \u9636\u6216\u8005 \\(2\\) \u9636\uff0c\u4f46\u4e0d\u80fd\u8fde\u7eed\u4e24\u8f6e\u8df3 \\(1\\) \u9636\uff0c\u8bf7\u95ee\u6709\u591a\u5c11\u79cd\u65b9\u6848\u53ef\u4ee5\u722c\u5230\u697c\u9876\uff1f
\u5982\u56fe 14-8 \u6240\u793a\uff0c\u722c\u4e0a\u7b2c \\(3\\) \u9636\u4ec5\u5269 \\(2\\) \u79cd\u53ef\u884c\u65b9\u6848\uff0c\u5176\u4e2d\u8fde\u7eed\u4e09\u6b21\u8df3 \\(1\\) \u9636\u7684\u65b9\u6848\u4e0d\u6ee1\u8db3\u7ea6\u675f\u6761\u4ef6\uff0c\u56e0\u6b64\u88ab\u820d\u5f03\u3002
\u56fe 14-8 \u00a0 \u5e26\u7ea6\u675f\u722c\u5230\u7b2c 3 \u9636\u7684\u65b9\u6848\u6570\u91cf
\u5728\u8be5\u95ee\u9898\u4e2d\uff0c\u5982\u679c\u4e0a\u4e00\u8f6e\u662f\u8df3 \\(1\\) \u9636\u4e0a\u6765\u7684\uff0c\u90a3\u4e48\u4e0b\u4e00\u8f6e\u5c31\u5fc5\u987b\u8df3 \\(2\\) \u9636\u3002\u8fd9\u610f\u5473\u7740\uff0c\u4e0b\u4e00\u6b65\u9009\u62e9\u4e0d\u80fd\u7531\u5f53\u524d\u72b6\u6001\uff08\u5f53\u524d\u6240\u5728\u697c\u68af\u9636\u6570\uff09\u72ec\u7acb\u51b3\u5b9a\uff0c\u8fd8\u548c\u524d\u4e00\u4e2a\u72b6\u6001\uff08\u4e0a\u8f6e\u6240\u5728\u697c\u68af\u9636\u6570\uff09\u6709\u5173\u3002
\u4e0d\u96be\u53d1\u73b0\uff0c\u6b64\u95ee\u9898\u5df2\u4e0d\u6ee1\u8db3\u65e0\u540e\u6548\u6027\uff0c\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b \\(dp[i] = dp[i-1] + dp[i-2]\\) \u4e5f\u5931\u6548\u4e86\uff0c\u56e0\u4e3a \\(dp[i-1]\\) \u4ee3\u8868\u672c\u8f6e\u8df3 \\(1\\) \u9636\uff0c\u4f46\u5176\u4e2d\u5305\u542b\u4e86\u8bb8\u591a\u201c\u4e0a\u4e00\u8f6e\u662f\u8df3 \\(1\\) \u9636\u4e0a\u6765\u7684\u201d\u65b9\u6848\uff0c\u800c\u4e3a\u4e86\u6ee1\u8db3\u7ea6\u675f\uff0c\u6211\u4eec\u5c31\u4e0d\u80fd\u5c06 \\(dp[i-1]\\) \u76f4\u63a5\u8ba1\u5165 \\(dp[i]\\) \u4e2d\u3002
\u4e3a\u6b64\uff0c\u6211\u4eec\u9700\u8981\u6269\u5c55\u72b6\u6001\u5b9a\u4e49\uff1a\u72b6\u6001 \\([i, j]\\) \u8868\u793a\u5904\u5728\u7b2c \\(i\\) \u9636\u5e76\u4e14\u4e0a\u4e00\u8f6e\u8df3\u4e86 \\(j\\) \u9636\uff0c\u5176\u4e2d \\(j \\in \\{1, 2\\}\\) \u3002\u6b64\u72b6\u6001\u5b9a\u4e49\u6709\u6548\u5730\u533a\u5206\u4e86\u4e0a\u4e00\u8f6e\u8df3\u4e86 \\(1\\) \u9636\u8fd8\u662f \\(2\\) \u9636\uff0c\u6211\u4eec\u53ef\u4ee5\u636e\u6b64\u5224\u65ad\u5f53\u524d\u72b6\u6001\u662f\u4ece\u4f55\u800c\u6765\u7684\u3002
\u5982\u56fe 14-9 \u6240\u793a\uff0c\u5728\u8be5\u5b9a\u4e49\u4e0b\uff0c\\(dp[i, j]\\) \u8868\u793a\u72b6\u6001 \\([i, j]\\) \u5bf9\u5e94\u7684\u65b9\u6848\u6570\u3002\u6b64\u65f6\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u4e3a\uff1a
\\[ \\begin{cases} dp[i, 1] = dp[i-1, 2] \\\\ dp[i, 2] = dp[i-2, 1] + dp[i-2, 2] \\end{cases} \\]\u56fe 14-9 \u00a0 \u8003\u8651\u7ea6\u675f\u4e0b\u7684\u9012\u63a8\u5173\u7cfb
\u6700\u7ec8\uff0c\u8fd4\u56de \\(dp[n, 1] + dp[n, 2]\\) \u5373\u53ef\uff0c\u4e24\u8005\u4e4b\u548c\u4ee3\u8868\u722c\u5230\u7b2c \\(n\\) \u9636\u7684\u65b9\u6848\u603b\u6570\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig climbing_stairs_constraint_dp.pydef climbing_stairs_constraint_dp(n: int) -> int:\n \"\"\"\u5e26\u7ea6\u675f\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212\"\"\"\n if n == 1 or n == 2:\n return 1\n # \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n dp = [[0] * 3 for _ in range(n + 1)]\n # \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n dp[1][1], dp[1][2] = 1, 0\n dp[2][1], dp[2][2] = 0, 1\n # \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n for i in range(3, n + 1):\n dp[i][1] = dp[i - 1][2]\n dp[i][2] = dp[i - 2][1] + dp[i - 2][2]\n return dp[n][1] + dp[n][2]\n
climbing_stairs_constraint_dp.cpp/* \u5e26\u7ea6\u675f\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nint climbingStairsConstraintDP(int n) {\n if (n == 1 || n == 2) {\n return 1;\n }\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n vector<vector<int>> dp(n + 1, vector<int>(3, 0));\n // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n dp[1][1] = 1;\n dp[1][2] = 0;\n dp[2][1] = 0;\n dp[2][2] = 1;\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n for (int i = 3; i <= n; i++) {\n dp[i][1] = dp[i - 1][2];\n dp[i][2] = dp[i - 2][1] + dp[i - 2][2];\n }\n return dp[n][1] + dp[n][2];\n}\n
climbing_stairs_constraint_dp.java/* \u5e26\u7ea6\u675f\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nint climbingStairsConstraintDP(int n) {\n if (n == 1 || n == 2) {\n return 1;\n }\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n int[][] dp = new int[n + 1][3];\n // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n dp[1][1] = 1;\n dp[1][2] = 0;\n dp[2][1] = 0;\n dp[2][2] = 1;\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n for (int i = 3; i <= n; i++) {\n dp[i][1] = dp[i - 1][2];\n dp[i][2] = dp[i - 2][1] + dp[i - 2][2];\n }\n return dp[n][1] + dp[n][2];\n}\n
climbing_stairs_constraint_dp.cs/* \u5e26\u7ea6\u675f\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nint ClimbingStairsConstraintDP(int n) {\n if (n == 1 || n == 2) {\n return 1;\n }\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n int[,] dp = new int[n + 1, 3];\n // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n dp[1, 1] = 1;\n dp[1, 2] = 0;\n dp[2, 1] = 0;\n dp[2, 2] = 1;\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n for (int i = 3; i <= n; i++) {\n dp[i, 1] = dp[i - 1, 2];\n dp[i, 2] = dp[i - 2, 1] + dp[i - 2, 2];\n }\n return dp[n, 1] + dp[n, 2];\n}\n
climbing_stairs_constraint_dp.go/* \u5e26\u7ea6\u675f\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc climbingStairsConstraintDP(n int) int {\n if n == 1 || n == 2 {\n return 1\n }\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n dp := make([][3]int, n+1)\n // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n dp[1][1] = 1\n dp[1][2] = 0\n dp[2][1] = 0\n dp[2][2] = 1\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n for i := 3; i <= n; i++ {\n dp[i][1] = dp[i-1][2]\n dp[i][2] = dp[i-2][1] + dp[i-2][2]\n }\n return dp[n][1] + dp[n][2]\n}\n
climbing_stairs_constraint_dp.swift/* \u5e26\u7ea6\u675f\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc climbingStairsConstraintDP(n: Int) -> Int {\n if n == 1 || n == 2 {\n return 1\n }\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n var dp = Array(repeating: Array(repeating: 0, count: 3), count: n + 1)\n // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n dp[1][1] = 1\n dp[1][2] = 0\n dp[2][1] = 0\n dp[2][2] = 1\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n for i in stride(from: 3, through: n, by: 1) {\n dp[i][1] = dp[i - 1][2]\n dp[i][2] = dp[i - 2][1] + dp[i - 2][2]\n }\n return dp[n][1] + dp[n][2]\n}\n
climbing_stairs_constraint_dp.js/* \u5e26\u7ea6\u675f\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction climbingStairsConstraintDP(n) {\n if (n === 1 || n === 2) {\n return 1;\n }\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n const dp = Array.from(new Array(n + 1), () => new Array(3));\n // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n dp[1][1] = 1;\n dp[1][2] = 0;\n dp[2][1] = 0;\n dp[2][2] = 1;\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n for (let i = 3; i <= n; i++) {\n dp[i][1] = dp[i - 1][2];\n dp[i][2] = dp[i - 2][1] + dp[i - 2][2];\n }\n return dp[n][1] + dp[n][2];\n}\n
climbing_stairs_constraint_dp.ts/* \u5e26\u7ea6\u675f\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction climbingStairsConstraintDP(n: number): number {\n if (n === 1 || n === 2) {\n return 1;\n }\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n const dp = Array.from({ length: n + 1 }, () => new Array(3));\n // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n dp[1][1] = 1;\n dp[1][2] = 0;\n dp[2][1] = 0;\n dp[2][2] = 1;\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n for (let i = 3; i <= n; i++) {\n dp[i][1] = dp[i - 1][2];\n dp[i][2] = dp[i - 2][1] + dp[i - 2][2];\n }\n return dp[n][1] + dp[n][2];\n}\n
climbing_stairs_constraint_dp.dart/* \u5e26\u7ea6\u675f\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nint climbingStairsConstraintDP(int n) {\n if (n == 1 || n == 2) {\n return 1;\n }\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n List<List<int>> dp = List.generate(n + 1, (index) => List.filled(3, 0));\n // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n dp[1][1] = 1;\n dp[1][2] = 0;\n dp[2][1] = 0;\n dp[2][2] = 1;\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n for (int i = 3; i <= n; i++) {\n dp[i][1] = dp[i - 1][2];\n dp[i][2] = dp[i - 2][1] + dp[i - 2][2];\n }\n return dp[n][1] + dp[n][2];\n}\n
climbing_stairs_constraint_dp.rs/* \u5e26\u7ea6\u675f\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nfn climbing_stairs_constraint_dp(n: usize) -> i32 {\n if n == 1 || n == 2 { return 1 };\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n let mut dp = vec![vec![-1; 3]; n + 1];\n // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n dp[1][1] = 1;\n dp[1][2] = 0;\n dp[2][1] = 0;\n dp[2][2] = 1;\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n for i in 3..=n {\n dp[i][1] = dp[i - 1][2];\n dp[i][2] = dp[i - 2][1] + dp[i - 2][2];\n }\n dp[n][1] + dp[n][2]\n}\n
climbing_stairs_constraint_dp.c/* \u5e26\u7ea6\u675f\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nint climbingStairsConstraintDP(int n) {\n if (n == 1 || n == 2) {\n return 1;\n }\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n int **dp = malloc((n + 1) * sizeof(int *));\n for (int i = 0; i <= n; i++) {\n dp[i] = calloc(3, sizeof(int));\n }\n // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n dp[1][1] = 1;\n dp[1][2] = 0;\n dp[2][1] = 0;\n dp[2][2] = 1;\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n for (int i = 3; i <= n; i++) {\n dp[i][1] = dp[i - 1][2];\n dp[i][2] = dp[i - 2][1] + dp[i - 2][2];\n }\n int res = dp[n][1] + dp[n][2];\n // \u91ca\u653e\u5185\u5b58\n for (int i = 0; i <= n; i++) {\n free(dp[i]);\n }\n free(dp);\n return res;\n}\n
climbing_stairs_constraint_dp.zig// \u5e26\u7ea6\u675f\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212\nfn climbingStairsConstraintDP(comptime n: usize) i32 {\n if (n == 1 or n == 2) {\n return 1;\n }\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n var dp = [_][3]i32{ [_]i32{ -1, -1, -1 } } ** (n + 1);\n // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n dp[1][1] = 1;\n dp[1][2] = 0;\n dp[2][1] = 0;\n dp[2][2] = 1;\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n for (3..n + 1) |i| {\n dp[i][1] = dp[i - 1][2];\n dp[i][2] = dp[i - 2][1] + dp[i - 2][2];\n }\n return dp[n][1] + dp[n][2];\n}\n
\u5728\u4e0a\u9762\u7684\u6848\u4f8b\u4e2d\uff0c\u7531\u4e8e\u4ec5\u9700\u591a\u8003\u8651\u524d\u9762\u4e00\u4e2a\u72b6\u6001\uff0c\u56e0\u6b64\u6211\u4eec\u4ecd\u7136\u53ef\u4ee5\u901a\u8fc7\u6269\u5c55\u72b6\u6001\u5b9a\u4e49\uff0c\u4f7f\u5f97\u95ee\u9898\u91cd\u65b0\u6ee1\u8db3\u65e0\u540e\u6548\u6027\u3002\u7136\u800c\uff0c\u67d0\u4e9b\u95ee\u9898\u5177\u6709\u975e\u5e38\u4e25\u91cd\u7684\u201c\u6709\u540e\u6548\u6027\u201d\u3002
\u722c\u697c\u68af\u4e0e\u969c\u788d\u751f\u6210
\u7ed9\u5b9a\u4e00\u4e2a\u5171\u6709 \\(n\\) \u9636\u7684\u697c\u68af\uff0c\u4f60\u6bcf\u6b65\u53ef\u4ee5\u4e0a \\(1\\) \u9636\u6216\u8005 \\(2\\) \u9636\u3002\u89c4\u5b9a\u5f53\u722c\u5230\u7b2c \\(i\\) \u9636\u65f6\uff0c\u7cfb\u7edf\u81ea\u52a8\u4f1a\u5728\u7b2c \\(2i\\) \u9636\u4e0a\u653e\u4e0a\u969c\u788d\u7269\uff0c\u4e4b\u540e\u6240\u6709\u8f6e\u90fd\u4e0d\u5141\u8bb8\u8df3\u5230\u7b2c \\(2i\\) \u9636\u4e0a\u3002\u4f8b\u5982\uff0c\u524d\u4e24\u8f6e\u5206\u522b\u8df3\u5230\u4e86\u7b2c \\(2\\)\u3001\\(3\\) \u9636\u4e0a\uff0c\u5219\u4e4b\u540e\u5c31\u4e0d\u80fd\u8df3\u5230\u7b2c \\(4\\)\u3001\\(6\\) \u9636\u4e0a\u3002\u8bf7\u95ee\u6709\u591a\u5c11\u79cd\u65b9\u6848\u53ef\u4ee5\u722c\u5230\u697c\u9876\uff1f
\u5728\u8fd9\u4e2a\u95ee\u9898\u4e2d\uff0c\u4e0b\u6b21\u8df3\u8dc3\u4f9d\u8d56\u8fc7\u53bb\u6240\u6709\u7684\u72b6\u6001\uff0c\u56e0\u4e3a\u6bcf\u4e00\u6b21\u8df3\u8dc3\u90fd\u4f1a\u5728\u66f4\u9ad8\u7684\u9636\u68af\u4e0a\u8bbe\u7f6e\u969c\u788d\uff0c\u5e76\u5f71\u54cd\u672a\u6765\u7684\u8df3\u8dc3\u3002\u5bf9\u4e8e\u8fd9\u7c7b\u95ee\u9898\uff0c\u52a8\u6001\u89c4\u5212\u5f80\u5f80\u96be\u4ee5\u89e3\u51b3\u3002
\u5b9e\u9645\u4e0a\uff0c\u8bb8\u591a\u590d\u6742\u7684\u7ec4\u5408\u4f18\u5316\u95ee\u9898\uff08\u4f8b\u5982\u65c5\u884c\u5546\u95ee\u9898\uff09\u4e0d\u6ee1\u8db3\u65e0\u540e\u6548\u6027\u3002\u5bf9\u4e8e\u8fd9\u7c7b\u95ee\u9898\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u9009\u62e9\u4f7f\u7528\u5176\u4ed6\u65b9\u6cd5\uff0c\u4f8b\u5982\u542f\u53d1\u5f0f\u641c\u7d22\u3001\u9057\u4f20\u7b97\u6cd5\u3001\u5f3a\u5316\u5b66\u4e60\u7b49\uff0c\u4ece\u800c\u5728\u6709\u9650\u65f6\u95f4\u5185\u5f97\u5230\u53ef\u7528\u7684\u5c40\u90e8\u6700\u4f18\u89e3\u3002
"},{"location":"chapter_dynamic_programming/dp_solution_pipeline/","title":"14.3 \u00a0 \u52a8\u6001\u89c4\u5212\u89e3\u9898\u601d\u8def","text":"\u4e0a\u4e24\u8282\u4ecb\u7ecd\u4e86\u52a8\u6001\u89c4\u5212\u95ee\u9898\u7684\u4e3b\u8981\u7279\u5f81\uff0c\u63a5\u4e0b\u6765\u6211\u4eec\u4e00\u8d77\u63a2\u7a76\u4e24\u4e2a\u66f4\u52a0\u5b9e\u7528\u7684\u95ee\u9898\u3002
\u603b\u7684\u6765\u8bf4\uff0c\u5982\u679c\u4e00\u4e2a\u95ee\u9898\u5305\u542b\u91cd\u53e0\u5b50\u95ee\u9898\u3001\u6700\u4f18\u5b50\u7ed3\u6784\uff0c\u5e76\u6ee1\u8db3\u65e0\u540e\u6548\u6027\uff0c\u90a3\u4e48\u5b83\u901a\u5e38\u9002\u5408\u7528\u52a8\u6001\u89c4\u5212\u6c42\u89e3\u3002\u7136\u800c\uff0c\u6211\u4eec\u5f88\u96be\u4ece\u95ee\u9898\u63cf\u8ff0\u4e2d\u76f4\u63a5\u63d0\u53d6\u51fa\u8fd9\u4e9b\u7279\u6027\u3002\u56e0\u6b64\u6211\u4eec\u901a\u5e38\u4f1a\u653e\u5bbd\u6761\u4ef6\uff0c\u5148\u89c2\u5bdf\u95ee\u9898\u662f\u5426\u9002\u5408\u4f7f\u7528\u56de\u6eaf\uff08\u7a77\u4e3e\uff09\u89e3\u51b3\u3002
\u9002\u5408\u7528\u56de\u6eaf\u89e3\u51b3\u7684\u95ee\u9898\u901a\u5e38\u6ee1\u8db3\u201c\u51b3\u7b56\u6811\u6a21\u578b\u201d\uff0c\u8fd9\u79cd\u95ee\u9898\u53ef\u4ee5\u4f7f\u7528\u6811\u5f62\u7ed3\u6784\u6765\u63cf\u8ff0\uff0c\u5176\u4e2d\u6bcf\u4e00\u4e2a\u8282\u70b9\u4ee3\u8868\u4e00\u4e2a\u51b3\u7b56\uff0c\u6bcf\u4e00\u6761\u8def\u5f84\u4ee3\u8868\u4e00\u4e2a\u51b3\u7b56\u5e8f\u5217\u3002
\u6362\u53e5\u8bdd\u8bf4\uff0c\u5982\u679c\u95ee\u9898\u5305\u542b\u660e\u786e\u7684\u51b3\u7b56\u6982\u5ff5\uff0c\u5e76\u4e14\u89e3\u662f\u901a\u8fc7\u4e00\u7cfb\u5217\u51b3\u7b56\u4ea7\u751f\u7684\uff0c\u90a3\u4e48\u5b83\u5c31\u6ee1\u8db3\u51b3\u7b56\u6811\u6a21\u578b\uff0c\u901a\u5e38\u53ef\u4ee5\u4f7f\u7528\u56de\u6eaf\u6765\u89e3\u51b3\u3002
\u5728\u6b64\u57fa\u7840\u4e0a\uff0c\u52a8\u6001\u89c4\u5212\u95ee\u9898\u8fd8\u6709\u4e00\u4e9b\u5224\u65ad\u7684\u201c\u52a0\u5206\u9879\u201d\u3002
\u76f8\u5e94\u5730\uff0c\u4e5f\u5b58\u5728\u4e00\u4e9b\u201c\u51cf\u5206\u9879\u201d\u3002
\u5982\u679c\u4e00\u4e2a\u95ee\u9898\u6ee1\u8db3\u51b3\u7b56\u6811\u6a21\u578b\uff0c\u5e76\u5177\u6709\u8f83\u4e3a\u660e\u663e\u7684\u201c\u52a0\u5206\u9879\u201c\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5047\u8bbe\u5b83\u662f\u4e00\u4e2a\u52a8\u6001\u89c4\u5212\u95ee\u9898\uff0c\u5e76\u5728\u6c42\u89e3\u8fc7\u7a0b\u4e2d\u9a8c\u8bc1\u5b83\u3002
"},{"location":"chapter_dynamic_programming/dp_solution_pipeline/#1432","title":"14.3.2 \u00a0 \u95ee\u9898\u6c42\u89e3\u6b65\u9aa4","text":"\u52a8\u6001\u89c4\u5212\u7684\u89e3\u9898\u6d41\u7a0b\u4f1a\u56e0\u95ee\u9898\u7684\u6027\u8d28\u548c\u96be\u5ea6\u800c\u6709\u6240\u4e0d\u540c\uff0c\u4f46\u901a\u5e38\u9075\u5faa\u4ee5\u4e0b\u6b65\u9aa4\uff1a\u63cf\u8ff0\u51b3\u7b56\uff0c\u5b9a\u4e49\u72b6\u6001\uff0c\u5efa\u7acb \\(dp\\) \u8868\uff0c\u63a8\u5bfc\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\uff0c\u786e\u5b9a\u8fb9\u754c\u6761\u4ef6\u7b49\u3002
\u4e3a\u4e86\u66f4\u5f62\u8c61\u5730\u5c55\u793a\u89e3\u9898\u6b65\u9aa4\uff0c\u6211\u4eec\u4f7f\u7528\u4e00\u4e2a\u7ecf\u5178\u95ee\u9898\u201c\u6700\u5c0f\u8def\u5f84\u548c\u201d\u6765\u4e3e\u4f8b\u3002
Question
\u7ed9\u5b9a\u4e00\u4e2a \\(n \\times m\\) \u7684\u4e8c\u7ef4\u7f51\u683c grid
\uff0c\u7f51\u683c\u4e2d\u7684\u6bcf\u4e2a\u5355\u5143\u683c\u5305\u542b\u4e00\u4e2a\u975e\u8d1f\u6574\u6570\uff0c\u8868\u793a\u8be5\u5355\u5143\u683c\u7684\u4ee3\u4ef7\u3002\u673a\u5668\u4eba\u4ee5\u5de6\u4e0a\u89d2\u5355\u5143\u683c\u4e3a\u8d77\u59cb\u70b9\uff0c\u6bcf\u6b21\u53ea\u80fd\u5411\u4e0b\u6216\u8005\u5411\u53f3\u79fb\u52a8\u4e00\u6b65\uff0c\u76f4\u81f3\u5230\u8fbe\u53f3\u4e0b\u89d2\u5355\u5143\u683c\u3002\u8bf7\u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230\u53f3\u4e0b\u89d2\u7684\u6700\u5c0f\u8def\u5f84\u548c\u3002
\u56fe 14-10 \u5c55\u793a\u4e86\u4e00\u4e2a\u4f8b\u5b50\uff0c\u7ed9\u5b9a\u7f51\u683c\u7684\u6700\u5c0f\u8def\u5f84\u548c\u4e3a \\(13\\) \u3002
\u56fe 14-10 \u00a0 \u6700\u5c0f\u8def\u5f84\u548c\u793a\u4f8b\u6570\u636e
\u7b2c\u4e00\u6b65\uff1a\u601d\u8003\u6bcf\u8f6e\u7684\u51b3\u7b56\uff0c\u5b9a\u4e49\u72b6\u6001\uff0c\u4ece\u800c\u5f97\u5230 \\(dp\\) \u8868
\u672c\u9898\u7684\u6bcf\u4e00\u8f6e\u7684\u51b3\u7b56\u5c31\u662f\u4ece\u5f53\u524d\u683c\u5b50\u5411\u4e0b\u6216\u5411\u53f3\u8d70\u4e00\u6b65\u3002\u8bbe\u5f53\u524d\u683c\u5b50\u7684\u884c\u5217\u7d22\u5f15\u4e3a \\([i, j]\\) \uff0c\u5219\u5411\u4e0b\u6216\u5411\u53f3\u8d70\u4e00\u6b65\u540e\uff0c\u7d22\u5f15\u53d8\u4e3a \\([i+1, j]\\) \u6216 \\([i, j+1]\\) \u3002\u56e0\u6b64\uff0c\u72b6\u6001\u5e94\u5305\u542b\u884c\u7d22\u5f15\u548c\u5217\u7d22\u5f15\u4e24\u4e2a\u53d8\u91cf\uff0c\u8bb0\u4e3a \\([i, j]\\) \u3002
\u72b6\u6001 \\([i, j]\\) \u5bf9\u5e94\u7684\u5b50\u95ee\u9898\u4e3a\uff1a\u4ece\u8d77\u59cb\u70b9 \\([0, 0]\\) \u8d70\u5230 \\([i, j]\\) \u7684\u6700\u5c0f\u8def\u5f84\u548c\uff0c\u89e3\u8bb0\u4e3a \\(dp[i, j]\\) \u3002
\u81f3\u6b64\uff0c\u6211\u4eec\u5c31\u5f97\u5230\u4e86\u56fe 14-11 \u6240\u793a\u7684\u4e8c\u7ef4 \\(dp\\) \u77e9\u9635\uff0c\u5176\u5c3a\u5bf8\u4e0e\u8f93\u5165\u7f51\u683c \\(grid\\) \u76f8\u540c\u3002
\u56fe 14-11 \u00a0 \u72b6\u6001\u5b9a\u4e49\u4e0e dp \u8868
Note
\u52a8\u6001\u89c4\u5212\u548c\u56de\u6eaf\u8fc7\u7a0b\u53ef\u4ee5\u63cf\u8ff0\u4e3a\u4e00\u4e2a\u51b3\u7b56\u5e8f\u5217\uff0c\u800c\u72b6\u6001\u7531\u6240\u6709\u51b3\u7b56\u53d8\u91cf\u6784\u6210\u3002\u5b83\u5e94\u5f53\u5305\u542b\u63cf\u8ff0\u89e3\u9898\u8fdb\u5ea6\u7684\u6240\u6709\u53d8\u91cf\uff0c\u5176\u5305\u542b\u4e86\u8db3\u591f\u7684\u4fe1\u606f\uff0c\u80fd\u591f\u7528\u6765\u63a8\u5bfc\u51fa\u4e0b\u4e00\u4e2a\u72b6\u6001\u3002
\u6bcf\u4e2a\u72b6\u6001\u90fd\u5bf9\u5e94\u4e00\u4e2a\u5b50\u95ee\u9898\uff0c\u6211\u4eec\u4f1a\u5b9a\u4e49\u4e00\u4e2a \\(dp\\) \u8868\u6765\u5b58\u50a8\u6240\u6709\u5b50\u95ee\u9898\u7684\u89e3\uff0c\u72b6\u6001\u7684\u6bcf\u4e2a\u72ec\u7acb\u53d8\u91cf\u90fd\u662f \\(dp\\) \u8868\u7684\u4e00\u4e2a\u7ef4\u5ea6\u3002\u4ece\u672c\u8d28\u4e0a\u770b\uff0c\\(dp\\) \u8868\u662f\u72b6\u6001\u548c\u5b50\u95ee\u9898\u7684\u89e3\u4e4b\u95f4\u7684\u6620\u5c04\u3002
\u7b2c\u4e8c\u6b65\uff1a\u627e\u51fa\u6700\u4f18\u5b50\u7ed3\u6784\uff0c\u8fdb\u800c\u63a8\u5bfc\u51fa\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b
\u5bf9\u4e8e\u72b6\u6001 \\([i, j]\\) \uff0c\u5b83\u53ea\u80fd\u4ece\u4e0a\u8fb9\u683c\u5b50 \\([i-1, j]\\) \u548c\u5de6\u8fb9\u683c\u5b50 \\([i, j-1]\\) \u8f6c\u79fb\u800c\u6765\u3002\u56e0\u6b64\u6700\u4f18\u5b50\u7ed3\u6784\u4e3a\uff1a\u5230\u8fbe \\([i, j]\\) \u7684\u6700\u5c0f\u8def\u5f84\u548c\u7531 \\([i, j-1]\\) \u7684\u6700\u5c0f\u8def\u5f84\u548c\u4e0e \\([i-1, j]\\) \u7684\u6700\u5c0f\u8def\u5f84\u548c\u4e2d\u8f83\u5c0f\u7684\u90a3\u4e00\u4e2a\u51b3\u5b9a\u3002
\u6839\u636e\u4ee5\u4e0a\u5206\u6790\uff0c\u53ef\u63a8\u51fa\u56fe 14-12 \u6240\u793a\u7684\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\uff1a
\\[ dp[i, j] = \\min(dp[i-1, j], dp[i, j-1]) + grid[i, j] \\]\u56fe 14-12 \u00a0 \u6700\u4f18\u5b50\u7ed3\u6784\u4e0e\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b
Note
\u6839\u636e\u5b9a\u4e49\u597d\u7684 \\(dp\\) \u8868\uff0c\u601d\u8003\u539f\u95ee\u9898\u548c\u5b50\u95ee\u9898\u7684\u5173\u7cfb\uff0c\u627e\u51fa\u901a\u8fc7\u5b50\u95ee\u9898\u7684\u6700\u4f18\u89e3\u6765\u6784\u9020\u539f\u95ee\u9898\u7684\u6700\u4f18\u89e3\u7684\u65b9\u6cd5\uff0c\u5373\u6700\u4f18\u5b50\u7ed3\u6784\u3002
\u4e00\u65e6\u6211\u4eec\u627e\u5230\u4e86\u6700\u4f18\u5b50\u7ed3\u6784\uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u6784\u5efa\u51fa\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u3002
\u7b2c\u4e09\u6b65\uff1a\u786e\u5b9a\u8fb9\u754c\u6761\u4ef6\u548c\u72b6\u6001\u8f6c\u79fb\u987a\u5e8f
\u5728\u672c\u9898\u4e2d\uff0c\u5904\u5728\u9996\u884c\u7684\u72b6\u6001\u53ea\u80fd\u4ece\u5176\u5de6\u8fb9\u7684\u72b6\u6001\u5f97\u6765\uff0c\u5904\u5728\u9996\u5217\u7684\u72b6\u6001\u53ea\u80fd\u4ece\u5176\u4e0a\u8fb9\u7684\u72b6\u6001\u5f97\u6765\uff0c\u56e0\u6b64\u9996\u884c \\(i = 0\\) \u548c\u9996\u5217 \\(j = 0\\) \u662f\u8fb9\u754c\u6761\u4ef6\u3002
\u5982\u56fe 14-13 \u6240\u793a\uff0c\u7531\u4e8e\u6bcf\u4e2a\u683c\u5b50\u662f\u7531\u5176\u5de6\u65b9\u683c\u5b50\u548c\u4e0a\u65b9\u683c\u5b50\u8f6c\u79fb\u800c\u6765\uff0c\u56e0\u6b64\u6211\u4eec\u4f7f\u7528\u5faa\u73af\u6765\u904d\u5386\u77e9\u9635\uff0c\u5916\u5faa\u73af\u904d\u5386\u5404\u884c\uff0c\u5185\u5faa\u73af\u904d\u5386\u5404\u5217\u3002
\u56fe 14-13 \u00a0 \u8fb9\u754c\u6761\u4ef6\u4e0e\u72b6\u6001\u8f6c\u79fb\u987a\u5e8f
Note
\u8fb9\u754c\u6761\u4ef6\u5728\u52a8\u6001\u89c4\u5212\u4e2d\u7528\u4e8e\u521d\u59cb\u5316 \\(dp\\) \u8868\uff0c\u5728\u641c\u7d22\u4e2d\u7528\u4e8e\u526a\u679d\u3002
\u72b6\u6001\u8f6c\u79fb\u987a\u5e8f\u7684\u6838\u5fc3\u662f\u8981\u4fdd\u8bc1\u5728\u8ba1\u7b97\u5f53\u524d\u95ee\u9898\u7684\u89e3\u65f6\uff0c\u6240\u6709\u5b83\u4f9d\u8d56\u7684\u66f4\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\u90fd\u5df2\u7ecf\u88ab\u6b63\u786e\u5730\u8ba1\u7b97\u51fa\u6765\u3002
\u6839\u636e\u4ee5\u4e0a\u5206\u6790\uff0c\u6211\u4eec\u5df2\u7ecf\u53ef\u4ee5\u76f4\u63a5\u5199\u51fa\u52a8\u6001\u89c4\u5212\u4ee3\u7801\u3002\u7136\u800c\u5b50\u95ee\u9898\u5206\u89e3\u662f\u4e00\u79cd\u4ece\u9876\u81f3\u5e95\u7684\u601d\u60f3\uff0c\u56e0\u6b64\u6309\u7167\u201c\u66b4\u529b\u641c\u7d22 \\(\\rightarrow\\) \u8bb0\u5fc6\u5316\u641c\u7d22 \\(\\rightarrow\\) \u52a8\u6001\u89c4\u5212\u201d\u7684\u987a\u5e8f\u5b9e\u73b0\u66f4\u52a0\u7b26\u5408\u601d\u7ef4\u4e60\u60ef\u3002
"},{"location":"chapter_dynamic_programming/dp_solution_pipeline/#1","title":"1. \u00a0 \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u641c\u7d22","text":"\u4ece\u72b6\u6001 \\([i, j]\\) \u5f00\u59cb\u641c\u7d22\uff0c\u4e0d\u65ad\u5206\u89e3\u4e3a\u66f4\u5c0f\u7684\u72b6\u6001 \\([i-1, j]\\) \u548c \\([i, j-1]\\) \uff0c\u9012\u5f52\u51fd\u6570\u5305\u62ec\u4ee5\u4e0b\u8981\u7d20\u3002
\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig min_path_sum.pydef min_path_sum_dfs(grid: list[list[int]], i: int, j: int) -> int:\n \"\"\"\u6700\u5c0f\u8def\u5f84\u548c\uff1a\u66b4\u529b\u641c\u7d22\"\"\"\n # \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n if i == 0 and j == 0:\n return grid[0][0]\n # \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n if i < 0 or j < 0:\n return inf\n # \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n up = min_path_sum_dfs(grid, i - 1, j)\n left = min_path_sum_dfs(grid, i, j - 1)\n # \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n return min(left, up) + grid[i][j]\n
min_path_sum.cpp/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u66b4\u529b\u641c\u7d22 */\nint minPathSumDFS(vector<vector<int>> &grid, int i, int j) {\n // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n if (i == 0 && j == 0) {\n return grid[0][0];\n }\n // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n if (i < 0 || j < 0) {\n return INT_MAX;\n }\n // \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n int up = minPathSumDFS(grid, i - 1, j);\n int left = minPathSumDFS(grid, i, j - 1);\n // \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n return min(left, up) != INT_MAX ? min(left, up) + grid[i][j] : INT_MAX;\n}\n
min_path_sum.java/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u66b4\u529b\u641c\u7d22 */\nint minPathSumDFS(int[][] grid, int i, int j) {\n // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n if (i == 0 && j == 0) {\n return grid[0][0];\n }\n // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n if (i < 0 || j < 0) {\n return Integer.MAX_VALUE;\n }\n // \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n int up = minPathSumDFS(grid, i - 1, j);\n int left = minPathSumDFS(grid, i, j - 1);\n // \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n return Math.min(left, up) + grid[i][j];\n}\n
min_path_sum.cs/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u66b4\u529b\u641c\u7d22 */\nint MinPathSumDFS(int[][] grid, int i, int j) {\n // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n if (i == 0 && j == 0) {\n return grid[0][0];\n }\n // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n if (i < 0 || j < 0) {\n return int.MaxValue;\n }\n // \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n int up = MinPathSumDFS(grid, i - 1, j);\n int left = MinPathSumDFS(grid, i, j - 1);\n // \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n return Math.Min(left, up) + grid[i][j];\n}\n
min_path_sum.go/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u66b4\u529b\u641c\u7d22 */\nfunc minPathSumDFS(grid [][]int, i, j int) int {\n // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n if i == 0 && j == 0 {\n return grid[0][0]\n }\n // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n if i < 0 || j < 0 {\n return math.MaxInt\n }\n // \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n up := minPathSumDFS(grid, i-1, j)\n left := minPathSumDFS(grid, i, j-1)\n // \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n return int(math.Min(float64(left), float64(up))) + grid[i][j]\n}\n
min_path_sum.swift/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u66b4\u529b\u641c\u7d22 */\nfunc minPathSumDFS(grid: [[Int]], i: Int, j: Int) -> Int {\n // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n if i == 0, j == 0 {\n return grid[0][0]\n }\n // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n if i < 0 || j < 0 {\n return .max\n }\n // \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n let up = minPathSumDFS(grid: grid, i: i - 1, j: j)\n let left = minPathSumDFS(grid: grid, i: i, j: j - 1)\n // \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n return min(left, up) + grid[i][j]\n}\n
min_path_sum.js/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u66b4\u529b\u641c\u7d22 */\nfunction minPathSumDFS(grid, i, j) {\n // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n if (i === 0 && j === 0) {\n return grid[0][0];\n }\n // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n if (i < 0 || j < 0) {\n return Infinity;\n }\n // \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n const up = minPathSumDFS(grid, i - 1, j);\n const left = minPathSumDFS(grid, i, j - 1);\n // \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n return Math.min(left, up) + grid[i][j];\n}\n
min_path_sum.ts/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u66b4\u529b\u641c\u7d22 */\nfunction minPathSumDFS(\n grid: Array<Array<number>>,\n i: number,\n j: number\n): number {\n // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n if (i === 0 && j == 0) {\n return grid[0][0];\n }\n // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n if (i < 0 || j < 0) {\n return Infinity;\n }\n // \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n const up = minPathSumDFS(grid, i - 1, j);\n const left = minPathSumDFS(grid, i, j - 1);\n // \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n return Math.min(left, up) + grid[i][j];\n}\n
min_path_sum.dart/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u66b4\u529b\u641c\u7d22 */\nint minPathSumDFS(List<List<int>> grid, int i, int j) {\n // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n if (i == 0 && j == 0) {\n return grid[0][0];\n }\n // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n if (i < 0 || j < 0) {\n // \u5728 Dart \u4e2d\uff0cint \u7c7b\u578b\u662f\u56fa\u5b9a\u8303\u56f4\u7684\u6574\u6570\uff0c\u4e0d\u5b58\u5728\u8868\u793a\u201c\u65e0\u7a77\u5927\u201d\u7684\u503c\n return BigInt.from(2).pow(31).toInt();\n }\n // \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n int up = minPathSumDFS(grid, i - 1, j);\n int left = minPathSumDFS(grid, i, j - 1);\n // \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n return min(left, up) + grid[i][j];\n}\n
min_path_sum.rs/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u66b4\u529b\u641c\u7d22 */\nfn min_path_sum_dfs(grid: &Vec<Vec<i32>>, i: i32, j: i32) -> i32 {\n // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n if i == 0 && j == 0 {\n return grid[0][0];\n }\n // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n if i < 0 || j < 0 {\n return i32::MAX;\n }\n // \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n let up = min_path_sum_dfs(grid, i - 1, j);\n let left = min_path_sum_dfs(grid, i, j - 1);\n // \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n std::cmp::min(left, up) + grid[i as usize][j as usize]\n}\n
min_path_sum.c/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u66b4\u529b\u641c\u7d22 */\nint minPathSumDFS(int grid[MAX_SIZE][MAX_SIZE], int i, int j) {\n // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n if (i == 0 && j == 0) {\n return grid[0][0];\n }\n // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n if (i < 0 || j < 0) {\n return INT_MAX;\n }\n // \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n int up = minPathSumDFS(grid, i - 1, j);\n int left = minPathSumDFS(grid, i, j - 1);\n // \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n return myMin(left, up) != INT_MAX ? myMin(left, up) + grid[i][j] : INT_MAX;\n}\n
min_path_sum.zig// \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u66b4\u529b\u641c\u7d22\nfn minPathSumDFS(grid: anytype, i: i32, j: i32) i32 {\n // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n if (i == 0 and j == 0) {\n return grid[0][0];\n }\n // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n if (i < 0 or j < 0) {\n return std.math.maxInt(i32);\n }\n // \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n var up = minPathSumDFS(grid, i - 1, j);\n var left = minPathSumDFS(grid, i, j - 1);\n // \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n return @min(left, up) + grid[@as(usize, @intCast(i))][@as(usize, @intCast(j))];\n}\n
\u56fe 14-14 \u7ed9\u51fa\u4e86\u4ee5 \\(dp[2, 1]\\) \u4e3a\u6839\u8282\u70b9\u7684\u9012\u5f52\u6811\uff0c\u5176\u4e2d\u5305\u542b\u4e00\u4e9b\u91cd\u53e0\u5b50\u95ee\u9898\uff0c\u5176\u6570\u91cf\u4f1a\u968f\u7740\u7f51\u683c grid
\u7684\u5c3a\u5bf8\u53d8\u5927\u800c\u6025\u5267\u589e\u591a\u3002
\u4ece\u672c\u8d28\u4e0a\u770b\uff0c\u9020\u6210\u91cd\u53e0\u5b50\u95ee\u9898\u7684\u539f\u56e0\u4e3a\uff1a\u5b58\u5728\u591a\u6761\u8def\u5f84\u53ef\u4ee5\u4ece\u5de6\u4e0a\u89d2\u5230\u8fbe\u67d0\u4e00\u5355\u5143\u683c\u3002
\u56fe 14-14 \u00a0 \u66b4\u529b\u641c\u7d22\u9012\u5f52\u6811
\u6bcf\u4e2a\u72b6\u6001\u90fd\u6709\u5411\u4e0b\u548c\u5411\u53f3\u4e24\u79cd\u9009\u62e9\uff0c\u4ece\u5de6\u4e0a\u89d2\u8d70\u5230\u53f3\u4e0b\u89d2\u603b\u5171\u9700\u8981 \\(m + n - 2\\) \u6b65\uff0c\u6240\u4ee5\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(2^{m + n})\\) \u3002\u8bf7\u6ce8\u610f\uff0c\u8fd9\u79cd\u8ba1\u7b97\u65b9\u5f0f\u672a\u8003\u8651\u4e34\u8fd1\u7f51\u683c\u8fb9\u754c\u7684\u60c5\u51b5\uff0c\u5f53\u5230\u8fbe\u7f51\u7edc\u8fb9\u754c\u65f6\u53ea\u5269\u4e0b\u4e00\u79cd\u9009\u62e9\uff0c\u56e0\u6b64\u5b9e\u9645\u7684\u8def\u5f84\u6570\u91cf\u4f1a\u5c11\u4e00\u4e9b\u3002
"},{"location":"chapter_dynamic_programming/dp_solution_pipeline/#2","title":"2. \u00a0 \u65b9\u6cd5\u4e8c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22","text":"\u6211\u4eec\u5f15\u5165\u4e00\u4e2a\u548c\u7f51\u683c grid
\u76f8\u540c\u5c3a\u5bf8\u7684\u8bb0\u5fc6\u5217\u8868 mem
\uff0c\u7528\u4e8e\u8bb0\u5f55\u5404\u4e2a\u5b50\u95ee\u9898\u7684\u89e3\uff0c\u5e76\u5c06\u91cd\u53e0\u5b50\u95ee\u9898\u8fdb\u884c\u526a\u679d\uff1a
def min_path_sum_dfs_mem(\n grid: list[list[int]], mem: list[list[int]], i: int, j: int\n) -> int:\n \"\"\"\u6700\u5c0f\u8def\u5f84\u548c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22\"\"\"\n # \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n if i == 0 and j == 0:\n return grid[0][0]\n # \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n if i < 0 or j < 0:\n return inf\n # \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if mem[i][j] != -1:\n return mem[i][j]\n # \u5de6\u8fb9\u548c\u4e0a\u8fb9\u5355\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n up = min_path_sum_dfs_mem(grid, mem, i - 1, j)\n left = min_path_sum_dfs_mem(grid, mem, i, j - 1)\n # \u8bb0\u5f55\u5e76\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n mem[i][j] = min(left, up) + grid[i][j]\n return mem[i][j]\n
min_path_sum.cpp/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint minPathSumDFSMem(vector<vector<int>> &grid, vector<vector<int>> &mem, int i, int j) {\n // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n if (i == 0 && j == 0) {\n return grid[0][0];\n }\n // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n if (i < 0 || j < 0) {\n return INT_MAX;\n }\n // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if (mem[i][j] != -1) {\n return mem[i][j];\n }\n // \u5de6\u8fb9\u548c\u4e0a\u8fb9\u5355\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n int up = minPathSumDFSMem(grid, mem, i - 1, j);\n int left = minPathSumDFSMem(grid, mem, i, j - 1);\n // \u8bb0\u5f55\u5e76\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n mem[i][j] = min(left, up) != INT_MAX ? min(left, up) + grid[i][j] : INT_MAX;\n return mem[i][j];\n}\n
min_path_sum.java/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint minPathSumDFSMem(int[][] grid, int[][] mem, int i, int j) {\n // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n if (i == 0 && j == 0) {\n return grid[0][0];\n }\n // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n if (i < 0 || j < 0) {\n return Integer.MAX_VALUE;\n }\n // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if (mem[i][j] != -1) {\n return mem[i][j];\n }\n // \u5de6\u8fb9\u548c\u4e0a\u8fb9\u5355\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n int up = minPathSumDFSMem(grid, mem, i - 1, j);\n int left = minPathSumDFSMem(grid, mem, i, j - 1);\n // \u8bb0\u5f55\u5e76\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n mem[i][j] = Math.min(left, up) + grid[i][j];\n return mem[i][j];\n}\n
min_path_sum.cs/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint MinPathSumDFSMem(int[][] grid, int[][] mem, int i, int j) {\n // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n if (i == 0 && j == 0) {\n return grid[0][0];\n }\n // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n if (i < 0 || j < 0) {\n return int.MaxValue;\n }\n // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if (mem[i][j] != -1) {\n return mem[i][j];\n }\n // \u5de6\u8fb9\u548c\u4e0a\u8fb9\u5355\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n int up = MinPathSumDFSMem(grid, mem, i - 1, j);\n int left = MinPathSumDFSMem(grid, mem, i, j - 1);\n // \u8bb0\u5f55\u5e76\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n mem[i][j] = Math.Min(left, up) + grid[i][j];\n return mem[i][j];\n}\n
min_path_sum.go/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunc minPathSumDFSMem(grid, mem [][]int, i, j int) int {\n // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n if i == 0 && j == 0 {\n return grid[0][0]\n }\n // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n if i < 0 || j < 0 {\n return math.MaxInt\n }\n // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if mem[i][j] != -1 {\n return mem[i][j]\n }\n // \u5de6\u8fb9\u548c\u4e0a\u8fb9\u5355\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n up := minPathSumDFSMem(grid, mem, i-1, j)\n left := minPathSumDFSMem(grid, mem, i, j-1)\n // \u8bb0\u5f55\u5e76\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n mem[i][j] = int(math.Min(float64(left), float64(up))) + grid[i][j]\n return mem[i][j]\n}\n
min_path_sum.swift/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunc minPathSumDFSMem(grid: [[Int]], mem: inout [[Int]], i: Int, j: Int) -> Int {\n // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n if i == 0, j == 0 {\n return grid[0][0]\n }\n // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n if i < 0 || j < 0 {\n return .max\n }\n // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if mem[i][j] != -1 {\n return mem[i][j]\n }\n // \u5de6\u8fb9\u548c\u4e0a\u8fb9\u5355\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n let up = minPathSumDFSMem(grid: grid, mem: &mem, i: i - 1, j: j)\n let left = minPathSumDFSMem(grid: grid, mem: &mem, i: i, j: j - 1)\n // \u8bb0\u5f55\u5e76\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n mem[i][j] = min(left, up) + grid[i][j]\n return mem[i][j]\n}\n
min_path_sum.js/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunction minPathSumDFSMem(grid, mem, i, j) {\n // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n if (i === 0 && j === 0) {\n return grid[0][0];\n }\n // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n if (i < 0 || j < 0) {\n return Infinity;\n }\n // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if (mem[i][j] !== -1) {\n return mem[i][j];\n }\n // \u5de6\u8fb9\u548c\u4e0a\u8fb9\u5355\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n const up = minPathSumDFSMem(grid, mem, i - 1, j);\n const left = minPathSumDFSMem(grid, mem, i, j - 1);\n // \u8bb0\u5f55\u5e76\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n mem[i][j] = Math.min(left, up) + grid[i][j];\n return mem[i][j];\n}\n
min_path_sum.ts/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunction minPathSumDFSMem(\n grid: Array<Array<number>>,\n mem: Array<Array<number>>,\n i: number,\n j: number\n): number {\n // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n if (i === 0 && j === 0) {\n return grid[0][0];\n }\n // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n if (i < 0 || j < 0) {\n return Infinity;\n }\n // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if (mem[i][j] != -1) {\n return mem[i][j];\n }\n // \u5de6\u8fb9\u548c\u4e0a\u8fb9\u5355\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n const up = minPathSumDFSMem(grid, mem, i - 1, j);\n const left = minPathSumDFSMem(grid, mem, i, j - 1);\n // \u8bb0\u5f55\u5e76\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n mem[i][j] = Math.min(left, up) + grid[i][j];\n return mem[i][j];\n}\n
min_path_sum.dart/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint minPathSumDFSMem(List<List<int>> grid, List<List<int>> mem, int i, int j) {\n // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n if (i == 0 && j == 0) {\n return grid[0][0];\n }\n // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n if (i < 0 || j < 0) {\n // \u5728 Dart \u4e2d\uff0cint \u7c7b\u578b\u662f\u56fa\u5b9a\u8303\u56f4\u7684\u6574\u6570\uff0c\u4e0d\u5b58\u5728\u8868\u793a\u201c\u65e0\u7a77\u5927\u201d\u7684\u503c\n return BigInt.from(2).pow(31).toInt();\n }\n // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if (mem[i][j] != -1) {\n return mem[i][j];\n }\n // \u5de6\u8fb9\u548c\u4e0a\u8fb9\u5355\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n int up = minPathSumDFSMem(grid, mem, i - 1, j);\n int left = minPathSumDFSMem(grid, mem, i, j - 1);\n // \u8bb0\u5f55\u5e76\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n mem[i][j] = min(left, up) + grid[i][j];\n return mem[i][j];\n}\n
min_path_sum.rs/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfn min_path_sum_dfs_mem(grid: &Vec<Vec<i32>>, mem: &mut Vec<Vec<i32>>, i: i32, j: i32) -> i32 {\n // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n if i == 0 && j == 0 {\n return grid[0][0];\n }\n // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n if i < 0 || j < 0 {\n return i32::MAX;\n }\n // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if mem[i as usize][j as usize] != -1 {\n return mem[i as usize][j as usize];\n }\n // \u5de6\u8fb9\u548c\u4e0a\u8fb9\u5355\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n let up = min_path_sum_dfs_mem(grid, mem, i - 1, j);\n let left = min_path_sum_dfs_mem(grid, mem, i, j - 1);\n // \u8bb0\u5f55\u5e76\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n mem[i as usize][j as usize] = std::cmp::min(left, up) + grid[i as usize][j as usize];\n mem[i as usize][j as usize]\n}\n
min_path_sum.c/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint minPathSumDFSMem(int grid[MAX_SIZE][MAX_SIZE], int mem[MAX_SIZE][MAX_SIZE], int i, int j) {\n // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n if (i == 0 && j == 0) {\n return grid[0][0];\n }\n // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n if (i < 0 || j < 0) {\n return INT_MAX;\n }\n // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if (mem[i][j] != -1) {\n return mem[i][j];\n }\n // \u5de6\u8fb9\u548c\u4e0a\u8fb9\u5355\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n int up = minPathSumDFSMem(grid, mem, i - 1, j);\n int left = minPathSumDFSMem(grid, mem, i, j - 1);\n // \u8bb0\u5f55\u5e76\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n mem[i][j] = myMin(left, up) != INT_MAX ? myMin(left, up) + grid[i][j] : INT_MAX;\n return mem[i][j];\n}\n
min_path_sum.zig// \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22\nfn minPathSumDFSMem(grid: anytype, mem: anytype, i: i32, j: i32) i32 {\n // \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\n if (i == 0 and j == 0) {\n return grid[0][0];\n }\n // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\n if (i < 0 or j < 0) {\n return std.math.maxInt(i32);\n }\n // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if (mem[@as(usize, @intCast(i))][@as(usize, @intCast(j))] != -1) {\n return mem[@as(usize, @intCast(i))][@as(usize, @intCast(j))];\n }\n // \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n var up = minPathSumDFSMem(grid, mem, i - 1, j);\n var left = minPathSumDFSMem(grid, mem, i, j - 1);\n // \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n // \u8bb0\u5f55\u5e76\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n mem[@as(usize, @intCast(i))][@as(usize, @intCast(j))] = @min(left, up) + grid[@as(usize, @intCast(i))][@as(usize, @intCast(j))];\n return mem[@as(usize, @intCast(i))][@as(usize, @intCast(j))];\n}\n
\u5982\u56fe 14-15 \u6240\u793a\uff0c\u5728\u5f15\u5165\u8bb0\u5fc6\u5316\u540e\uff0c\u6240\u6709\u5b50\u95ee\u9898\u7684\u89e3\u53ea\u9700\u8ba1\u7b97\u4e00\u6b21\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u53d6\u51b3\u4e8e\u72b6\u6001\u603b\u6570\uff0c\u5373\u7f51\u683c\u5c3a\u5bf8 \\(O(nm)\\) \u3002
\u56fe 14-15 \u00a0 \u8bb0\u5fc6\u5316\u641c\u7d22\u9012\u5f52\u6811
"},{"location":"chapter_dynamic_programming/dp_solution_pipeline/#3","title":"3. \u00a0 \u65b9\u6cd5\u4e09\uff1a\u52a8\u6001\u89c4\u5212","text":"\u57fa\u4e8e\u8fed\u4ee3\u5b9e\u73b0\u52a8\u6001\u89c4\u5212\u89e3\u6cd5\uff0c\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig min_path_sum.pydef min_path_sum_dp(grid: list[list[int]]) -> int:\n \"\"\"\u6700\u5c0f\u8def\u5f84\u548c\uff1a\u52a8\u6001\u89c4\u5212\"\"\"\n n, m = len(grid), len(grid[0])\n # \u521d\u59cb\u5316 dp \u8868\n dp = [[0] * m for _ in range(n)]\n dp[0][0] = grid[0][0]\n # \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n for j in range(1, m):\n dp[0][j] = dp[0][j - 1] + grid[0][j]\n # \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n for i in range(1, n):\n dp[i][0] = dp[i - 1][0] + grid[i][0]\n # \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n for i in range(1, n):\n for j in range(1, m):\n dp[i][j] = min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j]\n return dp[n - 1][m - 1]\n
min_path_sum.cpp/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u52a8\u6001\u89c4\u5212 */\nint minPathSumDP(vector<vector<int>> &grid) {\n int n = grid.size(), m = grid[0].size();\n // \u521d\u59cb\u5316 dp \u8868\n vector<vector<int>> dp(n, vector<int>(m));\n dp[0][0] = grid[0][0];\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n for (int j = 1; j < m; j++) {\n dp[0][j] = dp[0][j - 1] + grid[0][j];\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n for (int i = 1; i < n; i++) {\n dp[i][0] = dp[i - 1][0] + grid[i][0];\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n for (int i = 1; i < n; i++) {\n for (int j = 1; j < m; j++) {\n dp[i][j] = min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];\n }\n }\n return dp[n - 1][m - 1];\n}\n
min_path_sum.java/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u52a8\u6001\u89c4\u5212 */\nint minPathSumDP(int[][] grid) {\n int n = grid.length, m = grid[0].length;\n // \u521d\u59cb\u5316 dp \u8868\n int[][] dp = new int[n][m];\n dp[0][0] = grid[0][0];\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n for (int j = 1; j < m; j++) {\n dp[0][j] = dp[0][j - 1] + grid[0][j];\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n for (int i = 1; i < n; i++) {\n dp[i][0] = dp[i - 1][0] + grid[i][0];\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n for (int i = 1; i < n; i++) {\n for (int j = 1; j < m; j++) {\n dp[i][j] = Math.min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];\n }\n }\n return dp[n - 1][m - 1];\n}\n
min_path_sum.cs/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u52a8\u6001\u89c4\u5212 */\nint MinPathSumDP(int[][] grid) {\n int n = grid.Length, m = grid[0].Length;\n // \u521d\u59cb\u5316 dp \u8868\n int[,] dp = new int[n, m];\n dp[0, 0] = grid[0][0];\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n for (int j = 1; j < m; j++) {\n dp[0, j] = dp[0, j - 1] + grid[0][j];\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n for (int i = 1; i < n; i++) {\n dp[i, 0] = dp[i - 1, 0] + grid[i][0];\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n for (int i = 1; i < n; i++) {\n for (int j = 1; j < m; j++) {\n dp[i, j] = Math.Min(dp[i, j - 1], dp[i - 1, j]) + grid[i][j];\n }\n }\n return dp[n - 1, m - 1];\n}\n
min_path_sum.go/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc minPathSumDP(grid [][]int) int {\n n, m := len(grid), len(grid[0])\n // \u521d\u59cb\u5316 dp \u8868\n dp := make([][]int, n)\n for i := 0; i < n; i++ {\n dp[i] = make([]int, m)\n }\n dp[0][0] = grid[0][0]\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n for j := 1; j < m; j++ {\n dp[0][j] = dp[0][j-1] + grid[0][j]\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n for i := 1; i < n; i++ {\n dp[i][0] = dp[i-1][0] + grid[i][0]\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n for i := 1; i < n; i++ {\n for j := 1; j < m; j++ {\n dp[i][j] = int(math.Min(float64(dp[i][j-1]), float64(dp[i-1][j]))) + grid[i][j]\n }\n }\n return dp[n-1][m-1]\n}\n
min_path_sum.swift/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc minPathSumDP(grid: [[Int]]) -> Int {\n let n = grid.count\n let m = grid[0].count\n // \u521d\u59cb\u5316 dp \u8868\n var dp = Array(repeating: Array(repeating: 0, count: m), count: n)\n dp[0][0] = grid[0][0]\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n for j in stride(from: 1, to: m, by: 1) {\n dp[0][j] = dp[0][j - 1] + grid[0][j]\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n for i in stride(from: 1, to: n, by: 1) {\n dp[i][0] = dp[i - 1][0] + grid[i][0]\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n for i in stride(from: 1, to: n, by: 1) {\n for j in stride(from: 1, to: m, by: 1) {\n dp[i][j] = min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j]\n }\n }\n return dp[n - 1][m - 1]\n}\n
min_path_sum.js/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction minPathSumDP(grid) {\n const n = grid.length,\n m = grid[0].length;\n // \u521d\u59cb\u5316 dp \u8868\n const dp = Array.from({ length: n }, () =>\n Array.from({ length: m }, () => 0)\n );\n dp[0][0] = grid[0][0];\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n for (let j = 1; j < m; j++) {\n dp[0][j] = dp[0][j - 1] + grid[0][j];\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n for (let i = 1; i < n; i++) {\n dp[i][0] = dp[i - 1][0] + grid[i][0];\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n for (let i = 1; i < n; i++) {\n for (let j = 1; j < m; j++) {\n dp[i][j] = Math.min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];\n }\n }\n return dp[n - 1][m - 1];\n}\n
min_path_sum.ts/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction minPathSumDP(grid: Array<Array<number>>): number {\n const n = grid.length,\n m = grid[0].length;\n // \u521d\u59cb\u5316 dp \u8868\n const dp = Array.from({ length: n }, () =>\n Array.from({ length: m }, () => 0)\n );\n dp[0][0] = grid[0][0];\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n for (let j = 1; j < m; j++) {\n dp[0][j] = dp[0][j - 1] + grid[0][j];\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n for (let i = 1; i < n; i++) {\n dp[i][0] = dp[i - 1][0] + grid[i][0];\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n for (let i = 1; i < n; i++) {\n for (let j: number = 1; j < m; j++) {\n dp[i][j] = Math.min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];\n }\n }\n return dp[n - 1][m - 1];\n}\n
min_path_sum.dart/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u52a8\u6001\u89c4\u5212 */\nint minPathSumDP(List<List<int>> grid) {\n int n = grid.length, m = grid[0].length;\n // \u521d\u59cb\u5316 dp \u8868\n List<List<int>> dp = List.generate(n, (i) => List.filled(m, 0));\n dp[0][0] = grid[0][0];\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n for (int j = 1; j < m; j++) {\n dp[0][j] = dp[0][j - 1] + grid[0][j];\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n for (int i = 1; i < n; i++) {\n dp[i][0] = dp[i - 1][0] + grid[i][0];\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n for (int i = 1; i < n; i++) {\n for (int j = 1; j < m; j++) {\n dp[i][j] = min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];\n }\n }\n return dp[n - 1][m - 1];\n}\n
min_path_sum.rs/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u52a8\u6001\u89c4\u5212 */\nfn min_path_sum_dp(grid: &Vec<Vec<i32>>) -> i32 {\n let (n, m) = (grid.len(), grid[0].len());\n // \u521d\u59cb\u5316 dp \u8868\n let mut dp = vec![vec![0; m]; n];\n dp[0][0] = grid[0][0];\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n for j in 1..m {\n dp[0][j] = dp[0][j - 1] + grid[0][j];\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n for i in 1..n {\n dp[i][0] = dp[i - 1][0] + grid[i][0];\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n for i in 1..n {\n for j in 1..m {\n dp[i][j] = std::cmp::min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];\n }\n }\n dp[n - 1][m - 1]\n}\n
min_path_sum.c/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u52a8\u6001\u89c4\u5212 */\nint minPathSumDP(int grid[MAX_SIZE][MAX_SIZE], int n, int m) {\n // \u521d\u59cb\u5316 dp \u8868\n int **dp = malloc(n * sizeof(int *));\n for (int i = 0; i < n; i++) {\n dp[i] = calloc(m, sizeof(int));\n }\n dp[0][0] = grid[0][0];\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n for (int j = 1; j < m; j++) {\n dp[0][j] = dp[0][j - 1] + grid[0][j];\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n for (int i = 1; i < n; i++) {\n dp[i][0] = dp[i - 1][0] + grid[i][0];\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n for (int i = 1; i < n; i++) {\n for (int j = 1; j < m; j++) {\n dp[i][j] = myMin(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];\n }\n }\n int res = dp[n - 1][m - 1];\n // \u91ca\u653e\u5185\u5b58\n for (int i = 0; i < n; i++) {\n free(dp[i]);\n }\n return res;\n}\n
min_path_sum.zig// \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u52a8\u6001\u89c4\u5212\nfn minPathSumDP(comptime grid: anytype) i32 {\n comptime var n = grid.len;\n comptime var m = grid[0].len;\n // \u521d\u59cb\u5316 dp \u8868\n var dp = [_][m]i32{[_]i32{0} ** m} ** n;\n dp[0][0] = grid[0][0];\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n for (1..m) |j| {\n dp[0][j] = dp[0][j - 1] + grid[0][j];\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n for (1..n) |i| {\n dp[i][0] = dp[i - 1][0] + grid[i][0];\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n for (1..n) |i| {\n for (1..m) |j| {\n dp[i][j] = @min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];\n }\n }\n return dp[n - 1][m - 1];\n}\n
\u56fe 14-16 \u5c55\u793a\u4e86\u6700\u5c0f\u8def\u5f84\u548c\u7684\u72b6\u6001\u8f6c\u79fb\u8fc7\u7a0b\uff0c\u5176\u904d\u5386\u4e86\u6574\u4e2a\u7f51\u683c\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(nm)\\) \u3002
\u6570\u7ec4 dp
\u5927\u5c0f\u4e3a \\(n \\times m\\) \uff0c\u56e0\u6b64\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(nm)\\) \u3002
\u56fe 14-16 \u00a0 \u6700\u5c0f\u8def\u5f84\u548c\u7684\u52a8\u6001\u89c4\u5212\u8fc7\u7a0b
"},{"location":"chapter_dynamic_programming/dp_solution_pipeline/#4","title":"4. \u00a0 \u7a7a\u95f4\u4f18\u5316","text":"\u7531\u4e8e\u6bcf\u4e2a\u683c\u5b50\u53ea\u4e0e\u5176\u5de6\u8fb9\u548c\u4e0a\u8fb9\u7684\u683c\u5b50\u6709\u5173\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u53ea\u7528\u4e00\u4e2a\u5355\u884c\u6570\u7ec4\u6765\u5b9e\u73b0 \\(dp\\) \u8868\u3002
\u8bf7\u6ce8\u610f\uff0c\u56e0\u4e3a\u6570\u7ec4 dp
\u53ea\u80fd\u8868\u793a\u4e00\u884c\u7684\u72b6\u6001\uff0c\u6240\u4ee5\u6211\u4eec\u65e0\u6cd5\u63d0\u524d\u521d\u59cb\u5316\u9996\u5217\u72b6\u6001\uff0c\u800c\u662f\u5728\u904d\u5386\u6bcf\u884c\u65f6\u66f4\u65b0\u5b83\uff1a
def min_path_sum_dp_comp(grid: list[list[int]]) -> int:\n \"\"\"\u6700\u5c0f\u8def\u5f84\u548c\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\"\"\"\n n, m = len(grid), len(grid[0])\n # \u521d\u59cb\u5316 dp \u8868\n dp = [0] * m\n # \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n dp[0] = grid[0][0]\n for j in range(1, m):\n dp[j] = dp[j - 1] + grid[0][j]\n # \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n for i in range(1, n):\n # \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n dp[0] = dp[0] + grid[i][0]\n # \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n for j in range(1, m):\n dp[j] = min(dp[j - 1], dp[j]) + grid[i][j]\n return dp[m - 1]\n
min_path_sum.cpp/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint minPathSumDPComp(vector<vector<int>> &grid) {\n int n = grid.size(), m = grid[0].size();\n // \u521d\u59cb\u5316 dp \u8868\n vector<int> dp(m);\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n dp[0] = grid[0][0];\n for (int j = 1; j < m; j++) {\n dp[j] = dp[j - 1] + grid[0][j];\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n for (int i = 1; i < n; i++) {\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n dp[0] = dp[0] + grid[i][0];\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n for (int j = 1; j < m; j++) {\n dp[j] = min(dp[j - 1], dp[j]) + grid[i][j];\n }\n }\n return dp[m - 1];\n}\n
min_path_sum.java/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint minPathSumDPComp(int[][] grid) {\n int n = grid.length, m = grid[0].length;\n // \u521d\u59cb\u5316 dp \u8868\n int[] dp = new int[m];\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n dp[0] = grid[0][0];\n for (int j = 1; j < m; j++) {\n dp[j] = dp[j - 1] + grid[0][j];\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n for (int i = 1; i < n; i++) {\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n dp[0] = dp[0] + grid[i][0];\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n for (int j = 1; j < m; j++) {\n dp[j] = Math.min(dp[j - 1], dp[j]) + grid[i][j];\n }\n }\n return dp[m - 1];\n}\n
min_path_sum.cs/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint MinPathSumDPComp(int[][] grid) {\n int n = grid.Length, m = grid[0].Length;\n // \u521d\u59cb\u5316 dp \u8868\n int[] dp = new int[m];\n dp[0] = grid[0][0];\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n for (int j = 1; j < m; j++) {\n dp[j] = dp[j - 1] + grid[0][j];\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n for (int i = 1; i < n; i++) {\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n dp[0] = dp[0] + grid[i][0];\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n for (int j = 1; j < m; j++) {\n dp[j] = Math.Min(dp[j - 1], dp[j]) + grid[i][j];\n }\n }\n return dp[m - 1];\n}\n
min_path_sum.go/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc minPathSumDPComp(grid [][]int) int {\n n, m := len(grid), len(grid[0])\n // \u521d\u59cb\u5316 dp \u8868\n dp := make([]int, m)\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n dp[0] = grid[0][0]\n for j := 1; j < m; j++ {\n dp[j] = dp[j-1] + grid[0][j]\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n for i := 1; i < n; i++ {\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n dp[0] = dp[0] + grid[i][0]\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n for j := 1; j < m; j++ {\n dp[j] = int(math.Min(float64(dp[j-1]), float64(dp[j]))) + grid[i][j]\n }\n }\n return dp[m-1]\n}\n
min_path_sum.swift/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc minPathSumDPComp(grid: [[Int]]) -> Int {\n let n = grid.count\n let m = grid[0].count\n // \u521d\u59cb\u5316 dp \u8868\n var dp = Array(repeating: 0, count: m)\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n dp[0] = grid[0][0]\n for j in stride(from: 1, to: m, by: 1) {\n dp[j] = dp[j - 1] + grid[0][j]\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n for i in stride(from: 1, to: n, by: 1) {\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n dp[0] = dp[0] + grid[i][0]\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n for j in stride(from: 1, to: m, by: 1) {\n dp[j] = min(dp[j - 1], dp[j]) + grid[i][j]\n }\n }\n return dp[m - 1]\n}\n
min_path_sum.js/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction minPathSumDPComp(grid) {\n const n = grid.length,\n m = grid[0].length;\n // \u521d\u59cb\u5316 dp \u8868\n const dp = new Array(m);\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n dp[0] = grid[0][0];\n for (let j = 1; j < m; j++) {\n dp[j] = dp[j - 1] + grid[0][j];\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n for (let i = 1; i < n; i++) {\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n dp[0] = dp[0] + grid[i][0];\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n for (let j = 1; j < m; j++) {\n dp[j] = Math.min(dp[j - 1], dp[j]) + grid[i][j];\n }\n }\n return dp[m - 1];\n}\n
min_path_sum.ts/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction minPathSumDPComp(grid: Array<Array<number>>): number {\n const n = grid.length,\n m = grid[0].length;\n // \u521d\u59cb\u5316 dp \u8868\n const dp = new Array(m);\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n dp[0] = grid[0][0];\n for (let j = 1; j < m; j++) {\n dp[j] = dp[j - 1] + grid[0][j];\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n for (let i = 1; i < n; i++) {\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n dp[0] = dp[0] + grid[i][0];\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n for (let j = 1; j < m; j++) {\n dp[j] = Math.min(dp[j - 1], dp[j]) + grid[i][j];\n }\n }\n return dp[m - 1];\n}\n
min_path_sum.dart/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint minPathSumDPComp(List<List<int>> grid) {\n int n = grid.length, m = grid[0].length;\n // \u521d\u59cb\u5316 dp \u8868\n List<int> dp = List.filled(m, 0);\n dp[0] = grid[0][0];\n for (int j = 1; j < m; j++) {\n dp[j] = dp[j - 1] + grid[0][j];\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n for (int i = 1; i < n; i++) {\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n dp[0] = dp[0] + grid[i][0];\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n for (int j = 1; j < m; j++) {\n dp[j] = min(dp[j - 1], dp[j]) + grid[i][j];\n }\n }\n return dp[m - 1];\n}\n
min_path_sum.rs/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfn min_path_sum_dp_comp(grid: &Vec<Vec<i32>>) -> i32 {\n let (n, m) = (grid.len(), grid[0].len());\n // \u521d\u59cb\u5316 dp \u8868\n let mut dp = vec![0; m];\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n dp[0] = grid[0][0];\n for j in 1..m {\n dp[j] = dp[j - 1] + grid[0][j];\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n for i in 1..n {\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n dp[0] = dp[0] + grid[i][0];\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n for j in 1..m {\n dp[j] = std::cmp::min(dp[j - 1], dp[j]) + grid[i][j];\n }\n }\n dp[m - 1]\n}\n
min_path_sum.c/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint minPathSumDPComp(int grid[MAX_SIZE][MAX_SIZE], int n, int m) {\n // \u521d\u59cb\u5316 dp \u8868\n int *dp = calloc(m, sizeof(int));\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n dp[0] = grid[0][0];\n for (int j = 1; j < m; j++) {\n dp[j] = dp[j - 1] + grid[0][j];\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n for (int i = 1; i < n; i++) {\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n dp[0] = dp[0] + grid[i][0];\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n for (int j = 1; j < m; j++) {\n dp[j] = myMin(dp[j - 1], dp[j]) + grid[i][j];\n }\n }\n int res = dp[m - 1];\n // \u91ca\u653e\u5185\u5b58\n free(dp);\n return res;\n}\n
min_path_sum.zig// \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\nfn minPathSumDPComp(comptime grid: anytype) i32 {\n comptime var n = grid.len;\n comptime var m = grid[0].len;\n // \u521d\u59cb\u5316 dp \u8868\n var dp = [_]i32{0} ** m;\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n dp[0] = grid[0][0];\n for (1..m) |j| {\n dp[j] = dp[j - 1] + grid[0][j];\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n for (1..n) |i| {\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n dp[0] = dp[0] + grid[i][0];\n for (1..m) |j| {\n dp[j] = @min(dp[j - 1], dp[j]) + grid[i][j];\n }\n }\n return dp[m - 1];\n}\n
"},{"location":"chapter_dynamic_programming/edit_distance_problem/","title":"14.6 \u00a0 \u7f16\u8f91\u8ddd\u79bb\u95ee\u9898","text":"\u7f16\u8f91\u8ddd\u79bb\uff0c\u4e5f\u79f0 Levenshtein \u8ddd\u79bb\uff0c\u6307\u4e24\u4e2a\u5b57\u7b26\u4e32\u4e4b\u95f4\u4e92\u76f8\u8f6c\u6362\u7684\u6700\u5c11\u4fee\u6539\u6b21\u6570\uff0c\u901a\u5e38\u7528\u4e8e\u5728\u4fe1\u606f\u68c0\u7d22\u548c\u81ea\u7136\u8bed\u8a00\u5904\u7406\u4e2d\u5ea6\u91cf\u4e24\u4e2a\u5e8f\u5217\u7684\u76f8\u4f3c\u5ea6\u3002
Question
\u8f93\u5165\u4e24\u4e2a\u5b57\u7b26\u4e32 \\(s\\) \u548c \\(t\\) \uff0c\u8fd4\u56de\u5c06 \\(s\\) \u8f6c\u6362\u4e3a \\(t\\) \u6240\u9700\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570\u3002
\u4f60\u53ef\u4ee5\u5728\u4e00\u4e2a\u5b57\u7b26\u4e32\u4e2d\u8fdb\u884c\u4e09\u79cd\u7f16\u8f91\u64cd\u4f5c\uff1a\u63d2\u5165\u4e00\u4e2a\u5b57\u7b26\u3001\u5220\u9664\u4e00\u4e2a\u5b57\u7b26\u3001\u5c06\u5b57\u7b26\u66ff\u6362\u4e3a\u4efb\u610f\u4e00\u4e2a\u5b57\u7b26\u3002
\u5982\u56fe 14-27 \u6240\u793a\uff0c\u5c06 kitten
\u8f6c\u6362\u4e3a sitting
\u9700\u8981\u7f16\u8f91 3 \u6b65\uff0c\u5305\u62ec 2 \u6b21\u66ff\u6362\u64cd\u4f5c\u4e0e 1 \u6b21\u6dfb\u52a0\u64cd\u4f5c\uff1b\u5c06 hello
\u8f6c\u6362\u4e3a algo
\u9700\u8981 3 \u6b65\uff0c\u5305\u62ec 2 \u6b21\u66ff\u6362\u64cd\u4f5c\u548c 1 \u6b21\u5220\u9664\u64cd\u4f5c\u3002
\u56fe 14-27 \u00a0 \u7f16\u8f91\u8ddd\u79bb\u7684\u793a\u4f8b\u6570\u636e
\u7f16\u8f91\u8ddd\u79bb\u95ee\u9898\u53ef\u4ee5\u5f88\u81ea\u7136\u5730\u7528\u51b3\u7b56\u6811\u6a21\u578b\u6765\u89e3\u91ca\u3002\u5b57\u7b26\u4e32\u5bf9\u5e94\u6811\u8282\u70b9\uff0c\u4e00\u8f6e\u51b3\u7b56\uff08\u4e00\u6b21\u7f16\u8f91\u64cd\u4f5c\uff09\u5bf9\u5e94\u6811\u7684\u4e00\u6761\u8fb9\u3002
\u5982\u56fe 14-28 \u6240\u793a\uff0c\u5728\u4e0d\u9650\u5236\u64cd\u4f5c\u7684\u60c5\u51b5\u4e0b\uff0c\u6bcf\u4e2a\u8282\u70b9\u90fd\u53ef\u4ee5\u6d3e\u751f\u51fa\u8bb8\u591a\u6761\u8fb9\uff0c\u6bcf\u6761\u8fb9\u5bf9\u5e94\u4e00\u79cd\u64cd\u4f5c\uff0c\u8fd9\u610f\u5473\u7740\u4ece hello
\u8f6c\u6362\u5230 algo
\u6709\u8bb8\u591a\u79cd\u53ef\u80fd\u7684\u8def\u5f84\u3002
\u4ece\u51b3\u7b56\u6811\u7684\u89d2\u5ea6\u770b\uff0c\u672c\u9898\u7684\u76ee\u6807\u662f\u6c42\u89e3\u8282\u70b9 hello
\u548c\u8282\u70b9 algo
\u4e4b\u95f4\u7684\u6700\u77ed\u8def\u5f84\u3002
\u56fe 14-28 \u00a0 \u57fa\u4e8e\u51b3\u7b56\u6811\u6a21\u578b\u8868\u793a\u7f16\u8f91\u8ddd\u79bb\u95ee\u9898
"},{"location":"chapter_dynamic_programming/edit_distance_problem/#1","title":"1. \u00a0 \u52a8\u6001\u89c4\u5212\u601d\u8def","text":"\u7b2c\u4e00\u6b65\uff1a\u601d\u8003\u6bcf\u8f6e\u7684\u51b3\u7b56\uff0c\u5b9a\u4e49\u72b6\u6001\uff0c\u4ece\u800c\u5f97\u5230 \\(dp\\) \u8868
\u6bcf\u4e00\u8f6e\u7684\u51b3\u7b56\u662f\u5bf9\u5b57\u7b26\u4e32 \\(s\\) \u8fdb\u884c\u4e00\u6b21\u7f16\u8f91\u64cd\u4f5c\u3002
\u6211\u4eec\u5e0c\u671b\u5728\u7f16\u8f91\u64cd\u4f5c\u7684\u8fc7\u7a0b\u4e2d\uff0c\u95ee\u9898\u7684\u89c4\u6a21\u9010\u6e10\u7f29\u5c0f\uff0c\u8fd9\u6837\u624d\u80fd\u6784\u5efa\u5b50\u95ee\u9898\u3002\u8bbe\u5b57\u7b26\u4e32 \\(s\\) \u548c \\(t\\) \u7684\u957f\u5ea6\u5206\u522b\u4e3a \\(n\\) \u548c \\(m\\) \uff0c\u6211\u4eec\u5148\u8003\u8651\u4e24\u5b57\u7b26\u4e32\u5c3e\u90e8\u7684\u5b57\u7b26 \\(s[n-1]\\) \u548c \\(t[m-1]\\) \u3002
\u4e5f\u5c31\u662f\u8bf4\uff0c\u6211\u4eec\u5728\u5b57\u7b26\u4e32 \\(s\\) \u4e2d\u8fdb\u884c\u7684\u6bcf\u4e00\u8f6e\u51b3\u7b56\uff08\u7f16\u8f91\u64cd\u4f5c\uff09\uff0c\u90fd\u4f1a\u4f7f\u5f97 \\(s\\) \u548c \\(t\\) \u4e2d\u5269\u4f59\u7684\u5f85\u5339\u914d\u5b57\u7b26\u53d1\u751f\u53d8\u5316\u3002\u56e0\u6b64\uff0c\u72b6\u6001\u4e3a\u5f53\u524d\u5728 \\(s\\) \u548c \\(t\\) \u4e2d\u8003\u8651\u7684\u7b2c \\(i\\) \u548c\u7b2c \\(j\\) \u4e2a\u5b57\u7b26\uff0c\u8bb0\u4e3a \\([i, j]\\) \u3002
\u72b6\u6001 \\([i, j]\\) \u5bf9\u5e94\u7684\u5b50\u95ee\u9898\uff1a\u5c06 \\(s\\) \u7684\u524d \\(i\\) \u4e2a\u5b57\u7b26\u66f4\u6539\u4e3a \\(t\\) \u7684\u524d \\(j\\) \u4e2a\u5b57\u7b26\u6240\u9700\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570\u3002
\u81f3\u6b64\uff0c\u5f97\u5230\u4e00\u4e2a\u5c3a\u5bf8\u4e3a \\((i+1) \\times (j+1)\\) \u7684\u4e8c\u7ef4 \\(dp\\) \u8868\u3002
\u7b2c\u4e8c\u6b65\uff1a\u627e\u51fa\u6700\u4f18\u5b50\u7ed3\u6784\uff0c\u8fdb\u800c\u63a8\u5bfc\u51fa\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b
\u8003\u8651\u5b50\u95ee\u9898 \\(dp[i, j]\\) \uff0c\u5176\u5bf9\u5e94\u7684\u4e24\u4e2a\u5b57\u7b26\u4e32\u7684\u5c3e\u90e8\u5b57\u7b26\u4e3a \\(s[i-1]\\) \u548c \\(t[j-1]\\) \uff0c\u53ef\u6839\u636e\u4e0d\u540c\u7f16\u8f91\u64cd\u4f5c\u5206\u4e3a\u56fe 14-29 \u6240\u793a\u7684\u4e09\u79cd\u60c5\u51b5\u3002
\u56fe 14-29 \u00a0 \u7f16\u8f91\u8ddd\u79bb\u7684\u72b6\u6001\u8f6c\u79fb
\u6839\u636e\u4ee5\u4e0a\u5206\u6790\uff0c\u53ef\u5f97\u6700\u4f18\u5b50\u7ed3\u6784\uff1a\\(dp[i, j]\\) \u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570\u7b49\u4e8e \\(dp[i, j-1]\\)\u3001\\(dp[i-1, j]\\)\u3001\\(dp[i-1, j-1]\\) \u4e09\u8005\u4e2d\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570\uff0c\u518d\u52a0\u4e0a\u672c\u6b21\u7684\u7f16\u8f91\u6b65\u6570 \\(1\\) \u3002\u5bf9\u5e94\u7684\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u4e3a\uff1a
\\[ dp[i, j] = \\min(dp[i, j-1], dp[i-1, j], dp[i-1, j-1]) + 1 \\]\u8bf7\u6ce8\u610f\uff0c\u5f53 \\(s[i-1]\\) \u548c \\(t[j-1]\\) \u76f8\u540c\u65f6\uff0c\u65e0\u987b\u7f16\u8f91\u5f53\u524d\u5b57\u7b26\uff0c\u8fd9\u79cd\u60c5\u51b5\u4e0b\u7684\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u4e3a\uff1a
\\[ dp[i, j] = dp[i-1, j-1] \\]\u7b2c\u4e09\u6b65\uff1a\u786e\u5b9a\u8fb9\u754c\u6761\u4ef6\u548c\u72b6\u6001\u8f6c\u79fb\u987a\u5e8f
\u5f53\u4e24\u5b57\u7b26\u4e32\u90fd\u4e3a\u7a7a\u65f6\uff0c\u7f16\u8f91\u6b65\u6570\u4e3a \\(0\\) \uff0c\u5373 \\(dp[0, 0] = 0\\) \u3002\u5f53 \\(s\\) \u4e3a\u7a7a\u4f46 \\(t\\) \u4e0d\u4e3a\u7a7a\u65f6\uff0c\u6700\u5c11\u7f16\u8f91\u6b65\u6570\u7b49\u4e8e \\(t\\) \u7684\u957f\u5ea6\uff0c\u5373\u9996\u884c \\(dp[0, j] = j\\) \u3002\u5f53 \\(s\\) \u4e0d\u4e3a\u7a7a\u4f46 \\(t\\) \u4e3a\u7a7a\u65f6\uff0c\u6700\u5c11\u7f16\u8f91\u6b65\u6570\u7b49\u4e8e \\(s\\) \u7684\u957f\u5ea6\uff0c\u5373\u9996\u5217 \\(dp[i, 0] = i\\) \u3002
\u89c2\u5bdf\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\uff0c\u89e3 \\(dp[i, j]\\) \u4f9d\u8d56\u5de6\u65b9\u3001\u4e0a\u65b9\u3001\u5de6\u4e0a\u65b9\u7684\u89e3\uff0c\u56e0\u6b64\u901a\u8fc7\u4e24\u5c42\u5faa\u73af\u6b63\u5e8f\u904d\u5386\u6574\u4e2a \\(dp\\) \u8868\u5373\u53ef\u3002
"},{"location":"chapter_dynamic_programming/edit_distance_problem/#2","title":"2. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"PythonC++JavaC#GoSwiftJSTSDartRustCZig edit_distance.pydef edit_distance_dp(s: str, t: str) -> int:\n \"\"\"\u7f16\u8f91\u8ddd\u79bb\uff1a\u52a8\u6001\u89c4\u5212\"\"\"\n n, m = len(s), len(t)\n dp = [[0] * (m + 1) for _ in range(n + 1)]\n # \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n for i in range(1, n + 1):\n dp[i][0] = i\n for j in range(1, m + 1):\n dp[0][j] = j\n # \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n for i in range(1, n + 1):\n for j in range(1, m + 1):\n if s[i - 1] == t[j - 1]:\n # \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n dp[i][j] = dp[i - 1][j - 1]\n else:\n # \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n dp[i][j] = min(dp[i][j - 1], dp[i - 1][j], dp[i - 1][j - 1]) + 1\n return dp[n][m]\n
edit_distance.cpp/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u52a8\u6001\u89c4\u5212 */\nint editDistanceDP(string s, string t) {\n int n = s.length(), m = t.length();\n vector<vector<int>> dp(n + 1, vector<int>(m + 1, 0));\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n for (int i = 1; i <= n; i++) {\n dp[i][0] = i;\n }\n for (int j = 1; j <= m; j++) {\n dp[0][j] = j;\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n for (int i = 1; i <= n; i++) {\n for (int j = 1; j <= m; j++) {\n if (s[i - 1] == t[j - 1]) {\n // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n dp[i][j] = dp[i - 1][j - 1];\n } else {\n // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n dp[i][j] = min(min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1;\n }\n }\n }\n return dp[n][m];\n}\n
edit_distance.java/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u52a8\u6001\u89c4\u5212 */\nint editDistanceDP(String s, String t) {\n int n = s.length(), m = t.length();\n int[][] dp = new int[n + 1][m + 1];\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n for (int i = 1; i <= n; i++) {\n dp[i][0] = i;\n }\n for (int j = 1; j <= m; j++) {\n dp[0][j] = j;\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n for (int i = 1; i <= n; i++) {\n for (int j = 1; j <= m; j++) {\n if (s.charAt(i - 1) == t.charAt(j - 1)) {\n // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n dp[i][j] = dp[i - 1][j - 1];\n } else {\n // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n dp[i][j] = Math.min(Math.min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1;\n }\n }\n }\n return dp[n][m];\n}\n
edit_distance.cs/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u52a8\u6001\u89c4\u5212 */\nint EditDistanceDP(string s, string t) {\n int n = s.Length, m = t.Length;\n int[,] dp = new int[n + 1, m + 1];\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n for (int i = 1; i <= n; i++) {\n dp[i, 0] = i;\n }\n for (int j = 1; j <= m; j++) {\n dp[0, j] = j;\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n for (int i = 1; i <= n; i++) {\n for (int j = 1; j <= m; j++) {\n if (s[i - 1] == t[j - 1]) {\n // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n dp[i, j] = dp[i - 1, j - 1];\n } else {\n // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n dp[i, j] = Math.Min(Math.Min(dp[i, j - 1], dp[i - 1, j]), dp[i - 1, j - 1]) + 1;\n }\n }\n }\n return dp[n, m];\n}\n
edit_distance.go/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc editDistanceDP(s string, t string) int {\n n := len(s)\n m := len(t)\n dp := make([][]int, n+1)\n for i := 0; i <= n; i++ {\n dp[i] = make([]int, m+1)\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n for i := 1; i <= n; i++ {\n dp[i][0] = i\n }\n for j := 1; j <= m; j++ {\n dp[0][j] = j\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n for i := 1; i <= n; i++ {\n for j := 1; j <= m; j++ {\n if s[i-1] == t[j-1] {\n // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n dp[i][j] = dp[i-1][j-1]\n } else {\n // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n dp[i][j] = MinInt(MinInt(dp[i][j-1], dp[i-1][j]), dp[i-1][j-1]) + 1\n }\n }\n }\n return dp[n][m]\n}\n
edit_distance.swift/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc editDistanceDP(s: String, t: String) -> Int {\n let n = s.utf8CString.count\n let m = t.utf8CString.count\n var dp = Array(repeating: Array(repeating: 0, count: m + 1), count: n + 1)\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n for i in stride(from: 1, through: n, by: 1) {\n dp[i][0] = i\n }\n for j in stride(from: 1, through: m, by: 1) {\n dp[0][j] = j\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n for i in stride(from: 1, through: n, by: 1) {\n for j in stride(from: 1, through: m, by: 1) {\n if s.utf8CString[i - 1] == t.utf8CString[j - 1] {\n // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n dp[i][j] = dp[i - 1][j - 1]\n } else {\n // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n dp[i][j] = min(min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1\n }\n }\n }\n return dp[n][m]\n}\n
edit_distance.js/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction editDistanceDP(s, t) {\n const n = s.length,\n m = t.length;\n const dp = Array.from({ length: n + 1 }, () => new Array(m + 1).fill(0));\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n for (let i = 1; i <= n; i++) {\n dp[i][0] = i;\n }\n for (let j = 1; j <= m; j++) {\n dp[0][j] = j;\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n for (let i = 1; i <= n; i++) {\n for (let j = 1; j <= m; j++) {\n if (s.charAt(i - 1) === t.charAt(j - 1)) {\n // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n dp[i][j] = dp[i - 1][j - 1];\n } else {\n // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n dp[i][j] =\n Math.min(dp[i][j - 1], dp[i - 1][j], dp[i - 1][j - 1]) + 1;\n }\n }\n }\n return dp[n][m];\n}\n
edit_distance.ts/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction editDistanceDP(s: string, t: string): number {\n const n = s.length,\n m = t.length;\n const dp = Array.from({ length: n + 1 }, () =>\n Array.from({ length: m + 1 }, () => 0)\n );\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n for (let i = 1; i <= n; i++) {\n dp[i][0] = i;\n }\n for (let j = 1; j <= m; j++) {\n dp[0][j] = j;\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n for (let i = 1; i <= n; i++) {\n for (let j = 1; j <= m; j++) {\n if (s.charAt(i - 1) === t.charAt(j - 1)) {\n // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n dp[i][j] = dp[i - 1][j - 1];\n } else {\n // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n dp[i][j] =\n Math.min(dp[i][j - 1], dp[i - 1][j], dp[i - 1][j - 1]) + 1;\n }\n }\n }\n return dp[n][m];\n}\n
edit_distance.dart/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u52a8\u6001\u89c4\u5212 */\nint editDistanceDP(String s, String t) {\n int n = s.length, m = t.length;\n List<List<int>> dp = List.generate(n + 1, (_) => List.filled(m + 1, 0));\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n for (int i = 1; i <= n; i++) {\n dp[i][0] = i;\n }\n for (int j = 1; j <= m; j++) {\n dp[0][j] = j;\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n for (int i = 1; i <= n; i++) {\n for (int j = 1; j <= m; j++) {\n if (s[i - 1] == t[j - 1]) {\n // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n dp[i][j] = dp[i - 1][j - 1];\n } else {\n // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n dp[i][j] = min(min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1;\n }\n }\n }\n return dp[n][m];\n}\n
edit_distance.rs/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u52a8\u6001\u89c4\u5212 */\nfn edit_distance_dp(s: &str, t: &str) -> i32 {\n let (n, m) = (s.len(), t.len());\n let mut dp = vec![vec![0; m + 1]; n + 1];\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n for i in 1..= n {\n dp[i][0] = i as i32;\n }\n for j in 1..m {\n dp[0][j] = j as i32;\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n for i in 1..=n {\n for j in 1..=m {\n if s.chars().nth(i - 1) == t.chars().nth(j - 1) {\n // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n dp[i][j] = dp[i - 1][j - 1];\n } else {\n // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n dp[i][j] = std::cmp::min(std::cmp::min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1;\n }\n }\n }\n dp[n][m]\n}\n
edit_distance.c/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u52a8\u6001\u89c4\u5212 */\nint editDistanceDP(char *s, char *t, int n, int m) {\n int **dp = malloc((n + 1) * sizeof(int *));\n for (int i = 0; i <= n; i++) {\n dp[i] = calloc(m + 1, sizeof(int));\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n for (int i = 1; i <= n; i++) {\n dp[i][0] = i;\n }\n for (int j = 1; j <= m; j++) {\n dp[0][j] = j;\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n for (int i = 1; i <= n; i++) {\n for (int j = 1; j <= m; j++) {\n if (s[i - 1] == t[j - 1]) {\n // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n dp[i][j] = dp[i - 1][j - 1];\n } else {\n // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n dp[i][j] = myMin(myMin(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1;\n }\n }\n }\n int res = dp[n][m];\n // \u91ca\u653e\u5185\u5b58\n for (int i = 0; i <= n; i++) {\n free(dp[i]);\n }\n return res;\n}\n
edit_distance.zig// \u7f16\u8f91\u8ddd\u79bb\uff1a\u52a8\u6001\u89c4\u5212\nfn editDistanceDP(comptime s: []const u8, comptime t: []const u8) i32 {\n comptime var n = s.len;\n comptime var m = t.len;\n var dp = [_][m + 1]i32{[_]i32{0} ** (m + 1)} ** (n + 1);\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n for (1..n + 1) |i| {\n dp[i][0] = @intCast(i);\n }\n for (1..m + 1) |j| {\n dp[0][j] = @intCast(j);\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n for (1..n + 1) |i| {\n for (1..m + 1) |j| {\n if (s[i - 1] == t[j - 1]) {\n // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n dp[i][j] = dp[i - 1][j - 1];\n } else {\n // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n dp[i][j] = @min(@min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1;\n }\n }\n }\n return dp[n][m];\n}\n
\u5982\u56fe 14-30 \u6240\u793a\uff0c\u7f16\u8f91\u8ddd\u79bb\u95ee\u9898\u7684\u72b6\u6001\u8f6c\u79fb\u8fc7\u7a0b\u4e0e\u80cc\u5305\u95ee\u9898\u975e\u5e38\u7c7b\u4f3c\uff0c\u90fd\u53ef\u4ee5\u770b\u4f5c\u586b\u5199\u4e00\u4e2a\u4e8c\u7ef4\u7f51\u683c\u7684\u8fc7\u7a0b\u3002
<1><2><3><4><5><6><7><8><9><10><11><12><13><14><15>\u56fe 14-30 \u00a0 \u7f16\u8f91\u8ddd\u79bb\u7684\u52a8\u6001\u89c4\u5212\u8fc7\u7a0b
"},{"location":"chapter_dynamic_programming/edit_distance_problem/#3","title":"3. \u00a0 \u7a7a\u95f4\u4f18\u5316","text":"\u7531\u4e8e \\(dp[i,j]\\) \u662f\u7531\u4e0a\u65b9 \\(dp[i-1, j]\\)\u3001\u5de6\u65b9 \\(dp[i, j-1]\\)\u3001\u5de6\u4e0a\u65b9 \\(dp[i-1, j-1]\\) \u8f6c\u79fb\u800c\u6765\u7684\uff0c\u800c\u6b63\u5e8f\u904d\u5386\u4f1a\u4e22\u5931\u5de6\u4e0a\u65b9 \\(dp[i-1, j-1]\\) \uff0c\u5012\u5e8f\u904d\u5386\u65e0\u6cd5\u63d0\u524d\u6784\u5efa \\(dp[i, j-1]\\) \uff0c\u56e0\u6b64\u4e24\u79cd\u904d\u5386\u987a\u5e8f\u90fd\u4e0d\u53ef\u53d6\u3002
\u4e3a\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u53d8\u91cf leftup
\u6765\u6682\u5b58\u5de6\u4e0a\u65b9\u7684\u89e3 \\(dp[i-1, j-1]\\) \uff0c\u4ece\u800c\u53ea\u9700\u8003\u8651\u5de6\u65b9\u548c\u4e0a\u65b9\u7684\u89e3\u3002\u6b64\u65f6\u7684\u60c5\u51b5\u4e0e\u5b8c\u5168\u80cc\u5305\u95ee\u9898\u76f8\u540c\uff0c\u53ef\u4f7f\u7528\u6b63\u5e8f\u904d\u5386\u3002\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a
def edit_distance_dp_comp(s: str, t: str) -> int:\n \"\"\"\u7f16\u8f91\u8ddd\u79bb\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\"\"\"\n n, m = len(s), len(t)\n dp = [0] * (m + 1)\n # \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n for j in range(1, m + 1):\n dp[j] = j\n # \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n for i in range(1, n + 1):\n # \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n leftup = dp[0] # \u6682\u5b58 dp[i-1, j-1]\n dp[0] += 1\n # \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n for j in range(1, m + 1):\n temp = dp[j]\n if s[i - 1] == t[j - 1]:\n # \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n dp[j] = leftup\n else:\n # \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n dp[j] = min(dp[j - 1], dp[j], leftup) + 1\n leftup = temp # \u66f4\u65b0\u4e3a\u4e0b\u4e00\u8f6e\u7684 dp[i-1, j-1]\n return dp[m]\n
edit_distance.cpp/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint editDistanceDPComp(string s, string t) {\n int n = s.length(), m = t.length();\n vector<int> dp(m + 1, 0);\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n for (int j = 1; j <= m; j++) {\n dp[j] = j;\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n for (int i = 1; i <= n; i++) {\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n int leftup = dp[0]; // \u6682\u5b58 dp[i-1, j-1]\n dp[0] = i;\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n for (int j = 1; j <= m; j++) {\n int temp = dp[j];\n if (s[i - 1] == t[j - 1]) {\n // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n dp[j] = leftup;\n } else {\n // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n dp[j] = min(min(dp[j - 1], dp[j]), leftup) + 1;\n }\n leftup = temp; // \u66f4\u65b0\u4e3a\u4e0b\u4e00\u8f6e\u7684 dp[i-1, j-1]\n }\n }\n return dp[m];\n}\n
edit_distance.java/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint editDistanceDPComp(String s, String t) {\n int n = s.length(), m = t.length();\n int[] dp = new int[m + 1];\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n for (int j = 1; j <= m; j++) {\n dp[j] = j;\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n for (int i = 1; i <= n; i++) {\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n int leftup = dp[0]; // \u6682\u5b58 dp[i-1, j-1]\n dp[0] = i;\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n for (int j = 1; j <= m; j++) {\n int temp = dp[j];\n if (s.charAt(i - 1) == t.charAt(j - 1)) {\n // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n dp[j] = leftup;\n } else {\n // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n dp[j] = Math.min(Math.min(dp[j - 1], dp[j]), leftup) + 1;\n }\n leftup = temp; // \u66f4\u65b0\u4e3a\u4e0b\u4e00\u8f6e\u7684 dp[i-1, j-1]\n }\n }\n return dp[m];\n}\n
edit_distance.cs/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint EditDistanceDPComp(string s, string t) {\n int n = s.Length, m = t.Length;\n int[] dp = new int[m + 1];\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n for (int j = 1; j <= m; j++) {\n dp[j] = j;\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n for (int i = 1; i <= n; i++) {\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n int leftup = dp[0]; // \u6682\u5b58 dp[i-1, j-1]\n dp[0] = i;\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n for (int j = 1; j <= m; j++) {\n int temp = dp[j];\n if (s[i - 1] == t[j - 1]) {\n // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n dp[j] = leftup;\n } else {\n // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n dp[j] = Math.Min(Math.Min(dp[j - 1], dp[j]), leftup) + 1;\n }\n leftup = temp; // \u66f4\u65b0\u4e3a\u4e0b\u4e00\u8f6e\u7684 dp[i-1, j-1]\n }\n }\n return dp[m];\n}\n
edit_distance.go/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc editDistanceDPComp(s string, t string) int {\n n := len(s)\n m := len(t)\n dp := make([]int, m+1)\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n for j := 1; j <= m; j++ {\n dp[j] = j\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n for i := 1; i <= n; i++ {\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n leftUp := dp[0] // \u6682\u5b58 dp[i-1, j-1]\n dp[0] = i\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n for j := 1; j <= m; j++ {\n temp := dp[j]\n if s[i-1] == t[j-1] {\n // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n dp[j] = leftUp\n } else {\n // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n dp[j] = MinInt(MinInt(dp[j-1], dp[j]), leftUp) + 1\n }\n leftUp = temp // \u66f4\u65b0\u4e3a\u4e0b\u4e00\u8f6e\u7684 dp[i-1, j-1]\n }\n }\n return dp[m]\n}\n
edit_distance.swift/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc editDistanceDPComp(s: String, t: String) -> Int {\n let n = s.utf8CString.count\n let m = t.utf8CString.count\n var dp = Array(repeating: 0, count: m + 1)\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n for j in stride(from: 1, through: m, by: 1) {\n dp[j] = j\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n for i in stride(from: 1, through: n, by: 1) {\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n var leftup = dp[0] // \u6682\u5b58 dp[i-1, j-1]\n dp[0] = i\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n for j in stride(from: 1, through: m, by: 1) {\n let temp = dp[j]\n if s.utf8CString[i - 1] == t.utf8CString[j - 1] {\n // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n dp[j] = leftup\n } else {\n // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n dp[j] = min(min(dp[j - 1], dp[j]), leftup) + 1\n }\n leftup = temp // \u66f4\u65b0\u4e3a\u4e0b\u4e00\u8f6e\u7684 dp[i-1, j-1]\n }\n }\n return dp[m]\n}\n
edit_distance.js/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction editDistanceDPComp(s, t) {\n const n = s.length,\n m = t.length;\n const dp = new Array(m + 1).fill(0);\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n for (let j = 1; j <= m; j++) {\n dp[j] = j;\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n for (let i = 1; i <= n; i++) {\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n let leftup = dp[0]; // \u6682\u5b58 dp[i-1, j-1]\n dp[0] = i;\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n for (let j = 1; j <= m; j++) {\n const temp = dp[j];\n if (s.charAt(i - 1) === t.charAt(j - 1)) {\n // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n dp[j] = leftup;\n } else {\n // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n dp[j] = Math.min(dp[j - 1], dp[j], leftup) + 1;\n }\n leftup = temp; // \u66f4\u65b0\u4e3a\u4e0b\u4e00\u8f6e\u7684 dp[i-1, j-1]\n }\n }\n return dp[m];\n}\n
edit_distance.ts/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction editDistanceDPComp(s: string, t: string): number {\n const n = s.length,\n m = t.length;\n const dp = new Array(m + 1).fill(0);\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n for (let j = 1; j <= m; j++) {\n dp[j] = j;\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n for (let i = 1; i <= n; i++) {\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n let leftup = dp[0]; // \u6682\u5b58 dp[i-1, j-1]\n dp[0] = i;\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n for (let j = 1; j <= m; j++) {\n const temp = dp[j];\n if (s.charAt(i - 1) === t.charAt(j - 1)) {\n // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n dp[j] = leftup;\n } else {\n // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n dp[j] = Math.min(dp[j - 1], dp[j], leftup) + 1;\n }\n leftup = temp; // \u66f4\u65b0\u4e3a\u4e0b\u4e00\u8f6e\u7684 dp[i-1, j-1]\n }\n }\n return dp[m];\n}\n
edit_distance.dart/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint editDistanceDPComp(String s, String t) {\n int n = s.length, m = t.length;\n List<int> dp = List.filled(m + 1, 0);\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n for (int j = 1; j <= m; j++) {\n dp[j] = j;\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n for (int i = 1; i <= n; i++) {\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n int leftup = dp[0]; // \u6682\u5b58 dp[i-1, j-1]\n dp[0] = i;\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n for (int j = 1; j <= m; j++) {\n int temp = dp[j];\n if (s[i - 1] == t[j - 1]) {\n // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n dp[j] = leftup;\n } else {\n // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n dp[j] = min(min(dp[j - 1], dp[j]), leftup) + 1;\n }\n leftup = temp; // \u66f4\u65b0\u4e3a\u4e0b\u4e00\u8f6e\u7684 dp[i-1, j-1]\n }\n }\n return dp[m];\n}\n
edit_distance.rs/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfn edit_distance_dp_comp(s: &str, t: &str) -> i32 {\n let (n, m) = (s.len(), t.len());\n let mut dp = vec![0; m + 1];\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n for j in 1..m {\n dp[j] = j as i32;\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n for i in 1..=n {\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n let mut leftup = dp[0]; // \u6682\u5b58 dp[i-1, j-1]\n dp[0] = i as i32;\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n for j in 1..=m {\n let temp = dp[j];\n if s.chars().nth(i - 1) == t.chars().nth(j - 1) {\n // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n dp[j] = leftup;\n } else {\n // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n dp[j] = std::cmp::min(std::cmp::min(dp[j - 1], dp[j]), leftup) + 1;\n }\n leftup = temp; // \u66f4\u65b0\u4e3a\u4e0b\u4e00\u8f6e\u7684 dp[i-1, j-1]\n }\n }\n dp[m]\n}\n
edit_distance.c/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint editDistanceDPComp(char *s, char *t, int n, int m) {\n int *dp = calloc(m + 1, sizeof(int));\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n for (int j = 1; j <= m; j++) {\n dp[j] = j;\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n for (int i = 1; i <= n; i++) {\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n int leftup = dp[0]; // \u6682\u5b58 dp[i-1, j-1]\n dp[0] = i;\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n for (int j = 1; j <= m; j++) {\n int temp = dp[j];\n if (s[i - 1] == t[j - 1]) {\n // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n dp[j] = leftup;\n } else {\n // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n dp[j] = myMin(myMin(dp[j - 1], dp[j]), leftup) + 1;\n }\n leftup = temp; // \u66f4\u65b0\u4e3a\u4e0b\u4e00\u8f6e\u7684 dp[i-1, j-1]\n }\n }\n int res = dp[m];\n // \u91ca\u653e\u5185\u5b58\n free(dp);\n return res;\n}\n
edit_distance.zig// \u7f16\u8f91\u8ddd\u79bb\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\nfn editDistanceDPComp(comptime s: []const u8, comptime t: []const u8) i32 {\n comptime var n = s.len;\n comptime var m = t.len;\n var dp = [_]i32{0} ** (m + 1);\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\n for (1..m + 1) |j| {\n dp[j] = @intCast(j);\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\n for (1..n + 1) |i| {\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\n var leftup = dp[0]; // \u6682\u5b58 dp[i-1, j-1]\n dp[0] = @intCast(i);\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\n for (1..m + 1) |j| {\n var temp = dp[j];\n if (s[i - 1] == t[j - 1]) {\n // \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\n dp[j] = leftup;\n } else {\n // \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\n dp[j] = @min(@min(dp[j - 1], dp[j]), leftup) + 1;\n }\n leftup = temp; // \u66f4\u65b0\u4e3a\u4e0b\u4e00\u8f6e\u7684 dp[i-1, j-1]\n }\n }\n return dp[m];\n}\n
"},{"location":"chapter_dynamic_programming/intro_to_dynamic_programming/","title":"14.1 \u00a0 \u521d\u63a2\u52a8\u6001\u89c4\u5212","text":"\u300c\u52a8\u6001\u89c4\u5212 dynamic programming\u300d\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u7b97\u6cd5\u8303\u5f0f\uff0c\u5b83\u5c06\u4e00\u4e2a\u95ee\u9898\u5206\u89e3\u4e3a\u4e00\u7cfb\u5217\u66f4\u5c0f\u7684\u5b50\u95ee\u9898\uff0c\u5e76\u901a\u8fc7\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\u6765\u907f\u514d\u91cd\u590d\u8ba1\u7b97\uff0c\u4ece\u800c\u5927\u5e45\u63d0\u5347\u65f6\u95f4\u6548\u7387\u3002
\u5728\u672c\u8282\u4e2d\uff0c\u6211\u4eec\u4ece\u4e00\u4e2a\u7ecf\u5178\u4f8b\u9898\u5165\u624b\uff0c\u5148\u7ed9\u51fa\u5b83\u7684\u66b4\u529b\u56de\u6eaf\u89e3\u6cd5\uff0c\u89c2\u5bdf\u5176\u4e2d\u5305\u542b\u7684\u91cd\u53e0\u5b50\u95ee\u9898\uff0c\u518d\u9010\u6b65\u5bfc\u51fa\u66f4\u9ad8\u6548\u7684\u52a8\u6001\u89c4\u5212\u89e3\u6cd5\u3002
\u722c\u697c\u68af
\u7ed9\u5b9a\u4e00\u4e2a\u5171\u6709 \\(n\\) \u9636\u7684\u697c\u68af\uff0c\u4f60\u6bcf\u6b65\u53ef\u4ee5\u4e0a \\(1\\) \u9636\u6216\u8005 \\(2\\) \u9636\uff0c\u8bf7\u95ee\u6709\u591a\u5c11\u79cd\u65b9\u6848\u53ef\u4ee5\u722c\u5230\u697c\u9876\uff1f
\u5982\u56fe 14-1 \u6240\u793a\uff0c\u5bf9\u4e8e\u4e00\u4e2a \\(3\\) \u9636\u697c\u68af\uff0c\u5171\u6709 \\(3\\) \u79cd\u65b9\u6848\u53ef\u4ee5\u722c\u5230\u697c\u9876\u3002
\u56fe 14-1 \u00a0 \u722c\u5230\u7b2c 3 \u9636\u7684\u65b9\u6848\u6570\u91cf
\u672c\u9898\u7684\u76ee\u6807\u662f\u6c42\u89e3\u65b9\u6848\u6570\u91cf\uff0c\u6211\u4eec\u53ef\u4ee5\u8003\u8651\u901a\u8fc7\u56de\u6eaf\u6765\u7a77\u4e3e\u6240\u6709\u53ef\u80fd\u6027\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u5c06\u722c\u697c\u68af\u60f3\u8c61\u4e3a\u4e00\u4e2a\u591a\u8f6e\u9009\u62e9\u7684\u8fc7\u7a0b\uff1a\u4ece\u5730\u9762\u51fa\u53d1\uff0c\u6bcf\u8f6e\u9009\u62e9\u4e0a \\(1\\) \u9636\u6216 \\(2\\) \u9636\uff0c\u6bcf\u5f53\u5230\u8fbe\u697c\u68af\u9876\u90e8\u65f6\u5c31\u5c06\u65b9\u6848\u6570\u91cf\u52a0 \\(1\\) \uff0c\u5f53\u8d8a\u8fc7\u697c\u68af\u9876\u90e8\u65f6\u5c31\u5c06\u5176\u526a\u679d\u3002\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig climbing_stairs_backtrack.pydef backtrack(choices: list[int], state: int, n: int, res: list[int]) -> int:\n \"\"\"\u56de\u6eaf\"\"\"\n # \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\n if state == n:\n res[0] += 1\n # \u904d\u5386\u6240\u6709\u9009\u62e9\n for choice in choices:\n # \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\n if state + choice > n:\n continue\n # \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n backtrack(choices, state + choice, n, res)\n # \u56de\u9000\n\ndef climbing_stairs_backtrack(n: int) -> int:\n \"\"\"\u722c\u697c\u68af\uff1a\u56de\u6eaf\"\"\"\n choices = [1, 2] # \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u9636\u6216 2 \u9636\n state = 0 # \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\n res = [0] # \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\n backtrack(choices, state, n, res)\n return res[0]\n
climbing_stairs_backtrack.cpp/* \u56de\u6eaf */\nvoid backtrack(vector<int> &choices, int state, int n, vector<int> &res) {\n // \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\n if (state == n)\n res[0]++;\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for (auto &choice : choices) {\n // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\n if (state + choice > n)\n continue;\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n backtrack(choices, state + choice, n, res);\n // \u56de\u9000\n }\n}\n\n/* \u722c\u697c\u68af\uff1a\u56de\u6eaf */\nint climbingStairsBacktrack(int n) {\n vector<int> choices = {1, 2}; // \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u9636\u6216 2 \u9636\n int state = 0; // \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\n vector<int> res = {0}; // \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\n backtrack(choices, state, n, res);\n return res[0];\n}\n
climbing_stairs_backtrack.java/* \u56de\u6eaf */\nvoid backtrack(List<Integer> choices, int state, int n, List<Integer> res) {\n // \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\n if (state == n)\n res.set(0, res.get(0) + 1);\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for (Integer choice : choices) {\n // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\n if (state + choice > n)\n continue;\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n backtrack(choices, state + choice, n, res);\n // \u56de\u9000\n }\n}\n\n/* \u722c\u697c\u68af\uff1a\u56de\u6eaf */\nint climbingStairsBacktrack(int n) {\n List<Integer> choices = Arrays.asList(1, 2); // \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u9636\u6216 2 \u9636\n int state = 0; // \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\n List<Integer> res = new ArrayList<>();\n res.add(0); // \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\n backtrack(choices, state, n, res);\n return res.get(0);\n}\n
climbing_stairs_backtrack.cs/* \u56de\u6eaf */\nvoid Backtrack(List<int> choices, int state, int n, List<int> res) {\n // \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\n if (state == n)\n res[0]++;\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n foreach (int choice in choices) {\n // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\n if (state + choice > n)\n continue;\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n Backtrack(choices, state + choice, n, res);\n // \u56de\u9000\n }\n}\n\n/* \u722c\u697c\u68af\uff1a\u56de\u6eaf */\nint ClimbingStairsBacktrack(int n) {\n List<int> choices = [1, 2]; // \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u9636\u6216 2 \u9636\n int state = 0; // \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\n List<int> res = [0]; // \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\n Backtrack(choices, state, n, res);\n return res[0];\n}\n
climbing_stairs_backtrack.go/* \u56de\u6eaf */\nfunc backtrack(choices []int, state, n int, res []int) {\n // \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\n if state == n {\n res[0] = res[0] + 1\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for _, choice := range choices {\n // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\n if state+choice > n {\n continue\n }\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n backtrack(choices, state+choice, n, res)\n // \u56de\u9000\n }\n}\n\n/* \u722c\u697c\u68af\uff1a\u56de\u6eaf */\nfunc climbingStairsBacktrack(n int) int {\n // \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u9636\u6216 2 \u9636\n choices := []int{1, 2}\n // \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\n state := 0\n res := make([]int, 1)\n // \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\n res[0] = 0\n backtrack(choices, state, n, res)\n return res[0]\n}\n
climbing_stairs_backtrack.swift/* \u56de\u6eaf */\nfunc backtrack(choices: [Int], state: Int, n: Int, res: inout [Int]) {\n // \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\n if state == n {\n res[0] += 1\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for choice in choices {\n // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\n if state + choice > n {\n continue\n }\n backtrack(choices: choices, state: state + choice, n: n, res: &res)\n }\n}\n\n/* \u722c\u697c\u68af\uff1a\u56de\u6eaf */\nfunc climbingStairsBacktrack(n: Int) -> Int {\n let choices = [1, 2] // \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u9636\u6216 2 \u9636\n let state = 0 // \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\n var res: [Int] = []\n res.append(0) // \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\n backtrack(choices: choices, state: state, n: n, res: &res)\n return res[0]\n}\n
climbing_stairs_backtrack.js/* \u56de\u6eaf */\nfunction backtrack(choices, state, n, res) {\n // \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\n if (state === n) res.set(0, res.get(0) + 1);\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for (const choice of choices) {\n // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\n if (state + choice > n) continue;\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n backtrack(choices, state + choice, n, res);\n // \u56de\u9000\n }\n}\n\n/* \u722c\u697c\u68af\uff1a\u56de\u6eaf */\nfunction climbingStairsBacktrack(n) {\n const choices = [1, 2]; // \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u9636\u6216 2 \u9636\n const state = 0; // \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\n const res = new Map();\n res.set(0, 0); // \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\n backtrack(choices, state, n, res);\n return res.get(0);\n}\n
climbing_stairs_backtrack.ts/* \u56de\u6eaf */\nfunction backtrack(\n choices: number[],\n state: number,\n n: number,\n res: Map<0, any>\n): void {\n // \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\n if (state === n) res.set(0, res.get(0) + 1);\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for (const choice of choices) {\n // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\n if (state + choice > n) continue;\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n backtrack(choices, state + choice, n, res);\n // \u56de\u9000\n }\n}\n\n/* \u722c\u697c\u68af\uff1a\u56de\u6eaf */\nfunction climbingStairsBacktrack(n: number): number {\n const choices = [1, 2]; // \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u9636\u6216 2 \u9636\n const state = 0; // \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\n const res = new Map();\n res.set(0, 0); // \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\n backtrack(choices, state, n, res);\n return res.get(0);\n}\n
climbing_stairs_backtrack.dart/* \u56de\u6eaf */\nvoid backtrack(List<int> choices, int state, int n, List<int> res) {\n // \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\n if (state == n) {\n res[0]++;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for (int choice in choices) {\n // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\n if (state + choice > n) continue;\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n backtrack(choices, state + choice, n, res);\n // \u56de\u9000\n }\n}\n\n/* \u722c\u697c\u68af\uff1a\u56de\u6eaf */\nint climbingStairsBacktrack(int n) {\n List<int> choices = [1, 2]; // \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u9636\u6216 2 \u9636\n int state = 0; // \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\n List<int> res = [];\n res.add(0); // \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\n backtrack(choices, state, n, res);\n return res[0];\n}\n
climbing_stairs_backtrack.rs/* \u56de\u6eaf */\nfn backtrack(choices: &[i32], state: i32, n: i32, res: &mut [i32]) {\n // \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\n if state == n { res[0] = res[0] + 1; }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for &choice in choices {\n // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\n if state + choice > n { continue; }\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n backtrack(choices, state + choice, n, res);\n // \u56de\u9000\n }\n}\n\n/* \u722c\u697c\u68af\uff1a\u56de\u6eaf */\nfn climbing_stairs_backtrack(n: usize) -> i32 {\n let choices = vec![ 1, 2 ]; // \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u9636\u6216 2 \u9636\n let state = 0; // \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\n let mut res = Vec::new();\n res.push(0); // \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\n backtrack(&choices, state, n as i32, &mut res);\n res[0]\n}\n
climbing_stairs_backtrack.c/* \u56de\u6eaf */\nvoid backtrack(int *choices, int state, int n, int *res, int len) {\n // \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\n if (state == n)\n res[0]++;\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for (int i = 0; i < len; i++) {\n int choice = choices[i];\n // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\n if (state + choice > n)\n continue;\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n backtrack(choices, state + choice, n, res, len);\n // \u56de\u9000\n }\n}\n\n/* \u722c\u697c\u68af\uff1a\u56de\u6eaf */\nint climbingStairsBacktrack(int n) {\n int choices[2] = {1, 2}; // \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u9636\u6216 2 \u9636\n int state = 0; // \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\n int *res = (int *)malloc(sizeof(int));\n *res = 0; // \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\n int len = sizeof(choices) / sizeof(int);\n backtrack(choices, state, n, res, len);\n int result = *res;\n free(res);\n return result;\n}\n
climbing_stairs_backtrack.zig// \u56de\u6eaf\nfn backtrack(choices: []i32, state: i32, n: i32, res: std.ArrayList(i32)) void {\n // \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\n if (state == n) {\n res.items[0] = res.items[0] + 1;\n }\n // \u904d\u5386\u6240\u6709\u9009\u62e9\n for (choices) |choice| {\n // \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\n if (state + choice > n) {\n continue;\n }\n // \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n backtrack(choices, state + choice, n, res);\n // \u56de\u9000\n }\n}\n\n// \u722c\u697c\u68af\uff1a\u56de\u6eaf\nfn climbingStairsBacktrack(n: usize) !i32 {\n var choices = [_]i32{ 1, 2 }; // \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u9636\u6216 2 \u9636\n var state: i32 = 0; // \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\n var res = std.ArrayList(i32).init(std.heap.page_allocator);\n defer res.deinit();\n try res.append(0); // \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\n backtrack(&choices, state, @intCast(n), res);\n return res.items[0];\n}\n
"},{"location":"chapter_dynamic_programming/intro_to_dynamic_programming/#1411","title":"14.1.1 \u00a0 \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u641c\u7d22","text":"\u56de\u6eaf\u7b97\u6cd5\u901a\u5e38\u5e76\u4e0d\u663e\u5f0f\u5730\u5bf9\u95ee\u9898\u8fdb\u884c\u62c6\u89e3\uff0c\u800c\u662f\u5c06\u6c42\u89e3\u95ee\u9898\u770b\u4f5c\u4e00\u7cfb\u5217\u51b3\u7b56\u6b65\u9aa4\uff0c\u901a\u8fc7\u8bd5\u63a2\u548c\u526a\u679d\uff0c\u641c\u7d22\u6240\u6709\u53ef\u80fd\u7684\u89e3\u3002
\u6211\u4eec\u53ef\u4ee5\u5c1d\u8bd5\u4ece\u95ee\u9898\u5206\u89e3\u7684\u89d2\u5ea6\u5206\u6790\u8fd9\u9053\u9898\u3002\u8bbe\u722c\u5230\u7b2c \\(i\\) \u9636\u5171\u6709 \\(dp[i]\\) \u79cd\u65b9\u6848\uff0c\u90a3\u4e48 \\(dp[i]\\) \u5c31\u662f\u539f\u95ee\u9898\uff0c\u5176\u5b50\u95ee\u9898\u5305\u62ec\uff1a
\\[ dp[i-1], dp[i-2], \\dots, dp[2], dp[1] \\]\u7531\u4e8e\u6bcf\u8f6e\u53ea\u80fd\u4e0a \\(1\\) \u9636\u6216 \\(2\\) \u9636\uff0c\u56e0\u6b64\u5f53\u6211\u4eec\u7ad9\u5728\u7b2c \\(i\\) \u9636\u697c\u68af\u4e0a\u65f6\uff0c\u4e0a\u4e00\u8f6e\u53ea\u53ef\u80fd\u7ad9\u5728\u7b2c \\(i - 1\\) \u9636\u6216\u7b2c \\(i - 2\\) \u9636\u4e0a\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u6211\u4eec\u53ea\u80fd\u4ece\u7b2c \\(i -1\\) \u9636\u6216\u7b2c \\(i - 2\\) \u9636\u8fc8\u5411\u7b2c \\(i\\) \u9636\u3002
\u7531\u6b64\u4fbf\u53ef\u5f97\u51fa\u4e00\u4e2a\u91cd\u8981\u63a8\u8bba\uff1a\u722c\u5230\u7b2c \\(i - 1\\) \u9636\u7684\u65b9\u6848\u6570\u52a0\u4e0a\u722c\u5230\u7b2c \\(i - 2\\) \u9636\u7684\u65b9\u6848\u6570\u5c31\u7b49\u4e8e\u722c\u5230\u7b2c \\(i\\) \u9636\u7684\u65b9\u6848\u6570\u3002\u516c\u5f0f\u5982\u4e0b\uff1a
\\[ dp[i] = dp[i-1] + dp[i-2] \\]\u8fd9\u610f\u5473\u7740\u5728\u722c\u697c\u68af\u95ee\u9898\u4e2d\uff0c\u5404\u4e2a\u5b50\u95ee\u9898\u4e4b\u95f4\u5b58\u5728\u9012\u63a8\u5173\u7cfb\uff0c\u539f\u95ee\u9898\u7684\u89e3\u53ef\u4ee5\u7531\u5b50\u95ee\u9898\u7684\u89e3\u6784\u5efa\u5f97\u6765\u3002\u56fe 14-2 \u5c55\u793a\u4e86\u8be5\u9012\u63a8\u5173\u7cfb\u3002
\u56fe 14-2 \u00a0 \u65b9\u6848\u6570\u91cf\u9012\u63a8\u5173\u7cfb
\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u9012\u63a8\u516c\u5f0f\u5f97\u5230\u66b4\u529b\u641c\u7d22\u89e3\u6cd5\u3002\u4ee5 \\(dp[n]\\) \u4e3a\u8d77\u59cb\u70b9\uff0c\u9012\u5f52\u5730\u5c06\u4e00\u4e2a\u8f83\u5927\u95ee\u9898\u62c6\u89e3\u4e3a\u4e24\u4e2a\u8f83\u5c0f\u95ee\u9898\u7684\u548c\uff0c\u76f4\u81f3\u5230\u8fbe\u6700\u5c0f\u5b50\u95ee\u9898 \\(dp[1]\\) \u548c \\(dp[2]\\) \u65f6\u8fd4\u56de\u3002\u5176\u4e2d\uff0c\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\u662f\u5df2\u77e5\u7684\uff0c\u5373 \\(dp[1] = 1\\)\u3001\\(dp[2] = 2\\) \uff0c\u8868\u793a\u722c\u5230\u7b2c \\(1\\)\u3001\\(2\\) \u9636\u5206\u522b\u6709 \\(1\\)\u3001\\(2\\) \u79cd\u65b9\u6848\u3002
\u89c2\u5bdf\u4ee5\u4e0b\u4ee3\u7801\uff0c\u5b83\u548c\u6807\u51c6\u56de\u6eaf\u4ee3\u7801\u90fd\u5c5e\u4e8e\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\uff0c\u4f46\u66f4\u52a0\u7b80\u6d01\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig climbing_stairs_dfs.pydef dfs(i: int) -> int:\n \"\"\"\u641c\u7d22\"\"\"\n # \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n if i == 1 or i == 2:\n return i\n # dp[i] = dp[i-1] + dp[i-2]\n count = dfs(i - 1) + dfs(i - 2)\n return count\n\ndef climbing_stairs_dfs(n: int) -> int:\n \"\"\"\u722c\u697c\u68af\uff1a\u641c\u7d22\"\"\"\n return dfs(n)\n
climbing_stairs_dfs.cpp/* \u641c\u7d22 */\nint dfs(int i) {\n // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n if (i == 1 || i == 2)\n return i;\n // dp[i] = dp[i-1] + dp[i-2]\n int count = dfs(i - 1) + dfs(i - 2);\n return count;\n}\n\n/* \u722c\u697c\u68af\uff1a\u641c\u7d22 */\nint climbingStairsDFS(int n) {\n return dfs(n);\n}\n
climbing_stairs_dfs.java/* \u641c\u7d22 */\nint dfs(int i) {\n // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n if (i == 1 || i == 2)\n return i;\n // dp[i] = dp[i-1] + dp[i-2]\n int count = dfs(i - 1) + dfs(i - 2);\n return count;\n}\n\n/* \u722c\u697c\u68af\uff1a\u641c\u7d22 */\nint climbingStairsDFS(int n) {\n return dfs(n);\n}\n
climbing_stairs_dfs.cs/* \u641c\u7d22 */\nint DFS(int i) {\n // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n if (i == 1 || i == 2)\n return i;\n // dp[i] = dp[i-1] + dp[i-2]\n int count = DFS(i - 1) + DFS(i - 2);\n return count;\n}\n\n/* \u722c\u697c\u68af\uff1a\u641c\u7d22 */\nint ClimbingStairsDFS(int n) {\n return DFS(n);\n}\n
climbing_stairs_dfs.go/* \u641c\u7d22 */\nfunc dfs(i int) int {\n // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n if i == 1 || i == 2 {\n return i\n }\n // dp[i] = dp[i-1] + dp[i-2]\n count := dfs(i-1) + dfs(i-2)\n return count\n}\n\n/* \u722c\u697c\u68af\uff1a\u641c\u7d22 */\nfunc climbingStairsDFS(n int) int {\n return dfs(n)\n}\n
climbing_stairs_dfs.swift/* \u641c\u7d22 */\nfunc dfs(i: Int) -> Int {\n // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n if i == 1 || i == 2 {\n return i\n }\n // dp[i] = dp[i-1] + dp[i-2]\n let count = dfs(i: i - 1) + dfs(i: i - 2)\n return count\n}\n\n/* \u722c\u697c\u68af\uff1a\u641c\u7d22 */\nfunc climbingStairsDFS(n: Int) -> Int {\n dfs(i: n)\n}\n
climbing_stairs_dfs.js/* \u641c\u7d22 */\nfunction dfs(i) {\n // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n if (i === 1 || i === 2) return i;\n // dp[i] = dp[i-1] + dp[i-2]\n const count = dfs(i - 1) + dfs(i - 2);\n return count;\n}\n\n/* \u722c\u697c\u68af\uff1a\u641c\u7d22 */\nfunction climbingStairsDFS(n) {\n return dfs(n);\n}\n
climbing_stairs_dfs.ts/* \u641c\u7d22 */\nfunction dfs(i: number): number {\n // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n if (i === 1 || i === 2) return i;\n // dp[i] = dp[i-1] + dp[i-2]\n const count = dfs(i - 1) + dfs(i - 2);\n return count;\n}\n\n/* \u722c\u697c\u68af\uff1a\u641c\u7d22 */\nfunction climbingStairsDFS(n: number): number {\n return dfs(n);\n}\n
climbing_stairs_dfs.dart/* \u641c\u7d22 */\nint dfs(int i) {\n // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n if (i == 1 || i == 2) return i;\n // dp[i] = dp[i-1] + dp[i-2]\n int count = dfs(i - 1) + dfs(i - 2);\n return count;\n}\n\n/* \u722c\u697c\u68af\uff1a\u641c\u7d22 */\nint climbingStairsDFS(int n) {\n return dfs(n);\n}\n
climbing_stairs_dfs.rs/* \u641c\u7d22 */\nfn dfs(i: usize) -> i32 {\n // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n if i == 1 || i == 2 { return i as i32; }\n // dp[i] = dp[i-1] + dp[i-2]\n let count = dfs(i - 1) + dfs(i - 2);\n count\n}\n\n/* \u722c\u697c\u68af\uff1a\u641c\u7d22 */\nfn climbing_stairs_dfs(n: usize) -> i32 {\n dfs(n) \n}\n
climbing_stairs_dfs.c/* \u641c\u7d22 */\nint dfs(int i) {\n // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n if (i == 1 || i == 2)\n return i;\n // dp[i] = dp[i-1] + dp[i-2]\n int count = dfs(i - 1) + dfs(i - 2);\n return count;\n}\n\n/* \u722c\u697c\u68af\uff1a\u641c\u7d22 */\nint climbingStairsDFS(int n) {\n return dfs(n);\n}\n
climbing_stairs_dfs.zig// \u641c\u7d22\nfn dfs(i: usize) i32 {\n // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n if (i == 1 or i == 2) {\n return @intCast(i);\n }\n // dp[i] = dp[i-1] + dp[i-2]\n var count = dfs(i - 1) + dfs(i - 2);\n return count;\n}\n\n// \u722c\u697c\u68af\uff1a\u641c\u7d22\nfn climbingStairsDFS(comptime n: usize) i32 {\n return dfs(n);\n}\n
\u56fe 14-3 \u5c55\u793a\u4e86\u66b4\u529b\u641c\u7d22\u5f62\u6210\u7684\u9012\u5f52\u6811\u3002\u5bf9\u4e8e\u95ee\u9898 \\(dp[n]\\) \uff0c\u5176\u9012\u5f52\u6811\u7684\u6df1\u5ea6\u4e3a \\(n\\) \uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(2^n)\\) \u3002\u6307\u6570\u9636\u5c5e\u4e8e\u7206\u70b8\u5f0f\u589e\u957f\uff0c\u5982\u679c\u6211\u4eec\u8f93\u5165\u4e00\u4e2a\u6bd4\u8f83\u5927\u7684 \\(n\\) \uff0c\u5219\u4f1a\u9677\u5165\u6f2b\u957f\u7684\u7b49\u5f85\u4e4b\u4e2d\u3002
\u56fe 14-3 \u00a0 \u722c\u697c\u68af\u5bf9\u5e94\u9012\u5f52\u6811
\u89c2\u5bdf\u56fe 14-3 \uff0c\u6307\u6570\u9636\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u662f\u201c\u91cd\u53e0\u5b50\u95ee\u9898\u201d\u5bfc\u81f4\u7684\u3002\u4f8b\u5982 \\(dp[9]\\) \u88ab\u5206\u89e3\u4e3a \\(dp[8]\\) \u548c \\(dp[7]\\) \uff0c\\(dp[8]\\) \u88ab\u5206\u89e3\u4e3a \\(dp[7]\\) \u548c \\(dp[6]\\) \uff0c\u4e24\u8005\u90fd\u5305\u542b\u5b50\u95ee\u9898 \\(dp[7]\\) \u3002
\u4ee5\u6b64\u7c7b\u63a8\uff0c\u5b50\u95ee\u9898\u4e2d\u5305\u542b\u66f4\u5c0f\u7684\u91cd\u53e0\u5b50\u95ee\u9898\uff0c\u5b50\u5b50\u5b59\u5b59\u65e0\u7a77\u5c3d\u4e5f\u3002\u7edd\u5927\u90e8\u5206\u8ba1\u7b97\u8d44\u6e90\u90fd\u6d6a\u8d39\u5728\u8fd9\u4e9b\u91cd\u53e0\u7684\u95ee\u9898\u4e0a\u3002
"},{"location":"chapter_dynamic_programming/intro_to_dynamic_programming/#1412","title":"14.1.2 \u00a0 \u65b9\u6cd5\u4e8c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22","text":"\u4e3a\u4e86\u63d0\u5347\u7b97\u6cd5\u6548\u7387\uff0c\u6211\u4eec\u5e0c\u671b\u6240\u6709\u7684\u91cd\u53e0\u5b50\u95ee\u9898\u90fd\u53ea\u88ab\u8ba1\u7b97\u4e00\u6b21\u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u58f0\u660e\u4e00\u4e2a\u6570\u7ec4 mem
\u6765\u8bb0\u5f55\u6bcf\u4e2a\u5b50\u95ee\u9898\u7684\u89e3\uff0c\u5e76\u5728\u641c\u7d22\u8fc7\u7a0b\u4e2d\u5c06\u91cd\u53e0\u5b50\u95ee\u9898\u526a\u679d\u3002
mem[i]
\uff0c\u4ee5\u4fbf\u4e4b\u540e\u4f7f\u7528\u3002mem[i]
\u4e2d\u83b7\u53d6\u7ed3\u679c\uff0c\u4ece\u800c\u907f\u514d\u91cd\u590d\u8ba1\u7b97\u8be5\u5b50\u95ee\u9898\u3002\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig climbing_stairs_dfs_mem.pydef dfs(i: int, mem: list[int]) -> int:\n \"\"\"\u8bb0\u5fc6\u5316\u641c\u7d22\"\"\"\n # \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n if i == 1 or i == 2:\n return i\n # \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\n if mem[i] != -1:\n return mem[i]\n # dp[i] = dp[i-1] + dp[i-2]\n count = dfs(i - 1, mem) + dfs(i - 2, mem)\n # \u8bb0\u5f55 dp[i]\n mem[i] = count\n return count\n\ndef climbing_stairs_dfs_mem(n: int) -> int:\n \"\"\"\u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22\"\"\"\n # mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\n mem = [-1] * (n + 1)\n return dfs(n, mem)\n
climbing_stairs_dfs_mem.cpp/* \u8bb0\u5fc6\u5316\u641c\u7d22 */\nint dfs(int i, vector<int> &mem) {\n // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n if (i == 1 || i == 2)\n return i;\n // \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\n if (mem[i] != -1)\n return mem[i];\n // dp[i] = dp[i-1] + dp[i-2]\n int count = dfs(i - 1, mem) + dfs(i - 2, mem);\n // \u8bb0\u5f55 dp[i]\n mem[i] = count;\n return count;\n}\n\n/* \u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint climbingStairsDFSMem(int n) {\n // mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\n vector<int> mem(n + 1, -1);\n return dfs(n, mem);\n}\n
climbing_stairs_dfs_mem.java/* \u8bb0\u5fc6\u5316\u641c\u7d22 */\nint dfs(int i, int[] mem) {\n // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n if (i == 1 || i == 2)\n return i;\n // \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\n if (mem[i] != -1)\n return mem[i];\n // dp[i] = dp[i-1] + dp[i-2]\n int count = dfs(i - 1, mem) + dfs(i - 2, mem);\n // \u8bb0\u5f55 dp[i]\n mem[i] = count;\n return count;\n}\n\n/* \u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint climbingStairsDFSMem(int n) {\n // mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\n int[] mem = new int[n + 1];\n Arrays.fill(mem, -1);\n return dfs(n, mem);\n}\n
climbing_stairs_dfs_mem.cs/* \u8bb0\u5fc6\u5316\u641c\u7d22 */\nint DFS(int i, int[] mem) {\n // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n if (i == 1 || i == 2)\n return i;\n // \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\n if (mem[i] != -1)\n return mem[i];\n // dp[i] = dp[i-1] + dp[i-2]\n int count = DFS(i - 1, mem) + DFS(i - 2, mem);\n // \u8bb0\u5f55 dp[i]\n mem[i] = count;\n return count;\n}\n\n/* \u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint ClimbingStairsDFSMem(int n) {\n // mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\n int[] mem = new int[n + 1];\n Array.Fill(mem, -1);\n return DFS(n, mem);\n}\n
climbing_stairs_dfs_mem.go/* \u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunc dfsMem(i int, mem []int) int {\n // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n if i == 1 || i == 2 {\n return i\n }\n // \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\n if mem[i] != -1 {\n return mem[i]\n }\n // dp[i] = dp[i-1] + dp[i-2]\n count := dfsMem(i-1, mem) + dfsMem(i-2, mem)\n // \u8bb0\u5f55 dp[i]\n mem[i] = count\n return count\n}\n\n/* \u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunc climbingStairsDFSMem(n int) int {\n // mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\n mem := make([]int, n+1)\n for i := range mem {\n mem[i] = -1\n }\n return dfsMem(n, mem)\n}\n
climbing_stairs_dfs_mem.swift/* \u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunc dfs(i: Int, mem: inout [Int]) -> Int {\n // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n if i == 1 || i == 2 {\n return i\n }\n // \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\n if mem[i] != -1 {\n return mem[i]\n }\n // dp[i] = dp[i-1] + dp[i-2]\n let count = dfs(i: i - 1, mem: &mem) + dfs(i: i - 2, mem: &mem)\n // \u8bb0\u5f55 dp[i]\n mem[i] = count\n return count\n}\n\n/* \u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunc climbingStairsDFSMem(n: Int) -> Int {\n // mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\n var mem = Array(repeating: -1, count: n + 1)\n return dfs(i: n, mem: &mem)\n}\n
climbing_stairs_dfs_mem.js/* \u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunction dfs(i, mem) {\n // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n if (i === 1 || i === 2) return i;\n // \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\n if (mem[i] != -1) return mem[i];\n // dp[i] = dp[i-1] + dp[i-2]\n const count = dfs(i - 1, mem) + dfs(i - 2, mem);\n // \u8bb0\u5f55 dp[i]\n mem[i] = count;\n return count;\n}\n\n/* \u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunction climbingStairsDFSMem(n) {\n // mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\n const mem = new Array(n + 1).fill(-1);\n return dfs(n, mem);\n}\n
climbing_stairs_dfs_mem.ts/* \u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunction dfs(i: number, mem: number[]): number {\n // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n if (i === 1 || i === 2) return i;\n // \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\n if (mem[i] != -1) return mem[i];\n // dp[i] = dp[i-1] + dp[i-2]\n const count = dfs(i - 1, mem) + dfs(i - 2, mem);\n // \u8bb0\u5f55 dp[i]\n mem[i] = count;\n return count;\n}\n\n/* \u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunction climbingStairsDFSMem(n: number): number {\n // mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\n const mem = new Array(n + 1).fill(-1);\n return dfs(n, mem);\n}\n
climbing_stairs_dfs_mem.dart/* \u8bb0\u5fc6\u5316\u641c\u7d22 */\nint dfs(int i, List<int> mem) {\n // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n if (i == 1 || i == 2) return i;\n // \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\n if (mem[i] != -1) return mem[i];\n // dp[i] = dp[i-1] + dp[i-2]\n int count = dfs(i - 1, mem) + dfs(i - 2, mem);\n // \u8bb0\u5f55 dp[i]\n mem[i] = count;\n return count;\n}\n\n/* \u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint climbingStairsDFSMem(int n) {\n // mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\n List<int> mem = List.filled(n + 1, -1);\n return dfs(n, mem);\n}\n
climbing_stairs_dfs_mem.rs/* \u8bb0\u5fc6\u5316\u641c\u7d22 */\nfn dfs(i: usize, mem: &mut [i32]) -> i32 {\n // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n if i == 1 || i == 2 { return i as i32; }\n // \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\n if mem[i] != -1 { return mem[i]; }\n // dp[i] = dp[i-1] + dp[i-2]\n let count = dfs(i - 1, mem) + dfs(i - 2, mem);\n // \u8bb0\u5f55 dp[i]\n mem[i] = count;\n count\n}\n\n/* \u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfn climbing_stairs_dfs_mem(n: usize) -> i32 {\n // mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\n let mut mem = vec![-1; n + 1];\n dfs(n, &mut mem)\n}\n
climbing_stairs_dfs_mem.c/* \u8bb0\u5fc6\u5316\u641c\u7d22 */\nint dfs(int i, int *mem) {\n // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n if (i == 1 || i == 2)\n return i;\n // \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\n if (mem[i] != -1)\n return mem[i];\n // dp[i] = dp[i-1] + dp[i-2]\n int count = dfs(i - 1, mem) + dfs(i - 2, mem);\n // \u8bb0\u5f55 dp[i]\n mem[i] = count;\n return count;\n}\n\n/* \u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint climbingStairsDFSMem(int n) {\n // mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\n int *mem = (int *)malloc((n + 1) * sizeof(int));\n for (int i = 0; i <= n; i++) {\n mem[i] = -1;\n }\n int result = dfs(n, mem);\n free(mem);\n return result;\n}\n
climbing_stairs_dfs_mem.zig// \u8bb0\u5fc6\u5316\u641c\u7d22\nfn dfs(i: usize, mem: []i32) i32 {\n // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n if (i == 1 or i == 2) {\n return @intCast(i);\n }\n // \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\n if (mem[i] != -1) {\n return mem[i];\n }\n // dp[i] = dp[i-1] + dp[i-2]\n var count = dfs(i - 1, mem) + dfs(i - 2, mem);\n // \u8bb0\u5f55 dp[i]\n mem[i] = count;\n return count;\n}\n\n// \u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22\nfn climbingStairsDFSMem(comptime n: usize) i32 {\n // mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\n var mem = [_]i32{ -1 } ** (n + 1);\n return dfs(n, &mem);\n}\n
\u89c2\u5bdf\u56fe 14-4 \uff0c\u7ecf\u8fc7\u8bb0\u5fc6\u5316\u5904\u7406\u540e\uff0c\u6240\u6709\u91cd\u53e0\u5b50\u95ee\u9898\u90fd\u53ea\u9700\u8ba1\u7b97\u4e00\u6b21\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4f18\u5316\u81f3 \\(O(n)\\) \uff0c\u8fd9\u662f\u4e00\u4e2a\u5de8\u5927\u7684\u98de\u8dc3\u3002
\u56fe 14-4 \u00a0 \u8bb0\u5fc6\u5316\u641c\u7d22\u5bf9\u5e94\u9012\u5f52\u6811
"},{"location":"chapter_dynamic_programming/intro_to_dynamic_programming/#1413","title":"14.1.3 \u00a0 \u65b9\u6cd5\u4e09\uff1a\u52a8\u6001\u89c4\u5212","text":"\u8bb0\u5fc6\u5316\u641c\u7d22\u662f\u4e00\u79cd\u201c\u4ece\u9876\u81f3\u5e95\u201d\u7684\u65b9\u6cd5\uff1a\u6211\u4eec\u4ece\u539f\u95ee\u9898\uff08\u6839\u8282\u70b9\uff09\u5f00\u59cb\uff0c\u9012\u5f52\u5730\u5c06\u8f83\u5927\u5b50\u95ee\u9898\u5206\u89e3\u4e3a\u8f83\u5c0f\u5b50\u95ee\u9898\uff0c\u76f4\u81f3\u89e3\u5df2\u77e5\u7684\u6700\u5c0f\u5b50\u95ee\u9898\uff08\u53f6\u8282\u70b9\uff09\u3002\u4e4b\u540e\uff0c\u901a\u8fc7\u56de\u6eaf\u9010\u5c42\u6536\u96c6\u5b50\u95ee\u9898\u7684\u89e3\uff0c\u6784\u5efa\u51fa\u539f\u95ee\u9898\u7684\u89e3\u3002
\u4e0e\u4e4b\u76f8\u53cd\uff0c\u52a8\u6001\u89c4\u5212\u662f\u4e00\u79cd\u201c\u4ece\u5e95\u81f3\u9876\u201d\u7684\u65b9\u6cd5\uff1a\u4ece\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\u5f00\u59cb\uff0c\u8fed\u4ee3\u5730\u6784\u5efa\u66f4\u5927\u5b50\u95ee\u9898\u7684\u89e3\uff0c\u76f4\u81f3\u5f97\u5230\u539f\u95ee\u9898\u7684\u89e3\u3002
\u7531\u4e8e\u52a8\u6001\u89c4\u5212\u4e0d\u5305\u542b\u56de\u6eaf\u8fc7\u7a0b\uff0c\u56e0\u6b64\u53ea\u9700\u4f7f\u7528\u5faa\u73af\u8fed\u4ee3\u5b9e\u73b0\uff0c\u65e0\u987b\u4f7f\u7528\u9012\u5f52\u3002\u5728\u4ee5\u4e0b\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521d\u59cb\u5316\u4e00\u4e2a\u6570\u7ec4 dp
\u6765\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\uff0c\u5b83\u8d77\u5230\u4e86\u4e0e\u8bb0\u5fc6\u5316\u641c\u7d22\u4e2d\u6570\u7ec4 mem
\u76f8\u540c\u7684\u8bb0\u5f55\u4f5c\u7528\uff1a
def climbing_stairs_dp(n: int) -> int:\n \"\"\"\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212\"\"\"\n if n == 1 or n == 2:\n return n\n # \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n dp = [0] * (n + 1)\n # \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n dp[1], dp[2] = 1, 2\n # \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n for i in range(3, n + 1):\n dp[i] = dp[i - 1] + dp[i - 2]\n return dp[n]\n
climbing_stairs_dp.cpp/* \u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nint climbingStairsDP(int n) {\n if (n == 1 || n == 2)\n return n;\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n vector<int> dp(n + 1);\n // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n dp[1] = 1;\n dp[2] = 2;\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n for (int i = 3; i <= n; i++) {\n dp[i] = dp[i - 1] + dp[i - 2];\n }\n return dp[n];\n}\n
climbing_stairs_dp.java/* \u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nint climbingStairsDP(int n) {\n if (n == 1 || n == 2)\n return n;\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n int[] dp = new int[n + 1];\n // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n dp[1] = 1;\n dp[2] = 2;\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n for (int i = 3; i <= n; i++) {\n dp[i] = dp[i - 1] + dp[i - 2];\n }\n return dp[n];\n}\n
climbing_stairs_dp.cs/* \u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nint ClimbingStairsDP(int n) {\n if (n == 1 || n == 2)\n return n;\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n int[] dp = new int[n + 1];\n // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n dp[1] = 1;\n dp[2] = 2;\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n for (int i = 3; i <= n; i++) {\n dp[i] = dp[i - 1] + dp[i - 2];\n }\n return dp[n];\n}\n
climbing_stairs_dp.go/* \u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc climbingStairsDP(n int) int {\n if n == 1 || n == 2 {\n return n\n }\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n dp := make([]int, n+1)\n // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n dp[1] = 1\n dp[2] = 2\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n for i := 3; i <= n; i++ {\n dp[i] = dp[i-1] + dp[i-2]\n }\n return dp[n]\n}\n
climbing_stairs_dp.swift/* \u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc climbingStairsDP(n: Int) -> Int {\n if n == 1 || n == 2 {\n return n\n }\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n var dp = Array(repeating: 0, count: n + 1)\n // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n dp[1] = 1\n dp[2] = 2\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n for i in stride(from: 3, through: n, by: 1) {\n dp[i] = dp[i - 1] + dp[i - 2]\n }\n return dp[n]\n}\n
climbing_stairs_dp.js/* \u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction climbingStairsDP(n) {\n if (n === 1 || n === 2) return n;\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n const dp = new Array(n + 1).fill(-1);\n // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n dp[1] = 1;\n dp[2] = 2;\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n for (let i = 3; i <= n; i++) {\n dp[i] = dp[i - 1] + dp[i - 2];\n }\n return dp[n];\n}\n
climbing_stairs_dp.ts/* \u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction climbingStairsDP(n: number): number {\n if (n === 1 || n === 2) return n;\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n const dp = new Array(n + 1).fill(-1);\n // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n dp[1] = 1;\n dp[2] = 2;\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n for (let i = 3; i <= n; i++) {\n dp[i] = dp[i - 1] + dp[i - 2];\n }\n return dp[n];\n}\n
climbing_stairs_dp.dart/* \u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nint climbingStairsDP(int n) {\n if (n == 1 || n == 2) return n;\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n List<int> dp = List.filled(n + 1, 0);\n // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n dp[1] = 1;\n dp[2] = 2;\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n for (int i = 3; i <= n; i++) {\n dp[i] = dp[i - 1] + dp[i - 2];\n }\n return dp[n];\n}\n
climbing_stairs_dp.rs/* \u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nfn climbing_stairs_dp(n: usize) -> i32 {\n // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n if n == 1 || n == 2 { return n as i32; }\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n let mut dp = vec![-1; n + 1];\n // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n dp[1] = 1;\n dp[2] = 2;\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n for i in 3..=n {\n dp[i] = dp[i - 1] + dp[i - 2];\n }\n dp[n]\n}\n
climbing_stairs_dp.c/* \u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nint climbingStairsDP(int n) {\n if (n == 1 || n == 2)\n return n;\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n int *dp = (int *)malloc((n + 1) * sizeof(int));\n // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n dp[1] = 1;\n dp[2] = 2;\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n for (int i = 3; i <= n; i++) {\n dp[i] = dp[i - 1] + dp[i - 2];\n }\n int result = dp[n];\n free(dp);\n return result;\n}\n
climbing_stairs_dp.zig// \u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212\nfn climbingStairsDP(comptime n: usize) i32 {\n // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n if (n == 1 or n == 2) {\n return @intCast(n);\n }\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\n var dp = [_]i32{-1} ** (n + 1);\n // \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\n dp[1] = 1;\n dp[2] = 2;\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n for (3..n + 1) |i| {\n dp[i] = dp[i - 1] + dp[i - 2];\n }\n return dp[n];\n}\n
\u56fe 14-5 \u6a21\u62df\u4e86\u4ee5\u4e0a\u4ee3\u7801\u7684\u6267\u884c\u8fc7\u7a0b\u3002
\u56fe 14-5 \u00a0 \u722c\u697c\u68af\u7684\u52a8\u6001\u89c4\u5212\u8fc7\u7a0b
\u4e0e\u56de\u6eaf\u7b97\u6cd5\u4e00\u6837\uff0c\u52a8\u6001\u89c4\u5212\u4e5f\u4f7f\u7528\u201c\u72b6\u6001\u201d\u6982\u5ff5\u6765\u8868\u793a\u95ee\u9898\u6c42\u89e3\u7684\u7279\u5b9a\u9636\u6bb5\uff0c\u6bcf\u4e2a\u72b6\u6001\u90fd\u5bf9\u5e94\u4e00\u4e2a\u5b50\u95ee\u9898\u4ee5\u53ca\u76f8\u5e94\u7684\u5c40\u90e8\u6700\u4f18\u89e3\u3002\u4f8b\u5982\uff0c\u722c\u697c\u68af\u95ee\u9898\u7684\u72b6\u6001\u5b9a\u4e49\u4e3a\u5f53\u524d\u6240\u5728\u697c\u68af\u9636\u6570 \\(i\\) \u3002
\u6839\u636e\u4ee5\u4e0a\u5185\u5bb9\uff0c\u6211\u4eec\u53ef\u4ee5\u603b\u7ed3\u51fa\u52a8\u6001\u89c4\u5212\u7684\u5e38\u7528\u672f\u8bed\u3002
dp
\u79f0\u4e3a\u300c\\(dp\\) \u8868\u300d\uff0c\\(dp[i]\\) \u8868\u793a\u72b6\u6001 \\(i\\) \u5bf9\u5e94\u5b50\u95ee\u9898\u7684\u89e3\u3002\u7ec6\u5fc3\u7684\u8bfb\u8005\u53ef\u80fd\u53d1\u73b0\u4e86\uff0c\u7531\u4e8e \\(dp[i]\\) \u53ea\u4e0e \\(dp[i-1]\\) \u548c \\(dp[i-2]\\) \u6709\u5173\uff0c\u56e0\u6b64\u6211\u4eec\u65e0\u987b\u4f7f\u7528\u4e00\u4e2a\u6570\u7ec4 dp
\u6765\u5b58\u50a8\u6240\u6709\u5b50\u95ee\u9898\u7684\u89e3\uff0c\u800c\u53ea\u9700\u4e24\u4e2a\u53d8\u91cf\u6eda\u52a8\u524d\u8fdb\u5373\u53ef\u3002\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a
def climbing_stairs_dp_comp(n: int) -> int:\n \"\"\"\u722c\u697c\u68af\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\"\"\"\n if n == 1 or n == 2:\n return n\n a, b = 1, 2\n for _ in range(3, n + 1):\n a, b = b, a + b\n return b\n
climbing_stairs_dp.cpp/* \u722c\u697c\u68af\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint climbingStairsDPComp(int n) {\n if (n == 1 || n == 2)\n return n;\n int a = 1, b = 2;\n for (int i = 3; i <= n; i++) {\n int tmp = b;\n b = a + b;\n a = tmp;\n }\n return b;\n}\n
climbing_stairs_dp.java/* \u722c\u697c\u68af\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint climbingStairsDPComp(int n) {\n if (n == 1 || n == 2)\n return n;\n int a = 1, b = 2;\n for (int i = 3; i <= n; i++) {\n int tmp = b;\n b = a + b;\n a = tmp;\n }\n return b;\n}\n
climbing_stairs_dp.cs/* \u722c\u697c\u68af\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint ClimbingStairsDPComp(int n) {\n if (n == 1 || n == 2)\n return n;\n int a = 1, b = 2;\n for (int i = 3; i <= n; i++) {\n int tmp = b;\n b = a + b;\n a = tmp;\n }\n return b;\n}\n
climbing_stairs_dp.go/* \u722c\u697c\u68af\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc climbingStairsDPComp(n int) int {\n if n == 1 || n == 2 {\n return n\n }\n a, b := 1, 2\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\n for i := 3; i <= n; i++ {\n a, b = b, a+b\n }\n return b\n}\n
climbing_stairs_dp.swift/* \u722c\u697c\u68af\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc climbingStairsDPComp(n: Int) -> Int {\n if n == 1 || n == 2 {\n return n\n }\n var a = 1\n var b = 2\n for _ in stride(from: 3, through: n, by: 1) {\n (a, b) = (b, a + b)\n }\n return b\n}\n
climbing_stairs_dp.js/* \u722c\u697c\u68af\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction climbingStairsDPComp(n) {\n if (n === 1 || n === 2) return n;\n let a = 1,\n b = 2;\n for (let i = 3; i <= n; i++) {\n const tmp = b;\n b = a + b;\n a = tmp;\n }\n return b;\n}\n
climbing_stairs_dp.ts/* \u722c\u697c\u68af\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction climbingStairsDPComp(n: number): number {\n if (n === 1 || n === 2) return n;\n let a = 1,\n b = 2;\n for (let i = 3; i <= n; i++) {\n const tmp = b;\n b = a + b;\n a = tmp;\n }\n return b;\n}\n
climbing_stairs_dp.dart/* \u722c\u697c\u68af\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint climbingStairsDPComp(int n) {\n if (n == 1 || n == 2) return n;\n int a = 1, b = 2;\n for (int i = 3; i <= n; i++) {\n int tmp = b;\n b = a + b;\n a = tmp;\n }\n return b;\n}\n
climbing_stairs_dp.rs/* \u722c\u697c\u68af\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfn climbing_stairs_dp_comp(n: usize) -> i32 {\n if n == 1 || n == 2 { return n as i32; }\n let (mut a, mut b) = (1, 2);\n for _ in 3..=n {\n let tmp = b;\n b = a + b;\n a = tmp;\n }\n b\n}\n
climbing_stairs_dp.c/* \u722c\u697c\u68af\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint climbingStairsDPComp(int n) {\n if (n == 1 || n == 2)\n return n;\n int a = 1, b = 2;\n for (int i = 3; i <= n; i++) {\n int tmp = b;\n b = a + b;\n a = tmp;\n }\n return b;\n}\n
climbing_stairs_dp.zig// \u722c\u697c\u68af\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\nfn climbingStairsDPComp(comptime n: usize) i32 {\n if (n == 1 or n == 2) {\n return @intCast(n);\n }\n var a: i32 = 1;\n var b: i32 = 2;\n for (3..n + 1) |_| {\n var tmp = b;\n b = a + b;\n a = tmp;\n }\n return b;\n}\n
\u89c2\u5bdf\u4ee5\u4e0a\u4ee3\u7801\uff0c\u7531\u4e8e\u7701\u53bb\u4e86\u6570\u7ec4 dp
\u5360\u7528\u7684\u7a7a\u95f4\uff0c\u56e0\u6b64\u7a7a\u95f4\u590d\u6742\u5ea6\u4ece \\(O(n)\\) \u964d\u81f3 \\(O(1)\\) \u3002
\u5728\u52a8\u6001\u89c4\u5212\u95ee\u9898\u4e2d\uff0c\u5f53\u524d\u72b6\u6001\u5f80\u5f80\u4ec5\u4e0e\u524d\u9762\u6709\u9650\u4e2a\u72b6\u6001\u6709\u5173\uff0c\u8fd9\u65f6\u6211\u4eec\u53ef\u4ee5\u53ea\u4fdd\u7559\u5fc5\u8981\u7684\u72b6\u6001\uff0c\u901a\u8fc7\u201c\u964d\u7ef4\u201d\u6765\u8282\u7701\u5185\u5b58\u7a7a\u95f4\u3002\u8fd9\u79cd\u7a7a\u95f4\u4f18\u5316\u6280\u5de7\u88ab\u79f0\u4e3a\u201c\u6eda\u52a8\u53d8\u91cf\u201d\u6216\u201c\u6eda\u52a8\u6570\u7ec4\u201d\u3002
"},{"location":"chapter_dynamic_programming/knapsack_problem/","title":"14.4 \u00a0 0-1 \u80cc\u5305\u95ee\u9898","text":"\u80cc\u5305\u95ee\u9898\u662f\u4e00\u4e2a\u975e\u5e38\u597d\u7684\u52a8\u6001\u89c4\u5212\u5165\u95e8\u9898\u76ee\uff0c\u662f\u52a8\u6001\u89c4\u5212\u4e2d\u6700\u5e38\u89c1\u7684\u95ee\u9898\u5f62\u5f0f\u3002\u5176\u5177\u6709\u5f88\u591a\u53d8\u79cd\uff0c\u4f8b\u5982 0-1 \u80cc\u5305\u95ee\u9898\u3001\u5b8c\u5168\u80cc\u5305\u95ee\u9898\u3001\u591a\u91cd\u80cc\u5305\u95ee\u9898\u7b49\u3002
\u5728\u672c\u8282\u4e2d\uff0c\u6211\u4eec\u5148\u6765\u6c42\u89e3\u6700\u5e38\u89c1\u7684 0-1 \u80cc\u5305\u95ee\u9898\u3002
Question
\u7ed9\u5b9a \\(n\\) \u4e2a\u7269\u54c1\uff0c\u7b2c \\(i\\) \u4e2a\u7269\u54c1\u7684\u91cd\u91cf\u4e3a \\(wgt[i-1]\\)\u3001\u4ef7\u503c\u4e3a \\(val[i-1]\\) \uff0c\u548c\u4e00\u4e2a\u5bb9\u91cf\u4e3a \\(cap\\) \u7684\u80cc\u5305\u3002\u6bcf\u4e2a\u7269\u54c1\u53ea\u80fd\u9009\u62e9\u4e00\u6b21\uff0c\u95ee\u5728\u9650\u5b9a\u80cc\u5305\u5bb9\u91cf\u4e0b\u80fd\u653e\u5165\u7269\u54c1\u7684\u6700\u5927\u4ef7\u503c\u3002
\u89c2\u5bdf\u56fe 14-17 \uff0c\u7531\u4e8e\u7269\u54c1\u7f16\u53f7 \\(i\\) \u4ece \\(1\\) \u5f00\u59cb\u8ba1\u6570\uff0c\u6570\u7ec4\u7d22\u5f15\u4ece \\(0\\) \u5f00\u59cb\u8ba1\u6570\uff0c\u56e0\u6b64\u7269\u54c1 \\(i\\) \u5bf9\u5e94\u91cd\u91cf \\(wgt[i-1]\\) \u548c\u4ef7\u503c \\(val[i-1]\\) \u3002
\u56fe 14-17 \u00a0 0-1 \u80cc\u5305\u7684\u793a\u4f8b\u6570\u636e
\u6211\u4eec\u53ef\u4ee5\u5c06 0-1 \u80cc\u5305\u95ee\u9898\u770b\u4f5c\u4e00\u4e2a\u7531 \\(n\\) \u8f6e\u51b3\u7b56\u7ec4\u6210\u7684\u8fc7\u7a0b\uff0c\u5bf9\u4e8e\u6bcf\u4e2a\u7269\u4f53\u90fd\u6709\u4e0d\u653e\u5165\u548c\u653e\u5165\u4e24\u79cd\u51b3\u7b56\uff0c\u56e0\u6b64\u8be5\u95ee\u9898\u6ee1\u8db3\u51b3\u7b56\u6811\u6a21\u578b\u3002
\u8be5\u95ee\u9898\u7684\u76ee\u6807\u662f\u6c42\u89e3\u201c\u5728\u9650\u5b9a\u80cc\u5305\u5bb9\u91cf\u4e0b\u80fd\u653e\u5165\u7269\u54c1\u7684\u6700\u5927\u4ef7\u503c\u201d\uff0c\u56e0\u6b64\u8f83\u5927\u6982\u7387\u662f\u4e00\u4e2a\u52a8\u6001\u89c4\u5212\u95ee\u9898\u3002
\u7b2c\u4e00\u6b65\uff1a\u601d\u8003\u6bcf\u8f6e\u7684\u51b3\u7b56\uff0c\u5b9a\u4e49\u72b6\u6001\uff0c\u4ece\u800c\u5f97\u5230 \\(dp\\) \u8868
\u5bf9\u4e8e\u6bcf\u4e2a\u7269\u54c1\u6765\u8bf4\uff0c\u4e0d\u653e\u5165\u80cc\u5305\uff0c\u80cc\u5305\u5bb9\u91cf\u4e0d\u53d8\uff1b\u653e\u5165\u80cc\u5305\uff0c\u80cc\u5305\u5bb9\u91cf\u51cf\u5c0f\u3002\u7531\u6b64\u53ef\u5f97\u72b6\u6001\u5b9a\u4e49\uff1a\u5f53\u524d\u7269\u54c1\u7f16\u53f7 \\(i\\) \u548c\u5269\u4f59\u80cc\u5305\u5bb9\u91cf \\(c\\) \uff0c\u8bb0\u4e3a \\([i, c]\\) \u3002
\u72b6\u6001 \\([i, c]\\) \u5bf9\u5e94\u7684\u5b50\u95ee\u9898\u4e3a\uff1a\u524d \\(i\\) \u4e2a\u7269\u54c1\u5728\u5269\u4f59\u5bb9\u91cf\u4e3a \\(c\\) \u7684\u80cc\u5305\u4e2d\u7684\u6700\u5927\u4ef7\u503c\uff0c\u8bb0\u4e3a \\(dp[i, c]\\) \u3002
\u5f85\u6c42\u89e3\u7684\u662f \\(dp[n, cap]\\) \uff0c\u56e0\u6b64\u9700\u8981\u4e00\u4e2a\u5c3a\u5bf8\u4e3a \\((n+1) \\times (cap+1)\\) \u7684\u4e8c\u7ef4 \\(dp\\) \u8868\u3002
\u7b2c\u4e8c\u6b65\uff1a\u627e\u51fa\u6700\u4f18\u5b50\u7ed3\u6784\uff0c\u8fdb\u800c\u63a8\u5bfc\u51fa\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b
\u5f53\u6211\u4eec\u505a\u51fa\u7269\u54c1 \\(i\\) \u7684\u51b3\u7b56\u540e\uff0c\u5269\u4f59\u7684\u662f\u524d \\(i-1\\) \u4e2a\u7269\u54c1\u7684\u51b3\u7b56\uff0c\u53ef\u5206\u4e3a\u4ee5\u4e0b\u4e24\u79cd\u60c5\u51b5\u3002
\u4e0a\u8ff0\u5206\u6790\u5411\u6211\u4eec\u63ed\u793a\u4e86\u672c\u9898\u7684\u6700\u4f18\u5b50\u7ed3\u6784\uff1a\u6700\u5927\u4ef7\u503c \\(dp[i, c]\\) \u7b49\u4e8e\u4e0d\u653e\u5165\u7269\u54c1 \\(i\\) \u548c\u653e\u5165\u7269\u54c1 \\(i\\) \u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\u3002\u7531\u6b64\u53ef\u63a8\u5bfc\u51fa\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\uff1a
\\[ dp[i, c] = \\max(dp[i-1, c], dp[i-1, c - wgt[i-1]] + val[i-1]) \\]\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u82e5\u5f53\u524d\u7269\u54c1\u91cd\u91cf \\(wgt[i - 1]\\) \u8d85\u51fa\u5269\u4f59\u80cc\u5305\u5bb9\u91cf \\(c\\) \uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\u3002
\u7b2c\u4e09\u6b65\uff1a\u786e\u5b9a\u8fb9\u754c\u6761\u4ef6\u548c\u72b6\u6001\u8f6c\u79fb\u987a\u5e8f
\u5f53\u65e0\u7269\u54c1\u6216\u65e0\u5269\u4f59\u80cc\u5305\u5bb9\u91cf\u65f6\u6700\u5927\u4ef7\u503c\u4e3a \\(0\\) \uff0c\u5373\u9996\u5217 \\(dp[i, 0]\\) \u548c\u9996\u884c \\(dp[0, c]\\) \u90fd\u7b49\u4e8e \\(0\\) \u3002
\u5f53\u524d\u72b6\u6001 \\([i, c]\\) \u4ece\u4e0a\u65b9\u7684\u72b6\u6001 \\([i-1, c]\\) \u548c\u5de6\u4e0a\u65b9\u7684\u72b6\u6001 \\([i-1, c-wgt[i-1]]\\) \u8f6c\u79fb\u800c\u6765\uff0c\u56e0\u6b64\u901a\u8fc7\u4e24\u5c42\u5faa\u73af\u6b63\u5e8f\u904d\u5386\u6574\u4e2a \\(dp\\) \u8868\u5373\u53ef\u3002
\u6839\u636e\u4ee5\u4e0a\u5206\u6790\uff0c\u6211\u4eec\u63a5\u4e0b\u6765\u6309\u987a\u5e8f\u5b9e\u73b0\u66b4\u529b\u641c\u7d22\u3001\u8bb0\u5fc6\u5316\u641c\u7d22\u3001\u52a8\u6001\u89c4\u5212\u89e3\u6cd5\u3002
"},{"location":"chapter_dynamic_programming/knapsack_problem/#1","title":"1. \u00a0 \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u641c\u7d22","text":"\u641c\u7d22\u4ee3\u7801\u5305\u542b\u4ee5\u4e0b\u8981\u7d20\u3002
def knapsack_dfs(wgt: list[int], val: list[int], i: int, c: int) -> int:\n \"\"\"0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u7d22\"\"\"\n # \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n if i == 0 or c == 0:\n return 0\n # \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n if wgt[i - 1] > c:\n return knapsack_dfs(wgt, val, i - 1, c)\n # \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n no = knapsack_dfs(wgt, val, i - 1, c)\n yes = knapsack_dfs(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1]\n # \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n return max(no, yes)\n
knapsack.cpp/* 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u7d22 */\nint knapsackDFS(vector<int> &wgt, vector<int> &val, int i, int c) {\n // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n if (i == 0 || c == 0) {\n return 0;\n }\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n if (wgt[i - 1] > c) {\n return knapsackDFS(wgt, val, i - 1, c);\n }\n // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n int no = knapsackDFS(wgt, val, i - 1, c);\n int yes = knapsackDFS(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1];\n // \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n return max(no, yes);\n}\n
knapsack.java/* 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u7d22 */\nint knapsackDFS(int[] wgt, int[] val, int i, int c) {\n // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n if (i == 0 || c == 0) {\n return 0;\n }\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n if (wgt[i - 1] > c) {\n return knapsackDFS(wgt, val, i - 1, c);\n }\n // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n int no = knapsackDFS(wgt, val, i - 1, c);\n int yes = knapsackDFS(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1];\n // \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n return Math.max(no, yes);\n}\n
knapsack.cs/* 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u7d22 */\nint KnapsackDFS(int[] weight, int[] val, int i, int c) {\n // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n if (i == 0 || c == 0) {\n return 0;\n }\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n if (weight[i - 1] > c) {\n return KnapsackDFS(weight, val, i - 1, c);\n }\n // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n int no = KnapsackDFS(weight, val, i - 1, c);\n int yes = KnapsackDFS(weight, val, i - 1, c - weight[i - 1]) + val[i - 1];\n // \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n return Math.Max(no, yes);\n}\n
knapsack.go/* 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u7d22 */\nfunc knapsackDFS(wgt, val []int, i, c int) int {\n // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n if i == 0 || c == 0 {\n return 0\n }\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n if wgt[i-1] > c {\n return knapsackDFS(wgt, val, i-1, c)\n }\n // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n no := knapsackDFS(wgt, val, i-1, c)\n yes := knapsackDFS(wgt, val, i-1, c-wgt[i-1]) + val[i-1]\n // \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n return int(math.Max(float64(no), float64(yes)))\n}\n
knapsack.swift/* 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u7d22 */\nfunc knapsackDFS(wgt: [Int], val: [Int], i: Int, c: Int) -> Int {\n // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n if i == 0 || c == 0 {\n return 0\n }\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n if wgt[i - 1] > c {\n return knapsackDFS(wgt: wgt, val: val, i: i - 1, c: c)\n }\n // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n let no = knapsackDFS(wgt: wgt, val: val, i: i - 1, c: c)\n let yes = knapsackDFS(wgt: wgt, val: val, i: i - 1, c: c - wgt[i - 1]) + val[i - 1]\n // \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n return max(no, yes)\n}\n
knapsack.js/* 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u7d22 */\nfunction knapsackDFS(wgt, val, i, c) {\n // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n if (i === 0 || c === 0) {\n return 0;\n }\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n if (wgt[i - 1] > c) {\n return knapsackDFS(wgt, val, i - 1, c);\n }\n // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n const no = knapsackDFS(wgt, val, i - 1, c);\n const yes = knapsackDFS(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1];\n // \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n return Math.max(no, yes);\n}\n
knapsack.ts/* 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u7d22 */\nfunction knapsackDFS(\n wgt: Array<number>,\n val: Array<number>,\n i: number,\n c: number\n): number {\n // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n if (i === 0 || c === 0) {\n return 0;\n }\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n if (wgt[i - 1] > c) {\n return knapsackDFS(wgt, val, i - 1, c);\n }\n // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n const no = knapsackDFS(wgt, val, i - 1, c);\n const yes = knapsackDFS(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1];\n // \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n return Math.max(no, yes);\n}\n
knapsack.dart/* 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u7d22 */\nint knapsackDFS(List<int> wgt, List<int> val, int i, int c) {\n // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n if (i == 0 || c == 0) {\n return 0;\n }\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n if (wgt[i - 1] > c) {\n return knapsackDFS(wgt, val, i - 1, c);\n }\n // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n int no = knapsackDFS(wgt, val, i - 1, c);\n int yes = knapsackDFS(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1];\n // \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n return max(no, yes);\n}\n
knapsack.rs/* 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u7d22 */\nfn knapsack_dfs(wgt: &[i32], val: &[i32], i: usize, c: usize) -> i32 {\n // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n if i == 0 || c == 0 {\n return 0;\n }\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n if wgt[i - 1] > c as i32 {\n return knapsack_dfs(wgt, val, i - 1, c);\n }\n // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n let no = knapsack_dfs(wgt, val, i - 1, c);\n let yes = knapsack_dfs(wgt, val, i - 1, c - wgt[i - 1] as usize) + val[i - 1];\n // \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n std::cmp::max(no, yes)\n}\n
knapsack.c/* 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u7d22 */\nint knapsackDFS(int wgt[], int val[], int i, int c) {\n // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n if (i == 0 || c == 0) {\n return 0;\n }\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n if (wgt[i - 1] > c) {\n return knapsackDFS(wgt, val, i - 1, c);\n }\n // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n int no = knapsackDFS(wgt, val, i - 1, c);\n int yes = knapsackDFS(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1];\n // \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n return myMax(no, yes);\n}\n
knapsack.zig// 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u7d22\nfn knapsackDFS(wgt: []i32, val: []i32, i: usize, c: usize) i32 {\n // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n if (i == 0 or c == 0) {\n return 0;\n }\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n if (wgt[i - 1] > c) {\n return knapsackDFS(wgt, val, i - 1, c);\n }\n // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n var no = knapsackDFS(wgt, val, i - 1, c);\n var yes = knapsackDFS(wgt, val, i - 1, c - @as(usize, @intCast(wgt[i - 1]))) + val[i - 1];\n // \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n return @max(no, yes);\n}\n
\u5982\u56fe 14-18 \u6240\u793a\uff0c\u7531\u4e8e\u6bcf\u4e2a\u7269\u54c1\u90fd\u4f1a\u4ea7\u751f\u4e0d\u9009\u548c\u9009\u4e24\u6761\u641c\u7d22\u5206\u652f\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(2^n)\\) \u3002
\u89c2\u5bdf\u9012\u5f52\u6811\uff0c\u5bb9\u6613\u53d1\u73b0\u5176\u4e2d\u5b58\u5728\u91cd\u53e0\u5b50\u95ee\u9898\uff0c\u4f8b\u5982 \\(dp[1, 10]\\) \u7b49\u3002\u800c\u5f53\u7269\u54c1\u8f83\u591a\u3001\u80cc\u5305\u5bb9\u91cf\u8f83\u5927\uff0c\u5c24\u5176\u662f\u76f8\u540c\u91cd\u91cf\u7684\u7269\u54c1\u8f83\u591a\u65f6\uff0c\u91cd\u53e0\u5b50\u95ee\u9898\u7684\u6570\u91cf\u5c06\u4f1a\u5927\u5e45\u589e\u591a\u3002
\u56fe 14-18 \u00a0 0-1 \u80cc\u5305\u95ee\u9898\u7684\u66b4\u529b\u641c\u7d22\u9012\u5f52\u6811
"},{"location":"chapter_dynamic_programming/knapsack_problem/#2","title":"2. \u00a0 \u65b9\u6cd5\u4e8c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22","text":"\u4e3a\u4e86\u4fdd\u8bc1\u91cd\u53e0\u5b50\u95ee\u9898\u53ea\u88ab\u8ba1\u7b97\u4e00\u6b21\uff0c\u6211\u4eec\u501f\u52a9\u8bb0\u5fc6\u5217\u8868 mem
\u6765\u8bb0\u5f55\u5b50\u95ee\u9898\u7684\u89e3\uff0c\u5176\u4e2d mem[i][c]
\u5bf9\u5e94 \\(dp[i, c]\\) \u3002
\u5f15\u5165\u8bb0\u5fc6\u5316\u4e4b\u540e\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u53d6\u51b3\u4e8e\u5b50\u95ee\u9898\u6570\u91cf\uff0c\u4e5f\u5c31\u662f \\(O(n \\times cap)\\) \u3002\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig knapsack.pydef knapsack_dfs_mem(\n wgt: list[int], val: list[int], mem: list[list[int]], i: int, c: int\n) -> int:\n \"\"\"0-1 \u80cc\u5305\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22\"\"\"\n # \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n if i == 0 or c == 0:\n return 0\n # \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if mem[i][c] != -1:\n return mem[i][c]\n # \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n if wgt[i - 1] > c:\n return knapsack_dfs_mem(wgt, val, mem, i - 1, c)\n # \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n no = knapsack_dfs_mem(wgt, val, mem, i - 1, c)\n yes = knapsack_dfs_mem(wgt, val, mem, i - 1, c - wgt[i - 1]) + val[i - 1]\n # \u8bb0\u5f55\u5e76\u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n mem[i][c] = max(no, yes)\n return mem[i][c]\n
knapsack.cpp/* 0-1 \u80cc\u5305\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint knapsackDFSMem(vector<int> &wgt, vector<int> &val, vector<vector<int>> &mem, int i, int c) {\n // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n if (i == 0 || c == 0) {\n return 0;\n }\n // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if (mem[i][c] != -1) {\n return mem[i][c];\n }\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n if (wgt[i - 1] > c) {\n return knapsackDFSMem(wgt, val, mem, i - 1, c);\n }\n // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n int no = knapsackDFSMem(wgt, val, mem, i - 1, c);\n int yes = knapsackDFSMem(wgt, val, mem, i - 1, c - wgt[i - 1]) + val[i - 1];\n // \u8bb0\u5f55\u5e76\u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n mem[i][c] = max(no, yes);\n return mem[i][c];\n}\n
knapsack.java/* 0-1 \u80cc\u5305\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint knapsackDFSMem(int[] wgt, int[] val, int[][] mem, int i, int c) {\n // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n if (i == 0 || c == 0) {\n return 0;\n }\n // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if (mem[i][c] != -1) {\n return mem[i][c];\n }\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n if (wgt[i - 1] > c) {\n return knapsackDFSMem(wgt, val, mem, i - 1, c);\n }\n // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n int no = knapsackDFSMem(wgt, val, mem, i - 1, c);\n int yes = knapsackDFSMem(wgt, val, mem, i - 1, c - wgt[i - 1]) + val[i - 1];\n // \u8bb0\u5f55\u5e76\u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n mem[i][c] = Math.max(no, yes);\n return mem[i][c];\n}\n
knapsack.cs/* 0-1 \u80cc\u5305\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint KnapsackDFSMem(int[] weight, int[] val, int[][] mem, int i, int c) {\n // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n if (i == 0 || c == 0) {\n return 0;\n }\n // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if (mem[i][c] != -1) {\n return mem[i][c];\n }\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n if (weight[i - 1] > c) {\n return KnapsackDFSMem(weight, val, mem, i - 1, c);\n }\n // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n int no = KnapsackDFSMem(weight, val, mem, i - 1, c);\n int yes = KnapsackDFSMem(weight, val, mem, i - 1, c - weight[i - 1]) + val[i - 1];\n // \u8bb0\u5f55\u5e76\u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n mem[i][c] = Math.Max(no, yes);\n return mem[i][c];\n}\n
knapsack.go/* 0-1 \u80cc\u5305\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunc knapsackDFSMem(wgt, val []int, mem [][]int, i, c int) int {\n // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n if i == 0 || c == 0 {\n return 0\n }\n // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if mem[i][c] != -1 {\n return mem[i][c]\n }\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n if wgt[i-1] > c {\n return knapsackDFSMem(wgt, val, mem, i-1, c)\n }\n // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n no := knapsackDFSMem(wgt, val, mem, i-1, c)\n yes := knapsackDFSMem(wgt, val, mem, i-1, c-wgt[i-1]) + val[i-1]\n // \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n mem[i][c] = int(math.Max(float64(no), float64(yes)))\n return mem[i][c]\n}\n
knapsack.swift/* 0-1 \u80cc\u5305\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunc knapsackDFSMem(wgt: [Int], val: [Int], mem: inout [[Int]], i: Int, c: Int) -> Int {\n // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n if i == 0 || c == 0 {\n return 0\n }\n // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if mem[i][c] != -1 {\n return mem[i][c]\n }\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n if wgt[i - 1] > c {\n return knapsackDFSMem(wgt: wgt, val: val, mem: &mem, i: i - 1, c: c)\n }\n // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n let no = knapsackDFSMem(wgt: wgt, val: val, mem: &mem, i: i - 1, c: c)\n let yes = knapsackDFSMem(wgt: wgt, val: val, mem: &mem, i: i - 1, c: c - wgt[i - 1]) + val[i - 1]\n // \u8bb0\u5f55\u5e76\u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n mem[i][c] = max(no, yes)\n return mem[i][c]\n}\n
knapsack.js/* 0-1 \u80cc\u5305\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunction knapsackDFSMem(wgt, val, mem, i, c) {\n // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n if (i === 0 || c === 0) {\n return 0;\n }\n // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if (mem[i][c] !== -1) {\n return mem[i][c];\n }\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n if (wgt[i - 1] > c) {\n return knapsackDFSMem(wgt, val, mem, i - 1, c);\n }\n // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n const no = knapsackDFSMem(wgt, val, mem, i - 1, c);\n const yes =\n knapsackDFSMem(wgt, val, mem, i - 1, c - wgt[i - 1]) + val[i - 1];\n // \u8bb0\u5f55\u5e76\u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n mem[i][c] = Math.max(no, yes);\n return mem[i][c];\n}\n
knapsack.ts/* 0-1 \u80cc\u5305\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunction knapsackDFSMem(\n wgt: Array<number>,\n val: Array<number>,\n mem: Array<Array<number>>,\n i: number,\n c: number\n): number {\n // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n if (i === 0 || c === 0) {\n return 0;\n }\n // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if (mem[i][c] !== -1) {\n return mem[i][c];\n }\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n if (wgt[i - 1] > c) {\n return knapsackDFSMem(wgt, val, mem, i - 1, c);\n }\n // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n const no = knapsackDFSMem(wgt, val, mem, i - 1, c);\n const yes =\n knapsackDFSMem(wgt, val, mem, i - 1, c - wgt[i - 1]) + val[i - 1];\n // \u8bb0\u5f55\u5e76\u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n mem[i][c] = Math.max(no, yes);\n return mem[i][c];\n}\n
knapsack.dart/* 0-1 \u80cc\u5305\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint knapsackDFSMem(\n List<int> wgt,\n List<int> val,\n List<List<int>> mem,\n int i,\n int c,\n) {\n // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n if (i == 0 || c == 0) {\n return 0;\n }\n // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if (mem[i][c] != -1) {\n return mem[i][c];\n }\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n if (wgt[i - 1] > c) {\n return knapsackDFSMem(wgt, val, mem, i - 1, c);\n }\n // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n int no = knapsackDFSMem(wgt, val, mem, i - 1, c);\n int yes = knapsackDFSMem(wgt, val, mem, i - 1, c - wgt[i - 1]) + val[i - 1];\n // \u8bb0\u5f55\u5e76\u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n mem[i][c] = max(no, yes);\n return mem[i][c];\n}\n
knapsack.rs/* 0-1 \u80cc\u5305\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfn knapsack_dfs_mem(wgt: &[i32], val: &[i32], mem: &mut Vec<Vec<i32>>, i: usize, c: usize) -> i32 {\n // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n if i == 0 || c == 0 {\n return 0;\n }\n // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if mem[i][c] != -1 {\n return mem[i][c];\n }\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n if wgt[i - 1] > c as i32 {\n return knapsack_dfs_mem(wgt, val, mem, i - 1, c);\n }\n // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n let no = knapsack_dfs_mem(wgt, val, mem, i - 1, c);\n let yes = knapsack_dfs_mem(wgt, val, mem, i - 1, c - wgt[i - 1] as usize) + val[i - 1];\n // \u8bb0\u5f55\u5e76\u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n mem[i][c] = std::cmp::max(no, yes);\n mem[i][c]\n}\n
knapsack.c/* 0-1 \u80cc\u5305\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint knapsackDFSMem(int wgt[], int val[], int memCols, int **mem, int i, int c) {\n // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n if (i == 0 || c == 0) {\n return 0;\n }\n // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if (mem[i][c] != -1) {\n return mem[i][c];\n }\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n if (wgt[i - 1] > c) {\n return knapsackDFSMem(wgt, val, memCols, mem, i - 1, c);\n }\n // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n int no = knapsackDFSMem(wgt, val, memCols, mem, i - 1, c);\n int yes = knapsackDFSMem(wgt, val, memCols, mem, i - 1, c - wgt[i - 1]) + val[i - 1];\n // \u8bb0\u5f55\u5e76\u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n mem[i][c] = myMax(no, yes);\n return mem[i][c];\n}\n
knapsack.zig// 0-1 \u80cc\u5305\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22\nfn knapsackDFSMem(wgt: []i32, val: []i32, mem: anytype, i: usize, c: usize) i32 {\n // \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\n if (i == 0 or c == 0) {\n return 0;\n }\n // \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if (mem[i][c] != -1) {\n return mem[i][c];\n }\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\n if (wgt[i - 1] > c) {\n return knapsackDFSMem(wgt, val, mem, i - 1, c);\n }\n // \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\n var no = knapsackDFSMem(wgt, val, mem, i - 1, c);\n var yes = knapsackDFSMem(wgt, val, mem, i - 1, c - @as(usize, @intCast(wgt[i - 1]))) + val[i - 1];\n // \u8bb0\u5f55\u5e76\u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\n mem[i][c] = @max(no, yes);\n return mem[i][c];\n}\n
\u56fe 14-19 \u5c55\u793a\u4e86\u5728\u8bb0\u5fc6\u5316\u641c\u7d22\u4e2d\u88ab\u526a\u6389\u7684\u641c\u7d22\u5206\u652f\u3002
\u56fe 14-19 \u00a0 0-1 \u80cc\u5305\u95ee\u9898\u7684\u8bb0\u5fc6\u5316\u641c\u7d22\u9012\u5f52\u6811
"},{"location":"chapter_dynamic_programming/knapsack_problem/#3","title":"3. \u00a0 \u65b9\u6cd5\u4e09\uff1a\u52a8\u6001\u89c4\u5212","text":"\u52a8\u6001\u89c4\u5212\u5b9e\u8d28\u4e0a\u5c31\u662f\u5728\u72b6\u6001\u8f6c\u79fb\u4e2d\u586b\u5145 \\(dp\\) \u8868\u7684\u8fc7\u7a0b\uff0c\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig knapsack.pydef knapsack_dp(wgt: list[int], val: list[int], cap: int) -> int:\n \"\"\"0-1 \u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212\"\"\"\n n = len(wgt)\n # \u521d\u59cb\u5316 dp \u8868\n dp = [[0] * (cap + 1) for _ in range(n + 1)]\n # \u72b6\u6001\u8f6c\u79fb\n for i in range(1, n + 1):\n for c in range(1, cap + 1):\n if wgt[i - 1] > c:\n # \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n dp[i][c] = dp[i - 1][c]\n else:\n # \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[i][c] = max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + val[i - 1])\n return dp[n][cap]\n
knapsack.cpp/* 0-1 \u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nint knapsackDP(vector<int> &wgt, vector<int> &val, int cap) {\n int n = wgt.size();\n // \u521d\u59cb\u5316 dp \u8868\n vector<vector<int>> dp(n + 1, vector<int>(cap + 1, 0));\n // \u72b6\u6001\u8f6c\u79fb\n for (int i = 1; i <= n; i++) {\n for (int c = 1; c <= cap; c++) {\n if (wgt[i - 1] > c) {\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n dp[i][c] = dp[i - 1][c];\n } else {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[i][c] = max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + val[i - 1]);\n }\n }\n }\n return dp[n][cap];\n}\n
knapsack.java/* 0-1 \u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nint knapsackDP(int[] wgt, int[] val, int cap) {\n int n = wgt.length;\n // \u521d\u59cb\u5316 dp \u8868\n int[][] dp = new int[n + 1][cap + 1];\n // \u72b6\u6001\u8f6c\u79fb\n for (int i = 1; i <= n; i++) {\n for (int c = 1; c <= cap; c++) {\n if (wgt[i - 1] > c) {\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n dp[i][c] = dp[i - 1][c];\n } else {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[i][c] = Math.max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + val[i - 1]);\n }\n }\n }\n return dp[n][cap];\n}\n
knapsack.cs/* 0-1 \u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nint KnapsackDP(int[] weight, int[] val, int cap) {\n int n = weight.Length;\n // \u521d\u59cb\u5316 dp \u8868\n int[,] dp = new int[n + 1, cap + 1];\n // \u72b6\u6001\u8f6c\u79fb\n for (int i = 1; i <= n; i++) {\n for (int c = 1; c <= cap; c++) {\n if (weight[i - 1] > c) {\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n dp[i, c] = dp[i - 1, c];\n } else {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[i, c] = Math.Max(dp[i - 1, c - weight[i - 1]] + val[i - 1], dp[i - 1, c]);\n }\n }\n }\n return dp[n, cap];\n}\n
knapsack.go/* 0-1 \u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc knapsackDP(wgt, val []int, cap int) int {\n n := len(wgt)\n // \u521d\u59cb\u5316 dp \u8868\n dp := make([][]int, n+1)\n for i := 0; i <= n; i++ {\n dp[i] = make([]int, cap+1)\n }\n // \u72b6\u6001\u8f6c\u79fb\n for i := 1; i <= n; i++ {\n for c := 1; c <= cap; c++ {\n if wgt[i-1] > c {\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n dp[i][c] = dp[i-1][c]\n } else {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[i][c] = int(math.Max(float64(dp[i-1][c]), float64(dp[i-1][c-wgt[i-1]]+val[i-1])))\n }\n }\n }\n return dp[n][cap]\n}\n
knapsack.swift/* 0-1 \u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc knapsackDP(wgt: [Int], val: [Int], cap: Int) -> Int {\n let n = wgt.count\n // \u521d\u59cb\u5316 dp \u8868\n var dp = Array(repeating: Array(repeating: 0, count: cap + 1), count: n + 1)\n // \u72b6\u6001\u8f6c\u79fb\n for i in stride(from: 1, through: n, by: 1) {\n for c in stride(from: 1, through: cap, by: 1) {\n if wgt[i - 1] > c {\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n dp[i][c] = dp[i - 1][c]\n } else {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[i][c] = max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + val[i - 1])\n }\n }\n }\n return dp[n][cap]\n}\n
knapsack.js/* 0-1 \u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction knapsackDP(wgt, val, cap) {\n const n = wgt.length;\n // \u521d\u59cb\u5316 dp \u8868\n const dp = Array(n + 1)\n .fill(0)\n .map(() => Array(cap + 1).fill(0));\n // \u72b6\u6001\u8f6c\u79fb\n for (let i = 1; i <= n; i++) {\n for (let c = 1; c <= cap; c++) {\n if (wgt[i - 1] > c) {\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n dp[i][c] = dp[i - 1][c];\n } else {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[i][c] = Math.max(\n dp[i - 1][c],\n dp[i - 1][c - wgt[i - 1]] + val[i - 1]\n );\n }\n }\n }\n return dp[n][cap];\n}\n
knapsack.ts/* 0-1 \u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction knapsackDP(\n wgt: Array<number>,\n val: Array<number>,\n cap: number\n): number {\n const n = wgt.length;\n // \u521d\u59cb\u5316 dp \u8868\n const dp = Array.from({ length: n + 1 }, () =>\n Array.from({ length: cap + 1 }, () => 0)\n );\n // \u72b6\u6001\u8f6c\u79fb\n for (let i = 1; i <= n; i++) {\n for (let c = 1; c <= cap; c++) {\n if (wgt[i - 1] > c) {\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n dp[i][c] = dp[i - 1][c];\n } else {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[i][c] = Math.max(\n dp[i - 1][c],\n dp[i - 1][c - wgt[i - 1]] + val[i - 1]\n );\n }\n }\n }\n return dp[n][cap];\n}\n
knapsack.dart/* 0-1 \u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nint knapsackDP(List<int> wgt, List<int> val, int cap) {\n int n = wgt.length;\n // \u521d\u59cb\u5316 dp \u8868\n List<List<int>> dp = List.generate(n + 1, (index) => List.filled(cap + 1, 0));\n // \u72b6\u6001\u8f6c\u79fb\n for (int i = 1; i <= n; i++) {\n for (int c = 1; c <= cap; c++) {\n if (wgt[i - 1] > c) {\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n dp[i][c] = dp[i - 1][c];\n } else {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[i][c] = max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + val[i - 1]);\n }\n }\n }\n return dp[n][cap];\n}\n
knapsack.rs/* 0-1 \u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nfn knapsack_dp(wgt: &[i32], val: &[i32], cap: usize) -> i32 {\n let n = wgt.len();\n // \u521d\u59cb\u5316 dp \u8868\n let mut dp = vec![vec![0; cap + 1]; n + 1];\n // \u72b6\u6001\u8f6c\u79fb\n for i in 1..=n {\n for c in 1..=cap {\n if wgt[i - 1] > c as i32 {\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n dp[i][c] = dp[i - 1][c];\n } else {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[i][c] = std::cmp::max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1] as usize] + val[i - 1]);\n }\n }\n }\n dp[n][cap]\n}\n
knapsack.c/* 0-1 \u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nint knapsackDP(int wgt[], int val[], int cap, int wgtSize) {\n int n = wgtSize;\n // \u521d\u59cb\u5316 dp \u8868\n int **dp = malloc((n + 1) * sizeof(int *));\n for (int i = 0; i <= n; i++) {\n dp[i] = calloc(cap + 1, sizeof(int));\n }\n // \u72b6\u6001\u8f6c\u79fb\n for (int i = 1; i <= n; i++) {\n for (int c = 1; c <= cap; c++) {\n if (wgt[i - 1] > c) {\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n dp[i][c] = dp[i - 1][c];\n } else {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[i][c] = myMax(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + val[i - 1]);\n }\n }\n }\n int res = dp[n][cap];\n // \u91ca\u653e\u5185\u5b58\n for (int i = 0; i <= n; i++) {\n free(dp[i]);\n }\n return res;\n}\n
knapsack.zig// 0-1 \u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212\nfn knapsackDP(comptime wgt: []i32, val: []i32, comptime cap: usize) i32 {\n comptime var n = wgt.len;\n // \u521d\u59cb\u5316 dp \u8868\n var dp = [_][cap + 1]i32{[_]i32{0} ** (cap + 1)} ** (n + 1);\n // \u72b6\u6001\u8f6c\u79fb\n for (1..n + 1) |i| {\n for (1..cap + 1) |c| {\n if (wgt[i - 1] > c) {\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n dp[i][c] = dp[i - 1][c];\n } else {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[i][c] = @max(dp[i - 1][c], dp[i - 1][c - @as(usize, @intCast(wgt[i - 1]))] + val[i - 1]);\n }\n }\n }\n return dp[n][cap];\n}\n
\u5982\u56fe 14-20 \u6240\u793a\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u548c\u7a7a\u95f4\u590d\u6742\u5ea6\u90fd\u7531\u6570\u7ec4 dp
\u5927\u5c0f\u51b3\u5b9a\uff0c\u5373 \\(O(n \\times cap)\\) \u3002
\u56fe 14-20 \u00a0 0-1 \u80cc\u5305\u95ee\u9898\u7684\u52a8\u6001\u89c4\u5212\u8fc7\u7a0b
"},{"location":"chapter_dynamic_programming/knapsack_problem/#4","title":"4. \u00a0 \u7a7a\u95f4\u4f18\u5316","text":"\u7531\u4e8e\u6bcf\u4e2a\u72b6\u6001\u90fd\u53ea\u4e0e\u5176\u4e0a\u4e00\u884c\u7684\u72b6\u6001\u6709\u5173\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e24\u4e2a\u6570\u7ec4\u6eda\u52a8\u524d\u8fdb\uff0c\u5c06\u7a7a\u95f4\u590d\u6742\u5ea6\u4ece \\(O(n^2)\\) \u964d\u81f3 \\(O(n)\\) \u3002
\u8fdb\u4e00\u6b65\u601d\u8003\uff0c\u6211\u4eec\u80fd\u5426\u4ec5\u7528\u4e00\u4e2a\u6570\u7ec4\u5b9e\u73b0\u7a7a\u95f4\u4f18\u5316\u5462\uff1f\u89c2\u5bdf\u53ef\u77e5\uff0c\u6bcf\u4e2a\u72b6\u6001\u90fd\u662f\u7531\u6b63\u4e0a\u65b9\u6216\u5de6\u4e0a\u65b9\u7684\u683c\u5b50\u8f6c\u79fb\u8fc7\u6765\u7684\u3002\u5047\u8bbe\u53ea\u6709\u4e00\u4e2a\u6570\u7ec4\uff0c\u5f53\u5f00\u59cb\u904d\u5386\u7b2c \\(i\\) \u884c\u65f6\uff0c\u8be5\u6570\u7ec4\u5b58\u50a8\u7684\u4ecd\u7136\u662f\u7b2c \\(i-1\\) \u884c\u7684\u72b6\u6001\u3002
\u56fe 14-21 \u5c55\u793a\u4e86\u5728\u5355\u4e2a\u6570\u7ec4\u4e0b\u4ece\u7b2c \\(i = 1\\) \u884c\u8f6c\u6362\u81f3\u7b2c \\(i = 2\\) \u884c\u7684\u8fc7\u7a0b\u3002\u8bf7\u601d\u8003\u6b63\u5e8f\u904d\u5386\u548c\u5012\u5e8f\u904d\u5386\u7684\u533a\u522b\u3002
<1><2><3><4><5><6>\u56fe 14-21 \u00a0 0-1 \u80cc\u5305\u7684\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\u8fc7\u7a0b
\u5728\u4ee3\u7801\u5b9e\u73b0\u4e2d\uff0c\u6211\u4eec\u4ec5\u9700\u5c06\u6570\u7ec4 dp
\u7684\u7b2c\u4e00\u7ef4 \\(i\\) \u76f4\u63a5\u5220\u9664\uff0c\u5e76\u4e14\u628a\u5185\u5faa\u73af\u66f4\u6539\u4e3a\u5012\u5e8f\u904d\u5386\u5373\u53ef\uff1a
def knapsack_dp_comp(wgt: list[int], val: list[int], cap: int) -> int:\n \"\"\"0-1 \u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\"\"\"\n n = len(wgt)\n # \u521d\u59cb\u5316 dp \u8868\n dp = [0] * (cap + 1)\n # \u72b6\u6001\u8f6c\u79fb\n for i in range(1, n + 1):\n # \u5012\u5e8f\u904d\u5386\n for c in range(cap, 0, -1):\n if wgt[i - 1] > c:\n # \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n dp[c] = dp[c]\n else:\n # \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1])\n return dp[cap]\n
knapsack.cpp/* 0-1 \u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint knapsackDPComp(vector<int> &wgt, vector<int> &val, int cap) {\n int n = wgt.size();\n // \u521d\u59cb\u5316 dp \u8868\n vector<int> dp(cap + 1, 0);\n // \u72b6\u6001\u8f6c\u79fb\n for (int i = 1; i <= n; i++) {\n // \u5012\u5e8f\u904d\u5386\n for (int c = cap; c >= 1; c--) {\n if (wgt[i - 1] <= c) {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n }\n }\n }\n return dp[cap];\n}\n
knapsack.java/* 0-1 \u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint knapsackDPComp(int[] wgt, int[] val, int cap) {\n int n = wgt.length;\n // \u521d\u59cb\u5316 dp \u8868\n int[] dp = new int[cap + 1];\n // \u72b6\u6001\u8f6c\u79fb\n for (int i = 1; i <= n; i++) {\n // \u5012\u5e8f\u904d\u5386\n for (int c = cap; c >= 1; c--) {\n if (wgt[i - 1] <= c) {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[c] = Math.max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n }\n }\n }\n return dp[cap];\n}\n
knapsack.cs/* 0-1 \u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint KnapsackDPComp(int[] weight, int[] val, int cap) {\n int n = weight.Length;\n // \u521d\u59cb\u5316 dp \u8868\n int[] dp = new int[cap + 1];\n // \u72b6\u6001\u8f6c\u79fb\n for (int i = 1; i <= n; i++) {\n // \u5012\u5e8f\u904d\u5386\n for (int c = cap; c > 0; c--) {\n if (weight[i - 1] > c) {\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n dp[c] = dp[c];\n } else {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[c] = Math.Max(dp[c], dp[c - weight[i - 1]] + val[i - 1]);\n }\n }\n }\n return dp[cap];\n}\n
knapsack.go/* 0-1 \u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc knapsackDPComp(wgt, val []int, cap int) int {\n n := len(wgt)\n // \u521d\u59cb\u5316 dp \u8868\n dp := make([]int, cap+1)\n // \u72b6\u6001\u8f6c\u79fb\n for i := 1; i <= n; i++ {\n // \u5012\u5e8f\u904d\u5386\n for c := cap; c >= 1; c-- {\n if wgt[i-1] <= c {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[c] = int(math.Max(float64(dp[c]), float64(dp[c-wgt[i-1]]+val[i-1])))\n }\n }\n }\n return dp[cap]\n}\n
knapsack.swift/* 0-1 \u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc knapsackDPComp(wgt: [Int], val: [Int], cap: Int) -> Int {\n let n = wgt.count\n // \u521d\u59cb\u5316 dp \u8868\n var dp = Array(repeating: 0, count: cap + 1)\n // \u72b6\u6001\u8f6c\u79fb\n for i in stride(from: 1, through: n, by: 1) {\n // \u5012\u5e8f\u904d\u5386\n for c in stride(from: cap, through: 1, by: -1) {\n if wgt[i - 1] <= c {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1])\n }\n }\n }\n return dp[cap]\n}\n
knapsack.js/* 0-1 \u80cc\u5305\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction knapsackDPComp(wgt, val, cap) {\n const n = wgt.length;\n // \u521d\u59cb\u5316 dp \u8868\n const dp = Array(cap + 1).fill(0);\n // \u72b6\u6001\u8f6c\u79fb\n for (let i = 1; i <= n; i++) {\n // \u5012\u5e8f\u904d\u5386\n for (let c = cap; c >= 1; c--) {\n if (wgt[i - 1] <= c) {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[c] = Math.max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n }\n }\n }\n return dp[cap];\n}\n
knapsack.ts/* 0-1 \u80cc\u5305\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction knapsackDPComp(\n wgt: Array<number>,\n val: Array<number>,\n cap: number\n): number {\n const n = wgt.length;\n // \u521d\u59cb\u5316 dp \u8868\n const dp = Array(cap + 1).fill(0);\n // \u72b6\u6001\u8f6c\u79fb\n for (let i = 1; i <= n; i++) {\n // \u5012\u5e8f\u904d\u5386\n for (let c = cap; c >= 1; c--) {\n if (wgt[i - 1] <= c) {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[c] = Math.max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n }\n }\n }\n return dp[cap];\n}\n
knapsack.dart/* 0-1 \u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint knapsackDPComp(List<int> wgt, List<int> val, int cap) {\n int n = wgt.length;\n // \u521d\u59cb\u5316 dp \u8868\n List<int> dp = List.filled(cap + 1, 0);\n // \u72b6\u6001\u8f6c\u79fb\n for (int i = 1; i <= n; i++) {\n // \u5012\u5e8f\u904d\u5386\n for (int c = cap; c >= 1; c--) {\n if (wgt[i - 1] <= c) {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n }\n }\n }\n return dp[cap];\n}\n
knapsack.rs/* 0-1 \u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfn knapsack_dp_comp(wgt: &[i32], val: &[i32], cap: usize) -> i32 {\n let n = wgt.len();\n // \u521d\u59cb\u5316 dp \u8868\n let mut dp = vec![0; cap + 1];\n // \u72b6\u6001\u8f6c\u79fb\n for i in 1..=n {\n // \u5012\u5e8f\u904d\u5386\n for c in (1..=cap).rev() {\n if wgt[i - 1] <= c as i32 {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[c] = std::cmp::max(dp[c], dp[c - wgt[i - 1] as usize] + val[i - 1]);\n }\n }\n }\n dp[cap]\n}\n
knapsack.c/* 0-1 \u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint knapsackDPComp(int wgt[], int val[], int cap, int wgtSize) {\n int n = wgtSize;\n // \u521d\u59cb\u5316 dp \u8868\n int *dp = calloc(cap + 1, sizeof(int));\n // \u72b6\u6001\u8f6c\u79fb\n for (int i = 1; i <= n; i++) {\n // \u5012\u5e8f\u904d\u5386\n for (int c = cap; c >= 1; c--) {\n if (wgt[i - 1] <= c) {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[c] = myMax(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n }\n }\n }\n int res = dp[cap];\n // \u91ca\u653e\u5185\u5b58\n free(dp);\n return res;\n}\n
knapsack.zig// 0-1 \u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\nfn knapsackDPComp(wgt: []i32, val: []i32, comptime cap: usize) i32 {\n var n = wgt.len;\n // \u521d\u59cb\u5316 dp \u8868\n var dp = [_]i32{0} ** (cap + 1);\n // \u72b6\u6001\u8f6c\u79fb\n for (1..n + 1) |i| {\n // \u5012\u5e8f\u904d\u5386\n var c = cap;\n while (c > 0) : (c -= 1) {\n if (wgt[i - 1] < c) {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[c] = @max(dp[c], dp[c - @as(usize, @intCast(wgt[i - 1]))] + val[i - 1]);\n }\n }\n }\n return dp[cap];\n}\n
"},{"location":"chapter_dynamic_programming/summary/","title":"14.7 \u00a0 \u5c0f\u7ed3","text":"\u80cc\u5305\u95ee\u9898
\u7f16\u8f91\u8ddd\u79bb\u95ee\u9898
\u5728\u672c\u8282\u4e2d\uff0c\u6211\u4eec\u5148\u6c42\u89e3\u53e6\u4e00\u4e2a\u5e38\u89c1\u7684\u80cc\u5305\u95ee\u9898\uff1a\u5b8c\u5168\u80cc\u5305\uff0c\u518d\u4e86\u89e3\u5b83\u7684\u4e00\u79cd\u7279\u4f8b\uff1a\u96f6\u94b1\u5151\u6362\u3002
"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#1451","title":"14.5.1 \u00a0 \u5b8c\u5168\u80cc\u5305","text":"Question
\u7ed9\u5b9a \\(n\\) \u4e2a\u7269\u54c1\uff0c\u7b2c \\(i\\) \u4e2a\u7269\u54c1\u7684\u91cd\u91cf\u4e3a \\(wgt[i-1]\\)\u3001\u4ef7\u503c\u4e3a \\(val[i-1]\\) \uff0c\u548c\u4e00\u4e2a\u5bb9\u91cf\u4e3a \\(cap\\) \u7684\u80cc\u5305\u3002\u6bcf\u4e2a\u7269\u54c1\u53ef\u4ee5\u91cd\u590d\u9009\u53d6\uff0c\u95ee\u5728\u9650\u5b9a\u80cc\u5305\u5bb9\u91cf\u4e0b\u80fd\u653e\u5165\u7269\u54c1\u7684\u6700\u5927\u4ef7\u503c\u3002\u793a\u4f8b\u5982\u56fe 14-22 \u6240\u793a\u3002
\u56fe 14-22 \u00a0 \u5b8c\u5168\u80cc\u5305\u95ee\u9898\u7684\u793a\u4f8b\u6570\u636e
"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#1","title":"1. \u00a0 \u52a8\u6001\u89c4\u5212\u601d\u8def","text":"\u5b8c\u5168\u80cc\u5305\u95ee\u9898\u548c 0-1 \u80cc\u5305\u95ee\u9898\u975e\u5e38\u76f8\u4f3c\uff0c\u533a\u522b\u4ec5\u5728\u4e8e\u4e0d\u9650\u5236\u7269\u54c1\u7684\u9009\u62e9\u6b21\u6570\u3002
\u5728\u5b8c\u5168\u80cc\u5305\u95ee\u9898\u7684\u89c4\u5b9a\u4e0b\uff0c\u72b6\u6001 \\([i, c]\\) \u7684\u53d8\u5316\u5206\u4e3a\u4e24\u79cd\u60c5\u51b5\u3002
\u4ece\u800c\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u53d8\u4e3a\uff1a
\\[ dp[i, c] = \\max(dp[i-1, c], dp[i, c - wgt[i-1]] + val[i-1]) \\]"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#2","title":"2. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"\u5bf9\u6bd4\u4e24\u9053\u9898\u76ee\u7684\u4ee3\u7801\uff0c\u72b6\u6001\u8f6c\u79fb\u4e2d\u6709\u4e00\u5904\u4ece \\(i-1\\) \u53d8\u4e3a \\(i\\) \uff0c\u5176\u4f59\u5b8c\u5168\u4e00\u81f4\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig unbounded_knapsack.pydef unbounded_knapsack_dp(wgt: list[int], val: list[int], cap: int) -> int:\n \"\"\"\u5b8c\u5168\u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212\"\"\"\n n = len(wgt)\n # \u521d\u59cb\u5316 dp \u8868\n dp = [[0] * (cap + 1) for _ in range(n + 1)]\n # \u72b6\u6001\u8f6c\u79fb\n for i in range(1, n + 1):\n for c in range(1, cap + 1):\n if wgt[i - 1] > c:\n # \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n dp[i][c] = dp[i - 1][c]\n else:\n # \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[i][c] = max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1])\n return dp[n][cap]\n
unbounded_knapsack.cpp/* \u5b8c\u5168\u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nint unboundedKnapsackDP(vector<int> &wgt, vector<int> &val, int cap) {\n int n = wgt.size();\n // \u521d\u59cb\u5316 dp \u8868\n vector<vector<int>> dp(n + 1, vector<int>(cap + 1, 0));\n // \u72b6\u6001\u8f6c\u79fb\n for (int i = 1; i <= n; i++) {\n for (int c = 1; c <= cap; c++) {\n if (wgt[i - 1] > c) {\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n dp[i][c] = dp[i - 1][c];\n } else {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[i][c] = max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1]);\n }\n }\n }\n return dp[n][cap];\n}\n
unbounded_knapsack.java/* \u5b8c\u5168\u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nint unboundedKnapsackDP(int[] wgt, int[] val, int cap) {\n int n = wgt.length;\n // \u521d\u59cb\u5316 dp \u8868\n int[][] dp = new int[n + 1][cap + 1];\n // \u72b6\u6001\u8f6c\u79fb\n for (int i = 1; i <= n; i++) {\n for (int c = 1; c <= cap; c++) {\n if (wgt[i - 1] > c) {\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n dp[i][c] = dp[i - 1][c];\n } else {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[i][c] = Math.max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1]);\n }\n }\n }\n return dp[n][cap];\n}\n
unbounded_knapsack.cs/* \u5b8c\u5168\u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nint UnboundedKnapsackDP(int[] wgt, int[] val, int cap) {\n int n = wgt.Length;\n // \u521d\u59cb\u5316 dp \u8868\n int[,] dp = new int[n + 1, cap + 1];\n // \u72b6\u6001\u8f6c\u79fb\n for (int i = 1; i <= n; i++) {\n for (int c = 1; c <= cap; c++) {\n if (wgt[i - 1] > c) {\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n dp[i, c] = dp[i - 1, c];\n } else {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[i, c] = Math.Max(dp[i - 1, c], dp[i, c - wgt[i - 1]] + val[i - 1]);\n }\n }\n }\n return dp[n, cap];\n}\n
unbounded_knapsack.go/* \u5b8c\u5168\u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc unboundedKnapsackDP(wgt, val []int, cap int) int {\n n := len(wgt)\n // \u521d\u59cb\u5316 dp \u8868\n dp := make([][]int, n+1)\n for i := 0; i <= n; i++ {\n dp[i] = make([]int, cap+1)\n }\n // \u72b6\u6001\u8f6c\u79fb\n for i := 1; i <= n; i++ {\n for c := 1; c <= cap; c++ {\n if wgt[i-1] > c {\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n dp[i][c] = dp[i-1][c]\n } else {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[i][c] = int(math.Max(float64(dp[i-1][c]), float64(dp[i][c-wgt[i-1]]+val[i-1])))\n }\n }\n }\n return dp[n][cap]\n}\n
unbounded_knapsack.swift/* \u5b8c\u5168\u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc unboundedKnapsackDP(wgt: [Int], val: [Int], cap: Int) -> Int {\n let n = wgt.count\n // \u521d\u59cb\u5316 dp \u8868\n var dp = Array(repeating: Array(repeating: 0, count: cap + 1), count: n + 1)\n // \u72b6\u6001\u8f6c\u79fb\n for i in stride(from: 1, through: n, by: 1) {\n for c in stride(from: 1, through: cap, by: 1) {\n if wgt[i - 1] > c {\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n dp[i][c] = dp[i - 1][c]\n } else {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[i][c] = max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1])\n }\n }\n }\n return dp[n][cap]\n}\n
unbounded_knapsack.js/* \u5b8c\u5168\u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction unboundedKnapsackDP(wgt, val, cap) {\n const n = wgt.length;\n // \u521d\u59cb\u5316 dp \u8868\n const dp = Array.from({ length: n + 1 }, () =>\n Array.from({ length: cap + 1 }, () => 0)\n );\n // \u72b6\u6001\u8f6c\u79fb\n for (let i = 1; i <= n; i++) {\n for (let c = 1; c <= cap; c++) {\n if (wgt[i - 1] > c) {\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n dp[i][c] = dp[i - 1][c];\n } else {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[i][c] = Math.max(\n dp[i - 1][c],\n dp[i][c - wgt[i - 1]] + val[i - 1]\n );\n }\n }\n }\n return dp[n][cap];\n}\n
unbounded_knapsack.ts/* \u5b8c\u5168\u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction unboundedKnapsackDP(\n wgt: Array<number>,\n val: Array<number>,\n cap: number\n): number {\n const n = wgt.length;\n // \u521d\u59cb\u5316 dp \u8868\n const dp = Array.from({ length: n + 1 }, () =>\n Array.from({ length: cap + 1 }, () => 0)\n );\n // \u72b6\u6001\u8f6c\u79fb\n for (let i = 1; i <= n; i++) {\n for (let c = 1; c <= cap; c++) {\n if (wgt[i - 1] > c) {\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n dp[i][c] = dp[i - 1][c];\n } else {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[i][c] = Math.max(\n dp[i - 1][c],\n dp[i][c - wgt[i - 1]] + val[i - 1]\n );\n }\n }\n }\n return dp[n][cap];\n}\n
unbounded_knapsack.dart/* \u5b8c\u5168\u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nint unboundedKnapsackDP(List<int> wgt, List<int> val, int cap) {\n int n = wgt.length;\n // \u521d\u59cb\u5316 dp \u8868\n List<List<int>> dp = List.generate(n + 1, (index) => List.filled(cap + 1, 0));\n // \u72b6\u6001\u8f6c\u79fb\n for (int i = 1; i <= n; i++) {\n for (int c = 1; c <= cap; c++) {\n if (wgt[i - 1] > c) {\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n dp[i][c] = dp[i - 1][c];\n } else {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[i][c] = max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1]);\n }\n }\n }\n return dp[n][cap];\n}\n
unbounded_knapsack.rs/* \u5b8c\u5168\u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nfn unbounded_knapsack_dp(wgt: &[i32], val: &[i32], cap: usize) -> i32 {\n let n = wgt.len();\n // \u521d\u59cb\u5316 dp \u8868\n let mut dp = vec![vec![0; cap + 1]; n + 1];\n // \u72b6\u6001\u8f6c\u79fb\n for i in 1..=n {\n for c in 1..=cap {\n if wgt[i - 1] > c as i32 {\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n dp[i][c] = dp[i - 1][c];\n } else {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[i][c] = std::cmp::max(dp[i - 1][c], dp[i][c - wgt[i - 1] as usize] + val[i - 1]);\n }\n }\n }\n return dp[n][cap];\n}\n
unbounded_knapsack.c/* \u5b8c\u5168\u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nint unboundedKnapsackDP(int wgt[], int val[], int cap, int wgtSize) {\n int n = wgtSize;\n // \u521d\u59cb\u5316 dp \u8868\n int **dp = malloc((n + 1) * sizeof(int *));\n for (int i = 0; i <= n; i++) {\n dp[i] = calloc(cap + 1, sizeof(int));\n }\n // \u72b6\u6001\u8f6c\u79fb\n for (int i = 1; i <= n; i++) {\n for (int c = 1; c <= cap; c++) {\n if (wgt[i - 1] > c) {\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n dp[i][c] = dp[i - 1][c];\n } else {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[i][c] = myMax(dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1]);\n }\n }\n }\n int res = dp[n][cap];\n // \u91ca\u653e\u5185\u5b58\n for (int i = 0; i <= n; i++) {\n free(dp[i]);\n }\n return res;\n}\n
unbounded_knapsack.zig// \u5b8c\u5168\u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212\nfn unboundedKnapsackDP(comptime wgt: []i32, val: []i32, comptime cap: usize) i32 {\n comptime var n = wgt.len;\n // \u521d\u59cb\u5316 dp \u8868\n var dp = [_][cap + 1]i32{[_]i32{0} ** (cap + 1)} ** (n + 1);\n // \u72b6\u6001\u8f6c\u79fb\n for (1..n + 1) |i| {\n for (1..cap + 1) |c| {\n if (wgt[i - 1] > c) {\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n dp[i][c] = dp[i - 1][c];\n } else {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[i][c] = @max(dp[i - 1][c], dp[i][c - @as(usize, @intCast(wgt[i - 1]))] + val[i - 1]);\n }\n }\n }\n return dp[n][cap];\n}\n
"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#3","title":"3. \u00a0 \u7a7a\u95f4\u4f18\u5316","text":"\u7531\u4e8e\u5f53\u524d\u72b6\u6001\u662f\u4ece\u5de6\u8fb9\u548c\u4e0a\u8fb9\u7684\u72b6\u6001\u8f6c\u79fb\u800c\u6765\u7684\uff0c\u56e0\u6b64\u7a7a\u95f4\u4f18\u5316\u540e\u5e94\u8be5\u5bf9 \\(dp\\) \u8868\u4e2d\u7684\u6bcf\u4e00\u884c\u8fdb\u884c\u6b63\u5e8f\u904d\u5386\u3002
\u8fd9\u4e2a\u904d\u5386\u987a\u5e8f\u4e0e 0-1 \u80cc\u5305\u6b63\u597d\u76f8\u53cd\u3002\u8bf7\u501f\u52a9\u56fe 14-23 \u6765\u7406\u89e3\u4e24\u8005\u7684\u533a\u522b\u3002
<1><2><3><4><5><6>\u56fe 14-23 \u00a0 \u5b8c\u5168\u80cc\u5305\u95ee\u9898\u5728\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\u8fc7\u7a0b
\u4ee3\u7801\u5b9e\u73b0\u6bd4\u8f83\u7b80\u5355\uff0c\u4ec5\u9700\u5c06\u6570\u7ec4 dp
\u7684\u7b2c\u4e00\u7ef4\u5220\u9664\uff1a
def unbounded_knapsack_dp_comp(wgt: list[int], val: list[int], cap: int) -> int:\n \"\"\"\u5b8c\u5168\u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\"\"\"\n n = len(wgt)\n # \u521d\u59cb\u5316 dp \u8868\n dp = [0] * (cap + 1)\n # \u72b6\u6001\u8f6c\u79fb\n for i in range(1, n + 1):\n # \u6b63\u5e8f\u904d\u5386\n for c in range(1, cap + 1):\n if wgt[i - 1] > c:\n # \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n dp[c] = dp[c]\n else:\n # \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1])\n return dp[cap]\n
unbounded_knapsack.cpp/* \u5b8c\u5168\u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint unboundedKnapsackDPComp(vector<int> &wgt, vector<int> &val, int cap) {\n int n = wgt.size();\n // \u521d\u59cb\u5316 dp \u8868\n vector<int> dp(cap + 1, 0);\n // \u72b6\u6001\u8f6c\u79fb\n for (int i = 1; i <= n; i++) {\n for (int c = 1; c <= cap; c++) {\n if (wgt[i - 1] > c) {\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n dp[c] = dp[c];\n } else {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n }\n }\n }\n return dp[cap];\n}\n
unbounded_knapsack.java/* \u5b8c\u5168\u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint unboundedKnapsackDPComp(int[] wgt, int[] val, int cap) {\n int n = wgt.length;\n // \u521d\u59cb\u5316 dp \u8868\n int[] dp = new int[cap + 1];\n // \u72b6\u6001\u8f6c\u79fb\n for (int i = 1; i <= n; i++) {\n for (int c = 1; c <= cap; c++) {\n if (wgt[i - 1] > c) {\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n dp[c] = dp[c];\n } else {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[c] = Math.max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n }\n }\n }\n return dp[cap];\n}\n
unbounded_knapsack.cs/* \u5b8c\u5168\u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint UnboundedKnapsackDPComp(int[] wgt, int[] val, int cap) {\n int n = wgt.Length;\n // \u521d\u59cb\u5316 dp \u8868\n int[] dp = new int[cap + 1];\n // \u72b6\u6001\u8f6c\u79fb\n for (int i = 1; i <= n; i++) {\n for (int c = 1; c <= cap; c++) {\n if (wgt[i - 1] > c) {\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n dp[c] = dp[c];\n } else {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[c] = Math.Max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n }\n }\n }\n return dp[cap];\n}\n
unbounded_knapsack.go/* \u5b8c\u5168\u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc unboundedKnapsackDPComp(wgt, val []int, cap int) int {\n n := len(wgt)\n // \u521d\u59cb\u5316 dp \u8868\n dp := make([]int, cap+1)\n // \u72b6\u6001\u8f6c\u79fb\n for i := 1; i <= n; i++ {\n for c := 1; c <= cap; c++ {\n if wgt[i-1] > c {\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n dp[c] = dp[c]\n } else {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[c] = int(math.Max(float64(dp[c]), float64(dp[c-wgt[i-1]]+val[i-1])))\n }\n }\n }\n return dp[cap]\n}\n
unbounded_knapsack.swift/* \u5b8c\u5168\u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc unboundedKnapsackDPComp(wgt: [Int], val: [Int], cap: Int) -> Int {\n let n = wgt.count\n // \u521d\u59cb\u5316 dp \u8868\n var dp = Array(repeating: 0, count: cap + 1)\n // \u72b6\u6001\u8f6c\u79fb\n for i in stride(from: 1, through: n, by: 1) {\n for c in stride(from: 1, through: cap, by: 1) {\n if wgt[i - 1] > c {\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n dp[c] = dp[c]\n } else {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1])\n }\n }\n }\n return dp[cap]\n}\n
unbounded_knapsack.js/* \u5b8c\u5168\u80cc\u5305\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction unboundedKnapsackDPComp(wgt, val, cap) {\n const n = wgt.length;\n // \u521d\u59cb\u5316 dp \u8868\n const dp = Array.from({ length: cap + 1 }, () => 0);\n // \u72b6\u6001\u8f6c\u79fb\n for (let i = 1; i <= n; i++) {\n for (let c = 1; c <= cap; c++) {\n if (wgt[i - 1] > c) {\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n dp[c] = dp[c];\n } else {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[c] = Math.max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n }\n }\n }\n return dp[cap];\n}\n
unbounded_knapsack.ts/* \u5b8c\u5168\u80cc\u5305\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction unboundedKnapsackDPComp(\n wgt: Array<number>,\n val: Array<number>,\n cap: number\n): number {\n const n = wgt.length;\n // \u521d\u59cb\u5316 dp \u8868\n const dp = Array.from({ length: cap + 1 }, () => 0);\n // \u72b6\u6001\u8f6c\u79fb\n for (let i = 1; i <= n; i++) {\n for (let c = 1; c <= cap; c++) {\n if (wgt[i - 1] > c) {\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n dp[c] = dp[c];\n } else {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[c] = Math.max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n }\n }\n }\n return dp[cap];\n}\n
unbounded_knapsack.dart/* \u5b8c\u5168\u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint unboundedKnapsackDPComp(List<int> wgt, List<int> val, int cap) {\n int n = wgt.length;\n // \u521d\u59cb\u5316 dp \u8868\n List<int> dp = List.filled(cap + 1, 0);\n // \u72b6\u6001\u8f6c\u79fb\n for (int i = 1; i <= n; i++) {\n for (int c = 1; c <= cap; c++) {\n if (wgt[i - 1] > c) {\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n dp[c] = dp[c];\n } else {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n }\n }\n }\n return dp[cap];\n}\n
unbounded_knapsack.rs/* \u5b8c\u5168\u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfn unbounded_knapsack_dp_comp(wgt: &[i32], val: &[i32], cap: usize) -> i32 {\n let n = wgt.len();\n // \u521d\u59cb\u5316 dp \u8868\n let mut dp = vec![0; cap + 1];\n // \u72b6\u6001\u8f6c\u79fb\n for i in 1..=n {\n for c in 1..=cap {\n if wgt[i - 1] > c as i32 {\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n dp[c] = dp[c];\n } else {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[c] = std::cmp::max(dp[c], dp[c - wgt[i - 1] as usize] + val[i - 1]);\n }\n }\n }\n dp[cap]\n}\n
unbounded_knapsack.c/* \u5b8c\u5168\u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint unboundedKnapsackDPComp(int wgt[], int val[], int cap, int wgtSize) {\n int n = wgtSize;\n // \u521d\u59cb\u5316 dp \u8868\n int *dp = calloc(cap + 1, sizeof(int));\n // \u72b6\u6001\u8f6c\u79fb\n for (int i = 1; i <= n; i++) {\n for (int c = 1; c <= cap; c++) {\n if (wgt[i - 1] > c) {\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n dp[c] = dp[c];\n } else {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[c] = myMax(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n }\n }\n }\n int res = dp[cap];\n // \u91ca\u653e\u5185\u5b58\n free(dp);\n return res;\n}\n
unbounded_knapsack.zig// \u5b8c\u5168\u80cc\u5305\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\nfn unboundedKnapsackDPComp(comptime wgt: []i32, val: []i32, comptime cap: usize) i32 {\n comptime var n = wgt.len;\n // \u521d\u59cb\u5316 dp \u8868\n var dp = [_]i32{0} ** (cap + 1);\n // \u72b6\u6001\u8f6c\u79fb\n for (1..n + 1) |i| {\n for (1..cap + 1) |c| {\n if (wgt[i - 1] > c) {\n // \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\n dp[c] = dp[c];\n } else {\n // \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\n dp[c] = @max(dp[c], dp[c - @as(usize, @intCast(wgt[i - 1]))] + val[i - 1]);\n }\n }\n }\n return dp[cap];\n}\n
"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#1452","title":"14.5.2 \u00a0 \u96f6\u94b1\u5151\u6362\u95ee\u9898","text":"\u80cc\u5305\u95ee\u9898\u662f\u4e00\u5927\u7c7b\u52a8\u6001\u89c4\u5212\u95ee\u9898\u7684\u4ee3\u8868\uff0c\u5176\u62e5\u6709\u5f88\u591a\u53d8\u79cd\uff0c\u4f8b\u5982\u96f6\u94b1\u5151\u6362\u95ee\u9898\u3002
Question
\u7ed9\u5b9a \\(n\\) \u79cd\u786c\u5e01\uff0c\u7b2c \\(i\\) \u79cd\u786c\u5e01\u7684\u9762\u503c\u4e3a \\(coins[i - 1]\\) \uff0c\u76ee\u6807\u91d1\u989d\u4e3a \\(amt\\) \uff0c\u6bcf\u79cd\u786c\u5e01\u53ef\u4ee5\u91cd\u590d\u9009\u53d6\uff0c\u95ee\u80fd\u591f\u51d1\u51fa\u76ee\u6807\u91d1\u989d\u7684\u6700\u5c11\u786c\u5e01\u6570\u91cf\u3002\u5982\u679c\u65e0\u6cd5\u51d1\u51fa\u76ee\u6807\u91d1\u989d\uff0c\u5219\u8fd4\u56de \\(-1\\) \u3002\u793a\u4f8b\u5982\u56fe 14-24 \u6240\u793a\u3002
\u56fe 14-24 \u00a0 \u96f6\u94b1\u5151\u6362\u95ee\u9898\u7684\u793a\u4f8b\u6570\u636e
"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#1_1","title":"1. \u00a0 \u52a8\u6001\u89c4\u5212\u601d\u8def","text":"\u96f6\u94b1\u5151\u6362\u53ef\u4ee5\u770b\u4f5c\u5b8c\u5168\u80cc\u5305\u95ee\u9898\u7684\u4e00\u79cd\u7279\u6b8a\u60c5\u51b5\uff0c\u4e24\u8005\u5177\u6709\u4ee5\u4e0b\u8054\u7cfb\u4e0e\u4e0d\u540c\u70b9\u3002
\u7b2c\u4e00\u6b65\uff1a\u601d\u8003\u6bcf\u8f6e\u7684\u51b3\u7b56\uff0c\u5b9a\u4e49\u72b6\u6001\uff0c\u4ece\u800c\u5f97\u5230 \\(dp\\) \u8868
\u72b6\u6001 \\([i, a]\\) \u5bf9\u5e94\u7684\u5b50\u95ee\u9898\u4e3a\uff1a\u524d \\(i\\) \u79cd\u786c\u5e01\u80fd\u591f\u51d1\u51fa\u91d1\u989d \\(a\\) \u7684\u6700\u5c11\u786c\u5e01\u6570\u91cf\uff0c\u8bb0\u4e3a \\(dp[i, a]\\) \u3002
\u4e8c\u7ef4 \\(dp\\) \u8868\u7684\u5c3a\u5bf8\u4e3a \\((n+1) \\times (amt+1)\\) \u3002
\u7b2c\u4e8c\u6b65\uff1a\u627e\u51fa\u6700\u4f18\u5b50\u7ed3\u6784\uff0c\u8fdb\u800c\u63a8\u5bfc\u51fa\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b
\u672c\u9898\u4e0e\u5b8c\u5168\u80cc\u5305\u95ee\u9898\u7684\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u5b58\u5728\u4ee5\u4e0b\u4e24\u70b9\u5dee\u5f02\u3002
\u7b2c\u4e09\u6b65\uff1a\u786e\u5b9a\u8fb9\u754c\u6761\u4ef6\u548c\u72b6\u6001\u8f6c\u79fb\u987a\u5e8f
\u5f53\u76ee\u6807\u91d1\u989d\u4e3a \\(0\\) \u65f6\uff0c\u51d1\u51fa\u5b83\u7684\u6700\u5c11\u786c\u5e01\u6570\u91cf\u4e3a \\(0\\) \uff0c\u5373\u9996\u5217\u6240\u6709 \\(dp[i, 0]\\) \u90fd\u7b49\u4e8e \\(0\\) \u3002
\u5f53\u65e0\u786c\u5e01\u65f6\uff0c\u65e0\u6cd5\u51d1\u51fa\u4efb\u610f \\(> 0\\) \u7684\u76ee\u6807\u91d1\u989d\uff0c\u5373\u662f\u65e0\u6548\u89e3\u3002\u4e3a\u4f7f\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u4e2d\u7684 \\(\\min()\\) \u51fd\u6570\u80fd\u591f\u8bc6\u522b\u5e76\u8fc7\u6ee4\u65e0\u6548\u89e3\uff0c\u6211\u4eec\u8003\u8651\u4f7f\u7528 \\(+ \\infty\\) \u6765\u8868\u793a\u5b83\u4eec\uff0c\u5373\u4ee4\u9996\u884c\u6240\u6709 \\(dp[0, a]\\) \u90fd\u7b49\u4e8e \\(+ \\infty\\) \u3002
"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#2_1","title":"2. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"\u5927\u591a\u6570\u7f16\u7a0b\u8bed\u8a00\u5e76\u672a\u63d0\u4f9b \\(+ \\infty\\) \u53d8\u91cf\uff0c\u53ea\u80fd\u4f7f\u7528\u6574\u578b int
\u7684\u6700\u5927\u503c\u6765\u4ee3\u66ff\u3002\u800c\u8fd9\u53c8\u4f1a\u5bfc\u81f4\u5927\u6570\u8d8a\u754c\uff1a\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u4e2d\u7684 \\(+ 1\\) \u64cd\u4f5c\u53ef\u80fd\u53d1\u751f\u6ea2\u51fa\u3002
\u4e3a\u6b64\uff0c\u6211\u4eec\u91c7\u7528\u6570\u5b57 \\(amt + 1\\) \u6765\u8868\u793a\u65e0\u6548\u89e3\uff0c\u56e0\u4e3a\u51d1\u51fa \\(amt\\) \u7684\u786c\u5e01\u6570\u91cf\u6700\u591a\u4e3a \\(amt\\) \u3002\u6700\u540e\u8fd4\u56de\u524d\uff0c\u5224\u65ad \\(dp[n, amt]\\) \u662f\u5426\u7b49\u4e8e \\(amt + 1\\) \uff0c\u82e5\u662f\u5219\u8fd4\u56de \\(-1\\) \uff0c\u4ee3\u8868\u65e0\u6cd5\u51d1\u51fa\u76ee\u6807\u91d1\u989d\u3002\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig coin_change.pydef coin_change_dp(coins: list[int], amt: int) -> int:\n \"\"\"\u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212\"\"\"\n n = len(coins)\n MAX = amt + 1\n # \u521d\u59cb\u5316 dp \u8868\n dp = [[0] * (amt + 1) for _ in range(n + 1)]\n # \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n for a in range(1, amt + 1):\n dp[0][a] = MAX\n # \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n for i in range(1, n + 1):\n for a in range(1, amt + 1):\n if coins[i - 1] > a:\n # \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[i][a] = dp[i - 1][a]\n else:\n # \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n dp[i][a] = min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1)\n return dp[n][amt] if dp[n][amt] != MAX else -1\n
coin_change.cpp/* \u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212 */\nint coinChangeDP(vector<int> &coins, int amt) {\n int n = coins.size();\n int MAX = amt + 1;\n // \u521d\u59cb\u5316 dp \u8868\n vector<vector<int>> dp(n + 1, vector<int>(amt + 1, 0));\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n for (int a = 1; a <= amt; a++) {\n dp[0][a] = MAX;\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n for (int i = 1; i <= n; i++) {\n for (int a = 1; a <= amt; a++) {\n if (coins[i - 1] > a) {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[i][a] = dp[i - 1][a];\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n dp[i][a] = min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1);\n }\n }\n }\n return dp[n][amt] != MAX ? dp[n][amt] : -1;\n}\n
coin_change.java/* \u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212 */\nint coinChangeDP(int[] coins, int amt) {\n int n = coins.length;\n int MAX = amt + 1;\n // \u521d\u59cb\u5316 dp \u8868\n int[][] dp = new int[n + 1][amt + 1];\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n for (int a = 1; a <= amt; a++) {\n dp[0][a] = MAX;\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n for (int i = 1; i <= n; i++) {\n for (int a = 1; a <= amt; a++) {\n if (coins[i - 1] > a) {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[i][a] = dp[i - 1][a];\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n dp[i][a] = Math.min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1);\n }\n }\n }\n return dp[n][amt] != MAX ? dp[n][amt] : -1;\n}\n
coin_change.cs/* \u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212 */\nint CoinChangeDP(int[] coins, int amt) {\n int n = coins.Length;\n int MAX = amt + 1;\n // \u521d\u59cb\u5316 dp \u8868\n int[,] dp = new int[n + 1, amt + 1];\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n for (int a = 1; a <= amt; a++) {\n dp[0, a] = MAX;\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n for (int i = 1; i <= n; i++) {\n for (int a = 1; a <= amt; a++) {\n if (coins[i - 1] > a) {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[i, a] = dp[i - 1, a];\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n dp[i, a] = Math.Min(dp[i - 1, a], dp[i, a - coins[i - 1]] + 1);\n }\n }\n }\n return dp[n, amt] != MAX ? dp[n, amt] : -1;\n}\n
coin_change.go/* \u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc coinChangeDP(coins []int, amt int) int {\n n := len(coins)\n max := amt + 1\n // \u521d\u59cb\u5316 dp \u8868\n dp := make([][]int, n+1)\n for i := 0; i <= n; i++ {\n dp[i] = make([]int, amt+1)\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n for a := 1; a <= amt; a++ {\n dp[0][a] = max\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n for i := 1; i <= n; i++ {\n for a := 1; a <= amt; a++ {\n if coins[i-1] > a {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[i][a] = dp[i-1][a]\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n dp[i][a] = int(math.Min(float64(dp[i-1][a]), float64(dp[i][a-coins[i-1]]+1)))\n }\n }\n }\n if dp[n][amt] != max {\n return dp[n][amt]\n }\n return -1\n}\n
coin_change.swift/* \u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc coinChangeDP(coins: [Int], amt: Int) -> Int {\n let n = coins.count\n let MAX = amt + 1\n // \u521d\u59cb\u5316 dp \u8868\n var dp = Array(repeating: Array(repeating: 0, count: amt + 1), count: n + 1)\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n for a in stride(from: 1, through: amt, by: 1) {\n dp[0][a] = MAX\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n for i in stride(from: 1, through: n, by: 1) {\n for a in stride(from: 1, through: amt, by: 1) {\n if coins[i - 1] > a {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[i][a] = dp[i - 1][a]\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n dp[i][a] = min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1)\n }\n }\n }\n return dp[n][amt] != MAX ? dp[n][amt] : -1\n}\n
coin_change.js/* \u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction coinChangeDP(coins, amt) {\n const n = coins.length;\n const MAX = amt + 1;\n // \u521d\u59cb\u5316 dp \u8868\n const dp = Array.from({ length: n + 1 }, () =>\n Array.from({ length: amt + 1 }, () => 0)\n );\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n for (let a = 1; a <= amt; a++) {\n dp[0][a] = MAX;\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n for (let i = 1; i <= n; i++) {\n for (let a = 1; a <= amt; a++) {\n if (coins[i - 1] > a) {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[i][a] = dp[i - 1][a];\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n dp[i][a] = Math.min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1);\n }\n }\n }\n return dp[n][amt] !== MAX ? dp[n][amt] : -1;\n}\n
coin_change.ts/* \u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction coinChangeDP(coins: Array<number>, amt: number): number {\n const n = coins.length;\n const MAX = amt + 1;\n // \u521d\u59cb\u5316 dp \u8868\n const dp = Array.from({ length: n + 1 }, () =>\n Array.from({ length: amt + 1 }, () => 0)\n );\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n for (let a = 1; a <= amt; a++) {\n dp[0][a] = MAX;\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n for (let i = 1; i <= n; i++) {\n for (let a = 1; a <= amt; a++) {\n if (coins[i - 1] > a) {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[i][a] = dp[i - 1][a];\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n dp[i][a] = Math.min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1);\n }\n }\n }\n return dp[n][amt] !== MAX ? dp[n][amt] : -1;\n}\n
coin_change.dart/* \u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212 */\nint coinChangeDP(List<int> coins, int amt) {\n int n = coins.length;\n int MAX = amt + 1;\n // \u521d\u59cb\u5316 dp \u8868\n List<List<int>> dp = List.generate(n + 1, (index) => List.filled(amt + 1, 0));\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n for (int a = 1; a <= amt; a++) {\n dp[0][a] = MAX;\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n for (int i = 1; i <= n; i++) {\n for (int a = 1; a <= amt; a++) {\n if (coins[i - 1] > a) {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[i][a] = dp[i - 1][a];\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n dp[i][a] = min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1);\n }\n }\n }\n return dp[n][amt] != MAX ? dp[n][amt] : -1;\n}\n
coin_change.rs/* \u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212 */\nfn coin_change_dp(coins: &[i32], amt: usize) -> i32 {\n let n = coins.len();\n let max = amt + 1;\n // \u521d\u59cb\u5316 dp \u8868\n let mut dp = vec![vec![0; amt + 1]; n + 1];\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n for a in 1..= amt {\n dp[0][a] = max;\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n for i in 1..=n {\n for a in 1..=amt {\n if coins[i - 1] > a as i32 {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[i][a] = dp[i - 1][a];\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n dp[i][a] = std::cmp::min(dp[i - 1][a], dp[i][a - coins[i - 1] as usize] + 1);\n }\n }\n }\n if dp[n][amt] != max { return dp[n][amt] as i32; } else { -1 }\n}\n
coin_change.c/* \u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212 */\nint coinChangeDP(int coins[], int amt, int coinsSize) {\n int n = coinsSize;\n int MAX = amt + 1;\n // \u521d\u59cb\u5316 dp \u8868\n int **dp = malloc((n + 1) * sizeof(int *));\n for (int i = 0; i <= n; i++) {\n dp[i] = calloc(amt + 1, sizeof(int));\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n for (int a = 1; a <= amt; a++) {\n dp[0][a] = MAX;\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n for (int i = 1; i <= n; i++) {\n for (int a = 1; a <= amt; a++) {\n if (coins[i - 1] > a) {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[i][a] = dp[i - 1][a];\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n dp[i][a] = myMin(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1);\n }\n }\n }\n int res = dp[n][amt] != MAX ? dp[n][amt] : -1;\n // \u91ca\u653e\u5185\u5b58\n for (int i = 0; i <= n; i++) {\n free(dp[i]);\n }\n free(dp);\n return res;\n}\n
coin_change.zig// \u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212\nfn coinChangeDP(comptime coins: []i32, comptime amt: usize) i32 {\n comptime var n = coins.len;\n comptime var max = amt + 1;\n // \u521d\u59cb\u5316 dp \u8868\n var dp = [_][amt + 1]i32{[_]i32{0} ** (amt + 1)} ** (n + 1);\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\n for (1..amt + 1) |a| {\n dp[0][a] = max;\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n for (1..n + 1) |i| {\n for (1..amt + 1) |a| {\n if (coins[i - 1] > @as(i32, @intCast(a))) {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[i][a] = dp[i - 1][a];\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n dp[i][a] = @min(dp[i - 1][a], dp[i][a - @as(usize, @intCast(coins[i - 1]))] + 1);\n }\n }\n }\n if (dp[n][amt] != max) {\n return @intCast(dp[n][amt]);\n } else {\n return -1;\n }\n}\n
\u56fe 14-25 \u5c55\u793a\u4e86\u96f6\u94b1\u5151\u6362\u7684\u52a8\u6001\u89c4\u5212\u8fc7\u7a0b\uff0c\u548c\u5b8c\u5168\u80cc\u5305\u95ee\u9898\u975e\u5e38\u76f8\u4f3c\u3002
<1><2><3><4><5><6><7><8><9><10><11><12><13><14><15>\u56fe 14-25 \u00a0 \u96f6\u94b1\u5151\u6362\u95ee\u9898\u7684\u52a8\u6001\u89c4\u5212\u8fc7\u7a0b
"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#3_1","title":"3. \u00a0 \u7a7a\u95f4\u4f18\u5316","text":"\u96f6\u94b1\u5151\u6362\u7684\u7a7a\u95f4\u4f18\u5316\u7684\u5904\u7406\u65b9\u5f0f\u548c\u5b8c\u5168\u80cc\u5305\u95ee\u9898\u4e00\u81f4\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig coin_change.pydef coin_change_dp_comp(coins: list[int], amt: int) -> int:\n \"\"\"\u96f6\u94b1\u5151\u6362\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\"\"\"\n n = len(coins)\n MAX = amt + 1\n # \u521d\u59cb\u5316 dp \u8868\n dp = [MAX] * (amt + 1)\n dp[0] = 0\n # \u72b6\u6001\u8f6c\u79fb\n for i in range(1, n + 1):\n # \u6b63\u5e8f\u904d\u5386\n for a in range(1, amt + 1):\n if coins[i - 1] > a:\n # \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[a] = dp[a]\n else:\n # \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n dp[a] = min(dp[a], dp[a - coins[i - 1]] + 1)\n return dp[amt] if dp[amt] != MAX else -1\n
coin_change.cpp/* \u96f6\u94b1\u5151\u6362\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint coinChangeDPComp(vector<int> &coins, int amt) {\n int n = coins.size();\n int MAX = amt + 1;\n // \u521d\u59cb\u5316 dp \u8868\n vector<int> dp(amt + 1, MAX);\n dp[0] = 0;\n // \u72b6\u6001\u8f6c\u79fb\n for (int i = 1; i <= n; i++) {\n for (int a = 1; a <= amt; a++) {\n if (coins[i - 1] > a) {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[a] = dp[a];\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n dp[a] = min(dp[a], dp[a - coins[i - 1]] + 1);\n }\n }\n }\n return dp[amt] != MAX ? dp[amt] : -1;\n}\n
coin_change.java/* \u96f6\u94b1\u5151\u6362\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint coinChangeDPComp(int[] coins, int amt) {\n int n = coins.length;\n int MAX = amt + 1;\n // \u521d\u59cb\u5316 dp \u8868\n int[] dp = new int[amt + 1];\n Arrays.fill(dp, MAX);\n dp[0] = 0;\n // \u72b6\u6001\u8f6c\u79fb\n for (int i = 1; i <= n; i++) {\n for (int a = 1; a <= amt; a++) {\n if (coins[i - 1] > a) {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[a] = dp[a];\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n dp[a] = Math.min(dp[a], dp[a - coins[i - 1]] + 1);\n }\n }\n }\n return dp[amt] != MAX ? dp[amt] : -1;\n}\n
coin_change.cs/* \u96f6\u94b1\u5151\u6362\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint CoinChangeDPComp(int[] coins, int amt) {\n int n = coins.Length;\n int MAX = amt + 1;\n // \u521d\u59cb\u5316 dp \u8868\n int[] dp = new int[amt + 1];\n Array.Fill(dp, MAX);\n dp[0] = 0;\n // \u72b6\u6001\u8f6c\u79fb\n for (int i = 1; i <= n; i++) {\n for (int a = 1; a <= amt; a++) {\n if (coins[i - 1] > a) {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[a] = dp[a];\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n dp[a] = Math.Min(dp[a], dp[a - coins[i - 1]] + 1);\n }\n }\n }\n return dp[amt] != MAX ? dp[amt] : -1;\n}\n
coin_change.go/* \u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc coinChangeDPComp(coins []int, amt int) int {\n n := len(coins)\n max := amt + 1\n // \u521d\u59cb\u5316 dp \u8868\n dp := make([]int, amt+1)\n for i := 1; i <= amt; i++ {\n dp[i] = max\n }\n // \u72b6\u6001\u8f6c\u79fb\n for i := 1; i <= n; i++ {\n // \u5012\u5e8f\u904d\u5386\n for a := 1; a <= amt; a++ {\n if coins[i-1] > a {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[a] = dp[a]\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n dp[a] = int(math.Min(float64(dp[a]), float64(dp[a-coins[i-1]]+1)))\n }\n }\n }\n if dp[amt] != max {\n return dp[amt]\n }\n return -1\n}\n
coin_change.swift/* \u96f6\u94b1\u5151\u6362\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc coinChangeDPComp(coins: [Int], amt: Int) -> Int {\n let n = coins.count\n let MAX = amt + 1\n // \u521d\u59cb\u5316 dp \u8868\n var dp = Array(repeating: MAX, count: amt + 1)\n dp[0] = 0\n // \u72b6\u6001\u8f6c\u79fb\n for i in stride(from: 1, through: n, by: 1) {\n for a in stride(from: 1, through: amt, by: 1) {\n if coins[i - 1] > a {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[a] = dp[a]\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n dp[a] = min(dp[a], dp[a - coins[i - 1]] + 1)\n }\n }\n }\n return dp[amt] != MAX ? dp[amt] : -1\n}\n
coin_change.js/* \u96f6\u94b1\u5151\u6362\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction coinChangeDPComp(coins, amt) {\n const n = coins.length;\n const MAX = amt + 1;\n // \u521d\u59cb\u5316 dp \u8868\n const dp = Array.from({ length: amt + 1 }, () => MAX);\n dp[0] = 0;\n // \u72b6\u6001\u8f6c\u79fb\n for (let i = 1; i <= n; i++) {\n for (let a = 1; a <= amt; a++) {\n if (coins[i - 1] > a) {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[a] = dp[a];\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n dp[a] = Math.min(dp[a], dp[a - coins[i - 1]] + 1);\n }\n }\n }\n return dp[amt] !== MAX ? dp[amt] : -1;\n}\n
coin_change.ts/* \u96f6\u94b1\u5151\u6362\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction coinChangeDPComp(coins: Array<number>, amt: number): number {\n const n = coins.length;\n const MAX = amt + 1;\n // \u521d\u59cb\u5316 dp \u8868\n const dp = Array.from({ length: amt + 1 }, () => MAX);\n dp[0] = 0;\n // \u72b6\u6001\u8f6c\u79fb\n for (let i = 1; i <= n; i++) {\n for (let a = 1; a <= amt; a++) {\n if (coins[i - 1] > a) {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[a] = dp[a];\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n dp[a] = Math.min(dp[a], dp[a - coins[i - 1]] + 1);\n }\n }\n }\n return dp[amt] !== MAX ? dp[amt] : -1;\n}\n
coin_change.dart/* \u96f6\u94b1\u5151\u6362\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint coinChangeDPComp(List<int> coins, int amt) {\n int n = coins.length;\n int MAX = amt + 1;\n // \u521d\u59cb\u5316 dp \u8868\n List<int> dp = List.filled(amt + 1, MAX);\n dp[0] = 0;\n // \u72b6\u6001\u8f6c\u79fb\n for (int i = 1; i <= n; i++) {\n for (int a = 1; a <= amt; a++) {\n if (coins[i - 1] > a) {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[a] = dp[a];\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n dp[a] = min(dp[a], dp[a - coins[i - 1]] + 1);\n }\n }\n }\n return dp[amt] != MAX ? dp[amt] : -1;\n}\n
coin_change.rs/* \u96f6\u94b1\u5151\u6362\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfn coin_change_dp_comp(coins: &[i32], amt: usize) -> i32 {\n let n = coins.len();\n let max = amt + 1;\n // \u521d\u59cb\u5316 dp \u8868\n let mut dp = vec![0; amt + 1];\n dp.fill(max);\n dp[0] = 0;\n // \u72b6\u6001\u8f6c\u79fb\n for i in 1..=n {\n for a in 1..=amt {\n if coins[i - 1] > a as i32 {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[a] = dp[a];\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n dp[a] = std::cmp::min(dp[a], dp[a - coins[i - 1] as usize] + 1);\n }\n }\n }\n if dp[amt] != max { return dp[amt] as i32; } else { -1 }\n}\n
coin_change.c/* \u96f6\u94b1\u5151\u6362\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint coinChangeDPComp(int coins[], int amt, int coinsSize) {\n int n = coinsSize;\n int MAX = amt + 1;\n // \u521d\u59cb\u5316 dp \u8868\n int *dp = calloc(amt + 1, sizeof(int));\n dp[0] = 0;\n // \u72b6\u6001\u8f6c\u79fb\n for (int i = 1; i <= n; i++) {\n for (int a = 1; a <= amt; a++) {\n if (coins[i - 1] > a) {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[a] = dp[a];\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n dp[a] = myMin(dp[a], dp[a - coins[i - 1]] + 1);\n }\n }\n }\n int res = dp[amt] != MAX ? dp[amt] : -1;\n // \u91ca\u653e\u5185\u5b58\n free(dp);\n return res;\n}\n
coin_change.zig// \u96f6\u94b1\u5151\u6362\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\nfn coinChangeDPComp(comptime coins: []i32, comptime amt: usize) i32 {\n comptime var n = coins.len;\n comptime var max = amt + 1;\n // \u521d\u59cb\u5316 dp \u8868\n var dp = [_]i32{0} ** (amt + 1);\n @memset(&dp, max);\n dp[0] = 0;\n // \u72b6\u6001\u8f6c\u79fb\n for (1..n + 1) |i| {\n for (1..amt + 1) |a| {\n if (coins[i - 1] > @as(i32, @intCast(a))) {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[a] = dp[a];\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n dp[a] = @min(dp[a], dp[a - @as(usize, @intCast(coins[i - 1]))] + 1);\n }\n }\n }\n if (dp[amt] != max) {\n return @intCast(dp[amt]);\n } else {\n return -1;\n }\n}\n
"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#1453-ii","title":"14.5.3 \u00a0 \u96f6\u94b1\u5151\u6362\u95ee\u9898 II","text":"Question
\u7ed9\u5b9a \\(n\\) \u79cd\u786c\u5e01\uff0c\u7b2c \\(i\\) \u79cd\u786c\u5e01\u7684\u9762\u503c\u4e3a \\(coins[i - 1]\\) \uff0c\u76ee\u6807\u91d1\u989d\u4e3a \\(amt\\) \uff0c\u6bcf\u79cd\u786c\u5e01\u53ef\u4ee5\u91cd\u590d\u9009\u53d6\uff0c\u95ee\u51d1\u51fa\u76ee\u6807\u91d1\u989d\u7684\u786c\u5e01\u7ec4\u5408\u6570\u91cf\u3002\u793a\u4f8b\u5982\u56fe 14-26 \u6240\u793a\u3002
\u56fe 14-26 \u00a0 \u96f6\u94b1\u5151\u6362\u95ee\u9898 II \u7684\u793a\u4f8b\u6570\u636e
"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#1_2","title":"1. \u00a0 \u52a8\u6001\u89c4\u5212\u601d\u8def","text":"\u76f8\u6bd4\u4e8e\u4e0a\u4e00\u9898\uff0c\u672c\u9898\u76ee\u6807\u662f\u6c42\u7ec4\u5408\u6570\u91cf\uff0c\u56e0\u6b64\u5b50\u95ee\u9898\u53d8\u4e3a\uff1a\u524d \\(i\\) \u79cd\u786c\u5e01\u80fd\u591f\u51d1\u51fa\u91d1\u989d \\(a\\) \u7684\u7ec4\u5408\u6570\u91cf\u3002\u800c \\(dp\\) \u8868\u4ecd\u7136\u662f\u5c3a\u5bf8\u4e3a \\((n+1) \\times (amt + 1)\\) \u7684\u4e8c\u7ef4\u77e9\u9635\u3002
\u5f53\u524d\u72b6\u6001\u7684\u7ec4\u5408\u6570\u91cf\u7b49\u4e8e\u4e0d\u9009\u5f53\u524d\u786c\u5e01\u4e0e\u9009\u5f53\u524d\u786c\u5e01\u8fd9\u4e24\u79cd\u51b3\u7b56\u7684\u7ec4\u5408\u6570\u91cf\u4e4b\u548c\u3002\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u4e3a\uff1a
\\[ dp[i, a] = dp[i-1, a] + dp[i, a - coins[i-1]] \\]\u5f53\u76ee\u6807\u91d1\u989d\u4e3a \\(0\\) \u65f6\uff0c\u65e0\u987b\u9009\u62e9\u4efb\u4f55\u786c\u5e01\u5373\u53ef\u51d1\u51fa\u76ee\u6807\u91d1\u989d\uff0c\u56e0\u6b64\u5e94\u5c06\u9996\u5217\u6240\u6709 \\(dp[i, 0]\\) \u90fd\u521d\u59cb\u5316\u4e3a \\(1\\) \u3002\u5f53\u65e0\u786c\u5e01\u65f6\uff0c\u65e0\u6cd5\u51d1\u51fa\u4efb\u4f55 \\(>0\\) \u7684\u76ee\u6807\u91d1\u989d\uff0c\u56e0\u6b64\u9996\u884c\u6240\u6709 \\(dp[0, a]\\) \u90fd\u7b49\u4e8e \\(0\\) \u3002
"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#2_2","title":"2. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"PythonC++JavaC#GoSwiftJSTSDartRustCZig coin_change_ii.pydef coin_change_ii_dp(coins: list[int], amt: int) -> int:\n \"\"\"\u96f6\u94b1\u5151\u6362 II\uff1a\u52a8\u6001\u89c4\u5212\"\"\"\n n = len(coins)\n # \u521d\u59cb\u5316 dp \u8868\n dp = [[0] * (amt + 1) for _ in range(n + 1)]\n # \u521d\u59cb\u5316\u9996\u5217\n for i in range(n + 1):\n dp[i][0] = 1\n # \u72b6\u6001\u8f6c\u79fb\n for i in range(1, n + 1):\n for a in range(1, amt + 1):\n if coins[i - 1] > a:\n # \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[i][a] = dp[i - 1][a]\n else:\n # \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]]\n return dp[n][amt]\n
coin_change_ii.cpp/* \u96f6\u94b1\u5151\u6362 II\uff1a\u52a8\u6001\u89c4\u5212 */\nint coinChangeIIDP(vector<int> &coins, int amt) {\n int n = coins.size();\n // \u521d\u59cb\u5316 dp \u8868\n vector<vector<int>> dp(n + 1, vector<int>(amt + 1, 0));\n // \u521d\u59cb\u5316\u9996\u5217\n for (int i = 0; i <= n; i++) {\n dp[i][0] = 1;\n }\n // \u72b6\u6001\u8f6c\u79fb\n for (int i = 1; i <= n; i++) {\n for (int a = 1; a <= amt; a++) {\n if (coins[i - 1] > a) {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[i][a] = dp[i - 1][a];\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]];\n }\n }\n }\n return dp[n][amt];\n}\n
coin_change_ii.java/* \u96f6\u94b1\u5151\u6362 II\uff1a\u52a8\u6001\u89c4\u5212 */\nint coinChangeIIDP(int[] coins, int amt) {\n int n = coins.length;\n // \u521d\u59cb\u5316 dp \u8868\n int[][] dp = new int[n + 1][amt + 1];\n // \u521d\u59cb\u5316\u9996\u5217\n for (int i = 0; i <= n; i++) {\n dp[i][0] = 1;\n }\n // \u72b6\u6001\u8f6c\u79fb\n for (int i = 1; i <= n; i++) {\n for (int a = 1; a <= amt; a++) {\n if (coins[i - 1] > a) {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[i][a] = dp[i - 1][a];\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]];\n }\n }\n }\n return dp[n][amt];\n}\n
coin_change_ii.cs/* \u96f6\u94b1\u5151\u6362 II\uff1a\u52a8\u6001\u89c4\u5212 */\nint CoinChangeIIDP(int[] coins, int amt) {\n int n = coins.Length;\n // \u521d\u59cb\u5316 dp \u8868\n int[,] dp = new int[n + 1, amt + 1];\n // \u521d\u59cb\u5316\u9996\u5217\n for (int i = 0; i <= n; i++) {\n dp[i, 0] = 1;\n }\n // \u72b6\u6001\u8f6c\u79fb\n for (int i = 1; i <= n; i++) {\n for (int a = 1; a <= amt; a++) {\n if (coins[i - 1] > a) {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[i, a] = dp[i - 1, a];\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n dp[i, a] = dp[i - 1, a] + dp[i, a - coins[i - 1]];\n }\n }\n }\n return dp[n, amt];\n}\n
coin_change_ii.go/* \u96f6\u94b1\u5151\u6362 II\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc coinChangeIIDP(coins []int, amt int) int {\n n := len(coins)\n // \u521d\u59cb\u5316 dp \u8868\n dp := make([][]int, n+1)\n for i := 0; i <= n; i++ {\n dp[i] = make([]int, amt+1)\n }\n // \u521d\u59cb\u5316\u9996\u5217\n for i := 0; i <= n; i++ {\n dp[i][0] = 1\n }\n // \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u548c\u5217\n for i := 1; i <= n; i++ {\n for a := 1; a <= amt; a++ {\n if coins[i-1] > a {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[i][a] = dp[i-1][a]\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n dp[i][a] = dp[i-1][a] + dp[i][a-coins[i-1]]\n }\n }\n }\n return dp[n][amt]\n}\n
coin_change_ii.swift/* \u96f6\u94b1\u5151\u6362 II\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc coinChangeIIDP(coins: [Int], amt: Int) -> Int {\n let n = coins.count\n // \u521d\u59cb\u5316 dp \u8868\n var dp = Array(repeating: Array(repeating: 0, count: amt + 1), count: n + 1)\n // \u521d\u59cb\u5316\u9996\u5217\n for i in stride(from: 0, through: n, by: 1) {\n dp[i][0] = 1\n }\n // \u72b6\u6001\u8f6c\u79fb\n for i in stride(from: 1, through: n, by: 1) {\n for a in stride(from: 1, through: amt, by: 1) {\n if coins[i - 1] > a {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[i][a] = dp[i - 1][a]\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]]\n }\n }\n }\n return dp[n][amt]\n}\n
coin_change_ii.js/* \u96f6\u94b1\u5151\u6362 II\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction coinChangeIIDP(coins, amt) {\n const n = coins.length;\n // \u521d\u59cb\u5316 dp \u8868\n const dp = Array.from({ length: n + 1 }, () =>\n Array.from({ length: amt + 1 }, () => 0)\n );\n // \u521d\u59cb\u5316\u9996\u5217\n for (let i = 0; i <= n; i++) {\n dp[i][0] = 1;\n }\n // \u72b6\u6001\u8f6c\u79fb\n for (let i = 1; i <= n; i++) {\n for (let a = 1; a <= amt; a++) {\n if (coins[i - 1] > a) {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[i][a] = dp[i - 1][a];\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]];\n }\n }\n }\n return dp[n][amt];\n}\n
coin_change_ii.ts/* \u96f6\u94b1\u5151\u6362 II\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction coinChangeIIDP(coins: Array<number>, amt: number): number {\n const n = coins.length;\n // \u521d\u59cb\u5316 dp \u8868\n const dp = Array.from({ length: n + 1 }, () =>\n Array.from({ length: amt + 1 }, () => 0)\n );\n // \u521d\u59cb\u5316\u9996\u5217\n for (let i = 0; i <= n; i++) {\n dp[i][0] = 1;\n }\n // \u72b6\u6001\u8f6c\u79fb\n for (let i = 1; i <= n; i++) {\n for (let a = 1; a <= amt; a++) {\n if (coins[i - 1] > a) {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[i][a] = dp[i - 1][a];\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]];\n }\n }\n }\n return dp[n][amt];\n}\n
coin_change_ii.dart/* \u96f6\u94b1\u5151\u6362 II\uff1a\u52a8\u6001\u89c4\u5212 */\nint coinChangeIIDP(List<int> coins, int amt) {\n int n = coins.length;\n // \u521d\u59cb\u5316 dp \u8868\n List<List<int>> dp = List.generate(n + 1, (index) => List.filled(amt + 1, 0));\n // \u521d\u59cb\u5316\u9996\u5217\n for (int i = 0; i <= n; i++) {\n dp[i][0] = 1;\n }\n // \u72b6\u6001\u8f6c\u79fb\n for (int i = 1; i <= n; i++) {\n for (int a = 1; a <= amt; a++) {\n if (coins[i - 1] > a) {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[i][a] = dp[i - 1][a];\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]];\n }\n }\n }\n return dp[n][amt];\n}\n
coin_change_ii.rs/* \u96f6\u94b1\u5151\u6362 II\uff1a\u52a8\u6001\u89c4\u5212 */\nfn coin_change_ii_dp(coins: &[i32], amt: usize) -> i32 {\n let n = coins.len();\n // \u521d\u59cb\u5316 dp \u8868\n let mut dp = vec![vec![0; amt + 1]; n + 1];\n // \u521d\u59cb\u5316\u9996\u5217\n for i in 0..= n {\n dp[i][0] = 1;\n }\n // \u72b6\u6001\u8f6c\u79fb\n for i in 1..=n {\n for a in 1..=amt {\n if coins[i - 1] > a as i32 {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[i][a] = dp[i - 1][a];\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1] as usize];\n }\n }\n }\n dp[n][amt]\n}\n
coin_change_ii.c/* \u96f6\u94b1\u5151\u6362 II\uff1a\u52a8\u6001\u89c4\u5212 */\nint coinChangeIIDP(int coins[], int amt, int coinsSize) {\n int n = coinsSize;\n // \u521d\u59cb\u5316 dp \u8868\n int **dp = malloc((n + 1) * sizeof(int *));\n for (int i = 0; i <= n; i++) {\n dp[i] = calloc(amt + 1, sizeof(int));\n }\n // \u521d\u59cb\u5316\u9996\u5217\n for (int i = 0; i <= n; i++) {\n dp[i][0] = 1;\n }\n // \u72b6\u6001\u8f6c\u79fb\n for (int i = 1; i <= n; i++) {\n for (int a = 1; a <= amt; a++) {\n if (coins[i - 1] > a) {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[i][a] = dp[i - 1][a];\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]];\n }\n }\n }\n int res = dp[n][amt];\n // \u91ca\u653e\u5185\u5b58\n for (int i = 0; i <= n; i++) {\n free(dp[i]);\n }\n free(dp);\n return res;\n}\n
coin_change_ii.zig// \u96f6\u94b1\u5151\u6362 II\uff1a\u52a8\u6001\u89c4\u5212\nfn coinChangeIIDP(comptime coins: []i32, comptime amt: usize) i32 {\n comptime var n = coins.len;\n // \u521d\u59cb\u5316 dp \u8868\n var dp = [_][amt + 1]i32{[_]i32{0} ** (amt + 1)} ** (n + 1);\n // \u521d\u59cb\u5316\u9996\u5217\n for (0..n + 1) |i| {\n dp[i][0] = 1;\n }\n // \u72b6\u6001\u8f6c\u79fb\n for (1..n + 1) |i| {\n for (1..amt + 1) |a| {\n if (coins[i - 1] > @as(i32, @intCast(a))) {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[i][a] = dp[i - 1][a];\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n dp[i][a] = dp[i - 1][a] + dp[i][a - @as(usize, @intCast(coins[i - 1]))];\n }\n }\n }\n return dp[n][amt];\n}\n
"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#3_2","title":"3. \u00a0 \u7a7a\u95f4\u4f18\u5316","text":"\u7a7a\u95f4\u4f18\u5316\u5904\u7406\u65b9\u5f0f\u76f8\u540c\uff0c\u5220\u9664\u786c\u5e01\u7ef4\u5ea6\u5373\u53ef\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig coin_change_ii.pydef coin_change_ii_dp_comp(coins: list[int], amt: int) -> int:\n \"\"\"\u96f6\u94b1\u5151\u6362 II\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\"\"\"\n n = len(coins)\n # \u521d\u59cb\u5316 dp \u8868\n dp = [0] * (amt + 1)\n dp[0] = 1\n # \u72b6\u6001\u8f6c\u79fb\n for i in range(1, n + 1):\n # \u6b63\u5e8f\u904d\u5386\n for a in range(1, amt + 1):\n if coins[i - 1] > a:\n # \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[a] = dp[a]\n else:\n # \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n dp[a] = dp[a] + dp[a - coins[i - 1]]\n return dp[amt]\n
coin_change_ii.cpp/* \u96f6\u94b1\u5151\u6362 II\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint coinChangeIIDPComp(vector<int> &coins, int amt) {\n int n = coins.size();\n // \u521d\u59cb\u5316 dp \u8868\n vector<int> dp(amt + 1, 0);\n dp[0] = 1;\n // \u72b6\u6001\u8f6c\u79fb\n for (int i = 1; i <= n; i++) {\n for (int a = 1; a <= amt; a++) {\n if (coins[i - 1] > a) {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[a] = dp[a];\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n dp[a] = dp[a] + dp[a - coins[i - 1]];\n }\n }\n }\n return dp[amt];\n}\n
coin_change_ii.java/* \u96f6\u94b1\u5151\u6362 II\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint coinChangeIIDPComp(int[] coins, int amt) {\n int n = coins.length;\n // \u521d\u59cb\u5316 dp \u8868\n int[] dp = new int[amt + 1];\n dp[0] = 1;\n // \u72b6\u6001\u8f6c\u79fb\n for (int i = 1; i <= n; i++) {\n for (int a = 1; a <= amt; a++) {\n if (coins[i - 1] > a) {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[a] = dp[a];\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n dp[a] = dp[a] + dp[a - coins[i - 1]];\n }\n }\n }\n return dp[amt];\n}\n
coin_change_ii.cs/* \u96f6\u94b1\u5151\u6362 II\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint CoinChangeIIDPComp(int[] coins, int amt) {\n int n = coins.Length;\n // \u521d\u59cb\u5316 dp \u8868\n int[] dp = new int[amt + 1];\n dp[0] = 1;\n // \u72b6\u6001\u8f6c\u79fb\n for (int i = 1; i <= n; i++) {\n for (int a = 1; a <= amt; a++) {\n if (coins[i - 1] > a) {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[a] = dp[a];\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n dp[a] = dp[a] + dp[a - coins[i - 1]];\n }\n }\n }\n return dp[amt];\n}\n
coin_change_ii.go/* \u96f6\u94b1\u5151\u6362 II\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc coinChangeIIDPComp(coins []int, amt int) int {\n n := len(coins)\n // \u521d\u59cb\u5316 dp \u8868\n dp := make([]int, amt+1)\n dp[0] = 1\n // \u72b6\u6001\u8f6c\u79fb\n for i := 1; i <= n; i++ {\n // \u5012\u5e8f\u904d\u5386\n for a := 1; a <= amt; a++ {\n if coins[i-1] > a {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[a] = dp[a]\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n dp[a] = dp[a] + dp[a-coins[i-1]]\n }\n }\n }\n return dp[amt]\n}\n
coin_change_ii.swift/* \u96f6\u94b1\u5151\u6362 II\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc coinChangeIIDPComp(coins: [Int], amt: Int) -> Int {\n let n = coins.count\n // \u521d\u59cb\u5316 dp \u8868\n var dp = Array(repeating: 0, count: amt + 1)\n dp[0] = 1\n // \u72b6\u6001\u8f6c\u79fb\n for i in stride(from: 1, through: n, by: 1) {\n for a in stride(from: 1, through: amt, by: 1) {\n if coins[i - 1] > a {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[a] = dp[a]\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n dp[a] = dp[a] + dp[a - coins[i - 1]]\n }\n }\n }\n return dp[amt]\n}\n
coin_change_ii.js/* \u96f6\u94b1\u5151\u6362 II\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction coinChangeIIDPComp(coins, amt) {\n const n = coins.length;\n // \u521d\u59cb\u5316 dp \u8868\n const dp = Array.from({ length: amt + 1 }, () => 0);\n dp[0] = 1;\n // \u72b6\u6001\u8f6c\u79fb\n for (let i = 1; i <= n; i++) {\n for (let a = 1; a <= amt; a++) {\n if (coins[i - 1] > a) {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[a] = dp[a];\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n dp[a] = dp[a] + dp[a - coins[i - 1]];\n }\n }\n }\n return dp[amt];\n}\n
coin_change_ii.ts/* \u96f6\u94b1\u5151\u6362 II\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction coinChangeIIDPComp(coins: Array<number>, amt: number): number {\n const n = coins.length;\n // \u521d\u59cb\u5316 dp \u8868\n const dp = Array.from({ length: amt + 1 }, () => 0);\n dp[0] = 1;\n // \u72b6\u6001\u8f6c\u79fb\n for (let i = 1; i <= n; i++) {\n for (let a = 1; a <= amt; a++) {\n if (coins[i - 1] > a) {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[a] = dp[a];\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n dp[a] = dp[a] + dp[a - coins[i - 1]];\n }\n }\n }\n return dp[amt];\n}\n
coin_change_ii.dart/* \u96f6\u94b1\u5151\u6362 II\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint coinChangeIIDPComp(List<int> coins, int amt) {\n int n = coins.length;\n // \u521d\u59cb\u5316 dp \u8868\n List<int> dp = List.filled(amt + 1, 0);\n dp[0] = 1;\n // \u72b6\u6001\u8f6c\u79fb\n for (int i = 1; i <= n; i++) {\n for (int a = 1; a <= amt; a++) {\n if (coins[i - 1] > a) {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[a] = dp[a];\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n dp[a] = dp[a] + dp[a - coins[i - 1]];\n }\n }\n }\n return dp[amt];\n}\n
coin_change_ii.rs/* \u96f6\u94b1\u5151\u6362 II\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfn coin_change_ii_dp_comp(coins: &[i32], amt: usize) -> i32 {\n let n = coins.len();\n // \u521d\u59cb\u5316 dp \u8868\n let mut dp = vec![0; amt + 1];\n dp[0] = 1;\n // \u72b6\u6001\u8f6c\u79fb\n for i in 1..=n {\n for a in 1..=amt {\n if coins[i - 1] > a as i32 {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[a] = dp[a];\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n dp[a] = dp[a] + dp[a - coins[i - 1] as usize];\n }\n }\n }\n dp[amt]\n}\n
coin_change_ii.c/* \u96f6\u94b1\u5151\u6362 II\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint coinChangeIIDPComp(int coins[], int amt, int coinsSize) {\n int n = coinsSize;\n // \u521d\u59cb\u5316 dp \u8868\n int *dp = calloc(amt + 1, sizeof(int));\n dp[0] = 1;\n // \u72b6\u6001\u8f6c\u79fb\n for (int i = 1; i <= n; i++) {\n for (int a = 1; a <= amt; a++) {\n if (coins[i - 1] > a) {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[a] = dp[a];\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\n dp[a] = dp[a] + dp[a - coins[i - 1]];\n }\n }\n }\n int res = dp[amt];\n // \u91ca\u653e\u5185\u5b58\n free(dp);\n return res;\n}\n
coin_change_ii.zig// \u96f6\u94b1\u5151\u6362 II\uff1a\u7a7a\u95f4\u4f18\u5316\u540e\u7684\u52a8\u6001\u89c4\u5212\nfn coinChangeIIDPComp(comptime coins: []i32, comptime amt: usize) i32 {\n comptime var n = coins.len;\n // \u521d\u59cb\u5316 dp \u8868\n var dp = [_]i32{0} ** (amt + 1);\n dp[0] = 1;\n // \u72b6\u6001\u8f6c\u79fb\n for (1..n + 1) |i| {\n for (1..amt + 1) |a| {\n if (coins[i - 1] > @as(i32, @intCast(a))) {\n // \u82e5\u8d85\u8fc7\u76ee\u6807\u91d1\u989d\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\n dp[a] = dp[a];\n } else {\n // \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\n dp[a] = dp[a] + dp[a - @as(usize, @intCast(coins[i - 1]))];\n }\n }\n }\n return dp[amt];\n}\n
"},{"location":"chapter_graph/","title":"\u7b2c 9 \u7ae0 \u00a0 \u56fe","text":"Abstract
\u5728\u751f\u547d\u65c5\u9014\u4e2d\uff0c\u6211\u4eec\u5c31\u50cf\u662f\u4e00\u4e2a\u4e2a\u8282\u70b9\uff0c\u88ab\u65e0\u6570\u770b\u4e0d\u89c1\u7684\u8fb9\u76f8\u8fde\u3002
\u6bcf\u4e00\u6b21\u7684\u76f8\u8bc6\u4e0e\u76f8\u79bb\uff0c\u90fd\u5728\u8fd9\u5f20\u5de8\u5927\u7684\u7f51\u7edc\u56fe\u4e2d\u7559\u4e0b\u72ec\u7279\u7684\u5370\u8bb0\u3002
"},{"location":"chapter_graph/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"\u300c\u56fe graph\u300d\u662f\u4e00\u79cd\u975e\u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff0c\u7531\u300c\u9876\u70b9 vertex\u300d\u548c\u300c\u8fb9 edge\u300d\u7ec4\u6210\u3002\u6211\u4eec\u53ef\u4ee5\u5c06\u56fe \\(G\\) \u62bd\u8c61\u5730\u8868\u793a\u4e3a\u4e00\u7ec4\u9876\u70b9 \\(V\\) \u548c\u4e00\u7ec4\u8fb9 \\(E\\) \u7684\u96c6\u5408\u3002\u4ee5\u4e0b\u793a\u4f8b\u5c55\u793a\u4e86\u4e00\u4e2a\u5305\u542b 5 \u4e2a\u9876\u70b9\u548c 7 \u6761\u8fb9\u7684\u56fe\u3002
\\[ \\begin{aligned} V & = \\{ 1, 2, 3, 4, 5 \\} \\newline E & = \\{ (1,2), (1,3), (1,5), (2,3), (2,4), (2,5), (4,5) \\} \\newline G & = \\{ V, E \\} \\newline \\end{aligned} \\]\u5982\u679c\u5c06\u9876\u70b9\u770b\u4f5c\u8282\u70b9\uff0c\u5c06\u8fb9\u770b\u4f5c\u8fde\u63a5\u5404\u4e2a\u8282\u70b9\u7684\u5f15\u7528\uff08\u6307\u9488\uff09\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5c06\u56fe\u770b\u4f5c\u4e00\u79cd\u4ece\u94fe\u8868\u62d3\u5c55\u800c\u6765\u7684\u6570\u636e\u7ed3\u6784\u3002\u5982\u56fe 9-1 \u6240\u793a\uff0c\u76f8\u8f83\u4e8e\u7ebf\u6027\u5173\u7cfb\uff08\u94fe\u8868\uff09\u548c\u5206\u6cbb\u5173\u7cfb\uff08\u6811\uff09\uff0c\u7f51\u7edc\u5173\u7cfb\uff08\u56fe\uff09\u7684\u81ea\u7531\u5ea6\u66f4\u9ad8\uff0c\u56e0\u800c\u66f4\u4e3a\u590d\u6742\u3002
\u56fe 9-1 \u00a0 \u94fe\u8868\u3001\u6811\u3001\u56fe\u4e4b\u95f4\u7684\u5173\u7cfb
"},{"location":"chapter_graph/graph/#911","title":"9.1.1 \u00a0 \u56fe\u5e38\u89c1\u7c7b\u578b\u4e0e\u672f\u8bed","text":"\u6839\u636e\u8fb9\u662f\u5426\u5177\u6709\u65b9\u5411\uff0c\u53ef\u5206\u4e3a\u300c\u65e0\u5411\u56fe undirected graph\u300d\u548c\u300c\u6709\u5411\u56fe directed graph\u300d\uff0c\u5982\u56fe 9-2 \u6240\u793a\u3002
\u56fe 9-2 \u00a0 \u6709\u5411\u56fe\u4e0e\u65e0\u5411\u56fe
\u6839\u636e\u6240\u6709\u9876\u70b9\u662f\u5426\u8fde\u901a\uff0c\u53ef\u5206\u4e3a\u300c\u8fde\u901a\u56fe connected graph\u300d\u548c\u300c\u975e\u8fde\u901a\u56fe disconnected graph\u300d\uff0c\u5982\u56fe 9-3 \u6240\u793a\u3002
\u56fe 9-3 \u00a0 \u8fde\u901a\u56fe\u4e0e\u975e\u8fde\u901a\u56fe
\u6211\u4eec\u8fd8\u53ef\u4ee5\u4e3a\u8fb9\u6dfb\u52a0\u201c\u6743\u91cd\u201d\u53d8\u91cf\uff0c\u4ece\u800c\u5f97\u5230\u5982\u56fe 9-4 \u6240\u793a\u7684\u300c\u6709\u6743\u56fe weighted graph\u300d\u3002\u4f8b\u5982\u5728\u201c\u738b\u8005\u8363\u8000\u201d\u7b49\u624b\u6e38\u4e2d\uff0c\u7cfb\u7edf\u4f1a\u6839\u636e\u5171\u540c\u6e38\u620f\u65f6\u95f4\u6765\u8ba1\u7b97\u73a9\u5bb6\u4e4b\u95f4\u7684\u201c\u4eb2\u5bc6\u5ea6\u201d\uff0c\u8fd9\u79cd\u4eb2\u5bc6\u5ea6\u7f51\u7edc\u5c31\u53ef\u4ee5\u7528\u6709\u6743\u56fe\u6765\u8868\u793a\u3002
\u56fe 9-4 \u00a0 \u6709\u6743\u56fe\u4e0e\u65e0\u6743\u56fe
\u56fe\u6570\u636e\u7ed3\u6784\u5305\u542b\u4ee5\u4e0b\u5e38\u7528\u672f\u8bed\u3002
\u56fe\u7684\u5e38\u7528\u8868\u793a\u65b9\u5f0f\u5305\u62ec\u201c\u90bb\u63a5\u77e9\u9635\u201d\u548c\u201c\u90bb\u63a5\u8868\u201d\u3002\u4ee5\u4e0b\u4f7f\u7528\u65e0\u5411\u56fe\u8fdb\u884c\u4e3e\u4f8b\u3002
"},{"location":"chapter_graph/graph/#1","title":"1. \u00a0 \u90bb\u63a5\u77e9\u9635","text":"\u8bbe\u56fe\u7684\u9876\u70b9\u6570\u91cf\u4e3a \\(n\\) \uff0c\u300c\u90bb\u63a5\u77e9\u9635 adjacency matrix\u300d\u4f7f\u7528\u4e00\u4e2a \\(n \\times n\\) \u5927\u5c0f\u7684\u77e9\u9635\u6765\u8868\u793a\u56fe\uff0c\u6bcf\u4e00\u884c\uff08\u5217\uff09\u4ee3\u8868\u4e00\u4e2a\u9876\u70b9\uff0c\u77e9\u9635\u5143\u7d20\u4ee3\u8868\u8fb9\uff0c\u7528 \\(1\\) \u6216 \\(0\\) \u8868\u793a\u4e24\u4e2a\u9876\u70b9\u4e4b\u95f4\u662f\u5426\u5b58\u5728\u8fb9\u3002
\u5982\u56fe 9-5 \u6240\u793a\uff0c\u8bbe\u90bb\u63a5\u77e9\u9635\u4e3a \\(M\\)\u3001\u9876\u70b9\u5217\u8868\u4e3a \\(V\\) \uff0c\u90a3\u4e48\u77e9\u9635\u5143\u7d20 \\(M[i, j] = 1\\) \u8868\u793a\u9876\u70b9 \\(V[i]\\) \u5230\u9876\u70b9 \\(V[j]\\) \u4e4b\u95f4\u5b58\u5728\u8fb9\uff0c\u53cd\u4e4b \\(M[i, j] = 0\\) \u8868\u793a\u4e24\u9876\u70b9\u4e4b\u95f4\u65e0\u8fb9\u3002
\u56fe 9-5 \u00a0 \u56fe\u7684\u90bb\u63a5\u77e9\u9635\u8868\u793a
\u90bb\u63a5\u77e9\u9635\u5177\u6709\u4ee5\u4e0b\u7279\u6027\u3002
\u4f7f\u7528\u90bb\u63a5\u77e9\u9635\u8868\u793a\u56fe\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u77e9\u9635\u5143\u7d20\u4ee5\u83b7\u53d6\u8fb9\uff0c\u56e0\u6b64\u589e\u5220\u67e5\u6539\u64cd\u4f5c\u7684\u6548\u7387\u5f88\u9ad8\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u5747\u4e3a \\(O(1)\\) \u3002\u7136\u800c\uff0c\u77e9\u9635\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\) \uff0c\u5185\u5b58\u5360\u7528\u8f83\u591a\u3002
"},{"location":"chapter_graph/graph/#2","title":"2. \u00a0 \u90bb\u63a5\u8868","text":"\u300c\u90bb\u63a5\u8868 adjacency list\u300d\u4f7f\u7528 \\(n\\) \u4e2a\u94fe\u8868\u6765\u8868\u793a\u56fe\uff0c\u94fe\u8868\u8282\u70b9\u8868\u793a\u9876\u70b9\u3002\u7b2c \\(i\\) \u4e2a\u94fe\u8868\u5bf9\u5e94\u9876\u70b9 \\(i\\) \uff0c\u5176\u4e2d\u5b58\u50a8\u4e86\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\uff08\u4e0e\u8be5\u9876\u70b9\u76f8\u8fde\u7684\u9876\u70b9\uff09\u3002\u56fe 9-6 \u5c55\u793a\u4e86\u4e00\u4e2a\u4f7f\u7528\u90bb\u63a5\u8868\u5b58\u50a8\u7684\u56fe\u7684\u793a\u4f8b\u3002
\u56fe 9-6 \u00a0 \u56fe\u7684\u90bb\u63a5\u8868\u8868\u793a
\u90bb\u63a5\u8868\u4ec5\u5b58\u50a8\u5b9e\u9645\u5b58\u5728\u7684\u8fb9\uff0c\u800c\u8fb9\u7684\u603b\u6570\u901a\u5e38\u8fdc\u5c0f\u4e8e \\(n^2\\) \uff0c\u56e0\u6b64\u5b83\u66f4\u52a0\u8282\u7701\u7a7a\u95f4\u3002\u7136\u800c\uff0c\u5728\u90bb\u63a5\u8868\u4e2d\u9700\u8981\u901a\u8fc7\u904d\u5386\u94fe\u8868\u6765\u67e5\u627e\u8fb9\uff0c\u56e0\u6b64\u5176\u65f6\u95f4\u6548\u7387\u4e0d\u5982\u90bb\u63a5\u77e9\u9635\u3002
\u89c2\u5bdf\u56fe 9-6 \uff0c\u90bb\u63a5\u8868\u7ed3\u6784\u4e0e\u54c8\u5e0c\u8868\u4e2d\u7684\u201c\u94fe\u5f0f\u5730\u5740\u201d\u975e\u5e38\u76f8\u4f3c\uff0c\u56e0\u6b64\u6211\u4eec\u4e5f\u53ef\u4ee5\u91c7\u7528\u7c7b\u4f3c\u7684\u65b9\u6cd5\u6765\u4f18\u5316\u6548\u7387\u3002\u6bd4\u5982\u5f53\u94fe\u8868\u8f83\u957f\u65f6\uff0c\u53ef\u4ee5\u5c06\u94fe\u8868\u8f6c\u5316\u4e3a AVL \u6811\u6216\u7ea2\u9ed1\u6811\uff0c\u4ece\u800c\u5c06\u65f6\u95f4\u6548\u7387\u4ece \\(O(n)\\) \u4f18\u5316\u81f3 \\(O(\\log n)\\) \uff1b\u8fd8\u53ef\u4ee5\u628a\u94fe\u8868\u8f6c\u6362\u4e3a\u54c8\u5e0c\u8868\uff0c\u4ece\u800c\u5c06\u65f6\u95f4\u590d\u6742\u5ea6\u964d\u81f3 \\(O(1)\\) \u3002
"},{"location":"chapter_graph/graph/#913","title":"9.1.3 \u00a0 \u56fe\u5e38\u89c1\u5e94\u7528","text":"\u5982\u8868 9-1 \u6240\u793a\uff0c\u8bb8\u591a\u73b0\u5b9e\u7cfb\u7edf\u53ef\u4ee5\u7528\u56fe\u6765\u5efa\u6a21\uff0c\u76f8\u5e94\u7684\u95ee\u9898\u4e5f\u53ef\u4ee5\u7ea6\u5316\u4e3a\u56fe\u8ba1\u7b97\u95ee\u9898\u3002
\u8868 9-1 \u00a0 \u73b0\u5b9e\u751f\u6d3b\u4e2d\u5e38\u89c1\u7684\u56fe
\u9876\u70b9 \u8fb9 \u56fe\u8ba1\u7b97\u95ee\u9898 \u793e\u4ea4\u7f51\u7edc \u7528\u6237 \u597d\u53cb\u5173\u7cfb \u6f5c\u5728\u597d\u53cb\u63a8\u8350 \u5730\u94c1\u7ebf\u8def \u7ad9\u70b9 \u7ad9\u70b9\u95f4\u7684\u8fde\u901a\u6027 \u6700\u77ed\u8def\u7ebf\u63a8\u8350 \u592a\u9633\u7cfb \u661f\u4f53 \u661f\u4f53\u95f4\u7684\u4e07\u6709\u5f15\u529b\u4f5c\u7528 \u884c\u661f\u8f68\u9053\u8ba1\u7b97"},{"location":"chapter_graph/graph_operations/","title":"9.2 \u00a0 \u56fe\u57fa\u7840\u64cd\u4f5c","text":"\u56fe\u7684\u57fa\u7840\u64cd\u4f5c\u53ef\u5206\u4e3a\u5bf9\u201c\u8fb9\u201d\u7684\u64cd\u4f5c\u548c\u5bf9\u201c\u9876\u70b9\u201d\u7684\u64cd\u4f5c\u3002\u5728\u201c\u90bb\u63a5\u77e9\u9635\u201d\u548c\u201c\u90bb\u63a5\u8868\u201d\u4e24\u79cd\u8868\u793a\u65b9\u6cd5\u4e0b\uff0c\u5b9e\u73b0\u65b9\u5f0f\u6709\u6240\u4e0d\u540c\u3002
"},{"location":"chapter_graph/graph_operations/#921","title":"9.2.1 \u00a0 \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u7684\u5b9e\u73b0","text":"\u7ed9\u5b9a\u4e00\u4e2a\u9876\u70b9\u6570\u91cf\u4e3a \\(n\\) \u7684\u65e0\u5411\u56fe\uff0c\u5219\u5404\u79cd\u64cd\u4f5c\u7684\u5b9e\u73b0\u65b9\u5f0f\u5982\u56fe 9-7 \u6240\u793a\u3002
vertices
\uff0c\u4f7f\u7528 \\(O(n)\\) \u65f6\u95f4\uff1b\u521d\u59cb\u5316 \\(n \\times n\\) \u5927\u5c0f\u7684\u90bb\u63a5\u77e9\u9635 adjMat
\uff0c\u4f7f\u7528 \\(O(n^2)\\) \u65f6\u95f4\u3002\u56fe 9-7 \u00a0 \u90bb\u63a5\u77e9\u9635\u7684\u521d\u59cb\u5316\u3001\u589e\u5220\u8fb9\u3001\u589e\u5220\u9876\u70b9
\u4ee5\u4e0b\u662f\u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u8868\u793a\u56fe\u7684\u5b9e\u73b0\u4ee3\u7801\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig graph_adjacency_matrix.pyclass GraphAdjMat:\n \"\"\"\u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b\"\"\"\n\n def __init__(self, vertices: list[int], edges: list[list[int]]):\n \"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n # \u9876\u70b9\u5217\u8868\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\n self.vertices: list[int] = []\n # \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n self.adj_mat: list[list[int]] = []\n # \u6dfb\u52a0\u9876\u70b9\n for val in vertices:\n self.add_vertex(val)\n # \u6dfb\u52a0\u8fb9\n # \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n for e in edges:\n self.add_edge(e[0], e[1])\n\n def size(self) -> int:\n \"\"\"\u83b7\u53d6\u9876\u70b9\u6570\u91cf\"\"\"\n return len(self.vertices)\n\n def add_vertex(self, val: int):\n \"\"\"\u6dfb\u52a0\u9876\u70b9\"\"\"\n n = self.size()\n # \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\n self.vertices.append(val)\n # \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\n new_row = [0] * n\n self.adj_mat.append(new_row)\n # \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\n for row in self.adj_mat:\n row.append(0)\n\n def remove_vertex(self, index: int):\n \"\"\"\u5220\u9664\u9876\u70b9\"\"\"\n if index >= self.size():\n raise IndexError()\n # \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\n self.vertices.pop(index)\n # \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\n self.adj_mat.pop(index)\n # \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\n for row in self.adj_mat:\n row.pop(index)\n\n def add_edge(self, i: int, j: int):\n \"\"\"\u6dfb\u52a0\u8fb9\"\"\"\n # \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n # \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n if i < 0 or j < 0 or i >= self.size() or j >= self.size() or i == j:\n raise IndexError()\n # \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u5173\u4e8e\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\n self.adj_mat[i][j] = 1\n self.adj_mat[j][i] = 1\n\n def remove_edge(self, i: int, j: int):\n \"\"\"\u5220\u9664\u8fb9\"\"\"\n # \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n # \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n if i < 0 or j < 0 or i >= self.size() or j >= self.size() or i == j:\n raise IndexError()\n self.adj_mat[i][j] = 0\n self.adj_mat[j][i] = 0\n\n def print(self):\n \"\"\"\u6253\u5370\u90bb\u63a5\u77e9\u9635\"\"\"\n print(\"\u9876\u70b9\u5217\u8868 =\", self.vertices)\n print(\"\u90bb\u63a5\u77e9\u9635 =\")\n print_matrix(self.adj_mat)\n
graph_adjacency_matrix.cpp/* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjMat {\n vector<int> vertices; // \u9876\u70b9\u5217\u8868\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\n vector<vector<int>> adjMat; // \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n\n public:\n /* \u6784\u9020\u65b9\u6cd5 */\n GraphAdjMat(const vector<int> &vertices, const vector<vector<int>> &edges) {\n // \u6dfb\u52a0\u9876\u70b9\n for (int val : vertices) {\n addVertex(val);\n }\n // \u6dfb\u52a0\u8fb9\n // \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n for (const vector<int> &edge : edges) {\n addEdge(edge[0], edge[1]);\n }\n }\n\n /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n int size() const {\n return vertices.size();\n }\n\n /* \u6dfb\u52a0\u9876\u70b9 */\n void addVertex(int val) {\n int n = size();\n // \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\n vertices.push_back(val);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\n adjMat.emplace_back(vector<int>(n, 0));\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\n for (vector<int> &row : adjMat) {\n row.push_back(0);\n }\n }\n\n /* \u5220\u9664\u9876\u70b9 */\n void removeVertex(int index) {\n if (index >= size()) {\n throw out_of_range(\"\u9876\u70b9\u4e0d\u5b58\u5728\");\n }\n // \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\n vertices.erase(vertices.begin() + index);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\n adjMat.erase(adjMat.begin() + index);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\n for (vector<int> &row : adjMat) {\n row.erase(row.begin() + index);\n }\n }\n\n /* \u6dfb\u52a0\u8fb9 */\n // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n void addEdge(int i, int j) {\n // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n if (i < 0 || j < 0 || i >= size() || j >= size() || i == j) {\n throw out_of_range(\"\u9876\u70b9\u4e0d\u5b58\u5728\");\n }\n // \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u5173\u4e8e\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\n adjMat[i][j] = 1;\n adjMat[j][i] = 1;\n }\n\n /* \u5220\u9664\u8fb9 */\n // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n void removeEdge(int i, int j) {\n // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n if (i < 0 || j < 0 || i >= size() || j >= size() || i == j) {\n throw out_of_range(\"\u9876\u70b9\u4e0d\u5b58\u5728\");\n }\n adjMat[i][j] = 0;\n adjMat[j][i] = 0;\n }\n\n /* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\n void print() {\n cout << \"\u9876\u70b9\u5217\u8868 = \";\n printVector(vertices);\n cout << \"\u90bb\u63a5\u77e9\u9635 =\" << endl;\n printVectorMatrix(adjMat);\n }\n};\n
graph_adjacency_matrix.java/* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjMat {\n List<Integer> vertices; // \u9876\u70b9\u5217\u8868\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\n List<List<Integer>> adjMat; // \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n\n /* \u6784\u9020\u65b9\u6cd5 */\n public GraphAdjMat(int[] vertices, int[][] edges) {\n this.vertices = new ArrayList<>();\n this.adjMat = new ArrayList<>();\n // \u6dfb\u52a0\u9876\u70b9\n for (int val : vertices) {\n addVertex(val);\n }\n // \u6dfb\u52a0\u8fb9\n // \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n for (int[] e : edges) {\n addEdge(e[0], e[1]);\n }\n }\n\n /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n public int size() {\n return vertices.size();\n }\n\n /* \u6dfb\u52a0\u9876\u70b9 */\n public void addVertex(int val) {\n int n = size();\n // \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\n vertices.add(val);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\n List<Integer> newRow = new ArrayList<>(n);\n for (int j = 0; j < n; j++) {\n newRow.add(0);\n }\n adjMat.add(newRow);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\n for (List<Integer> row : adjMat) {\n row.add(0);\n }\n }\n\n /* \u5220\u9664\u9876\u70b9 */\n public void removeVertex(int index) {\n if (index >= size())\n throw new IndexOutOfBoundsException();\n // \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\n vertices.remove(index);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\n adjMat.remove(index);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\n for (List<Integer> row : adjMat) {\n row.remove(index);\n }\n }\n\n /* \u6dfb\u52a0\u8fb9 */\n // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n public void addEdge(int i, int j) {\n // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n if (i < 0 || j < 0 || i >= size() || j >= size() || i == j)\n throw new IndexOutOfBoundsException();\n // \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u5173\u4e8e\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\n adjMat.get(i).set(j, 1);\n adjMat.get(j).set(i, 1);\n }\n\n /* \u5220\u9664\u8fb9 */\n // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n public void removeEdge(int i, int j) {\n // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n if (i < 0 || j < 0 || i >= size() || j >= size() || i == j)\n throw new IndexOutOfBoundsException();\n adjMat.get(i).set(j, 0);\n adjMat.get(j).set(i, 0);\n }\n\n /* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\n public void print() {\n System.out.print(\"\u9876\u70b9\u5217\u8868 = \");\n System.out.println(vertices);\n System.out.println(\"\u90bb\u63a5\u77e9\u9635 =\");\n PrintUtil.printMatrix(adjMat);\n }\n}\n
graph_adjacency_matrix.cs/* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjMat {\n List<int> vertices; // \u9876\u70b9\u5217\u8868\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\n List<List<int>> adjMat; // \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n\n /* \u6784\u9020\u51fd\u6570 */\n public GraphAdjMat(int[] vertices, int[][] edges) {\n this.vertices = [];\n this.adjMat = [];\n // \u6dfb\u52a0\u9876\u70b9\n foreach (int val in vertices) {\n AddVertex(val);\n }\n // \u6dfb\u52a0\u8fb9\n // \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n foreach (int[] e in edges) {\n AddEdge(e[0], e[1]);\n }\n }\n\n /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n int Size() {\n return vertices.Count;\n }\n\n /* \u6dfb\u52a0\u9876\u70b9 */\n public void AddVertex(int val) {\n int n = Size();\n // \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\n vertices.Add(val);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\n List<int> newRow = new(n);\n for (int j = 0; j < n; j++) {\n newRow.Add(0);\n }\n adjMat.Add(newRow);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\n foreach (List<int> row in adjMat) {\n row.Add(0);\n }\n }\n\n /* \u5220\u9664\u9876\u70b9 */\n public void RemoveVertex(int index) {\n if (index >= Size())\n throw new IndexOutOfRangeException();\n // \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\n vertices.RemoveAt(index);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\n adjMat.RemoveAt(index);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\n foreach (List<int> row in adjMat) {\n row.RemoveAt(index);\n }\n }\n\n /* \u6dfb\u52a0\u8fb9 */\n // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n public void AddEdge(int i, int j) {\n // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n if (i < 0 || j < 0 || i >= Size() || j >= Size() || i == j)\n throw new IndexOutOfRangeException();\n // \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u5173\u4e8e\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\n adjMat[i][j] = 1;\n adjMat[j][i] = 1;\n }\n\n /* \u5220\u9664\u8fb9 */\n // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n public void RemoveEdge(int i, int j) {\n // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n if (i < 0 || j < 0 || i >= Size() || j >= Size() || i == j)\n throw new IndexOutOfRangeException();\n adjMat[i][j] = 0;\n adjMat[j][i] = 0;\n }\n\n /* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\n public void Print() {\n Console.Write(\"\u9876\u70b9\u5217\u8868 = \");\n PrintUtil.PrintList(vertices);\n Console.WriteLine(\"\u90bb\u63a5\u77e9\u9635 =\");\n PrintUtil.PrintMatrix(adjMat);\n }\n}\n
graph_adjacency_matrix.go/* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\ntype graphAdjMat struct {\n // \u9876\u70b9\u5217\u8868\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\n vertices []int\n // \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n adjMat [][]int\n}\n\n/* \u6784\u9020\u51fd\u6570 */\nfunc newGraphAdjMat(vertices []int, edges [][]int) *graphAdjMat {\n // \u6dfb\u52a0\u9876\u70b9\n n := len(vertices)\n adjMat := make([][]int, n)\n for i := range adjMat {\n adjMat[i] = make([]int, n)\n }\n // \u521d\u59cb\u5316\u56fe\n g := &graphAdjMat{\n vertices: vertices,\n adjMat: adjMat,\n }\n // \u6dfb\u52a0\u8fb9\n // \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n for i := range edges {\n g.addEdge(edges[i][0], edges[i][1])\n }\n return g\n}\n\n/* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\nfunc (g *graphAdjMat) size() int {\n return len(g.vertices)\n}\n\n/* \u6dfb\u52a0\u9876\u70b9 */\nfunc (g *graphAdjMat) addVertex(val int) {\n n := g.size()\n // \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\n g.vertices = append(g.vertices, val)\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\n newRow := make([]int, n)\n g.adjMat = append(g.adjMat, newRow)\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\n for i := range g.adjMat {\n g.adjMat[i] = append(g.adjMat[i], 0)\n }\n}\n\n/* \u5220\u9664\u9876\u70b9 */\nfunc (g *graphAdjMat) removeVertex(index int) {\n if index >= g.size() {\n return\n }\n // \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\n g.vertices = append(g.vertices[:index], g.vertices[index+1:]...)\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\n g.adjMat = append(g.adjMat[:index], g.adjMat[index+1:]...)\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\n for i := range g.adjMat {\n g.adjMat[i] = append(g.adjMat[i][:index], g.adjMat[i][index+1:]...)\n }\n}\n\n/* \u6dfb\u52a0\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nfunc (g *graphAdjMat) addEdge(i, j int) {\n // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n if i < 0 || j < 0 || i >= g.size() || j >= g.size() || i == j {\n fmt.Errorf(\"%s\", \"Index Out Of Bounds Exception\")\n }\n // \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u5173\u4e8e\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\n g.adjMat[i][j] = 1\n g.adjMat[j][i] = 1\n}\n\n/* \u5220\u9664\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nfunc (g *graphAdjMat) removeEdge(i, j int) {\n // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n if i < 0 || j < 0 || i >= g.size() || j >= g.size() || i == j {\n fmt.Errorf(\"%s\", \"Index Out Of Bounds Exception\")\n }\n g.adjMat[i][j] = 0\n g.adjMat[j][i] = 0\n}\n\n/* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\nfunc (g *graphAdjMat) print() {\n fmt.Printf(\"\\t\u9876\u70b9\u5217\u8868 = %v\\n\", g.vertices)\n fmt.Printf(\"\\t\u90bb\u63a5\u77e9\u9635 = \\n\")\n for i := range g.adjMat {\n fmt.Printf(\"\\t\\t\\t%v\\n\", g.adjMat[i])\n }\n}\n
graph_adjacency_matrix.swift/* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjMat {\n private var vertices: [Int] // \u9876\u70b9\u5217\u8868\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\n private var adjMat: [[Int]] // \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n\n /* \u6784\u9020\u65b9\u6cd5 */\n init(vertices: [Int], edges: [[Int]]) {\n self.vertices = []\n adjMat = []\n // \u6dfb\u52a0\u9876\u70b9\n for val in vertices {\n addVertex(val: val)\n }\n // \u6dfb\u52a0\u8fb9\n // \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n for e in edges {\n addEdge(i: e[0], j: e[1])\n }\n }\n\n /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n func size() -> Int {\n vertices.count\n }\n\n /* \u6dfb\u52a0\u9876\u70b9 */\n func addVertex(val: Int) {\n let n = size()\n // \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\n vertices.append(val)\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\n let newRow = Array(repeating: 0, count: n)\n adjMat.append(newRow)\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\n for i in adjMat.indices {\n adjMat[i].append(0)\n }\n }\n\n /* \u5220\u9664\u9876\u70b9 */\n func removeVertex(index: Int) {\n if index >= size() {\n fatalError(\"\u8d8a\u754c\")\n }\n // \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\n vertices.remove(at: index)\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\n adjMat.remove(at: index)\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\n for i in adjMat.indices {\n adjMat[i].remove(at: index)\n }\n }\n\n /* \u6dfb\u52a0\u8fb9 */\n // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n func addEdge(i: Int, j: Int) {\n // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n if i < 0 || j < 0 || i >= size() || j >= size() || i == j {\n fatalError(\"\u8d8a\u754c\")\n }\n // \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u5173\u4e8e\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\n adjMat[i][j] = 1\n adjMat[j][i] = 1\n }\n\n /* \u5220\u9664\u8fb9 */\n // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n func removeEdge(i: Int, j: Int) {\n // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n if i < 0 || j < 0 || i >= size() || j >= size() || i == j {\n fatalError(\"\u8d8a\u754c\")\n }\n adjMat[i][j] = 0\n adjMat[j][i] = 0\n }\n\n /* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\n func print() {\n Swift.print(\"\u9876\u70b9\u5217\u8868 = \", terminator: \"\")\n Swift.print(vertices)\n Swift.print(\"\u90bb\u63a5\u77e9\u9635 =\")\n PrintUtil.printMatrix(matrix: adjMat)\n }\n}\n
graph_adjacency_matrix.js/* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjMat {\n vertices; // \u9876\u70b9\u5217\u8868\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\n adjMat; // \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n\n /* \u6784\u9020\u51fd\u6570 */\n constructor(vertices, edges) {\n this.vertices = [];\n this.adjMat = [];\n // \u6dfb\u52a0\u9876\u70b9\n for (const val of vertices) {\n this.addVertex(val);\n }\n // \u6dfb\u52a0\u8fb9\n // \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n for (const e of edges) {\n this.addEdge(e[0], e[1]);\n }\n }\n\n /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n size() {\n return this.vertices.length;\n }\n\n /* \u6dfb\u52a0\u9876\u70b9 */\n addVertex(val) {\n const n = this.size();\n // \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\n this.vertices.push(val);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\n const newRow = [];\n for (let j = 0; j < n; j++) {\n newRow.push(0);\n }\n this.adjMat.push(newRow);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\n for (const row of this.adjMat) {\n row.push(0);\n }\n }\n\n /* \u5220\u9664\u9876\u70b9 */\n removeVertex(index) {\n if (index >= this.size()) {\n throw new RangeError('Index Out Of Bounds Exception');\n }\n // \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\n this.vertices.splice(index, 1);\n\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\n this.adjMat.splice(index, 1);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\n for (const row of this.adjMat) {\n row.splice(index, 1);\n }\n }\n\n /* \u6dfb\u52a0\u8fb9 */\n // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n addEdge(i, j) {\n // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n if (i < 0 || j < 0 || i >= this.size() || j >= this.size() || i === j) {\n throw new RangeError('Index Out Of Bounds Exception');\n }\n // \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u5173\u4e8e\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) === (j, i)\n this.adjMat[i][j] = 1;\n this.adjMat[j][i] = 1;\n }\n\n /* \u5220\u9664\u8fb9 */\n // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n removeEdge(i, j) {\n // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n if (i < 0 || j < 0 || i >= this.size() || j >= this.size() || i === j) {\n throw new RangeError('Index Out Of Bounds Exception');\n }\n this.adjMat[i][j] = 0;\n this.adjMat[j][i] = 0;\n }\n\n /* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\n print() {\n console.log('\u9876\u70b9\u5217\u8868 = ', this.vertices);\n console.log('\u90bb\u63a5\u77e9\u9635 =', this.adjMat);\n }\n}\n
graph_adjacency_matrix.ts/* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjMat {\n vertices: number[]; // \u9876\u70b9\u5217\u8868\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\n adjMat: number[][]; // \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n\n /* \u6784\u9020\u51fd\u6570 */\n constructor(vertices: number[], edges: number[][]) {\n this.vertices = [];\n this.adjMat = [];\n // \u6dfb\u52a0\u9876\u70b9\n for (const val of vertices) {\n this.addVertex(val);\n }\n // \u6dfb\u52a0\u8fb9\n // \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n for (const e of edges) {\n this.addEdge(e[0], e[1]);\n }\n }\n\n /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n size(): number {\n return this.vertices.length;\n }\n\n /* \u6dfb\u52a0\u9876\u70b9 */\n addVertex(val: number): void {\n const n: number = this.size();\n // \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\n this.vertices.push(val);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\n const newRow: number[] = [];\n for (let j: number = 0; j < n; j++) {\n newRow.push(0);\n }\n this.adjMat.push(newRow);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\n for (const row of this.adjMat) {\n row.push(0);\n }\n }\n\n /* \u5220\u9664\u9876\u70b9 */\n removeVertex(index: number): void {\n if (index >= this.size()) {\n throw new RangeError('Index Out Of Bounds Exception');\n }\n // \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\n this.vertices.splice(index, 1);\n\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\n this.adjMat.splice(index, 1);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\n for (const row of this.adjMat) {\n row.splice(index, 1);\n }\n }\n\n /* \u6dfb\u52a0\u8fb9 */\n // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n addEdge(i: number, j: number): void {\n // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n if (i < 0 || j < 0 || i >= this.size() || j >= this.size() || i === j) {\n throw new RangeError('Index Out Of Bounds Exception');\n }\n // \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u5173\u4e8e\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) === (j, i)\n this.adjMat[i][j] = 1;\n this.adjMat[j][i] = 1;\n }\n\n /* \u5220\u9664\u8fb9 */\n // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n removeEdge(i: number, j: number): void {\n // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n if (i < 0 || j < 0 || i >= this.size() || j >= this.size() || i === j) {\n throw new RangeError('Index Out Of Bounds Exception');\n }\n this.adjMat[i][j] = 0;\n this.adjMat[j][i] = 0;\n }\n\n /* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\n print(): void {\n console.log('\u9876\u70b9\u5217\u8868 = ', this.vertices);\n console.log('\u90bb\u63a5\u77e9\u9635 =', this.adjMat);\n }\n}\n
graph_adjacency_matrix.dart/* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjMat {\n List<int> vertices = []; // \u9876\u70b9\u5143\u7d20\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\n List<List<int>> adjMat = []; //\u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n\n /* \u6784\u9020\u65b9\u6cd5 */\n GraphAdjMat(List<int> vertices, List<List<int>> edges) {\n this.vertices = [];\n this.adjMat = [];\n // \u6dfb\u52a0\u9876\u70b9\n for (int val in vertices) {\n addVertex(val);\n }\n // \u6dfb\u52a0\u8fb9\n // \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n for (List<int> e in edges) {\n addEdge(e[0], e[1]);\n }\n }\n\n /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n int size() {\n return vertices.length;\n }\n\n /* \u6dfb\u52a0\u9876\u70b9 */\n void addVertex(int val) {\n int n = size();\n // \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\n vertices.add(val);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\n List<int> newRow = List.filled(n, 0, growable: true);\n adjMat.add(newRow);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\n for (List<int> row in adjMat) {\n row.add(0);\n }\n }\n\n /* \u5220\u9664\u9876\u70b9 */\n void removeVertex(int index) {\n if (index >= size()) {\n throw IndexError;\n }\n // \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\n vertices.removeAt(index);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\n adjMat.removeAt(index);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\n for (List<int> row in adjMat) {\n row.removeAt(index);\n }\n }\n\n /* \u6dfb\u52a0\u8fb9 */\n // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n void addEdge(int i, int j) {\n // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n if (i < 0 || j < 0 || i >= size() || j >= size() || i == j) {\n throw IndexError;\n }\n // \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u5173\u4e8e\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\n adjMat[i][j] = 1;\n adjMat[j][i] = 1;\n }\n\n /* \u5220\u9664\u8fb9 */\n // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n void removeEdge(int i, int j) {\n // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n if (i < 0 || j < 0 || i >= size() || j >= size() || i == j) {\n throw IndexError;\n }\n adjMat[i][j] = 0;\n adjMat[j][i] = 0;\n }\n\n /* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\n void printAdjMat() {\n print(\"\u9876\u70b9\u5217\u8868 = $vertices\");\n print(\"\u90bb\u63a5\u77e9\u9635 = \");\n printMatrix(adjMat);\n }\n}\n
graph_adjacency_matrix.rs/* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b\u578b */\npub struct GraphAdjMat {\n // \u9876\u70b9\u5217\u8868\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\n pub vertices: Vec<i32>,\n // \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n pub adj_mat: Vec<Vec<i32>>,\n}\n\nimpl GraphAdjMat {\n /* \u6784\u9020\u65b9\u6cd5 */\n pub fn new(vertices: Vec<i32>, edges: Vec<[usize; 2]>) -> Self {\n let mut graph = GraphAdjMat {\n vertices: vec![],\n adj_mat: vec![],\n };\n // \u6dfb\u52a0\u9876\u70b9\n for val in vertices {\n graph.add_vertex(val);\n }\n // \u6dfb\u52a0\u8fb9\n // \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n for edge in edges {\n graph.add_edge(edge[0], edge[1])\n }\n\n graph\n }\n\n /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n pub fn size(&self) -> usize {\n self.vertices.len()\n }\n\n /* \u6dfb\u52a0\u9876\u70b9 */\n pub fn add_vertex(&mut self, val: i32) {\n let n = self.size();\n // \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\n self.vertices.push(val);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\n self.adj_mat.push(vec![0; n]);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\n for row in &mut self.adj_mat {\n row.push(0);\n }\n }\n\n /* \u5220\u9664\u9876\u70b9 */\n pub fn remove_vertex(&mut self, index: usize) {\n if index >= self.size() {\n panic!(\"index error\")\n }\n // \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\n self.vertices.remove(index);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\n self.adj_mat.remove(index);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\n for row in &mut self.adj_mat {\n row.remove(index);\n }\n }\n\n /* \u6dfb\u52a0\u8fb9 */\n pub fn add_edge(&mut self, i: usize, j: usize) {\n // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n if i >= self.size() || j >= self.size() || i == j {\n panic!(\"index error\")\n }\n // \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u5173\u4e8e\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\n self.adj_mat[i][j] = 1;\n self.adj_mat[j][i] = 1;\n }\n\n /* \u5220\u9664\u8fb9 */\n // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n pub fn remove_edge(&mut self, i: usize, j: usize) {\n // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n if i >= self.size() || j >= self.size() || i == j {\n panic!(\"index error\")\n }\n self.adj_mat[i][j] = 0;\n self.adj_mat[j][i] = 0;\n }\n\n /* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\n pub fn print(&self) {\n println!(\"\u9876\u70b9\u5217\u8868 = {:?}\", self.vertices);\n println!(\"\u90bb\u63a5\u77e9\u9635 =\");\n println!(\"[\");\n for row in &self.adj_mat {\n println!(\" {:?},\", row);\n }\n println!(\"]\")\n }\n}\n
graph_adjacency_matrix.c/* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7ed3\u6784\u4f53 */\ntypedef struct {\n int vertices[MAX_SIZE];\n int adjMat[MAX_SIZE][MAX_SIZE];\n int size;\n} GraphAdjMat;\n\n/* \u6784\u9020\u51fd\u6570 */\nGraphAdjMat *newGraphAdjMat() {\n GraphAdjMat *graph = (GraphAdjMat *)malloc(sizeof(GraphAdjMat));\n graph->size = 0;\n for (int i = 0; i < MAX_SIZE; i++) {\n for (int j = 0; j < MAX_SIZE; j++) {\n graph->adjMat[i][j] = 0;\n }\n }\n return graph;\n}\n\n/* \u6790\u6784\u51fd\u6570 */\nvoid delGraphAdjMat(GraphAdjMat *graph) {\n free(graph);\n}\n\n/* \u6dfb\u52a0\u9876\u70b9 */\nvoid addVertex(GraphAdjMat *graph, int val) {\n if (graph->size == MAX_SIZE) {\n fprintf(stderr, \"\u56fe\u7684\u9876\u70b9\u6570\u91cf\u5df2\u8fbe\u6700\u5927\u503c\\n\");\n return;\n }\n // \u6dfb\u52a0\u7b2c n \u4e2a\u9876\u70b9\uff0c\u5e76\u5c06\u7b2c n \u884c\u548c\u5217\u7f6e\u96f6\n int n = graph->size;\n graph->vertices[n] = val;\n for (int i = 0; i <= n; i++) {\n graph->adjMat[n][i] = graph->adjMat[i][n] = 0;\n }\n graph->size++;\n}\n\n/* \u5220\u9664\u9876\u70b9 */\nvoid removeVertex(GraphAdjMat *graph, int index) {\n if (index < 0 || index >= graph->size) {\n fprintf(stderr, \"\u9876\u70b9\u7d22\u5f15\u8d8a\u754c\\n\");\n return;\n }\n // \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\n for (int i = index; i < graph->size - 1; i++) {\n graph->vertices[i] = graph->vertices[i + 1];\n }\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\n for (int i = index; i < graph->size - 1; i++) {\n for (int j = 0; j < graph->size; j++) {\n graph->adjMat[i][j] = graph->adjMat[i + 1][j];\n }\n }\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\n for (int i = 0; i < graph->size; i++) {\n for (int j = index; j < graph->size - 1; j++) {\n graph->adjMat[i][j] = graph->adjMat[i][j + 1];\n }\n }\n graph->size--;\n}\n\n/* \u6dfb\u52a0\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nvoid addEdge(GraphAdjMat *graph, int i, int j) {\n if (i < 0 || j < 0 || i >= graph->size || j >= graph->size || i == j) {\n fprintf(stderr, \"\u8fb9\u7d22\u5f15\u8d8a\u754c\u6216\u76f8\u7b49\\n\");\n return;\n }\n graph->adjMat[i][j] = 1;\n graph->adjMat[j][i] = 1;\n}\n\n/* \u5220\u9664\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nvoid removeEdge(GraphAdjMat *graph, int i, int j) {\n if (i < 0 || j < 0 || i >= graph->size || j >= graph->size || i == j) {\n fprintf(stderr, \"\u8fb9\u7d22\u5f15\u8d8a\u754c\u6216\u76f8\u7b49\\n\");\n return;\n }\n graph->adjMat[i][j] = 0;\n graph->adjMat[j][i] = 0;\n}\n\n/* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\nvoid printGraphAdjMat(GraphAdjMat *graph) {\n printf(\"\u9876\u70b9\u5217\u8868 = \");\n printArray(graph->vertices, graph->size);\n printf(\"\u90bb\u63a5\u77e9\u9635 =\\n\");\n for (int i = 0; i < graph->size; i++) {\n printArray(graph->adjMat[i], graph->size);\n }\n}\n
graph_adjacency_matrix.zig[class]{GraphAdjMat}-[func]{}\n
"},{"location":"chapter_graph/graph_operations/#922","title":"9.2.2 \u00a0 \u57fa\u4e8e\u90bb\u63a5\u8868\u7684\u5b9e\u73b0","text":"\u8bbe\u65e0\u5411\u56fe\u7684\u9876\u70b9\u603b\u6570\u4e3a \\(n\\)\u3001\u8fb9\u603b\u6570\u4e3a \\(m\\) \uff0c\u5219\u53ef\u6839\u636e\u56fe 9-8 \u6240\u793a\u7684\u65b9\u6cd5\u5b9e\u73b0\u5404\u79cd\u64cd\u4f5c\u3002
\u56fe 9-8 \u00a0 \u90bb\u63a5\u8868\u7684\u521d\u59cb\u5316\u3001\u589e\u5220\u8fb9\u3001\u589e\u5220\u9876\u70b9
\u4ee5\u4e0b\u662f\u90bb\u63a5\u8868\u7684\u4ee3\u7801\u5b9e\u73b0\u3002\u5bf9\u6bd4\u56fe 9-8 \uff0c\u5b9e\u9645\u4ee3\u7801\u6709\u4ee5\u4e0b\u4e0d\u540c\u3002
key
\u4e3a\u9876\u70b9\u5b9e\u4f8b\uff0cvalue
\u4e3a\u8be5\u9876\u70b9\u7684\u90bb\u63a5\u9876\u70b9\u5217\u8868\uff08\u94fe\u8868\uff09\u3002\u53e6\u5916\uff0c\u6211\u4eec\u5728\u90bb\u63a5\u8868\u4e2d\u4f7f\u7528 Vertex
\u7c7b\u6765\u8868\u793a\u9876\u70b9\uff0c\u8fd9\u6837\u505a\u7684\u539f\u56e0\u662f\uff1a\u5982\u679c\u4e0e\u90bb\u63a5\u77e9\u9635\u4e00\u6837\uff0c\u7528\u5217\u8868\u7d22\u5f15\u6765\u533a\u5206\u4e0d\u540c\u9876\u70b9\uff0c\u90a3\u4e48\u5047\u8bbe\u8981\u5220\u9664\u7d22\u5f15\u4e3a \\(i\\) \u7684\u9876\u70b9\uff0c\u5219\u9700\u904d\u5386\u6574\u4e2a\u90bb\u63a5\u8868\uff0c\u5c06\u6240\u6709\u5927\u4e8e \\(i\\) \u7684\u7d22\u5f15\u5168\u90e8\u51cf \\(1\\) \uff0c\u6548\u7387\u5f88\u4f4e\u3002\u800c\u5982\u679c\u6bcf\u4e2a\u9876\u70b9\u90fd\u662f\u552f\u4e00\u7684 Vertex
\u5b9e\u4f8b\uff0c\u5220\u9664\u67d0\u4e00\u9876\u70b9\u4e4b\u540e\u5c31\u65e0\u987b\u6539\u52a8\u5176\u4ed6\u9876\u70b9\u4e86\u3002
class GraphAdjList:\n \"\"\"\u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b\"\"\"\n\n def __init__(self, edges: list[list[Vertex]]):\n \"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n # \u90bb\u63a5\u8868\uff0ckey\uff1a\u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n self.adj_list = dict[Vertex, list[Vertex]]()\n # \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\n for edge in edges:\n self.add_vertex(edge[0])\n self.add_vertex(edge[1])\n self.add_edge(edge[0], edge[1])\n\n def size(self) -> int:\n \"\"\"\u83b7\u53d6\u9876\u70b9\u6570\u91cf\"\"\"\n return len(self.adj_list)\n\n def add_edge(self, vet1: Vertex, vet2: Vertex):\n \"\"\"\u6dfb\u52a0\u8fb9\"\"\"\n if vet1 not in self.adj_list or vet2 not in self.adj_list or vet1 == vet2:\n raise ValueError()\n # \u6dfb\u52a0\u8fb9 vet1 - vet2\n self.adj_list[vet1].append(vet2)\n self.adj_list[vet2].append(vet1)\n\n def remove_edge(self, vet1: Vertex, vet2: Vertex):\n \"\"\"\u5220\u9664\u8fb9\"\"\"\n if vet1 not in self.adj_list or vet2 not in self.adj_list or vet1 == vet2:\n raise ValueError()\n # \u5220\u9664\u8fb9 vet1 - vet2\n self.adj_list[vet1].remove(vet2)\n self.adj_list[vet2].remove(vet1)\n\n def add_vertex(self, vet: Vertex):\n \"\"\"\u6dfb\u52a0\u9876\u70b9\"\"\"\n if vet in self.adj_list:\n return\n # \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\n self.adj_list[vet] = []\n\n def remove_vertex(self, vet: Vertex):\n \"\"\"\u5220\u9664\u9876\u70b9\"\"\"\n if vet not in self.adj_list:\n raise ValueError()\n # \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\n self.adj_list.pop(vet)\n # \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\n for vertex in self.adj_list:\n if vet in self.adj_list[vertex]:\n self.adj_list[vertex].remove(vet)\n\n def print(self):\n \"\"\"\u6253\u5370\u90bb\u63a5\u8868\"\"\"\n print(\"\u90bb\u63a5\u8868 =\")\n for vertex in self.adj_list:\n tmp = [v.val for v in self.adj_list[vertex]]\n print(f\"{vertex.val}: {tmp},\")\n
graph_adjacency_list.cpp/* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjList {\n public:\n // \u90bb\u63a5\u8868\uff0ckey\uff1a\u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n unordered_map<Vertex *, vector<Vertex *>> adjList;\n\n /* \u5728 vector \u4e2d\u5220\u9664\u6307\u5b9a\u8282\u70b9 */\n void remove(vector<Vertex *> &vec, Vertex *vet) {\n for (int i = 0; i < vec.size(); i++) {\n if (vec[i] == vet) {\n vec.erase(vec.begin() + i);\n break;\n }\n }\n }\n\n /* \u6784\u9020\u65b9\u6cd5 */\n GraphAdjList(const vector<vector<Vertex *>> &edges) {\n // \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\n for (const vector<Vertex *> &edge : edges) {\n addVertex(edge[0]);\n addVertex(edge[1]);\n addEdge(edge[0], edge[1]);\n }\n }\n\n /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n int size() {\n return adjList.size();\n }\n\n /* \u6dfb\u52a0\u8fb9 */\n void addEdge(Vertex *vet1, Vertex *vet2) {\n if (!adjList.count(vet1) || !adjList.count(vet2) || vet1 == vet2)\n throw invalid_argument(\"\u4e0d\u5b58\u5728\u9876\u70b9\");\n // \u6dfb\u52a0\u8fb9 vet1 - vet2\n adjList[vet1].push_back(vet2);\n adjList[vet2].push_back(vet1);\n }\n\n /* \u5220\u9664\u8fb9 */\n void removeEdge(Vertex *vet1, Vertex *vet2) {\n if (!adjList.count(vet1) || !adjList.count(vet2) || vet1 == vet2)\n throw invalid_argument(\"\u4e0d\u5b58\u5728\u9876\u70b9\");\n // \u5220\u9664\u8fb9 vet1 - vet2\n remove(adjList[vet1], vet2);\n remove(adjList[vet2], vet1);\n }\n\n /* \u6dfb\u52a0\u9876\u70b9 */\n void addVertex(Vertex *vet) {\n if (adjList.count(vet))\n return;\n // \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\n adjList[vet] = vector<Vertex *>();\n }\n\n /* \u5220\u9664\u9876\u70b9 */\n void removeVertex(Vertex *vet) {\n if (!adjList.count(vet))\n throw invalid_argument(\"\u4e0d\u5b58\u5728\u9876\u70b9\");\n // \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\n adjList.erase(vet);\n // \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\n for (auto &adj : adjList) {\n remove(adj.second, vet);\n }\n }\n\n /* \u6253\u5370\u90bb\u63a5\u8868 */\n void print() {\n cout << \"\u90bb\u63a5\u8868 =\" << endl;\n for (auto &adj : adjList) {\n const auto &key = adj.first;\n const auto &vec = adj.second;\n cout << key->val << \": \";\n printVector(vetsToVals(vec));\n }\n }\n};\n
graph_adjacency_list.java/* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjList {\n // \u90bb\u63a5\u8868\uff0ckey\uff1a\u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n Map<Vertex, List<Vertex>> adjList;\n\n /* \u6784\u9020\u65b9\u6cd5 */\n public GraphAdjList(Vertex[][] edges) {\n this.adjList = new HashMap<>();\n // \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\n for (Vertex[] edge : edges) {\n addVertex(edge[0]);\n addVertex(edge[1]);\n addEdge(edge[0], edge[1]);\n }\n }\n\n /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n public int size() {\n return adjList.size();\n }\n\n /* \u6dfb\u52a0\u8fb9 */\n public void addEdge(Vertex vet1, Vertex vet2) {\n if (!adjList.containsKey(vet1) || !adjList.containsKey(vet2) || vet1 == vet2)\n throw new IllegalArgumentException();\n // \u6dfb\u52a0\u8fb9 vet1 - vet2\n adjList.get(vet1).add(vet2);\n adjList.get(vet2).add(vet1);\n }\n\n /* \u5220\u9664\u8fb9 */\n public void removeEdge(Vertex vet1, Vertex vet2) {\n if (!adjList.containsKey(vet1) || !adjList.containsKey(vet2) || vet1 == vet2)\n throw new IllegalArgumentException();\n // \u5220\u9664\u8fb9 vet1 - vet2\n adjList.get(vet1).remove(vet2);\n adjList.get(vet2).remove(vet1);\n }\n\n /* \u6dfb\u52a0\u9876\u70b9 */\n public void addVertex(Vertex vet) {\n if (adjList.containsKey(vet))\n return;\n // \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\n adjList.put(vet, new ArrayList<>());\n }\n\n /* \u5220\u9664\u9876\u70b9 */\n public void removeVertex(Vertex vet) {\n if (!adjList.containsKey(vet))\n throw new IllegalArgumentException();\n // \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\n adjList.remove(vet);\n // \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\n for (List<Vertex> list : adjList.values()) {\n list.remove(vet);\n }\n }\n\n /* \u6253\u5370\u90bb\u63a5\u8868 */\n public void print() {\n System.out.println(\"\u90bb\u63a5\u8868 =\");\n for (Map.Entry<Vertex, List<Vertex>> pair : adjList.entrySet()) {\n List<Integer> tmp = new ArrayList<>();\n for (Vertex vertex : pair.getValue())\n tmp.add(vertex.val);\n System.out.println(pair.getKey().val + \": \" + tmp + \",\");\n }\n }\n}\n
graph_adjacency_list.cs/* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjList {\n // \u90bb\u63a5\u8868\uff0ckey\uff1a\u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n public Dictionary<Vertex, List<Vertex>> adjList;\n\n /* \u6784\u9020\u51fd\u6570 */\n public GraphAdjList(Vertex[][] edges) {\n adjList = [];\n // \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\n foreach (Vertex[] edge in edges) {\n AddVertex(edge[0]);\n AddVertex(edge[1]);\n AddEdge(edge[0], edge[1]);\n }\n }\n\n /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n int Size() {\n return adjList.Count;\n }\n\n /* \u6dfb\u52a0\u8fb9 */\n public void AddEdge(Vertex vet1, Vertex vet2) {\n if (!adjList.ContainsKey(vet1) || !adjList.ContainsKey(vet2) || vet1 == vet2)\n throw new InvalidOperationException();\n // \u6dfb\u52a0\u8fb9 vet1 - vet2\n adjList[vet1].Add(vet2);\n adjList[vet2].Add(vet1);\n }\n\n /* \u5220\u9664\u8fb9 */\n public void RemoveEdge(Vertex vet1, Vertex vet2) {\n if (!adjList.ContainsKey(vet1) || !adjList.ContainsKey(vet2) || vet1 == vet2)\n throw new InvalidOperationException();\n // \u5220\u9664\u8fb9 vet1 - vet2\n adjList[vet1].Remove(vet2);\n adjList[vet2].Remove(vet1);\n }\n\n /* \u6dfb\u52a0\u9876\u70b9 */\n public void AddVertex(Vertex vet) {\n if (adjList.ContainsKey(vet))\n return;\n // \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\n adjList.Add(vet, []);\n }\n\n /* \u5220\u9664\u9876\u70b9 */\n public void RemoveVertex(Vertex vet) {\n if (!adjList.ContainsKey(vet))\n throw new InvalidOperationException();\n // \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\n adjList.Remove(vet);\n // \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\n foreach (List<Vertex> list in adjList.Values) {\n list.Remove(vet);\n }\n }\n\n /* \u6253\u5370\u90bb\u63a5\u8868 */\n public void Print() {\n Console.WriteLine(\"\u90bb\u63a5\u8868 =\");\n foreach (KeyValuePair<Vertex, List<Vertex>> pair in adjList) {\n List<int> tmp = [];\n foreach (Vertex vertex in pair.Value)\n tmp.Add(vertex.val);\n Console.WriteLine(pair.Key.val + \": [\" + string.Join(\", \", tmp) + \"],\");\n }\n }\n}\n
graph_adjacency_list.go/* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\ntype graphAdjList struct {\n // \u90bb\u63a5\u8868\uff0ckey\uff1a\u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n adjList map[Vertex][]Vertex\n}\n\n/* \u6784\u9020\u51fd\u6570 */\nfunc newGraphAdjList(edges [][]Vertex) *graphAdjList {\n g := &graphAdjList{\n adjList: make(map[Vertex][]Vertex),\n }\n // \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\n for _, edge := range edges {\n g.addVertex(edge[0])\n g.addVertex(edge[1])\n g.addEdge(edge[0], edge[1])\n }\n return g\n}\n\n/* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\nfunc (g *graphAdjList) size() int {\n return len(g.adjList)\n}\n\n/* \u6dfb\u52a0\u8fb9 */\nfunc (g *graphAdjList) addEdge(vet1 Vertex, vet2 Vertex) {\n _, ok1 := g.adjList[vet1]\n _, ok2 := g.adjList[vet2]\n if !ok1 || !ok2 || vet1 == vet2 {\n panic(\"error\")\n }\n // \u6dfb\u52a0\u8fb9 vet1 - vet2, \u6dfb\u52a0\u533f\u540d struct{},\n g.adjList[vet1] = append(g.adjList[vet1], vet2)\n g.adjList[vet2] = append(g.adjList[vet2], vet1)\n}\n\n/* \u5220\u9664\u8fb9 */\nfunc (g *graphAdjList) removeEdge(vet1 Vertex, vet2 Vertex) {\n _, ok1 := g.adjList[vet1]\n _, ok2 := g.adjList[vet2]\n if !ok1 || !ok2 || vet1 == vet2 {\n panic(\"error\")\n }\n // \u5220\u9664\u8fb9 vet1 - vet2\n g.adjList[vet1] = DeleteSliceElms(g.adjList[vet1], vet2)\n g.adjList[vet2] = DeleteSliceElms(g.adjList[vet2], vet1)\n}\n\n/* \u6dfb\u52a0\u9876\u70b9 */\nfunc (g *graphAdjList) addVertex(vet Vertex) {\n _, ok := g.adjList[vet]\n if ok {\n return\n }\n // \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\n g.adjList[vet] = make([]Vertex, 0)\n}\n\n/* \u5220\u9664\u9876\u70b9 */\nfunc (g *graphAdjList) removeVertex(vet Vertex) {\n _, ok := g.adjList[vet]\n if !ok {\n panic(\"error\")\n }\n // \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\n delete(g.adjList, vet)\n // \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\n for v, list := range g.adjList {\n g.adjList[v] = DeleteSliceElms(list, vet)\n }\n}\n\n/* \u6253\u5370\u90bb\u63a5\u8868 */\nfunc (g *graphAdjList) print() {\n var builder strings.Builder\n fmt.Printf(\"\u90bb\u63a5\u8868 = \\n\")\n for k, v := range g.adjList {\n builder.WriteString(\"\\t\\t\" + strconv.Itoa(k.Val) + \": \")\n for _, vet := range v {\n builder.WriteString(strconv.Itoa(vet.Val) + \" \")\n }\n fmt.Println(builder.String())\n builder.Reset()\n }\n}\n
graph_adjacency_list.swift/* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjList {\n // \u90bb\u63a5\u8868\uff0ckey\uff1a\u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n public private(set) var adjList: [Vertex: [Vertex]]\n\n /* \u6784\u9020\u65b9\u6cd5 */\n public init(edges: [[Vertex]]) {\n adjList = [:]\n // \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\n for edge in edges {\n addVertex(vet: edge[0])\n addVertex(vet: edge[1])\n addEdge(vet1: edge[0], vet2: edge[1])\n }\n }\n\n /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n public func size() -> Int {\n adjList.count\n }\n\n /* \u6dfb\u52a0\u8fb9 */\n public func addEdge(vet1: Vertex, vet2: Vertex) {\n if adjList[vet1] == nil || adjList[vet2] == nil || vet1 == vet2 {\n fatalError(\"\u53c2\u6570\u9519\u8bef\")\n }\n // \u6dfb\u52a0\u8fb9 vet1 - vet2\n adjList[vet1]?.append(vet2)\n adjList[vet2]?.append(vet1)\n }\n\n /* \u5220\u9664\u8fb9 */\n public func removeEdge(vet1: Vertex, vet2: Vertex) {\n if adjList[vet1] == nil || adjList[vet2] == nil || vet1 == vet2 {\n fatalError(\"\u53c2\u6570\u9519\u8bef\")\n }\n // \u5220\u9664\u8fb9 vet1 - vet2\n adjList[vet1]?.removeAll(where: { $0 == vet2 })\n adjList[vet2]?.removeAll(where: { $0 == vet1 })\n }\n\n /* \u6dfb\u52a0\u9876\u70b9 */\n public func addVertex(vet: Vertex) {\n if adjList[vet] != nil {\n return\n }\n // \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\n adjList[vet] = []\n }\n\n /* \u5220\u9664\u9876\u70b9 */\n public func removeVertex(vet: Vertex) {\n if adjList[vet] == nil {\n fatalError(\"\u53c2\u6570\u9519\u8bef\")\n }\n // \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\n adjList.removeValue(forKey: vet)\n // \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\n for key in adjList.keys {\n adjList[key]?.removeAll(where: { $0 == vet })\n }\n }\n\n /* \u6253\u5370\u90bb\u63a5\u8868 */\n public func print() {\n Swift.print(\"\u90bb\u63a5\u8868 =\")\n for pair in adjList {\n var tmp: [Int] = []\n for vertex in pair.value {\n tmp.append(vertex.val)\n }\n Swift.print(\"\\(pair.key.val): \\(tmp),\")\n }\n }\n}\n
graph_adjacency_list.js/* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjList {\n // \u90bb\u63a5\u8868\uff0ckey\uff1a\u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n adjList;\n\n /* \u6784\u9020\u65b9\u6cd5 */\n constructor(edges) {\n this.adjList = new Map();\n // \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\n for (const edge of edges) {\n this.addVertex(edge[0]);\n this.addVertex(edge[1]);\n this.addEdge(edge[0], edge[1]);\n }\n }\n\n /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n size() {\n return this.adjList.size;\n }\n\n /* \u6dfb\u52a0\u8fb9 */\n addEdge(vet1, vet2) {\n if (\n !this.adjList.has(vet1) ||\n !this.adjList.has(vet2) ||\n vet1 === vet2\n ) {\n throw new Error('Illegal Argument Exception');\n }\n // \u6dfb\u52a0\u8fb9 vet1 - vet2\n this.adjList.get(vet1).push(vet2);\n this.adjList.get(vet2).push(vet1);\n }\n\n /* \u5220\u9664\u8fb9 */\n removeEdge(vet1, vet2) {\n if (\n !this.adjList.has(vet1) ||\n !this.adjList.has(vet2) ||\n vet1 === vet2\n ) {\n throw new Error('Illegal Argument Exception');\n }\n // \u5220\u9664\u8fb9 vet1 - vet2\n this.adjList.get(vet1).splice(this.adjList.get(vet1).indexOf(vet2), 1);\n this.adjList.get(vet2).splice(this.adjList.get(vet2).indexOf(vet1), 1);\n }\n\n /* \u6dfb\u52a0\u9876\u70b9 */\n addVertex(vet) {\n if (this.adjList.has(vet)) return;\n // \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\n this.adjList.set(vet, []);\n }\n\n /* \u5220\u9664\u9876\u70b9 */\n removeVertex(vet) {\n if (!this.adjList.has(vet)) {\n throw new Error('Illegal Argument Exception');\n }\n // \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\n this.adjList.delete(vet);\n // \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\n for (const set of this.adjList.values()) {\n const index = set.indexOf(vet);\n if (index > -1) {\n set.splice(index, 1);\n }\n }\n }\n\n /* \u6253\u5370\u90bb\u63a5\u8868 */\n print() {\n console.log('\u90bb\u63a5\u8868 =');\n for (const [key, value] of this.adjList) {\n const tmp = [];\n for (const vertex of value) {\n tmp.push(vertex.val);\n }\n console.log(key.val + ': ' + tmp.join());\n }\n }\n}\n
graph_adjacency_list.ts/* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjList {\n // \u90bb\u63a5\u8868\uff0ckey\uff1a\u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n adjList: Map<Vertex, Vertex[]>;\n\n /* \u6784\u9020\u65b9\u6cd5 */\n constructor(edges: Vertex[][]) {\n this.adjList = new Map();\n // \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\n for (const edge of edges) {\n this.addVertex(edge[0]);\n this.addVertex(edge[1]);\n this.addEdge(edge[0], edge[1]);\n }\n }\n\n /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n size(): number {\n return this.adjList.size;\n }\n\n /* \u6dfb\u52a0\u8fb9 */\n addEdge(vet1: Vertex, vet2: Vertex): void {\n if (\n !this.adjList.has(vet1) ||\n !this.adjList.has(vet2) ||\n vet1 === vet2\n ) {\n throw new Error('Illegal Argument Exception');\n }\n // \u6dfb\u52a0\u8fb9 vet1 - vet2\n this.adjList.get(vet1).push(vet2);\n this.adjList.get(vet2).push(vet1);\n }\n\n /* \u5220\u9664\u8fb9 */\n removeEdge(vet1: Vertex, vet2: Vertex): void {\n if (\n !this.adjList.has(vet1) ||\n !this.adjList.has(vet2) ||\n vet1 === vet2\n ) {\n throw new Error('Illegal Argument Exception');\n }\n // \u5220\u9664\u8fb9 vet1 - vet2\n this.adjList.get(vet1).splice(this.adjList.get(vet1).indexOf(vet2), 1);\n this.adjList.get(vet2).splice(this.adjList.get(vet2).indexOf(vet1), 1);\n }\n\n /* \u6dfb\u52a0\u9876\u70b9 */\n addVertex(vet: Vertex): void {\n if (this.adjList.has(vet)) return;\n // \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\n this.adjList.set(vet, []);\n }\n\n /* \u5220\u9664\u9876\u70b9 */\n removeVertex(vet: Vertex): void {\n if (!this.adjList.has(vet)) {\n throw new Error('Illegal Argument Exception');\n }\n // \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\n this.adjList.delete(vet);\n // \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\n for (const set of this.adjList.values()) {\n const index: number = set.indexOf(vet);\n if (index > -1) {\n set.splice(index, 1);\n }\n }\n }\n\n /* \u6253\u5370\u90bb\u63a5\u8868 */\n print(): void {\n console.log('\u90bb\u63a5\u8868 =');\n for (const [key, value] of this.adjList.entries()) {\n const tmp = [];\n for (const vertex of value) {\n tmp.push(vertex.val);\n }\n console.log(key.val + ': ' + tmp.join());\n }\n }\n}\n
graph_adjacency_list.dart/* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjList {\n // \u90bb\u63a5\u8868\uff0ckey\uff1a\u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n Map<Vertex, List<Vertex>> adjList = {};\n\n /* \u6784\u9020\u65b9\u6cd5 */\n GraphAdjList(List<List<Vertex>> edges) {\n for (List<Vertex> edge in edges) {\n addVertex(edge[0]);\n addVertex(edge[1]);\n addEdge(edge[0], edge[1]);\n }\n }\n\n /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n int size() {\n return adjList.length;\n }\n\n /* \u6dfb\u52a0\u8fb9 */\n void addEdge(Vertex vet1, Vertex vet2) {\n if (!adjList.containsKey(vet1) ||\n !adjList.containsKey(vet2) ||\n vet1 == vet2) {\n throw ArgumentError;\n }\n // \u6dfb\u52a0\u8fb9 vet1 - vet2\n adjList[vet1]!.add(vet2);\n adjList[vet2]!.add(vet1);\n }\n\n /* \u5220\u9664\u8fb9 */\n void removeEdge(Vertex vet1, Vertex vet2) {\n if (!adjList.containsKey(vet1) ||\n !adjList.containsKey(vet2) ||\n vet1 == vet2) {\n throw ArgumentError;\n }\n // \u5220\u9664\u8fb9 vet1 - vet2\n adjList[vet1]!.remove(vet2);\n adjList[vet2]!.remove(vet1);\n }\n\n /* \u6dfb\u52a0\u9876\u70b9 */\n void addVertex(Vertex vet) {\n if (adjList.containsKey(vet)) return;\n // \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\n adjList[vet] = [];\n }\n\n /* \u5220\u9664\u9876\u70b9 */\n void removeVertex(Vertex vet) {\n if (!adjList.containsKey(vet)) {\n throw ArgumentError;\n }\n // \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\n adjList.remove(vet);\n // \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\n adjList.forEach((key, value) {\n value.remove(vet);\n });\n }\n\n /* \u6253\u5370\u90bb\u63a5\u8868 */\n void printAdjList() {\n print(\"\u90bb\u63a5\u8868 =\");\n adjList.forEach((key, value) {\n List<int> tmp = [];\n for (Vertex vertex in value) {\n tmp.add(vertex.val);\n }\n print(\"${key.val}: $tmp,\");\n });\n }\n}\n
graph_adjacency_list.rs/* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b\u578b */\npub struct GraphAdjList {\n // \u90bb\u63a5\u8868\uff0ckey\uff1a\u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n pub adj_list: HashMap<Vertex, Vec<Vertex>>,\n}\n\nimpl GraphAdjList {\n /* \u6784\u9020\u65b9\u6cd5 */\n pub fn new(edges: Vec<[Vertex; 2]>) -> Self {\n let mut graph = GraphAdjList {\n adj_list: HashMap::new(),\n };\n // \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\n for edge in edges {\n graph.add_vertex(edge[0]);\n graph.add_vertex(edge[1]);\n graph.add_edge(edge[0], edge[1]);\n }\n\n graph\n }\n\n /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n #[allow(unused)]\n pub fn size(&self) -> usize {\n self.adj_list.len()\n }\n\n /* \u6dfb\u52a0\u8fb9 */\n pub fn add_edge(&mut self, vet1: Vertex, vet2: Vertex) {\n if !self.adj_list.contains_key(&vet1) || !self.adj_list.contains_key(&vet2) || vet1 == vet2\n {\n panic!(\"value error\");\n }\n // \u6dfb\u52a0\u8fb9 vet1 - vet2\n self.adj_list.get_mut(&vet1).unwrap().push(vet2);\n self.adj_list.get_mut(&vet2).unwrap().push(vet1);\n }\n\n /* \u5220\u9664\u8fb9 */\n #[allow(unused)]\n pub fn remove_edge(&mut self, vet1: Vertex, vet2: Vertex) {\n if !self.adj_list.contains_key(&vet1) || !self.adj_list.contains_key(&vet2) || vet1 == vet2\n {\n panic!(\"value error\");\n }\n // \u5220\u9664\u8fb9 vet1 - vet2\n self.adj_list\n .get_mut(&vet1)\n .unwrap()\n .retain(|&vet| vet != vet2);\n self.adj_list\n .get_mut(&vet2)\n .unwrap()\n .retain(|&vet| vet != vet1);\n }\n\n /* \u6dfb\u52a0\u9876\u70b9 */\n pub fn add_vertex(&mut self, vet: Vertex) {\n if self.adj_list.contains_key(&vet) {\n return;\n }\n // \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\n self.adj_list.insert(vet, vec![]);\n }\n\n /* \u5220\u9664\u9876\u70b9 */\n #[allow(unused)]\n pub fn remove_vertex(&mut self, vet: Vertex) {\n if !self.adj_list.contains_key(&vet) {\n panic!(\"value error\");\n }\n // \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\n self.adj_list.remove(&vet);\n // \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\n for list in self.adj_list.values_mut() {\n list.retain(|&v| v != vet);\n }\n }\n\n /* \u6253\u5370\u90bb\u63a5\u8868 */\n pub fn print(&self) {\n println!(\"\u90bb\u63a5\u8868 =\");\n for (vertex, list) in &self.adj_list {\n let list = list.iter().map(|vertex| vertex.val).collect::<Vec<i32>>();\n println!(\"{}: {:?},\", vertex.val, list);\n }\n }\n}\n
graph_adjacency_list.c/* \u8282\u70b9\u7ed3\u6784\u4f53 */\ntypedef struct AdjListNode {\n Vertex *vertex; // \u9876\u70b9\n struct AdjListNode *next; // \u540e\u7ee7\u8282\u70b9\n} AdjListNode;\n\n/* \u67e5\u627e\u9876\u70b9\u5bf9\u5e94\u7684\u8282\u70b9 */\nAdjListNode *findNode(GraphAdjList *graph, Vertex *vet) {\n for (int i = 0; i < graph->size; i++) {\n if (graph->heads[i]->vertex == vet) {\n return graph->heads[i];\n }\n }\n return NULL;\n}\n\n/* \u6dfb\u52a0\u8fb9\u8f85\u52a9\u51fd\u6570 */\nvoid addEdgeHelper(AdjListNode *head, Vertex *vet) {\n AdjListNode *node = (AdjListNode *)malloc(sizeof(AdjListNode));\n node->vertex = vet;\n // \u5934\u63d2\u6cd5\n node->next = head->next;\n head->next = node;\n}\n\n/* \u5220\u9664\u8fb9\u8f85\u52a9\u51fd\u6570 */\nvoid removeEdgeHelper(AdjListNode *head, Vertex *vet) {\n AdjListNode *pre = head;\n AdjListNode *cur = head->next;\n // \u5728\u94fe\u8868\u4e2d\u641c\u7d22 vet \u5bf9\u5e94\u8282\u70b9\n while (cur != NULL && cur->vertex != vet) {\n pre = cur;\n cur = cur->next;\n }\n if (cur == NULL)\n return;\n // \u5c06 vet \u5bf9\u5e94\u8282\u70b9\u4ece\u94fe\u8868\u4e2d\u5220\u9664\n pre->next = cur->next;\n // \u91ca\u653e\u5185\u5b58\n free(cur);\n}\n\n/* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\ntypedef struct {\n AdjListNode *heads[MAX_SIZE]; // \u8282\u70b9\u6570\u7ec4\n int size; // \u8282\u70b9\u6570\u91cf\n} GraphAdjList;\n\n/* \u6784\u9020\u51fd\u6570 */\nGraphAdjList *newGraphAdjList() {\n GraphAdjList *graph = (GraphAdjList *)malloc(sizeof(GraphAdjList));\n if (!graph) {\n return NULL;\n }\n graph->size = 0;\n for (int i = 0; i < MAX_SIZE; i++) {\n graph->heads[i] = NULL;\n }\n return graph;\n}\n\n/* \u6790\u6784\u51fd\u6570 */\nvoid delGraphAdjList(GraphAdjList *graph) {\n for (int i = 0; i < graph->size; i++) {\n AdjListNode *cur = graph->heads[i];\n while (cur != NULL) {\n AdjListNode *next = cur->next;\n if (cur != graph->heads[i]) {\n free(cur);\n }\n cur = next;\n }\n free(graph->heads[i]->vertex);\n free(graph->heads[i]);\n }\n free(graph);\n}\n\n/* \u67e5\u627e\u9876\u70b9\u5bf9\u5e94\u7684\u8282\u70b9 */\nAdjListNode *findNode(GraphAdjList *graph, Vertex *vet) {\n for (int i = 0; i < graph->size; i++) {\n if (graph->heads[i]->vertex == vet) {\n return graph->heads[i];\n }\n }\n return NULL;\n}\n\n/* \u6dfb\u52a0\u8fb9 */\nvoid addEdge(GraphAdjList *graph, Vertex *vet1, Vertex *vet2) {\n AdjListNode *head1 = findNode(graph, vet1);\n AdjListNode *head2 = findNode(graph, vet2);\n assert(head1 != NULL && head2 != NULL && head1 != head2);\n // \u6dfb\u52a0\u8fb9 vet1 - vet2\n addEdgeHelper(head1, vet2);\n addEdgeHelper(head2, vet1);\n}\n\n/* \u5220\u9664\u8fb9 */\nvoid removeEdge(GraphAdjList *graph, Vertex *vet1, Vertex *vet2) {\n AdjListNode *head1 = findNode(graph, vet1);\n AdjListNode *head2 = findNode(graph, vet2);\n assert(head1 != NULL && head2 != NULL);\n // \u5220\u9664\u8fb9 vet1 - vet2\n removeEdgeHelper(head1, head2->vertex);\n removeEdgeHelper(head2, head1->vertex);\n}\n\n/* \u6dfb\u52a0\u9876\u70b9 */\nvoid addVertex(GraphAdjList *graph, Vertex *vet) {\n assert(graph != NULL && graph->size < MAX_SIZE);\n AdjListNode *head = (AdjListNode *)malloc(sizeof(AdjListNode));\n head->vertex = vet;\n head->next = NULL;\n // \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\n graph->heads[graph->size++] = head;\n}\n\n/* \u5220\u9664\u9876\u70b9 */\nvoid removeVertex(GraphAdjList *graph, Vertex *vet) {\n AdjListNode *node = findNode(graph, vet);\n assert(node != NULL);\n // \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\n AdjListNode *cur = node, *pre = NULL;\n while (cur) {\n pre = cur;\n cur = cur->next;\n free(pre);\n }\n // \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\n for (int i = 0; i < graph->size; i++) {\n cur = graph->heads[i];\n pre = NULL;\n while (cur) {\n pre = cur;\n cur = cur->next;\n if (cur && cur->vertex == vet) {\n pre->next = cur->next;\n free(cur);\n break;\n }\n }\n }\n // \u5c06\u8be5\u9876\u70b9\u4e4b\u540e\u7684\u9876\u70b9\u5411\u524d\u79fb\u52a8\uff0c\u4ee5\u586b\u8865\u7a7a\u7f3a\n int i;\n for (i = 0; i < graph->size; i++) {\n if (graph->heads[i] == node)\n break;\n }\n for (int j = i; j < graph->size - 1; j++) {\n graph->heads[j] = graph->heads[j + 1];\n }\n graph->size--;\n free(vet);\n}\n
graph_adjacency_list.zig[class]{GraphAdjList}-[func]{}\n
"},{"location":"chapter_graph/graph_operations/#923","title":"9.2.3 \u00a0 \u6548\u7387\u5bf9\u6bd4","text":"\u8bbe\u56fe\u4e2d\u5171\u6709 \\(n\\) \u4e2a\u9876\u70b9\u548c \\(m\\) \u6761\u8fb9\uff0c\u8868 9-2 \u5bf9\u6bd4\u4e86\u90bb\u63a5\u77e9\u9635\u548c\u90bb\u63a5\u8868\u7684\u65f6\u95f4\u6548\u7387\u548c\u7a7a\u95f4\u6548\u7387\u3002
\u8868 9-2 \u00a0 \u90bb\u63a5\u77e9\u9635\u4e0e\u90bb\u63a5\u8868\u5bf9\u6bd4
\u90bb\u63a5\u77e9\u9635 \u90bb\u63a5\u8868\uff08\u94fe\u8868\uff09 \u90bb\u63a5\u8868\uff08\u54c8\u5e0c\u8868\uff09 \u5224\u65ad\u662f\u5426\u90bb\u63a5 \\(O(1)\\) \\(O(m)\\) \\(O(1)\\) \u6dfb\u52a0\u8fb9 \\(O(1)\\) \\(O(1)\\) \\(O(1)\\) \u5220\u9664\u8fb9 \\(O(1)\\) \\(O(m)\\) \\(O(1)\\) \u6dfb\u52a0\u9876\u70b9 \\(O(n)\\) \\(O(1)\\) \\(O(1)\\) \u5220\u9664\u9876\u70b9 \\(O(n^2)\\) \\(O(n + m)\\) \\(O(n)\\) \u5185\u5b58\u7a7a\u95f4\u5360\u7528 \\(O(n^2)\\) \\(O(n + m)\\) \\(O(n + m)\\)\u89c2\u5bdf\u8868 9-2 \uff0c\u4f3c\u4e4e\u90bb\u63a5\u8868\uff08\u54c8\u5e0c\u8868\uff09\u7684\u65f6\u95f4\u6548\u7387\u4e0e\u7a7a\u95f4\u6548\u7387\u6700\u4f18\u3002\u4f46\u5b9e\u9645\u4e0a\uff0c\u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u64cd\u4f5c\u8fb9\u7684\u6548\u7387\u66f4\u9ad8\uff0c\u53ea\u9700\u4e00\u6b21\u6570\u7ec4\u8bbf\u95ee\u6216\u8d4b\u503c\u64cd\u4f5c\u5373\u53ef\u3002\u7efc\u5408\u6765\u770b\uff0c\u90bb\u63a5\u77e9\u9635\u4f53\u73b0\u4e86\u201c\u4ee5\u7a7a\u95f4\u6362\u65f6\u95f4\u201d\u7684\u539f\u5219\uff0c\u800c\u90bb\u63a5\u8868\u4f53\u73b0\u4e86\u201c\u4ee5\u65f6\u95f4\u6362\u7a7a\u95f4\u201d\u7684\u539f\u5219\u3002
"},{"location":"chapter_graph/graph_traversal/","title":"9.3 \u00a0 \u56fe\u7684\u904d\u5386","text":"\u6811\u4ee3\u8868\u7684\u662f\u201c\u4e00\u5bf9\u591a\u201d\u7684\u5173\u7cfb\uff0c\u800c\u56fe\u5219\u5177\u6709\u66f4\u9ad8\u7684\u81ea\u7531\u5ea6\uff0c\u53ef\u4ee5\u8868\u793a\u4efb\u610f\u7684\u201c\u591a\u5bf9\u591a\u201d\u5173\u7cfb\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u628a\u6811\u770b\u4f5c\u56fe\u7684\u4e00\u79cd\u7279\u4f8b\u3002\u663e\u7136\uff0c\u6811\u7684\u904d\u5386\u64cd\u4f5c\u4e5f\u662f\u56fe\u7684\u904d\u5386\u64cd\u4f5c\u7684\u4e00\u79cd\u7279\u4f8b\u3002
\u56fe\u548c\u6811\u90fd\u9700\u8981\u5e94\u7528\u641c\u7d22\u7b97\u6cd5\u6765\u5b9e\u73b0\u904d\u5386\u64cd\u4f5c\u3002\u56fe\u7684\u904d\u5386\u65b9\u5f0f\u53ef\u5206\u4e3a\u4e24\u79cd\uff1a\u300c\u5e7f\u5ea6\u4f18\u5148\u904d\u5386 breadth-first traversal\u300d\u548c\u300c\u6df1\u5ea6\u4f18\u5148\u904d\u5386 depth-first traversal\u300d\u3002\u5b83\u4eec\u4e5f\u5e38\u88ab\u79f0\u4e3a\u300c\u5e7f\u5ea6\u4f18\u5148\u641c\u7d22 breadth-first search\u300d\u548c\u300c\u6df1\u5ea6\u4f18\u5148\u641c\u7d22 depth-first search\u300d\uff0c\u7b80\u79f0 BFS \u548c DFS \u3002
"},{"location":"chapter_graph/graph_traversal/#931","title":"9.3.1 \u00a0 \u5e7f\u5ea6\u4f18\u5148\u904d\u5386","text":"\u5e7f\u5ea6\u4f18\u5148\u904d\u5386\u662f\u4e00\u79cd\u7531\u8fd1\u53ca\u8fdc\u7684\u904d\u5386\u65b9\u5f0f\uff0c\u4ece\u67d0\u4e2a\u8282\u70b9\u51fa\u53d1\uff0c\u59cb\u7ec8\u4f18\u5148\u8bbf\u95ee\u8ddd\u79bb\u6700\u8fd1\u7684\u9876\u70b9\uff0c\u5e76\u4e00\u5c42\u5c42\u5411\u5916\u6269\u5f20\u3002\u5982\u56fe 9-9 \u6240\u793a\uff0c\u4ece\u5de6\u4e0a\u89d2\u9876\u70b9\u51fa\u53d1\uff0c\u9996\u5148\u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\uff0c\u7136\u540e\u904d\u5386\u4e0b\u4e00\u4e2a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\uff0c\u4ee5\u6b64\u7c7b\u63a8\uff0c\u76f4\u81f3\u6240\u6709\u9876\u70b9\u8bbf\u95ee\u5b8c\u6bd5\u3002
\u56fe 9-9 \u00a0 \u56fe\u7684\u5e7f\u5ea6\u4f18\u5148\u904d\u5386
"},{"location":"chapter_graph/graph_traversal/#1","title":"1. \u00a0 \u7b97\u6cd5\u5b9e\u73b0","text":"BFS \u901a\u5e38\u501f\u52a9\u961f\u5217\u6765\u5b9e\u73b0\uff0c\u4ee3\u7801\u5982\u4e0b\u6240\u793a\u3002\u961f\u5217\u5177\u6709\u201c\u5148\u5165\u5148\u51fa\u201d\u7684\u6027\u8d28\uff0c\u8fd9\u4e0e BFS \u7684\u201c\u7531\u8fd1\u53ca\u8fdc\u201d\u7684\u601d\u60f3\u5f02\u66f2\u540c\u5de5\u3002
startVet
\u52a0\u5165\u961f\u5217\uff0c\u5e76\u5f00\u542f\u5faa\u73af\u30022.
\uff0c\u76f4\u5230\u6240\u6709\u9876\u70b9\u88ab\u8bbf\u95ee\u5b8c\u6bd5\u540e\u7ed3\u675f\u3002\u4e3a\u4e86\u9632\u6b62\u91cd\u590d\u904d\u5386\u9876\u70b9\uff0c\u6211\u4eec\u9700\u8981\u501f\u52a9\u4e00\u4e2a\u54c8\u5e0c\u8868 visited
\u6765\u8bb0\u5f55\u54ea\u4e9b\u8282\u70b9\u5df2\u88ab\u8bbf\u95ee\u3002
def graph_bfs(graph: GraphAdjList, start_vet: Vertex) -> list[Vertex]:\n \"\"\"\u5e7f\u5ea6\u4f18\u5148\u904d\u5386 BFS\"\"\"\n # \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n # \u9876\u70b9\u904d\u5386\u5e8f\u5217\n res = []\n # \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n visited = set[Vertex]([start_vet])\n # \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\n que = deque[Vertex]([start_vet])\n # \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\n while len(que) > 0:\n vet = que.popleft() # \u961f\u9996\u9876\u70b9\u51fa\u961f\n res.append(vet) # \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n # \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n for adj_vet in graph.adj_list[vet]:\n if adj_vet in visited:\n continue # \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n que.append(adj_vet) # \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\n visited.add(adj_vet) # \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n # \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\n return res\n
graph_bfs.cpp/* \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 BFS */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nvector<Vertex *> graphBFS(GraphAdjList &graph, Vertex *startVet) {\n // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n vector<Vertex *> res;\n // \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n unordered_set<Vertex *> visited = {startVet};\n // \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\n queue<Vertex *> que;\n que.push(startVet);\n // \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\n while (!que.empty()) {\n Vertex *vet = que.front();\n que.pop(); // \u961f\u9996\u9876\u70b9\u51fa\u961f\n res.push_back(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n for (auto adjVet : graph.adjList[vet]) {\n if (visited.count(adjVet))\n continue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n que.push(adjVet); // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\n visited.emplace(adjVet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n }\n }\n // \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\n return res;\n}\n
graph_bfs.java/* \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 BFS */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nList<Vertex> graphBFS(GraphAdjList graph, Vertex startVet) {\n // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n List<Vertex> res = new ArrayList<>();\n // \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n Set<Vertex> visited = new HashSet<>();\n visited.add(startVet);\n // \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\n Queue<Vertex> que = new LinkedList<>();\n que.offer(startVet);\n // \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\n while (!que.isEmpty()) {\n Vertex vet = que.poll(); // \u961f\u9996\u9876\u70b9\u51fa\u961f\n res.add(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n for (Vertex adjVet : graph.adjList.get(vet)) {\n if (visited.contains(adjVet))\n continue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n que.offer(adjVet); // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\n visited.add(adjVet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n }\n }\n // \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\n return res;\n}\n
graph_bfs.cs/* \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 BFS */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nList<Vertex> GraphBFS(GraphAdjList graph, Vertex startVet) {\n // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n List<Vertex> res = [];\n // \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n HashSet<Vertex> visited = [startVet];\n // \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\n Queue<Vertex> que = new();\n que.Enqueue(startVet);\n // \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\n while (que.Count > 0) {\n Vertex vet = que.Dequeue(); // \u961f\u9996\u9876\u70b9\u51fa\u961f\n res.Add(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n foreach (Vertex adjVet in graph.adjList[vet]) {\n if (visited.Contains(adjVet)) {\n continue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n }\n que.Enqueue(adjVet); // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\n visited.Add(adjVet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n }\n }\n\n // \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\n return res;\n}\n
graph_bfs.go/* \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 BFS */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfunc graphBFS(g *graphAdjList, startVet Vertex) []Vertex {\n // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n res := make([]Vertex, 0)\n // \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n visited := make(map[Vertex]struct{})\n visited[startVet] = struct{}{}\n // \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS, \u4f7f\u7528\u5207\u7247\u6a21\u62df\u961f\u5217\n queue := make([]Vertex, 0)\n queue = append(queue, startVet)\n // \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\n for len(queue) > 0 {\n // \u961f\u9996\u9876\u70b9\u51fa\u961f\n vet := queue[0]\n queue = queue[1:]\n // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n res = append(res, vet)\n // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n for _, adjVet := range g.adjList[vet] {\n _, isExist := visited[adjVet]\n // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\n if !isExist {\n queue = append(queue, adjVet)\n visited[adjVet] = struct{}{}\n }\n }\n }\n // \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\n return res\n}\n
graph_bfs.swift/* \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 BFS */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfunc graphBFS(graph: GraphAdjList, startVet: Vertex) -> [Vertex] {\n // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n var res: [Vertex] = []\n // \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n var visited: Set<Vertex> = [startVet]\n // \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\n var que: [Vertex] = [startVet]\n // \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\n while !que.isEmpty {\n let vet = que.removeFirst() // \u961f\u9996\u9876\u70b9\u51fa\u961f\n res.append(vet) // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n for adjVet in graph.adjList[vet] ?? [] {\n if visited.contains(adjVet) {\n continue // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n }\n que.append(adjVet) // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\n visited.insert(adjVet) // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n }\n }\n // \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\n return res\n}\n
graph_bfs.js/* \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 BFS */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfunction graphBFS(graph, startVet) {\n // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n const res = [];\n // \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n const visited = new Set();\n visited.add(startVet);\n // \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\n const que = [startVet];\n // \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\n while (que.length) {\n const vet = que.shift(); // \u961f\u9996\u9876\u70b9\u51fa\u961f\n res.push(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n for (const adjVet of graph.adjList.get(vet) ?? []) {\n if (visited.has(adjVet)) {\n continue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n }\n que.push(adjVet); // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\n visited.add(adjVet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n }\n }\n // \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\n return res;\n}\n
graph_bfs.ts/* \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 BFS */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfunction graphBFS(graph: GraphAdjList, startVet: Vertex): Vertex[] {\n // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n const res: Vertex[] = [];\n // \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n const visited: Set<Vertex> = new Set();\n visited.add(startVet);\n // \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\n const que = [startVet];\n // \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\n while (que.length) {\n const vet = que.shift(); // \u961f\u9996\u9876\u70b9\u51fa\u961f\n res.push(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n for (const adjVet of graph.adjList.get(vet) ?? []) {\n if (visited.has(adjVet)) {\n continue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n }\n que.push(adjVet); // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\n visited.add(adjVet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n }\n }\n // \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\n return res;\n}\n
graph_bfs.dart/* \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 BFS */\nList<Vertex> graphBFS(GraphAdjList graph, Vertex startVet) {\n // \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n List<Vertex> res = [];\n // \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n Set<Vertex> visited = {};\n visited.add(startVet);\n // \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\n Queue<Vertex> que = Queue();\n que.add(startVet);\n // \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\n while (que.isNotEmpty) {\n Vertex vet = que.removeFirst(); // \u961f\u9996\u9876\u70b9\u51fa\u961f\n res.add(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n for (Vertex adjVet in graph.adjList[vet]!) {\n if (visited.contains(adjVet)) {\n continue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n }\n que.add(adjVet); // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\n visited.add(adjVet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n }\n }\n // \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\n return res;\n}\n
graph_bfs.rs/* \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 BFS */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfn graph_bfs(graph: GraphAdjList, start_vet: Vertex) -> Vec<Vertex> {\n // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n let mut res = vec![];\n // \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n let mut visited = HashSet::new();\n visited.insert(start_vet);\n // \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\n let mut que = VecDeque::new();\n que.push_back(start_vet);\n // \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\n while !que.is_empty() {\n let vet = que.pop_front().unwrap(); // \u961f\u9996\u9876\u70b9\u51fa\u961f\n res.push(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n if let Some(adj_vets) = graph.adj_list.get(&vet) {\n for &adj_vet in adj_vets {\n if visited.contains(&adj_vet) {\n continue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n }\n que.push_back(adj_vet); // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\n visited.insert(adj_vet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n }\n }\n }\n // \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\n res\n}\n
graph_bfs.c/* \u8282\u70b9\u961f\u5217\u7ed3\u6784\u4f53 */\ntypedef struct {\n Vertex *vertices[MAX_SIZE];\n int front, rear, size;\n} Queue;\n\n/* \u6784\u9020\u51fd\u6570 */\nQueue *newQueue() {\n Queue *q = (Queue *)malloc(sizeof(Queue));\n q->front = q->rear = q->size = 0;\n return q;\n}\n\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nint isEmpty(Queue *q) {\n return q->size == 0;\n}\n\n/* \u5165\u961f\u64cd\u4f5c */\nvoid enqueue(Queue *q, Vertex *vet) {\n q->vertices[q->rear] = vet;\n q->rear = (q->rear + 1) % MAX_SIZE;\n q->size++;\n}\n\n/* \u51fa\u961f\u64cd\u4f5c */\nVertex *dequeue(Queue *q) {\n Vertex *vet = q->vertices[q->front];\n q->front = (q->front + 1) % MAX_SIZE;\n q->size--;\n return vet;\n}\n\n/* \u68c0\u67e5\u9876\u70b9\u662f\u5426\u5df2\u88ab\u8bbf\u95ee */\nint isVisited(Vertex **visited, int size, Vertex *vet) {\n // \u904d\u5386\u67e5\u627e\u8282\u70b9\uff0c\u4f7f\u7528 O(n) \u65f6\u95f4\n for (int i = 0; i < size; i++) {\n if (visited[i] == vet)\n return 1;\n }\n return 0;\n}\n\n/* \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 BFS */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nvoid graphBFS(GraphAdjList *graph, Vertex *startVet, Vertex **res, int *resSize, Vertex **visited, int *visitedSize) {\n // \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\n Queue *queue = newQueue();\n enqueue(queue, startVet);\n visited[(*visitedSize)++] = startVet;\n // \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\n while (!isEmpty(queue)) {\n Vertex *vet = dequeue(queue); // \u961f\u9996\u9876\u70b9\u51fa\u961f\n res[(*resSize)++] = vet; // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n AdjListNode *node = findNode(graph, vet);\n while (node != NULL) {\n // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n if (!isVisited(visited, *visitedSize, node->vertex)) {\n enqueue(queue, node->vertex); // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\n visited[(*visitedSize)++] = node->vertex; // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n }\n node = node->next;\n }\n }\n // \u91ca\u653e\u5185\u5b58\n free(queue);\n}\n
graph_bfs.zig[class]{}-[func]{graphBFS}\n
\u4ee3\u7801\u76f8\u5bf9\u62bd\u8c61\uff0c\u5efa\u8bae\u5bf9\u7167\u56fe 9-10 \u6765\u52a0\u6df1\u7406\u89e3\u3002
<1><2><3><4><5><6><7><8><9><10><11>\u56fe 9-10 \u00a0 \u56fe\u7684\u5e7f\u5ea6\u4f18\u5148\u904d\u5386\u6b65\u9aa4
\u5e7f\u5ea6\u4f18\u5148\u904d\u5386\u7684\u5e8f\u5217\u662f\u5426\u552f\u4e00\uff1f
\u4e0d\u552f\u4e00\u3002\u5e7f\u5ea6\u4f18\u5148\u904d\u5386\u53ea\u8981\u6c42\u6309\u201c\u7531\u8fd1\u53ca\u8fdc\u201d\u7684\u987a\u5e8f\u904d\u5386\uff0c\u800c\u591a\u4e2a\u76f8\u540c\u8ddd\u79bb\u7684\u9876\u70b9\u7684\u904d\u5386\u987a\u5e8f\u5141\u8bb8\u88ab\u4efb\u610f\u6253\u4e71\u3002\u4ee5\u56fe 9-10 \u4e3a\u4f8b\uff0c\u9876\u70b9 \\(1\\)\u3001\\(3\\) \u7684\u8bbf\u95ee\u987a\u5e8f\u53ef\u4ee5\u4ea4\u6362\uff0c\u9876\u70b9 \\(2\\)\u3001\\(4\\)\u3001\\(6\\) \u7684\u8bbf\u95ee\u987a\u5e8f\u4e5f\u53ef\u4ee5\u4efb\u610f\u4ea4\u6362\u3002
"},{"location":"chapter_graph/graph_traversal/#2","title":"2. \u00a0 \u590d\u6742\u5ea6\u5206\u6790","text":"\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u6240\u6709\u9876\u70b9\u90fd\u4f1a\u5165\u961f\u5e76\u51fa\u961f\u4e00\u6b21\uff0c\u4f7f\u7528 \\(O(|V|)\\) \u65f6\u95f4\uff1b\u5728\u904d\u5386\u90bb\u63a5\u9876\u70b9\u7684\u8fc7\u7a0b\u4e2d\uff0c\u7531\u4e8e\u662f\u65e0\u5411\u56fe\uff0c\u56e0\u6b64\u6240\u6709\u8fb9\u90fd\u4f1a\u88ab\u8bbf\u95ee \\(2\\) \u6b21\uff0c\u4f7f\u7528 \\(O(2|E|)\\) \u65f6\u95f4\uff1b\u603b\u4f53\u4f7f\u7528 \\(O(|V| + |E|)\\) \u65f6\u95f4\u3002
\u7a7a\u95f4\u590d\u6742\u5ea6\uff1a\u5217\u8868 res
\uff0c\u54c8\u5e0c\u8868 visited
\uff0c\u961f\u5217 que
\u4e2d\u7684\u9876\u70b9\u6570\u91cf\u6700\u591a\u4e3a \\(|V|\\) \uff0c\u4f7f\u7528 \\(O(|V|)\\) \u7a7a\u95f4\u3002
\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u662f\u4e00\u79cd\u4f18\u5148\u8d70\u5230\u5e95\u3001\u65e0\u8def\u53ef\u8d70\u518d\u56de\u5934\u7684\u904d\u5386\u65b9\u5f0f\u3002\u5982\u56fe 9-11 \u6240\u793a\uff0c\u4ece\u5de6\u4e0a\u89d2\u9876\u70b9\u51fa\u53d1\uff0c\u8bbf\u95ee\u5f53\u524d\u9876\u70b9\u7684\u67d0\u4e2a\u90bb\u63a5\u9876\u70b9\uff0c\u76f4\u5230\u8d70\u5230\u5c3d\u5934\u65f6\u8fd4\u56de\uff0c\u518d\u7ee7\u7eed\u8d70\u5230\u5c3d\u5934\u5e76\u8fd4\u56de\uff0c\u4ee5\u6b64\u7c7b\u63a8\uff0c\u76f4\u81f3\u6240\u6709\u9876\u70b9\u904d\u5386\u5b8c\u6210\u3002
\u56fe 9-11 \u00a0 \u56fe\u7684\u6df1\u5ea6\u4f18\u5148\u904d\u5386
"},{"location":"chapter_graph/graph_traversal/#1_1","title":"1. \u00a0 \u7b97\u6cd5\u5b9e\u73b0","text":"\u8fd9\u79cd\u201c\u8d70\u5230\u5c3d\u5934\u518d\u8fd4\u56de\u201d\u7684\u7b97\u6cd5\u8303\u5f0f\u901a\u5e38\u57fa\u4e8e\u9012\u5f52\u6765\u5b9e\u73b0\u3002\u4e0e\u5e7f\u5ea6\u4f18\u5148\u904d\u5386\u7c7b\u4f3c\uff0c\u5728\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u4e2d\uff0c\u6211\u4eec\u4e5f\u9700\u8981\u501f\u52a9\u4e00\u4e2a\u54c8\u5e0c\u8868 visited
\u6765\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\uff0c\u4ee5\u907f\u514d\u91cd\u590d\u8bbf\u95ee\u9876\u70b9\u3002
def dfs(graph: GraphAdjList, visited: set[Vertex], res: list[Vertex], vet: Vertex):\n \"\"\"\u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS \u8f85\u52a9\u51fd\u6570\"\"\"\n res.append(vet) # \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n visited.add(vet) # \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n # \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n for adjVet in graph.adj_list[vet]:\n if adjVet in visited:\n continue # \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n # \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\n dfs(graph, visited, res, adjVet)\n\ndef graph_dfs(graph: GraphAdjList, start_vet: Vertex) -> list[Vertex]:\n \"\"\"\u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS\"\"\"\n # \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n # \u9876\u70b9\u904d\u5386\u5e8f\u5217\n res = []\n # \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n visited = set[Vertex]()\n dfs(graph, visited, res, start_vet)\n return res\n
graph_dfs.cpp/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS \u8f85\u52a9\u51fd\u6570 */\nvoid dfs(GraphAdjList &graph, unordered_set<Vertex *> &visited, vector<Vertex *> &res, Vertex *vet) {\n res.push_back(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n visited.emplace(vet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n for (Vertex *adjVet : graph.adjList[vet]) {\n if (visited.count(adjVet))\n continue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n // \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\n dfs(graph, visited, res, adjVet);\n }\n}\n\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nvector<Vertex *> graphDFS(GraphAdjList &graph, Vertex *startVet) {\n // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n vector<Vertex *> res;\n // \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n unordered_set<Vertex *> visited;\n dfs(graph, visited, res, startVet);\n return res;\n}\n
graph_dfs.java/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS \u8f85\u52a9\u51fd\u6570 */\nvoid dfs(GraphAdjList graph, Set<Vertex> visited, List<Vertex> res, Vertex vet) {\n res.add(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n visited.add(vet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n for (Vertex adjVet : graph.adjList.get(vet)) {\n if (visited.contains(adjVet))\n continue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n // \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\n dfs(graph, visited, res, adjVet);\n }\n}\n\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nList<Vertex> graphDFS(GraphAdjList graph, Vertex startVet) {\n // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n List<Vertex> res = new ArrayList<>();\n // \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n Set<Vertex> visited = new HashSet<>();\n dfs(graph, visited, res, startVet);\n return res;\n}\n
graph_dfs.cs/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS \u8f85\u52a9\u51fd\u6570 */\nvoid DFS(GraphAdjList graph, HashSet<Vertex> visited, List<Vertex> res, Vertex vet) {\n res.Add(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n visited.Add(vet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n foreach (Vertex adjVet in graph.adjList[vet]) {\n if (visited.Contains(adjVet)) {\n continue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9 \n }\n // \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\n DFS(graph, visited, res, adjVet);\n }\n}\n\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nList<Vertex> GraphDFS(GraphAdjList graph, Vertex startVet) {\n // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n List<Vertex> res = [];\n // \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n HashSet<Vertex> visited = [];\n DFS(graph, visited, res, startVet);\n return res;\n}\n
graph_dfs.go/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS \u8f85\u52a9\u51fd\u6570 */\nfunc dfs(g *graphAdjList, visited map[Vertex]struct{}, res *[]Vertex, vet Vertex) {\n // append \u64cd\u4f5c\u4f1a\u8fd4\u56de\u65b0\u7684\u7684\u5f15\u7528\uff0c\u5fc5\u987b\u8ba9\u539f\u5f15\u7528\u91cd\u65b0\u8d4b\u503c\u4e3a\u65b0slice\u7684\u5f15\u7528\n *res = append(*res, vet)\n visited[vet] = struct{}{}\n // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n for _, adjVet := range g.adjList[vet] {\n _, isExist := visited[adjVet]\n // \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\n if !isExist {\n dfs(g, visited, res, adjVet)\n }\n }\n}\n\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfunc graphDFS(g *graphAdjList, startVet Vertex) []Vertex {\n // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n res := make([]Vertex, 0)\n // \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n visited := make(map[Vertex]struct{})\n dfs(g, visited, &res, startVet)\n // \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\n return res\n}\n
graph_dfs.swift/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS \u8f85\u52a9\u51fd\u6570 */\nfunc dfs(graph: GraphAdjList, visited: inout Set<Vertex>, res: inout [Vertex], vet: Vertex) {\n res.append(vet) // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n visited.insert(vet) // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n for adjVet in graph.adjList[vet] ?? [] {\n if visited.contains(adjVet) {\n continue // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n }\n // \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\n dfs(graph: graph, visited: &visited, res: &res, vet: adjVet)\n }\n}\n\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfunc graphDFS(graph: GraphAdjList, startVet: Vertex) -> [Vertex] {\n // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n var res: [Vertex] = []\n // \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n var visited: Set<Vertex> = []\n dfs(graph: graph, visited: &visited, res: &res, vet: startVet)\n return res\n}\n
graph_dfs.js/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfunction dfs(graph, visited, res, vet) {\n res.push(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n visited.add(vet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n for (const adjVet of graph.adjList.get(vet)) {\n if (visited.has(adjVet)) {\n continue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n }\n // \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\n dfs(graph, visited, res, adjVet);\n }\n}\n\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfunction graphDFS(graph, startVet) {\n // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n const res = [];\n // \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n const visited = new Set();\n dfs(graph, visited, res, startVet);\n return res;\n}\n
graph_dfs.ts/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS \u8f85\u52a9\u51fd\u6570 */\nfunction dfs(\n graph: GraphAdjList,\n visited: Set<Vertex>,\n res: Vertex[],\n vet: Vertex\n): void {\n res.push(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n visited.add(vet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n for (const adjVet of graph.adjList.get(vet)) {\n if (visited.has(adjVet)) {\n continue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n }\n // \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\n dfs(graph, visited, res, adjVet);\n }\n}\n\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfunction graphDFS(graph: GraphAdjList, startVet: Vertex): Vertex[] {\n // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n const res: Vertex[] = [];\n // \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n const visited: Set<Vertex> = new Set();\n dfs(graph, visited, res, startVet);\n return res;\n}\n
graph_dfs.dart/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS \u8f85\u52a9\u51fd\u6570 */\nvoid dfs(\n GraphAdjList graph,\n Set<Vertex> visited,\n List<Vertex> res,\n Vertex vet,\n) {\n res.add(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n visited.add(vet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n for (Vertex adjVet in graph.adjList[vet]!) {\n if (visited.contains(adjVet)) {\n continue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n }\n // \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\n dfs(graph, visited, res, adjVet);\n }\n}\n\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS */\nList<Vertex> graphDFS(GraphAdjList graph, Vertex startVet) {\n // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n List<Vertex> res = [];\n // \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n Set<Vertex> visited = {};\n dfs(graph, visited, res, startVet);\n return res;\n}\n
graph_dfs.rs/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS \u8f85\u52a9\u51fd\u6570 */\nfn dfs(graph: &GraphAdjList, visited: &mut HashSet<Vertex>, res: &mut Vec<Vertex>, vet: Vertex) {\n res.push(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n visited.insert(vet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n if let Some(adj_vets) = graph.adj_list.get(&vet) {\n for &adj_vet in adj_vets {\n if visited.contains(&adj_vet) {\n continue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n }\n // \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\n dfs(graph, visited, res, adj_vet);\n }\n }\n}\n\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfn graph_dfs(graph: GraphAdjList, start_vet: Vertex) -> Vec<Vertex> {\n // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n let mut res = vec![];\n // \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n let mut visited = HashSet::new();\n dfs(&graph, &mut visited, &mut res, start_vet);\n\n res\n}\n
graph_dfs.c/* \u68c0\u67e5\u9876\u70b9\u662f\u5426\u5df2\u88ab\u8bbf\u95ee */\nint isVisited(Vertex **res, int size, Vertex *vet) {\n // \u904d\u5386\u67e5\u627e\u8282\u70b9\uff0c\u4f7f\u7528 O(n) \u65f6\u95f4\n for (int i = 0; i < size; i++) {\n if (res[i] == vet) {\n return 1;\n }\n }\n return 0;\n}\n\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS \u8f85\u52a9\u51fd\u6570 */\nvoid dfs(GraphAdjList *graph, Vertex **res, int *resSize, Vertex *vet) {\n // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n res[(*resSize)++] = vet;\n // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n AdjListNode *node = findNode(graph, vet);\n while (node != NULL) {\n // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n if (!isVisited(res, *resSize, node->vertex)) {\n // \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\n dfs(graph, res, resSize, node->vertex);\n }\n node = node->next;\n }\n}\n\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nvoid graphDFS(GraphAdjList *graph, Vertex *startVet, Vertex **res, int *resSize) {\n dfs(graph, res, resSize, startVet);\n}\n
graph_dfs.zig[class]{}-[func]{dfs}\n\n[class]{}-[func]{graphDFS}\n
\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u7684\u7b97\u6cd5\u6d41\u7a0b\u5982\u56fe 9-12 \u6240\u793a\u3002
\u4e3a\u4e86\u52a0\u6df1\u7406\u89e3\uff0c\u5efa\u8bae\u5c06\u56fe 9-12 \u4e0e\u4ee3\u7801\u7ed3\u5408\u8d77\u6765\uff0c\u5728\u8111\u4e2d\u6a21\u62df\uff08\u6216\u8005\u7528\u7b14\u753b\u4e0b\u6765\uff09\u6574\u4e2a DFS \u8fc7\u7a0b\uff0c\u5305\u62ec\u6bcf\u4e2a\u9012\u5f52\u65b9\u6cd5\u4f55\u65f6\u5f00\u542f\u3001\u4f55\u65f6\u8fd4\u56de\u3002
<1><2><3><4><5><6><7><8><9><10><11>\u56fe 9-12 \u00a0 \u56fe\u7684\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u6b65\u9aa4
\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u7684\u5e8f\u5217\u662f\u5426\u552f\u4e00\uff1f
\u4e0e\u5e7f\u5ea6\u4f18\u5148\u904d\u5386\u7c7b\u4f3c\uff0c\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u5e8f\u5217\u7684\u987a\u5e8f\u4e5f\u4e0d\u662f\u552f\u4e00\u7684\u3002\u7ed9\u5b9a\u67d0\u9876\u70b9\uff0c\u5148\u5f80\u54ea\u4e2a\u65b9\u5411\u63a2\u7d22\u90fd\u53ef\u4ee5\uff0c\u5373\u90bb\u63a5\u9876\u70b9\u7684\u987a\u5e8f\u53ef\u4ee5\u4efb\u610f\u6253\u4e71\uff0c\u90fd\u662f\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u3002
\u4ee5\u6811\u7684\u904d\u5386\u4e3a\u4f8b\uff0c\u201c\u6839 \\(\\rightarrow\\) \u5de6 \\(\\rightarrow\\) \u53f3\u201d\u201c\u5de6 \\(\\rightarrow\\) \u6839 \\(\\rightarrow\\) \u53f3\u201d\u201c\u5de6 \\(\\rightarrow\\) \u53f3 \\(\\rightarrow\\) \u6839\u201d\u5206\u522b\u5bf9\u5e94\u524d\u5e8f\u3001\u4e2d\u5e8f\u3001\u540e\u5e8f\u904d\u5386\uff0c\u5b83\u4eec\u5c55\u793a\u4e86\u4e09\u79cd\u904d\u5386\u4f18\u5148\u7ea7\uff0c\u7136\u800c\u8fd9\u4e09\u8005\u90fd\u5c5e\u4e8e\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u3002
"},{"location":"chapter_graph/graph_traversal/#2_1","title":"2. \u00a0 \u590d\u6742\u5ea6\u5206\u6790","text":"\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u6240\u6709\u9876\u70b9\u90fd\u4f1a\u88ab\u8bbf\u95ee \\(1\\) \u6b21\uff0c\u4f7f\u7528 \\(O(|V|)\\) \u65f6\u95f4\uff1b\u6240\u6709\u8fb9\u90fd\u4f1a\u88ab\u8bbf\u95ee \\(2\\) \u6b21\uff0c\u4f7f\u7528 \\(O(2|E|)\\) \u65f6\u95f4\uff1b\u603b\u4f53\u4f7f\u7528 \\(O(|V| + |E|)\\) \u65f6\u95f4\u3002
\u7a7a\u95f4\u590d\u6742\u5ea6\uff1a\u5217\u8868 res
\uff0c\u54c8\u5e0c\u8868 visited
\u9876\u70b9\u6570\u91cf\u6700\u591a\u4e3a \\(|V|\\) \uff0c\u9012\u5f52\u6df1\u5ea6\u6700\u5927\u4e3a \\(|V|\\) \uff0c\u56e0\u6b64\u4f7f\u7528 \\(O(|V|)\\) \u7a7a\u95f4\u3002
\u8def\u5f84\u7684\u5b9a\u4e49\u662f\u9876\u70b9\u5e8f\u5217\u8fd8\u662f\u8fb9\u5e8f\u5217\uff1f
\u7ef4\u57fa\u767e\u79d1\u4e0a\u4e0d\u540c\u8bed\u8a00\u7248\u672c\u7684\u5b9a\u4e49\u4e0d\u4e00\u81f4\uff1a\u82f1\u6587\u7248\u662f\u201c\u8def\u5f84\u662f\u4e00\u4e2a\u8fb9\u5e8f\u5217\u201d\uff0c\u800c\u4e2d\u6587\u7248\u662f\u201c\u8def\u5f84\u662f\u4e00\u4e2a\u9876\u70b9\u5e8f\u5217\u201d\u3002\u4ee5\u4e0b\u662f\u82f1\u6587\u7248\u539f\u6587\uff1aIn graph theory, a path in a graph is a finite or infinite sequence of edges which joins a sequence of vertices. \u5728\u672c\u6587\u4e2d\uff0c\u8def\u5f84\u88ab\u89c6\u4e3a\u4e00\u4e2a\u8fb9\u5e8f\u5217\uff0c\u800c\u4e0d\u662f\u4e00\u4e2a\u9876\u70b9\u5e8f\u5217\u3002\u8fd9\u662f\u56e0\u4e3a\u4e24\u4e2a\u9876\u70b9\u4e4b\u95f4\u53ef\u80fd\u5b58\u5728\u591a\u6761\u8fb9\u8fde\u63a5\uff0c\u6b64\u65f6\u6bcf\u6761\u8fb9\u90fd\u5bf9\u5e94\u4e00\u6761\u8def\u5f84\u3002
\u975e\u8fde\u901a\u56fe\u4e2d\u662f\u5426\u4f1a\u6709\u65e0\u6cd5\u904d\u5386\u5230\u7684\u70b9\uff1f
\u5728\u975e\u8fde\u901a\u56fe\u4e2d\uff0c\u4ece\u67d0\u4e2a\u9876\u70b9\u51fa\u53d1\uff0c\u81f3\u5c11\u6709\u4e00\u4e2a\u9876\u70b9\u65e0\u6cd5\u5230\u8fbe\u3002\u904d\u5386\u975e\u8fde\u901a\u56fe\u9700\u8981\u8bbe\u7f6e\u591a\u4e2a\u8d77\u70b9\uff0c\u4ee5\u904d\u5386\u5230\u56fe\u7684\u6240\u6709\u8fde\u901a\u5206\u91cf\u3002
\u5728\u90bb\u63a5\u8868\u4e2d\uff0c\u201c\u4e0e\u8be5\u9876\u70b9\u76f8\u8fde\u7684\u6240\u6709\u9876\u70b9\u201d\u7684\u9876\u70b9\u987a\u5e8f\u662f\u5426\u6709\u8981\u6c42\uff1f
\u53ef\u4ee5\u662f\u4efb\u610f\u987a\u5e8f\u3002\u4f46\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u6309\u7167\u6307\u5b9a\u89c4\u5219\u6765\u6392\u5e8f\uff0c\u6bd4\u5982\u6309\u7167\u9876\u70b9\u6dfb\u52a0\u7684\u6b21\u5e8f\uff0c\u6216\u8005\u6309\u7167\u9876\u70b9\u503c\u5927\u5c0f\u7684\u987a\u5e8f\u7b49\uff0c\u8fd9\u6837\u6709\u52a9\u4e8e\u5feb\u901f\u67e5\u627e\u201c\u5e26\u6709\u67d0\u79cd\u6781\u503c\u201d\u7684\u9876\u70b9\u3002
"},{"location":"chapter_greedy/","title":"\u7b2c 15 \u7ae0 \u00a0 \u8d2a\u5fc3","text":"Abstract
\u5411\u65e5\u8475\u671d\u7740\u592a\u9633\u8f6c\u52a8\uff0c\u65f6\u523b\u8ffd\u6c42\u81ea\u8eab\u6210\u957f\u7684\u6700\u5927\u53ef\u80fd\u3002
\u8d2a\u5fc3\u7b56\u7565\u5728\u4e00\u8f6e\u8f6e\u7684\u7b80\u5355\u9009\u62e9\u4e2d\uff0c\u9010\u6b65\u5bfc\u5411\u6700\u4f73\u7b54\u6848\u3002
"},{"location":"chapter_greedy/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"Question
\u7ed9\u5b9a \\(n\\) \u4e2a\u7269\u54c1\uff0c\u7b2c \\(i\\) \u4e2a\u7269\u54c1\u7684\u91cd\u91cf\u4e3a \\(wgt[i-1]\\)\u3001\u4ef7\u503c\u4e3a \\(val[i-1]\\) \uff0c\u548c\u4e00\u4e2a\u5bb9\u91cf\u4e3a \\(cap\\) \u7684\u80cc\u5305\u3002\u6bcf\u4e2a\u7269\u54c1\u53ea\u80fd\u9009\u62e9\u4e00\u6b21\uff0c\u4f46\u53ef\u4ee5\u9009\u62e9\u7269\u54c1\u7684\u4e00\u90e8\u5206\uff0c\u4ef7\u503c\u6839\u636e\u9009\u62e9\u7684\u91cd\u91cf\u6bd4\u4f8b\u8ba1\u7b97\uff0c\u95ee\u5728\u9650\u5b9a\u80cc\u5305\u5bb9\u91cf\u4e0b\u80cc\u5305\u4e2d\u7269\u54c1\u7684\u6700\u5927\u4ef7\u503c\u3002\u793a\u4f8b\u5982\u56fe 15-3 \u6240\u793a\u3002
\u56fe 15-3 \u00a0 \u5206\u6570\u80cc\u5305\u95ee\u9898\u7684\u793a\u4f8b\u6570\u636e
\u5206\u6570\u80cc\u5305\u95ee\u9898\u548c 0-1 \u80cc\u5305\u95ee\u9898\u6574\u4f53\u4e0a\u975e\u5e38\u76f8\u4f3c\uff0c\u72b6\u6001\u5305\u542b\u5f53\u524d\u7269\u54c1 \\(i\\) \u548c\u5bb9\u91cf \\(c\\) \uff0c\u76ee\u6807\u662f\u6c42\u9650\u5b9a\u80cc\u5305\u5bb9\u91cf\u4e0b\u7684\u6700\u5927\u4ef7\u503c\u3002
\u4e0d\u540c\u70b9\u5728\u4e8e\uff0c\u672c\u9898\u5141\u8bb8\u53ea\u9009\u62e9\u7269\u54c1\u7684\u4e00\u90e8\u5206\u3002\u5982\u56fe 15-4 \u6240\u793a\uff0c\u6211\u4eec\u53ef\u4ee5\u5bf9\u7269\u54c1\u4efb\u610f\u5730\u8fdb\u884c\u5207\u5206\uff0c\u5e76\u6309\u7167\u91cd\u91cf\u6bd4\u4f8b\u6765\u8ba1\u7b97\u76f8\u5e94\u4ef7\u503c\u3002
\u56fe 15-4 \u00a0 \u7269\u54c1\u5728\u5355\u4f4d\u91cd\u91cf\u4e0b\u7684\u4ef7\u503c
"},{"location":"chapter_greedy/fractional_knapsack_problem/#1","title":"1. \u00a0 \u8d2a\u5fc3\u7b56\u7565\u786e\u5b9a","text":"\u6700\u5927\u5316\u80cc\u5305\u5185\u7269\u54c1\u603b\u4ef7\u503c\uff0c\u672c\u8d28\u4e0a\u662f\u6700\u5927\u5316\u5355\u4f4d\u91cd\u91cf\u4e0b\u7684\u7269\u54c1\u4ef7\u503c\u3002\u7531\u6b64\u4fbf\u53ef\u63a8\u7406\u51fa\u56fe 15-5 \u6240\u793a\u7684\u8d2a\u5fc3\u7b56\u7565\u3002
\u56fe 15-5 \u00a0 \u5206\u6570\u80cc\u5305\u95ee\u9898\u7684\u8d2a\u5fc3\u7b56\u7565
"},{"location":"chapter_greedy/fractional_knapsack_problem/#2","title":"2. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"\u6211\u4eec\u5efa\u7acb\u4e86\u4e00\u4e2a\u7269\u54c1\u7c7b Item
\uff0c\u4ee5\u4fbf\u5c06\u7269\u54c1\u6309\u7167\u5355\u4f4d\u4ef7\u503c\u8fdb\u884c\u6392\u5e8f\u3002\u5faa\u73af\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u5f53\u80cc\u5305\u5df2\u6ee1\u65f6\u8df3\u51fa\u5e76\u8fd4\u56de\u89e3\uff1a
class Item:\n \"\"\"\u7269\u54c1\"\"\"\n\n def __init__(self, w: int, v: int):\n self.w = w # \u7269\u54c1\u91cd\u91cf\n self.v = v # \u7269\u54c1\u4ef7\u503c\n\ndef fractional_knapsack(wgt: list[int], val: list[int], cap: int) -> int:\n \"\"\"\u5206\u6570\u80cc\u5305\uff1a\u8d2a\u5fc3\"\"\"\n # \u521b\u5efa\u7269\u54c1\u5217\u8868\uff0c\u5305\u542b\u4e24\u4e2a\u5c5e\u6027\uff1a\u91cd\u91cf\u3001\u4ef7\u503c\n items = [Item(w, v) for w, v in zip(wgt, val)]\n # \u6309\u7167\u5355\u4f4d\u4ef7\u503c item.v / item.w \u4ece\u9ad8\u5230\u4f4e\u8fdb\u884c\u6392\u5e8f\n items.sort(key=lambda item: item.v / item.w, reverse=True)\n # \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\n res = 0\n for item in items:\n if item.w <= cap:\n # \u82e5\u5269\u4f59\u5bb9\u91cf\u5145\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u6574\u4e2a\u88c5\u8fdb\u80cc\u5305\n res += item.v\n cap -= item.w\n else:\n # \u82e5\u5269\u4f59\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88c5\u8fdb\u80cc\u5305\n res += (item.v / item.w) * cap\n # \u5df2\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u56e0\u6b64\u8df3\u51fa\u5faa\u73af\n break\n return res\n
fractional_knapsack.cpp/* \u7269\u54c1 */\nclass Item {\n public:\n int w; // \u7269\u54c1\u91cd\u91cf\n int v; // \u7269\u54c1\u4ef7\u503c\n\n Item(int w, int v) : w(w), v(v) {\n }\n};\n\n/* \u5206\u6570\u80cc\u5305\uff1a\u8d2a\u5fc3 */\ndouble fractionalKnapsack(vector<int> &wgt, vector<int> &val, int cap) {\n // \u521b\u5efa\u7269\u54c1\u5217\u8868\uff0c\u5305\u542b\u4e24\u4e2a\u5c5e\u6027\uff1a\u91cd\u91cf\u3001\u4ef7\u503c\n vector<Item> items;\n for (int i = 0; i < wgt.size(); i++) {\n items.push_back(Item(wgt[i], val[i]));\n }\n // \u6309\u7167\u5355\u4f4d\u4ef7\u503c item.v / item.w \u4ece\u9ad8\u5230\u4f4e\u8fdb\u884c\u6392\u5e8f\n sort(items.begin(), items.end(), [](Item &a, Item &b) { return (double)a.v / a.w > (double)b.v / b.w; });\n // \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\n double res = 0;\n for (auto &item : items) {\n if (item.w <= cap) {\n // \u82e5\u5269\u4f59\u5bb9\u91cf\u5145\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u6574\u4e2a\u88c5\u8fdb\u80cc\u5305\n res += item.v;\n cap -= item.w;\n } else {\n // \u82e5\u5269\u4f59\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88c5\u8fdb\u80cc\u5305\n res += (double)item.v / item.w * cap;\n // \u5df2\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u56e0\u6b64\u8df3\u51fa\u5faa\u73af\n break;\n }\n }\n return res;\n}\n
fractional_knapsack.java/* \u7269\u54c1 */\nclass Item {\n int w; // \u7269\u54c1\u91cd\u91cf\n int v; // \u7269\u54c1\u4ef7\u503c\n\n public Item(int w, int v) {\n this.w = w;\n this.v = v;\n }\n}\n\n/* \u5206\u6570\u80cc\u5305\uff1a\u8d2a\u5fc3 */\ndouble fractionalKnapsack(int[] wgt, int[] val, int cap) {\n // \u521b\u5efa\u7269\u54c1\u5217\u8868\uff0c\u5305\u542b\u4e24\u4e2a\u5c5e\u6027\uff1a\u91cd\u91cf\u3001\u4ef7\u503c\n Item[] items = new Item[wgt.length];\n for (int i = 0; i < wgt.length; i++) {\n items[i] = new Item(wgt[i], val[i]);\n }\n // \u6309\u7167\u5355\u4f4d\u4ef7\u503c item.v / item.w \u4ece\u9ad8\u5230\u4f4e\u8fdb\u884c\u6392\u5e8f\n Arrays.sort(items, Comparator.comparingDouble(item -> -((double) item.v / item.w)));\n // \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\n double res = 0;\n for (Item item : items) {\n if (item.w <= cap) {\n // \u82e5\u5269\u4f59\u5bb9\u91cf\u5145\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u6574\u4e2a\u88c5\u8fdb\u80cc\u5305\n res += item.v;\n cap -= item.w;\n } else {\n // \u82e5\u5269\u4f59\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88c5\u8fdb\u80cc\u5305\n res += (double) item.v / item.w * cap;\n // \u5df2\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u56e0\u6b64\u8df3\u51fa\u5faa\u73af\n break;\n }\n }\n return res;\n}\n
fractional_knapsack.cs/* \u7269\u54c1 */\nclass Item(int w, int v) {\n public int w = w; // \u7269\u54c1\u91cd\u91cf\n public int v = v; // \u7269\u54c1\u4ef7\u503c\n}\n\n/* \u5206\u6570\u80cc\u5305\uff1a\u8d2a\u5fc3 */\ndouble FractionalKnapsack(int[] wgt, int[] val, int cap) {\n // \u521b\u5efa\u7269\u54c1\u5217\u8868\uff0c\u5305\u542b\u4e24\u4e2a\u5c5e\u6027\uff1a\u91cd\u91cf\u3001\u4ef7\u503c\n Item[] items = new Item[wgt.Length];\n for (int i = 0; i < wgt.Length; i++) {\n items[i] = new Item(wgt[i], val[i]);\n }\n // \u6309\u7167\u5355\u4f4d\u4ef7\u503c item.v / item.w \u4ece\u9ad8\u5230\u4f4e\u8fdb\u884c\u6392\u5e8f\n Array.Sort(items, (x, y) => (y.v / y.w).CompareTo(x.v / x.w));\n // \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\n double res = 0;\n foreach (Item item in items) {\n if (item.w <= cap) {\n // \u82e5\u5269\u4f59\u5bb9\u91cf\u5145\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u6574\u4e2a\u88c5\u8fdb\u80cc\u5305\n res += item.v;\n cap -= item.w;\n } else {\n // \u82e5\u5269\u4f59\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88c5\u8fdb\u80cc\u5305\n res += (double)item.v / item.w * cap;\n // \u5df2\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u56e0\u6b64\u8df3\u51fa\u5faa\u73af\n break;\n }\n }\n return res;\n}\n
fractional_knapsack.go/* \u7269\u54c1 */\ntype Item struct {\n w int // \u7269\u54c1\u91cd\u91cf\n v int // \u7269\u54c1\u4ef7\u503c\n}\n\n/* \u5206\u6570\u80cc\u5305\uff1a\u8d2a\u5fc3 */\nfunc fractionalKnapsack(wgt []int, val []int, cap int) float64 {\n // \u521b\u5efa\u7269\u54c1\u5217\u8868\uff0c\u5305\u542b\u4e24\u4e2a\u5c5e\u6027\uff1a\u91cd\u91cf\u3001\u4ef7\u503c\n items := make([]Item, len(wgt))\n for i := 0; i < len(wgt); i++ {\n items[i] = Item{wgt[i], val[i]}\n }\n // \u6309\u7167\u5355\u4f4d\u4ef7\u503c item.v / item.w \u4ece\u9ad8\u5230\u4f4e\u8fdb\u884c\u6392\u5e8f\n sort.Slice(items, func(i, j int) bool {\n return float64(items[i].v)/float64(items[i].w) > float64(items[j].v)/float64(items[j].w)\n })\n // \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\n res := 0.0\n for _, item := range items {\n if item.w <= cap {\n // \u82e5\u5269\u4f59\u5bb9\u91cf\u5145\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u6574\u4e2a\u88c5\u8fdb\u80cc\u5305\n res += float64(item.v)\n cap -= item.w\n } else {\n // \u82e5\u5269\u4f59\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88c5\u8fdb\u80cc\u5305\n res += float64(item.v) / float64(item.w) * float64(cap)\n // \u5df2\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u56e0\u6b64\u8df3\u51fa\u5faa\u73af\n break\n }\n }\n return res\n}\n
fractional_knapsack.swift/* \u7269\u54c1 */\nclass Item {\n var w: Int // \u7269\u54c1\u91cd\u91cf\n var v: Int // \u7269\u54c1\u4ef7\u503c\n\n init(w: Int, v: Int) {\n self.w = w\n self.v = v\n }\n}\n\n/* \u5206\u6570\u80cc\u5305\uff1a\u8d2a\u5fc3 */\nfunc fractionalKnapsack(wgt: [Int], val: [Int], cap: Int) -> Double {\n // \u521b\u5efa\u7269\u54c1\u5217\u8868\uff0c\u5305\u542b\u4e24\u4e2a\u5c5e\u6027\uff1a\u91cd\u91cf\u3001\u4ef7\u503c\n var items = zip(wgt, val).map { Item(w: $0, v: $1) }\n // \u6309\u7167\u5355\u4f4d\u4ef7\u503c item.v / item.w \u4ece\u9ad8\u5230\u4f4e\u8fdb\u884c\u6392\u5e8f\n items.sort(by: { -(Double($0.v) / Double($0.w)) < -(Double($1.v) / Double($1.w)) })\n // \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\n var res = 0.0\n var cap = cap\n for item in items {\n if item.w <= cap {\n // \u82e5\u5269\u4f59\u5bb9\u91cf\u5145\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u6574\u4e2a\u88c5\u8fdb\u80cc\u5305\n res += Double(item.v)\n cap -= item.w\n } else {\n // \u82e5\u5269\u4f59\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88c5\u8fdb\u80cc\u5305\n res += Double(item.v) / Double(item.w) * Double(cap)\n // \u5df2\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u56e0\u6b64\u8df3\u51fa\u5faa\u73af\n break\n }\n }\n return res\n}\n
fractional_knapsack.js/* \u7269\u54c1 */\nclass Item {\n constructor(w, v) {\n this.w = w; // \u7269\u54c1\u91cd\u91cf\n this.v = v; // \u7269\u54c1\u4ef7\u503c\n }\n}\n\n/* \u5206\u6570\u80cc\u5305\uff1a\u8d2a\u5fc3 */\nfunction fractionalKnapsack(wgt, val, cap) {\n // \u521b\u5efa\u7269\u54c1\u5217\u8868\uff0c\u5305\u542b\u4e24\u4e2a\u5c5e\u6027\uff1a\u91cd\u91cf\u3001\u4ef7\u503c\n const items = wgt.map((w, i) => new Item(w, val[i]));\n // \u6309\u7167\u5355\u4f4d\u4ef7\u503c item.v / item.w \u4ece\u9ad8\u5230\u4f4e\u8fdb\u884c\u6392\u5e8f\n items.sort((a, b) => b.v / b.w - a.v / a.w);\n // \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\n let res = 0;\n for (const item of items) {\n if (item.w <= cap) {\n // \u82e5\u5269\u4f59\u5bb9\u91cf\u5145\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u6574\u4e2a\u88c5\u8fdb\u80cc\u5305\n res += item.v;\n cap -= item.w;\n } else {\n // \u82e5\u5269\u4f59\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88c5\u8fdb\u80cc\u5305\n res += (item.v / item.w) * cap;\n // \u5df2\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u56e0\u6b64\u8df3\u51fa\u5faa\u73af\n break;\n }\n }\n return res;\n}\n
fractional_knapsack.ts/* \u7269\u54c1 */\nclass Item {\n w: number; // \u7269\u54c1\u91cd\u91cf\n v: number; // \u7269\u54c1\u4ef7\u503c\n\n constructor(w: number, v: number) {\n this.w = w;\n this.v = v;\n }\n}\n\n/* \u5206\u6570\u80cc\u5305\uff1a\u8d2a\u5fc3 */\nfunction fractionalKnapsack(wgt: number[], val: number[], cap: number): number {\n // \u521b\u5efa\u7269\u54c1\u5217\u8868\uff0c\u5305\u542b\u4e24\u4e2a\u5c5e\u6027\uff1a\u91cd\u91cf\u3001\u4ef7\u503c\n const items: Item[] = wgt.map((w, i) => new Item(w, val[i]));\n // \u6309\u7167\u5355\u4f4d\u4ef7\u503c item.v / item.w \u4ece\u9ad8\u5230\u4f4e\u8fdb\u884c\u6392\u5e8f\n items.sort((a, b) => b.v / b.w - a.v / a.w);\n // \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\n let res = 0;\n for (const item of items) {\n if (item.w <= cap) {\n // \u82e5\u5269\u4f59\u5bb9\u91cf\u5145\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u6574\u4e2a\u88c5\u8fdb\u80cc\u5305\n res += item.v;\n cap -= item.w;\n } else {\n // \u82e5\u5269\u4f59\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88c5\u8fdb\u80cc\u5305\n res += (item.v / item.w) * cap;\n // \u5df2\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u56e0\u6b64\u8df3\u51fa\u5faa\u73af\n break;\n }\n }\n return res;\n}\n
fractional_knapsack.dart/* \u7269\u54c1 */\nclass Item {\n int w; // \u7269\u54c1\u91cd\u91cf\n int v; // \u7269\u54c1\u4ef7\u503c\n\n Item(this.w, this.v);\n}\n\n/* \u5206\u6570\u80cc\u5305\uff1a\u8d2a\u5fc3 */\ndouble fractionalKnapsack(List<int> wgt, List<int> val, int cap) {\n // \u521b\u5efa\u7269\u54c1\u5217\u8868\uff0c\u5305\u542b\u4e24\u4e2a\u5c5e\u6027\uff1a\u91cd\u91cf\u3001\u4ef7\u503c\n List<Item> items = List.generate(wgt.length, (i) => Item(wgt[i], val[i]));\n // \u6309\u7167\u5355\u4f4d\u4ef7\u503c item.v / item.w \u4ece\u9ad8\u5230\u4f4e\u8fdb\u884c\u6392\u5e8f\n items.sort((a, b) => (b.v / b.w).compareTo(a.v / a.w));\n // \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\n double res = 0;\n for (Item item in items) {\n if (item.w <= cap) {\n // \u82e5\u5269\u4f59\u5bb9\u91cf\u5145\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u6574\u4e2a\u88c5\u8fdb\u80cc\u5305\n res += item.v;\n cap -= item.w;\n } else {\n // \u82e5\u5269\u4f59\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88c5\u8fdb\u80cc\u5305\n res += item.v / item.w * cap;\n // \u5df2\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u56e0\u6b64\u8df3\u51fa\u5faa\u73af\n break;\n }\n }\n return res;\n}\n
fractional_knapsack.rs/* \u7269\u54c1 */\nstruct Item {\n w: i32, // \u7269\u54c1\u91cd\u91cf\n v: i32, // \u7269\u54c1\u4ef7\u503c\n}\n\nimpl Item {\n fn new(w: i32, v: i32) -> Self {\n Self { w, v }\n }\n}\n\n/* \u5206\u6570\u80cc\u5305\uff1a\u8d2a\u5fc3 */\nfn fractional_knapsack(wgt: &[i32], val: &[i32], mut cap: i32) -> f64 {\n // \u521b\u5efa\u7269\u54c1\u5217\u8868\uff0c\u5305\u542b\u4e24\u4e2a\u5c5e\u6027\uff1a\u91cd\u91cf\u3001\u4ef7\u503c\n let mut items = wgt\n .iter()\n .zip(val.iter())\n .map(|(&w, &v)| Item::new(w, v))\n .collect::<Vec<Item>>();\n // \u6309\u7167\u5355\u4f4d\u4ef7\u503c item.v / item.w \u4ece\u9ad8\u5230\u4f4e\u8fdb\u884c\u6392\u5e8f\n items.sort_by(|a, b| {\n (b.v as f64 / b.w as f64)\n .partial_cmp(&(a.v as f64 / a.w as f64))\n .unwrap()\n });\n // \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\n let mut res = 0.0;\n for item in &items {\n if item.w <= cap {\n // \u82e5\u5269\u4f59\u5bb9\u91cf\u5145\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u6574\u4e2a\u88c5\u8fdb\u80cc\u5305\n res += item.v as f64;\n cap -= item.w;\n } else {\n // \u82e5\u5269\u4f59\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88c5\u8fdb\u80cc\u5305\n res += item.v as f64 / item.w as f64 * cap as f64;\n // \u5df2\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u56e0\u6b64\u8df3\u51fa\u5faa\u73af\n break;\n }\n }\n res\n}\n
fractional_knapsack.c/* \u7269\u54c1 */\ntypedef struct {\n int w; // \u7269\u54c1\u91cd\u91cf\n int v; // \u7269\u54c1\u4ef7\u503c\n} Item;\n\n/* \u5206\u6570\u80cc\u5305\uff1a\u8d2a\u5fc3 */\nfloat fractionalKnapsack(int wgt[], int val[], int itemCount, int cap) {\n // \u521b\u5efa\u7269\u54c1\u5217\u8868\uff0c\u5305\u542b\u4e24\u4e2a\u5c5e\u6027\uff1a\u91cd\u91cf\u3001\u4ef7\u503c\n Item *items = malloc(sizeof(Item) * itemCount);\n for (int i = 0; i < itemCount; i++) {\n items[i] = (Item){.w = wgt[i], .v = val[i]};\n }\n // \u6309\u7167\u5355\u4f4d\u4ef7\u503c item.v / item.w \u4ece\u9ad8\u5230\u4f4e\u8fdb\u884c\u6392\u5e8f\n qsort(items, (size_t)itemCount, sizeof(Item), sortByValueDensity);\n // \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\n float res = 0.0;\n for (int i = 0; i < itemCount; i++) {\n if (items[i].w <= cap) {\n // \u82e5\u5269\u4f59\u5bb9\u91cf\u5145\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u6574\u4e2a\u88c5\u8fdb\u80cc\u5305\n res += items[i].v;\n cap -= items[i].w;\n } else {\n // \u82e5\u5269\u4f59\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88c5\u8fdb\u80cc\u5305\n res += (float)cap / items[i].w * items[i].v;\n cap = 0;\n break;\n }\n }\n free(items);\n return res;\n}\n
fractional_knapsack.zig[class]{Item}-[func]{}\n\n[class]{}-[func]{fractionalKnapsack}\n
\u5728\u6700\u5dee\u60c5\u51b5\u4e0b\uff0c\u9700\u8981\u904d\u5386\u6574\u4e2a\u7269\u54c1\u5217\u8868\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \uff0c\u5176\u4e2d \\(n\\) \u4e3a\u7269\u54c1\u6570\u91cf\u3002
\u7531\u4e8e\u521d\u59cb\u5316\u4e86\u4e00\u4e2a Item
\u5bf9\u8c61\u5217\u8868\uff0c\u56e0\u6b64\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002
\u91c7\u7528\u53cd\u8bc1\u6cd5\u3002\u5047\u8bbe\u7269\u54c1 \\(x\\) \u662f\u5355\u4f4d\u4ef7\u503c\u6700\u9ad8\u7684\u7269\u54c1\uff0c\u4f7f\u7528\u67d0\u7b97\u6cd5\u6c42\u5f97\u6700\u5927\u4ef7\u503c\u4e3a res
\uff0c\u4f46\u8be5\u89e3\u4e2d\u4e0d\u5305\u542b\u7269\u54c1 \\(x\\) \u3002
\u73b0\u5728\u4ece\u80cc\u5305\u4e2d\u62ff\u51fa\u5355\u4f4d\u91cd\u91cf\u7684\u4efb\u610f\u7269\u54c1\uff0c\u5e76\u66ff\u6362\u4e3a\u5355\u4f4d\u91cd\u91cf\u7684\u7269\u54c1 \\(x\\) \u3002\u7531\u4e8e\u7269\u54c1 \\(x\\) \u7684\u5355\u4f4d\u4ef7\u503c\u6700\u9ad8\uff0c\u56e0\u6b64\u66ff\u6362\u540e\u7684\u603b\u4ef7\u503c\u4e00\u5b9a\u5927\u4e8e res
\u3002\u8fd9\u4e0e res
\u662f\u6700\u4f18\u89e3\u77db\u76fe\uff0c\u8bf4\u660e\u6700\u4f18\u89e3\u4e2d\u5fc5\u987b\u5305\u542b\u7269\u54c1 \\(x\\) \u3002
\u5bf9\u4e8e\u8be5\u89e3\u4e2d\u7684\u5176\u4ed6\u7269\u54c1\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u6784\u5efa\u51fa\u4e0a\u8ff0\u77db\u76fe\u3002\u603b\u800c\u8a00\u4e4b\uff0c\u5355\u4f4d\u4ef7\u503c\u66f4\u5927\u7684\u7269\u54c1\u603b\u662f\u66f4\u4f18\u9009\u62e9\uff0c\u8fd9\u8bf4\u660e\u8d2a\u5fc3\u7b56\u7565\u662f\u6709\u6548\u7684\u3002
\u5982\u56fe 15-6 \u6240\u793a\uff0c\u5982\u679c\u5c06\u7269\u54c1\u91cd\u91cf\u548c\u7269\u54c1\u5355\u4f4d\u4ef7\u503c\u5206\u522b\u770b\u4f5c\u4e00\u5f20\u4e8c\u7ef4\u56fe\u8868\u7684\u6a2a\u8f74\u548c\u7eb5\u8f74\uff0c\u5219\u5206\u6570\u80cc\u5305\u95ee\u9898\u53ef\u8f6c\u5316\u4e3a\u201c\u6c42\u5728\u6709\u9650\u6a2a\u8f74\u533a\u95f4\u4e0b\u7684\u6700\u5927\u56f4\u6210\u9762\u79ef\u201d\u3002\u8fd9\u4e2a\u7c7b\u6bd4\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u4ece\u51e0\u4f55\u89d2\u5ea6\u7406\u89e3\u8d2a\u5fc3\u7b56\u7565\u7684\u6709\u6548\u6027\u3002
\u56fe 15-6 \u00a0 \u5206\u6570\u80cc\u5305\u95ee\u9898\u7684\u51e0\u4f55\u8868\u793a
"},{"location":"chapter_greedy/greedy_algorithm/","title":"15.1 \u00a0 \u8d2a\u5fc3\u7b97\u6cd5","text":"\u300c\u8d2a\u5fc3\u7b97\u6cd5 greedy algorithm\u300d\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u89e3\u51b3\u4f18\u5316\u95ee\u9898\u7684\u7b97\u6cd5\uff0c\u5176\u57fa\u672c\u601d\u60f3\u662f\u5728\u95ee\u9898\u7684\u6bcf\u4e2a\u51b3\u7b56\u9636\u6bb5\uff0c\u90fd\u9009\u62e9\u5f53\u524d\u770b\u8d77\u6765\u6700\u4f18\u7684\u9009\u62e9\uff0c\u5373\u8d2a\u5fc3\u5730\u505a\u51fa\u5c40\u90e8\u6700\u4f18\u7684\u51b3\u7b56\uff0c\u4ee5\u671f\u83b7\u5f97\u5168\u5c40\u6700\u4f18\u89e3\u3002\u8d2a\u5fc3\u7b97\u6cd5\u7b80\u6d01\u4e14\u9ad8\u6548\uff0c\u5728\u8bb8\u591a\u5b9e\u9645\u95ee\u9898\u4e2d\u6709\u7740\u5e7f\u6cdb\u7684\u5e94\u7528\u3002
\u8d2a\u5fc3\u7b97\u6cd5\u548c\u52a8\u6001\u89c4\u5212\u90fd\u5e38\u7528\u4e8e\u89e3\u51b3\u4f18\u5316\u95ee\u9898\u3002\u5b83\u4eec\u4e4b\u95f4\u5b58\u5728\u4e00\u4e9b\u76f8\u4f3c\u4e4b\u5904\uff0c\u6bd4\u5982\u90fd\u4f9d\u8d56\u6700\u4f18\u5b50\u7ed3\u6784\u6027\u8d28\uff0c\u4f46\u5de5\u4f5c\u539f\u7406\u4e0d\u540c\u3002
\u6211\u4eec\u5148\u901a\u8fc7\u4f8b\u9898\u201c\u96f6\u94b1\u5151\u6362\u201d\u4e86\u89e3\u8d2a\u5fc3\u7b97\u6cd5\u7684\u5de5\u4f5c\u539f\u7406\u3002\u8fd9\u9053\u9898\u5df2\u7ecf\u5728\u201c\u5b8c\u5168\u80cc\u5305\u95ee\u9898\u201d\u7ae0\u8282\u4e2d\u4ecb\u7ecd\u8fc7\uff0c\u76f8\u4fe1\u4f60\u5bf9\u5b83\u5e76\u4e0d\u964c\u751f\u3002
Question
\u7ed9\u5b9a \\(n\\) \u79cd\u786c\u5e01\uff0c\u7b2c \\(i\\) \u79cd\u786c\u5e01\u7684\u9762\u503c\u4e3a \\(coins[i - 1]\\) \uff0c\u76ee\u6807\u91d1\u989d\u4e3a \\(amt\\) \uff0c\u6bcf\u79cd\u786c\u5e01\u53ef\u4ee5\u91cd\u590d\u9009\u53d6\uff0c\u95ee\u80fd\u591f\u51d1\u51fa\u76ee\u6807\u91d1\u989d\u7684\u6700\u5c11\u786c\u5e01\u6570\u91cf\u3002\u5982\u679c\u65e0\u6cd5\u51d1\u51fa\u76ee\u6807\u91d1\u989d\u5219\u8fd4\u56de \\(-1\\) \u3002
\u672c\u9898\u91c7\u53d6\u7684\u8d2a\u5fc3\u7b56\u7565\u5982\u56fe 15-1 \u6240\u793a\u3002\u7ed9\u5b9a\u76ee\u6807\u91d1\u989d\uff0c\u6211\u4eec\u8d2a\u5fc3\u5730\u9009\u62e9\u4e0d\u5927\u4e8e\u4e14\u6700\u63a5\u8fd1\u5b83\u7684\u786c\u5e01\uff0c\u4e0d\u65ad\u5faa\u73af\u8be5\u6b65\u9aa4\uff0c\u76f4\u81f3\u51d1\u51fa\u76ee\u6807\u91d1\u989d\u4e3a\u6b62\u3002
\u56fe 15-1 \u00a0 \u96f6\u94b1\u5151\u6362\u7684\u8d2a\u5fc3\u7b56\u7565
\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\u6240\u793a\u3002\u4f60\u53ef\u80fd\u4f1a\u4e0d\u7531\u5730\u53d1\u51fa\u611f\u53f9\uff1aSo clean \uff01\u8d2a\u5fc3\u7b97\u6cd5\u4ec5\u7528\u7ea6\u5341\u884c\u4ee3\u7801\u5c31\u89e3\u51b3\u4e86\u96f6\u94b1\u5151\u6362\u95ee\u9898\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig coin_change_greedy.pydef coin_change_greedy(coins: list[int], amt: int) -> int:\n \"\"\"\u96f6\u94b1\u5151\u6362\uff1a\u8d2a\u5fc3\"\"\"\n # \u5047\u8bbe coins \u5217\u8868\u6709\u5e8f\n i = len(coins) - 1\n count = 0\n # \u5faa\u73af\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u5230\u65e0\u5269\u4f59\u91d1\u989d\n while amt > 0:\n # \u627e\u5230\u5c0f\u4e8e\u4e14\u6700\u63a5\u8fd1\u5269\u4f59\u91d1\u989d\u7684\u786c\u5e01\n while i > 0 and coins[i] > amt:\n i -= 1\n # \u9009\u62e9 coins[i]\n amt -= coins[i]\n count += 1\n # \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5219\u8fd4\u56de -1\n return count if amt == 0 else -1\n
coin_change_greedy.cpp/* \u96f6\u94b1\u5151\u6362\uff1a\u8d2a\u5fc3 */\nint coinChangeGreedy(vector<int> &coins, int amt) {\n // \u5047\u8bbe coins \u5217\u8868\u6709\u5e8f\n int i = coins.size() - 1;\n int count = 0;\n // \u5faa\u73af\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u5230\u65e0\u5269\u4f59\u91d1\u989d\n while (amt > 0) {\n // \u627e\u5230\u5c0f\u4e8e\u4e14\u6700\u63a5\u8fd1\u5269\u4f59\u91d1\u989d\u7684\u786c\u5e01\n while (i > 0 && coins[i] > amt) {\n i--;\n }\n // \u9009\u62e9 coins[i]\n amt -= coins[i];\n count++;\n }\n // \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5219\u8fd4\u56de -1\n return amt == 0 ? count : -1;\n}\n
coin_change_greedy.java/* \u96f6\u94b1\u5151\u6362\uff1a\u8d2a\u5fc3 */\nint coinChangeGreedy(int[] coins, int amt) {\n // \u5047\u8bbe coins \u5217\u8868\u6709\u5e8f\n int i = coins.length - 1;\n int count = 0;\n // \u5faa\u73af\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u5230\u65e0\u5269\u4f59\u91d1\u989d\n while (amt > 0) {\n // \u627e\u5230\u5c0f\u4e8e\u4e14\u6700\u63a5\u8fd1\u5269\u4f59\u91d1\u989d\u7684\u786c\u5e01\n while (i > 0 && coins[i] > amt) {\n i--;\n }\n // \u9009\u62e9 coins[i]\n amt -= coins[i];\n count++;\n }\n // \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5219\u8fd4\u56de -1\n return amt == 0 ? count : -1;\n}\n
coin_change_greedy.cs/* \u96f6\u94b1\u5151\u6362\uff1a\u8d2a\u5fc3 */\nint CoinChangeGreedy(int[] coins, int amt) {\n // \u5047\u8bbe coins \u5217\u8868\u6709\u5e8f\n int i = coins.Length - 1;\n int count = 0;\n // \u5faa\u73af\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u5230\u65e0\u5269\u4f59\u91d1\u989d\n while (amt > 0) {\n // \u627e\u5230\u5c0f\u4e8e\u4e14\u6700\u63a5\u8fd1\u5269\u4f59\u91d1\u989d\u7684\u786c\u5e01\n while (i > 0 && coins[i] > amt) {\n i--;\n }\n // \u9009\u62e9 coins[i]\n amt -= coins[i];\n count++;\n }\n // \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5219\u8fd4\u56de -1\n return amt == 0 ? count : -1;\n}\n
coin_change_greedy.go/* \u96f6\u94b1\u5151\u6362\uff1a\u8d2a\u5fc3 */\nfunc coinChangeGreedy(coins []int, amt int) int {\n // \u5047\u8bbe coins \u5217\u8868\u6709\u5e8f\n i := len(coins) - 1\n count := 0\n // \u5faa\u73af\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u5230\u65e0\u5269\u4f59\u91d1\u989d\n for amt > 0 {\n // \u627e\u5230\u5c0f\u4e8e\u4e14\u6700\u63a5\u8fd1\u5269\u4f59\u91d1\u989d\u7684\u786c\u5e01\n for i > 0 && coins[i] > amt {\n i--\n }\n // \u9009\u62e9 coins[i]\n amt -= coins[i]\n count++\n }\n // \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5219\u8fd4\u56de -1\n if amt != 0 {\n return -1\n }\n return count\n}\n
coin_change_greedy.swift/* \u96f6\u94b1\u5151\u6362\uff1a\u8d2a\u5fc3 */\nfunc coinChangeGreedy(coins: [Int], amt: Int) -> Int {\n // \u5047\u8bbe coins \u5217\u8868\u6709\u5e8f\n var i = coins.count - 1\n var count = 0\n var amt = amt\n // \u5faa\u73af\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u5230\u65e0\u5269\u4f59\u91d1\u989d\n while amt > 0 {\n // \u627e\u5230\u5c0f\u4e8e\u4e14\u6700\u63a5\u8fd1\u5269\u4f59\u91d1\u989d\u7684\u786c\u5e01\n while i > 0 && coins[i] > amt {\n i -= 1\n }\n // \u9009\u62e9 coins[i]\n amt -= coins[i]\n count += 1\n }\n // \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5219\u8fd4\u56de -1\n return amt == 0 ? count : -1\n}\n
coin_change_greedy.js/* \u96f6\u94b1\u5151\u6362\uff1a\u8d2a\u5fc3 */\nfunction coinChangeGreedy(coins, amt) {\n // \u5047\u8bbe coins \u6570\u7ec4\u6709\u5e8f\n let i = coins.length - 1;\n let count = 0;\n // \u5faa\u73af\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u5230\u65e0\u5269\u4f59\u91d1\u989d\n while (amt > 0) {\n // \u627e\u5230\u5c0f\u4e8e\u4e14\u6700\u63a5\u8fd1\u5269\u4f59\u91d1\u989d\u7684\u786c\u5e01\n while (i > 0 && coins[i] > amt) {\n i--;\n }\n // \u9009\u62e9 coins[i]\n amt -= coins[i];\n count++;\n }\n // \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5219\u8fd4\u56de -1\n return amt === 0 ? count : -1;\n}\n
coin_change_greedy.ts/* \u96f6\u94b1\u5151\u6362\uff1a\u8d2a\u5fc3 */\nfunction coinChangeGreedy(coins: number[], amt: number): number {\n // \u5047\u8bbe coins \u6570\u7ec4\u6709\u5e8f\n let i = coins.length - 1;\n let count = 0;\n // \u5faa\u73af\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u5230\u65e0\u5269\u4f59\u91d1\u989d\n while (amt > 0) {\n // \u627e\u5230\u5c0f\u4e8e\u4e14\u6700\u63a5\u8fd1\u5269\u4f59\u91d1\u989d\u7684\u786c\u5e01\n while (i > 0 && coins[i] > amt) {\n i--;\n }\n // \u9009\u62e9 coins[i]\n amt -= coins[i];\n count++;\n }\n // \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5219\u8fd4\u56de -1\n return amt === 0 ? count : -1;\n}\n
coin_change_greedy.dart/* \u96f6\u94b1\u5151\u6362\uff1a\u8d2a\u5fc3 */\nint coinChangeGreedy(List<int> coins, int amt) {\n // \u5047\u8bbe coins \u5217\u8868\u6709\u5e8f\n int i = coins.length - 1;\n int count = 0;\n // \u5faa\u73af\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u5230\u65e0\u5269\u4f59\u91d1\u989d\n while (amt > 0) {\n // \u627e\u5230\u5c0f\u4e8e\u4e14\u6700\u63a5\u8fd1\u5269\u4f59\u91d1\u989d\u7684\u786c\u5e01\n while (i > 0 && coins[i] > amt) {\n i--;\n }\n // \u9009\u62e9 coins[i]\n amt -= coins[i];\n count++;\n }\n // \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5219\u8fd4\u56de -1\n return amt == 0 ? count : -1;\n}\n
coin_change_greedy.rs/* \u96f6\u94b1\u5151\u6362\uff1a\u8d2a\u5fc3 */\nfn coin_change_greedy(coins: &[i32], mut amt: i32) -> i32 {\n // \u5047\u8bbe coins \u5217\u8868\u6709\u5e8f\n let mut i = coins.len() - 1;\n let mut count = 0;\n // \u5faa\u73af\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u5230\u65e0\u5269\u4f59\u91d1\u989d\n while amt > 0 {\n // \u627e\u5230\u5c0f\u4e8e\u4e14\u6700\u63a5\u8fd1\u5269\u4f59\u91d1\u989d\u7684\u786c\u5e01\n while i > 0 && coins[i] > amt {\n i -= 1;\n }\n // \u9009\u62e9 coins[i]\n amt -= coins[i];\n count += 1;\n }\n // \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5219\u8fd4\u56de -1\n if amt == 0 {\n count\n } else {\n -1\n }\n}\n
coin_change_greedy.c/* \u96f6\u94b1\u5151\u6362\uff1a\u8d2a\u5fc3 */\nint coinChangeGreedy(int *coins, int size, int amt) {\n // \u5047\u8bbe coins \u5217\u8868\u6709\u5e8f\n int i = size - 1;\n int count = 0;\n // \u5faa\u73af\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u5230\u65e0\u5269\u4f59\u91d1\u989d\n while (amt > 0) {\n // \u627e\u5230\u5c0f\u4e8e\u4e14\u6700\u63a5\u8fd1\u5269\u4f59\u91d1\u989d\u7684\u786c\u5e01\n while (i > 0 && coins[i] > amt) {\n i--;\n }\n // \u9009\u62e9 coins[i]\n amt -= coins[i];\n count++;\n }\n // \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5219\u8fd4\u56de -1\n return amt == 0 ? count : -1;\n}\n
coin_change_greedy.zig[class]{}-[func]{coinChangeGreedy}\n
"},{"location":"chapter_greedy/greedy_algorithm/#1511","title":"15.1.1 \u00a0 \u8d2a\u5fc3\u7684\u4f18\u70b9\u4e0e\u5c40\u9650\u6027","text":"\u8d2a\u5fc3\u7b97\u6cd5\u4e0d\u4ec5\u64cd\u4f5c\u76f4\u63a5\u3001\u5b9e\u73b0\u7b80\u5355\uff0c\u800c\u4e14\u901a\u5e38\u6548\u7387\u4e5f\u5f88\u9ad8\u3002\u5728\u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0c\u8bb0\u786c\u5e01\u6700\u5c0f\u9762\u503c\u4e3a \\(\\min(coins)\\) \uff0c\u5219\u8d2a\u5fc3\u9009\u62e9\u6700\u591a\u5faa\u73af \\(amt / \\min(coins)\\) \u6b21\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(amt / \\min(coins))\\) \u3002\u8fd9\u6bd4\u52a8\u6001\u89c4\u5212\u89e3\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6 \\(O(n \\times amt)\\) \u63d0\u5347\u4e86\u4e00\u4e2a\u6570\u91cf\u7ea7\u3002
\u7136\u800c\uff0c\u5bf9\u4e8e\u67d0\u4e9b\u786c\u5e01\u9762\u503c\u7ec4\u5408\uff0c\u8d2a\u5fc3\u7b97\u6cd5\u5e76\u4e0d\u80fd\u627e\u5230\u6700\u4f18\u89e3\u3002\u56fe 15-2 \u7ed9\u51fa\u4e86\u4e24\u4e2a\u793a\u4f8b\u3002
\u56fe 15-2 \u00a0 \u8d2a\u5fc3\u65e0\u6cd5\u627e\u51fa\u6700\u4f18\u89e3\u7684\u793a\u4f8b
\u4e5f\u5c31\u662f\u8bf4\uff0c\u5bf9\u4e8e\u96f6\u94b1\u5151\u6362\u95ee\u9898\uff0c\u8d2a\u5fc3\u7b97\u6cd5\u65e0\u6cd5\u4fdd\u8bc1\u627e\u5230\u5168\u5c40\u6700\u4f18\u89e3\uff0c\u5e76\u4e14\u6709\u53ef\u80fd\u627e\u5230\u975e\u5e38\u5dee\u7684\u89e3\u3002\u5b83\u66f4\u9002\u5408\u7528\u52a8\u6001\u89c4\u5212\u89e3\u51b3\u3002
\u4e00\u822c\u60c5\u51b5\u4e0b\uff0c\u8d2a\u5fc3\u7b97\u6cd5\u7684\u9002\u7528\u60c5\u51b5\u5206\u4ee5\u4e0b\u4e24\u79cd\u3002
\u90a3\u4e48\u95ee\u9898\u6765\u4e86\uff0c\u4ec0\u4e48\u6837\u7684\u95ee\u9898\u9002\u5408\u7528\u8d2a\u5fc3\u7b97\u6cd5\u6c42\u89e3\u5462\uff1f\u6216\u8005\u8bf4\uff0c\u8d2a\u5fc3\u7b97\u6cd5\u5728\u4ec0\u4e48\u60c5\u51b5\u4e0b\u53ef\u4ee5\u4fdd\u8bc1\u627e\u5230\u6700\u4f18\u89e3\uff1f
\u76f8\u8f83\u4e8e\u52a8\u6001\u89c4\u5212\uff0c\u8d2a\u5fc3\u7b97\u6cd5\u7684\u4f7f\u7528\u6761\u4ef6\u66f4\u52a0\u82db\u523b\uff0c\u5176\u4e3b\u8981\u5173\u6ce8\u95ee\u9898\u7684\u4e24\u4e2a\u6027\u8d28\u3002
\u6700\u4f18\u5b50\u7ed3\u6784\u5df2\u7ecf\u5728\u201c\u52a8\u6001\u89c4\u5212\u201d\u7ae0\u8282\u4e2d\u4ecb\u7ecd\u8fc7\uff0c\u8fd9\u91cc\u4e0d\u518d\u8d58\u8ff0\u3002\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u4e00\u4e9b\u95ee\u9898\u7684\u6700\u4f18\u5b50\u7ed3\u6784\u5e76\u4e0d\u660e\u663e\uff0c\u4f46\u4ecd\u7136\u53ef\u4f7f\u7528\u8d2a\u5fc3\u7b97\u6cd5\u89e3\u51b3\u3002
\u6211\u4eec\u4e3b\u8981\u63a2\u7a76\u8d2a\u5fc3\u9009\u62e9\u6027\u8d28\u7684\u5224\u65ad\u65b9\u6cd5\u3002\u867d\u7136\u5b83\u7684\u63cf\u8ff0\u770b\u4e0a\u53bb\u6bd4\u8f83\u7b80\u5355\uff0c\u4f46\u5b9e\u9645\u4e0a\u5bf9\u4e8e\u8bb8\u591a\u95ee\u9898\uff0c\u8bc1\u660e\u8d2a\u5fc3\u9009\u62e9\u6027\u8d28\u5e76\u975e\u6613\u4e8b\u3002
\u4f8b\u5982\u96f6\u94b1\u5151\u6362\u95ee\u9898\uff0c\u6211\u4eec\u867d\u7136\u80fd\u591f\u5bb9\u6613\u5730\u4e3e\u51fa\u53cd\u4f8b\uff0c\u5bf9\u8d2a\u5fc3\u9009\u62e9\u6027\u8d28\u8fdb\u884c\u8bc1\u4f2a\uff0c\u4f46\u8bc1\u5b9e\u7684\u96be\u5ea6\u8f83\u5927\u3002\u5982\u679c\u95ee\uff1a\u6ee1\u8db3\u4ec0\u4e48\u6761\u4ef6\u7684\u786c\u5e01\u7ec4\u5408\u53ef\u4ee5\u4f7f\u7528\u8d2a\u5fc3\u7b97\u6cd5\u6c42\u89e3\uff1f\u6211\u4eec\u5f80\u5f80\u53ea\u80fd\u51ed\u501f\u76f4\u89c9\u6216\u4e3e\u4f8b\u5b50\u6765\u7ed9\u51fa\u4e00\u4e2a\u6a21\u68f1\u4e24\u53ef\u7684\u7b54\u6848\uff0c\u800c\u96be\u4ee5\u7ed9\u51fa\u4e25\u8c28\u7684\u6570\u5b66\u8bc1\u660e\u3002
Quote
\u6709\u4e00\u7bc7\u8bba\u6587\u7ed9\u51fa\u4e86\u4e00\u4e2a \\(O(n^3)\\) \u65f6\u95f4\u590d\u6742\u5ea6\u7684\u7b97\u6cd5\uff0c\u7528\u4e8e\u5224\u65ad\u4e00\u4e2a\u786c\u5e01\u7ec4\u5408\u80fd\u5426\u4f7f\u7528\u8d2a\u5fc3\u7b97\u6cd5\u627e\u51fa\u4efb\u610f\u91d1\u989d\u7684\u6700\u4f18\u89e3\u3002
Pearson, David. A polynomial-time algorithm for the change-making problem. Operations Research Letters 33.3 (2005): 231-234.
"},{"location":"chapter_greedy/greedy_algorithm/#1513","title":"15.1.3 \u00a0 \u8d2a\u5fc3\u89e3\u9898\u6b65\u9aa4","text":"\u8d2a\u5fc3\u95ee\u9898\u7684\u89e3\u51b3\u6d41\u7a0b\u5927\u4f53\u53ef\u5206\u4e3a\u4ee5\u4e0b\u4e09\u6b65\u3002
\u786e\u5b9a\u8d2a\u5fc3\u7b56\u7565\u662f\u6c42\u89e3\u95ee\u9898\u7684\u6838\u5fc3\u6b65\u9aa4\uff0c\u4f46\u5b9e\u65bd\u8d77\u6765\u53ef\u80fd\u5e76\u4e0d\u5bb9\u6613\uff0c\u4e3b\u8981\u6709\u4ee5\u4e0b\u539f\u56e0\u3002
\u4e3a\u4e86\u4fdd\u8bc1\u6b63\u786e\u6027\uff0c\u6211\u4eec\u5e94\u8be5\u5bf9\u8d2a\u5fc3\u7b56\u7565\u8fdb\u884c\u4e25\u8c28\u7684\u6570\u5b66\u8bc1\u660e\uff0c\u901a\u5e38\u9700\u8981\u7528\u5230\u53cd\u8bc1\u6cd5\u6216\u6570\u5b66\u5f52\u7eb3\u6cd5\u3002
\u7136\u800c\uff0c\u6b63\u786e\u6027\u8bc1\u660e\u4e5f\u5f88\u53ef\u80fd\u4e0d\u662f\u4e00\u4ef6\u6613\u4e8b\u3002\u5982\u82e5\u6ca1\u6709\u5934\u7eea\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u9009\u62e9\u9762\u5411\u6d4b\u8bd5\u7528\u4f8b\u8fdb\u884c\u4ee3\u7801\u8c03\u8bd5\uff0c\u4e00\u6b65\u6b65\u4fee\u6539\u4e0e\u9a8c\u8bc1\u8d2a\u5fc3\u7b56\u7565\u3002
"},{"location":"chapter_greedy/greedy_algorithm/#1514","title":"15.1.4 \u00a0 \u8d2a\u5fc3\u5178\u578b\u4f8b\u9898","text":"\u8d2a\u5fc3\u7b97\u6cd5\u5e38\u5e38\u5e94\u7528\u5728\u6ee1\u8db3\u8d2a\u5fc3\u9009\u62e9\u6027\u8d28\u548c\u6700\u4f18\u5b50\u7ed3\u6784\u7684\u4f18\u5316\u95ee\u9898\u4e2d\uff0c\u4ee5\u4e0b\u5217\u4e3e\u4e86\u4e00\u4e9b\u5178\u578b\u7684\u8d2a\u5fc3\u7b97\u6cd5\u95ee\u9898\u3002
Question
\u8f93\u5165\u4e00\u4e2a\u6570\u7ec4 \\(ht\\) \uff0c\u5176\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u4ee3\u8868\u4e00\u4e2a\u5782\u76f4\u9694\u677f\u7684\u9ad8\u5ea6\u3002\u6570\u7ec4\u4e2d\u7684\u4efb\u610f\u4e24\u4e2a\u9694\u677f\uff0c\u4ee5\u53ca\u5b83\u4eec\u4e4b\u95f4\u7684\u7a7a\u95f4\u53ef\u4ee5\u7ec4\u6210\u4e00\u4e2a\u5bb9\u5668\u3002
\u5bb9\u5668\u7684\u5bb9\u91cf\u7b49\u4e8e\u9ad8\u5ea6\u548c\u5bbd\u5ea6\u7684\u4e58\u79ef\uff08\u9762\u79ef\uff09\uff0c\u5176\u4e2d\u9ad8\u5ea6\u7531\u8f83\u77ed\u7684\u9694\u677f\u51b3\u5b9a\uff0c\u5bbd\u5ea6\u662f\u4e24\u4e2a\u9694\u677f\u7684\u6570\u7ec4\u7d22\u5f15\u4e4b\u5dee\u3002
\u8bf7\u5728\u6570\u7ec4\u4e2d\u9009\u62e9\u4e24\u4e2a\u9694\u677f\uff0c\u4f7f\u5f97\u7ec4\u6210\u7684\u5bb9\u5668\u7684\u5bb9\u91cf\u6700\u5927\uff0c\u8fd4\u56de\u6700\u5927\u5bb9\u91cf\u3002\u793a\u4f8b\u5982\u56fe 15-7 \u6240\u793a\u3002
\u56fe 15-7 \u00a0 \u6700\u5927\u5bb9\u91cf\u95ee\u9898\u7684\u793a\u4f8b\u6570\u636e
\u5bb9\u5668\u7531\u4efb\u610f\u4e24\u4e2a\u9694\u677f\u56f4\u6210\uff0c\u56e0\u6b64\u672c\u9898\u7684\u72b6\u6001\u4e3a\u4e24\u4e2a\u9694\u677f\u7684\u7d22\u5f15\uff0c\u8bb0\u4e3a \\([i, j]\\) \u3002
\u6839\u636e\u9898\u610f\uff0c\u5bb9\u91cf\u7b49\u4e8e\u9ad8\u5ea6\u4e58\u4ee5\u5bbd\u5ea6\uff0c\u5176\u4e2d\u9ad8\u5ea6\u7531\u77ed\u677f\u51b3\u5b9a\uff0c\u5bbd\u5ea6\u662f\u4e24\u9694\u677f\u7684\u6570\u7ec4\u7d22\u5f15\u4e4b\u5dee\u3002\u8bbe\u5bb9\u91cf\u4e3a \\(cap[i, j]\\) \uff0c\u5219\u53ef\u5f97\u8ba1\u7b97\u516c\u5f0f\uff1a
\\[ cap[i, j] = \\min(ht[i], ht[j]) \\times (j - i) \\]\u8bbe\u6570\u7ec4\u957f\u5ea6\u4e3a \\(n\\) \uff0c\u4e24\u4e2a\u9694\u677f\u7684\u7ec4\u5408\u6570\u91cf\uff08\u72b6\u6001\u603b\u6570\uff09\u4e3a \\(C_n^2 = \\frac{n(n - 1)}{2}\\) \u4e2a\u3002\u6700\u76f4\u63a5\u5730\uff0c\u6211\u4eec\u53ef\u4ee5\u7a77\u4e3e\u6240\u6709\u72b6\u6001\uff0c\u4ece\u800c\u6c42\u5f97\u6700\u5927\u5bb9\u91cf\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\) \u3002
"},{"location":"chapter_greedy/max_capacity_problem/#1","title":"1. \u00a0 \u8d2a\u5fc3\u7b56\u7565\u786e\u5b9a","text":"\u8fd9\u9053\u9898\u8fd8\u6709\u66f4\u9ad8\u6548\u7387\u7684\u89e3\u6cd5\u3002\u5982\u56fe 15-8 \u6240\u793a\uff0c\u73b0\u9009\u53d6\u4e00\u4e2a\u72b6\u6001 \\([i, j]\\) \uff0c\u5176\u6ee1\u8db3\u7d22\u5f15 \\(i < j\\) \u4e14\u9ad8\u5ea6 \\(ht[i] < ht[j]\\) \uff0c\u5373 \\(i\\) \u4e3a\u77ed\u677f\u3001\\(j\\) \u4e3a\u957f\u677f\u3002
\u56fe 15-8 \u00a0 \u521d\u59cb\u72b6\u6001
\u5982\u56fe 15-9 \u6240\u793a\uff0c\u82e5\u6b64\u65f6\u5c06\u957f\u677f \\(j\\) \u5411\u77ed\u677f \\(i\\) \u9760\u8fd1\uff0c\u5219\u5bb9\u91cf\u4e00\u5b9a\u53d8\u5c0f\u3002
\u8fd9\u662f\u56e0\u4e3a\u5728\u79fb\u52a8\u957f\u677f \\(j\\) \u540e\uff0c\u5bbd\u5ea6 \\(j-i\\) \u80af\u5b9a\u53d8\u5c0f\uff1b\u800c\u9ad8\u5ea6\u7531\u77ed\u677f\u51b3\u5b9a\uff0c\u56e0\u6b64\u9ad8\u5ea6\u53ea\u53ef\u80fd\u4e0d\u53d8\uff08 \\(i\\) \u4ecd\u4e3a\u77ed\u677f\uff09\u6216\u53d8\u5c0f\uff08\u79fb\u52a8\u540e\u7684 \\(j\\) \u6210\u4e3a\u77ed\u677f\uff09\u3002
\u56fe 15-9 \u00a0 \u5411\u5185\u79fb\u52a8\u957f\u677f\u540e\u7684\u72b6\u6001
\u53cd\u5411\u601d\u8003\uff0c\u6211\u4eec\u53ea\u6709\u5411\u5185\u6536\u7f29\u77ed\u677f \\(i\\) \uff0c\u624d\u6709\u53ef\u80fd\u4f7f\u5bb9\u91cf\u53d8\u5927\u3002\u56e0\u4e3a\u867d\u7136\u5bbd\u5ea6\u4e00\u5b9a\u53d8\u5c0f\uff0c\u4f46\u9ad8\u5ea6\u53ef\u80fd\u4f1a\u53d8\u5927\uff08\u79fb\u52a8\u540e\u7684\u77ed\u677f \\(i\\) \u53ef\u80fd\u4f1a\u53d8\u957f\uff09\u3002\u4f8b\u5982\u5728\u56fe 15-10 \u4e2d\uff0c\u79fb\u52a8\u77ed\u677f\u540e\u9762\u79ef\u53d8\u5927\u3002
\u56fe 15-10 \u00a0 \u5411\u5185\u79fb\u52a8\u77ed\u677f\u540e\u7684\u72b6\u6001
\u7531\u6b64\u4fbf\u53ef\u63a8\u51fa\u672c\u9898\u7684\u8d2a\u5fc3\u7b56\u7565\uff1a\u521d\u59cb\u5316\u4e24\u6307\u9488\u5206\u5217\u5bb9\u5668\u4e24\u7aef\uff0c\u6bcf\u8f6e\u5411\u5185\u6536\u7f29\u77ed\u677f\u5bf9\u5e94\u7684\u6307\u9488\uff0c\u76f4\u81f3\u4e24\u6307\u9488\u76f8\u9047\u3002
\u56fe 15-11 \u5c55\u793a\u4e86\u8d2a\u5fc3\u7b56\u7565\u7684\u6267\u884c\u8fc7\u7a0b\u3002
2.
\u6b65\u548c\u7b2c 3.
\u6b65\uff0c\u76f4\u81f3 \\(i\\) \u548c \\(j\\) \u76f8\u9047\u65f6\u7ed3\u675f\u3002\u56fe 15-11 \u00a0 \u6700\u5927\u5bb9\u91cf\u95ee\u9898\u7684\u8d2a\u5fc3\u8fc7\u7a0b
"},{"location":"chapter_greedy/max_capacity_problem/#2","title":"2. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"\u4ee3\u7801\u5faa\u73af\u6700\u591a \\(n\\) \u8f6e\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002
\u53d8\u91cf \\(i\\)\u3001\\(j\\)\u3001\\(res\\) \u4f7f\u7528\u5e38\u6570\u5927\u5c0f\u7684\u989d\u5916\u7a7a\u95f4\uff0c\u56e0\u6b64\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(1)\\) \u3002
PythonC++JavaC#GoSwiftJSTSDartRustCZig max_capacity.pydef max_capacity(ht: list[int]) -> int:\n \"\"\"\u6700\u5927\u5bb9\u91cf\uff1a\u8d2a\u5fc3\"\"\"\n # \u521d\u59cb\u5316 i, j \u5206\u5217\u6570\u7ec4\u4e24\u7aef\n i, j = 0, len(ht) - 1\n # \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u4e3a 0\n res = 0\n # \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u81f3\u4e24\u677f\u76f8\u9047\n while i < j:\n # \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\n cap = min(ht[i], ht[j]) * (j - i)\n res = max(res, cap)\n # \u5411\u5185\u79fb\u52a8\u77ed\u677f\n if ht[i] < ht[j]:\n i += 1\n else:\n j -= 1\n return res\n
max_capacity.cpp/* \u6700\u5927\u5bb9\u91cf\uff1a\u8d2a\u5fc3 */\nint maxCapacity(vector<int> &ht) {\n // \u521d\u59cb\u5316 i, j \u5206\u5217\u6570\u7ec4\u4e24\u7aef\n int i = 0, j = ht.size() - 1;\n // \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u4e3a 0\n int res = 0;\n // \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u81f3\u4e24\u677f\u76f8\u9047\n while (i < j) {\n // \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\n int cap = min(ht[i], ht[j]) * (j - i);\n res = max(res, cap);\n // \u5411\u5185\u79fb\u52a8\u77ed\u677f\n if (ht[i] < ht[j]) {\n i++;\n } else {\n j--;\n }\n }\n return res;\n}\n
max_capacity.java/* \u6700\u5927\u5bb9\u91cf\uff1a\u8d2a\u5fc3 */\nint maxCapacity(int[] ht) {\n // \u521d\u59cb\u5316 i, j \u5206\u5217\u6570\u7ec4\u4e24\u7aef\n int i = 0, j = ht.length - 1;\n // \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u4e3a 0\n int res = 0;\n // \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u81f3\u4e24\u677f\u76f8\u9047\n while (i < j) {\n // \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\n int cap = Math.min(ht[i], ht[j]) * (j - i);\n res = Math.max(res, cap);\n // \u5411\u5185\u79fb\u52a8\u77ed\u677f\n if (ht[i] < ht[j]) {\n i++;\n } else {\n j--;\n }\n }\n return res;\n}\n
max_capacity.cs/* \u6700\u5927\u5bb9\u91cf\uff1a\u8d2a\u5fc3 */\nint MaxCapacity(int[] ht) {\n // \u521d\u59cb\u5316 i, j \u5206\u5217\u6570\u7ec4\u4e24\u7aef\n int i = 0, j = ht.Length - 1;\n // \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u4e3a 0\n int res = 0;\n // \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u81f3\u4e24\u677f\u76f8\u9047\n while (i < j) {\n // \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\n int cap = Math.Min(ht[i], ht[j]) * (j - i);\n res = Math.Max(res, cap);\n // \u5411\u5185\u79fb\u52a8\u77ed\u677f\n if (ht[i] < ht[j]) {\n i++;\n } else {\n j--;\n }\n }\n return res;\n}\n
max_capacity.go/* \u6700\u5927\u5bb9\u91cf\uff1a\u8d2a\u5fc3 */\nfunc maxCapacity(ht []int) int {\n // \u521d\u59cb\u5316 i, j \u5206\u5217\u6570\u7ec4\u4e24\u7aef\n i, j := 0, len(ht)-1\n // \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u4e3a 0\n res := 0\n // \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u81f3\u4e24\u677f\u76f8\u9047\n for i < j {\n // \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\n capacity := int(math.Min(float64(ht[i]), float64(ht[j]))) * (j - i)\n res = int(math.Max(float64(res), float64(capacity)))\n // \u5411\u5185\u79fb\u52a8\u77ed\u677f\n if ht[i] < ht[j] {\n i++\n } else {\n j--\n }\n }\n return res\n}\n
max_capacity.swift/* \u6700\u5927\u5bb9\u91cf\uff1a\u8d2a\u5fc3 */\nfunc maxCapacity(ht: [Int]) -> Int {\n // \u521d\u59cb\u5316 i, j \u5206\u5217\u6570\u7ec4\u4e24\u7aef\n var i = 0, j = ht.count - 1\n // \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u4e3a 0\n var res = 0\n // \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u81f3\u4e24\u677f\u76f8\u9047\n while i < j {\n // \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\n let cap = min(ht[i], ht[j]) * (j - i)\n res = max(res, cap)\n // \u5411\u5185\u79fb\u52a8\u77ed\u677f\n if ht[i] < ht[j] {\n i += 1\n } else {\n j -= 1\n }\n }\n return res\n}\n
max_capacity.js/* \u6700\u5927\u5bb9\u91cf\uff1a\u8d2a\u5fc3 */\nfunction maxCapacity(ht) {\n // \u521d\u59cb\u5316 i, j \u5206\u5217\u6570\u7ec4\u4e24\u7aef\n let i = 0,\n j = ht.length - 1;\n // \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u4e3a 0\n let res = 0;\n // \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u81f3\u4e24\u677f\u76f8\u9047\n while (i < j) {\n // \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\n const cap = Math.min(ht[i], ht[j]) * (j - i);\n res = Math.max(res, cap);\n // \u5411\u5185\u79fb\u52a8\u77ed\u677f\n if (ht[i] < ht[j]) {\n i += 1;\n } else {\n j -= 1;\n }\n }\n return res;\n}\n
max_capacity.ts/* \u6700\u5927\u5bb9\u91cf\uff1a\u8d2a\u5fc3 */\nfunction maxCapacity(ht: number[]): number {\n // \u521d\u59cb\u5316 i, j \u5206\u5217\u6570\u7ec4\u4e24\u7aef\n let i = 0,\n j = ht.length - 1;\n // \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u4e3a 0\n let res = 0;\n // \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u81f3\u4e24\u677f\u76f8\u9047\n while (i < j) {\n // \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\n const cap: number = Math.min(ht[i], ht[j]) * (j - i);\n res = Math.max(res, cap);\n // \u5411\u5185\u79fb\u52a8\u77ed\u677f\n if (ht[i] < ht[j]) {\n i += 1;\n } else {\n j -= 1;\n }\n }\n return res;\n}\n
max_capacity.dart/* \u6700\u5927\u5bb9\u91cf\uff1a\u8d2a\u5fc3 */\nint maxCapacity(List<int> ht) {\n // \u521d\u59cb\u5316 i, j \u5206\u5217\u6570\u7ec4\u4e24\u7aef\n int i = 0, j = ht.length - 1;\n // \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u4e3a 0\n int res = 0;\n // \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u81f3\u4e24\u677f\u76f8\u9047\n while (i < j) {\n // \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\n int cap = min(ht[i], ht[j]) * (j - i);\n res = max(res, cap);\n // \u5411\u5185\u79fb\u52a8\u77ed\u677f\n if (ht[i] < ht[j]) {\n i++;\n } else {\n j--;\n }\n }\n return res;\n}\n
max_capacity.rs/* \u6700\u5927\u5bb9\u91cf\uff1a\u8d2a\u5fc3 */\nfn max_capacity(ht: &[i32]) -> i32 {\n // \u521d\u59cb\u5316 i, j \u5206\u5217\u6570\u7ec4\u4e24\u7aef\n let mut i = 0;\n let mut j = ht.len() - 1;\n // \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u4e3a 0\n let mut res = 0;\n // \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u81f3\u4e24\u677f\u76f8\u9047\n while i < j {\n // \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\n let cap = std::cmp::min(ht[i], ht[j]) * (j - i) as i32;\n res = std::cmp::max(res, cap);\n // \u5411\u5185\u79fb\u52a8\u77ed\u677f\n if ht[i] < ht[j] {\n i += 1;\n } else {\n j -= 1;\n }\n }\n res\n}\n
max_capacity.c/* \u6700\u5927\u5bb9\u91cf\uff1a\u8d2a\u5fc3 */\nint maxCapacity(int ht[], int htLength) {\n // \u521d\u59cb\u5316 i, j \u5206\u5217\u6570\u7ec4\u4e24\u7aef\n int i = 0;\n int j = htLength - 1;\n // \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u4e3a 0\n int res = 0;\n // \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u81f3\u4e24\u677f\u76f8\u9047\n while (i < j) {\n // \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\n int capacity = myMin(ht[i], ht[j]) * (j - i);\n res = myMax(res, capacity);\n // \u5411\u5185\u79fb\u52a8\u77ed\u677f\n if (ht[i] < ht[j]) {\n i++;\n } else {\n j--;\n }\n }\n return res;\n}\n
max_capacity.zig[class]{}-[func]{maxCapacity}\n
"},{"location":"chapter_greedy/max_capacity_problem/#3","title":"3. \u00a0 \u6b63\u786e\u6027\u8bc1\u660e","text":"\u4e4b\u6240\u4ee5\u8d2a\u5fc3\u6bd4\u7a77\u4e3e\u66f4\u5feb\uff0c\u662f\u56e0\u4e3a\u6bcf\u8f6e\u7684\u8d2a\u5fc3\u9009\u62e9\u90fd\u4f1a\u201c\u8df3\u8fc7\u201d\u4e00\u4e9b\u72b6\u6001\u3002
\u6bd4\u5982\u5728\u72b6\u6001 \\(cap[i, j]\\) \u4e0b\uff0c\\(i\\) \u4e3a\u77ed\u677f\u3001\\(j\\) \u4e3a\u957f\u677f\u3002\u82e5\u8d2a\u5fc3\u5730\u5c06\u77ed\u677f \\(i\\) \u5411\u5185\u79fb\u52a8\u4e00\u683c\uff0c\u4f1a\u5bfc\u81f4\u56fe 15-12 \u6240\u793a\u7684\u72b6\u6001\u88ab\u201c\u8df3\u8fc7\u201d\u3002\u8fd9\u610f\u5473\u7740\u4e4b\u540e\u65e0\u6cd5\u9a8c\u8bc1\u8fd9\u4e9b\u72b6\u6001\u7684\u5bb9\u91cf\u5927\u5c0f\u3002
\\[ cap[i, i+1], cap[i, i+2], \\dots, cap[i, j-2], cap[i, j-1] \\]\u56fe 15-12 \u00a0 \u79fb\u52a8\u77ed\u677f\u5bfc\u81f4\u88ab\u8df3\u8fc7\u7684\u72b6\u6001
\u89c2\u5bdf\u53d1\u73b0\uff0c\u8fd9\u4e9b\u88ab\u8df3\u8fc7\u7684\u72b6\u6001\u5b9e\u9645\u4e0a\u5c31\u662f\u5c06\u957f\u677f \\(j\\) \u5411\u5185\u79fb\u52a8\u7684\u6240\u6709\u72b6\u6001\u3002\u524d\u9762\u6211\u4eec\u5df2\u7ecf\u8bc1\u660e\u5185\u79fb\u957f\u677f\u4e00\u5b9a\u4f1a\u5bfc\u81f4\u5bb9\u91cf\u53d8\u5c0f\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u88ab\u8df3\u8fc7\u7684\u72b6\u6001\u90fd\u4e0d\u53ef\u80fd\u662f\u6700\u4f18\u89e3\uff0c\u8df3\u8fc7\u5b83\u4eec\u4e0d\u4f1a\u5bfc\u81f4\u9519\u8fc7\u6700\u4f18\u89e3\u3002
\u4ee5\u4e0a\u5206\u6790\u8bf4\u660e\uff0c\u79fb\u52a8\u77ed\u677f\u7684\u64cd\u4f5c\u662f\u201c\u5b89\u5168\u201d\u7684\uff0c\u8d2a\u5fc3\u7b56\u7565\u662f\u6709\u6548\u7684\u3002
"},{"location":"chapter_greedy/max_product_cutting_problem/","title":"15.4 \u00a0 \u6700\u5927\u5207\u5206\u4e58\u79ef\u95ee\u9898","text":"Question
\u7ed9\u5b9a\u4e00\u4e2a\u6b63\u6574\u6570 \\(n\\) \uff0c\u5c06\u5176\u5207\u5206\u4e3a\u81f3\u5c11\u4e24\u4e2a\u6b63\u6574\u6570\u7684\u548c\uff0c\u6c42\u5207\u5206\u540e\u6240\u6709\u6574\u6570\u7684\u4e58\u79ef\u6700\u5927\u662f\u591a\u5c11\uff0c\u5982\u56fe 15-13 \u6240\u793a\u3002
\u56fe 15-13 \u00a0 \u6700\u5927\u5207\u5206\u4e58\u79ef\u7684\u95ee\u9898\u5b9a\u4e49
\u5047\u8bbe\u6211\u4eec\u5c06 \\(n\\) \u5207\u5206\u4e3a \\(m\\) \u4e2a\u6574\u6570\u56e0\u5b50\uff0c\u5176\u4e2d\u7b2c \\(i\\) \u4e2a\u56e0\u5b50\u8bb0\u4e3a \\(n_i\\) \uff0c\u5373
\\[ n = \\sum_{i=1}^{m}n_i \\]\u672c\u9898\u7684\u76ee\u6807\u662f\u6c42\u5f97\u6240\u6709\u6574\u6570\u56e0\u5b50\u7684\u6700\u5927\u4e58\u79ef\uff0c\u5373
\\[ \\max(\\prod_{i=1}^{m}n_i) \\]\u6211\u4eec\u9700\u8981\u601d\u8003\u7684\u662f\uff1a\u5207\u5206\u6570\u91cf \\(m\\) \u5e94\u8be5\u591a\u5927\uff0c\u6bcf\u4e2a \\(n_i\\) \u5e94\u8be5\u662f\u591a\u5c11\uff1f
"},{"location":"chapter_greedy/max_product_cutting_problem/#1","title":"1. \u00a0 \u8d2a\u5fc3\u7b56\u7565\u786e\u5b9a","text":"\u6839\u636e\u7ecf\u9a8c\uff0c\u4e24\u4e2a\u6574\u6570\u7684\u4e58\u79ef\u5f80\u5f80\u6bd4\u5b83\u4eec\u7684\u52a0\u548c\u66f4\u5927\u3002\u5047\u8bbe\u4ece \\(n\\) \u4e2d\u5206\u51fa\u4e00\u4e2a\u56e0\u5b50 \\(2\\) \uff0c\u5219\u5b83\u4eec\u7684\u4e58\u79ef\u4e3a \\(2(n-2)\\) \u3002\u6211\u4eec\u5c06\u8be5\u4e58\u79ef\u4e0e \\(n\\) \u4f5c\u6bd4\u8f83\uff1a
\\[ \\begin{aligned} 2(n-2) & \\geq n \\newline 2n - n - 4 & \\geq 0 \\newline n & \\geq 4 \\end{aligned} \\]\u5982\u56fe 15-14 \u6240\u793a\uff0c\u5f53 \\(n \\geq 4\\) \u65f6\uff0c\u5207\u5206\u51fa\u4e00\u4e2a \\(2\\) \u540e\u4e58\u79ef\u4f1a\u53d8\u5927\uff0c\u8fd9\u8bf4\u660e\u5927\u4e8e\u7b49\u4e8e \\(4\\) \u7684\u6574\u6570\u90fd\u5e94\u8be5\u88ab\u5207\u5206\u3002
\u8d2a\u5fc3\u7b56\u7565\u4e00\uff1a\u5982\u679c\u5207\u5206\u65b9\u6848\u4e2d\u5305\u542b \\(\\geq 4\\) \u7684\u56e0\u5b50\uff0c\u90a3\u4e48\u5b83\u5c31\u5e94\u8be5\u88ab\u7ee7\u7eed\u5207\u5206\u3002\u6700\u7ec8\u7684\u5207\u5206\u65b9\u6848\u53ea\u5e94\u51fa\u73b0 \\(1\\)\u3001\\(2\\)\u3001\\(3\\) \u8fd9\u4e09\u79cd\u56e0\u5b50\u3002
\u56fe 15-14 \u00a0 \u5207\u5206\u5bfc\u81f4\u4e58\u79ef\u53d8\u5927
\u63a5\u4e0b\u6765\u601d\u8003\u54ea\u4e2a\u56e0\u5b50\u662f\u6700\u4f18\u7684\u3002\u5728 \\(1\\)\u3001\\(2\\)\u3001\\(3\\) \u8fd9\u4e09\u4e2a\u56e0\u5b50\u4e2d\uff0c\u663e\u7136 \\(1\\) \u662f\u6700\u5dee\u7684\uff0c\u56e0\u4e3a \\(1 \\times (n-1) < n\\) \u6052\u6210\u7acb\uff0c\u5373\u5207\u5206\u51fa \\(1\\) \u53cd\u800c\u4f1a\u5bfc\u81f4\u4e58\u79ef\u51cf\u5c0f\u3002
\u5982\u56fe 15-15 \u6240\u793a\uff0c\u5f53 \\(n = 6\\) \u65f6\uff0c\u6709 \\(3 \\times 3 > 2 \\times 2 \\times 2\\) \u3002\u8fd9\u610f\u5473\u7740\u5207\u5206\u51fa \\(3\\) \u6bd4\u5207\u5206\u51fa \\(2\\) \u66f4\u4f18\u3002
\u8d2a\u5fc3\u7b56\u7565\u4e8c\uff1a\u5728\u5207\u5206\u65b9\u6848\u4e2d\uff0c\u6700\u591a\u53ea\u5e94\u5b58\u5728\u4e24\u4e2a \\(2\\) \u3002\u56e0\u4e3a\u4e09\u4e2a \\(2\\) \u603b\u662f\u53ef\u4ee5\u66ff\u6362\u4e3a\u4e24\u4e2a \\(3\\) \uff0c\u4ece\u800c\u83b7\u5f97\u66f4\u5927\u7684\u4e58\u79ef\u3002
\u56fe 15-15 \u00a0 \u6700\u4f18\u5207\u5206\u56e0\u5b50
\u7efc\u4e0a\u6240\u8ff0\uff0c\u53ef\u63a8\u7406\u51fa\u4ee5\u4e0b\u8d2a\u5fc3\u7b56\u7565\u3002
\u5982\u56fe 15-16 \u6240\u793a\uff0c\u6211\u4eec\u65e0\u987b\u901a\u8fc7\u5faa\u73af\u6765\u5207\u5206\u6574\u6570\uff0c\u800c\u53ef\u4ee5\u5229\u7528\u5411\u4e0b\u6574\u9664\u8fd0\u7b97\u5f97\u5230 \\(3\\) \u7684\u4e2a\u6570 \\(a\\) \uff0c\u7528\u53d6\u6a21\u8fd0\u7b97\u5f97\u5230\u4f59\u6570 \\(b\\) \uff0c\u6b64\u65f6\u6709\uff1a
\\[ n = 3 a + b \\]\u8bf7\u6ce8\u610f\uff0c\u5bf9\u4e8e \\(n \\leq 3\\) \u7684\u8fb9\u754c\u60c5\u51b5\uff0c\u5fc5\u987b\u62c6\u5206\u51fa\u4e00\u4e2a \\(1\\) \uff0c\u4e58\u79ef\u4e3a \\(1 \\times (n - 1)\\) \u3002
PythonC++JavaC#GoSwiftJSTSDartRustCZig max_product_cutting.pydef max_product_cutting(n: int) -> int:\n \"\"\"\u6700\u5927\u5207\u5206\u4e58\u79ef\uff1a\u8d2a\u5fc3\"\"\"\n # \u5f53 n <= 3 \u65f6\uff0c\u5fc5\u987b\u5207\u5206\u51fa\u4e00\u4e2a 1\n if n <= 3:\n return 1 * (n - 1)\n # \u8d2a\u5fc3\u5730\u5207\u5206\u51fa 3 \uff0ca \u4e3a 3 \u7684\u4e2a\u6570\uff0cb \u4e3a\u4f59\u6570\n a, b = n // 3, n % 3\n if b == 1:\n # \u5f53\u4f59\u6570\u4e3a 1 \u65f6\uff0c\u5c06\u4e00\u5bf9 1 * 3 \u8f6c\u5316\u4e3a 2 * 2\n return int(math.pow(3, a - 1)) * 2 * 2\n if b == 2:\n # \u5f53\u4f59\u6570\u4e3a 2 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n return int(math.pow(3, a)) * 2\n # \u5f53\u4f59\u6570\u4e3a 0 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n return int(math.pow(3, a))\n
max_product_cutting.cpp/* \u6700\u5927\u5207\u5206\u4e58\u79ef\uff1a\u8d2a\u5fc3 */\nint maxProductCutting(int n) {\n // \u5f53 n <= 3 \u65f6\uff0c\u5fc5\u987b\u5207\u5206\u51fa\u4e00\u4e2a 1\n if (n <= 3) {\n return 1 * (n - 1);\n }\n // \u8d2a\u5fc3\u5730\u5207\u5206\u51fa 3 \uff0ca \u4e3a 3 \u7684\u4e2a\u6570\uff0cb \u4e3a\u4f59\u6570\n int a = n / 3;\n int b = n % 3;\n if (b == 1) {\n // \u5f53\u4f59\u6570\u4e3a 1 \u65f6\uff0c\u5c06\u4e00\u5bf9 1 * 3 \u8f6c\u5316\u4e3a 2 * 2\n return (int)pow(3, a - 1) * 2 * 2;\n }\n if (b == 2) {\n // \u5f53\u4f59\u6570\u4e3a 2 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n return (int)pow(3, a) * 2;\n }\n // \u5f53\u4f59\u6570\u4e3a 0 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n return (int)pow(3, a);\n}\n
max_product_cutting.java/* \u6700\u5927\u5207\u5206\u4e58\u79ef\uff1a\u8d2a\u5fc3 */\nint maxProductCutting(int n) {\n // \u5f53 n <= 3 \u65f6\uff0c\u5fc5\u987b\u5207\u5206\u51fa\u4e00\u4e2a 1\n if (n <= 3) {\n return 1 * (n - 1);\n }\n // \u8d2a\u5fc3\u5730\u5207\u5206\u51fa 3 \uff0ca \u4e3a 3 \u7684\u4e2a\u6570\uff0cb \u4e3a\u4f59\u6570\n int a = n / 3;\n int b = n % 3;\n if (b == 1) {\n // \u5f53\u4f59\u6570\u4e3a 1 \u65f6\uff0c\u5c06\u4e00\u5bf9 1 * 3 \u8f6c\u5316\u4e3a 2 * 2\n return (int) Math.pow(3, a - 1) * 2 * 2;\n }\n if (b == 2) {\n // \u5f53\u4f59\u6570\u4e3a 2 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n return (int) Math.pow(3, a) * 2;\n }\n // \u5f53\u4f59\u6570\u4e3a 0 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n return (int) Math.pow(3, a);\n}\n
max_product_cutting.cs/* \u6700\u5927\u5207\u5206\u4e58\u79ef\uff1a\u8d2a\u5fc3 */\nint MaxProductCutting(int n) {\n // \u5f53 n <= 3 \u65f6\uff0c\u5fc5\u987b\u5207\u5206\u51fa\u4e00\u4e2a 1\n if (n <= 3) {\n return 1 * (n - 1);\n }\n // \u8d2a\u5fc3\u5730\u5207\u5206\u51fa 3 \uff0ca \u4e3a 3 \u7684\u4e2a\u6570\uff0cb \u4e3a\u4f59\u6570\n int a = n / 3;\n int b = n % 3;\n if (b == 1) {\n // \u5f53\u4f59\u6570\u4e3a 1 \u65f6\uff0c\u5c06\u4e00\u5bf9 1 * 3 \u8f6c\u5316\u4e3a 2 * 2\n return (int)Math.Pow(3, a - 1) * 2 * 2;\n }\n if (b == 2) {\n // \u5f53\u4f59\u6570\u4e3a 2 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n return (int)Math.Pow(3, a) * 2;\n }\n // \u5f53\u4f59\u6570\u4e3a 0 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n return (int)Math.Pow(3, a);\n}\n
max_product_cutting.go/* \u6700\u5927\u5207\u5206\u4e58\u79ef\uff1a\u8d2a\u5fc3 */\nfunc maxProductCutting(n int) int {\n // \u5f53 n <= 3 \u65f6\uff0c\u5fc5\u987b\u5207\u5206\u51fa\u4e00\u4e2a 1\n if n <= 3 {\n return 1 * (n - 1)\n }\n // \u8d2a\u5fc3\u5730\u5207\u5206\u51fa 3 \uff0ca \u4e3a 3 \u7684\u4e2a\u6570\uff0cb \u4e3a\u4f59\u6570\n a := n / 3\n b := n % 3\n if b == 1 {\n // \u5f53\u4f59\u6570\u4e3a 1 \u65f6\uff0c\u5c06\u4e00\u5bf9 1 * 3 \u8f6c\u5316\u4e3a 2 * 2\n return int(math.Pow(3, float64(a-1))) * 2 * 2\n }\n if b == 2 {\n // \u5f53\u4f59\u6570\u4e3a 2 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n return int(math.Pow(3, float64(a))) * 2\n }\n // \u5f53\u4f59\u6570\u4e3a 0 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n return int(math.Pow(3, float64(a)))\n}\n
max_product_cutting.swift/* \u6700\u5927\u5207\u5206\u4e58\u79ef\uff1a\u8d2a\u5fc3 */\nfunc maxProductCutting(n: Int) -> Int {\n // \u5f53 n <= 3 \u65f6\uff0c\u5fc5\u987b\u5207\u5206\u51fa\u4e00\u4e2a 1\n if n <= 3 {\n return 1 * (n - 1)\n }\n // \u8d2a\u5fc3\u5730\u5207\u5206\u51fa 3 \uff0ca \u4e3a 3 \u7684\u4e2a\u6570\uff0cb \u4e3a\u4f59\u6570\n let a = n / 3\n let b = n % 3\n if b == 1 {\n // \u5f53\u4f59\u6570\u4e3a 1 \u65f6\uff0c\u5c06\u4e00\u5bf9 1 * 3 \u8f6c\u5316\u4e3a 2 * 2\n return pow(3, a - 1) * 2 * 2\n }\n if b == 2 {\n // \u5f53\u4f59\u6570\u4e3a 2 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n return pow(3, a) * 2\n }\n // \u5f53\u4f59\u6570\u4e3a 0 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n return pow(3, a)\n}\n
max_product_cutting.js/* \u6700\u5927\u5207\u5206\u4e58\u79ef\uff1a\u8d2a\u5fc3 */\nfunction maxProductCutting(n) {\n // \u5f53 n <= 3 \u65f6\uff0c\u5fc5\u987b\u5207\u5206\u51fa\u4e00\u4e2a 1\n if (n <= 3) {\n return 1 * (n - 1);\n }\n // \u8d2a\u5fc3\u5730\u5207\u5206\u51fa 3 \uff0ca \u4e3a 3 \u7684\u4e2a\u6570\uff0cb \u4e3a\u4f59\u6570\n let a = Math.floor(n / 3);\n let b = n % 3;\n if (b === 1) {\n // \u5f53\u4f59\u6570\u4e3a 1 \u65f6\uff0c\u5c06\u4e00\u5bf9 1 * 3 \u8f6c\u5316\u4e3a 2 * 2\n return Math.pow(3, a - 1) * 2 * 2;\n }\n if (b === 2) {\n // \u5f53\u4f59\u6570\u4e3a 2 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n return Math.pow(3, a) * 2;\n }\n // \u5f53\u4f59\u6570\u4e3a 0 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n return Math.pow(3, a);\n}\n
max_product_cutting.ts/* \u6700\u5927\u5207\u5206\u4e58\u79ef\uff1a\u8d2a\u5fc3 */\nfunction maxProductCutting(n: number): number {\n // \u5f53 n <= 3 \u65f6\uff0c\u5fc5\u987b\u5207\u5206\u51fa\u4e00\u4e2a 1\n if (n <= 3) {\n return 1 * (n - 1);\n }\n // \u8d2a\u5fc3\u5730\u5207\u5206\u51fa 3 \uff0ca \u4e3a 3 \u7684\u4e2a\u6570\uff0cb \u4e3a\u4f59\u6570\n let a: number = Math.floor(n / 3);\n let b: number = n % 3;\n if (b === 1) {\n // \u5f53\u4f59\u6570\u4e3a 1 \u65f6\uff0c\u5c06\u4e00\u5bf9 1 * 3 \u8f6c\u5316\u4e3a 2 * 2\n return Math.pow(3, a - 1) * 2 * 2;\n }\n if (b === 2) {\n // \u5f53\u4f59\u6570\u4e3a 2 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n return Math.pow(3, a) * 2;\n }\n // \u5f53\u4f59\u6570\u4e3a 0 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n return Math.pow(3, a);\n}\n
max_product_cutting.dart/* \u6700\u5927\u5207\u5206\u4e58\u79ef\uff1a\u8d2a\u5fc3 */\nint maxProductCutting(int n) {\n // \u5f53 n <= 3 \u65f6\uff0c\u5fc5\u987b\u5207\u5206\u51fa\u4e00\u4e2a 1\n if (n <= 3) {\n return 1 * (n - 1);\n }\n // \u8d2a\u5fc3\u5730\u5207\u5206\u51fa 3 \uff0ca \u4e3a 3 \u7684\u4e2a\u6570\uff0cb \u4e3a\u4f59\u6570\n int a = n ~/ 3;\n int b = n % 3;\n if (b == 1) {\n // \u5f53\u4f59\u6570\u4e3a 1 \u65f6\uff0c\u5c06\u4e00\u5bf9 1 * 3 \u8f6c\u5316\u4e3a 2 * 2\n return (pow(3, a - 1) * 2 * 2).toInt();\n }\n if (b == 2) {\n // \u5f53\u4f59\u6570\u4e3a 2 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n return (pow(3, a) * 2).toInt();\n }\n // \u5f53\u4f59\u6570\u4e3a 0 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n return pow(3, a).toInt();\n}\n
max_product_cutting.rs/* \u6700\u5927\u5207\u5206\u4e58\u79ef\uff1a\u8d2a\u5fc3 */\nfn max_product_cutting(n: i32) -> i32 {\n // \u5f53 n <= 3 \u65f6\uff0c\u5fc5\u987b\u5207\u5206\u51fa\u4e00\u4e2a 1\n if n <= 3 {\n return 1 * (n - 1);\n }\n // \u8d2a\u5fc3\u5730\u5207\u5206\u51fa 3 \uff0ca \u4e3a 3 \u7684\u4e2a\u6570\uff0cb \u4e3a\u4f59\u6570\n let a = n / 3;\n let b = n % 3;\n if b == 1 {\n // \u5f53\u4f59\u6570\u4e3a 1 \u65f6\uff0c\u5c06\u4e00\u5bf9 1 * 3 \u8f6c\u5316\u4e3a 2 * 2\n 3_i32.pow(a as u32 - 1) * 2 * 2\n } else if b == 2 {\n // \u5f53\u4f59\u6570\u4e3a 2 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n 3_i32.pow(a as u32) * 2\n } else {\n // \u5f53\u4f59\u6570\u4e3a 0 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n 3_i32.pow(a as u32)\n }\n}\n
max_product_cutting.c/* \u6700\u5927\u5207\u5206\u4e58\u79ef\uff1a\u8d2a\u5fc3 */\nint maxProductCutting(int n) {\n // \u5f53 n <= 3 \u65f6\uff0c\u5fc5\u987b\u5207\u5206\u51fa\u4e00\u4e2a 1\n if (n <= 3) {\n return 1 * (n - 1);\n }\n // \u8d2a\u5fc3\u5730\u5207\u5206\u51fa 3 \uff0ca \u4e3a 3 \u7684\u4e2a\u6570\uff0cb \u4e3a\u4f59\u6570\n int a = n / 3;\n int b = n % 3;\n if (b == 1) {\n // \u5f53\u4f59\u6570\u4e3a 1 \u65f6\uff0c\u5c06\u4e00\u5bf9 1 * 3 \u8f6c\u5316\u4e3a 2 * 2\n return pow(3, a - 1) * 2 * 2;\n }\n if (b == 2) {\n // \u5f53\u4f59\u6570\u4e3a 2 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n return pow(3, a) * 2;\n }\n // \u5f53\u4f59\u6570\u4e3a 0 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n return pow(3, a);\n}\n
max_product_cutting.zig[class]{}-[func]{maxProductCutting}\n
\u56fe 15-16 \u00a0 \u6700\u5927\u5207\u5206\u4e58\u79ef\u7684\u8ba1\u7b97\u65b9\u6cd5
\u65f6\u95f4\u590d\u6742\u5ea6\u53d6\u51b3\u4e8e\u7f16\u7a0b\u8bed\u8a00\u7684\u5e42\u8fd0\u7b97\u7684\u5b9e\u73b0\u65b9\u6cd5\u3002\u4ee5 Python \u4e3a\u4f8b\uff0c\u5e38\u7528\u7684\u5e42\u8ba1\u7b97\u51fd\u6570\u6709\u4e09\u79cd\u3002
**
\u548c\u51fd\u6570 pow()
\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u5747\u4e3a \\(O(\\log\u2061 a)\\) \u3002math.pow()
\u5185\u90e8\u8c03\u7528 C \u8bed\u8a00\u5e93\u7684 pow()
\u51fd\u6570\uff0c\u5176\u6267\u884c\u6d6e\u70b9\u53d6\u5e42\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(1)\\) \u3002\u53d8\u91cf \\(a\\) \u548c \\(b\\) \u4f7f\u7528\u5e38\u6570\u5927\u5c0f\u7684\u989d\u5916\u7a7a\u95f4\uff0c\u56e0\u6b64\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(1)\\) \u3002
"},{"location":"chapter_greedy/max_product_cutting_problem/#3","title":"3. \u00a0 \u6b63\u786e\u6027\u8bc1\u660e","text":"\u4f7f\u7528\u53cd\u8bc1\u6cd5\uff0c\u53ea\u5206\u6790 \\(n \\geq 3\\) \u7684\u60c5\u51b5\u3002
Abstract
\u5728\u8ba1\u7b97\u673a\u4e16\u754c\u4e2d\uff0c\u54c8\u5e0c\u8868\u5982\u540c\u4e00\u4f4d\u806a\u6167\u7684\u56fe\u4e66\u7ba1\u7406\u5458\u3002
\u4ed6\u77e5\u9053\u5982\u4f55\u8ba1\u7b97\u7d22\u4e66\u53f7\uff0c\u4ece\u800c\u53ef\u4ee5\u5feb\u901f\u627e\u5230\u76ee\u6807\u56fe\u4e66\u3002
"},{"location":"chapter_hashing/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"\u524d\u4e24\u8282\u4ecb\u7ecd\u4e86\u54c8\u5e0c\u8868\u7684\u5de5\u4f5c\u539f\u7406\u548c\u54c8\u5e0c\u51b2\u7a81\u7684\u5904\u7406\u65b9\u6cd5\u3002\u7136\u800c\u65e0\u8bba\u662f\u5f00\u653e\u5bfb\u5740\u8fd8\u662f\u94fe\u5f0f\u5730\u5740\uff0c\u5b83\u4eec\u53ea\u80fd\u4fdd\u8bc1\u54c8\u5e0c\u8868\u53ef\u4ee5\u5728\u53d1\u751f\u51b2\u7a81\u65f6\u6b63\u5e38\u5de5\u4f5c\uff0c\u800c\u65e0\u6cd5\u51cf\u5c11\u54c8\u5e0c\u51b2\u7a81\u7684\u53d1\u751f\u3002
\u5982\u679c\u54c8\u5e0c\u51b2\u7a81\u8fc7\u4e8e\u9891\u7e41\uff0c\u54c8\u5e0c\u8868\u7684\u6027\u80fd\u5219\u4f1a\u6025\u5267\u52a3\u5316\u3002\u5982\u56fe 6-8 \u6240\u793a\uff0c\u5bf9\u4e8e\u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868\uff0c\u7406\u60f3\u60c5\u51b5\u4e0b\u952e\u503c\u5bf9\u5747\u5300\u5206\u5e03\u5728\u5404\u4e2a\u6876\u4e2d\uff0c\u8fbe\u5230\u6700\u4f73\u67e5\u8be2\u6548\u7387\uff1b\u6700\u5dee\u60c5\u51b5\u4e0b\u6240\u6709\u952e\u503c\u5bf9\u90fd\u5b58\u50a8\u5230\u540c\u4e00\u4e2a\u6876\u4e2d\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u9000\u5316\u81f3 \\(O(n)\\) \u3002
\u56fe 6-8 \u00a0 \u54c8\u5e0c\u51b2\u7a81\u7684\u6700\u4f73\u60c5\u51b5\u4e0e\u6700\u5dee\u60c5\u51b5
\u952e\u503c\u5bf9\u7684\u5206\u5e03\u60c5\u51b5\u7531\u54c8\u5e0c\u51fd\u6570\u51b3\u5b9a\u3002\u56de\u5fc6\u54c8\u5e0c\u51fd\u6570\u7684\u8ba1\u7b97\u6b65\u9aa4\uff0c\u5148\u8ba1\u7b97\u54c8\u5e0c\u503c\uff0c\u518d\u5bf9\u6570\u7ec4\u957f\u5ea6\u53d6\u6a21\uff1a
index = hash(key) % capacity\n
\u89c2\u5bdf\u4ee5\u4e0a\u516c\u5f0f\uff0c\u5f53\u54c8\u5e0c\u8868\u5bb9\u91cf capacity
\u56fa\u5b9a\u65f6\uff0c\u54c8\u5e0c\u7b97\u6cd5 hash()
\u51b3\u5b9a\u4e86\u8f93\u51fa\u503c\uff0c\u8fdb\u800c\u51b3\u5b9a\u4e86\u952e\u503c\u5bf9\u5728\u54c8\u5e0c\u8868\u4e2d\u7684\u5206\u5e03\u60c5\u51b5\u3002
\u8fd9\u610f\u5473\u7740\uff0c\u4e3a\u4e86\u964d\u4f4e\u54c8\u5e0c\u51b2\u7a81\u7684\u53d1\u751f\u6982\u7387\uff0c\u6211\u4eec\u5e94\u5f53\u5c06\u6ce8\u610f\u529b\u96c6\u4e2d\u5728\u54c8\u5e0c\u7b97\u6cd5 hash()
\u7684\u8bbe\u8ba1\u4e0a\u3002
\u4e3a\u4e86\u5b9e\u73b0\u201c\u65e2\u5feb\u53c8\u7a33\u201d\u7684\u54c8\u5e0c\u8868\u6570\u636e\u7ed3\u6784\uff0c\u54c8\u5e0c\u7b97\u6cd5\u5e94\u5177\u5907\u4ee5\u4e0b\u7279\u70b9\u3002
\u5b9e\u9645\u4e0a\uff0c\u54c8\u5e0c\u7b97\u6cd5\u9664\u4e86\u53ef\u4ee5\u7528\u4e8e\u5b9e\u73b0\u54c8\u5e0c\u8868\uff0c\u8fd8\u5e7f\u6cdb\u5e94\u7528\u4e8e\u5176\u4ed6\u9886\u57df\u4e2d\u3002
\u5bf9\u4e8e\u5bc6\u7801\u5b66\u7684\u76f8\u5173\u5e94\u7528\uff0c\u4e3a\u4e86\u9632\u6b62\u4ece\u54c8\u5e0c\u503c\u63a8\u5bfc\u51fa\u539f\u59cb\u5bc6\u7801\u7b49\u9006\u5411\u5de5\u7a0b\uff0c\u54c8\u5e0c\u7b97\u6cd5\u9700\u8981\u5177\u5907\u66f4\u9ad8\u7b49\u7ea7\u7684\u5b89\u5168\u7279\u6027\u3002
\u8bf7\u6ce8\u610f\uff0c\u201c\u5747\u5300\u5206\u5e03\u201d\u4e0e\u201c\u6297\u78b0\u649e\u6027\u201d\u662f\u4e24\u4e2a\u72ec\u7acb\u7684\u6982\u5ff5\uff0c\u6ee1\u8db3\u5747\u5300\u5206\u5e03\u4e0d\u4e00\u5b9a\u6ee1\u8db3\u6297\u78b0\u649e\u6027\u3002\u4f8b\u5982\uff0c\u5728\u968f\u673a\u8f93\u5165 key
\u4e0b\uff0c\u54c8\u5e0c\u51fd\u6570 key % 100
\u53ef\u4ee5\u4ea7\u751f\u5747\u5300\u5206\u5e03\u7684\u8f93\u51fa\u3002\u7136\u800c\u8be5\u54c8\u5e0c\u7b97\u6cd5\u8fc7\u4e8e\u7b80\u5355\uff0c\u6240\u6709\u540e\u4e24\u4f4d\u76f8\u7b49\u7684 key
\u7684\u8f93\u51fa\u90fd\u76f8\u540c\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u4ece\u54c8\u5e0c\u503c\u53cd\u63a8\u51fa\u53ef\u7528\u7684 key
\uff0c\u4ece\u800c\u7834\u89e3\u5bc6\u7801\u3002
\u54c8\u5e0c\u7b97\u6cd5\u7684\u8bbe\u8ba1\u662f\u4e00\u4e2a\u9700\u8981\u8003\u8651\u8bb8\u591a\u56e0\u7d20\u7684\u590d\u6742\u95ee\u9898\u3002\u7136\u800c\u5bf9\u4e8e\u67d0\u4e9b\u8981\u6c42\u4e0d\u9ad8\u7684\u573a\u666f\uff0c\u6211\u4eec\u4e5f\u80fd\u8bbe\u8ba1\u4e00\u4e9b\u7b80\u5355\u7684\u54c8\u5e0c\u7b97\u6cd5\u3002
def add_hash(key: str) -> int:\n \"\"\"\u52a0\u6cd5\u54c8\u5e0c\"\"\"\n hash = 0\n modulus = 1000000007\n for c in key:\n hash += ord(c)\n return hash % modulus\n\ndef mul_hash(key: str) -> int:\n \"\"\"\u4e58\u6cd5\u54c8\u5e0c\"\"\"\n hash = 0\n modulus = 1000000007\n for c in key:\n hash = 31 * hash + ord(c)\n return hash % modulus\n\ndef xor_hash(key: str) -> int:\n \"\"\"\u5f02\u6216\u54c8\u5e0c\"\"\"\n hash = 0\n modulus = 1000000007\n for c in key:\n hash ^= ord(c)\n return hash % modulus\n\ndef rot_hash(key: str) -> int:\n \"\"\"\u65cb\u8f6c\u54c8\u5e0c\"\"\"\n hash = 0\n modulus = 1000000007\n for c in key:\n hash = (hash << 4) ^ (hash >> 28) ^ ord(c)\n return hash % modulus\n
simple_hash.cpp/* \u52a0\u6cd5\u54c8\u5e0c */\nint addHash(string key) {\n long long hash = 0;\n const int MODULUS = 1000000007;\n for (unsigned char c : key) {\n hash = (hash + (int)c) % MODULUS;\n }\n return (int)hash;\n}\n\n/* \u4e58\u6cd5\u54c8\u5e0c */\nint mulHash(string key) {\n long long hash = 0;\n const int MODULUS = 1000000007;\n for (unsigned char c : key) {\n hash = (31 * hash + (int)c) % MODULUS;\n }\n return (int)hash;\n}\n\n/* \u5f02\u6216\u54c8\u5e0c */\nint xorHash(string key) {\n int hash = 0;\n const int MODULUS = 1000000007;\n for (unsigned char c : key) {\n hash ^= (int)c;\n }\n return hash & MODULUS;\n}\n\n/* \u65cb\u8f6c\u54c8\u5e0c */\nint rotHash(string key) {\n long long hash = 0;\n const int MODULUS = 1000000007;\n for (unsigned char c : key) {\n hash = ((hash << 4) ^ (hash >> 28) ^ (int)c) % MODULUS;\n }\n return (int)hash;\n}\n
simple_hash.java/* \u52a0\u6cd5\u54c8\u5e0c */\nint addHash(String key) {\n long hash = 0;\n final int MODULUS = 1000000007;\n for (char c : key.toCharArray()) {\n hash = (hash + (int) c) % MODULUS;\n }\n return (int) hash;\n}\n\n/* \u4e58\u6cd5\u54c8\u5e0c */\nint mulHash(String key) {\n long hash = 0;\n final int MODULUS = 1000000007;\n for (char c : key.toCharArray()) {\n hash = (31 * hash + (int) c) % MODULUS;\n }\n return (int) hash;\n}\n\n/* \u5f02\u6216\u54c8\u5e0c */\nint xorHash(String key) {\n int hash = 0;\n final int MODULUS = 1000000007;\n for (char c : key.toCharArray()) {\n hash ^= (int) c;\n }\n return hash & MODULUS;\n}\n\n/* \u65cb\u8f6c\u54c8\u5e0c */\nint rotHash(String key) {\n long hash = 0;\n final int MODULUS = 1000000007;\n for (char c : key.toCharArray()) {\n hash = ((hash << 4) ^ (hash >> 28) ^ (int) c) % MODULUS;\n }\n return (int) hash;\n}\n
simple_hash.cs/* \u52a0\u6cd5\u54c8\u5e0c */\nint AddHash(string key) {\n long hash = 0;\n const int MODULUS = 1000000007;\n foreach (char c in key) {\n hash = (hash + c) % MODULUS;\n }\n return (int)hash;\n}\n\n/* \u4e58\u6cd5\u54c8\u5e0c */\nint MulHash(string key) {\n long hash = 0;\n const int MODULUS = 1000000007;\n foreach (char c in key) {\n hash = (31 * hash + c) % MODULUS;\n }\n return (int)hash;\n}\n\n/* \u5f02\u6216\u54c8\u5e0c */\nint XorHash(string key) {\n int hash = 0;\n const int MODULUS = 1000000007;\n foreach (char c in key) {\n hash ^= c;\n }\n return hash & MODULUS;\n}\n\n/* \u65cb\u8f6c\u54c8\u5e0c */\nint RotHash(string key) {\n long hash = 0;\n const int MODULUS = 1000000007;\n foreach (char c in key) {\n hash = ((hash << 4) ^ (hash >> 28) ^ c) % MODULUS;\n }\n return (int)hash;\n}\n
simple_hash.go/* \u52a0\u6cd5\u54c8\u5e0c */\nfunc addHash(key string) int {\n var hash int64\n var modulus int64\n\n modulus = 1000000007\n for _, b := range []byte(key) {\n hash = (hash + int64(b)) % modulus\n }\n return int(hash)\n}\n\n/* \u4e58\u6cd5\u54c8\u5e0c */\nfunc mulHash(key string) int {\n var hash int64\n var modulus int64\n\n modulus = 1000000007\n for _, b := range []byte(key) {\n hash = (31*hash + int64(b)) % modulus\n }\n return int(hash)\n}\n\n/* \u5f02\u6216\u54c8\u5e0c */\nfunc xorHash(key string) int {\n hash := 0\n modulus := 1000000007\n for _, b := range []byte(key) {\n fmt.Println(int(b))\n hash ^= int(b)\n hash = (31*hash + int(b)) % modulus\n }\n return hash & modulus\n}\n\n/* \u65cb\u8f6c\u54c8\u5e0c */\nfunc rotHash(key string) int {\n var hash int64\n var modulus int64\n\n modulus = 1000000007\n for _, b := range []byte(key) {\n hash = ((hash << 4) ^ (hash >> 28) ^ int64(b)) % modulus\n }\n return int(hash)\n}\n
simple_hash.swift/* \u52a0\u6cd5\u54c8\u5e0c */\nfunc addHash(key: String) -> Int {\n var hash = 0\n let MODULUS = 1_000_000_007\n for c in key {\n for scalar in c.unicodeScalars {\n hash = (hash + Int(scalar.value)) % MODULUS\n }\n }\n return hash\n}\n\n/* \u4e58\u6cd5\u54c8\u5e0c */\nfunc mulHash(key: String) -> Int {\n var hash = 0\n let MODULUS = 1_000_000_007\n for c in key {\n for scalar in c.unicodeScalars {\n hash = (31 * hash + Int(scalar.value)) % MODULUS\n }\n }\n return hash\n}\n\n/* \u5f02\u6216\u54c8\u5e0c */\nfunc xorHash(key: String) -> Int {\n var hash = 0\n let MODULUS = 1_000_000_007\n for c in key {\n for scalar in c.unicodeScalars {\n hash ^= Int(scalar.value)\n }\n }\n return hash & MODULUS\n}\n\n/* \u65cb\u8f6c\u54c8\u5e0c */\nfunc rotHash(key: String) -> Int {\n var hash = 0\n let MODULUS = 1_000_000_007\n for c in key {\n for scalar in c.unicodeScalars {\n hash = ((hash << 4) ^ (hash >> 28) ^ Int(scalar.value)) % MODULUS\n }\n }\n return hash\n}\n
simple_hash.js/* \u52a0\u6cd5\u54c8\u5e0c */\nfunction addHash(key) {\n let hash = 0;\n const MODULUS = 1000000007;\n for (const c of key) {\n hash = (hash + c.charCodeAt(0)) % MODULUS;\n }\n return hash;\n}\n\n/* \u4e58\u6cd5\u54c8\u5e0c */\nfunction mulHash(key) {\n let hash = 0;\n const MODULUS = 1000000007;\n for (const c of key) {\n hash = (31 * hash + c.charCodeAt(0)) % MODULUS;\n }\n return hash;\n}\n\n/* \u5f02\u6216\u54c8\u5e0c */\nfunction xorHash(key) {\n let hash = 0;\n const MODULUS = 1000000007;\n for (const c of key) {\n hash ^= c.charCodeAt(0);\n }\n return hash & MODULUS;\n}\n\n/* \u65cb\u8f6c\u54c8\u5e0c */\nfunction rotHash(key) {\n let hash = 0;\n const MODULUS = 1000000007;\n for (const c of key) {\n hash = ((hash << 4) ^ (hash >> 28) ^ c.charCodeAt(0)) % MODULUS;\n }\n return hash;\n}\n
simple_hash.ts/* \u52a0\u6cd5\u54c8\u5e0c */\nfunction addHash(key: string): number {\n let hash = 0;\n const MODULUS = 1000000007;\n for (const c of key) {\n hash = (hash + c.charCodeAt(0)) % MODULUS;\n }\n return hash;\n}\n\n/* \u4e58\u6cd5\u54c8\u5e0c */\nfunction mulHash(key: string): number {\n let hash = 0;\n const MODULUS = 1000000007;\n for (const c of key) {\n hash = (31 * hash + c.charCodeAt(0)) % MODULUS;\n }\n return hash;\n}\n\n/* \u5f02\u6216\u54c8\u5e0c */\nfunction xorHash(key: string): number {\n let hash = 0;\n const MODULUS = 1000000007;\n for (const c of key) {\n hash ^= c.charCodeAt(0);\n }\n return hash & MODULUS;\n}\n\n/* \u65cb\u8f6c\u54c8\u5e0c */\nfunction rotHash(key: string): number {\n let hash = 0;\n const MODULUS = 1000000007;\n for (const c of key) {\n hash = ((hash << 4) ^ (hash >> 28) ^ c.charCodeAt(0)) % MODULUS;\n }\n return hash;\n}\n
simple_hash.dart/* \u52a0\u6cd5\u54c8\u5e0c */\nint addHash(String key) {\n int hash = 0;\n final int MODULUS = 1000000007;\n for (int i = 0; i < key.length; i++) {\n hash = (hash + key.codeUnitAt(i)) % MODULUS;\n }\n return hash;\n}\n\n/* \u4e58\u6cd5\u54c8\u5e0c */\nint mulHash(String key) {\n int hash = 0;\n final int MODULUS = 1000000007;\n for (int i = 0; i < key.length; i++) {\n hash = (31 * hash + key.codeUnitAt(i)) % MODULUS;\n }\n return hash;\n}\n\n/* \u5f02\u6216\u54c8\u5e0c */\nint xorHash(String key) {\n int hash = 0;\n final int MODULUS = 1000000007;\n for (int i = 0; i < key.length; i++) {\n hash ^= key.codeUnitAt(i);\n }\n return hash & MODULUS;\n}\n\n/* \u65cb\u8f6c\u54c8\u5e0c */\nint rotHash(String key) {\n int hash = 0;\n final int MODULUS = 1000000007;\n for (int i = 0; i < key.length; i++) {\n hash = ((hash << 4) ^ (hash >> 28) ^ key.codeUnitAt(i)) % MODULUS;\n }\n return hash;\n}\n
simple_hash.rs/* \u52a0\u6cd5\u54c8\u5e0c */\nfn add_hash(key: &str) -> i32 {\n let mut hash = 0_i64;\n const MODULUS: i64 = 1000000007;\n\n for c in key.chars() {\n hash = (hash + c as i64) % MODULUS;\n }\n\n hash as i32\n} \n\n/* \u4e58\u6cd5\u54c8\u5e0c */\nfn mul_hash(key: &str) -> i32 {\n let mut hash = 0_i64;\n const MODULUS: i64 = 1000000007;\n\n for c in key.chars() {\n hash = (31 * hash + c as i64) % MODULUS;\n }\n\n hash as i32\n}\n\n/* \u5f02\u6216\u54c8\u5e0c */\nfn xor_hash(key: &str) -> i32 {\n let mut hash = 0_i64;\n const MODULUS: i64 = 1000000007;\n\n for c in key.chars() {\n hash ^= c as i64;\n }\n\n (hash & MODULUS) as i32\n}\n\n/* \u65cb\u8f6c\u54c8\u5e0c */\nfn rot_hash(key: &str) -> i32 {\n let mut hash = 0_i64;\n const MODULUS: i64 = 1000000007;\n\n for c in key.chars() {\n hash = ((hash << 4) ^ (hash >> 28) ^ c as i64) % MODULUS;\n }\n\n hash as i32\n}\n
simple_hash.c/* \u52a0\u6cd5\u54c8\u5e0c */\nint addHash(char *key) {\n long long hash = 0;\n const int MODULUS = 1000000007;\n for (int i = 0; i < strlen(key); i++) {\n hash = (hash + (unsigned char)key[i]) % MODULUS;\n }\n return (int)hash;\n}\n\n/* \u4e58\u6cd5\u54c8\u5e0c */\nint mulHash(char *key) {\n long long hash = 0;\n const int MODULUS = 1000000007;\n for (int i = 0; i < strlen(key); i++) {\n hash = (31 * hash + (unsigned char)key[i]) % MODULUS;\n }\n return (int)hash;\n}\n\n/* \u5f02\u6216\u54c8\u5e0c */\nint xorHash(char *key) {\n int hash = 0;\n const int MODULUS = 1000000007;\n\n for (int i = 0; i < strlen(key); i++) {\n hash ^= (unsigned char)key[i];\n }\n return hash & MODULUS;\n}\n\n/* \u65cb\u8f6c\u54c8\u5e0c */\nint rotHash(char *key) {\n long long hash = 0;\n const int MODULUS = 1000000007;\n for (int i = 0; i < strlen(key); i++) {\n hash = ((hash << 4) ^ (hash >> 28) ^ (unsigned char)key[i]) % MODULUS;\n }\n\n return (int)hash;\n}\n
simple_hash.zig[class]{}-[func]{addHash}\n\n[class]{}-[func]{mulHash}\n\n[class]{}-[func]{xorHash}\n\n[class]{}-[func]{rotHash}\n
\u89c2\u5bdf\u53d1\u73b0\uff0c\u6bcf\u79cd\u54c8\u5e0c\u7b97\u6cd5\u7684\u6700\u540e\u4e00\u6b65\u90fd\u662f\u5bf9\u5927\u8d28\u6570 \\(1000000007\\) \u53d6\u6a21\uff0c\u4ee5\u786e\u4fdd\u54c8\u5e0c\u503c\u5728\u5408\u9002\u7684\u8303\u56f4\u5185\u3002\u503c\u5f97\u601d\u8003\u7684\u662f\uff0c\u4e3a\u4ec0\u4e48\u8981\u5f3a\u8c03\u5bf9\u8d28\u6570\u53d6\u6a21\uff0c\u6216\u8005\u8bf4\u5bf9\u5408\u6570\u53d6\u6a21\u7684\u5f0a\u7aef\u662f\u4ec0\u4e48\uff1f\u8fd9\u662f\u4e00\u4e2a\u6709\u8da3\u7684\u95ee\u9898\u3002
\u5148\u629b\u51fa\u7ed3\u8bba\uff1a\u4f7f\u7528\u5927\u8d28\u6570\u4f5c\u4e3a\u6a21\u6570\uff0c\u53ef\u4ee5\u6700\u5927\u5316\u5730\u4fdd\u8bc1\u54c8\u5e0c\u503c\u7684\u5747\u5300\u5206\u5e03\u3002\u56e0\u4e3a\u8d28\u6570\u4e0d\u4e0e\u5176\u4ed6\u6570\u5b57\u5b58\u5728\u516c\u7ea6\u6570\uff0c\u53ef\u4ee5\u51cf\u5c11\u56e0\u53d6\u6a21\u64cd\u4f5c\u800c\u4ea7\u751f\u7684\u5468\u671f\u6027\u6a21\u5f0f\uff0c\u4ece\u800c\u907f\u514d\u54c8\u5e0c\u51b2\u7a81\u3002
\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u5047\u8bbe\u6211\u4eec\u9009\u62e9\u5408\u6570 \\(9\\) \u4f5c\u4e3a\u6a21\u6570\uff0c\u5b83\u53ef\u4ee5\u88ab \\(3\\) \u6574\u9664\uff0c\u90a3\u4e48\u6240\u6709\u53ef\u4ee5\u88ab \\(3\\) \u6574\u9664\u7684 key
\u90fd\u4f1a\u88ab\u6620\u5c04\u5230 \\(0\\)\u3001\\(3\\)\u3001\\(6\\) \u8fd9\u4e09\u4e2a\u54c8\u5e0c\u503c\u3002
\u5982\u679c\u8f93\u5165 key
\u6070\u597d\u6ee1\u8db3\u8fd9\u79cd\u7b49\u5dee\u6570\u5217\u7684\u6570\u636e\u5206\u5e03\uff0c\u90a3\u4e48\u54c8\u5e0c\u503c\u5c31\u4f1a\u51fa\u73b0\u805a\u5806\uff0c\u4ece\u800c\u52a0\u91cd\u54c8\u5e0c\u51b2\u7a81\u3002\u73b0\u5728\uff0c\u5047\u8bbe\u5c06 modulus
\u66ff\u6362\u4e3a\u8d28\u6570 \\(13\\) \uff0c\u7531\u4e8e key
\u548c modulus
\u4e4b\u95f4\u4e0d\u5b58\u5728\u516c\u7ea6\u6570\uff0c\u56e0\u6b64\u8f93\u51fa\u7684\u54c8\u5e0c\u503c\u7684\u5747\u5300\u6027\u4f1a\u660e\u663e\u63d0\u5347\u3002
\u503c\u5f97\u8bf4\u660e\u7684\u662f\uff0c\u5982\u679c\u80fd\u591f\u4fdd\u8bc1 key
\u662f\u968f\u673a\u5747\u5300\u5206\u5e03\u7684\uff0c\u90a3\u4e48\u9009\u62e9\u8d28\u6570\u6216\u8005\u5408\u6570\u4f5c\u4e3a\u6a21\u6570\u90fd\u53ef\u4ee5\uff0c\u5b83\u4eec\u90fd\u80fd\u8f93\u51fa\u5747\u5300\u5206\u5e03\u7684\u54c8\u5e0c\u503c\u3002\u800c\u5f53 key
\u7684\u5206\u5e03\u5b58\u5728\u67d0\u79cd\u5468\u671f\u6027\u65f6\uff0c\u5bf9\u5408\u6570\u53d6\u6a21\u66f4\u5bb9\u6613\u51fa\u73b0\u805a\u96c6\u73b0\u8c61\u3002
\u603b\u800c\u8a00\u4e4b\uff0c\u6211\u4eec\u901a\u5e38\u9009\u53d6\u8d28\u6570\u4f5c\u4e3a\u6a21\u6570\uff0c\u5e76\u4e14\u8fd9\u4e2a\u8d28\u6570\u6700\u597d\u8db3\u591f\u5927\uff0c\u4ee5\u5c3d\u53ef\u80fd\u6d88\u9664\u5468\u671f\u6027\u6a21\u5f0f\uff0c\u63d0\u5347\u54c8\u5e0c\u7b97\u6cd5\u7684\u7a33\u5065\u6027\u3002
"},{"location":"chapter_hashing/hash_algorithm/#633","title":"6.3.3 \u00a0 \u5e38\u89c1\u54c8\u5e0c\u7b97\u6cd5","text":"\u4e0d\u96be\u53d1\u73b0\uff0c\u4ee5\u4e0a\u4ecb\u7ecd\u7684\u7b80\u5355\u54c8\u5e0c\u7b97\u6cd5\u90fd\u6bd4\u8f83\u201c\u8106\u5f31\u201d\uff0c\u8fdc\u8fdc\u6ca1\u6709\u8fbe\u5230\u54c8\u5e0c\u7b97\u6cd5\u7684\u8bbe\u8ba1\u76ee\u6807\u3002\u4f8b\u5982\uff0c\u7531\u4e8e\u52a0\u6cd5\u548c\u5f02\u6216\u6ee1\u8db3\u4ea4\u6362\u5f8b\uff0c\u56e0\u6b64\u52a0\u6cd5\u54c8\u5e0c\u548c\u5f02\u6216\u54c8\u5e0c\u65e0\u6cd5\u533a\u5206\u5185\u5bb9\u76f8\u540c\u4f46\u987a\u5e8f\u4e0d\u540c\u7684\u5b57\u7b26\u4e32\uff0c\u8fd9\u53ef\u80fd\u4f1a\u52a0\u5267\u54c8\u5e0c\u51b2\u7a81\uff0c\u5e76\u5f15\u8d77\u4e00\u4e9b\u5b89\u5168\u95ee\u9898\u3002
\u5728\u5b9e\u9645\u4e2d\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u7528\u4e00\u4e9b\u6807\u51c6\u54c8\u5e0c\u7b97\u6cd5\uff0c\u4f8b\u5982 MD5\u3001SHA-1\u3001SHA-2\u3001SHA-3 \u7b49\u3002\u5b83\u4eec\u53ef\u4ee5\u5c06\u4efb\u610f\u957f\u5ea6\u7684\u8f93\u5165\u6570\u636e\u6620\u5c04\u5230\u6052\u5b9a\u957f\u5ea6\u7684\u54c8\u5e0c\u503c\u3002
\u8fd1\u4e00\u4e2a\u4e16\u7eaa\u4ee5\u6765\uff0c\u54c8\u5e0c\u7b97\u6cd5\u5904\u5728\u4e0d\u65ad\u5347\u7ea7\u4e0e\u4f18\u5316\u7684\u8fc7\u7a0b\u4e2d\u3002\u4e00\u90e8\u5206\u7814\u7a76\u4eba\u5458\u52aa\u529b\u63d0\u5347\u54c8\u5e0c\u7b97\u6cd5\u7684\u6027\u80fd\uff0c\u53e6\u4e00\u90e8\u5206\u7814\u7a76\u4eba\u5458\u548c\u9ed1\u5ba2\u5219\u81f4\u529b\u4e8e\u5bfb\u627e\u54c8\u5e0c\u7b97\u6cd5\u7684\u5b89\u5168\u6027\u95ee\u9898\u3002\u8868 6-2 \u5c55\u793a\u4e86\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u5e38\u89c1\u7684\u54c8\u5e0c\u7b97\u6cd5\u3002
\u8868 6-2 \u00a0 \u5e38\u89c1\u7684\u54c8\u5e0c\u7b97\u6cd5
MD5 SHA-1 SHA-2 SHA-3 \u63a8\u51fa\u65f6\u95f4 1992 1995 2002 2008 \u8f93\u51fa\u957f\u5ea6 128 bits 160 bits 256/512 bits 224/256/384/512 bits \u54c8\u5e0c\u51b2\u7a81 \u8f83\u591a \u8f83\u591a \u5f88\u5c11 \u5f88\u5c11 \u5b89\u5168\u7b49\u7ea7 \u4f4e\uff0c\u5df2\u88ab\u6210\u529f\u653b\u51fb \u4f4e\uff0c\u5df2\u88ab\u6210\u529f\u653b\u51fb \u9ad8 \u9ad8 \u5e94\u7528 \u5df2\u88ab\u5f03\u7528\uff0c\u4ecd\u7528\u4e8e\u6570\u636e\u5b8c\u6574\u6027\u68c0\u67e5 \u5df2\u88ab\u5f03\u7528 \u52a0\u5bc6\u8d27\u5e01\u4ea4\u6613\u9a8c\u8bc1\u3001\u6570\u5b57\u7b7e\u540d\u7b49 \u53ef\u7528\u4e8e\u66ff\u4ee3 SHA-2"},{"location":"chapter_hashing/hash_algorithm/#634","title":"6.3.4 \u00a0 \u6570\u636e\u7ed3\u6784\u7684\u54c8\u5e0c\u503c","text":"\u6211\u4eec\u77e5\u9053\uff0c\u54c8\u5e0c\u8868\u7684 key
\u53ef\u4ee5\u662f\u6574\u6570\u3001\u5c0f\u6570\u6216\u5b57\u7b26\u4e32\u7b49\u6570\u636e\u7c7b\u578b\u3002\u7f16\u7a0b\u8bed\u8a00\u901a\u5e38\u4f1a\u4e3a\u8fd9\u4e9b\u6570\u636e\u7c7b\u578b\u63d0\u4f9b\u5185\u7f6e\u7684\u54c8\u5e0c\u7b97\u6cd5\uff0c\u7528\u4e8e\u8ba1\u7b97\u54c8\u5e0c\u8868\u4e2d\u7684\u6876\u7d22\u5f15\u3002\u4ee5 Python \u4e3a\u4f8b\uff0c\u6211\u4eec\u53ef\u4ee5\u8c03\u7528 hash()
\u51fd\u6570\u6765\u8ba1\u7b97\u5404\u79cd\u6570\u636e\u7c7b\u578b\u7684\u54c8\u5e0c\u503c\u3002
Tip
\u8bf7\u6ce8\u610f\uff0c\u4e0d\u540c\u7f16\u7a0b\u8bed\u8a00\u7684\u5185\u7f6e\u54c8\u5e0c\u503c\u8ba1\u7b97\u51fd\u6570\u7684\u5b9a\u4e49\u548c\u65b9\u6cd5\u4e0d\u540c\u3002
PythonC++JavaC#GoSwiftJSTSDartRustCZig built_in_hash.pynum = 3\nhash_num = hash(num)\n# \u6574\u6570 3 \u7684\u54c8\u5e0c\u503c\u4e3a 3\n\nbol = True\nhash_bol = hash(bol)\n# \u5e03\u5c14\u91cf True \u7684\u54c8\u5e0c\u503c\u4e3a 1\n\ndec = 3.14159\nhash_dec = hash(dec)\n# \u5c0f\u6570 3.14159 \u7684\u54c8\u5e0c\u503c\u4e3a 326484311674566659\n\nstr = \"Hello \u7b97\u6cd5\"\nhash_str = hash(str)\n# \u5b57\u7b26\u4e32\u201cHello \u7b97\u6cd5\u201d\u7684\u54c8\u5e0c\u503c\u4e3a 4617003410720528961\n\ntup = (12836, \"\u5c0f\u54c8\")\nhash_tup = hash(tup)\n# \u5143\u7ec4 (12836, '\u5c0f\u54c8') \u7684\u54c8\u5e0c\u503c\u4e3a 1029005403108185979\n\nobj = ListNode(0)\nhash_obj = hash(obj)\n# \u8282\u70b9\u5bf9\u8c61 <ListNode object at 0x1058fd810> \u7684\u54c8\u5e0c\u503c\u4e3a 274267521\n
built_in_hash.cppint num = 3;\nsize_t hashNum = hash<int>()(num);\n// \u6574\u6570 3 \u7684\u54c8\u5e0c\u503c\u4e3a 3\n\nbool bol = true;\nsize_t hashBol = hash<bool>()(bol);\n// \u5e03\u5c14\u91cf 1 \u7684\u54c8\u5e0c\u503c\u4e3a 1\n\ndouble dec = 3.14159;\nsize_t hashDec = hash<double>()(dec);\n// \u5c0f\u6570 3.14159 \u7684\u54c8\u5e0c\u503c\u4e3a 4614256650576692846\n\nstring str = \"Hello \u7b97\u6cd5\";\nsize_t hashStr = hash<string>()(str);\n// \u5b57\u7b26\u4e32\u201cHello \u7b97\u6cd5\u201d\u7684\u54c8\u5e0c\u503c\u4e3a 15466937326284535026\n\n// \u5728 C++ \u4e2d\uff0c\u5185\u7f6e std:hash() \u4ec5\u63d0\u4f9b\u57fa\u672c\u6570\u636e\u7c7b\u578b\u7684\u54c8\u5e0c\u503c\u8ba1\u7b97\n// \u6570\u7ec4\u3001\u5bf9\u8c61\u7684\u54c8\u5e0c\u503c\u8ba1\u7b97\u9700\u8981\u81ea\u884c\u5b9e\u73b0\n
built_in_hash.javaint num = 3;\nint hashNum = Integer.hashCode(num);\n// \u6574\u6570 3 \u7684\u54c8\u5e0c\u503c\u4e3a 3\n\nboolean bol = true;\nint hashBol = Boolean.hashCode(bol);\n// \u5e03\u5c14\u91cf true \u7684\u54c8\u5e0c\u503c\u4e3a 1231\n\ndouble dec = 3.14159;\nint hashDec = Double.hashCode(dec);\n// \u5c0f\u6570 3.14159 \u7684\u54c8\u5e0c\u503c\u4e3a -1340954729\n\nString str = \"Hello \u7b97\u6cd5\";\nint hashStr = str.hashCode();\n// \u5b57\u7b26\u4e32\u201cHello \u7b97\u6cd5\u201d\u7684\u54c8\u5e0c\u503c\u4e3a -727081396\n\nObject[] arr = { 12836, \"\u5c0f\u54c8\" };\nint hashTup = Arrays.hashCode(arr);\n// \u6570\u7ec4 [12836, \u5c0f\u54c8] \u7684\u54c8\u5e0c\u503c\u4e3a 1151158\n\nListNode obj = new ListNode(0);\nint hashObj = obj.hashCode();\n// \u8282\u70b9\u5bf9\u8c61 utils.ListNode@7dc5e7b4 \u7684\u54c8\u5e0c\u503c\u4e3a 2110121908\n
built_in_hash.csint num = 3;\nint hashNum = num.GetHashCode();\n// \u6574\u6570 3 \u7684\u54c8\u5e0c\u503c\u4e3a 3;\n\nbool bol = true;\nint hashBol = bol.GetHashCode();\n// \u5e03\u5c14\u91cf true \u7684\u54c8\u5e0c\u503c\u4e3a 1;\n\ndouble dec = 3.14159;\nint hashDec = dec.GetHashCode();\n// \u5c0f\u6570 3.14159 \u7684\u54c8\u5e0c\u503c\u4e3a -1340954729;\n\nstring str = \"Hello \u7b97\u6cd5\";\nint hashStr = str.GetHashCode();\n// \u5b57\u7b26\u4e32\u201cHello \u7b97\u6cd5\u201d\u7684\u54c8\u5e0c\u503c\u4e3a -586107568;\n\nobject[] arr = [12836, \"\u5c0f\u54c8\"];\nint hashTup = arr.GetHashCode();\n// \u6570\u7ec4 [12836, \u5c0f\u54c8] \u7684\u54c8\u5e0c\u503c\u4e3a 42931033;\n\nListNode obj = new(0);\nint hashObj = obj.GetHashCode();\n// \u8282\u70b9\u5bf9\u8c61 0 \u7684\u54c8\u5e0c\u503c\u4e3a 39053774;\n
built_in_hash.go// Go \u672a\u63d0\u4f9b\u5185\u7f6e hash code \u51fd\u6570\n
built_in_hash.swiftlet num = 3\nlet hashNum = num.hashValue\n// \u6574\u6570 3 \u7684\u54c8\u5e0c\u503c\u4e3a 9047044699613009734\n\nlet bol = true\nlet hashBol = bol.hashValue\n// \u5e03\u5c14\u91cf true \u7684\u54c8\u5e0c\u503c\u4e3a -4431640247352757451\n\nlet dec = 3.14159\nlet hashDec = dec.hashValue\n// \u5c0f\u6570 3.14159 \u7684\u54c8\u5e0c\u503c\u4e3a -2465384235396674631\n\nlet str = \"Hello \u7b97\u6cd5\"\nlet hashStr = str.hashValue\n// \u5b57\u7b26\u4e32\u201cHello \u7b97\u6cd5\u201d\u7684\u54c8\u5e0c\u503c\u4e3a -7850626797806988787\n\nlet arr = [AnyHashable(12836), AnyHashable(\"\u5c0f\u54c8\")]\nlet hashTup = arr.hashValue\n// \u6570\u7ec4 [AnyHashable(12836), AnyHashable(\"\u5c0f\u54c8\")] \u7684\u54c8\u5e0c\u503c\u4e3a -2308633508154532996\n\nlet obj = ListNode(x: 0)\nlet hashObj = obj.hashValue\n// \u8282\u70b9\u5bf9\u8c61 utils.ListNode \u7684\u54c8\u5e0c\u503c\u4e3a -2434780518035996159\n
built_in_hash.js// JavaScript \u672a\u63d0\u4f9b\u5185\u7f6e hash code \u51fd\u6570\n
built_in_hash.ts// TypeScript \u672a\u63d0\u4f9b\u5185\u7f6e hash code \u51fd\u6570\n
built_in_hash.dartint num = 3;\nint hashNum = num.hashCode;\n// \u6574\u6570 3 \u7684\u54c8\u5e0c\u503c\u4e3a 34803\n\nbool bol = true;\nint hashBol = bol.hashCode;\n// \u5e03\u5c14\u503c true \u7684\u54c8\u5e0c\u503c\u4e3a 1231\n\ndouble dec = 3.14159;\nint hashDec = dec.hashCode;\n// \u5c0f\u6570 3.14159 \u7684\u54c8\u5e0c\u503c\u4e3a 2570631074981783\n\nString str = \"Hello \u7b97\u6cd5\";\nint hashStr = str.hashCode;\n// \u5b57\u7b26\u4e32\u201cHello \u7b97\u6cd5\u201d\u7684\u54c8\u5e0c\u503c\u4e3a 468167534\n\nList arr = [12836, \"\u5c0f\u54c8\"];\nint hashArr = arr.hashCode;\n// \u6570\u7ec4 [12836, \u5c0f\u54c8] \u7684\u54c8\u5e0c\u503c\u4e3a 976512528\n\nListNode obj = new ListNode(0);\nint hashObj = obj.hashCode;\n// \u8282\u70b9\u5bf9\u8c61 Instance of 'ListNode' \u7684\u54c8\u5e0c\u503c\u4e3a 1033450432\n
built_in_hash.rsuse std::collections::hash_map::DefaultHasher;\nuse std::hash::{Hash, Hasher};\n\nlet num = 3;\nlet mut num_hasher = DefaultHasher::new();\nnum.hash(&mut num_hasher);\nlet hash_num = num_hasher.finish();\n// \u6574\u6570 3 \u7684\u54c8\u5e0c\u503c\u4e3a 568126464209439262\n\nlet bol = true;\nlet mut bol_hasher = DefaultHasher::new();\nbol.hash(&mut bol_hasher);\nlet hash_bol = bol_hasher.finish();\n// \u5e03\u5c14\u91cf true \u7684\u54c8\u5e0c\u503c\u4e3a 4952851536318644461\n\nlet dec: f32 = 3.14159;\nlet mut dec_hasher = DefaultHasher::new();\ndec.to_bits().hash(&mut dec_hasher);\nlet hash_dec = dec_hasher.finish();\nprintln!(\"\u5c0f\u6570 {} \u7684\u54c8\u5e0c\u503c\u4e3a {}\", dec, hash_dec);\n// \u5c0f\u6570 3.14159 \u7684\u54c8\u5e0c\u503c\u4e3a 2566941990314602357\n\nlet str = \"Hello \u7b97\u6cd5\";\nlet mut str_hasher = DefaultHasher::new();\nstr.hash(&mut str_hasher);\nlet hash_str = str_hasher.finish();\n// \u5b57\u7b26\u4e32\u201cHello \u7b97\u6cd5\u201d\u7684\u54c8\u5e0c\u503c\u4e3a 16092673739211250988\n\nlet arr = (&12836, &\"\u5c0f\u54c8\");\nlet mut tup_hasher = DefaultHasher::new();\narr.hash(&mut tup_hasher);\nlet hash_tup = tup_hasher.finish();\n// \u5143\u7ec4 (12836, \"\u5c0f\u54c8\") \u7684\u54c8\u5e0c\u503c\u4e3a 1885128010422702749\n\nlet node = ListNode::new(42);\nlet mut hasher = DefaultHasher::new();\nnode.borrow().val.hash(&mut hasher);\nlet hash = hasher.finish();\n// \u8282\u70b9\u5bf9\u8c61 RefCell { value: ListNode { val: 42, next: None } } \u7684\u54c8\u5e0c\u503c\u4e3a15387811073369036852\n
built_in_hash.c// C \u672a\u63d0\u4f9b\u5185\u7f6e hash code \u51fd\u6570\n
built_in_hash.zig\n
\u5728\u8bb8\u591a\u7f16\u7a0b\u8bed\u8a00\u4e2d\uff0c\u53ea\u6709\u4e0d\u53ef\u53d8\u5bf9\u8c61\u624d\u53ef\u4f5c\u4e3a\u54c8\u5e0c\u8868\u7684 key
\u3002\u5047\u5982\u6211\u4eec\u5c06\u5217\u8868\uff08\u52a8\u6001\u6570\u7ec4\uff09\u4f5c\u4e3a key
\uff0c\u5f53\u5217\u8868\u7684\u5185\u5bb9\u53d1\u751f\u53d8\u5316\u65f6\uff0c\u5b83\u7684\u54c8\u5e0c\u503c\u4e5f\u968f\u4e4b\u6539\u53d8\uff0c\u6211\u4eec\u5c31\u65e0\u6cd5\u5728\u54c8\u5e0c\u8868\u4e2d\u67e5\u8be2\u5230\u539f\u5148\u7684 value
\u4e86\u3002
\u867d\u7136\u81ea\u5b9a\u4e49\u5bf9\u8c61\uff08\u6bd4\u5982\u94fe\u8868\u8282\u70b9\uff09\u7684\u6210\u5458\u53d8\u91cf\u662f\u53ef\u53d8\u7684\uff0c\u4f46\u5b83\u662f\u53ef\u54c8\u5e0c\u7684\u3002\u8fd9\u662f\u56e0\u4e3a\u5bf9\u8c61\u7684\u54c8\u5e0c\u503c\u901a\u5e38\u662f\u57fa\u4e8e\u5185\u5b58\u5730\u5740\u751f\u6210\u7684\uff0c\u5373\u4f7f\u5bf9\u8c61\u7684\u5185\u5bb9\u53d1\u751f\u4e86\u53d8\u5316\uff0c\u4f46\u5b83\u7684\u5185\u5b58\u5730\u5740\u4e0d\u53d8\uff0c\u54c8\u5e0c\u503c\u4ecd\u7136\u662f\u4e0d\u53d8\u7684\u3002
\u7ec6\u5fc3\u7684\u4f60\u53ef\u80fd\u53d1\u73b0\u5728\u4e0d\u540c\u63a7\u5236\u53f0\u4e2d\u8fd0\u884c\u7a0b\u5e8f\u65f6\uff0c\u8f93\u51fa\u7684\u54c8\u5e0c\u503c\u662f\u4e0d\u540c\u7684\u3002\u8fd9\u662f\u56e0\u4e3a Python \u89e3\u91ca\u5668\u5728\u6bcf\u6b21\u542f\u52a8\u65f6\uff0c\u90fd\u4f1a\u4e3a\u5b57\u7b26\u4e32\u54c8\u5e0c\u51fd\u6570\u52a0\u5165\u4e00\u4e2a\u968f\u673a\u7684\u76d0\uff08Salt\uff09\u503c\u3002\u8fd9\u79cd\u505a\u6cd5\u53ef\u4ee5\u6709\u6548\u9632\u6b62 HashDoS \u653b\u51fb\uff0c\u63d0\u5347\u54c8\u5e0c\u7b97\u6cd5\u7684\u5b89\u5168\u6027\u3002
"},{"location":"chapter_hashing/hash_collision/","title":"6.2 \u00a0 \u54c8\u5e0c\u51b2\u7a81","text":"\u4e0a\u4e00\u8282\u63d0\u5230\uff0c\u901a\u5e38\u60c5\u51b5\u4e0b\u54c8\u5e0c\u51fd\u6570\u7684\u8f93\u5165\u7a7a\u95f4\u8fdc\u5927\u4e8e\u8f93\u51fa\u7a7a\u95f4\uff0c\u56e0\u6b64\u7406\u8bba\u4e0a\u54c8\u5e0c\u51b2\u7a81\u662f\u4e0d\u53ef\u907f\u514d\u7684\u3002\u6bd4\u5982\uff0c\u8f93\u5165\u7a7a\u95f4\u4e3a\u5168\u4f53\u6574\u6570\uff0c\u8f93\u51fa\u7a7a\u95f4\u4e3a\u6570\u7ec4\u5bb9\u91cf\u5927\u5c0f\uff0c\u5219\u5fc5\u7136\u6709\u591a\u4e2a\u6574\u6570\u6620\u5c04\u81f3\u540c\u4e00\u6876\u7d22\u5f15\u3002
\u54c8\u5e0c\u51b2\u7a81\u4f1a\u5bfc\u81f4\u67e5\u8be2\u7ed3\u679c\u9519\u8bef\uff0c\u4e25\u91cd\u5f71\u54cd\u54c8\u5e0c\u8868\u7684\u53ef\u7528\u6027\u3002\u4e3a\u89e3\u51b3\u8be5\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u6bcf\u5f53\u9047\u5230\u54c8\u5e0c\u51b2\u7a81\u5c31\u8fdb\u884c\u54c8\u5e0c\u8868\u6269\u5bb9\uff0c\u76f4\u81f3\u51b2\u7a81\u6d88\u5931\u4e3a\u6b62\u3002\u6b64\u65b9\u6cd5\u7b80\u5355\u7c97\u66b4\u4e14\u6709\u6548\uff0c\u4f46\u6548\u7387\u592a\u4f4e\uff0c\u56e0\u4e3a\u54c8\u5e0c\u8868\u6269\u5bb9\u9700\u8981\u8fdb\u884c\u5927\u91cf\u7684\u6570\u636e\u642c\u8fd0\u4e0e\u54c8\u5e0c\u503c\u8ba1\u7b97\u3002\u4e3a\u4e86\u63d0\u5347\u6548\u7387\uff0c\u6211\u4eec\u53ef\u4ee5\u91c7\u7528\u4ee5\u4e0b\u7b56\u7565\u3002
\u54c8\u5e0c\u8868\u7684\u7ed3\u6784\u6539\u826f\u65b9\u6cd5\u4e3b\u8981\u5305\u62ec\u201c\u94fe\u5f0f\u5730\u5740\u201d\u548c\u201c\u5f00\u653e\u5bfb\u5740\u201d\u3002
"},{"location":"chapter_hashing/hash_collision/#621","title":"6.2.1 \u00a0 \u94fe\u5f0f\u5730\u5740","text":"\u5728\u539f\u59cb\u54c8\u5e0c\u8868\u4e2d\uff0c\u6bcf\u4e2a\u6876\u4ec5\u80fd\u5b58\u50a8\u4e00\u4e2a\u952e\u503c\u5bf9\u3002\u300c\u94fe\u5f0f\u5730\u5740 separate chaining\u300d\u5c06\u5355\u4e2a\u5143\u7d20\u8f6c\u6362\u4e3a\u94fe\u8868\uff0c\u5c06\u952e\u503c\u5bf9\u4f5c\u4e3a\u94fe\u8868\u8282\u70b9\uff0c\u5c06\u6240\u6709\u53d1\u751f\u51b2\u7a81\u7684\u952e\u503c\u5bf9\u90fd\u5b58\u50a8\u5728\u540c\u4e00\u94fe\u8868\u4e2d\u3002\u56fe 6-5 \u5c55\u793a\u4e86\u4e00\u4e2a\u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868\u7684\u4f8b\u5b50\u3002
\u56fe 6-5 \u00a0 \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868
\u57fa\u4e8e\u94fe\u5f0f\u5730\u5740\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868\u7684\u64cd\u4f5c\u65b9\u6cd5\u53d1\u751f\u4e86\u4ee5\u4e0b\u53d8\u5316\u3002
key
\uff0c\u7ecf\u8fc7\u54c8\u5e0c\u51fd\u6570\u5f97\u5230\u6876\u7d22\u5f15\uff0c\u5373\u53ef\u8bbf\u95ee\u94fe\u8868\u5934\u8282\u70b9\uff0c\u7136\u540e\u904d\u5386\u94fe\u8868\u5e76\u5bf9\u6bd4 key
\u4ee5\u67e5\u627e\u76ee\u6807\u952e\u503c\u5bf9\u3002\u94fe\u5f0f\u5730\u5740\u5b58\u5728\u4ee5\u4e0b\u5c40\u9650\u6027\u3002
\u4ee5\u4e0b\u4ee3\u7801\u7ed9\u51fa\u4e86\u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868\u7684\u7b80\u5355\u5b9e\u73b0\uff0c\u9700\u8981\u6ce8\u610f\u4e24\u70b9\u3002
class HashMapChaining:\n \"\"\"\u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868\"\"\"\n\n def __init__(self):\n \"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n self.size = 0 # \u952e\u503c\u5bf9\u6570\u91cf\n self.capacity = 4 # \u54c8\u5e0c\u8868\u5bb9\u91cf\n self.load_thres = 2.0 / 3.0 # \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n self.extend_ratio = 2 # \u6269\u5bb9\u500d\u6570\n self.buckets = [[] for _ in range(self.capacity)] # \u6876\u6570\u7ec4\n\n def hash_func(self, key: int) -> int:\n \"\"\"\u54c8\u5e0c\u51fd\u6570\"\"\"\n return key % self.capacity\n\n def load_factor(self) -> float:\n \"\"\"\u8d1f\u8f7d\u56e0\u5b50\"\"\"\n return self.size / self.capacity\n\n def get(self, key: int) -> str | None:\n \"\"\"\u67e5\u8be2\u64cd\u4f5c\"\"\"\n index = self.hash_func(key)\n bucket = self.buckets[index]\n # \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \u5219\u8fd4\u56de\u5bf9\u5e94 val\n for pair in bucket:\n if pair.key == key:\n return pair.val\n # \u82e5\u672a\u627e\u5230 key \u5219\u8fd4\u56de None\n return None\n\n def put(self, key: int, val: str):\n \"\"\"\u6dfb\u52a0\u64cd\u4f5c\"\"\"\n # \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if self.load_factor() > self.load_thres:\n self.extend()\n index = self.hash_func(key)\n bucket = self.buckets[index]\n # \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\n for pair in bucket:\n if pair.key == key:\n pair.val = val\n return\n # \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\n pair = Pair(key, val)\n bucket.append(pair)\n self.size += 1\n\n def remove(self, key: int):\n \"\"\"\u5220\u9664\u64cd\u4f5c\"\"\"\n index = self.hash_func(key)\n bucket = self.buckets[index]\n # \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\n for pair in bucket:\n if pair.key == key:\n bucket.remove(pair)\n self.size -= 1\n break\n\n def extend(self):\n \"\"\"\u6269\u5bb9\u54c8\u5e0c\u8868\"\"\"\n # \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n buckets = self.buckets\n # \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n self.capacity *= self.extend_ratio\n self.buckets = [[] for _ in range(self.capacity)]\n self.size = 0\n # \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n for bucket in buckets:\n for pair in bucket:\n self.put(pair.key, pair.val)\n\n def print(self):\n \"\"\"\u6253\u5370\u54c8\u5e0c\u8868\"\"\"\n for bucket in self.buckets:\n res = []\n for pair in bucket:\n res.append(str(pair.key) + \" -> \" + pair.val)\n print(res)\n
hash_map_chaining.cpp/* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\nclass HashMapChaining {\n private:\n int size; // \u952e\u503c\u5bf9\u6570\u91cf\n int capacity; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n double loadThres; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n int extendRatio; // \u6269\u5bb9\u500d\u6570\n vector<vector<Pair *>> buckets; // \u6876\u6570\u7ec4\n\n public:\n /* \u6784\u9020\u65b9\u6cd5 */\n HashMapChaining() : size(0), capacity(4), loadThres(2.0 / 3.0), extendRatio(2) {\n buckets.resize(capacity);\n }\n\n /* \u6790\u6784\u65b9\u6cd5 */\n ~HashMapChaining() {\n for (auto &bucket : buckets) {\n for (Pair *pair : bucket) {\n // \u91ca\u653e\u5185\u5b58\n delete pair;\n }\n }\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n int hashFunc(int key) {\n return key % capacity;\n }\n\n /* \u8d1f\u8f7d\u56e0\u5b50 */\n double loadFactor() {\n return (double)size / (double)capacity;\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n string get(int key) {\n int index = hashFunc(key);\n // \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \u5219\u8fd4\u56de\u5bf9\u5e94 val\n for (Pair *pair : buckets[index]) {\n if (pair->key == key) {\n return pair->val;\n }\n }\n // \u82e5\u672a\u627e\u5230 key \u5219\u8fd4\u56de\u7a7a\u5b57\u7b26\u4e32\n return \"\";\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n void put(int key, string val) {\n // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if (loadFactor() > loadThres) {\n extend();\n }\n int index = hashFunc(key);\n // \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\n for (Pair *pair : buckets[index]) {\n if (pair->key == key) {\n pair->val = val;\n return;\n }\n }\n // \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\n buckets[index].push_back(new Pair(key, val));\n size++;\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n void remove(int key) {\n int index = hashFunc(key);\n auto &bucket = buckets[index];\n // \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\n for (int i = 0; i < bucket.size(); i++) {\n if (bucket[i]->key == key) {\n Pair *tmp = bucket[i];\n bucket.erase(bucket.begin() + i); // \u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\n delete tmp; // \u91ca\u653e\u5185\u5b58\n size--;\n return;\n }\n }\n }\n\n /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n void extend() {\n // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n vector<vector<Pair *>> bucketsTmp = buckets;\n // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n capacity *= extendRatio;\n buckets.clear();\n buckets.resize(capacity);\n size = 0;\n // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n for (auto &bucket : bucketsTmp) {\n for (Pair *pair : bucket) {\n put(pair->key, pair->val);\n // \u91ca\u653e\u5185\u5b58\n delete pair;\n }\n }\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n void print() {\n for (auto &bucket : buckets) {\n cout << \"[\";\n for (Pair *pair : bucket) {\n cout << pair->key << \" -> \" << pair->val << \", \";\n }\n cout << \"]\\n\";\n }\n }\n};\n
hash_map_chaining.java/* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\nclass HashMapChaining {\n int size; // \u952e\u503c\u5bf9\u6570\u91cf\n int capacity; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n double loadThres; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n int extendRatio; // \u6269\u5bb9\u500d\u6570\n List<List<Pair>> buckets; // \u6876\u6570\u7ec4\n\n /* \u6784\u9020\u65b9\u6cd5 */\n public HashMapChaining() {\n size = 0;\n capacity = 4;\n loadThres = 2.0 / 3.0;\n extendRatio = 2;\n buckets = new ArrayList<>(capacity);\n for (int i = 0; i < capacity; i++) {\n buckets.add(new ArrayList<>());\n }\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n int hashFunc(int key) {\n return key % capacity;\n }\n\n /* \u8d1f\u8f7d\u56e0\u5b50 */\n double loadFactor() {\n return (double) size / capacity;\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n String get(int key) {\n int index = hashFunc(key);\n List<Pair> bucket = buckets.get(index);\n // \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \u5219\u8fd4\u56de\u5bf9\u5e94 val\n for (Pair pair : bucket) {\n if (pair.key == key) {\n return pair.val;\n }\n }\n // \u82e5\u672a\u627e\u5230 key \u5219\u8fd4\u56de null\n return null;\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n void put(int key, String val) {\n // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if (loadFactor() > loadThres) {\n extend();\n }\n int index = hashFunc(key);\n List<Pair> bucket = buckets.get(index);\n // \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\n for (Pair pair : bucket) {\n if (pair.key == key) {\n pair.val = val;\n return;\n }\n }\n // \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\n Pair pair = new Pair(key, val);\n bucket.add(pair);\n size++;\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n void remove(int key) {\n int index = hashFunc(key);\n List<Pair> bucket = buckets.get(index);\n // \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\n for (Pair pair : bucket) {\n if (pair.key == key) {\n bucket.remove(pair);\n size--;\n break;\n }\n }\n }\n\n /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n void extend() {\n // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n List<List<Pair>> bucketsTmp = buckets;\n // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n capacity *= extendRatio;\n buckets = new ArrayList<>(capacity);\n for (int i = 0; i < capacity; i++) {\n buckets.add(new ArrayList<>());\n }\n size = 0;\n // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n for (List<Pair> bucket : bucketsTmp) {\n for (Pair pair : bucket) {\n put(pair.key, pair.val);\n }\n }\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n void print() {\n for (List<Pair> bucket : buckets) {\n List<String> res = new ArrayList<>();\n for (Pair pair : bucket) {\n res.add(pair.key + \" -> \" + pair.val);\n }\n System.out.println(res);\n }\n }\n}\n
hash_map_chaining.cs/* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\nclass HashMapChaining {\n int size; // \u952e\u503c\u5bf9\u6570\u91cf\n int capacity; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n double loadThres; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n int extendRatio; // \u6269\u5bb9\u500d\u6570\n List<List<Pair>> buckets; // \u6876\u6570\u7ec4\n\n /* \u6784\u9020\u65b9\u6cd5 */\n public HashMapChaining() {\n size = 0;\n capacity = 4;\n loadThres = 2.0 / 3.0;\n extendRatio = 2;\n buckets = new List<List<Pair>>(capacity);\n for (int i = 0; i < capacity; i++) {\n buckets.Add([]);\n }\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n int HashFunc(int key) {\n return key % capacity;\n }\n\n /* \u8d1f\u8f7d\u56e0\u5b50 */\n double LoadFactor() {\n return (double)size / capacity;\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n public string? Get(int key) {\n int index = HashFunc(key);\n // \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \u5219\u8fd4\u56de\u5bf9\u5e94 val\n foreach (Pair pair in buckets[index]) {\n if (pair.key == key) {\n return pair.val;\n }\n }\n // \u82e5\u672a\u627e\u5230 key \u5219\u8fd4\u56de null\n return null;\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n public void Put(int key, string val) {\n // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if (LoadFactor() > loadThres) {\n Extend();\n }\n int index = HashFunc(key);\n // \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\n foreach (Pair pair in buckets[index]) {\n if (pair.key == key) {\n pair.val = val;\n return;\n }\n }\n // \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\n buckets[index].Add(new Pair(key, val));\n size++;\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n public void Remove(int key) {\n int index = HashFunc(key);\n // \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\n foreach (Pair pair in buckets[index].ToList()) {\n if (pair.key == key) {\n buckets[index].Remove(pair);\n size--;\n break;\n }\n }\n }\n\n /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n void Extend() {\n // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n List<List<Pair>> bucketsTmp = buckets;\n // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n capacity *= extendRatio;\n buckets = new List<List<Pair>>(capacity);\n for (int i = 0; i < capacity; i++) {\n buckets.Add([]);\n }\n size = 0;\n // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n foreach (List<Pair> bucket in bucketsTmp) {\n foreach (Pair pair in bucket) {\n Put(pair.key, pair.val);\n }\n }\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n public void Print() {\n foreach (List<Pair> bucket in buckets) {\n List<string> res = [];\n foreach (Pair pair in bucket) {\n res.Add(pair.key + \" -> \" + pair.val);\n }\n foreach (string kv in res) {\n Console.WriteLine(kv);\n }\n }\n }\n}\n
hash_map_chaining.go/* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\ntype hashMapChaining struct {\n size int // \u952e\u503c\u5bf9\u6570\u91cf\n capacity int // \u54c8\u5e0c\u8868\u5bb9\u91cf\n loadThres float64 // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n extendRatio int // \u6269\u5bb9\u500d\u6570\n buckets [][]pair // \u6876\u6570\u7ec4\n}\n\n/* \u6784\u9020\u65b9\u6cd5 */\nfunc newHashMapChaining() *hashMapChaining {\n buckets := make([][]pair, 4)\n for i := 0; i < 4; i++ {\n buckets[i] = make([]pair, 0)\n }\n return &hashMapChaining{\n size: 0,\n capacity: 4,\n loadThres: 2.0 / 3.0,\n extendRatio: 2,\n buckets: buckets,\n }\n}\n\n/* \u54c8\u5e0c\u51fd\u6570 */\nfunc (m *hashMapChaining) hashFunc(key int) int {\n return key % m.capacity\n}\n\n/* \u8d1f\u8f7d\u56e0\u5b50 */\nfunc (m *hashMapChaining) loadFactor() float64 {\n return float64(m.size / m.capacity)\n}\n\n/* \u67e5\u8be2\u64cd\u4f5c */\nfunc (m *hashMapChaining) get(key int) string {\n idx := m.hashFunc(key)\n bucket := m.buckets[idx]\n // \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \u5219\u8fd4\u56de\u5bf9\u5e94 val\n for _, p := range bucket {\n if p.key == key {\n return p.val\n }\n }\n // \u82e5\u672a\u627e\u5230 key \u5219\u8fd4\u56de\u7a7a\u5b57\u7b26\u4e32\n return \"\"\n}\n\n/* \u6dfb\u52a0\u64cd\u4f5c */\nfunc (m *hashMapChaining) put(key int, val string) {\n // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if m.loadFactor() > m.loadThres {\n m.extend()\n }\n idx := m.hashFunc(key)\n // \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\n for _, p := range m.buckets[idx] {\n if p.key == key {\n p.val = val\n return\n }\n }\n // \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\n p := pair{\n key: key,\n val: val,\n }\n m.buckets[idx] = append(m.buckets[idx], p)\n m.size += 1\n}\n\n/* \u5220\u9664\u64cd\u4f5c */\nfunc (m *hashMapChaining) remove(key int) {\n idx := m.hashFunc(key)\n // \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\n for i, p := range m.buckets[idx] {\n if p.key == key {\n // \u5207\u7247\u5220\u9664\n m.buckets[idx] = append(m.buckets[idx][:i], m.buckets[idx][i+1:]...)\n m.size -= 1\n break\n }\n }\n}\n\n/* \u6269\u5bb9\u54c8\u5e0c\u8868 */\nfunc (m *hashMapChaining) extend() {\n // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n tmpBuckets := make([][]pair, len(m.buckets))\n for i := 0; i < len(m.buckets); i++ {\n tmpBuckets[i] = make([]pair, len(m.buckets[i]))\n copy(tmpBuckets[i], m.buckets[i])\n }\n // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n m.capacity *= m.extendRatio\n m.buckets = make([][]pair, m.capacity)\n for i := 0; i < m.capacity; i++ {\n m.buckets[i] = make([]pair, 0)\n }\n m.size = 0\n // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n for _, bucket := range tmpBuckets {\n for _, p := range bucket {\n m.put(p.key, p.val)\n }\n }\n}\n\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nfunc (m *hashMapChaining) print() {\n var builder strings.Builder\n\n for _, bucket := range m.buckets {\n builder.WriteString(\"[\")\n for _, p := range bucket {\n builder.WriteString(strconv.Itoa(p.key) + \" -> \" + p.val + \" \")\n }\n builder.WriteString(\"]\")\n fmt.Println(builder.String())\n builder.Reset()\n }\n}\n
hash_map_chaining.swift/* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\nclass HashMapChaining {\n var size: Int // \u952e\u503c\u5bf9\u6570\u91cf\n var capacity: Int // \u54c8\u5e0c\u8868\u5bb9\u91cf\n var loadThres: Double // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n var extendRatio: Int // \u6269\u5bb9\u500d\u6570\n var buckets: [[Pair]] // \u6876\u6570\u7ec4\n\n /* \u6784\u9020\u65b9\u6cd5 */\n init() {\n size = 0\n capacity = 4\n loadThres = 2.0 / 3.0\n extendRatio = 2\n buckets = Array(repeating: [], count: capacity)\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n func hashFunc(key: Int) -> Int {\n key % capacity\n }\n\n /* \u8d1f\u8f7d\u56e0\u5b50 */\n func loadFactor() -> Double {\n Double(size / capacity)\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n func get(key: Int) -> String? {\n let index = hashFunc(key: key)\n let bucket = buckets[index]\n // \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \u5219\u8fd4\u56de\u5bf9\u5e94 val\n for pair in bucket {\n if pair.key == key {\n return pair.val\n }\n }\n // \u82e5\u672a\u627e\u5230 key \u5219\u8fd4\u56de nil\n return nil\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n func put(key: Int, val: String) {\n // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if loadFactor() > loadThres {\n extend()\n }\n let index = hashFunc(key: key)\n let bucket = buckets[index]\n // \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\n for pair in bucket {\n if pair.key == key {\n pair.val = val\n return\n }\n }\n // \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\n let pair = Pair(key: key, val: val)\n buckets[index].append(pair)\n size += 1\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n func remove(key: Int) {\n let index = hashFunc(key: key)\n let bucket = buckets[index]\n // \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\n for (pairIndex, pair) in bucket.enumerated() {\n if pair.key == key {\n buckets[index].remove(at: pairIndex)\n }\n }\n size -= 1\n }\n\n /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n func extend() {\n // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n let bucketsTmp = buckets\n // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n capacity *= extendRatio\n buckets = Array(repeating: [], count: capacity)\n size = 0\n // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n for bucket in bucketsTmp {\n for pair in bucket {\n put(key: pair.key, val: pair.val)\n }\n }\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n func print() {\n for bucket in buckets {\n let res = bucket.map { \"\\($0.key) -> \\($0.val)\" }\n Swift.print(res)\n }\n }\n}\n
hash_map_chaining.js/* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\nclass HashMapChaining {\n #size; // \u952e\u503c\u5bf9\u6570\u91cf\n #capacity; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n #loadThres; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n #extendRatio; // \u6269\u5bb9\u500d\u6570\n #buckets; // \u6876\u6570\u7ec4\n\n /* \u6784\u9020\u65b9\u6cd5 */\n constructor() {\n this.#size = 0;\n this.#capacity = 4;\n this.#loadThres = 2.0 / 3.0;\n this.#extendRatio = 2;\n this.#buckets = new Array(this.#capacity).fill(null).map((x) => []);\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n #hashFunc(key) {\n return key % this.#capacity;\n }\n\n /* \u8d1f\u8f7d\u56e0\u5b50 */\n #loadFactor() {\n return this.#size / this.#capacity;\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n get(key) {\n const index = this.#hashFunc(key);\n const bucket = this.#buckets[index];\n // \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \u5219\u8fd4\u56de\u5bf9\u5e94 val\n for (const pair of bucket) {\n if (pair.key === key) {\n return pair.val;\n }\n }\n // \u82e5\u672a\u627e\u5230 key \u5219\u8fd4\u56de null\n return null;\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n put(key, val) {\n // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if (this.#loadFactor() > this.#loadThres) {\n this.#extend();\n }\n const index = this.#hashFunc(key);\n const bucket = this.#buckets[index];\n // \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\n for (const pair of bucket) {\n if (pair.key === key) {\n pair.val = val;\n return;\n }\n }\n // \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\n const pair = new Pair(key, val);\n bucket.push(pair);\n this.#size++;\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n remove(key) {\n const index = this.#hashFunc(key);\n let bucket = this.#buckets[index];\n // \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\n for (let i = 0; i < bucket.length; i++) {\n if (bucket[i].key === key) {\n bucket.splice(i, 1);\n this.#size--;\n break;\n }\n }\n }\n\n /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n #extend() {\n // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n const bucketsTmp = this.#buckets;\n // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n this.#capacity *= this.#extendRatio;\n this.#buckets = new Array(this.#capacity).fill(null).map((x) => []);\n this.#size = 0;\n // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n for (const bucket of bucketsTmp) {\n for (const pair of bucket) {\n this.put(pair.key, pair.val);\n }\n }\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n print() {\n for (const bucket of this.#buckets) {\n let res = [];\n for (const pair of bucket) {\n res.push(pair.key + ' -> ' + pair.val);\n }\n console.log(res);\n }\n }\n}\n
hash_map_chaining.ts/* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\nclass HashMapChaining {\n #size: number; // \u952e\u503c\u5bf9\u6570\u91cf\n #capacity: number; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n #loadThres: number; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n #extendRatio: number; // \u6269\u5bb9\u500d\u6570\n #buckets: Pair[][]; // \u6876\u6570\u7ec4\n\n /* \u6784\u9020\u65b9\u6cd5 */\n constructor() {\n this.#size = 0;\n this.#capacity = 4;\n this.#loadThres = 2.0 / 3.0;\n this.#extendRatio = 2;\n this.#buckets = new Array(this.#capacity).fill(null).map((x) => []);\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n #hashFunc(key: number): number {\n return key % this.#capacity;\n }\n\n /* \u8d1f\u8f7d\u56e0\u5b50 */\n #loadFactor(): number {\n return this.#size / this.#capacity;\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n get(key: number): string | null {\n const index = this.#hashFunc(key);\n const bucket = this.#buckets[index];\n // \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \u5219\u8fd4\u56de\u5bf9\u5e94 val\n for (const pair of bucket) {\n if (pair.key === key) {\n return pair.val;\n }\n }\n // \u82e5\u672a\u627e\u5230 key \u5219\u8fd4\u56de null\n return null;\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n put(key: number, val: string): void {\n // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if (this.#loadFactor() > this.#loadThres) {\n this.#extend();\n }\n const index = this.#hashFunc(key);\n const bucket = this.#buckets[index];\n // \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\n for (const pair of bucket) {\n if (pair.key === key) {\n pair.val = val;\n return;\n }\n }\n // \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\n const pair = new Pair(key, val);\n bucket.push(pair);\n this.#size++;\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n remove(key: number): void {\n const index = this.#hashFunc(key);\n let bucket = this.#buckets[index];\n // \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\n for (let i = 0; i < bucket.length; i++) {\n if (bucket[i].key === key) {\n bucket.splice(i, 1);\n this.#size--;\n break;\n }\n }\n }\n\n /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n #extend(): void {\n // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n const bucketsTmp = this.#buckets;\n // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n this.#capacity *= this.#extendRatio;\n this.#buckets = new Array(this.#capacity).fill(null).map((x) => []);\n this.#size = 0;\n // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n for (const bucket of bucketsTmp) {\n for (const pair of bucket) {\n this.put(pair.key, pair.val);\n }\n }\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n print(): void {\n for (const bucket of this.#buckets) {\n let res = [];\n for (const pair of bucket) {\n res.push(pair.key + ' -> ' + pair.val);\n }\n console.log(res);\n }\n }\n}\n
hash_map_chaining.dart/* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\nclass HashMapChaining {\n late int size; // \u952e\u503c\u5bf9\u6570\u91cf\n late int capacity; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n late double loadThres; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n late int extendRatio; // \u6269\u5bb9\u500d\u6570\n late List<List<Pair>> buckets; // \u6876\u6570\u7ec4\n\n /* \u6784\u9020\u65b9\u6cd5 */\n HashMapChaining() {\n size = 0;\n capacity = 4;\n loadThres = 2.0 / 3.0;\n extendRatio = 2;\n buckets = List.generate(capacity, (_) => []);\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n int hashFunc(int key) {\n return key % capacity;\n }\n\n /* \u8d1f\u8f7d\u56e0\u5b50 */\n double loadFactor() {\n return size / capacity;\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n String? get(int key) {\n int index = hashFunc(key);\n List<Pair> bucket = buckets[index];\n // \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \u5219\u8fd4\u56de\u5bf9\u5e94 val\n for (Pair pair in bucket) {\n if (pair.key == key) {\n return pair.val;\n }\n }\n // \u82e5\u672a\u627e\u5230 key \u5219\u8fd4\u56de null\n return null;\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n void put(int key, String val) {\n // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if (loadFactor() > loadThres) {\n extend();\n }\n int index = hashFunc(key);\n List<Pair> bucket = buckets[index];\n // \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\n for (Pair pair in bucket) {\n if (pair.key == key) {\n pair.val = val;\n return;\n }\n }\n // \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\n Pair pair = Pair(key, val);\n bucket.add(pair);\n size++;\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n void remove(int key) {\n int index = hashFunc(key);\n List<Pair> bucket = buckets[index];\n // \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\n for (Pair pair in bucket) {\n if (pair.key == key) {\n bucket.remove(pair);\n size--;\n break;\n }\n }\n }\n\n /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n void extend() {\n // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n List<List<Pair>> bucketsTmp = buckets;\n // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n capacity *= extendRatio;\n buckets = List.generate(capacity, (_) => []);\n size = 0;\n // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n for (List<Pair> bucket in bucketsTmp) {\n for (Pair pair in bucket) {\n put(pair.key, pair.val);\n }\n }\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n void printHashMap() {\n for (List<Pair> bucket in buckets) {\n List<String> res = [];\n for (Pair pair in bucket) {\n res.add(\"${pair.key} -> ${pair.val}\");\n }\n print(res);\n }\n }\n}\n
hash_map_chaining.rs/* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\nstruct HashMapChaining {\n size: i32,\n capacity: i32,\n load_thres: f32,\n extend_ratio: i32,\n buckets: Vec<Vec<Pair>>,\n}\n\nimpl HashMapChaining {\n /* \u6784\u9020\u65b9\u6cd5 */\n fn new() -> Self {\n Self {\n size: 0,\n capacity: 4,\n load_thres: 2.0 / 3.0,\n extend_ratio: 2,\n buckets: vec![vec![]; 4],\n }\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n fn hash_func(&self, key: i32) -> usize {\n key as usize % self.capacity as usize\n }\n\n /* \u8d1f\u8f7d\u56e0\u5b50 */\n fn load_factor(&self) -> f32 {\n self.size as f32 / self.capacity as f32\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n fn remove(&mut self, key: i32) -> Option<String> {\n let index = self.hash_func(key);\n let bucket = &mut self.buckets[index];\n\n // \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\n for i in 0..bucket.len() {\n if bucket[i].key == key {\n let pair = bucket.remove(i);\n self.size -= 1;\n return Some(pair.val);\n }\n }\n\n // \u82e5\u672a\u627e\u5230 key \u5219\u8fd4\u56de None\n None\n }\n\n /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n fn extend(&mut self) {\n // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n let buckets_tmp = std::mem::replace(&mut self.buckets, vec![]);\n\n // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n self.capacity *= self.extend_ratio;\n self.buckets = vec![Vec::new(); self.capacity as usize];\n self.size = 0;\n\n // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n for bucket in buckets_tmp {\n for pair in bucket {\n self.put(pair.key, pair.val);\n }\n }\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n fn print(&self) {\n for bucket in &self.buckets {\n let mut res = Vec::new();\n for pair in bucket {\n res.push(format!(\"{} -> {}\", pair.key, pair.val));\n }\n println!(\"{:?}\", res);\n }\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n fn put(&mut self, key: i32, val: String) {\n // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if self.load_factor() > self.load_thres {\n self.extend();\n }\n\n let index = self.hash_func(key);\n let bucket = &mut self.buckets[index];\n\n // \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\n for pair in bucket {\n if pair.key == key {\n pair.val = val.clone();\n return;\n }\n }\n let bucket = &mut self.buckets[index];\n\n // \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\n let pair = Pair {\n key,\n val: val.clone(),\n };\n bucket.push(pair);\n self.size += 1;\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n fn get(&self, key: i32) -> Option<&str> {\n let index = self.hash_func(key);\n let bucket = &self.buckets[index];\n\n // \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \u5219\u8fd4\u56de\u5bf9\u5e94 val\n for pair in bucket {\n if pair.key == key {\n return Some(&pair.val);\n }\n }\n\n // \u82e5\u672a\u627e\u5230 key \u5219\u8fd4\u56de None\n None\n }\n}\n
hash_map_chaining.c/* \u94fe\u8868\u8282\u70b9 */\ntypedef struct Node {\n Pair *pair;\n struct Node *next;\n} Node;\n\n/* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\ntypedef struct {\n int size; // \u952e\u503c\u5bf9\u6570\u91cf\n int capacity; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n double loadThres; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n int extendRatio; // \u6269\u5bb9\u500d\u6570\n Node **buckets; // \u6876\u6570\u7ec4\n} HashMapChaining;\n\n/* \u6784\u9020\u51fd\u6570 */\nHashMapChaining *newHashMapChaining() {\n HashMapChaining *hashMap = (HashMapChaining *)malloc(sizeof(HashMapChaining));\n hashMap->size = 0;\n hashMap->capacity = 4;\n hashMap->loadThres = 2.0 / 3.0;\n hashMap->extendRatio = 2;\n hashMap->buckets = (Node **)malloc(hashMap->capacity * sizeof(Node *));\n for (int i = 0; i < hashMap->capacity; i++) {\n hashMap->buckets[i] = NULL;\n }\n return hashMap;\n}\n\n/* \u6790\u6784\u51fd\u6570 */\nvoid delHashMapChaining(HashMapChaining *hashMap) {\n for (int i = 0; i < hashMap->capacity; i++) {\n Node *cur = hashMap->buckets[i];\n while (cur) {\n Node *tmp = cur;\n cur = cur->next;\n free(tmp->pair);\n free(tmp);\n }\n }\n free(hashMap->buckets);\n free(hashMap);\n}\n\n/* \u54c8\u5e0c\u51fd\u6570 */\nint hashFunc(HashMapChaining *hashMap, int key) {\n return key % hashMap->capacity;\n}\n\n/* \u8d1f\u8f7d\u56e0\u5b50 */\ndouble loadFactor(HashMapChaining *hashMap) {\n return (double)hashMap->size / (double)hashMap->capacity;\n}\n\n/* \u67e5\u8be2\u64cd\u4f5c */\nchar *get(HashMapChaining *hashMap, int key) {\n int index = hashFunc(hashMap, key);\n // \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \u5219\u8fd4\u56de\u5bf9\u5e94 val\n Node *cur = hashMap->buckets[index];\n while (cur) {\n if (cur->pair->key == key) {\n return cur->pair->val;\n }\n cur = cur->next;\n }\n return \"\"; // \u82e5\u672a\u627e\u5230 key \u5219\u8fd4\u56de\u7a7a\u5b57\u7b26\u4e32\n}\n\n/* \u6dfb\u52a0\u64cd\u4f5c */\nvoid put(HashMapChaining *hashMap, int key, const char *val) {\n // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if (loadFactor(hashMap) > hashMap->loadThres) {\n extend(hashMap);\n }\n int index = hashFunc(hashMap, key);\n // \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\n Node *cur = hashMap->buckets[index];\n while (cur) {\n if (cur->pair->key == key) {\n strcpy(cur->pair->val, val); // \u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\n return;\n }\n cur = cur->next;\n }\n // \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\n Pair *newPair = (Pair *)malloc(sizeof(Pair));\n newPair->key = key;\n strcpy(newPair->val, val);\n Node *newNode = (Node *)malloc(sizeof(Node));\n newNode->pair = newPair;\n newNode->next = hashMap->buckets[index];\n hashMap->buckets[index] = newNode;\n hashMap->size++;\n}\n\n/* \u6269\u5bb9\u54c8\u5e0c\u8868 */\nvoid extend(HashMapChaining *hashMap) {\n // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n int oldCapacity = hashMap->capacity;\n Node **oldBuckets = hashMap->buckets;\n // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n hashMap->capacity *= hashMap->extendRatio;\n hashMap->buckets = (Node **)malloc(hashMap->capacity * sizeof(Node *));\n for (int i = 0; i < hashMap->capacity; i++) {\n hashMap->buckets[i] = NULL;\n }\n hashMap->size = 0;\n // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n for (int i = 0; i < oldCapacity; i++) {\n Node *cur = oldBuckets[i];\n while (cur) {\n put(hashMap, cur->pair->key, cur->pair->val);\n Node *temp = cur;\n cur = cur->next;\n // \u91ca\u653e\u5185\u5b58\n free(temp->pair);\n free(temp);\n }\n }\n\n free(oldBuckets);\n}\n\n/* \u5220\u9664\u64cd\u4f5c */\nvoid removeItem(HashMapChaining *hashMap, int key) {\n int index = hashFunc(hashMap, key);\n Node *cur = hashMap->buckets[index];\n Node *pre = NULL;\n while (cur) {\n if (cur->pair->key == key) {\n // \u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\n if (pre) {\n pre->next = cur->next;\n } else {\n hashMap->buckets[index] = cur->next;\n }\n // \u91ca\u653e\u5185\u5b58\n free(cur->pair);\n free(cur);\n hashMap->size--;\n return;\n }\n pre = cur;\n cur = cur->next;\n }\n}\n\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nvoid print(HashMapChaining *hashMap) {\n for (int i = 0; i < hashMap->capacity; i++) {\n Node *cur = hashMap->buckets[i];\n printf(\"[\");\n while (cur) {\n printf(\"%d -> %s, \", cur->pair->key, cur->pair->val);\n cur = cur->next;\n }\n printf(\"]\\n\");\n }\n}\n
hash_map_chaining.zig[class]{HashMapChaining}-[func]{}\n
\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u5f53\u94fe\u8868\u5f88\u957f\u65f6\uff0c\u67e5\u8be2\u6548\u7387 \\(O(n)\\) \u5f88\u5dee\u3002\u6b64\u65f6\u53ef\u4ee5\u5c06\u94fe\u8868\u8f6c\u6362\u4e3a\u201cAVL \u6811\u201d\u6216\u201c\u7ea2\u9ed1\u6811\u201d\uff0c\u4ece\u800c\u5c06\u67e5\u8be2\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4f18\u5316\u81f3 \\(O(\\log n)\\) \u3002
"},{"location":"chapter_hashing/hash_collision/#622","title":"6.2.2 \u00a0 \u5f00\u653e\u5bfb\u5740","text":"\u300c\u5f00\u653e\u5bfb\u5740 open addressing\u300d\u4e0d\u5f15\u5165\u989d\u5916\u7684\u6570\u636e\u7ed3\u6784\uff0c\u800c\u662f\u901a\u8fc7\u201c\u591a\u6b21\u63a2\u6d4b\u201d\u6765\u5904\u7406\u54c8\u5e0c\u51b2\u7a81\uff0c\u63a2\u6d4b\u65b9\u5f0f\u4e3b\u8981\u5305\u62ec\u7ebf\u6027\u63a2\u6d4b\u3001\u5e73\u65b9\u63a2\u6d4b\u3001\u591a\u6b21\u54c8\u5e0c\u7b49\u3002
\u4e0b\u9762\u4ee5\u7ebf\u6027\u63a2\u6d4b\u4e3a\u4f8b\uff0c\u4ecb\u7ecd\u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868\u7684\u5de5\u4f5c\u673a\u5236\u3002
"},{"location":"chapter_hashing/hash_collision/#1","title":"1. \u00a0 \u7ebf\u6027\u63a2\u6d4b","text":"\u7ebf\u6027\u63a2\u6d4b\u91c7\u7528\u56fa\u5b9a\u6b65\u957f\u7684\u7ebf\u6027\u641c\u7d22\u6765\u8fdb\u884c\u63a2\u6d4b\uff0c\u5176\u64cd\u4f5c\u65b9\u6cd5\u4e0e\u666e\u901a\u54c8\u5e0c\u8868\u6709\u6240\u4e0d\u540c\u3002
value
\u5373\u53ef\uff1b\u5982\u679c\u9047\u5230\u7a7a\u6876\uff0c\u8bf4\u660e\u76ee\u6807\u5143\u7d20\u4e0d\u5728\u54c8\u5e0c\u8868\u4e2d\uff0c\u8fd4\u56de \\(\\text{None}\\) \u3002\u56fe 6-6 \u5c55\u793a\u4e86\u5f00\u653e\u5bfb\u5740\uff08\u7ebf\u6027\u63a2\u6d4b\uff09\u54c8\u5e0c\u8868\u7684\u952e\u503c\u5bf9\u5206\u5e03\u3002\u6839\u636e\u6b64\u54c8\u5e0c\u51fd\u6570\uff0c\u6700\u540e\u4e24\u4f4d\u76f8\u540c\u7684 key
\u90fd\u4f1a\u88ab\u6620\u5c04\u5230\u76f8\u540c\u7684\u6876\u3002\u800c\u901a\u8fc7\u7ebf\u6027\u63a2\u6d4b\uff0c\u5b83\u4eec\u88ab\u4f9d\u6b21\u5b58\u50a8\u5728\u8be5\u6876\u4ee5\u53ca\u4e4b\u4e0b\u7684\u6876\u4e2d\u3002
\u56fe 6-6 \u00a0 \u5f00\u653e\u5bfb\u5740\u548c\u7ebf\u6027\u63a2\u6d4b
\u7136\u800c\uff0c\u7ebf\u6027\u63a2\u6d4b\u5bb9\u6613\u4ea7\u751f\u201c\u805a\u96c6\u73b0\u8c61\u201d\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u6570\u7ec4\u4e2d\u8fde\u7eed\u88ab\u5360\u7528\u7684\u4f4d\u7f6e\u8d8a\u957f\uff0c\u8fd9\u4e9b\u8fde\u7eed\u4f4d\u7f6e\u53d1\u751f\u54c8\u5e0c\u51b2\u7a81\u7684\u53ef\u80fd\u6027\u8d8a\u5927\uff0c\u4ece\u800c\u8fdb\u4e00\u6b65\u4fc3\u4f7f\u8be5\u4f4d\u7f6e\u7684\u805a\u5806\u751f\u957f\uff0c\u5f62\u6210\u6076\u6027\u5faa\u73af\uff0c\u6700\u7ec8\u5bfc\u81f4\u589e\u5220\u67e5\u6539\u64cd\u4f5c\u6548\u7387\u52a3\u5316\u3002
\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u6211\u4eec\u4e0d\u80fd\u5728\u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868\u4e2d\u76f4\u63a5\u5220\u9664\u5143\u7d20\u3002\u8fd9\u662f\u56e0\u4e3a\u5220\u9664\u5143\u7d20\u4f1a\u5728\u6570\u7ec4\u5185\u4ea7\u751f\u4e00\u4e2a\u7a7a\u6876 \\(\\text{None}\\) \uff0c\u800c\u5f53\u67e5\u8be2\u5143\u7d20\u65f6\uff0c\u7ebf\u6027\u63a2\u6d4b\u5230\u8be5\u7a7a\u6876\u5c31\u4f1a\u8fd4\u56de\uff0c\u56e0\u6b64\u5728\u8be5\u7a7a\u6876\u4e4b\u4e0b\u7684\u5143\u7d20\u90fd\u65e0\u6cd5\u518d\u88ab\u8bbf\u95ee\u5230\uff0c\u7a0b\u5e8f\u53ef\u80fd\u8bef\u5224\u8fd9\u4e9b\u5143\u7d20\u4e0d\u5b58\u5728\u3002
\u56fe 6-7 \u00a0 \u5728\u5f00\u653e\u5bfb\u5740\u4e2d\u5220\u9664\u5143\u7d20\u5bfc\u81f4\u7684\u67e5\u8be2\u95ee\u9898
\u4e3a\u4e86\u89e3\u51b3\u8be5\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u91c7\u7528\u300c\u61d2\u5220\u9664 lazy deletion\u300d\u673a\u5236\uff1a\u5b83\u4e0d\u76f4\u63a5\u4ece\u54c8\u5e0c\u8868\u4e2d\u79fb\u9664\u5143\u7d20\uff0c\u800c\u662f\u5229\u7528\u4e00\u4e2a\u5e38\u91cf TOMBSTONE
\u6765\u6807\u8bb0\u8fd9\u4e2a\u6876\u3002\u5728\u8be5\u673a\u5236\u4e0b\uff0c\\(\\text{None}\\) \u548c TOMBSTONE
\u90fd\u4ee3\u8868\u7a7a\u6876\uff0c\u90fd\u53ef\u4ee5\u653e\u7f6e\u952e\u503c\u5bf9\u3002\u4f46\u4e0d\u540c\u7684\u662f\uff0c\u7ebf\u6027\u63a2\u6d4b\u5230 TOMBSTONE
\u65f6\u5e94\u8be5\u7ee7\u7eed\u904d\u5386\uff0c\u56e0\u4e3a\u5176\u4e4b\u4e0b\u53ef\u80fd\u8fd8\u5b58\u5728\u952e\u503c\u5bf9\u3002
\u7136\u800c\uff0c\u61d2\u5220\u9664\u53ef\u80fd\u4f1a\u52a0\u901f\u54c8\u5e0c\u8868\u7684\u6027\u80fd\u9000\u5316\u3002\u8fd9\u662f\u56e0\u4e3a\u6bcf\u6b21\u5220\u9664\u64cd\u4f5c\u90fd\u4f1a\u4ea7\u751f\u4e00\u4e2a\u5220\u9664\u6807\u8bb0\uff0c\u968f\u7740 TOMBSTONE
\u7684\u589e\u52a0\uff0c\u641c\u7d22\u65f6\u95f4\u4e5f\u4f1a\u589e\u52a0\uff0c\u56e0\u4e3a\u7ebf\u6027\u63a2\u6d4b\u53ef\u80fd\u9700\u8981\u8df3\u8fc7\u591a\u4e2a TOMBSTONE
\u624d\u80fd\u627e\u5230\u76ee\u6807\u5143\u7d20\u3002
\u4e3a\u6b64\uff0c\u8003\u8651\u5728\u7ebf\u6027\u63a2\u6d4b\u4e2d\u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a TOMBSTONE
\u7684\u7d22\u5f15\uff0c\u5e76\u5c06\u641c\u7d22\u5230\u7684\u76ee\u6807\u5143\u7d20\u4e0e\u8be5 TOMBSTONE
\u4ea4\u6362\u4f4d\u7f6e\u3002\u8fd9\u6837\u505a\u7684\u597d\u5904\u662f\u5f53\u6bcf\u6b21\u67e5\u8be2\u6216\u6dfb\u52a0\u5143\u7d20\u65f6\uff0c\u5143\u7d20\u4f1a\u88ab\u79fb\u52a8\u81f3\u8ddd\u79bb\u7406\u60f3\u4f4d\u7f6e\uff08\u63a2\u6d4b\u8d77\u59cb\u70b9\uff09\u66f4\u8fd1\u7684\u6876\uff0c\u4ece\u800c\u4f18\u5316\u67e5\u8be2\u6548\u7387\u3002
\u4ee5\u4e0b\u4ee3\u7801\u5b9e\u73b0\u4e86\u4e00\u4e2a\u5305\u542b\u61d2\u5220\u9664\u7684\u5f00\u653e\u5bfb\u5740\uff08\u7ebf\u6027\u63a2\u6d4b\uff09\u54c8\u5e0c\u8868\u3002\u4e3a\u4e86\u66f4\u52a0\u5145\u5206\u5730\u4f7f\u7528\u54c8\u5e0c\u8868\u7684\u7a7a\u95f4\uff0c\u6211\u4eec\u5c06\u54c8\u5e0c\u8868\u770b\u4f5c\u4e00\u4e2a\u201c\u73af\u5f62\u6570\u7ec4\u201d\uff0c\u5f53\u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u56de\u5230\u5934\u90e8\u7ee7\u7eed\u904d\u5386\u3002
PythonC++JavaC#GoSwiftJSTSDartRustCZig hash_map_open_addressing.pyclass HashMapOpenAddressing:\n \"\"\"\u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868\"\"\"\n\n def __init__(self):\n \"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n self.size = 0 # \u952e\u503c\u5bf9\u6570\u91cf\n self.capacity = 4 # \u54c8\u5e0c\u8868\u5bb9\u91cf\n self.load_thres = 2.0 / 3.0 # \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n self.extend_ratio = 2 # \u6269\u5bb9\u500d\u6570\n self.buckets: list[Pair | None] = [None] * self.capacity # \u6876\u6570\u7ec4\n self.TOMBSTONE = Pair(-1, \"-1\") # \u5220\u9664\u6807\u8bb0\n\n def hash_func(self, key: int) -> int:\n \"\"\"\u54c8\u5e0c\u51fd\u6570\"\"\"\n return key % self.capacity\n\n def load_factor(self) -> float:\n \"\"\"\u8d1f\u8f7d\u56e0\u5b50\"\"\"\n return self.size / self.capacity\n\n def find_bucket(self, key: int) -> int:\n \"\"\"\u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\"\"\"\n index = self.hash_func(key)\n first_tombstone = -1\n # \u7ebf\u6027\u63a2\u6d4b\uff0c\u5f53\u9047\u5230\u7a7a\u6876\u65f6\u8df3\u51fa\n while self.buckets[index] is not None:\n # \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5bf9\u5e94\u6876\u7d22\u5f15\n if self.buckets[index].key == key:\n # \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\n if first_tombstone != -1:\n self.buckets[first_tombstone] = self.buckets[index]\n self.buckets[index] = self.TOMBSTONE\n return first_tombstone # \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\n return index # \u8fd4\u56de\u6876\u7d22\u5f15\n # \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\n if first_tombstone == -1 and self.buckets[index] is self.TOMBSTONE:\n first_tombstone = index\n # \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\n index = (index + 1) % self.capacity\n # \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\n return index if first_tombstone == -1 else first_tombstone\n\n def get(self, key: int) -> str:\n \"\"\"\u67e5\u8be2\u64cd\u4f5c\"\"\"\n # \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n index = self.find_bucket(key)\n # \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n if self.buckets[index] not in [None, self.TOMBSTONE]:\n return self.buckets[index].val\n # \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de None\n return None\n\n def put(self, key: int, val: str):\n \"\"\"\u6dfb\u52a0\u64cd\u4f5c\"\"\"\n # \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if self.load_factor() > self.load_thres:\n self.extend()\n # \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n index = self.find_bucket(key)\n # \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val \u5e76\u8fd4\u56de\n if self.buckets[index] not in [None, self.TOMBSTONE]:\n self.buckets[index].val = val\n return\n # \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\n self.buckets[index] = Pair(key, val)\n self.size += 1\n\n def remove(self, key: int):\n \"\"\"\u5220\u9664\u64cd\u4f5c\"\"\"\n # \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n index = self.find_bucket(key)\n # \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\n if self.buckets[index] not in [None, self.TOMBSTONE]:\n self.buckets[index] = self.TOMBSTONE\n self.size -= 1\n\n def extend(self):\n \"\"\"\u6269\u5bb9\u54c8\u5e0c\u8868\"\"\"\n # \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n buckets_tmp = self.buckets\n # \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n self.capacity *= self.extend_ratio\n self.buckets = [None] * self.capacity\n self.size = 0\n # \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n for pair in buckets_tmp:\n if pair not in [None, self.TOMBSTONE]:\n self.put(pair.key, pair.val)\n\n def print(self):\n \"\"\"\u6253\u5370\u54c8\u5e0c\u8868\"\"\"\n for pair in self.buckets:\n if pair is None:\n print(\"None\")\n elif pair is self.TOMBSTONE:\n print(\"TOMBSTONE\")\n else:\n print(pair.key, \"->\", pair.val)\n
hash_map_open_addressing.cpp/* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\nclass HashMapOpenAddressing {\n private:\n int size; // \u952e\u503c\u5bf9\u6570\u91cf\n int capacity = 4; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n const double loadThres = 2.0 / 3.0; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n const int extendRatio = 2; // \u6269\u5bb9\u500d\u6570\n vector<Pair *> buckets; // \u6876\u6570\u7ec4\n Pair *TOMBSTONE = new Pair(-1, \"-1\"); // \u5220\u9664\u6807\u8bb0\n\n public:\n /* \u6784\u9020\u65b9\u6cd5 */\n HashMapOpenAddressing() : size(0), buckets(capacity, nullptr) {\n }\n\n /* \u6790\u6784\u65b9\u6cd5 */\n ~HashMapOpenAddressing() {\n for (Pair *pair : buckets) {\n if (pair != nullptr && pair != TOMBSTONE) {\n delete pair;\n }\n }\n delete TOMBSTONE;\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n int hashFunc(int key) {\n return key % capacity;\n }\n\n /* \u8d1f\u8f7d\u56e0\u5b50 */\n double loadFactor() {\n return (double)size / capacity;\n }\n\n /* \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15 */\n int findBucket(int key) {\n int index = hashFunc(key);\n int firstTombstone = -1;\n // \u7ebf\u6027\u63a2\u6d4b\uff0c\u5f53\u9047\u5230\u7a7a\u6876\u65f6\u8df3\u51fa\n while (buckets[index] != nullptr) {\n // \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5bf9\u5e94\u6876\u7d22\u5f15\n if (buckets[index]->key == key) {\n // \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\n if (firstTombstone != -1) {\n buckets[firstTombstone] = buckets[index];\n buckets[index] = TOMBSTONE;\n return firstTombstone; // \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\n }\n return index; // \u8fd4\u56de\u6876\u7d22\u5f15\n }\n // \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\n if (firstTombstone == -1 && buckets[index] == TOMBSTONE) {\n firstTombstone = index;\n }\n // \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\n index = (index + 1) % capacity;\n }\n // \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\n return firstTombstone == -1 ? index : firstTombstone;\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n string get(int key) {\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n int index = findBucket(key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n if (buckets[index] != nullptr && buckets[index] != TOMBSTONE) {\n return buckets[index]->val;\n }\n // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u7a7a\u5b57\u7b26\u4e32\n return \"\";\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n void put(int key, string val) {\n // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if (loadFactor() > loadThres) {\n extend();\n }\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n int index = findBucket(key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val \u5e76\u8fd4\u56de\n if (buckets[index] != nullptr && buckets[index] != TOMBSTONE) {\n buckets[index]->val = val;\n return;\n }\n // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\n buckets[index] = new Pair(key, val);\n size++;\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n void remove(int key) {\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n int index = findBucket(key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\n if (buckets[index] != nullptr && buckets[index] != TOMBSTONE) {\n delete buckets[index];\n buckets[index] = TOMBSTONE;\n size--;\n }\n }\n\n /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n void extend() {\n // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n vector<Pair *> bucketsTmp = buckets;\n // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n capacity *= extendRatio;\n buckets = vector<Pair *>(capacity, nullptr);\n size = 0;\n // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n for (Pair *pair : bucketsTmp) {\n if (pair != nullptr && pair != TOMBSTONE) {\n put(pair->key, pair->val);\n delete pair;\n }\n }\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n void print() {\n for (Pair *pair : buckets) {\n if (pair == nullptr) {\n cout << \"nullptr\" << endl;\n } else if (pair == TOMBSTONE) {\n cout << \"TOMBSTONE\" << endl;\n } else {\n cout << pair->key << \" -> \" << pair->val << endl;\n }\n }\n }\n};\n
hash_map_open_addressing.java/* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\nclass HashMapOpenAddressing {\n private int size; // \u952e\u503c\u5bf9\u6570\u91cf\n private int capacity = 4; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n private final double loadThres = 2.0 / 3.0; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n private final int extendRatio = 2; // \u6269\u5bb9\u500d\u6570\n private Pair[] buckets; // \u6876\u6570\u7ec4\n private final Pair TOMBSTONE = new Pair(-1, \"-1\"); // \u5220\u9664\u6807\u8bb0\n\n /* \u6784\u9020\u65b9\u6cd5 */\n public HashMapOpenAddressing() {\n size = 0;\n buckets = new Pair[capacity];\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n private int hashFunc(int key) {\n return key % capacity;\n }\n\n /* \u8d1f\u8f7d\u56e0\u5b50 */\n private double loadFactor() {\n return (double) size / capacity;\n }\n\n /* \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15 */\n private int findBucket(int key) {\n int index = hashFunc(key);\n int firstTombstone = -1;\n // \u7ebf\u6027\u63a2\u6d4b\uff0c\u5f53\u9047\u5230\u7a7a\u6876\u65f6\u8df3\u51fa\n while (buckets[index] != null) {\n // \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5bf9\u5e94\u6876\u7d22\u5f15\n if (buckets[index].key == key) {\n // \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\n if (firstTombstone != -1) {\n buckets[firstTombstone] = buckets[index];\n buckets[index] = TOMBSTONE;\n return firstTombstone; // \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\n }\n return index; // \u8fd4\u56de\u6876\u7d22\u5f15\n }\n // \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\n if (firstTombstone == -1 && buckets[index] == TOMBSTONE) {\n firstTombstone = index;\n }\n // \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\n index = (index + 1) % capacity;\n }\n // \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\n return firstTombstone == -1 ? index : firstTombstone;\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n public String get(int key) {\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n int index = findBucket(key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n if (buckets[index] != null && buckets[index] != TOMBSTONE) {\n return buckets[index].val;\n }\n // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de null\n return null;\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n public void put(int key, String val) {\n // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if (loadFactor() > loadThres) {\n extend();\n }\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n int index = findBucket(key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val \u5e76\u8fd4\u56de\n if (buckets[index] != null && buckets[index] != TOMBSTONE) {\n buckets[index].val = val;\n return;\n }\n // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\n buckets[index] = new Pair(key, val);\n size++;\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n public void remove(int key) {\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n int index = findBucket(key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\n if (buckets[index] != null && buckets[index] != TOMBSTONE) {\n buckets[index] = TOMBSTONE;\n size--;\n }\n }\n\n /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n private void extend() {\n // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n Pair[] bucketsTmp = buckets;\n // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n capacity *= extendRatio;\n buckets = new Pair[capacity];\n size = 0;\n // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n for (Pair pair : bucketsTmp) {\n if (pair != null && pair != TOMBSTONE) {\n put(pair.key, pair.val);\n }\n }\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n public void print() {\n for (Pair pair : buckets) {\n if (pair == null) {\n System.out.println(\"null\");\n } else if (pair == TOMBSTONE) {\n System.out.println(\"TOMBSTONE\");\n } else {\n System.out.println(pair.key + \" -> \" + pair.val);\n }\n }\n }\n}\n
hash_map_open_addressing.cs/* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\nclass HashMapOpenAddressing {\n int size; // \u952e\u503c\u5bf9\u6570\u91cf\n int capacity = 4; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n double loadThres = 2.0 / 3.0; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n int extendRatio = 2; // \u6269\u5bb9\u500d\u6570\n Pair[] buckets; // \u6876\u6570\u7ec4\n Pair TOMBSTONE = new(-1, \"-1\"); // \u5220\u9664\u6807\u8bb0\n\n /* \u6784\u9020\u65b9\u6cd5 */\n public HashMapOpenAddressing() {\n size = 0;\n buckets = new Pair[capacity];\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n int HashFunc(int key) {\n return key % capacity;\n }\n\n /* \u8d1f\u8f7d\u56e0\u5b50 */\n double LoadFactor() {\n return (double)size / capacity;\n }\n\n /* \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15 */\n int FindBucket(int key) {\n int index = HashFunc(key);\n int firstTombstone = -1;\n // \u7ebf\u6027\u63a2\u6d4b\uff0c\u5f53\u9047\u5230\u7a7a\u6876\u65f6\u8df3\u51fa\n while (buckets[index] != null) {\n // \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5bf9\u5e94\u6876\u7d22\u5f15\n if (buckets[index].key == key) {\n // \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\n if (firstTombstone != -1) {\n buckets[firstTombstone] = buckets[index];\n buckets[index] = TOMBSTONE;\n return firstTombstone; // \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\n }\n return index; // \u8fd4\u56de\u6876\u7d22\u5f15\n }\n // \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\n if (firstTombstone == -1 && buckets[index] == TOMBSTONE) {\n firstTombstone = index;\n }\n // \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\n index = (index + 1) % capacity;\n }\n // \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\n return firstTombstone == -1 ? index : firstTombstone;\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n public string? Get(int key) {\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n int index = FindBucket(key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n if (buckets[index] != null && buckets[index] != TOMBSTONE) {\n return buckets[index].val;\n }\n // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de null\n return null;\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n public void Put(int key, string val) {\n // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if (LoadFactor() > loadThres) {\n Extend();\n }\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n int index = FindBucket(key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val \u5e76\u8fd4\u56de\n if (buckets[index] != null && buckets[index] != TOMBSTONE) {\n buckets[index].val = val;\n return;\n }\n // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\n buckets[index] = new Pair(key, val);\n size++;\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n public void Remove(int key) {\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n int index = FindBucket(key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\n if (buckets[index] != null && buckets[index] != TOMBSTONE) {\n buckets[index] = TOMBSTONE;\n size--;\n }\n }\n\n /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n void Extend() {\n // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n Pair[] bucketsTmp = buckets;\n // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n capacity *= extendRatio;\n buckets = new Pair[capacity];\n size = 0;\n // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n foreach (Pair pair in bucketsTmp) {\n if (pair != null && pair != TOMBSTONE) {\n Put(pair.key, pair.val);\n }\n }\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n public void Print() {\n foreach (Pair pair in buckets) {\n if (pair == null) {\n Console.WriteLine(\"null\");\n } else if (pair == TOMBSTONE) {\n Console.WriteLine(\"TOMBSTONE\");\n } else {\n Console.WriteLine(pair.key + \" -> \" + pair.val);\n }\n }\n }\n}\n
hash_map_open_addressing.go/* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\ntype hashMapOpenAddressing struct {\n size int // \u952e\u503c\u5bf9\u6570\u91cf\n capacity int // \u54c8\u5e0c\u8868\u5bb9\u91cf\n loadThres float64 // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n extendRatio int // \u6269\u5bb9\u500d\u6570\n buckets []pair // \u6876\u6570\u7ec4\n removed pair // \u5220\u9664\u6807\u8bb0\n}\n\n/* \u6784\u9020\u65b9\u6cd5 */\nfunc newHashMapOpenAddressing() *hashMapOpenAddressing {\n buckets := make([]pair, 4)\n return &hashMapOpenAddressing{\n size: 0,\n capacity: 4,\n loadThres: 2.0 / 3.0,\n extendRatio: 2,\n buckets: buckets,\n removed: pair{\n key: -1,\n val: \"-1\",\n },\n }\n}\n\n/* \u54c8\u5e0c\u51fd\u6570 */\nfunc (m *hashMapOpenAddressing) hashFunc(key int) int {\n return key % m.capacity\n}\n\n/* \u8d1f\u8f7d\u56e0\u5b50 */\nfunc (m *hashMapOpenAddressing) loadFactor() float64 {\n return float64(m.size) / float64(m.capacity)\n}\n\n/* \u67e5\u8be2\u64cd\u4f5c */\nfunc (m *hashMapOpenAddressing) get(key int) string {\n idx := m.hashFunc(key)\n // \u7ebf\u6027\u63a2\u6d4b\uff0c\u4ece index \u5f00\u59cb\u5411\u540e\u904d\u5386\n for i := 0; i < m.capacity; i++ {\n // \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\n j := (idx + i) % m.capacity\n // \u82e5\u9047\u5230\u7a7a\u6876\uff0c\u8bf4\u660e\u65e0\u6b64 key \uff0c\u5219\u8fd4\u56de null\n if m.buckets[j] == (pair{}) {\n return \"\"\n }\n // \u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n if m.buckets[j].key == key && m.buckets[j] != m.removed {\n return m.buckets[j].val\n }\n }\n // \u82e5\u672a\u627e\u5230 key \u5219\u8fd4\u56de\u7a7a\u5b57\u7b26\u4e32\n return \"\"\n}\n\n/* \u6dfb\u52a0\u64cd\u4f5c */\nfunc (m *hashMapOpenAddressing) put(key int, val string) {\n // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if m.loadFactor() > m.loadThres {\n m.extend()\n }\n idx := m.hashFunc(key)\n // \u7ebf\u6027\u63a2\u6d4b\uff0c\u4ece index \u5f00\u59cb\u5411\u540e\u904d\u5386\n for i := 0; i < m.capacity; i++ {\n // \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\n j := (idx + i) % m.capacity\n // \u82e5\u9047\u5230\u7a7a\u6876\u3001\u6216\u5e26\u6709\u5220\u9664\u6807\u8bb0\u7684\u6876\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u653e\u5165\u8be5\u6876\n if m.buckets[j] == (pair{}) || m.buckets[j] == m.removed {\n m.buckets[j] = pair{\n key: key,\n val: val,\n }\n m.size += 1\n return\n }\n // \u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val\n if m.buckets[j].key == key {\n m.buckets[j].val = val\n }\n }\n}\n\n/* \u5220\u9664\u64cd\u4f5c */\nfunc (m *hashMapOpenAddressing) remove(key int) {\n idx := m.hashFunc(key)\n // \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\n // \u7ebf\u6027\u63a2\u6d4b\uff0c\u4ece index \u5f00\u59cb\u5411\u540e\u904d\u5386\n for i := 0; i < m.capacity; i++ {\n // \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\n j := (idx + i) % m.capacity\n // \u82e5\u9047\u5230\u7a7a\u6876\uff0c\u8bf4\u660e\u65e0\u6b64 key \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if m.buckets[j] == (pair{}) {\n return\n }\n // \u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u6807\u8bb0\u5220\u9664\u5e76\u8fd4\u56de\n if m.buckets[j].key == key {\n m.buckets[j] = m.removed\n m.size -= 1\n }\n }\n}\n\n/* \u6269\u5bb9\u54c8\u5e0c\u8868 */\nfunc (m *hashMapOpenAddressing) extend() {\n // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n tmpBuckets := make([]pair, len(m.buckets))\n copy(tmpBuckets, m.buckets)\n\n // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n m.capacity *= m.extendRatio\n m.buckets = make([]pair, m.capacity)\n m.size = 0\n // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n for _, p := range tmpBuckets {\n if p != (pair{}) && p != m.removed {\n m.put(p.key, p.val)\n }\n }\n}\n\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nfunc (m *hashMapOpenAddressing) print() {\n for _, p := range m.buckets {\n if p != (pair{}) {\n fmt.Println(strconv.Itoa(p.key) + \" -> \" + p.val)\n } else {\n fmt.Println(\"nil\")\n }\n }\n}\n
hash_map_open_addressing.swift/* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\nclass HashMapOpenAddressing {\n var size: Int // \u952e\u503c\u5bf9\u6570\u91cf\n var capacity: Int // \u54c8\u5e0c\u8868\u5bb9\u91cf\n var loadThres: Double // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n var extendRatio: Int // \u6269\u5bb9\u500d\u6570\n var buckets: [Pair?] // \u6876\u6570\u7ec4\n var TOMBSTONE: Pair // \u5220\u9664\u6807\u8bb0\n\n /* \u6784\u9020\u65b9\u6cd5 */\n init() {\n size = 0\n capacity = 4\n loadThres = 2.0 / 3.0\n extendRatio = 2\n buckets = Array(repeating: nil, count: capacity)\n TOMBSTONE = Pair(key: -1, val: \"-1\")\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n func hashFunc(key: Int) -> Int {\n key % capacity\n }\n\n /* \u8d1f\u8f7d\u56e0\u5b50 */\n func loadFactor() -> Double {\n Double(size / capacity)\n }\n\n /* \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15 */\n func findBucket(key: Int) -> Int {\n var index = hashFunc(key: key)\n var firstTombstone = -1\n // \u7ebf\u6027\u63a2\u6d4b\uff0c\u5f53\u9047\u5230\u7a7a\u6876\u65f6\u8df3\u51fa\n while buckets[index] != nil {\n // \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5bf9\u5e94\u6876\u7d22\u5f15\n if buckets[index]!.key == key {\n // \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\n if firstTombstone != -1 {\n buckets[firstTombstone] = buckets[index]\n buckets[index] = TOMBSTONE\n return firstTombstone // \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\n }\n return index // \u8fd4\u56de\u6876\u7d22\u5f15\n }\n // \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\n if firstTombstone == -1 && buckets[index] == TOMBSTONE {\n firstTombstone = index\n }\n // \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\n index = (index + 1) % capacity\n }\n // \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\n return firstTombstone == -1 ? index : firstTombstone\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n func get(key: Int) -> String? {\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n let index = findBucket(key: key)\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n if buckets[index] != nil, buckets[index] != TOMBSTONE {\n return buckets[index]!.val\n }\n // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de null\n return nil\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n func put(key: Int, val: String) {\n // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if loadFactor() > loadThres {\n extend()\n }\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n let index = findBucket(key: key)\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val \u5e76\u8fd4\u56de\n if buckets[index] != nil, buckets[index] != TOMBSTONE {\n buckets[index]!.val = val\n return\n }\n // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\n buckets[index] = Pair(key: key, val: val)\n size += 1\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n func remove(key: Int) {\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n let index = findBucket(key: key)\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\n if buckets[index] != nil, buckets[index] != TOMBSTONE {\n buckets[index] = TOMBSTONE\n size -= 1\n }\n }\n\n /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n func extend() {\n // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n let bucketsTmp = buckets\n // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n capacity *= extendRatio\n buckets = Array(repeating: nil, count: capacity)\n size = 0\n // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n for pair in bucketsTmp {\n if let pair, pair != TOMBSTONE {\n put(key: pair.key, val: pair.val)\n }\n }\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n func print() {\n for pair in buckets {\n if pair == nil {\n Swift.print(\"null\")\n } else if pair == TOMBSTONE {\n Swift.print(\"TOMBSTONE\")\n } else {\n Swift.print(\"\\(pair!.key) -> \\(pair!.val)\")\n }\n }\n }\n}\n
hash_map_open_addressing.js/* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\nclass HashMapOpenAddressing {\n #size; // \u952e\u503c\u5bf9\u6570\u91cf\n #capacity; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n #loadThres; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n #extendRatio; // \u6269\u5bb9\u500d\u6570\n #buckets; // \u6876\u6570\u7ec4\n #TOMBSTONE; // \u5220\u9664\u6807\u8bb0\n\n /* \u6784\u9020\u65b9\u6cd5 */\n constructor() {\n this.#size = 0; // \u952e\u503c\u5bf9\u6570\u91cf\n this.#capacity = 4; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n this.#loadThres = 2.0 / 3.0; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n this.#extendRatio = 2; // \u6269\u5bb9\u500d\u6570\n this.#buckets = Array(this.#capacity).fill(null); // \u6876\u6570\u7ec4\n this.#TOMBSTONE = new Pair(-1, '-1'); // \u5220\u9664\u6807\u8bb0\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n #hashFunc(key) {\n return key % this.#capacity;\n }\n\n /* \u8d1f\u8f7d\u56e0\u5b50 */\n #loadFactor() {\n return this.#size / this.#capacity;\n }\n\n /* \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15 */\n #findBucket(key) {\n let index = this.#hashFunc(key);\n let firstTombstone = -1;\n // \u7ebf\u6027\u63a2\u6d4b\uff0c\u5f53\u9047\u5230\u7a7a\u6876\u65f6\u8df3\u51fa\n while (this.#buckets[index] !== null) {\n // \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5bf9\u5e94\u6876\u7d22\u5f15\n if (this.#buckets[index].key === key) {\n // \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\n if (firstTombstone !== -1) {\n this.#buckets[firstTombstone] = this.#buckets[index];\n this.#buckets[index] = this.#TOMBSTONE;\n return firstTombstone; // \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\n }\n return index; // \u8fd4\u56de\u6876\u7d22\u5f15\n }\n // \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\n if (\n firstTombstone === -1 &&\n this.#buckets[index] === this.#TOMBSTONE\n ) {\n firstTombstone = index;\n }\n // \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\n index = (index + 1) % this.#capacity;\n }\n // \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\n return firstTombstone === -1 ? index : firstTombstone;\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n get(key) {\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n const index = this.#findBucket(key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n if (\n this.#buckets[index] !== null &&\n this.#buckets[index] !== this.#TOMBSTONE\n ) {\n return this.#buckets[index].val;\n }\n // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de null\n return null;\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n put(key, val) {\n // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if (this.#loadFactor() > this.#loadThres) {\n this.#extend();\n }\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n const index = this.#findBucket(key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val \u5e76\u8fd4\u56de\n if (\n this.#buckets[index] !== null &&\n this.#buckets[index] !== this.#TOMBSTONE\n ) {\n this.#buckets[index].val = val;\n return;\n }\n // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\n this.#buckets[index] = new Pair(key, val);\n this.#size++;\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n remove(key) {\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n const index = this.#findBucket(key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\n if (\n this.#buckets[index] !== null &&\n this.#buckets[index] !== this.#TOMBSTONE\n ) {\n this.#buckets[index] = this.#TOMBSTONE;\n this.#size--;\n }\n }\n\n /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n #extend() {\n // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n const bucketsTmp = this.#buckets;\n // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n this.#capacity *= this.#extendRatio;\n this.#buckets = Array(this.#capacity).fill(null);\n this.#size = 0;\n // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n for (const pair of bucketsTmp) {\n if (pair !== null && pair !== this.#TOMBSTONE) {\n this.put(pair.key, pair.val);\n }\n }\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n print() {\n for (const pair of this.#buckets) {\n if (pair === null) {\n console.log('null');\n } else if (pair === this.#TOMBSTONE) {\n console.log('TOMBSTONE');\n } else {\n console.log(pair.key + ' -> ' + pair.val);\n }\n }\n }\n}\n
hash_map_open_addressing.ts/* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\nclass HashMapOpenAddressing {\n private size: number; // \u952e\u503c\u5bf9\u6570\u91cf\n private capacity: number; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n private loadThres: number; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n private extendRatio: number; // \u6269\u5bb9\u500d\u6570\n private buckets: Array<Pair | null>; // \u6876\u6570\u7ec4\n private TOMBSTONE: Pair; // \u5220\u9664\u6807\u8bb0\n\n /* \u6784\u9020\u65b9\u6cd5 */\n constructor() {\n this.size = 0; // \u952e\u503c\u5bf9\u6570\u91cf\n this.capacity = 4; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n this.loadThres = 2.0 / 3.0; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n this.extendRatio = 2; // \u6269\u5bb9\u500d\u6570\n this.buckets = Array(this.capacity).fill(null); // \u6876\u6570\u7ec4\n this.TOMBSTONE = new Pair(-1, '-1'); // \u5220\u9664\u6807\u8bb0\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n private hashFunc(key: number): number {\n return key % this.capacity;\n }\n\n /* \u8d1f\u8f7d\u56e0\u5b50 */\n private loadFactor(): number {\n return this.size / this.capacity;\n }\n\n /* \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15 */\n private findBucket(key: number): number {\n let index = this.hashFunc(key);\n let firstTombstone = -1;\n // \u7ebf\u6027\u63a2\u6d4b\uff0c\u5f53\u9047\u5230\u7a7a\u6876\u65f6\u8df3\u51fa\n while (this.buckets[index] !== null) {\n // \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5bf9\u5e94\u6876\u7d22\u5f15\n if (this.buckets[index]!.key === key) {\n // \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\n if (firstTombstone !== -1) {\n this.buckets[firstTombstone] = this.buckets[index];\n this.buckets[index] = this.TOMBSTONE;\n return firstTombstone; // \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\n }\n return index; // \u8fd4\u56de\u6876\u7d22\u5f15\n }\n // \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\n if (\n firstTombstone === -1 &&\n this.buckets[index] === this.TOMBSTONE\n ) {\n firstTombstone = index;\n }\n // \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\n index = (index + 1) % this.capacity;\n }\n // \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\n return firstTombstone === -1 ? index : firstTombstone;\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n get(key: number): string | null {\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n const index = this.findBucket(key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n if (\n this.buckets[index] !== null &&\n this.buckets[index] !== this.TOMBSTONE\n ) {\n return this.buckets[index]!.val;\n }\n // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de null\n return null;\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n put(key: number, val: string): void {\n // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if (this.loadFactor() > this.loadThres) {\n this.extend();\n }\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n const index = this.findBucket(key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val \u5e76\u8fd4\u56de\n if (\n this.buckets[index] !== null &&\n this.buckets[index] !== this.TOMBSTONE\n ) {\n this.buckets[index]!.val = val;\n return;\n }\n // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\n this.buckets[index] = new Pair(key, val);\n this.size++;\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n remove(key: number): void {\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n const index = this.findBucket(key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\n if (\n this.buckets[index] !== null &&\n this.buckets[index] !== this.TOMBSTONE\n ) {\n this.buckets[index] = this.TOMBSTONE;\n this.size--;\n }\n }\n\n /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n private extend(): void {\n // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n const bucketsTmp = this.buckets;\n // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n this.capacity *= this.extendRatio;\n this.buckets = Array(this.capacity).fill(null);\n this.size = 0;\n // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n for (const pair of bucketsTmp) {\n if (pair !== null && pair !== this.TOMBSTONE) {\n this.put(pair.key, pair.val);\n }\n }\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n print(): void {\n for (const pair of this.buckets) {\n if (pair === null) {\n console.log('null');\n } else if (pair === this.TOMBSTONE) {\n console.log('TOMBSTONE');\n } else {\n console.log(pair.key + ' -> ' + pair.val);\n }\n }\n }\n}\n
hash_map_open_addressing.dart/* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\nclass HashMapOpenAddressing {\n late int _size; // \u952e\u503c\u5bf9\u6570\u91cf\n int _capacity = 4; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n double _loadThres = 2.0 / 3.0; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n int _extendRatio = 2; // \u6269\u5bb9\u500d\u6570\n late List<Pair?> _buckets; // \u6876\u6570\u7ec4\n Pair _TOMBSTONE = Pair(-1, \"-1\"); // \u5220\u9664\u6807\u8bb0\n\n /* \u6784\u9020\u65b9\u6cd5 */\n HashMapOpenAddressing() {\n _size = 0;\n _buckets = List.generate(_capacity, (index) => null);\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n int hashFunc(int key) {\n return key % _capacity;\n }\n\n /* \u8d1f\u8f7d\u56e0\u5b50 */\n double loadFactor() {\n return _size / _capacity;\n }\n\n /* \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15 */\n int findBucket(int key) {\n int index = hashFunc(key);\n int firstTombstone = -1;\n // \u7ebf\u6027\u63a2\u6d4b\uff0c\u5f53\u9047\u5230\u7a7a\u6876\u65f6\u8df3\u51fa\n while (_buckets[index] != null) {\n // \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5bf9\u5e94\u6876\u7d22\u5f15\n if (_buckets[index]!.key == key) {\n // \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\n if (firstTombstone != -1) {\n _buckets[firstTombstone] = _buckets[index];\n _buckets[index] = _TOMBSTONE;\n return firstTombstone; // \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\n }\n return index; // \u8fd4\u56de\u6876\u7d22\u5f15\n }\n // \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\n if (firstTombstone == -1 && _buckets[index] == _TOMBSTONE) {\n firstTombstone = index;\n }\n // \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\n index = (index + 1) % _capacity;\n }\n // \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\n return firstTombstone == -1 ? index : firstTombstone;\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n String? get(int key) {\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n int index = findBucket(key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n if (_buckets[index] != null && _buckets[index] != _TOMBSTONE) {\n return _buckets[index]!.val;\n }\n // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de null\n return null;\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n void put(int key, String val) {\n // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if (loadFactor() > _loadThres) {\n extend();\n }\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n int index = findBucket(key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val \u5e76\u8fd4\u56de\n if (_buckets[index] != null && _buckets[index] != _TOMBSTONE) {\n _buckets[index]!.val = val;\n return;\n }\n // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\n _buckets[index] = new Pair(key, val);\n _size++;\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n void remove(int key) {\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n int index = findBucket(key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\n if (_buckets[index] != null && _buckets[index] != _TOMBSTONE) {\n _buckets[index] = _TOMBSTONE;\n _size--;\n }\n }\n\n /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n void extend() {\n // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n List<Pair?> bucketsTmp = _buckets;\n // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n _capacity *= _extendRatio;\n _buckets = List.generate(_capacity, (index) => null);\n _size = 0;\n // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n for (Pair? pair in bucketsTmp) {\n if (pair != null && pair != _TOMBSTONE) {\n put(pair.key, pair.val);\n }\n }\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n void printHashMap() {\n for (Pair? pair in _buckets) {\n if (pair == null) {\n print(\"null\");\n } else if (pair == _TOMBSTONE) {\n print(\"TOMBSTONE\");\n } else {\n print(\"${pair.key} -> ${pair.val}\");\n }\n }\n }\n}\n
hash_map_open_addressing.rs/* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\nstruct HashMapOpenAddressing {\n size: usize, // \u952e\u503c\u5bf9\u6570\u91cf\n capacity: usize, // \u54c8\u5e0c\u8868\u5bb9\u91cf\n load_thres: f64, // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n extend_ratio: usize, // \u6269\u5bb9\u500d\u6570\n buckets: Vec<Option<Pair>>, // \u6876\u6570\u7ec4\n TOMBSTONE: Option<Pair>, // \u5220\u9664\u6807\u8bb0\n}\n\n\nimpl HashMapOpenAddressing {\n /* \u6784\u9020\u65b9\u6cd5 */\n fn new() -> Self {\n Self {\n size: 0,\n capacity: 4,\n load_thres: 2.0 / 3.0,\n extend_ratio: 2,\n buckets: vec![None; 4],\n TOMBSTONE: Some(Pair {key: -1, val: \"-1\".to_string()}),\n }\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n fn hash_func(&self, key: i32) -> usize {\n (key % self.capacity as i32) as usize\n }\n\n /* \u8d1f\u8f7d\u56e0\u5b50 */\n fn load_factor(&self) -> f64 {\n self.size as f64 / self.capacity as f64\n }\n\n /* \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15 */\n fn find_bucket(&mut self, key: i32) -> usize {\n let mut index = self.hash_func(key);\n let mut first_tombstone = -1;\n // \u7ebf\u6027\u63a2\u6d4b\uff0c\u5f53\u9047\u5230\u7a7a\u6876\u65f6\u8df3\u51fa\n while self.buckets[index].is_some() {\n // \u82e5\u9047\u5230 key\uff0c\u8fd4\u56de\u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n if self.buckets[index].as_ref().unwrap().key == key {\n // \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u5efa\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\n if first_tombstone != -1 {\n self.buckets[first_tombstone as usize] = self.buckets[index].take();\n self.buckets[index] = self.TOMBSTONE.clone();\n return first_tombstone as usize; // \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\n }\n return index; // \u8fd4\u56de\u6876\u7d22\u5f15\n }\n // \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\n if first_tombstone == -1 && self.buckets[index] == self.TOMBSTONE {\n first_tombstone = index as i32;\n }\n // \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\n index = (index + 1) % self.capacity;\n }\n // \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\n if first_tombstone == -1 { index } else { first_tombstone as usize }\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n fn get(&mut self, key: i32) -> Option<&str> {\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n let index = self.find_bucket(key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n if self.buckets[index].is_some() && self.buckets[index] != self.TOMBSTONE {\n return self.buckets[index].as_ref().map(|pair| &pair.val as &str);\n }\n // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de null\n None\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n fn put(&mut self, key: i32, val: String) {\n // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if self.load_factor() > self.load_thres {\n self.extend();\n }\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n let index = self.find_bucket(key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val \u5e76\u8fd4\u56de\n if self.buckets[index].is_some() && self.buckets[index] != self.TOMBSTONE {\n self.buckets[index].as_mut().unwrap().val = val;\n return;\n }\n // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\n self.buckets[index] = Some(Pair { key, val });\n self.size += 1;\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n fn remove(&mut self, key: i32) {\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n let index = self.find_bucket(key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\n if self.buckets[index].is_some() && self.buckets[index] != self.TOMBSTONE {\n self.buckets[index] = self.TOMBSTONE.clone();\n self.size -= 1;\n }\n }\n\n /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n fn extend(&mut self) {\n // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n let buckets_tmp = self.buckets.clone();\n // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n self.capacity *= self.extend_ratio;\n self.buckets = vec![None; self.capacity];\n self.size = 0;\n\n // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n for pair in buckets_tmp {\n if pair.is_none() || pair == self.TOMBSTONE {\n continue;\n }\n let pair = pair.unwrap();\n\n self.put(pair.key, pair.val);\n }\n }\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n fn print(&self) {\n for pair in &self.buckets {\n if pair.is_none() {\n println!(\"null\");\n } else if pair == &self.TOMBSTONE {\n println!(\"TOMBSTONE\");\n } else {\n let pair = pair.as_ref().unwrap();\n println!(\"{} -> {}\", pair.key, pair.val);\n }\n }\n }\n}\n
hash_map_open_addressing.c/* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\ntypedef struct {\n int size; // \u952e\u503c\u5bf9\u6570\u91cf\n int capacity; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n double loadThres; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n int extendRatio; // \u6269\u5bb9\u500d\u6570\n Pair **buckets; // \u6876\u6570\u7ec4\n Pair *TOMBSTONE; // \u5220\u9664\u6807\u8bb0\n} HashMapOpenAddressing;\n\n/* \u6784\u9020\u51fd\u6570 */\nHashMapOpenAddressing *newHashMapOpenAddressing() {\n HashMapOpenAddressing *hashMap = (HashMapOpenAddressing *)malloc(sizeof(HashMapOpenAddressing));\n hashMap->size = 0;\n hashMap->capacity = 4;\n hashMap->loadThres = 2.0 / 3.0;\n hashMap->extendRatio = 2;\n hashMap->buckets = (Pair **)malloc(sizeof(Pair *) * hashMap->capacity);\n hashMap->TOMBSTONE = (Pair *)malloc(sizeof(Pair));\n hashMap->TOMBSTONE->key = -1;\n hashMap->TOMBSTONE->val = \"-1\";\n\n return hashMap;\n}\n\n/* \u6790\u6784\u51fd\u6570 */\nvoid delHashMapOpenAddressing(HashMapOpenAddressing *hashMap) {\n for (int i = 0; i < hashMap->capacity; i++) {\n Pair *pair = hashMap->buckets[i];\n if (pair != NULL && pair != hashMap->TOMBSTONE) {\n free(pair->val);\n free(pair);\n }\n }\n}\n\n/* \u54c8\u5e0c\u51fd\u6570 */\nint hashFunc(HashMapOpenAddressing *hashMap, int key) {\n return key % hashMap->capacity;\n}\n\n/* \u8d1f\u8f7d\u56e0\u5b50 */\ndouble loadFactor(HashMapOpenAddressing *hashMap) {\n return (double)hashMap->size / (double)hashMap->capacity;\n}\n\n/* \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15 */\nint findBucket(HashMapOpenAddressing *hashMap, int key) {\n int index = hashFunc(hashMap, key);\n int firstTombstone = -1;\n // \u7ebf\u6027\u63a2\u6d4b\uff0c\u5f53\u9047\u5230\u7a7a\u6876\u65f6\u8df3\u51fa\n while (hashMap->buckets[index] != NULL) {\n // \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5bf9\u5e94\u6876\u7d22\u5f15\n if (hashMap->buckets[index]->key == key) {\n // \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\n if (firstTombstone != -1) {\n hashMap->buckets[firstTombstone] = hashMap->buckets[index];\n hashMap->buckets[index] = hashMap->TOMBSTONE;\n return firstTombstone; // \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\n }\n return index; // \u8fd4\u56de\u6876\u7d22\u5f15\n }\n // \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\n if (firstTombstone == -1 && hashMap->buckets[index] == hashMap->TOMBSTONE) {\n firstTombstone = index;\n }\n // \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\n index = (index + 1) % hashMap->capacity;\n }\n // \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\n return firstTombstone == -1 ? index : firstTombstone;\n}\n\n/* \u67e5\u8be2\u64cd\u4f5c */\nchar *get(HashMapOpenAddressing *hashMap, int key) {\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n int index = findBucket(hashMap, key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n if (hashMap->buckets[index] != NULL && hashMap->buckets[index] != hashMap->TOMBSTONE) {\n return hashMap->buckets[index]->val;\n }\n // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u7a7a\u5b57\u7b26\u4e32\n return \"\";\n}\n\n/* \u6dfb\u52a0\u64cd\u4f5c */\nvoid put(HashMapOpenAddressing *hashMap, int key, char *val) {\n // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if (loadFactor(hashMap) > hashMap->loadThres) {\n extend(hashMap);\n }\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n int index = findBucket(hashMap, key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val \u5e76\u8fd4\u56de\n if (hashMap->buckets[index] != NULL && hashMap->buckets[index] != hashMap->TOMBSTONE) {\n free(hashMap->buckets[index]->val);\n hashMap->buckets[index]->val = (char *)malloc(sizeof(strlen(val + 1)));\n strcpy(hashMap->buckets[index]->val, val);\n hashMap->buckets[index]->val[strlen(val)] = '\\0';\n return;\n }\n // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\n Pair *pair = (Pair *)malloc(sizeof(Pair));\n pair->key = key;\n pair->val = (char *)malloc(sizeof(strlen(val + 1)));\n strcpy(pair->val, val);\n pair->val[strlen(val)] = '\\0';\n\n hashMap->buckets[index] = pair;\n hashMap->size++;\n}\n\n/* \u5220\u9664\u64cd\u4f5c */\nvoid removeItem(HashMapOpenAddressing *hashMap, int key) {\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n int index = findBucket(hashMap, key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\n if (hashMap->buckets[index] != NULL && hashMap->buckets[index] != hashMap->TOMBSTONE) {\n Pair *pair = hashMap->buckets[index];\n free(pair->val);\n free(pair);\n hashMap->buckets[index] = hashMap->TOMBSTONE;\n hashMap->size--;\n }\n}\n\n/* \u6269\u5bb9\u54c8\u5e0c\u8868 */\nvoid extend(HashMapOpenAddressing *hashMap) {\n // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n Pair **bucketsTmp = hashMap->buckets;\n int oldCapacity = hashMap->capacity;\n // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n hashMap->capacity *= hashMap->extendRatio;\n hashMap->buckets = (Pair **)malloc(sizeof(Pair *) * hashMap->capacity);\n hashMap->size = 0;\n // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n for (int i = 0; i < oldCapacity; i++) {\n Pair *pair = bucketsTmp[i];\n if (pair != NULL && pair != hashMap->TOMBSTONE) {\n put(hashMap, pair->key, pair->val);\n free(pair->val);\n free(pair);\n }\n }\n free(bucketsTmp);\n}\n\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nvoid print(HashMapOpenAddressing *hashMap) {\n for (int i = 0; i < hashMap->capacity; i++) {\n Pair *pair = hashMap->buckets[i];\n if (pair == NULL) {\n printf(\"NULL\\n\");\n } else if (pair == hashMap->TOMBSTONE) {\n printf(\"TOMBSTONE\\n\");\n } else {\n printf(\"%d -> %s\\n\", pair->key, pair->val);\n }\n }\n}\n
hash_map_open_addressing.zig[class]{HashMapOpenAddressing}-[func]{}\n
"},{"location":"chapter_hashing/hash_collision/#2","title":"2. \u00a0 \u5e73\u65b9\u63a2\u6d4b","text":"\u5e73\u65b9\u63a2\u6d4b\u4e0e\u7ebf\u6027\u63a2\u6d4b\u7c7b\u4f3c\uff0c\u90fd\u662f\u5f00\u653e\u5bfb\u5740\u7684\u5e38\u89c1\u7b56\u7565\u4e4b\u4e00\u3002\u5f53\u53d1\u751f\u51b2\u7a81\u65f6\uff0c\u5e73\u65b9\u63a2\u6d4b\u4e0d\u662f\u7b80\u5355\u5730\u8df3\u8fc7\u4e00\u4e2a\u56fa\u5b9a\u7684\u6b65\u6570\uff0c\u800c\u662f\u8df3\u8fc7\u201c\u63a2\u6d4b\u6b21\u6570\u7684\u5e73\u65b9\u201d\u7684\u6b65\u6570\uff0c\u5373 \\(1, 4, 9, \\dots\\) \u6b65\u3002
\u5e73\u65b9\u63a2\u6d4b\u4e3b\u8981\u5177\u6709\u4ee5\u4e0b\u4f18\u52bf\u3002
\u7136\u800c\uff0c\u5e73\u65b9\u63a2\u6d4b\u5e76\u4e0d\u662f\u5b8c\u7f8e\u7684\u3002
\u987e\u540d\u601d\u4e49\uff0c\u591a\u6b21\u54c8\u5e0c\u65b9\u6cd5\u4f7f\u7528\u591a\u4e2a\u54c8\u5e0c\u51fd\u6570 \\(f_1(x)\\)\u3001\\(f_2(x)\\)\u3001\\(f_3(x)\\)\u3001\\(\\dots\\) \u8fdb\u884c\u63a2\u6d4b\u3002
\u4e0e\u7ebf\u6027\u63a2\u6d4b\u76f8\u6bd4\uff0c\u591a\u6b21\u54c8\u5e0c\u65b9\u6cd5\u4e0d\u6613\u4ea7\u751f\u805a\u96c6\uff0c\u4f46\u591a\u4e2a\u54c8\u5e0c\u51fd\u6570\u4f1a\u5e26\u6765\u989d\u5916\u7684\u8ba1\u7b97\u91cf\u3002
Tip
\u8bf7\u6ce8\u610f\uff0c\u5f00\u653e\u5bfb\u5740\uff08\u7ebf\u6027\u63a2\u6d4b\u3001\u5e73\u65b9\u63a2\u6d4b\u548c\u591a\u6b21\u54c8\u5e0c\uff09\u54c8\u5e0c\u8868\u90fd\u5b58\u5728\u201c\u4e0d\u80fd\u76f4\u63a5\u5220\u9664\u5143\u7d20\u201d\u7684\u95ee\u9898\u3002
"},{"location":"chapter_hashing/hash_collision/#623","title":"6.2.3 \u00a0 \u7f16\u7a0b\u8bed\u8a00\u7684\u9009\u62e9","text":"\u5404\u79cd\u7f16\u7a0b\u8bed\u8a00\u91c7\u53d6\u4e86\u4e0d\u540c\u7684\u54c8\u5e0c\u8868\u5b9e\u73b0\u7b56\u7565\uff0c\u4e0b\u9762\u4e3e\u51e0\u4e2a\u4f8b\u5b50\u3002
\u300c\u54c8\u5e0c\u8868 hash table\u300d\uff0c\u53c8\u79f0\u300c\u6563\u5217\u8868\u300d\uff0c\u5176\u901a\u8fc7\u5efa\u7acb\u952e key
\u4e0e\u503c value
\u4e4b\u95f4\u7684\u6620\u5c04\uff0c\u5b9e\u73b0\u9ad8\u6548\u7684\u5143\u7d20\u67e5\u8be2\u3002\u5177\u4f53\u800c\u8a00\uff0c\u6211\u4eec\u5411\u54c8\u5e0c\u8868\u8f93\u5165\u4e00\u4e2a\u952e key
\uff0c\u5219\u53ef\u4ee5\u5728 \\(O(1)\\) \u65f6\u95f4\u5185\u83b7\u53d6\u5bf9\u5e94\u7684\u503c value
\u3002
\u5982\u56fe 6-1 \u6240\u793a\uff0c\u7ed9\u5b9a \\(n\\) \u4e2a\u5b66\u751f\uff0c\u6bcf\u4e2a\u5b66\u751f\u90fd\u6709\u201c\u59d3\u540d\u201d\u548c\u201c\u5b66\u53f7\u201d\u4e24\u9879\u6570\u636e\u3002\u5047\u5982\u6211\u4eec\u5e0c\u671b\u5b9e\u73b0\u201c\u8f93\u5165\u4e00\u4e2a\u5b66\u53f7\uff0c\u8fd4\u56de\u5bf9\u5e94\u7684\u59d3\u540d\u201d\u7684\u67e5\u8be2\u529f\u80fd\uff0c\u5219\u53ef\u4ee5\u91c7\u7528\u56fe 6-1 \u6240\u793a\u7684\u54c8\u5e0c\u8868\u6765\u5b9e\u73b0\u3002
\u56fe 6-1 \u00a0 \u54c8\u5e0c\u8868\u7684\u62bd\u8c61\u8868\u793a
\u9664\u54c8\u5e0c\u8868\u5916\uff0c\u6570\u7ec4\u548c\u94fe\u8868\u4e5f\u53ef\u4ee5\u5b9e\u73b0\u67e5\u8be2\u529f\u80fd\uff0c\u5b83\u4eec\u7684\u6548\u7387\u5bf9\u6bd4\u5982\u8868 6-1 \u6240\u793a\u3002
\u8868 6-1 \u00a0 \u5143\u7d20\u67e5\u8be2\u6548\u7387\u5bf9\u6bd4
\u6570\u7ec4 \u94fe\u8868 \u54c8\u5e0c\u8868 \u67e5\u627e\u5143\u7d20 \\(O(n)\\) \\(O(n)\\) \\(O(1)\\) \u6dfb\u52a0\u5143\u7d20 \\(O(1)\\) \\(O(1)\\) \\(O(1)\\) \u5220\u9664\u5143\u7d20 \\(O(n)\\) \\(O(n)\\) \\(O(1)\\)\u89c2\u5bdf\u53d1\u73b0\uff0c\u5728\u54c8\u5e0c\u8868\u4e2d\u8fdb\u884c\u589e\u5220\u67e5\u6539\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u90fd\u662f \\(O(1)\\) \uff0c\u975e\u5e38\u9ad8\u6548\u3002
"},{"location":"chapter_hashing/hash_map/#611","title":"6.1.1 \u00a0 \u54c8\u5e0c\u8868\u5e38\u7528\u64cd\u4f5c","text":"\u54c8\u5e0c\u8868\u7684\u5e38\u89c1\u64cd\u4f5c\u5305\u62ec\uff1a\u521d\u59cb\u5316\u3001\u67e5\u8be2\u64cd\u4f5c\u3001\u6dfb\u52a0\u952e\u503c\u5bf9\u548c\u5220\u9664\u952e\u503c\u5bf9\u7b49\uff0c\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig hash_map.py# \u521d\u59cb\u5316\u54c8\u5e0c\u8868\nhmap: dict = {}\n\n# \u6dfb\u52a0\u64cd\u4f5c\n# \u5728\u54c8\u5e0c\u8868\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9 (key, value)\nhmap[12836] = \"\u5c0f\u54c8\"\nhmap[15937] = \"\u5c0f\u5570\"\nhmap[16750] = \"\u5c0f\u7b97\"\nhmap[13276] = \"\u5c0f\u6cd5\"\nhmap[10583] = \"\u5c0f\u9e2d\"\n\n# \u67e5\u8be2\u64cd\u4f5c\n# \u5411\u54c8\u5e0c\u8868\u8f93\u5165\u952e key \uff0c\u5f97\u5230\u503c value\nname: str = hmap[15937]\n\n# \u5220\u9664\u64cd\u4f5c\n# \u5728\u54c8\u5e0c\u8868\u4e2d\u5220\u9664\u952e\u503c\u5bf9 (key, value)\nhmap.pop(10583)\n
hash_map.cpp/* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nunordered_map<int, string> map;\n\n/* \u6dfb\u52a0\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9 (key, value)\nmap[12836] = \"\u5c0f\u54c8\";\nmap[15937] = \"\u5c0f\u5570\";\nmap[16750] = \"\u5c0f\u7b97\";\nmap[13276] = \"\u5c0f\u6cd5\";\nmap[10583] = \"\u5c0f\u9e2d\";\n\n/* \u67e5\u8be2\u64cd\u4f5c */\n// \u5411\u54c8\u5e0c\u8868\u8f93\u5165\u952e key \uff0c\u5f97\u5230\u503c value\nstring name = map[15937];\n\n/* \u5220\u9664\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u5220\u9664\u952e\u503c\u5bf9 (key, value)\nmap.erase(10583);\n
hash_map.java/* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nMap<Integer, String> map = new HashMap<>();\n\n/* \u6dfb\u52a0\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9 (key, value)\nmap.put(12836, \"\u5c0f\u54c8\"); \nmap.put(15937, \"\u5c0f\u5570\"); \nmap.put(16750, \"\u5c0f\u7b97\"); \nmap.put(13276, \"\u5c0f\u6cd5\");\nmap.put(10583, \"\u5c0f\u9e2d\");\n\n/* \u67e5\u8be2\u64cd\u4f5c */\n// \u5411\u54c8\u5e0c\u8868\u8f93\u5165\u952e key \uff0c\u5f97\u5230\u503c value\nString name = map.get(15937);\n\n/* \u5220\u9664\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u5220\u9664\u952e\u503c\u5bf9 (key, value)\nmap.remove(10583);\n
hash_map.cs/* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nDictionary<int, string> map = new() {\n /* \u6dfb\u52a0\u64cd\u4f5c */\n // \u5728\u54c8\u5e0c\u8868\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9 (key, value)\n { 12836, \"\u5c0f\u54c8\" },\n { 15937, \"\u5c0f\u5570\" },\n { 16750, \"\u5c0f\u7b97\" },\n { 13276, \"\u5c0f\u6cd5\" },\n { 10583, \"\u5c0f\u9e2d\" }\n};\n\n/* \u67e5\u8be2\u64cd\u4f5c */\n// \u5411\u54c8\u5e0c\u8868\u8f93\u5165\u952e key \uff0c\u5f97\u5230\u503c value\nstring name = map[15937];\n\n/* \u5220\u9664\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u5220\u9664\u952e\u503c\u5bf9 (key, value)\nmap.Remove(10583);\n
hash_map_test.go/* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nhmap := make(map[int]string)\n\n/* \u6dfb\u52a0\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9 (key, value)\nhmap[12836] = \"\u5c0f\u54c8\"\nhmap[15937] = \"\u5c0f\u5570\"\nhmap[16750] = \"\u5c0f\u7b97\"\nhmap[13276] = \"\u5c0f\u6cd5\"\nhmap[10583] = \"\u5c0f\u9e2d\"\n\n/* \u67e5\u8be2\u64cd\u4f5c */\n// \u5411\u54c8\u5e0c\u8868\u8f93\u5165\u952e key \uff0c\u5f97\u5230\u503c value\nname := hmap[15937]\n\n/* \u5220\u9664\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u5220\u9664\u952e\u503c\u5bf9 (key, value)\ndelete(hmap, 10583)\n
hash_map.swift/* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nvar map: [Int: String] = [:]\n\n/* \u6dfb\u52a0\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9 (key, value)\nmap[12836] = \"\u5c0f\u54c8\"\nmap[15937] = \"\u5c0f\u5570\"\nmap[16750] = \"\u5c0f\u7b97\"\nmap[13276] = \"\u5c0f\u6cd5\"\nmap[10583] = \"\u5c0f\u9e2d\"\n\n/* \u67e5\u8be2\u64cd\u4f5c */\n// \u5411\u54c8\u5e0c\u8868\u8f93\u5165\u952e key \uff0c\u5f97\u5230\u503c value\nlet name = map[15937]!\n\n/* \u5220\u9664\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u5220\u9664\u952e\u503c\u5bf9 (key, value)\nmap.removeValue(forKey: 10583)\n
hash_map.js/* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nconst map = new Map();\n/* \u6dfb\u52a0\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9 (key, value)\nmap.set(12836, '\u5c0f\u54c8');\nmap.set(15937, '\u5c0f\u5570');\nmap.set(16750, '\u5c0f\u7b97');\nmap.set(13276, '\u5c0f\u6cd5');\nmap.set(10583, '\u5c0f\u9e2d');\n\n/* \u67e5\u8be2\u64cd\u4f5c */\n// \u5411\u54c8\u5e0c\u8868\u8f93\u5165\u952e key \uff0c\u5f97\u5230\u503c value\nlet name = map.get(15937);\n\n/* \u5220\u9664\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u5220\u9664\u952e\u503c\u5bf9 (key, value)\nmap.delete(10583);\n
hash_map.ts/* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nconst map = new Map<number, string>();\n/* \u6dfb\u52a0\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9 (key, value)\nmap.set(12836, '\u5c0f\u54c8');\nmap.set(15937, '\u5c0f\u5570');\nmap.set(16750, '\u5c0f\u7b97');\nmap.set(13276, '\u5c0f\u6cd5');\nmap.set(10583, '\u5c0f\u9e2d');\nconsole.info('\\n\u6dfb\u52a0\u5b8c\u6210\u540e\uff0c\u54c8\u5e0c\u8868\u4e3a\\nKey -> Value');\nconsole.info(map);\n\n/* \u67e5\u8be2\u64cd\u4f5c */\n// \u5411\u54c8\u5e0c\u8868\u8f93\u5165\u952e key \uff0c\u5f97\u5230\u503c value\nlet name = map.get(15937);\nconsole.info('\\n\u8f93\u5165\u5b66\u53f7 15937 \uff0c\u67e5\u8be2\u5230\u59d3\u540d ' + name);\n\n/* \u5220\u9664\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u5220\u9664\u952e\u503c\u5bf9 (key, value)\nmap.delete(10583);\nconsole.info('\\n\u5220\u9664 10583 \u540e\uff0c\u54c8\u5e0c\u8868\u4e3a\\nKey -> Value');\nconsole.info(map);\n
hash_map.dart/* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nMap<int, String> map = {};\n\n/* \u6dfb\u52a0\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9 (key, value)\nmap[12836] = \"\u5c0f\u54c8\";\nmap[15937] = \"\u5c0f\u5570\";\nmap[16750] = \"\u5c0f\u7b97\";\nmap[13276] = \"\u5c0f\u6cd5\";\nmap[10583] = \"\u5c0f\u9e2d\";\n\n/* \u67e5\u8be2\u64cd\u4f5c */\n// \u5411\u54c8\u5e0c\u8868\u8f93\u5165\u952e key \uff0c\u5f97\u5230\u503c value\nString name = map[15937];\n\n/* \u5220\u9664\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u5220\u9664\u952e\u503c\u5bf9 (key, value)\nmap.remove(10583);\n
hash_map.rsuse std::collections::HashMap;\n\n/* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nlet mut map: HashMap<i32, String> = HashMap::new();\n\n/* \u6dfb\u52a0\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9 (key, value)\nmap.insert(12836, \"\u5c0f\u54c8\".to_string());\nmap.insert(15937, \"\u5c0f\u5570\".to_string());\nmap.insert(16750, \"\u5c0f\u7b97\".to_string());\nmap.insert(13279, \"\u5c0f\u6cd5\".to_string());\nmap.insert(10583, \"\u5c0f\u9e2d\".to_string());\n\n/* \u67e5\u8be2\u64cd\u4f5c */\n// \u5411\u54c8\u5e0c\u8868\u4e2d\u8f93\u5165\u952e key \uff0c\u5f97\u5230\u503c value\nlet _name: Option<&String> = map.get(&15937);\n\n/* \u5220\u9664\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u5220\u9664\u952e\u503c\u5bf9 (key, value)\nlet _removed_value: Option<String> = map.remove(&10583);\n
hash_map.c// C \u672a\u63d0\u4f9b\u5185\u7f6e\u54c8\u5e0c\u8868\n
hash_map.zig\n
\u54c8\u5e0c\u8868\u6709\u4e09\u79cd\u5e38\u7528\u7684\u904d\u5386\u65b9\u5f0f\uff1a\u904d\u5386\u952e\u503c\u5bf9\u3001\u904d\u5386\u952e\u548c\u904d\u5386\u503c\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig hash_map.py# \u904d\u5386\u54c8\u5e0c\u8868\n# \u904d\u5386\u952e\u503c\u5bf9 key->value\nfor key, value in hmap.items():\n print(key, \"->\", value)\n# \u5355\u72ec\u904d\u5386\u952e key\nfor key in hmap.keys():\n print(key)\n# \u5355\u72ec\u904d\u5386\u503c value\nfor value in hmap.values():\n print(value)\n
hash_map.cpp/* \u904d\u5386\u54c8\u5e0c\u8868 */\n// \u904d\u5386\u952e\u503c\u5bf9 key->value\nfor (auto kv: map) {\n cout << kv.first << \" -> \" << kv.second << endl;\n}\n// \u4f7f\u7528\u8fed\u4ee3\u5668\u904d\u5386 key->value\nfor (auto iter = map.begin(); iter != map.end(); iter++) {\n cout << iter->first << \"->\" << iter->second << endl;\n}\n
hash_map.java/* \u904d\u5386\u54c8\u5e0c\u8868 */\n// \u904d\u5386\u952e\u503c\u5bf9 key->value\nfor (Map.Entry <Integer, String> kv: map.entrySet()) {\n System.out.println(kv.getKey() + \" -> \" + kv.getValue());\n}\n// \u5355\u72ec\u904d\u5386\u952e key\nfor (int key: map.keySet()) {\n System.out.println(key);\n}\n// \u5355\u72ec\u904d\u5386\u503c value\nfor (String val: map.values()) {\n System.out.println(val);\n}\n
hash_map.cs/* \u904d\u5386\u54c8\u5e0c\u8868 */\n// \u904d\u5386\u952e\u503c\u5bf9 Key->Value\nforeach (var kv in map) {\n Console.WriteLine(kv.Key + \" -> \" + kv.Value);\n}\n// \u5355\u72ec\u904d\u5386\u952e key\nforeach (int key in map.Keys) {\n Console.WriteLine(key);\n}\n// \u5355\u72ec\u904d\u5386\u503c value\nforeach (string val in map.Values) {\n Console.WriteLine(val);\n}\n
hash_map_test.go/* \u904d\u5386\u54c8\u5e0c\u8868 */\n// \u904d\u5386\u952e\u503c\u5bf9 key->value\nfor key, value := range hmap {\n fmt.Println(key, \"->\", value)\n}\n// \u5355\u72ec\u904d\u5386\u952e key\nfor key := range hmap {\n fmt.Println(key)\n}\n// \u5355\u72ec\u904d\u5386\u503c value\nfor _, value := range hmap {\n fmt.Println(value)\n}\n
hash_map.swift/* \u904d\u5386\u54c8\u5e0c\u8868 */\n// \u904d\u5386\u952e\u503c\u5bf9 Key->Value\nfor (key, value) in map {\n print(\"\\(key) -> \\(value)\")\n}\n// \u5355\u72ec\u904d\u5386\u952e Key\nfor key in map.keys {\n print(key)\n}\n// \u5355\u72ec\u904d\u5386\u503c Value\nfor value in map.values {\n print(value)\n}\n
hash_map.js/* \u904d\u5386\u54c8\u5e0c\u8868 */\nconsole.info('\\n\u904d\u5386\u952e\u503c\u5bf9 Key->Value');\nfor (const [k, v] of map.entries()) {\n console.info(k + ' -> ' + v);\n}\nconsole.info('\\n\u5355\u72ec\u904d\u5386\u952e Key');\nfor (const k of map.keys()) {\n console.info(k);\n}\nconsole.info('\\n\u5355\u72ec\u904d\u5386\u503c Value');\nfor (const v of map.values()) {\n console.info(v);\n}\n
hash_map.ts/* \u904d\u5386\u54c8\u5e0c\u8868 */\nconsole.info('\\n\u904d\u5386\u952e\u503c\u5bf9 Key->Value');\nfor (const [k, v] of map.entries()) {\n console.info(k + ' -> ' + v);\n}\nconsole.info('\\n\u5355\u72ec\u904d\u5386\u952e Key');\nfor (const k of map.keys()) {\n console.info(k);\n}\nconsole.info('\\n\u5355\u72ec\u904d\u5386\u503c Value');\nfor (const v of map.values()) {\n console.info(v);\n}\n
hash_map.dart/* \u904d\u5386\u54c8\u5e0c\u8868 */\n// \u904d\u5386\u952e\u503c\u5bf9 Key->Value\nmap.forEach((key, value) {\n print('$key -> $value');\n});\n\n// \u5355\u72ec\u904d\u5386\u952e Key\nmap.keys.forEach((key) {\n print(key);\n});\n\n// \u5355\u72ec\u904d\u5386\u503c Value\nmap.values.forEach((value) {\n print(value);\n});\n
hash_map.rs/* \u904d\u5386\u54c8\u5e0c\u8868 */\n// \u904d\u5386\u952e\u503c\u5bf9 Key->Value\nfor (key, value) in &map {\n println!(\"{key} -> {value}\");\n}\n\n// \u5355\u72ec\u904d\u5386\u952e Key\nfor key in map.keys() {\n println!(\"{key}\"); \n}\n\n// \u5355\u72ec\u904d\u5386\u503c Value\nfor value in map.values() {\n println!(\"{value}\");\n}\n
hash_map.c// C \u672a\u63d0\u4f9b\u5185\u7f6e\u54c8\u5e0c\u8868\n
hash_map.zig\n
"},{"location":"chapter_hashing/hash_map/#612","title":"6.1.2 \u00a0 \u54c8\u5e0c\u8868\u7b80\u5355\u5b9e\u73b0","text":"\u6211\u4eec\u5148\u8003\u8651\u6700\u7b80\u5355\u7684\u60c5\u51b5\uff0c\u4ec5\u7528\u4e00\u4e2a\u6570\u7ec4\u6765\u5b9e\u73b0\u54c8\u5e0c\u8868\u3002\u5728\u54c8\u5e0c\u8868\u4e2d\uff0c\u6211\u4eec\u5c06\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u7a7a\u4f4d\u79f0\u4e3a\u300c\u6876 bucket\u300d\uff0c\u6bcf\u4e2a\u6876\u53ef\u5b58\u50a8\u4e00\u4e2a\u952e\u503c\u5bf9\u3002\u56e0\u6b64\uff0c\u67e5\u8be2\u64cd\u4f5c\u5c31\u662f\u627e\u5230 key
\u5bf9\u5e94\u7684\u6876\uff0c\u5e76\u5728\u6876\u4e2d\u83b7\u53d6 value
\u3002
\u90a3\u4e48\uff0c\u5982\u4f55\u57fa\u4e8e key
\u5b9a\u4f4d\u5bf9\u5e94\u7684\u6876\u5462\uff1f\u8fd9\u662f\u901a\u8fc7\u300c\u54c8\u5e0c\u51fd\u6570 hash function\u300d\u5b9e\u73b0\u7684\u3002\u54c8\u5e0c\u51fd\u6570\u7684\u4f5c\u7528\u662f\u5c06\u4e00\u4e2a\u8f83\u5927\u7684\u8f93\u5165\u7a7a\u95f4\u6620\u5c04\u5230\u4e00\u4e2a\u8f83\u5c0f\u7684\u8f93\u51fa\u7a7a\u95f4\u3002\u5728\u54c8\u5e0c\u8868\u4e2d\uff0c\u8f93\u5165\u7a7a\u95f4\u662f\u6240\u6709 key
\uff0c\u8f93\u51fa\u7a7a\u95f4\u662f\u6240\u6709\u6876\uff08\u6570\u7ec4\u7d22\u5f15\uff09\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u8f93\u5165\u4e00\u4e2a key
\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u54c8\u5e0c\u51fd\u6570\u5f97\u5230\u8be5 key
\u5bf9\u5e94\u7684\u952e\u503c\u5bf9\u5728\u6570\u7ec4\u4e2d\u7684\u5b58\u50a8\u4f4d\u7f6e\u3002
\u8f93\u5165\u4e00\u4e2a key
\uff0c\u54c8\u5e0c\u51fd\u6570\u7684\u8ba1\u7b97\u8fc7\u7a0b\u5206\u4e3a\u4ee5\u4e0b\u4e24\u6b65\u3002
hash()
\u8ba1\u7b97\u5f97\u5230\u54c8\u5e0c\u503c\u3002capacity
\u53d6\u6a21\uff0c\u4ece\u800c\u83b7\u53d6\u8be5 key
\u5bf9\u5e94\u7684\u6570\u7ec4\u7d22\u5f15 index
\u3002index = hash(key) % capacity\n
\u968f\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5229\u7528 index
\u5728\u54c8\u5e0c\u8868\u4e2d\u8bbf\u95ee\u5bf9\u5e94\u7684\u6876\uff0c\u4ece\u800c\u83b7\u53d6 value
\u3002
\u8bbe\u6570\u7ec4\u957f\u5ea6 capacity = 100
\u3001\u54c8\u5e0c\u7b97\u6cd5 hash(key) = key
\uff0c\u6613\u5f97\u54c8\u5e0c\u51fd\u6570\u4e3a key % 100
\u3002\u56fe 6-2 \u4ee5 key
\u5b66\u53f7\u548c value
\u59d3\u540d\u4e3a\u4f8b\uff0c\u5c55\u793a\u4e86\u54c8\u5e0c\u51fd\u6570\u7684\u5de5\u4f5c\u539f\u7406\u3002
\u56fe 6-2 \u00a0 \u54c8\u5e0c\u51fd\u6570\u5de5\u4f5c\u539f\u7406
\u4ee5\u4e0b\u4ee3\u7801\u5b9e\u73b0\u4e86\u4e00\u4e2a\u7b80\u5355\u54c8\u5e0c\u8868\u3002\u5176\u4e2d\uff0c\u6211\u4eec\u5c06 key
\u548c value
\u5c01\u88c5\u6210\u4e00\u4e2a\u7c7b Pair
\uff0c\u4ee5\u8868\u793a\u952e\u503c\u5bf9\u3002
class Pair:\n \"\"\"\u952e\u503c\u5bf9\"\"\"\n\n def __init__(self, key: int, val: str):\n self.key = key\n self.val = val\n\nclass ArrayHashMap:\n \"\"\"\u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868\"\"\"\n\n def __init__(self):\n \"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n # \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\n self.buckets: list[Pair | None] = [None] * 100\n\n def hash_func(self, key: int) -> int:\n \"\"\"\u54c8\u5e0c\u51fd\u6570\"\"\"\n index = key % 100\n return index\n\n def get(self, key: int) -> str:\n \"\"\"\u67e5\u8be2\u64cd\u4f5c\"\"\"\n index: int = self.hash_func(key)\n pair: Pair = self.buckets[index]\n if pair is None:\n return None\n return pair.val\n\n def put(self, key: int, val: str):\n \"\"\"\u6dfb\u52a0\u64cd\u4f5c\"\"\"\n pair = Pair(key, val)\n index: int = self.hash_func(key)\n self.buckets[index] = pair\n\n def remove(self, key: int):\n \"\"\"\u5220\u9664\u64cd\u4f5c\"\"\"\n index: int = self.hash_func(key)\n # \u7f6e\u4e3a None \uff0c\u4ee3\u8868\u5220\u9664\n self.buckets[index] = None\n\n def entry_set(self) -> list[Pair]:\n \"\"\"\u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9\"\"\"\n result: list[Pair] = []\n for pair in self.buckets:\n if pair is not None:\n result.append(pair)\n return result\n\n def key_set(self) -> list[int]:\n \"\"\"\u83b7\u53d6\u6240\u6709\u952e\"\"\"\n result = []\n for pair in self.buckets:\n if pair is not None:\n result.append(pair.key)\n return result\n\n def value_set(self) -> list[str]:\n \"\"\"\u83b7\u53d6\u6240\u6709\u503c\"\"\"\n result = []\n for pair in self.buckets:\n if pair is not None:\n result.append(pair.val)\n return result\n\n def print(self):\n \"\"\"\u6253\u5370\u54c8\u5e0c\u8868\"\"\"\n for pair in self.buckets:\n if pair is not None:\n print(pair.key, \"->\", pair.val)\n
array_hash_map.cpp/* \u952e\u503c\u5bf9 */\nstruct Pair {\n public:\n int key;\n string val;\n Pair(int key, string val) {\n this->key = key;\n this->val = val;\n }\n};\n\n/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\nclass ArrayHashMap {\n private:\n vector<Pair *> buckets;\n\n public:\n ArrayHashMap() {\n // \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\n buckets = vector<Pair *>(100);\n }\n\n ~ArrayHashMap() {\n // \u91ca\u653e\u5185\u5b58\n for (const auto &bucket : buckets) {\n delete bucket;\n }\n buckets.clear();\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n int hashFunc(int key) {\n int index = key % 100;\n return index;\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n string get(int key) {\n int index = hashFunc(key);\n Pair *pair = buckets[index];\n if (pair == nullptr)\n return \"\";\n return pair->val;\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n void put(int key, string val) {\n Pair *pair = new Pair(key, val);\n int index = hashFunc(key);\n buckets[index] = pair;\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n void remove(int key) {\n int index = hashFunc(key);\n // \u91ca\u653e\u5185\u5b58\u5e76\u7f6e\u4e3a nullptr\n delete buckets[index];\n buckets[index] = nullptr;\n }\n\n /* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\n vector<Pair *> pairSet() {\n vector<Pair *> pairSet;\n for (Pair *pair : buckets) {\n if (pair != nullptr) {\n pairSet.push_back(pair);\n }\n }\n return pairSet;\n }\n\n /* \u83b7\u53d6\u6240\u6709\u952e */\n vector<int> keySet() {\n vector<int> keySet;\n for (Pair *pair : buckets) {\n if (pair != nullptr) {\n keySet.push_back(pair->key);\n }\n }\n return keySet;\n }\n\n /* \u83b7\u53d6\u6240\u6709\u503c */\n vector<string> valueSet() {\n vector<string> valueSet;\n for (Pair *pair : buckets) {\n if (pair != nullptr) {\n valueSet.push_back(pair->val);\n }\n }\n return valueSet;\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n void print() {\n for (Pair *kv : pairSet()) {\n cout << kv->key << \" -> \" << kv->val << endl;\n }\n }\n};\n
array_hash_map.java/* \u952e\u503c\u5bf9 */\nclass Pair {\n public int key;\n public String val;\n\n public Pair(int key, String val) {\n this.key = key;\n this.val = val;\n }\n}\n\n/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\nclass ArrayHashMap {\n private List<Pair> buckets;\n\n public ArrayHashMap() {\n // \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\n buckets = new ArrayList<>();\n for (int i = 0; i < 100; i++) {\n buckets.add(null);\n }\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n private int hashFunc(int key) {\n int index = key % 100;\n return index;\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n public String get(int key) {\n int index = hashFunc(key);\n Pair pair = buckets.get(index);\n if (pair == null)\n return null;\n return pair.val;\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n public void put(int key, String val) {\n Pair pair = new Pair(key, val);\n int index = hashFunc(key);\n buckets.set(index, pair);\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n public void remove(int key) {\n int index = hashFunc(key);\n // \u7f6e\u4e3a null \uff0c\u4ee3\u8868\u5220\u9664\n buckets.set(index, null);\n }\n\n /* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\n public List<Pair> pairSet() {\n List<Pair> pairSet = new ArrayList<>();\n for (Pair pair : buckets) {\n if (pair != null)\n pairSet.add(pair);\n }\n return pairSet;\n }\n\n /* \u83b7\u53d6\u6240\u6709\u952e */\n public List<Integer> keySet() {\n List<Integer> keySet = new ArrayList<>();\n for (Pair pair : buckets) {\n if (pair != null)\n keySet.add(pair.key);\n }\n return keySet;\n }\n\n /* \u83b7\u53d6\u6240\u6709\u503c */\n public List<String> valueSet() {\n List<String> valueSet = new ArrayList<>();\n for (Pair pair : buckets) {\n if (pair != null)\n valueSet.add(pair.val);\n }\n return valueSet;\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n public void print() {\n for (Pair kv : pairSet()) {\n System.out.println(kv.key + \" -> \" + kv.val);\n }\n }\n}\n
array_hash_map.cs/* \u952e\u503c\u5bf9 int->string */\nclass Pair(int key, string val) {\n public int key = key;\n public string val = val;\n}\n\n/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\nclass ArrayHashMap {\n List<Pair?> buckets;\n public ArrayHashMap() {\n // \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\n buckets = [];\n for (int i = 0; i < 100; i++) {\n buckets.Add(null);\n }\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n int HashFunc(int key) {\n int index = key % 100;\n return index;\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n public string? Get(int key) {\n int index = HashFunc(key);\n Pair? pair = buckets[index];\n if (pair == null) return null;\n return pair.val;\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n public void Put(int key, string val) {\n Pair pair = new(key, val);\n int index = HashFunc(key);\n buckets[index] = pair;\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n public void Remove(int key) {\n int index = HashFunc(key);\n // \u7f6e\u4e3a null \uff0c\u4ee3\u8868\u5220\u9664\n buckets[index] = null;\n }\n\n /* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\n public List<Pair> PairSet() {\n List<Pair> pairSet = [];\n foreach (Pair? pair in buckets) {\n if (pair != null)\n pairSet.Add(pair);\n }\n return pairSet;\n }\n\n /* \u83b7\u53d6\u6240\u6709\u952e */\n public List<int> KeySet() {\n List<int> keySet = [];\n foreach (Pair? pair in buckets) {\n if (pair != null)\n keySet.Add(pair.key);\n }\n return keySet;\n }\n\n /* \u83b7\u53d6\u6240\u6709\u503c */\n public List<string> ValueSet() {\n List<string> valueSet = [];\n foreach (Pair? pair in buckets) {\n if (pair != null)\n valueSet.Add(pair.val);\n }\n return valueSet;\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n public void Print() {\n foreach (Pair kv in PairSet()) {\n Console.WriteLine(kv.key + \" -> \" + kv.val);\n }\n }\n}\n
array_hash_map.go/* \u952e\u503c\u5bf9 */\ntype pair struct {\n key int\n val string\n}\n\n/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\ntype arrayHashMap struct {\n buckets []*pair\n}\n\n/* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nfunc newArrayHashMap() *arrayHashMap {\n // \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\n buckets := make([]*pair, 100)\n return &arrayHashMap{buckets: buckets}\n}\n\n/* \u54c8\u5e0c\u51fd\u6570 */\nfunc (a *arrayHashMap) hashFunc(key int) int {\n index := key % 100\n return index\n}\n\n/* \u67e5\u8be2\u64cd\u4f5c */\nfunc (a *arrayHashMap) get(key int) string {\n index := a.hashFunc(key)\n pair := a.buckets[index]\n if pair == nil {\n return \"Not Found\"\n }\n return pair.val\n}\n\n/* \u6dfb\u52a0\u64cd\u4f5c */\nfunc (a *arrayHashMap) put(key int, val string) {\n pair := &pair{key: key, val: val}\n index := a.hashFunc(key)\n a.buckets[index] = pair\n}\n\n/* \u5220\u9664\u64cd\u4f5c */\nfunc (a *arrayHashMap) remove(key int) {\n index := a.hashFunc(key)\n // \u7f6e\u4e3a nil \uff0c\u4ee3\u8868\u5220\u9664\n a.buckets[index] = nil\n}\n\n/* \u83b7\u53d6\u6240\u6709\u952e\u5bf9 */\nfunc (a *arrayHashMap) pairSet() []*pair {\n var pairs []*pair\n for _, pair := range a.buckets {\n if pair != nil {\n pairs = append(pairs, pair)\n }\n }\n return pairs\n}\n\n/* \u83b7\u53d6\u6240\u6709\u952e */\nfunc (a *arrayHashMap) keySet() []int {\n var keys []int\n for _, pair := range a.buckets {\n if pair != nil {\n keys = append(keys, pair.key)\n }\n }\n return keys\n}\n\n/* \u83b7\u53d6\u6240\u6709\u503c */\nfunc (a *arrayHashMap) valueSet() []string {\n var values []string\n for _, pair := range a.buckets {\n if pair != nil {\n values = append(values, pair.val)\n }\n }\n return values\n}\n\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nfunc (a *arrayHashMap) print() {\n for _, pair := range a.buckets {\n if pair != nil {\n fmt.Println(pair.key, \"->\", pair.val)\n }\n }\n}\n
array_hash_map.swift/* \u952e\u503c\u5bf9 */\nclass Pair: Equatable {\n public var key: Int\n public var val: String\n\n public init(key: Int, val: String) {\n self.key = key\n self.val = val\n }\n\n public static func == (lhs: Pair, rhs: Pair) -> Bool {\n lhs.key == rhs.key && lhs.val == rhs.val\n }\n}\n\n/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\nclass ArrayHashMap {\n private var buckets: [Pair?] = []\n\n init() {\n // \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\n for _ in 0 ..< 100 {\n buckets.append(nil)\n }\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n private func hashFunc(key: Int) -> Int {\n let index = key % 100\n return index\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n func get(key: Int) -> String? {\n let index = hashFunc(key: key)\n let pair = buckets[index]\n return pair?.val\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n func put(key: Int, val: String) {\n let pair = Pair(key: key, val: val)\n let index = hashFunc(key: key)\n buckets[index] = pair\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n func remove(key: Int) {\n let index = hashFunc(key: key)\n // \u7f6e\u4e3a nil \uff0c\u4ee3\u8868\u5220\u9664\n buckets[index] = nil\n }\n\n /* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\n func pairSet() -> [Pair] {\n var pairSet: [Pair] = []\n for pair in buckets {\n if let pair = pair {\n pairSet.append(pair)\n }\n }\n return pairSet\n }\n\n /* \u83b7\u53d6\u6240\u6709\u952e */\n func keySet() -> [Int] {\n var keySet: [Int] = []\n for pair in buckets {\n if let pair = pair {\n keySet.append(pair.key)\n }\n }\n return keySet\n }\n\n /* \u83b7\u53d6\u6240\u6709\u503c */\n func valueSet() -> [String] {\n var valueSet: [String] = []\n for pair in buckets {\n if let pair = pair {\n valueSet.append(pair.val)\n }\n }\n return valueSet\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n func print() {\n for pair in pairSet() {\n Swift.print(\"\\(pair.key) -> \\(pair.val)\")\n }\n }\n}\n
array_hash_map.js/* \u952e\u503c\u5bf9 Number -> String */\nclass Pair {\n constructor(key, val) {\n this.key = key;\n this.val = val;\n }\n}\n\n/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\nclass ArrayHashMap {\n #buckets;\n constructor() {\n // \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\n this.#buckets = new Array(100).fill(null);\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n #hashFunc(key) {\n return key % 100;\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n get(key) {\n let index = this.#hashFunc(key);\n let pair = this.#buckets[index];\n if (pair === null) return null;\n return pair.val;\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n set(key, val) {\n let index = this.#hashFunc(key);\n this.#buckets[index] = new Pair(key, val);\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n delete(key) {\n let index = this.#hashFunc(key);\n // \u7f6e\u4e3a null \uff0c\u4ee3\u8868\u5220\u9664\n this.#buckets[index] = null;\n }\n\n /* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\n entries() {\n let arr = [];\n for (let i = 0; i < this.#buckets.length; i++) {\n if (this.#buckets[i]) {\n arr.push(this.#buckets[i]);\n }\n }\n return arr;\n }\n\n /* \u83b7\u53d6\u6240\u6709\u952e */\n keys() {\n let arr = [];\n for (let i = 0; i < this.#buckets.length; i++) {\n if (this.#buckets[i]) {\n arr.push(this.#buckets[i].key);\n }\n }\n return arr;\n }\n\n /* \u83b7\u53d6\u6240\u6709\u503c */\n values() {\n let arr = [];\n for (let i = 0; i < this.#buckets.length; i++) {\n if (this.#buckets[i]) {\n arr.push(this.#buckets[i].val);\n }\n }\n return arr;\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n print() {\n let pairSet = this.entries();\n for (const pair of pairSet) {\n console.info(`${pair.key} -> ${pair.val}`);\n }\n }\n}\n
array_hash_map.ts/* \u952e\u503c\u5bf9 Number -> String */\nclass Pair {\n public key: number;\n public val: string;\n\n constructor(key: number, val: string) {\n this.key = key;\n this.val = val;\n }\n}\n\n/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\nclass ArrayHashMap {\n private readonly buckets: (Pair | null)[];\n\n constructor() {\n // \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\n this.buckets = new Array(100).fill(null);\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n private hashFunc(key: number): number {\n return key % 100;\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n public get(key: number): string | null {\n let index = this.hashFunc(key);\n let pair = this.buckets[index];\n if (pair === null) return null;\n return pair.val;\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n public set(key: number, val: string) {\n let index = this.hashFunc(key);\n this.buckets[index] = new Pair(key, val);\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n public delete(key: number) {\n let index = this.hashFunc(key);\n // \u7f6e\u4e3a null \uff0c\u4ee3\u8868\u5220\u9664\n this.buckets[index] = null;\n }\n\n /* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\n public entries(): (Pair | null)[] {\n let arr: (Pair | null)[] = [];\n for (let i = 0; i < this.buckets.length; i++) {\n if (this.buckets[i]) {\n arr.push(this.buckets[i]);\n }\n }\n return arr;\n }\n\n /* \u83b7\u53d6\u6240\u6709\u952e */\n public keys(): (number | undefined)[] {\n let arr: (number | undefined)[] = [];\n for (let i = 0; i < this.buckets.length; i++) {\n if (this.buckets[i]) {\n arr.push(this.buckets[i].key);\n }\n }\n return arr;\n }\n\n /* \u83b7\u53d6\u6240\u6709\u503c */\n public values(): (string | undefined)[] {\n let arr: (string | undefined)[] = [];\n for (let i = 0; i < this.buckets.length; i++) {\n if (this.buckets[i]) {\n arr.push(this.buckets[i].val);\n }\n }\n return arr;\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n public print() {\n let pairSet = this.entries();\n for (const pair of pairSet) {\n console.info(`${pair.key} -> ${pair.val}`);\n }\n }\n}\n
array_hash_map.dart/* \u952e\u503c\u5bf9 */\nclass Pair {\n int key;\n String val;\n Pair(this.key, this.val);\n}\n\n/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\nclass ArrayHashMap {\n late List<Pair?> _buckets;\n\n ArrayHashMap() {\n // \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\n _buckets = List.filled(100, null);\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n int _hashFunc(int key) {\n final int index = key % 100;\n return index;\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n String? get(int key) {\n final int index = _hashFunc(key);\n final Pair? pair = _buckets[index];\n if (pair == null) {\n return null;\n }\n return pair.val;\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n void put(int key, String val) {\n final Pair pair = Pair(key, val);\n final int index = _hashFunc(key);\n _buckets[index] = pair;\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n void remove(int key) {\n final int index = _hashFunc(key);\n _buckets[index] = null;\n }\n\n /* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\n List<Pair> pairSet() {\n List<Pair> pairSet = [];\n for (final Pair? pair in _buckets) {\n if (pair != null) {\n pairSet.add(pair);\n }\n }\n return pairSet;\n }\n\n /* \u83b7\u53d6\u6240\u6709\u952e */\n List<int> keySet() {\n List<int> keySet = [];\n for (final Pair? pair in _buckets) {\n if (pair != null) {\n keySet.add(pair.key);\n }\n }\n return keySet;\n }\n\n /* \u83b7\u53d6\u6240\u6709\u503c */\n List<String> values() {\n List<String> valueSet = [];\n for (final Pair? pair in _buckets) {\n if (pair != null) {\n valueSet.add(pair.val);\n }\n }\n return valueSet;\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n void printHashMap() {\n for (final Pair kv in pairSet()) {\n print(\"${kv.key} -> ${kv.val}\");\n }\n }\n}\n
array_hash_map.rs/* \u952e\u503c\u5bf9 */\n#[derive(Debug, Clone, PartialEq)]\npub struct Pair {\n pub key: i32,\n pub val: String,\n}\n\n/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\npub struct ArrayHashMap {\n buckets: Vec<Option<Pair>>\n}\n\nimpl ArrayHashMap {\n pub fn new() -> ArrayHashMap {\n // \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\n Self { buckets: vec![None; 100] }\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n fn hash_func(&self, key: i32) -> usize {\n key as usize % 100\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n pub fn get(&self, key: i32) -> Option<&String> {\n let index = self.hash_func(key);\n self.buckets[index].as_ref().map(|pair| &pair.val)\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n pub fn put(&mut self, key: i32, val: &str) {\n let index = self.hash_func(key);\n self.buckets[index] = Some(Pair {\n key,\n val: val.to_string(),\n });\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n pub fn remove(&mut self, key: i32) {\n let index = self.hash_func(key);\n // \u7f6e\u4e3a None \uff0c\u4ee3\u8868\u5220\u9664\n self.buckets[index] = None;\n }\n\n /* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\n pub fn entry_set(&self) -> Vec<&Pair> {\n self.buckets.iter().filter_map(|pair| pair.as_ref()).collect()\n }\n\n /* \u83b7\u53d6\u6240\u6709\u952e */\n pub fn key_set(&self) -> Vec<&i32> {\n self.buckets.iter().filter_map(|pair| pair.as_ref().map(|pair| &pair.key)).collect()\n }\n\n /* \u83b7\u53d6\u6240\u6709\u503c */\n pub fn value_set(&self) -> Vec<&String> {\n self.buckets.iter().filter_map(|pair| pair.as_ref().map(|pair| &pair.val)).collect()\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n pub fn print(&self) {\n for pair in self.entry_set() {\n println!(\"{} -> {}\", pair.key, pair.val);\n }\n }\n}\n
array_hash_map.c/* \u952e\u503c\u5bf9 int->string */\ntypedef struct {\n int key;\n char *val;\n} Pair;\n\n/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\ntypedef struct {\n Pair *buckets[HASHTABLE_CAPACITY];\n} ArrayHashMap;\n\n/* \u6784\u9020\u51fd\u6570 */\nArrayHashMap *newArrayHashMap() {\n ArrayHashMap *hmap = malloc(sizeof(ArrayHashMap));\n return hmap;\n}\n\n/* \u6790\u6784\u51fd\u6570 */\nvoid delArrayHashMap(ArrayHashMap *hmap) {\n for (int i = 0; i < HASHTABLE_CAPACITY; i++) {\n if (hmap->buckets[i] != NULL) {\n free(hmap->buckets[i]->val);\n free(hmap->buckets[i]);\n }\n }\n free(hmap);\n}\n\n/* \u6dfb\u52a0\u64cd\u4f5c */\nvoid put(ArrayHashMap *hmap, const int key, const char *val) {\n Pair *Pair = malloc(sizeof(Pair));\n Pair->key = key;\n Pair->val = malloc(strlen(val) + 1);\n strcpy(Pair->val, val);\n\n int index = hashFunc(key);\n hmap->buckets[index] = Pair;\n}\n\n/* \u5220\u9664\u64cd\u4f5c */\nvoid removeItem(ArrayHashMap *hmap, const int key) {\n int index = hashFunc(key);\n free(hmap->buckets[index]->val);\n free(hmap->buckets[index]);\n hmap->buckets[index] = NULL;\n}\n\n/* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\nvoid pairSet(ArrayHashMap *hmap, MapSet *set) {\n Pair *entries;\n int i = 0, index = 0;\n int total = 0;\n /* \u7edf\u8ba1\u6709\u6548\u952e\u503c\u5bf9\u6570\u91cf */\n for (i = 0; i < HASHTABLE_CAPACITY; i++) {\n if (hmap->buckets[i] != NULL) {\n total++;\n }\n }\n entries = malloc(sizeof(Pair) * total);\n for (i = 0; i < HASHTABLE_CAPACITY; i++) {\n if (hmap->buckets[i] != NULL) {\n entries[index].key = hmap->buckets[i]->key;\n entries[index].val = malloc(strlen(hmap->buckets[i]->val) + 1);\n strcpy(entries[index].val, hmap->buckets[i]->val);\n index++;\n }\n }\n set->set = entries;\n set->len = total;\n}\n\n/* \u83b7\u53d6\u6240\u6709\u952e */\nvoid keySet(ArrayHashMap *hmap, MapSet *set) {\n int *keys;\n int i = 0, index = 0;\n int total = 0;\n /* \u7edf\u8ba1\u6709\u6548\u952e\u503c\u5bf9\u6570\u91cf */\n for (i = 0; i < HASHTABLE_CAPACITY; i++) {\n if (hmap->buckets[i] != NULL) {\n total++;\n }\n }\n keys = malloc(total * sizeof(int));\n for (i = 0; i < HASHTABLE_CAPACITY; i++) {\n if (hmap->buckets[i] != NULL) {\n keys[index] = hmap->buckets[i]->key;\n index++;\n }\n }\n set->set = keys;\n set->len = total;\n}\n\n/* \u83b7\u53d6\u6240\u6709\u503c */\nvoid valueSet(ArrayHashMap *hmap, MapSet *set) {\n char **vals;\n int i = 0, index = 0;\n int total = 0;\n /* \u7edf\u8ba1\u6709\u6548\u952e\u503c\u5bf9\u6570\u91cf */\n for (i = 0; i < HASHTABLE_CAPACITY; i++) {\n if (hmap->buckets[i] != NULL) {\n total++;\n }\n }\n vals = malloc(total * sizeof(char *));\n for (i = 0; i < HASHTABLE_CAPACITY; i++) {\n if (hmap->buckets[i] != NULL) {\n vals[index] = hmap->buckets[i]->val;\n index++;\n }\n }\n set->set = vals;\n set->len = total;\n}\n\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nvoid print(ArrayHashMap *hmap) {\n int i;\n MapSet set;\n pairSet(hmap, &set);\n Pair *entries = (Pair *)set.set;\n for (i = 0; i < set.len; i++) {\n printf(\"%d -> %s\\n\", entries[i].key, entries[i].val);\n }\n free(set.set);\n}\n
array_hash_map.zig// \u952e\u503c\u5bf9\nconst Pair = struct {\n key: usize = undefined,\n val: []const u8 = undefined,\n\n pub fn init(key: usize, val: []const u8) Pair {\n return Pair {\n .key = key,\n .val = val,\n };\n }\n};\n\n// \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868\nfn ArrayHashMap(comptime T: type) type {\n return struct {\n bucket: ?std.ArrayList(?T) = null,\n mem_allocator: std.mem.Allocator = undefined,\n\n const Self = @This();\n\n // \u6784\u9020\u51fd\u6570\n pub fn init(self: *Self, allocator: std.mem.Allocator) !void {\n self.mem_allocator = allocator;\n // \u521d\u59cb\u5316\u4e00\u4e2a\u957f\u5ea6\u4e3a 100 \u7684\u6876\uff08\u6570\u7ec4\uff09\n self.bucket = std.ArrayList(?T).init(self.mem_allocator);\n var i: i32 = 0;\n while (i < 100) : (i += 1) {\n try self.bucket.?.append(null);\n }\n }\n\n // \u6790\u6784\u51fd\u6570\n pub fn deinit(self: *Self) void {\n if (self.bucket != null) self.bucket.?.deinit();\n }\n\n // \u54c8\u5e0c\u51fd\u6570\n fn hashFunc(key: usize) usize {\n var index = key % 100;\n return index;\n }\n\n // \u67e5\u8be2\u64cd\u4f5c\n pub fn get(self: *Self, key: usize) []const u8 {\n var index = hashFunc(key);\n var pair = self.bucket.?.items[index];\n return pair.?.val;\n }\n\n // \u6dfb\u52a0\u64cd\u4f5c\n pub fn put(self: *Self, key: usize, val: []const u8) !void {\n var pair = Pair.init(key, val);\n var index = hashFunc(key);\n self.bucket.?.items[index] = pair;\n }\n\n // \u5220\u9664\u64cd\u4f5c\n pub fn remove(self: *Self, key: usize) !void {\n var index = hashFunc(key);\n // \u7f6e\u4e3a null \uff0c\u4ee3\u8868\u5220\u9664\n self.bucket.?.items[index] = null;\n } \n\n // \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9\n pub fn pairSet(self: *Self) !std.ArrayList(T) {\n var entry_set = std.ArrayList(T).init(self.mem_allocator);\n for (self.bucket.?.items) |item| {\n if (item == null) continue;\n try entry_set.append(item.?);\n }\n return entry_set;\n } \n\n // \u83b7\u53d6\u6240\u6709\u952e\n pub fn keySet(self: *Self) !std.ArrayList(usize) {\n var key_set = std.ArrayList(usize).init(self.mem_allocator);\n for (self.bucket.?.items) |item| {\n if (item == null) continue;\n try key_set.append(item.?.key);\n }\n return key_set;\n } \n\n // \u83b7\u53d6\u6240\u6709\u503c\n pub fn valueSet(self: *Self) !std.ArrayList([]const u8) {\n var value_set = std.ArrayList([]const u8).init(self.mem_allocator);\n for (self.bucket.?.items) |item| {\n if (item == null) continue;\n try value_set.append(item.?.val);\n }\n return value_set;\n }\n\n // \u6253\u5370\u54c8\u5e0c\u8868\n pub fn print(self: *Self) !void {\n var entry_set = try self.pairSet();\n defer entry_set.deinit();\n for (entry_set.items) |item| {\n std.debug.print(\"{} -> {s}\\n\", .{item.key, item.val});\n }\n }\n };\n}\n
"},{"location":"chapter_hashing/hash_map/#613","title":"6.1.3 \u00a0 \u54c8\u5e0c\u51b2\u7a81\u4e0e\u6269\u5bb9","text":"\u4ece\u672c\u8d28\u4e0a\u770b\uff0c\u54c8\u5e0c\u51fd\u6570\u7684\u4f5c\u7528\u662f\u5c06\u6240\u6709 key
\u6784\u6210\u7684\u8f93\u5165\u7a7a\u95f4\u6620\u5c04\u5230\u6570\u7ec4\u6240\u6709\u7d22\u5f15\u6784\u6210\u7684\u8f93\u51fa\u7a7a\u95f4\uff0c\u800c\u8f93\u5165\u7a7a\u95f4\u5f80\u5f80\u8fdc\u5927\u4e8e\u8f93\u51fa\u7a7a\u95f4\u3002\u56e0\u6b64\uff0c\u7406\u8bba\u4e0a\u4e00\u5b9a\u5b58\u5728\u201c\u591a\u4e2a\u8f93\u5165\u5bf9\u5e94\u76f8\u540c\u8f93\u51fa\u201d\u7684\u60c5\u51b5\u3002
\u5bf9\u4e8e\u4e0a\u8ff0\u793a\u4f8b\u4e2d\u7684\u54c8\u5e0c\u51fd\u6570\uff0c\u5f53\u8f93\u5165\u7684 key
\u540e\u4e24\u4f4d\u76f8\u540c\u65f6\uff0c\u54c8\u5e0c\u51fd\u6570\u7684\u8f93\u51fa\u7ed3\u679c\u4e5f\u76f8\u540c\u3002\u4f8b\u5982\uff0c\u67e5\u8be2\u5b66\u53f7\u4e3a 12836 \u548c 20336 \u7684\u4e24\u4e2a\u5b66\u751f\u65f6\uff0c\u6211\u4eec\u5f97\u5230\uff1a
12836 % 100 = 36\n20336 % 100 = 36\n
\u5982\u56fe 6-3 \u6240\u793a\uff0c\u4e24\u4e2a\u5b66\u53f7\u6307\u5411\u4e86\u540c\u4e00\u4e2a\u59d3\u540d\uff0c\u8fd9\u663e\u7136\u662f\u4e0d\u5bf9\u7684\u3002\u6211\u4eec\u5c06\u8fd9\u79cd\u591a\u4e2a\u8f93\u5165\u5bf9\u5e94\u540c\u4e00\u8f93\u51fa\u7684\u60c5\u51b5\u79f0\u4e3a\u300c\u54c8\u5e0c\u51b2\u7a81 hash collision\u300d\u3002
\u56fe 6-3 \u00a0 \u54c8\u5e0c\u51b2\u7a81\u793a\u4f8b
\u5bb9\u6613\u60f3\u5230\uff0c\u54c8\u5e0c\u8868\u5bb9\u91cf \\(n\\) \u8d8a\u5927\uff0c\u591a\u4e2a key
\u88ab\u5206\u914d\u5230\u540c\u4e00\u4e2a\u6876\u4e2d\u7684\u6982\u7387\u5c31\u8d8a\u4f4e\uff0c\u51b2\u7a81\u5c31\u8d8a\u5c11\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u6269\u5bb9\u54c8\u5e0c\u8868\u6765\u51cf\u5c11\u54c8\u5e0c\u51b2\u7a81\u3002
\u5982\u56fe 6-4 \u6240\u793a\uff0c\u6269\u5bb9\u524d\u952e\u503c\u5bf9 (136, A)
\u548c (236, D)
\u53d1\u751f\u51b2\u7a81\uff0c\u6269\u5bb9\u540e\u51b2\u7a81\u6d88\u5931\u3002
\u56fe 6-4 \u00a0 \u54c8\u5e0c\u8868\u6269\u5bb9
\u7c7b\u4f3c\u4e8e\u6570\u7ec4\u6269\u5bb9\uff0c\u54c8\u5e0c\u8868\u6269\u5bb9\u9700\u5c06\u6240\u6709\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u8fc1\u79fb\u81f3\u65b0\u54c8\u5e0c\u8868\uff0c\u975e\u5e38\u8017\u65f6\uff1b\u5e76\u4e14\u7531\u4e8e\u54c8\u5e0c\u8868\u5bb9\u91cf capacity
\u6539\u53d8\uff0c\u6211\u4eec\u9700\u8981\u901a\u8fc7\u54c8\u5e0c\u51fd\u6570\u6765\u91cd\u65b0\u8ba1\u7b97\u6240\u6709\u952e\u503c\u5bf9\u7684\u5b58\u50a8\u4f4d\u7f6e\uff0c\u8fd9\u8fdb\u4e00\u6b65\u63d0\u9ad8\u4e86\u6269\u5bb9\u8fc7\u7a0b\u7684\u8ba1\u7b97\u5f00\u9500\u3002\u4e3a\u6b64\uff0c\u7f16\u7a0b\u8bed\u8a00\u901a\u5e38\u4f1a\u9884\u7559\u8db3\u591f\u5927\u7684\u54c8\u5e0c\u8868\u5bb9\u91cf\uff0c\u9632\u6b62\u9891\u7e41\u6269\u5bb9\u3002
\u300c\u8d1f\u8f7d\u56e0\u5b50 load factor\u300d\u662f\u54c8\u5e0c\u8868\u7684\u4e00\u4e2a\u91cd\u8981\u6982\u5ff5\uff0c\u5176\u5b9a\u4e49\u4e3a\u54c8\u5e0c\u8868\u7684\u5143\u7d20\u6570\u91cf\u9664\u4ee5\u6876\u6570\u91cf\uff0c\u7528\u4e8e\u8861\u91cf\u54c8\u5e0c\u51b2\u7a81\u7684\u4e25\u91cd\u7a0b\u5ea6\uff0c\u4e5f\u5e38\u4f5c\u4e3a\u54c8\u5e0c\u8868\u6269\u5bb9\u7684\u89e6\u53d1\u6761\u4ef6\u3002\u4f8b\u5982\u5728 Java \u4e2d\uff0c\u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7 \\(0.75\\) \u65f6\uff0c\u7cfb\u7edf\u4f1a\u5c06\u54c8\u5e0c\u8868\u6269\u5bb9\u81f3\u539f\u5148\u7684 \\(2\\) \u500d\u3002
"},{"location":"chapter_hashing/summary/","title":"6.4 \u00a0 \u5c0f\u7ed3","text":""},{"location":"chapter_hashing/summary/#1","title":"1. \u00a0 \u91cd\u70b9\u56de\u987e","text":"key
\uff0c\u54c8\u5e0c\u8868\u80fd\u591f\u5728 \\(O(1)\\) \u65f6\u95f4\u5185\u67e5\u8be2\u5230 value
\uff0c\u6548\u7387\u975e\u5e38\u9ad8\u3002key
\u6620\u5c04\u4e3a\u6570\u7ec4\u7d22\u5f15\uff0c\u4ece\u800c\u8bbf\u95ee\u5bf9\u5e94\u6876\u5e76\u83b7\u53d6 value
\u3002key
\u53ef\u80fd\u5728\u7ecf\u8fc7\u54c8\u5e0c\u51fd\u6570\u540e\u5f97\u5230\u76f8\u540c\u7684\u6570\u7ec4\u7d22\u5f15\uff0c\u5bfc\u81f4\u67e5\u8be2\u7ed3\u679c\u51fa\u9519\uff0c\u8fd9\u79cd\u73b0\u8c61\u88ab\u79f0\u4e3a\u54c8\u5e0c\u51b2\u7a81\u3002HashMap
\u4f7f\u7528\u94fe\u5f0f\u5730\u5740\uff0c\u800c Python \u7684 Dict
\u91c7\u7528\u5f00\u653e\u5bfb\u5740\u3002\u54c8\u5e0c\u8868\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u5728\u4ec0\u4e48\u60c5\u51b5\u4e0b\u662f \\(O(n)\\) \uff1f
\u5f53\u54c8\u5e0c\u51b2\u7a81\u6bd4\u8f83\u4e25\u91cd\u65f6\uff0c\u54c8\u5e0c\u8868\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4f1a\u9000\u5316\u81f3 \\(O(n)\\) \u3002\u5f53\u54c8\u5e0c\u51fd\u6570\u8bbe\u8ba1\u5f97\u6bd4\u8f83\u597d\u3001\u5bb9\u91cf\u8bbe\u7f6e\u6bd4\u8f83\u5408\u7406\u3001\u51b2\u7a81\u6bd4\u8f83\u5e73\u5747\u65f6\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u662f \\(O(1)\\) \u3002\u6211\u4eec\u4f7f\u7528\u7f16\u7a0b\u8bed\u8a00\u5185\u7f6e\u7684\u54c8\u5e0c\u8868\u65f6\uff0c\u901a\u5e38\u8ba4\u4e3a\u65f6\u95f4\u590d\u6742\u5ea6\u662f \\(O(1)\\) \u3002
\u4e3a\u4ec0\u4e48\u4e0d\u4f7f\u7528\u54c8\u5e0c\u51fd\u6570 \\(f(x) = x\\) \u5462\uff1f\u8fd9\u6837\u5c31\u4e0d\u4f1a\u6709\u51b2\u7a81\u4e86
\u5728 \\(f(x) = x\\) \u54c8\u5e0c\u51fd\u6570\u4e0b\uff0c\u6bcf\u4e2a\u5143\u7d20\u5bf9\u5e94\u552f\u4e00\u7684\u6876\u7d22\u5f15\uff0c\u8fd9\u4e0e\u6570\u7ec4\u7b49\u4ef7\u3002\u7136\u800c\uff0c\u8f93\u5165\u7a7a\u95f4\u901a\u5e38\u8fdc\u5927\u4e8e\u8f93\u51fa\u7a7a\u95f4\uff08\u6570\u7ec4\u957f\u5ea6\uff09\uff0c\u56e0\u6b64\u54c8\u5e0c\u51fd\u6570\u7684\u6700\u540e\u4e00\u6b65\u5f80\u5f80\u662f\u5bf9\u6570\u7ec4\u957f\u5ea6\u53d6\u6a21\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u54c8\u5e0c\u8868\u7684\u76ee\u6807\u662f\u5c06\u4e00\u4e2a\u8f83\u5927\u7684\u72b6\u6001\u7a7a\u95f4\u6620\u5c04\u5230\u4e00\u4e2a\u8f83\u5c0f\u7684\u7a7a\u95f4\uff0c\u5e76\u63d0\u4f9b \\(O(1)\\) \u7684\u67e5\u8be2\u6548\u7387\u3002
\u54c8\u5e0c\u8868\u5e95\u5c42\u5b9e\u73b0\u662f\u6570\u7ec4\u3001\u94fe\u8868\u3001\u4e8c\u53c9\u6811\uff0c\u4f46\u4e3a\u4ec0\u4e48\u6548\u7387\u53ef\u4ee5\u6bd4\u5b83\u4eec\u66f4\u9ad8\u5462\uff1f
\u9996\u5148\uff0c\u54c8\u5e0c\u8868\u7684\u65f6\u95f4\u6548\u7387\u53d8\u9ad8\uff0c\u4f46\u7a7a\u95f4\u6548\u7387\u53d8\u4f4e\u4e86\u3002\u54c8\u5e0c\u8868\u6709\u76f8\u5f53\u4e00\u90e8\u5206\u5185\u5b58\u672a\u4f7f\u7528\u3002
\u5176\u6b21\uff0c\u53ea\u662f\u5728\u7279\u5b9a\u4f7f\u7528\u573a\u666f\u4e0b\u65f6\u95f4\u6548\u7387\u53d8\u9ad8\u4e86\u3002\u5982\u679c\u4e00\u4e2a\u529f\u80fd\u80fd\u591f\u5728\u76f8\u540c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e0b\u4f7f\u7528\u6570\u7ec4\u6216\u94fe\u8868\u5b9e\u73b0\uff0c\u90a3\u4e48\u901a\u5e38\u6bd4\u54c8\u5e0c\u8868\u66f4\u5feb\u3002\u8fd9\u662f\u56e0\u4e3a\u54c8\u5e0c\u51fd\u6570\u8ba1\u7b97\u9700\u8981\u5f00\u9500\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u7684\u5e38\u6570\u9879\u66f4\u5927\u3002
\u6700\u540e\uff0c\u54c8\u5e0c\u8868\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u53ef\u80fd\u53d1\u751f\u52a3\u5316\u3002\u4f8b\u5982\u5728\u94fe\u5f0f\u5730\u5740\u4e2d\uff0c\u6211\u4eec\u91c7\u53d6\u5728\u94fe\u8868\u6216\u7ea2\u9ed1\u6811\u4e2d\u6267\u884c\u67e5\u627e\u64cd\u4f5c\uff0c\u4ecd\u7136\u6709\u9000\u5316\u81f3 \\(O(n)\\) \u65f6\u95f4\u7684\u98ce\u9669\u3002
\u591a\u6b21\u54c8\u5e0c\u6709\u4e0d\u80fd\u76f4\u63a5\u5220\u9664\u5143\u7d20\u7684\u7f3a\u9677\u5417\uff1f\u6807\u8bb0\u4e3a\u5df2\u5220\u9664\u7684\u7a7a\u95f4\u8fd8\u80fd\u518d\u6b21\u4f7f\u7528\u5417\uff1f
\u591a\u6b21\u54c8\u5e0c\u662f\u5f00\u653e\u5bfb\u5740\u7684\u4e00\u79cd\uff0c\u5f00\u653e\u5bfb\u5740\u6cd5\u90fd\u6709\u4e0d\u80fd\u76f4\u63a5\u5220\u9664\u5143\u7d20\u7684\u7f3a\u9677\uff0c\u9700\u8981\u901a\u8fc7\u6807\u8bb0\u5220\u9664\u3002\u6807\u8bb0\u4e3a\u5df2\u5220\u9664\u7684\u7a7a\u95f4\u53ef\u4ee5\u518d\u6b21\u4f7f\u7528\u3002\u5f53\u5c06\u65b0\u5143\u7d20\u63d2\u5165\u54c8\u5e0c\u8868\uff0c\u5e76\u4e14\u901a\u8fc7\u54c8\u5e0c\u51fd\u6570\u627e\u5230\u6807\u8bb0\u4e3a\u5df2\u5220\u9664\u7684\u4f4d\u7f6e\u65f6\uff0c\u8be5\u4f4d\u7f6e\u53ef\u4ee5\u88ab\u65b0\u5143\u7d20\u4f7f\u7528\u3002\u8fd9\u6837\u505a\u65e2\u80fd\u4fdd\u6301\u54c8\u5e0c\u8868\u7684\u63a2\u6d4b\u5e8f\u5217\u4e0d\u53d8\uff0c\u53c8\u80fd\u4fdd\u8bc1\u54c8\u5e0c\u8868\u7684\u7a7a\u95f4\u4f7f\u7528\u7387\u3002
\u4e3a\u4ec0\u4e48\u5728\u7ebf\u6027\u63a2\u6d4b\u4e2d\uff0c\u67e5\u627e\u5143\u7d20\u7684\u65f6\u5019\u4f1a\u51fa\u73b0\u54c8\u5e0c\u51b2\u7a81\u5462\uff1f
\u67e5\u627e\u7684\u65f6\u5019\u901a\u8fc7\u54c8\u5e0c\u51fd\u6570\u627e\u5230\u5bf9\u5e94\u7684\u6876\u548c\u952e\u503c\u5bf9\uff0c\u53d1\u73b0 key
\u4e0d\u5339\u914d\uff0c\u8fd9\u5c31\u4ee3\u8868\u6709\u54c8\u5e0c\u51b2\u7a81\u3002\u56e0\u6b64\uff0c\u7ebf\u6027\u63a2\u6d4b\u6cd5\u4f1a\u6839\u636e\u9884\u5148\u8bbe\u5b9a\u7684\u6b65\u957f\u4f9d\u6b21\u5411\u4e0b\u67e5\u627e\uff0c\u76f4\u81f3\u627e\u5230\u6b63\u786e\u7684\u952e\u503c\u5bf9\u6216\u65e0\u6cd5\u627e\u5230\u8df3\u51fa\u4e3a\u6b62\u3002
\u4e3a\u4ec0\u4e48\u54c8\u5e0c\u8868\u6269\u5bb9\u80fd\u591f\u7f13\u89e3\u54c8\u5e0c\u51b2\u7a81\uff1f
\u54c8\u5e0c\u51fd\u6570\u7684\u6700\u540e\u4e00\u6b65\u5f80\u5f80\u662f\u5bf9\u6570\u7ec4\u957f\u5ea6 \\(n\\) \u53d6\u4f59\uff0c\u8ba9\u8f93\u51fa\u503c\u843d\u5728\u6570\u7ec4\u7d22\u5f15\u8303\u56f4\u5185\uff1b\u5728\u6269\u5bb9\u540e\uff0c\u6570\u7ec4\u957f\u5ea6 \\(n\\) \u53d1\u751f\u53d8\u5316\uff0c\u800c key
\u5bf9\u5e94\u7684\u7d22\u5f15\u4e5f\u53ef\u80fd\u53d1\u751f\u53d8\u5316\u3002\u539f\u5148\u843d\u5728\u540c\u4e00\u4e2a\u6876\u7684\u591a\u4e2a key
\uff0c\u5728\u6269\u5bb9\u540e\u53ef\u80fd\u4f1a\u88ab\u5206\u914d\u5230\u591a\u4e2a\u6876\u4e2d\uff0c\u4ece\u800c\u5b9e\u73b0\u54c8\u5e0c\u51b2\u7a81\u7684\u7f13\u89e3\u3002
Abstract
\u5806\u5c31\u50cf\u662f\u5c71\u5cb3\u5cf0\u5ce6\uff0c\u5c42\u53e0\u8d77\u4f0f\u3001\u5f62\u6001\u5404\u5f02\u3002
\u5ea7\u5ea7\u5c71\u5cf0\u9ad8\u4f4e\u9519\u843d\uff0c\u800c\u6700\u9ad8\u7684\u5c71\u5cf0\u603b\u662f\u6700\u5148\u6620\u5165\u773c\u5e18\u3002
"},{"location":"chapter_heap/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u5e0c\u671b\u4f7f\u7528\u4e00\u4e2a\u5217\u8868\u7684\u6240\u6709\u5143\u7d20\u6765\u6784\u5efa\u4e00\u4e2a\u5806\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u88ab\u79f0\u4e3a\u201c\u5efa\u5806\u64cd\u4f5c\u201d\u3002
"},{"location":"chapter_heap/build_heap/#821","title":"8.2.1 \u00a0 \u501f\u52a9\u5165\u5806\u64cd\u4f5c\u5b9e\u73b0","text":"\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e00\u4e2a\u7a7a\u5806\uff0c\u7136\u540e\u904d\u5386\u5217\u8868\uff0c\u4f9d\u6b21\u5bf9\u6bcf\u4e2a\u5143\u7d20\u6267\u884c\u201c\u5165\u5806\u64cd\u4f5c\u201d\uff0c\u5373\u5148\u5c06\u5143\u7d20\u6dfb\u52a0\u81f3\u5806\u7684\u5c3e\u90e8\uff0c\u518d\u5bf9\u8be5\u5143\u7d20\u6267\u884c\u201c\u4ece\u5e95\u81f3\u9876\u201d\u5806\u5316\u3002
\u6bcf\u5f53\u4e00\u4e2a\u5143\u7d20\u5165\u5806\uff0c\u5806\u7684\u957f\u5ea6\u5c31\u52a0\u4e00\u3002\u7531\u4e8e\u8282\u70b9\u662f\u4ece\u9876\u5230\u5e95\u4f9d\u6b21\u88ab\u6dfb\u52a0\u8fdb\u4e8c\u53c9\u6811\u7684\uff0c\u56e0\u6b64\u5806\u662f\u201c\u81ea\u4e0a\u800c\u4e0b\u201d\u6784\u5efa\u7684\u3002
\u8bbe\u5143\u7d20\u6570\u91cf\u4e3a \\(n\\) \uff0c\u6bcf\u4e2a\u5143\u7d20\u7684\u5165\u5806\u64cd\u4f5c\u4f7f\u7528 \\(O(\\log{n})\\) \u65f6\u95f4\uff0c\u56e0\u6b64\u8be5\u5efa\u5806\u65b9\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n \\log n)\\) \u3002
"},{"location":"chapter_heap/build_heap/#822","title":"8.2.2 \u00a0 \u901a\u8fc7\u904d\u5386\u5806\u5316\u5b9e\u73b0","text":"\u5b9e\u9645\u4e0a\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9e\u73b0\u4e00\u79cd\u66f4\u4e3a\u9ad8\u6548\u7684\u5efa\u5806\u65b9\u6cd5\uff0c\u5171\u5206\u4e3a\u4e24\u6b65\u3002
\u6bcf\u5f53\u5806\u5316\u4e00\u4e2a\u8282\u70b9\u540e\uff0c\u4ee5\u8be5\u8282\u70b9\u4e3a\u6839\u8282\u70b9\u7684\u5b50\u6811\u5c31\u5f62\u6210\u4e00\u4e2a\u5408\u6cd5\u7684\u5b50\u5806\u3002\u800c\u7531\u4e8e\u662f\u5012\u5e8f\u904d\u5386\uff0c\u56e0\u6b64\u5806\u662f\u201c\u81ea\u4e0b\u800c\u4e0a\u201d\u6784\u5efa\u7684\u3002
\u4e4b\u6240\u4ee5\u9009\u62e9\u5012\u5e8f\u904d\u5386\uff0c\u662f\u56e0\u4e3a\u8fd9\u6837\u80fd\u591f\u4fdd\u8bc1\u5f53\u524d\u8282\u70b9\u4e4b\u4e0b\u7684\u5b50\u6811\u5df2\u7ecf\u662f\u5408\u6cd5\u7684\u5b50\u5806\uff0c\u8fd9\u6837\u5806\u5316\u5f53\u524d\u8282\u70b9\u624d\u662f\u6709\u6548\u7684\u3002
\u503c\u5f97\u8bf4\u660e\u7684\u662f\uff0c\u7531\u4e8e\u53f6\u8282\u70b9\u6ca1\u6709\u5b50\u8282\u70b9\uff0c\u56e0\u6b64\u5b83\u4eec\u5929\u7136\u5c31\u662f\u5408\u6cd5\u7684\u5b50\u5806\uff0c\u65e0\u987b\u5806\u5316\u3002\u5982\u4ee5\u4e0b\u4ee3\u7801\u6240\u793a\uff0c\u6700\u540e\u4e00\u4e2a\u975e\u53f6\u8282\u70b9\u662f\u6700\u540e\u4e00\u4e2a\u8282\u70b9\u7684\u7236\u8282\u70b9\uff0c\u6211\u4eec\u4ece\u5b83\u5f00\u59cb\u5012\u5e8f\u904d\u5386\u5e76\u6267\u884c\u5806\u5316\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig my_heap.pydef __init__(self, nums: list[int]):\n \"\"\"\u6784\u9020\u65b9\u6cd5\uff0c\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806\"\"\"\n # \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\n self.max_heap = nums\n # \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n for i in range(self.parent(self.size() - 1), -1, -1):\n self.sift_down(i)\n
my_heap.cpp/* \u6784\u9020\u65b9\u6cd5\uff0c\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806 */\nMaxHeap(vector<int> nums) {\n // \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\n maxHeap = nums;\n // \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n for (int i = parent(size() - 1); i >= 0; i--) {\n siftDown(i);\n }\n}\n
my_heap.java/* \u6784\u9020\u65b9\u6cd5\uff0c\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806 */\nMaxHeap(List<Integer> nums) {\n // \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\n maxHeap = new ArrayList<>(nums);\n // \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n for (int i = parent(size() - 1); i >= 0; i--) {\n siftDown(i);\n }\n}\n
my_heap.cs/* \u6784\u9020\u51fd\u6570\uff0c\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806 */\nMaxHeap(IEnumerable<int> nums) {\n // \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\n maxHeap = new List<int>(nums);\n // \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n var size = Parent(this.Size() - 1);\n for (int i = size; i >= 0; i--) {\n SiftDown(i);\n }\n}\n
my_heap.go/* \u6784\u9020\u51fd\u6570\uff0c\u6839\u636e\u5207\u7247\u5efa\u5806 */\nfunc newMaxHeap(nums []any) *maxHeap {\n // \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\n h := &maxHeap{data: nums}\n for i := h.parent(len(h.data) - 1); i >= 0; i-- {\n // \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n h.siftDown(i)\n }\n return h\n}\n
my_heap.swift/* \u6784\u9020\u65b9\u6cd5\uff0c\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806 */\ninit(nums: [Int]) {\n // \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\n maxHeap = nums\n // \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n for i in stride(from: parent(i: size() - 1), through: 0, by: -1) {\n siftDown(i: i)\n }\n}\n
my_heap.js/* \u6784\u9020\u65b9\u6cd5\uff0c\u5efa\u7acb\u7a7a\u5806\u6216\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806 */\nconstructor(nums) {\n // \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\n this.#maxHeap = nums === undefined ? [] : [...nums];\n // \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n for (let i = this.#parent(this.size() - 1); i >= 0; i--) {\n this.#siftDown(i);\n }\n}\n
my_heap.ts/* \u6784\u9020\u65b9\u6cd5\uff0c\u5efa\u7acb\u7a7a\u5806\u6216\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806 */\nconstructor(nums?: number[]) {\n // \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\n this.maxHeap = nums === undefined ? [] : [...nums];\n // \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n for (let i = this.parent(this.size() - 1); i >= 0; i--) {\n this.siftDown(i);\n }\n}\n
my_heap.dart/* \u6784\u9020\u65b9\u6cd5\uff0c\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806 */\nMaxHeap(List<int> nums) {\n // \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\n _maxHeap = nums;\n // \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n for (int i = _parent(size() - 1); i >= 0; i--) {\n siftDown(i);\n }\n}\n
my_heap.rs/* \u6784\u9020\u65b9\u6cd5\uff0c\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806 */\nfn new(nums: Vec<i32>) -> Self {\n // \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\n let mut heap = MaxHeap { max_heap: nums };\n // \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n for i in (0..=Self::parent(heap.size() - 1)).rev() {\n heap.sift_down(i);\n }\n heap\n}\n
my_heap.c/* \u6784\u9020\u51fd\u6570\uff0c\u6839\u636e\u5207\u7247\u5efa\u5806 */\nMaxHeap *newMaxHeap(int nums[], int size) {\n // \u6240\u6709\u5143\u7d20\u5165\u5806\n MaxHeap *maxHeap = (MaxHeap *)malloc(sizeof(MaxHeap));\n maxHeap->size = size;\n memcpy(maxHeap->data, nums, size * sizeof(int));\n for (int i = parent(maxHeap, size - 1); i >= 0; i--) {\n // \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n siftDown(maxHeap, i);\n }\n return maxHeap;\n}\n
my_heap.zig// \u6784\u9020\u65b9\u6cd5\uff0c\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806\nfn init(self: *Self, allocator: std.mem.Allocator, nums: []const T) !void {\n if (self.max_heap != null) return;\n self.max_heap = std.ArrayList(T).init(allocator);\n // \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\n try self.max_heap.?.appendSlice(nums);\n // \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n var i: usize = parent(self.size() - 1) + 1;\n while (i > 0) : (i -= 1) {\n try self.siftDown(i - 1);\n }\n}\n
"},{"location":"chapter_heap/build_heap/#823","title":"8.2.3 \u00a0 \u590d\u6742\u5ea6\u5206\u6790","text":"\u4e0b\u9762\uff0c\u6211\u4eec\u6765\u5c1d\u8bd5\u63a8\u7b97\u7b2c\u4e8c\u79cd\u5efa\u5806\u65b9\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u3002
\u5c06\u4e0a\u8ff0\u4e24\u8005\u76f8\u4e58\uff0c\u53ef\u5f97\u5230\u5efa\u5806\u8fc7\u7a0b\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n \\log n)\\) \u3002\u4f46\u8fd9\u4e2a\u4f30\u7b97\u7ed3\u679c\u5e76\u4e0d\u51c6\u786e\uff0c\u56e0\u4e3a\u6211\u4eec\u6ca1\u6709\u8003\u8651\u5230\u4e8c\u53c9\u6811\u5e95\u5c42\u8282\u70b9\u6570\u91cf\u8fdc\u591a\u4e8e\u9876\u5c42\u8282\u70b9\u7684\u6027\u8d28\u3002
\u63a5\u4e0b\u6765\u6211\u4eec\u6765\u8fdb\u884c\u66f4\u4e3a\u51c6\u786e\u7684\u8ba1\u7b97\u3002\u4e3a\u4e86\u964d\u4f4e\u8ba1\u7b97\u96be\u5ea6\uff0c\u5047\u8bbe\u7ed9\u5b9a\u4e00\u4e2a\u8282\u70b9\u6570\u91cf\u4e3a \\(n\\) \u3001\u9ad8\u5ea6\u4e3a \\(h\\) \u7684\u201c\u5b8c\u7f8e\u4e8c\u53c9\u6811\u201d\uff0c\u8be5\u5047\u8bbe\u4e0d\u4f1a\u5f71\u54cd\u8ba1\u7b97\u7ed3\u679c\u7684\u6b63\u786e\u6027\u3002
\u56fe 8-5 \u00a0 \u5b8c\u7f8e\u4e8c\u53c9\u6811\u7684\u5404\u5c42\u8282\u70b9\u6570\u91cf
\u5982\u56fe 8-5 \u6240\u793a\uff0c\u8282\u70b9\u201c\u4ece\u9876\u81f3\u5e95\u5806\u5316\u201d\u7684\u6700\u5927\u8fed\u4ee3\u6b21\u6570\u7b49\u4e8e\u8be5\u8282\u70b9\u5230\u53f6\u8282\u70b9\u7684\u8ddd\u79bb\uff0c\u800c\u8be5\u8ddd\u79bb\u6b63\u662f\u201c\u8282\u70b9\u9ad8\u5ea6\u201d\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u5bf9\u5404\u5c42\u7684\u201c\u8282\u70b9\u6570\u91cf \\(\\times\\) \u8282\u70b9\u9ad8\u5ea6\u201d\u6c42\u548c\uff0c\u5f97\u5230\u6240\u6709\u8282\u70b9\u7684\u5806\u5316\u8fed\u4ee3\u6b21\u6570\u7684\u603b\u548c\u3002
\\[ T(h) = 2^0h + 2^1(h-1) + 2^2(h-2) + \\dots + 2^{(h-1)}\\times1 \\]\u5316\u7b80\u4e0a\u5f0f\u9700\u8981\u501f\u52a9\u4e2d\u5b66\u7684\u6570\u5217\u77e5\u8bc6\uff0c\u5148\u5bf9 \\(T(h)\\) \u4e58\u4ee5 \\(2\\) \uff0c\u5f97\u5230\uff1a
\\[ \\begin{aligned} T(h) & = 2^0h + 2^1(h-1) + 2^2(h-2) + \\dots + 2^{h-1}\\times1 \\newline 2 T(h) & = 2^1h + 2^2(h-1) + 2^3(h-2) + \\dots + 2^{h}\\times1 \\newline \\end{aligned} \\]\u4f7f\u7528\u9519\u4f4d\u76f8\u51cf\u6cd5\uff0c\u7528\u4e0b\u5f0f \\(2 T(h)\\) \u51cf\u53bb\u4e0a\u5f0f \\(T(h)\\) \uff0c\u53ef\u5f97\uff1a
\\[ 2T(h) - T(h) = T(h) = -2^0h + 2^1 + 2^2 + \\dots + 2^{h-1} + 2^h \\]\u89c2\u5bdf\u4e0a\u5f0f\uff0c\u53d1\u73b0 \\(T(h)\\) \u662f\u4e00\u4e2a\u7b49\u6bd4\u6570\u5217\uff0c\u53ef\u76f4\u63a5\u4f7f\u7528\u6c42\u548c\u516c\u5f0f\uff0c\u5f97\u5230\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a\uff1a
\\[ \\begin{aligned} T(h) & = 2 \\frac{1 - 2^h}{1 - 2} - h \\newline & = 2^{h+1} - h - 2 \\newline & = O(2^h) \\end{aligned} \\]\u8fdb\u4e00\u6b65\u5730\uff0c\u9ad8\u5ea6\u4e3a \\(h\\) \u7684\u5b8c\u7f8e\u4e8c\u53c9\u6811\u7684\u8282\u70b9\u6570\u91cf\u4e3a \\(n = 2^{h+1} - 1\\) \uff0c\u6613\u5f97\u590d\u6742\u5ea6\u4e3a \\(O(2^h) = O(n)\\) \u3002\u4ee5\u4e0a\u63a8\u7b97\u8868\u660e\uff0c\u8f93\u5165\u5217\u8868\u5e76\u5efa\u5806\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \uff0c\u975e\u5e38\u9ad8\u6548\u3002
"},{"location":"chapter_heap/heap/","title":"8.1 \u00a0 \u5806","text":"\u300c\u5806 heap\u300d\u662f\u4e00\u79cd\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u7684\u5b8c\u5168\u4e8c\u53c9\u6811\uff0c\u4e3b\u8981\u53ef\u5206\u4e3a\u4e24\u79cd\u7c7b\u578b\uff0c\u5982\u56fe 8-1 \u6240\u793a\u3002
\u56fe 8-1 \u00a0 \u5c0f\u9876\u5806\u4e0e\u5927\u9876\u5806
\u5806\u4f5c\u4e3a\u5b8c\u5168\u4e8c\u53c9\u6811\u7684\u4e00\u4e2a\u7279\u4f8b\uff0c\u5177\u6709\u4ee5\u4e0b\u7279\u6027\u3002
\u9700\u8981\u6307\u51fa\u7684\u662f\uff0c\u8bb8\u591a\u7f16\u7a0b\u8bed\u8a00\u63d0\u4f9b\u7684\u662f\u300c\u4f18\u5148\u961f\u5217 priority queue\u300d\uff0c\u8fd9\u662f\u4e00\u79cd\u62bd\u8c61\u6570\u636e\u7ed3\u6784\uff0c\u5b9a\u4e49\u4e3a\u5177\u6709\u4f18\u5148\u7ea7\u6392\u5e8f\u7684\u961f\u5217\u3002
\u5b9e\u9645\u4e0a\uff0c\u5806\u901a\u5e38\u7528\u4e8e\u5b9e\u73b0\u4f18\u5148\u961f\u5217\uff0c\u5927\u9876\u5806\u76f8\u5f53\u4e8e\u5143\u7d20\u6309\u4ece\u5927\u5230\u5c0f\u7684\u987a\u5e8f\u51fa\u961f\u7684\u4f18\u5148\u961f\u5217\u3002\u4ece\u4f7f\u7528\u89d2\u5ea6\u6765\u770b\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u201c\u4f18\u5148\u961f\u5217\u201d\u548c\u201c\u5806\u201d\u770b\u4f5c\u7b49\u4ef7\u7684\u6570\u636e\u7ed3\u6784\u3002\u56e0\u6b64\uff0c\u672c\u4e66\u5bf9\u4e24\u8005\u4e0d\u505a\u7279\u522b\u533a\u5206\uff0c\u7edf\u4e00\u79f0\u4f5c\u201c\u5806\u201d\u3002
\u5806\u7684\u5e38\u7528\u64cd\u4f5c\u89c1\u8868 8-1 \uff0c\u65b9\u6cd5\u540d\u9700\u8981\u6839\u636e\u7f16\u7a0b\u8bed\u8a00\u6765\u786e\u5b9a\u3002
\u8868 8-1 \u00a0 \u5806\u7684\u64cd\u4f5c\u6548\u7387
\u65b9\u6cd5\u540d \u63cf\u8ff0 \u65f6\u95f4\u590d\u6742\u5ea6 push() \u5143\u7d20\u5165\u5806 \\(O(\\log n)\\) pop() \u5806\u9876\u5143\u7d20\u51fa\u5806 \\(O(\\log n)\\) peek() \u8bbf\u95ee\u5806\u9876\u5143\u7d20\uff08\u5927 / \u5c0f\u9876\u5806\u5206\u522b\u4e3a\u6700\u5927 / \u5c0f\u503c\uff09 \\(O(1)\\) size() \u83b7\u53d6\u5806\u7684\u5143\u7d20\u6570\u91cf \\(O(1)\\) isEmpty() \u5224\u65ad\u5806\u662f\u5426\u4e3a\u7a7a \\(O(1)\\)\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u7f16\u7a0b\u8bed\u8a00\u63d0\u4f9b\u7684\u5806\u7c7b\uff08\u6216\u4f18\u5148\u961f\u5217\u7c7b\uff09\u3002
\u7c7b\u4f3c\u4e8e\u6392\u5e8f\u7b97\u6cd5\u4e2d\u7684\u201c\u4ece\u5c0f\u5230\u5927\u6392\u5217\u201d\u548c\u201c\u4ece\u5927\u5230\u5c0f\u6392\u5217\u201d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u4e00\u4e2a flag
\u6216\u4fee\u6539 Comparator
\u5b9e\u73b0\u201c\u5c0f\u9876\u5806\u201d\u4e0e\u201c\u5927\u9876\u5806\u201d\u4e4b\u95f4\u7684\u8f6c\u6362\u3002\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a
# \u521d\u59cb\u5316\u5c0f\u9876\u5806\nmin_heap, flag = [], 1\n# \u521d\u59cb\u5316\u5927\u9876\u5806\nmax_heap, flag = [], -1\n\n# Python \u7684 heapq \u6a21\u5757\u9ed8\u8ba4\u5b9e\u73b0\u5c0f\u9876\u5806\n# \u8003\u8651\u5c06\u201c\u5143\u7d20\u53d6\u8d1f\u201d\u540e\u518d\u5165\u5806\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u5c06\u5927\u5c0f\u5173\u7cfb\u98a0\u5012\uff0c\u4ece\u800c\u5b9e\u73b0\u5927\u9876\u5806\n# \u5728\u672c\u793a\u4f8b\u4e2d\uff0cflag = 1 \u65f6\u5bf9\u5e94\u5c0f\u9876\u5806\uff0cflag = -1 \u65f6\u5bf9\u5e94\u5927\u9876\u5806\n\n# \u5143\u7d20\u5165\u5806\nheapq.heappush(max_heap, flag * 1)\nheapq.heappush(max_heap, flag * 3)\nheapq.heappush(max_heap, flag * 2)\nheapq.heappush(max_heap, flag * 5)\nheapq.heappush(max_heap, flag * 4)\n\n# \u83b7\u53d6\u5806\u9876\u5143\u7d20\npeek: int = flag * max_heap[0] # 5\n\n# \u5806\u9876\u5143\u7d20\u51fa\u5806\n# \u51fa\u5806\u5143\u7d20\u4f1a\u5f62\u6210\u4e00\u4e2a\u4ece\u5927\u5230\u5c0f\u7684\u5e8f\u5217\nval = flag * heapq.heappop(max_heap) # 5\nval = flag * heapq.heappop(max_heap) # 4\nval = flag * heapq.heappop(max_heap) # 3\nval = flag * heapq.heappop(max_heap) # 2\nval = flag * heapq.heappop(max_heap) # 1\n\n# \u83b7\u53d6\u5806\u5927\u5c0f\nsize: int = len(max_heap)\n\n# \u5224\u65ad\u5806\u662f\u5426\u4e3a\u7a7a\nis_empty: bool = not max_heap\n\n# \u8f93\u5165\u5217\u8868\u5e76\u5efa\u5806\nmin_heap: list[int] = [1, 3, 2, 5, 4]\nheapq.heapify(min_heap)\n
heap.cpp/* \u521d\u59cb\u5316\u5806 */\n// \u521d\u59cb\u5316\u5c0f\u9876\u5806\npriority_queue<int, vector<int>, greater<int>> minHeap;\n// \u521d\u59cb\u5316\u5927\u9876\u5806\npriority_queue<int, vector<int>, less<int>> maxHeap;\n\n/* \u5143\u7d20\u5165\u5806 */\nmaxHeap.push(1);\nmaxHeap.push(3);\nmaxHeap.push(2);\nmaxHeap.push(5);\nmaxHeap.push(4);\n\n/* \u83b7\u53d6\u5806\u9876\u5143\u7d20 */\nint peek = maxHeap.top(); // 5\n\n/* \u5806\u9876\u5143\u7d20\u51fa\u5806 */\n// \u51fa\u5806\u5143\u7d20\u4f1a\u5f62\u6210\u4e00\u4e2a\u4ece\u5927\u5230\u5c0f\u7684\u5e8f\u5217\nmaxHeap.pop(); // 5\nmaxHeap.pop(); // 4\nmaxHeap.pop(); // 3\nmaxHeap.pop(); // 2\nmaxHeap.pop(); // 1\n\n/* \u83b7\u53d6\u5806\u5927\u5c0f */\nint size = maxHeap.size();\n\n/* \u5224\u65ad\u5806\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty = maxHeap.empty();\n\n/* \u8f93\u5165\u5217\u8868\u5e76\u5efa\u5806 */\nvector<int> input{1, 3, 2, 5, 4};\npriority_queue<int, vector<int>, greater<int>> minHeap(input.begin(), input.end());\n
heap.java/* \u521d\u59cb\u5316\u5806 */\n// \u521d\u59cb\u5316\u5c0f\u9876\u5806\nQueue<Integer> minHeap = new PriorityQueue<>();\n// \u521d\u59cb\u5316\u5927\u9876\u5806\uff08\u4f7f\u7528 lambda \u8868\u8fbe\u5f0f\u4fee\u6539 Comparator \u5373\u53ef\uff09\nQueue<Integer> maxHeap = new PriorityQueue<>((a, b) -> b - a);\n\n/* \u5143\u7d20\u5165\u5806 */\nmaxHeap.offer(1);\nmaxHeap.offer(3);\nmaxHeap.offer(2);\nmaxHeap.offer(5);\nmaxHeap.offer(4);\n\n/* \u83b7\u53d6\u5806\u9876\u5143\u7d20 */\nint peek = maxHeap.peek(); // 5\n\n/* \u5806\u9876\u5143\u7d20\u51fa\u5806 */\n// \u51fa\u5806\u5143\u7d20\u4f1a\u5f62\u6210\u4e00\u4e2a\u4ece\u5927\u5230\u5c0f\u7684\u5e8f\u5217\npeek = maxHeap.poll(); // 5\npeek = maxHeap.poll(); // 4\npeek = maxHeap.poll(); // 3\npeek = maxHeap.poll(); // 2\npeek = maxHeap.poll(); // 1\n\n/* \u83b7\u53d6\u5806\u5927\u5c0f */\nint size = maxHeap.size();\n\n/* \u5224\u65ad\u5806\u662f\u5426\u4e3a\u7a7a */\nboolean isEmpty = maxHeap.isEmpty();\n\n/* \u8f93\u5165\u5217\u8868\u5e76\u5efa\u5806 */\nminHeap = new PriorityQueue<>(Arrays.asList(1, 3, 2, 5, 4));\n
heap.cs/* \u521d\u59cb\u5316\u5806 */\n// \u521d\u59cb\u5316\u5c0f\u9876\u5806\nPriorityQueue<int, int> minHeap = new();\n// \u521d\u59cb\u5316\u5927\u9876\u5806\uff08\u4f7f\u7528 lambda \u8868\u8fbe\u5f0f\u4fee\u6539 Comparator \u5373\u53ef\uff09\nPriorityQueue<int, int> maxHeap = new(Comparer<int>.Create((x, y) => y - x));\n\n/* \u5143\u7d20\u5165\u5806 */\nmaxHeap.Enqueue(1, 1);\nmaxHeap.Enqueue(3, 3);\nmaxHeap.Enqueue(2, 2);\nmaxHeap.Enqueue(5, 5);\nmaxHeap.Enqueue(4, 4);\n\n/* \u83b7\u53d6\u5806\u9876\u5143\u7d20 */\nint peek = maxHeap.Peek();//5\n\n/* \u5806\u9876\u5143\u7d20\u51fa\u5806 */\n// \u51fa\u5806\u5143\u7d20\u4f1a\u5f62\u6210\u4e00\u4e2a\u4ece\u5927\u5230\u5c0f\u7684\u5e8f\u5217\npeek = maxHeap.Dequeue(); // 5\npeek = maxHeap.Dequeue(); // 4\npeek = maxHeap.Dequeue(); // 3\npeek = maxHeap.Dequeue(); // 2\npeek = maxHeap.Dequeue(); // 1\n\n/* \u83b7\u53d6\u5806\u5927\u5c0f */\nint size = maxHeap.Count;\n\n/* \u5224\u65ad\u5806\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty = maxHeap.Count == 0;\n\n/* \u8f93\u5165\u5217\u8868\u5e76\u5efa\u5806 */\nminHeap = new PriorityQueue<int, int>([(1, 1), (3, 3), (2, 2), (5, 5), (4, 4)]);\n
heap.go// Go \u8bed\u8a00\u4e2d\u53ef\u4ee5\u901a\u8fc7\u5b9e\u73b0 heap.Interface \u6765\u6784\u5efa\u6574\u6570\u5927\u9876\u5806\n// \u5b9e\u73b0 heap.Interface \u9700\u8981\u540c\u65f6\u5b9e\u73b0 sort.Interface\ntype intHeap []any\n\n// Push heap.Interface \u7684\u65b9\u6cd5\uff0c\u5b9e\u73b0\u63a8\u5165\u5143\u7d20\u5230\u5806\nfunc (h *intHeap) Push(x any) {\n // Push \u548c Pop \u4f7f\u7528 pointer receiver \u4f5c\u4e3a\u53c2\u6570\n // \u56e0\u4e3a\u5b83\u4eec\u4e0d\u4ec5\u4f1a\u5bf9\u5207\u7247\u7684\u5185\u5bb9\u8fdb\u884c\u8c03\u6574\uff0c\u8fd8\u4f1a\u4fee\u6539\u5207\u7247\u7684\u957f\u5ea6\u3002\n *h = append(*h, x.(int))\n}\n\n// Pop heap.Interface \u7684\u65b9\u6cd5\uff0c\u5b9e\u73b0\u5f39\u51fa\u5806\u9876\u5143\u7d20\nfunc (h *intHeap) Pop() any {\n // \u5f85\u51fa\u5806\u5143\u7d20\u5b58\u653e\u5728\u6700\u540e\n last := (*h)[len(*h)-1]\n *h = (*h)[:len(*h)-1]\n return last\n}\n\n// Len sort.Interface \u7684\u65b9\u6cd5\nfunc (h *intHeap) Len() int {\n return len(*h)\n}\n\n// Less sort.Interface \u7684\u65b9\u6cd5\nfunc (h *intHeap) Less(i, j int) bool {\n // \u5982\u679c\u5b9e\u73b0\u5c0f\u9876\u5806\uff0c\u5219\u9700\u8981\u8c03\u6574\u4e3a\u5c0f\u4e8e\u53f7\n return (*h)[i].(int) > (*h)[j].(int)\n}\n\n// Swap sort.Interface \u7684\u65b9\u6cd5\nfunc (h *intHeap) Swap(i, j int) {\n (*h)[i], (*h)[j] = (*h)[j], (*h)[i]\n}\n\n// Top \u83b7\u53d6\u5806\u9876\u5143\u7d20\nfunc (h *intHeap) Top() any {\n return (*h)[0]\n}\n\n/* Driver Code */\nfunc TestHeap(t *testing.T) {\n /* \u521d\u59cb\u5316\u5806 */\n // \u521d\u59cb\u5316\u5927\u9876\u5806\n maxHeap := &intHeap{}\n heap.Init(maxHeap)\n /* \u5143\u7d20\u5165\u5806 */\n // \u8c03\u7528 heap.Interface \u7684\u65b9\u6cd5\uff0c\u6765\u6dfb\u52a0\u5143\u7d20\n heap.Push(maxHeap, 1)\n heap.Push(maxHeap, 3)\n heap.Push(maxHeap, 2)\n heap.Push(maxHeap, 4)\n heap.Push(maxHeap, 5)\n\n /* \u83b7\u53d6\u5806\u9876\u5143\u7d20 */\n top := maxHeap.Top()\n fmt.Printf(\"\u5806\u9876\u5143\u7d20\u4e3a %d\\n\", top)\n\n /* \u5806\u9876\u5143\u7d20\u51fa\u5806 */\n // \u8c03\u7528 heap.Interface \u7684\u65b9\u6cd5\uff0c\u6765\u79fb\u9664\u5143\u7d20\n heap.Pop(maxHeap) // 5\n heap.Pop(maxHeap) // 4\n heap.Pop(maxHeap) // 3\n heap.Pop(maxHeap) // 2\n heap.Pop(maxHeap) // 1\n\n /* \u83b7\u53d6\u5806\u5927\u5c0f */\n size := len(*maxHeap)\n fmt.Printf(\"\u5806\u5143\u7d20\u6570\u91cf\u4e3a %d\\n\", size)\n\n /* \u5224\u65ad\u5806\u662f\u5426\u4e3a\u7a7a */\n isEmpty := len(*maxHeap) == 0\n fmt.Printf(\"\u5806\u662f\u5426\u4e3a\u7a7a %t\\n\", isEmpty)\n}\n
heap.swift// Swift \u672a\u63d0\u4f9b\u5185\u7f6e Heap \u7c7b\n
heap.js// JavaScript \u672a\u63d0\u4f9b\u5185\u7f6e Heap \u7c7b\n
heap.ts// TypeScript \u672a\u63d0\u4f9b\u5185\u7f6e Heap \u7c7b\n
heap.dart// Dart \u672a\u63d0\u4f9b\u5185\u7f6e Heap \u7c7b\n
heap.rsuse std::collections::BinaryHeap;\nuse std::cmp::Reverse;\n\n/* \u521d\u59cb\u5316\u5806 */\n// \u521d\u59cb\u5316\u5c0f\u9876\u5806\nlet mut min_heap = BinaryHeap::<Reverse<i32>>::new();\n// \u521d\u59cb\u5316\u5927\u9876\u5806\nlet mut max_heap = BinaryHeap::new();\n\n/* \u5143\u7d20\u5165\u5806 */\nmax_heap.push(1);\nmax_heap.push(3);\nmax_heap.push(2);\nmax_heap.push(5);\nmax_heap.push(4);\n\n/* \u83b7\u53d6\u5806\u9876\u5143\u7d20 */\nlet peek = max_heap.peek().unwrap(); // 5\n\n/* \u5806\u9876\u5143\u7d20\u51fa\u5806 */\n// \u51fa\u5806\u5143\u7d20\u4f1a\u5f62\u6210\u4e00\u4e2a\u4ece\u5927\u5230\u5c0f\u7684\u5e8f\u5217\nlet peek = max_heap.pop().unwrap(); // 5\nlet peek = max_heap.pop().unwrap(); // 4\nlet peek = max_heap.pop().unwrap(); // 3\nlet peek = max_heap.pop().unwrap(); // 2\nlet peek = max_heap.pop().unwrap(); // 1\n\n/* \u83b7\u53d6\u5806\u5927\u5c0f */\nlet size = max_heap.len();\n\n/* \u5224\u65ad\u5806\u662f\u5426\u4e3a\u7a7a */\nlet is_empty = max_heap.is_empty();\n\n/* \u8f93\u5165\u5217\u8868\u5e76\u5efa\u5806 */\nlet min_heap = BinaryHeap::from(vec![Reverse(1), Reverse(3), Reverse(2), Reverse(5), Reverse(4)]);\n
heap.c// C \u672a\u63d0\u4f9b\u5185\u7f6e Heap \u7c7b\n
heap.zig\n
"},{"location":"chapter_heap/heap/#812","title":"8.1.2 \u00a0 \u5806\u7684\u5b9e\u73b0","text":"\u4e0b\u6587\u5b9e\u73b0\u7684\u662f\u5927\u9876\u5806\u3002\u82e5\u8981\u5c06\u5176\u8f6c\u6362\u4e3a\u5c0f\u9876\u5806\uff0c\u53ea\u9700\u5c06\u6240\u6709\u5927\u5c0f\u903b\u8f91\u5224\u65ad\u53d6\u9006\uff08\u4f8b\u5982\uff0c\u5c06 \\(\\geq\\) \u66ff\u6362\u4e3a \\(\\leq\\) \uff09\u3002\u611f\u5174\u8da3\u7684\u8bfb\u8005\u53ef\u4ee5\u81ea\u884c\u5b9e\u73b0\u3002
"},{"location":"chapter_heap/heap/#1","title":"1. \u00a0 \u5806\u7684\u5b58\u50a8\u4e0e\u8868\u793a","text":"\u201c\u4e8c\u53c9\u6811\u201d\u7ae0\u8282\u8bb2\u8fc7\uff0c\u5b8c\u5168\u4e8c\u53c9\u6811\u975e\u5e38\u9002\u5408\u7528\u6570\u7ec4\u6765\u8868\u793a\u3002\u7531\u4e8e\u5806\u6b63\u662f\u4e00\u79cd\u5b8c\u5168\u4e8c\u53c9\u6811\uff0c\u56e0\u6b64\u6211\u4eec\u5c06\u91c7\u7528\u6570\u7ec4\u6765\u5b58\u50a8\u5806\u3002
\u5f53\u4f7f\u7528\u6570\u7ec4\u8868\u793a\u4e8c\u53c9\u6811\u65f6\uff0c\u5143\u7d20\u4ee3\u8868\u8282\u70b9\u503c\uff0c\u7d22\u5f15\u4ee3\u8868\u8282\u70b9\u5728\u4e8c\u53c9\u6811\u4e2d\u7684\u4f4d\u7f6e\u3002\u8282\u70b9\u6307\u9488\u901a\u8fc7\u7d22\u5f15\u6620\u5c04\u516c\u5f0f\u6765\u5b9e\u73b0\u3002
\u5982\u56fe 8-2 \u6240\u793a\uff0c\u7ed9\u5b9a\u7d22\u5f15 \\(i\\) \uff0c\u5176\u5de6\u5b50\u8282\u70b9\u7d22\u5f15\u4e3a \\(2i + 1\\) \uff0c\u53f3\u5b50\u8282\u70b9\u7d22\u5f15\u4e3a \\(2i + 2\\) \uff0c\u7236\u8282\u70b9\u7d22\u5f15\u4e3a \\((i - 1) / 2\\)\uff08\u5411\u4e0b\u6574\u9664\uff09\u3002\u5f53\u7d22\u5f15\u8d8a\u754c\u65f6\uff0c\u8868\u793a\u7a7a\u8282\u70b9\u6216\u8282\u70b9\u4e0d\u5b58\u5728\u3002
\u56fe 8-2 \u00a0 \u5806\u7684\u8868\u793a\u4e0e\u5b58\u50a8
\u6211\u4eec\u53ef\u4ee5\u5c06\u7d22\u5f15\u6620\u5c04\u516c\u5f0f\u5c01\u88c5\u6210\u51fd\u6570\uff0c\u65b9\u4fbf\u540e\u7eed\u4f7f\u7528\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig my_heap.pydef left(self, i: int) -> int:\n \"\"\"\u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7d22\u5f15\"\"\"\n return 2 * i + 1\n\ndef right(self, i: int) -> int:\n \"\"\"\u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7d22\u5f15\"\"\"\n return 2 * i + 2\n\ndef parent(self, i: int) -> int:\n \"\"\"\u83b7\u53d6\u7236\u8282\u70b9\u7d22\u5f15\"\"\"\n return (i - 1) // 2 # \u5411\u4e0b\u6574\u9664\n
my_heap.cpp/* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7d22\u5f15 */\nint left(int i) {\n return 2 * i + 1;\n}\n\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7d22\u5f15 */\nint right(int i) {\n return 2 * i + 2;\n}\n\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7d22\u5f15 */\nint parent(int i) {\n return (i - 1) / 2; // \u5411\u4e0b\u6574\u9664\n}\n
my_heap.java/* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7d22\u5f15 */\nint left(int i) {\n return 2 * i + 1;\n}\n\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7d22\u5f15 */\nint right(int i) {\n return 2 * i + 2;\n}\n\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7d22\u5f15 */\nint parent(int i) {\n return (i - 1) / 2; // \u5411\u4e0b\u6574\u9664\n}\n
my_heap.cs/* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7d22\u5f15 */\nint Left(int i) {\n return 2 * i + 1;\n}\n\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7d22\u5f15 */\nint Right(int i) {\n return 2 * i + 2;\n}\n\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7d22\u5f15 */\nint Parent(int i) {\n return (i - 1) / 2; // \u5411\u4e0b\u6574\u9664\n}\n
my_heap.go/* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7d22\u5f15 */\nfunc (h *maxHeap) left(i int) int {\n return 2*i + 1\n}\n\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7d22\u5f15 */\nfunc (h *maxHeap) right(i int) int {\n return 2*i + 2\n}\n\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7d22\u5f15 */\nfunc (h *maxHeap) parent(i int) int {\n // \u5411\u4e0b\u6574\u9664\n return (i - 1) / 2\n}\n
my_heap.swift/* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7d22\u5f15 */\nfunc left(i: Int) -> Int {\n 2 * i + 1\n}\n\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7d22\u5f15 */\nfunc right(i: Int) -> Int {\n 2 * i + 2\n}\n\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7d22\u5f15 */\nfunc parent(i: Int) -> Int {\n (i - 1) / 2 // \u5411\u4e0b\u6574\u9664\n}\n
my_heap.js/* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7d22\u5f15 */\n#left(i) {\n return 2 * i + 1;\n}\n\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7d22\u5f15 */\n#right(i) {\n return 2 * i + 2;\n}\n\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7d22\u5f15 */\n#parent(i) {\n return Math.floor((i - 1) / 2); // \u5411\u4e0b\u6574\u9664\n}\n
my_heap.ts/* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7d22\u5f15 */\nleft(i: number): number {\n return 2 * i + 1;\n}\n\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7d22\u5f15 */\nright(i: number): number {\n return 2 * i + 2;\n}\n\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7d22\u5f15 */\nparent(i: number): number {\n return Math.floor((i - 1) / 2); // \u5411\u4e0b\u6574\u9664\n}\n
my_heap.dart/* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7d22\u5f15 */\nint _left(int i) {\n return 2 * i + 1;\n}\n\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7d22\u5f15 */\nint _right(int i) {\n return 2 * i + 2;\n}\n\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7d22\u5f15 */\nint _parent(int i) {\n return (i - 1) ~/ 2; // \u5411\u4e0b\u6574\u9664\n}\n
my_heap.rs/* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7d22\u5f15 */\nfn left(i: usize) -> usize {\n 2 * i + 1\n}\n\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7d22\u5f15 */\nfn right(i: usize) -> usize {\n 2 * i + 2\n}\n\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7d22\u5f15 */\nfn parent(i: usize) -> usize {\n (i - 1) / 2 // \u5411\u4e0b\u6574\u9664\n}\n
my_heap.c/* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7d22\u5f15 */\nint left(MaxHeap *maxHeap, int i) {\n return 2 * i + 1;\n}\n\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7d22\u5f15 */\nint right(MaxHeap *maxHeap, int i) {\n return 2 * i + 2;\n}\n\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7d22\u5f15 */\nint parent(MaxHeap *maxHeap, int i) {\n return (i - 1) / 2;\n}\n
my_heap.zig// \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7d22\u5f15\nfn left(i: usize) usize {\n return 2 * i + 1;\n}\n\n// \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7d22\u5f15\nfn right(i: usize) usize {\n return 2 * i + 2;\n}\n\n// \u83b7\u53d6\u7236\u8282\u70b9\u7d22\u5f15\nfn parent(i: usize) usize {\n // return (i - 1) / 2; // \u5411\u4e0b\u6574\u9664\n return @divFloor(i - 1, 2);\n}\n
"},{"location":"chapter_heap/heap/#2","title":"2. \u00a0 \u8bbf\u95ee\u5806\u9876\u5143\u7d20","text":"\u5806\u9876\u5143\u7d20\u5373\u4e3a\u4e8c\u53c9\u6811\u7684\u6839\u8282\u70b9\uff0c\u4e5f\u5c31\u662f\u5217\u8868\u7684\u9996\u4e2a\u5143\u7d20\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig my_heap.pydef peek(self) -> int:\n \"\"\"\u8bbf\u95ee\u5806\u9876\u5143\u7d20\"\"\"\n return self.max_heap[0]\n
my_heap.cpp/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nint peek() {\n return maxHeap[0];\n}\n
my_heap.java/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nint peek() {\n return maxHeap.get(0);\n}\n
my_heap.cs/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nint Peek() {\n return maxHeap[0];\n}\n
my_heap.go/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nfunc (h *maxHeap) peek() any {\n return h.data[0]\n}\n
my_heap.swift/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nfunc peek() -> Int {\n maxHeap[0]\n}\n
my_heap.js/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\npeek() {\n return this.#maxHeap[0];\n}\n
my_heap.ts/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\npeek(): number {\n return this.maxHeap[0];\n}\n
my_heap.dart/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nint peek() {\n return _maxHeap[0];\n}\n
my_heap.rs/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nfn peek(&self) -> Option<i32> {\n self.max_heap.first().copied()\n}\n
my_heap.c/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nint peek(MaxHeap *maxHeap) {\n return maxHeap->data[0];\n}\n
my_heap.zig// \u8bbf\u95ee\u5806\u9876\u5143\u7d20\nfn peek(self: *Self) T {\n return self.max_heap.?.items[0];\n} \n
"},{"location":"chapter_heap/heap/#3","title":"3. \u00a0 \u5143\u7d20\u5165\u5806","text":"\u7ed9\u5b9a\u5143\u7d20 val
\uff0c\u6211\u4eec\u9996\u5148\u5c06\u5176\u6dfb\u52a0\u5230\u5806\u5e95\u3002\u6dfb\u52a0\u4e4b\u540e\uff0c\u7531\u4e8e val \u53ef\u80fd\u5927\u4e8e\u5806\u4e2d\u5176\u4ed6\u5143\u7d20\uff0c\u5806\u7684\u6210\u7acb\u6761\u4ef6\u53ef\u80fd\u5df2\u88ab\u7834\u574f\uff0c\u56e0\u6b64\u9700\u8981\u4fee\u590d\u4ece\u63d2\u5165\u8282\u70b9\u5230\u6839\u8282\u70b9\u7684\u8def\u5f84\u4e0a\u7684\u5404\u4e2a\u8282\u70b9\uff0c\u8fd9\u4e2a\u64cd\u4f5c\u88ab\u79f0\u4e3a\u300c\u5806\u5316 heapify\u300d\u3002
\u8003\u8651\u4ece\u5165\u5806\u8282\u70b9\u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u6267\u884c\u5806\u5316\u3002\u5982\u56fe 8-3 \u6240\u793a\uff0c\u6211\u4eec\u6bd4\u8f83\u63d2\u5165\u8282\u70b9\u4e0e\u5176\u7236\u8282\u70b9\u7684\u503c\uff0c\u5982\u679c\u63d2\u5165\u8282\u70b9\u66f4\u5927\uff0c\u5219\u5c06\u5b83\u4eec\u4ea4\u6362\u3002\u7136\u540e\u7ee7\u7eed\u6267\u884c\u6b64\u64cd\u4f5c\uff0c\u4ece\u5e95\u81f3\u9876\u4fee\u590d\u5806\u4e2d\u7684\u5404\u4e2a\u8282\u70b9\uff0c\u76f4\u81f3\u8d8a\u8fc7\u6839\u8282\u70b9\u6216\u9047\u5230\u65e0\u987b\u4ea4\u6362\u7684\u8282\u70b9\u65f6\u7ed3\u675f\u3002
<1><2><3><4><5><6><7><8><9>\u56fe 8-3 \u00a0 \u5143\u7d20\u5165\u5806\u6b65\u9aa4
\u8bbe\u8282\u70b9\u603b\u6570\u4e3a \\(n\\) \uff0c\u5219\u6811\u7684\u9ad8\u5ea6\u4e3a \\(O(\\log n)\\) \u3002\u7531\u6b64\u53ef\u77e5\uff0c\u5806\u5316\u64cd\u4f5c\u7684\u5faa\u73af\u8f6e\u6570\u6700\u591a\u4e3a \\(O(\\log n)\\) \uff0c\u5143\u7d20\u5165\u5806\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(\\log n)\\) \u3002\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig my_heap.pydef push(self, val: int):\n \"\"\"\u5143\u7d20\u5165\u5806\"\"\"\n # \u6dfb\u52a0\u8282\u70b9\n self.max_heap.append(val)\n # \u4ece\u5e95\u81f3\u9876\u5806\u5316\n self.sift_up(self.size() - 1)\n\ndef sift_up(self, i: int):\n \"\"\"\u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316\"\"\"\n while True:\n # \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\n p = self.parent(i)\n # \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u987b\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\n if p < 0 or self.max_heap[i] <= self.max_heap[p]:\n break\n # \u4ea4\u6362\u4e24\u8282\u70b9\n self.swap(i, p)\n # \u5faa\u73af\u5411\u4e0a\u5806\u5316\n i = p\n
my_heap.cpp/* \u5143\u7d20\u5165\u5806 */\nvoid push(int val) {\n // \u6dfb\u52a0\u8282\u70b9\n maxHeap.push_back(val);\n // \u4ece\u5e95\u81f3\u9876\u5806\u5316\n siftUp(size() - 1);\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nvoid siftUp(int i) {\n while (true) {\n // \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\n int p = parent(i);\n // \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u987b\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\n if (p < 0 || maxHeap[i] <= maxHeap[p])\n break;\n // \u4ea4\u6362\u4e24\u8282\u70b9\n swap(maxHeap[i], maxHeap[p]);\n // \u5faa\u73af\u5411\u4e0a\u5806\u5316\n i = p;\n }\n}\n
my_heap.java/* \u5143\u7d20\u5165\u5806 */\nvoid push(int val) {\n // \u6dfb\u52a0\u8282\u70b9\n maxHeap.add(val);\n // \u4ece\u5e95\u81f3\u9876\u5806\u5316\n siftUp(size() - 1);\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nvoid siftUp(int i) {\n while (true) {\n // \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\n int p = parent(i);\n // \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u987b\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\n if (p < 0 || maxHeap.get(i) <= maxHeap.get(p))\n break;\n // \u4ea4\u6362\u4e24\u8282\u70b9\n swap(i, p);\n // \u5faa\u73af\u5411\u4e0a\u5806\u5316\n i = p;\n }\n}\n
my_heap.cs/* \u5143\u7d20\u5165\u5806 */\nvoid Push(int val) {\n // \u6dfb\u52a0\u8282\u70b9\n maxHeap.Add(val);\n // \u4ece\u5e95\u81f3\u9876\u5806\u5316\n SiftUp(Size() - 1);\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nvoid SiftUp(int i) {\n while (true) {\n // \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\n int p = Parent(i);\n // \u82e5\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u987b\u4fee\u590d\u201d\uff0c\u5219\u7ed3\u675f\u5806\u5316\n if (p < 0 || maxHeap[i] <= maxHeap[p])\n break;\n // \u4ea4\u6362\u4e24\u8282\u70b9\n Swap(i, p);\n // \u5faa\u73af\u5411\u4e0a\u5806\u5316\n i = p;\n }\n}\n
my_heap.go/* \u5143\u7d20\u5165\u5806 */\nfunc (h *maxHeap) push(val any) {\n // \u6dfb\u52a0\u8282\u70b9\n h.data = append(h.data, val)\n // \u4ece\u5e95\u81f3\u9876\u5806\u5316\n h.siftUp(len(h.data) - 1)\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nfunc (h *maxHeap) siftUp(i int) {\n for true {\n // \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\n p := h.parent(i)\n // \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u987b\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\n if p < 0 || h.data[i].(int) <= h.data[p].(int) {\n break\n }\n // \u4ea4\u6362\u4e24\u8282\u70b9\n h.swap(i, p)\n // \u5faa\u73af\u5411\u4e0a\u5806\u5316\n i = p\n }\n}\n
my_heap.swift/* \u5143\u7d20\u5165\u5806 */\nfunc push(val: Int) {\n // \u6dfb\u52a0\u8282\u70b9\n maxHeap.append(val)\n // \u4ece\u5e95\u81f3\u9876\u5806\u5316\n siftUp(i: size() - 1)\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nfunc siftUp(i: Int) {\n var i = i\n while true {\n // \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\n let p = parent(i: i)\n // \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u987b\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\n if p < 0 || maxHeap[i] <= maxHeap[p] {\n break\n }\n // \u4ea4\u6362\u4e24\u8282\u70b9\n swap(i: i, j: p)\n // \u5faa\u73af\u5411\u4e0a\u5806\u5316\n i = p\n }\n}\n
my_heap.js/* \u5143\u7d20\u5165\u5806 */\npush(val) {\n // \u6dfb\u52a0\u8282\u70b9\n this.#maxHeap.push(val);\n // \u4ece\u5e95\u81f3\u9876\u5806\u5316\n this.#siftUp(this.size() - 1);\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\n#siftUp(i) {\n while (true) {\n // \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\n const p = this.#parent(i);\n // \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u987b\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\n if (p < 0 || this.#maxHeap[i] <= this.#maxHeap[p]) break;\n // \u4ea4\u6362\u4e24\u8282\u70b9\n this.#swap(i, p);\n // \u5faa\u73af\u5411\u4e0a\u5806\u5316\n i = p;\n }\n}\n
my_heap.ts/* \u5143\u7d20\u5165\u5806 */\npush(val: number): void {\n // \u6dfb\u52a0\u8282\u70b9\n this.maxHeap.push(val);\n // \u4ece\u5e95\u81f3\u9876\u5806\u5316\n this.siftUp(this.size() - 1);\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nsiftUp(i: number): void {\n while (true) {\n // \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\n const p = this.parent(i);\n // \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u987b\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\n if (p < 0 || this.maxHeap[i] <= this.maxHeap[p]) break;\n // \u4ea4\u6362\u4e24\u8282\u70b9\n this.swap(i, p);\n // \u5faa\u73af\u5411\u4e0a\u5806\u5316\n i = p;\n }\n}\n
my_heap.dart/* \u5143\u7d20\u5165\u5806 */\nvoid push(int val) {\n // \u6dfb\u52a0\u8282\u70b9\n _maxHeap.add(val);\n // \u4ece\u5e95\u81f3\u9876\u5806\u5316\n siftUp(size() - 1);\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nvoid siftUp(int i) {\n while (true) {\n // \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\n int p = _parent(i);\n // \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u987b\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\n if (p < 0 || _maxHeap[i] <= _maxHeap[p]) {\n break;\n }\n // \u4ea4\u6362\u4e24\u8282\u70b9\n _swap(i, p);\n // \u5faa\u73af\u5411\u4e0a\u5806\u5316\n i = p;\n }\n}\n
my_heap.rs/* \u5143\u7d20\u5165\u5806 */\nfn push(&mut self, val: i32) {\n // \u6dfb\u52a0\u8282\u70b9\n self.max_heap.push(val);\n // \u4ece\u5e95\u81f3\u9876\u5806\u5316\n self.sift_up(self.size() - 1);\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nfn sift_up(&mut self, mut i: usize) {\n loop {\n // \u8282\u70b9 i \u5df2\u7ecf\u662f\u5806\u9876\u8282\u70b9\u4e86\uff0c\u7ed3\u675f\u5806\u5316\n if i == 0 {\n break;\n }\n // \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\n let p = Self::parent(i);\n // \u5f53\u201c\u8282\u70b9\u65e0\u987b\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\n if self.max_heap[i] <= self.max_heap[p] {\n break;\n }\n // \u4ea4\u6362\u4e24\u8282\u70b9\n self.swap(i, p);\n // \u5faa\u73af\u5411\u4e0a\u5806\u5316\n i = p;\n }\n}\n
my_heap.c/* \u5143\u7d20\u5165\u5806 */\nvoid push(MaxHeap *maxHeap, int val) {\n // \u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u4e0d\u5e94\u8be5\u6dfb\u52a0\u8fd9\u4e48\u591a\u8282\u70b9\n if (maxHeap->size == MAX_SIZE) {\n printf(\"heap is full!\");\n return;\n }\n // \u6dfb\u52a0\u8282\u70b9\n maxHeap->data[maxHeap->size] = val;\n maxHeap->size++;\n\n // \u4ece\u5e95\u81f3\u9876\u5806\u5316\n siftUp(maxHeap, maxHeap->size - 1);\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nvoid siftUp(MaxHeap *maxHeap, int i) {\n while (true) {\n // \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\n int p = parent(maxHeap, i);\n // \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u987b\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\n if (p < 0 || maxHeap->data[i] <= maxHeap->data[p]) {\n break;\n }\n // \u4ea4\u6362\u4e24\u8282\u70b9\n swap(maxHeap, i, p);\n // \u5faa\u73af\u5411\u4e0a\u5806\u5316\n i = p;\n }\n}\n
my_heap.zig// \u5143\u7d20\u5165\u5806\nfn push(self: *Self, val: T) !void {\n // \u6dfb\u52a0\u8282\u70b9\n try self.max_heap.?.append(val);\n // \u4ece\u5e95\u81f3\u9876\u5806\u5316\n try self.siftUp(self.size() - 1);\n} \n\n// \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316\nfn siftUp(self: *Self, i_: usize) !void {\n var i = i_;\n while (true) {\n // \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\n var p = parent(i);\n // \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u987b\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\n if (p < 0 or self.max_heap.?.items[i] <= self.max_heap.?.items[p]) break;\n // \u4ea4\u6362\u4e24\u8282\u70b9\n try self.swap(i, p);\n // \u5faa\u73af\u5411\u4e0a\u5806\u5316\n i = p;\n }\n}\n
"},{"location":"chapter_heap/heap/#4","title":"4. \u00a0 \u5806\u9876\u5143\u7d20\u51fa\u5806","text":"\u5806\u9876\u5143\u7d20\u662f\u4e8c\u53c9\u6811\u7684\u6839\u8282\u70b9\uff0c\u5373\u5217\u8868\u9996\u5143\u7d20\u3002\u5982\u679c\u6211\u4eec\u76f4\u63a5\u4ece\u5217\u8868\u4e2d\u5220\u9664\u9996\u5143\u7d20\uff0c\u90a3\u4e48\u4e8c\u53c9\u6811\u4e2d\u6240\u6709\u8282\u70b9\u7684\u7d22\u5f15\u90fd\u4f1a\u53d1\u751f\u53d8\u5316\uff0c\u8fd9\u5c06\u4f7f\u5f97\u540e\u7eed\u4f7f\u7528\u5806\u5316\u8fdb\u884c\u4fee\u590d\u53d8\u5f97\u56f0\u96be\u3002\u4e3a\u4e86\u5c3d\u91cf\u51cf\u5c11\u5143\u7d20\u7d22\u5f15\u7684\u53d8\u52a8\uff0c\u6211\u4eec\u91c7\u7528\u4ee5\u4e0b\u64cd\u4f5c\u6b65\u9aa4\u3002
\u5982\u56fe 8-4 \u6240\u793a\uff0c\u201c\u4ece\u9876\u81f3\u5e95\u5806\u5316\u201d\u7684\u64cd\u4f5c\u65b9\u5411\u4e0e\u201c\u4ece\u5e95\u81f3\u9876\u5806\u5316\u201d\u76f8\u53cd\uff0c\u6211\u4eec\u5c06\u6839\u8282\u70b9\u7684\u503c\u4e0e\u5176\u4e24\u4e2a\u5b50\u8282\u70b9\u7684\u503c\u8fdb\u884c\u6bd4\u8f83\uff0c\u5c06\u6700\u5927\u7684\u5b50\u8282\u70b9\u4e0e\u6839\u8282\u70b9\u4ea4\u6362\u3002\u7136\u540e\u5faa\u73af\u6267\u884c\u6b64\u64cd\u4f5c\uff0c\u76f4\u5230\u8d8a\u8fc7\u53f6\u8282\u70b9\u6216\u9047\u5230\u65e0\u987b\u4ea4\u6362\u7684\u8282\u70b9\u65f6\u7ed3\u675f\u3002
<1><2><3><4><5><6><7><8><9><10>\u56fe 8-4 \u00a0 \u5806\u9876\u5143\u7d20\u51fa\u5806\u6b65\u9aa4
\u4e0e\u5143\u7d20\u5165\u5806\u64cd\u4f5c\u76f8\u4f3c\uff0c\u5806\u9876\u5143\u7d20\u51fa\u5806\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e5f\u4e3a \\(O(\\log n)\\) \u3002\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig my_heap.pydef pop(self) -> int:\n \"\"\"\u5143\u7d20\u51fa\u5806\"\"\"\n # \u5224\u7a7a\u5904\u7406\n if self.is_empty():\n raise IndexError(\"\u5806\u4e3a\u7a7a\")\n # \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n self.swap(0, self.size() - 1)\n # \u5220\u9664\u8282\u70b9\n val = self.max_heap.pop()\n # \u4ece\u9876\u81f3\u5e95\u5806\u5316\n self.sift_down(0)\n # \u8fd4\u56de\u5806\u9876\u5143\u7d20\n return val\n\ndef sift_down(self, i: int):\n \"\"\"\u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316\"\"\"\n while True:\n # \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n l, r, ma = self.left(i), self.right(i), i\n if l < self.size() and self.max_heap[l] > self.max_heap[ma]:\n ma = l\n if r < self.size() and self.max_heap[r] > self.max_heap[ma]:\n ma = r\n # \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n if ma == i:\n break\n # \u4ea4\u6362\u4e24\u8282\u70b9\n self.swap(i, ma)\n # \u5faa\u73af\u5411\u4e0b\u5806\u5316\n i = ma\n
my_heap.cpp/* \u5143\u7d20\u51fa\u5806 */\nvoid pop() {\n // \u5224\u7a7a\u5904\u7406\n if (isEmpty()) {\n throw out_of_range(\"\u5806\u4e3a\u7a7a\");\n }\n // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n swap(maxHeap[0], maxHeap[size() - 1]);\n // \u5220\u9664\u8282\u70b9\n maxHeap.pop_back();\n // \u4ece\u9876\u81f3\u5e95\u5806\u5316\n siftDown(0);\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nvoid siftDown(int i) {\n while (true) {\n // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n int l = left(i), r = right(i), ma = i;\n if (l < size() && maxHeap[l] > maxHeap[ma])\n ma = l;\n if (r < size() && maxHeap[r] > maxHeap[ma])\n ma = r;\n // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n if (ma == i)\n break;\n swap(maxHeap[i], maxHeap[ma]);\n // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n i = ma;\n }\n}\n
my_heap.java/* \u5143\u7d20\u51fa\u5806 */\nint pop() {\n // \u5224\u7a7a\u5904\u7406\n if (isEmpty())\n throw new IndexOutOfBoundsException();\n // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n swap(0, size() - 1);\n // \u5220\u9664\u8282\u70b9\n int val = maxHeap.remove(size() - 1);\n // \u4ece\u9876\u81f3\u5e95\u5806\u5316\n siftDown(0);\n // \u8fd4\u56de\u5806\u9876\u5143\u7d20\n return val;\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nvoid siftDown(int i) {\n while (true) {\n // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n int l = left(i), r = right(i), ma = i;\n if (l < size() && maxHeap.get(l) > maxHeap.get(ma))\n ma = l;\n if (r < size() && maxHeap.get(r) > maxHeap.get(ma))\n ma = r;\n // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n if (ma == i)\n break;\n // \u4ea4\u6362\u4e24\u8282\u70b9\n swap(i, ma);\n // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n i = ma;\n }\n}\n
my_heap.cs/* \u5143\u7d20\u51fa\u5806 */\nint Pop() {\n // \u5224\u7a7a\u5904\u7406\n if (IsEmpty())\n throw new IndexOutOfRangeException();\n // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n Swap(0, Size() - 1);\n // \u5220\u9664\u8282\u70b9\n int val = maxHeap.Last();\n maxHeap.RemoveAt(Size() - 1);\n // \u4ece\u9876\u81f3\u5e95\u5806\u5316\n SiftDown(0);\n // \u8fd4\u56de\u5806\u9876\u5143\u7d20\n return val;\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nvoid SiftDown(int i) {\n while (true) {\n // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n int l = Left(i), r = Right(i), ma = i;\n if (l < Size() && maxHeap[l] > maxHeap[ma])\n ma = l;\n if (r < Size() && maxHeap[r] > maxHeap[ma])\n ma = r;\n // \u82e5\u201c\u8282\u70b9 i \u6700\u5927\u201d\u6216\u201c\u8d8a\u8fc7\u53f6\u8282\u70b9\u201d\uff0c\u5219\u7ed3\u675f\u5806\u5316\n if (ma == i) break;\n // \u4ea4\u6362\u4e24\u8282\u70b9\n Swap(i, ma);\n // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n i = ma;\n }\n}\n
my_heap.go/* \u5143\u7d20\u51fa\u5806 */\nfunc (h *maxHeap) pop() any {\n // \u5224\u7a7a\u5904\u7406\n if h.isEmpty() {\n fmt.Println(\"error\")\n return nil\n }\n // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n h.swap(0, h.size()-1)\n // \u5220\u9664\u8282\u70b9\n val := h.data[len(h.data)-1]\n h.data = h.data[:len(h.data)-1]\n // \u4ece\u9876\u81f3\u5e95\u5806\u5316\n h.siftDown(0)\n\n // \u8fd4\u56de\u5806\u9876\u5143\u7d20\n return val\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nfunc (h *maxHeap) siftDown(i int) {\n for true {\n // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a max\n l, r, max := h.left(i), h.right(i), i\n if l < h.size() && h.data[l].(int) > h.data[max].(int) {\n max = l\n }\n if r < h.size() && h.data[r].(int) > h.data[max].(int) {\n max = r\n }\n // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n if max == i {\n break\n }\n // \u4ea4\u6362\u4e24\u8282\u70b9\n h.swap(i, max)\n // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n i = max\n }\n}\n
my_heap.swift/* \u5143\u7d20\u51fa\u5806 */\nfunc pop() -> Int {\n // \u5224\u7a7a\u5904\u7406\n if isEmpty() {\n fatalError(\"\u5806\u4e3a\u7a7a\")\n }\n // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n swap(i: 0, j: size() - 1)\n // \u5220\u9664\u8282\u70b9\n let val = maxHeap.remove(at: size() - 1)\n // \u4ece\u9876\u81f3\u5e95\u5806\u5316\n siftDown(i: 0)\n // \u8fd4\u56de\u5806\u9876\u5143\u7d20\n return val\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nfunc siftDown(i: Int) {\n var i = i\n while true {\n // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n let l = left(i: i)\n let r = right(i: i)\n var ma = i\n if l < size(), maxHeap[l] > maxHeap[ma] {\n ma = l\n }\n if r < size(), maxHeap[r] > maxHeap[ma] {\n ma = r\n }\n // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n if ma == i {\n break\n }\n // \u4ea4\u6362\u4e24\u8282\u70b9\n swap(i: i, j: ma)\n // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n i = ma\n }\n}\n
my_heap.js/* \u5143\u7d20\u51fa\u5806 */\npop() {\n // \u5224\u7a7a\u5904\u7406\n if (this.isEmpty()) throw new Error('\u5806\u4e3a\u7a7a');\n // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n this.#swap(0, this.size() - 1);\n // \u5220\u9664\u8282\u70b9\n const val = this.#maxHeap.pop();\n // \u4ece\u9876\u81f3\u5e95\u5806\u5316\n this.#siftDown(0);\n // \u8fd4\u56de\u5806\u9876\u5143\u7d20\n return val;\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\n#siftDown(i) {\n while (true) {\n // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n const l = this.#left(i),\n r = this.#right(i);\n let ma = i;\n if (l < this.size() && this.#maxHeap[l] > this.#maxHeap[ma]) ma = l;\n if (r < this.size() && this.#maxHeap[r] > this.#maxHeap[ma]) ma = r;\n // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n if (ma === i) break;\n // \u4ea4\u6362\u4e24\u8282\u70b9\n this.#swap(i, ma);\n // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n i = ma;\n }\n}\n
my_heap.ts/* \u5143\u7d20\u51fa\u5806 */\npop(): number {\n // \u5224\u7a7a\u5904\u7406\n if (this.isEmpty()) throw new RangeError('Heap is empty.');\n // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n this.swap(0, this.size() - 1);\n // \u5220\u9664\u8282\u70b9\n const val = this.maxHeap.pop();\n // \u4ece\u9876\u81f3\u5e95\u5806\u5316\n this.siftDown(0);\n // \u8fd4\u56de\u5806\u9876\u5143\u7d20\n return val;\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nsiftDown(i: number): void {\n while (true) {\n // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n const l = this.left(i),\n r = this.right(i);\n let ma = i;\n if (l < this.size() && this.maxHeap[l] > this.maxHeap[ma]) ma = l;\n if (r < this.size() && this.maxHeap[r] > this.maxHeap[ma]) ma = r;\n // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n if (ma === i) break;\n // \u4ea4\u6362\u4e24\u8282\u70b9\n this.swap(i, ma);\n // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n i = ma;\n }\n}\n
my_heap.dart/* \u5143\u7d20\u51fa\u5806 */\nint pop() {\n // \u5224\u7a7a\u5904\u7406\n if (isEmpty()) throw Exception('\u5806\u4e3a\u7a7a');\n // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n _swap(0, size() - 1);\n // \u5220\u9664\u8282\u70b9\n int val = _maxHeap.removeLast();\n // \u4ece\u9876\u81f3\u5e95\u5806\u5316\n siftDown(0);\n // \u8fd4\u56de\u5806\u9876\u5143\u7d20\n return val;\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nvoid siftDown(int i) {\n while (true) {\n // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n int l = _left(i);\n int r = _right(i);\n int ma = i;\n if (l < size() && _maxHeap[l] > _maxHeap[ma]) ma = l;\n if (r < size() && _maxHeap[r] > _maxHeap[ma]) ma = r;\n // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n if (ma == i) break;\n // \u4ea4\u6362\u4e24\u8282\u70b9\n _swap(i, ma);\n // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n i = ma;\n }\n}\n
my_heap.rs/* \u5143\u7d20\u51fa\u5806 */\nfn pop(&mut self) -> i32 {\n // \u5224\u7a7a\u5904\u7406\n if self.is_empty() {\n panic!(\"index out of bounds\");\n }\n // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n self.swap(0, self.size() - 1);\n // \u5220\u9664\u8282\u70b9\n let val = self.max_heap.remove(self.size() - 1);\n // \u4ece\u9876\u81f3\u5e95\u5806\u5316\n self.sift_down(0);\n // \u8fd4\u56de\u5806\u9876\u5143\u7d20\n val\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nfn sift_down(&mut self, mut i: usize) {\n loop {\n // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n let (l, r, mut ma) = (Self::left(i), Self::right(i), i);\n if l < self.size() && self.max_heap[l] > self.max_heap[ma] {\n ma = l;\n }\n if r < self.size() && self.max_heap[r] > self.max_heap[ma] {\n ma = r;\n }\n // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n if ma == i {\n break;\n }\n // \u4ea4\u6362\u4e24\u8282\u70b9\n self.swap(i, ma);\n // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n i = ma;\n }\n}\n
my_heap.c/* \u5143\u7d20\u51fa\u5806 */\nint pop(MaxHeap *maxHeap) {\n // \u5224\u7a7a\u5904\u7406\n if (isEmpty(maxHeap)) {\n printf(\"heap is empty!\");\n return INT_MAX;\n }\n // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n swap(maxHeap, 0, size(maxHeap) - 1);\n // \u5220\u9664\u8282\u70b9\n int val = maxHeap->data[maxHeap->size - 1];\n maxHeap->size--;\n // \u4ece\u9876\u81f3\u5e95\u5806\u5316\n siftDown(maxHeap, 0);\n\n // \u8fd4\u56de\u5806\u9876\u5143\u7d20\n return val;\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nvoid siftDown(MaxHeap *maxHeap, int i) {\n while (true) {\n // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a max\n int l = left(maxHeap, i);\n int r = right(maxHeap, i);\n int max = i;\n if (l < size(maxHeap) && maxHeap->data[l] > maxHeap->data[max]) {\n max = l;\n }\n if (r < size(maxHeap) && maxHeap->data[r] > maxHeap->data[max]) {\n max = r;\n }\n // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n if (max == i) {\n break;\n }\n // \u4ea4\u6362\u4e24\u8282\u70b9\n swap(maxHeap, i, max);\n // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n i = max;\n }\n}\n
my_heap.zig// \u5143\u7d20\u51fa\u5806\nfn pop(self: *Self) !T {\n // \u5224\u65ad\u5904\u7406\n if (self.isEmpty()) unreachable;\n // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n try self.swap(0, self.size() - 1);\n // \u5220\u9664\u8282\u70b9\n var val = self.max_heap.?.pop();\n // \u4ece\u9876\u81f3\u5e95\u5806\u5316\n try self.siftDown(0);\n // \u8fd4\u56de\u5806\u9876\u5143\u7d20\n return val;\n} \n\n// \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316\nfn siftDown(self: *Self, i_: usize) !void {\n var i = i_;\n while (true) {\n // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n var l = left(i);\n var r = right(i);\n var ma = i;\n if (l < self.size() and self.max_heap.?.items[l] > self.max_heap.?.items[ma]) ma = l;\n if (r < self.size() and self.max_heap.?.items[r] > self.max_heap.?.items[ma]) ma = r;\n // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n if (ma == i) break;\n // \u4ea4\u6362\u4e24\u8282\u70b9\n try self.swap(i, ma);\n // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n i = ma;\n }\n}\n
"},{"location":"chapter_heap/heap/#813","title":"8.1.3 \u00a0 \u5806\u5e38\u89c1\u5e94\u7528","text":"\u6570\u636e\u7ed3\u6784\u7684\u201c\u5806\u201d\u4e0e\u5185\u5b58\u7ba1\u7406\u7684\u201c\u5806\u201d\u662f\u540c\u4e00\u4e2a\u6982\u5ff5\u5417\uff1f
\u4e24\u8005\u4e0d\u662f\u540c\u4e00\u4e2a\u6982\u5ff5\uff0c\u53ea\u662f\u78b0\u5de7\u90fd\u53eb\u5806\u3002\u8ba1\u7b97\u673a\u7cfb\u7edf\u5185\u5b58\u4e2d\u7684\u5806\u662f\u52a8\u6001\u5185\u5b58\u5206\u914d\u7684\u4e00\u90e8\u5206\uff0c\u7a0b\u5e8f\u5728\u8fd0\u884c\u65f6\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u5b58\u50a8\u6570\u636e\u3002\u7a0b\u5e8f\u53ef\u4ee5\u8bf7\u6c42\u4e00\u5b9a\u91cf\u7684\u5806\u5185\u5b58\uff0c\u7528\u4e8e\u5b58\u50a8\u5982\u5bf9\u8c61\u548c\u6570\u7ec4\u7b49\u590d\u6742\u7ed3\u6784\u3002\u5f53\u8fd9\u4e9b\u6570\u636e\u4e0d\u518d\u9700\u8981\u65f6\uff0c\u7a0b\u5e8f\u9700\u8981\u91ca\u653e\u8fd9\u4e9b\u5185\u5b58\uff0c\u4ee5\u9632\u6b62\u5185\u5b58\u6cc4\u6f0f\u3002\u76f8\u8f83\u4e8e\u6808\u5185\u5b58\uff0c\u5806\u5185\u5b58\u7684\u7ba1\u7406\u548c\u4f7f\u7528\u9700\u8981\u66f4\u8c28\u614e\uff0c\u4f7f\u7528\u4e0d\u5f53\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\u548c\u91ce\u6307\u9488\u7b49\u95ee\u9898\u3002
"},{"location":"chapter_heap/top_k/","title":"8.3 \u00a0 Top-K \u95ee\u9898","text":"Question
\u7ed9\u5b9a\u4e00\u4e2a\u957f\u5ea6\u4e3a \\(n\\) \u7684\u65e0\u5e8f\u6570\u7ec4 nums
\uff0c\u8bf7\u8fd4\u56de\u6570\u7ec4\u4e2d\u524d \\(k\\) \u5927\u7684\u5143\u7d20\u3002
\u5bf9\u4e8e\u8be5\u95ee\u9898\uff0c\u6211\u4eec\u5148\u4ecb\u7ecd\u4e24\u79cd\u601d\u8def\u6bd4\u8f83\u76f4\u63a5\u7684\u89e3\u6cd5\uff0c\u518d\u4ecb\u7ecd\u6548\u7387\u66f4\u9ad8\u7684\u5806\u89e3\u6cd5\u3002
"},{"location":"chapter_heap/top_k/#831","title":"8.3.1 \u00a0 \u65b9\u6cd5\u4e00\uff1a\u904d\u5386\u9009\u62e9","text":"\u6211\u4eec\u53ef\u4ee5\u8fdb\u884c\u56fe 8-6 \u6240\u793a\u7684 \\(k\\) \u8f6e\u904d\u5386\uff0c\u5206\u522b\u5728\u6bcf\u8f6e\u4e2d\u63d0\u53d6\u7b2c \\(1\\)\u3001\\(2\\)\u3001\\(\\dots\\)\u3001\\(k\\) \u5927\u7684\u5143\u7d20\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(nk)\\) \u3002
\u6b64\u65b9\u6cd5\u53ea\u9002\u7528\u4e8e \\(k \\ll n\\) \u7684\u60c5\u51b5\uff0c\u56e0\u4e3a\u5f53 \\(k\\) \u4e0e \\(n\\) \u6bd4\u8f83\u63a5\u8fd1\u65f6\uff0c\u5176\u65f6\u95f4\u590d\u6742\u5ea6\u8d8b\u5411\u4e8e \\(O(n^2)\\) \uff0c\u975e\u5e38\u8017\u65f6\u3002
\u56fe 8-6 \u00a0 \u904d\u5386\u5bfb\u627e\u6700\u5927\u7684 k \u4e2a\u5143\u7d20
Tip
\u5f53 \\(k = n\\) \u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u5f97\u5230\u5b8c\u6574\u7684\u6709\u5e8f\u5e8f\u5217\uff0c\u6b64\u65f6\u7b49\u4ef7\u4e8e\u201c\u9009\u62e9\u6392\u5e8f\u201d\u7b97\u6cd5\u3002
"},{"location":"chapter_heap/top_k/#832","title":"8.3.2 \u00a0 \u65b9\u6cd5\u4e8c\uff1a\u6392\u5e8f","text":"\u5982\u56fe 8-7 \u6240\u793a\uff0c\u6211\u4eec\u53ef\u4ee5\u5148\u5bf9\u6570\u7ec4 nums
\u8fdb\u884c\u6392\u5e8f\uff0c\u518d\u8fd4\u56de\u6700\u53f3\u8fb9\u7684 \\(k\\) \u4e2a\u5143\u7d20\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n \\log n)\\) \u3002
\u663e\u7136\uff0c\u8be5\u65b9\u6cd5\u201c\u8d85\u989d\u201d\u5b8c\u6210\u4efb\u52a1\u4e86\uff0c\u56e0\u4e3a\u6211\u4eec\u53ea\u9700\u627e\u51fa\u6700\u5927\u7684 \\(k\\) \u4e2a\u5143\u7d20\u5373\u53ef\uff0c\u800c\u4e0d\u9700\u8981\u6392\u5e8f\u5176\u4ed6\u5143\u7d20\u3002
\u56fe 8-7 \u00a0 \u6392\u5e8f\u5bfb\u627e\u6700\u5927\u7684 k \u4e2a\u5143\u7d20
"},{"location":"chapter_heap/top_k/#833","title":"8.3.3 \u00a0 \u65b9\u6cd5\u4e09\uff1a\u5806","text":"\u6211\u4eec\u53ef\u4ee5\u57fa\u4e8e\u5806\u66f4\u52a0\u9ad8\u6548\u5730\u89e3\u51b3 Top-K \u95ee\u9898\uff0c\u6d41\u7a0b\u5982\u56fe 8-8 \u6240\u793a\u3002
\u56fe 8-8 \u00a0 \u57fa\u4e8e\u5806\u5bfb\u627e\u6700\u5927\u7684 k \u4e2a\u5143\u7d20
\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig top_k.pydef top_k_heap(nums: list[int], k: int) -> list[int]:\n \"\"\"\u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20\"\"\"\n # \u521d\u59cb\u5316\u5c0f\u9876\u5806\n heap = []\n # \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\n for i in range(k):\n heapq.heappush(heap, nums[i])\n # \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\n for i in range(k, len(nums)):\n # \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\n if nums[i] > heap[0]:\n heapq.heappop(heap)\n heapq.heappush(heap, nums[i])\n return heap\n
top_k.cpp/* \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20 */\npriority_queue<int, vector<int>, greater<int>> topKHeap(vector<int> &nums, int k) {\n // \u521d\u59cb\u5316\u5c0f\u9876\u5806\n priority_queue<int, vector<int>, greater<int>> heap;\n // \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\n for (int i = 0; i < k; i++) {\n heap.push(nums[i]);\n }\n // \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\n for (int i = k; i < nums.size(); i++) {\n // \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\n if (nums[i] > heap.top()) {\n heap.pop();\n heap.push(nums[i]);\n }\n }\n return heap;\n}\n
top_k.java/* \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20 */\nQueue<Integer> topKHeap(int[] nums, int k) {\n // \u521d\u59cb\u5316\u5c0f\u9876\u5806\n Queue<Integer> heap = new PriorityQueue<Integer>();\n // \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\n for (int i = 0; i < k; i++) {\n heap.offer(nums[i]);\n }\n // \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\n for (int i = k; i < nums.length; i++) {\n // \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\n if (nums[i] > heap.peek()) {\n heap.poll();\n heap.offer(nums[i]);\n }\n }\n return heap;\n}\n
top_k.cs/* \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20 */\nPriorityQueue<int, int> TopKHeap(int[] nums, int k) {\n // \u521d\u59cb\u5316\u5c0f\u9876\u5806\n PriorityQueue<int, int> heap = new();\n // \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\n for (int i = 0; i < k; i++) {\n heap.Enqueue(nums[i], nums[i]);\n }\n // \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\n for (int i = k; i < nums.Length; i++) {\n // \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\n if (nums[i] > heap.Peek()) {\n heap.Dequeue();\n heap.Enqueue(nums[i], nums[i]);\n }\n }\n return heap;\n}\n
top_k.go/* \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20 */\nfunc topKHeap(nums []int, k int) *minHeap {\n // \u521d\u59cb\u5316\u5c0f\u9876\u5806\n h := &minHeap{}\n heap.Init(h)\n // \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\n for i := 0; i < k; i++ {\n heap.Push(h, nums[i])\n }\n // \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\n for i := k; i < len(nums); i++ {\n // \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\n if nums[i] > h.Top().(int) {\n heap.Pop(h)\n heap.Push(h, nums[i])\n }\n }\n return h\n}\n
top_k.swift/* \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20 */\nfunc topKHeap(nums: [Int], k: Int) -> [Int] {\n // \u521d\u59cb\u5316\u4e00\u4e2a\u5c0f\u9876\u5806\uff0c\u5e76\u5c06\u524d k \u4e2a\u5143\u7d20\u5efa\u5806\n var heap = Heap(nums.prefix(k))\n // \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\n for i in stride(from: k, to: nums.count, by: 1) {\n // \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\n if nums[i] > heap.min()! {\n _ = heap.removeMin()\n heap.insert(nums[i])\n }\n }\n return heap.unordered\n}\n
top_k.js/* \u5143\u7d20\u5165\u5806 */\nfunction pushMinHeap(maxHeap, val) {\n // \u5143\u7d20\u53d6\u53cd\n maxHeap.push(-val);\n}\n\n/* \u5143\u7d20\u51fa\u5806 */\nfunction popMinHeap(maxHeap) {\n // \u5143\u7d20\u53d6\u53cd\n return -maxHeap.pop();\n}\n\n/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nfunction peekMinHeap(maxHeap) {\n // \u5143\u7d20\u53d6\u53cd\n return -maxHeap.peek();\n}\n\n/* \u53d6\u51fa\u5806\u4e2d\u5143\u7d20 */\nfunction getMinHeap(maxHeap) {\n // \u5143\u7d20\u53d6\u53cd\n return maxHeap.getMaxHeap().map((num) => -num);\n}\n\n/* \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20 */\nfunction topKHeap(nums, k) {\n // \u521d\u59cb\u5316\u5c0f\u9876\u5806\n // \u8bf7\u6ce8\u610f\uff1a\u6211\u4eec\u5c06\u5806\u4e2d\u6240\u6709\u5143\u7d20\u53d6\u53cd\uff0c\u4ece\u800c\u7528\u5927\u9876\u5806\u6765\u6a21\u62df\u5c0f\u9876\u5806\n const maxHeap = new MaxHeap([]);\n // \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\n for (let i = 0; i < k; i++) {\n pushMinHeap(maxHeap, nums[i]);\n }\n // \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\n for (let i = k; i < nums.length; i++) {\n // \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\n if (nums[i] > peekMinHeap(maxHeap)) {\n popMinHeap(maxHeap);\n pushMinHeap(maxHeap, nums[i]);\n }\n }\n // \u8fd4\u56de\u5806\u4e2d\u5143\u7d20\n return getMinHeap(maxHeap);\n}\n
top_k.ts/* \u5143\u7d20\u5165\u5806 */\nfunction pushMinHeap(maxHeap: MaxHeap, val: number): void {\n // \u5143\u7d20\u53d6\u53cd\n maxHeap.push(-val);\n}\n\n/* \u5143\u7d20\u51fa\u5806 */\nfunction popMinHeap(maxHeap: MaxHeap): number {\n // \u5143\u7d20\u53d6\u53cd\n return -maxHeap.pop();\n}\n\n/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nfunction peekMinHeap(maxHeap: MaxHeap): number {\n // \u5143\u7d20\u53d6\u53cd\n return -maxHeap.peek();\n}\n\n/* \u53d6\u51fa\u5806\u4e2d\u5143\u7d20 */\nfunction getMinHeap(maxHeap: MaxHeap): number[] {\n // \u5143\u7d20\u53d6\u53cd\n return maxHeap.getMaxHeap().map((num: number) => -num);\n}\n\n/* \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20 */\nfunction topKHeap(nums: number[], k: number): number[] {\n // \u521d\u59cb\u5316\u5c0f\u9876\u5806\n // \u8bf7\u6ce8\u610f\uff1a\u6211\u4eec\u5c06\u5806\u4e2d\u6240\u6709\u5143\u7d20\u53d6\u53cd\uff0c\u4ece\u800c\u7528\u5927\u9876\u5806\u6765\u6a21\u62df\u5c0f\u9876\u5806\n const maxHeap = new MaxHeap([]);\n // \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\n for (let i = 0; i < k; i++) {\n pushMinHeap(maxHeap, nums[i]);\n }\n // \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\n for (let i = k; i < nums.length; i++) {\n // \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\n if (nums[i] > peekMinHeap(maxHeap)) {\n popMinHeap(maxHeap);\n pushMinHeap(maxHeap, nums[i]);\n }\n }\n // \u8fd4\u56de\u5806\u4e2d\u5143\u7d20\n return getMinHeap(maxHeap);\n}\n
top_k.dart/* \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20 */\nMinHeap topKHeap(List<int> nums, int k) {\n // \u521d\u59cb\u5316\u5c0f\u9876\u5806\uff0c\u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\n MinHeap heap = MinHeap(nums.sublist(0, k));\n // \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\n for (int i = k; i < nums.length; i++) {\n // \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\n if (nums[i] > heap.peek()) {\n heap.pop();\n heap.push(nums[i]);\n }\n }\n return heap;\n}\n
top_k.rs/* \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20 */\nfn top_k_heap(nums: Vec<i32>, k: usize) -> BinaryHeap<Reverse<i32>> {\n // BinaryHeap \u662f\u5927\u9876\u5806\uff0c\u4f7f\u7528 Reverse \u5c06\u5143\u7d20\u53d6\u53cd\uff0c\u4ece\u800c\u5b9e\u73b0\u5c0f\u9876\u5806\n let mut heap = BinaryHeap::<Reverse<i32>>::new();\n // \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\n for &num in nums.iter().take(k) {\n heap.push(Reverse(num));\n }\n // \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\n for &num in nums.iter().skip(k) {\n // \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\n if num > heap.peek().unwrap().0 {\n heap.pop();\n heap.push(Reverse(num));\n }\n }\n heap\n}\n
top_k.c/* \u5143\u7d20\u5165\u5806 */\nvoid pushMinHeap(MaxHeap *maxHeap, int val) {\n // \u5143\u7d20\u53d6\u53cd\n push(maxHeap, -val);\n}\n\n/* \u5143\u7d20\u51fa\u5806 */\nint popMinHeap(MaxHeap *maxHeap) {\n // \u5143\u7d20\u53d6\u53cd\n return -pop(maxHeap);\n}\n\n/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nint peekMinHeap(MaxHeap *maxHeap) {\n // \u5143\u7d20\u53d6\u53cd\n return -peek(maxHeap);\n}\n\n/* \u53d6\u51fa\u5806\u4e2d\u5143\u7d20 */\nint *getMinHeap(MaxHeap *maxHeap) {\n // \u5c06\u5806\u4e2d\u6240\u6709\u5143\u7d20\u53d6\u53cd\u5e76\u5b58\u5165 res \u6570\u7ec4\n int *res = (int *)malloc(maxHeap->size * sizeof(int));\n for (int i = 0; i < maxHeap->size; i++) {\n res[i] = -maxHeap->data[i];\n }\n return res;\n}\n\n/* \u53d6\u51fa\u5806\u4e2d\u5143\u7d20 */\nint *getMinHeap(MaxHeap *maxHeap) {\n // \u5c06\u5806\u4e2d\u6240\u6709\u5143\u7d20\u53d6\u53cd\u5e76\u5b58\u5165 res \u6570\u7ec4\n int *res = (int *)malloc(maxHeap->size * sizeof(int));\n for (int i = 0; i < maxHeap->size; i++) {\n res[i] = -maxHeap->data[i];\n }\n return res;\n}\n\n// \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20\u7684\u51fd\u6570\nint *topKHeap(int *nums, int sizeNums, int k) {\n // \u521d\u59cb\u5316\u5c0f\u9876\u5806\n // \u8bf7\u6ce8\u610f\uff1a\u6211\u4eec\u5c06\u5806\u4e2d\u6240\u6709\u5143\u7d20\u53d6\u53cd\uff0c\u4ece\u800c\u7528\u5927\u9876\u5806\u6765\u6a21\u62df\u5c0f\u9876\u5806\n int *empty = (int *)malloc(0);\n MaxHeap *maxHeap = newMaxHeap(empty, 0);\n // \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\n for (int i = 0; i < k; i++) {\n pushMinHeap(maxHeap, nums[i]);\n }\n // \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\n for (int i = k; i < sizeNums; i++) {\n // \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\n if (nums[i] > peekMinHeap(maxHeap)) {\n popMinHeap(maxHeap);\n pushMinHeap(maxHeap, nums[i]);\n }\n }\n int *res = getMinHeap(maxHeap);\n // \u91ca\u653e\u5185\u5b58\n delMaxHeap(maxHeap);\n return res;\n}\n
top_k.zig[class]{}-[func]{topKHeap}\n
\u603b\u5171\u6267\u884c\u4e86 \\(n\\) \u8f6e\u5165\u5806\u548c\u51fa\u5806\uff0c\u5806\u7684\u6700\u5927\u957f\u5ea6\u4e3a \\(k\\) \uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n \\log k)\\) \u3002\u8be5\u65b9\u6cd5\u7684\u6548\u7387\u5f88\u9ad8\uff0c\u5f53 \\(k\\) \u8f83\u5c0f\u65f6\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u8d8b\u5411 \\(O(n)\\) \uff1b\u5f53 \\(k\\) \u8f83\u5927\u65f6\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e0d\u4f1a\u8d85\u8fc7 \\(O(n \\log n)\\) \u3002
\u53e6\u5916\uff0c\u8be5\u65b9\u6cd5\u9002\u7528\u4e8e\u52a8\u6001\u6570\u636e\u6d41\u7684\u4f7f\u7528\u573a\u666f\u3002\u5728\u4e0d\u65ad\u52a0\u5165\u6570\u636e\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u6301\u7eed\u7ef4\u62a4\u5806\u5185\u7684\u5143\u7d20\uff0c\u4ece\u800c\u5b9e\u73b0\u6700\u5927 \\(k\\) \u4e2a\u5143\u7d20\u7684\u52a8\u6001\u66f4\u65b0\u3002
"},{"location":"chapter_introduction/","title":"\u7b2c 1 \u7ae0 \u00a0 \u521d\u8bc6\u7b97\u6cd5","text":"Abstract
\u4e00\u4f4d\u5c11\u5973\u7fe9\u7fe9\u8d77\u821e\uff0c\u4e0e\u6570\u636e\u4ea4\u7ec7\u5728\u4e00\u8d77\uff0c\u88d9\u6446\u4e0a\u98d8\u626c\u7740\u7b97\u6cd5\u7684\u65cb\u5f8b\u3002
\u5979\u9080\u8bf7\u4f60\u5171\u821e\uff0c\u8bf7\u7d27\u8ddf\u5979\u7684\u6b65\u4f10\uff0c\u8e0f\u5165\u5145\u6ee1\u903b\u8f91\u4e0e\u7f8e\u611f\u7684\u7b97\u6cd5\u4e16\u754c\u3002
"},{"location":"chapter_introduction/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"\u5f53\u6211\u4eec\u542c\u5230\u201c\u7b97\u6cd5\u201d\u8fd9\u4e2a\u8bcd\u65f6\uff0c\u5f88\u81ea\u7136\u5730\u4f1a\u60f3\u5230\u6570\u5b66\u3002\u7136\u800c\u5b9e\u9645\u4e0a\uff0c\u8bb8\u591a\u7b97\u6cd5\u5e76\u4e0d\u6d89\u53ca\u590d\u6742\u6570\u5b66\uff0c\u800c\u662f\u66f4\u591a\u5730\u4f9d\u8d56\u57fa\u672c\u903b\u8f91\uff0c\u8fd9\u4e9b\u903b\u8f91\u5728\u6211\u4eec\u7684\u65e5\u5e38\u751f\u6d3b\u4e2d\u5904\u5904\u53ef\u89c1\u3002
\u5728\u6b63\u5f0f\u63a2\u8ba8\u7b97\u6cd5\u4e4b\u524d\uff0c\u6709\u4e00\u4e2a\u6709\u8da3\u7684\u4e8b\u5b9e\u503c\u5f97\u5206\u4eab\uff1a\u4f60\u5df2\u7ecf\u5728\u4e0d\u77e5\u4e0d\u89c9\u4e2d\u5b66\u4f1a\u4e86\u8bb8\u591a\u7b97\u6cd5\uff0c\u5e76\u4e60\u60ef\u5c06\u5b83\u4eec\u5e94\u7528\u5230\u65e5\u5e38\u751f\u6d3b\u4e2d\u4e86\u3002\u4e0b\u9762\u6211\u5c06\u4e3e\u51e0\u4e2a\u5177\u4f53\u7684\u4f8b\u5b50\u6765\u8bc1\u5b9e\u8fd9\u4e00\u70b9\u3002
\u4f8b\u4e00\uff1a\u67e5\u9605\u5b57\u5178\u3002\u5728\u5b57\u5178\u91cc\uff0c\u6bcf\u4e2a\u6c49\u5b57\u90fd\u5bf9\u5e94\u4e00\u4e2a\u62fc\u97f3\uff0c\u800c\u5b57\u5178\u662f\u6309\u7167\u62fc\u97f3\u5b57\u6bcd\u987a\u5e8f\u6392\u5217\u7684\u3002\u5047\u8bbe\u6211\u4eec\u9700\u8981\u67e5\u627e\u4e00\u4e2a\u62fc\u97f3\u9996\u5b57\u6bcd\u4e3a \\(r\\) \u7684\u5b57\uff0c\u901a\u5e38\u4f1a\u6309\u7167\u56fe 1-1 \u6240\u793a\u7684\u65b9\u5f0f\u5b9e\u73b0\u3002
1.
\u548c \u6b65\u9aa4 2.
\uff0c\u76f4\u81f3\u627e\u5230\u62fc\u97f3\u9996\u5b57\u6bcd\u4e3a \\(r\\) \u7684\u9875\u7801\u4e3a\u6b62\u3002\u56fe 1-1 \u00a0 \u67e5\u5b57\u5178\u6b65\u9aa4
\u67e5\u5b57\u5178\u8fd9\u4e2a\u5c0f\u5b66\u751f\u5fc5\u5907\u6280\u80fd\uff0c\u5b9e\u9645\u4e0a\u5c31\u662f\u8457\u540d\u7684\u201c\u4e8c\u5206\u67e5\u627e\u201d\u7b97\u6cd5\u3002\u4ece\u6570\u636e\u7ed3\u6784\u7684\u89d2\u5ea6\uff0c\u6211\u4eec\u53ef\u4ee5\u628a\u5b57\u5178\u89c6\u4e3a\u4e00\u4e2a\u5df2\u6392\u5e8f\u7684\u201c\u6570\u7ec4\u201d\uff1b\u4ece\u7b97\u6cd5\u7684\u89d2\u5ea6\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u4e0a\u8ff0\u67e5\u5b57\u5178\u7684\u4e00\u7cfb\u5217\u64cd\u4f5c\u770b\u4f5c\u201c\u4e8c\u5206\u67e5\u627e\u201d\u3002
\u4f8b\u4e8c\uff1a\u6574\u7406\u6251\u514b\u3002\u6211\u4eec\u5728\u6253\u724c\u65f6\uff0c\u6bcf\u5c40\u90fd\u9700\u8981\u6574\u7406\u624b\u4e2d\u7684\u6251\u514b\u724c\uff0c\u4f7f\u5176\u4ece\u5c0f\u5230\u5927\u6392\u5217\uff0c\u5b9e\u73b0\u6d41\u7a0b\u5982\u56fe 1-2 \u6240\u793a\u3002
2.
\uff0c\u6bcf\u4e00\u8f6e\u5c06\u4e00\u5f20\u6251\u514b\u724c\u4ece\u65e0\u5e8f\u90e8\u5206\u63d2\u5165\u81f3\u6709\u5e8f\u90e8\u5206\uff0c\u76f4\u81f3\u6240\u6709\u6251\u514b\u724c\u90fd\u6709\u5e8f\u3002\u56fe 1-2 \u00a0 \u6251\u514b\u6392\u5e8f\u6b65\u9aa4
\u4e0a\u8ff0\u6574\u7406\u6251\u514b\u724c\u7684\u65b9\u6cd5\u672c\u8d28\u4e0a\u662f\u201c\u63d2\u5165\u6392\u5e8f\u201d\u7b97\u6cd5\uff0c\u5b83\u5728\u5904\u7406\u5c0f\u578b\u6570\u636e\u96c6\u65f6\u975e\u5e38\u9ad8\u6548\u3002\u8bb8\u591a\u7f16\u7a0b\u8bed\u8a00\u7684\u6392\u5e8f\u5e93\u51fd\u6570\u4e2d\u90fd\u6709\u63d2\u5165\u6392\u5e8f\u7684\u8eab\u5f71\u3002
\u4f8b\u4e09\uff1a\u8d27\u5e01\u627e\u96f6\u3002\u5047\u8bbe\u6211\u4eec\u5728\u8d85\u5e02\u8d2d\u4e70\u4e86 \\(69\\) \u5143\u7684\u5546\u54c1\uff0c\u7ed9\u4e86\u6536\u94f6\u5458 \\(100\\) \u5143\uff0c\u5219\u6536\u94f6\u5458\u9700\u8981\u627e\u6211\u4eec \\(31\\) \u5143\u3002\u4ed6\u4f1a\u5f88\u81ea\u7136\u5730\u5b8c\u6210\u5982\u56fe 1-3 \u6240\u793a\u7684\u601d\u8003\u3002
\u56fe 1-3 \u00a0 \u8d27\u5e01\u627e\u96f6\u8fc7\u7a0b
\u5728\u4ee5\u4e0a\u6b65\u9aa4\u4e2d\uff0c\u6211\u4eec\u6bcf\u4e00\u6b65\u90fd\u91c7\u53d6\u5f53\u524d\u770b\u6765\u6700\u597d\u7684\u9009\u62e9\uff08\u5c3d\u53ef\u80fd\u7528\u5927\u9762\u989d\u7684\u8d27\u5e01\uff09\uff0c\u6700\u7ec8\u5f97\u5230\u4e86\u53ef\u884c\u7684\u627e\u96f6\u65b9\u6848\u3002\u4ece\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7684\u89d2\u5ea6\u770b\uff0c\u8fd9\u79cd\u65b9\u6cd5\u672c\u8d28\u4e0a\u662f\u201c\u8d2a\u5fc3\u201d\u7b97\u6cd5\u3002
\u5c0f\u5230\u70f9\u996a\u4e00\u9053\u83dc\uff0c\u5927\u5230\u661f\u9645\u822a\u884c\uff0c\u51e0\u4e4e\u6240\u6709\u95ee\u9898\u7684\u89e3\u51b3\u90fd\u79bb\u4e0d\u5f00\u7b97\u6cd5\u3002\u8ba1\u7b97\u673a\u7684\u51fa\u73b0\u4f7f\u5f97\u6211\u4eec\u80fd\u591f\u901a\u8fc7\u7f16\u7a0b\u5c06\u6570\u636e\u7ed3\u6784\u5b58\u50a8\u5728\u5185\u5b58\u4e2d\uff0c\u540c\u65f6\u7f16\u5199\u4ee3\u7801\u8c03\u7528 CPU \u548c GPU \u6267\u884c\u7b97\u6cd5\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u6211\u4eec\u5c31\u80fd\u628a\u751f\u6d3b\u4e2d\u7684\u95ee\u9898\u8f6c\u79fb\u5230\u8ba1\u7b97\u673a\u4e0a\uff0c\u4ee5\u66f4\u9ad8\u6548\u7684\u65b9\u5f0f\u89e3\u51b3\u5404\u79cd\u590d\u6742\u95ee\u9898\u3002
Tip
\u5982\u679c\u4f60\u5bf9\u6570\u636e\u7ed3\u6784\u3001\u7b97\u6cd5\u3001\u6570\u7ec4\u548c\u4e8c\u5206\u67e5\u627e\u7b49\u6982\u5ff5\u4ecd\u611f\u5230\u4e00\u77e5\u534a\u89e3\uff0c\u8bf7\u7ee7\u7eed\u5f80\u4e0b\u9605\u8bfb\uff0c\u672c\u4e66\u5c06\u5f15\u5bfc\u4f60\u8fc8\u5165\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7684\u77e5\u8bc6\u6bbf\u5802\u3002
"},{"location":"chapter_introduction/summary/","title":"1.3 \u00a0 \u5c0f\u7ed3","text":"\u300c\u7b97\u6cd5 algorithm\u300d\u662f\u5728\u6709\u9650\u65f6\u95f4\u5185\u89e3\u51b3\u7279\u5b9a\u95ee\u9898\u7684\u4e00\u7ec4\u6307\u4ee4\u6216\u64cd\u4f5c\u6b65\u9aa4\uff0c\u5b83\u5177\u6709\u4ee5\u4e0b\u7279\u6027\u3002
\u300c\u6570\u636e\u7ed3\u6784 data structure\u300d\u662f\u8ba1\u7b97\u673a\u4e2d\u7ec4\u7ec7\u548c\u5b58\u50a8\u6570\u636e\u7684\u65b9\u5f0f\uff0c\u5177\u6709\u4ee5\u4e0b\u8bbe\u8ba1\u76ee\u6807\u3002
\u6570\u636e\u7ed3\u6784\u8bbe\u8ba1\u662f\u4e00\u4e2a\u5145\u6ee1\u6743\u8861\u7684\u8fc7\u7a0b\u3002\u5982\u679c\u60f3\u5728\u67d0\u65b9\u9762\u53d6\u5f97\u63d0\u5347\uff0c\u5f80\u5f80\u9700\u8981\u5728\u53e6\u4e00\u65b9\u9762\u4f5c\u51fa\u59a5\u534f\u3002\u4e0b\u9762\u4e3e\u4e24\u4e2a\u4f8b\u5b50\u3002
\u5982\u56fe 1-4 \u6240\u793a\uff0c\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u9ad8\u5ea6\u76f8\u5173\u3001\u7d27\u5bc6\u7ed3\u5408\uff0c\u5177\u4f53\u8868\u73b0\u5728\u4ee5\u4e0b\u4e09\u4e2a\u65b9\u9762\u3002
\u56fe 1-4 \u00a0 \u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7684\u5173\u7cfb
\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u72b9\u5982\u56fe 1-5 \u6240\u793a\u7684\u62fc\u88c5\u79ef\u6728\u3002\u4e00\u5957\u79ef\u6728\uff0c\u9664\u4e86\u5305\u542b\u8bb8\u591a\u96f6\u4ef6\u4e4b\u5916\uff0c\u8fd8\u9644\u6709\u8be6\u7ec6\u7684\u7ec4\u88c5\u8bf4\u660e\u4e66\u3002\u6211\u4eec\u6309\u7167\u8bf4\u660e\u4e66\u4e00\u6b65\u6b65\u64cd\u4f5c\uff0c\u5c31\u80fd\u7ec4\u88c5\u51fa\u7cbe\u7f8e\u7684\u79ef\u6728\u6a21\u578b\u3002
\u56fe 1-5 \u00a0 \u62fc\u88c5\u79ef\u6728
\u4e24\u8005\u7684\u8be6\u7ec6\u5bf9\u5e94\u5173\u7cfb\u5982\u8868 1-1 \u6240\u793a\u3002
\u8868 1-1 \u00a0 \u5c06\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7c7b\u6bd4\u4e3a\u62fc\u88c5\u79ef\u6728
\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5 \u62fc\u88c5\u79ef\u6728 \u8f93\u5165\u6570\u636e \u672a\u62fc\u88c5\u7684\u79ef\u6728 \u6570\u636e\u7ed3\u6784 \u79ef\u6728\u7ec4\u7ec7\u5f62\u5f0f\uff0c\u5305\u62ec\u5f62\u72b6\u3001\u5927\u5c0f\u3001\u8fde\u63a5\u65b9\u5f0f\u7b49 \u7b97\u6cd5 \u628a\u79ef\u6728\u62fc\u6210\u76ee\u6807\u5f62\u6001\u7684\u4e00\u7cfb\u5217\u64cd\u4f5c\u6b65\u9aa4 \u8f93\u51fa\u6570\u636e \u79ef\u6728\u6a21\u578b\u503c\u5f97\u8bf4\u660e\u7684\u662f\uff0c\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u662f\u72ec\u7acb\u4e8e\u7f16\u7a0b\u8bed\u8a00\u7684\u3002\u6b63\u56e0\u5982\u6b64\uff0c\u672c\u4e66\u5f97\u4ee5\u63d0\u4f9b\u57fa\u4e8e\u591a\u79cd\u7f16\u7a0b\u8bed\u8a00\u7684\u5b9e\u73b0\u3002
\u7ea6\u5b9a\u4fd7\u6210\u7684\u7b80\u79f0
\u5728\u5b9e\u9645\u8ba8\u8bba\u65f6\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u5c06\u201c\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u201d\u7b80\u79f0\u4e3a\u201c\u7b97\u6cd5\u201d\u3002\u6bd4\u5982\u4f17\u6240\u5468\u77e5\u7684 LeetCode \u7b97\u6cd5\u9898\u76ee\uff0c\u5b9e\u9645\u4e0a\u540c\u65f6\u8003\u67e5\u6570\u636e\u7ed3\u6784\u548c\u7b97\u6cd5\u4e24\u65b9\u9762\u7684\u77e5\u8bc6\u3002
"},{"location":"chapter_preface/","title":"\u7b2c 0 \u7ae0 \u00a0 \u524d\u8a00","text":"Abstract
\u7b97\u6cd5\u72b9\u5982\u7f8e\u5999\u7684\u4ea4\u54cd\u4e50\uff0c\u6bcf\u4e00\u884c\u4ee3\u7801\u90fd\u50cf\u97f5\u5f8b\u822c\u6d41\u6dcc\u3002
\u613f\u8fd9\u672c\u4e66\u5728\u4f60\u7684\u8111\u6d77\u4e2d\u8f7b\u8f7b\u54cd\u8d77\uff0c\u7559\u4e0b\u72ec\u7279\u800c\u6df1\u523b\u7684\u65cb\u5f8b\u3002
"},{"location":"chapter_preface/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"\u672c\u9879\u76ee\u65e8\u5728\u521b\u5efa\u4e00\u672c\u5f00\u6e90\u3001\u514d\u8d39\u3001\u65b0\u624b\u53cb\u597d\u7684\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u5165\u95e8\u6559\u7a0b\u3002
\u82e5\u4f60\u662f\u7b97\u6cd5\u521d\u5b66\u8005\uff0c\u4ece\u672a\u63a5\u89e6\u8fc7\u7b97\u6cd5\uff0c\u6216\u8005\u5df2\u7ecf\u6709\u4e00\u4e9b\u5237\u9898\u7ecf\u9a8c\uff0c\u5bf9\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u6709\u6a21\u7cca\u7684\u8ba4\u8bc6\uff0c\u5728\u4f1a\u4e0e\u4e0d\u4f1a\u4e4b\u95f4\u53cd\u590d\u6a2a\u8df3\uff0c\u90a3\u4e48\u672c\u4e66\u6b63\u662f\u4e3a\u4f60\u91cf\u8eab\u5b9a\u5236\u7684\uff01
\u5982\u679c\u4f60\u5df2\u7ecf\u79ef\u7d2f\u4e00\u5b9a\u5237\u9898\u91cf\uff0c\u719f\u6089\u5927\u90e8\u5206\u9898\u578b\uff0c\u90a3\u4e48\u672c\u4e66\u53ef\u52a9\u4f60\u56de\u987e\u4e0e\u68b3\u7406\u7b97\u6cd5\u77e5\u8bc6\u4f53\u7cfb\uff0c\u4ed3\u5e93\u6e90\u4ee3\u7801\u53ef\u4ee5\u5f53\u4f5c\u201c\u5237\u9898\u5de5\u5177\u5e93\u201d\u6216\u201c\u7b97\u6cd5\u5b57\u5178\u201d\u6765\u4f7f\u7528\u3002
\u82e5\u4f60\u662f\u7b97\u6cd5\u5927\u795e\uff0c\u6211\u4eec\u671f\u5f85\u6536\u5230\u4f60\u7684\u5b9d\u8d35\u5efa\u8bae\uff0c\u6216\u8005\u4e00\u8d77\u53c2\u4e0e\u521b\u4f5c\u3002
\u524d\u7f6e\u6761\u4ef6
\u4f60\u9700\u8981\u81f3\u5c11\u5177\u5907\u4efb\u4e00\u8bed\u8a00\u7684\u7f16\u7a0b\u57fa\u7840\uff0c\u80fd\u591f\u9605\u8bfb\u548c\u7f16\u5199\u7b80\u5355\u4ee3\u7801\u3002
"},{"location":"chapter_preface/about_the_book/#012","title":"0.1.2 \u00a0 \u5185\u5bb9\u7ed3\u6784","text":"\u672c\u4e66\u7684\u4e3b\u8981\u5185\u5bb9\u5982\u56fe 0-1 \u6240\u793a\u3002
\u56fe 0-1 \u00a0 \u672c\u4e66\u4e3b\u8981\u5185\u5bb9
"},{"location":"chapter_preface/about_the_book/#013","title":"0.1.3 \u00a0 \u81f4\u8c22","text":"\u5728\u672c\u4e66\u7684\u521b\u4f5c\u8fc7\u7a0b\u4e2d\uff0c\u6211\u5f97\u5230\u4e86\u8bb8\u591a\u4eba\u7684\u5e2e\u52a9\uff0c\u5305\u62ec\u4f46\u4e0d\u9650\u4e8e\uff1a
\u5728\u5199\u4f5c\u8fc7\u7a0b\u4e2d\uff0c\u6211\u9605\u8bfb\u4e86\u8bb8\u591a\u5173\u4e8e\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7684\u6559\u6750\u548c\u6587\u7ae0\u3002\u8fd9\u4e9b\u4f5c\u54c1\u4e3a\u672c\u4e66\u63d0\u4f9b\u4e86\u4f18\u79c0\u7684\u8303\u672c\uff0c\u786e\u4fdd\u4e86\u672c\u4e66\u5185\u5bb9\u7684\u51c6\u786e\u6027\u4e0e\u54c1\u8d28\u3002\u5728\u6b64\u611f\u8c22\u6240\u6709\u8001\u5e08\u548c\u524d\u8f88\u4eec\u7684\u6770\u51fa\u8d21\u732e\uff01
\u672c\u4e66\u5021\u5bfc\u624b\u8111\u5e76\u7528\u7684\u5b66\u4e60\u65b9\u5f0f\uff0c\u5728\u8fd9\u4e00\u70b9\u4e0a\u6df1\u53d7\u300a\u52a8\u624b\u5b66\u6df1\u5ea6\u5b66\u4e60\u300b\u7684\u542f\u53d1\u3002\u5728\u6b64\u5411\u5404\u4f4d\u8bfb\u8005\u5f3a\u70c8\u63a8\u8350\u8fd9\u672c\u4f18\u79c0\u7684\u8457\u4f5c\u3002
\u8877\u5fc3\u611f\u8c22\u6211\u7684\u7236\u6bcd\uff0c\u6b63\u662f\u4f60\u4eec\u4e00\u76f4\u4ee5\u6765\u7684\u652f\u6301\u4e0e\u9f13\u52b1\uff0c\u8ba9\u6211\u6709\u673a\u4f1a\u505a\u8fd9\u4ef6\u5bcc\u6709\u8da3\u5473\u7684\u4e8b\u3002
"},{"location":"chapter_preface/suggestions/","title":"0.2 \u00a0 \u5982\u4f55\u4f7f\u7528\u672c\u4e66","text":"Tip
\u4e3a\u4e86\u83b7\u5f97\u6700\u4f73\u7684\u9605\u8bfb\u4f53\u9a8c\uff0c\u5efa\u8bae\u4f60\u901a\u8bfb\u672c\u8282\u5185\u5bb9\u3002
"},{"location":"chapter_preface/suggestions/#021","title":"0.2.1 \u00a0 \u884c\u6587\u98ce\u683c\u7ea6\u5b9a","text":"*
\u7684\u662f\u9009\u8bfb\u7ae0\u8282\uff0c\u5185\u5bb9\u76f8\u5bf9\u56f0\u96be\u3002\u5982\u679c\u4f60\u7684\u65f6\u95f4\u6709\u9650\uff0c\u53ef\u4ee5\u5148\u8df3\u8fc7\u3002\u300c \u300d
\u62ec\u53f7\u6807\u6ce8\uff0c\u4f8b\u5982 \u300c\u6570\u7ec4 array\u300d
\u3002\u5efa\u8bae\u8bb0\u4f4f\u5b83\u4eec\uff0c\u4ee5\u4fbf\u9605\u8bfb\u6587\u732e\u3002\u201c\u5f15\u53f7\u201d
\u6807\u6ce8\uff0c\u4ee5\u907f\u514d\u6b67\u4e49\u3002\"\"\"\u6807\u9898\u6ce8\u91ca\uff0c\u7528\u4e8e\u6807\u6ce8\u51fd\u6570\u3001\u7c7b\u3001\u6d4b\u8bd5\u6837\u4f8b\u7b49\"\"\"\n\n# \u5185\u5bb9\u6ce8\u91ca\uff0c\u7528\u4e8e\u8be6\u89e3\u4ee3\u7801\n\n\"\"\"\n\u591a\u884c\n\u6ce8\u91ca\n\"\"\"\n
/* \u6807\u9898\u6ce8\u91ca\uff0c\u7528\u4e8e\u6807\u6ce8\u51fd\u6570\u3001\u7c7b\u3001\u6d4b\u8bd5\u6837\u4f8b\u7b49 */\n\n// \u5185\u5bb9\u6ce8\u91ca\uff0c\u7528\u4e8e\u8be6\u89e3\u4ee3\u7801\n\n/**\n * \u591a\u884c\n * \u6ce8\u91ca\n */\n
/* \u6807\u9898\u6ce8\u91ca\uff0c\u7528\u4e8e\u6807\u6ce8\u51fd\u6570\u3001\u7c7b\u3001\u6d4b\u8bd5\u6837\u4f8b\u7b49 */\n\n// \u5185\u5bb9\u6ce8\u91ca\uff0c\u7528\u4e8e\u8be6\u89e3\u4ee3\u7801\n\n/**\n * \u591a\u884c\n * \u6ce8\u91ca\n */\n
/* \u6807\u9898\u6ce8\u91ca\uff0c\u7528\u4e8e\u6807\u6ce8\u51fd\u6570\u3001\u7c7b\u3001\u6d4b\u8bd5\u6837\u4f8b\u7b49 */\n\n// \u5185\u5bb9\u6ce8\u91ca\uff0c\u7528\u4e8e\u8be6\u89e3\u4ee3\u7801\n\n/**\n * \u591a\u884c\n * \u6ce8\u91ca\n */\n
/* \u6807\u9898\u6ce8\u91ca\uff0c\u7528\u4e8e\u6807\u6ce8\u51fd\u6570\u3001\u7c7b\u3001\u6d4b\u8bd5\u6837\u4f8b\u7b49 */\n\n// \u5185\u5bb9\u6ce8\u91ca\uff0c\u7528\u4e8e\u8be6\u89e3\u4ee3\u7801\n\n/**\n * \u591a\u884c\n * \u6ce8\u91ca\n */\n
/* \u6807\u9898\u6ce8\u91ca\uff0c\u7528\u4e8e\u6807\u6ce8\u51fd\u6570\u3001\u7c7b\u3001\u6d4b\u8bd5\u6837\u4f8b\u7b49 */\n\n// \u5185\u5bb9\u6ce8\u91ca\uff0c\u7528\u4e8e\u8be6\u89e3\u4ee3\u7801\n\n/**\n * \u591a\u884c\n * \u6ce8\u91ca\n */\n
/* \u6807\u9898\u6ce8\u91ca\uff0c\u7528\u4e8e\u6807\u6ce8\u51fd\u6570\u3001\u7c7b\u3001\u6d4b\u8bd5\u6837\u4f8b\u7b49 */\n\n// \u5185\u5bb9\u6ce8\u91ca\uff0c\u7528\u4e8e\u8be6\u89e3\u4ee3\u7801\n\n/**\n * \u591a\u884c\n * \u6ce8\u91ca\n */\n
/* \u6807\u9898\u6ce8\u91ca\uff0c\u7528\u4e8e\u6807\u6ce8\u51fd\u6570\u3001\u7c7b\u3001\u6d4b\u8bd5\u6837\u4f8b\u7b49 */\n\n// \u5185\u5bb9\u6ce8\u91ca\uff0c\u7528\u4e8e\u8be6\u89e3\u4ee3\u7801\n\n/**\n * \u591a\u884c\n * \u6ce8\u91ca\n */\n
/* \u6807\u9898\u6ce8\u91ca\uff0c\u7528\u4e8e\u6807\u6ce8\u51fd\u6570\u3001\u7c7b\u3001\u6d4b\u8bd5\u6837\u4f8b\u7b49 */\n\n// \u5185\u5bb9\u6ce8\u91ca\uff0c\u7528\u4e8e\u8be6\u89e3\u4ee3\u7801\n\n/**\n * \u591a\u884c\n * \u6ce8\u91ca\n */\n
/* \u6807\u9898\u6ce8\u91ca\uff0c\u7528\u4e8e\u6807\u6ce8\u51fd\u6570\u3001\u7c7b\u3001\u6d4b\u8bd5\u6837\u4f8b\u7b49 */\n\n// \u5185\u5bb9\u6ce8\u91ca\uff0c\u7528\u4e8e\u8be6\u89e3\u4ee3\u7801\n\n/**\n * \u591a\u884c\n * \u6ce8\u91ca\n */\n
/* \u6807\u9898\u6ce8\u91ca\uff0c\u7528\u4e8e\u6807\u6ce8\u51fd\u6570\u3001\u7c7b\u3001\u6d4b\u8bd5\u6837\u4f8b\u7b49 */\n\n// \u5185\u5bb9\u6ce8\u91ca\uff0c\u7528\u4e8e\u8be6\u89e3\u4ee3\u7801\n\n/**\n * \u591a\u884c\n * \u6ce8\u91ca\n */\n
// \u6807\u9898\u6ce8\u91ca\uff0c\u7528\u4e8e\u6807\u6ce8\u51fd\u6570\u3001\u7c7b\u3001\u6d4b\u8bd5\u6837\u4f8b\u7b49\n\n// \u5185\u5bb9\u6ce8\u91ca\uff0c\u7528\u4e8e\u8be6\u89e3\u4ee3\u7801\n\n// \u591a\u884c\n// \u6ce8\u91ca\n
"},{"location":"chapter_preface/suggestions/#022","title":"0.2.2 \u00a0 \u5728\u52a8\u753b\u56fe\u89e3\u4e2d\u9ad8\u6548\u5b66\u4e60","text":"\u76f8\u8f83\u4e8e\u6587\u5b57\uff0c\u89c6\u9891\u548c\u56fe\u7247\u5177\u6709\u66f4\u9ad8\u7684\u4fe1\u606f\u5bc6\u5ea6\u548c\u7ed3\u6784\u5316\u7a0b\u5ea6\uff0c\u66f4\u6613\u4e8e\u7406\u89e3\u3002\u5728\u672c\u4e66\u4e2d\uff0c\u91cd\u70b9\u548c\u96be\u70b9\u77e5\u8bc6\u5c06\u4e3b\u8981\u901a\u8fc7\u52a8\u753b\u548c\u56fe\u89e3\u5f62\u5f0f\u5c55\u793a\uff0c\u800c\u6587\u5b57\u5219\u4f5c\u4e3a\u52a8\u753b\u548c\u56fe\u7247\u7684\u89e3\u91ca\u4e0e\u8865\u5145\u3002
\u5982\u679c\u4f60\u5728\u9605\u8bfb\u672c\u4e66\u65f6\uff0c\u53d1\u73b0\u67d0\u6bb5\u5185\u5bb9\u63d0\u4f9b\u4e86\u5982\u56fe 0-2 \u6240\u793a\u7684\u52a8\u753b\u6216\u56fe\u89e3\uff0c\u8bf7\u4ee5\u56fe\u4e3a\u4e3b\u3001\u4ee5\u6587\u5b57\u4e3a\u8f85\uff0c\u7efc\u5408\u4e24\u8005\u6765\u7406\u89e3\u5185\u5bb9\u3002
\u56fe 0-2 \u00a0 \u52a8\u753b\u56fe\u89e3\u793a\u4f8b
"},{"location":"chapter_preface/suggestions/#023","title":"0.2.3 \u00a0 \u5728\u4ee3\u7801\u5b9e\u8df5\u4e2d\u52a0\u6df1\u7406\u89e3","text":"\u672c\u4e66\u7684\u914d\u5957\u4ee3\u7801\u6258\u7ba1\u5728 GitHub \u4ed3\u5e93\u3002\u5982\u56fe 0-3 \u6240\u793a\uff0c\u6e90\u4ee3\u7801\u9644\u6709\u6d4b\u8bd5\u6837\u4f8b\uff0c\u53ef\u4e00\u952e\u8fd0\u884c\u3002
\u5982\u679c\u65f6\u95f4\u5141\u8bb8\uff0c\u5efa\u8bae\u4f60\u53c2\u7167\u4ee3\u7801\u81ea\u884c\u6572\u4e00\u904d\u3002\u5982\u679c\u5b66\u4e60\u65f6\u95f4\u6709\u9650\uff0c\u8bf7\u81f3\u5c11\u901a\u8bfb\u5e76\u8fd0\u884c\u6240\u6709\u4ee3\u7801\u3002
\u4e0e\u9605\u8bfb\u4ee3\u7801\u76f8\u6bd4\uff0c\u7f16\u5199\u4ee3\u7801\u7684\u8fc7\u7a0b\u5f80\u5f80\u80fd\u5e26\u6765\u66f4\u591a\u6536\u83b7\u3002\u52a8\u624b\u5b66\uff0c\u624d\u662f\u771f\u7684\u5b66\u3002
\u56fe 0-3 \u00a0 \u8fd0\u884c\u4ee3\u7801\u793a\u4f8b
\u8fd0\u884c\u4ee3\u7801\u7684\u524d\u7f6e\u5de5\u4f5c\u4e3b\u8981\u5206\u4e3a\u4e09\u6b65\u3002
\u7b2c\u4e00\u6b65\uff1a\u5b89\u88c5\u672c\u5730\u7f16\u7a0b\u73af\u5883\u3002\u8bf7\u53c2\u7167\u9644\u5f55\u6240\u793a\u7684\u6559\u7a0b\u8fdb\u884c\u5b89\u88c5\uff0c\u5982\u679c\u5df2\u5b89\u88c5\uff0c\u5219\u53ef\u8df3\u8fc7\u6b64\u6b65\u9aa4\u3002
\u7b2c\u4e8c\u6b65\uff1a\u514b\u9686\u6216\u4e0b\u8f7d\u4ee3\u7801\u4ed3\u5e93\u3002\u524d\u5f80 GitHub \u4ed3\u5e93\u3002
\u5982\u679c\u5df2\u7ecf\u5b89\u88c5 Git \uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u514b\u9686\u672c\u4ed3\u5e93\uff1a
git clone https://github.com/krahets/hello-algo.git\n
\u5f53\u7136\uff0c\u4f60\u4e5f\u53ef\u4ee5\u5728\u56fe 0-4 \u6240\u793a\u7684\u4f4d\u7f6e\uff0c\u70b9\u51fb\u201cDownload ZIP\u201d\u6309\u94ae\u76f4\u63a5\u4e0b\u8f7d\u4ee3\u7801\u538b\u7f29\u5305\uff0c\u7136\u540e\u5728\u672c\u5730\u89e3\u538b\u5373\u53ef\u3002
\u56fe 0-4 \u00a0 \u514b\u9686\u4ed3\u5e93\u4e0e\u4e0b\u8f7d\u4ee3\u7801
\u7b2c\u4e09\u6b65\uff1a\u8fd0\u884c\u6e90\u4ee3\u7801\u3002\u5982\u56fe 0-5 \u6240\u793a\uff0c\u5bf9\u4e8e\u9876\u90e8\u6807\u6709\u6587\u4ef6\u540d\u79f0\u7684\u4ee3\u7801\u5757\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u4ed3\u5e93\u7684 codes
\u6587\u4ef6\u5939\u5185\u627e\u5230\u5bf9\u5e94\u7684\u6e90\u4ee3\u7801\u6587\u4ef6\u3002\u6e90\u4ee3\u7801\u6587\u4ef6\u53ef\u4e00\u952e\u8fd0\u884c\uff0c\u5c06\u5e2e\u52a9\u4f60\u8282\u7701\u4e0d\u5fc5\u8981\u7684\u8c03\u8bd5\u65f6\u95f4\uff0c\u8ba9\u4f60\u80fd\u591f\u4e13\u6ce8\u4e8e\u5b66\u4e60\u5185\u5bb9\u3002
\u56fe 0-5 \u00a0 \u4ee3\u7801\u5757\u4e0e\u5bf9\u5e94\u7684\u6e90\u4ee3\u7801\u6587\u4ef6
"},{"location":"chapter_preface/suggestions/#024","title":"0.2.4 \u00a0 \u5728\u63d0\u95ee\u8ba8\u8bba\u4e2d\u5171\u540c\u6210\u957f","text":"\u5728\u9605\u8bfb\u672c\u4e66\u65f6\uff0c\u8bf7\u4e0d\u8981\u8f7b\u6613\u8df3\u8fc7\u90a3\u4e9b\u6ca1\u5b66\u660e\u767d\u7684\u77e5\u8bc6\u70b9\u3002\u6b22\u8fce\u5728\u8bc4\u8bba\u533a\u63d0\u51fa\u4f60\u7684\u95ee\u9898\uff0c\u6211\u548c\u5c0f\u4f19\u4f34\u4eec\u5c06\u7aed\u8bda\u4e3a\u4f60\u89e3\u7b54\uff0c\u4e00\u822c\u60c5\u51b5\u4e0b\u53ef\u5728\u4e24\u5929\u5185\u56de\u590d\u3002
\u5982\u56fe 0-6 \u6240\u793a\uff0c\u6bcf\u4e2a\u7ae0\u8282\u7684\u5e95\u90e8\u90fd\u914d\u6709\u8bc4\u8bba\u533a\u3002\u5e0c\u671b\u4f60\u80fd\u591a\u5173\u6ce8\u8bc4\u8bba\u533a\u7684\u5185\u5bb9\u3002\u4e00\u65b9\u9762\uff0c\u4f60\u53ef\u4ee5\u4e86\u89e3\u5927\u5bb6\u9047\u5230\u7684\u95ee\u9898\uff0c\u4ece\u800c\u67e5\u6f0f\u8865\u7f3a\uff0c\u6fc0\u53d1\u66f4\u6df1\u5165\u7684\u601d\u8003\u3002\u53e6\u4e00\u65b9\u9762\uff0c\u671f\u5f85\u4f60\u80fd\u6177\u6168\u5730\u56de\u7b54\u5176\u4ed6\u5c0f\u4f19\u4f34\u7684\u95ee\u9898\uff0c\u5206\u4eab\u4f60\u7684\u89c1\u89e3\uff0c\u5e2e\u52a9\u4ed6\u4eba\u8fdb\u6b65\u3002
\u56fe 0-6 \u00a0 \u8bc4\u8bba\u533a\u793a\u4f8b
"},{"location":"chapter_preface/suggestions/#025","title":"0.2.5 \u00a0 \u7b97\u6cd5\u5b66\u4e60\u8def\u7ebf","text":"\u4ece\u603b\u4f53\u4e0a\u770b\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5b66\u4e60\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7684\u8fc7\u7a0b\u5212\u5206\u4e3a\u4e09\u4e2a\u9636\u6bb5\u3002
\u5982\u56fe 0-7 \u6240\u793a\uff0c\u672c\u4e66\u5185\u5bb9\u4e3b\u8981\u6db5\u76d6\u201c\u9636\u6bb5\u4e00\u201d\uff0c\u65e8\u5728\u5e2e\u52a9\u4f60\u66f4\u9ad8\u6548\u5730\u5c55\u5f00\u9636\u6bb5\u4e8c\u548c\u9636\u6bb5\u4e09\u7684\u5b66\u4e60\u3002
\u56fe 0-7 \u00a0 \u7b97\u6cd5\u5b66\u4e60\u8def\u7ebf
"},{"location":"chapter_preface/summary/","title":"0.3 \u00a0 \u5c0f\u7ed3","text":"[1] Thomas H. Cormen, et al. Introduction to Algorithms (3rd Edition).
[2] Aditya Bhargava. Grokking Algorithms: An Illustrated Guide for Programmers and Other Curious People (1st Edition).
[3] \u4e25\u851a\u654f. \u6570\u636e\u7ed3\u6784\uff08C \u8bed\u8a00\u7248\uff09.
[4] \u9093\u4fca\u8f89. \u6570\u636e\u7ed3\u6784\uff08C++ \u8bed\u8a00\u7248\uff0c\u7b2c\u4e09\u7248\uff09.
[5] \u9a6c\u514b \u827e\u4f26 \u7ef4\u65af\u8457\uff0c\u9648\u8d8a\u8bd1. \u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u5206\u6790\uff1aJava\u8bed\u8a00\u63cf\u8ff0\uff08\u7b2c\u4e09\u7248\uff09.
[6] \u7a0b\u6770. \u5927\u8bdd\u6570\u636e\u7ed3\u6784.
[7] \u738b\u4e89. \u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u4e4b\u7f8e.
[8] Gayle Laakmann McDowell. Cracking the Coding Interview: 189 Programming Questions and Solutions (6th Edition).
[9] Aston Zhang, et al. Dive into Deep Learning.
"},{"location":"chapter_searching/","title":"\u7b2c 10 \u7ae0 \u00a0 \u641c\u7d22","text":"Abstract
\u641c\u7d22\u662f\u4e00\u573a\u672a\u77e5\u7684\u5192\u9669\uff0c\u6211\u4eec\u6216\u8bb8\u9700\u8981\u8d70\u904d\u795e\u79d8\u7a7a\u95f4\u7684\u6bcf\u4e2a\u89d2\u843d\uff0c\u53c8\u6216\u8bb8\u53ef\u4ee5\u5feb\u901f\u9501\u5b9a\u76ee\u6807\u3002
\u5728\u8fd9\u573a\u5bfb\u89c5\u4e4b\u65c5\u4e2d\uff0c\u6bcf\u4e00\u6b21\u63a2\u7d22\u90fd\u53ef\u80fd\u5f97\u5230\u4e00\u4e2a\u672a\u66fe\u6599\u60f3\u7684\u7b54\u6848\u3002
"},{"location":"chapter_searching/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"\u300c\u4e8c\u5206\u67e5\u627e binary search\u300d\u662f\u4e00\u79cd\u57fa\u4e8e\u5206\u6cbb\u7b56\u7565\u7684\u9ad8\u6548\u641c\u7d22\u7b97\u6cd5\u3002\u5b83\u5229\u7528\u6570\u636e\u7684\u6709\u5e8f\u6027\uff0c\u6bcf\u8f6e\u7f29\u5c0f\u4e00\u534a\u641c\u7d22\u8303\u56f4\uff0c\u76f4\u81f3\u627e\u5230\u76ee\u6807\u5143\u7d20\u6216\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u4e3a\u6b62\u3002
Question
\u7ed9\u5b9a\u4e00\u4e2a\u957f\u5ea6\u4e3a \\(n\\) \u7684\u6570\u7ec4 nums
\uff0c\u5143\u7d20\u6309\u4ece\u5c0f\u5230\u5927\u7684\u987a\u5e8f\u6392\u5217\u4e14\u4e0d\u91cd\u590d\u3002\u8bf7\u67e5\u627e\u5e76\u8fd4\u56de\u5143\u7d20 target
\u5728\u8be5\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15\u3002\u82e5\u6570\u7ec4\u4e0d\u5305\u542b\u8be5\u5143\u7d20\uff0c\u5219\u8fd4\u56de \\(-1\\) \u3002\u793a\u4f8b\u5982\u56fe 10-1 \u6240\u793a\u3002
\u56fe 10-1 \u00a0 \u4e8c\u5206\u67e5\u627e\u793a\u4f8b\u6570\u636e
\u5982\u56fe 10-2 \u6240\u793a\uff0c\u6211\u4eec\u5148\u521d\u59cb\u5316\u6307\u9488 \\(i = 0\\) \u548c \\(j = n - 1\\) \uff0c\u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u548c\u5c3e\u5143\u7d20\uff0c\u4ee3\u8868\u641c\u7d22\u533a\u95f4 \\([0, n - 1]\\) \u3002\u8bf7\u6ce8\u610f\uff0c\u4e2d\u62ec\u53f7\u8868\u793a\u95ed\u533a\u95f4\uff0c\u5176\u5305\u542b\u8fb9\u754c\u503c\u672c\u8eab\u3002
\u63a5\u4e0b\u6765\uff0c\u5faa\u73af\u6267\u884c\u4ee5\u4e0b\u4e24\u6b65\u3002
nums[m]
\u548c target
\u7684\u5927\u5c0f\u5173\u7cfb\uff0c\u5206\u4e3a\u4ee5\u4e0b\u4e09\u79cd\u60c5\u51b5\u3002nums[m] < target
\u65f6\uff0c\u8bf4\u660e target
\u5728\u533a\u95f4 \\([m + 1, j]\\) \u4e2d\uff0c\u56e0\u6b64\u6267\u884c \\(i = m + 1\\) \u3002nums[m] > target
\u65f6\uff0c\u8bf4\u660e target
\u5728\u533a\u95f4 \\([i, m - 1]\\) \u4e2d\uff0c\u56e0\u6b64\u6267\u884c \\(j = m - 1\\) \u3002nums[m] = target
\u65f6\uff0c\u8bf4\u660e\u627e\u5230 target
\uff0c\u56e0\u6b64\u8fd4\u56de\u7d22\u5f15 \\(m\\) \u3002\u82e5\u6570\u7ec4\u4e0d\u5305\u542b\u76ee\u6807\u5143\u7d20\uff0c\u641c\u7d22\u533a\u95f4\u6700\u7ec8\u4f1a\u7f29\u5c0f\u4e3a\u7a7a\u3002\u6b64\u65f6\u8fd4\u56de \\(-1\\) \u3002
<1><2><3><4><5><6><7>\u56fe 10-2 \u00a0 \u4e8c\u5206\u67e5\u627e\u6d41\u7a0b
\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u7531\u4e8e \\(i\\) \u548c \\(j\\) \u90fd\u662f int
\u7c7b\u578b\uff0c\u56e0\u6b64 \\(i + j\\) \u53ef\u80fd\u4f1a\u8d85\u51fa int
\u7c7b\u578b\u7684\u53d6\u503c\u8303\u56f4\u3002\u4e3a\u4e86\u907f\u514d\u5927\u6570\u8d8a\u754c\uff0c\u6211\u4eec\u901a\u5e38\u91c7\u7528\u516c\u5f0f \\(m = \\lfloor {i + (j - i) / 2} \\rfloor\\) \u6765\u8ba1\u7b97\u4e2d\u70b9\u3002
\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig binary_search.pydef binary_search(nums: list[int], target: int) -> int:\n \"\"\"\u4e8c\u5206\u67e5\u627e\uff08\u53cc\u95ed\u533a\u95f4\uff09\"\"\"\n # \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1] \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\n i, j = 0, len(nums) - 1\n # \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i > j \u65f6\u4e3a\u7a7a\uff09\n while i <= j:\n # \u7406\u8bba\u4e0a Python \u7684\u6570\u5b57\u53ef\u4ee5\u65e0\u9650\u5927\uff08\u53d6\u51b3\u4e8e\u5185\u5b58\u5927\u5c0f\uff09\uff0c\u65e0\u987b\u8003\u8651\u5927\u6570\u8d8a\u754c\u95ee\u9898\n m = (i + j) // 2 # \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n if nums[m] < target:\n i = m + 1 # \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n elif nums[m] > target:\n j = m - 1 # \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n else:\n return m # \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return -1 # \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n
binary_search.cpp/* \u4e8c\u5206\u67e5\u627e\uff08\u53cc\u95ed\u533a\u95f4\uff09 */\nint binarySearch(vector<int> &nums, int target) {\n // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1] \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\n int i = 0, j = nums.size() - 1;\n // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i > j \u65f6\u4e3a\u7a7a\uff09\n while (i <= j) {\n int m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n if (nums[m] < target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n i = m + 1;\n else if (nums[m] > target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n j = m - 1;\n else // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m;\n }\n // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n return -1;\n}\n
binary_search.java/* \u4e8c\u5206\u67e5\u627e\uff08\u53cc\u95ed\u533a\u95f4\uff09 */\nint binarySearch(int[] nums, int target) {\n // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1] \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\n int i = 0, j = nums.length - 1;\n // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i > j \u65f6\u4e3a\u7a7a\uff09\n while (i <= j) {\n int m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n if (nums[m] < target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n i = m + 1;\n else if (nums[m] > target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n j = m - 1;\n else // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m;\n }\n // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n return -1;\n}\n
binary_search.cs/* \u4e8c\u5206\u67e5\u627e\uff08\u53cc\u95ed\u533a\u95f4\uff09 */\nint BinarySearch(int[] nums, int target) {\n // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1] \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\n int i = 0, j = nums.Length - 1;\n // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i > j \u65f6\u4e3a\u7a7a\uff09\n while (i <= j) {\n int m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n if (nums[m] < target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n i = m + 1;\n else if (nums[m] > target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n j = m - 1;\n else // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m;\n }\n // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n return -1;\n}\n
binary_search.go/* \u4e8c\u5206\u67e5\u627e\uff08\u53cc\u95ed\u533a\u95f4\uff09 */\nfunc binarySearch(nums []int, target int) int {\n // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1] \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\n i, j := 0, len(nums)-1\n // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i > j \u65f6\u4e3a\u7a7a\uff09\n for i <= j {\n m := i + (j-i)/2 // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n if nums[m] < target { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n i = m + 1\n } else if nums[m] > target { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n j = m - 1\n } else { // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m\n }\n }\n // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n return -1\n}\n
binary_search.swift/* \u4e8c\u5206\u67e5\u627e\uff08\u53cc\u95ed\u533a\u95f4\uff09 */\nfunc binarySearch(nums: [Int], target: Int) -> Int {\n // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1] \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\n var i = 0\n var j = nums.count - 1\n // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i > j \u65f6\u4e3a\u7a7a\uff09\n while i <= j {\n let m = i + (j - i) / 2 // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n if nums[m] < target { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n i = m + 1\n } else if nums[m] > target { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n j = m - 1\n } else { // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m\n }\n }\n // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n return -1\n}\n
binary_search.js/* \u4e8c\u5206\u67e5\u627e\uff08\u53cc\u95ed\u533a\u95f4\uff09 */\nfunction binarySearch(nums, target) {\n // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1] \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\n let i = 0,\n j = nums.length - 1;\n // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i > j \u65f6\u4e3a\u7a7a\uff09\n while (i <= j) {\n // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m \uff0c\u4f7f\u7528 parseInt() \u5411\u4e0b\u53d6\u6574\n const m = parseInt(i + (j - i) / 2);\n if (nums[m] < target)\n // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n i = m + 1;\n else if (nums[m] > target)\n // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n j = m - 1;\n else return m; // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n }\n // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n return -1;\n}\n
binary_search.ts/* \u4e8c\u5206\u67e5\u627e\uff08\u53cc\u95ed\u533a\u95f4\uff09 */\nfunction binarySearch(nums: number[], target: number): number {\n // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1] \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\n let i = 0,\n j = nums.length - 1;\n // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i > j \u65f6\u4e3a\u7a7a\uff09\n while (i <= j) {\n // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n const m = Math.floor(i + (j - i) / 2);\n if (nums[m] < target) {\n // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n i = m + 1;\n } else if (nums[m] > target) {\n // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n j = m - 1;\n } else {\n // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m;\n }\n }\n return -1; // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n}\n
binary_search.dart/* \u4e8c\u5206\u67e5\u627e\uff08\u53cc\u95ed\u533a\u95f4\uff09 */\nint binarySearch(List<int> nums, int target) {\n // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1] \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\n int i = 0, j = nums.length - 1;\n // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i > j \u65f6\u4e3a\u7a7a\uff09\n while (i <= j) {\n int m = i + (j - i) ~/ 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n if (nums[m] < target) {\n // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n i = m + 1;\n } else if (nums[m] > target) {\n // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n j = m - 1;\n } else {\n // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m;\n }\n }\n // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n return -1;\n}\n
binary_search.rs/* \u4e8c\u5206\u67e5\u627e\uff08\u53cc\u95ed\u533a\u95f4\uff09 */\nfn binary_search(nums: &[i32], target: i32) -> i32 {\n // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1] \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\n let mut i = 0;\n let mut j = nums.len() as i32 - 1;\n // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i > j \u65f6\u4e3a\u7a7a\uff09\n while i <= j {\n let m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n if nums[m as usize] < target { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n i = m + 1;\n } else if nums[m as usize] > target { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n j = m - 1;\n } else { // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m;\n } \n }\n // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n return -1;\n}\n
binary_search.c/* \u4e8c\u5206\u67e5\u627e\uff08\u53cc\u95ed\u533a\u95f4\uff09 */\nint binarySearch(int *nums, int len, int target) {\n // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1] \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\n int i = 0, j = len - 1;\n // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i > j \u65f6\u4e3a\u7a7a\uff09\n while (i <= j) {\n int m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n if (nums[m] < target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n i = m + 1;\n else if (nums[m] > target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n j = m - 1;\n else // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m;\n }\n // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n return -1;\n}\n
binary_search.zig// \u4e8c\u5206\u67e5\u627e\uff08\u53cc\u95ed\u533a\u95f4\uff09\nfn binarySearch(comptime T: type, nums: std.ArrayList(T), target: T) T {\n // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1] \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\n var i: usize = 0;\n var j: usize = nums.items.len - 1;\n // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i > j \u65f6\u4e3a\u7a7a\uff09\n while (i <= j) {\n var m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n if (nums.items[m] < target) { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n i = m + 1;\n } else if (nums.items[m] > target) { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n j = m - 1;\n } else { // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return @intCast(m);\n }\n }\n // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n return -1;\n}\n
\u65f6\u95f4\u590d\u6742\u5ea6 \\(O(\\log n)\\) \uff1a\u5728\u4e8c\u5206\u5faa\u73af\u4e2d\uff0c\u533a\u95f4\u6bcf\u8f6e\u7f29\u5c0f\u4e00\u534a\uff0c\u5faa\u73af\u6b21\u6570\u4e3a \\(\\log_2 n\\) \u3002
\u7a7a\u95f4\u590d\u6742\u5ea6 \\(O(1)\\) \uff1a\u6307\u9488 \\(i\\) \u548c \\(j\\) \u4f7f\u7528\u5e38\u6570\u5927\u5c0f\u7a7a\u95f4\u3002
"},{"location":"chapter_searching/binary_search/#1011","title":"10.1.1 \u00a0 \u533a\u95f4\u8868\u793a\u65b9\u6cd5","text":"\u9664\u4e86\u4e0a\u8ff0\u53cc\u95ed\u533a\u95f4\u5916\uff0c\u5e38\u89c1\u7684\u533a\u95f4\u8868\u793a\u8fd8\u6709\u201c\u5de6\u95ed\u53f3\u5f00\u201d\u533a\u95f4\uff0c\u5b9a\u4e49\u4e3a \\([0, n)\\) \uff0c\u5373\u5de6\u8fb9\u754c\u5305\u542b\u81ea\u8eab\uff0c\u53f3\u8fb9\u754c\u4e0d\u5305\u542b\u81ea\u8eab\u3002\u5728\u8be5\u8868\u793a\u4e0b\uff0c\u533a\u95f4 \\([i, j)\\) \u5728 \\(i = j\\) \u65f6\u4e3a\u7a7a\u3002
\u6211\u4eec\u53ef\u4ee5\u57fa\u4e8e\u8be5\u8868\u793a\u5b9e\u73b0\u5177\u6709\u76f8\u540c\u529f\u80fd\u7684\u4e8c\u5206\u67e5\u627e\u7b97\u6cd5\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig binary_search.pydef binary_search_lcro(nums: list[int], target: int) -> int:\n \"\"\"\u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\u533a\u95f4\uff09\"\"\"\n # \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00\u533a\u95f4 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\n i, j = 0, len(nums)\n # \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i = j \u65f6\u4e3a\u7a7a\uff09\n while i < j:\n m = (i + j) // 2 # \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n if nums[m] < target:\n i = m + 1 # \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\n elif nums[m] > target:\n j = m # \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\n else:\n return m # \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return -1 # \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n
binary_search.cpp/* \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\u533a\u95f4\uff09 */\nint binarySearchLCRO(vector<int> &nums, int target) {\n // \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00\u533a\u95f4 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\n int i = 0, j = nums.size();\n // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i = j \u65f6\u4e3a\u7a7a\uff09\n while (i < j) {\n int m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n if (nums[m] < target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\n i = m + 1;\n else if (nums[m] > target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\n j = m;\n else // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m;\n }\n // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n return -1;\n}\n
binary_search.java/* \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\u533a\u95f4\uff09 */\nint binarySearchLCRO(int[] nums, int target) {\n // \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00\u533a\u95f4 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\n int i = 0, j = nums.length;\n // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i = j \u65f6\u4e3a\u7a7a\uff09\n while (i < j) {\n int m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n if (nums[m] < target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\n i = m + 1;\n else if (nums[m] > target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\n j = m;\n else // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m;\n }\n // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n return -1;\n}\n
binary_search.cs/* \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\u533a\u95f4\uff09 */\nint BinarySearchLCRO(int[] nums, int target) {\n // \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00\u533a\u95f4 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\n int i = 0, j = nums.Length;\n // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i = j \u65f6\u4e3a\u7a7a\uff09\n while (i < j) {\n int m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n if (nums[m] < target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\n i = m + 1;\n else if (nums[m] > target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\n j = m;\n else // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m;\n }\n // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n return -1;\n}\n
binary_search.go/* \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\u533a\u95f4\uff09 */\nfunc binarySearchLCRO(nums []int, target int) int {\n // \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00\u533a\u95f4 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\n i, j := 0, len(nums)\n // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i = j \u65f6\u4e3a\u7a7a\uff09\n for i < j {\n m := i + (j-i)/2 // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n if nums[m] < target { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\n i = m + 1\n } else if nums[m] > target { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\n j = m\n } else { // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m\n }\n }\n // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n return -1\n}\n
binary_search.swift/* \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\u533a\u95f4\uff09 */\nfunc binarySearchLCRO(nums: [Int], target: Int) -> Int {\n // \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00\u533a\u95f4 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\n var i = 0\n var j = nums.count\n // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i = j \u65f6\u4e3a\u7a7a\uff09\n while i < j {\n let m = i + (j - i) / 2 // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n if nums[m] < target { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\n i = m + 1\n } else if nums[m] > target { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\n j = m\n } else { // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m\n }\n }\n // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n return -1\n}\n
binary_search.js/* \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\u533a\u95f4\uff09 */\nfunction binarySearchLCRO(nums, target) {\n // \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00\u533a\u95f4 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\n let i = 0,\n j = nums.length;\n // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i = j \u65f6\u4e3a\u7a7a\uff09\n while (i < j) {\n // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m \uff0c\u4f7f\u7528 parseInt() \u5411\u4e0b\u53d6\u6574\n const m = parseInt(i + (j - i) / 2);\n if (nums[m] < target)\n // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\n i = m + 1;\n else if (nums[m] > target)\n // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\n j = m;\n // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n else return m;\n }\n // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n return -1;\n}\n
binary_search.ts/* \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\u533a\u95f4\uff09 */\nfunction binarySearchLCRO(nums: number[], target: number): number {\n // \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00\u533a\u95f4 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\n let i = 0,\n j = nums.length;\n // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i = j \u65f6\u4e3a\u7a7a\uff09\n while (i < j) {\n // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n const m = Math.floor(i + (j - i) / 2);\n if (nums[m] < target) {\n // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\n i = m + 1;\n } else if (nums[m] > target) {\n // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\n j = m;\n } else {\n // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m;\n }\n }\n return -1; // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n}\n
binary_search.dart/* \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\u533a\u95f4\uff09 */\nint binarySearchLCRO(List<int> nums, int target) {\n // \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00\u533a\u95f4 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\n int i = 0, j = nums.length;\n // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i = j \u65f6\u4e3a\u7a7a\uff09\n while (i < j) {\n int m = i + (j - i) ~/ 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n if (nums[m] < target) {\n // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\n i = m + 1;\n } else if (nums[m] > target) {\n // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\n j = m;\n } else {\n // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m;\n }\n }\n // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n return -1;\n}\n
binary_search.rs/* \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\u533a\u95f4\uff09 */\nfn binary_search_lcro(nums: &[i32], target: i32) -> i32 {\n // \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00\u533a\u95f4 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\n let mut i = 0;\n let mut j = nums.len() as i32;\n // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i = j \u65f6\u4e3a\u7a7a\uff09\n while i < j {\n let m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n if nums[m as usize] < target { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\n i = m + 1;\n } else if nums[m as usize] > target { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\n j = m - 1;\n } else { // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m;\n } \n }\n // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n return -1;\n}\n
binary_search.c/* \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\u533a\u95f4\uff09 */\nint binarySearchLCRO(int *nums, int len, int target) {\n // \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00\u533a\u95f4 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\n int i = 0, j = len;\n // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i = j \u65f6\u4e3a\u7a7a\uff09\n while (i < j) {\n int m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n if (nums[m] < target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\n i = m + 1;\n else if (nums[m] > target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\n j = m;\n else // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m;\n }\n // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n return -1;\n}\n
binary_search.zig// \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\u533a\u95f4\uff09\nfn binarySearchLCRO(comptime T: type, nums: std.ArrayList(T), target: T) T {\n // \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00\u533a\u95f4 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\n var i: usize = 0;\n var j: usize = nums.items.len;\n // \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i = j \u65f6\u4e3a\u7a7a\uff09\n while (i <= j) {\n var m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n if (nums.items[m] < target) { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\n i = m + 1;\n } else if (nums.items[m] > target) { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\n j = m;\n } else { // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return @intCast(m);\n }\n }\n // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n return -1;\n}\n
\u5982\u56fe 10-3 \u6240\u793a\uff0c\u5728\u4e24\u79cd\u533a\u95f4\u8868\u793a\u4e0b\uff0c\u4e8c\u5206\u67e5\u627e\u7b97\u6cd5\u7684\u521d\u59cb\u5316\u3001\u5faa\u73af\u6761\u4ef6\u548c\u7f29\u5c0f\u533a\u95f4\u64cd\u4f5c\u7686\u6709\u6240\u4e0d\u540c\u3002
\u7531\u4e8e\u201c\u53cc\u95ed\u533a\u95f4\u201d\u8868\u793a\u4e2d\u7684\u5de6\u53f3\u8fb9\u754c\u90fd\u88ab\u5b9a\u4e49\u4e3a\u95ed\u533a\u95f4\uff0c\u56e0\u6b64\u901a\u8fc7\u6307\u9488 \\(i\\) \u548c\u6307\u9488 \\(j\\) \u7f29\u5c0f\u533a\u95f4\u7684\u64cd\u4f5c\u4e5f\u662f\u5bf9\u79f0\u7684\u3002\u8fd9\u6837\u66f4\u4e0d\u5bb9\u6613\u51fa\u9519\uff0c\u56e0\u6b64\u4e00\u822c\u5efa\u8bae\u91c7\u7528\u201c\u53cc\u95ed\u533a\u95f4\u201d\u7684\u5199\u6cd5\u3002
\u56fe 10-3 \u00a0 \u4e24\u79cd\u533a\u95f4\u5b9a\u4e49
"},{"location":"chapter_searching/binary_search/#1012","title":"10.1.2 \u00a0 \u4f18\u70b9\u4e0e\u5c40\u9650\u6027","text":"\u4e8c\u5206\u67e5\u627e\u5728\u65f6\u95f4\u548c\u7a7a\u95f4\u65b9\u9762\u90fd\u6709\u8f83\u597d\u7684\u6027\u80fd\u3002
\u7136\u800c\uff0c\u4e8c\u5206\u67e5\u627e\u5e76\u975e\u9002\u7528\u4e8e\u6240\u6709\u60c5\u51b5\uff0c\u4e3b\u8981\u6709\u4ee5\u4e0b\u539f\u56e0\u3002
Question
\u7ed9\u5b9a\u4e00\u4e2a\u957f\u5ea6\u4e3a \\(n\\) \u7684\u6709\u5e8f\u6570\u7ec4 nums
\uff0c\u5176\u4e2d\u53ef\u80fd\u5305\u542b\u91cd\u590d\u5143\u7d20\u3002\u8bf7\u8fd4\u56de\u6570\u7ec4\u4e2d\u6700\u5de6\u4e00\u4e2a\u5143\u7d20 target
\u7684\u7d22\u5f15\u3002\u82e5\u6570\u7ec4\u4e2d\u4e0d\u5305\u542b\u8be5\u5143\u7d20\uff0c\u5219\u8fd4\u56de \\(-1\\) \u3002
\u56de\u5fc6\u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\u7684\u65b9\u6cd5\uff0c\u641c\u7d22\u5b8c\u6210\u540e \\(i\\) \u6307\u5411\u6700\u5de6\u4e00\u4e2a target
\uff0c\u56e0\u6b64\u67e5\u627e\u63d2\u5165\u70b9\u672c\u8d28\u4e0a\u662f\u5728\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target
\u7684\u7d22\u5f15\u3002
\u8003\u8651\u901a\u8fc7\u67e5\u627e\u63d2\u5165\u70b9\u7684\u51fd\u6570\u5b9e\u73b0\u67e5\u627e\u5de6\u8fb9\u754c\u3002\u8bf7\u6ce8\u610f\uff0c\u6570\u7ec4\u4e2d\u53ef\u80fd\u4e0d\u5305\u542b target
\uff0c\u8fd9\u79cd\u60c5\u51b5\u53ef\u80fd\u5bfc\u81f4\u4ee5\u4e0b\u4e24\u79cd\u7ed3\u679c\u3002
nums[i]
\u4e0e target
\u4e0d\u76f8\u7b49\u3002\u5f53\u9047\u5230\u4ee5\u4e0a\u4e24\u79cd\u60c5\u51b5\u65f6\uff0c\u76f4\u63a5\u8fd4\u56de \\(-1\\) \u5373\u53ef\u3002\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig binary_search_edge.pydef binary_search_left_edge(nums: list[int], target: int) -> int:\n \"\"\"\u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target\"\"\"\n # \u7b49\u4ef7\u4e8e\u67e5\u627e target \u7684\u63d2\u5165\u70b9\n i = binary_search_insertion(nums, target)\n # \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n if i == len(nums) or nums[i] != target:\n return -1\n # \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\n return i\n
binary_search_edge.cpp/* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target */\nint binarySearchLeftEdge(vector<int> &nums, int target) {\n // \u7b49\u4ef7\u4e8e\u67e5\u627e target \u7684\u63d2\u5165\u70b9\n int i = binarySearchInsertion(nums, target);\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n if (i == nums.size() || nums[i] != target) {\n return -1;\n }\n // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\n return i;\n}\n
binary_search_edge.java/* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target */\nint binarySearchLeftEdge(int[] nums, int target) {\n // \u7b49\u4ef7\u4e8e\u67e5\u627e target \u7684\u63d2\u5165\u70b9\n int i = binary_search_insertion.binarySearchInsertion(nums, target);\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n if (i == nums.length || nums[i] != target) {\n return -1;\n }\n // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\n return i;\n}\n
binary_search_edge.cs/* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target */\nint BinarySearchLeftEdge(int[] nums, int target) {\n // \u7b49\u4ef7\u4e8e\u67e5\u627e target \u7684\u63d2\u5165\u70b9\n int i = binary_search_insertion.BinarySearchInsertion(nums, target);\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n if (i == nums.Length || nums[i] != target) {\n return -1;\n }\n // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\n return i;\n}\n
binary_search_edge.go/* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target */\nfunc binarySearchLeftEdge(nums []int, target int) int {\n // \u7b49\u4ef7\u4e8e\u67e5\u627e target \u7684\u63d2\u5165\u70b9\n i := binarySearchInsertion(nums, target)\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n if i == len(nums) || nums[i] != target {\n return -1\n }\n // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\n return i\n}\n
binary_search_edge.swift/* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target */\nfunc binarySearchLeftEdge(nums: [Int], target: Int) -> Int {\n // \u7b49\u4ef7\u4e8e\u67e5\u627e target \u7684\u63d2\u5165\u70b9\n let i = binarySearchInsertion(nums: nums, target: target)\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n if i == nums.count || nums[i] != target {\n return -1\n }\n // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\n return i\n}\n
binary_search_edge.js/* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target */\nfunction binarySearchLeftEdge(nums, target) {\n // \u7b49\u4ef7\u4e8e\u67e5\u627e target \u7684\u63d2\u5165\u70b9\n const i = binarySearchInsertion(nums, target);\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n if (i === nums.length || nums[i] !== target) {\n return -1;\n }\n // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\n return i;\n}\n
binary_search_edge.ts/* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target */\nfunction binarySearchLeftEdge(nums: Array<number>, target: number): number {\n // \u7b49\u4ef7\u4e8e\u67e5\u627e target \u7684\u63d2\u5165\u70b9\n const i = binarySearchInsertion(nums, target);\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n if (i === nums.length || nums[i] !== target) {\n return -1;\n }\n // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\n return i;\n}\n
binary_search_edge.dart/* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target */\nint binarySearchLeftEdge(List<int> nums, int target) {\n // \u7b49\u4ef7\u4e8e\u67e5\u627e target \u7684\u63d2\u5165\u70b9\n int i = binarySearchInsertion(nums, target);\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n if (i == nums.length || nums[i] != target) {\n return -1;\n }\n // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\n return i;\n}\n
binary_search_edge.rs/* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target */\nfn binary_search_left_edge(nums: &[i32], target: i32) -> i32 {\n // \u7b49\u4ef7\u4e8e\u67e5\u627e target \u7684\u63d2\u5165\u70b9\n let i = binary_search_insertion(nums, target);\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n if i == nums.len() as i32 || nums[i as usize] != target {\n return -1;\n }\n // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\n i\n}\n
binary_search_edge.c/* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target */\nint binarySearchLeftEdge(int *nums, int numSize, int target) {\n // \u7b49\u4ef7\u4e8e\u67e5\u627e target \u7684\u63d2\u5165\u70b9\n int i = binarySearchInsertion(nums, numSize, target);\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n if (i == numSize || nums[i] != target) {\n return -1;\n }\n // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\n return i;\n}\n
binary_search_edge.zig[class]{}-[func]{binarySearchLeftEdge}\n
"},{"location":"chapter_searching/binary_search_edge/#1032","title":"10.3.2 \u00a0 \u67e5\u627e\u53f3\u8fb9\u754c","text":"\u90a3\u4e48\u5982\u4f55\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target
\u5462\uff1f\u6700\u76f4\u63a5\u7684\u65b9\u5f0f\u662f\u4fee\u6539\u4ee3\u7801\uff0c\u66ff\u6362\u5728 nums[m] == target
\u60c5\u51b5\u4e0b\u7684\u6307\u9488\u6536\u7f29\u64cd\u4f5c\u3002\u4ee3\u7801\u5728\u6b64\u7701\u7565\uff0c\u6709\u5174\u8da3\u7684\u8bfb\u8005\u53ef\u4ee5\u81ea\u884c\u5b9e\u73b0\u3002
\u4e0b\u9762\u6211\u4eec\u4ecb\u7ecd\u4e24\u79cd\u66f4\u52a0\u53d6\u5de7\u7684\u65b9\u6cd5\u3002
"},{"location":"chapter_searching/binary_search_edge/#1","title":"1. \u00a0 \u590d\u7528\u67e5\u627e\u5de6\u8fb9\u754c","text":"\u5b9e\u9645\u4e0a\uff0c\u6211\u4eec\u53ef\u4ee5\u5229\u7528\u67e5\u627e\u6700\u5de6\u5143\u7d20\u7684\u51fd\u6570\u6765\u67e5\u627e\u6700\u53f3\u5143\u7d20\uff0c\u5177\u4f53\u65b9\u6cd5\u4e3a\uff1a\u5c06\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target
\u8f6c\u5316\u4e3a\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target + 1
\u3002
\u5982\u56fe 10-7 \u6240\u793a\uff0c\u67e5\u627e\u5b8c\u6210\u540e\uff0c\u6307\u9488 \\(i\\) \u6307\u5411\u6700\u5de6\u4e00\u4e2a target + 1
\uff08\u5982\u679c\u5b58\u5728\uff09\uff0c\u800c \\(j\\) \u6307\u5411\u6700\u53f3\u4e00\u4e2a target
\uff0c\u56e0\u6b64\u8fd4\u56de \\(j\\) \u5373\u53ef\u3002
\u56fe 10-7 \u00a0 \u5c06\u67e5\u627e\u53f3\u8fb9\u754c\u8f6c\u5316\u4e3a\u67e5\u627e\u5de6\u8fb9\u754c
\u8bf7\u6ce8\u610f\uff0c\u8fd4\u56de\u7684\u63d2\u5165\u70b9\u662f \\(i\\) \uff0c\u56e0\u6b64\u9700\u8981\u5c06\u5176\u51cf \\(1\\) \uff0c\u4ece\u800c\u83b7\u5f97 \\(j\\) \uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig binary_search_edge.pydef binary_search_right_edge(nums: list[int], target: int) -> int:\n \"\"\"\u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target\"\"\"\n # \u8f6c\u5316\u4e3a\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target + 1\n i = binary_search_insertion(nums, target + 1)\n # j \u6307\u5411\u6700\u53f3\u4e00\u4e2a target \uff0ci \u6307\u5411\u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\n j = i - 1\n # \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n if j == -1 or nums[j] != target:\n return -1\n # \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\n return j\n
binary_search_edge.cpp/* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target */\nint binarySearchRightEdge(vector<int> &nums, int target) {\n // \u8f6c\u5316\u4e3a\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target + 1\n int i = binarySearchInsertion(nums, target + 1);\n // j \u6307\u5411\u6700\u53f3\u4e00\u4e2a target \uff0ci \u6307\u5411\u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\n int j = i - 1;\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n if (j == -1 || nums[j] != target) {\n return -1;\n }\n // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\n return j;\n}\n
binary_search_edge.java/* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target */\nint binarySearchRightEdge(int[] nums, int target) {\n // \u8f6c\u5316\u4e3a\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target + 1\n int i = binary_search_insertion.binarySearchInsertion(nums, target + 1);\n // j \u6307\u5411\u6700\u53f3\u4e00\u4e2a target \uff0ci \u6307\u5411\u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\n int j = i - 1;\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n if (j == -1 || nums[j] != target) {\n return -1;\n }\n // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\n return j;\n}\n
binary_search_edge.cs/* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target */\nint BinarySearchRightEdge(int[] nums, int target) {\n // \u8f6c\u5316\u4e3a\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target + 1\n int i = binary_search_insertion.BinarySearchInsertion(nums, target + 1);\n // j \u6307\u5411\u6700\u53f3\u4e00\u4e2a target \uff0ci \u6307\u5411\u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\n int j = i - 1;\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n if (j == -1 || nums[j] != target) {\n return -1;\n }\n // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\n return j;\n}\n
binary_search_edge.go/* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target */\nfunc binarySearchRightEdge(nums []int, target int) int {\n // \u8f6c\u5316\u4e3a\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target + 1\n i := binarySearchInsertion(nums, target+1)\n // j \u6307\u5411\u6700\u53f3\u4e00\u4e2a target \uff0ci \u6307\u5411\u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\n j := i - 1\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n if j == -1 || nums[j] != target {\n return -1\n }\n // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\n return j\n}\n
binary_search_edge.swift/* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target */\nfunc binarySearchRightEdge(nums: [Int], target: Int) -> Int {\n // \u8f6c\u5316\u4e3a\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target + 1\n let i = binarySearchInsertion(nums: nums, target: target + 1)\n // j \u6307\u5411\u6700\u53f3\u4e00\u4e2a target \uff0ci \u6307\u5411\u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\n let j = i - 1\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n if j == -1 || nums[j] != target {\n return -1\n }\n // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\n return j\n}\n
binary_search_edge.js/* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target */\nfunction binarySearchRightEdge(nums, target) {\n // \u8f6c\u5316\u4e3a\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target + 1\n const i = binarySearchInsertion(nums, target + 1);\n // j \u6307\u5411\u6700\u53f3\u4e00\u4e2a target \uff0ci \u6307\u5411\u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\n const j = i - 1;\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n if (j === -1 || nums[j] !== target) {\n return -1;\n }\n // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\n return j;\n}\n
binary_search_edge.ts/* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target */\nfunction binarySearchRightEdge(nums: Array<number>, target: number): number {\n // \u8f6c\u5316\u4e3a\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target + 1\n const i = binarySearchInsertion(nums, target + 1);\n // j \u6307\u5411\u6700\u53f3\u4e00\u4e2a target \uff0ci \u6307\u5411\u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\n const j = i - 1;\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n if (j === -1 || nums[j] !== target) {\n return -1;\n }\n // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\n return j;\n}\n
binary_search_edge.dart/* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target */\nint binarySearchRightEdge(List<int> nums, int target) {\n // \u8f6c\u5316\u4e3a\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target + 1\n int i = binarySearchInsertion(nums, target + 1);\n // j \u6307\u5411\u6700\u53f3\u4e00\u4e2a target \uff0ci \u6307\u5411\u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\n int j = i - 1;\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n if (j == -1 || nums[j] != target) {\n return -1;\n }\n // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\n return j;\n}\n
binary_search_edge.rs/* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target */\nfn binary_search_right_edge(nums: &[i32], target: i32) -> i32 {\n // \u8f6c\u5316\u4e3a\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target + 1\n let i = binary_search_insertion(nums, target + 1);\n // j \u6307\u5411\u6700\u53f3\u4e00\u4e2a target \uff0ci \u6307\u5411\u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\n let j = i - 1;\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n if j == -1 || nums[j as usize] != target {\n return -1;\n }\n // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\n j\n}\n
binary_search_edge.c/* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target */\nint binarySearchRightEdge(int *nums, int numSize, int target) {\n // \u8f6c\u5316\u4e3a\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target + 1\n int i = binarySearchInsertion(nums, numSize, target + 1);\n // j \u6307\u5411\u6700\u53f3\u4e00\u4e2a target \uff0ci \u6307\u5411\u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\n int j = i - 1;\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n if (j == -1 || nums[j] != target) {\n return -1;\n }\n // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\n return j;\n}\n
binary_search_edge.zig[class]{}-[func]{binarySearchRightEdge}\n
"},{"location":"chapter_searching/binary_search_edge/#2","title":"2. \u00a0 \u8f6c\u5316\u4e3a\u67e5\u627e\u5143\u7d20","text":"\u6211\u4eec\u77e5\u9053\uff0c\u5f53\u6570\u7ec4\u4e0d\u5305\u542b target
\u65f6\uff0c\u6700\u7ec8 \\(i\\) \u548c \\(j\\) \u4f1a\u5206\u522b\u6307\u5411\u9996\u4e2a\u5927\u4e8e\u3001\u5c0f\u4e8e target
\u7684\u5143\u7d20\u3002
\u56e0\u6b64\uff0c\u5982\u56fe 10-8 \u6240\u793a\uff0c\u6211\u4eec\u53ef\u4ee5\u6784\u9020\u4e00\u4e2a\u6570\u7ec4\u4e2d\u4e0d\u5b58\u5728\u7684\u5143\u7d20\uff0c\u7528\u4e8e\u67e5\u627e\u5de6\u53f3\u8fb9\u754c\u3002
target
\uff1a\u53ef\u4ee5\u8f6c\u5316\u4e3a\u67e5\u627e target - 0.5
\uff0c\u5e76\u8fd4\u56de\u6307\u9488 \\(i\\) \u3002target
\uff1a\u53ef\u4ee5\u8f6c\u5316\u4e3a\u67e5\u627e target + 0.5
\uff0c\u5e76\u8fd4\u56de\u6307\u9488 \\(j\\) \u3002\u56fe 10-8 \u00a0 \u5c06\u67e5\u627e\u8fb9\u754c\u8f6c\u5316\u4e3a\u67e5\u627e\u5143\u7d20
\u4ee3\u7801\u5728\u6b64\u7701\u7565\uff0c\u4ee5\u4e0b\u4e24\u70b9\u503c\u5f97\u6ce8\u610f\u3002
target
\u6539\u4e3a\u6d6e\u70b9\u6570\u7c7b\u578b\u3002\u4e8c\u5206\u67e5\u627e\u4e0d\u4ec5\u53ef\u7528\u4e8e\u641c\u7d22\u76ee\u6807\u5143\u7d20\uff0c\u8fd8\u53ef\u7528\u4e8e\u89e3\u51b3\u8bb8\u591a\u53d8\u79cd\u95ee\u9898\uff0c\u6bd4\u5982\u641c\u7d22\u76ee\u6807\u5143\u7d20\u7684\u63d2\u5165\u4f4d\u7f6e\u3002
"},{"location":"chapter_searching/binary_search_insertion/#1021","title":"10.2.1 \u00a0 \u65e0\u91cd\u590d\u5143\u7d20\u7684\u60c5\u51b5","text":"Question
\u7ed9\u5b9a\u4e00\u4e2a\u957f\u5ea6\u4e3a \\(n\\) \u7684\u6709\u5e8f\u6570\u7ec4 nums
\u548c\u4e00\u4e2a\u5143\u7d20 target
\uff0c\u6570\u7ec4\u4e0d\u5b58\u5728\u91cd\u590d\u5143\u7d20\u3002\u73b0\u5c06 target
\u63d2\u5165\u6570\u7ec4 nums
\u4e2d\uff0c\u5e76\u4fdd\u6301\u5176\u6709\u5e8f\u6027\u3002\u82e5\u6570\u7ec4\u4e2d\u5df2\u5b58\u5728\u5143\u7d20 target
\uff0c\u5219\u63d2\u5165\u5230\u5176\u5de6\u65b9\u3002\u8bf7\u8fd4\u56de\u63d2\u5165\u540e target
\u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15\u3002
\u56fe 10-4 \u00a0 \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\u793a\u4f8b\u6570\u636e
\u5982\u679c\u60f3\u590d\u7528\u4e0a\u4e00\u8282\u7684\u4e8c\u5206\u67e5\u627e\u4ee3\u7801\uff0c\u5219\u9700\u8981\u56de\u7b54\u4ee5\u4e0b\u4e24\u4e2a\u95ee\u9898\u3002
\u95ee\u9898\u4e00\uff1a\u5f53\u6570\u7ec4\u4e2d\u5305\u542b target
\u65f6\uff0c\u63d2\u5165\u70b9\u7684\u7d22\u5f15\u662f\u5426\u662f\u8be5\u5143\u7d20\u7684\u7d22\u5f15\uff1f
\u9898\u76ee\u8981\u6c42\u5c06 target
\u63d2\u5165\u5230\u76f8\u7b49\u5143\u7d20\u7684\u5de6\u8fb9\uff0c\u8fd9\u610f\u5473\u7740\u65b0\u63d2\u5165\u7684 target
\u66ff\u6362\u4e86\u539f\u6765 target
\u7684\u4f4d\u7f6e\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5f53\u6570\u7ec4\u5305\u542b target
\u65f6\uff0c\u63d2\u5165\u70b9\u7684\u7d22\u5f15\u5c31\u662f\u8be5 target
\u7684\u7d22\u5f15\u3002
\u95ee\u9898\u4e8c\uff1a\u5f53\u6570\u7ec4\u4e2d\u4e0d\u5b58\u5728 target
\u65f6\uff0c\u63d2\u5165\u70b9\u662f\u54ea\u4e2a\u5143\u7d20\u7684\u7d22\u5f15\uff1f
\u8fdb\u4e00\u6b65\u601d\u8003\u4e8c\u5206\u67e5\u627e\u8fc7\u7a0b\uff1a\u5f53 nums[m] < target
\u65f6 \\(i\\) \u79fb\u52a8\uff0c\u8fd9\u610f\u5473\u7740\u6307\u9488 \\(i\\) \u5728\u5411\u5927\u4e8e\u7b49\u4e8e target
\u7684\u5143\u7d20\u9760\u8fd1\u3002\u540c\u7406\uff0c\u6307\u9488 \\(j\\) \u59cb\u7ec8\u5728\u5411\u5c0f\u4e8e\u7b49\u4e8e target
\u7684\u5143\u7d20\u9760\u8fd1\u3002
\u56e0\u6b64\u4e8c\u5206\u7ed3\u675f\u65f6\u4e00\u5b9a\u6709\uff1a\\(i\\) \u6307\u5411\u9996\u4e2a\u5927\u4e8e target
\u7684\u5143\u7d20\uff0c\\(j\\) \u6307\u5411\u9996\u4e2a\u5c0f\u4e8e target
\u7684\u5143\u7d20\u3002\u6613\u5f97\u5f53\u6570\u7ec4\u4e0d\u5305\u542b target
\u65f6\uff0c\u63d2\u5165\u7d22\u5f15\u4e3a \\(i\\) \u3002\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a
def binary_search_insertion_simple(nums: list[int], target: int) -> int:\n \"\"\"\u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u65e0\u91cd\u590d\u5143\u7d20\uff09\"\"\"\n i, j = 0, len(nums) - 1 # \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n while i <= j:\n m = (i + j) // 2 # \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n if nums[m] < target:\n i = m + 1 # target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n elif nums[m] > target:\n j = m - 1 # target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n else:\n return m # \u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 m\n # \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 i\n return i\n
binary_search_insertion.cpp/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u65e0\u91cd\u590d\u5143\u7d20\uff09 */\nint binarySearchInsertionSimple(vector<int> &nums, int target) {\n int i = 0, j = nums.size() - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n while (i <= j) {\n int m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n if (nums[m] < target) {\n i = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n } else if (nums[m] > target) {\n j = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n } else {\n return m; // \u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 m\n }\n }\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 i\n return i;\n}\n
binary_search_insertion.java/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u65e0\u91cd\u590d\u5143\u7d20\uff09 */\nint binarySearchInsertionSimple(int[] nums, int target) {\n int i = 0, j = nums.length - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n while (i <= j) {\n int m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n if (nums[m] < target) {\n i = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n } else if (nums[m] > target) {\n j = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n } else {\n return m; // \u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 m\n }\n }\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 i\n return i;\n}\n
binary_search_insertion.cs/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u65e0\u91cd\u590d\u5143\u7d20\uff09 */\nint BinarySearchInsertionSimple(int[] nums, int target) {\n int i = 0, j = nums.Length - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n while (i <= j) {\n int m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n if (nums[m] < target) {\n i = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n } else if (nums[m] > target) {\n j = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n } else {\n return m; // \u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 m\n }\n }\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 i\n return i;\n}\n
binary_search_insertion.go/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u65e0\u91cd\u590d\u5143\u7d20\uff09 */\nfunc binarySearchInsertionSimple(nums []int, target int) int {\n // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n i, j := 0, len(nums)-1\n for i <= j {\n // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n m := i + (j-i)/2\n if nums[m] < target {\n // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n i = m + 1\n } else if nums[m] > target {\n // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n j = m - 1\n } else {\n // \u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 m\n return m\n }\n }\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 i\n return i\n}\n
binary_search_insertion.swift/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u65e0\u91cd\u590d\u5143\u7d20\uff09 */\nfunc binarySearchInsertionSimple(nums: [Int], target: Int) -> Int {\n var i = 0, j = nums.count - 1 // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n while i <= j {\n let m = i + (j - i) / 2 // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n if nums[m] < target {\n i = m + 1 // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n } else if nums[m] > target {\n j = m - 1 // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n } else {\n return m // \u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 m\n }\n }\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 i\n return i\n}\n
binary_search_insertion.js/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u65e0\u91cd\u590d\u5143\u7d20\uff09 */\nfunction binarySearchInsertionSimple(nums, target) {\n let i = 0,\n j = nums.length - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n while (i <= j) {\n const m = Math.floor(i + (j - i) / 2); // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m, \u4f7f\u7528 Math.floor() \u5411\u4e0b\u53d6\u6574\n if (nums[m] < target) {\n i = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n } else if (nums[m] > target) {\n j = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n } else {\n return m; // \u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 m\n }\n }\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 i\n return i;\n}\n
binary_search_insertion.ts/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u65e0\u91cd\u590d\u5143\u7d20\uff09 */\nfunction binarySearchInsertionSimple(\n nums: Array<number>,\n target: number\n): number {\n let i = 0,\n j = nums.length - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n while (i <= j) {\n const m = Math.floor(i + (j - i) / 2); // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m, \u4f7f\u7528 Math.floor() \u5411\u4e0b\u53d6\u6574\n if (nums[m] < target) {\n i = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n } else if (nums[m] > target) {\n j = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n } else {\n return m; // \u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 m\n }\n }\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 i\n return i;\n}\n
binary_search_insertion.dart/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u65e0\u91cd\u590d\u5143\u7d20\uff09 */\nint binarySearchInsertionSimple(List<int> nums, int target) {\n int i = 0, j = nums.length - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n while (i <= j) {\n int m = i + (j - i) ~/ 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n if (nums[m] < target) {\n i = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n } else if (nums[m] > target) {\n j = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n } else {\n return m; // \u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 m\n }\n }\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 i\n return i;\n}\n
binary_search_insertion.rs/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff09 */\npub fn binary_search_insertion(nums: &[i32], target: i32) -> i32 {\n let (mut i, mut j) = (0, nums.len() as i32 - 1); // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n while i <= j {\n let m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n if nums[m as usize] < target {\n i = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n } else if nums[m as usize] > target {\n j = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n } else {\n j = m - 1; // \u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [i, m-1] \u4e2d\n }\n }\n // \u8fd4\u56de\u63d2\u5165\u70b9 i\n i\n}\n
binary_search_insertion.c/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u65e0\u91cd\u590d\u5143\u7d20\uff09 */\nint binarySearchInsertionSimple(int *nums, int numSize, int target) {\n int i = 0, j = numSize - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n while (i <= j) {\n int m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n if (nums[m] < target) {\n i = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n } else if (nums[m] > target) {\n j = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n } else {\n return m; // \u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 m\n }\n }\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 i\n return i;\n}\n
binary_search_insertion.zig[class]{}-[func]{binarySearchInsertionSimple}\n
"},{"location":"chapter_searching/binary_search_insertion/#1022","title":"10.2.2 \u00a0 \u5b58\u5728\u91cd\u590d\u5143\u7d20\u7684\u60c5\u51b5","text":"Question
\u5728\u4e0a\u4e00\u9898\u7684\u57fa\u7840\u4e0a\uff0c\u89c4\u5b9a\u6570\u7ec4\u53ef\u80fd\u5305\u542b\u91cd\u590d\u5143\u7d20\uff0c\u5176\u4f59\u4e0d\u53d8\u3002
\u5047\u8bbe\u6570\u7ec4\u4e2d\u5b58\u5728\u591a\u4e2a target
\uff0c\u5219\u666e\u901a\u4e8c\u5206\u67e5\u627e\u53ea\u80fd\u8fd4\u56de\u5176\u4e2d\u4e00\u4e2a target
\u7684\u7d22\u5f15\uff0c\u800c\u65e0\u6cd5\u786e\u5b9a\u8be5\u5143\u7d20\u7684\u5de6\u8fb9\u548c\u53f3\u8fb9\u8fd8\u6709\u591a\u5c11 target
\u3002
\u9898\u76ee\u8981\u6c42\u5c06\u76ee\u6807\u5143\u7d20\u63d2\u5165\u5230\u6700\u5de6\u8fb9\uff0c\u6240\u4ee5\u6211\u4eec\u9700\u8981\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5de6\u4e00\u4e2a target
\u7684\u7d22\u5f15\u3002\u521d\u6b65\u8003\u8651\u901a\u8fc7\u56fe 10-5 \u6240\u793a\u7684\u6b65\u9aa4\u5b9e\u73b0\u3002
target
\u7684\u7d22\u5f15\uff0c\u8bb0\u4e3a \\(k\\) \u3002target
\u65f6\u8fd4\u56de\u3002\u56fe 10-5 \u00a0 \u7ebf\u6027\u67e5\u627e\u91cd\u590d\u5143\u7d20\u7684\u63d2\u5165\u70b9
\u6b64\u65b9\u6cd5\u867d\u7136\u53ef\u7528\uff0c\u4f46\u5176\u5305\u542b\u7ebf\u6027\u67e5\u627e\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002\u5f53\u6570\u7ec4\u4e2d\u5b58\u5728\u5f88\u591a\u91cd\u590d\u7684 target
\u65f6\uff0c\u8be5\u65b9\u6cd5\u6548\u7387\u5f88\u4f4e\u3002
\u73b0\u8003\u8651\u62d3\u5c55\u4e8c\u5206\u67e5\u627e\u4ee3\u7801\u3002\u5982\u56fe 10-6 \u6240\u793a\uff0c\u6574\u4f53\u6d41\u7a0b\u4fdd\u6301\u4e0d\u53d8\uff0c\u6bcf\u8f6e\u5148\u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 \\(m\\) \uff0c\u518d\u5224\u65ad target
\u548c nums[m]
\u7684\u5927\u5c0f\u5173\u7cfb\uff0c\u5206\u4e3a\u4ee5\u4e0b\u51e0\u79cd\u60c5\u51b5\u3002
nums[m] < target
\u6216 nums[m] > target
\u65f6\uff0c\u8bf4\u660e\u8fd8\u6ca1\u6709\u627e\u5230 target
\uff0c\u56e0\u6b64\u91c7\u7528\u666e\u901a\u4e8c\u5206\u67e5\u627e\u7684\u7f29\u5c0f\u533a\u95f4\u64cd\u4f5c\uff0c\u4ece\u800c\u4f7f\u6307\u9488 \\(i\\) \u548c \\(j\\) \u5411 target
\u9760\u8fd1\u3002nums[m] == target
\u65f6\uff0c\u8bf4\u660e\u5c0f\u4e8e target
\u7684\u5143\u7d20\u5728\u533a\u95f4 \\([i, m - 1]\\) \u4e2d\uff0c\u56e0\u6b64\u91c7\u7528 \\(j = m - 1\\) \u6765\u7f29\u5c0f\u533a\u95f4\uff0c\u4ece\u800c\u4f7f\u6307\u9488 \\(j\\) \u5411\u5c0f\u4e8e target
\u7684\u5143\u7d20\u9760\u8fd1\u3002\u5faa\u73af\u5b8c\u6210\u540e\uff0c\\(i\\) \u6307\u5411\u6700\u5de6\u8fb9\u7684 target
\uff0c\\(j\\) \u6307\u5411\u9996\u4e2a\u5c0f\u4e8e target
\u7684\u5143\u7d20\uff0c\u56e0\u6b64\u7d22\u5f15 \\(i\\) \u5c31\u662f\u63d2\u5165\u70b9\u3002
\u56fe 10-6 \u00a0 \u4e8c\u5206\u67e5\u627e\u91cd\u590d\u5143\u7d20\u7684\u63d2\u5165\u70b9\u7684\u6b65\u9aa4
\u89c2\u5bdf\u4ee5\u4e0b\u4ee3\u7801\uff0c\u5224\u65ad\u5206\u652f nums[m] > target
\u548c nums[m] == target
\u7684\u64cd\u4f5c\u76f8\u540c\uff0c\u56e0\u6b64\u4e24\u8005\u53ef\u4ee5\u5408\u5e76\u3002
\u5373\u4fbf\u5982\u6b64\uff0c\u6211\u4eec\u4ecd\u7136\u53ef\u4ee5\u5c06\u5224\u65ad\u6761\u4ef6\u4fdd\u6301\u5c55\u5f00\uff0c\u56e0\u4e3a\u5176\u903b\u8f91\u66f4\u52a0\u6e05\u6670\u3001\u53ef\u8bfb\u6027\u66f4\u597d\u3002
PythonC++JavaC#GoSwiftJSTSDartRustCZig binary_search_insertion.pydef binary_search_insertion(nums: list[int], target: int) -> int:\n \"\"\"\u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff09\"\"\"\n i, j = 0, len(nums) - 1 # \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n while i <= j:\n m = (i + j) // 2 # \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n if nums[m] < target:\n i = m + 1 # target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n elif nums[m] > target:\n j = m - 1 # target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n else:\n j = m - 1 # \u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [i, m-1] \u4e2d\n # \u8fd4\u56de\u63d2\u5165\u70b9 i\n return i\n
binary_search_insertion.cpp/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff09 */\nint binarySearchInsertion(vector<int> &nums, int target) {\n int i = 0, j = nums.size() - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n while (i <= j) {\n int m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n if (nums[m] < target) {\n i = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n } else if (nums[m] > target) {\n j = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n } else {\n j = m - 1; // \u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [i, m-1] \u4e2d\n }\n }\n // \u8fd4\u56de\u63d2\u5165\u70b9 i\n return i;\n}\n
binary_search_insertion.java/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff09 */\nint binarySearchInsertion(int[] nums, int target) {\n int i = 0, j = nums.length - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n while (i <= j) {\n int m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n if (nums[m] < target) {\n i = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n } else if (nums[m] > target) {\n j = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n } else {\n j = m - 1; // \u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [i, m-1] \u4e2d\n }\n }\n // \u8fd4\u56de\u63d2\u5165\u70b9 i\n return i;\n}\n
binary_search_insertion.cs/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff09 */\nint BinarySearchInsertion(int[] nums, int target) {\n int i = 0, j = nums.Length - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n while (i <= j) {\n int m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n if (nums[m] < target) {\n i = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n } else if (nums[m] > target) {\n j = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n } else {\n j = m - 1; // \u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [i, m-1] \u4e2d\n }\n }\n // \u8fd4\u56de\u63d2\u5165\u70b9 i\n return i;\n}\n
binary_search_insertion.go/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff09 */\nfunc binarySearchInsertion(nums []int, target int) int {\n // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n i, j := 0, len(nums)-1\n for i <= j {\n // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n m := i + (j-i)/2\n if nums[m] < target {\n // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n i = m + 1\n } else if nums[m] > target {\n // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n j = m - 1\n } else {\n // \u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [i, m-1] \u4e2d\n j = m - 1\n }\n }\n // \u8fd4\u56de\u63d2\u5165\u70b9 i\n return i\n}\n
binary_search_insertion.swift/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff09 */\nfunc binarySearchInsertion(nums: [Int], target: Int) -> Int {\n var i = 0, j = nums.count - 1 // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n while i <= j {\n let m = i + (j - i) / 2 // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n if nums[m] < target {\n i = m + 1 // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n } else if nums[m] > target {\n j = m - 1 // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n } else {\n j = m - 1 // \u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [i, m-1] \u4e2d\n }\n }\n // \u8fd4\u56de\u63d2\u5165\u70b9 i\n return i\n}\n
binary_search_insertion.js/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff09 */\nfunction binarySearchInsertion(nums, target) {\n let i = 0,\n j = nums.length - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n while (i <= j) {\n const m = Math.floor(i + (j - i) / 2); // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m, \u4f7f\u7528 Math.floor() \u5411\u4e0b\u53d6\u6574\n if (nums[m] < target) {\n i = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n } else if (nums[m] > target) {\n j = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n } else {\n j = m - 1; // \u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [i, m-1] \u4e2d\n }\n }\n // \u8fd4\u56de\u63d2\u5165\u70b9 i\n return i;\n}\n
binary_search_insertion.ts/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff09 */\nfunction binarySearchInsertion(nums: Array<number>, target: number): number {\n let i = 0,\n j = nums.length - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n while (i <= j) {\n const m = Math.floor(i + (j - i) / 2); // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m, \u4f7f\u7528 Math.floor() \u5411\u4e0b\u53d6\u6574\n if (nums[m] < target) {\n i = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n } else if (nums[m] > target) {\n j = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n } else {\n j = m - 1; // \u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [i, m-1] \u4e2d\n }\n }\n // \u8fd4\u56de\u63d2\u5165\u70b9 i\n return i;\n}\n
binary_search_insertion.dart/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff09 */\nint binarySearchInsertion(List<int> nums, int target) {\n int i = 0, j = nums.length - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n while (i <= j) {\n int m = i + (j - i) ~/ 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n if (nums[m] < target) {\n i = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n } else if (nums[m] > target) {\n j = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n } else {\n j = m - 1; // \u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [i, m-1] \u4e2d\n }\n }\n // \u8fd4\u56de\u63d2\u5165\u70b9 i\n return i;\n}\n
binary_search_insertion.rs/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff09 */\npub fn binary_search_insertion(nums: &[i32], target: i32) -> i32 {\n let (mut i, mut j) = (0, nums.len() as i32 - 1); // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n while i <= j {\n let m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n if nums[m as usize] < target {\n i = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n } else if nums[m as usize] > target {\n j = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n } else {\n j = m - 1; // \u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [i, m-1] \u4e2d\n }\n }\n // \u8fd4\u56de\u63d2\u5165\u70b9 i\n i\n}\n
binary_search_insertion.c/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff09 */\nint binarySearchInsertion(int *nums, int numSize, int target) {\n int i = 0, j = numSize - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\n while (i <= j) {\n int m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\n if (nums[m] < target) {\n i = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n } else if (nums[m] > target) {\n j = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n } else {\n j = m - 1; // \u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [i, m-1] \u4e2d\n }\n }\n // \u8fd4\u56de\u63d2\u5165\u70b9 i\n return i;\n}\n
binary_search_insertion.zig[class]{}-[func]{binarySearchInsertion}\n
Tip
\u672c\u8282\u7684\u4ee3\u7801\u90fd\u662f\u201c\u53cc\u95ed\u533a\u95f4\u201d\u5199\u6cd5\u3002\u6709\u5174\u8da3\u7684\u8bfb\u8005\u53ef\u4ee5\u81ea\u884c\u5b9e\u73b0\u201c\u5de6\u95ed\u53f3\u5f00\u201d\u5199\u6cd5\u3002
\u603b\u7684\u6765\u770b\uff0c\u4e8c\u5206\u67e5\u627e\u65e0\u975e\u5c31\u662f\u7ed9\u6307\u9488 \\(i\\) \u548c \\(j\\) \u5206\u522b\u8bbe\u5b9a\u641c\u7d22\u76ee\u6807\uff0c\u76ee\u6807\u53ef\u80fd\u662f\u4e00\u4e2a\u5177\u4f53\u7684\u5143\u7d20\uff08\u4f8b\u5982 target
\uff09\uff0c\u4e5f\u53ef\u80fd\u662f\u4e00\u4e2a\u5143\u7d20\u8303\u56f4\uff08\u4f8b\u5982\u5c0f\u4e8e target
\u7684\u5143\u7d20\uff09\u3002
\u5728\u4e0d\u65ad\u7684\u5faa\u73af\u4e8c\u5206\u4e2d\uff0c\u6307\u9488 \\(i\\) \u548c \\(j\\) \u90fd\u9010\u6e10\u903c\u8fd1\u9884\u5148\u8bbe\u5b9a\u7684\u76ee\u6807\u3002\u6700\u7ec8\uff0c\u5b83\u4eec\u6216\u662f\u6210\u529f\u627e\u5230\u7b54\u6848\uff0c\u6216\u662f\u8d8a\u8fc7\u8fb9\u754c\u540e\u505c\u6b62\u3002
"},{"location":"chapter_searching/replace_linear_by_hashing/","title":"10.4 \u00a0 \u54c8\u5e0c\u4f18\u5316\u7b56\u7565","text":"\u5728\u7b97\u6cd5\u9898\u4e2d\uff0c\u6211\u4eec\u5e38\u901a\u8fc7\u5c06\u7ebf\u6027\u67e5\u627e\u66ff\u6362\u4e3a\u54c8\u5e0c\u67e5\u627e\u6765\u964d\u4f4e\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u3002\u6211\u4eec\u501f\u52a9\u4e00\u4e2a\u7b97\u6cd5\u9898\u6765\u52a0\u6df1\u7406\u89e3\u3002
Question
\u7ed9\u5b9a\u4e00\u4e2a\u6574\u6570\u6570\u7ec4 nums
\u548c\u4e00\u4e2a\u76ee\u6807\u5143\u7d20 target
\uff0c\u8bf7\u5728\u6570\u7ec4\u4e2d\u641c\u7d22\u201c\u548c\u201d\u4e3a target
\u7684\u4e24\u4e2a\u5143\u7d20\uff0c\u5e76\u8fd4\u56de\u5b83\u4eec\u7684\u6570\u7ec4\u7d22\u5f15\u3002\u8fd4\u56de\u4efb\u610f\u4e00\u4e2a\u89e3\u5373\u53ef\u3002
\u8003\u8651\u76f4\u63a5\u904d\u5386\u6240\u6709\u53ef\u80fd\u7684\u7ec4\u5408\u3002\u5982\u56fe 10-9 \u6240\u793a\uff0c\u6211\u4eec\u5f00\u542f\u4e00\u4e2a\u4e24\u5c42\u5faa\u73af\uff0c\u5728\u6bcf\u8f6e\u4e2d\u5224\u65ad\u4e24\u4e2a\u6574\u6570\u7684\u548c\u662f\u5426\u4e3a target
\uff0c\u82e5\u662f\uff0c\u5219\u8fd4\u56de\u5b83\u4eec\u7684\u7d22\u5f15\u3002
\u56fe 10-9 \u00a0 \u7ebf\u6027\u67e5\u627e\u6c42\u89e3\u4e24\u6570\u4e4b\u548c
\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig two_sum.pydef two_sum_brute_force(nums: list[int], target: int) -> list[int]:\n \"\"\"\u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e\"\"\"\n # \u4e24\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n^2)\n for i in range(len(nums) - 1):\n for j in range(i + 1, len(nums)):\n if nums[i] + nums[j] == target:\n return [i, j]\n return []\n
two_sum.cpp/* \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e */\nvector<int> twoSumBruteForce(vector<int> &nums, int target) {\n int size = nums.size();\n // \u4e24\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n^2)\n for (int i = 0; i < size - 1; i++) {\n for (int j = i + 1; j < size; j++) {\n if (nums[i] + nums[j] == target)\n return {i, j};\n }\n }\n return {};\n}\n
two_sum.java/* \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e */\nint[] twoSumBruteForce(int[] nums, int target) {\n int size = nums.length;\n // \u4e24\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n^2)\n for (int i = 0; i < size - 1; i++) {\n for (int j = i + 1; j < size; j++) {\n if (nums[i] + nums[j] == target)\n return new int[] { i, j };\n }\n }\n return new int[0];\n}\n
two_sum.cs/* \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e */\nint[] TwoSumBruteForce(int[] nums, int target) {\n int size = nums.Length;\n // \u4e24\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n^2)\n for (int i = 0; i < size - 1; i++) {\n for (int j = i + 1; j < size; j++) {\n if (nums[i] + nums[j] == target)\n return [i, j];\n }\n }\n return [];\n}\n
two_sum.go/* \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e */\nfunc twoSumBruteForce(nums []int, target int) []int {\n size := len(nums)\n // \u4e24\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n^2)\n for i := 0; i < size-1; i++ {\n for j := i + 1; i < size; j++ {\n if nums[i]+nums[j] == target {\n return []int{i, j}\n }\n }\n }\n return nil\n}\n
two_sum.swift/* \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e */\nfunc twoSumBruteForce(nums: [Int], target: Int) -> [Int] {\n // \u4e24\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n^2)\n for i in nums.indices.dropLast() {\n for j in nums.indices.dropFirst(i + 1) {\n if nums[i] + nums[j] == target {\n return [i, j]\n }\n }\n }\n return [0]\n}\n
two_sum.js/* \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e */\nfunction twoSumBruteForce(nums, target) {\n const n = nums.length;\n // \u4e24\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n^2)\n for (let i = 0; i < n; i++) {\n for (let j = i + 1; j < n; j++) {\n if (nums[i] + nums[j] === target) {\n return [i, j];\n }\n }\n }\n return [];\n}\n
two_sum.ts/* \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e */\nfunction twoSumBruteForce(nums: number[], target: number): number[] {\n const n = nums.length;\n // \u4e24\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n^2)\n for (let i = 0; i < n; i++) {\n for (let j = i + 1; j < n; j++) {\n if (nums[i] + nums[j] === target) {\n return [i, j];\n }\n }\n }\n return [];\n}\n
two_sum.dart/* \u65b9\u6cd5\u4e00\uff1a \u66b4\u529b\u679a\u4e3e */\nList<int> twoSumBruteForce(List<int> nums, int target) {\n int size = nums.length;\n // \u4e24\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n^2)\n for (var i = 0; i < size - 1; i++) {\n for (var j = i + 1; j < size; j++) {\n if (nums[i] + nums[j] == target) return [i, j];\n }\n }\n return [0];\n}\n
two_sum.rs/* \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e */\npub fn two_sum_brute_force(nums: &Vec<i32>, target: i32) -> Option<Vec<i32>> {\n let size = nums.len();\n // \u4e24\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n^2)\n for i in 0..size - 1 {\n for j in i + 1..size {\n if nums[i] + nums[j] == target {\n return Some(vec![i as i32, j as i32]);\n }\n }\n }\n None\n}\n
two_sum.c/* \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e */\nint *twoSumBruteForce(int *nums, int numsSize, int target, int *returnSize) {\n for (int i = 0; i < numsSize; ++i) {\n for (int j = i + 1; j < numsSize; ++j) {\n if (nums[i] + nums[j] == target) {\n int *res = malloc(sizeof(int) * 2);\n res[0] = i, res[1] = j;\n *returnSize = 2;\n return res;\n }\n }\n }\n *returnSize = 0;\n return NULL;\n}\n
two_sum.zig// \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e\nfn twoSumBruteForce(nums: []i32, target: i32) ?[2]i32 {\n var size: usize = nums.len;\n var i: usize = 0;\n // \u4e24\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n^2)\n while (i < size - 1) : (i += 1) {\n var j = i + 1;\n while (j < size) : (j += 1) {\n if (nums[i] + nums[j] == target) {\n return [_]i32{@intCast(i), @intCast(j)};\n }\n }\n }\n return null;\n}\n
\u6b64\u65b9\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\) \uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(1)\\) \uff0c\u5728\u5927\u6570\u636e\u91cf\u4e0b\u975e\u5e38\u8017\u65f6\u3002
"},{"location":"chapter_searching/replace_linear_by_hashing/#1042","title":"10.4.2 \u00a0 \u54c8\u5e0c\u67e5\u627e\uff1a\u4ee5\u7a7a\u95f4\u6362\u65f6\u95f4","text":"\u8003\u8651\u501f\u52a9\u4e00\u4e2a\u54c8\u5e0c\u8868\uff0c\u952e\u503c\u5bf9\u5206\u522b\u4e3a\u6570\u7ec4\u5143\u7d20\u548c\u5143\u7d20\u7d22\u5f15\u3002\u5faa\u73af\u904d\u5386\u6570\u7ec4\uff0c\u6bcf\u8f6e\u6267\u884c\u56fe 10-10 \u6240\u793a\u7684\u6b65\u9aa4\u3002
target - nums[i]
\u662f\u5426\u5728\u54c8\u5e0c\u8868\u4e2d\uff0c\u82e5\u662f\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u8fd9\u4e24\u4e2a\u5143\u7d20\u7684\u7d22\u5f15\u3002nums[i]
\u548c\u7d22\u5f15 i
\u6dfb\u52a0\u8fdb\u54c8\u5e0c\u8868\u3002\u56fe 10-10 \u00a0 \u8f85\u52a9\u54c8\u5e0c\u8868\u6c42\u89e3\u4e24\u6570\u4e4b\u548c
\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff0c\u4ec5\u9700\u5355\u5c42\u5faa\u73af\u5373\u53ef\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig two_sum.pydef two_sum_hash_table(nums: list[int], target: int) -> list[int]:\n \"\"\"\u65b9\u6cd5\u4e8c\uff1a\u8f85\u52a9\u54c8\u5e0c\u8868\"\"\"\n # \u8f85\u52a9\u54c8\u5e0c\u8868\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n dic = {}\n # \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n for i in range(len(nums)):\n if target - nums[i] in dic:\n return [dic[target - nums[i]], i]\n dic[nums[i]] = i\n return []\n
two_sum.cpp/* \u65b9\u6cd5\u4e8c\uff1a\u8f85\u52a9\u54c8\u5e0c\u8868 */\nvector<int> twoSumHashTable(vector<int> &nums, int target) {\n int size = nums.size();\n // \u8f85\u52a9\u54c8\u5e0c\u8868\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n unordered_map<int, int> dic;\n // \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n for (int i = 0; i < size; i++) {\n if (dic.find(target - nums[i]) != dic.end()) {\n return {dic[target - nums[i]], i};\n }\n dic.emplace(nums[i], i);\n }\n return {};\n}\n
two_sum.java/* \u65b9\u6cd5\u4e8c\uff1a\u8f85\u52a9\u54c8\u5e0c\u8868 */\nint[] twoSumHashTable(int[] nums, int target) {\n int size = nums.length;\n // \u8f85\u52a9\u54c8\u5e0c\u8868\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n Map<Integer, Integer> dic = new HashMap<>();\n // \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n for (int i = 0; i < size; i++) {\n if (dic.containsKey(target - nums[i])) {\n return new int[] { dic.get(target - nums[i]), i };\n }\n dic.put(nums[i], i);\n }\n return new int[0];\n}\n
two_sum.cs/* \u65b9\u6cd5\u4e8c\uff1a\u8f85\u52a9\u54c8\u5e0c\u8868 */\nint[] TwoSumHashTable(int[] nums, int target) {\n int size = nums.Length;\n // \u8f85\u52a9\u54c8\u5e0c\u8868\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n Dictionary<int, int> dic = [];\n // \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n for (int i = 0; i < size; i++) {\n if (dic.ContainsKey(target - nums[i])) {\n return [dic[target - nums[i]], i];\n }\n dic.Add(nums[i], i);\n }\n return [];\n}\n
two_sum.go/* \u65b9\u6cd5\u4e8c\uff1a\u8f85\u52a9\u54c8\u5e0c\u8868 */\nfunc twoSumHashTable(nums []int, target int) []int {\n // \u8f85\u52a9\u54c8\u5e0c\u8868\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n hashTable := map[int]int{}\n // \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n for idx, val := range nums {\n if preIdx, ok := hashTable[target-val]; ok {\n return []int{preIdx, idx}\n }\n hashTable[val] = idx\n }\n return nil\n}\n
two_sum.swift/* \u65b9\u6cd5\u4e8c\uff1a\u8f85\u52a9\u54c8\u5e0c\u8868 */\nfunc twoSumHashTable(nums: [Int], target: Int) -> [Int] {\n // \u8f85\u52a9\u54c8\u5e0c\u8868\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n var dic: [Int: Int] = [:]\n // \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n for i in nums.indices {\n if let j = dic[target - nums[i]] {\n return [j, i]\n }\n dic[nums[i]] = i\n }\n return [0]\n}\n
two_sum.js/* \u65b9\u6cd5\u4e8c\uff1a\u8f85\u52a9\u54c8\u5e0c\u8868 */\nfunction twoSumHashTable(nums, target) {\n // \u8f85\u52a9\u54c8\u5e0c\u8868\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n let m = {};\n // \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n for (let i = 0; i < nums.length; i++) {\n if (m[target - nums[i]] !== undefined) {\n return [m[target - nums[i]], i];\n } else {\n m[nums[i]] = i;\n }\n }\n return [];\n}\n
two_sum.ts/* \u65b9\u6cd5\u4e8c\uff1a\u8f85\u52a9\u54c8\u5e0c\u8868 */\nfunction twoSumHashTable(nums: number[], target: number): number[] {\n // \u8f85\u52a9\u54c8\u5e0c\u8868\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n let m: Map<number, number> = new Map();\n // \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n for (let i = 0; i < nums.length; i++) {\n let index = m.get(target - nums[i]);\n if (index !== undefined) {\n return [index, i];\n } else {\n m.set(nums[i], i);\n }\n }\n return [];\n}\n
two_sum.dart/* \u65b9\u6cd5\u4e8c\uff1a \u8f85\u52a9\u54c8\u5e0c\u8868 */\nList<int> twoSumHashTable(List<int> nums, int target) {\n int size = nums.length;\n // \u8f85\u52a9\u54c8\u5e0c\u8868\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n Map<int, int> dic = HashMap();\n // \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n for (var i = 0; i < size; i++) {\n if (dic.containsKey(target - nums[i])) {\n return [dic[target - nums[i]]!, i];\n }\n dic.putIfAbsent(nums[i], () => i);\n }\n return [0];\n}\n
two_sum.rs/* \u65b9\u6cd5\u4e8c\uff1a\u8f85\u52a9\u54c8\u5e0c\u8868 */\npub fn two_sum_hash_table(nums: &Vec<i32>, target: i32) -> Option<Vec<i32>> {\n // \u8f85\u52a9\u54c8\u5e0c\u8868\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n let mut dic = HashMap::new();\n // \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n for (i, num) in nums.iter().enumerate() {\n match dic.get(&(target - num)) {\n Some(v) => return Some(vec![*v as i32, i as i32]),\n None => dic.insert(num, i as i32)\n };\n }\n None\n}\n
two_sum.c/* \u54c8\u5e0c\u8868 */\ntypedef struct {\n int key;\n int val;\n UT_hash_handle hh; // \u57fa\u4e8e uthash.h \u5b9e\u73b0\n} HashTable;\n\n/* \u54c8\u5e0c\u8868\u67e5\u8be2 */\nHashTable *find(HashTable *h, int key) {\n HashTable *tmp;\n HASH_FIND_INT(h, &key, tmp);\n return tmp;\n}\n\n/* \u54c8\u5e0c\u8868\u5143\u7d20\u63d2\u5165 */\nvoid insert(HashTable *h, int key, int val) {\n HashTable *t = find(h, key);\n if (t == NULL) {\n HashTable *tmp = malloc(sizeof(HashTable));\n tmp->key = key, tmp->val = val;\n HASH_ADD_INT(h, key, tmp);\n } else {\n t->val = val;\n }\n}\n\n/* \u65b9\u6cd5\u4e8c\uff1a\u8f85\u52a9\u54c8\u5e0c\u8868 */\nint *twoSumHashTable(int *nums, int numsSize, int target, int *returnSize) {\n HashTable *hashtable = NULL;\n for (int i = 0; i < numsSize; i++) {\n HashTable *t = find(hashtable, target - nums[i]);\n if (t != NULL) {\n int *res = malloc(sizeof(int) * 2);\n res[0] = t->val, res[1] = i;\n *returnSize = 2;\n return res;\n }\n insert(hashtable, nums[i], i);\n }\n *returnSize = 0;\n return NULL;\n}\n
two_sum.zig// \u65b9\u6cd5\u4e8c\uff1a\u8f85\u52a9\u54c8\u5e0c\u8868\nfn twoSumHashTable(nums: []i32, target: i32) !?[2]i32 {\n var size: usize = nums.len;\n // \u8f85\u52a9\u54c8\u5e0c\u8868\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n var dic = std.AutoHashMap(i32, i32).init(std.heap.page_allocator);\n defer dic.deinit();\n var i: usize = 0;\n // \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\n while (i < size) : (i += 1) {\n if (dic.contains(target - nums[i])) {\n return [_]i32{dic.get(target - nums[i]).?, @intCast(i)};\n }\n try dic.put(nums[i], @intCast(i));\n }\n return null;\n}\n
\u6b64\u65b9\u6cd5\u901a\u8fc7\u54c8\u5e0c\u67e5\u627e\u5c06\u65f6\u95f4\u590d\u6742\u5ea6\u4ece \\(O(n^2)\\) \u964d\u81f3 \\(O(n)\\) \uff0c\u5927\u5e45\u63d0\u5347\u8fd0\u884c\u6548\u7387\u3002
\u7531\u4e8e\u9700\u8981\u7ef4\u62a4\u4e00\u4e2a\u989d\u5916\u7684\u54c8\u5e0c\u8868\uff0c\u56e0\u6b64\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002\u5c3d\u7ba1\u5982\u6b64\uff0c\u8be5\u65b9\u6cd5\u7684\u6574\u4f53\u65f6\u7a7a\u6548\u7387\u66f4\u4e3a\u5747\u8861\uff0c\u56e0\u6b64\u5b83\u662f\u672c\u9898\u7684\u6700\u4f18\u89e3\u6cd5\u3002
"},{"location":"chapter_searching/searching_algorithm_revisited/","title":"10.5 \u00a0 \u91cd\u8bc6\u641c\u7d22\u7b97\u6cd5","text":"\u300c\u641c\u7d22\u7b97\u6cd5 searching algorithm\u300d\u7528\u4e8e\u5728\u6570\u636e\u7ed3\u6784\uff08\u4f8b\u5982\u6570\u7ec4\u3001\u94fe\u8868\u3001\u6811\u6216\u56fe\uff09\u4e2d\u641c\u7d22\u4e00\u4e2a\u6216\u4e00\u7ec4\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u7684\u5143\u7d20\u3002
\u641c\u7d22\u7b97\u6cd5\u53ef\u6839\u636e\u5b9e\u73b0\u601d\u8def\u5206\u4e3a\u4ee5\u4e0b\u4e24\u7c7b\u3002
\u4e0d\u96be\u53d1\u73b0\uff0c\u8fd9\u4e9b\u77e5\u8bc6\u70b9\u90fd\u5df2\u5728\u524d\u9762\u7684\u7ae0\u8282\u4e2d\u4ecb\u7ecd\u8fc7\uff0c\u56e0\u6b64\u641c\u7d22\u7b97\u6cd5\u5bf9\u4e8e\u6211\u4eec\u6765\u8bf4\u5e76\u4e0d\u964c\u751f\u3002\u5728\u672c\u8282\u4e2d\uff0c\u6211\u4eec\u5c06\u4ece\u66f4\u52a0\u7cfb\u7edf\u7684\u89c6\u89d2\u5207\u5165\uff0c\u91cd\u65b0\u5ba1\u89c6\u641c\u7d22\u7b97\u6cd5\u3002
"},{"location":"chapter_searching/searching_algorithm_revisited/#1051","title":"10.5.1 \u00a0 \u66b4\u529b\u641c\u7d22","text":"\u66b4\u529b\u641c\u7d22\u901a\u8fc7\u904d\u5386\u6570\u636e\u7ed3\u6784\u7684\u6bcf\u4e2a\u5143\u7d20\u6765\u5b9a\u4f4d\u76ee\u6807\u5143\u7d20\u3002
\u66b4\u529b\u641c\u7d22\u7684\u4f18\u70b9\u662f\u7b80\u5355\u4e14\u901a\u7528\u6027\u597d\uff0c\u65e0\u987b\u5bf9\u6570\u636e\u505a\u9884\u5904\u7406\u548c\u501f\u52a9\u989d\u5916\u7684\u6570\u636e\u7ed3\u6784\u3002
\u7136\u800c\uff0c\u6b64\u7c7b\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \uff0c\u5176\u4e2d \\(n\\) \u4e3a\u5143\u7d20\u6570\u91cf\uff0c\u56e0\u6b64\u5728\u6570\u636e\u91cf\u8f83\u5927\u7684\u60c5\u51b5\u4e0b\u6027\u80fd\u8f83\u5dee\u3002
"},{"location":"chapter_searching/searching_algorithm_revisited/#1052","title":"10.5.2 \u00a0 \u81ea\u9002\u5e94\u641c\u7d22","text":"\u81ea\u9002\u5e94\u641c\u7d22\u5229\u7528\u6570\u636e\u7684\u7279\u6709\u5c5e\u6027\uff08\u4f8b\u5982\u6709\u5e8f\u6027\uff09\u6765\u4f18\u5316\u641c\u7d22\u8fc7\u7a0b\uff0c\u4ece\u800c\u66f4\u9ad8\u6548\u5730\u5b9a\u4f4d\u76ee\u6807\u5143\u7d20\u3002
\u6b64\u7c7b\u7b97\u6cd5\u7684\u4f18\u70b9\u662f\u6548\u7387\u9ad8\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u53ef\u8fbe\u5230 \\(O(\\log n)\\) \u751a\u81f3 \\(O(1)\\) \u3002
\u7136\u800c\uff0c\u4f7f\u7528\u8fd9\u4e9b\u7b97\u6cd5\u5f80\u5f80\u9700\u8981\u5bf9\u6570\u636e\u8fdb\u884c\u9884\u5904\u7406\u3002\u4f8b\u5982\uff0c\u4e8c\u5206\u67e5\u627e\u9700\u8981\u9884\u5148\u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\uff0c\u54c8\u5e0c\u67e5\u627e\u548c\u6811\u67e5\u627e\u90fd\u9700\u8981\u501f\u52a9\u989d\u5916\u7684\u6570\u636e\u7ed3\u6784\uff0c\u7ef4\u62a4\u8fd9\u4e9b\u6570\u636e\u7ed3\u6784\u4e5f\u9700\u8981\u989d\u5916\u7684\u65f6\u95f4\u548c\u7a7a\u95f4\u5f00\u9500\u3002
Tip
\u81ea\u9002\u5e94\u641c\u7d22\u7b97\u6cd5\u5e38\u88ab\u79f0\u4e3a\u67e5\u627e\u7b97\u6cd5\uff0c\u4e3b\u8981\u7528\u4e8e\u5728\u7279\u5b9a\u6570\u636e\u7ed3\u6784\u4e2d\u5feb\u901f\u68c0\u7d22\u76ee\u6807\u5143\u7d20\u3002
"},{"location":"chapter_searching/searching_algorithm_revisited/#1053","title":"10.5.3 \u00a0 \u641c\u7d22\u65b9\u6cd5\u9009\u53d6","text":"\u7ed9\u5b9a\u5927\u5c0f\u4e3a \\(n\\) \u7684\u4e00\u7ec4\u6570\u636e\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u7ebf\u6027\u641c\u7d22\u3001\u4e8c\u5206\u67e5\u627e\u3001\u6811\u67e5\u627e\u3001\u54c8\u5e0c\u67e5\u627e\u7b49\u591a\u79cd\u65b9\u6cd5\u4ece\u4e2d\u641c\u7d22\u76ee\u6807\u5143\u7d20\u3002\u5404\u4e2a\u65b9\u6cd5\u7684\u5de5\u4f5c\u539f\u7406\u5982\u56fe 10-11 \u6240\u793a\u3002
\u56fe 10-11 \u00a0 \u591a\u79cd\u641c\u7d22\u7b56\u7565
\u4e0a\u8ff0\u51e0\u79cd\u65b9\u6cd5\u7684\u64cd\u4f5c\u6548\u7387\u4e0e\u7279\u6027\u5982\u8868 10-1 \u6240\u793a\u3002
\u8868 10-1 \u00a0 \u67e5\u627e\u7b97\u6cd5\u6548\u7387\u5bf9\u6bd4
\u7ebf\u6027\u641c\u7d22 \u4e8c\u5206\u67e5\u627e \u6811\u67e5\u627e \u54c8\u5e0c\u67e5\u627e \u67e5\u627e\u5143\u7d20 \\(O(n)\\) \\(O(\\log n)\\) \\(O(\\log n)\\) \\(O(1)\\) \u63d2\u5165\u5143\u7d20 \\(O(1)\\) \\(O(n)\\) \\(O(\\log n)\\) \\(O(1)\\) \u5220\u9664\u5143\u7d20 \\(O(n)\\) \\(O(n)\\) \\(O(\\log n)\\) \\(O(1)\\) \u989d\u5916\u7a7a\u95f4 \\(O(1)\\) \\(O(1)\\) \\(O(n)\\) \\(O(n)\\) \u6570\u636e\u9884\u5904\u7406 / \u6392\u5e8f \\(O(n \\log n)\\) \u5efa\u6811 \\(O(n \\log n)\\) \u5efa\u54c8\u5e0c\u8868 \\(O(n)\\) \u6570\u636e\u662f\u5426\u6709\u5e8f \u65e0\u5e8f \u6709\u5e8f \u6709\u5e8f \u65e0\u5e8f\u641c\u7d22\u7b97\u6cd5\u7684\u9009\u62e9\u8fd8\u53d6\u51b3\u4e8e\u6570\u636e\u4f53\u91cf\u3001\u641c\u7d22\u6027\u80fd\u8981\u6c42\u3001\u6570\u636e\u67e5\u8be2\u4e0e\u66f4\u65b0\u9891\u7387\u7b49\u3002
\u7ebf\u6027\u641c\u7d22
\u4e8c\u5206\u67e5\u627e
\u54c8\u5e0c\u67e5\u627e
\u6811\u67e5\u627e
Abstract
\u6392\u5e8f\u72b9\u5982\u4e00\u628a\u5c06\u6df7\u4e71\u53d8\u4e3a\u79e9\u5e8f\u7684\u9b54\u6cd5\u94a5\u5319\uff0c\u4f7f\u6211\u4eec\u80fd\u4ee5\u66f4\u9ad8\u6548\u7684\u65b9\u5f0f\u7406\u89e3\u4e0e\u5904\u7406\u6570\u636e\u3002
\u65e0\u8bba\u662f\u7b80\u5355\u7684\u5347\u5e8f\uff0c\u8fd8\u662f\u590d\u6742\u7684\u5206\u7c7b\u6392\u5217\uff0c\u6392\u5e8f\u90fd\u5411\u6211\u4eec\u5c55\u793a\u4e86\u6570\u636e\u7684\u548c\u8c10\u7f8e\u611f\u3002
"},{"location":"chapter_sorting/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"\u300c\u5192\u6ce1\u6392\u5e8f bubble sort\u300d\u901a\u8fc7\u8fde\u7eed\u5730\u6bd4\u8f83\u4e0e\u4ea4\u6362\u76f8\u90bb\u5143\u7d20\u5b9e\u73b0\u6392\u5e8f\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u5c31\u50cf\u6c14\u6ce1\u4ece\u5e95\u90e8\u5347\u5230\u9876\u90e8\u4e00\u6837\uff0c\u56e0\u6b64\u5f97\u540d\u5192\u6ce1\u6392\u5e8f\u3002
\u5982\u56fe 11-4 \u6240\u793a\uff0c\u5192\u6ce1\u8fc7\u7a0b\u53ef\u4ee5\u5229\u7528\u5143\u7d20\u4ea4\u6362\u64cd\u4f5c\u6765\u6a21\u62df\uff1a\u4ece\u6570\u7ec4\u6700\u5de6\u7aef\u5f00\u59cb\u5411\u53f3\u904d\u5386\uff0c\u4f9d\u6b21\u6bd4\u8f83\u76f8\u90bb\u5143\u7d20\u5927\u5c0f\uff0c\u5982\u679c\u201c\u5de6\u5143\u7d20 > \u53f3\u5143\u7d20\u201d\u5c31\u4ea4\u6362\u4e8c\u8005\u3002\u904d\u5386\u5b8c\u6210\u540e\uff0c\u6700\u5927\u7684\u5143\u7d20\u4f1a\u88ab\u79fb\u52a8\u5230\u6570\u7ec4\u7684\u6700\u53f3\u7aef\u3002
<1><2><3><4><5><6><7>\u56fe 11-4 \u00a0 \u5229\u7528\u5143\u7d20\u4ea4\u6362\u64cd\u4f5c\u6a21\u62df\u5192\u6ce1
"},{"location":"chapter_sorting/bubble_sort/#1131","title":"11.3.1 \u00a0 \u7b97\u6cd5\u6d41\u7a0b","text":"\u8bbe\u6570\u7ec4\u7684\u957f\u5ea6\u4e3a \\(n\\) \uff0c\u5192\u6ce1\u6392\u5e8f\u7684\u6b65\u9aa4\u5982\u56fe 11-5 \u6240\u793a\u3002
\u56fe 11-5 \u00a0 \u5192\u6ce1\u6392\u5e8f\u6d41\u7a0b
\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig bubble_sort.pydef bubble_sort(nums: list[int]):\n \"\"\"\u5192\u6ce1\u6392\u5e8f\"\"\"\n n = len(nums)\n # \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for i in range(n - 1, 0, -1):\n # \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n for j in range(i):\n if nums[j] > nums[j + 1]:\n # \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n nums[j], nums[j + 1] = nums[j + 1], nums[j]\n
bubble_sort.cpp/* \u5192\u6ce1\u6392\u5e8f */\nvoid bubbleSort(vector<int> &nums) {\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for (int i = nums.size() - 1; i > 0; i--) {\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n for (int j = 0; j < i; j++) {\n if (nums[j] > nums[j + 1]) {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n // \u8fd9\u91cc\u4f7f\u7528\u4e86 std::swap() \u51fd\u6570\n swap(nums[j], nums[j + 1]);\n }\n }\n }\n}\n
bubble_sort.java/* \u5192\u6ce1\u6392\u5e8f */\nvoid bubbleSort(int[] nums) {\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for (int i = nums.length - 1; i > 0; i--) {\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n for (int j = 0; j < i; j++) {\n if (nums[j] > nums[j + 1]) {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n int tmp = nums[j];\n nums[j] = nums[j + 1];\n nums[j + 1] = tmp;\n }\n }\n }\n}\n
bubble_sort.cs/* \u5192\u6ce1\u6392\u5e8f */\nvoid BubbleSort(int[] nums) {\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for (int i = nums.Length - 1; i > 0; i--) {\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef \n for (int j = 0; j < i; j++) {\n if (nums[j] > nums[j + 1]) {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n (nums[j + 1], nums[j]) = (nums[j], nums[j + 1]);\n }\n }\n }\n}\n
bubble_sort.go/* \u5192\u6ce1\u6392\u5e8f */\nfunc bubbleSort(nums []int) {\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for i := len(nums) - 1; i > 0; i-- {\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n for j := 0; j < i; j++ {\n if nums[j] > nums[j+1] {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n nums[j], nums[j+1] = nums[j+1], nums[j]\n }\n }\n }\n}\n
bubble_sort.swift/* \u5192\u6ce1\u6392\u5e8f */\nfunc bubbleSort(nums: inout [Int]) {\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for i in stride(from: nums.count - 1, to: 0, by: -1) {\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef \n for j in stride(from: 0, to: i, by: 1) {\n if nums[j] > nums[j + 1] {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n let tmp = nums[j]\n nums[j] = nums[j + 1]\n nums[j + 1] = tmp\n }\n }\n }\n}\n
bubble_sort.js/* \u5192\u6ce1\u6392\u5e8f */\nfunction bubbleSort(nums) {\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for (let i = nums.length - 1; i > 0; i--) {\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n for (let j = 0; j < i; j++) {\n if (nums[j] > nums[j + 1]) {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n let tmp = nums[j];\n nums[j] = nums[j + 1];\n nums[j + 1] = tmp;\n }\n }\n }\n}\n
bubble_sort.ts/* \u5192\u6ce1\u6392\u5e8f */\nfunction bubbleSort(nums: number[]): void {\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for (let i = nums.length - 1; i > 0; i--) {\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n for (let j = 0; j < i; j++) {\n if (nums[j] > nums[j + 1]) {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n let tmp = nums[j];\n nums[j] = nums[j + 1];\n nums[j + 1] = tmp;\n }\n }\n }\n}\n
bubble_sort.dart/* \u5192\u6ce1\u6392\u5e8f */\nvoid bubbleSort(List<int> nums) {\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for (int i = nums.length - 1; i > 0; i--) {\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n for (int j = 0; j < i; j++) {\n if (nums[j] > nums[j + 1]) {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n int tmp = nums[j];\n nums[j] = nums[j + 1];\n nums[j + 1] = tmp;\n }\n }\n }\n}\n
bubble_sort.rs/* \u5192\u6ce1\u6392\u5e8f */\nfn bubble_sort(nums: &mut [i32]) {\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for i in (1..nums.len()).rev() {\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef \n for j in 0..i {\n if nums[j] > nums[j + 1] {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n let tmp = nums[j];\n nums[j] = nums[j + 1];\n nums[j + 1] = tmp;\n }\n }\n }\n}\n
bubble_sort.c/* \u5192\u6ce1\u6392\u5e8f */\nvoid bubbleSort(int nums[], int size) {\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for (int i = size - 1; i > 0; i--) {\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n for (int j = 0; j < size - 1 - i; j++) {\n if (nums[j] > nums[j + 1]) {\n int temp = nums[j];\n nums[j] = nums[j + 1];\n nums[j + 1] = temp;\n }\n }\n }\n}\n
bubble_sort.zig// \u5192\u6ce1\u6392\u5e8f\nfn bubbleSort(nums: []i32) void {\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n var i: usize = nums.len - 1;\n while (i > 0) : (i -= 1) {\n var j: usize = 0;\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef \n while (j < i) : (j += 1) {\n if (nums[j] > nums[j + 1]) {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n var tmp = nums[j];\n nums[j] = nums[j + 1];\n nums[j + 1] = tmp;\n }\n }\n }\n}\n
"},{"location":"chapter_sorting/bubble_sort/#1132","title":"11.3.2 \u00a0 \u6548\u7387\u4f18\u5316","text":"\u6211\u4eec\u53d1\u73b0\uff0c\u5982\u679c\u67d0\u8f6e\u201c\u5192\u6ce1\u201d\u4e2d\u6ca1\u6709\u6267\u884c\u4efb\u4f55\u4ea4\u6362\u64cd\u4f5c\uff0c\u8bf4\u660e\u6570\u7ec4\u5df2\u7ecf\u5b8c\u6210\u6392\u5e8f\uff0c\u53ef\u76f4\u63a5\u8fd4\u56de\u7ed3\u679c\u3002\u56e0\u6b64\uff0c\u53ef\u4ee5\u589e\u52a0\u4e00\u4e2a\u6807\u5fd7\u4f4d flag
\u6765\u76d1\u6d4b\u8fd9\u79cd\u60c5\u51b5\uff0c\u4e00\u65e6\u51fa\u73b0\u5c31\u7acb\u5373\u8fd4\u56de\u3002
\u7ecf\u8fc7\u4f18\u5316\uff0c\u5192\u6ce1\u6392\u5e8f\u7684\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6\u548c\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u4ecd\u4e3a \\(O(n^2)\\) \uff1b\u4f46\u5f53\u8f93\u5165\u6570\u7ec4\u5b8c\u5168\u6709\u5e8f\u65f6\uff0c\u53ef\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 \\(O(n)\\) \u3002
PythonC++JavaC#GoSwiftJSTSDartRustCZig bubble_sort.pydef bubble_sort_with_flag(nums: list[int]):\n \"\"\"\u5192\u6ce1\u6392\u5e8f\uff08\u6807\u5fd7\u4f18\u5316\uff09\"\"\"\n n = len(nums)\n # \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for i in range(n - 1, 0, -1):\n flag = False # \u521d\u59cb\u5316\u6807\u5fd7\u4f4d\n # \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n for j in range(i):\n if nums[j] > nums[j + 1]:\n # \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n nums[j], nums[j + 1] = nums[j + 1], nums[j]\n flag = True # \u8bb0\u5f55\u4ea4\u6362\u5143\u7d20\n if not flag:\n break # \u6b64\u8f6e\u5192\u6ce1\u672a\u4ea4\u6362\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\n
bubble_sort.cpp/* \u5192\u6ce1\u6392\u5e8f\uff08\u6807\u5fd7\u4f18\u5316\uff09*/\nvoid bubbleSortWithFlag(vector<int> &nums) {\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for (int i = nums.size() - 1; i > 0; i--) {\n bool flag = false; // \u521d\u59cb\u5316\u6807\u5fd7\u4f4d\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n for (int j = 0; j < i; j++) {\n if (nums[j] > nums[j + 1]) {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n // \u8fd9\u91cc\u4f7f\u7528\u4e86 std::swap() \u51fd\u6570\n swap(nums[j], nums[j + 1]);\n flag = true; // \u8bb0\u5f55\u4ea4\u6362\u5143\u7d20\n }\n }\n if (!flag)\n break; // \u6b64\u8f6e\u5192\u6ce1\u672a\u4ea4\u6362\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\n }\n}\n
bubble_sort.java/* \u5192\u6ce1\u6392\u5e8f\uff08\u6807\u5fd7\u4f18\u5316\uff09 */\nvoid bubbleSortWithFlag(int[] nums) {\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for (int i = nums.length - 1; i > 0; i--) {\n boolean flag = false; // \u521d\u59cb\u5316\u6807\u5fd7\u4f4d\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n for (int j = 0; j < i; j++) {\n if (nums[j] > nums[j + 1]) {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n int tmp = nums[j];\n nums[j] = nums[j + 1];\n nums[j + 1] = tmp;\n flag = true; // \u8bb0\u5f55\u4ea4\u6362\u5143\u7d20\n }\n }\n if (!flag)\n break; // \u6b64\u8f6e\u5192\u6ce1\u672a\u4ea4\u6362\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\n }\n}\n
bubble_sort.cs/* \u5192\u6ce1\u6392\u5e8f\uff08\u6807\u5fd7\u4f18\u5316\uff09*/\nvoid BubbleSortWithFlag(int[] nums) {\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for (int i = nums.Length - 1; i > 0; i--) {\n bool flag = false; // \u521d\u59cb\u5316\u6807\u5fd7\u4f4d\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef \n for (int j = 0; j < i; j++) {\n if (nums[j] > nums[j + 1]) {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n (nums[j + 1], nums[j]) = (nums[j], nums[j + 1]);\n flag = true; // \u8bb0\u5f55\u4ea4\u6362\u5143\u7d20\n }\n }\n if (!flag) break; // \u6b64\u8f6e\u5192\u6ce1\u672a\u4ea4\u6362\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\n }\n}\n
bubble_sort.go/* \u5192\u6ce1\u6392\u5e8f\uff08\u6807\u5fd7\u4f18\u5316\uff09*/\nfunc bubbleSortWithFlag(nums []int) {\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for i := len(nums) - 1; i > 0; i-- {\n flag := false // \u521d\u59cb\u5316\u6807\u5fd7\u4f4d\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n for j := 0; j < i; j++ {\n if nums[j] > nums[j+1] {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n nums[j], nums[j+1] = nums[j+1], nums[j]\n flag = true // \u8bb0\u5f55\u4ea4\u6362\u5143\u7d20\n }\n }\n if flag == false { // \u6b64\u8f6e\u5192\u6ce1\u672a\u4ea4\u6362\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\n break\n }\n }\n}\n
bubble_sort.swift/* \u5192\u6ce1\u6392\u5e8f\uff08\u6807\u5fd7\u4f18\u5316\uff09*/\nfunc bubbleSortWithFlag(nums: inout [Int]) {\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for i in stride(from: nums.count - 1, to: 0, by: -1) {\n var flag = false // \u521d\u59cb\u5316\u6807\u5fd7\u4f4d\n for j in stride(from: 0, to: i, by: 1) {\n if nums[j] > nums[j + 1] {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n let tmp = nums[j]\n nums[j] = nums[j + 1]\n nums[j + 1] = tmp\n flag = true // \u8bb0\u5f55\u4ea4\u6362\u5143\u7d20\n }\n }\n if !flag { // \u6b64\u8f6e\u5192\u6ce1\u672a\u4ea4\u6362\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\n break\n }\n }\n}\n
bubble_sort.js/* \u5192\u6ce1\u6392\u5e8f\uff08\u6807\u5fd7\u4f18\u5316\uff09*/\nfunction bubbleSortWithFlag(nums) {\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for (let i = nums.length - 1; i > 0; i--) {\n let flag = false; // \u521d\u59cb\u5316\u6807\u5fd7\u4f4d\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n for (let j = 0; j < i; j++) {\n if (nums[j] > nums[j + 1]) {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n let tmp = nums[j];\n nums[j] = nums[j + 1];\n nums[j + 1] = tmp;\n flag = true; // \u8bb0\u5f55\u4ea4\u6362\u5143\u7d20\n }\n }\n if (!flag) break; // \u6b64\u8f6e\u5192\u6ce1\u672a\u4ea4\u6362\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\n }\n}\n
bubble_sort.ts/* \u5192\u6ce1\u6392\u5e8f\uff08\u6807\u5fd7\u4f18\u5316\uff09*/\nfunction bubbleSortWithFlag(nums: number[]): void {\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for (let i = nums.length - 1; i > 0; i--) {\n let flag = false; // \u521d\u59cb\u5316\u6807\u5fd7\u4f4d\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n for (let j = 0; j < i; j++) {\n if (nums[j] > nums[j + 1]) {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n let tmp = nums[j];\n nums[j] = nums[j + 1];\n nums[j + 1] = tmp;\n flag = true; // \u8bb0\u5f55\u4ea4\u6362\u5143\u7d20\n }\n }\n if (!flag) break; // \u6b64\u8f6e\u5192\u6ce1\u672a\u4ea4\u6362\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\n }\n}\n
bubble_sort.dart/* \u5192\u6ce1\u6392\u5e8f\uff08\u6807\u5fd7\u4f18\u5316\uff09*/\nvoid bubbleSortWithFlag(List<int> nums) {\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for (int i = nums.length - 1; i > 0; i--) {\n bool flag = false; // \u521d\u59cb\u5316\u6807\u5fd7\u4f4d\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n for (int j = 0; j < i; j++) {\n if (nums[j] > nums[j + 1]) {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n int tmp = nums[j];\n nums[j] = nums[j + 1];\n nums[j + 1] = tmp;\n flag = true; // \u8bb0\u5f55\u4ea4\u6362\u5143\u7d20\n }\n }\n if (!flag) break; // \u6b64\u8f6e\u5192\u6ce1\u672a\u4ea4\u6362\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\n }\n}\n
bubble_sort.rs/* \u5192\u6ce1\u6392\u5e8f\uff08\u6807\u5fd7\u4f18\u5316\uff09 */\nfn bubble_sort_with_flag(nums: &mut [i32]) {\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for i in (1..nums.len()).rev() {\n let mut flag = false; // \u521d\u59cb\u5316\u6807\u5fd7\u4f4d\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef \n for j in 0..i {\n if nums[j] > nums[j + 1] {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n let tmp = nums[j];\n nums[j] = nums[j + 1];\n nums[j + 1] = tmp;\n flag = true; // \u8bb0\u5f55\u4ea4\u6362\u5143\u7d20\n }\n }\n if !flag {break}; // \u6b64\u8f6e\u5192\u6ce1\u672a\u4ea4\u6362\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\n }\n}\n
bubble_sort.c/* \u5192\u6ce1\u6392\u5e8f\uff08\u6807\u5fd7\u4f18\u5316\uff09*/\nvoid bubbleSortWithFlag(int nums[], int size) {\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for (int i = size - 1; i > 0; i--) {\n bool flag = false;\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n for (int j = 0; j < size - 1 - i; j++) {\n if (nums[j] > nums[j + 1]) {\n int temp = nums[j];\n nums[j] = nums[j + 1];\n nums[j + 1] = temp;\n flag = true;\n }\n }\n if (!flag)\n break;\n }\n}\n
bubble_sort.zig// \u5192\u6ce1\u6392\u5e8f\uff08\u6807\u5fd7\u4f18\u5316\uff09\nfn bubbleSortWithFlag(nums: []i32) void {\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n var i: usize = nums.len - 1;\n while (i > 0) : (i -= 1) {\n var flag = false; // \u521d\u59cb\u5316\u6807\u5fd7\u4f4d\n var j: usize = 0;\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef \n while (j < i) : (j += 1) {\n if (nums[j] > nums[j + 1]) {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n var tmp = nums[j];\n nums[j] = nums[j + 1];\n nums[j + 1] = tmp;\n flag = true;\n }\n }\n if (!flag) break; // \u6b64\u8f6e\u5192\u6ce1\u672a\u4ea4\u6362\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\n }\n}\n
"},{"location":"chapter_sorting/bubble_sort/#1133","title":"11.3.3 \u00a0 \u7b97\u6cd5\u7279\u6027","text":"flag
\u4f18\u5316\u540e\uff0c\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6\u53ef\u8fbe\u5230 \\(O(n)\\) \u3002\u524d\u8ff0\u51e0\u79cd\u6392\u5e8f\u7b97\u6cd5\u90fd\u5c5e\u4e8e\u201c\u57fa\u4e8e\u6bd4\u8f83\u7684\u6392\u5e8f\u7b97\u6cd5\u201d\uff0c\u5b83\u4eec\u901a\u8fc7\u6bd4\u8f83\u5143\u7d20\u95f4\u7684\u5927\u5c0f\u6765\u5b9e\u73b0\u6392\u5e8f\u3002\u6b64\u7c7b\u6392\u5e8f\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u65e0\u6cd5\u8d85\u8d8a \\(O(n \\log n)\\) \u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u63a2\u8ba8\u51e0\u79cd\u201c\u975e\u6bd4\u8f83\u6392\u5e8f\u7b97\u6cd5\u201d\uff0c\u5b83\u4eec\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u53ef\u4ee5\u8fbe\u5230\u7ebf\u6027\u9636\u3002
\u300c\u6876\u6392\u5e8f bucket sort\u300d\u662f\u5206\u6cbb\u7b56\u7565\u7684\u4e00\u4e2a\u5178\u578b\u5e94\u7528\u3002\u5b83\u901a\u8fc7\u8bbe\u7f6e\u4e00\u4e9b\u5177\u6709\u5927\u5c0f\u987a\u5e8f\u7684\u6876\uff0c\u6bcf\u4e2a\u6876\u5bf9\u5e94\u4e00\u4e2a\u6570\u636e\u8303\u56f4\uff0c\u5c06\u6570\u636e\u5e73\u5747\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\uff1b\u7136\u540e\uff0c\u5728\u6bcf\u4e2a\u6876\u5185\u90e8\u5206\u522b\u6267\u884c\u6392\u5e8f\uff1b\u6700\u7ec8\u6309\u7167\u6876\u7684\u987a\u5e8f\u5c06\u6240\u6709\u6570\u636e\u5408\u5e76\u3002
"},{"location":"chapter_sorting/bucket_sort/#1181","title":"11.8.1 \u00a0 \u7b97\u6cd5\u6d41\u7a0b","text":"\u8003\u8651\u4e00\u4e2a\u957f\u5ea6\u4e3a \\(n\\) \u7684\u6570\u7ec4\uff0c\u5176\u5143\u7d20\u662f\u8303\u56f4 \\([0, 1)\\) \u5185\u7684\u6d6e\u70b9\u6570\u3002\u6876\u6392\u5e8f\u7684\u6d41\u7a0b\u5982\u56fe 11-13 \u6240\u793a\u3002
\u56fe 11-13 \u00a0 \u6876\u6392\u5e8f\u7b97\u6cd5\u6d41\u7a0b
\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig bucket_sort.pydef bucket_sort(nums: list[float]):\n \"\"\"\u6876\u6392\u5e8f\"\"\"\n # \u521d\u59cb\u5316 k = n/2 \u4e2a\u6876\uff0c\u9884\u671f\u5411\u6bcf\u4e2a\u6876\u5206\u914d 2 \u4e2a\u5143\u7d20\n k = len(nums) // 2\n buckets = [[] for _ in range(k)]\n # 1. \u5c06\u6570\u7ec4\u5143\u7d20\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\n for num in nums:\n # \u8f93\u5165\u6570\u636e\u8303\u56f4\u4e3a [0, 1)\uff0c\u4f7f\u7528 num * k \u6620\u5c04\u5230\u7d22\u5f15\u8303\u56f4 [0, k-1]\n i = int(num * k)\n # \u5c06 num \u6dfb\u52a0\u8fdb\u6876 i\n buckets[i].append(num)\n # 2. \u5bf9\u5404\u4e2a\u6876\u6267\u884c\u6392\u5e8f\n for bucket in buckets:\n # \u4f7f\u7528\u5185\u7f6e\u6392\u5e8f\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u66ff\u6362\u6210\u5176\u4ed6\u6392\u5e8f\u7b97\u6cd5\n bucket.sort()\n # 3. \u904d\u5386\u6876\u5408\u5e76\u7ed3\u679c\n i = 0\n for bucket in buckets:\n for num in bucket:\n nums[i] = num\n i += 1\n
bucket_sort.cpp/* \u6876\u6392\u5e8f */\nvoid bucketSort(vector<float> &nums) {\n // \u521d\u59cb\u5316 k = n/2 \u4e2a\u6876\uff0c\u9884\u671f\u5411\u6bcf\u4e2a\u6876\u5206\u914d 2 \u4e2a\u5143\u7d20\n int k = nums.size() / 2;\n vector<vector<float>> buckets(k);\n // 1. \u5c06\u6570\u7ec4\u5143\u7d20\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\n for (float num : nums) {\n // \u8f93\u5165\u6570\u636e\u8303\u56f4\u4e3a [0, 1)\uff0c\u4f7f\u7528 num * k \u6620\u5c04\u5230\u7d22\u5f15\u8303\u56f4 [0, k-1]\n int i = num * k;\n // \u5c06 num \u6dfb\u52a0\u8fdb\u6876 bucket_idx\n buckets[i].push_back(num);\n }\n // 2. \u5bf9\u5404\u4e2a\u6876\u6267\u884c\u6392\u5e8f\n for (vector<float> &bucket : buckets) {\n // \u4f7f\u7528\u5185\u7f6e\u6392\u5e8f\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u66ff\u6362\u6210\u5176\u4ed6\u6392\u5e8f\u7b97\u6cd5\n sort(bucket.begin(), bucket.end());\n }\n // 3. \u904d\u5386\u6876\u5408\u5e76\u7ed3\u679c\n int i = 0;\n for (vector<float> &bucket : buckets) {\n for (float num : bucket) {\n nums[i++] = num;\n }\n }\n}\n
bucket_sort.java/* \u6876\u6392\u5e8f */\nvoid bucketSort(float[] nums) {\n // \u521d\u59cb\u5316 k = n/2 \u4e2a\u6876\uff0c\u9884\u671f\u5411\u6bcf\u4e2a\u6876\u5206\u914d 2 \u4e2a\u5143\u7d20\n int k = nums.length / 2;\n List<List<Float>> buckets = new ArrayList<>();\n for (int i = 0; i < k; i++) {\n buckets.add(new ArrayList<>());\n }\n // 1. \u5c06\u6570\u7ec4\u5143\u7d20\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\n for (float num : nums) {\n // \u8f93\u5165\u6570\u636e\u8303\u56f4\u4e3a [0, 1)\uff0c\u4f7f\u7528 num * k \u6620\u5c04\u5230\u7d22\u5f15\u8303\u56f4 [0, k-1]\n int i = (int) (num * k);\n // \u5c06 num \u6dfb\u52a0\u8fdb\u6876 i\n buckets.get(i).add(num);\n }\n // 2. \u5bf9\u5404\u4e2a\u6876\u6267\u884c\u6392\u5e8f\n for (List<Float> bucket : buckets) {\n // \u4f7f\u7528\u5185\u7f6e\u6392\u5e8f\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u66ff\u6362\u6210\u5176\u4ed6\u6392\u5e8f\u7b97\u6cd5\n Collections.sort(bucket);\n }\n // 3. \u904d\u5386\u6876\u5408\u5e76\u7ed3\u679c\n int i = 0;\n for (List<Float> bucket : buckets) {\n for (float num : bucket) {\n nums[i++] = num;\n }\n }\n}\n
bucket_sort.cs/* \u6876\u6392\u5e8f */\nvoid BucketSort(float[] nums) {\n // \u521d\u59cb\u5316 k = n/2 \u4e2a\u6876\uff0c\u9884\u671f\u5411\u6bcf\u4e2a\u6876\u5206\u914d 2 \u4e2a\u5143\u7d20\n int k = nums.Length / 2;\n List<List<float>> buckets = [];\n for (int i = 0; i < k; i++) {\n buckets.Add([]);\n }\n // 1. \u5c06\u6570\u7ec4\u5143\u7d20\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\n foreach (float num in nums) {\n // \u8f93\u5165\u6570\u636e\u8303\u56f4\u4e3a [0, 1)\uff0c\u4f7f\u7528 num * k \u6620\u5c04\u5230\u7d22\u5f15\u8303\u56f4 [0, k-1]\n int i = (int)(num * k);\n // \u5c06 num \u6dfb\u52a0\u8fdb\u6876 i\n buckets[i].Add(num);\n }\n // 2. \u5bf9\u5404\u4e2a\u6876\u6267\u884c\u6392\u5e8f\n foreach (List<float> bucket in buckets) {\n // \u4f7f\u7528\u5185\u7f6e\u6392\u5e8f\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u66ff\u6362\u6210\u5176\u4ed6\u6392\u5e8f\u7b97\u6cd5\n bucket.Sort();\n }\n // 3. \u904d\u5386\u6876\u5408\u5e76\u7ed3\u679c\n int j = 0;\n foreach (List<float> bucket in buckets) {\n foreach (float num in bucket) {\n nums[j++] = num;\n }\n }\n}\n
bucket_sort.go/* \u6876\u6392\u5e8f */\nfunc bucketSort(nums []float64) {\n // \u521d\u59cb\u5316 k = n/2 \u4e2a\u6876\uff0c\u9884\u671f\u5411\u6bcf\u4e2a\u6876\u5206\u914d 2 \u4e2a\u5143\u7d20\n k := len(nums) / 2\n buckets := make([][]float64, k)\n for i := 0; i < k; i++ {\n buckets[i] = make([]float64, 0)\n }\n // 1. \u5c06\u6570\u7ec4\u5143\u7d20\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\n for _, num := range nums {\n // \u8f93\u5165\u6570\u636e\u8303\u56f4\u4e3a [0, 1)\uff0c\u4f7f\u7528 num * k \u6620\u5c04\u5230\u7d22\u5f15\u8303\u56f4 [0, k-1]\n i := int(num * float64(k))\n // \u5c06 num \u6dfb\u52a0\u8fdb\u6876 i\n buckets[i] = append(buckets[i], num)\n }\n // 2. \u5bf9\u5404\u4e2a\u6876\u6267\u884c\u6392\u5e8f\n for i := 0; i < k; i++ {\n // \u4f7f\u7528\u5185\u7f6e\u5207\u7247\u6392\u5e8f\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u66ff\u6362\u6210\u5176\u4ed6\u6392\u5e8f\u7b97\u6cd5\n sort.Float64s(buckets[i])\n }\n // 3. \u904d\u5386\u6876\u5408\u5e76\u7ed3\u679c\n i := 0\n for _, bucket := range buckets {\n for _, num := range bucket {\n nums[i] = num\n i++\n }\n }\n}\n
bucket_sort.swift/* \u6876\u6392\u5e8f */\nfunc bucketSort(nums: inout [Double]) {\n // \u521d\u59cb\u5316 k = n/2 \u4e2a\u6876\uff0c\u9884\u671f\u5411\u6bcf\u4e2a\u6876\u5206\u914d 2 \u4e2a\u5143\u7d20\n let k = nums.count / 2\n var buckets = (0 ..< k).map { _ in [Double]() }\n // 1. \u5c06\u6570\u7ec4\u5143\u7d20\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\n for num in nums {\n // \u8f93\u5165\u6570\u636e\u8303\u56f4\u4e3a [0, 1)\uff0c\u4f7f\u7528 num * k \u6620\u5c04\u5230\u7d22\u5f15\u8303\u56f4 [0, k-1]\n let i = Int(num * Double(k))\n // \u5c06 num \u6dfb\u52a0\u8fdb\u6876 i\n buckets[i].append(num)\n }\n // 2. \u5bf9\u5404\u4e2a\u6876\u6267\u884c\u6392\u5e8f\n for i in buckets.indices {\n // \u4f7f\u7528\u5185\u7f6e\u6392\u5e8f\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u66ff\u6362\u6210\u5176\u4ed6\u6392\u5e8f\u7b97\u6cd5\n buckets[i].sort()\n }\n // 3. \u904d\u5386\u6876\u5408\u5e76\u7ed3\u679c\n var i = nums.startIndex\n for bucket in buckets {\n for num in bucket {\n nums[i] = num\n nums.formIndex(after: &i)\n }\n }\n}\n
bucket_sort.js/* \u6876\u6392\u5e8f */\nfunction bucketSort(nums) {\n // \u521d\u59cb\u5316 k = n/2 \u4e2a\u6876\uff0c\u9884\u671f\u5411\u6bcf\u4e2a\u6876\u5206\u914d 2 \u4e2a\u5143\u7d20\n const k = nums.length / 2;\n const buckets = [];\n for (let i = 0; i < k; i++) {\n buckets.push([]);\n }\n // 1. \u5c06\u6570\u7ec4\u5143\u7d20\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\n for (const num of nums) {\n // \u8f93\u5165\u6570\u636e\u8303\u56f4\u4e3a [0, 1)\uff0c\u4f7f\u7528 num * k \u6620\u5c04\u5230\u7d22\u5f15\u8303\u56f4 [0, k-1]\n const i = Math.floor(num * k);\n // \u5c06 num \u6dfb\u52a0\u8fdb\u6876 i\n buckets[i].push(num);\n }\n // 2. \u5bf9\u5404\u4e2a\u6876\u6267\u884c\u6392\u5e8f\n for (const bucket of buckets) {\n // \u4f7f\u7528\u5185\u7f6e\u6392\u5e8f\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u66ff\u6362\u6210\u5176\u4ed6\u6392\u5e8f\u7b97\u6cd5\n bucket.sort((a, b) => a - b);\n }\n // 3. \u904d\u5386\u6876\u5408\u5e76\u7ed3\u679c\n let i = 0;\n for (const bucket of buckets) {\n for (const num of bucket) {\n nums[i++] = num;\n }\n }\n}\n
bucket_sort.ts/* \u6876\u6392\u5e8f */\nfunction bucketSort(nums: number[]): void {\n // \u521d\u59cb\u5316 k = n/2 \u4e2a\u6876\uff0c\u9884\u671f\u5411\u6bcf\u4e2a\u6876\u5206\u914d 2 \u4e2a\u5143\u7d20\n const k = nums.length / 2;\n const buckets: number[][] = [];\n for (let i = 0; i < k; i++) {\n buckets.push([]);\n }\n // 1. \u5c06\u6570\u7ec4\u5143\u7d20\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\n for (const num of nums) {\n // \u8f93\u5165\u6570\u636e\u8303\u56f4\u4e3a [0, 1)\uff0c\u4f7f\u7528 num * k \u6620\u5c04\u5230\u7d22\u5f15\u8303\u56f4 [0, k-1]\n const i = Math.floor(num * k);\n // \u5c06 num \u6dfb\u52a0\u8fdb\u6876 i\n buckets[i].push(num);\n }\n // 2. \u5bf9\u5404\u4e2a\u6876\u6267\u884c\u6392\u5e8f\n for (const bucket of buckets) {\n // \u4f7f\u7528\u5185\u7f6e\u6392\u5e8f\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u66ff\u6362\u6210\u5176\u4ed6\u6392\u5e8f\u7b97\u6cd5\n bucket.sort((a, b) => a - b);\n }\n // 3. \u904d\u5386\u6876\u5408\u5e76\u7ed3\u679c\n let i = 0;\n for (const bucket of buckets) {\n for (const num of bucket) {\n nums[i++] = num;\n }\n }\n}\n
bucket_sort.dart/* \u6876\u6392\u5e8f */\nvoid bucketSort(List<double> nums) {\n // \u521d\u59cb\u5316 k = n/2 \u4e2a\u6876\uff0c\u9884\u671f\u5411\u6bcf\u4e2a\u6876\u5206\u914d 2 \u4e2a\u5143\u7d20\n int k = nums.length ~/ 2;\n List<List<double>> buckets = List.generate(k, (index) => []);\n\n // 1. \u5c06\u6570\u7ec4\u5143\u7d20\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\n for (double _num in nums) {\n // \u8f93\u5165\u6570\u636e\u8303\u56f4\u4e3a [0, 1)\uff0c\u4f7f\u7528 _num * k \u6620\u5c04\u5230\u7d22\u5f15\u8303\u56f4 [0, k-1]\n int i = (_num * k).toInt();\n // \u5c06 _num \u6dfb\u52a0\u8fdb\u6876 bucket_idx\n buckets[i].add(_num);\n }\n // 2. \u5bf9\u5404\u4e2a\u6876\u6267\u884c\u6392\u5e8f\n for (List<double> bucket in buckets) {\n bucket.sort();\n }\n // 3. \u904d\u5386\u6876\u5408\u5e76\u7ed3\u679c\n int i = 0;\n for (List<double> bucket in buckets) {\n for (double _num in bucket) {\n nums[i++] = _num;\n }\n }\n}\n
bucket_sort.rs/* \u6876\u6392\u5e8f */\nfn bucket_sort(nums: &mut [f64]) {\n // \u521d\u59cb\u5316 k = n/2 \u4e2a\u6876\uff0c\u9884\u671f\u5411\u6bcf\u4e2a\u6876\u5206\u914d 2 \u4e2a\u5143\u7d20\n let k = nums.len() / 2;\n let mut buckets = vec![vec![]; k];\n // 1. \u5c06\u6570\u7ec4\u5143\u7d20\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\n for &mut num in &mut *nums {\n // \u8f93\u5165\u6570\u636e\u8303\u56f4\u4e3a [0, 1)\uff0c\u4f7f\u7528 num * k \u6620\u5c04\u5230\u7d22\u5f15\u8303\u56f4 [0, k-1]\n let i = (num * k as f64) as usize;\n // \u5c06 num \u6dfb\u52a0\u8fdb\u6876 i\n buckets[i].push(num);\n }\n // 2. \u5bf9\u5404\u4e2a\u6876\u6267\u884c\u6392\u5e8f\n for bucket in &mut buckets {\n // \u4f7f\u7528\u5185\u7f6e\u6392\u5e8f\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u66ff\u6362\u6210\u5176\u4ed6\u6392\u5e8f\u7b97\u6cd5\n bucket.sort_by(|a, b| a.partial_cmp(b).unwrap());\n }\n // 3. \u904d\u5386\u6876\u5408\u5e76\u7ed3\u679c\n let mut i = 0;\n for bucket in &mut buckets {\n for &mut num in bucket {\n nums[i] = num;\n i += 1;\n }\n }\n}\n
bucket_sort.c/* \u6876\u6392\u5e8f */\nvoid bucketSort(float nums[], int size) {\n // \u521d\u59cb\u5316 k = n/2 \u4e2a\u6876\uff0c\u9884\u671f\u5411\u6bcf\u4e2a\u6876\u5206\u914d 2 \u4e2a\u5143\u7d20\n int k = size / 2;\n float **buckets = calloc(k, sizeof(float *));\n for (int i = 0; i < k; i++) {\n // \u6bcf\u4e2a\u6876\u6700\u591a\u53ef\u4ee5\u5206\u914d k \u4e2a\u5143\u7d20\n buckets[i] = calloc(ARRAY_SIZE, sizeof(float));\n }\n\n // 1. \u5c06\u6570\u7ec4\u5143\u7d20\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\n for (int i = 0; i < size; i++) {\n // \u8f93\u5165\u6570\u636e\u8303\u56f4\u4e3a [0, 1)\uff0c\u4f7f\u7528 num * k \u6620\u5c04\u5230\u7d22\u5f15\u8303\u56f4 [0, k-1]\n int bucket_idx = nums[i] * k;\n int j = 0;\n // \u5982\u679c\u6876\u4e2d\u6709\u6570\u636e\u4e14\u6570\u636e\u5c0f\u4e8e\u5f53\u524d\u503c nums[i], \u8981\u5c06\u5176\u653e\u5230\u5f53\u524d\u6876\u7684\u540e\u9762\uff0c\u76f8\u5f53\u4e8e cpp \u4e2d\u7684 push_back\n while (buckets[bucket_idx][j] > 0 && buckets[bucket_idx][j] < nums[i]) {\n j++;\n }\n float temp = nums[i];\n while (j < ARRAY_SIZE && buckets[bucket_idx][j] > 0) {\n swap(&temp, &buckets[bucket_idx][j]);\n j++;\n }\n buckets[bucket_idx][j] = temp;\n }\n\n // 2. \u5bf9\u5404\u4e2a\u6876\u6267\u884c\u6392\u5e8f\n for (int i = 0; i < k; i++) {\n qsort(buckets[i], ARRAY_SIZE, sizeof(float), compare_float);\n }\n\n // 3. \u904d\u5386\u6876\u5408\u5e76\u7ed3\u679c\n for (int i = 0, j = 0; j < k; j++) {\n for (int l = 0; l < ARRAY_SIZE; l++) {\n if (buckets[j][l] > 0) {\n nums[i++] = buckets[j][l];\n }\n }\n }\n\n // \u91ca\u653e\u4e0a\u8ff0\u5206\u914d\u7684\u5185\u5b58\n for (int i = 0; i < k; i++) {\n free(buckets[i]);\n }\n free(buckets);\n}\n
bucket_sort.zig[class]{}-[func]{bucketSort}\n
"},{"location":"chapter_sorting/bucket_sort/#1182","title":"11.8.2 \u00a0 \u7b97\u6cd5\u7279\u6027","text":"\u6876\u6392\u5e8f\u9002\u7528\u4e8e\u5904\u7406\u4f53\u91cf\u5f88\u5927\u7684\u6570\u636e\u3002\u4f8b\u5982\uff0c\u8f93\u5165\u6570\u636e\u5305\u542b 100 \u4e07\u4e2a\u5143\u7d20\uff0c\u7531\u4e8e\u7a7a\u95f4\u9650\u5236\uff0c\u7cfb\u7edf\u5185\u5b58\u65e0\u6cd5\u4e00\u6b21\u6027\u52a0\u8f7d\u6240\u6709\u6570\u636e\u3002\u6b64\u65f6\uff0c\u53ef\u4ee5\u5c06\u6570\u636e\u5206\u6210 1000 \u4e2a\u6876\uff0c\u7136\u540e\u5206\u522b\u5bf9\u6bcf\u4e2a\u6876\u8fdb\u884c\u6392\u5e8f\uff0c\u6700\u540e\u5c06\u7ed3\u679c\u5408\u5e76\u3002
\u6876\u6392\u5e8f\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u7406\u8bba\u4e0a\u53ef\u4ee5\u8fbe\u5230 \\(O(n)\\) \uff0c\u5173\u952e\u5728\u4e8e\u5c06\u5143\u7d20\u5747\u5300\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\uff0c\u56e0\u4e3a\u5b9e\u9645\u6570\u636e\u5f80\u5f80\u4e0d\u662f\u5747\u5300\u5206\u5e03\u7684\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u60f3\u8981\u5c06\u6dd8\u5b9d\u4e0a\u7684\u6240\u6709\u5546\u54c1\u6309\u4ef7\u683c\u8303\u56f4\u5e73\u5747\u5206\u914d\u5230 10 \u4e2a\u6876\u4e2d\uff0c\u4f46\u5546\u54c1\u4ef7\u683c\u5206\u5e03\u4e0d\u5747\uff0c\u4f4e\u4e8e 100 \u5143\u7684\u975e\u5e38\u591a\uff0c\u9ad8\u4e8e 1000 \u5143\u7684\u975e\u5e38\u5c11\u3002\u82e5\u5c06\u4ef7\u683c\u533a\u95f4\u5e73\u5747\u5212\u5206\u4e3a 10 \u4e2a\uff0c\u5404\u4e2a\u6876\u4e2d\u7684\u5546\u54c1\u6570\u91cf\u5dee\u8ddd\u4f1a\u975e\u5e38\u5927\u3002
\u4e3a\u5b9e\u73b0\u5e73\u5747\u5206\u914d\uff0c\u6211\u4eec\u53ef\u4ee5\u5148\u8bbe\u5b9a\u4e00\u6761\u5927\u81f4\u7684\u5206\u754c\u7ebf\uff0c\u5c06\u6570\u636e\u7c97\u7565\u5730\u5206\u5230 3 \u4e2a\u6876\u4e2d\u3002\u5206\u914d\u5b8c\u6bd5\u540e\uff0c\u518d\u5c06\u5546\u54c1\u8f83\u591a\u7684\u6876\u7ee7\u7eed\u5212\u5206\u4e3a 3 \u4e2a\u6876\uff0c\u76f4\u81f3\u6240\u6709\u6876\u4e2d\u7684\u5143\u7d20\u6570\u91cf\u5927\u81f4\u76f8\u7b49\u3002
\u5982\u56fe 11-14 \u6240\u793a\uff0c\u8fd9\u79cd\u65b9\u6cd5\u672c\u8d28\u4e0a\u662f\u521b\u5efa\u4e00\u68f5\u9012\u5f52\u6811\uff0c\u76ee\u6807\u662f\u8ba9\u53f6\u8282\u70b9\u7684\u503c\u5c3d\u53ef\u80fd\u5e73\u5747\u3002\u5f53\u7136\uff0c\u4e0d\u4e00\u5b9a\u8981\u6bcf\u8f6e\u5c06\u6570\u636e\u5212\u5206\u4e3a 3 \u4e2a\u6876\uff0c\u5177\u4f53\u5212\u5206\u65b9\u5f0f\u53ef\u6839\u636e\u6570\u636e\u7279\u70b9\u7075\u6d3b\u9009\u62e9\u3002
\u56fe 11-14 \u00a0 \u9012\u5f52\u5212\u5206\u6876
\u5982\u679c\u6211\u4eec\u63d0\u524d\u77e5\u9053\u5546\u54c1\u4ef7\u683c\u7684\u6982\u7387\u5206\u5e03\uff0c\u5219\u53ef\u4ee5\u6839\u636e\u6570\u636e\u6982\u7387\u5206\u5e03\u8bbe\u7f6e\u6bcf\u4e2a\u6876\u7684\u4ef7\u683c\u5206\u754c\u7ebf\u3002\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u6570\u636e\u5206\u5e03\u5e76\u4e0d\u4e00\u5b9a\u9700\u8981\u7279\u610f\u7edf\u8ba1\uff0c\u4e5f\u53ef\u4ee5\u6839\u636e\u6570\u636e\u7279\u70b9\u91c7\u7528\u67d0\u79cd\u6982\u7387\u6a21\u578b\u8fdb\u884c\u8fd1\u4f3c\u3002
\u5982\u56fe 11-15 \u6240\u793a\uff0c\u6211\u4eec\u5047\u8bbe\u5546\u54c1\u4ef7\u683c\u670d\u4ece\u6b63\u6001\u5206\u5e03\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u5408\u7406\u5730\u8bbe\u5b9a\u4ef7\u683c\u533a\u95f4\uff0c\u4ece\u800c\u5c06\u5546\u54c1\u5e73\u5747\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\u3002
\u56fe 11-15 \u00a0 \u6839\u636e\u6982\u7387\u5206\u5e03\u5212\u5206\u6876
"},{"location":"chapter_sorting/counting_sort/","title":"11.9 \u00a0 \u8ba1\u6570\u6392\u5e8f","text":"\u300c\u8ba1\u6570\u6392\u5e8f counting sort\u300d\u901a\u8fc7\u7edf\u8ba1\u5143\u7d20\u6570\u91cf\u6765\u5b9e\u73b0\u6392\u5e8f\uff0c\u901a\u5e38\u5e94\u7528\u4e8e\u6574\u6570\u6570\u7ec4\u3002
"},{"location":"chapter_sorting/counting_sort/#1191","title":"11.9.1 \u00a0 \u7b80\u5355\u5b9e\u73b0","text":"\u5148\u6765\u770b\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\u3002\u7ed9\u5b9a\u4e00\u4e2a\u957f\u5ea6\u4e3a \\(n\\) \u7684\u6570\u7ec4 nums
\uff0c\u5176\u4e2d\u7684\u5143\u7d20\u90fd\u662f\u201c\u975e\u8d1f\u6574\u6570\u201d\uff0c\u8ba1\u6570\u6392\u5e8f\u7684\u6574\u4f53\u6d41\u7a0b\u5982\u56fe 11-16 \u6240\u793a\u3002
counter
\u3002counter
\u7edf\u8ba1 nums
\u4e2d\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\uff0c\u5176\u4e2d counter[num]
\u5bf9\u5e94\u6570\u5b57 num
\u7684\u51fa\u73b0\u6b21\u6570\u3002\u7edf\u8ba1\u65b9\u6cd5\u5f88\u7b80\u5355\uff0c\u53ea\u9700\u904d\u5386 nums
\uff08\u8bbe\u5f53\u524d\u6570\u5b57\u4e3a num
\uff09\uff0c\u6bcf\u8f6e\u5c06 counter[num]
\u589e\u52a0 \\(1\\) \u5373\u53ef\u3002counter
\u7684\u5404\u4e2a\u7d22\u5f15\u5929\u7136\u6709\u5e8f\uff0c\u56e0\u6b64\u76f8\u5f53\u4e8e\u6240\u6709\u6570\u5b57\u5df2\u7ecf\u6392\u5e8f\u597d\u4e86\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u904d\u5386 counter
\uff0c\u6839\u636e\u5404\u6570\u5b57\u51fa\u73b0\u6b21\u6570\u4ece\u5c0f\u5230\u5927\u7684\u987a\u5e8f\u586b\u5165 nums
\u5373\u53ef\u3002\u56fe 11-16 \u00a0 \u8ba1\u6570\u6392\u5e8f\u6d41\u7a0b
\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig counting_sort.pydef counting_sort_naive(nums: list[int]):\n \"\"\"\u8ba1\u6570\u6392\u5e8f\"\"\"\n # \u7b80\u5355\u5b9e\u73b0\uff0c\u65e0\u6cd5\u7528\u4e8e\u6392\u5e8f\u5bf9\u8c61\n # 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n m = 0\n for num in nums:\n m = max(m, num)\n # 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n # counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n counter = [0] * (m + 1)\n for num in nums:\n counter[num] += 1\n # 3. \u904d\u5386 counter \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u539f\u6570\u7ec4 nums\n i = 0\n for num in range(m + 1):\n for _ in range(counter[num]):\n nums[i] = num\n i += 1\n
counting_sort.cpp/* \u8ba1\u6570\u6392\u5e8f */\n// \u7b80\u5355\u5b9e\u73b0\uff0c\u65e0\u6cd5\u7528\u4e8e\u6392\u5e8f\u5bf9\u8c61\nvoid countingSortNaive(vector<int> &nums) {\n // 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n int m = 0;\n for (int num : nums) {\n m = max(m, num);\n }\n // 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n vector<int> counter(m + 1, 0);\n for (int num : nums) {\n counter[num]++;\n }\n // 3. \u904d\u5386 counter \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u539f\u6570\u7ec4 nums\n int i = 0;\n for (int num = 0; num < m + 1; num++) {\n for (int j = 0; j < counter[num]; j++, i++) {\n nums[i] = num;\n }\n }\n}\n
counting_sort.java/* \u8ba1\u6570\u6392\u5e8f */\n// \u7b80\u5355\u5b9e\u73b0\uff0c\u65e0\u6cd5\u7528\u4e8e\u6392\u5e8f\u5bf9\u8c61\nvoid countingSortNaive(int[] nums) {\n // 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n int m = 0;\n for (int num : nums) {\n m = Math.max(m, num);\n }\n // 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n int[] counter = new int[m + 1];\n for (int num : nums) {\n counter[num]++;\n }\n // 3. \u904d\u5386 counter \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u539f\u6570\u7ec4 nums\n int i = 0;\n for (int num = 0; num < m + 1; num++) {\n for (int j = 0; j < counter[num]; j++, i++) {\n nums[i] = num;\n }\n }\n}\n
counting_sort.cs/* \u8ba1\u6570\u6392\u5e8f */\n// \u7b80\u5355\u5b9e\u73b0\uff0c\u65e0\u6cd5\u7528\u4e8e\u6392\u5e8f\u5bf9\u8c61\nvoid CountingSortNaive(int[] nums) {\n // 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n int m = 0;\n foreach (int num in nums) {\n m = Math.Max(m, num);\n }\n // 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n int[] counter = new int[m + 1];\n foreach (int num in nums) {\n counter[num]++;\n }\n // 3. \u904d\u5386 counter \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u539f\u6570\u7ec4 nums\n int i = 0;\n for (int num = 0; num < m + 1; num++) {\n for (int j = 0; j < counter[num]; j++, i++) {\n nums[i] = num;\n }\n }\n}\n
counting_sort.go/* \u8ba1\u6570\u6392\u5e8f */\n// \u7b80\u5355\u5b9e\u73b0\uff0c\u65e0\u6cd5\u7528\u4e8e\u6392\u5e8f\u5bf9\u8c61\nfunc countingSortNaive(nums []int) {\n // 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n m := 0\n for _, num := range nums {\n if num > m {\n m = num\n }\n }\n // 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n counter := make([]int, m+1)\n for _, num := range nums {\n counter[num]++\n }\n // 3. \u904d\u5386 counter \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u539f\u6570\u7ec4 nums\n for i, num := 0, 0; num < m+1; num++ {\n for j := 0; j < counter[num]; j++ {\n nums[i] = num\n i++\n }\n }\n}\n
counting_sort.swift/* \u8ba1\u6570\u6392\u5e8f */\n// \u7b80\u5355\u5b9e\u73b0\uff0c\u65e0\u6cd5\u7528\u4e8e\u6392\u5e8f\u5bf9\u8c61\nfunc countingSortNaive(nums: inout [Int]) {\n // 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n let m = nums.max()!\n // 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n var counter = Array(repeating: 0, count: m + 1)\n for num in nums {\n counter[num] += 1\n }\n // 3. \u904d\u5386 counter \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u539f\u6570\u7ec4 nums\n var i = 0\n for num in stride(from: 0, to: m + 1, by: 1) {\n for _ in stride(from: 0, to: counter[num], by: 1) {\n nums[i] = num\n i += 1\n }\n }\n}\n
counting_sort.js/* \u8ba1\u6570\u6392\u5e8f */\n// \u7b80\u5355\u5b9e\u73b0\uff0c\u65e0\u6cd5\u7528\u4e8e\u6392\u5e8f\u5bf9\u8c61\nfunction countingSortNaive(nums) {\n // 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n let m = 0;\n for (const num of nums) {\n m = Math.max(m, num);\n }\n // 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n const counter = new Array(m + 1).fill(0);\n for (const num of nums) {\n counter[num]++;\n }\n // 3. \u904d\u5386 counter \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u539f\u6570\u7ec4 nums\n let i = 0;\n for (let num = 0; num < m + 1; num++) {\n for (let j = 0; j < counter[num]; j++, i++) {\n nums[i] = num;\n }\n }\n}\n
counting_sort.ts/* \u8ba1\u6570\u6392\u5e8f */\n// \u7b80\u5355\u5b9e\u73b0\uff0c\u65e0\u6cd5\u7528\u4e8e\u6392\u5e8f\u5bf9\u8c61\nfunction countingSortNaive(nums: number[]): void {\n // 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n let m = 0;\n for (const num of nums) {\n m = Math.max(m, num);\n }\n // 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n const counter: number[] = new Array<number>(m + 1).fill(0);\n for (const num of nums) {\n counter[num]++;\n }\n // 3. \u904d\u5386 counter \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u539f\u6570\u7ec4 nums\n let i = 0;\n for (let num = 0; num < m + 1; num++) {\n for (let j = 0; j < counter[num]; j++, i++) {\n nums[i] = num;\n }\n }\n}\n
counting_sort.dart/* \u8ba1\u6570\u6392\u5e8f */\n// \u7b80\u5355\u5b9e\u73b0\uff0c\u65e0\u6cd5\u7528\u4e8e\u6392\u5e8f\u5bf9\u8c61\nvoid countingSortNaive(List<int> nums) {\n // 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n int m = 0;\n for (int _num in nums) {\n m = max(m, _num);\n }\n // 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n // counter[_num] \u4ee3\u8868 _num \u7684\u51fa\u73b0\u6b21\u6570\n List<int> counter = List.filled(m + 1, 0);\n for (int _num in nums) {\n counter[_num]++;\n }\n // 3. \u904d\u5386 counter \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u539f\u6570\u7ec4 nums\n int i = 0;\n for (int _num = 0; _num < m + 1; _num++) {\n for (int j = 0; j < counter[_num]; j++, i++) {\n nums[i] = _num;\n }\n }\n}\n
counting_sort.rs/* \u8ba1\u6570\u6392\u5e8f */\n// \u7b80\u5355\u5b9e\u73b0\uff0c\u65e0\u6cd5\u7528\u4e8e\u6392\u5e8f\u5bf9\u8c61\nfn counting_sort_naive(nums: &mut [i32]) {\n // 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n let m = *nums.into_iter().max().unwrap();\n // 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n let mut counter = vec![0; m as usize + 1];\n for &num in &*nums {\n counter[num as usize] += 1;\n }\n // 3. \u904d\u5386 counter \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u539f\u6570\u7ec4 nums\n let mut i = 0;\n for num in 0..m + 1 {\n for _ in 0..counter[num as usize] {\n nums[i] = num;\n i += 1;\n }\n }\n}\n
counting_sort.c/* \u8ba1\u6570\u6392\u5e8f */\n// \u7b80\u5355\u5b9e\u73b0\uff0c\u65e0\u6cd5\u7528\u4e8e\u6392\u5e8f\u5bf9\u8c61\nvoid countingSortNaive(int nums[], int size) {\n // 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n int m = 0;\n for (int i = 0; i < size; i++) {\n if (nums[i] > m) {\n m = nums[i];\n }\n }\n // 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n int *counter = calloc(m, sizeof(int));\n for (int i = 0; i < size; i++) {\n counter[nums[i]]++;\n }\n // 3. \u904d\u5386 counter \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u539f\u6570\u7ec4 nums\n int i = 0;\n for (int num = 0; num < m + 1; num++) {\n for (int j = 0; j < counter[num]; j++, i++) {\n nums[i] = num;\n }\n }\n // 4. \u91ca\u653e\u5185\u5b58\n free(counter);\n}\n
counting_sort.zig[class]{}-[func]{countingSortNaive}\n
\u8ba1\u6570\u6392\u5e8f\u4e0e\u6876\u6392\u5e8f\u7684\u8054\u7cfb
\u4ece\u6876\u6392\u5e8f\u7684\u89d2\u5ea6\u770b\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u8ba1\u6570\u6392\u5e8f\u4e2d\u7684\u8ba1\u6570\u6570\u7ec4 counter
\u7684\u6bcf\u4e2a\u7d22\u5f15\u89c6\u4e3a\u4e00\u4e2a\u6876\uff0c\u5c06\u7edf\u8ba1\u6570\u91cf\u7684\u8fc7\u7a0b\u770b\u4f5c\u5c06\u5404\u4e2a\u5143\u7d20\u5206\u914d\u5230\u5bf9\u5e94\u7684\u6876\u4e2d\u3002\u672c\u8d28\u4e0a\uff0c\u8ba1\u6570\u6392\u5e8f\u662f\u6876\u6392\u5e8f\u5728\u6574\u578b\u6570\u636e\u4e0b\u7684\u4e00\u4e2a\u7279\u4f8b\u3002
\u7ec6\u5fc3\u7684\u8bfb\u8005\u53ef\u80fd\u53d1\u73b0\u4e86\uff0c\u5982\u679c\u8f93\u5165\u6570\u636e\u662f\u5bf9\u8c61\uff0c\u4e0a\u8ff0\u6b65\u9aa4 3.
\u5c31\u5931\u6548\u4e86\u3002\u5047\u8bbe\u8f93\u5165\u6570\u636e\u662f\u5546\u54c1\u5bf9\u8c61\uff0c\u6211\u4eec\u60f3\u6309\u7167\u5546\u54c1\u4ef7\u683c\uff08\u7c7b\u7684\u6210\u5458\u53d8\u91cf\uff09\u5bf9\u5546\u54c1\u8fdb\u884c\u6392\u5e8f\uff0c\u800c\u4e0a\u8ff0\u7b97\u6cd5\u53ea\u80fd\u7ed9\u51fa\u4ef7\u683c\u7684\u6392\u5e8f\u7ed3\u679c\u3002
\u90a3\u4e48\u5982\u4f55\u624d\u80fd\u5f97\u5230\u539f\u6570\u636e\u7684\u6392\u5e8f\u7ed3\u679c\u5462\uff1f\u6211\u4eec\u9996\u5148\u8ba1\u7b97 counter
\u7684\u201c\u524d\u7f00\u548c\u201d\u3002\u987e\u540d\u601d\u4e49\uff0c\u7d22\u5f15 i
\u5904\u7684\u524d\u7f00\u548c prefix[i]
\u7b49\u4e8e\u6570\u7ec4\u524d i
\u4e2a\u5143\u7d20\u4e4b\u548c\uff1a
\u524d\u7f00\u548c\u5177\u6709\u660e\u786e\u7684\u610f\u4e49\uff0cprefix[num] - 1
\u4ee3\u8868\u5143\u7d20 num
\u5728\u7ed3\u679c\u6570\u7ec4 res
\u4e2d\u6700\u540e\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\u3002\u8fd9\u4e2a\u4fe1\u606f\u975e\u5e38\u5173\u952e\uff0c\u56e0\u4e3a\u5b83\u544a\u8bc9\u6211\u4eec\u5404\u4e2a\u5143\u7d20\u5e94\u8be5\u51fa\u73b0\u5728\u7ed3\u679c\u6570\u7ec4\u7684\u54ea\u4e2a\u4f4d\u7f6e\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5012\u5e8f\u904d\u5386\u539f\u6570\u7ec4 nums
\u7684\u6bcf\u4e2a\u5143\u7d20 num
\uff0c\u5728\u6bcf\u8f6e\u8fed\u4ee3\u4e2d\u6267\u884c\u4ee5\u4e0b\u4e24\u6b65\u3002
num
\u586b\u5165\u6570\u7ec4 res
\u7684\u7d22\u5f15 prefix[num] - 1
\u5904\u3002prefix[num]
\u51cf\u5c0f \\(1\\) \uff0c\u4ece\u800c\u5f97\u5230\u4e0b\u6b21\u653e\u7f6e num
\u7684\u7d22\u5f15\u3002\u904d\u5386\u5b8c\u6210\u540e\uff0c\u6570\u7ec4 res
\u4e2d\u5c31\u662f\u6392\u5e8f\u597d\u7684\u7ed3\u679c\uff0c\u6700\u540e\u4f7f\u7528 res
\u8986\u76d6\u539f\u6570\u7ec4 nums
\u5373\u53ef\u3002\u56fe 11-17 \u5c55\u793a\u4e86\u5b8c\u6574\u7684\u8ba1\u6570\u6392\u5e8f\u6d41\u7a0b\u3002
\u56fe 11-17 \u00a0 \u8ba1\u6570\u6392\u5e8f\u6b65\u9aa4
\u8ba1\u6570\u6392\u5e8f\u7684\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig counting_sort.pydef counting_sort(nums: list[int]):\n \"\"\"\u8ba1\u6570\u6392\u5e8f\"\"\"\n # \u5b8c\u6574\u5b9e\u73b0\uff0c\u53ef\u6392\u5e8f\u5bf9\u8c61\uff0c\u5e76\u4e14\u662f\u7a33\u5b9a\u6392\u5e8f\n # 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n m = max(nums)\n # 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n # counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n counter = [0] * (m + 1)\n for num in nums:\n counter[num] += 1\n # 3. \u6c42 counter \u7684\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u6b21\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u5c3e\u7d22\u5f15\u201d\n # \u5373 counter[num]-1 \u662f num \u5728 res \u4e2d\u6700\u540e\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\n for i in range(m):\n counter[i + 1] += counter[i]\n # 4. \u5012\u5e8f\u904d\u5386 nums \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u7ed3\u679c\u6570\u7ec4 res\n # \u521d\u59cb\u5316\u6570\u7ec4 res \u7528\u4e8e\u8bb0\u5f55\u7ed3\u679c\n n = len(nums)\n res = [0] * n\n for i in range(n - 1, -1, -1):\n num = nums[i]\n res[counter[num] - 1] = num # \u5c06 num \u653e\u7f6e\u5230\u5bf9\u5e94\u7d22\u5f15\u5904\n counter[num] -= 1 # \u4ee4\u524d\u7f00\u548c\u81ea\u51cf 1 \uff0c\u5f97\u5230\u4e0b\u6b21\u653e\u7f6e num \u7684\u7d22\u5f15\n # \u4f7f\u7528\u7ed3\u679c\u6570\u7ec4 res \u8986\u76d6\u539f\u6570\u7ec4 nums\n for i in range(n):\n nums[i] = res[i]\n
counting_sort.cpp/* \u8ba1\u6570\u6392\u5e8f */\n// \u5b8c\u6574\u5b9e\u73b0\uff0c\u53ef\u6392\u5e8f\u5bf9\u8c61\uff0c\u5e76\u4e14\u662f\u7a33\u5b9a\u6392\u5e8f\nvoid countingSort(vector<int> &nums) {\n // 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n int m = 0;\n for (int num : nums) {\n m = max(m, num);\n }\n // 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n vector<int> counter(m + 1, 0);\n for (int num : nums) {\n counter[num]++;\n }\n // 3. \u6c42 counter \u7684\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u6b21\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u5c3e\u7d22\u5f15\u201d\n // \u5373 counter[num]-1 \u662f num \u5728 res \u4e2d\u6700\u540e\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\n for (int i = 0; i < m; i++) {\n counter[i + 1] += counter[i];\n }\n // 4. \u5012\u5e8f\u904d\u5386 nums \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u7ed3\u679c\u6570\u7ec4 res\n // \u521d\u59cb\u5316\u6570\u7ec4 res \u7528\u4e8e\u8bb0\u5f55\u7ed3\u679c\n int n = nums.size();\n vector<int> res(n);\n for (int i = n - 1; i >= 0; i--) {\n int num = nums[i];\n res[counter[num] - 1] = num; // \u5c06 num \u653e\u7f6e\u5230\u5bf9\u5e94\u7d22\u5f15\u5904\n counter[num]--; // \u4ee4\u524d\u7f00\u548c\u81ea\u51cf 1 \uff0c\u5f97\u5230\u4e0b\u6b21\u653e\u7f6e num \u7684\u7d22\u5f15\n }\n // \u4f7f\u7528\u7ed3\u679c\u6570\u7ec4 res \u8986\u76d6\u539f\u6570\u7ec4 nums\n nums = res;\n}\n
counting_sort.java/* \u8ba1\u6570\u6392\u5e8f */\n// \u5b8c\u6574\u5b9e\u73b0\uff0c\u53ef\u6392\u5e8f\u5bf9\u8c61\uff0c\u5e76\u4e14\u662f\u7a33\u5b9a\u6392\u5e8f\nvoid countingSort(int[] nums) {\n // 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n int m = 0;\n for (int num : nums) {\n m = Math.max(m, num);\n }\n // 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n int[] counter = new int[m + 1];\n for (int num : nums) {\n counter[num]++;\n }\n // 3. \u6c42 counter \u7684\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u6b21\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u5c3e\u7d22\u5f15\u201d\n // \u5373 counter[num]-1 \u662f num \u5728 res \u4e2d\u6700\u540e\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\n for (int i = 0; i < m; i++) {\n counter[i + 1] += counter[i];\n }\n // 4. \u5012\u5e8f\u904d\u5386 nums \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u7ed3\u679c\u6570\u7ec4 res\n // \u521d\u59cb\u5316\u6570\u7ec4 res \u7528\u4e8e\u8bb0\u5f55\u7ed3\u679c\n int n = nums.length;\n int[] res = new int[n];\n for (int i = n - 1; i >= 0; i--) {\n int num = nums[i];\n res[counter[num] - 1] = num; // \u5c06 num \u653e\u7f6e\u5230\u5bf9\u5e94\u7d22\u5f15\u5904\n counter[num]--; // \u4ee4\u524d\u7f00\u548c\u81ea\u51cf 1 \uff0c\u5f97\u5230\u4e0b\u6b21\u653e\u7f6e num \u7684\u7d22\u5f15\n }\n // \u4f7f\u7528\u7ed3\u679c\u6570\u7ec4 res \u8986\u76d6\u539f\u6570\u7ec4 nums\n for (int i = 0; i < n; i++) {\n nums[i] = res[i];\n }\n}\n
counting_sort.cs/* \u8ba1\u6570\u6392\u5e8f */\n// \u5b8c\u6574\u5b9e\u73b0\uff0c\u53ef\u6392\u5e8f\u5bf9\u8c61\uff0c\u5e76\u4e14\u662f\u7a33\u5b9a\u6392\u5e8f\nvoid CountingSort(int[] nums) {\n // 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n int m = 0;\n foreach (int num in nums) {\n m = Math.Max(m, num);\n }\n // 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n int[] counter = new int[m + 1];\n foreach (int num in nums) {\n counter[num]++;\n }\n // 3. \u6c42 counter \u7684\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u6b21\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u5c3e\u7d22\u5f15\u201d\n // \u5373 counter[num]-1 \u662f num \u5728 res \u4e2d\u6700\u540e\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\n for (int i = 0; i < m; i++) {\n counter[i + 1] += counter[i];\n }\n // 4. \u5012\u5e8f\u904d\u5386 nums \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u7ed3\u679c\u6570\u7ec4 res\n // \u521d\u59cb\u5316\u6570\u7ec4 res \u7528\u4e8e\u8bb0\u5f55\u7ed3\u679c\n int n = nums.Length;\n int[] res = new int[n];\n for (int i = n - 1; i >= 0; i--) {\n int num = nums[i];\n res[counter[num] - 1] = num; // \u5c06 num \u653e\u7f6e\u5230\u5bf9\u5e94\u7d22\u5f15\u5904\n counter[num]--; // \u4ee4\u524d\u7f00\u548c\u81ea\u51cf 1 \uff0c\u5f97\u5230\u4e0b\u6b21\u653e\u7f6e num \u7684\u7d22\u5f15\n }\n // \u4f7f\u7528\u7ed3\u679c\u6570\u7ec4 res \u8986\u76d6\u539f\u6570\u7ec4 nums\n for (int i = 0; i < n; i++) {\n nums[i] = res[i];\n }\n}\n
counting_sort.go/* \u8ba1\u6570\u6392\u5e8f */\n// \u5b8c\u6574\u5b9e\u73b0\uff0c\u53ef\u6392\u5e8f\u5bf9\u8c61\uff0c\u5e76\u4e14\u662f\u7a33\u5b9a\u6392\u5e8f\nfunc countingSort(nums []int) {\n // 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n m := 0\n for _, num := range nums {\n if num > m {\n m = num\n }\n }\n // 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n counter := make([]int, m+1)\n for _, num := range nums {\n counter[num]++\n }\n // 3. \u6c42 counter \u7684\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u6b21\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u5c3e\u7d22\u5f15\u201d\n // \u5373 counter[num]-1 \u662f num \u5728 res \u4e2d\u6700\u540e\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\n for i := 0; i < m; i++ {\n counter[i+1] += counter[i]\n }\n // 4. \u5012\u5e8f\u904d\u5386 nums \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u7ed3\u679c\u6570\u7ec4 res\n // \u521d\u59cb\u5316\u6570\u7ec4 res \u7528\u4e8e\u8bb0\u5f55\u7ed3\u679c\n n := len(nums)\n res := make([]int, n)\n for i := n - 1; i >= 0; i-- {\n num := nums[i]\n // \u5c06 num \u653e\u7f6e\u5230\u5bf9\u5e94\u7d22\u5f15\u5904\n res[counter[num]-1] = num\n // \u4ee4\u524d\u7f00\u548c\u81ea\u51cf 1 \uff0c\u5f97\u5230\u4e0b\u6b21\u653e\u7f6e num \u7684\u7d22\u5f15\n counter[num]--\n }\n // \u4f7f\u7528\u7ed3\u679c\u6570\u7ec4 res \u8986\u76d6\u539f\u6570\u7ec4 nums\n copy(nums, res)\n}\n
counting_sort.swift/* \u8ba1\u6570\u6392\u5e8f */\n// \u5b8c\u6574\u5b9e\u73b0\uff0c\u53ef\u6392\u5e8f\u5bf9\u8c61\uff0c\u5e76\u4e14\u662f\u7a33\u5b9a\u6392\u5e8f\nfunc countingSort(nums: inout [Int]) {\n // 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n let m = nums.max()!\n // 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n var counter = Array(repeating: 0, count: m + 1)\n for num in nums {\n counter[num] += 1\n }\n // 3. \u6c42 counter \u7684\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u6b21\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u5c3e\u7d22\u5f15\u201d\n // \u5373 counter[num]-1 \u662f num \u5728 res \u4e2d\u6700\u540e\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\n for i in stride(from: 0, to: m, by: 1) {\n counter[i + 1] += counter[i]\n }\n // 4. \u5012\u5e8f\u904d\u5386 nums \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u7ed3\u679c\u6570\u7ec4 res\n // \u521d\u59cb\u5316\u6570\u7ec4 res \u7528\u4e8e\u8bb0\u5f55\u7ed3\u679c\n var res = Array(repeating: 0, count: nums.count)\n for i in stride(from: nums.count - 1, through: 0, by: -1) {\n let num = nums[i]\n res[counter[num] - 1] = num // \u5c06 num \u653e\u7f6e\u5230\u5bf9\u5e94\u7d22\u5f15\u5904\n counter[num] -= 1 // \u4ee4\u524d\u7f00\u548c\u81ea\u51cf 1 \uff0c\u5f97\u5230\u4e0b\u6b21\u653e\u7f6e num \u7684\u7d22\u5f15\n }\n // \u4f7f\u7528\u7ed3\u679c\u6570\u7ec4 res \u8986\u76d6\u539f\u6570\u7ec4 nums\n for i in stride(from: 0, to: nums.count, by: 1) {\n nums[i] = res[i]\n }\n}\n
counting_sort.js/* \u8ba1\u6570\u6392\u5e8f */\n// \u5b8c\u6574\u5b9e\u73b0\uff0c\u53ef\u6392\u5e8f\u5bf9\u8c61\uff0c\u5e76\u4e14\u662f\u7a33\u5b9a\u6392\u5e8f\nfunction countingSort(nums) {\n // 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n let m = 0;\n for (const num of nums) {\n m = Math.max(m, num);\n }\n // 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n const counter = new Array(m + 1).fill(0);\n for (const num of nums) {\n counter[num]++;\n }\n // 3. \u6c42 counter \u7684\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u6b21\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u5c3e\u7d22\u5f15\u201d\n // \u5373 counter[num]-1 \u662f num \u5728 res \u4e2d\u6700\u540e\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\n for (let i = 0; i < m; i++) {\n counter[i + 1] += counter[i];\n }\n // 4. \u5012\u5e8f\u904d\u5386 nums \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u7ed3\u679c\u6570\u7ec4 res\n // \u521d\u59cb\u5316\u6570\u7ec4 res \u7528\u4e8e\u8bb0\u5f55\u7ed3\u679c\n const n = nums.length;\n const res = new Array(n);\n for (let i = n - 1; i >= 0; i--) {\n const num = nums[i];\n res[counter[num] - 1] = num; // \u5c06 num \u653e\u7f6e\u5230\u5bf9\u5e94\u7d22\u5f15\u5904\n counter[num]--; // \u4ee4\u524d\u7f00\u548c\u81ea\u51cf 1 \uff0c\u5f97\u5230\u4e0b\u6b21\u653e\u7f6e num \u7684\u7d22\u5f15\n }\n // \u4f7f\u7528\u7ed3\u679c\u6570\u7ec4 res \u8986\u76d6\u539f\u6570\u7ec4 nums\n for (let i = 0; i < n; i++) {\n nums[i] = res[i];\n }\n}\n
counting_sort.ts/* \u8ba1\u6570\u6392\u5e8f */\n// \u5b8c\u6574\u5b9e\u73b0\uff0c\u53ef\u6392\u5e8f\u5bf9\u8c61\uff0c\u5e76\u4e14\u662f\u7a33\u5b9a\u6392\u5e8f\nfunction countingSort(nums: number[]): void {\n // 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n let m = 0;\n for (const num of nums) {\n m = Math.max(m, num);\n }\n // 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n const counter: number[] = new Array<number>(m + 1).fill(0);\n for (const num of nums) {\n counter[num]++;\n }\n // 3. \u6c42 counter \u7684\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u6b21\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u5c3e\u7d22\u5f15\u201d\n // \u5373 counter[num]-1 \u662f num \u5728 res \u4e2d\u6700\u540e\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\n for (let i = 0; i < m; i++) {\n counter[i + 1] += counter[i];\n }\n // 4. \u5012\u5e8f\u904d\u5386 nums \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u7ed3\u679c\u6570\u7ec4 res\n // \u521d\u59cb\u5316\u6570\u7ec4 res \u7528\u4e8e\u8bb0\u5f55\u7ed3\u679c\n const n = nums.length;\n const res: number[] = new Array<number>(n);\n for (let i = n - 1; i >= 0; i--) {\n const num = nums[i];\n res[counter[num] - 1] = num; // \u5c06 num \u653e\u7f6e\u5230\u5bf9\u5e94\u7d22\u5f15\u5904\n counter[num]--; // \u4ee4\u524d\u7f00\u548c\u81ea\u51cf 1 \uff0c\u5f97\u5230\u4e0b\u6b21\u653e\u7f6e num \u7684\u7d22\u5f15\n }\n // \u4f7f\u7528\u7ed3\u679c\u6570\u7ec4 res \u8986\u76d6\u539f\u6570\u7ec4 nums\n for (let i = 0; i < n; i++) {\n nums[i] = res[i];\n }\n}\n
counting_sort.dart/* \u8ba1\u6570\u6392\u5e8f */\n// \u5b8c\u6574\u5b9e\u73b0\uff0c\u53ef\u6392\u5e8f\u5bf9\u8c61\uff0c\u5e76\u4e14\u662f\u7a33\u5b9a\u6392\u5e8f\nvoid countingSort(List<int> nums) {\n // 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n int m = 0;\n for (int _num in nums) {\n m = max(m, _num);\n }\n // 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n // counter[_num] \u4ee3\u8868 _num \u7684\u51fa\u73b0\u6b21\u6570\n List<int> counter = List.filled(m + 1, 0);\n for (int _num in nums) {\n counter[_num]++;\n }\n // 3. \u6c42 counter \u7684\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u6b21\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u5c3e\u7d22\u5f15\u201d\n // \u5373 counter[_num]-1 \u662f _num \u5728 res \u4e2d\u6700\u540e\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\n for (int i = 0; i < m; i++) {\n counter[i + 1] += counter[i];\n }\n // 4. \u5012\u5e8f\u904d\u5386 nums \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u7ed3\u679c\u6570\u7ec4 res\n // \u521d\u59cb\u5316\u6570\u7ec4 res \u7528\u4e8e\u8bb0\u5f55\u7ed3\u679c\n int n = nums.length;\n List<int> res = List.filled(n, 0);\n for (int i = n - 1; i >= 0; i--) {\n int _num = nums[i];\n res[counter[_num] - 1] = _num; // \u5c06 _num \u653e\u7f6e\u5230\u5bf9\u5e94\u7d22\u5f15\u5904\n counter[_num]--; // \u4ee4\u524d\u7f00\u548c\u81ea\u51cf 1 \uff0c\u5f97\u5230\u4e0b\u6b21\u653e\u7f6e _num \u7684\u7d22\u5f15\n }\n // \u4f7f\u7528\u7ed3\u679c\u6570\u7ec4 res \u8986\u76d6\u539f\u6570\u7ec4 nums\n nums.setAll(0, res);\n}\n
counting_sort.rs/* \u8ba1\u6570\u6392\u5e8f */\n// \u5b8c\u6574\u5b9e\u73b0\uff0c\u53ef\u6392\u5e8f\u5bf9\u8c61\uff0c\u5e76\u4e14\u662f\u7a33\u5b9a\u6392\u5e8f\nfn counting_sort(nums: &mut [i32]) {\n // 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n let m = *nums.into_iter().max().unwrap();\n // 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n let mut counter = vec![0; m as usize + 1];\n for &num in &*nums {\n counter[num as usize] += 1;\n }\n // 3. \u6c42 counter \u7684\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u6b21\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u5c3e\u7d22\u5f15\u201d\n // \u5373 counter[num]-1 \u662f num \u5728 res \u4e2d\u6700\u540e\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\n for i in 0..m as usize {\n counter[i + 1] += counter[i];\n }\n // 4. \u5012\u5e8f\u904d\u5386 nums \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u7ed3\u679c\u6570\u7ec4 res\n // \u521d\u59cb\u5316\u6570\u7ec4 res \u7528\u4e8e\u8bb0\u5f55\u7ed3\u679c\n let n = nums.len();\n let mut res = vec![0; n];\n for i in (0..n).rev() {\n let num = nums[i];\n res[counter[num as usize] - 1] = num; // \u5c06 num \u653e\u7f6e\u5230\u5bf9\u5e94\u7d22\u5f15\u5904\n counter[num as usize] -= 1; // \u4ee4\u524d\u7f00\u548c\u81ea\u51cf 1 \uff0c\u5f97\u5230\u4e0b\u6b21\u653e\u7f6e num \u7684\u7d22\u5f15\n }\n // \u4f7f\u7528\u7ed3\u679c\u6570\u7ec4 res \u8986\u76d6\u539f\u6570\u7ec4 nums\n for i in 0..n {\n nums[i] = res[i];\n }\n}\n
counting_sort.c/* \u8ba1\u6570\u6392\u5e8f */\n// \u5b8c\u6574\u5b9e\u73b0\uff0c\u53ef\u6392\u5e8f\u5bf9\u8c61\uff0c\u5e76\u4e14\u662f\u7a33\u5b9a\u6392\u5e8f\nvoid countingSort(int nums[], int size) {\n // 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\n int m = 0;\n for (int i = 0; i < size; i++) {\n if (nums[i] > m) {\n m = nums[i];\n }\n }\n // 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\n int *counter = calloc(m, sizeof(int));\n for (int i = 0; i < size; i++) {\n counter[nums[i]]++;\n }\n // 3. \u6c42 counter \u7684\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u6b21\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u5c3e\u7d22\u5f15\u201d\n // \u5373 counter[num]-1 \u662f num \u5728 res \u4e2d\u6700\u540e\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\n for (int i = 0; i < m; i++) {\n counter[i + 1] += counter[i];\n }\n // 4. \u5012\u5e8f\u904d\u5386 nums \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u7ed3\u679c\u6570\u7ec4 res\n // \u521d\u59cb\u5316\u6570\u7ec4 res \u7528\u4e8e\u8bb0\u5f55\u7ed3\u679c\n int *res = malloc(sizeof(int) * size);\n for (int i = size - 1; i >= 0; i--) {\n int num = nums[i];\n res[counter[num] - 1] = num; // \u5c06 num \u653e\u7f6e\u5230\u5bf9\u5e94\u7d22\u5f15\u5904\n counter[num]--; // \u4ee4\u524d\u7f00\u548c\u81ea\u51cf 1 \uff0c\u5f97\u5230\u4e0b\u6b21\u653e\u7f6e num \u7684\u7d22\u5f15\n }\n // \u4f7f\u7528\u7ed3\u679c\u6570\u7ec4 res \u8986\u76d6\u539f\u6570\u7ec4 nums\n memcpy(nums, res, size * sizeof(int));\n // 5. \u91ca\u653e\u5185\u5b58\n free(counter);\n}\n
counting_sort.zig[class]{}-[func]{countingSort}\n
"},{"location":"chapter_sorting/counting_sort/#1193","title":"11.9.3 \u00a0 \u7b97\u6cd5\u7279\u6027","text":"nums
\u548c\u904d\u5386 counter
\uff0c\u90fd\u4f7f\u7528\u7ebf\u6027\u65f6\u95f4\u3002\u4e00\u822c\u60c5\u51b5\u4e0b \\(n \\gg m\\) \uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u8d8b\u4e8e \\(O(n)\\) \u3002res
\u548c counter
\u3002res
\u4e2d\u586b\u5145\u5143\u7d20\u7684\u987a\u5e8f\u662f\u201c\u4ece\u53f3\u5411\u5de6\u201d\u7684\uff0c\u56e0\u6b64\u5012\u5e8f\u904d\u5386 nums
\u53ef\u4ee5\u907f\u514d\u6539\u53d8\u76f8\u7b49\u5143\u7d20\u4e4b\u95f4\u7684\u76f8\u5bf9\u4f4d\u7f6e\uff0c\u4ece\u800c\u5b9e\u73b0\u7a33\u5b9a\u6392\u5e8f\u3002\u5b9e\u9645\u4e0a\uff0c\u6b63\u5e8f\u904d\u5386 nums
\u4e5f\u53ef\u4ee5\u5f97\u5230\u6b63\u786e\u7684\u6392\u5e8f\u7ed3\u679c\uff0c\u4f46\u7ed3\u679c\u662f\u975e\u7a33\u5b9a\u7684\u3002\u770b\u5230\u8fd9\u91cc\uff0c\u4f60\u4e5f\u8bb8\u4f1a\u89c9\u5f97\u8ba1\u6570\u6392\u5e8f\u975e\u5e38\u5de7\u5999\uff0c\u4ec5\u901a\u8fc7\u7edf\u8ba1\u6570\u91cf\u5c31\u53ef\u4ee5\u5b9e\u73b0\u9ad8\u6548\u7684\u6392\u5e8f\u3002\u7136\u800c\uff0c\u4f7f\u7528\u8ba1\u6570\u6392\u5e8f\u7684\u524d\u7f6e\u6761\u4ef6\u76f8\u5bf9\u8f83\u4e3a\u4e25\u683c\u3002
\u8ba1\u6570\u6392\u5e8f\u53ea\u9002\u7528\u4e8e\u975e\u8d1f\u6574\u6570\u3002\u82e5\u60f3\u5c06\u5176\u7528\u4e8e\u5176\u4ed6\u7c7b\u578b\u7684\u6570\u636e\uff0c\u9700\u8981\u786e\u4fdd\u8fd9\u4e9b\u6570\u636e\u53ef\u4ee5\u8f6c\u6362\u4e3a\u975e\u8d1f\u6574\u6570\uff0c\u5e76\u4e14\u5728\u8f6c\u6362\u8fc7\u7a0b\u4e2d\u4e0d\u80fd\u6539\u53d8\u5404\u4e2a\u5143\u7d20\u4e4b\u95f4\u7684\u76f8\u5bf9\u5927\u5c0f\u5173\u7cfb\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u5305\u542b\u8d1f\u6570\u7684\u6574\u6570\u6570\u7ec4\uff0c\u53ef\u4ee5\u5148\u7ed9\u6240\u6709\u6570\u5b57\u52a0\u4e0a\u4e00\u4e2a\u5e38\u6570\uff0c\u5c06\u5168\u90e8\u6570\u5b57\u8f6c\u5316\u4e3a\u6b63\u6570\uff0c\u6392\u5e8f\u5b8c\u6210\u540e\u518d\u8f6c\u6362\u56de\u53bb\u3002
\u8ba1\u6570\u6392\u5e8f\u9002\u7528\u4e8e\u6570\u636e\u91cf\u5927\u4f46\u6570\u636e\u8303\u56f4\u8f83\u5c0f\u7684\u60c5\u51b5\u3002\u6bd4\u5982\uff0c\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d \\(m\\) \u4e0d\u80fd\u592a\u5927\uff0c\u5426\u5219\u4f1a\u5360\u7528\u8fc7\u591a\u7a7a\u95f4\u3002\u800c\u5f53 \\(n \\ll m\\) \u65f6\uff0c\u8ba1\u6570\u6392\u5e8f\u4f7f\u7528 \\(O(m)\\) \u65f6\u95f4\uff0c\u53ef\u80fd\u6bd4 \\(O(n \\log n)\\) \u7684\u6392\u5e8f\u7b97\u6cd5\u8fd8\u8981\u6162\u3002
"},{"location":"chapter_sorting/heap_sort/","title":"11.7 \u00a0 \u5806\u6392\u5e8f","text":"Tip
\u9605\u8bfb\u672c\u8282\u524d\uff0c\u8bf7\u786e\u4fdd\u5df2\u5b66\u5b8c\u201c\u5806\u201c\u7ae0\u8282\u3002
\u300c\u5806\u6392\u5e8f heap sort\u300d\u662f\u4e00\u79cd\u57fa\u4e8e\u5806\u6570\u636e\u7ed3\u6784\u5b9e\u73b0\u7684\u9ad8\u6548\u6392\u5e8f\u7b97\u6cd5\u3002\u6211\u4eec\u53ef\u4ee5\u5229\u7528\u5df2\u7ecf\u5b66\u8fc7\u7684\u201c\u5efa\u5806\u64cd\u4f5c\u201d\u548c\u201c\u5143\u7d20\u51fa\u5806\u64cd\u4f5c\u201d\u5b9e\u73b0\u5806\u6392\u5e8f\u3002
\u4ee5\u4e0a\u65b9\u6cd5\u867d\u7136\u53ef\u884c\uff0c\u4f46\u9700\u8981\u501f\u52a9\u4e00\u4e2a\u989d\u5916\u6570\u7ec4\u6765\u4fdd\u5b58\u5f39\u51fa\u7684\u5143\u7d20\uff0c\u6bd4\u8f83\u6d6a\u8d39\u7a7a\u95f4\u3002\u5728\u5b9e\u9645\u4e2d\uff0c\u6211\u4eec\u901a\u5e38\u4f7f\u7528\u4e00\u79cd\u66f4\u52a0\u4f18\u96c5\u7684\u5b9e\u73b0\u65b9\u5f0f\u3002
"},{"location":"chapter_sorting/heap_sort/#1171","title":"11.7.1 \u00a0 \u7b97\u6cd5\u6d41\u7a0b","text":"\u8bbe\u6570\u7ec4\u7684\u957f\u5ea6\u4e3a \\(n\\) \uff0c\u5806\u6392\u5e8f\u7684\u6d41\u7a0b\u5982\u56fe 11-12 \u6240\u793a\u3002
2.
\u6b65\u548c\u7b2c 3.
\u6b65\u3002\u5faa\u73af \\(n - 1\\) \u8f6e\u540e\uff0c\u5373\u53ef\u5b8c\u6210\u6570\u7ec4\u6392\u5e8f\u3002Tip
\u5b9e\u9645\u4e0a\uff0c\u5143\u7d20\u51fa\u5806\u64cd\u4f5c\u4e2d\u4e5f\u5305\u542b\u7b2c 2.
\u6b65\u548c\u7b2c 3.
\u6b65\uff0c\u53ea\u662f\u591a\u4e86\u4e00\u4e2a\u5f39\u51fa\u5143\u7d20\u7684\u6b65\u9aa4\u3002
\u56fe 11-12 \u00a0 \u5806\u6392\u5e8f\u6b65\u9aa4
\u5728\u4ee3\u7801\u5b9e\u73b0\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u4e0e\u201c\u5806\u201d\u7ae0\u8282\u76f8\u540c\u7684\u4ece\u9876\u81f3\u5e95\u5806\u5316 sift_down()
\u51fd\u6570\u3002\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u7531\u4e8e\u5806\u7684\u957f\u5ea6\u4f1a\u968f\u7740\u63d0\u53d6\u6700\u5927\u5143\u7d20\u800c\u51cf\u5c0f\uff0c\u56e0\u6b64\u6211\u4eec\u9700\u8981\u7ed9 sift_down()
\u51fd\u6570\u6dfb\u52a0\u4e00\u4e2a\u957f\u5ea6\u53c2\u6570 \\(n\\) \uff0c\u7528\u4e8e\u6307\u5b9a\u5806\u7684\u5f53\u524d\u6709\u6548\u957f\u5ea6\u3002\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a
def sift_down(nums: list[int], n: int, i: int):\n \"\"\"\u5806\u7684\u957f\u5ea6\u4e3a n \uff0c\u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316\"\"\"\n while True:\n # \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n l = 2 * i + 1\n r = 2 * i + 2\n ma = i\n if l < n and nums[l] > nums[ma]:\n ma = l\n if r < n and nums[r] > nums[ma]:\n ma = r\n # \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n if ma == i:\n break\n # \u4ea4\u6362\u4e24\u8282\u70b9\n nums[i], nums[ma] = nums[ma], nums[i]\n # \u5faa\u73af\u5411\u4e0b\u5806\u5316\n i = ma\n\ndef heap_sort(nums: list[int]):\n \"\"\"\u5806\u6392\u5e8f\"\"\"\n # \u5efa\u5806\u64cd\u4f5c\uff1a\u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n for i in range(len(nums) // 2 - 1, -1, -1):\n sift_down(nums, len(nums), i)\n # \u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u5faa\u73af n-1 \u8f6e\n for i in range(len(nums) - 1, 0, -1):\n # \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n nums[0], nums[i] = nums[i], nums[0]\n # \u4ee5\u6839\u8282\u70b9\u4e3a\u8d77\u70b9\uff0c\u4ece\u9876\u81f3\u5e95\u8fdb\u884c\u5806\u5316\n sift_down(nums, i, 0)\n
heap_sort.cpp/* \u5806\u7684\u957f\u5ea6\u4e3a n \uff0c\u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nvoid siftDown(vector<int> &nums, int n, int i) {\n while (true) {\n // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n int l = 2 * i + 1;\n int r = 2 * i + 2;\n int ma = i;\n if (l < n && nums[l] > nums[ma])\n ma = l;\n if (r < n && nums[r] > nums[ma])\n ma = r;\n // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n if (ma == i) {\n break;\n }\n // \u4ea4\u6362\u4e24\u8282\u70b9\n swap(nums[i], nums[ma]);\n // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n i = ma;\n }\n}\n\n/* \u5806\u6392\u5e8f */\nvoid heapSort(vector<int> &nums) {\n // \u5efa\u5806\u64cd\u4f5c\uff1a\u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n for (int i = nums.size() / 2 - 1; i >= 0; --i) {\n siftDown(nums, nums.size(), i);\n }\n // \u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u5faa\u73af n-1 \u8f6e\n for (int i = nums.size() - 1; i > 0; --i) {\n // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n swap(nums[0], nums[i]);\n // \u4ee5\u6839\u8282\u70b9\u4e3a\u8d77\u70b9\uff0c\u4ece\u9876\u81f3\u5e95\u8fdb\u884c\u5806\u5316\n siftDown(nums, i, 0);\n }\n}\n
heap_sort.java/* \u5806\u7684\u957f\u5ea6\u4e3a n \uff0c\u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nvoid siftDown(int[] nums, int n, int i) {\n while (true) {\n // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n int l = 2 * i + 1;\n int r = 2 * i + 2;\n int ma = i;\n if (l < n && nums[l] > nums[ma])\n ma = l;\n if (r < n && nums[r] > nums[ma])\n ma = r;\n // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n if (ma == i)\n break;\n // \u4ea4\u6362\u4e24\u8282\u70b9\n int temp = nums[i];\n nums[i] = nums[ma];\n nums[ma] = temp;\n // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n i = ma;\n }\n}\n\n/* \u5806\u6392\u5e8f */\nvoid heapSort(int[] nums) {\n // \u5efa\u5806\u64cd\u4f5c\uff1a\u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n for (int i = nums.length / 2 - 1; i >= 0; i--) {\n siftDown(nums, nums.length, i);\n }\n // \u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u5faa\u73af n-1 \u8f6e\n for (int i = nums.length - 1; i > 0; i--) {\n // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n int tmp = nums[0];\n nums[0] = nums[i];\n nums[i] = tmp;\n // \u4ee5\u6839\u8282\u70b9\u4e3a\u8d77\u70b9\uff0c\u4ece\u9876\u81f3\u5e95\u8fdb\u884c\u5806\u5316\n siftDown(nums, i, 0);\n }\n}\n
heap_sort.cs/* \u5806\u7684\u957f\u5ea6\u4e3a n \uff0c\u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nvoid SiftDown(int[] nums, int n, int i) {\n while (true) {\n // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n int l = 2 * i + 1;\n int r = 2 * i + 2;\n int ma = i;\n if (l < n && nums[l] > nums[ma])\n ma = l;\n if (r < n && nums[r] > nums[ma])\n ma = r;\n // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n if (ma == i)\n break;\n // \u4ea4\u6362\u4e24\u8282\u70b9\n (nums[ma], nums[i]) = (nums[i], nums[ma]);\n // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n i = ma;\n }\n}\n\n/* \u5806\u6392\u5e8f */\nvoid HeapSort(int[] nums) {\n // \u5efa\u5806\u64cd\u4f5c\uff1a\u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n for (int i = nums.Length / 2 - 1; i >= 0; i--) {\n SiftDown(nums, nums.Length, i);\n }\n // \u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u5faa\u73af n-1 \u8f6e\n for (int i = nums.Length - 1; i > 0; i--) {\n // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n (nums[i], nums[0]) = (nums[0], nums[i]);\n // \u4ee5\u6839\u8282\u70b9\u4e3a\u8d77\u70b9\uff0c\u4ece\u9876\u81f3\u5e95\u8fdb\u884c\u5806\u5316\n SiftDown(nums, i, 0);\n }\n}\n
heap_sort.go/* \u5806\u7684\u957f\u5ea6\u4e3a n \uff0c\u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nfunc siftDown(nums *[]int, n, i int) {\n for true {\n // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n l := 2*i + 1\n r := 2*i + 2\n ma := i\n if l < n && (*nums)[l] > (*nums)[ma] {\n ma = l\n }\n if r < n && (*nums)[r] > (*nums)[ma] {\n ma = r\n }\n // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n if ma == i {\n break\n }\n // \u4ea4\u6362\u4e24\u8282\u70b9\n (*nums)[i], (*nums)[ma] = (*nums)[ma], (*nums)[i]\n // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n i = ma\n }\n}\n\n/* \u5806\u6392\u5e8f */\nfunc heapSort(nums *[]int) {\n // \u5efa\u5806\u64cd\u4f5c\uff1a\u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n for i := len(*nums)/2 - 1; i >= 0; i-- {\n siftDown(nums, len(*nums), i)\n }\n // \u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u5faa\u73af n-1 \u8f6e\n for i := len(*nums) - 1; i > 0; i-- {\n // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n (*nums)[0], (*nums)[i] = (*nums)[i], (*nums)[0]\n // \u4ee5\u6839\u8282\u70b9\u4e3a\u8d77\u70b9\uff0c\u4ece\u9876\u81f3\u5e95\u8fdb\u884c\u5806\u5316\n siftDown(nums, i, 0)\n }\n}\n
heap_sort.swift/* \u5806\u7684\u957f\u5ea6\u4e3a n \uff0c\u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nfunc siftDown(nums: inout [Int], n: Int, i: Int) {\n var i = i\n while true {\n // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n let l = 2 * i + 1\n let r = 2 * i + 2\n var ma = i\n if l < n, nums[l] > nums[ma] {\n ma = l\n }\n if r < n, nums[r] > nums[ma] {\n ma = r\n }\n // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n if ma == i {\n break\n }\n // \u4ea4\u6362\u4e24\u8282\u70b9\n nums.swapAt(i, ma)\n // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n i = ma\n }\n}\n\n/* \u5806\u6392\u5e8f */\nfunc heapSort(nums: inout [Int]) {\n // \u5efa\u5806\u64cd\u4f5c\uff1a\u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n for i in stride(from: nums.count / 2 - 1, through: 0, by: -1) {\n siftDown(nums: &nums, n: nums.count, i: i)\n }\n // \u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u5faa\u73af n-1 \u8f6e\n for i in stride(from: nums.count - 1, to: 0, by: -1) {\n // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n nums.swapAt(0, i)\n // \u4ee5\u6839\u8282\u70b9\u4e3a\u8d77\u70b9\uff0c\u4ece\u9876\u81f3\u5e95\u8fdb\u884c\u5806\u5316\n siftDown(nums: &nums, n: i, i: 0)\n }\n}\n
heap_sort.js/* \u5806\u7684\u957f\u5ea6\u4e3a n \uff0c\u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nfunction siftDown(nums, n, i) {\n while (true) {\n // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n let l = 2 * i + 1;\n let r = 2 * i + 2;\n let ma = i;\n if (l < n && nums[l] > nums[ma]) {\n ma = l;\n }\n if (r < n && nums[r] > nums[ma]) {\n ma = r;\n }\n // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n if (ma === i) {\n break;\n }\n // \u4ea4\u6362\u4e24\u8282\u70b9\n [nums[i], nums[ma]] = [nums[ma], nums[i]];\n // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n i = ma;\n }\n}\n\n/* \u5806\u6392\u5e8f */\nfunction heapSort(nums) {\n // \u5efa\u5806\u64cd\u4f5c\uff1a\u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n for (let i = Math.floor(nums.length / 2) - 1; i >= 0; i--) {\n siftDown(nums, nums.length, i);\n }\n // \u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u5faa\u73af n-1 \u8f6e\n for (let i = nums.length - 1; i > 0; i--) {\n // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n [nums[0], nums[i]] = [nums[i], nums[0]];\n // \u4ee5\u6839\u8282\u70b9\u4e3a\u8d77\u70b9\uff0c\u4ece\u9876\u81f3\u5e95\u8fdb\u884c\u5806\u5316\n siftDown(nums, i, 0);\n }\n}\n
heap_sort.ts/* \u5806\u7684\u957f\u5ea6\u4e3a n \uff0c\u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nfunction siftDown(nums: number[], n: number, i: number): void {\n while (true) {\n // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n let l = 2 * i + 1;\n let r = 2 * i + 2;\n let ma = i;\n if (l < n && nums[l] > nums[ma]) {\n ma = l;\n }\n if (r < n && nums[r] > nums[ma]) {\n ma = r;\n }\n // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n if (ma === i) {\n break;\n }\n // \u4ea4\u6362\u4e24\u8282\u70b9\n [nums[i], nums[ma]] = [nums[ma], nums[i]];\n // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n i = ma;\n }\n}\n\n/* \u5806\u6392\u5e8f */\nfunction heapSort(nums: number[]): void {\n // \u5efa\u5806\u64cd\u4f5c\uff1a\u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n for (let i = Math.floor(nums.length / 2) - 1; i >= 0; i--) {\n siftDown(nums, nums.length, i);\n }\n // \u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u5faa\u73af n-1 \u8f6e\n for (let i = nums.length - 1; i > 0; i--) {\n // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n [nums[0], nums[i]] = [nums[i], nums[0]];\n // \u4ee5\u6839\u8282\u70b9\u4e3a\u8d77\u70b9\uff0c\u4ece\u9876\u81f3\u5e95\u8fdb\u884c\u5806\u5316\n siftDown(nums, i, 0);\n }\n}\n
heap_sort.dart/* \u5806\u7684\u957f\u5ea6\u4e3a n \uff0c\u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nvoid siftDown(List<int> nums, int n, int i) {\n while (true) {\n // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n int l = 2 * i + 1;\n int r = 2 * i + 2;\n int ma = i;\n if (l < n && nums[l] > nums[ma]) ma = l;\n if (r < n && nums[r] > nums[ma]) ma = r;\n // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n if (ma == i) break;\n // \u4ea4\u6362\u4e24\u8282\u70b9\n int temp = nums[i];\n nums[i] = nums[ma];\n nums[ma] = temp;\n // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n i = ma;\n }\n}\n\n/* \u5806\u6392\u5e8f */\nvoid heapSort(List<int> nums) {\n // \u5efa\u5806\u64cd\u4f5c\uff1a\u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n for (int i = nums.length ~/ 2 - 1; i >= 0; i--) {\n siftDown(nums, nums.length, i);\n }\n // \u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u5faa\u73af n-1 \u8f6e\n for (int i = nums.length - 1; i > 0; i--) {\n // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n int tmp = nums[0];\n nums[0] = nums[i];\n nums[i] = tmp;\n // \u4ee5\u6839\u8282\u70b9\u4e3a\u8d77\u70b9\uff0c\u4ece\u9876\u81f3\u5e95\u8fdb\u884c\u5806\u5316\n siftDown(nums, i, 0);\n }\n}\n
heap_sort.rs/* \u5806\u7684\u957f\u5ea6\u4e3a n \uff0c\u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nfn sift_down(nums: &mut [i32], n: usize, mut i: usize) {\n loop {\n // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n let l = 2 * i + 1;\n let r = 2 * i + 2;\n let mut ma = i;\n if l < n && nums[l] > nums[ma] {\n ma = l;\n }\n if r < n && nums[r] > nums[ma] {\n ma = r;\n }\n // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n if ma == i {\n break;\n }\n // \u4ea4\u6362\u4e24\u8282\u70b9\n let temp = nums[i];\n nums[i] = nums[ma];\n nums[ma] = temp;\n // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n i = ma;\n }\n}\n\n/* \u5806\u6392\u5e8f */\nfn heap_sort(nums: &mut [i32]) {\n // \u5efa\u5806\u64cd\u4f5c\uff1a\u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n for i in (0..=nums.len() / 2 - 1).rev() {\n sift_down(nums, nums.len(), i);\n }\n // \u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u5faa\u73af n-1 \u8f6e\n for i in (1..=nums.len() - 1).rev() {\n // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n let tmp = nums[0];\n nums[0] = nums[i];\n nums[i] = tmp;\n // \u4ee5\u6839\u8282\u70b9\u4e3a\u8d77\u70b9\uff0c\u4ece\u9876\u81f3\u5e95\u8fdb\u884c\u5806\u5316\n sift_down(nums, i, 0);\n }\n}\n
heap_sort.c/* \u5806\u7684\u957f\u5ea6\u4e3a n \uff0c\u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nvoid siftDown(int nums[], int n, int i) {\n while (1) {\n // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n int l = 2 * i + 1;\n int r = 2 * i + 2;\n int ma = i;\n if (l < n && nums[l] > nums[ma])\n ma = l;\n if (r < n && nums[r] > nums[ma])\n ma = r;\n // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n if (ma == i) {\n break;\n }\n // \u4ea4\u6362\u4e24\u8282\u70b9\n int temp = nums[i];\n nums[i] = nums[ma];\n nums[ma] = temp;\n // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n i = ma;\n }\n}\n\n/* \u5806\u6392\u5e8f */\nvoid heapSort(int nums[], int n) {\n // \u5efa\u5806\u64cd\u4f5c\uff1a\u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n for (int i = n / 2 - 1; i >= 0; --i) {\n siftDown(nums, n, i);\n }\n // \u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u5faa\u73af n-1 \u8f6e\n for (int i = n - 1; i > 0; --i) {\n // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n int tmp = nums[0];\n nums[0] = nums[i];\n nums[i] = tmp;\n // \u4ee5\u6839\u8282\u70b9\u4e3a\u8d77\u70b9\uff0c\u4ece\u9876\u81f3\u5e95\u8fdb\u884c\u5806\u5316\n siftDown(nums, i, 0);\n }\n}\n
heap_sort.zig[class]{}-[func]{siftDown}\n\n[class]{}-[func]{heapSort}\n
"},{"location":"chapter_sorting/heap_sort/#1172","title":"11.7.2 \u00a0 \u7b97\u6cd5\u7279\u6027","text":"\u300c\u63d2\u5165\u6392\u5e8f insertion sort\u300d\u662f\u4e00\u79cd\u7b80\u5355\u7684\u6392\u5e8f\u7b97\u6cd5\uff0c\u5b83\u7684\u5de5\u4f5c\u539f\u7406\u4e0e\u624b\u52a8\u6574\u7406\u4e00\u526f\u724c\u7684\u8fc7\u7a0b\u975e\u5e38\u76f8\u4f3c\u3002
\u5177\u4f53\u6765\u8bf4\uff0c\u6211\u4eec\u5728\u672a\u6392\u5e8f\u533a\u95f4\u9009\u62e9\u4e00\u4e2a\u57fa\u51c6\u5143\u7d20\uff0c\u5c06\u8be5\u5143\u7d20\u4e0e\u5176\u5de6\u4fa7\u5df2\u6392\u5e8f\u533a\u95f4\u7684\u5143\u7d20\u9010\u4e00\u6bd4\u8f83\u5927\u5c0f\uff0c\u5e76\u5c06\u8be5\u5143\u7d20\u63d2\u5165\u5230\u6b63\u786e\u7684\u4f4d\u7f6e\u3002
\u56fe 11-6 \u5c55\u793a\u4e86\u6570\u7ec4\u63d2\u5165\u5143\u7d20\u7684\u64cd\u4f5c\u6d41\u7a0b\u3002\u8bbe\u57fa\u51c6\u5143\u7d20\u4e3a base
\uff0c\u6211\u4eec\u9700\u8981\u5c06\u4ece\u76ee\u6807\u7d22\u5f15\u5230 base
\u4e4b\u95f4\u7684\u6240\u6709\u5143\u7d20\u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\uff0c\u7136\u540e\u5c06 base
\u8d4b\u503c\u7ed9\u76ee\u6807\u7d22\u5f15\u3002
\u56fe 11-6 \u00a0 \u5355\u6b21\u63d2\u5165\u64cd\u4f5c
"},{"location":"chapter_sorting/insertion_sort/#1141","title":"11.4.1 \u00a0 \u7b97\u6cd5\u6d41\u7a0b","text":"\u63d2\u5165\u6392\u5e8f\u7684\u6574\u4f53\u6d41\u7a0b\u5982\u56fe 11-7 \u6240\u793a\u3002
base
\uff0c\u5c06\u5176\u63d2\u5165\u5230\u6b63\u786e\u4f4d\u7f6e\u540e\uff0c\u6570\u7ec4\u7684\u524d 2 \u4e2a\u5143\u7d20\u5df2\u6392\u5e8f\u3002base
\uff0c\u5c06\u5176\u63d2\u5165\u5230\u6b63\u786e\u4f4d\u7f6e\u540e\uff0c\u6570\u7ec4\u7684\u524d 3 \u4e2a\u5143\u7d20\u5df2\u6392\u5e8f\u3002base
\uff0c\u5c06\u5176\u63d2\u5165\u5230\u6b63\u786e\u4f4d\u7f6e\u540e\uff0c\u6240\u6709\u5143\u7d20\u5747\u5df2\u6392\u5e8f\u3002\u56fe 11-7 \u00a0 \u63d2\u5165\u6392\u5e8f\u6d41\u7a0b
\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig insertion_sort.pydef insertion_sort(nums: list[int]):\n \"\"\"\u63d2\u5165\u6392\u5e8f\"\"\"\n # \u5916\u5faa\u73af\uff1a\u5df2\u6392\u5e8f\u533a\u95f4\u4e3a [0, i-1]\n for i in range(1, len(nums)):\n base = nums[i]\n j = i - 1\n # \u5185\u5faa\u73af\uff1a\u5c06 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u533a\u95f4 [0, i-1] \u4e2d\u7684\u6b63\u786e\u4f4d\u7f6e\n while j >= 0 and nums[j] > base:\n nums[j + 1] = nums[j] # \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\n j -= 1\n nums[j + 1] = base # \u5c06 base \u8d4b\u503c\u5230\u6b63\u786e\u4f4d\u7f6e\n
insertion_sort.cpp/* \u63d2\u5165\u6392\u5e8f */\nvoid insertionSort(vector<int> &nums) {\n // \u5916\u5faa\u73af\uff1a\u5df2\u6392\u5e8f\u5143\u7d20\u6570\u91cf\u4e3a 1, 2, ..., n\n for (int i = 1; i < nums.size(); i++) {\n int base = nums[i], j = i - 1;\n // \u5185\u5faa\u73af\uff1a\u5c06 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u90e8\u5206\u7684\u6b63\u786e\u4f4d\u7f6e\n while (j >= 0 && nums[j] > base) {\n nums[j + 1] = nums[j]; // \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\n j--;\n }\n nums[j + 1] = base; // \u5c06 base \u8d4b\u503c\u5230\u6b63\u786e\u4f4d\u7f6e\n }\n}\n
insertion_sort.java/* \u63d2\u5165\u6392\u5e8f */\nvoid insertionSort(int[] nums) {\n // \u5916\u5faa\u73af\uff1a\u5df2\u6392\u5e8f\u5143\u7d20\u6570\u91cf\u4e3a 1, 2, ..., n\n for (int i = 1; i < nums.length; i++) {\n int base = nums[i], j = i - 1;\n // \u5185\u5faa\u73af\uff1a\u5c06 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u90e8\u5206\u7684\u6b63\u786e\u4f4d\u7f6e\n while (j >= 0 && nums[j] > base) {\n nums[j + 1] = nums[j]; // \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\n j--;\n }\n nums[j + 1] = base; // \u5c06 base \u8d4b\u503c\u5230\u6b63\u786e\u4f4d\u7f6e\n }\n}\n
insertion_sort.cs/* \u63d2\u5165\u6392\u5e8f */\nvoid InsertionSort(int[] nums) {\n // \u5916\u5faa\u73af\uff1a\u5df2\u6392\u5e8f\u5143\u7d20\u6570\u91cf\u4e3a 1, 2, ..., n\n for (int i = 1; i < nums.Length; i++) {\n int bas = nums[i], j = i - 1;\n // \u5185\u5faa\u73af\uff1a\u5c06 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u90e8\u5206\u7684\u6b63\u786e\u4f4d\u7f6e\n while (j >= 0 && nums[j] > bas) {\n nums[j + 1] = nums[j]; // \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\n j--;\n }\n nums[j + 1] = bas; // \u5c06 base \u8d4b\u503c\u5230\u6b63\u786e\u4f4d\u7f6e\n }\n}\n
insertion_sort.go/* \u63d2\u5165\u6392\u5e8f */\nfunc insertionSort(nums []int) {\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for i := 1; i < len(nums); i++ {\n base := nums[i]\n j := i - 1\n // \u5185\u5faa\u73af\uff1a\u5c06 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u90e8\u5206\u7684\u6b63\u786e\u4f4d\u7f6e\n for j >= 0 && nums[j] > base {\n nums[j+1] = nums[j] // \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\n j--\n }\n nums[j+1] = base // \u5c06 base \u8d4b\u503c\u5230\u6b63\u786e\u4f4d\u7f6e\n }\n}\n
insertion_sort.swift/* \u63d2\u5165\u6392\u5e8f */\nfunc insertionSort(nums: inout [Int]) {\n // \u5916\u5faa\u73af\uff1a\u5df2\u6392\u5e8f\u5143\u7d20\u6570\u91cf\u4e3a 1, 2, ..., n\n for i in stride(from: 1, to: nums.count, by: 1) {\n let base = nums[i]\n var j = i - 1\n // \u5185\u5faa\u73af\uff1a\u5c06 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u90e8\u5206\u7684\u6b63\u786e\u4f4d\u7f6e\n while j >= 0, nums[j] > base {\n nums[j + 1] = nums[j] // \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\n j -= 1\n }\n nums[j + 1] = base // \u5c06 base \u8d4b\u503c\u5230\u6b63\u786e\u4f4d\u7f6e\n }\n}\n
insertion_sort.js/* \u63d2\u5165\u6392\u5e8f */\nfunction insertionSort(nums) {\n // \u5916\u5faa\u73af\uff1a\u5df2\u6392\u5e8f\u5143\u7d20\u6570\u91cf\u4e3a 1, 2, ..., n\n for (let i = 1; i < nums.length; i++) {\n let base = nums[i],\n j = i - 1;\n // \u5185\u5faa\u73af\uff1a\u5c06 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u90e8\u5206\u7684\u6b63\u786e\u4f4d\u7f6e\n while (j >= 0 && nums[j] > base) {\n nums[j + 1] = nums[j]; // \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\n j--;\n }\n nums[j + 1] = base; // \u5c06 base \u8d4b\u503c\u5230\u6b63\u786e\u4f4d\u7f6e\n }\n}\n
insertion_sort.ts/* \u63d2\u5165\u6392\u5e8f */\nfunction insertionSort(nums: number[]): void {\n // \u5916\u5faa\u73af\uff1a\u5df2\u6392\u5e8f\u5143\u7d20\u6570\u91cf\u4e3a 1, 2, ..., n\n for (let i = 1; i < nums.length; i++) {\n const base = nums[i];\n let j = i - 1;\n // \u5185\u5faa\u73af\uff1a\u5c06 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u90e8\u5206\u7684\u6b63\u786e\u4f4d\u7f6e\n while (j >= 0 && nums[j] > base) {\n nums[j + 1] = nums[j]; // \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\n j--;\n }\n nums[j + 1] = base; // \u5c06 base \u8d4b\u503c\u5230\u6b63\u786e\u4f4d\u7f6e\n }\n}\n
insertion_sort.dart/* \u63d2\u5165\u6392\u5e8f */\nvoid insertionSort(List<int> nums) {\n // \u5916\u5faa\u73af\uff1a\u5df2\u6392\u5e8f\u5143\u7d20\u6570\u91cf\u4e3a 1, 2, ..., n\n for (int i = 1; i < nums.length; i++) {\n int base = nums[i], j = i - 1;\n // \u5185\u5faa\u73af\uff1a\u5c06 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u90e8\u5206\u7684\u6b63\u786e\u4f4d\u7f6e\n while (j >= 0 && nums[j] > base) {\n nums[j + 1] = nums[j]; // \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\n j--;\n }\n nums[j + 1] = base; // \u5c06 base \u8d4b\u503c\u5230\u6b63\u786e\u4f4d\u7f6e\n }\n}\n
insertion_sort.rs/* \u63d2\u5165\u6392\u5e8f */\nfn insertion_sort(nums: &mut [i32]) {\n // \u5916\u5faa\u73af\uff1a\u5df2\u6392\u5e8f\u5143\u7d20\u6570\u91cf\u4e3a 1, 2, ..., n\n for i in 1..nums.len() {\n let (base, mut j) = (nums[i], (i - 1) as i32);\n // \u5185\u5faa\u73af\uff1a\u5c06 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u90e8\u5206\u7684\u6b63\u786e\u4f4d\u7f6e\n while j >= 0 && nums[j as usize] > base {\n nums[(j + 1) as usize] = nums[j as usize]; // \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\n j -= 1;\n }\n nums[(j + 1) as usize] = base; // \u5c06 base \u8d4b\u503c\u5230\u6b63\u786e\u4f4d\u7f6e\n }\n}\n
insertion_sort.c/* \u63d2\u5165\u6392\u5e8f */\nvoid insertionSort(int nums[], int size) {\n // \u5916\u5faa\u73af\uff1a\u5df2\u6392\u5e8f\u5143\u7d20\u6570\u91cf\u4e3a 1, 2, ..., n\n for (int i = 1; i < size; i++) {\n int base = nums[i], j = i - 1;\n // \u5185\u5faa\u73af\uff1a\u5c06 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u90e8\u5206\u7684\u6b63\u786e\u4f4d\u7f6e\n while (j >= 0 && nums[j] > base) {\n // \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\n nums[j + 1] = nums[j];\n j--;\n }\n // \u5c06 base \u8d4b\u503c\u5230\u6b63\u786e\u4f4d\u7f6e\n nums[j + 1] = base;\n }\n}\n
insertion_sort.zig// \u63d2\u5165\u6392\u5e8f\nfn insertionSort(nums: []i32) void {\n // \u5916\u5faa\u73af\uff1a\u5df2\u6392\u5e8f\u5143\u7d20\u6570\u91cf\u4e3a 1, 2, ..., n\n var i: usize = 1;\n while (i < nums.len) : (i += 1) {\n var base = nums[i];\n var j: usize = i;\n // \u5185\u5faa\u73af\uff1a\u5c06 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u90e8\u5206\u7684\u6b63\u786e\u4f4d\u7f6e\n while (j >= 1 and nums[j - 1] > base) : (j -= 1) {\n nums[j] = nums[j - 1]; // \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\n }\n nums[j] = base; // \u5c06 base \u8d4b\u503c\u5230\u6b63\u786e\u4f4d\u7f6e\n }\n}\n
"},{"location":"chapter_sorting/insertion_sort/#1142","title":"11.4.2 \u00a0 \u7b97\u6cd5\u7279\u6027","text":"\u63d2\u5165\u6392\u5e8f\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\) \uff0c\u800c\u6211\u4eec\u5373\u5c06\u5b66\u4e60\u7684\u5feb\u901f\u6392\u5e8f\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n \\log n)\\) \u3002\u5c3d\u7ba1\u63d2\u5165\u6392\u5e8f\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u66f4\u9ad8\uff0c\u4f46\u5728\u6570\u636e\u91cf\u8f83\u5c0f\u7684\u60c5\u51b5\u4e0b\uff0c\u63d2\u5165\u6392\u5e8f\u901a\u5e38\u66f4\u5feb\u3002
\u8fd9\u4e2a\u7ed3\u8bba\u4e0e\u7ebf\u6027\u67e5\u627e\u548c\u4e8c\u5206\u67e5\u627e\u7684\u9002\u7528\u60c5\u51b5\u7684\u7ed3\u8bba\u7c7b\u4f3c\u3002\u5feb\u901f\u6392\u5e8f\u8fd9\u7c7b \\(O(n \\log n)\\) \u7684\u7b97\u6cd5\u5c5e\u4e8e\u57fa\u4e8e\u5206\u6cbb\u7b56\u7565\u7684\u6392\u5e8f\u7b97\u6cd5\uff0c\u5f80\u5f80\u5305\u542b\u66f4\u591a\u5355\u5143\u8ba1\u7b97\u64cd\u4f5c\u3002\u800c\u5728\u6570\u636e\u91cf\u8f83\u5c0f\u65f6\uff0c\\(n^2\\) \u548c \\(n \\log n\\) \u7684\u6570\u503c\u6bd4\u8f83\u63a5\u8fd1\uff0c\u590d\u6742\u5ea6\u4e0d\u5360\u4e3b\u5bfc\u5730\u4f4d\uff1b\u6bcf\u8f6e\u4e2d\u7684\u5355\u5143\u64cd\u4f5c\u6570\u91cf\u8d77\u5230\u51b3\u5b9a\u6027\u4f5c\u7528\u3002
\u5b9e\u9645\u4e0a\uff0c\u8bb8\u591a\u7f16\u7a0b\u8bed\u8a00\uff08\u4f8b\u5982 Java\uff09\u7684\u5185\u7f6e\u6392\u5e8f\u51fd\u6570\u91c7\u7528\u4e86\u63d2\u5165\u6392\u5e8f\uff0c\u5927\u81f4\u601d\u8def\u4e3a\uff1a\u5bf9\u4e8e\u957f\u6570\u7ec4\uff0c\u91c7\u7528\u57fa\u4e8e\u5206\u6cbb\u7b56\u7565\u7684\u6392\u5e8f\u7b97\u6cd5\uff0c\u4f8b\u5982\u5feb\u901f\u6392\u5e8f\uff1b\u5bf9\u4e8e\u77ed\u6570\u7ec4\uff0c\u76f4\u63a5\u4f7f\u7528\u63d2\u5165\u6392\u5e8f\u3002
\u867d\u7136\u5192\u6ce1\u6392\u5e8f\u3001\u9009\u62e9\u6392\u5e8f\u548c\u63d2\u5165\u6392\u5e8f\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u90fd\u4e3a \\(O(n^2)\\) \uff0c\u4f46\u5728\u5b9e\u9645\u60c5\u51b5\u4e2d\uff0c\u63d2\u5165\u6392\u5e8f\u7684\u4f7f\u7528\u9891\u7387\u663e\u8457\u9ad8\u4e8e\u5192\u6ce1\u6392\u5e8f\u548c\u9009\u62e9\u6392\u5e8f\uff0c\u4e3b\u8981\u6709\u4ee5\u4e0b\u539f\u56e0\u3002
\u300c\u5f52\u5e76\u6392\u5e8f merge sort\u300d\u662f\u4e00\u79cd\u57fa\u4e8e\u5206\u6cbb\u7b56\u7565\u7684\u6392\u5e8f\u7b97\u6cd5\uff0c\u5305\u542b\u56fe 11-10 \u6240\u793a\u7684\u201c\u5212\u5206\u201d\u548c\u201c\u5408\u5e76\u201d\u9636\u6bb5\u3002
\u56fe 11-10 \u00a0 \u5f52\u5e76\u6392\u5e8f\u7684\u5212\u5206\u4e0e\u5408\u5e76\u9636\u6bb5
"},{"location":"chapter_sorting/merge_sort/#1161","title":"11.6.1 \u00a0 \u7b97\u6cd5\u6d41\u7a0b","text":"\u5982\u56fe 11-11 \u6240\u793a\uff0c\u201c\u5212\u5206\u9636\u6bb5\u201d\u4ece\u9876\u81f3\u5e95\u9012\u5f52\u5730\u5c06\u6570\u7ec4\u4ece\u4e2d\u70b9\u5207\u5206\u4e3a\u4e24\u4e2a\u5b50\u6570\u7ec4\u3002
mid
\uff0c\u9012\u5f52\u5212\u5206\u5de6\u5b50\u6570\u7ec4\uff08\u533a\u95f4 [left, mid]
\uff09\u548c\u53f3\u5b50\u6570\u7ec4\uff08\u533a\u95f4 [mid + 1, right]
\uff09\u30021.
\uff0c\u76f4\u81f3\u5b50\u6570\u7ec4\u533a\u95f4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u3002\u201c\u5408\u5e76\u9636\u6bb5\u201d\u4ece\u5e95\u81f3\u9876\u5730\u5c06\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u5408\u5e76\u4e3a\u4e00\u4e2a\u6709\u5e8f\u6570\u7ec4\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u4ece\u957f\u5ea6\u4e3a 1 \u7684\u5b50\u6570\u7ec4\u5f00\u59cb\u5408\u5e76\uff0c\u5408\u5e76\u9636\u6bb5\u4e2d\u7684\u6bcf\u4e2a\u5b50\u6570\u7ec4\u90fd\u662f\u6709\u5e8f\u7684\u3002
<1><2><3><4><5><6><7><8><9><10>\u56fe 11-11 \u00a0 \u5f52\u5e76\u6392\u5e8f\u6b65\u9aa4
\u89c2\u5bdf\u53d1\u73b0\uff0c\u5f52\u5e76\u6392\u5e8f\u4e0e\u4e8c\u53c9\u6811\u540e\u5e8f\u904d\u5386\u7684\u9012\u5f52\u987a\u5e8f\u662f\u4e00\u81f4\u7684\u3002
\u5f52\u5e76\u6392\u5e8f\u7684\u5b9e\u73b0\u5982\u4ee5\u4e0b\u4ee3\u7801\u6240\u793a\u3002\u8bf7\u6ce8\u610f\uff0cnums
\u7684\u5f85\u5408\u5e76\u533a\u95f4\u4e3a [left, right]
\uff0c\u800c tmp
\u7684\u5bf9\u5e94\u533a\u95f4\u4e3a [0, right - left]
\u3002
def merge(nums: list[int], left: int, mid: int, right: int):\n \"\"\"\u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\"\"\"\n # \u5de6\u5b50\u6570\u7ec4\u533a\u95f4 [left, mid], \u53f3\u5b50\u6570\u7ec4\u533a\u95f4 [mid+1, right]\n # \u521b\u5efa\u4e00\u4e2a\u4e34\u65f6\u6570\u7ec4 tmp \uff0c\u7528\u4e8e\u5b58\u653e\u5408\u5e76\u540e\u7684\u7ed3\u679c\n tmp = [0] * (right - left + 1)\n # \u521d\u59cb\u5316\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\n i, j, k = left, mid + 1, 0\n # \u5f53\u5de6\u53f3\u5b50\u6570\u7ec4\u90fd\u8fd8\u6709\u5143\u7d20\u65f6\uff0c\u6bd4\u8f83\u5e76\u5c06\u8f83\u5c0f\u7684\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n while i <= mid and j <= right:\n if nums[i] <= nums[j]:\n tmp[k] = nums[i]\n i += 1\n else:\n tmp[k] = nums[j]\n j += 1\n k += 1\n # \u5c06\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u5269\u4f59\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n while i <= mid:\n tmp[k] = nums[i]\n i += 1\n k += 1\n while j <= right:\n tmp[k] = nums[j]\n j += 1\n k += 1\n # \u5c06\u4e34\u65f6\u6570\u7ec4 tmp \u4e2d\u7684\u5143\u7d20\u590d\u5236\u56de\u539f\u6570\u7ec4 nums \u7684\u5bf9\u5e94\u533a\u95f4\n for k in range(0, len(tmp)):\n nums[left + k] = tmp[k]\n\ndef merge_sort(nums: list[int], left: int, right: int):\n \"\"\"\u5f52\u5e76\u6392\u5e8f\"\"\"\n # \u7ec8\u6b62\u6761\u4ef6\n if left >= right:\n return # \u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n # \u5212\u5206\u9636\u6bb5\n mid = (left + right) // 2 # \u8ba1\u7b97\u4e2d\u70b9\n merge_sort(nums, left, mid) # \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\n merge_sort(nums, mid + 1, right) # \u9012\u5f52\u53f3\u5b50\u6570\u7ec4\n # \u5408\u5e76\u9636\u6bb5\n merge(nums, left, mid, right)\n
merge_sort.cpp/* \u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4 */\nvoid merge(vector<int> &nums, int left, int mid, int right) {\n // \u5de6\u5b50\u6570\u7ec4\u533a\u95f4 [left, mid], \u53f3\u5b50\u6570\u7ec4\u533a\u95f4 [mid+1, right]\n // \u521b\u5efa\u4e00\u4e2a\u4e34\u65f6\u6570\u7ec4 tmp \uff0c\u7528\u4e8e\u5b58\u653e\u5408\u5e76\u540e\u7684\u7ed3\u679c\n vector<int> tmp(right - left + 1);\n // \u521d\u59cb\u5316\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\n int i = left, j = mid + 1, k = 0;\n // \u5f53\u5de6\u53f3\u5b50\u6570\u7ec4\u90fd\u8fd8\u6709\u5143\u7d20\u65f6\uff0c\u6bd4\u8f83\u5e76\u5c06\u8f83\u5c0f\u7684\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n while (i <= mid && j <= right) {\n if (nums[i] <= nums[j])\n tmp[k++] = nums[i++];\n else\n tmp[k++] = nums[j++];\n }\n // \u5c06\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u5269\u4f59\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n while (i <= mid) {\n tmp[k++] = nums[i++];\n }\n while (j <= right) {\n tmp[k++] = nums[j++];\n }\n // \u5c06\u4e34\u65f6\u6570\u7ec4 tmp \u4e2d\u7684\u5143\u7d20\u590d\u5236\u56de\u539f\u6570\u7ec4 nums \u7684\u5bf9\u5e94\u533a\u95f4\n for (k = 0; k < tmp.size(); k++) {\n nums[left + k] = tmp[k];\n }\n}\n\n/* \u5f52\u5e76\u6392\u5e8f */\nvoid mergeSort(vector<int> &nums, int left, int right) {\n // \u7ec8\u6b62\u6761\u4ef6\n if (left >= right)\n return; // \u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n // \u5212\u5206\u9636\u6bb5\n int mid = (left + right) / 2; // \u8ba1\u7b97\u4e2d\u70b9\n mergeSort(nums, left, mid); // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\n mergeSort(nums, mid + 1, right); // \u9012\u5f52\u53f3\u5b50\u6570\u7ec4\n // \u5408\u5e76\u9636\u6bb5\n merge(nums, left, mid, right);\n}\n
merge_sort.java/* \u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4 */\nvoid merge(int[] nums, int left, int mid, int right) {\n // \u5de6\u5b50\u6570\u7ec4\u533a\u95f4 [left, mid], \u53f3\u5b50\u6570\u7ec4\u533a\u95f4 [mid+1, right]\n // \u521b\u5efa\u4e00\u4e2a\u4e34\u65f6\u6570\u7ec4 tmp \uff0c\u7528\u4e8e\u5b58\u653e\u5408\u5e76\u540e\u7684\u7ed3\u679c\n int[] tmp = new int[right - left + 1];\n // \u521d\u59cb\u5316\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\n int i = left, j = mid + 1, k = 0;\n // \u5f53\u5de6\u53f3\u5b50\u6570\u7ec4\u90fd\u8fd8\u6709\u5143\u7d20\u65f6\uff0c\u6bd4\u8f83\u5e76\u5c06\u8f83\u5c0f\u7684\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n while (i <= mid && j <= right) {\n if (nums[i] <= nums[j])\n tmp[k++] = nums[i++];\n else\n tmp[k++] = nums[j++];\n }\n // \u5c06\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u5269\u4f59\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n while (i <= mid) {\n tmp[k++] = nums[i++];\n }\n while (j <= right) {\n tmp[k++] = nums[j++];\n }\n // \u5c06\u4e34\u65f6\u6570\u7ec4 tmp \u4e2d\u7684\u5143\u7d20\u590d\u5236\u56de\u539f\u6570\u7ec4 nums \u7684\u5bf9\u5e94\u533a\u95f4\n for (k = 0; k < tmp.length; k++) {\n nums[left + k] = tmp[k];\n }\n}\n\n/* \u5f52\u5e76\u6392\u5e8f */\nvoid mergeSort(int[] nums, int left, int right) {\n // \u7ec8\u6b62\u6761\u4ef6\n if (left >= right)\n return; // \u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n // \u5212\u5206\u9636\u6bb5\n int mid = (left + right) / 2; // \u8ba1\u7b97\u4e2d\u70b9\n mergeSort(nums, left, mid); // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\n mergeSort(nums, mid + 1, right); // \u9012\u5f52\u53f3\u5b50\u6570\u7ec4\n // \u5408\u5e76\u9636\u6bb5\n merge(nums, left, mid, right);\n}\n
merge_sort.cs/* \u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4 */\nvoid Merge(int[] nums, int left, int mid, int right) {\n // \u5de6\u5b50\u6570\u7ec4\u533a\u95f4 [left, mid], \u53f3\u5b50\u6570\u7ec4\u533a\u95f4 [mid+1, right]\n // \u521b\u5efa\u4e00\u4e2a\u4e34\u65f6\u6570\u7ec4 tmp \uff0c\u7528\u4e8e\u5b58\u653e\u5408\u5e76\u540e\u7684\u7ed3\u679c\n int[] tmp = new int[right - left + 1];\n // \u521d\u59cb\u5316\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\n int i = left, j = mid + 1, k = 0;\n // \u5f53\u5de6\u53f3\u5b50\u6570\u7ec4\u90fd\u8fd8\u6709\u5143\u7d20\u65f6\uff0c\u6bd4\u8f83\u5e76\u5c06\u8f83\u5c0f\u7684\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n while (i <= mid && j <= right) {\n if (nums[i] <= nums[j])\n tmp[k++] = nums[i++];\n else\n tmp[k++] = nums[j++];\n }\n // \u5c06\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u5269\u4f59\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n while (i <= mid) {\n tmp[k++] = nums[i++];\n }\n while (j <= right) {\n tmp[k++] = nums[j++];\n }\n // \u5c06\u4e34\u65f6\u6570\u7ec4 tmp \u4e2d\u7684\u5143\u7d20\u590d\u5236\u56de\u539f\u6570\u7ec4 nums \u7684\u5bf9\u5e94\u533a\u95f4\n for (k = 0; k < tmp.Length; ++k) {\n nums[left + k] = tmp[k];\n }\n}\n\n/* \u5f52\u5e76\u6392\u5e8f */\nvoid MergeSort(int[] nums, int left, int right) {\n // \u7ec8\u6b62\u6761\u4ef6\n if (left >= right) return; // \u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n // \u5212\u5206\u9636\u6bb5\n int mid = (left + right) / 2; // \u8ba1\u7b97\u4e2d\u70b9\n MergeSort(nums, left, mid); // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\n MergeSort(nums, mid + 1, right); // \u9012\u5f52\u53f3\u5b50\u6570\u7ec4\n // \u5408\u5e76\u9636\u6bb5\n Merge(nums, left, mid, right);\n}\n
merge_sort.go/* \u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4 */\nfunc merge(nums []int, left, mid, right int) {\n // \u5de6\u5b50\u6570\u7ec4\u533a\u95f4 [left, mid], \u53f3\u5b50\u6570\u7ec4\u533a\u95f4 [mid+1, right]\n // \u521b\u5efa\u4e00\u4e2a\u4e34\u65f6\u6570\u7ec4 tmp \uff0c\u7528\u4e8e\u5b58\u653e\u5408\u5e76\u540e\u7684\u7ed3\u679c\n tmp := make([]int, right-left+1)\n // \u521d\u59cb\u5316\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\n i, j, k := left, mid+1, 0\n // \u5f53\u5de6\u53f3\u5b50\u6570\u7ec4\u90fd\u8fd8\u6709\u5143\u7d20\u65f6\uff0c\u6bd4\u8f83\u5e76\u5c06\u8f83\u5c0f\u7684\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n for i <= mid && j <= right {\n if nums[i] <= nums[j] {\n tmp[k] = nums[i]\n i++\n } else {\n tmp[k] = nums[j]\n j++\n }\n k++\n }\n // \u5c06\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u5269\u4f59\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n for i <= mid {\n tmp[k] = nums[i]\n i++\n k++\n }\n for j <= right {\n tmp[k] = nums[j]\n j++\n k++\n }\n // \u5c06\u4e34\u65f6\u6570\u7ec4 tmp \u4e2d\u7684\u5143\u7d20\u590d\u5236\u56de\u539f\u6570\u7ec4 nums \u7684\u5bf9\u5e94\u533a\u95f4\n for k := 0; k < len(tmp); k++ {\n nums[left+k] = tmp[k]\n }\n}\n\n/* \u5f52\u5e76\u6392\u5e8f */\nfunc mergeSort(nums []int, left, right int) {\n // \u7ec8\u6b62\u6761\u4ef6\n if left >= right {\n return\n }\n // \u5212\u5206\u9636\u6bb5\n mid := (left + right) / 2\n mergeSort(nums, left, mid)\n mergeSort(nums, mid+1, right)\n // \u5408\u5e76\u9636\u6bb5\n merge(nums, left, mid, right)\n}\n
merge_sort.swift/* \u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4 */\nfunc merge(nums: inout [Int], left: Int, mid: Int, right: Int) {\n // \u5de6\u5b50\u6570\u7ec4\u533a\u95f4 [left, mid], \u53f3\u5b50\u6570\u7ec4\u533a\u95f4 [mid+1, right]\n // \u521b\u5efa\u4e00\u4e2a\u4e34\u65f6\u6570\u7ec4 tmp \uff0c\u7528\u4e8e\u5b58\u653e\u5408\u5e76\u540e\u7684\u7ed3\u679c\n var tmp = Array(repeating: 0, count: right - left + 1)\n // \u521d\u59cb\u5316\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\n var i = left, j = mid + 1, k = 0\n // \u5f53\u5de6\u53f3\u5b50\u6570\u7ec4\u90fd\u8fd8\u6709\u5143\u7d20\u65f6\uff0c\u6bd4\u8f83\u5e76\u5c06\u8f83\u5c0f\u7684\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n while i <= mid, j <= right {\n if nums[i] <= nums[j] {\n tmp[k] = nums[i]\n i += 1\n k += 1\n } else {\n tmp[k] = nums[j]\n j += 1\n k += 1\n }\n }\n // \u5c06\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u5269\u4f59\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n while i <= mid {\n tmp[k] = nums[i]\n i += 1\n k += 1\n }\n while j <= right {\n tmp[k] = nums[j]\n j += 1\n k += 1\n }\n // \u5c06\u4e34\u65f6\u6570\u7ec4 tmp \u4e2d\u7684\u5143\u7d20\u590d\u5236\u56de\u539f\u6570\u7ec4 nums \u7684\u5bf9\u5e94\u533a\u95f4\n for k in tmp.indices {\n nums[left + k] = tmp[k]\n }\n}\n\n/* \u5f52\u5e76\u6392\u5e8f */\nfunc mergeSort(nums: inout [Int], left: Int, right: Int) {\n // \u7ec8\u6b62\u6761\u4ef6\n if left >= right { // \u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n return\n }\n // \u5212\u5206\u9636\u6bb5\n let mid = (left + right) / 2 // \u8ba1\u7b97\u4e2d\u70b9\n mergeSort(nums: &nums, left: left, right: mid) // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\n mergeSort(nums: &nums, left: mid + 1, right: right) // \u9012\u5f52\u53f3\u5b50\u6570\u7ec4\n // \u5408\u5e76\u9636\u6bb5\n merge(nums: &nums, left: left, mid: mid, right: right)\n}\n
merge_sort.js/* \u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4 */\nfunction merge(nums, left, mid, right) {\n // \u5de6\u5b50\u6570\u7ec4\u533a\u95f4 [left, mid], \u53f3\u5b50\u6570\u7ec4\u533a\u95f4 [mid+1, right]\n // \u521b\u5efa\u4e00\u4e2a\u4e34\u65f6\u6570\u7ec4 tmp \uff0c\u7528\u4e8e\u5b58\u653e\u5408\u5e76\u540e\u7684\u7ed3\u679c\n const tmp = new Array(right - left + 1);\n // \u521d\u59cb\u5316\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\n let i = left,\n j = mid + 1,\n k = 0;\n // \u5f53\u5de6\u53f3\u5b50\u6570\u7ec4\u90fd\u8fd8\u6709\u5143\u7d20\u65f6\uff0c\u6bd4\u8f83\u5e76\u5c06\u8f83\u5c0f\u7684\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n while (i <= mid && j <= right) {\n if (nums[i] <= nums[j]) {\n tmp[k++] = nums[i++];\n } else {\n tmp[k++] = nums[j++];\n }\n }\n // \u5c06\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u5269\u4f59\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n while (i <= mid) {\n tmp[k++] = nums[i++];\n }\n while (j <= right) {\n tmp[k++] = nums[j++];\n }\n // \u5c06\u4e34\u65f6\u6570\u7ec4 tmp \u4e2d\u7684\u5143\u7d20\u590d\u5236\u56de\u539f\u6570\u7ec4 nums \u7684\u5bf9\u5e94\u533a\u95f4\n for (k = 0; k < tmp.length; k++) {\n nums[left + k] = tmp[k];\n }\n}\n\n/* \u5f52\u5e76\u6392\u5e8f */\nfunction mergeSort(nums, left, right) {\n // \u7ec8\u6b62\u6761\u4ef6\n if (left >= right) return; // \u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n // \u5212\u5206\u9636\u6bb5\n let mid = Math.floor((left + right) / 2); // \u8ba1\u7b97\u4e2d\u70b9\n mergeSort(nums, left, mid); // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\n mergeSort(nums, mid + 1, right); // \u9012\u5f52\u53f3\u5b50\u6570\u7ec4\n // \u5408\u5e76\u9636\u6bb5\n merge(nums, left, mid, right);\n}\n
merge_sort.ts/* \u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4 */\nfunction merge(nums: number[], left: number, mid: number, right: number): void {\n // \u5de6\u5b50\u6570\u7ec4\u533a\u95f4 [left, mid], \u53f3\u5b50\u6570\u7ec4\u533a\u95f4 [mid+1, right]\n // \u521b\u5efa\u4e00\u4e2a\u4e34\u65f6\u6570\u7ec4 tmp \uff0c\u7528\u4e8e\u5b58\u653e\u5408\u5e76\u540e\u7684\u7ed3\u679c\n const tmp = new Array(right - left + 1);\n // \u521d\u59cb\u5316\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\n let i = left,\n j = mid + 1,\n k = 0;\n // \u5f53\u5de6\u53f3\u5b50\u6570\u7ec4\u90fd\u8fd8\u6709\u5143\u7d20\u65f6\uff0c\u6bd4\u8f83\u5e76\u5c06\u8f83\u5c0f\u7684\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n while (i <= mid && j <= right) {\n if (nums[i] <= nums[j]) {\n tmp[k++] = nums[i++];\n } else {\n tmp[k++] = nums[j++];\n }\n }\n // \u5c06\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u5269\u4f59\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n while (i <= mid) {\n tmp[k++] = nums[i++];\n }\n while (j <= right) {\n tmp[k++] = nums[j++];\n }\n // \u5c06\u4e34\u65f6\u6570\u7ec4 tmp \u4e2d\u7684\u5143\u7d20\u590d\u5236\u56de\u539f\u6570\u7ec4 nums \u7684\u5bf9\u5e94\u533a\u95f4\n for (k = 0; k < tmp.length; k++) {\n nums[left + k] = tmp[k];\n }\n}\n\n/* \u5f52\u5e76\u6392\u5e8f */\nfunction mergeSort(nums: number[], left: number, right: number): void {\n // \u7ec8\u6b62\u6761\u4ef6\n if (left >= right) return; // \u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n // \u5212\u5206\u9636\u6bb5\n let mid = Math.floor((left + right) / 2); // \u8ba1\u7b97\u4e2d\u70b9\n mergeSort(nums, left, mid); // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\n mergeSort(nums, mid + 1, right); // \u9012\u5f52\u53f3\u5b50\u6570\u7ec4\n // \u5408\u5e76\u9636\u6bb5\n merge(nums, left, mid, right);\n}\n
merge_sort.dart/* \u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4 */\nvoid merge(List<int> nums, int left, int mid, int right) {\n // \u5de6\u5b50\u6570\u7ec4\u533a\u95f4 [left, mid], \u53f3\u5b50\u6570\u7ec4\u533a\u95f4 [mid+1, right]\n // \u521b\u5efa\u4e00\u4e2a\u4e34\u65f6\u6570\u7ec4 tmp \uff0c\u7528\u4e8e\u5b58\u653e\u5408\u5e76\u540e\u7684\u7ed3\u679c\n List<int> tmp = List.filled(right - left + 1, 0);\n // \u521d\u59cb\u5316\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\n int i = left, j = mid + 1, k = 0;\n // \u5f53\u5de6\u53f3\u5b50\u6570\u7ec4\u90fd\u8fd8\u6709\u5143\u7d20\u65f6\uff0c\u6bd4\u8f83\u5e76\u5c06\u8f83\u5c0f\u7684\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n while (i <= mid && j <= right) {\n if (nums[i] <= nums[j])\n tmp[k++] = nums[i++];\n else\n tmp[k++] = nums[j++];\n }\n // \u5c06\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u5269\u4f59\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n while (i <= mid) {\n tmp[k++] = nums[i++];\n }\n while (j <= right) {\n tmp[k++] = nums[j++];\n }\n // \u5c06\u4e34\u65f6\u6570\u7ec4 tmp \u4e2d\u7684\u5143\u7d20\u590d\u5236\u56de\u539f\u6570\u7ec4 nums \u7684\u5bf9\u5e94\u533a\u95f4\n for (k = 0; k < tmp.length; k++) {\n nums[left + k] = tmp[k];\n }\n}\n\n/* \u5f52\u5e76\u6392\u5e8f */\nvoid mergeSort(List<int> nums, int left, int right) {\n // \u7ec8\u6b62\u6761\u4ef6\n if (left >= right) return; // \u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n // \u5212\u5206\u9636\u6bb5\n int mid = (left + right) ~/ 2; // \u8ba1\u7b97\u4e2d\u70b9\n mergeSort(nums, left, mid); // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\n mergeSort(nums, mid + 1, right); // \u9012\u5f52\u53f3\u5b50\u6570\u7ec4\n // \u5408\u5e76\u9636\u6bb5\n merge(nums, left, mid, right);\n}\n
merge_sort.rs/* \u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4 */\nfn merge(nums: &mut [i32], left: usize, mid: usize, right: usize) {\n // \u5de6\u5b50\u6570\u7ec4\u533a\u95f4 [left, mid], \u53f3\u5b50\u6570\u7ec4\u533a\u95f4 [mid+1, right]\n // \u521b\u5efa\u4e00\u4e2a\u4e34\u65f6\u6570\u7ec4 tmp \uff0c\u7528\u4e8e\u5b58\u653e\u5408\u5e76\u540e\u7684\u7ed3\u679c\n let tmp_size = right - left + 1;\n let mut tmp = vec![0; tmp_size];\n // \u521d\u59cb\u5316\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\n let (mut i, mut j, mut k) = (left, mid + 1, 0);\n // \u5f53\u5de6\u53f3\u5b50\u6570\u7ec4\u90fd\u8fd8\u6709\u5143\u7d20\u65f6\uff0c\u6bd4\u8f83\u5e76\u5c06\u8f83\u5c0f\u7684\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n while i <= mid && j <= right {\n if nums[i] <= nums[j] {\n tmp[k] = nums[j];\n i += 1;\n } else {\n tmp[k] = nums[j];\n j += 1;\n }\n k += 1;\n }\n // \u5c06\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u5269\u4f59\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n while i <= mid {\n tmp[k] = nums[i];\n k += 1;\n i += 1;\n }\n while j <= right {\n tmp[k] = nums[j];\n k += 1;\n j += 1;\n }\n // \u5c06\u4e34\u65f6\u6570\u7ec4 tmp \u4e2d\u7684\u5143\u7d20\u590d\u5236\u56de\u539f\u6570\u7ec4 nums \u7684\u5bf9\u5e94\u533a\u95f4\n for k in 0..tmp_size {\n nums[left + k] = tmp[k];\n }\n}\n\n/* \u5f52\u5e76\u6392\u5e8f */\nfn merge_sort(nums: &mut [i32], left: usize, right: usize) {\n // \u7ec8\u6b62\u6761\u4ef6\n if left >= right { return; } // \u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n // \u5212\u5206\u9636\u6bb5\n let mid = (left + right) / 2; // \u8ba1\u7b97\u4e2d\u70b9\n merge_sort(nums, left, mid); // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\n merge_sort(nums, mid + 1, right); // \u9012\u5f52\u53f3\u5b50\u6570\u7ec4\n // \u5408\u5e76\u9636\u6bb5\n merge(nums, left, mid, right);\n}\n
merge_sort.c/* \u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4 */\nvoid merge(int *nums, int left, int mid, int right) {\n // \u5de6\u5b50\u6570\u7ec4\u533a\u95f4 [left, mid], \u53f3\u5b50\u6570\u7ec4\u533a\u95f4 [mid+1, right]\n // \u521b\u5efa\u4e00\u4e2a\u4e34\u65f6\u6570\u7ec4 tmp \uff0c\u7528\u4e8e\u5b58\u653e\u5408\u5e76\u540e\u7684\u7ed3\u679c\n int tmpSize = right - left + 1;\n int *tmp = (int *)malloc(tmpSize * sizeof(int));\n // \u521d\u59cb\u5316\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\n int i = left, j = mid + 1, k = 0;\n // \u5f53\u5de6\u53f3\u5b50\u6570\u7ec4\u90fd\u8fd8\u6709\u5143\u7d20\u65f6\uff0c\u6bd4\u8f83\u5e76\u5c06\u8f83\u5c0f\u7684\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n while (i <= mid && j <= right) {\n if (nums[i] <= nums[j]) {\n tmp[k++] = nums[i++];\n } else {\n tmp[k++] = nums[j++];\n }\n }\n // \u5c06\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u7684\u5269\u4f59\u5143\u7d20\u590d\u5236\u5230\u4e34\u65f6\u6570\u7ec4\u4e2d\n while (i <= mid) {\n tmp[k++] = nums[i++];\n }\n while (j <= right) {\n tmp[k++] = nums[j++];\n }\n // \u5c06\u4e34\u65f6\u6570\u7ec4 tmp \u4e2d\u7684\u5143\u7d20\u590d\u5236\u56de\u539f\u6570\u7ec4 nums \u7684\u5bf9\u5e94\u533a\u95f4\n for (k = 0; k < tmpSize; ++k) {\n nums[left + k] = tmp[k];\n }\n // \u91ca\u653e\u5185\u5b58\n free(tmp);\n}\n\n/* \u5f52\u5e76\u6392\u5e8f */\nvoid mergeSort(int *nums, int left, int right) {\n // \u7ec8\u6b62\u6761\u4ef6\n if (left >= right)\n return; // \u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n // \u5212\u5206\u9636\u6bb5\n int mid = (left + right) / 2; // \u8ba1\u7b97\u4e2d\u70b9\n mergeSort(nums, left, mid); // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\n mergeSort(nums, mid + 1, right); // \u9012\u5f52\u53f3\u5b50\u6570\u7ec4\n // \u5408\u5e76\u9636\u6bb5\n merge(nums, left, mid, right);\n}\n
merge_sort.zig// \u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\n// \u5de6\u5b50\u6570\u7ec4\u533a\u95f4 [left, mid]\n// \u53f3\u5b50\u6570\u7ec4\u533a\u95f4 [mid + 1, right]\nfn merge(nums: []i32, left: usize, mid: usize, right: usize) !void {\n // \u521d\u59cb\u5316\u8f85\u52a9\u6570\u7ec4\n var mem_arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);\n defer mem_arena.deinit();\n const mem_allocator = mem_arena.allocator();\n var tmp = try mem_allocator.alloc(i32, right + 1 - left);\n std.mem.copy(i32, tmp, nums[left..right+1]);\n // \u5de6\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15 \n var leftStart = left - left;\n var leftEnd = mid - left;\n // \u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15 \n var rightStart = mid + 1 - left;\n var rightEnd = right - left;\n // i, j \u5206\u522b\u6307\u5411\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\u7684\u9996\u5143\u7d20\n var i = leftStart;\n var j = rightStart;\n // \u901a\u8fc7\u8986\u76d6\u539f\u6570\u7ec4 nums \u6765\u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\n var k = left;\n while (k <= right) : (k += 1) {\n // \u82e5\u201c\u5de6\u5b50\u6570\u7ec4\u5df2\u5168\u90e8\u5408\u5e76\u5b8c\u201d\uff0c\u5219\u9009\u53d6\u53f3\u5b50\u6570\u7ec4\u5143\u7d20\uff0c\u5e76\u4e14 j++\n if (i > leftEnd) {\n nums[k] = tmp[j];\n j += 1;\n // \u5426\u5219\uff0c\u82e5\u201c\u53f3\u5b50\u6570\u7ec4\u5df2\u5168\u90e8\u5408\u5e76\u5b8c\u201d\u6216\u201c\u5de6\u5b50\u6570\u7ec4\u5143\u7d20 <= \u53f3\u5b50\u6570\u7ec4\u5143\u7d20\u201d\uff0c\u5219\u9009\u53d6\u5de6\u5b50\u6570\u7ec4\u5143\u7d20\uff0c\u5e76\u4e14 i++\n } else if (j > rightEnd or tmp[i] <= tmp[j]) {\n nums[k] = tmp[i];\n i += 1;\n // \u5426\u5219\uff0c\u82e5\u201c\u5de6\u53f3\u5b50\u6570\u7ec4\u90fd\u672a\u5168\u90e8\u5408\u5e76\u5b8c\u201d\u4e14\u201c\u5de6\u5b50\u6570\u7ec4\u5143\u7d20 > \u53f3\u5b50\u6570\u7ec4\u5143\u7d20\u201d\uff0c\u5219\u9009\u53d6\u53f3\u5b50\u6570\u7ec4\u5143\u7d20\uff0c\u5e76\u4e14 j++\n } else {\n nums[k] = tmp[j];\n j += 1;\n }\n }\n}\n\n// \u5f52\u5e76\u6392\u5e8f\nfn mergeSort(nums: []i32, left: usize, right: usize) !void {\n // \u7ec8\u6b62\u6761\u4ef6\n if (left >= right) return; // \u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n // \u5212\u5206\u9636\u6bb5\n var mid = (left + right) / 2; // \u8ba1\u7b97\u4e2d\u70b9\n try mergeSort(nums, left, mid); // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\n try mergeSort(nums, mid + 1, right); // \u9012\u5f52\u53f3\u5b50\u6570\u7ec4\n // \u5408\u5e76\u9636\u6bb5\n try merge(nums, left, mid, right);\n}\n
"},{"location":"chapter_sorting/merge_sort/#1162","title":"11.6.2 \u00a0 \u7b97\u6cd5\u7279\u6027","text":"\u5bf9\u4e8e\u94fe\u8868\uff0c\u5f52\u5e76\u6392\u5e8f\u76f8\u8f83\u4e8e\u5176\u4ed6\u6392\u5e8f\u7b97\u6cd5\u5177\u6709\u663e\u8457\u4f18\u52bf\uff0c\u53ef\u4ee5\u5c06\u94fe\u8868\u6392\u5e8f\u4efb\u52a1\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u4f18\u5316\u81f3 \\(O(1)\\) \u3002
\u5177\u4f53\u5b9e\u73b0\u7ec6\u8282\u6bd4\u8f83\u590d\u6742\uff0c\u6709\u5174\u8da3\u7684\u8bfb\u8005\u53ef\u4ee5\u67e5\u9605\u76f8\u5173\u8d44\u6599\u8fdb\u884c\u5b66\u4e60\u3002
"},{"location":"chapter_sorting/quick_sort/","title":"11.5 \u00a0 \u5feb\u901f\u6392\u5e8f","text":"\u300c\u5feb\u901f\u6392\u5e8f quick sort\u300d\u662f\u4e00\u79cd\u57fa\u4e8e\u5206\u6cbb\u7b56\u7565\u7684\u6392\u5e8f\u7b97\u6cd5\uff0c\u8fd0\u884c\u9ad8\u6548\uff0c\u5e94\u7528\u5e7f\u6cdb\u3002
\u5feb\u901f\u6392\u5e8f\u7684\u6838\u5fc3\u64cd\u4f5c\u662f\u201c\u54e8\u5175\u5212\u5206\u201d\uff0c\u5176\u76ee\u6807\u662f\uff1a\u9009\u62e9\u6570\u7ec4\u4e2d\u7684\u67d0\u4e2a\u5143\u7d20\u4f5c\u4e3a\u201c\u57fa\u51c6\u6570\u201d\uff0c\u5c06\u6240\u6709\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\u79fb\u5230\u5176\u5de6\u4fa7\uff0c\u800c\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\u79fb\u5230\u5176\u53f3\u4fa7\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u54e8\u5175\u5212\u5206\u7684\u6d41\u7a0b\u5982\u56fe 11-8 \u6240\u793a\u3002
i
\u548c j
\u5206\u522b\u6307\u5411\u6570\u7ec4\u7684\u4e24\u7aef\u3002i
\uff08j
\uff09\u5206\u522b\u5bfb\u627e\u7b2c\u4e00\u4e2a\u6bd4\u57fa\u51c6\u6570\u5927\uff08\u5c0f\uff09\u7684\u5143\u7d20\uff0c\u7136\u540e\u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\u30022.
\uff0c\u76f4\u5230 i
\u548c j
\u76f8\u9047\u65f6\u505c\u6b62\uff0c\u6700\u540e\u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u4e2a\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\u3002\u56fe 11-8 \u00a0 \u54e8\u5175\u5212\u5206\u6b65\u9aa4
\u54e8\u5175\u5212\u5206\u5b8c\u6210\u540e\uff0c\u539f\u6570\u7ec4\u88ab\u5212\u5206\u6210\u4e09\u90e8\u5206\uff1a\u5de6\u5b50\u6570\u7ec4\u3001\u57fa\u51c6\u6570\u3001\u53f3\u5b50\u6570\u7ec4\uff0c\u4e14\u6ee1\u8db3\u201c\u5de6\u5b50\u6570\u7ec4\u4efb\u610f\u5143\u7d20 \\(\\leq\\) \u57fa\u51c6\u6570 \\(\\leq\\) \u53f3\u5b50\u6570\u7ec4\u4efb\u610f\u5143\u7d20\u201d\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u63a5\u4e0b\u6765\u53ea\u9700\u5bf9\u8fd9\u4e24\u4e2a\u5b50\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u3002
\u5feb\u901f\u6392\u5e8f\u7684\u5206\u6cbb\u7b56\u7565
\u54e8\u5175\u5212\u5206\u7684\u5b9e\u8d28\u662f\u5c06\u4e00\u4e2a\u8f83\u957f\u6570\u7ec4\u7684\u6392\u5e8f\u95ee\u9898\u7b80\u5316\u4e3a\u4e24\u4e2a\u8f83\u77ed\u6570\u7ec4\u7684\u6392\u5e8f\u95ee\u9898\u3002
PythonC++JavaC#GoSwiftJSTSDartRustCZig quick_sort.pydef partition(self, nums: list[int], left: int, right: int) -> int:\n \"\"\"\u54e8\u5175\u5212\u5206\"\"\"\n # \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n i, j = left, right\n while i < j:\n while i < j and nums[j] >= nums[left]:\n j -= 1 # \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n while i < j and nums[i] <= nums[left]:\n i += 1 # \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n # \u5143\u7d20\u4ea4\u6362\n nums[i], nums[j] = nums[j], nums[i]\n # \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n nums[i], nums[left] = nums[left], nums[i]\n return i # \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n
quick_sort.cpp/* \u5143\u7d20\u4ea4\u6362 */\nvoid swap(vector<int> &nums, int i, int j) {\n int tmp = nums[i];\n nums[i] = nums[j];\n nums[j] = tmp;\n}\n\n/* \u54e8\u5175\u5212\u5206 */\nint partition(vector<int> &nums, int left, int right) {\n // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n int i = left, j = right;\n while (i < j) {\n while (i < j && nums[j] >= nums[left])\n j--; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n while (i < j && nums[i] <= nums[left])\n i++; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n swap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n }\n swap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n return i; // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
quick_sort.java/* \u5143\u7d20\u4ea4\u6362 */\nvoid swap(int[] nums, int i, int j) {\n int tmp = nums[i];\n nums[i] = nums[j];\n nums[j] = tmp;\n}\n\n/* \u54e8\u5175\u5212\u5206 */\nint partition(int[] nums, int left, int right) {\n // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n int i = left, j = right;\n while (i < j) {\n while (i < j && nums[j] >= nums[left])\n j--; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n while (i < j && nums[i] <= nums[left])\n i++; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n swap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n }\n swap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n return i; // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
quick_sort.cs/* \u5143\u7d20\u4ea4\u6362 */\nvoid Swap(int[] nums, int i, int j) {\n (nums[j], nums[i]) = (nums[i], nums[j]);\n}\n\n/* \u54e8\u5175\u5212\u5206 */\nint Partition(int[] nums, int left, int right) {\n // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n int i = left, j = right;\n while (i < j) {\n while (i < j && nums[j] >= nums[left])\n j--; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n while (i < j && nums[i] <= nums[left])\n i++; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n Swap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n }\n Swap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n return i; // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
quick_sort.go/* \u54e8\u5175\u5212\u5206 */\nfunc (q *quickSort) partition(nums []int, left, right int) int {\n // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n i, j := left, right\n for i < j {\n for i < j && nums[j] >= nums[left] {\n j-- // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n }\n for i < j && nums[i] <= nums[left] {\n i++ // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n }\n // \u5143\u7d20\u4ea4\u6362\n nums[i], nums[j] = nums[j], nums[i]\n }\n // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n nums[i], nums[left] = nums[left], nums[i]\n return i // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
quick_sort.swift/* \u5143\u7d20\u4ea4\u6362 */\nfunc swap(nums: inout [Int], i: Int, j: Int) {\n let tmp = nums[i]\n nums[i] = nums[j]\n nums[j] = tmp\n}\n\n/* \u54e8\u5175\u5212\u5206 */\nfunc partition(nums: inout [Int], left: Int, right: Int) -> Int {\n // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n var i = left\n var j = right\n while i < j {\n while i < j, nums[j] >= nums[left] {\n j -= 1 // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n }\n while i < j, nums[i] <= nums[left] {\n i += 1 // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n }\n swap(nums: &nums, i: i, j: j) // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n }\n swap(nums: &nums, i: i, j: left) // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n return i // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
quick_sort.js/* \u5143\u7d20\u4ea4\u6362 */\nswap(nums, i, j) {\n let tmp = nums[i];\n nums[i] = nums[j];\n nums[j] = tmp;\n}\n\n/* \u54e8\u5175\u5212\u5206 */\npartition(nums, left, right) {\n // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n let i = left,\n j = right;\n while (i < j) {\n while (i < j && nums[j] >= nums[left]) {\n j -= 1; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n }\n while (i < j && nums[i] <= nums[left]) {\n i += 1; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n }\n // \u5143\u7d20\u4ea4\u6362\n this.swap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n }\n this.swap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n return i; // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
quick_sort.ts/* \u5143\u7d20\u4ea4\u6362 */\nswap(nums: number[], i: number, j: number): void {\n let tmp = nums[i];\n nums[i] = nums[j];\n nums[j] = tmp;\n}\n\n/* \u54e8\u5175\u5212\u5206 */\npartition(nums: number[], left: number, right: number): number {\n // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n let i = left,\n j = right;\n while (i < j) {\n while (i < j && nums[j] >= nums[left]) {\n j -= 1; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n }\n while (i < j && nums[i] <= nums[left]) {\n i += 1; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n }\n // \u5143\u7d20\u4ea4\u6362\n this.swap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n }\n this.swap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n return i; // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
quick_sort.dart/* \u5143\u7d20\u4ea4\u6362 */\nvoid _swap(List<int> nums, int i, int j) {\n int tmp = nums[i];\n nums[i] = nums[j];\n nums[j] = tmp;\n}\n\n/* \u54e8\u5175\u5212\u5206 */\nint _partition(List<int> nums, int left, int right) {\n // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n int i = left, j = right;\n while (i < j) {\n while (i < j && nums[j] >= nums[left]) j--; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n while (i < j && nums[i] <= nums[left]) i++; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n _swap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n }\n _swap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n return i; // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
quick_sort.rs/* \u54e8\u5175\u5212\u5206 */\nfn partition(nums: &mut [i32], left: usize, right: usize) -> usize {\n // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n let (mut i, mut j) = (left, right);\n while i < j {\n while i < j && nums[j] >= nums[left] {\n j -= 1; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n }\n while i < j && nums[i] <= nums[left] {\n i += 1; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n }\n nums.swap(i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n }\n nums.swap(i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n i // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
quick_sort.c/* \u5143\u7d20\u4ea4\u6362 */\nvoid swap(int nums[], int i, int j) {\n int tmp = nums[i];\n nums[i] = nums[j];\n nums[j] = tmp;\n}\n\n/* \u5feb\u901f\u6392\u5e8f\u7c7b */\n// \u5feb\u901f\u6392\u5e8f\u7c7b-\u54e8\u5175\u5212\u5206\nint partition(int nums[], int left, int right) {\n // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n int i = left, j = right;\n while (i < j) {\n while (i < j && nums[j] >= nums[left]) {\n // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n j--;\n }\n while (i < j && nums[i] <= nums[left]) {\n // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n i++;\n }\n // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n swap(nums, i, j);\n }\n // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n swap(nums, i, left);\n // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n return i;\n}\n
quick_sort.zig// \u5143\u7d20\u4ea4\u6362\nfn swap(nums: []i32, i: usize, j: usize) void {\n var tmp = nums[i];\n nums[i] = nums[j];\n nums[j] = tmp;\n}\n\n// \u54e8\u5175\u5212\u5206\nfn partition(nums: []i32, left: usize, right: usize) usize {\n // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n var i = left;\n var j = right;\n while (i < j) {\n while (i < j and nums[j] >= nums[left]) j -= 1; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n while (i < j and nums[i] <= nums[left]) i += 1; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n swap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n }\n swap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n return i; // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
"},{"location":"chapter_sorting/quick_sort/#1151","title":"11.5.1 \u00a0 \u7b97\u6cd5\u6d41\u7a0b","text":"\u5feb\u901f\u6392\u5e8f\u7684\u6574\u4f53\u6d41\u7a0b\u5982\u56fe 11-9 \u6240\u793a\u3002
\u56fe 11-9 \u00a0 \u5feb\u901f\u6392\u5e8f\u6d41\u7a0b
PythonC++JavaC#GoSwiftJSTSDartRustCZig quick_sort.pydef quick_sort(self, nums: list[int], left: int, right: int):\n \"\"\"\u5feb\u901f\u6392\u5e8f\"\"\"\n # \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n if left >= right:\n return\n # \u54e8\u5175\u5212\u5206\n pivot = self.partition(nums, left, right)\n # \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\n self.quick_sort(nums, left, pivot - 1)\n self.quick_sort(nums, pivot + 1, right)\n
quick_sort.cpp/* \u5feb\u901f\u6392\u5e8f */\nvoid quickSort(vector<int> &nums, int left, int right) {\n // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n if (left >= right)\n return;\n // \u54e8\u5175\u5212\u5206\n int pivot = partition(nums, left, right);\n // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\n quickSort(nums, left, pivot - 1);\n quickSort(nums, pivot + 1, right);\n}\n
quick_sort.java/* \u5feb\u901f\u6392\u5e8f */\nvoid quickSort(int[] nums, int left, int right) {\n // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n if (left >= right)\n return;\n // \u54e8\u5175\u5212\u5206\n int pivot = partition(nums, left, right);\n // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\n quickSort(nums, left, pivot - 1);\n quickSort(nums, pivot + 1, right);\n}\n
quick_sort.cs/* \u5feb\u901f\u6392\u5e8f */\nvoid QuickSort(int[] nums, int left, int right) {\n // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n if (left >= right)\n return;\n // \u54e8\u5175\u5212\u5206\n int pivot = Partition(nums, left, right);\n // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\n QuickSort(nums, left, pivot - 1);\n QuickSort(nums, pivot + 1, right);\n}\n
quick_sort.go/* \u5feb\u901f\u6392\u5e8f */\nfunc (q *quickSort) quickSort(nums []int, left, right int) {\n // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n if left >= right {\n return\n }\n // \u54e8\u5175\u5212\u5206\n pivot := q.partition(nums, left, right)\n // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\n q.quickSort(nums, left, pivot-1)\n q.quickSort(nums, pivot+1, right)\n}\n
quick_sort.swift/* \u5feb\u901f\u6392\u5e8f */\nfunc quickSort(nums: inout [Int], left: Int, right: Int) {\n // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n if left >= right {\n return\n }\n // \u54e8\u5175\u5212\u5206\n let pivot = partition(nums: &nums, left: left, right: right)\n // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\n quickSort(nums: &nums, left: left, right: pivot - 1)\n quickSort(nums: &nums, left: pivot + 1, right: right)\n}\n
quick_sort.js/* \u5feb\u901f\u6392\u5e8f */\nquickSort(nums, left, right) {\n // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n if (left >= right) return;\n // \u54e8\u5175\u5212\u5206\n const pivot = this.partition(nums, left, right);\n // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\n this.quickSort(nums, left, pivot - 1);\n this.quickSort(nums, pivot + 1, right);\n}\n
quick_sort.ts/* \u5feb\u901f\u6392\u5e8f */\nquickSort(nums: number[], left: number, right: number): void {\n // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n if (left >= right) {\n return;\n }\n // \u54e8\u5175\u5212\u5206\n const pivot = this.partition(nums, left, right);\n // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\n this.quickSort(nums, left, pivot - 1);\n this.quickSort(nums, pivot + 1, right);\n}\n
quick_sort.dart/* \u5feb\u901f\u6392\u5e8f */\nvoid quickSort(List<int> nums, int left, int right) {\n // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n if (left >= right) return;\n // \u54e8\u5175\u5212\u5206\n int pivot = _partition(nums, left, right);\n // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\n quickSort(nums, left, pivot - 1);\n quickSort(nums, pivot + 1, right);\n}\n
quick_sort.rs/* \u5feb\u901f\u6392\u5e8f */\npub fn quick_sort(left: i32, right: i32, nums: &mut [i32]) {\n // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n if left >= right {\n return;\n }\n // \u54e8\u5175\u5212\u5206\n let pivot = Self::partition(nums, left as usize, right as usize) as i32;\n // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\n Self::quick_sort(left, pivot - 1, nums);\n Self::quick_sort(pivot + 1, right, nums);\n}\n
quick_sort.c/* \u5feb\u901f\u6392\u5e8f\u7c7b */\n// \u5feb\u901f\u6392\u5e8f\u7c7b-\u54e8\u5175\u5212\u5206\nint partition(int nums[], int left, int right) {\n // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n int i = left, j = right;\n while (i < j) {\n while (i < j && nums[j] >= nums[left]) {\n // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n j--;\n }\n while (i < j && nums[i] <= nums[left]) {\n // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n i++;\n }\n // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n swap(nums, i, j);\n }\n // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n swap(nums, i, left);\n // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n return i;\n}\n\n// \u5feb\u901f\u6392\u5e8f\u7c7b-\u5feb\u901f\u6392\u5e8f\nvoid quickSort(int nums[], int left, int right) {\n // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n if (left >= right) {\n return;\n }\n // \u54e8\u5175\u5212\u5206\n int pivot = partition(nums, left, right);\n // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\n quickSort(nums, left, pivot - 1);\n quickSort(nums, pivot + 1, right);\n}\n
quick_sort.zig// \u5feb\u901f\u6392\u5e8f\nfn quickSort(nums: []i32, left: usize, right: usize) void {\n // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n if (left >= right) return;\n // \u54e8\u5175\u5212\u5206\n var pivot = partition(nums, left, right);\n // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\n quickSort(nums, left, pivot - 1);\n quickSort(nums, pivot + 1, right);\n}\n
"},{"location":"chapter_sorting/quick_sort/#1152","title":"11.5.2 \u00a0 \u7b97\u6cd5\u7279\u6027","text":"\u4ece\u540d\u79f0\u4e0a\u5c31\u80fd\u770b\u51fa\uff0c\u5feb\u901f\u6392\u5e8f\u5728\u6548\u7387\u65b9\u9762\u5e94\u8be5\u5177\u6709\u4e00\u5b9a\u7684\u4f18\u52bf\u3002\u5c3d\u7ba1\u5feb\u901f\u6392\u5e8f\u7684\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u4e0e\u201c\u5f52\u5e76\u6392\u5e8f\u201d\u548c\u201c\u5806\u6392\u5e8f\u201d\u76f8\u540c\uff0c\u4f46\u901a\u5e38\u5feb\u901f\u6392\u5e8f\u7684\u6548\u7387\u66f4\u9ad8\uff0c\u4e3b\u8981\u6709\u4ee5\u4e0b\u539f\u56e0\u3002
\u5feb\u901f\u6392\u5e8f\u5728\u67d0\u4e9b\u8f93\u5165\u4e0b\u7684\u65f6\u95f4\u6548\u7387\u53ef\u80fd\u964d\u4f4e\u3002\u4e3e\u4e00\u4e2a\u6781\u7aef\u4f8b\u5b50\uff0c\u5047\u8bbe\u8f93\u5165\u6570\u7ec4\u662f\u5b8c\u5168\u5012\u5e8f\u7684\uff0c\u7531\u4e8e\u6211\u4eec\u9009\u62e9\u6700\u5de6\u7aef\u5143\u7d20\u4f5c\u4e3a\u57fa\u51c6\u6570\uff0c\u90a3\u4e48\u5728\u54e8\u5175\u5212\u5206\u5b8c\u6210\u540e\uff0c\u57fa\u51c6\u6570\u88ab\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u53f3\u7aef\uff0c\u5bfc\u81f4\u5de6\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a \\(n - 1\\)\u3001\u53f3\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a \\(0\\) \u3002\u5982\u6b64\u9012\u5f52\u4e0b\u53bb\uff0c\u6bcf\u8f6e\u54e8\u5175\u5212\u5206\u540e\u7684\u53f3\u5b50\u6570\u7ec4\u957f\u5ea6\u90fd\u4e3a \\(0\\) \uff0c\u5206\u6cbb\u7b56\u7565\u5931\u6548\uff0c\u5feb\u901f\u6392\u5e8f\u9000\u5316\u4e3a\u201c\u5192\u6ce1\u6392\u5e8f\u201d\u3002
\u4e3a\u4e86\u5c3d\u91cf\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\u53d1\u751f\uff0c\u6211\u4eec\u53ef\u4ee5\u4f18\u5316\u54e8\u5175\u5212\u5206\u4e2d\u7684\u57fa\u51c6\u6570\u7684\u9009\u53d6\u7b56\u7565\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u968f\u673a\u9009\u53d6\u4e00\u4e2a\u5143\u7d20\u4f5c\u4e3a\u57fa\u51c6\u6570\u3002\u7136\u800c\uff0c\u5982\u679c\u8fd0\u6c14\u4e0d\u4f73\uff0c\u6bcf\u6b21\u90fd\u9009\u5230\u4e0d\u7406\u60f3\u7684\u57fa\u51c6\u6570\uff0c\u6548\u7387\u4ecd\u7136\u4e0d\u5c3d\u5982\u4eba\u610f\u3002
\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u7f16\u7a0b\u8bed\u8a00\u901a\u5e38\u751f\u6210\u7684\u662f\u201c\u4f2a\u968f\u673a\u6570\u201d\u3002\u5982\u679c\u6211\u4eec\u9488\u5bf9\u4f2a\u968f\u673a\u6570\u5e8f\u5217\u6784\u5efa\u4e00\u4e2a\u7279\u5b9a\u7684\u6d4b\u8bd5\u6837\u4f8b\uff0c\u90a3\u4e48\u5feb\u901f\u6392\u5e8f\u7684\u6548\u7387\u4ecd\u7136\u53ef\u80fd\u52a3\u5316\u3002
\u4e3a\u4e86\u8fdb\u4e00\u6b65\u6539\u8fdb\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u6570\u7ec4\u4e2d\u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\uff08\u901a\u5e38\u4e3a\u6570\u7ec4\u7684\u9996\u3001\u5c3e\u3001\u4e2d\u70b9\u5143\u7d20\uff09\uff0c\u5e76\u5c06\u8fd9\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570\u4f5c\u4e3a\u57fa\u51c6\u6570\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u57fa\u51c6\u6570\u201c\u65e2\u4e0d\u592a\u5c0f\u4e5f\u4e0d\u592a\u5927\u201d\u7684\u6982\u7387\u5c06\u5927\u5e45\u63d0\u5347\u3002\u5f53\u7136\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u9009\u53d6\u66f4\u591a\u5019\u9009\u5143\u7d20\uff0c\u4ee5\u8fdb\u4e00\u6b65\u63d0\u9ad8\u7b97\u6cd5\u7684\u7a33\u5065\u6027\u3002\u91c7\u7528\u8fd9\u79cd\u65b9\u6cd5\u540e\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u52a3\u5316\u81f3 \\(O(n^2)\\) \u7684\u6982\u7387\u5927\u5927\u964d\u4f4e\u3002
\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig quick_sort.pydef median_three(self, nums: list[int], left: int, mid: int, right: int) -> int:\n \"\"\"\u9009\u53d6\u4e09\u4e2a\u5143\u7d20\u7684\u4e2d\u4f4d\u6570\"\"\"\n # \u6b64\u5904\u4f7f\u7528\u5f02\u6216\u8fd0\u7b97\u6765\u7b80\u5316\u4ee3\u7801\n # \u5f02\u6216\u89c4\u5219\u4e3a 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1\n if (nums[left] < nums[mid]) ^ (nums[left] < nums[right]):\n return left\n elif (nums[mid] < nums[left]) ^ (nums[mid] < nums[right]):\n return mid\n return right\n\ndef partition(self, nums: list[int], left: int, right: int) -> int:\n \"\"\"\u54e8\u5175\u5212\u5206\uff08\u4e09\u6570\u53d6\u4e2d\u503c\uff09\"\"\"\n # \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n med = self.median_three(nums, left, (left + right) // 2, right)\n # \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\n nums[left], nums[med] = nums[med], nums[left]\n # \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n i, j = left, right\n while i < j:\n while i < j and nums[j] >= nums[left]:\n j -= 1 # \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n while i < j and nums[i] <= nums[left]:\n i += 1 # \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n # \u5143\u7d20\u4ea4\u6362\n nums[i], nums[j] = nums[j], nums[i]\n # \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n nums[i], nums[left] = nums[left], nums[i]\n return i # \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n
quick_sort.cpp/* \u9009\u53d6\u4e09\u4e2a\u5143\u7d20\u7684\u4e2d\u4f4d\u6570 */\nint medianThree(vector<int> &nums, int left, int mid, int right) {\n // \u6b64\u5904\u4f7f\u7528\u5f02\u6216\u8fd0\u7b97\u6765\u7b80\u5316\u4ee3\u7801\n // \u5f02\u6216\u89c4\u5219\u4e3a 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1\n if ((nums[left] < nums[mid]) ^ (nums[left] < nums[right]))\n return left;\n else if ((nums[mid] < nums[left]) ^ (nums[mid] < nums[right]))\n return mid;\n else\n return right;\n}\n\n/* \u54e8\u5175\u5212\u5206\uff08\u4e09\u6570\u53d6\u4e2d\u503c\uff09 */\nint partition(vector<int> &nums, int left, int right) {\n // \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570\n int med = medianThree(nums, left, (left + right) / 2, right);\n // \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\n swap(nums, left, med);\n // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n int i = left, j = right;\n while (i < j) {\n while (i < j && nums[j] >= nums[left])\n j--; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n while (i < j && nums[i] <= nums[left])\n i++; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n swap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n }\n swap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n return i; // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
quick_sort.java/* \u9009\u53d6\u4e09\u4e2a\u5143\u7d20\u7684\u4e2d\u4f4d\u6570 */\nint medianThree(int[] nums, int left, int mid, int right) {\n // \u6b64\u5904\u4f7f\u7528\u5f02\u6216\u8fd0\u7b97\u6765\u7b80\u5316\u4ee3\u7801\n // \u5f02\u6216\u89c4\u5219\u4e3a 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1\n if ((nums[left] < nums[mid]) ^ (nums[left] < nums[right]))\n return left;\n else if ((nums[mid] < nums[left]) ^ (nums[mid] < nums[right]))\n return mid;\n else\n return right;\n}\n\n/* \u54e8\u5175\u5212\u5206\uff08\u4e09\u6570\u53d6\u4e2d\u503c\uff09 */\nint partition(int[] nums, int left, int right) {\n // \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570\n int med = medianThree(nums, left, (left + right) / 2, right);\n // \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\n swap(nums, left, med);\n // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n int i = left, j = right;\n while (i < j) {\n while (i < j && nums[j] >= nums[left])\n j--; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n while (i < j && nums[i] <= nums[left])\n i++; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n swap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n }\n swap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n return i; // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
quick_sort.cs/* \u9009\u53d6\u4e09\u4e2a\u5143\u7d20\u7684\u4e2d\u4f4d\u6570 */\nint MedianThree(int[] nums, int left, int mid, int right) {\n // \u6b64\u5904\u4f7f\u7528\u5f02\u6216\u8fd0\u7b97\u6765\u7b80\u5316\u4ee3\u7801\n // \u5f02\u6216\u89c4\u5219\u4e3a 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1\n if ((nums[left] < nums[mid]) ^ (nums[left] < nums[right]))\n return left;\n else if ((nums[mid] < nums[left]) ^ (nums[mid] < nums[right]))\n return mid;\n else\n return right;\n}\n\n/* \u54e8\u5175\u5212\u5206\uff08\u4e09\u6570\u53d6\u4e2d\u503c\uff09 */\nint Partition(int[] nums, int left, int right) {\n // \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570\n int med = MedianThree(nums, left, (left + right) / 2, right);\n // \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\n Swap(nums, left, med);\n // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n int i = left, j = right;\n while (i < j) {\n while (i < j && nums[j] >= nums[left])\n j--; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n while (i < j && nums[i] <= nums[left])\n i++; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n Swap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n }\n Swap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n return i; // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
quick_sort.go/* \u9009\u53d6\u4e09\u4e2a\u5143\u7d20\u7684\u4e2d\u4f4d\u6570 */\nfunc (q *quickSortMedian) medianThree(nums []int, left, mid, right int) int {\n // \u6b64\u5904\u4f7f\u7528\u5f02\u6216\u8fd0\u7b97\u6765\u7b80\u5316\u4ee3\u7801\uff08!= \u5728\u8fd9\u91cc\u8d77\u5230\u5f02\u6216\u7684\u4f5c\u7528\uff09\n // \u5f02\u6216\u89c4\u5219\u4e3a 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1\n if (nums[left] < nums[mid]) != (nums[left] < nums[right]) {\n return left\n } else if (nums[mid] < nums[left]) != (nums[mid] < nums[right]) {\n return mid\n }\n return right\n}\n\n/* \u54e8\u5175\u5212\u5206\uff08\u4e09\u6570\u53d6\u4e2d\u503c\uff09*/\nfunc (q *quickSortMedian) partition(nums []int, left, right int) int {\n // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n med := q.medianThree(nums, left, (left+right)/2, right)\n // \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\n nums[left], nums[med] = nums[med], nums[left]\n // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n i, j := left, right\n for i < j {\n for i < j && nums[j] >= nums[left] {\n j-- //\u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n }\n for i < j && nums[i] <= nums[left] {\n i++ //\u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n }\n //\u5143\u7d20\u4ea4\u6362\n nums[i], nums[j] = nums[j], nums[i]\n }\n //\u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n nums[i], nums[left] = nums[left], nums[i]\n return i //\u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
quick_sort.swift/* \u9009\u53d6\u4e09\u4e2a\u5143\u7d20\u7684\u4e2d\u4f4d\u6570 */\nfunc medianThree(nums: [Int], left: Int, mid: Int, right: Int) -> Int {\n if (nums[left] < nums[mid]) != (nums[left] < nums[right]) {\n return left\n } else if (nums[mid] < nums[left]) != (nums[mid] < nums[right]) {\n return mid\n } else {\n return right\n }\n}\n\n/* \u54e8\u5175\u5212\u5206\uff08\u4e09\u6570\u53d6\u4e2d\u503c\uff09 */\nfunc partitionMedian(nums: inout [Int], left: Int, right: Int) -> Int {\n // \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570\n let med = medianThree(nums: nums, left: left, mid: (left + right) / 2, right: right)\n // \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\n swap(nums: &nums, i: left, j: med)\n return partition(nums: &nums, left: left, right: right)\n}\n
quick_sort.js/* \u9009\u53d6\u4e09\u4e2a\u5143\u7d20\u7684\u4e2d\u4f4d\u6570 */\nmedianThree(nums, left, mid, right) {\n // \u6b64\u5904\u4f7f\u7528\u5f02\u6216\u8fd0\u7b97\u6765\u7b80\u5316\u4ee3\u7801\n // \u5f02\u6216\u89c4\u5219\u4e3a 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1\n if ((nums[left] < nums[mid]) ^ (nums[left] < nums[right])) return left;\n else if ((nums[mid] < nums[left]) ^ (nums[mid] < nums[right]))\n return mid;\n else return right;\n}\n\n/* \u54e8\u5175\u5212\u5206\uff08\u4e09\u6570\u53d6\u4e2d\u503c\uff09 */\npartition(nums, left, right) {\n // \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570\n let med = this.medianThree(\n nums,\n left,\n Math.floor((left + right) / 2),\n right\n );\n // \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\n this.swap(nums, left, med);\n // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n let i = left,\n j = right;\n while (i < j) {\n while (i < j && nums[j] >= nums[left]) j--; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n while (i < j && nums[i] <= nums[left]) i++; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n this.swap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n }\n this.swap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n return i; // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
quick_sort.ts/* \u9009\u53d6\u4e09\u4e2a\u5143\u7d20\u7684\u4e2d\u4f4d\u6570 */\nmedianThree(\n nums: number[],\n left: number,\n mid: number,\n right: number\n): number {\n // \u6b64\u5904\u4f7f\u7528\u5f02\u6216\u8fd0\u7b97\u6765\u7b80\u5316\u4ee3\u7801\n // \u5f02\u6216\u89c4\u5219\u4e3a 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1\n if (Number(nums[left] < nums[mid]) ^ Number(nums[left] < nums[right])) {\n return left;\n } else if (\n Number(nums[mid] < nums[left]) ^ Number(nums[mid] < nums[right])\n ) {\n return mid;\n } else {\n return right;\n }\n}\n\n/* \u54e8\u5175\u5212\u5206\uff08\u4e09\u6570\u53d6\u4e2d\u503c\uff09 */\npartition(nums: number[], left: number, right: number): number {\n // \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570\n let med = this.medianThree(\n nums,\n left,\n Math.floor((left + right) / 2),\n right\n );\n // \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\n this.swap(nums, left, med);\n // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n let i = left,\n j = right;\n while (i < j) {\n while (i < j && nums[j] >= nums[left]) {\n j--; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n }\n while (i < j && nums[i] <= nums[left]) {\n i++; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n }\n this.swap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n }\n this.swap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n return i; // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
quick_sort.dart/* \u9009\u53d6\u4e09\u4e2a\u5143\u7d20\u7684\u4e2d\u4f4d\u6570 */\nint _medianThree(List<int> nums, int left, int mid, int right) {\n // \u6b64\u5904\u4f7f\u7528\u5f02\u6216\u8fd0\u7b97\u6765\u7b80\u5316\u4ee3\u7801\n // \u5f02\u6216\u89c4\u5219\u4e3a 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1\n if ((nums[left] < nums[mid]) ^ (nums[left] < nums[right]))\n return left;\n else if ((nums[mid] < nums[left]) ^ (nums[mid] < nums[right]))\n return mid;\n else\n return right;\n}\n\n/* \u54e8\u5175\u5212\u5206\uff08\u4e09\u6570\u53d6\u4e2d\u503c\uff09 */\nint _partition(List<int> nums, int left, int right) {\n // \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570\n int med = _medianThree(nums, left, (left + right) ~/ 2, right);\n // \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\n _swap(nums, left, med);\n // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n int i = left, j = right;\n while (i < j) {\n while (i < j && nums[j] >= nums[left]) j--; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n while (i < j && nums[i] <= nums[left]) i++; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n _swap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n }\n _swap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n return i; // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
quick_sort.rs/* \u9009\u53d6\u4e09\u4e2a\u5143\u7d20\u7684\u4e2d\u4f4d\u6570 */\nfn median_three(nums: &mut [i32], left: usize, mid: usize, right: usize) -> usize {\n // \u6b64\u5904\u4f7f\u7528\u5f02\u6216\u8fd0\u7b97\u6765\u7b80\u5316\u4ee3\u7801\n // \u5f02\u6216\u89c4\u5219\u4e3a 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1\n if (nums[left] < nums[mid]) ^ (nums[left] < nums[right]) {\n return left;\n } else if (nums[mid] < nums[left]) ^ (nums[mid] < nums[right]) {\n return mid;\n } \n right\n}\n\n/* \u54e8\u5175\u5212\u5206\uff08\u4e09\u6570\u53d6\u4e2d\u503c\uff09 */\nfn partition(nums: &mut [i32], left: usize, right: usize) -> usize {\n // \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570\n let med = Self::median_three(nums, left, (left + right) / 2, right);\n // \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\n nums.swap(left, med);\n // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n let (mut i, mut j) = (left, right);\n while i < j {\n while i < j && nums[j] >= nums[left] {\n j -= 1; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n }\n while i < j && nums[i] <= nums[left] {\n i += 1; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n }\n nums.swap(i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n }\n nums.swap(i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n i // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
quick_sort.c/* \u5feb\u901f\u6392\u5e8f\u7c7b\uff08\u4e2d\u4f4d\u57fa\u51c6\u6570\u4f18\u5316\uff09 */\n// \u9009\u53d6\u4e09\u4e2a\u5143\u7d20\u7684\u4e2d\u4f4d\u6570\nint medianThree(int nums[], int left, int mid, int right) {\n // \u6b64\u5904\u4f7f\u7528\u5f02\u6216\u8fd0\u7b97\u6765\u7b80\u5316\u4ee3\u7801\n // \u5f02\u6216\u89c4\u5219\u4e3a 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1\n if ((nums[left] < nums[mid]) ^ (nums[left] < nums[right]))\n return left;\n else if ((nums[mid] < nums[left]) ^ (nums[mid] < nums[right]))\n return mid;\n else\n return right;\n}\n\n/* \u54e8\u5175\u5212\u5206\uff08\u4e09\u6570\u53d6\u4e2d\u503c\uff09 */ \nint partitionMedian(int nums[], int left, int right) {\n // \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570\n int med = medianThree(nums, left, (left + right) / 2, right);\n // \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\n swap(nums, left, med);\n // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n int i = left, j = right;\n while (i < j) {\n while (i < j && nums[j] >= nums[left])\n j--; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n while (i < j && nums[i] <= nums[left])\n i++; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n swap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n }\n swap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n return i; // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
quick_sort.zig// \u9009\u53d6\u4e09\u4e2a\u5143\u7d20\u7684\u4e2d\u4f4d\u6570\nfn medianThree(nums: []i32, left: usize, mid: usize, right: usize) usize {\n // \u6b64\u5904\u4f7f\u7528\u5f02\u6216\u8fd0\u7b97\u6765\u7b80\u5316\u4ee3\u7801\n // \u5f02\u6216\u89c4\u5219\u4e3a 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1\n if ((nums[left] < nums[mid]) != (nums[left] < nums[right])) {\n return left;\n } else if ((nums[mid] < nums[left]) != (nums[mid] < nums[right])) {\n return mid;\n } else {\n return right;\n }\n}\n\n// \u54e8\u5175\u5212\u5206\uff08\u4e09\u6570\u53d6\u4e2d\u503c\uff09\nfn partition(nums: []i32, left: usize, right: usize) usize {\n // \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570\n var med = medianThree(nums, left, (left + right) / 2, right);\n // \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\n swap(nums, left, med);\n // \u4ee5 nums[left] \u4e3a\u57fa\u51c6\u6570\n var i = left;\n var j = right;\n while (i < j) {\n while (i < j and nums[j] >= nums[left]) j -= 1; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n while (i < j and nums[i] <= nums[left]) i += 1; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n swap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n }\n swap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\n return i; // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n
"},{"location":"chapter_sorting/quick_sort/#1155","title":"11.5.5 \u00a0 \u5c3e\u9012\u5f52\u4f18\u5316","text":"\u5728\u67d0\u4e9b\u8f93\u5165\u4e0b\uff0c\u5feb\u901f\u6392\u5e8f\u53ef\u80fd\u5360\u7528\u7a7a\u95f4\u8f83\u591a\u3002\u4ee5\u5b8c\u5168\u6709\u5e8f\u7684\u8f93\u5165\u6570\u7ec4\u4e3a\u4f8b\uff0c\u8bbe\u9012\u5f52\u4e2d\u7684\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a \\(m\\) \uff0c\u6bcf\u8f6e\u54e8\u5175\u5212\u5206\u64cd\u4f5c\u90fd\u5c06\u4ea7\u751f\u957f\u5ea6\u4e3a \\(0\\) \u7684\u5de6\u5b50\u6570\u7ec4\u548c\u957f\u5ea6\u4e3a \\(m - 1\\) \u7684\u53f3\u5b50\u6570\u7ec4\uff0c\u8fd9\u610f\u5473\u7740\u6bcf\u4e00\u5c42\u9012\u5f52\u8c03\u7528\u51cf\u5c11\u7684\u95ee\u9898\u89c4\u6a21\u975e\u5e38\u5c0f\uff08\u53ea\u51cf\u5c11\u4e00\u4e2a\u5143\u7d20\uff09\uff0c\u9012\u5f52\u6811\u7684\u9ad8\u5ea6\u4f1a\u8fbe\u5230 \\(n - 1\\) \uff0c\u6b64\u65f6\u9700\u8981\u5360\u7528 \\(O(n)\\) \u5927\u5c0f\u7684\u6808\u5e27\u7a7a\u95f4\u3002
\u4e3a\u4e86\u9632\u6b62\u6808\u5e27\u7a7a\u95f4\u7684\u7d2f\u79ef\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u6bcf\u8f6e\u54e8\u5175\u6392\u5e8f\u5b8c\u6210\u540e\uff0c\u6bd4\u8f83\u4e24\u4e2a\u5b50\u6570\u7ec4\u7684\u957f\u5ea6\uff0c\u4ec5\u5bf9\u8f83\u77ed\u7684\u5b50\u6570\u7ec4\u8fdb\u884c\u9012\u5f52\u3002\u7531\u4e8e\u8f83\u77ed\u5b50\u6570\u7ec4\u7684\u957f\u5ea6\u4e0d\u4f1a\u8d85\u8fc7 \\(n / 2\\) \uff0c\u56e0\u6b64\u8fd9\u79cd\u65b9\u6cd5\u80fd\u786e\u4fdd\u9012\u5f52\u6df1\u5ea6\u4e0d\u8d85\u8fc7 \\(\\log n\\) \uff0c\u4ece\u800c\u5c06\u6700\u5dee\u7a7a\u95f4\u590d\u6742\u5ea6\u4f18\u5316\u81f3 \\(O(\\log n)\\) \u3002\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig quick_sort.pydef quick_sort(self, nums: list[int], left: int, right: int):\n \"\"\"\u5feb\u901f\u6392\u5e8f\uff08\u5c3e\u9012\u5f52\u4f18\u5316\uff09\"\"\"\n # \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\n while left < right:\n # \u54e8\u5175\u5212\u5206\u64cd\u4f5c\n pivot = self.partition(nums, left, right)\n # \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u901f\u6392\u5e8f\n if pivot - left < right - pivot:\n self.quick_sort(nums, left, pivot - 1) # \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\n left = pivot + 1 # \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n else:\n self.quick_sort(nums, pivot + 1, right) # \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\n right = pivot - 1 # \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [left, pivot - 1]\n
quick_sort.cpp/* \u5feb\u901f\u6392\u5e8f\uff08\u5c3e\u9012\u5f52\u4f18\u5316\uff09 */\nvoid quickSort(vector<int> &nums, int left, int right) {\n // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\n while (left < right) {\n // \u54e8\u5175\u5212\u5206\u64cd\u4f5c\n int pivot = partition(nums, left, right);\n // \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u901f\u6392\u5e8f\n if (pivot - left < right - pivot) {\n quickSort(nums, left, pivot - 1); // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\n left = pivot + 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n } else {\n quickSort(nums, pivot + 1, right); // \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\n right = pivot - 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [left, pivot - 1]\n }\n }\n}\n
quick_sort.java/* \u5feb\u901f\u6392\u5e8f\uff08\u5c3e\u9012\u5f52\u4f18\u5316\uff09 */\nvoid quickSort(int[] nums, int left, int right) {\n // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\n while (left < right) {\n // \u54e8\u5175\u5212\u5206\u64cd\u4f5c\n int pivot = partition(nums, left, right);\n // \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u901f\u6392\u5e8f\n if (pivot - left < right - pivot) {\n quickSort(nums, left, pivot - 1); // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\n left = pivot + 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n } else {\n quickSort(nums, pivot + 1, right); // \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\n right = pivot - 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [left, pivot - 1]\n }\n }\n}\n
quick_sort.cs/* \u5feb\u901f\u6392\u5e8f\uff08\u5c3e\u9012\u5f52\u4f18\u5316\uff09 */\nvoid QuickSort(int[] nums, int left, int right) {\n // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\n while (left < right) {\n // \u54e8\u5175\u5212\u5206\u64cd\u4f5c\n int pivot = Partition(nums, left, right);\n // \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u901f\u6392\u5e8f\n if (pivot - left < right - pivot) {\n QuickSort(nums, left, pivot - 1); // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\n left = pivot + 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n } else {\n QuickSort(nums, pivot + 1, right); // \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\n right = pivot - 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [left, pivot - 1]\n }\n }\n}\n
quick_sort.go/* \u5feb\u901f\u6392\u5e8f\uff08\u5c3e\u9012\u5f52\u4f18\u5316\uff09*/\nfunc (q *quickSortTailCall) quickSort(nums []int, left, right int) {\n // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\n for left < right {\n // \u54e8\u5175\u5212\u5206\u64cd\u4f5c\n pivot := q.partition(nums, left, right)\n // \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u901f\u6392\u5e8f\n if pivot-left < right-pivot {\n q.quickSort(nums, left, pivot-1) // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\n left = pivot + 1 // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n } else {\n q.quickSort(nums, pivot+1, right) // \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\n right = pivot - 1 // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [left, pivot - 1]\n }\n }\n}\n
quick_sort.swift/* \u5feb\u901f\u6392\u5e8f\uff08\u5c3e\u9012\u5f52\u4f18\u5316\uff09 */\nfunc quickSortTailCall(nums: inout [Int], left: Int, right: Int) {\n var left = left\n var right = right\n // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\n while left < right {\n // \u54e8\u5175\u5212\u5206\u64cd\u4f5c\n let pivot = partition(nums: &nums, left: left, right: right)\n // \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u901f\u6392\u5e8f\n if (pivot - left) < (right - pivot) {\n quickSortTailCall(nums: &nums, left: left, right: pivot - 1) // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\n left = pivot + 1 // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n } else {\n quickSortTailCall(nums: &nums, left: pivot + 1, right: right) // \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\n right = pivot - 1 // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [left, pivot - 1]\n }\n }\n}\n
quick_sort.js/* \u5feb\u901f\u6392\u5e8f\uff08\u5c3e\u9012\u5f52\u4f18\u5316\uff09 */\nquickSort(nums, left, right) {\n // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\n while (left < right) {\n // \u54e8\u5175\u5212\u5206\u64cd\u4f5c\n let pivot = this.partition(nums, left, right);\n // \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u901f\u6392\u5e8f\n if (pivot - left < right - pivot) {\n this.quickSort(nums, left, pivot - 1); // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\n left = pivot + 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n } else {\n this.quickSort(nums, pivot + 1, right); // \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\n right = pivot - 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [left, pivot - 1]\n }\n }\n}\n
quick_sort.ts/* \u5feb\u901f\u6392\u5e8f\uff08\u5c3e\u9012\u5f52\u4f18\u5316\uff09 */\nquickSort(nums: number[], left: number, right: number): void {\n // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\n while (left < right) {\n // \u54e8\u5175\u5212\u5206\u64cd\u4f5c\n let pivot = this.partition(nums, left, right);\n // \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u901f\u6392\u5e8f\n if (pivot - left < right - pivot) {\n this.quickSort(nums, left, pivot - 1); // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\n left = pivot + 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n } else {\n this.quickSort(nums, pivot + 1, right); // \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\n right = pivot - 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [left, pivot - 1]\n }\n }\n}\n
quick_sort.dart/* \u5feb\u901f\u6392\u5e8f\uff08\u5c3e\u9012\u5f52\u4f18\u5316\uff09 */\nvoid quickSort(List<int> nums, int left, int right) {\n // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\n while (left < right) {\n // \u54e8\u5175\u5212\u5206\u64cd\u4f5c\n int pivot = _partition(nums, left, right);\n // \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u901f\u6392\u5e8f\n if (pivot - left < right - pivot) {\n quickSort(nums, left, pivot - 1); // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\n left = pivot + 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n } else {\n quickSort(nums, pivot + 1, right); // \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\n right = pivot - 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [left, pivot - 1]\n }\n }\n}\n
quick_sort.rs/* \u5feb\u901f\u6392\u5e8f\uff08\u5c3e\u9012\u5f52\u4f18\u5316\uff09 */\npub fn quick_sort(mut left: i32, mut right: i32, nums: &mut [i32]) {\n // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\n while left < right {\n // \u54e8\u5175\u5212\u5206\u64cd\u4f5c\n let pivot = Self::partition(nums, left as usize, right as usize) as i32;\n // \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u901f\u6392\u5e8f\n if pivot - left < right - pivot {\n Self::quick_sort(left, pivot - 1, nums); // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\n left = pivot + 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n } else {\n Self::quick_sort(pivot + 1, right, nums); // \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\n right = pivot - 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [left, pivot - 1]\n }\n }\n}\n
quick_sort.c/* \u5feb\u901f\u6392\u5e8f\u7c7b\uff08\u5c3e\u9012\u5f52\u4f18\u5316\uff09 */\n// \u5feb\u901f\u6392\u5e8f\uff08\u5c3e\u9012\u5f52\u4f18\u5316\uff09\nvoid quickSortTailCall(int nums[], int left, int right) {\n // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\n while (left < right) {\n // \u54e8\u5175\u5212\u5206\u64cd\u4f5c\n int pivot = partition(nums, left, right);\n // \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u901f\u6392\u5e8f\n if (pivot - left < right - pivot) {\n quickSortTailCall(nums, left, pivot - 1); // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\n left = pivot + 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n } else {\n quickSortTailCall(nums, pivot + 1, right); // \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\n right = pivot - 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [left, pivot - 1]\n }\n }\n}\n
quick_sort.zig// \u5feb\u901f\u6392\u5e8f\uff08\u5c3e\u9012\u5f52\u4f18\u5316\uff09\nfn quickSort(nums: []i32, left_: usize, right_: usize) void {\n var left = left_;\n var right = right_;\n // \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n while (left < right) {\n // \u54e8\u5175\u5212\u5206\u64cd\u4f5c\n var pivot = partition(nums, left, right);\n // \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u901f\u6392\u5e8f\n if (pivot - left < right - pivot) {\n quickSort(nums, left, pivot - 1); // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\n left = pivot + 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n } else {\n quickSort(nums, pivot + 1, right); // \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\n right = pivot - 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [left, pivot - 1]\n }\n }\n}\n
"},{"location":"chapter_sorting/radix_sort/","title":"11.10 \u00a0 \u57fa\u6570\u6392\u5e8f","text":"\u4e0a\u4e00\u8282\u4ecb\u7ecd\u4e86\u8ba1\u6570\u6392\u5e8f\uff0c\u5b83\u9002\u7528\u4e8e\u6570\u636e\u91cf \\(n\\) \u8f83\u5927\u4f46\u6570\u636e\u8303\u56f4 \\(m\\) \u8f83\u5c0f\u7684\u60c5\u51b5\u3002\u5047\u8bbe\u6211\u4eec\u9700\u8981\u5bf9 \\(n = 10^6\\) \u4e2a\u5b66\u53f7\u8fdb\u884c\u6392\u5e8f\uff0c\u800c\u5b66\u53f7\u662f\u4e00\u4e2a \\(8\\) \u4f4d\u6570\u5b57\uff0c\u8fd9\u610f\u5473\u7740\u6570\u636e\u8303\u56f4 \\(m = 10^8\\) \u975e\u5e38\u5927\uff0c\u4f7f\u7528\u8ba1\u6570\u6392\u5e8f\u9700\u8981\u5206\u914d\u5927\u91cf\u5185\u5b58\u7a7a\u95f4\uff0c\u800c\u57fa\u6570\u6392\u5e8f\u53ef\u4ee5\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\u3002
\u300c\u57fa\u6570\u6392\u5e8f radix sort\u300d\u7684\u6838\u5fc3\u601d\u60f3\u4e0e\u8ba1\u6570\u6392\u5e8f\u4e00\u81f4\uff0c\u4e5f\u901a\u8fc7\u7edf\u8ba1\u4e2a\u6570\u6765\u5b9e\u73b0\u6392\u5e8f\u3002\u5728\u6b64\u57fa\u7840\u4e0a\uff0c\u57fa\u6570\u6392\u5e8f\u5229\u7528\u6570\u5b57\u5404\u4f4d\u4e4b\u95f4\u7684\u9012\u8fdb\u5173\u7cfb\uff0c\u4f9d\u6b21\u5bf9\u6bcf\u4e00\u4f4d\u8fdb\u884c\u6392\u5e8f\uff0c\u4ece\u800c\u5f97\u5230\u6700\u7ec8\u7684\u6392\u5e8f\u7ed3\u679c\u3002
"},{"location":"chapter_sorting/radix_sort/#11101","title":"11.10.1 \u00a0 \u7b97\u6cd5\u6d41\u7a0b","text":"\u4ee5\u5b66\u53f7\u6570\u636e\u4e3a\u4f8b\uff0c\u5047\u8bbe\u6570\u5b57\u7684\u6700\u4f4e\u4f4d\u662f\u7b2c \\(1\\) \u4f4d\uff0c\u6700\u9ad8\u4f4d\u662f\u7b2c \\(8\\) \u4f4d\uff0c\u57fa\u6570\u6392\u5e8f\u7684\u6d41\u7a0b\u5982\u56fe 11-18 \u6240\u793a\u3002
2.
\u7ee7\u7eed\u8fed\u4ee3\uff0c\u76f4\u5230\u6240\u6709\u4f4d\u90fd\u6392\u5e8f\u5b8c\u6210\u540e\u7ed3\u675f\u3002\u56fe 11-18 \u00a0 \u57fa\u6570\u6392\u5e8f\u7b97\u6cd5\u6d41\u7a0b
\u4e0b\u9762\u5256\u6790\u4ee3\u7801\u5b9e\u73b0\u3002\u5bf9\u4e8e\u4e00\u4e2a \\(d\\) \u8fdb\u5236\u7684\u6570\u5b57 \\(x\\) \uff0c\u8981\u83b7\u53d6\u5176\u7b2c \\(k\\) \u4f4d \\(x_k\\) \uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u8ba1\u7b97\u516c\u5f0f\uff1a
\\[ x_k = \\lfloor\\frac{x}{d^{k-1}}\\rfloor \\bmod d \\]\u5176\u4e2d \\(\\lfloor a \\rfloor\\) \u8868\u793a\u5bf9\u6d6e\u70b9\u6570 \\(a\\) \u5411\u4e0b\u53d6\u6574\uff0c\u800c \\(\\bmod \\: d\\) \u8868\u793a\u5bf9 \\(d\\) \u53d6\u4f59\u3002\u5bf9\u4e8e\u5b66\u53f7\u6570\u636e\uff0c\\(d = 10\\) \u4e14 \\(k \\in [1, 8]\\) \u3002
\u6b64\u5916\uff0c\u6211\u4eec\u9700\u8981\u5c0f\u5e45\u6539\u52a8\u8ba1\u6570\u6392\u5e8f\u4ee3\u7801\uff0c\u4f7f\u4e4b\u53ef\u4ee5\u6839\u636e\u6570\u5b57\u7684\u7b2c \\(k\\) \u4f4d\u8fdb\u884c\u6392\u5e8f\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig radix_sort.pydef digit(num: int, exp: int) -> int:\n \"\"\"\u83b7\u53d6\u5143\u7d20 num \u7684\u7b2c k \u4f4d\uff0c\u5176\u4e2d exp = 10^(k-1)\"\"\"\n # \u4f20\u5165 exp \u800c\u975e k \u53ef\u4ee5\u907f\u514d\u5728\u6b64\u91cd\u590d\u6267\u884c\u6602\u8d35\u7684\u6b21\u65b9\u8ba1\u7b97\n return (num // exp) % 10\n\ndef counting_sort_digit(nums: list[int], exp: int):\n \"\"\"\u8ba1\u6570\u6392\u5e8f\uff08\u6839\u636e nums \u7b2c k \u4f4d\u6392\u5e8f\uff09\"\"\"\n # \u5341\u8fdb\u5236\u7684\u4f4d\u8303\u56f4\u4e3a 0~9 \uff0c\u56e0\u6b64\u9700\u8981\u957f\u5ea6\u4e3a 10 \u7684\u6876\u6570\u7ec4\n counter = [0] * 10\n n = len(nums)\n # \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n for i in range(n):\n d = digit(nums[i], exp) # \u83b7\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8bb0\u4e3a d\n counter[d] += 1 # \u7edf\u8ba1\u6570\u5b57 d \u7684\u51fa\u73b0\u6b21\u6570\n # \u6c42\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u4e2a\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u6570\u7ec4\u7d22\u5f15\u201d\n for i in range(1, 10):\n counter[i] += counter[i - 1]\n # \u5012\u5e8f\u904d\u5386\uff0c\u6839\u636e\u6876\u5185\u7edf\u8ba1\u7ed3\u679c\uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165 res\n res = [0] * n\n for i in range(n - 1, -1, -1):\n d = digit(nums[i], exp)\n j = counter[d] - 1 # \u83b7\u53d6 d \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15 j\n res[j] = nums[i] # \u5c06\u5f53\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\n counter[d] -= 1 # \u5c06 d \u7684\u6570\u91cf\u51cf 1\n # \u4f7f\u7528\u7ed3\u679c\u8986\u76d6\u539f\u6570\u7ec4 nums\n for i in range(n):\n nums[i] = res[i]\n\ndef radix_sort(nums: list[int]):\n \"\"\"\u57fa\u6570\u6392\u5e8f\"\"\"\n # \u83b7\u53d6\u6570\u7ec4\u7684\u6700\u5927\u5143\u7d20\uff0c\u7528\u4e8e\u5224\u65ad\u6700\u5927\u4f4d\u6570\n m = max(nums)\n # \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\n exp = 1\n while exp <= m:\n # \u5bf9\u6570\u7ec4\u5143\u7d20\u7684\u7b2c k \u4f4d\u6267\u884c\u8ba1\u6570\u6392\u5e8f\n # k = 1 -> exp = 1\n # k = 2 -> exp = 10\n # \u5373 exp = 10^(k-1)\n counting_sort_digit(nums, exp)\n exp *= 10\n
radix_sort.cpp/* \u83b7\u53d6\u5143\u7d20 num \u7684\u7b2c k \u4f4d\uff0c\u5176\u4e2d exp = 10^(k-1) */\nint digit(int num, int exp) {\n // \u4f20\u5165 exp \u800c\u975e k \u53ef\u4ee5\u907f\u514d\u5728\u6b64\u91cd\u590d\u6267\u884c\u6602\u8d35\u7684\u6b21\u65b9\u8ba1\u7b97\n return (num / exp) % 10;\n}\n\n/* \u8ba1\u6570\u6392\u5e8f\uff08\u6839\u636e nums \u7b2c k \u4f4d\u6392\u5e8f\uff09 */\nvoid countingSortDigit(vector<int> &nums, int exp) {\n // \u5341\u8fdb\u5236\u7684\u4f4d\u8303\u56f4\u4e3a 0~9 \uff0c\u56e0\u6b64\u9700\u8981\u957f\u5ea6\u4e3a 10 \u7684\u6876\u6570\u7ec4\n vector<int> counter(10, 0);\n int n = nums.size();\n // \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n for (int i = 0; i < n; i++) {\n int d = digit(nums[i], exp); // \u83b7\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8bb0\u4e3a d\n counter[d]++; // \u7edf\u8ba1\u6570\u5b57 d \u7684\u51fa\u73b0\u6b21\u6570\n }\n // \u6c42\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u4e2a\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u6570\u7ec4\u7d22\u5f15\u201d\n for (int i = 1; i < 10; i++) {\n counter[i] += counter[i - 1];\n }\n // \u5012\u5e8f\u904d\u5386\uff0c\u6839\u636e\u6876\u5185\u7edf\u8ba1\u7ed3\u679c\uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165 res\n vector<int> res(n, 0);\n for (int i = n - 1; i >= 0; i--) {\n int d = digit(nums[i], exp);\n int j = counter[d] - 1; // \u83b7\u53d6 d \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15 j\n res[j] = nums[i]; // \u5c06\u5f53\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\n counter[d]--; // \u5c06 d \u7684\u6570\u91cf\u51cf 1\n }\n // \u4f7f\u7528\u7ed3\u679c\u8986\u76d6\u539f\u6570\u7ec4 nums\n for (int i = 0; i < n; i++)\n nums[i] = res[i];\n}\n\n/* \u57fa\u6570\u6392\u5e8f */\nvoid radixSort(vector<int> &nums) {\n // \u83b7\u53d6\u6570\u7ec4\u7684\u6700\u5927\u5143\u7d20\uff0c\u7528\u4e8e\u5224\u65ad\u6700\u5927\u4f4d\u6570\n int m = *max_element(nums.begin(), nums.end());\n // \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\n for (int exp = 1; exp <= m; exp *= 10)\n // \u5bf9\u6570\u7ec4\u5143\u7d20\u7684\u7b2c k \u4f4d\u6267\u884c\u8ba1\u6570\u6392\u5e8f\n // k = 1 -> exp = 1\n // k = 2 -> exp = 10\n // \u5373 exp = 10^(k-1)\n countingSortDigit(nums, exp);\n}\n
radix_sort.java/* \u83b7\u53d6\u5143\u7d20 num \u7684\u7b2c k \u4f4d\uff0c\u5176\u4e2d exp = 10^(k-1) */\nint digit(int num, int exp) {\n // \u4f20\u5165 exp \u800c\u975e k \u53ef\u4ee5\u907f\u514d\u5728\u6b64\u91cd\u590d\u6267\u884c\u6602\u8d35\u7684\u6b21\u65b9\u8ba1\u7b97\n return (num / exp) % 10;\n}\n\n/* \u8ba1\u6570\u6392\u5e8f\uff08\u6839\u636e nums \u7b2c k \u4f4d\u6392\u5e8f\uff09 */\nvoid countingSortDigit(int[] nums, int exp) {\n // \u5341\u8fdb\u5236\u7684\u4f4d\u8303\u56f4\u4e3a 0~9 \uff0c\u56e0\u6b64\u9700\u8981\u957f\u5ea6\u4e3a 10 \u7684\u6876\u6570\u7ec4\n int[] counter = new int[10];\n int n = nums.length;\n // \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n for (int i = 0; i < n; i++) {\n int d = digit(nums[i], exp); // \u83b7\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8bb0\u4e3a d\n counter[d]++; // \u7edf\u8ba1\u6570\u5b57 d \u7684\u51fa\u73b0\u6b21\u6570\n }\n // \u6c42\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u4e2a\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u6570\u7ec4\u7d22\u5f15\u201d\n for (int i = 1; i < 10; i++) {\n counter[i] += counter[i - 1];\n }\n // \u5012\u5e8f\u904d\u5386\uff0c\u6839\u636e\u6876\u5185\u7edf\u8ba1\u7ed3\u679c\uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165 res\n int[] res = new int[n];\n for (int i = n - 1; i >= 0; i--) {\n int d = digit(nums[i], exp);\n int j = counter[d] - 1; // \u83b7\u53d6 d \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15 j\n res[j] = nums[i]; // \u5c06\u5f53\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\n counter[d]--; // \u5c06 d \u7684\u6570\u91cf\u51cf 1\n }\n // \u4f7f\u7528\u7ed3\u679c\u8986\u76d6\u539f\u6570\u7ec4 nums\n for (int i = 0; i < n; i++)\n nums[i] = res[i];\n}\n\n/* \u57fa\u6570\u6392\u5e8f */\nvoid radixSort(int[] nums) {\n // \u83b7\u53d6\u6570\u7ec4\u7684\u6700\u5927\u5143\u7d20\uff0c\u7528\u4e8e\u5224\u65ad\u6700\u5927\u4f4d\u6570\n int m = Integer.MIN_VALUE;\n for (int num : nums)\n if (num > m)\n m = num;\n // \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\n for (int exp = 1; exp <= m; exp *= 10)\n // \u5bf9\u6570\u7ec4\u5143\u7d20\u7684\u7b2c k \u4f4d\u6267\u884c\u8ba1\u6570\u6392\u5e8f\n // k = 1 -> exp = 1\n // k = 2 -> exp = 10\n // \u5373 exp = 10^(k-1)\n countingSortDigit(nums, exp);\n}\n
radix_sort.cs/* \u83b7\u53d6\u5143\u7d20 num \u7684\u7b2c k \u4f4d\uff0c\u5176\u4e2d exp = 10^(k-1) */\nint Digit(int num, int exp) {\n // \u4f20\u5165 exp \u800c\u975e k \u53ef\u4ee5\u907f\u514d\u5728\u6b64\u91cd\u590d\u6267\u884c\u6602\u8d35\u7684\u6b21\u65b9\u8ba1\u7b97\n return (num / exp) % 10;\n}\n\n/* \u8ba1\u6570\u6392\u5e8f\uff08\u6839\u636e nums \u7b2c k \u4f4d\u6392\u5e8f\uff09 */\nvoid CountingSortDigit(int[] nums, int exp) {\n // \u5341\u8fdb\u5236\u7684\u4f4d\u8303\u56f4\u4e3a 0~9 \uff0c\u56e0\u6b64\u9700\u8981\u957f\u5ea6\u4e3a 10 \u7684\u6876\u6570\u7ec4\n int[] counter = new int[10];\n int n = nums.Length;\n // \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n for (int i = 0; i < n; i++) {\n int d = Digit(nums[i], exp); // \u83b7\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8bb0\u4e3a d\n counter[d]++; // \u7edf\u8ba1\u6570\u5b57 d \u7684\u51fa\u73b0\u6b21\u6570\n }\n // \u6c42\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u4e2a\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u6570\u7ec4\u7d22\u5f15\u201d\n for (int i = 1; i < 10; i++) {\n counter[i] += counter[i - 1];\n }\n // \u5012\u5e8f\u904d\u5386\uff0c\u6839\u636e\u6876\u5185\u7edf\u8ba1\u7ed3\u679c\uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165 res\n int[] res = new int[n];\n for (int i = n - 1; i >= 0; i--) {\n int d = Digit(nums[i], exp);\n int j = counter[d] - 1; // \u83b7\u53d6 d \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15 j\n res[j] = nums[i]; // \u5c06\u5f53\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\n counter[d]--; // \u5c06 d \u7684\u6570\u91cf\u51cf 1\n }\n // \u4f7f\u7528\u7ed3\u679c\u8986\u76d6\u539f\u6570\u7ec4 nums\n for (int i = 0; i < n; i++) {\n nums[i] = res[i];\n }\n}\n\n/* \u57fa\u6570\u6392\u5e8f */\nvoid RadixSort(int[] nums) {\n // \u83b7\u53d6\u6570\u7ec4\u7684\u6700\u5927\u5143\u7d20\uff0c\u7528\u4e8e\u5224\u65ad\u6700\u5927\u4f4d\u6570\n int m = int.MinValue;\n foreach (int num in nums) {\n if (num > m) m = num;\n }\n // \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\n for (int exp = 1; exp <= m; exp *= 10) {\n // \u5bf9\u6570\u7ec4\u5143\u7d20\u7684\u7b2c k \u4f4d\u6267\u884c\u8ba1\u6570\u6392\u5e8f\n // k = 1 -> exp = 1\n // k = 2 -> exp = 10\n // \u5373 exp = 10^(k-1)\n CountingSortDigit(nums, exp);\n }\n}\n
radix_sort.go/* \u83b7\u53d6\u5143\u7d20 num \u7684\u7b2c k \u4f4d\uff0c\u5176\u4e2d exp = 10^(k-1) */\nfunc digit(num, exp int) int {\n // \u4f20\u5165 exp \u800c\u975e k \u53ef\u4ee5\u907f\u514d\u5728\u6b64\u91cd\u590d\u6267\u884c\u6602\u8d35\u7684\u6b21\u65b9\u8ba1\u7b97\n return (num / exp) % 10\n}\n\n/* \u8ba1\u6570\u6392\u5e8f\uff08\u6839\u636e nums \u7b2c k \u4f4d\u6392\u5e8f\uff09 */\nfunc countingSortDigit(nums []int, exp int) {\n // \u5341\u8fdb\u5236\u7684\u4f4d\u8303\u56f4\u4e3a 0~9 \uff0c\u56e0\u6b64\u9700\u8981\u957f\u5ea6\u4e3a 10 \u7684\u6876\u6570\u7ec4\n counter := make([]int, 10)\n n := len(nums)\n // \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n for i := 0; i < n; i++ {\n d := digit(nums[i], exp) // \u83b7\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8bb0\u4e3a d\n counter[d]++ // \u7edf\u8ba1\u6570\u5b57 d \u7684\u51fa\u73b0\u6b21\u6570\n }\n // \u6c42\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u4e2a\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u6570\u7ec4\u7d22\u5f15\u201d\n for i := 1; i < 10; i++ {\n counter[i] += counter[i-1]\n }\n // \u5012\u5e8f\u904d\u5386\uff0c\u6839\u636e\u6876\u5185\u7edf\u8ba1\u7ed3\u679c\uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165 res\n res := make([]int, n)\n for i := n - 1; i >= 0; i-- {\n d := digit(nums[i], exp)\n j := counter[d] - 1 // \u83b7\u53d6 d \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15 j\n res[j] = nums[i] // \u5c06\u5f53\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\n counter[d]-- // \u5c06 d \u7684\u6570\u91cf\u51cf 1\n }\n // \u4f7f\u7528\u7ed3\u679c\u8986\u76d6\u539f\u6570\u7ec4 nums\n for i := 0; i < n; i++ {\n nums[i] = res[i]\n }\n}\n\n/* \u57fa\u6570\u6392\u5e8f */\nfunc radixSort(nums []int) {\n // \u83b7\u53d6\u6570\u7ec4\u7684\u6700\u5927\u5143\u7d20\uff0c\u7528\u4e8e\u5224\u65ad\u6700\u5927\u4f4d\u6570\n max := math.MinInt\n for _, num := range nums {\n if num > max {\n max = num\n }\n }\n // \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\n for exp := 1; max >= exp; exp *= 10 {\n // \u5bf9\u6570\u7ec4\u5143\u7d20\u7684\u7b2c k \u4f4d\u6267\u884c\u8ba1\u6570\u6392\u5e8f\n // k = 1 -> exp = 1\n // k = 2 -> exp = 10\n // \u5373 exp = 10^(k-1)\n countingSortDigit(nums, exp)\n }\n}\n
radix_sort.swift/* \u83b7\u53d6\u5143\u7d20 num \u7684\u7b2c k \u4f4d\uff0c\u5176\u4e2d exp = 10^(k-1) */\nfunc digit(num: Int, exp: Int) -> Int {\n // \u4f20\u5165 exp \u800c\u975e k \u53ef\u4ee5\u907f\u514d\u5728\u6b64\u91cd\u590d\u6267\u884c\u6602\u8d35\u7684\u6b21\u65b9\u8ba1\u7b97\n (num / exp) % 10\n}\n\n/* \u8ba1\u6570\u6392\u5e8f\uff08\u6839\u636e nums \u7b2c k \u4f4d\u6392\u5e8f\uff09 */\nfunc countingSortDigit(nums: inout [Int], exp: Int) {\n // \u5341\u8fdb\u5236\u7684\u4f4d\u8303\u56f4\u4e3a 0~9 \uff0c\u56e0\u6b64\u9700\u8981\u957f\u5ea6\u4e3a 10 \u7684\u6876\u6570\u7ec4\n var counter = Array(repeating: 0, count: 10)\n let n = nums.count\n // \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n for i in nums.indices {\n let d = digit(num: nums[i], exp: exp) // \u83b7\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8bb0\u4e3a d\n counter[d] += 1 // \u7edf\u8ba1\u6570\u5b57 d \u7684\u51fa\u73b0\u6b21\u6570\n }\n // \u6c42\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u4e2a\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u6570\u7ec4\u7d22\u5f15\u201d\n for i in 1 ..< 10 {\n counter[i] += counter[i - 1]\n }\n // \u5012\u5e8f\u904d\u5386\uff0c\u6839\u636e\u6876\u5185\u7edf\u8ba1\u7ed3\u679c\uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165 res\n var res = Array(repeating: 0, count: n)\n for i in stride(from: n - 1, through: 0, by: -1) {\n let d = digit(num: nums[i], exp: exp)\n let j = counter[d] - 1 // \u83b7\u53d6 d \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15 j\n res[j] = nums[i] // \u5c06\u5f53\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\n counter[d] -= 1 // \u5c06 d \u7684\u6570\u91cf\u51cf 1\n }\n // \u4f7f\u7528\u7ed3\u679c\u8986\u76d6\u539f\u6570\u7ec4 nums\n for i in nums.indices {\n nums[i] = res[i]\n }\n}\n\n/* \u57fa\u6570\u6392\u5e8f */\nfunc radixSort(nums: inout [Int]) {\n // \u83b7\u53d6\u6570\u7ec4\u7684\u6700\u5927\u5143\u7d20\uff0c\u7528\u4e8e\u5224\u65ad\u6700\u5927\u4f4d\u6570\n var m = Int.min\n for num in nums {\n if num > m {\n m = num\n }\n }\n // \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\n for exp in sequence(first: 1, next: { m >= ($0 * 10) ? $0 * 10 : nil }) {\n // \u5bf9\u6570\u7ec4\u5143\u7d20\u7684\u7b2c k \u4f4d\u6267\u884c\u8ba1\u6570\u6392\u5e8f\n // k = 1 -> exp = 1\n // k = 2 -> exp = 10\n // \u5373 exp = 10^(k-1)\n countingSortDigit(nums: &nums, exp: exp)\n }\n}\n
radix_sort.js/* \u83b7\u53d6\u5143\u7d20 num \u7684\u7b2c k \u4f4d\uff0c\u5176\u4e2d exp = 10^(k-1) */\nfunction digit(num, exp) {\n // \u4f20\u5165 exp \u800c\u975e k \u53ef\u4ee5\u907f\u514d\u5728\u6b64\u91cd\u590d\u6267\u884c\u6602\u8d35\u7684\u6b21\u65b9\u8ba1\u7b97\n return Math.floor(num / exp) % 10;\n}\n\n/* \u8ba1\u6570\u6392\u5e8f\uff08\u6839\u636e nums \u7b2c k \u4f4d\u6392\u5e8f\uff09 */\nfunction countingSortDigit(nums, exp) {\n // \u5341\u8fdb\u5236\u7684\u4f4d\u8303\u56f4\u4e3a 0~9 \uff0c\u56e0\u6b64\u9700\u8981\u957f\u5ea6\u4e3a 10 \u7684\u6876\u6570\u7ec4\n const counter = new Array(10).fill(0);\n const n = nums.length;\n // \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n for (let i = 0; i < n; i++) {\n const d = digit(nums[i], exp); // \u83b7\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8bb0\u4e3a d\n counter[d]++; // \u7edf\u8ba1\u6570\u5b57 d \u7684\u51fa\u73b0\u6b21\u6570\n }\n // \u6c42\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u4e2a\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u6570\u7ec4\u7d22\u5f15\u201d\n for (let i = 1; i < 10; i++) {\n counter[i] += counter[i - 1];\n }\n // \u5012\u5e8f\u904d\u5386\uff0c\u6839\u636e\u6876\u5185\u7edf\u8ba1\u7ed3\u679c\uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165 res\n const res = new Array(n).fill(0);\n for (let i = n - 1; i >= 0; i--) {\n const d = digit(nums[i], exp);\n const j = counter[d] - 1; // \u83b7\u53d6 d \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15 j\n res[j] = nums[i]; // \u5c06\u5f53\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\n counter[d]--; // \u5c06 d \u7684\u6570\u91cf\u51cf 1\n }\n // \u4f7f\u7528\u7ed3\u679c\u8986\u76d6\u539f\u6570\u7ec4 nums\n for (let i = 0; i < n; i++) {\n nums[i] = res[i];\n }\n}\n\n/* \u57fa\u6570\u6392\u5e8f */\nfunction radixSort(nums) {\n // \u83b7\u53d6\u6570\u7ec4\u7684\u6700\u5927\u5143\u7d20\uff0c\u7528\u4e8e\u5224\u65ad\u6700\u5927\u4f4d\u6570\n let m = Number.MIN_VALUE;\n for (const num of nums) {\n if (num > m) {\n m = num;\n }\n }\n // \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\n for (let exp = 1; exp <= m; exp *= 10) {\n // \u5bf9\u6570\u7ec4\u5143\u7d20\u7684\u7b2c k \u4f4d\u6267\u884c\u8ba1\u6570\u6392\u5e8f\n // k = 1 -> exp = 1\n // k = 2 -> exp = 10\n // \u5373 exp = 10^(k-1)\n countingSortDigit(nums, exp);\n }\n}\n
radix_sort.ts/* \u83b7\u53d6\u5143\u7d20 num \u7684\u7b2c k \u4f4d\uff0c\u5176\u4e2d exp = 10^(k-1) */\nfunction digit(num: number, exp: number): number {\n // \u4f20\u5165 exp \u800c\u975e k \u53ef\u4ee5\u907f\u514d\u5728\u6b64\u91cd\u590d\u6267\u884c\u6602\u8d35\u7684\u6b21\u65b9\u8ba1\u7b97\n return Math.floor(num / exp) % 10;\n}\n\n/* \u8ba1\u6570\u6392\u5e8f\uff08\u6839\u636e nums \u7b2c k \u4f4d\u6392\u5e8f\uff09 */\nfunction countingSortDigit(nums: number[], exp: number): void {\n // \u5341\u8fdb\u5236\u7684\u4f4d\u8303\u56f4\u4e3a 0~9 \uff0c\u56e0\u6b64\u9700\u8981\u957f\u5ea6\u4e3a 10 \u7684\u6876\u6570\u7ec4\n const counter = new Array(10).fill(0);\n const n = nums.length;\n // \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n for (let i = 0; i < n; i++) {\n const d = digit(nums[i], exp); // \u83b7\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8bb0\u4e3a d\n counter[d]++; // \u7edf\u8ba1\u6570\u5b57 d \u7684\u51fa\u73b0\u6b21\u6570\n }\n // \u6c42\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u4e2a\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u6570\u7ec4\u7d22\u5f15\u201d\n for (let i = 1; i < 10; i++) {\n counter[i] += counter[i - 1];\n }\n // \u5012\u5e8f\u904d\u5386\uff0c\u6839\u636e\u6876\u5185\u7edf\u8ba1\u7ed3\u679c\uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165 res\n const res = new Array(n).fill(0);\n for (let i = n - 1; i >= 0; i--) {\n const d = digit(nums[i], exp);\n const j = counter[d] - 1; // \u83b7\u53d6 d \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15 j\n res[j] = nums[i]; // \u5c06\u5f53\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\n counter[d]--; // \u5c06 d \u7684\u6570\u91cf\u51cf 1\n }\n // \u4f7f\u7528\u7ed3\u679c\u8986\u76d6\u539f\u6570\u7ec4 nums\n for (let i = 0; i < n; i++) {\n nums[i] = res[i];\n }\n}\n\n/* \u57fa\u6570\u6392\u5e8f */\nfunction radixSort(nums: number[]): void {\n // \u83b7\u53d6\u6570\u7ec4\u7684\u6700\u5927\u5143\u7d20\uff0c\u7528\u4e8e\u5224\u65ad\u6700\u5927\u4f4d\u6570\n let m = Number.MIN_VALUE;\n for (const num of nums) {\n if (num > m) {\n m = num;\n }\n }\n // \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\n for (let exp = 1; exp <= m; exp *= 10) {\n // \u5bf9\u6570\u7ec4\u5143\u7d20\u7684\u7b2c k \u4f4d\u6267\u884c\u8ba1\u6570\u6392\u5e8f\n // k = 1 -> exp = 1\n // k = 2 -> exp = 10\n // \u5373 exp = 10^(k-1)\n countingSortDigit(nums, exp);\n }\n}\n
radix_sort.dart/* \u83b7\u53d6\u5143\u7d20 _num \u7684\u7b2c k \u4f4d\uff0c\u5176\u4e2d exp = 10^(k-1) */\nint digit(int _num, int exp) {\n // \u4f20\u5165 exp \u800c\u975e k \u53ef\u4ee5\u907f\u514d\u5728\u6b64\u91cd\u590d\u6267\u884c\u6602\u8d35\u7684\u6b21\u65b9\u8ba1\u7b97\n return (_num ~/ exp) % 10;\n}\n\n/* \u8ba1\u6570\u6392\u5e8f\uff08\u6839\u636e nums \u7b2c k \u4f4d\u6392\u5e8f\uff09 */\nvoid countingSortDigit(List<int> nums, int exp) {\n // \u5341\u8fdb\u5236\u7684\u4f4d\u8303\u56f4\u4e3a 0~9 \uff0c\u56e0\u6b64\u9700\u8981\u957f\u5ea6\u4e3a 10 \u7684\u6876\u6570\u7ec4\n List<int> counter = List<int>.filled(10, 0);\n int n = nums.length;\n // \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n for (int i = 0; i < n; i++) {\n int d = digit(nums[i], exp); // \u83b7\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8bb0\u4e3a d\n counter[d]++; // \u7edf\u8ba1\u6570\u5b57 d \u7684\u51fa\u73b0\u6b21\u6570\n }\n // \u6c42\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u4e2a\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u6570\u7ec4\u7d22\u5f15\u201d\n for (int i = 1; i < 10; i++) {\n counter[i] += counter[i - 1];\n }\n // \u5012\u5e8f\u904d\u5386\uff0c\u6839\u636e\u6876\u5185\u7edf\u8ba1\u7ed3\u679c\uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165 res\n List<int> res = List<int>.filled(n, 0);\n for (int i = n - 1; i >= 0; i--) {\n int d = digit(nums[i], exp);\n int j = counter[d] - 1; // \u83b7\u53d6 d \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15 j\n res[j] = nums[i]; // \u5c06\u5f53\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\n counter[d]--; // \u5c06 d \u7684\u6570\u91cf\u51cf 1\n }\n // \u4f7f\u7528\u7ed3\u679c\u8986\u76d6\u539f\u6570\u7ec4 nums\n for (int i = 0; i < n; i++) nums[i] = res[i];\n}\n\n/* \u57fa\u6570\u6392\u5e8f */\nvoid radixSort(List<int> nums) {\n // \u83b7\u53d6\u6570\u7ec4\u7684\u6700\u5927\u5143\u7d20\uff0c\u7528\u4e8e\u5224\u65ad\u6700\u5927\u4f4d\u6570\n // dart \u4e2d int \u7684\u957f\u5ea6\u662f 64 \u4f4d\u7684\n int m = -1 << 63;\n for (int _num in nums) if (_num > m) m = _num;\n // \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\n for (int exp = 1; exp <= m; exp *= 10)\n // \u5bf9\u6570\u7ec4\u5143\u7d20\u7684\u7b2c k \u4f4d\u6267\u884c\u8ba1\u6570\u6392\u5e8f\n // k = 1 -> exp = 1\n // k = 2 -> exp = 10\n // \u5373 exp = 10^(k-1)\n countingSortDigit(nums, exp);\n}\n
radix_sort.rs/* \u83b7\u53d6\u5143\u7d20 num \u7684\u7b2c k \u4f4d\uff0c\u5176\u4e2d exp = 10^(k-1) */\nfn digit(num: i32, exp: i32) -> usize {\n // \u4f20\u5165 exp \u800c\u975e k \u53ef\u4ee5\u907f\u514d\u5728\u6b64\u91cd\u590d\u6267\u884c\u6602\u8d35\u7684\u6b21\u65b9\u8ba1\u7b97\n return ((num / exp) % 10) as usize;\n}\n\n/* \u8ba1\u6570\u6392\u5e8f\uff08\u6839\u636e nums \u7b2c k \u4f4d\u6392\u5e8f\uff09 */\nfn counting_sort_digit(nums: &mut [i32], exp: i32) {\n // \u5341\u8fdb\u5236\u7684\u4f4d\u8303\u56f4\u4e3a 0~9 \uff0c\u56e0\u6b64\u9700\u8981\u957f\u5ea6\u4e3a 10 \u7684\u6876\u6570\u7ec4\n let mut counter = [0; 10];\n let n = nums.len();\n // \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n for i in 0..n {\n let d = digit(nums[i], exp); // \u83b7\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8bb0\u4e3a d\n counter[d] += 1; // \u7edf\u8ba1\u6570\u5b57 d \u7684\u51fa\u73b0\u6b21\u6570\n }\n // \u6c42\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u4e2a\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u6570\u7ec4\u7d22\u5f15\u201d\n for i in 1..10 {\n counter[i] += counter[i - 1];\n }\n // \u5012\u5e8f\u904d\u5386\uff0c\u6839\u636e\u6876\u5185\u7edf\u8ba1\u7ed3\u679c\uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165 res\n let mut res = vec![0; n];\n for i in (0..n).rev() {\n let d = digit(nums[i], exp);\n let j = counter[d] - 1; // \u83b7\u53d6 d \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15 j\n res[j] = nums[i]; // \u5c06\u5f53\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\n counter[d] -= 1; // \u5c06 d \u7684\u6570\u91cf\u51cf 1\n }\n // \u4f7f\u7528\u7ed3\u679c\u8986\u76d6\u539f\u6570\u7ec4 nums\n for i in 0..n {\n nums[i] = res[i];\n }\n}\n\n/* \u57fa\u6570\u6392\u5e8f */\nfn radix_sort(nums: &mut [i32]) {\n // \u83b7\u53d6\u6570\u7ec4\u7684\u6700\u5927\u5143\u7d20\uff0c\u7528\u4e8e\u5224\u65ad\u6700\u5927\u4f4d\u6570\n let m = *nums.into_iter().max().unwrap();\n // \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\n let mut exp = 1;\n while exp <= m {\n counting_sort_digit(nums, exp);\n exp *= 10;\n }\n}\n
radix_sort.c/* \u83b7\u53d6\u5143\u7d20 num \u7684\u7b2c k \u4f4d\uff0c\u5176\u4e2d exp = 10^(k-1) */\nint digit(int num, int exp) {\n // \u4f20\u5165 exp \u800c\u975e k \u53ef\u4ee5\u907f\u514d\u5728\u6b64\u91cd\u590d\u6267\u884c\u6602\u8d35\u7684\u6b21\u65b9\u8ba1\u7b97\n return (num / exp) % 10;\n}\n\n/* \u8ba1\u6570\u6392\u5e8f\uff08\u6839\u636e nums \u7b2c k \u4f4d\u6392\u5e8f\uff09 */\nvoid countingSortDigit(int nums[], int size, int exp) {\n // \u5341\u8fdb\u5236\u7684\u4f4d\u8303\u56f4\u4e3a 0~9 \uff0c\u56e0\u6b64\u9700\u8981\u957f\u5ea6\u4e3a 10 \u7684\u6876\u6570\u7ec4\n int *counter = (int *)malloc((sizeof(int) * 10));\n // \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n for (int i = 0; i < size; i++) {\n // \u83b7\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8bb0\u4e3a d\n int d = digit(nums[i], exp);\n // \u7edf\u8ba1\u6570\u5b57 d \u7684\u51fa\u73b0\u6b21\u6570\n counter[d]++;\n }\n // \u6c42\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u4e2a\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u6570\u7ec4\u7d22\u5f15\u201d\n for (int i = 1; i < 10; i++) {\n counter[i] += counter[i - 1];\n }\n // \u5012\u5e8f\u904d\u5386\uff0c\u6839\u636e\u6876\u5185\u7edf\u8ba1\u7ed3\u679c\uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165 res\n int *res = (int *)malloc(sizeof(int) * size);\n for (int i = size - 1; i >= 0; i--) {\n int d = digit(nums[i], exp);\n int j = counter[d] - 1; // \u83b7\u53d6 d \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15 j\n res[j] = nums[i]; // \u5c06\u5f53\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\n counter[d]--; // \u5c06 d \u7684\u6570\u91cf\u51cf 1\n }\n // \u4f7f\u7528\u7ed3\u679c\u8986\u76d6\u539f\u6570\u7ec4 nums\n for (int i = 0; i < size; i++) {\n nums[i] = res[i];\n }\n}\n\n/* \u57fa\u6570\u6392\u5e8f */\nvoid radixSort(int nums[], int size) {\n // \u83b7\u53d6\u6570\u7ec4\u7684\u6700\u5927\u5143\u7d20\uff0c\u7528\u4e8e\u5224\u65ad\u6700\u5927\u4f4d\u6570\n int max = INT32_MIN;\n for (size_t i = 0; i < size - 1; i++) {\n if (nums[i] > max) {\n max = nums[i];\n }\n }\n // \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\n for (int exp = 1; max >= exp; exp *= 10)\n // \u5bf9\u6570\u7ec4\u5143\u7d20\u7684\u7b2c k \u4f4d\u6267\u884c\u8ba1\u6570\u6392\u5e8f\n // k = 1 -> exp = 1\n // k = 2 -> exp = 10\n // \u5373 exp = 10^(k-1)\n countingSortDigit(nums, size, exp);\n}\n
radix_sort.zig// \u83b7\u53d6\u5143\u7d20 num \u7684\u7b2c k \u4f4d\uff0c\u5176\u4e2d exp = 10^(k-1)\nfn digit(num: i32, exp: i32) i32 {\n // \u4f20\u5165 exp \u800c\u975e k \u53ef\u4ee5\u907f\u514d\u5728\u6b64\u91cd\u590d\u6267\u884c\u6602\u8d35\u7684\u6b21\u65b9\u8ba1\u7b97\n return @mod(@divFloor(num, exp), 10);\n}\n\n// \u8ba1\u6570\u6392\u5e8f\uff08\u6839\u636e nums \u7b2c k \u4f4d\u6392\u5e8f\uff09\nfn countingSortDigit(nums: []i32, exp: i32) !void {\n // \u5341\u8fdb\u5236\u7684\u4f4d\u8303\u56f4\u4e3a 0~9 \uff0c\u56e0\u6b64\u9700\u8981\u957f\u5ea6\u4e3a 10 \u7684\u6876\u6570\u7ec4\n var mem_arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);\n // defer mem_arena.deinit();\n const mem_allocator = mem_arena.allocator();\n var counter = try mem_allocator.alloc(usize, 10);\n @memset(counter, 0);\n var n = nums.len;\n // \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n for (nums) |num| {\n var d: u32 = @bitCast(digit(num, exp)); // \u83b7\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8bb0\u4e3a d\n counter[d] += 1; // \u7edf\u8ba1\u6570\u5b57 d \u7684\u51fa\u73b0\u6b21\u6570\n }\n // \u6c42\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u4e2a\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u6570\u7ec4\u7d22\u5f15\u201d\n var i: usize = 1;\n while (i < 10) : (i += 1) {\n counter[i] += counter[i - 1];\n }\n // \u5012\u5e8f\u904d\u5386\uff0c\u6839\u636e\u6876\u5185\u7edf\u8ba1\u7ed3\u679c\uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165 res\n var res = try mem_allocator.alloc(i32, n);\n i = n - 1;\n while (i >= 0) : (i -= 1) {\n var d: u32 = @bitCast(digit(nums[i], exp));\n var j = counter[d] - 1; // \u83b7\u53d6 d \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15 j\n res[j] = nums[i]; // \u5c06\u5f53\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\n counter[d] -= 1; // \u5c06 d \u7684\u6570\u91cf\u51cf 1\n if (i == 0) break;\n }\n // \u4f7f\u7528\u7ed3\u679c\u8986\u76d6\u539f\u6570\u7ec4 nums\n i = 0;\n while (i < n) : (i += 1) {\n nums[i] = res[i];\n }\n}\n\n// \u57fa\u6570\u6392\u5e8f\nfn radixSort(nums: []i32) !void {\n // \u83b7\u53d6\u6570\u7ec4\u7684\u6700\u5927\u5143\u7d20\uff0c\u7528\u4e8e\u5224\u65ad\u6700\u5927\u4f4d\u6570\n var m: i32 = std.math.minInt(i32);\n for (nums) |num| {\n if (num > m) m = num;\n }\n // \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\n var exp: i32 = 1;\n while (exp <= m) : (exp *= 10) {\n // \u5bf9\u6570\u7ec4\u5143\u7d20\u7684\u7b2c k \u4f4d\u6267\u884c\u8ba1\u6570\u6392\u5e8f\n // k = 1 -> exp = 1\n // k = 2 -> exp = 10\n // \u5373 exp = 10^(k-1)\n try countingSortDigit(nums, exp); \n }\n} \n
\u4e3a\u4ec0\u4e48\u4ece\u6700\u4f4e\u4f4d\u5f00\u59cb\u6392\u5e8f\uff1f
\u5728\u8fde\u7eed\u7684\u6392\u5e8f\u8f6e\u6b21\u4e2d\uff0c\u540e\u4e00\u8f6e\u6392\u5e8f\u4f1a\u8986\u76d6\u524d\u4e00\u8f6e\u6392\u5e8f\u7684\u7ed3\u679c\u3002\u4e3e\u4f8b\u6765\u8bf4\uff0c\u5982\u679c\u7b2c\u4e00\u8f6e\u6392\u5e8f\u7ed3\u679c \\(a < b\\) \uff0c\u800c\u7b2c\u4e8c\u8f6e\u6392\u5e8f\u7ed3\u679c \\(a > b\\) \uff0c\u90a3\u4e48\u7b2c\u4e8c\u8f6e\u7684\u7ed3\u679c\u5c06\u53d6\u4ee3\u7b2c\u4e00\u8f6e\u7684\u7ed3\u679c\u3002\u7531\u4e8e\u6570\u5b57\u7684\u9ad8\u4f4d\u4f18\u5148\u7ea7\u9ad8\u4e8e\u4f4e\u4f4d\uff0c\u56e0\u6b64\u5e94\u8be5\u5148\u6392\u5e8f\u4f4e\u4f4d\u518d\u6392\u5e8f\u9ad8\u4f4d\u3002
"},{"location":"chapter_sorting/radix_sort/#11102","title":"11.10.2 \u00a0 \u7b97\u6cd5\u7279\u6027","text":"\u76f8\u8f83\u4e8e\u8ba1\u6570\u6392\u5e8f\uff0c\u57fa\u6570\u6392\u5e8f\u9002\u7528\u4e8e\u6570\u503c\u8303\u56f4\u8f83\u5927\u7684\u60c5\u51b5\uff0c\u4f46\u524d\u63d0\u662f\u6570\u636e\u5fc5\u987b\u53ef\u4ee5\u8868\u793a\u4e3a\u56fa\u5b9a\u4f4d\u6570\u7684\u683c\u5f0f\uff0c\u4e14\u4f4d\u6570\u4e0d\u80fd\u8fc7\u5927\u3002\u4f8b\u5982\uff0c\u6d6e\u70b9\u6570\u4e0d\u9002\u5408\u4f7f\u7528\u57fa\u6570\u6392\u5e8f\uff0c\u56e0\u4e3a\u5176\u4f4d\u6570 \\(k\\) \u8fc7\u5927\uff0c\u53ef\u80fd\u5bfc\u81f4\u65f6\u95f4\u590d\u6742\u5ea6 \\(O(nk) \\gg O(n^2)\\) \u3002
res
\u548c counter
\u3002\u300c\u9009\u62e9\u6392\u5e8f selection sort\u300d\u7684\u5de5\u4f5c\u539f\u7406\u975e\u5e38\u7b80\u5355\uff1a\u5f00\u542f\u4e00\u4e2a\u5faa\u73af\uff0c\u6bcf\u8f6e\u4ece\u672a\u6392\u5e8f\u533a\u95f4\u9009\u62e9\u6700\u5c0f\u7684\u5143\u7d20\uff0c\u5c06\u5176\u653e\u5230\u5df2\u6392\u5e8f\u533a\u95f4\u7684\u672b\u5c3e\u3002
\u8bbe\u6570\u7ec4\u7684\u957f\u5ea6\u4e3a \\(n\\) \uff0c\u9009\u62e9\u6392\u5e8f\u7684\u7b97\u6cd5\u6d41\u7a0b\u5982\u56fe 11-2 \u6240\u793a\u3002
\u56fe 11-2 \u00a0 \u9009\u62e9\u6392\u5e8f\u6b65\u9aa4
\u5728\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u7528 \\(k\\) \u6765\u8bb0\u5f55\u672a\u6392\u5e8f\u533a\u95f4\u5185\u7684\u6700\u5c0f\u5143\u7d20\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig selection_sort.pydef selection_sort(nums: list[int]):\n \"\"\"\u9009\u62e9\u6392\u5e8f\"\"\"\n n = len(nums)\n # \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [i, n-1]\n for i in range(n - 1):\n # \u5185\u5faa\u73af\uff1a\u627e\u5230\u672a\u6392\u5e8f\u533a\u95f4\u5185\u7684\u6700\u5c0f\u5143\u7d20\n k = i\n for j in range(i + 1, n):\n if nums[j] < nums[k]:\n k = j # \u8bb0\u5f55\u6700\u5c0f\u5143\u7d20\u7684\u7d22\u5f15\n # \u5c06\u8be5\u6700\u5c0f\u5143\u7d20\u4e0e\u672a\u6392\u5e8f\u533a\u95f4\u7684\u9996\u4e2a\u5143\u7d20\u4ea4\u6362\n nums[i], nums[k] = nums[k], nums[i]\n
selection_sort.cpp/* \u9009\u62e9\u6392\u5e8f */\nvoid selectionSort(vector<int> &nums) {\n int n = nums.size();\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [i, n-1]\n for (int i = 0; i < n - 1; i++) {\n // \u5185\u5faa\u73af\uff1a\u627e\u5230\u672a\u6392\u5e8f\u533a\u95f4\u5185\u7684\u6700\u5c0f\u5143\u7d20\n int k = i;\n for (int j = i + 1; j < n; j++) {\n if (nums[j] < nums[k])\n k = j; // \u8bb0\u5f55\u6700\u5c0f\u5143\u7d20\u7684\u7d22\u5f15\n }\n // \u5c06\u8be5\u6700\u5c0f\u5143\u7d20\u4e0e\u672a\u6392\u5e8f\u533a\u95f4\u7684\u9996\u4e2a\u5143\u7d20\u4ea4\u6362\n swap(nums[i], nums[k]);\n }\n}\n
selection_sort.java/* \u9009\u62e9\u6392\u5e8f */\nvoid selectionSort(int[] nums) {\n int n = nums.length;\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [i, n-1]\n for (int i = 0; i < n - 1; i++) {\n // \u5185\u5faa\u73af\uff1a\u627e\u5230\u672a\u6392\u5e8f\u533a\u95f4\u5185\u7684\u6700\u5c0f\u5143\u7d20\n int k = i;\n for (int j = i + 1; j < n; j++) {\n if (nums[j] < nums[k])\n k = j; // \u8bb0\u5f55\u6700\u5c0f\u5143\u7d20\u7684\u7d22\u5f15\n }\n // \u5c06\u8be5\u6700\u5c0f\u5143\u7d20\u4e0e\u672a\u6392\u5e8f\u533a\u95f4\u7684\u9996\u4e2a\u5143\u7d20\u4ea4\u6362\n int temp = nums[i];\n nums[i] = nums[k];\n nums[k] = temp;\n }\n}\n
selection_sort.cs/* \u9009\u62e9\u6392\u5e8f */\nvoid SelectionSort(int[] nums) {\n int n = nums.Length;\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [i, n-1]\n for (int i = 0; i < n - 1; i++) {\n // \u5185\u5faa\u73af\uff1a\u627e\u5230\u672a\u6392\u5e8f\u533a\u95f4\u5185\u7684\u6700\u5c0f\u5143\u7d20\n int k = i;\n for (int j = i + 1; j < n; j++) {\n if (nums[j] < nums[k])\n k = j; // \u8bb0\u5f55\u6700\u5c0f\u5143\u7d20\u7684\u7d22\u5f15\n }\n // \u5c06\u8be5\u6700\u5c0f\u5143\u7d20\u4e0e\u672a\u6392\u5e8f\u533a\u95f4\u7684\u9996\u4e2a\u5143\u7d20\u4ea4\u6362\n (nums[k], nums[i]) = (nums[i], nums[k]);\n }\n}\n
selection_sort.go/* \u9009\u62e9\u6392\u5e8f */\nfunc selectionSort(nums []int) {\n n := len(nums)\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [i, n-1]\n for i := 0; i < n-1; i++ {\n // \u5185\u5faa\u73af\uff1a\u627e\u5230\u672a\u6392\u5e8f\u533a\u95f4\u5185\u7684\u6700\u5c0f\u5143\u7d20\n k := i\n for j := i + 1; j < n; j++ {\n if nums[j] < nums[k] {\n // \u8bb0\u5f55\u6700\u5c0f\u5143\u7d20\u7684\u7d22\u5f15\n k = j\n }\n }\n // \u5c06\u8be5\u6700\u5c0f\u5143\u7d20\u4e0e\u672a\u6392\u5e8f\u533a\u95f4\u7684\u9996\u4e2a\u5143\u7d20\u4ea4\u6362\n nums[i], nums[k] = nums[k], nums[i]\n\n }\n}\n
selection_sort.swift/* \u9009\u62e9\u6392\u5e8f */\nfunc selectionSort(nums: inout [Int]) {\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [i, n-1]\n for i in nums.indices.dropLast() {\n // \u5185\u5faa\u73af\uff1a\u627e\u5230\u672a\u6392\u5e8f\u533a\u95f4\u5185\u7684\u6700\u5c0f\u5143\u7d20\n var k = i\n for j in nums.indices.dropFirst(i + 1) {\n if nums[j] < nums[k] {\n k = j // \u8bb0\u5f55\u6700\u5c0f\u5143\u7d20\u7684\u7d22\u5f15\n }\n }\n // \u5c06\u8be5\u6700\u5c0f\u5143\u7d20\u4e0e\u672a\u6392\u5e8f\u533a\u95f4\u7684\u9996\u4e2a\u5143\u7d20\u4ea4\u6362\n nums.swapAt(i, k)\n }\n}\n
selection_sort.js/* \u9009\u62e9\u6392\u5e8f */\nfunction selectionSort(nums) {\n let n = nums.length;\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [i, n-1]\n for (let i = 0; i < n - 1; i++) {\n // \u5185\u5faa\u73af\uff1a\u627e\u5230\u672a\u6392\u5e8f\u533a\u95f4\u5185\u7684\u6700\u5c0f\u5143\u7d20\n let k = i;\n for (let j = i + 1; j < n; j++) {\n if (nums[j] < nums[k]) {\n k = j; // \u8bb0\u5f55\u6700\u5c0f\u5143\u7d20\u7684\u7d22\u5f15\n }\n }\n // \u5c06\u8be5\u6700\u5c0f\u5143\u7d20\u4e0e\u672a\u6392\u5e8f\u533a\u95f4\u7684\u9996\u4e2a\u5143\u7d20\u4ea4\u6362\n [nums[i], nums[k]] = [nums[k], nums[i]];\n }\n}\n
selection_sort.ts/* \u9009\u62e9\u6392\u5e8f */\nfunction selectionSort(nums: number[]): void {\n let n = nums.length;\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [i, n-1]\n for (let i = 0; i < n - 1; i++) {\n // \u5185\u5faa\u73af\uff1a\u627e\u5230\u672a\u6392\u5e8f\u533a\u95f4\u5185\u7684\u6700\u5c0f\u5143\u7d20\n let k = i;\n for (let j = i + 1; j < n; j++) {\n if (nums[j] < nums[k]) {\n k = j; // \u8bb0\u5f55\u6700\u5c0f\u5143\u7d20\u7684\u7d22\u5f15\n }\n }\n // \u5c06\u8be5\u6700\u5c0f\u5143\u7d20\u4e0e\u672a\u6392\u5e8f\u533a\u95f4\u7684\u9996\u4e2a\u5143\u7d20\u4ea4\u6362\n [nums[i], nums[k]] = [nums[k], nums[i]];\n }\n}\n
selection_sort.dart/* \u9009\u62e9\u6392\u5e8f */\nvoid selectionSort(List<int> nums) {\n int n = nums.length;\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [i, n-1]\n for (int i = 0; i < n - 1; i++) {\n // \u5185\u5faa\u73af\uff1a\u627e\u5230\u672a\u6392\u5e8f\u533a\u95f4\u5185\u7684\u6700\u5c0f\u5143\u7d20\n int k = i;\n for (int j = i + 1; j < n; j++) {\n if (nums[j] < nums[k]) k = j; // \u8bb0\u5f55\u6700\u5c0f\u5143\u7d20\u7684\u7d22\u5f15\n }\n // \u5c06\u8be5\u6700\u5c0f\u5143\u7d20\u4e0e\u672a\u6392\u5e8f\u533a\u95f4\u7684\u9996\u4e2a\u5143\u7d20\u4ea4\u6362\n int temp = nums[i];\n nums[i] = nums[k];\n nums[k] = temp;\n }\n}\n
selection_sort.rs/* \u9009\u62e9\u6392\u5e8f */\nfn selection_sort(nums: &mut [i32]) {\n let n = nums.len();\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [i, n-1]\n for i in 0..n-1 {\n // \u5185\u5faa\u73af\uff1a\u627e\u5230\u672a\u6392\u5e8f\u533a\u95f4\u5185\u7684\u6700\u5c0f\u5143\u7d20\n let mut k = i;\n for j in i+1..n {\n if nums[j] < nums[k] {\n k = j; // \u8bb0\u5f55\u6700\u5c0f\u5143\u7d20\u7684\u7d22\u5f15\n }\n }\n // \u5c06\u8be5\u6700\u5c0f\u5143\u7d20\u4e0e\u672a\u6392\u5e8f\u533a\u95f4\u7684\u9996\u4e2a\u5143\u7d20\u4ea4\u6362\n nums.swap(i, k);\n }\n}\n
selection_sort.c/* \u9009\u62e9\u6392\u5e8f */\nvoid selectionSort(int nums[], int n) {\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [i, n-1]\n for (int i = 0; i < n - 1; i++) {\n // \u5185\u5faa\u73af\uff1a\u627e\u5230\u672a\u6392\u5e8f\u533a\u95f4\u5185\u7684\u6700\u5c0f\u5143\u7d20\n int k = i;\n for (int j = i + 1; j < n; j++) {\n if (nums[j] < nums[k])\n k = j; // \u8bb0\u5f55\u6700\u5c0f\u5143\u7d20\u7684\u7d22\u5f15\n }\n // \u5c06\u8be5\u6700\u5c0f\u5143\u7d20\u4e0e\u672a\u6392\u5e8f\u533a\u95f4\u7684\u9996\u4e2a\u5143\u7d20\u4ea4\u6362\n int temp = nums[i];\n nums[i] = nums[k];\n nums[k] = temp;\n }\n}\n
selection_sort.zig[class]{}-[func]{selectionSort}\n
"},{"location":"chapter_sorting/selection_sort/#1121","title":"11.2.1 \u00a0 \u7b97\u6cd5\u7279\u6027","text":"nums[i]
\u6709\u53ef\u80fd\u88ab\u4ea4\u6362\u81f3\u4e0e\u5176\u76f8\u7b49\u7684\u5143\u7d20\u7684\u53f3\u8fb9\uff0c\u5bfc\u81f4\u4e24\u8005\u7684\u76f8\u5bf9\u987a\u5e8f\u53d1\u751f\u6539\u53d8\u3002\u56fe 11-3 \u00a0 \u9009\u62e9\u6392\u5e8f\u975e\u7a33\u5b9a\u793a\u4f8b
"},{"location":"chapter_sorting/sorting_algorithm/","title":"11.1 \u00a0 \u6392\u5e8f\u7b97\u6cd5","text":"\u300c\u6392\u5e8f\u7b97\u6cd5 sorting algorithm\u300d\u7528\u4e8e\u5bf9\u4e00\u7ec4\u6570\u636e\u6309\u7167\u7279\u5b9a\u987a\u5e8f\u8fdb\u884c\u6392\u5217\u3002\u6392\u5e8f\u7b97\u6cd5\u6709\u7740\u5e7f\u6cdb\u7684\u5e94\u7528\uff0c\u56e0\u4e3a\u6709\u5e8f\u6570\u636e\u901a\u5e38\u80fd\u591f\u88ab\u66f4\u9ad8\u6548\u5730\u67e5\u627e\u3001\u5206\u6790\u548c\u5904\u7406\u3002
\u5982\u56fe 11-1 \u6240\u793a\uff0c\u6392\u5e8f\u7b97\u6cd5\u4e2d\u7684\u6570\u636e\u7c7b\u578b\u53ef\u4ee5\u662f\u6574\u6570\u3001\u6d6e\u70b9\u6570\u3001\u5b57\u7b26\u6216\u5b57\u7b26\u4e32\u7b49\u3002\u6392\u5e8f\u7684\u5224\u65ad\u89c4\u5219\u53ef\u6839\u636e\u9700\u6c42\u8bbe\u5b9a\uff0c\u5982\u6570\u5b57\u5927\u5c0f\u3001\u5b57\u7b26 ASCII \u7801\u987a\u5e8f\u6216\u81ea\u5b9a\u4e49\u89c4\u5219\u3002
\u56fe 11-1 \u00a0 \u6570\u636e\u7c7b\u578b\u548c\u5224\u65ad\u89c4\u5219\u793a\u4f8b
"},{"location":"chapter_sorting/sorting_algorithm/#1111","title":"11.1.1 \u00a0 \u8bc4\u4ef7\u7ef4\u5ea6","text":"\u8fd0\u884c\u6548\u7387\uff1a\u6211\u4eec\u671f\u671b\u6392\u5e8f\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u5c3d\u91cf\u4f4e\uff0c\u4e14\u603b\u4f53\u64cd\u4f5c\u6570\u91cf\u8f83\u5c11\uff08\u65f6\u95f4\u590d\u6742\u5ea6\u4e2d\u7684\u5e38\u6570\u9879\u53d8\u5c0f\uff09\u3002\u5bf9\u4e8e\u5927\u6570\u636e\u91cf\u7684\u60c5\u51b5\uff0c\u8fd0\u884c\u6548\u7387\u663e\u5f97\u5c24\u4e3a\u91cd\u8981\u3002
\u5c31\u5730\u6027\uff1a\u987e\u540d\u601d\u4e49\uff0c\u300c\u539f\u5730\u6392\u5e8f\u300d\u901a\u8fc7\u5728\u539f\u6570\u7ec4\u4e0a\u76f4\u63a5\u64cd\u4f5c\u5b9e\u73b0\u6392\u5e8f\uff0c\u65e0\u987b\u501f\u52a9\u989d\u5916\u7684\u8f85\u52a9\u6570\u7ec4\uff0c\u4ece\u800c\u8282\u7701\u5185\u5b58\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u539f\u5730\u6392\u5e8f\u7684\u6570\u636e\u642c\u8fd0\u64cd\u4f5c\u8f83\u5c11\uff0c\u8fd0\u884c\u901f\u5ea6\u4e5f\u66f4\u5feb\u3002
\u7a33\u5b9a\u6027\uff1a\u300c\u7a33\u5b9a\u6392\u5e8f\u300d\u5728\u5b8c\u6210\u6392\u5e8f\u540e\uff0c\u76f8\u7b49\u5143\u7d20\u5728\u6570\u7ec4\u4e2d\u7684\u76f8\u5bf9\u987a\u5e8f\u4e0d\u53d1\u751f\u6539\u53d8\u3002
\u7a33\u5b9a\u6392\u5e8f\u662f\u591a\u7ea7\u6392\u5e8f\u573a\u666f\u7684\u5fc5\u8981\u6761\u4ef6\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5b58\u50a8\u5b66\u751f\u4fe1\u606f\u7684\u8868\u683c\uff0c\u7b2c 1 \u5217\u548c\u7b2c 2 \u5217\u5206\u522b\u662f\u59d3\u540d\u548c\u5e74\u9f84\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u300c\u975e\u7a33\u5b9a\u6392\u5e8f\u300d\u53ef\u80fd\u5bfc\u81f4\u8f93\u5165\u6570\u636e\u7684\u6709\u5e8f\u6027\u4e27\u5931\uff1a
# \u8f93\u5165\u6570\u636e\u662f\u6309\u7167\u59d3\u540d\u6392\u5e8f\u597d\u7684\n# (name, age)\n ('A', 19)\n ('B', 18)\n ('C', 21)\n ('D', 19)\n ('E', 23)\n\n# \u5047\u8bbe\u4f7f\u7528\u975e\u7a33\u5b9a\u6392\u5e8f\u7b97\u6cd5\u6309\u5e74\u9f84\u6392\u5e8f\u5217\u8868\uff0c\n# \u7ed3\u679c\u4e2d ('D', 19) \u548c ('A', 19) \u7684\u76f8\u5bf9\u4f4d\u7f6e\u6539\u53d8\uff0c\n# \u8f93\u5165\u6570\u636e\u6309\u59d3\u540d\u6392\u5e8f\u7684\u6027\u8d28\u4e22\u5931\n ('B', 18)\n ('D', 19)\n ('A', 19)\n ('C', 21)\n ('E', 23)\n
\u81ea\u9002\u5e94\u6027\uff1a\u300c\u81ea\u9002\u5e94\u6392\u5e8f\u300d\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4f1a\u53d7\u8f93\u5165\u6570\u636e\u7684\u5f71\u54cd\uff0c\u5373\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6\u3001\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6\u3001\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u5e76\u4e0d\u5b8c\u5168\u76f8\u7b49\u3002
\u81ea\u9002\u5e94\u6027\u9700\u8981\u6839\u636e\u5177\u4f53\u60c5\u51b5\u6765\u8bc4\u4f30\u3002\u5982\u679c\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6\u5dee\u4e8e\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\uff0c\u8bf4\u660e\u6392\u5e8f\u7b97\u6cd5\u5728\u67d0\u4e9b\u6570\u636e\u4e0b\u6027\u80fd\u53ef\u80fd\u52a3\u5316\uff0c\u56e0\u6b64\u88ab\u89c6\u4e3a\u8d1f\u9762\u5c5e\u6027\uff1b\u800c\u5982\u679c\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6\u4f18\u4e8e\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\uff0c\u5219\u88ab\u89c6\u4e3a\u6b63\u9762\u5c5e\u6027\u3002
\u662f\u5426\u57fa\u4e8e\u6bd4\u8f83\uff1a\u300c\u57fa\u4e8e\u6bd4\u8f83\u7684\u6392\u5e8f\u300d\u4f9d\u8d56\u6bd4\u8f83\u8fd0\u7b97\u7b26\uff08\\(<\\)\u3001\\(=\\)\u3001\\(>\\)\uff09\u6765\u5224\u65ad\u5143\u7d20\u7684\u76f8\u5bf9\u987a\u5e8f\uff0c\u4ece\u800c\u6392\u5e8f\u6574\u4e2a\u6570\u7ec4\uff0c\u7406\u8bba\u6700\u4f18\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n \\log n)\\) \u3002\u800c\u300c\u975e\u6bd4\u8f83\u6392\u5e8f\u300d\u4e0d\u4f7f\u7528\u6bd4\u8f83\u8fd0\u7b97\u7b26\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u53ef\u8fbe \\(O(n)\\) \uff0c\u4f46\u5176\u901a\u7528\u6027\u76f8\u5bf9\u8f83\u5dee\u3002
"},{"location":"chapter_sorting/sorting_algorithm/#1112","title":"11.1.2 \u00a0 \u7406\u60f3\u6392\u5e8f\u7b97\u6cd5","text":"\u8fd0\u884c\u5feb\u3001\u539f\u5730\u3001\u7a33\u5b9a\u3001\u6b63\u5411\u81ea\u9002\u5e94\u3001\u901a\u7528\u6027\u597d\u3002\u663e\u7136\uff0c\u8fc4\u4eca\u4e3a\u6b62\u5c1a\u672a\u53d1\u73b0\u517c\u5177\u4ee5\u4e0a\u6240\u6709\u7279\u6027\u7684\u6392\u5e8f\u7b97\u6cd5\u3002\u56e0\u6b64\uff0c\u5728\u9009\u62e9\u6392\u5e8f\u7b97\u6cd5\u65f6\uff0c\u9700\u8981\u6839\u636e\u5177\u4f53\u7684\u6570\u636e\u7279\u70b9\u548c\u95ee\u9898\u9700\u6c42\u6765\u51b3\u5b9a\u3002
\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u5171\u540c\u5b66\u4e60\u5404\u79cd\u6392\u5e8f\u7b97\u6cd5\uff0c\u5e76\u57fa\u4e8e\u4e0a\u8ff0\u8bc4\u4ef7\u7ef4\u5ea6\u5bf9\u5404\u4e2a\u6392\u5e8f\u7b97\u6cd5\u7684\u4f18\u7f3a\u70b9\u8fdb\u884c\u5206\u6790\u3002
"},{"location":"chapter_sorting/summary/","title":"11.11 \u00a0 \u5c0f\u7ed3","text":""},{"location":"chapter_sorting/summary/#1","title":"1. \u00a0 \u91cd\u70b9\u56de\u987e","text":"\u56fe 11-19 \u00a0 \u6392\u5e8f\u7b97\u6cd5\u5bf9\u6bd4
"},{"location":"chapter_sorting/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"\u6392\u5e8f\u7b97\u6cd5\u7a33\u5b9a\u6027\u5728\u4ec0\u4e48\u60c5\u51b5\u4e0b\u662f\u5fc5\u9700\u7684\uff1f
\u5728\u73b0\u5b9e\u4e2d\uff0c\u6211\u4eec\u6709\u53ef\u80fd\u662f\u57fa\u4e8e\u5bf9\u8c61\u7684\u67d0\u4e2a\u5c5e\u6027\u8fdb\u884c\u6392\u5e8f\u3002\u4f8b\u5982\uff0c\u5b66\u751f\u6709\u59d3\u540d\u548c\u8eab\u9ad8\u4e24\u4e2a\u5c5e\u6027\uff0c\u6211\u4eec\u5e0c\u671b\u5b9e\u73b0\u4e00\u4e2a\u591a\u7ea7\u6392\u5e8f\uff1a
\u5148\u6309\u7167\u59d3\u540d\u8fdb\u884c\u6392\u5e8f\uff0c\u5f97\u5230 (A, 180) (B, 185) (C, 170) (D, 170)
\uff1b\u518d\u5bf9\u8eab\u9ad8\u8fdb\u884c\u6392\u5e8f\u3002\u7531\u4e8e\u6392\u5e8f\u7b97\u6cd5\u4e0d\u7a33\u5b9a\uff0c\u56e0\u6b64\u53ef\u80fd\u5f97\u5230 (D, 170) (C, 170) (A, 180) (B, 185)
\u3002
\u53ef\u4ee5\u53d1\u73b0\uff0c\u5b66\u751f D \u548c C \u7684\u4f4d\u7f6e\u53d1\u751f\u4e86\u4ea4\u6362\uff0c\u59d3\u540d\u7684\u6709\u5e8f\u6027\u88ab\u7834\u574f\u4e86\uff0c\u800c\u8fd9\u662f\u6211\u4eec\u4e0d\u5e0c\u671b\u770b\u5230\u7684\u3002
\u54e8\u5175\u5212\u5206\u4e2d\u201c\u4ece\u53f3\u5f80\u5de6\u67e5\u627e\u201d\u4e0e\u201c\u4ece\u5de6\u5f80\u53f3\u67e5\u627e\u201d\u7684\u987a\u5e8f\u53ef\u4ee5\u4ea4\u6362\u5417\uff1f
\u4e0d\u884c\uff0c\u5f53\u6211\u4eec\u4ee5\u6700\u5de6\u7aef\u5143\u7d20\u4e3a\u57fa\u51c6\u6570\u65f6\uff0c\u5fc5\u987b\u5148\u201c\u4ece\u53f3\u5f80\u5de6\u67e5\u627e\u201d\u518d\u201c\u4ece\u5de6\u5f80\u53f3\u67e5\u627e\u201d\u3002\u8fd9\u4e2a\u7ed3\u8bba\u6709\u4e9b\u53cd\u76f4\u89c9\uff0c\u6211\u4eec\u6765\u5256\u6790\u4e00\u4e0b\u539f\u56e0\u3002
\u54e8\u5175\u5212\u5206 partition()
\u7684\u6700\u540e\u4e00\u6b65\u662f\u4ea4\u6362 nums[left]
\u548c nums[i]
\u3002\u5b8c\u6210\u4ea4\u6362\u540e\uff0c\u57fa\u51c6\u6570\u5de6\u8fb9\u7684\u5143\u7d20\u90fd <=
\u57fa\u51c6\u6570\uff0c\u8fd9\u5c31\u8981\u6c42\u6700\u540e\u4e00\u6b65\u4ea4\u6362\u524d nums[left] >= nums[i]
\u5fc5\u987b\u6210\u7acb\u3002\u5047\u8bbe\u6211\u4eec\u5148\u201c\u4ece\u5de6\u5f80\u53f3\u67e5\u627e\u201d\uff0c\u90a3\u4e48\u5982\u679c\u627e\u4e0d\u5230\u6bd4\u57fa\u51c6\u6570\u66f4\u5927\u7684\u5143\u7d20\uff0c\u5219\u4f1a\u5728 i == j
\u65f6\u8df3\u51fa\u5faa\u73af\uff0c\u6b64\u65f6\u53ef\u80fd nums[j] == nums[i] > nums[left]
\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u6b64\u65f6\u6700\u540e\u4e00\u6b65\u4ea4\u6362\u64cd\u4f5c\u4f1a\u628a\u4e00\u4e2a\u6bd4\u57fa\u51c6\u6570\u66f4\u5927\u7684\u5143\u7d20\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\uff0c\u5bfc\u81f4\u54e8\u5175\u5212\u5206\u5931\u8d25\u3002
\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u7ed9\u5b9a\u6570\u7ec4 [0, 0, 0, 0, 1]
\uff0c\u5982\u679c\u5148\u201c\u4ece\u5de6\u5411\u53f3\u67e5\u627e\u201d\uff0c\u54e8\u5175\u5212\u5206\u540e\u6570\u7ec4\u4e3a [1, 0, 0, 0, 0]
\uff0c\u8fd9\u4e2a\u7ed3\u679c\u662f\u4e0d\u6b63\u786e\u7684\u3002
\u518d\u6df1\u5165\u601d\u8003\u4e00\u4e0b\uff0c\u5982\u679c\u6211\u4eec\u9009\u62e9 nums[right]
\u4e3a\u57fa\u51c6\u6570\uff0c\u90a3\u4e48\u6b63\u597d\u53cd\u8fc7\u6765\uff0c\u5fc5\u987b\u5148\u201c\u4ece\u5de6\u5f80\u53f3\u67e5\u627e\u201d\u3002
\u5173\u4e8e\u5c3e\u9012\u5f52\u4f18\u5316\uff0c\u4e3a\u4ec0\u4e48\u9009\u77ed\u7684\u6570\u7ec4\u80fd\u4fdd\u8bc1\u9012\u5f52\u6df1\u5ea6\u4e0d\u8d85\u8fc7 \\(\\log n\\) \uff1f
\u9012\u5f52\u6df1\u5ea6\u5c31\u662f\u5f53\u524d\u672a\u8fd4\u56de\u7684\u9012\u5f52\u65b9\u6cd5\u7684\u6570\u91cf\u3002\u6bcf\u8f6e\u54e8\u5175\u5212\u5206\u6211\u4eec\u5c06\u539f\u6570\u7ec4\u5212\u5206\u4e3a\u4e24\u4e2a\u5b50\u6570\u7ec4\u3002\u5728\u5c3e\u9012\u5f52\u4f18\u5316\u540e\uff0c\u5411\u4e0b\u9012\u5f52\u7684\u5b50\u6570\u7ec4\u957f\u5ea6\u6700\u5927\u4e3a\u539f\u6570\u7ec4\u957f\u5ea6\u7684\u4e00\u534a\u3002\u5047\u8bbe\u6700\u5dee\u60c5\u51b5\uff0c\u4e00\u76f4\u4e3a\u4e00\u534a\u957f\u5ea6\uff0c\u90a3\u4e48\u6700\u7ec8\u7684\u9012\u5f52\u6df1\u5ea6\u5c31\u662f \\(\\log n\\) \u3002
\u56de\u987e\u539f\u59cb\u7684\u5feb\u901f\u6392\u5e8f\uff0c\u6211\u4eec\u6709\u53ef\u80fd\u4f1a\u8fde\u7eed\u5730\u9012\u5f52\u957f\u5ea6\u8f83\u5927\u7684\u6570\u7ec4\uff0c\u6700\u5dee\u60c5\u51b5\u4e0b\u4e3a \\(n\\)\u3001\\(n - 1\\)\u3001\\(\\dots\\)\u3001\\(2\\)\u3001\\(1\\) \uff0c\u9012\u5f52\u6df1\u5ea6\u4e3a \\(n\\) \u3002\u5c3e\u9012\u5f52\u4f18\u5316\u53ef\u4ee5\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\u51fa\u73b0\u3002
\u5f53\u6570\u7ec4\u4e2d\u6240\u6709\u5143\u7d20\u90fd\u76f8\u7b49\u65f6\uff0c\u5feb\u901f\u6392\u5e8f\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u662f \\(O(n^2)\\) \u5417\uff1f\u8be5\u5982\u4f55\u5904\u7406\u8fd9\u79cd\u9000\u5316\u60c5\u51b5\uff1f
\u662f\u7684\u3002\u5bf9\u4e8e\u8fd9\u79cd\u60c5\u51b5\uff0c\u53ef\u4ee5\u8003\u8651\u901a\u8fc7\u54e8\u5175\u5212\u5206\u5c06\u6570\u7ec4\u5212\u5206\u4e3a\u4e09\u4e2a\u90e8\u5206\uff1a\u5c0f\u4e8e\u3001\u7b49\u4e8e\u3001\u5927\u4e8e\u57fa\u51c6\u6570\u3002\u4ec5\u5411\u4e0b\u9012\u5f52\u5c0f\u4e8e\u548c\u5927\u4e8e\u7684\u4e24\u90e8\u5206\u3002\u5728\u8be5\u65b9\u6cd5\u4e0b\uff0c\u8f93\u5165\u5143\u7d20\u5168\u90e8\u76f8\u7b49\u7684\u6570\u7ec4\uff0c\u4ec5\u4e00\u8f6e\u54e8\u5175\u5212\u5206\u5373\u53ef\u5b8c\u6210\u6392\u5e8f\u3002
\u6876\u6392\u5e8f\u7684\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a\u4ec0\u4e48\u662f \\(O(n^2)\\) \uff1f
\u6700\u5dee\u60c5\u51b5\u4e0b\uff0c\u6240\u6709\u5143\u7d20\u88ab\u5206\u81f3\u540c\u4e00\u4e2a\u6876\u4e2d\u3002\u5982\u679c\u6211\u4eec\u91c7\u7528\u4e00\u4e2a \\(O(n^2)\\) \u7b97\u6cd5\u6765\u6392\u5e8f\u8fd9\u4e9b\u5143\u7d20\uff0c\u5219\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\) \u3002
"},{"location":"chapter_stack_and_queue/","title":"\u7b2c 5 \u7ae0 \u00a0 \u6808\u4e0e\u961f\u5217","text":"Abstract
\u6808\u5982\u540c\u53e0\u732b\u732b\uff0c\u800c\u961f\u5217\u5c31\u50cf\u732b\u732b\u6392\u961f\u3002
\u4e24\u8005\u5206\u522b\u4ee3\u8868\u5148\u5165\u540e\u51fa\u548c\u5148\u5165\u5148\u51fa\u7684\u903b\u8f91\u5173\u7cfb\u3002
"},{"location":"chapter_stack_and_queue/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"\u5728\u961f\u5217\u4e2d\uff0c\u6211\u4eec\u4ec5\u80fd\u5220\u9664\u5934\u90e8\u5143\u7d20\u6216\u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20\u3002\u5982\u56fe 5-7 \u6240\u793a\uff0c\u300c\u53cc\u5411\u961f\u5217 double-ended queue\u300d\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7684\u7075\u6d3b\u6027\uff0c\u5141\u8bb8\u5728\u5934\u90e8\u548c\u5c3e\u90e8\u6267\u884c\u5143\u7d20\u7684\u6dfb\u52a0\u6216\u5220\u9664\u64cd\u4f5c\u3002
\u56fe 5-7 \u00a0 \u53cc\u5411\u961f\u5217\u7684\u64cd\u4f5c
"},{"location":"chapter_stack_and_queue/deque/#531","title":"5.3.1 \u00a0 \u53cc\u5411\u961f\u5217\u5e38\u7528\u64cd\u4f5c","text":"\u53cc\u5411\u961f\u5217\u7684\u5e38\u7528\u64cd\u4f5c\u5982\u8868 5-3 \u6240\u793a\uff0c\u5177\u4f53\u7684\u65b9\u6cd5\u540d\u79f0\u9700\u8981\u6839\u636e\u6240\u4f7f\u7528\u7684\u7f16\u7a0b\u8bed\u8a00\u6765\u786e\u5b9a\u3002
\u8868 5-3 \u00a0 \u53cc\u5411\u961f\u5217\u64cd\u4f5c\u6548\u7387
\u65b9\u6cd5\u540d \u63cf\u8ff0 \u65f6\u95f4\u590d\u6742\u5ea6 pushFirst() \u5c06\u5143\u7d20\u6dfb\u52a0\u81f3\u961f\u9996 \\(O(1)\\) pushLast() \u5c06\u5143\u7d20\u6dfb\u52a0\u81f3\u961f\u5c3e \\(O(1)\\) popFirst() \u5220\u9664\u961f\u9996\u5143\u7d20 \\(O(1)\\) popLast() \u5220\u9664\u961f\u5c3e\u5143\u7d20 \\(O(1)\\) peekFirst() \u8bbf\u95ee\u961f\u9996\u5143\u7d20 \\(O(1)\\) peekLast() \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 \\(O(1)\\)\u540c\u6837\u5730\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u7f16\u7a0b\u8bed\u8a00\u4e2d\u5df2\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217\u7c7b\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig deque.pyfrom collections import deque\n\n# \u521d\u59cb\u5316\u53cc\u5411\u961f\u5217\ndeque: deque[int] = deque()\n\n# \u5143\u7d20\u5165\u961f\ndeque.append(2) # \u6dfb\u52a0\u81f3\u961f\u5c3e\ndeque.append(5)\ndeque.append(4)\ndeque.appendleft(3) # \u6dfb\u52a0\u81f3\u961f\u9996\ndeque.appendleft(1)\n\n# \u8bbf\u95ee\u5143\u7d20\nfront: int = deque[0] # \u961f\u9996\u5143\u7d20\nrear: int = deque[-1] # \u961f\u5c3e\u5143\u7d20\n\n# \u5143\u7d20\u51fa\u961f\npop_front: int = deque.popleft() # \u961f\u9996\u5143\u7d20\u51fa\u961f\npop_rear: int = deque.pop() # \u961f\u5c3e\u5143\u7d20\u51fa\u961f\n\n# \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\nsize: int = len(deque)\n\n# \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a\nis_empty: bool = len(deque) == 0\n
deque.cpp/* \u521d\u59cb\u5316\u53cc\u5411\u961f\u5217 */\ndeque<int> deque;\n\n/* \u5143\u7d20\u5165\u961f */\ndeque.push_back(2); // \u6dfb\u52a0\u81f3\u961f\u5c3e\ndeque.push_back(5);\ndeque.push_back(4);\ndeque.push_front(3); // \u6dfb\u52a0\u81f3\u961f\u9996\ndeque.push_front(1);\n\n/* \u8bbf\u95ee\u5143\u7d20 */\nint front = deque.front(); // \u961f\u9996\u5143\u7d20\nint back = deque.back(); // \u961f\u5c3e\u5143\u7d20\n\n/* \u5143\u7d20\u51fa\u961f */\ndeque.pop_front(); // \u961f\u9996\u5143\u7d20\u51fa\u961f\ndeque.pop_back(); // \u961f\u5c3e\u5143\u7d20\u51fa\u961f\n\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nint size = deque.size();\n\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool empty = deque.empty();\n
deque.java/* \u521d\u59cb\u5316\u53cc\u5411\u961f\u5217 */\nDeque<Integer> deque = new LinkedList<>();\n\n/* \u5143\u7d20\u5165\u961f */\ndeque.offerLast(2); // \u6dfb\u52a0\u81f3\u961f\u5c3e\ndeque.offerLast(5);\ndeque.offerLast(4);\ndeque.offerFirst(3); // \u6dfb\u52a0\u81f3\u961f\u9996\ndeque.offerFirst(1);\n\n/* \u8bbf\u95ee\u5143\u7d20 */\nint peekFirst = deque.peekFirst(); // \u961f\u9996\u5143\u7d20\nint peekLast = deque.peekLast(); // \u961f\u5c3e\u5143\u7d20\n\n/* \u5143\u7d20\u51fa\u961f */\nint popFirst = deque.pollFirst(); // \u961f\u9996\u5143\u7d20\u51fa\u961f\nint popLast = deque.pollLast(); // \u961f\u5c3e\u5143\u7d20\u51fa\u961f\n\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nint size = deque.size();\n\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nboolean isEmpty = deque.isEmpty();\n
deque.cs/* \u521d\u59cb\u5316\u53cc\u5411\u961f\u5217 */\n// \u5728 C# \u4e2d\uff0c\u5c06\u94fe\u8868 LinkedList \u770b\u4f5c\u53cc\u5411\u961f\u5217\u6765\u4f7f\u7528\nLinkedList<int> deque = new();\n\n/* \u5143\u7d20\u5165\u961f */\ndeque.AddLast(2); // \u6dfb\u52a0\u81f3\u961f\u5c3e\ndeque.AddLast(5);\ndeque.AddLast(4);\ndeque.AddFirst(3); // \u6dfb\u52a0\u81f3\u961f\u9996\ndeque.AddFirst(1);\n\n/* \u8bbf\u95ee\u5143\u7d20 */\nint peekFirst = deque.First.Value; // \u961f\u9996\u5143\u7d20\nint peekLast = deque.Last.Value; // \u961f\u5c3e\u5143\u7d20\n\n/* \u5143\u7d20\u51fa\u961f */\ndeque.RemoveFirst(); // \u961f\u9996\u5143\u7d20\u51fa\u961f\ndeque.RemoveLast(); // \u961f\u5c3e\u5143\u7d20\u51fa\u961f\n\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nint size = deque.Count;\n\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty = deque.Count == 0;\n
deque_test.go/* \u521d\u59cb\u5316\u53cc\u5411\u961f\u5217 */\n// \u5728 Go \u4e2d\uff0c\u5c06 list \u4f5c\u4e3a\u53cc\u5411\u961f\u5217\u4f7f\u7528\ndeque := list.New()\n\n/* \u5143\u7d20\u5165\u961f */\ndeque.PushBack(2) // \u6dfb\u52a0\u81f3\u961f\u5c3e\ndeque.PushBack(5)\ndeque.PushBack(4)\ndeque.PushFront(3) // \u6dfb\u52a0\u81f3\u961f\u9996\ndeque.PushFront(1)\n\n/* \u8bbf\u95ee\u5143\u7d20 */\nfront := deque.Front() // \u961f\u9996\u5143\u7d20\nrear := deque.Back() // \u961f\u5c3e\u5143\u7d20\n\n/* \u5143\u7d20\u51fa\u961f */\ndeque.Remove(front) // \u961f\u9996\u5143\u7d20\u51fa\u961f\ndeque.Remove(rear) // \u961f\u5c3e\u5143\u7d20\u51fa\u961f\n\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nsize := deque.Len()\n\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nisEmpty := deque.Len() == 0\n
deque.swift/* \u521d\u59cb\u5316\u53cc\u5411\u961f\u5217 */\n// Swift \u6ca1\u6709\u5185\u7f6e\u7684\u53cc\u5411\u961f\u5217\u7c7b\uff0c\u53ef\u4ee5\u628a Array \u5f53\u4f5c\u53cc\u5411\u961f\u5217\u6765\u4f7f\u7528\nvar deque: [Int] = []\n\n/* \u5143\u7d20\u5165\u961f */\ndeque.append(2) // \u6dfb\u52a0\u81f3\u961f\u5c3e\ndeque.append(5)\ndeque.append(4)\ndeque.insert(3, at: 0) // \u6dfb\u52a0\u81f3\u961f\u9996\ndeque.insert(1, at: 0)\n\n/* \u8bbf\u95ee\u5143\u7d20 */\nlet peekFirst = deque.first! // \u961f\u9996\u5143\u7d20\nlet peekLast = deque.last! // \u961f\u5c3e\u5143\u7d20\n\n/* \u5143\u7d20\u51fa\u961f */\n// \u4f7f\u7528 Array \u6a21\u62df\u65f6 popFirst \u7684\u590d\u6742\u5ea6\u4e3a O(n)\nlet popFirst = deque.removeFirst() // \u961f\u9996\u5143\u7d20\u51fa\u961f\nlet popLast = deque.removeLast() // \u961f\u5c3e\u5143\u7d20\u51fa\u961f\n\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nlet size = deque.count\n\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nlet isEmpty = deque.isEmpty\n
deque.js/* \u521d\u59cb\u5316\u53cc\u5411\u961f\u5217 */\n// JavaScript \u6ca1\u6709\u5185\u7f6e\u7684\u53cc\u7aef\u961f\u5217\uff0c\u53ea\u80fd\u628a Array \u5f53\u4f5c\u53cc\u7aef\u961f\u5217\u6765\u4f7f\u7528\nconst deque = [];\n\n/* \u5143\u7d20\u5165\u961f */\ndeque.push(2);\ndeque.push(5);\ndeque.push(4);\n// \u8bf7\u6ce8\u610f\uff0c\u7531\u4e8e\u662f\u6570\u7ec4\uff0cunshift() \u65b9\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\ndeque.unshift(3);\ndeque.unshift(1);\nconsole.log(\"\u53cc\u5411\u961f\u5217 deque = \", deque);\n\n/* \u8bbf\u95ee\u5143\u7d20 */\nconst peekFirst = deque[0];\nconsole.log(\"\u961f\u9996\u5143\u7d20 peekFirst = \" + peekFirst);\nconst peekLast = deque[deque.length - 1];\nconsole.log(\"\u961f\u5c3e\u5143\u7d20 peekLast = \" + peekLast);\n\n/* \u5143\u7d20\u51fa\u961f */\n// \u8bf7\u6ce8\u610f\uff0c\u7531\u4e8e\u662f\u6570\u7ec4\uff0cshift() \u65b9\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\nconst popFront = deque.shift();\nconsole.log(\"\u961f\u9996\u51fa\u961f\u5143\u7d20 popFront = \" + popFront + \"\uff0c\u961f\u9996\u51fa\u961f\u540e deque = \" + deque);\nconst popBack = deque.pop();\nconsole.log(\"\u961f\u5c3e\u51fa\u961f\u5143\u7d20 popBack = \" + popBack + \"\uff0c\u961f\u5c3e\u51fa\u961f\u540e deque = \" + deque);\n\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nconst size = deque.length;\nconsole.log(\"\u53cc\u5411\u961f\u5217\u957f\u5ea6 size = \" + size);\n\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nconst isEmpty = size === 0;\nconsole.log(\"\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a = \" + isEmpty);\n
deque.ts/* \u521d\u59cb\u5316\u53cc\u5411\u961f\u5217 */\n// TypeScript \u6ca1\u6709\u5185\u7f6e\u7684\u53cc\u7aef\u961f\u5217\uff0c\u53ea\u80fd\u628a Array \u5f53\u4f5c\u53cc\u7aef\u961f\u5217\u6765\u4f7f\u7528\nconst deque: number[] = [];\n\n/* \u5143\u7d20\u5165\u961f */\ndeque.push(2);\ndeque.push(5);\ndeque.push(4);\n// \u8bf7\u6ce8\u610f\uff0c\u7531\u4e8e\u662f\u6570\u7ec4\uff0cunshift() \u65b9\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\ndeque.unshift(3);\ndeque.unshift(1);\nconsole.log(\"\u53cc\u5411\u961f\u5217 deque = \", deque);\n\n/* \u8bbf\u95ee\u5143\u7d20 */\nconst peekFirst: number = deque[0];\nconsole.log(\"\u961f\u9996\u5143\u7d20 peekFirst = \" + peekFirst);\nconst peekLast: number = deque[deque.length - 1];\nconsole.log(\"\u961f\u5c3e\u5143\u7d20 peekLast = \" + peekLast);\n\n/* \u5143\u7d20\u51fa\u961f */\n// \u8bf7\u6ce8\u610f\uff0c\u7531\u4e8e\u662f\u6570\u7ec4\uff0cshift() \u65b9\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\nconst popFront: number = deque.shift() as number;\nconsole.log(\"\u961f\u9996\u51fa\u961f\u5143\u7d20 popFront = \" + popFront + \"\uff0c\u961f\u9996\u51fa\u961f\u540e deque = \" + deque);\nconst popBack: number = deque.pop() as number;\nconsole.log(\"\u961f\u5c3e\u51fa\u961f\u5143\u7d20 popBack = \" + popBack + \"\uff0c\u961f\u5c3e\u51fa\u961f\u540e deque = \" + deque);\n\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nconst size: number = deque.length;\nconsole.log(\"\u53cc\u5411\u961f\u5217\u957f\u5ea6 size = \" + size);\n\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nconst isEmpty: boolean = size === 0;\nconsole.log(\"\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a = \" + isEmpty);\n
deque.dart/* \u521d\u59cb\u5316\u53cc\u5411\u961f\u5217 */\n// \u5728 Dart \u4e2d\uff0cQueue \u88ab\u5b9a\u4e49\u4e3a\u53cc\u5411\u961f\u5217\nQueue<int> deque = Queue<int>();\n\n/* \u5143\u7d20\u5165\u961f */\ndeque.addLast(2); // \u6dfb\u52a0\u81f3\u961f\u5c3e\ndeque.addLast(5);\ndeque.addLast(4);\ndeque.addFirst(3); // \u6dfb\u52a0\u81f3\u961f\u9996\ndeque.addFirst(1);\n\n/* \u8bbf\u95ee\u5143\u7d20 */\nint peekFirst = deque.first; // \u961f\u9996\u5143\u7d20\nint peekLast = deque.last; // \u961f\u5c3e\u5143\u7d20\n\n/* \u5143\u7d20\u51fa\u961f */\nint popFirst = deque.removeFirst(); // \u961f\u9996\u5143\u7d20\u51fa\u961f\nint popLast = deque.removeLast(); // \u961f\u5c3e\u5143\u7d20\u51fa\u961f\n\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nint size = deque.length;\n\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty = deque.isEmpty;W\n
deque.rs/* \u521d\u59cb\u5316\u53cc\u5411\u961f\u5217 */\nlet mut deque: VecDeque<u32> = VecDeque::new();\n\n/* \u5143\u7d20\u5165\u961f */\ndeque.push_back(2); // \u6dfb\u52a0\u81f3\u961f\u5c3e\ndeque.push_back(5);\ndeque.push_back(4);\ndeque.push_front(3); // \u6dfb\u52a0\u81f3\u961f\u9996\ndeque.push_front(1);\n\n/* \u8bbf\u95ee\u5143\u7d20 */\nif let Some(front) = deque.front() { // \u961f\u9996\u5143\u7d20\n}\nif let Some(rear) = deque.back() { // \u961f\u5c3e\u5143\u7d20\n}\n\n/* \u5143\u7d20\u51fa\u961f */\nif let Some(pop_front) = deque.pop_front() { // \u961f\u9996\u5143\u7d20\u51fa\u961f\n}\nif let Some(pop_rear) = deque.pop_back() { // \u961f\u5c3e\u5143\u7d20\u51fa\u961f\n}\n\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nlet size = deque.len();\n\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nlet is_empty = deque.is_empty();\n
deque.c// C \u672a\u63d0\u4f9b\u5185\u7f6e\u53cc\u5411\u961f\u5217\n
deque.zig\n
"},{"location":"chapter_stack_and_queue/deque/#532","title":"5.3.2 \u00a0 \u53cc\u5411\u961f\u5217\u5b9e\u73b0 *","text":"\u53cc\u5411\u961f\u5217\u7684\u5b9e\u73b0\u4e0e\u961f\u5217\u7c7b\u4f3c\uff0c\u53ef\u4ee5\u9009\u62e9\u94fe\u8868\u6216\u6570\u7ec4\u4f5c\u4e3a\u5e95\u5c42\u6570\u636e\u7ed3\u6784\u3002
"},{"location":"chapter_stack_and_queue/deque/#1","title":"1. \u00a0 \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u7684\u5b9e\u73b0","text":"\u56de\u987e\u4e0a\u4e00\u8282\u5185\u5bb9\uff0c\u6211\u4eec\u4f7f\u7528\u666e\u901a\u5355\u5411\u94fe\u8868\u6765\u5b9e\u73b0\u961f\u5217\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u65b9\u4fbf\u5730\u5220\u9664\u5934\u8282\u70b9\uff08\u5bf9\u5e94\u51fa\u961f\u64cd\u4f5c\uff09\u548c\u5728\u5c3e\u8282\u70b9\u540e\u6dfb\u52a0\u65b0\u8282\u70b9\uff08\u5bf9\u5e94\u5165\u961f\u64cd\u4f5c\uff09\u3002
\u5bf9\u4e8e\u53cc\u5411\u961f\u5217\u800c\u8a00\uff0c\u5934\u90e8\u548c\u5c3e\u90e8\u90fd\u53ef\u4ee5\u6267\u884c\u5165\u961f\u548c\u51fa\u961f\u64cd\u4f5c\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u53cc\u5411\u961f\u5217\u9700\u8981\u5b9e\u73b0\u53e6\u4e00\u4e2a\u5bf9\u79f0\u65b9\u5411\u7684\u64cd\u4f5c\u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u91c7\u7528\u201c\u53cc\u5411\u94fe\u8868\u201d\u4f5c\u4e3a\u53cc\u5411\u961f\u5217\u7684\u5e95\u5c42\u6570\u636e\u7ed3\u6784\u3002
\u5982\u56fe 5-8 \u6240\u793a\uff0c\u6211\u4eec\u5c06\u53cc\u5411\u94fe\u8868\u7684\u5934\u8282\u70b9\u548c\u5c3e\u8282\u70b9\u89c6\u4e3a\u53cc\u5411\u961f\u5217\u7684\u961f\u9996\u548c\u961f\u5c3e\uff0c\u540c\u65f6\u5b9e\u73b0\u5728\u4e24\u7aef\u6dfb\u52a0\u548c\u5220\u9664\u8282\u70b9\u7684\u529f\u80fd\u3002
LinkedListDequepushLast()pushFirst()popLast()popFirst()\u56fe 5-8 \u00a0 \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u53cc\u5411\u961f\u5217\u7684\u5165\u961f\u51fa\u961f\u64cd\u4f5c
\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig linkedlist_deque.pyclass ListNode:\n \"\"\"\u53cc\u5411\u94fe\u8868\u8282\u70b9\"\"\"\n\n def __init__(self, val: int):\n \"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n self.val: int = val\n self.next: ListNode | None = None # \u540e\u7ee7\u8282\u70b9\u5f15\u7528\n self.prev: ListNode | None = None # \u524d\u9a71\u8282\u70b9\u5f15\u7528\n\nclass LinkedListDeque:\n \"\"\"\u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217\"\"\"\n\n def __init__(self):\n \"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n self._front: ListNode | None = None # \u5934\u8282\u70b9 front\n self._rear: ListNode | None = None # \u5c3e\u8282\u70b9 rear\n self._size: int = 0 # \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\n\n def size(self) -> int:\n \"\"\"\u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\"\"\"\n return self._size\n\n def is_empty(self) -> bool:\n \"\"\"\u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a\"\"\"\n return self.size() == 0\n\n def push(self, num: int, is_front: bool):\n \"\"\"\u5165\u961f\u64cd\u4f5c\"\"\"\n node = ListNode(num)\n # \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n if self.is_empty():\n self._front = self._rear = node\n # \u961f\u9996\u5165\u961f\u64cd\u4f5c\n elif is_front:\n # \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\n self._front.prev = node\n node.next = self._front\n self._front = node # \u66f4\u65b0\u5934\u8282\u70b9\n # \u961f\u5c3e\u5165\u961f\u64cd\u4f5c\n else:\n # \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\n self._rear.next = node\n node.prev = self._rear\n self._rear = node # \u66f4\u65b0\u5c3e\u8282\u70b9\n self._size += 1 # \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n\n def push_first(self, num: int):\n \"\"\"\u961f\u9996\u5165\u961f\"\"\"\n self.push(num, True)\n\n def push_last(self, num: int):\n \"\"\"\u961f\u5c3e\u5165\u961f\"\"\"\n self.push(num, False)\n\n def pop(self, is_front: bool) -> int:\n \"\"\"\u51fa\u961f\u64cd\u4f5c\"\"\"\n if self.is_empty():\n raise IndexError(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\n # \u961f\u9996\u51fa\u961f\u64cd\u4f5c\n if is_front:\n val: int = self._front.val # \u6682\u5b58\u5934\u8282\u70b9\u503c\n # \u5220\u9664\u5934\u8282\u70b9\n fnext: ListNode | None = self._front.next\n if fnext != None:\n fnext.prev = None\n self._front.next = None\n self._front = fnext # \u66f4\u65b0\u5934\u8282\u70b9\n # \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\n else:\n val: int = self._rear.val # \u6682\u5b58\u5c3e\u8282\u70b9\u503c\n # \u5220\u9664\u5c3e\u8282\u70b9\n rprev: ListNode | None = self._rear.prev\n if rprev != None:\n rprev.next = None\n self._rear.prev = None\n self._rear = rprev # \u66f4\u65b0\u5c3e\u8282\u70b9\n self._size -= 1 # \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n return val\n\n def pop_first(self) -> int:\n \"\"\"\u961f\u9996\u51fa\u961f\"\"\"\n return self.pop(True)\n\n def pop_last(self) -> int:\n \"\"\"\u961f\u5c3e\u51fa\u961f\"\"\"\n return self.pop(False)\n\n def peek_first(self) -> int:\n \"\"\"\u8bbf\u95ee\u961f\u9996\u5143\u7d20\"\"\"\n if self.is_empty():\n raise IndexError(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\n return self._front.val\n\n def peek_last(self) -> int:\n \"\"\"\u8bbf\u95ee\u961f\u5c3e\u5143\u7d20\"\"\"\n if self.is_empty():\n raise IndexError(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\n return self._rear.val\n\n def to_array(self) -> list[int]:\n \"\"\"\u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370\"\"\"\n node = self._front\n res = [0] * self.size()\n for i in range(self.size()):\n res[i] = node.val\n node = node.next\n return res\n
linkedlist_deque.cpp/* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\nstruct DoublyListNode {\n int val; // \u8282\u70b9\u503c\n DoublyListNode *next; // \u540e\u7ee7\u8282\u70b9\u6307\u9488\n DoublyListNode *prev; // \u524d\u9a71\u8282\u70b9\u6307\u9488\n DoublyListNode(int val) : val(val), prev(nullptr), next(nullptr) {\n }\n};\n\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass LinkedListDeque {\n private:\n DoublyListNode *front, *rear; // \u5934\u8282\u70b9 front \uff0c\u5c3e\u8282\u70b9 rear\n int queSize = 0; // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\n\n public:\n /* \u6784\u9020\u65b9\u6cd5 */\n LinkedListDeque() : front(nullptr), rear(nullptr) {\n }\n\n /* \u6790\u6784\u65b9\u6cd5 */\n ~LinkedListDeque() {\n // \u904d\u5386\u94fe\u8868\u5220\u9664\u8282\u70b9\uff0c\u91ca\u653e\u5185\u5b58\n DoublyListNode *pre, *cur = front;\n while (cur != nullptr) {\n pre = cur;\n cur = cur->next;\n delete pre;\n }\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n int size() {\n return queSize;\n }\n\n /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n bool isEmpty() {\n return size() == 0;\n }\n\n /* \u5165\u961f\u64cd\u4f5c */\n void push(int num, bool isFront) {\n DoublyListNode *node = new DoublyListNode(num);\n // \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n if (isEmpty())\n front = rear = node;\n // \u961f\u9996\u5165\u961f\u64cd\u4f5c\n else if (isFront) {\n // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\n front->prev = node;\n node->next = front;\n front = node; // \u66f4\u65b0\u5934\u8282\u70b9\n // \u961f\u5c3e\u5165\u961f\u64cd\u4f5c\n } else {\n // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\n rear->next = node;\n node->prev = rear;\n rear = node; // \u66f4\u65b0\u5c3e\u8282\u70b9\n }\n queSize++; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n }\n\n /* \u961f\u9996\u5165\u961f */\n void pushFirst(int num) {\n push(num, true);\n }\n\n /* \u961f\u5c3e\u5165\u961f */\n void pushLast(int num) {\n push(num, false);\n }\n\n /* \u51fa\u961f\u64cd\u4f5c */\n int pop(bool isFront) {\n if (isEmpty())\n throw out_of_range(\"\u961f\u5217\u4e3a\u7a7a\");\n int val;\n // \u961f\u9996\u51fa\u961f\u64cd\u4f5c\n if (isFront) {\n val = front->val; // \u6682\u5b58\u5934\u8282\u70b9\u503c\n // \u5220\u9664\u5934\u8282\u70b9\n DoublyListNode *fNext = front->next;\n if (fNext != nullptr) {\n fNext->prev = nullptr;\n front->next = nullptr;\n delete front;\n }\n front = fNext; // \u66f4\u65b0\u5934\u8282\u70b9\n // \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\n } else {\n val = rear->val; // \u6682\u5b58\u5c3e\u8282\u70b9\u503c\n // \u5220\u9664\u5c3e\u8282\u70b9\n DoublyListNode *rPrev = rear->prev;\n if (rPrev != nullptr) {\n rPrev->next = nullptr;\n rear->prev = nullptr;\n delete rear;\n }\n rear = rPrev; // \u66f4\u65b0\u5c3e\u8282\u70b9\n }\n queSize--; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n return val;\n }\n\n /* \u961f\u9996\u51fa\u961f */\n int popFirst() {\n return pop(true);\n }\n\n /* \u961f\u5c3e\u51fa\u961f */\n int popLast() {\n return pop(false);\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n int peekFirst() {\n if (isEmpty())\n throw out_of_range(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\");\n return front->val;\n }\n\n /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n int peekLast() {\n if (isEmpty())\n throw out_of_range(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\");\n return rear->val;\n }\n\n /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n vector<int> toVector() {\n DoublyListNode *node = front;\n vector<int> res(size());\n for (int i = 0; i < res.size(); i++) {\n res[i] = node->val;\n node = node->next;\n }\n return res;\n }\n};\n
linkedlist_deque.java/* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\nclass ListNode {\n int val; // \u8282\u70b9\u503c\n ListNode next; // \u540e\u7ee7\u8282\u70b9\u5f15\u7528\n ListNode prev; // \u524d\u9a71\u8282\u70b9\u5f15\u7528\n\n ListNode(int val) {\n this.val = val;\n prev = next = null;\n }\n}\n\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass LinkedListDeque {\n private ListNode front, rear; // \u5934\u8282\u70b9 front \uff0c\u5c3e\u8282\u70b9 rear\n private int queSize = 0; // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\n\n public LinkedListDeque() {\n front = rear = null;\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n public int size() {\n return queSize;\n }\n\n /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n public boolean isEmpty() {\n return size() == 0;\n }\n\n /* \u5165\u961f\u64cd\u4f5c */\n private void push(int num, boolean isFront) {\n ListNode node = new ListNode(num);\n // \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n if (isEmpty())\n front = rear = node;\n // \u961f\u9996\u5165\u961f\u64cd\u4f5c\n else if (isFront) {\n // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\n front.prev = node;\n node.next = front;\n front = node; // \u66f4\u65b0\u5934\u8282\u70b9\n // \u961f\u5c3e\u5165\u961f\u64cd\u4f5c\n } else {\n // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\n rear.next = node;\n node.prev = rear;\n rear = node; // \u66f4\u65b0\u5c3e\u8282\u70b9\n }\n queSize++; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n }\n\n /* \u961f\u9996\u5165\u961f */\n public void pushFirst(int num) {\n push(num, true);\n }\n\n /* \u961f\u5c3e\u5165\u961f */\n public void pushLast(int num) {\n push(num, false);\n }\n\n /* \u51fa\u961f\u64cd\u4f5c */\n private int pop(boolean isFront) {\n if (isEmpty())\n throw new IndexOutOfBoundsException();\n int val;\n // \u961f\u9996\u51fa\u961f\u64cd\u4f5c\n if (isFront) {\n val = front.val; // \u6682\u5b58\u5934\u8282\u70b9\u503c\n // \u5220\u9664\u5934\u8282\u70b9\n ListNode fNext = front.next;\n if (fNext != null) {\n fNext.prev = null;\n front.next = null;\n }\n front = fNext; // \u66f4\u65b0\u5934\u8282\u70b9\n // \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\n } else {\n val = rear.val; // \u6682\u5b58\u5c3e\u8282\u70b9\u503c\n // \u5220\u9664\u5c3e\u8282\u70b9\n ListNode rPrev = rear.prev;\n if (rPrev != null) {\n rPrev.next = null;\n rear.prev = null;\n }\n rear = rPrev; // \u66f4\u65b0\u5c3e\u8282\u70b9\n }\n queSize--; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n return val;\n }\n\n /* \u961f\u9996\u51fa\u961f */\n public int popFirst() {\n return pop(true);\n }\n\n /* \u961f\u5c3e\u51fa\u961f */\n public int popLast() {\n return pop(false);\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n public int peekFirst() {\n if (isEmpty())\n throw new IndexOutOfBoundsException();\n return front.val;\n }\n\n /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n public int peekLast() {\n if (isEmpty())\n throw new IndexOutOfBoundsException();\n return rear.val;\n }\n\n /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n public int[] toArray() {\n ListNode node = front;\n int[] res = new int[size()];\n for (int i = 0; i < res.length; i++) {\n res[i] = node.val;\n node = node.next;\n }\n return res;\n }\n}\n
linkedlist_deque.cs/* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\nclass ListNode(int val) {\n public int val = val; // \u8282\u70b9\u503c\n public ListNode? next = null; // \u540e\u7ee7\u8282\u70b9\u5f15\u7528\n public ListNode? prev = null; // \u524d\u9a71\u8282\u70b9\u5f15\u7528\n}\n\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass LinkedListDeque {\n ListNode? front, rear; // \u5934\u8282\u70b9 front, \u5c3e\u8282\u70b9 rear\n int queSize = 0; // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\n\n public LinkedListDeque() {\n front = null;\n rear = null;\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n public int Size() {\n return queSize;\n }\n\n /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n public bool IsEmpty() {\n return Size() == 0;\n }\n\n /* \u5165\u961f\u64cd\u4f5c */\n void Push(int num, bool isFront) {\n ListNode node = new(num);\n // \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n if (IsEmpty()) {\n front = node;\n rear = node;\n }\n // \u961f\u9996\u5165\u961f\u64cd\u4f5c\n else if (isFront) {\n // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\n front!.prev = node;\n node.next = front;\n front = node; // \u66f4\u65b0\u5934\u8282\u70b9 \n }\n // \u961f\u5c3e\u5165\u961f\u64cd\u4f5c\n else {\n // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\n rear!.next = node;\n node.prev = rear;\n rear = node; // \u66f4\u65b0\u5c3e\u8282\u70b9\n }\n\n queSize++; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n }\n\n /* \u961f\u9996\u5165\u961f */\n public void PushFirst(int num) {\n Push(num, true);\n }\n\n /* \u961f\u5c3e\u5165\u961f */\n public void PushLast(int num) {\n Push(num, false);\n }\n\n /* \u51fa\u961f\u64cd\u4f5c */\n int? Pop(bool isFront) {\n if (IsEmpty())\n throw new Exception();\n int? val;\n // \u961f\u9996\u51fa\u961f\u64cd\u4f5c\n if (isFront) {\n val = front?.val; // \u6682\u5b58\u5934\u8282\u70b9\u503c\n // \u5220\u9664\u5934\u8282\u70b9\n ListNode? fNext = front?.next;\n if (fNext != null) {\n fNext.prev = null;\n front!.next = null;\n }\n front = fNext; // \u66f4\u65b0\u5934\u8282\u70b9\n }\n // \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\n else {\n val = rear?.val; // \u6682\u5b58\u5c3e\u8282\u70b9\u503c\n // \u5220\u9664\u5c3e\u8282\u70b9\n ListNode? rPrev = rear?.prev;\n if (rPrev != null) {\n rPrev.next = null;\n rear!.prev = null;\n }\n rear = rPrev; // \u66f4\u65b0\u5c3e\u8282\u70b9\n }\n\n queSize--; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n return val;\n }\n\n /* \u961f\u9996\u51fa\u961f */\n public int? PopFirst() {\n return Pop(true);\n }\n\n /* \u961f\u5c3e\u51fa\u961f */\n public int? PopLast() {\n return Pop(false);\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n public int? PeekFirst() {\n if (IsEmpty())\n throw new Exception();\n return front?.val;\n }\n\n /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n public int? PeekLast() {\n if (IsEmpty())\n throw new Exception();\n return rear?.val;\n }\n\n /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n public int?[] ToArray() {\n ListNode? node = front;\n int?[] res = new int?[Size()];\n for (int i = 0; i < res.Length; i++) {\n res[i] = node?.val;\n node = node?.next;\n }\n\n return res;\n }\n}\n
linkedlist_deque.go/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\ntype linkedListDeque struct {\n // \u4f7f\u7528\u5185\u7f6e\u5305 list\n data *list.List\n}\n\n/* \u521d\u59cb\u5316\u53cc\u7aef\u961f\u5217 */\nfunc newLinkedListDeque() *linkedListDeque {\n return &linkedListDeque{\n data: list.New(),\n }\n}\n\n/* \u961f\u9996\u5143\u7d20\u5165\u961f */\nfunc (s *linkedListDeque) pushFirst(value any) {\n s.data.PushFront(value)\n}\n\n/* \u961f\u5c3e\u5143\u7d20\u5165\u961f */\nfunc (s *linkedListDeque) pushLast(value any) {\n s.data.PushBack(value)\n}\n\n/* \u961f\u9996\u5143\u7d20\u51fa\u961f */\nfunc (s *linkedListDeque) popFirst() any {\n if s.isEmpty() {\n return nil\n }\n e := s.data.Front()\n s.data.Remove(e)\n return e.Value\n}\n\n/* \u961f\u5c3e\u5143\u7d20\u51fa\u961f */\nfunc (s *linkedListDeque) popLast() any {\n if s.isEmpty() {\n return nil\n }\n e := s.data.Back()\n s.data.Remove(e)\n return e.Value\n}\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nfunc (s *linkedListDeque) peekFirst() any {\n if s.isEmpty() {\n return nil\n }\n e := s.data.Front()\n return e.Value\n}\n\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\nfunc (s *linkedListDeque) peekLast() any {\n if s.isEmpty() {\n return nil\n }\n e := s.data.Back()\n return e.Value\n}\n\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nfunc (s *linkedListDeque) size() int {\n return s.data.Len()\n}\n\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nfunc (s *linkedListDeque) isEmpty() bool {\n return s.data.Len() == 0\n}\n\n/* \u83b7\u53d6 List \u7528\u4e8e\u6253\u5370 */\nfunc (s *linkedListDeque) toList() *list.List {\n return s.data\n}\n
linkedlist_deque.swift/* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\nclass ListNode {\n var val: Int // \u8282\u70b9\u503c\n var next: ListNode? // \u540e\u7ee7\u8282\u70b9\u5f15\u7528\n weak var prev: ListNode? // \u524d\u9a71\u8282\u70b9\u5f15\u7528\n\n init(val: Int) {\n self.val = val\n }\n}\n\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass LinkedListDeque {\n private var front: ListNode? // \u5934\u8282\u70b9 front\n private var rear: ListNode? // \u5c3e\u8282\u70b9 rear\n private var queSize: Int // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\n\n init() {\n queSize = 0\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n func size() -> Int {\n queSize\n }\n\n /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n func isEmpty() -> Bool {\n size() == 0\n }\n\n /* \u5165\u961f\u64cd\u4f5c */\n private func push(num: Int, isFront: Bool) {\n let node = ListNode(val: num)\n // \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n if isEmpty() {\n front = node\n rear = node\n }\n // \u961f\u9996\u5165\u961f\u64cd\u4f5c\n else if isFront {\n // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\n front?.prev = node\n node.next = front\n front = node // \u66f4\u65b0\u5934\u8282\u70b9\n }\n // \u961f\u5c3e\u5165\u961f\u64cd\u4f5c\n else {\n // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\n rear?.next = node\n node.prev = rear\n rear = node // \u66f4\u65b0\u5c3e\u8282\u70b9\n }\n queSize += 1 // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n }\n\n /* \u961f\u9996\u5165\u961f */\n func pushFirst(num: Int) {\n push(num: num, isFront: true)\n }\n\n /* \u961f\u5c3e\u5165\u961f */\n func pushLast(num: Int) {\n push(num: num, isFront: false)\n }\n\n /* \u51fa\u961f\u64cd\u4f5c */\n private func pop(isFront: Bool) -> Int {\n if isEmpty() {\n fatalError(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\n }\n let val: Int\n // \u961f\u9996\u51fa\u961f\u64cd\u4f5c\n if isFront {\n val = front!.val // \u6682\u5b58\u5934\u8282\u70b9\u503c\n // \u5220\u9664\u5934\u8282\u70b9\n let fNext = front?.next\n if fNext != nil {\n fNext?.prev = nil\n front?.next = nil\n }\n front = fNext // \u66f4\u65b0\u5934\u8282\u70b9\n }\n // \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\n else {\n val = rear!.val // \u6682\u5b58\u5c3e\u8282\u70b9\u503c\n // \u5220\u9664\u5c3e\u8282\u70b9\n let rPrev = rear?.prev\n if rPrev != nil {\n rPrev?.next = nil\n rear?.prev = nil\n }\n rear = rPrev // \u66f4\u65b0\u5c3e\u8282\u70b9\n }\n queSize -= 1 // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n return val\n }\n\n /* \u961f\u9996\u51fa\u961f */\n func popFirst() -> Int {\n pop(isFront: true)\n }\n\n /* \u961f\u5c3e\u51fa\u961f */\n func popLast() -> Int {\n pop(isFront: false)\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n func peekFirst() -> Int? {\n isEmpty() ? nil : front?.val\n }\n\n /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n func peekLast() -> Int? {\n isEmpty() ? nil : rear?.val\n }\n\n /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n func toArray() -> [Int] {\n var node = front\n var res = Array(repeating: 0, count: size())\n for i in res.indices {\n res[i] = node!.val\n node = node?.next\n }\n return res\n }\n}\n
linkedlist_deque.js/* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\nclass ListNode {\n prev; // \u524d\u9a71\u8282\u70b9\u5f15\u7528 (\u6307\u9488)\n next; // \u540e\u7ee7\u8282\u70b9\u5f15\u7528 (\u6307\u9488)\n val; // \u8282\u70b9\u503c\n\n constructor(val) {\n this.val = val;\n this.next = null;\n this.prev = null;\n }\n}\n\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass LinkedListDeque {\n #front; // \u5934\u8282\u70b9 front\n #rear; // \u5c3e\u8282\u70b9 rear\n #queSize; // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\n\n constructor() {\n this.#front = null;\n this.#rear = null;\n this.#queSize = 0;\n }\n\n /* \u961f\u5c3e\u5165\u961f\u64cd\u4f5c */\n pushLast(val) {\n const node = new ListNode(val);\n // \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n if (this.#queSize === 0) {\n this.#front = node;\n this.#rear = node;\n } else {\n // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\n this.#rear.next = node;\n node.prev = this.#rear;\n this.#rear = node; // \u66f4\u65b0\u5c3e\u8282\u70b9\n }\n this.#queSize++;\n }\n\n /* \u961f\u9996\u5165\u961f\u64cd\u4f5c */\n pushFirst(val) {\n const node = new ListNode(val);\n // \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n if (this.#queSize === 0) {\n this.#front = node;\n this.#rear = node;\n } else {\n // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\n this.#front.prev = node;\n node.next = this.#front;\n this.#front = node; // \u66f4\u65b0\u5934\u8282\u70b9\n }\n this.#queSize++;\n }\n\n /* \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c */\n popLast() {\n if (this.#queSize === 0) {\n return null;\n }\n const value = this.#rear.val; // \u5b58\u50a8\u5c3e\u8282\u70b9\u503c\n // \u5220\u9664\u5c3e\u8282\u70b9\n let temp = this.#rear.prev;\n if (temp !== null) {\n temp.next = null;\n this.#rear.prev = null;\n }\n this.#rear = temp; // \u66f4\u65b0\u5c3e\u8282\u70b9\n this.#queSize--;\n return value;\n }\n\n /* \u961f\u9996\u51fa\u961f\u64cd\u4f5c */\n popFirst() {\n if (this.#queSize === 0) {\n return null;\n }\n const value = this.#front.val; // \u5b58\u50a8\u5c3e\u8282\u70b9\u503c\n // \u5220\u9664\u5934\u8282\u70b9\n let temp = this.#front.next;\n if (temp !== null) {\n temp.prev = null;\n this.#front.next = null;\n }\n this.#front = temp; // \u66f4\u65b0\u5934\u8282\u70b9\n this.#queSize--;\n return value;\n }\n\n /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n peekLast() {\n return this.#queSize === 0 ? null : this.#rear.val;\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n peekFirst() {\n return this.#queSize === 0 ? null : this.#front.val;\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n size() {\n return this.#queSize;\n }\n\n /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n isEmpty() {\n return this.#queSize === 0;\n }\n\n /* \u6253\u5370\u53cc\u5411\u961f\u5217 */\n print() {\n const arr = [];\n let temp = this.#front;\n while (temp !== null) {\n arr.push(temp.val);\n temp = temp.next;\n }\n console.log('[' + arr.join(', ') + ']');\n }\n}\n
linkedlist_deque.ts/* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\nclass ListNode {\n prev: ListNode; // \u524d\u9a71\u8282\u70b9\u5f15\u7528 (\u6307\u9488)\n next: ListNode; // \u540e\u7ee7\u8282\u70b9\u5f15\u7528 (\u6307\u9488)\n val: number; // \u8282\u70b9\u503c\n\n constructor(val: number) {\n this.val = val;\n this.next = null;\n this.prev = null;\n }\n}\n\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass LinkedListDeque {\n private front: ListNode; // \u5934\u8282\u70b9 front\n private rear: ListNode; // \u5c3e\u8282\u70b9 rear\n private queSize: number; // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\n\n constructor() {\n this.front = null;\n this.rear = null;\n this.queSize = 0;\n }\n\n /* \u961f\u5c3e\u5165\u961f\u64cd\u4f5c */\n pushLast(val: number): void {\n const node: ListNode = new ListNode(val);\n // \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n if (this.queSize === 0) {\n this.front = node;\n this.rear = node;\n } else {\n // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\n this.rear.next = node;\n node.prev = this.rear;\n this.rear = node; // \u66f4\u65b0\u5c3e\u8282\u70b9\n }\n this.queSize++;\n }\n\n /* \u961f\u9996\u5165\u961f\u64cd\u4f5c */\n pushFirst(val: number): void {\n const node: ListNode = new ListNode(val);\n // \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n if (this.queSize === 0) {\n this.front = node;\n this.rear = node;\n } else {\n // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\n this.front.prev = node;\n node.next = this.front;\n this.front = node; // \u66f4\u65b0\u5934\u8282\u70b9\n }\n this.queSize++;\n }\n\n /* \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c */\n popLast(): number {\n if (this.queSize === 0) {\n return null;\n }\n const value: number = this.rear.val; // \u5b58\u50a8\u5c3e\u8282\u70b9\u503c\n // \u5220\u9664\u5c3e\u8282\u70b9\n let temp: ListNode = this.rear.prev;\n if (temp !== null) {\n temp.next = null;\n this.rear.prev = null;\n }\n this.rear = temp; // \u66f4\u65b0\u5c3e\u8282\u70b9\n this.queSize--;\n return value;\n }\n\n /* \u961f\u9996\u51fa\u961f\u64cd\u4f5c */\n popFirst(): number {\n if (this.queSize === 0) {\n return null;\n }\n const value: number = this.front.val; // \u5b58\u50a8\u5c3e\u8282\u70b9\u503c\n // \u5220\u9664\u5934\u8282\u70b9\n let temp: ListNode = this.front.next;\n if (temp !== null) {\n temp.prev = null;\n this.front.next = null;\n }\n this.front = temp; // \u66f4\u65b0\u5934\u8282\u70b9\n this.queSize--;\n return value;\n }\n\n /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n peekLast(): number {\n return this.queSize === 0 ? null : this.rear.val;\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n peekFirst(): number {\n return this.queSize === 0 ? null : this.front.val;\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n size(): number {\n return this.queSize;\n }\n\n /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n isEmpty(): boolean {\n return this.queSize === 0;\n }\n\n /* \u6253\u5370\u53cc\u5411\u961f\u5217 */\n print(): void {\n const arr: number[] = [];\n let temp: ListNode = this.front;\n while (temp !== null) {\n arr.push(temp.val);\n temp = temp.next;\n }\n console.log('[' + arr.join(', ') + ']');\n }\n}\n
linkedlist_deque.dart/* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\nclass ListNode {\n int val; // \u8282\u70b9\u503c\n ListNode? next; // \u540e\u7ee7\u8282\u70b9\u5f15\u7528\n ListNode? prev; // \u524d\u9a71\u8282\u70b9\u5f15\u7528\n\n ListNode(this.val, {this.next, this.prev});\n}\n\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u5bf9\u5217 */\nclass LinkedListDeque {\n late ListNode? _front; // \u5934\u8282\u70b9 _front\n late ListNode? _rear; // \u5c3e\u8282\u70b9 _rear\n int _queSize = 0; // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\n\n LinkedListDeque() {\n this._front = null;\n this._rear = null;\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u957f\u5ea6 */\n int size() {\n return this._queSize;\n }\n\n /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n bool isEmpty() {\n return size() == 0;\n }\n\n /* \u5165\u961f\u64cd\u4f5c */\n void push(int _num, bool isFront) {\n final ListNode node = ListNode(_num);\n if (isEmpty()) {\n // \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 _front \u548c _rear \u90fd\u6307\u5411 node\n _front = _rear = node;\n } else if (isFront) {\n // \u961f\u9996\u5165\u961f\u64cd\u4f5c\n // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\n _front!.prev = node;\n node.next = _front;\n _front = node; // \u66f4\u65b0\u5934\u8282\u70b9\n } else {\n // \u961f\u5c3e\u5165\u961f\u64cd\u4f5c\n // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\n _rear!.next = node;\n node.prev = _rear;\n _rear = node; // \u66f4\u65b0\u5c3e\u8282\u70b9\n }\n _queSize++; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n }\n\n /* \u961f\u9996\u5165\u961f */\n void pushFirst(int _num) {\n push(_num, true);\n }\n\n /* \u961f\u5c3e\u5165\u961f */\n void pushLast(int _num) {\n push(_num, false);\n }\n\n /* \u51fa\u961f\u64cd\u4f5c */\n int? pop(bool isFront) {\n // \u82e5\u961f\u5217\u4e3a\u7a7a\uff0c\u76f4\u63a5\u8fd4\u56de null\n if (isEmpty()) {\n return null;\n }\n final int val;\n if (isFront) {\n // \u961f\u9996\u51fa\u961f\u64cd\u4f5c\n val = _front!.val; // \u6682\u5b58\u5934\u8282\u70b9\u503c\n // \u5220\u9664\u5934\u8282\u70b9\n ListNode? fNext = _front!.next;\n if (fNext != null) {\n fNext.prev = null;\n _front!.next = null;\n }\n _front = fNext; // \u66f4\u65b0\u5934\u8282\u70b9\n } else {\n // \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\n val = _rear!.val; // \u6682\u5b58\u5c3e\u8282\u70b9\u503c\n // \u5220\u9664\u5c3e\u8282\u70b9\n ListNode? rPrev = _rear!.prev;\n if (rPrev != null) {\n rPrev.next = null;\n _rear!.prev = null;\n }\n _rear = rPrev; // \u66f4\u65b0\u5c3e\u8282\u70b9\n }\n _queSize--; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n return val;\n }\n\n /* \u961f\u9996\u51fa\u961f */\n int? popFirst() {\n return pop(true);\n }\n\n /* \u961f\u5c3e\u51fa\u961f */\n int? popLast() {\n return pop(false);\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n int? peekFirst() {\n return _front?.val;\n }\n\n /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n int? peekLast() {\n return _rear?.val;\n }\n\n /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n List<int> toArray() {\n ListNode? node = _front;\n final List<int> res = [];\n for (int i = 0; i < _queSize; i++) {\n res.add(node!.val);\n node = node.next;\n }\n return res;\n }\n}\n
linkedlist_deque.rs/* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\npub struct ListNode<T> {\n pub val: T, // \u8282\u70b9\u503c\n pub next: Option<Rc<RefCell<ListNode<T>>>>, // \u540e\u7ee7\u8282\u70b9\u6307\u9488\n pub prev: Option<Rc<RefCell<ListNode<T>>>>, // \u524d\u9a71\u8282\u70b9\u6307\u9488\n}\n\nimpl<T> ListNode<T> {\n pub fn new(val: T) -> Rc<RefCell<ListNode<T>>> {\n Rc::new(RefCell::new(ListNode {\n val,\n next: None,\n prev: None,\n }))\n }\n}\n\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\n#[allow(dead_code)]\npub struct LinkedListDeque<T> {\n front: Option<Rc<RefCell<ListNode<T>>>>, // \u5934\u8282\u70b9 front\n rear: Option<Rc<RefCell<ListNode<T>>>>, // \u5c3e\u8282\u70b9 rear \n que_size: usize, // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\n}\n\nimpl<T: Copy> LinkedListDeque<T> {\n pub fn new() -> Self {\n Self {\n front: None,\n rear: None,\n que_size: 0, \n }\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n pub fn size(&self) -> usize {\n return self.que_size;\n }\n\n /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n pub fn is_empty(&self) -> bool {\n return self.size() == 0;\n }\n\n /* \u5165\u961f\u64cd\u4f5c */\n pub fn push(&mut self, num: T, is_front: bool) {\n let node = ListNode::new(num);\n // \u961f\u9996\u5165\u961f\u64cd\u4f5c\n if is_front {\n match self.front.take() {\n // \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n None => {\n self.rear = Some(node.clone());\n self.front = Some(node);\n }\n // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\n Some(old_front) => {\n old_front.borrow_mut().prev = Some(node.clone());\n node.borrow_mut().next = Some(old_front);\n self.front = Some(node); // \u66f4\u65b0\u5934\u8282\u70b9\n }\n }\n } \n // \u961f\u5c3e\u5165\u961f\u64cd\u4f5c\n else {\n match self.rear.take() {\n // \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n None => {\n self.front = Some(node.clone());\n self.rear = Some(node);\n }\n // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\n Some(old_rear) => {\n old_rear.borrow_mut().next = Some(node.clone());\n node.borrow_mut().prev = Some(old_rear);\n self.rear = Some(node); // \u66f4\u65b0\u5c3e\u8282\u70b9\n }\n }\n }\n self.que_size += 1; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n }\n\n /* \u961f\u9996\u5165\u961f */\n pub fn push_first(&mut self, num: T) {\n self.push(num, true);\n }\n\n /* \u961f\u5c3e\u5165\u961f */\n pub fn push_last(&mut self, num: T) {\n self.push(num, false);\n }\n\n /* \u51fa\u961f\u64cd\u4f5c */\n pub fn pop(&mut self, is_front: bool) -> Option<T> {\n // \u82e5\u961f\u5217\u4e3a\u7a7a\uff0c\u76f4\u63a5\u8fd4\u56de None\n if self.is_empty() { \n return None \n };\n // \u961f\u9996\u51fa\u961f\u64cd\u4f5c\n if is_front {\n self.front.take().map(|old_front| {\n match old_front.borrow_mut().next.take() {\n Some(new_front) => {\n new_front.borrow_mut().prev.take();\n self.front = Some(new_front); // \u66f4\u65b0\u5934\u8282\u70b9\n }\n None => {\n self.rear.take();\n }\n }\n self.que_size -= 1; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n Rc::try_unwrap(old_front).ok().unwrap().into_inner().val\n })\n\n } \n // \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\n else {\n self.rear.take().map(|old_rear| {\n match old_rear.borrow_mut().prev.take() {\n Some(new_rear) => {\n new_rear.borrow_mut().next.take();\n self.rear = Some(new_rear); // \u66f4\u65b0\u5c3e\u8282\u70b9\n }\n None => {\n self.front.take();\n }\n }\n self.que_size -= 1; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n Rc::try_unwrap(old_rear).ok().unwrap().into_inner().val\n })\n }\n }\n\n /* \u961f\u9996\u51fa\u961f */\n pub fn pop_first(&mut self) -> Option<T> {\n return self.pop(true);\n }\n\n /* \u961f\u5c3e\u51fa\u961f */\n pub fn pop_last(&mut self) -> Option<T> {\n return self.pop(false);\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n pub fn peek_first(&self) -> Option<&Rc<RefCell<ListNode<T>>>> {\n self.front.as_ref()\n }\n\n /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n pub fn peek_last(&self) -> Option<&Rc<RefCell<ListNode<T>>>> {\n self.rear.as_ref()\n }\n\n /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n pub fn to_array(&self, head: Option<&Rc<RefCell<ListNode<T>>>>) -> Vec<T> {\n if let Some(node) = head {\n let mut nums = self.to_array(node.borrow().next.as_ref());\n nums.insert(0, node.borrow().val);\n return nums;\n }\n return Vec::new();\n }\n}\n
linkedlist_deque.c/* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\ntypedef struct DoublyListNode {\n int val; // \u8282\u70b9\u503c\n struct DoublyListNode *next; // \u540e\u7ee7\u8282\u70b9\n struct DoublyListNode *prev; // \u524d\u9a71\u8282\u70b9\n} DoublyListNode;\n\n/* \u6784\u9020\u51fd\u6570 */\nDoublyListNode *newDoublyListNode(int num) {\n DoublyListNode *new = (DoublyListNode *)malloc(sizeof(DoublyListNode));\n new->val = num;\n new->next = NULL;\n new->prev = NULL;\n return new;\n}\n\n/* \u6790\u6784\u51fd\u6570 */\nvoid delDoublyListNode(DoublyListNode *node) {\n free(node);\n}\n\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\ntypedef struct {\n DoublyListNode *front, *rear; // \u5934\u8282\u70b9 front \uff0c\u5c3e\u8282\u70b9 rear\n int queSize; // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\n} LinkedListDeque;\n\n/* \u6784\u9020\u51fd\u6570 */\nLinkedListDeque *newLinkedListDeque() {\n LinkedListDeque *deque = (LinkedListDeque *)malloc(sizeof(LinkedListDeque));\n deque->front = NULL;\n deque->rear = NULL;\n deque->queSize = 0;\n return deque;\n}\n\n/* \u6790\u6784\u51fd\u6570 */\nvoid delLinkedListdeque(LinkedListDeque *deque) {\n // \u91ca\u653e\u6240\u6709\u8282\u70b9\n for (int i = 0; i < deque->queSize && deque->front != NULL; i++) {\n DoublyListNode *tmp = deque->front;\n deque->front = deque->front->next;\n free(tmp);\n }\n // \u91ca\u653e deque \u7ed3\u6784\u4f53\n free(deque);\n}\n\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nint size(LinkedListDeque *deque) {\n return deque->queSize;\n}\n\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool empty(LinkedListDeque *deque) {\n return (size(deque) == 0);\n}\n\n/* \u5165\u961f */\nvoid push(LinkedListDeque *deque, int num, bool isFront) {\n DoublyListNode *node = newDoublyListNode(num);\n // \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front \u548c rear \u90fd\u6307\u5411node\n if (empty(deque)) {\n deque->front = deque->rear = node;\n }\n // \u961f\u9996\u5165\u961f\u64cd\u4f5c\n else if (isFront) {\n // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\n deque->front->prev = node;\n node->next = deque->front;\n deque->front = node; // \u66f4\u65b0\u5934\u8282\u70b9\n }\n // \u961f\u5c3e\u5165\u961f\u64cd\u4f5c\n else {\n // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\n deque->rear->next = node;\n node->prev = deque->rear;\n deque->rear = node;\n }\n deque->queSize++; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n}\n\n/* \u961f\u9996\u5165\u961f */\nvoid pushFirst(LinkedListDeque *deque, int num) {\n push(deque, num, true);\n}\n\n/* \u961f\u5c3e\u5165\u961f */\nvoid pushLast(LinkedListDeque *deque, int num) {\n push(deque, num, false);\n}\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint peekFirst(LinkedListDeque *deque) {\n assert(size(deque) && deque->front);\n return deque->front->val;\n}\n\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\nint peekLast(LinkedListDeque *deque) {\n assert(size(deque) && deque->rear);\n return deque->rear->val;\n}\n\n/* \u51fa\u961f */\nint pop(LinkedListDeque *deque, bool isFront) {\n if (empty(deque))\n return -1;\n int val;\n // \u961f\u9996\u51fa\u961f\u64cd\u4f5c\n if (isFront) {\n val = peekFirst(deque); // \u6682\u5b58\u5934\u8282\u70b9\u503c\n DoublyListNode *fNext = deque->front->next;\n if (fNext) {\n fNext->prev = NULL;\n deque->front->next = NULL;\n delDoublyListNode(deque->front);\n }\n deque->front = fNext; // \u66f4\u65b0\u5934\u8282\u70b9\n }\n // \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\n else {\n val = peekLast(deque); // \u6682\u5b58\u5c3e\u8282\u70b9\u503c\n DoublyListNode *rPrev = deque->rear->prev;\n if (rPrev) {\n rPrev->next = NULL;\n deque->rear->prev = NULL;\n delDoublyListNode(deque->rear);\n }\n deque->rear = rPrev; // \u66f4\u65b0\u5c3e\u8282\u70b9\n }\n deque->queSize--; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n return val;\n}\n\n/* \u961f\u9996\u51fa\u961f */\nint popFirst(LinkedListDeque *deque) {\n return pop(deque, true);\n}\n\n/* \u961f\u5c3e\u51fa\u961f */\nint popLast(LinkedListDeque *deque) {\n return pop(deque, false);\n}\n\n/* \u6253\u5370\u961f\u5217 */\nvoid printLinkedListDeque(LinkedListDeque *deque) {\n int *arr = malloc(sizeof(int) * deque->queSize);\n // \u62f7\u8d1d\u94fe\u8868\u4e2d\u7684\u6570\u636e\u5230\u6570\u7ec4\n int i;\n DoublyListNode *node;\n for (i = 0, node = deque->front; i < deque->queSize; i++) {\n arr[i] = node->val;\n node = node->next;\n }\n printArray(arr, deque->queSize);\n free(arr);\n}\n
linkedlist_deque.zig// \u53cc\u5411\u94fe\u8868\u8282\u70b9\nfn ListNode(comptime T: type) type {\n return struct {\n const Self = @This();\n\n val: T = undefined, // \u8282\u70b9\u503c\n next: ?*Self = null, // \u540e\u7ee7\u8282\u70b9\u6307\u9488\n prev: ?*Self = null, // \u524d\u9a71\u8282\u70b9\u6307\u9488\n\n // Initialize a list node with specific value\n pub fn init(self: *Self, x: i32) void {\n self.val = x;\n self.next = null;\n self.prev = null;\n }\n };\n}\n\n// \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217\nfn LinkedListDeque(comptime T: type) type {\n return struct {\n const Self = @This();\n\n front: ?*ListNode(T) = null, // \u5934\u8282\u70b9 front\n rear: ?*ListNode(T) = null, // \u5c3e\u8282\u70b9 rear\n que_size: usize = 0, // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\n mem_arena: ?std.heap.ArenaAllocator = null,\n mem_allocator: std.mem.Allocator = undefined, // \u5185\u5b58\u5206\u914d\u5668\n\n // \u6784\u9020\u51fd\u6570\uff08\u5206\u914d\u5185\u5b58+\u521d\u59cb\u5316\u961f\u5217\uff09\n pub fn init(self: *Self, allocator: std.mem.Allocator) !void {\n if (self.mem_arena == null) {\n self.mem_arena = std.heap.ArenaAllocator.init(allocator);\n self.mem_allocator = self.mem_arena.?.allocator();\n }\n self.front = null;\n self.rear = null;\n self.que_size = 0;\n }\n\n // \u6790\u6784\u51fd\u6570\uff08\u91ca\u653e\u5185\u5b58\uff09\n pub fn deinit(self: *Self) void {\n if (self.mem_arena == null) return;\n self.mem_arena.?.deinit();\n }\n\n // \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\n pub fn size(self: *Self) usize {\n return self.que_size;\n }\n\n // \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a\n pub fn isEmpty(self: *Self) bool {\n return self.size() == 0;\n }\n\n // \u5165\u961f\u64cd\u4f5c\n pub fn push(self: *Self, num: T, is_front: bool) !void {\n var node = try self.mem_allocator.create(ListNode(T));\n node.init(num);\n // \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n if (self.isEmpty()) {\n self.front = node;\n self.rear = node;\n // \u961f\u9996\u5165\u961f\u64cd\u4f5c\n } else if (is_front) {\n // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\n self.front.?.prev = node;\n node.next = self.front;\n self.front = node; // \u66f4\u65b0\u5934\u8282\u70b9\n // \u961f\u5c3e\u5165\u961f\u64cd\u4f5c\n } else {\n // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\n self.rear.?.next = node;\n node.prev = self.rear;\n self.rear = node; // \u66f4\u65b0\u5c3e\u8282\u70b9\n }\n self.que_size += 1; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n } \n\n // \u961f\u9996\u5165\u961f\n pub fn pushFirst(self: *Self, num: T) !void {\n try self.push(num, true);\n } \n\n // \u961f\u5c3e\u5165\u961f\n pub fn pushLast(self: *Self, num: T) !void {\n try self.push(num, false);\n } \n\n // \u51fa\u961f\u64cd\u4f5c\n pub fn pop(self: *Self, is_front: bool) T {\n if (self.isEmpty()) @panic(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\");\n var val: T = undefined;\n // \u961f\u9996\u51fa\u961f\u64cd\u4f5c\n if (is_front) {\n val = self.front.?.val; // \u6682\u5b58\u5934\u8282\u70b9\u503c\n // \u5220\u9664\u5934\u8282\u70b9\n var fNext = self.front.?.next;\n if (fNext != null) {\n fNext.?.prev = null;\n self.front.?.next = null;\n }\n self.front = fNext; // \u66f4\u65b0\u5934\u8282\u70b9\n // \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\n } else {\n val = self.rear.?.val; // \u6682\u5b58\u5c3e\u8282\u70b9\u503c\n // \u5220\u9664\u5c3e\u8282\u70b9\n var rPrev = self.rear.?.prev;\n if (rPrev != null) {\n rPrev.?.next = null;\n self.rear.?.prev = null;\n }\n self.rear = rPrev; // \u66f4\u65b0\u5c3e\u8282\u70b9\n }\n self.que_size -= 1; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n return val;\n } \n\n // \u961f\u9996\u51fa\u961f\n pub fn popFirst(self: *Self) T {\n return self.pop(true);\n } \n\n // \u961f\u5c3e\u51fa\u961f\n pub fn popLast(self: *Self) T {\n return self.pop(false);\n } \n\n // \u8bbf\u95ee\u961f\u9996\u5143\u7d20\n pub fn peekFirst(self: *Self) T {\n if (self.isEmpty()) @panic(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\");\n return self.front.?.val;\n } \n\n // \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20\n pub fn peekLast(self: *Self) T {\n if (self.isEmpty()) @panic(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\");\n return self.rear.?.val;\n }\n\n // \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370\n pub fn toArray(self: *Self) ![]T {\n var node = self.front;\n var res = try self.mem_allocator.alloc(T, self.size());\n @memset(res, @as(T, 0));\n var i: usize = 0;\n while (i < res.len) : (i += 1) {\n res[i] = node.?.val;\n node = node.?.next;\n }\n return res;\n }\n };\n}\n
"},{"location":"chapter_stack_and_queue/deque/#2","title":"2. \u00a0 \u57fa\u4e8e\u6570\u7ec4\u7684\u5b9e\u73b0","text":"\u5982\u56fe 5-9 \u6240\u793a\uff0c\u4e0e\u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u961f\u5217\u7c7b\u4f3c\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u4f7f\u7528\u73af\u5f62\u6570\u7ec4\u6765\u5b9e\u73b0\u53cc\u5411\u961f\u5217\u3002
ArrayDequepushLast()pushFirst()popLast()popFirst()\u56fe 5-9 \u00a0 \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u53cc\u5411\u961f\u5217\u7684\u5165\u961f\u51fa\u961f\u64cd\u4f5c
\u5728\u961f\u5217\u7684\u5b9e\u73b0\u57fa\u7840\u4e0a\uff0c\u4ec5\u9700\u589e\u52a0\u201c\u961f\u9996\u5165\u961f\u201d\u548c\u201c\u961f\u5c3e\u51fa\u961f\u201d\u7684\u65b9\u6cd5\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig array_deque.pyclass ArrayDeque:\n \"\"\"\u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217\"\"\"\n\n def __init__(self, capacity: int):\n \"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n self._nums: list[int] = [0] * capacity\n self._front: int = 0\n self._size: int = 0\n\n def capacity(self) -> int:\n \"\"\"\u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf\"\"\"\n return len(self._nums)\n\n def size(self) -> int:\n \"\"\"\u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\"\"\"\n return self._size\n\n def is_empty(self) -> bool:\n \"\"\"\u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a\"\"\"\n return self._size == 0\n\n def index(self, i: int) -> int:\n \"\"\"\u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15\"\"\"\n # \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n # \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\uff0c\u56de\u5230\u5934\u90e8\n # \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\n return (i + self.capacity()) % self.capacity()\n\n def push_first(self, num: int):\n \"\"\"\u961f\u9996\u5165\u961f\"\"\"\n if self._size == self.capacity():\n print(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\")\n return\n # \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n # \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\uff0c\u5b9e\u73b0 front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\u56de\u5230\u5c3e\u90e8\n self._front = self.index(self._front - 1)\n # \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\n self._nums[self._front] = num\n self._size += 1\n\n def push_last(self, num: int):\n \"\"\"\u961f\u5c3e\u5165\u961f\"\"\"\n if self._size == self.capacity():\n print(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\")\n return\n # \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n rear = self.index(self._front + self._size)\n # \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n self._nums[rear] = num\n self._size += 1\n\n def pop_first(self) -> int:\n \"\"\"\u961f\u9996\u51fa\u961f\"\"\"\n num = self.peek_first()\n # \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n self._front = self.index(self._front + 1)\n self._size -= 1\n return num\n\n def pop_last(self) -> int:\n \"\"\"\u961f\u5c3e\u51fa\u961f\"\"\"\n num = self.peek_last()\n self._size -= 1\n return num\n\n def peek_first(self) -> int:\n \"\"\"\u8bbf\u95ee\u961f\u9996\u5143\u7d20\"\"\"\n if self.is_empty():\n raise IndexError(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\n return self._nums[self._front]\n\n def peek_last(self) -> int:\n \"\"\"\u8bbf\u95ee\u961f\u5c3e\u5143\u7d20\"\"\"\n if self.is_empty():\n raise IndexError(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\n # \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\n last = self.index(self._front + self._size - 1)\n return self._nums[last]\n\n def to_array(self) -> list[int]:\n \"\"\"\u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370\"\"\"\n # \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n res = []\n for i in range(self._size):\n res.append(self._nums[self.index(self._front + i)])\n return res\n
array_deque.cpp/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass ArrayDeque {\n private:\n vector<int> nums; // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n int front; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n int queSize; // \u53cc\u5411\u961f\u5217\u957f\u5ea6\n\n public:\n /* \u6784\u9020\u65b9\u6cd5 */\n ArrayDeque(int capacity) {\n nums.resize(capacity);\n front = queSize = 0;\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\n int capacity() {\n return nums.size();\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n int size() {\n return queSize;\n }\n\n /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n bool isEmpty() {\n return queSize == 0;\n }\n\n /* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\n int index(int i) {\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\uff0c\u56de\u5230\u5934\u90e8\n // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\n return (i + capacity()) % capacity();\n }\n\n /* \u961f\u9996\u5165\u961f */\n void pushFirst(int num) {\n if (queSize == capacity()) {\n cout << \"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\" << endl;\n return;\n }\n // \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\uff0c\u5b9e\u73b0 front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\u56de\u5230\u5c3e\u90e8\n front = index(front - 1);\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\n nums[front] = num;\n queSize++;\n }\n\n /* \u961f\u5c3e\u5165\u961f */\n void pushLast(int num) {\n if (queSize == capacity()) {\n cout << \"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\" << endl;\n return;\n }\n // \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n int rear = index(front + queSize);\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n nums[rear] = num;\n queSize++;\n }\n\n /* \u961f\u9996\u51fa\u961f */\n int popFirst() {\n int num = peekFirst();\n // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n front = index(front + 1);\n queSize--;\n return num;\n }\n\n /* \u961f\u5c3e\u51fa\u961f */\n int popLast() {\n int num = peekLast();\n queSize--;\n return num;\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n int peekFirst() {\n if (isEmpty())\n throw out_of_range(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\");\n return nums[front];\n }\n\n /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n int peekLast() {\n if (isEmpty())\n throw out_of_range(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\");\n // \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\n int last = index(front + queSize - 1);\n return nums[last];\n }\n\n /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n vector<int> toVector() {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n vector<int> res(queSize);\n for (int i = 0, j = front; i < queSize; i++, j++) {\n res[i] = nums[index(j)];\n }\n return res;\n }\n};\n
array_deque.java/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass ArrayDeque {\n private int[] nums; // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n private int front; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n private int queSize; // \u53cc\u5411\u961f\u5217\u957f\u5ea6\n\n /* \u6784\u9020\u65b9\u6cd5 */\n public ArrayDeque(int capacity) {\n this.nums = new int[capacity];\n front = queSize = 0;\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\n public int capacity() {\n return nums.length;\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n public int size() {\n return queSize;\n }\n\n /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n public boolean isEmpty() {\n return queSize == 0;\n }\n\n /* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\n private int index(int i) {\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\uff0c\u56de\u5230\u5934\u90e8\n // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\n return (i + capacity()) % capacity();\n }\n\n /* \u961f\u9996\u5165\u961f */\n public void pushFirst(int num) {\n if (queSize == capacity()) {\n System.out.println(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\");\n return;\n }\n // \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\uff0c\u5b9e\u73b0 front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\u56de\u5230\u5c3e\u90e8\n front = index(front - 1);\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\n nums[front] = num;\n queSize++;\n }\n\n /* \u961f\u5c3e\u5165\u961f */\n public void pushLast(int num) {\n if (queSize == capacity()) {\n System.out.println(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\");\n return;\n }\n // \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n int rear = index(front + queSize);\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n nums[rear] = num;\n queSize++;\n }\n\n /* \u961f\u9996\u51fa\u961f */\n public int popFirst() {\n int num = peekFirst();\n // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n front = index(front + 1);\n queSize--;\n return num;\n }\n\n /* \u961f\u5c3e\u51fa\u961f */\n public int popLast() {\n int num = peekLast();\n queSize--;\n return num;\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n public int peekFirst() {\n if (isEmpty())\n throw new IndexOutOfBoundsException();\n return nums[front];\n }\n\n /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n public int peekLast() {\n if (isEmpty())\n throw new IndexOutOfBoundsException();\n // \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\n int last = index(front + queSize - 1);\n return nums[last];\n }\n\n /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n public int[] toArray() {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n int[] res = new int[queSize];\n for (int i = 0, j = front; i < queSize; i++, j++) {\n res[i] = nums[index(j)];\n }\n return res;\n }\n}\n
array_deque.cs/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass ArrayDeque {\n int[] nums; // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n int front; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n int queSize; // \u53cc\u5411\u961f\u5217\u957f\u5ea6\n\n /* \u6784\u9020\u65b9\u6cd5 */\n public ArrayDeque(int capacity) {\n nums = new int[capacity];\n front = queSize = 0;\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\n int Capacity() {\n return nums.Length;\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n public int Size() {\n return queSize;\n }\n\n /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n public bool IsEmpty() {\n return queSize == 0;\n }\n\n /* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\n int Index(int i) {\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\uff0c\u56de\u5230\u5934\u90e8\n // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\n return (i + Capacity()) % Capacity();\n }\n\n /* \u961f\u9996\u5165\u961f */\n public void PushFirst(int num) {\n if (queSize == Capacity()) {\n Console.WriteLine(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\");\n return;\n }\n // \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\uff0c\u5b9e\u73b0 front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\u56de\u5230\u5c3e\u90e8\n front = Index(front - 1);\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\n nums[front] = num;\n queSize++;\n }\n\n /* \u961f\u5c3e\u5165\u961f */\n public void PushLast(int num) {\n if (queSize == Capacity()) {\n Console.WriteLine(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\");\n return;\n }\n // \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n int rear = Index(front + queSize);\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n nums[rear] = num;\n queSize++;\n }\n\n /* \u961f\u9996\u51fa\u961f */\n public int PopFirst() {\n int num = PeekFirst();\n // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n front = Index(front + 1);\n queSize--;\n return num;\n }\n\n /* \u961f\u5c3e\u51fa\u961f */\n public int PopLast() {\n int num = PeekLast();\n queSize--;\n return num;\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n public int PeekFirst() {\n if (IsEmpty()) {\n throw new InvalidOperationException();\n }\n return nums[front];\n }\n\n /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n public int PeekLast() {\n if (IsEmpty()) {\n throw new InvalidOperationException();\n }\n // \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\n int last = Index(front + queSize - 1);\n return nums[last];\n }\n\n /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n public int[] ToArray() {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n int[] res = new int[queSize];\n for (int i = 0, j = front; i < queSize; i++, j++) {\n res[i] = nums[Index(j)];\n }\n return res;\n }\n}\n
array_deque.go/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\ntype arrayDeque struct {\n nums []int // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n front int // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n queSize int // \u53cc\u5411\u961f\u5217\u957f\u5ea6\n queCapacity int // \u961f\u5217\u5bb9\u91cf\uff08\u5373\u6700\u5927\u5bb9\u7eb3\u5143\u7d20\u6570\u91cf\uff09\n}\n\n/* \u521d\u59cb\u5316\u961f\u5217 */\nfunc newArrayDeque(queCapacity int) *arrayDeque {\n return &arrayDeque{\n nums: make([]int, queCapacity),\n queCapacity: queCapacity,\n front: 0,\n queSize: 0,\n }\n}\n\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nfunc (q *arrayDeque) size() int {\n return q.queSize\n}\n\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nfunc (q *arrayDeque) isEmpty() bool {\n return q.queSize == 0\n}\n\n/* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\nfunc (q *arrayDeque) index(i int) int {\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\uff0c\u56de\u5230\u5934\u90e8\n // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\n return (i + q.queCapacity) % q.queCapacity\n}\n\n/* \u961f\u9996\u5165\u961f */\nfunc (q *arrayDeque) pushFirst(num int) {\n if q.queSize == q.queCapacity {\n fmt.Println(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\")\n return\n }\n // \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\uff0c\u5b9e\u73b0 front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\u56de\u5230\u5c3e\u90e8\n q.front = q.index(q.front - 1)\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\n q.nums[q.front] = num\n q.queSize++\n}\n\n/* \u961f\u5c3e\u5165\u961f */\nfunc (q *arrayDeque) pushLast(num int) {\n if q.queSize == q.queCapacity {\n fmt.Println(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\")\n return\n }\n // \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n rear := q.index(q.front + q.queSize)\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\n q.nums[rear] = num\n q.queSize++\n}\n\n/* \u961f\u9996\u51fa\u961f */\nfunc (q *arrayDeque) popFirst() any {\n num := q.peekFirst()\n // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n q.front = q.index(q.front + 1)\n q.queSize--\n return num\n}\n\n/* \u961f\u5c3e\u51fa\u961f */\nfunc (q *arrayDeque) popLast() any {\n num := q.peekLast()\n q.queSize--\n return num\n}\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nfunc (q *arrayDeque) peekFirst() any {\n if q.isEmpty() {\n return nil\n }\n return q.nums[q.front]\n}\n\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\nfunc (q *arrayDeque) peekLast() any {\n if q.isEmpty() {\n return nil\n }\n // \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\n last := q.index(q.front + q.queSize - 1)\n return q.nums[last]\n}\n\n/* \u83b7\u53d6 Slice \u7528\u4e8e\u6253\u5370 */\nfunc (q *arrayDeque) toSlice() []int {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n res := make([]int, q.queSize)\n for i, j := 0, q.front; i < q.queSize; i++ {\n res[i] = q.nums[q.index(j)]\n j++\n }\n return res\n}\n
array_deque.swift/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass ArrayDeque {\n private var nums: [Int] // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n private var front: Int // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n private var queSize: Int // \u53cc\u5411\u961f\u5217\u957f\u5ea6\n\n /* \u6784\u9020\u65b9\u6cd5 */\n init(capacity: Int) {\n nums = Array(repeating: 0, count: capacity)\n front = 0\n queSize = 0\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\n func capacity() -> Int {\n nums.count\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n func size() -> Int {\n queSize\n }\n\n /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n func isEmpty() -> Bool {\n size() == 0\n }\n\n /* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\n private func index(i: Int) -> Int {\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\uff0c\u56de\u5230\u5934\u90e8\n // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\n (i + capacity()) % capacity()\n }\n\n /* \u961f\u9996\u5165\u961f */\n func pushFirst(num: Int) {\n if size() == capacity() {\n print(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\")\n return\n }\n // \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\uff0c\u5b9e\u73b0 front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\u56de\u5230\u5c3e\u90e8\n front = index(i: front - 1)\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\n nums[front] = num\n queSize += 1\n }\n\n /* \u961f\u5c3e\u5165\u961f */\n func pushLast(num: Int) {\n if size() == capacity() {\n print(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\")\n return\n }\n // \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n let rear = index(i: front + size())\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n nums[rear] = num\n queSize += 1\n }\n\n /* \u961f\u9996\u51fa\u961f */\n func popFirst() -> Int {\n let num = peekFirst()\n // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n front = index(i: front + 1)\n queSize -= 1\n return num\n }\n\n /* \u961f\u5c3e\u51fa\u961f */\n func popLast() -> Int {\n let num = peekLast()\n queSize -= 1\n return num\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n func peekFirst() -> Int {\n if isEmpty() {\n fatalError(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\n }\n return nums[front]\n }\n\n /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n func peekLast() -> Int {\n if isEmpty() {\n fatalError(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\n }\n // \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\n let last = index(i: front + size() - 1)\n return nums[last]\n }\n\n /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n func toArray() -> [Int] {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n var res = Array(repeating: 0, count: size())\n for (i, j) in sequence(first: (0, front), next: { $0 < self.size() - 1 ? ($0 + 1, $1 + 1) : nil }) {\n res[i] = nums[index(i: j)]\n }\n return res\n }\n}\n
array_deque.js/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass ArrayDeque {\n #nums; // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n #front; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n #queSize; // \u53cc\u5411\u961f\u5217\u957f\u5ea6\n\n /* \u6784\u9020\u65b9\u6cd5 */\n constructor(capacity) {\n this.#nums = new Array(capacity);\n this.#front = 0;\n this.#queSize = 0;\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\n capacity() {\n return this.#nums.length;\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n size() {\n return this.#queSize;\n }\n\n /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n isEmpty() {\n return this.#queSize === 0;\n }\n\n /* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\n index(i) {\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\uff0c\u56de\u5230\u5934\u90e8\n // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\n return (i + this.capacity()) % this.capacity();\n }\n\n /* \u961f\u9996\u5165\u961f */\n pushFirst(num) {\n if (this.#queSize === this.capacity()) {\n console.log('\u53cc\u5411\u961f\u5217\u5df2\u6ee1');\n return;\n }\n // \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\uff0c\u5b9e\u73b0 front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\u56de\u5230\u5c3e\u90e8\n this.#front = this.index(this.#front - 1);\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\n this.#nums[this.#front] = num;\n this.#queSize++;\n }\n\n /* \u961f\u5c3e\u5165\u961f */\n pushLast(num) {\n if (this.#queSize === this.capacity()) {\n console.log('\u53cc\u5411\u961f\u5217\u5df2\u6ee1');\n return;\n }\n // \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n const rear = this.index(this.#front + this.#queSize);\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n this.#nums[rear] = num;\n this.#queSize++;\n }\n\n /* \u961f\u9996\u51fa\u961f */\n popFirst() {\n const num = this.peekFirst();\n // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n this.#front = this.index(this.#front + 1);\n this.#queSize--;\n return num;\n }\n\n /* \u961f\u5c3e\u51fa\u961f */\n popLast() {\n const num = this.peekLast();\n this.#queSize--;\n return num;\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n peekFirst() {\n if (this.isEmpty()) throw new Error('The Deque Is Empty.');\n return this.#nums[this.#front];\n }\n\n /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n peekLast() {\n if (this.isEmpty()) throw new Error('The Deque Is Empty.');\n // \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\n const last = this.index(this.#front + this.#queSize - 1);\n return this.#nums[last];\n }\n\n /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n toArray() {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n const res = [];\n for (let i = 0, j = this.#front; i < this.#queSize; i++, j++) {\n res[i] = this.#nums[this.index(j)];\n }\n return res;\n }\n}\n
array_deque.ts/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass ArrayDeque {\n private nums: number[]; // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n private front: number; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n private queSize: number; // \u53cc\u5411\u961f\u5217\u957f\u5ea6\n\n /* \u6784\u9020\u65b9\u6cd5 */\n constructor(capacity: number) {\n this.nums = new Array(capacity);\n this.front = 0;\n this.queSize = 0;\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\n capacity(): number {\n return this.nums.length;\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n size(): number {\n return this.queSize;\n }\n\n /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n isEmpty(): boolean {\n return this.queSize === 0;\n }\n\n /* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\n index(i: number): number {\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\uff0c\u56de\u5230\u5934\u90e8\n // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\n return (i + this.capacity()) % this.capacity();\n }\n\n /* \u961f\u9996\u5165\u961f */\n pushFirst(num: number): void {\n if (this.queSize === this.capacity()) {\n console.log('\u53cc\u5411\u961f\u5217\u5df2\u6ee1');\n return;\n }\n // \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\uff0c\u5b9e\u73b0 front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\u56de\u5230\u5c3e\u90e8\n this.front = this.index(this.front - 1);\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\n this.nums[this.front] = num;\n this.queSize++;\n }\n\n /* \u961f\u5c3e\u5165\u961f */\n pushLast(num: number): void {\n if (this.queSize === this.capacity()) {\n console.log('\u53cc\u5411\u961f\u5217\u5df2\u6ee1');\n return;\n }\n // \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n const rear: number = this.index(this.front + this.queSize);\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n this.nums[rear] = num;\n this.queSize++;\n }\n\n /* \u961f\u9996\u51fa\u961f */\n popFirst(): number {\n const num: number = this.peekFirst();\n // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n this.front = this.index(this.front + 1);\n this.queSize--;\n return num;\n }\n\n /* \u961f\u5c3e\u51fa\u961f */\n popLast(): number {\n const num: number = this.peekLast();\n this.queSize--;\n return num;\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n peekFirst(): number {\n if (this.isEmpty()) throw new Error('The Deque Is Empty.');\n return this.nums[this.front];\n }\n\n /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n peekLast(): number {\n if (this.isEmpty()) throw new Error('The Deque Is Empty.');\n // \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\n const last = this.index(this.front + this.queSize - 1);\n return this.nums[last];\n }\n\n /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n toArray(): number[] {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n const res: number[] = [];\n for (let i = 0, j = this.front; i < this.queSize; i++, j++) {\n res[i] = this.nums[this.index(j)];\n }\n return res;\n }\n}\n
array_deque.dart/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass ArrayDeque {\n late List<int> _nums; // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n late int _front; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n late int _queSize; // \u53cc\u5411\u961f\u5217\u957f\u5ea6\n\n /* \u6784\u9020\u65b9\u6cd5 */\n ArrayDeque(int capacity) {\n this._nums = List.filled(capacity, 0);\n this._front = this._queSize = 0;\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\n int capacity() {\n return _nums.length;\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n int size() {\n return _queSize;\n }\n\n /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n bool isEmpty() {\n return _queSize == 0;\n }\n\n /* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\n int index(int i) {\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\uff0c\u56de\u5230\u5934\u90e8\n // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\n return (i + capacity()) % capacity();\n }\n\n /* \u961f\u9996\u5165\u961f */\n void pushFirst(int _num) {\n if (_queSize == capacity()) {\n throw Exception(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\");\n }\n // \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\uff0c\u5b9e\u73b0 _front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\u56de\u5230\u5c3e\u90e8\n _front = index(_front - 1);\n // \u5c06 _num \u6dfb\u52a0\u81f3\u961f\u9996\n _nums[_front] = _num;\n _queSize++;\n }\n\n /* \u961f\u5c3e\u5165\u961f */\n void pushLast(int _num) {\n if (_queSize == capacity()) {\n throw Exception(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\");\n }\n // \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n int rear = index(_front + _queSize);\n // \u5c06 _num \u6dfb\u52a0\u81f3\u961f\u5c3e\n _nums[rear] = _num;\n _queSize++;\n }\n\n /* \u961f\u9996\u51fa\u961f */\n int popFirst() {\n int _num = peekFirst();\n // \u961f\u9996\u6307\u9488\u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\n _front = index(_front + 1);\n _queSize--;\n return _num;\n }\n\n /* \u961f\u5c3e\u51fa\u961f */\n int popLast() {\n int _num = peekLast();\n _queSize--;\n return _num;\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n int peekFirst() {\n if (isEmpty()) {\n throw Exception(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\");\n }\n return _nums[_front];\n }\n\n /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n int peekLast() {\n if (isEmpty()) {\n throw Exception(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\");\n }\n // \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\n int last = index(_front + _queSize - 1);\n return _nums[last];\n }\n\n /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n List<int> toArray() {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n List<int> res = List.filled(_queSize, 0);\n for (int i = 0, j = _front; i < _queSize; i++, j++) {\n res[i] = _nums[index(j)];\n }\n return res;\n }\n}\n
array_deque.rs/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nstruct ArrayDeque {\n nums: Vec<i32>, // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n front: usize, // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n que_size: usize, // \u53cc\u5411\u961f\u5217\u957f\u5ea6\n}\n\nimpl ArrayDeque {\n /* \u6784\u9020\u65b9\u6cd5 */\n pub fn new(capacity: usize) -> Self {\n Self {\n nums: vec![0; capacity],\n front: 0,\n que_size: 0,\n }\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\n pub fn capacity(&self) -> usize {\n self.nums.len()\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n pub fn size(&self) -> usize {\n self.que_size\n }\n\n /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n pub fn is_empty(&self) -> bool {\n self.que_size == 0\n }\n\n /* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\n fn index(&self, i: i32) -> usize {\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\uff0c\u56de\u5230\u5934\u90e8\n // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\n return ((i + self.capacity() as i32) % self.capacity() as i32) as usize;\n }\n\n /* \u961f\u9996\u5165\u961f */\n pub fn push_first(&mut self, num: i32) {\n if self.que_size == self.capacity() {\n println!(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\");\n return\n }\n // \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\uff0c\u5b9e\u73b0 front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\u56de\u5230\u5c3e\u90e8\n self.front = self.index(self.front as i32 - 1);\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\n self.nums[self.front] = num;\n self.que_size += 1;\n }\n\n /* \u961f\u5c3e\u5165\u961f */\n pub fn push_last(&mut self, num: i32) {\n if self.que_size == self.capacity() {\n println!(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\");\n return\n }\n // \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n let rear = self.index(self.front as i32 + self.que_size as i32);\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n self.nums[rear] = num;\n self.que_size += 1;\n }\n\n /* \u961f\u9996\u51fa\u961f */\n fn pop_first(&mut self) -> i32 {\n let num = self.peek_first();\n // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n self.front = self.index(self.front as i32 + 1);\n self.que_size -= 1;\n num\n }\n\n /* \u961f\u5c3e\u51fa\u961f */\n fn pop_last(&mut self) -> i32 {\n let num = self.peek_last();\n self.que_size -= 1;\n num\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n fn peek_first(&self) -> i32 {\n if self.is_empty() { panic!(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\") };\n self.nums[self.front]\n }\n\n /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n fn peek_last(&self) -> i32 {\n if self.is_empty() { panic!(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\") };\n // \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\n let last = self.index(self.front as i32 + self.que_size as i32 - 1);\n self.nums[last]\n }\n\n /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n fn to_array(&self) -> Vec<i32> {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n let mut res = vec![0; self.que_size];\n let mut j = self.front;\n for i in 0..self.que_size {\n res[i] = self.nums[self.index(j as i32)];\n j += 1;\n }\n res\n }\n}\n
array_deque.c/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\ntypedef struct {\n int *nums; // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n int front; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n int queSize; // \u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e + 1\n int queCapacity; // \u961f\u5217\u5bb9\u91cf\n} ArrayDeque;\n\n/* \u6784\u9020\u51fd\u6570 */\nArrayDeque *newArrayDeque(int capacity) {\n ArrayDeque *deque = (ArrayDeque *)malloc(sizeof(ArrayDeque));\n // \u521d\u59cb\u5316\u6570\u7ec4\n deque->queCapacity = capacity;\n deque->nums = (int *)malloc(sizeof(int) * deque->queCapacity);\n deque->front = deque->queSize = 0;\n return deque;\n}\n\n/* \u6790\u6784\u51fd\u6570 */\nvoid delArrayDeque(ArrayDeque *deque) {\n free(deque->nums);\n free(deque);\n}\n\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\nint capacity(ArrayDeque *deque) {\n return deque->queCapacity;\n}\n\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nint size(ArrayDeque *deque) {\n return deque->queSize;\n}\n\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool empty(ArrayDeque *deque) {\n return deque->queSize == 0;\n}\n\n/* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\nint dequeIndex(ArrayDeque *deque, int i) {\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u56de\u5230\u5934\u90e8\n // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\n return ((i + capacity(deque)) % capacity(deque));\n}\n\n/* \u961f\u9996\u5165\u961f */\nvoid pushFirst(ArrayDeque *deque, int num) {\n if (deque->queSize == capacity(deque)) {\n printf(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\\r\\n\");\n return;\n }\n // \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\uff0c\u5b9e\u73b0 front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u56de\u5230\u5c3e\u90e8\n deque->front = dequeIndex(deque, deque->front - 1);\n // \u5c06 num \u6dfb\u52a0\u5230\u961f\u9996\n deque->nums[deque->front] = num;\n deque->queSize++;\n}\n\n/* \u961f\u5c3e\u5165\u961f */\nvoid pushLast(ArrayDeque *deque, int num) {\n if (deque->queSize == capacity(deque)) {\n printf(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\\r\\n\");\n return;\n }\n // \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n int rear = dequeIndex(deque, deque->front + deque->queSize);\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n deque->nums[rear] = num;\n deque->queSize++;\n}\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint peekFirst(ArrayDeque *deque) {\n // \u8bbf\u95ee\u5f02\u5e38\uff1a\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\n assert(empty(deque) == 0);\n return deque->nums[deque->front];\n}\n\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\nint peekLast(ArrayDeque *deque) {\n // \u8bbf\u95ee\u5f02\u5e38\uff1a\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\n assert(empty(deque) == 0);\n int last = dequeIndex(deque, deque->front + deque->queSize - 1);\n return deque->nums[last];\n}\n\n/* \u961f\u9996\u51fa\u961f */\nint popFirst(ArrayDeque *deque) {\n int num = peekFirst(deque);\n // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n deque->front = dequeIndex(deque, deque->front + 1);\n deque->queSize--;\n return num;\n}\n\n/* \u961f\u5c3e\u51fa\u961f */\nint popLast(ArrayDeque *deque) {\n int num = peekLast(deque);\n deque->queSize--;\n return num;\n}\n
array_deque.zig[class]{ArrayDeque}-[func]{}\n
"},{"location":"chapter_stack_and_queue/deque/#533","title":"5.3.3 \u00a0 \u53cc\u5411\u961f\u5217\u5e94\u7528","text":"\u53cc\u5411\u961f\u5217\u517c\u5177\u6808\u4e0e\u961f\u5217\u7684\u903b\u8f91\uff0c\u56e0\u6b64\u5b83\u53ef\u4ee5\u5b9e\u73b0\u8fd9\u4e24\u8005\u7684\u6240\u6709\u5e94\u7528\u573a\u666f\uff0c\u540c\u65f6\u63d0\u4f9b\u66f4\u9ad8\u7684\u81ea\u7531\u5ea6\u3002
\u6211\u4eec\u77e5\u9053\uff0c\u8f6f\u4ef6\u7684\u201c\u64a4\u9500\u201d\u529f\u80fd\u901a\u5e38\u4f7f\u7528\u6808\u6765\u5b9e\u73b0\uff1a\u7cfb\u7edf\u5c06\u6bcf\u6b21\u66f4\u6539\u64cd\u4f5c push
\u5230\u6808\u4e2d\uff0c\u7136\u540e\u901a\u8fc7 pop
\u5b9e\u73b0\u64a4\u9500\u3002\u7136\u800c\uff0c\u8003\u8651\u5230\u7cfb\u7edf\u8d44\u6e90\u7684\u9650\u5236\uff0c\u8f6f\u4ef6\u901a\u5e38\u4f1a\u9650\u5236\u64a4\u9500\u7684\u6b65\u6570\uff08\u4f8b\u5982\u4ec5\u5141\u8bb8\u4fdd\u5b58 \\(50\\) \u6b65\uff09\u3002\u5f53\u6808\u7684\u957f\u5ea6\u8d85\u8fc7 \\(50\\) \u65f6\uff0c\u8f6f\u4ef6\u9700\u8981\u5728\u6808\u5e95\uff08\u961f\u9996\uff09\u6267\u884c\u5220\u9664\u64cd\u4f5c\u3002\u4f46\u6808\u65e0\u6cd5\u5b9e\u73b0\u8be5\u529f\u80fd\uff0c\u6b64\u65f6\u5c31\u9700\u8981\u4f7f\u7528\u53cc\u5411\u961f\u5217\u6765\u66ff\u4ee3\u6808\u3002\u8bf7\u6ce8\u610f\uff0c\u201c\u64a4\u9500\u201d\u7684\u6838\u5fc3\u903b\u8f91\u4ecd\u7136\u9075\u5faa\u6808\u7684\u5148\u5165\u540e\u51fa\u539f\u5219\uff0c\u53ea\u662f\u53cc\u5411\u961f\u5217\u80fd\u591f\u66f4\u52a0\u7075\u6d3b\u5730\u5b9e\u73b0\u4e00\u4e9b\u989d\u5916\u903b\u8f91\u3002
\u300c\u961f\u5217 queue\u300d\u662f\u4e00\u79cd\u9075\u5faa\u5148\u5165\u5148\u51fa\u89c4\u5219\u7684\u7ebf\u6027\u6570\u636e\u7ed3\u6784\u3002\u987e\u540d\u601d\u4e49\uff0c\u961f\u5217\u6a21\u62df\u4e86\u6392\u961f\u73b0\u8c61\uff0c\u5373\u65b0\u6765\u7684\u4eba\u4e0d\u65ad\u52a0\u5165\u961f\u5217\u5c3e\u90e8\uff0c\u800c\u4f4d\u4e8e\u961f\u5217\u5934\u90e8\u7684\u4eba\u9010\u4e2a\u79bb\u5f00\u3002
\u5982\u56fe 5-4 \u6240\u793a\uff0c\u6211\u4eec\u5c06\u961f\u5217\u5934\u90e8\u79f0\u4e3a\u201c\u961f\u9996\u201d\uff0c\u5c3e\u90e8\u79f0\u4e3a\u201c\u961f\u5c3e\u201d\uff0c\u5c06\u628a\u5143\u7d20\u52a0\u5165\u961f\u5c3e\u7684\u64cd\u4f5c\u79f0\u4e3a\u201c\u5165\u961f\u201d\uff0c\u5220\u9664\u961f\u9996\u5143\u7d20\u7684\u64cd\u4f5c\u79f0\u4e3a\u201c\u51fa\u961f\u201d\u3002
\u56fe 5-4 \u00a0 \u961f\u5217\u7684\u5148\u5165\u5148\u51fa\u89c4\u5219
"},{"location":"chapter_stack_and_queue/queue/#521","title":"5.2.1 \u00a0 \u961f\u5217\u5e38\u7528\u64cd\u4f5c","text":"\u961f\u5217\u7684\u5e38\u89c1\u64cd\u4f5c\u5982\u8868 5-2 \u6240\u793a\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u4e0d\u540c\u7f16\u7a0b\u8bed\u8a00\u7684\u65b9\u6cd5\u540d\u79f0\u53ef\u80fd\u4f1a\u6709\u6240\u4e0d\u540c\u3002\u6211\u4eec\u5728\u6b64\u91c7\u7528\u4e0e\u6808\u76f8\u540c\u7684\u65b9\u6cd5\u547d\u540d\u3002
\u8868 5-2 \u00a0 \u961f\u5217\u64cd\u4f5c\u6548\u7387
\u65b9\u6cd5\u540d \u63cf\u8ff0 \u65f6\u95f4\u590d\u6742\u5ea6 push() \u5143\u7d20\u5165\u961f\uff0c\u5373\u5c06\u5143\u7d20\u6dfb\u52a0\u81f3\u961f\u5c3e \\(O(1)\\) pop() \u961f\u9996\u5143\u7d20\u51fa\u961f \\(O(1)\\) peek() \u8bbf\u95ee\u961f\u9996\u5143\u7d20 \\(O(1)\\)\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u7f16\u7a0b\u8bed\u8a00\u4e2d\u73b0\u6210\u7684\u961f\u5217\u7c7b\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig queue.pyfrom collections import deque\n\n# \u521d\u59cb\u5316\u961f\u5217\n# \u5728 Python \u4e2d\uff0c\u6211\u4eec\u4e00\u822c\u5c06\u53cc\u5411\u961f\u5217\u7c7b deque \u5f53\u4f5c\u961f\u5217\u4f7f\u7528\n# \u867d\u7136 queue.Queue() \u662f\u7eaf\u6b63\u7684\u961f\u5217\u7c7b\uff0c\u4f46\u4e0d\u592a\u597d\u7528\uff0c\u56e0\u6b64\u4e0d\u63a8\u8350\nque: deque[int] = deque()\n\n# \u5143\u7d20\u5165\u961f\nque.append(1)\nque.append(3)\nque.append(2)\nque.append(5)\nque.append(4)\n\n# \u8bbf\u95ee\u961f\u9996\u5143\u7d20\nfront: int = que[0];\n\n# \u5143\u7d20\u51fa\u961f\npop: int = que.popleft()\n\n# \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6\nsize: int = len(que)\n\n# \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a\nis_empty: bool = len(que) == 0\n
queue.cpp/* \u521d\u59cb\u5316\u961f\u5217 */\nqueue<int> queue;\n\n/* \u5143\u7d20\u5165\u961f */\nqueue.push(1);\nqueue.push(3);\nqueue.push(2);\nqueue.push(5);\nqueue.push(4);\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint front = queue.front();\n\n/* \u5143\u7d20\u51fa\u961f */\nqueue.pop();\n\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nint size = queue.size();\n\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool empty = queue.empty();\n
queue.java/* \u521d\u59cb\u5316\u961f\u5217 */\nQueue<Integer> queue = new LinkedList<>();\n\n/* \u5143\u7d20\u5165\u961f */\nqueue.offer(1);\nqueue.offer(3);\nqueue.offer(2);\nqueue.offer(5);\nqueue.offer(4);\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint peek = queue.peek();\n\n/* \u5143\u7d20\u51fa\u961f */\nint pop = queue.poll();\n\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nint size = queue.size();\n\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nboolean isEmpty = queue.isEmpty();\n
queue.cs/* \u521d\u59cb\u5316\u961f\u5217 */\nQueue<int> queue = new();\n\n/* \u5143\u7d20\u5165\u961f */\nqueue.Enqueue(1);\nqueue.Enqueue(3);\nqueue.Enqueue(2);\nqueue.Enqueue(5);\nqueue.Enqueue(4);\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint peek = queue.Peek();\n\n/* \u5143\u7d20\u51fa\u961f */\nint pop = queue.Dequeue();\n\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nint size = queue.Count;\n\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty = queue.Count == 0;\n
queue_test.go/* \u521d\u59cb\u5316\u961f\u5217 */\n// \u5728 Go \u4e2d\uff0c\u5c06 list \u4f5c\u4e3a\u961f\u5217\u6765\u4f7f\u7528\nqueue := list.New()\n\n/* \u5143\u7d20\u5165\u961f */\nqueue.PushBack(1)\nqueue.PushBack(3)\nqueue.PushBack(2)\nqueue.PushBack(5)\nqueue.PushBack(4)\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\npeek := queue.Front()\n\n/* \u5143\u7d20\u51fa\u961f */\npop := queue.Front()\nqueue.Remove(pop)\n\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nsize := queue.Len()\n\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nisEmpty := queue.Len() == 0\n
queue.swift/* \u521d\u59cb\u5316\u961f\u5217 */\n// Swift \u6ca1\u6709\u5185\u7f6e\u7684\u961f\u5217\u7c7b\uff0c\u53ef\u4ee5\u628a Array \u5f53\u4f5c\u961f\u5217\u6765\u4f7f\u7528\nvar queue: [Int] = []\n\n/* \u5143\u7d20\u5165\u961f */\nqueue.append(1)\nqueue.append(3)\nqueue.append(2)\nqueue.append(5)\nqueue.append(4)\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nlet peek = queue.first!\n\n/* \u5143\u7d20\u51fa\u961f */\n// \u7531\u4e8e\u662f\u6570\u7ec4\uff0c\u56e0\u6b64 removeFirst \u7684\u590d\u6742\u5ea6\u4e3a O(n)\nlet pool = queue.removeFirst()\n\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nlet size = queue.count\n\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nlet isEmpty = queue.isEmpty\n
queue.js/* \u521d\u59cb\u5316\u961f\u5217 */\n// JavaScript \u6ca1\u6709\u5185\u7f6e\u7684\u961f\u5217\uff0c\u53ef\u4ee5\u628a Array \u5f53\u4f5c\u961f\u5217\u6765\u4f7f\u7528\nconst queue = [];\n\n/* \u5143\u7d20\u5165\u961f */\nqueue.push(1);\nqueue.push(3);\nqueue.push(2);\nqueue.push(5);\nqueue.push(4);\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nconst peek = queue[0];\n\n/* \u5143\u7d20\u51fa\u961f */\n// \u5e95\u5c42\u662f\u6570\u7ec4\uff0c\u56e0\u6b64 shift() \u65b9\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\nconst pop = queue.shift();\n\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nconst size = queue.length;\n\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nconst empty = queue.length === 0;\n
queue.ts/* \u521d\u59cb\u5316\u961f\u5217 */\n// TypeScript \u6ca1\u6709\u5185\u7f6e\u7684\u961f\u5217\uff0c\u53ef\u4ee5\u628a Array \u5f53\u4f5c\u961f\u5217\u6765\u4f7f\u7528 \nconst queue: number[] = [];\n\n/* \u5143\u7d20\u5165\u961f */\nqueue.push(1);\nqueue.push(3);\nqueue.push(2);\nqueue.push(5);\nqueue.push(4);\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nconst peek = queue[0];\n\n/* \u5143\u7d20\u51fa\u961f */\n// \u5e95\u5c42\u662f\u6570\u7ec4\uff0c\u56e0\u6b64 shift() \u65b9\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\nconst pop = queue.shift();\n\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nconst size = queue.length;\n\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nconst empty = queue.length === 0;\n
queue.dart/* \u521d\u59cb\u5316\u961f\u5217 */\n// \u5728 Dart \u4e2d\uff0c\u961f\u5217\u7c7b Qeque \u662f\u53cc\u5411\u961f\u5217\uff0c\u4e5f\u53ef\u4f5c\u4e3a\u961f\u5217\u4f7f\u7528\nQueue<int> queue = Queue();\n\n/* \u5143\u7d20\u5165\u961f */\nqueue.add(1);\nqueue.add(3);\nqueue.add(2);\nqueue.add(5);\nqueue.add(4);\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint peek = queue.first;\n\n/* \u5143\u7d20\u51fa\u961f */\nint pop = queue.removeFirst();\n\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nint size = queue.length;\n\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty = queue.isEmpty;\n
queue.rs/* \u521d\u59cb\u5316\u53cc\u5411\u961f\u5217 */\n// \u5728 Rust \u4e2d\u4f7f\u7528\u53cc\u5411\u961f\u5217\u4f5c\u4e3a\u666e\u901a\u961f\u5217\u6765\u4f7f\u7528\nlet mut deque: VecDeque<u32> = VecDeque::new();\n\n/* \u5143\u7d20\u5165\u961f */\ndeque.push_back(1);\ndeque.push_back(3);\ndeque.push_back(2);\ndeque.push_back(5);\ndeque.push_back(4);\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nif let Some(front) = deque.front() {\n}\n\n/* \u5143\u7d20\u51fa\u961f */\nif let Some(pop) = deque.pop_front() {\n}\n\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nlet size = deque.len();\n\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nlet is_empty = deque.is_empty();\n
queue.c// C \u672a\u63d0\u4f9b\u5185\u7f6e\u961f\u5217\n
queue.zig\n
"},{"location":"chapter_stack_and_queue/queue/#522","title":"5.2.2 \u00a0 \u961f\u5217\u5b9e\u73b0","text":"\u4e3a\u4e86\u5b9e\u73b0\u961f\u5217\uff0c\u6211\u4eec\u9700\u8981\u4e00\u79cd\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u5728\u4e00\u7aef\u6dfb\u52a0\u5143\u7d20\uff0c\u5e76\u5728\u53e6\u4e00\u7aef\u5220\u9664\u5143\u7d20\u3002\u94fe\u8868\u548c\u6570\u7ec4\u90fd\u7b26\u5408\u8981\u6c42\u3002
"},{"location":"chapter_stack_and_queue/queue/#1","title":"1. \u00a0 \u57fa\u4e8e\u94fe\u8868\u7684\u5b9e\u73b0","text":"\u5982\u56fe 5-5 \u6240\u793a\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u94fe\u8868\u7684\u201c\u5934\u8282\u70b9\u201d\u548c\u201c\u5c3e\u8282\u70b9\u201d\u5206\u522b\u89c6\u4e3a\u201c\u961f\u9996\u201d\u548c\u201c\u961f\u5c3e\u201d\uff0c\u89c4\u5b9a\u961f\u5c3e\u4ec5\u53ef\u6dfb\u52a0\u8282\u70b9\uff0c\u961f\u9996\u4ec5\u53ef\u5220\u9664\u8282\u70b9\u3002
LinkedListQueuepush()pop()\u56fe 5-5 \u00a0 \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u961f\u5217\u7684\u5165\u961f\u51fa\u961f\u64cd\u4f5c
\u4ee5\u4e0b\u662f\u7528\u94fe\u8868\u5b9e\u73b0\u961f\u5217\u7684\u4ee3\u7801\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig linkedlist_queue.pyclass LinkedListQueue:\n \"\"\"\u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217\"\"\"\n\n def __init__(self):\n \"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n self._front: ListNode | None = None # \u5934\u8282\u70b9 front\n self._rear: ListNode | None = None # \u5c3e\u8282\u70b9 rear\n self._size: int = 0\n\n def size(self) -> int:\n \"\"\"\u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6\"\"\"\n return self._size\n\n def is_empty(self) -> bool:\n \"\"\"\u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a\"\"\"\n return not self._front\n\n def push(self, num: int):\n \"\"\"\u5165\u961f\"\"\"\n # \u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\n node = ListNode(num)\n # \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\n if self._front is None:\n self._front = node\n self._rear = node\n # \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\n else:\n self._rear.next = node\n self._rear = node\n self._size += 1\n\n def pop(self) -> int:\n \"\"\"\u51fa\u961f\"\"\"\n num = self.peek()\n # \u5220\u9664\u5934\u8282\u70b9\n self._front = self._front.next\n self._size -= 1\n return num\n\n def peek(self) -> int:\n \"\"\"\u8bbf\u95ee\u961f\u9996\u5143\u7d20\"\"\"\n if self.is_empty():\n raise IndexError(\"\u961f\u5217\u4e3a\u7a7a\")\n return self._front.val\n\n def to_list(self) -> list[int]:\n \"\"\"\u8f6c\u5316\u4e3a\u5217\u8868\u7528\u4e8e\u6253\u5370\"\"\"\n queue = []\n temp = self._front\n while temp:\n queue.append(temp.val)\n temp = temp.next\n return queue\n
linkedlist_queue.cpp/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\nclass LinkedListQueue {\n private:\n ListNode *front, *rear; // \u5934\u8282\u70b9 front \uff0c\u5c3e\u8282\u70b9 rear\n int queSize;\n\n public:\n LinkedListQueue() {\n front = nullptr;\n rear = nullptr;\n queSize = 0;\n }\n\n ~LinkedListQueue() {\n // \u904d\u5386\u94fe\u8868\u5220\u9664\u8282\u70b9\uff0c\u91ca\u653e\u5185\u5b58\n freeMemoryLinkedList(front);\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n int size() {\n return queSize;\n }\n\n /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n bool isEmpty() {\n return queSize == 0;\n }\n\n /* \u5165\u961f */\n void push(int num) {\n // \u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\n ListNode *node = new ListNode(num);\n // \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\n if (front == nullptr) {\n front = node;\n rear = node;\n }\n // \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\n else {\n rear->next = node;\n rear = node;\n }\n queSize++;\n }\n\n /* \u51fa\u961f */\n void pop() {\n int num = peek();\n // \u5220\u9664\u5934\u8282\u70b9\n ListNode *tmp = front;\n front = front->next;\n // \u91ca\u653e\u5185\u5b58\n delete tmp;\n queSize--;\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n int peek() {\n if (size() == 0)\n throw out_of_range(\"\u961f\u5217\u4e3a\u7a7a\");\n return front->val;\n }\n\n /* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Vector \u5e76\u8fd4\u56de */\n vector<int> toVector() {\n ListNode *node = front;\n vector<int> res(size());\n for (int i = 0; i < res.size(); i++) {\n res[i] = node->val;\n node = node->next;\n }\n return res;\n }\n};\n
linkedlist_queue.java/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\nclass LinkedListQueue {\n private ListNode front, rear; // \u5934\u8282\u70b9 front \uff0c\u5c3e\u8282\u70b9 rear\n private int queSize = 0;\n\n public LinkedListQueue() {\n front = null;\n rear = null;\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n public int size() {\n return queSize;\n }\n\n /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n public boolean isEmpty() {\n return size() == 0;\n }\n\n /* \u5165\u961f */\n public void push(int num) {\n // \u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\n ListNode node = new ListNode(num);\n // \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\n if (front == null) {\n front = node;\n rear = node;\n // \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\n } else {\n rear.next = node;\n rear = node;\n }\n queSize++;\n }\n\n /* \u51fa\u961f */\n public int pop() {\n int num = peek();\n // \u5220\u9664\u5934\u8282\u70b9\n front = front.next;\n queSize--;\n return num;\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n public int peek() {\n if (isEmpty())\n throw new IndexOutOfBoundsException();\n return front.val;\n }\n\n /* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n public int[] toArray() {\n ListNode node = front;\n int[] res = new int[size()];\n for (int i = 0; i < res.length; i++) {\n res[i] = node.val;\n node = node.next;\n }\n return res;\n }\n}\n
linkedlist_queue.cs/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\nclass LinkedListQueue {\n ListNode? front, rear; // \u5934\u8282\u70b9 front \uff0c\u5c3e\u8282\u70b9 rear \n int queSize = 0;\n\n public LinkedListQueue() {\n front = null;\n rear = null;\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n public int Size() {\n return queSize;\n }\n\n /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n public bool IsEmpty() {\n return Size() == 0;\n }\n\n /* \u5165\u961f */\n public void Push(int num) {\n // \u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\n ListNode node = new(num);\n // \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\n if (front == null) {\n front = node;\n rear = node;\n // \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\n } else if (rear != null) {\n rear.next = node;\n rear = node;\n }\n queSize++;\n }\n\n /* \u51fa\u961f */\n public int Pop() {\n int num = Peek();\n // \u5220\u9664\u5934\u8282\u70b9\n front = front?.next;\n queSize--;\n return num;\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n public int Peek() {\n if (IsEmpty())\n throw new Exception();\n return front!.val;\n }\n\n /* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n public int[] ToArray() {\n if (front == null)\n return [];\n\n ListNode? node = front;\n int[] res = new int[Size()];\n for (int i = 0; i < res.Length; i++) {\n res[i] = node!.val;\n node = node.next;\n }\n return res;\n }\n}\n
linkedlist_queue.go/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\ntype linkedListQueue struct {\n // \u4f7f\u7528\u5185\u7f6e\u5305 list \u6765\u5b9e\u73b0\u961f\u5217\n data *list.List\n}\n\n/* \u521d\u59cb\u5316\u961f\u5217 */\nfunc newLinkedListQueue() *linkedListQueue {\n return &linkedListQueue{\n data: list.New(),\n }\n}\n\n/* \u5165\u961f */\nfunc (s *linkedListQueue) push(value any) {\n s.data.PushBack(value)\n}\n\n/* \u51fa\u961f */\nfunc (s *linkedListQueue) pop() any {\n if s.isEmpty() {\n return nil\n }\n e := s.data.Front()\n s.data.Remove(e)\n return e.Value\n}\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nfunc (s *linkedListQueue) peek() any {\n if s.isEmpty() {\n return nil\n }\n e := s.data.Front()\n return e.Value\n}\n\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nfunc (s *linkedListQueue) size() int {\n return s.data.Len()\n}\n\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nfunc (s *linkedListQueue) isEmpty() bool {\n return s.data.Len() == 0\n}\n\n/* \u83b7\u53d6 List \u7528\u4e8e\u6253\u5370 */\nfunc (s *linkedListQueue) toList() *list.List {\n return s.data\n}\n
linkedlist_queue.swift/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\nclass LinkedListQueue {\n private var front: ListNode? // \u5934\u8282\u70b9\n private var rear: ListNode? // \u5c3e\u8282\u70b9\n private var _size = 0\n\n init() {}\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n func size() -> Int {\n _size\n }\n\n /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n func isEmpty() -> Bool {\n size() == 0\n }\n\n /* \u5165\u961f */\n func push(num: Int) {\n // \u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\n let node = ListNode(x: num)\n // \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\n if front == nil {\n front = node\n rear = node\n }\n // \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\n else {\n rear?.next = node\n rear = node\n }\n _size += 1\n }\n\n /* \u51fa\u961f */\n @discardableResult\n func pop() -> Int {\n let num = peek()\n // \u5220\u9664\u5934\u8282\u70b9\n front = front?.next\n _size -= 1\n return num\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n func peek() -> Int {\n if isEmpty() {\n fatalError(\"\u961f\u5217\u4e3a\u7a7a\")\n }\n return front!.val\n }\n\n /* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n func toArray() -> [Int] {\n var node = front\n var res = Array(repeating: 0, count: size())\n for i in res.indices {\n res[i] = node!.val\n node = node?.next\n }\n return res\n }\n}\n
linkedlist_queue.js/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\nclass LinkedListQueue {\n #front; // \u5934\u8282\u70b9 #front\n #rear; // \u5c3e\u8282\u70b9 #rear\n #queSize = 0;\n\n constructor() {\n this.#front = null;\n this.#rear = null;\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n get size() {\n return this.#queSize;\n }\n\n /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n isEmpty() {\n return this.size === 0;\n }\n\n /* \u5165\u961f */\n push(num) {\n // \u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\n const node = new ListNode(num);\n // \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\n if (!this.#front) {\n this.#front = node;\n this.#rear = node;\n // \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\n } else {\n this.#rear.next = node;\n this.#rear = node;\n }\n this.#queSize++;\n }\n\n /* \u51fa\u961f */\n pop() {\n const num = this.peek();\n // \u5220\u9664\u5934\u8282\u70b9\n this.#front = this.#front.next;\n this.#queSize--;\n return num;\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n peek() {\n if (this.size === 0) throw new Error('\u961f\u5217\u4e3a\u7a7a');\n return this.#front.val;\n }\n\n /* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n toArray() {\n let node = this.#front;\n const res = new Array(this.size);\n for (let i = 0; i < res.length; i++) {\n res[i] = node.val;\n node = node.next;\n }\n return res;\n }\n}\n
linkedlist_queue.ts/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\nclass LinkedListQueue {\n private front: ListNode | null; // \u5934\u8282\u70b9 front\n private rear: ListNode | null; // \u5c3e\u8282\u70b9 rear\n private queSize: number = 0;\n\n constructor() {\n this.front = null;\n this.rear = null;\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n get size(): number {\n return this.queSize;\n }\n\n /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n isEmpty(): boolean {\n return this.size === 0;\n }\n\n /* \u5165\u961f */\n push(num: number): void {\n // \u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\n const node = new ListNode(num);\n // \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\n if (!this.front) {\n this.front = node;\n this.rear = node;\n // \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\n } else {\n this.rear!.next = node;\n this.rear = node;\n }\n this.queSize++;\n }\n\n /* \u51fa\u961f */\n pop(): number {\n const num = this.peek();\n if (!this.front) throw new Error('\u961f\u5217\u4e3a\u7a7a');\n // \u5220\u9664\u5934\u8282\u70b9\n this.front = this.front.next;\n this.queSize--;\n return num;\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n peek(): number {\n if (this.size === 0) throw new Error('\u961f\u5217\u4e3a\u7a7a');\n return this.front!.val;\n }\n\n /* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n toArray(): number[] {\n let node = this.front;\n const res = new Array<number>(this.size);\n for (let i = 0; i < res.length; i++) {\n res[i] = node!.val;\n node = node!.next;\n }\n return res;\n }\n}\n
linkedlist_queue.dart/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\nclass LinkedListQueue {\n ListNode? _front; // \u5934\u8282\u70b9 _front\n ListNode? _rear; // \u5c3e\u8282\u70b9 _rear\n int _queSize = 0; // \u961f\u5217\u957f\u5ea6\n\n LinkedListQueue() {\n _front = null;\n _rear = null;\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n int size() {\n return _queSize;\n }\n\n /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n bool isEmpty() {\n return _queSize == 0;\n }\n\n /* \u5165\u961f */\n void push(int _num) {\n // \u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 _num\n final node = ListNode(_num);\n // \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\n if (_front == null) {\n _front = node;\n _rear = node;\n } else {\n // \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\n _rear!.next = node;\n _rear = node;\n }\n _queSize++;\n }\n\n /* \u51fa\u961f */\n int pop() {\n final int _num = peek();\n // \u5220\u9664\u5934\u8282\u70b9\n _front = _front!.next;\n _queSize--;\n return _num;\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n int peek() {\n if (_queSize == 0) {\n throw Exception('\u961f\u5217\u4e3a\u7a7a');\n }\n return _front!.val;\n }\n\n /* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n List<int> toArray() {\n ListNode? node = _front;\n final List<int> queue = [];\n while (node != null) {\n queue.add(node.val);\n node = node.next;\n }\n return queue;\n }\n}\n
linkedlist_queue.rs/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\n#[allow(dead_code)]\npub struct LinkedListQueue<T> {\n front: Option<Rc<RefCell<ListNode<T>>>>, // \u5934\u8282\u70b9 front\n rear: Option<Rc<RefCell<ListNode<T>>>>, // \u5c3e\u8282\u70b9 rear \n que_size: usize, // \u961f\u5217\u7684\u957f\u5ea6\n}\n\nimpl<T: Copy> LinkedListQueue<T> {\n pub fn new() -> Self {\n Self {\n front: None,\n rear: None,\n que_size: 0, \n }\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n pub fn size(&self) -> usize {\n return self.que_size;\n }\n\n /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n pub fn is_empty(&self) -> bool {\n return self.size() == 0;\n }\n\n /* \u5165\u961f */\n pub fn push(&mut self, num: T) {\n // \u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\n let new_rear = ListNode::new(num);\n match self.rear.take() {\n // \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\n Some(old_rear) => {\n old_rear.borrow_mut().next = Some(new_rear.clone());\n self.rear = Some(new_rear);\n }\n // \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\n None => {\n self.front = Some(new_rear.clone());\n self.rear = Some(new_rear);\n }\n }\n self.que_size += 1;\n }\n\n /* \u51fa\u961f */\n pub fn pop(&mut self) -> Option<T> {\n self.front.take().map(|old_front| {\n match old_front.borrow_mut().next.take() {\n Some(new_front) => {\n self.front = Some(new_front);\n }\n None => {\n self.rear.take();\n }\n }\n self.que_size -= 1;\n Rc::try_unwrap(old_front).ok().unwrap().into_inner().val\n })\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n pub fn peek(&self) -> Option<&Rc<RefCell<ListNode<T>>>> {\n self.front.as_ref()\n }\n\n /* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n pub fn to_array(&self, head: Option<&Rc<RefCell<ListNode<T>>>>) -> Vec<T> {\n if let Some(node) = head {\n let mut nums = self.to_array(node.borrow().next.as_ref());\n nums.insert(0, node.borrow().val);\n return nums;\n }\n return Vec::new();\n }\n}\n
linkedlist_queue.c/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\ntypedef struct {\n ListNode *front, *rear;\n int queSize;\n} LinkedListQueue;\n\n/* \u6784\u9020\u51fd\u6570 */\nLinkedListQueue *newLinkedListQueue() {\n LinkedListQueue *queue = (LinkedListQueue *)malloc(sizeof(LinkedListQueue));\n queue->front = NULL;\n queue->rear = NULL;\n queue->queSize = 0;\n return queue;\n}\n\n/* \u6790\u6784\u51fd\u6570 */\nvoid delLinkedListQueue(LinkedListQueue *queue) {\n // \u91ca\u653e\u6240\u6709\u8282\u70b9\n for (int i = 0; i < queue->queSize && queue->front != NULL; i++) {\n ListNode *tmp = queue->front;\n queue->front = queue->front->next;\n free(tmp);\n }\n // \u91ca\u653e queue \u7ed3\u6784\u4f53\n free(queue);\n}\n\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nint size(LinkedListQueue *queue) {\n return queue->queSize;\n}\n\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool empty(LinkedListQueue *queue) {\n return (size(queue) == 0);\n}\n\n/* \u5165\u961f */\nvoid push(LinkedListQueue *queue, int num) {\n // \u5c3e\u8282\u70b9\u5904\u6dfb\u52a0 node\n ListNode *node = newListNode(num);\n // \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\n if (queue->front == NULL) {\n queue->front = node;\n queue->rear = node;\n }\n // \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\n else {\n queue->rear->next = node;\n queue->rear = node;\n }\n queue->queSize++;\n}\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint peek(LinkedListQueue *queue) {\n assert(size(queue) && queue->front);\n return queue->front->val;\n}\n\n/* \u51fa\u961f */\nvoid pop(LinkedListQueue *queue) {\n int num = peek(queue);\n ListNode *tmp = queue->front;\n queue->front = queue->front->next;\n free(tmp);\n queue->queSize--;\n}\n\n/* \u6253\u5370\u961f\u5217 */\nvoid printLinkedListQueue(LinkedListQueue *queue) {\n int *arr = malloc(sizeof(int) * queue->queSize);\n // \u62f7\u8d1d\u94fe\u8868\u4e2d\u7684\u6570\u636e\u5230\u6570\u7ec4\n int i;\n ListNode *node;\n for (i = 0, node = queue->front; i < queue->queSize; i++) {\n arr[i] = node->val;\n node = node->next;\n }\n printArray(arr, queue->queSize);\n free(arr);\n}\n
linkedlist_queue.zig// \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217\nfn LinkedListQueue(comptime T: type) type {\n return struct {\n const Self = @This();\n\n front: ?*inc.ListNode(T) = null, // \u5934\u8282\u70b9 front\n rear: ?*inc.ListNode(T) = null, // \u5c3e\u8282\u70b9 rear\n que_size: usize = 0, // \u961f\u5217\u7684\u957f\u5ea6\n mem_arena: ?std.heap.ArenaAllocator = null,\n mem_allocator: std.mem.Allocator = undefined, // \u5185\u5b58\u5206\u914d\u5668\n\n // \u6784\u9020\u51fd\u6570\uff08\u5206\u914d\u5185\u5b58+\u521d\u59cb\u5316\u961f\u5217\uff09\n pub fn init(self: *Self, allocator: std.mem.Allocator) !void {\n if (self.mem_arena == null) {\n self.mem_arena = std.heap.ArenaAllocator.init(allocator);\n self.mem_allocator = self.mem_arena.?.allocator();\n }\n self.front = null;\n self.rear = null;\n self.que_size = 0;\n }\n\n // \u6790\u6784\u51fd\u6570\uff08\u91ca\u653e\u5185\u5b58\uff09\n pub fn deinit(self: *Self) void {\n if (self.mem_arena == null) return;\n self.mem_arena.?.deinit();\n }\n\n // \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6\n pub fn size(self: *Self) usize {\n return self.que_size;\n }\n\n // \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a\n pub fn isEmpty(self: *Self) bool {\n return self.size() == 0;\n }\n\n // \u8bbf\u95ee\u961f\u9996\u5143\u7d20\n pub fn peek(self: *Self) T {\n if (self.size() == 0) @panic(\"\u961f\u5217\u4e3a\u7a7a\");\n return self.front.?.val;\n } \n\n // \u5165\u961f\n pub fn push(self: *Self, num: T) !void {\n // \u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\n var node = try self.mem_allocator.create(inc.ListNode(T));\n node.init(num);\n // \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\n if (self.front == null) {\n self.front = node;\n self.rear = node;\n // \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\n } else {\n self.rear.?.next = node;\n self.rear = node;\n }\n self.que_size += 1;\n } \n\n // \u51fa\u961f\n pub fn pop(self: *Self) T {\n var num = self.peek();\n // \u5220\u9664\u5934\u8282\u70b9\n self.front = self.front.?.next;\n self.que_size -= 1;\n return num;\n } \n\n // \u5c06\u94fe\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4\n pub fn toArray(self: *Self) ![]T {\n var node = self.front;\n var res = try self.mem_allocator.alloc(T, self.size());\n @memset(res, @as(T, 0));\n var i: usize = 0;\n while (i < res.len) : (i += 1) {\n res[i] = node.?.val;\n node = node.?.next;\n }\n return res;\n }\n };\n}\n
"},{"location":"chapter_stack_and_queue/queue/#2","title":"2. \u00a0 \u57fa\u4e8e\u6570\u7ec4\u7684\u5b9e\u73b0","text":"\u5728\u6570\u7ec4\u4e2d\u5220\u9664\u9996\u5143\u7d20\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \uff0c\u8fd9\u4f1a\u5bfc\u81f4\u51fa\u961f\u64cd\u4f5c\u6548\u7387\u8f83\u4f4e\u3002\u7136\u800c\uff0c\u6211\u4eec\u53ef\u4ee5\u91c7\u7528\u4ee5\u4e0b\u5de7\u5999\u65b9\u6cd5\u6765\u907f\u514d\u8fd9\u4e2a\u95ee\u9898\u3002
\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u53d8\u91cf front
\u6307\u5411\u961f\u9996\u5143\u7d20\u7684\u7d22\u5f15\uff0c\u5e76\u7ef4\u62a4\u4e00\u4e2a\u53d8\u91cf size
\u7528\u4e8e\u8bb0\u5f55\u961f\u5217\u957f\u5ea6\u3002\u5b9a\u4e49 rear = front + size
\uff0c\u8fd9\u4e2a\u516c\u5f0f\u8ba1\u7b97\u51fa\u7684 rear
\u6307\u5411\u961f\u5c3e\u5143\u7d20\u4e4b\u540e\u7684\u4e0b\u4e00\u4e2a\u4f4d\u7f6e\u3002
\u57fa\u4e8e\u6b64\u8bbe\u8ba1\uff0c\u6570\u7ec4\u4e2d\u5305\u542b\u5143\u7d20\u7684\u6709\u6548\u533a\u95f4\u4e3a [front, rear - 1]
\uff0c\u5404\u79cd\u64cd\u4f5c\u7684\u5b9e\u73b0\u65b9\u6cd5\u5982\u56fe 5-6 \u6240\u793a\u3002
rear
\u7d22\u5f15\u5904\uff0c\u5e76\u5c06 size
\u589e\u52a0 1 \u3002front
\u589e\u52a0 1 \uff0c\u5e76\u5c06 size
\u51cf\u5c11 1 \u3002\u53ef\u4ee5\u770b\u5230\uff0c\u5165\u961f\u548c\u51fa\u961f\u64cd\u4f5c\u90fd\u53ea\u9700\u8fdb\u884c\u4e00\u6b21\u64cd\u4f5c\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u5747\u4e3a \\(O(1)\\) \u3002
ArrayQueuepush()pop()\u56fe 5-6 \u00a0 \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u961f\u5217\u7684\u5165\u961f\u51fa\u961f\u64cd\u4f5c
\u4f60\u53ef\u80fd\u4f1a\u53d1\u73b0\u4e00\u4e2a\u95ee\u9898\uff1a\u5728\u4e0d\u65ad\u8fdb\u884c\u5165\u961f\u548c\u51fa\u961f\u7684\u8fc7\u7a0b\u4e2d\uff0cfront
\u548c rear
\u90fd\u5728\u5411\u53f3\u79fb\u52a8\uff0c\u5f53\u5b83\u4eec\u5230\u8fbe\u6570\u7ec4\u5c3e\u90e8\u65f6\u5c31\u65e0\u6cd5\u7ee7\u7eed\u79fb\u52a8\u4e86\u3002\u4e3a\u4e86\u89e3\u51b3\u6b64\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u6570\u7ec4\u89c6\u4e3a\u9996\u5c3e\u76f8\u63a5\u7684\u201c\u73af\u5f62\u6570\u7ec4\u201d\u3002
\u5bf9\u4e8e\u73af\u5f62\u6570\u7ec4\uff0c\u6211\u4eec\u9700\u8981\u8ba9 front
\u6216 rear
\u5728\u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u76f4\u63a5\u56de\u5230\u6570\u7ec4\u5934\u90e8\u7ee7\u7eed\u904d\u5386\u3002\u8fd9\u79cd\u5468\u671f\u6027\u89c4\u5f8b\u53ef\u4ee5\u901a\u8fc7\u201c\u53d6\u4f59\u64cd\u4f5c\u201d\u6765\u5b9e\u73b0\uff0c\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a
class ArrayQueue:\n \"\"\"\u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217\"\"\"\n\n def __init__(self, size: int):\n \"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n self._nums: list[int] = [0] * size # \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n self._front: int = 0 # \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n self._size: int = 0 # \u961f\u5217\u957f\u5ea6\n\n def capacity(self) -> int:\n \"\"\"\u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf\"\"\"\n return len(self._nums)\n\n def size(self) -> int:\n \"\"\"\u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6\"\"\"\n return self._size\n\n def is_empty(self) -> bool:\n \"\"\"\u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a\"\"\"\n return self._size == 0\n\n def push(self, num: int):\n \"\"\"\u5165\u961f\"\"\"\n if self._size == self.capacity():\n raise IndexError(\"\u961f\u5217\u5df2\u6ee1\")\n # \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n # \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\uff0c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\n rear: int = (self._front + self._size) % self.capacity()\n # \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n self._nums[rear] = num\n self._size += 1\n\n def pop(self) -> int:\n \"\"\"\u51fa\u961f\"\"\"\n num: int = self.peek()\n # \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\n self._front = (self._front + 1) % self.capacity()\n self._size -= 1\n return num\n\n def peek(self) -> int:\n \"\"\"\u8bbf\u95ee\u961f\u9996\u5143\u7d20\"\"\"\n if self.is_empty():\n raise IndexError(\"\u961f\u5217\u4e3a\u7a7a\")\n return self._nums[self._front]\n\n def to_list(self) -> list[int]:\n \"\"\"\u8fd4\u56de\u5217\u8868\u7528\u4e8e\u6253\u5370\"\"\"\n res = [0] * self.size()\n j: int = self._front\n for i in range(self.size()):\n res[i] = self._nums[(j % self.capacity())]\n j += 1\n return res\n
array_queue.cpp/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nclass ArrayQueue {\n private:\n int *nums; // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n int front; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n int queSize; // \u961f\u5217\u957f\u5ea6\n int queCapacity; // \u961f\u5217\u5bb9\u91cf\n\n public:\n ArrayQueue(int capacity) {\n // \u521d\u59cb\u5316\u6570\u7ec4\n nums = new int[capacity];\n queCapacity = capacity;\n front = queSize = 0;\n }\n\n ~ArrayQueue() {\n delete[] nums;\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\n int capacity() {\n return queCapacity;\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n int size() {\n return queSize;\n }\n\n /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n bool isEmpty() {\n return size() == 0;\n }\n\n /* \u5165\u961f */\n void push(int num) {\n if (queSize == queCapacity) {\n cout << \"\u961f\u5217\u5df2\u6ee1\" << endl;\n return;\n }\n // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\uff0c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\n int rear = (front + queSize) % queCapacity;\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n nums[rear] = num;\n queSize++;\n }\n\n /* \u51fa\u961f */\n void pop() {\n int num = peek();\n // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\n front = (front + 1) % queCapacity;\n queSize--;\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n int peek() {\n if (isEmpty())\n throw out_of_range(\"\u961f\u5217\u4e3a\u7a7a\");\n return nums[front];\n }\n\n /* \u5c06\u6570\u7ec4\u8f6c\u5316\u4e3a Vector \u5e76\u8fd4\u56de */\n vector<int> toVector() {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n vector<int> arr(queSize);\n for (int i = 0, j = front; i < queSize; i++, j++) {\n arr[i] = nums[j % queCapacity];\n }\n return arr;\n }\n};\n
array_queue.java/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nclass ArrayQueue {\n private int[] nums; // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n private int front; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n private int queSize; // \u961f\u5217\u957f\u5ea6\n\n public ArrayQueue(int capacity) {\n nums = new int[capacity];\n front = queSize = 0;\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\n public int capacity() {\n return nums.length;\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n public int size() {\n return queSize;\n }\n\n /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n public boolean isEmpty() {\n return queSize == 0;\n }\n\n /* \u5165\u961f */\n public void push(int num) {\n if (queSize == capacity()) {\n System.out.println(\"\u961f\u5217\u5df2\u6ee1\");\n return;\n }\n // \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\uff0c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\n int rear = (front + queSize) % capacity();\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n nums[rear] = num;\n queSize++;\n }\n\n /* \u51fa\u961f */\n public int pop() {\n int num = peek();\n // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\n front = (front + 1) % capacity();\n queSize--;\n return num;\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n public int peek() {\n if (isEmpty())\n throw new IndexOutOfBoundsException();\n return nums[front];\n }\n\n /* \u8fd4\u56de\u6570\u7ec4 */\n public int[] toArray() {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n int[] res = new int[queSize];\n for (int i = 0, j = front; i < queSize; i++, j++) {\n res[i] = nums[j % capacity()];\n }\n return res;\n }\n}\n
array_queue.cs/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nclass ArrayQueue {\n int[] nums; // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n int front; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n int queSize; // \u961f\u5217\u957f\u5ea6\n\n public ArrayQueue(int capacity) {\n nums = new int[capacity];\n front = queSize = 0;\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\n int Capacity() {\n return nums.Length;\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n public int Size() {\n return queSize;\n }\n\n /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n public bool IsEmpty() {\n return queSize == 0;\n }\n\n /* \u5165\u961f */\n public void Push(int num) {\n if (queSize == Capacity()) {\n Console.WriteLine(\"\u961f\u5217\u5df2\u6ee1\");\n return;\n }\n // \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\uff0c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\n int rear = (front + queSize) % Capacity();\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n nums[rear] = num;\n queSize++;\n }\n\n /* \u51fa\u961f */\n public int Pop() {\n int num = Peek();\n // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\n front = (front + 1) % Capacity();\n queSize--;\n return num;\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n public int Peek() {\n if (IsEmpty())\n throw new Exception();\n return nums[front];\n }\n\n /* \u8fd4\u56de\u6570\u7ec4 */\n public int[] ToArray() {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n int[] res = new int[queSize];\n for (int i = 0, j = front; i < queSize; i++, j++) {\n res[i] = nums[j % this.Capacity()];\n }\n return res;\n }\n}\n
array_queue.go/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\ntype arrayQueue struct {\n nums []int // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n front int // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n queSize int // \u961f\u5217\u957f\u5ea6\n queCapacity int // \u961f\u5217\u5bb9\u91cf\uff08\u5373\u6700\u5927\u5bb9\u7eb3\u5143\u7d20\u6570\u91cf\uff09\n}\n\n/* \u521d\u59cb\u5316\u961f\u5217 */\nfunc newArrayQueue(queCapacity int) *arrayQueue {\n return &arrayQueue{\n nums: make([]int, queCapacity),\n queCapacity: queCapacity,\n front: 0,\n queSize: 0,\n }\n}\n\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nfunc (q *arrayQueue) size() int {\n return q.queSize\n}\n\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nfunc (q *arrayQueue) isEmpty() bool {\n return q.queSize == 0\n}\n\n/* \u5165\u961f */\nfunc (q *arrayQueue) push(num int) {\n // \u5f53 rear == queCapacity \u8868\u793a\u961f\u5217\u5df2\u6ee1\n if q.queSize == q.queCapacity {\n return\n }\n // \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\uff0c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\n rear := (q.front + q.queSize) % q.queCapacity\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n q.nums[rear] = num\n q.queSize++\n}\n\n/* \u51fa\u961f */\nfunc (q *arrayQueue) pop() any {\n num := q.peek()\n // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\n q.front = (q.front + 1) % q.queCapacity\n q.queSize--\n return num\n}\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nfunc (q *arrayQueue) peek() any {\n if q.isEmpty() {\n return nil\n }\n return q.nums[q.front]\n}\n\n/* \u83b7\u53d6 Slice \u7528\u4e8e\u6253\u5370 */\nfunc (q *arrayQueue) toSlice() []int {\n rear := (q.front + q.queSize)\n if rear >= q.queCapacity {\n rear %= q.queCapacity\n return append(q.nums[q.front:], q.nums[:rear]...)\n }\n return q.nums[q.front:rear]\n}\n
array_queue.swift/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nclass ArrayQueue {\n private var nums: [Int] // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n private var front = 0 // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n private var queSize = 0 // \u961f\u5217\u957f\u5ea6\n\n init(capacity: Int) {\n // \u521d\u59cb\u5316\u6570\u7ec4\n nums = Array(repeating: 0, count: capacity)\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\n func capacity() -> Int {\n nums.count\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n func size() -> Int {\n queSize\n }\n\n /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n func isEmpty() -> Bool {\n queSize == 0\n }\n\n /* \u5165\u961f */\n func push(num: Int) {\n if size() == capacity() {\n print(\"\u961f\u5217\u5df2\u6ee1\")\n return\n }\n // \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\uff0c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\n let rear = (front + queSize) % capacity()\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n nums[rear] = num\n queSize += 1\n }\n\n /* \u51fa\u961f */\n @discardableResult\n func pop() -> Int {\n let num = peek()\n // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\n front = (front + 1) % capacity()\n queSize -= 1\n return num\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n func peek() -> Int {\n if isEmpty() {\n fatalError(\"\u961f\u5217\u4e3a\u7a7a\")\n }\n return nums[front]\n }\n\n /* \u8fd4\u56de\u6570\u7ec4 */\n func toArray() -> [Int] {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n var res = Array(repeating: 0, count: queSize)\n for (i, j) in sequence(first: (0, front), next: { $0 < self.queSize - 1 ? ($0 + 1, $1 + 1) : nil }) {\n res[i] = nums[j % capacity()]\n }\n return res\n }\n}\n
array_queue.js/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nclass ArrayQueue {\n #nums; // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n #front = 0; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n #queSize = 0; // \u961f\u5217\u957f\u5ea6\n\n constructor(capacity) {\n this.#nums = new Array(capacity);\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\n get capacity() {\n return this.#nums.length;\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n get size() {\n return this.#queSize;\n }\n\n /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n isEmpty() {\n return this.#queSize === 0;\n }\n\n /* \u5165\u961f */\n push(num) {\n if (this.size === this.capacity) {\n console.log('\u961f\u5217\u5df2\u6ee1');\n return;\n }\n // \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\uff0c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\n const rear = (this.#front + this.size) % this.capacity;\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n this.#nums[rear] = num;\n this.#queSize++;\n }\n\n /* \u51fa\u961f */\n pop() {\n const num = this.peek();\n // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\n this.#front = (this.#front + 1) % this.capacity;\n this.#queSize--;\n return num;\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n peek() {\n if (this.isEmpty()) throw new Error('\u961f\u5217\u4e3a\u7a7a');\n return this.#nums[this.#front];\n }\n\n /* \u8fd4\u56de Array */\n toArray() {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n const arr = new Array(this.size);\n for (let i = 0, j = this.#front; i < this.size; i++, j++) {\n arr[i] = this.#nums[j % this.capacity];\n }\n return arr;\n }\n}\n
array_queue.ts/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nclass ArrayQueue {\n private nums: number[]; // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n private front: number; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n private queSize: number; // \u961f\u5217\u957f\u5ea6\n\n constructor(capacity: number) {\n this.nums = new Array(capacity);\n this.front = this.queSize = 0;\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\n get capacity(): number {\n return this.nums.length;\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n get size(): number {\n return this.queSize;\n }\n\n /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n isEmpty(): boolean {\n return this.queSize === 0;\n }\n\n /* \u5165\u961f */\n push(num: number): void {\n if (this.size === this.capacity) {\n console.log('\u961f\u5217\u5df2\u6ee1');\n return;\n }\n // \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\uff0c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\n const rear = (this.front + this.queSize) % this.capacity;\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n this.nums[rear] = num;\n this.queSize++;\n }\n\n /* \u51fa\u961f */\n pop(): number {\n const num = this.peek();\n // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\n this.front = (this.front + 1) % this.capacity;\n this.queSize--;\n return num;\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n peek(): number {\n if (this.isEmpty()) throw new Error('\u961f\u5217\u4e3a\u7a7a');\n return this.nums[this.front];\n }\n\n /* \u8fd4\u56de Array */\n toArray(): number[] {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n const arr = new Array(this.size);\n for (let i = 0, j = this.front; i < this.size; i++, j++) {\n arr[i] = this.nums[j % this.capacity];\n }\n return arr;\n }\n}\n
array_queue.dart/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nclass ArrayQueue {\n late List<int> _nums; // \u7528\u4e8e\u50a8\u5b58\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n late int _front; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n late int _queSize; // \u961f\u5217\u957f\u5ea6\n\n ArrayQueue(int capacity) {\n _nums = List.filled(capacity, 0);\n _front = _queSize = 0;\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\n int capaCity() {\n return _nums.length;\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n int size() {\n return _queSize;\n }\n\n /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n bool isEmpty() {\n return _queSize == 0;\n }\n\n /* \u5165\u961f */\n void push(int _num) {\n if (_queSize == capaCity()) {\n throw Exception(\"\u961f\u5217\u5df2\u6ee1\");\n }\n // \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\uff0c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\n int rear = (_front + _queSize) % capaCity();\n // \u5c06 _num \u6dfb\u52a0\u81f3\u961f\u5c3e\n _nums[rear] = _num;\n _queSize++;\n }\n\n /* \u51fa\u961f */\n int pop() {\n int _num = peek();\n // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\n _front = (_front + 1) % capaCity();\n _queSize--;\n return _num;\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n int peek() {\n if (isEmpty()) {\n throw Exception(\"\u961f\u5217\u4e3a\u7a7a\");\n }\n return _nums[_front];\n }\n\n /* \u8fd4\u56de Array */\n List<int> toArray() {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n final List<int> res = List.filled(_queSize, 0);\n for (int i = 0, j = _front; i < _queSize; i++, j++) {\n res[i] = _nums[j % capaCity()];\n }\n return res;\n }\n}\n
array_queue.rs/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nstruct ArrayQueue {\n nums: Vec<i32>, // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n front: i32, // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n que_size: i32, // \u961f\u5217\u957f\u5ea6\n que_capacity: i32, // \u961f\u5217\u5bb9\u91cf\n}\n\nimpl ArrayQueue {\n /* \u6784\u9020\u65b9\u6cd5 */\n fn new(capacity: i32) -> ArrayQueue {\n ArrayQueue {\n nums: vec![0; capacity as usize],\n front: 0,\n que_size: 0,\n que_capacity: capacity,\n }\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\n fn capacity(&self) -> i32 {\n self.que_capacity\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n fn size(&self) -> i32 {\n self.que_size\n }\n\n /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n fn is_empty(&self) -> bool {\n self.que_size == 0\n }\n\n /* \u5165\u961f */\n fn push(&mut self, num: i32) {\n if self.que_size == self.capacity() {\n println!(\"\u961f\u5217\u5df2\u6ee1\");\n return;\n }\n // \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\uff0c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\n let rear = (self.front + self.que_size) % self.que_capacity;\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n self.nums[rear as usize] = num;\n self.que_size += 1;\n }\n\n /* \u51fa\u961f */\n fn pop(&mut self) -> i32 {\n let num = self.peek();\n // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\n self.front = (self.front + 1) % self.que_capacity;\n self.que_size -= 1;\n num\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n fn peek(&self) -> i32 {\n if self.is_empty() {\n panic!(\"index out of bounds\");\n }\n self.nums[self.front as usize]\n }\n\n /* \u8fd4\u56de\u6570\u7ec4 */\n fn to_vector(&self) -> Vec<i32> {\n let cap = self.que_capacity;\n let mut j = self.front;\n let mut arr = vec![0; self.que_size as usize];\n for i in 0..self.que_size {\n arr[i as usize] = self.nums[(j % cap) as usize];\n j += 1;\n }\n arr\n }\n}\n
array_queue.c/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\ntypedef struct {\n int *nums; // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n int front; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n int queSize; // \u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e + 1\n int queCapacity; // \u961f\u5217\u5bb9\u91cf\n} ArrayQueue;\n\n/* \u6784\u9020\u51fd\u6570 */\nArrayQueue *newArrayQueue(int capacity) {\n ArrayQueue *queue = (ArrayQueue *)malloc(sizeof(ArrayQueue));\n // \u521d\u59cb\u5316\u6570\u7ec4\n queue->queCapacity = capacity;\n queue->nums = (int *)malloc(sizeof(int) * queue->queCapacity);\n queue->front = queue->queSize = 0;\n return queue;\n}\n\n/* \u6790\u6784\u51fd\u6570 */\nvoid delArrayQueue(ArrayQueue *queue) {\n free(queue->nums);\n free(queue);\n}\n\n/* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\nint capacity(ArrayQueue *queue) {\n return queue->queCapacity;\n}\n\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nint size(ArrayQueue *queue) {\n return queue->queSize;\n}\n\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool empty(ArrayQueue *queue) {\n return queue->queSize == 0;\n}\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint peek(ArrayQueue *queue) {\n assert(size(queue) != 0);\n return queue->nums[queue->front];\n}\n\n/* \u5165\u961f */\nvoid push(ArrayQueue *queue, int num) {\n if (size(queue) == capacity(queue)) {\n printf(\"\u961f\u5217\u5df2\u6ee1\\r\\n\");\n return;\n }\n // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\uff0c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\n int rear = (queue->front + queue->queSize) % queue->queCapacity;\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n queue->nums[rear] = num;\n queue->queSize++;\n}\n\n/* \u51fa\u961f */\nvoid pop(ArrayQueue *queue) {\n int num = peek(queue);\n // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\n queue->front = (queue->front + 1) % queue->queCapacity;\n queue->queSize--;\n}\n
array_queue.zig// \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217\nfn ArrayQueue(comptime T: type) type {\n return struct {\n const Self = @This();\n\n nums: []T = undefined, // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4 \n cap: usize = 0, // \u961f\u5217\u5bb9\u91cf\n front: usize = 0, // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n queSize: usize = 0, // \u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e + 1\n mem_arena: ?std.heap.ArenaAllocator = null,\n mem_allocator: std.mem.Allocator = undefined, // \u5185\u5b58\u5206\u914d\u5668\n\n // \u6784\u9020\u51fd\u6570\uff08\u5206\u914d\u5185\u5b58+\u521d\u59cb\u5316\u6570\u7ec4\uff09\n pub fn init(self: *Self, allocator: std.mem.Allocator, cap: usize) !void {\n if (self.mem_arena == null) {\n self.mem_arena = std.heap.ArenaAllocator.init(allocator);\n self.mem_allocator = self.mem_arena.?.allocator();\n }\n self.cap = cap;\n self.nums = try self.mem_allocator.alloc(T, self.cap);\n @memset(self.nums, @as(T, 0));\n }\n\n // \u6790\u6784\u51fd\u6570\uff08\u91ca\u653e\u5185\u5b58\uff09\n pub fn deinit(self: *Self) void {\n if (self.mem_arena == null) return;\n self.mem_arena.?.deinit();\n }\n\n // \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf\n pub fn capacity(self: *Self) usize {\n return self.cap;\n }\n\n // \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6\n pub fn size(self: *Self) usize {\n return self.queSize;\n }\n\n // \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a\n pub fn isEmpty(self: *Self) bool {\n return self.queSize == 0;\n }\n\n // \u5165\u961f\n pub fn push(self: *Self, num: T) !void {\n if (self.size() == self.capacity()) {\n std.debug.print(\"\u961f\u5217\u5df2\u6ee1\\n\", .{});\n return;\n }\n // \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\uff0c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\n var rear = (self.front + self.queSize) % self.capacity();\n // \u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\n self.nums[rear] = num;\n self.queSize += 1;\n } \n\n // \u51fa\u961f\n pub fn pop(self: *Self) T {\n var num = self.peek();\n // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\n self.front = (self.front + 1) % self.capacity();\n self.queSize -= 1;\n return num;\n } \n\n // \u8bbf\u95ee\u961f\u9996\u5143\u7d20\n pub fn peek(self: *Self) T {\n if (self.isEmpty()) @panic(\"\u961f\u5217\u4e3a\u7a7a\");\n return self.nums[self.front];\n } \n\n // \u8fd4\u56de\u6570\u7ec4\n pub fn toArray(self: *Self) ![]T {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n var res = try self.mem_allocator.alloc(T, self.size());\n @memset(res, @as(T, 0));\n var i: usize = 0;\n var j: usize = self.front;\n while (i < self.size()) : ({ i += 1; j += 1; }) {\n res[i] = self.nums[j % self.capacity()];\n }\n return res;\n }\n };\n}\n
\u4ee5\u4e0a\u5b9e\u73b0\u7684\u961f\u5217\u4ecd\u7136\u5177\u6709\u5c40\u9650\u6027\uff1a\u5176\u957f\u5ea6\u4e0d\u53ef\u53d8\u3002\u7136\u800c\uff0c\u8fd9\u4e2a\u95ee\u9898\u4e0d\u96be\u89e3\u51b3\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u6570\u7ec4\u66ff\u6362\u4e3a\u52a8\u6001\u6570\u7ec4\uff0c\u4ece\u800c\u5f15\u5165\u6269\u5bb9\u673a\u5236\u3002\u6709\u5174\u8da3\u7684\u8bfb\u8005\u53ef\u4ee5\u5c1d\u8bd5\u81ea\u884c\u5b9e\u73b0\u3002
\u4e24\u79cd\u5b9e\u73b0\u7684\u5bf9\u6bd4\u7ed3\u8bba\u4e0e\u6808\u4e00\u81f4\uff0c\u5728\u6b64\u4e0d\u518d\u8d58\u8ff0\u3002
"},{"location":"chapter_stack_and_queue/queue/#523","title":"5.2.3 \u00a0 \u961f\u5217\u5178\u578b\u5e94\u7528","text":"\u300c\u6808 stack\u300d\u662f\u4e00\u79cd\u9075\u5faa\u5148\u5165\u540e\u51fa\u7684\u903b\u8f91\u7684\u7ebf\u6027\u6570\u636e\u7ed3\u6784\u3002
\u6211\u4eec\u53ef\u4ee5\u5c06\u6808\u7c7b\u6bd4\u4e3a\u684c\u9762\u4e0a\u7684\u4e00\u645e\u76d8\u5b50\uff0c\u5982\u679c\u60f3\u53d6\u51fa\u5e95\u90e8\u7684\u76d8\u5b50\uff0c\u5219\u9700\u8981\u5148\u5c06\u4e0a\u9762\u7684\u76d8\u5b50\u4f9d\u6b21\u79fb\u8d70\u3002\u6211\u4eec\u5c06\u76d8\u5b50\u66ff\u6362\u4e3a\u5404\u79cd\u7c7b\u578b\u7684\u5143\u7d20\uff08\u5982\u6574\u6570\u3001\u5b57\u7b26\u3001\u5bf9\u8c61\u7b49\uff09\uff0c\u5c31\u5f97\u5230\u4e86\u6808\u8fd9\u79cd\u6570\u636e\u7ed3\u6784\u3002
\u5982\u56fe 5-1 \u6240\u793a\uff0c\u6211\u4eec\u628a\u5806\u53e0\u5143\u7d20\u7684\u9876\u90e8\u79f0\u4e3a\u201c\u6808\u9876\u201d\uff0c\u5e95\u90e8\u79f0\u4e3a\u201c\u6808\u5e95\u201d\u3002\u5c06\u628a\u5143\u7d20\u6dfb\u52a0\u5230\u6808\u9876\u7684\u64cd\u4f5c\u53eb\u4f5c\u201c\u5165\u6808\u201d\uff0c\u5220\u9664\u6808\u9876\u5143\u7d20\u7684\u64cd\u4f5c\u53eb\u4f5c\u201c\u51fa\u6808\u201d\u3002
\u56fe 5-1 \u00a0 \u6808\u7684\u5148\u5165\u540e\u51fa\u89c4\u5219
"},{"location":"chapter_stack_and_queue/stack/#511","title":"5.1.1 \u00a0 \u6808\u5e38\u7528\u64cd\u4f5c","text":"\u6808\u7684\u5e38\u7528\u64cd\u4f5c\u5982\u8868 5-1 \u6240\u793a\uff0c\u5177\u4f53\u7684\u65b9\u6cd5\u540d\u9700\u8981\u6839\u636e\u6240\u4f7f\u7528\u7684\u7f16\u7a0b\u8bed\u8a00\u6765\u786e\u5b9a\u3002\u5728\u6b64\uff0c\u6211\u4eec\u4ee5\u5e38\u89c1\u7684 push()
\u3001pop()
\u3001peek()
\u547d\u540d\u4e3a\u4f8b\u3002
\u8868 5-1 \u00a0 \u6808\u7684\u64cd\u4f5c\u6548\u7387
\u65b9\u6cd5 \u63cf\u8ff0 \u65f6\u95f4\u590d\u6742\u5ea6 push() \u5143\u7d20\u5165\u6808\uff08\u6dfb\u52a0\u81f3\u6808\u9876\uff09 \\(O(1)\\) pop() \u6808\u9876\u5143\u7d20\u51fa\u6808 \\(O(1)\\) peek() \u8bbf\u95ee\u6808\u9876\u5143\u7d20 \\(O(1)\\)\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u7f16\u7a0b\u8bed\u8a00\u5185\u7f6e\u7684\u6808\u7c7b\u3002\u7136\u800c\uff0c\u67d0\u4e9b\u8bed\u8a00\u53ef\u80fd\u6ca1\u6709\u4e13\u95e8\u63d0\u4f9b\u6808\u7c7b\uff0c\u8fd9\u65f6\u6211\u4eec\u53ef\u4ee5\u5c06\u8be5\u8bed\u8a00\u7684\u201c\u6570\u7ec4\u201d\u6216\u201c\u94fe\u8868\u201d\u5f53\u4f5c\u6808\u6765\u4f7f\u7528\uff0c\u5e76\u5728\u7a0b\u5e8f\u903b\u8f91\u4e0a\u5ffd\u7565\u4e0e\u6808\u65e0\u5173\u7684\u64cd\u4f5c\u3002
PythonC++JavaC#GoSwiftJSTSDartRustCZig stack.py# \u521d\u59cb\u5316\u6808\n# Python \u6ca1\u6709\u5185\u7f6e\u7684\u6808\u7c7b\uff0c\u53ef\u4ee5\u628a List \u5f53\u4f5c\u6808\u6765\u4f7f\u7528 \nstack: list[int] = []\n\n# \u5143\u7d20\u5165\u6808\nstack.append(1)\nstack.append(3)\nstack.append(2)\nstack.append(5)\nstack.append(4)\n\n# \u8bbf\u95ee\u6808\u9876\u5143\u7d20\npeek: int = stack[-1]\n\n# \u5143\u7d20\u51fa\u6808\npop: int = stack.pop()\n\n# \u83b7\u53d6\u6808\u7684\u957f\u5ea6\nsize: int = len(stack)\n\n# \u5224\u65ad\u662f\u5426\u4e3a\u7a7a\nis_empty: bool = len(stack) == 0\n
stack.cpp/* \u521d\u59cb\u5316\u6808 */\nstack<int> stack;\n\n/* \u5143\u7d20\u5165\u6808 */\nstack.push(1);\nstack.push(3);\nstack.push(2);\nstack.push(5);\nstack.push(4);\n\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nint top = stack.top();\n\n/* \u5143\u7d20\u51fa\u6808 */\nstack.pop(); // \u65e0\u8fd4\u56de\u503c\n\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nint size = stack.size();\n\n/* \u5224\u65ad\u662f\u5426\u4e3a\u7a7a */\nbool empty = stack.empty();\n
stack.java/* \u521d\u59cb\u5316\u6808 */\nStack<Integer> stack = new Stack<>();\n\n/* \u5143\u7d20\u5165\u6808 */\nstack.push(1);\nstack.push(3);\nstack.push(2);\nstack.push(5);\nstack.push(4);\n\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nint peek = stack.peek();\n\n/* \u5143\u7d20\u51fa\u6808 */\nint pop = stack.pop();\n\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nint size = stack.size();\n\n/* \u5224\u65ad\u662f\u5426\u4e3a\u7a7a */\nboolean isEmpty = stack.isEmpty();\n
stack.cs/* \u521d\u59cb\u5316\u6808 */\nStack<int> stack = new();\n\n/* \u5143\u7d20\u5165\u6808 */\nstack.Push(1);\nstack.Push(3);\nstack.Push(2);\nstack.Push(5);\nstack.Push(4);\n\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nint peek = stack.Peek();\n\n/* \u5143\u7d20\u51fa\u6808 */\nint pop = stack.Pop();\n\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nint size = stack.Count;\n\n/* \u5224\u65ad\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty = stack.Count == 0;\n
stack_test.go/* \u521d\u59cb\u5316\u6808 */\n// \u5728 Go \u4e2d\uff0c\u63a8\u8350\u5c06 Slice \u5f53\u4f5c\u6808\u6765\u4f7f\u7528\nvar stack []int\n\n/* \u5143\u7d20\u5165\u6808 */\nstack = append(stack, 1)\nstack = append(stack, 3)\nstack = append(stack, 2)\nstack = append(stack, 5)\nstack = append(stack, 4)\n\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\npeek := stack[len(stack)-1]\n\n/* \u5143\u7d20\u51fa\u6808 */\npop := stack[len(stack)-1]\nstack = stack[:len(stack)-1]\n\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nsize := len(stack)\n\n/* \u5224\u65ad\u662f\u5426\u4e3a\u7a7a */\nisEmpty := len(stack) == 0\n
stack.swift/* \u521d\u59cb\u5316\u6808 */\n// Swift \u6ca1\u6709\u5185\u7f6e\u7684\u6808\u7c7b\uff0c\u53ef\u4ee5\u628a Array \u5f53\u4f5c\u6808\u6765\u4f7f\u7528\nvar stack: [Int] = []\n\n/* \u5143\u7d20\u5165\u6808 */\nstack.append(1)\nstack.append(3)\nstack.append(2)\nstack.append(5)\nstack.append(4)\n\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nlet peek = stack.last!\n\n/* \u5143\u7d20\u51fa\u6808 */\nlet pop = stack.removeLast()\n\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nlet size = stack.count\n\n/* \u5224\u65ad\u662f\u5426\u4e3a\u7a7a */\nlet isEmpty = stack.isEmpty\n
stack.js/* \u521d\u59cb\u5316\u6808 */\n// Javascript \u6ca1\u6709\u5185\u7f6e\u7684\u6808\u7c7b\uff0c\u53ef\u4ee5\u628a Array \u5f53\u4f5c\u6808\u6765\u4f7f\u7528 \nconst stack = [];\n\n/* \u5143\u7d20\u5165\u6808 */\nstack.push(1);\nstack.push(3);\nstack.push(2);\nstack.push(5);\nstack.push(4);\n\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nconst peek = stack[stack.length-1];\n\n/* \u5143\u7d20\u51fa\u6808 */\nconst pop = stack.pop();\n\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nconst size = stack.length;\n\n/* \u5224\u65ad\u662f\u5426\u4e3a\u7a7a */\nconst is_empty = stack.length === 0;\n
stack.ts/* \u521d\u59cb\u5316\u6808 */\n// Typescript \u6ca1\u6709\u5185\u7f6e\u7684\u6808\u7c7b\uff0c\u53ef\u4ee5\u628a Array \u5f53\u4f5c\u6808\u6765\u4f7f\u7528 \nconst stack: number[] = [];\n\n/* \u5143\u7d20\u5165\u6808 */\nstack.push(1);\nstack.push(3);\nstack.push(2);\nstack.push(5);\nstack.push(4);\n\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nconst peek = stack[stack.length - 1];\n\n/* \u5143\u7d20\u51fa\u6808 */\nconst pop = stack.pop();\n\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nconst size = stack.length;\n\n/* \u5224\u65ad\u662f\u5426\u4e3a\u7a7a */\nconst is_empty = stack.length === 0;\n
stack.dart/* \u521d\u59cb\u5316\u6808 */\n// Dart \u6ca1\u6709\u5185\u7f6e\u7684\u6808\u7c7b\uff0c\u53ef\u4ee5\u628a List \u5f53\u4f5c\u6808\u6765\u4f7f\u7528\nList<int> stack = [];\n\n/* \u5143\u7d20\u5165\u6808 */\nstack.add(1);\nstack.add(3);\nstack.add(2);\nstack.add(5);\nstack.add(4);\n\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nint peek = stack.last;\n\n/* \u5143\u7d20\u51fa\u6808 */\nint pop = stack.removeLast();\n\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nint size = stack.length;\n\n/* \u5224\u65ad\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty = stack.isEmpty;\n
stack.rs/* \u521d\u59cb\u5316\u6808 */\n// \u628a Vec \u5f53\u4f5c\u6808\u6765\u4f7f\u7528\nlet mut stack: Vec<i32> = Vec::new();\n\n/* \u5143\u7d20\u5165\u6808 */\nstack.push(1);\nstack.push(3);\nstack.push(2);\nstack.push(5);\nstack.push(4);\n\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nlet top = stack.last().unwrap();\n\n/* \u5143\u7d20\u51fa\u6808 */\nlet pop = stack.pop().unwrap();\n\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nlet size = stack.len();\n\n/* \u5224\u65ad\u662f\u5426\u4e3a\u7a7a */\nlet is_empty = stack.is_empty();\n
stack.c// C \u672a\u63d0\u4f9b\u5185\u7f6e\u6808\n
stack.zig\n
"},{"location":"chapter_stack_and_queue/stack/#512","title":"5.1.2 \u00a0 \u6808\u7684\u5b9e\u73b0","text":"\u4e3a\u4e86\u6df1\u5165\u4e86\u89e3\u6808\u7684\u8fd0\u884c\u673a\u5236\uff0c\u6211\u4eec\u6765\u5c1d\u8bd5\u81ea\u5df1\u5b9e\u73b0\u4e00\u4e2a\u6808\u7c7b\u3002
\u6808\u9075\u5faa\u5148\u5165\u540e\u51fa\u7684\u539f\u5219\uff0c\u56e0\u6b64\u6211\u4eec\u53ea\u80fd\u5728\u6808\u9876\u6dfb\u52a0\u6216\u5220\u9664\u5143\u7d20\u3002\u7136\u800c\uff0c\u6570\u7ec4\u548c\u94fe\u8868\u90fd\u53ef\u4ee5\u5728\u4efb\u610f\u4f4d\u7f6e\u6dfb\u52a0\u548c\u5220\u9664\u5143\u7d20\uff0c\u56e0\u6b64\u6808\u53ef\u4ee5\u89c6\u4e3a\u4e00\u79cd\u53d7\u9650\u5236\u7684\u6570\u7ec4\u6216\u94fe\u8868\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u6211\u4eec\u53ef\u4ee5\u201c\u5c4f\u853d\u201d\u6570\u7ec4\u6216\u94fe\u8868\u7684\u90e8\u5206\u65e0\u5173\u64cd\u4f5c\uff0c\u4f7f\u5176\u5bf9\u5916\u8868\u73b0\u7684\u903b\u8f91\u7b26\u5408\u6808\u7684\u7279\u6027\u3002
"},{"location":"chapter_stack_and_queue/stack/#1","title":"1. \u00a0 \u57fa\u4e8e\u94fe\u8868\u7684\u5b9e\u73b0","text":"\u4f7f\u7528\u94fe\u8868\u5b9e\u73b0\u6808\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u94fe\u8868\u7684\u5934\u8282\u70b9\u89c6\u4e3a\u6808\u9876\uff0c\u5c3e\u8282\u70b9\u89c6\u4e3a\u6808\u5e95\u3002
\u5982\u56fe 5-2 \u6240\u793a\uff0c\u5bf9\u4e8e\u5165\u6808\u64cd\u4f5c\uff0c\u6211\u4eec\u53ea\u9700\u5c06\u5143\u7d20\u63d2\u5165\u94fe\u8868\u5934\u90e8\uff0c\u8fd9\u79cd\u8282\u70b9\u63d2\u5165\u65b9\u6cd5\u88ab\u79f0\u4e3a\u201c\u5934\u63d2\u6cd5\u201d\u3002\u800c\u5bf9\u4e8e\u51fa\u6808\u64cd\u4f5c\uff0c\u53ea\u9700\u5c06\u5934\u8282\u70b9\u4ece\u94fe\u8868\u4e2d\u5220\u9664\u5373\u53ef\u3002
LinkedListStackpush()pop()\u56fe 5-2 \u00a0 \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u6808\u7684\u5165\u6808\u51fa\u6808\u64cd\u4f5c
\u4ee5\u4e0b\u662f\u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u6808\u7684\u793a\u4f8b\u4ee3\u7801\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig linkedlist_stack.pyclass LinkedListStack:\n \"\"\"\u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808\"\"\"\n\n def __init__(self):\n \"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n self._peek: ListNode | None = None\n self._size: int = 0\n\n def size(self) -> int:\n \"\"\"\u83b7\u53d6\u6808\u7684\u957f\u5ea6\"\"\"\n return self._size\n\n def is_empty(self) -> bool:\n \"\"\"\u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a\"\"\"\n return not self._peek\n\n def push(self, val: int):\n \"\"\"\u5165\u6808\"\"\"\n node = ListNode(val)\n node.next = self._peek\n self._peek = node\n self._size += 1\n\n def pop(self) -> int:\n \"\"\"\u51fa\u6808\"\"\"\n num = self.peek()\n self._peek = self._peek.next\n self._size -= 1\n return num\n\n def peek(self) -> int:\n \"\"\"\u8bbf\u95ee\u6808\u9876\u5143\u7d20\"\"\"\n if self.is_empty():\n raise IndexError(\"\u6808\u4e3a\u7a7a\")\n return self._peek.val\n\n def to_list(self) -> list[int]:\n \"\"\"\u8f6c\u5316\u4e3a\u5217\u8868\u7528\u4e8e\u6253\u5370\"\"\"\n arr = []\n node = self._peek\n while node:\n arr.append(node.val)\n node = node.next\n arr.reverse()\n return arr\n
linkedlist_stack.cpp/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\nclass LinkedListStack {\n private:\n ListNode *stackTop; // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\n int stkSize; // \u6808\u7684\u957f\u5ea6\n\n public:\n LinkedListStack() {\n stackTop = nullptr;\n stkSize = 0;\n }\n\n ~LinkedListStack() {\n // \u904d\u5386\u94fe\u8868\u5220\u9664\u8282\u70b9\uff0c\u91ca\u653e\u5185\u5b58\n freeMemoryLinkedList(stackTop);\n }\n\n /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n int size() {\n return stkSize;\n }\n\n /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n bool isEmpty() {\n return size() == 0;\n }\n\n /* \u5165\u6808 */\n void push(int num) {\n ListNode *node = new ListNode(num);\n node->next = stackTop;\n stackTop = node;\n stkSize++;\n }\n\n /* \u51fa\u6808 */\n void pop() {\n int num = top();\n ListNode *tmp = stackTop;\n stackTop = stackTop->next;\n // \u91ca\u653e\u5185\u5b58\n delete tmp;\n stkSize--;\n }\n\n /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n int top() {\n if (isEmpty())\n throw out_of_range(\"\u6808\u4e3a\u7a7a\");\n return stackTop->val;\n }\n\n /* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n vector<int> toVector() {\n ListNode *node = stackTop;\n vector<int> res(size());\n for (int i = res.size() - 1; i >= 0; i--) {\n res[i] = node->val;\n node = node->next;\n }\n return res;\n }\n};\n
linkedlist_stack.java/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\nclass LinkedListStack {\n private ListNode stackPeek; // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\n private int stkSize = 0; // \u6808\u7684\u957f\u5ea6\n\n public LinkedListStack() {\n stackPeek = null;\n }\n\n /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n public int size() {\n return stkSize;\n }\n\n /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n public boolean isEmpty() {\n return size() == 0;\n }\n\n /* \u5165\u6808 */\n public void push(int num) {\n ListNode node = new ListNode(num);\n node.next = stackPeek;\n stackPeek = node;\n stkSize++;\n }\n\n /* \u51fa\u6808 */\n public int pop() {\n int num = peek();\n stackPeek = stackPeek.next;\n stkSize--;\n return num;\n }\n\n /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n public int peek() {\n if (isEmpty())\n throw new IndexOutOfBoundsException();\n return stackPeek.val;\n }\n\n /* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n public int[] toArray() {\n ListNode node = stackPeek;\n int[] res = new int[size()];\n for (int i = res.length - 1; i >= 0; i--) {\n res[i] = node.val;\n node = node.next;\n }\n return res;\n }\n}\n
linkedlist_stack.cs/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\nclass LinkedListStack {\n ListNode? stackPeek; // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\n int stkSize = 0; // \u6808\u7684\u957f\u5ea6\n\n public LinkedListStack() {\n stackPeek = null;\n }\n\n /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n public int Size() {\n return stkSize;\n }\n\n /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n public bool IsEmpty() {\n return Size() == 0;\n }\n\n /* \u5165\u6808 */\n public void Push(int num) {\n ListNode node = new(num) {\n next = stackPeek\n };\n stackPeek = node;\n stkSize++;\n }\n\n /* \u51fa\u6808 */\n public int Pop() {\n int num = Peek();\n stackPeek = stackPeek!.next;\n stkSize--;\n return num;\n }\n\n /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n public int Peek() {\n if (IsEmpty())\n throw new Exception();\n return stackPeek!.val;\n }\n\n /* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n public int[] ToArray() {\n if (stackPeek == null)\n return [];\n\n ListNode? node = stackPeek;\n int[] res = new int[Size()];\n for (int i = res.Length - 1; i >= 0; i--) {\n res[i] = node!.val;\n node = node.next;\n }\n return res;\n }\n}\n
linkedlist_stack.go/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\ntype linkedListStack struct {\n // \u4f7f\u7528\u5185\u7f6e\u5305 list \u6765\u5b9e\u73b0\u6808\n data *list.List\n}\n\n/* \u521d\u59cb\u5316\u6808 */\nfunc newLinkedListStack() *linkedListStack {\n return &linkedListStack{\n data: list.New(),\n }\n}\n\n/* \u5165\u6808 */\nfunc (s *linkedListStack) push(value int) {\n s.data.PushBack(value)\n}\n\n/* \u51fa\u6808 */\nfunc (s *linkedListStack) pop() any {\n if s.isEmpty() {\n return nil\n }\n e := s.data.Back()\n s.data.Remove(e)\n return e.Value\n}\n\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nfunc (s *linkedListStack) peek() any {\n if s.isEmpty() {\n return nil\n }\n e := s.data.Back()\n return e.Value\n}\n\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nfunc (s *linkedListStack) size() int {\n return s.data.Len()\n}\n\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\nfunc (s *linkedListStack) isEmpty() bool {\n return s.data.Len() == 0\n}\n\n/* \u83b7\u53d6 List \u7528\u4e8e\u6253\u5370 */\nfunc (s *linkedListStack) toList() *list.List {\n return s.data\n}\n
linkedlist_stack.swift/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\nclass LinkedListStack {\n private var _peek: ListNode? // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\n private var _size = 0 // \u6808\u7684\u957f\u5ea6\n\n init() {}\n\n /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n func size() -> Int {\n _size\n }\n\n /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n func isEmpty() -> Bool {\n size() == 0\n }\n\n /* \u5165\u6808 */\n func push(num: Int) {\n let node = ListNode(x: num)\n node.next = _peek\n _peek = node\n _size += 1\n }\n\n /* \u51fa\u6808 */\n @discardableResult\n func pop() -> Int {\n let num = peek()\n _peek = _peek?.next\n _size -= 1\n return num\n }\n\n /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n func peek() -> Int {\n if isEmpty() {\n fatalError(\"\u6808\u4e3a\u7a7a\")\n }\n return _peek!.val\n }\n\n /* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n func toArray() -> [Int] {\n var node = _peek\n var res = Array(repeating: 0, count: _size)\n for i in sequence(first: res.count - 1, next: { $0 >= 0 + 1 ? $0 - 1 : nil }) {\n res[i] = node!.val\n node = node?.next\n }\n return res\n }\n}\n
linkedlist_stack.js/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\nclass LinkedListStack {\n #stackPeek; // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\n #stkSize = 0; // \u6808\u7684\u957f\u5ea6\n\n constructor() {\n this.#stackPeek = null;\n }\n\n /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n get size() {\n return this.#stkSize;\n }\n\n /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n isEmpty() {\n return this.size === 0;\n }\n\n /* \u5165\u6808 */\n push(num) {\n const node = new ListNode(num);\n node.next = this.#stackPeek;\n this.#stackPeek = node;\n this.#stkSize++;\n }\n\n /* \u51fa\u6808 */\n pop() {\n const num = this.peek();\n this.#stackPeek = this.#stackPeek.next;\n this.#stkSize--;\n return num;\n }\n\n /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n peek() {\n if (!this.#stackPeek) throw new Error('\u6808\u4e3a\u7a7a');\n return this.#stackPeek.val;\n }\n\n /* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n toArray() {\n let node = this.#stackPeek;\n const res = new Array(this.size);\n for (let i = res.length - 1; i >= 0; i--) {\n res[i] = node.val;\n node = node.next;\n }\n return res;\n }\n}\n
linkedlist_stack.ts/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\nclass LinkedListStack {\n private stackPeek: ListNode | null; // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\n private stkSize: number = 0; // \u6808\u7684\u957f\u5ea6\n\n constructor() {\n this.stackPeek = null;\n }\n\n /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n get size(): number {\n return this.stkSize;\n }\n\n /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n isEmpty(): boolean {\n return this.size === 0;\n }\n\n /* \u5165\u6808 */\n push(num: number): void {\n const node = new ListNode(num);\n node.next = this.stackPeek;\n this.stackPeek = node;\n this.stkSize++;\n }\n\n /* \u51fa\u6808 */\n pop(): number {\n const num = this.peek();\n if (!this.stackPeek) throw new Error('\u6808\u4e3a\u7a7a');\n this.stackPeek = this.stackPeek.next;\n this.stkSize--;\n return num;\n }\n\n /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n peek(): number {\n if (!this.stackPeek) throw new Error('\u6808\u4e3a\u7a7a');\n return this.stackPeek.val;\n }\n\n /* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n toArray(): number[] {\n let node = this.stackPeek;\n const res = new Array<number>(this.size);\n for (let i = res.length - 1; i >= 0; i--) {\n res[i] = node!.val;\n node = node!.next;\n }\n return res;\n }\n}\n
linkedlist_stack.dart/* \u57fa\u4e8e\u94fe\u8868\u7c7b\u5b9e\u73b0\u7684\u6808 */\nclass LinkedListStack {\n ListNode? _stackPeek; // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\n int _stkSize = 0; // \u6808\u7684\u957f\u5ea6\n\n LinkedListStack() {\n _stackPeek = null;\n }\n\n /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n int size() {\n return _stkSize;\n }\n\n /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n bool isEmpty() {\n return _stkSize == 0;\n }\n\n /* \u5165\u6808 */\n void push(int _num) {\n final ListNode node = ListNode(_num);\n node.next = _stackPeek;\n _stackPeek = node;\n _stkSize++;\n }\n\n /* \u51fa\u6808 */\n int pop() {\n final int _num = peek();\n _stackPeek = _stackPeek!.next;\n _stkSize--;\n return _num;\n }\n\n /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n int peek() {\n if (_stackPeek == null) {\n throw Exception(\"\u6808\u4e3a\u7a7a\");\n }\n return _stackPeek!.val;\n }\n\n /* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a List \u5e76\u8fd4\u56de */\n List<int> toList() {\n ListNode? node = _stackPeek;\n List<int> list = [];\n while (node != null) {\n list.add(node.val);\n node = node.next;\n }\n list = list.reversed.toList();\n return list;\n }\n}\n
linkedlist_stack.rs/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\n#[allow(dead_code)]\npub struct LinkedListStack<T> {\n stack_peek: Option<Rc<RefCell<ListNode<T>>>>, // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\n stk_size: usize, // \u6808\u7684\u957f\u5ea6\n}\n\nimpl<T: Copy> LinkedListStack<T> {\n pub fn new() -> Self {\n Self {\n stack_peek: None,\n stk_size: 0,\n }\n }\n\n /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n pub fn size(&self) -> usize {\n return self.stk_size;\n }\n\n /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n pub fn is_empty(&self) -> bool {\n return self.size() == 0;\n }\n\n /* \u5165\u6808 */\n pub fn push(&mut self, num: T) {\n let node = ListNode::new(num);\n node.borrow_mut().next = self.stack_peek.take();\n self.stack_peek = Some(node);\n self.stk_size += 1;\n }\n\n /* \u51fa\u6808 */\n pub fn pop(&mut self) -> Option<T> {\n self.stack_peek.take().map(|old_head| {\n match old_head.borrow_mut().next.take() {\n Some(new_head) => {\n self.stack_peek = Some(new_head);\n }\n None => {\n self.stack_peek = None;\n }\n }\n self.stk_size -= 1;\n Rc::try_unwrap(old_head).ok().unwrap().into_inner().val\n })\n }\n\n /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n pub fn peek(&self) -> Option<&Rc<RefCell<ListNode<T>>>> {\n self.stack_peek.as_ref()\n }\n\n /* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n pub fn to_array(&self, head: Option<&Rc<RefCell<ListNode<T>>>>) -> Vec<T> {\n if let Some(node) = head {\n let mut nums = self.to_array(node.borrow().next.as_ref());\n nums.push(node.borrow().val);\n return nums;\n }\n return Vec::new();\n }\n}\n
linkedlist_stack.c/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\ntypedef struct {\n ListNode *top; // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\n int size; // \u6808\u7684\u957f\u5ea6\n} LinkedListStack;\n\n/* \u6784\u9020\u51fd\u6570 */\nLinkedListStack *newLinkedListStack() {\n LinkedListStack *s = malloc(sizeof(LinkedListStack));\n s->top = NULL;\n s->size = 0;\n return s;\n}\n\n/* \u6790\u6784\u51fd\u6570 */\nvoid delLinkedListStack(LinkedListStack *s) {\n while (s->top) {\n ListNode *n = s->top->next;\n free(s->top);\n s->top = n;\n }\n free(s);\n}\n\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nint size(LinkedListStack *s) {\n assert(s);\n return s->size;\n}\n\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty(LinkedListStack *s) {\n assert(s);\n return size(s) == 0;\n}\n\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nint peek(LinkedListStack *s) {\n assert(s);\n assert(size(s) != 0);\n return s->top->val;\n}\n\n/* \u5165\u6808 */\nvoid push(LinkedListStack *s, int num) {\n assert(s);\n ListNode *node = (ListNode *)malloc(sizeof(ListNode));\n node->next = s->top; // \u66f4\u65b0\u65b0\u52a0\u8282\u70b9\u6307\u9488\u57df\n node->val = num; // \u66f4\u65b0\u65b0\u52a0\u8282\u70b9\u6570\u636e\u57df\n s->top = node; // \u66f4\u65b0\u6808\u9876\n s->size++; // \u66f4\u65b0\u6808\u5927\u5c0f\n}\n\n/* \u51fa\u6808 */\nint pop(LinkedListStack *s) {\n if (s->size == 0) {\n printf(\"stack is empty.\\n\");\n return INT_MAX;\n }\n assert(s);\n int val = peek(s);\n ListNode *tmp = s->top;\n s->top = s->top->next;\n // \u91ca\u653e\u5185\u5b58\n free(tmp);\n s->size--;\n return val;\n}\n
linkedlist_stack.zig// \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808\nfn LinkedListStack(comptime T: type) type {\n return struct {\n const Self = @This();\n\n stack_top: ?*inc.ListNode(T) = null, // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\n stk_size: usize = 0, // \u6808\u7684\u957f\u5ea6\n mem_arena: ?std.heap.ArenaAllocator = null,\n mem_allocator: std.mem.Allocator = undefined, // \u5185\u5b58\u5206\u914d\u5668\n\n // \u6784\u9020\u51fd\u6570\uff08\u5206\u914d\u5185\u5b58+\u521d\u59cb\u5316\u6808\uff09\n pub fn init(self: *Self, allocator: std.mem.Allocator) !void {\n if (self.mem_arena == null) {\n self.mem_arena = std.heap.ArenaAllocator.init(allocator);\n self.mem_allocator = self.mem_arena.?.allocator();\n }\n self.stack_top = null;\n self.stk_size = 0;\n }\n\n // \u6790\u6784\u51fd\u6570\uff08\u91ca\u653e\u5185\u5b58\uff09\n pub fn deinit(self: *Self) void {\n if (self.mem_arena == null) return;\n self.mem_arena.?.deinit();\n }\n\n // \u83b7\u53d6\u6808\u7684\u957f\u5ea6\n pub fn size(self: *Self) usize {\n return self.stk_size;\n }\n\n // \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a\n pub fn isEmpty(self: *Self) bool {\n return self.size() == 0;\n }\n\n // \u8bbf\u95ee\u6808\u9876\u5143\u7d20\n pub fn peek(self: *Self) T {\n if (self.size() == 0) @panic(\"\u6808\u4e3a\u7a7a\");\n return self.stack_top.?.val;\n } \n\n // \u5165\u6808\n pub fn push(self: *Self, num: T) !void {\n var node = try self.mem_allocator.create(inc.ListNode(T));\n node.init(num);\n node.next = self.stack_top;\n self.stack_top = node;\n self.stk_size += 1;\n } \n\n // \u51fa\u6808\n pub fn pop(self: *Self) T {\n var num = self.peek();\n self.stack_top = self.stack_top.?.next;\n self.stk_size -= 1;\n return num;\n } \n\n // \u5c06\u6808\u8f6c\u6362\u4e3a\u6570\u7ec4\n pub fn toArray(self: *Self) ![]T {\n var node = self.stack_top;\n var res = try self.mem_allocator.alloc(T, self.size());\n @memset(res, @as(T, 0));\n var i: usize = 0;\n while (i < res.len) : (i += 1) {\n res[res.len - i - 1] = node.?.val;\n node = node.?.next;\n }\n return res;\n }\n };\n}\n
"},{"location":"chapter_stack_and_queue/stack/#2","title":"2. \u00a0 \u57fa\u4e8e\u6570\u7ec4\u7684\u5b9e\u73b0","text":"\u4f7f\u7528\u6570\u7ec4\u5b9e\u73b0\u6808\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u6570\u7ec4\u7684\u5c3e\u90e8\u4f5c\u4e3a\u6808\u9876\u3002\u5982\u56fe 5-3 \u6240\u793a\uff0c\u5165\u6808\u4e0e\u51fa\u6808\u64cd\u4f5c\u5206\u522b\u5bf9\u5e94\u5728\u6570\u7ec4\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20\u4e0e\u5220\u9664\u5143\u7d20\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u90fd\u4e3a \\(O(1)\\) \u3002
ArrayStackpush()pop()\u56fe 5-3 \u00a0 \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u6808\u7684\u5165\u6808\u51fa\u6808\u64cd\u4f5c
\u7531\u4e8e\u5165\u6808\u7684\u5143\u7d20\u53ef\u80fd\u4f1a\u6e90\u6e90\u4e0d\u65ad\u5730\u589e\u52a0\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u52a8\u6001\u6570\u7ec4\uff0c\u8fd9\u6837\u5c31\u65e0\u987b\u81ea\u884c\u5904\u7406\u6570\u7ec4\u6269\u5bb9\u95ee\u9898\u3002\u4ee5\u4e0b\u4e3a\u793a\u4f8b\u4ee3\u7801\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig array_stack.pyclass ArrayStack:\n \"\"\"\u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808\"\"\"\n\n def __init__(self):\n \"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n self._stack: list[int] = []\n\n def size(self) -> int:\n \"\"\"\u83b7\u53d6\u6808\u7684\u957f\u5ea6\"\"\"\n return len(self._stack)\n\n def is_empty(self) -> bool:\n \"\"\"\u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a\"\"\"\n return self._stack == []\n\n def push(self, item: int):\n \"\"\"\u5165\u6808\"\"\"\n self._stack.append(item)\n\n def pop(self) -> int:\n \"\"\"\u51fa\u6808\"\"\"\n if self.is_empty():\n raise IndexError(\"\u6808\u4e3a\u7a7a\")\n return self._stack.pop()\n\n def peek(self) -> int:\n \"\"\"\u8bbf\u95ee\u6808\u9876\u5143\u7d20\"\"\"\n if self.is_empty():\n raise IndexError(\"\u6808\u4e3a\u7a7a\")\n return self._stack[-1]\n\n def to_list(self) -> list[int]:\n \"\"\"\u8fd4\u56de\u5217\u8868\u7528\u4e8e\u6253\u5370\"\"\"\n return self._stack\n
array_stack.cpp/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nclass ArrayStack {\n private:\n vector<int> stack;\n\n public:\n /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n int size() {\n return stack.size();\n }\n\n /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n bool isEmpty() {\n return stack.size() == 0;\n }\n\n /* \u5165\u6808 */\n void push(int num) {\n stack.push_back(num);\n }\n\n /* \u51fa\u6808 */\n void pop() {\n int oldTop = top();\n stack.pop_back();\n }\n\n /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n int top() {\n if (isEmpty())\n throw out_of_range(\"\u6808\u4e3a\u7a7a\");\n return stack.back();\n }\n\n /* \u8fd4\u56de Vector */\n vector<int> toVector() {\n return stack;\n }\n};\n
array_stack.java/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nclass ArrayStack {\n private ArrayList<Integer> stack;\n\n public ArrayStack() {\n // \u521d\u59cb\u5316\u5217\u8868\uff08\u52a8\u6001\u6570\u7ec4\uff09\n stack = new ArrayList<>();\n }\n\n /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n public int size() {\n return stack.size();\n }\n\n /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n public boolean isEmpty() {\n return size() == 0;\n }\n\n /* \u5165\u6808 */\n public void push(int num) {\n stack.add(num);\n }\n\n /* \u51fa\u6808 */\n public int pop() {\n if (isEmpty())\n throw new IndexOutOfBoundsException();\n return stack.remove(size() - 1);\n }\n\n /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n public int peek() {\n if (isEmpty())\n throw new IndexOutOfBoundsException();\n return stack.get(size() - 1);\n }\n\n /* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n public Object[] toArray() {\n return stack.toArray();\n }\n}\n
array_stack.cs/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nclass ArrayStack {\n List<int> stack;\n public ArrayStack() {\n // \u521d\u59cb\u5316\u5217\u8868\uff08\u52a8\u6001\u6570\u7ec4\uff09\n stack = [];\n }\n\n /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n public int Size() {\n return stack.Count;\n }\n\n /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n public bool IsEmpty() {\n return Size() == 0;\n }\n\n /* \u5165\u6808 */\n public void Push(int num) {\n stack.Add(num);\n }\n\n /* \u51fa\u6808 */\n public int Pop() {\n if (IsEmpty())\n throw new Exception();\n var val = Peek();\n stack.RemoveAt(Size() - 1);\n return val;\n }\n\n /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n public int Peek() {\n if (IsEmpty())\n throw new Exception();\n return stack[Size() - 1];\n }\n\n /* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n public int[] ToArray() {\n return [.. stack];\n }\n}\n
array_stack.go/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\ntype arrayStack struct {\n data []int // \u6570\u636e\n}\n\n/* \u521d\u59cb\u5316\u6808 */\nfunc newArrayStack() *arrayStack {\n return &arrayStack{\n // \u8bbe\u7f6e\u6808\u7684\u957f\u5ea6\u4e3a 0\uff0c\u5bb9\u91cf\u4e3a 16\n data: make([]int, 0, 16),\n }\n}\n\n/* \u6808\u7684\u957f\u5ea6 */\nfunc (s *arrayStack) size() int {\n return len(s.data)\n}\n\n/* \u6808\u662f\u5426\u4e3a\u7a7a */\nfunc (s *arrayStack) isEmpty() bool {\n return s.size() == 0\n}\n\n/* \u5165\u6808 */\nfunc (s *arrayStack) push(v int) {\n // \u5207\u7247\u4f1a\u81ea\u52a8\u6269\u5bb9\n s.data = append(s.data, v)\n}\n\n/* \u51fa\u6808 */\nfunc (s *arrayStack) pop() any {\n val := s.peek()\n s.data = s.data[:len(s.data)-1]\n return val\n}\n\n/* \u83b7\u53d6\u6808\u9876\u5143\u7d20 */\nfunc (s *arrayStack) peek() any {\n if s.isEmpty() {\n return nil\n }\n val := s.data[len(s.data)-1]\n return val\n}\n\n/* \u83b7\u53d6 Slice \u7528\u4e8e\u6253\u5370 */\nfunc (s *arrayStack) toSlice() []int {\n return s.data\n}\n
array_stack.swift/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nclass ArrayStack {\n private var stack: [Int]\n\n init() {\n // \u521d\u59cb\u5316\u5217\u8868\uff08\u52a8\u6001\u6570\u7ec4\uff09\n stack = []\n }\n\n /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n func size() -> Int {\n stack.count\n }\n\n /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n func isEmpty() -> Bool {\n stack.isEmpty\n }\n\n /* \u5165\u6808 */\n func push(num: Int) {\n stack.append(num)\n }\n\n /* \u51fa\u6808 */\n @discardableResult\n func pop() -> Int {\n if isEmpty() {\n fatalError(\"\u6808\u4e3a\u7a7a\")\n }\n return stack.removeLast()\n }\n\n /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n func peek() -> Int {\n if isEmpty() {\n fatalError(\"\u6808\u4e3a\u7a7a\")\n }\n return stack.last!\n }\n\n /* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n func toArray() -> [Int] {\n stack\n }\n}\n
array_stack.js/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nclass ArrayStack {\n #stack;\n constructor() {\n this.#stack = [];\n }\n\n /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n get size() {\n return this.#stack.length;\n }\n\n /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n isEmpty() {\n return this.#stack.length === 0;\n }\n\n /* \u5165\u6808 */\n push(num) {\n this.#stack.push(num);\n }\n\n /* \u51fa\u6808 */\n pop() {\n if (this.isEmpty()) throw new Error('\u6808\u4e3a\u7a7a');\n return this.#stack.pop();\n }\n\n /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n top() {\n if (this.isEmpty()) throw new Error('\u6808\u4e3a\u7a7a');\n return this.#stack[this.#stack.length - 1];\n }\n\n /* \u8fd4\u56de Array */\n toArray() {\n return this.#stack;\n }\n}\n
array_stack.ts/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nclass ArrayStack {\n private stack: number[];\n constructor() {\n this.stack = [];\n }\n\n /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n get size(): number {\n return this.stack.length;\n }\n\n /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n isEmpty(): boolean {\n return this.stack.length === 0;\n }\n\n /* \u5165\u6808 */\n push(num: number): void {\n this.stack.push(num);\n }\n\n /* \u51fa\u6808 */\n pop(): number | undefined {\n if (this.isEmpty()) throw new Error('\u6808\u4e3a\u7a7a');\n return this.stack.pop();\n }\n\n /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n top(): number | undefined {\n if (this.isEmpty()) throw new Error('\u6808\u4e3a\u7a7a');\n return this.stack[this.stack.length - 1];\n }\n\n /* \u8fd4\u56de Array */\n toArray() {\n return this.stack;\n }\n}\n
array_stack.dart/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nclass ArrayStack {\n late List<int> _stack;\n ArrayStack() {\n _stack = [];\n }\n\n /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n int size() {\n return _stack.length;\n }\n\n /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n bool isEmpty() {\n return _stack.isEmpty;\n }\n\n /* \u5165\u6808 */\n void push(int _num) {\n _stack.add(_num);\n }\n\n /* \u51fa\u6808 */\n int pop() {\n if (isEmpty()) {\n throw Exception(\"\u6808\u4e3a\u7a7a\");\n }\n return _stack.removeLast();\n }\n\n /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n int peek() {\n if (isEmpty()) {\n throw Exception(\"\u6808\u4e3a\u7a7a\");\n }\n return _stack.last;\n }\n\n /* \u5c06\u6808\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n List<int> toArray() => _stack;\n}\n
array_stack.rs/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nstruct ArrayStack<T> {\n stack: Vec<T>,\n}\n\nimpl<T> ArrayStack<T> {\n /* \u521d\u59cb\u5316\u6808 */\n fn new() -> ArrayStack<T> {\n ArrayStack::<T> { stack: Vec::<T>::new() }\n }\n\n /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n fn size(&self) -> usize {\n self.stack.len()\n }\n\n /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n fn is_empty(&self) -> bool {\n self.size() == 0\n }\n\n /* \u5165\u6808 */\n fn push(&mut self, num: T) {\n self.stack.push(num);\n }\n\n /* \u51fa\u6808 */\n fn pop(&mut self) -> Option<T> {\n match self.stack.pop() {\n Some(num) => Some(num),\n None => None,\n }\n }\n\n /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n fn peek(&self) -> Option<&T> {\n if self.is_empty() { panic!(\"\u6808\u4e3a\u7a7a\") };\n self.stack.last()\n }\n\n /* \u8fd4\u56de &Vec */\n fn to_array(&self) -> &Vec<T> {\n &self.stack\n }\n}\n
array_stack.c/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\ntypedef struct {\n int *data;\n int size;\n} ArrayStack;\n\n/* \u6784\u9020\u51fd\u6570 */\nArrayStack *newArrayStack() {\n ArrayStack *stack = malloc(sizeof(ArrayStack));\n // \u521d\u59cb\u5316\u4e00\u4e2a\u5927\u5bb9\u91cf\uff0c\u907f\u514d\u6269\u5bb9\n stack->data = malloc(sizeof(int) * MAX_SIZE);\n stack->size = 0;\n return stack;\n}\n\n/* \u6790\u6784\u51fd\u6570 */\nvoid delArrayStack(ArrayStack *stack) {\n free(stack->data);\n free(stack);\n}\n\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nint size(ArrayStack *stack) {\n return stack->size;\n}\n\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty(ArrayStack *stack) {\n return stack->size == 0;\n}\n\n/* \u5165\u6808 */\nvoid push(ArrayStack *stack, int num) {\n if (stack->size == MAX_SIZE) {\n printf(\"stack is full.\\n\");\n return;\n }\n stack->data[stack->size] = num;\n stack->size++;\n}\n\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nint peek(ArrayStack *stack) {\n if (stack->size == 0) {\n printf(\"stack is empty.\\n\");\n return INT_MAX;\n }\n return stack->data[stack->size - 1];\n}\n\n/* \u51fa\u6808 */\nint pop(ArrayStack *stack) {\n if (stack->size == 0) {\n printf(\"stack is empty.\\n\");\n return INT_MAX;\n }\n int val = peek(stack);\n stack->size--;\n return val;\n}\n
array_stack.zig// \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808\nfn ArrayStack(comptime T: type) type {\n return struct {\n const Self = @This();\n\n stack: ?std.ArrayList(T) = null, \n\n // \u6784\u9020\u65b9\u6cd5\uff08\u5206\u914d\u5185\u5b58+\u521d\u59cb\u5316\u6808\uff09\n pub fn init(self: *Self, allocator: std.mem.Allocator) void {\n if (self.stack == null) {\n self.stack = std.ArrayList(T).init(allocator);\n }\n }\n\n // \u6790\u6784\u65b9\u6cd5\uff08\u91ca\u653e\u5185\u5b58\uff09\n pub fn deinit(self: *Self) void {\n if (self.stack == null) return;\n self.stack.?.deinit();\n }\n\n // \u83b7\u53d6\u6808\u7684\u957f\u5ea6\n pub fn size(self: *Self) usize {\n return self.stack.?.items.len;\n }\n\n // \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a\n pub fn isEmpty(self: *Self) bool {\n return self.size() == 0;\n }\n\n // \u8bbf\u95ee\u6808\u9876\u5143\u7d20\n pub fn peek(self: *Self) T {\n if (self.isEmpty()) @panic(\"\u6808\u4e3a\u7a7a\");\n return self.stack.?.items[self.size() - 1];\n } \n\n // \u5165\u6808\n pub fn push(self: *Self, num: T) !void {\n try self.stack.?.append(num);\n } \n\n // \u51fa\u6808\n pub fn pop(self: *Self) T {\n var num = self.stack.?.pop();\n return num;\n } \n\n // \u8fd4\u56de ArrayList\n pub fn toList(self: *Self) std.ArrayList(T) {\n return self.stack.?;\n }\n };\n}\n
"},{"location":"chapter_stack_and_queue/stack/#513","title":"5.1.3 \u00a0 \u4e24\u79cd\u5b9e\u73b0\u5bf9\u6bd4","text":"\u652f\u6301\u64cd\u4f5c
\u4e24\u79cd\u5b9e\u73b0\u90fd\u652f\u6301\u6808\u5b9a\u4e49\u4e2d\u7684\u5404\u9879\u64cd\u4f5c\u3002\u6570\u7ec4\u5b9e\u73b0\u989d\u5916\u652f\u6301\u968f\u673a\u8bbf\u95ee\uff0c\u4f46\u8fd9\u5df2\u8d85\u51fa\u4e86\u6808\u7684\u5b9a\u4e49\u8303\u7574\uff0c\u56e0\u6b64\u4e00\u822c\u4e0d\u4f1a\u7528\u5230\u3002
\u65f6\u95f4\u6548\u7387
\u5728\u57fa\u4e8e\u6570\u7ec4\u7684\u5b9e\u73b0\u4e2d\uff0c\u5165\u6808\u548c\u51fa\u6808\u64cd\u4f5c\u90fd\u5728\u9884\u5148\u5206\u914d\u597d\u7684\u8fde\u7eed\u5185\u5b58\u4e2d\u8fdb\u884c\uff0c\u5177\u6709\u5f88\u597d\u7684\u7f13\u5b58\u672c\u5730\u6027\uff0c\u56e0\u6b64\u6548\u7387\u8f83\u9ad8\u3002\u7136\u800c\uff0c\u5982\u679c\u5165\u6808\u65f6\u8d85\u51fa\u6570\u7ec4\u5bb9\u91cf\uff0c\u4f1a\u89e6\u53d1\u6269\u5bb9\u673a\u5236\uff0c\u5bfc\u81f4\u8be5\u6b21\u5165\u6808\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u53d8\u4e3a \\(O(n)\\) \u3002
\u5728\u57fa\u4e8e\u94fe\u8868\u7684\u5b9e\u73b0\u4e2d\uff0c\u94fe\u8868\u7684\u6269\u5bb9\u975e\u5e38\u7075\u6d3b\uff0c\u4e0d\u5b58\u5728\u4e0a\u8ff0\u6570\u7ec4\u6269\u5bb9\u65f6\u6548\u7387\u964d\u4f4e\u7684\u95ee\u9898\u3002\u4f46\u662f\uff0c\u5165\u6808\u64cd\u4f5c\u9700\u8981\u521d\u59cb\u5316\u8282\u70b9\u5bf9\u8c61\u5e76\u4fee\u6539\u6307\u9488\uff0c\u56e0\u6b64\u6548\u7387\u76f8\u5bf9\u8f83\u4f4e\u3002\u4e0d\u8fc7\uff0c\u5982\u679c\u5165\u6808\u5143\u7d20\u672c\u8eab\u5c31\u662f\u8282\u70b9\u5bf9\u8c61\uff0c\u90a3\u4e48\u53ef\u4ee5\u7701\u53bb\u521d\u59cb\u5316\u6b65\u9aa4\uff0c\u4ece\u800c\u63d0\u9ad8\u6548\u7387\u3002
\u7efc\u4e0a\u6240\u8ff0\uff0c\u5f53\u5165\u6808\u4e0e\u51fa\u6808\u64cd\u4f5c\u7684\u5143\u7d20\u662f\u57fa\u672c\u6570\u636e\u7c7b\u578b\u65f6\uff0c\u4f8b\u5982 int
\u6216 double
\uff0c\u6211\u4eec\u53ef\u4ee5\u5f97\u51fa\u4ee5\u4e0b\u7ed3\u8bba\u3002
\u7a7a\u95f4\u6548\u7387
\u5728\u521d\u59cb\u5316\u5217\u8868\u65f6\uff0c\u7cfb\u7edf\u4f1a\u4e3a\u5217\u8868\u5206\u914d\u201c\u521d\u59cb\u5bb9\u91cf\u201d\uff0c\u8be5\u5bb9\u91cf\u53ef\u80fd\u8d85\u51fa\u5b9e\u9645\u9700\u6c42\uff1b\u5e76\u4e14\uff0c\u6269\u5bb9\u673a\u5236\u901a\u5e38\u662f\u6309\u7167\u7279\u5b9a\u500d\u7387\uff08\u4f8b\u5982 2 \u500d\uff09\u8fdb\u884c\u6269\u5bb9\u7684\uff0c\u6269\u5bb9\u540e\u7684\u5bb9\u91cf\u4e5f\u53ef\u80fd\u8d85\u51fa\u5b9e\u9645\u9700\u6c42\u3002\u56e0\u6b64\uff0c\u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808\u53ef\u80fd\u9020\u6210\u4e00\u5b9a\u7684\u7a7a\u95f4\u6d6a\u8d39\u3002
\u7136\u800c\uff0c\u7531\u4e8e\u94fe\u8868\u8282\u70b9\u9700\u8981\u989d\u5916\u5b58\u50a8\u6307\u9488\uff0c\u56e0\u6b64\u94fe\u8868\u8282\u70b9\u5360\u7528\u7684\u7a7a\u95f4\u76f8\u5bf9\u8f83\u5927\u3002
\u7efc\u4e0a\uff0c\u6211\u4eec\u4e0d\u80fd\u7b80\u5355\u5730\u786e\u5b9a\u54ea\u79cd\u5b9e\u73b0\u66f4\u52a0\u8282\u7701\u5185\u5b58\uff0c\u9700\u8981\u9488\u5bf9\u5177\u4f53\u60c5\u51b5\u8fdb\u884c\u5206\u6790\u3002
"},{"location":"chapter_stack_and_queue/stack/#514","title":"5.1.4 \u00a0 \u6808\u5178\u578b\u5e94\u7528","text":"\u6d4f\u89c8\u5668\u7684\u524d\u8fdb\u540e\u9000\u662f\u5426\u662f\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\uff1f
\u6d4f\u89c8\u5668\u7684\u524d\u8fdb\u540e\u9000\u529f\u80fd\u672c\u8d28\u4e0a\u662f\u201c\u6808\u201d\u7684\u4f53\u73b0\u3002\u5f53\u7528\u6237\u8bbf\u95ee\u4e00\u4e2a\u65b0\u9875\u9762\u65f6\uff0c\u8be5\u9875\u9762\u4f1a\u88ab\u6dfb\u52a0\u5230\u6808\u9876\uff1b\u5f53\u7528\u6237\u70b9\u51fb\u540e\u9000\u6309\u94ae\u65f6\uff0c\u8be5\u9875\u9762\u4f1a\u4ece\u6808\u9876\u5f39\u51fa\u3002\u4f7f\u7528\u53cc\u5411\u961f\u5217\u53ef\u4ee5\u65b9\u4fbf\u5730\u5b9e\u73b0\u4e00\u4e9b\u989d\u5916\u64cd\u4f5c\uff0c\u8fd9\u4e2a\u5728\u201c\u53cc\u5411\u961f\u5217\u201d\u7ae0\u8282\u6709\u63d0\u5230\u3002
\u5728\u51fa\u6808\u540e\uff0c\u662f\u5426\u9700\u8981\u91ca\u653e\u51fa\u6808\u8282\u70b9\u7684\u5185\u5b58\uff1f
\u5982\u679c\u540e\u7eed\u4ecd\u9700\u8981\u4f7f\u7528\u5f39\u51fa\u8282\u70b9\uff0c\u5219\u4e0d\u9700\u8981\u91ca\u653e\u5185\u5b58\u3002\u82e5\u4e4b\u540e\u4e0d\u9700\u8981\u7528\u5230\uff0cJava
\u548c Python
\u7b49\u8bed\u8a00\u62e5\u6709\u81ea\u52a8\u5783\u573e\u56de\u6536\u673a\u5236\uff0c\u56e0\u6b64\u4e0d\u9700\u8981\u624b\u52a8\u91ca\u653e\u5185\u5b58\uff1b\u5728 C
\u548c C++
\u4e2d\u9700\u8981\u624b\u52a8\u91ca\u653e\u5185\u5b58\u3002
\u53cc\u5411\u961f\u5217\u50cf\u662f\u4e24\u4e2a\u6808\u62fc\u63a5\u5728\u4e86\u4e00\u8d77\uff0c\u5b83\u7684\u7528\u9014\u662f\u4ec0\u4e48\uff1f
\u53cc\u5411\u961f\u5217\u5c31\u50cf\u662f\u6808\u548c\u961f\u5217\u7684\u7ec4\u5408\uff0c\u6216\u4e24\u4e2a\u6808\u62fc\u5728\u4e86\u4e00\u8d77\u3002\u5b83\u8868\u73b0\u7684\u662f\u6808 + \u961f\u5217\u7684\u903b\u8f91\uff0c\u56e0\u6b64\u53ef\u4ee5\u5b9e\u73b0\u6808\u4e0e\u961f\u5217\u7684\u6240\u6709\u5e94\u7528\uff0c\u5e76\u4e14\u66f4\u52a0\u7075\u6d3b\u3002
\u64a4\u9500\uff08undo\uff09\u548c\u53cd\u64a4\u9500\uff08redo\uff09\u5177\u4f53\u662f\u5982\u4f55\u5b9e\u73b0\u7684\uff1f
\u4f7f\u7528\u4e24\u4e2a\u6808\uff0c\u6808 A
\u7528\u4e8e\u64a4\u9500\uff0c\u6808 B
\u7528\u4e8e\u53cd\u64a4\u9500\u3002
A
\uff0c\u5e76\u6e05\u7a7a\u6808 B
\u3002A
\u4e2d\u5f39\u51fa\u6700\u8fd1\u7684\u64cd\u4f5c\uff0c\u5e76\u5c06\u5176\u538b\u5165\u6808 B
\u3002B
\u4e2d\u5f39\u51fa\u6700\u8fd1\u7684\u64cd\u4f5c\uff0c\u5e76\u5c06\u5176\u538b\u5165\u6808 A
\u3002Abstract
\u53c2\u5929\u5927\u6811\u5145\u6ee1\u751f\u547d\u529b\uff0c\u6839\u6df1\u53f6\u8302\uff0c\u5206\u679d\u6276\u758f\u3002
\u5b83\u4e3a\u6211\u4eec\u5c55\u73b0\u4e86\u6570\u636e\u5206\u6cbb\u7684\u751f\u52a8\u5f62\u6001\u3002
"},{"location":"chapter_tree/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"\u5728\u94fe\u8868\u8868\u793a\u4e0b\uff0c\u4e8c\u53c9\u6811\u7684\u5b58\u50a8\u5355\u5143\u4e3a\u8282\u70b9 TreeNode
\uff0c\u8282\u70b9\u4e4b\u95f4\u901a\u8fc7\u6307\u9488\u76f8\u8fde\u63a5\u3002\u4e0a\u4e00\u8282\u4ecb\u7ecd\u4e86\u94fe\u8868\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7684\u5404\u9879\u57fa\u672c\u64cd\u4f5c\u3002
\u90a3\u4e48\uff0c\u6211\u4eec\u80fd\u5426\u7528\u6570\u7ec4\u6765\u8868\u793a\u4e8c\u53c9\u6811\u5462\uff1f\u7b54\u6848\u662f\u80af\u5b9a\u7684\u3002
"},{"location":"chapter_tree/array_representation_of_tree/#731","title":"7.3.1 \u00a0 \u8868\u793a\u5b8c\u7f8e\u4e8c\u53c9\u6811","text":"\u5148\u5206\u6790\u4e00\u4e2a\u7b80\u5355\u6848\u4f8b\u3002\u7ed9\u5b9a\u4e00\u68f5\u5b8c\u7f8e\u4e8c\u53c9\u6811\uff0c\u6211\u4eec\u5c06\u6240\u6709\u8282\u70b9\u6309\u7167\u5c42\u5e8f\u904d\u5386\u7684\u987a\u5e8f\u5b58\u50a8\u5728\u4e00\u4e2a\u6570\u7ec4\u4e2d\uff0c\u5219\u6bcf\u4e2a\u8282\u70b9\u90fd\u5bf9\u5e94\u552f\u4e00\u7684\u6570\u7ec4\u7d22\u5f15\u3002
\u6839\u636e\u5c42\u5e8f\u904d\u5386\u7684\u7279\u6027\uff0c\u6211\u4eec\u53ef\u4ee5\u63a8\u5bfc\u51fa\u7236\u8282\u70b9\u7d22\u5f15\u4e0e\u5b50\u8282\u70b9\u7d22\u5f15\u4e4b\u95f4\u7684\u201c\u6620\u5c04\u516c\u5f0f\u201d\uff1a\u82e5\u67d0\u8282\u70b9\u7684\u7d22\u5f15\u4e3a \\(i\\) \uff0c\u5219\u8be5\u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7d22\u5f15\u4e3a \\(2i + 1\\) \uff0c\u53f3\u5b50\u8282\u70b9\u7d22\u5f15\u4e3a \\(2i + 2\\) \u3002\u56fe 7-12 \u5c55\u793a\u4e86\u5404\u4e2a\u8282\u70b9\u7d22\u5f15\u4e4b\u95f4\u7684\u6620\u5c04\u5173\u7cfb\u3002
\u56fe 7-12 \u00a0 \u5b8c\u7f8e\u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a
\u6620\u5c04\u516c\u5f0f\u7684\u89d2\u8272\u76f8\u5f53\u4e8e\u94fe\u8868\u4e2d\u7684\u6307\u9488\u3002\u7ed9\u5b9a\u6570\u7ec4\u4e2d\u7684\u4efb\u610f\u4e00\u4e2a\u8282\u70b9\uff0c\u6211\u4eec\u90fd\u53ef\u4ee5\u901a\u8fc7\u6620\u5c04\u516c\u5f0f\u6765\u8bbf\u95ee\u5b83\u7684\u5de6\uff08\u53f3\uff09\u5b50\u8282\u70b9\u3002
"},{"location":"chapter_tree/array_representation_of_tree/#732","title":"7.3.2 \u00a0 \u8868\u793a\u4efb\u610f\u4e8c\u53c9\u6811","text":"\u5b8c\u7f8e\u4e8c\u53c9\u6811\u662f\u4e00\u4e2a\u7279\u4f8b\uff0c\u5728\u4e8c\u53c9\u6811\u7684\u4e2d\u95f4\u5c42\u901a\u5e38\u5b58\u5728\u8bb8\u591a \\(\\text{None}\\) \u3002\u7531\u4e8e\u5c42\u5e8f\u904d\u5386\u5e8f\u5217\u5e76\u4e0d\u5305\u542b\u8fd9\u4e9b \\(\\text{None}\\) \uff0c\u56e0\u6b64\u6211\u4eec\u65e0\u6cd5\u4ec5\u51ed\u8be5\u5e8f\u5217\u6765\u63a8\u6d4b \\(\\text{None}\\) \u7684\u6570\u91cf\u548c\u5206\u5e03\u4f4d\u7f6e\u3002\u8fd9\u610f\u5473\u7740\u5b58\u5728\u591a\u79cd\u4e8c\u53c9\u6811\u7ed3\u6784\u90fd\u7b26\u5408\u8be5\u5c42\u5e8f\u904d\u5386\u5e8f\u5217\u3002
\u5982\u56fe 7-13 \u6240\u793a\uff0c\u7ed9\u5b9a\u4e00\u68f5\u975e\u5b8c\u7f8e\u4e8c\u53c9\u6811\uff0c\u4e0a\u8ff0\u6570\u7ec4\u8868\u793a\u65b9\u6cd5\u5df2\u7ecf\u5931\u6548\u3002
\u56fe 7-13 \u00a0 \u5c42\u5e8f\u904d\u5386\u5e8f\u5217\u5bf9\u5e94\u591a\u79cd\u4e8c\u53c9\u6811\u53ef\u80fd\u6027
\u4e3a\u4e86\u89e3\u51b3\u6b64\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u8003\u8651\u5728\u5c42\u5e8f\u904d\u5386\u5e8f\u5217\u4e2d\u663e\u5f0f\u5730\u5199\u51fa\u6240\u6709 \\(\\text{None}\\) \u3002\u5982\u56fe 7-14 \u6240\u793a\uff0c\u8fd9\u6837\u5904\u7406\u540e\uff0c\u5c42\u5e8f\u904d\u5386\u5e8f\u5217\u5c31\u53ef\u4ee5\u552f\u4e00\u8868\u793a\u4e8c\u53c9\u6811\u4e86\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig# \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a\n# \u4f7f\u7528 None \u6765\u8868\u793a\u7a7a\u4f4d\ntree = [1, 2, 3, 4, None, 6, 7, 8, 9, None, None, 12, None, None, 15]\n
/* \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a */\n// \u4f7f\u7528 int \u6700\u5927\u503c INT_MAX \u6807\u8bb0\u7a7a\u4f4d\nvector<int> tree = {1, 2, 3, 4, INT_MAX, 6, 7, 8, 9, INT_MAX, INT_MAX, 12, INT_MAX, INT_MAX, 15};\n
/* \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a */\n// \u4f7f\u7528 int \u7684\u5305\u88c5\u7c7b Integer \uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528 null \u6765\u6807\u8bb0\u7a7a\u4f4d\nInteger[] tree = { 1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15 };\n
/* \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a */\n// \u4f7f\u7528 int? \u53ef\u7a7a\u7c7b\u578b \uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528 null \u6765\u6807\u8bb0\u7a7a\u4f4d\nint?[] tree = [1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15];\n
/* \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a */\n// \u4f7f\u7528 any \u7c7b\u578b\u7684\u5207\u7247, \u5c31\u53ef\u4ee5\u4f7f\u7528 nil \u6765\u6807\u8bb0\u7a7a\u4f4d\ntree := []any{1, 2, 3, 4, nil, 6, 7, 8, 9, nil, nil, 12, nil, nil, 15}\n
/* \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a */\n// \u4f7f\u7528 Int? \u53ef\u7a7a\u7c7b\u578b \uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528 nil \u6765\u6807\u8bb0\u7a7a\u4f4d\nlet tree: [Int?] = [1, 2, 3, 4, nil, 6, 7, 8, 9, nil, nil, 12, nil, nil, 15]\n
/* \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a */\n// \u4f7f\u7528 null \u6765\u8868\u793a\u7a7a\u4f4d\nlet tree = [1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15];\n
/* \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a */\n// \u4f7f\u7528 null \u6765\u8868\u793a\u7a7a\u4f4d\nlet tree: (number | null)[] = [1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15];\n
/* \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a */\n// \u4f7f\u7528 int? \u53ef\u7a7a\u7c7b\u578b \uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528 null \u6765\u6807\u8bb0\u7a7a\u4f4d\nList<int?> tree = [1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15];\n
/* \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a */\n// \u4f7f\u7528 None \u6765\u6807\u8bb0\u7a7a\u4f4d\nlet tree = [Some(1), Some(2), Some(3), Some(4), None, Some(6), Some(7), Some(8), Some(9), None, None, Some(12), None, None, Some(15)];\n
/* \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a */\n// \u4f7f\u7528 int \u6700\u5927\u503c\u6807\u8bb0\u7a7a\u4f4d\uff0c\u56e0\u6b64\u8981\u6c42\u8282\u70b9\u503c\u4e0d\u80fd\u4e3a INT_MAX\nint tree[] = {1, 2, 3, 4, INT_MAX, 6, 7, 8, 9, INT_MAX, INT_MAX, 12, INT_MAX, INT_MAX, 15};\n
\n
\u56fe 7-14 \u00a0 \u4efb\u610f\u7c7b\u578b\u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a
\u503c\u5f97\u8bf4\u660e\u7684\u662f\uff0c\u5b8c\u5168\u4e8c\u53c9\u6811\u975e\u5e38\u9002\u5408\u4f7f\u7528\u6570\u7ec4\u6765\u8868\u793a\u3002\u56de\u987e\u5b8c\u5168\u4e8c\u53c9\u6811\u7684\u5b9a\u4e49\uff0c\\(\\text{None}\\) \u53ea\u51fa\u73b0\u5728\u6700\u5e95\u5c42\u4e14\u9760\u53f3\u7684\u4f4d\u7f6e\uff0c\u56e0\u6b64\u6240\u6709 \\(\\text{None}\\) \u4e00\u5b9a\u51fa\u73b0\u5728\u5c42\u5e8f\u904d\u5386\u5e8f\u5217\u7684\u672b\u5c3e\u3002
\u8fd9\u610f\u5473\u7740\u4f7f\u7528\u6570\u7ec4\u8868\u793a\u5b8c\u5168\u4e8c\u53c9\u6811\u65f6\uff0c\u53ef\u4ee5\u7701\u7565\u5b58\u50a8\u6240\u6709 \\(\\text{None}\\) \uff0c\u975e\u5e38\u65b9\u4fbf\u3002\u56fe 7-15 \u7ed9\u51fa\u4e86\u4e00\u4e2a\u4f8b\u5b50\u3002
\u56fe 7-15 \u00a0 \u5b8c\u5168\u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a
\u4ee5\u4e0b\u4ee3\u7801\u5b9e\u73b0\u4e86\u4e00\u68f5\u57fa\u4e8e\u6570\u7ec4\u8868\u793a\u7684\u4e8c\u53c9\u6811\uff0c\u5305\u62ec\u4ee5\u4e0b\u51e0\u79cd\u64cd\u4f5c\u3002
class ArrayBinaryTree:\n \"\"\"\u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b\"\"\"\n\n def __init__(self, arr: list[int | None]):\n \"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n self._tree = list(arr)\n\n def size(self):\n \"\"\"\u8282\u70b9\u6570\u91cf\"\"\"\n return len(self._tree)\n\n def val(self, i: int) -> int:\n \"\"\"\u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c\"\"\"\n # \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de None \uff0c\u4ee3\u8868\u7a7a\u4f4d\n if i < 0 or i >= self.size():\n return None\n return self._tree[i]\n\n def left(self, i: int) -> int | None:\n \"\"\"\u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15\"\"\"\n return 2 * i + 1\n\n def right(self, i: int) -> int | None:\n \"\"\"\u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15\"\"\"\n return 2 * i + 2\n\n def parent(self, i: int) -> int | None:\n \"\"\"\u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15\"\"\"\n return (i - 1) // 2\n\n def level_order(self) -> list[int]:\n \"\"\"\u5c42\u5e8f\u904d\u5386\"\"\"\n self.res = []\n # \u76f4\u63a5\u904d\u5386\u6570\u7ec4\n for i in range(self.size()):\n if self.val(i) is not None:\n self.res.append(self.val(i))\n return self.res\n\n def dfs(self, i: int, order: str):\n \"\"\"\u6df1\u5ea6\u4f18\u5148\u904d\u5386\"\"\"\n if self.val(i) is None:\n return\n # \u524d\u5e8f\u904d\u5386\n if order == \"pre\":\n self.res.append(self.val(i))\n self.dfs(self.left(i), order)\n # \u4e2d\u5e8f\u904d\u5386\n if order == \"in\":\n self.res.append(self.val(i))\n self.dfs(self.right(i), order)\n # \u540e\u5e8f\u904d\u5386\n if order == \"post\":\n self.res.append(self.val(i))\n\n def pre_order(self) -> list[int]:\n \"\"\"\u524d\u5e8f\u904d\u5386\"\"\"\n self.res = []\n self.dfs(0, order=\"pre\")\n return self.res\n\n def in_order(self) -> list[int]:\n \"\"\"\u4e2d\u5e8f\u904d\u5386\"\"\"\n self.res = []\n self.dfs(0, order=\"in\")\n return self.res\n\n def post_order(self) -> list[int]:\n \"\"\"\u540e\u5e8f\u904d\u5386\"\"\"\n self.res = []\n self.dfs(0, order=\"post\")\n return self.res\n
array_binary_tree.cpp/* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\nclass ArrayBinaryTree {\n public:\n /* \u6784\u9020\u65b9\u6cd5 */\n ArrayBinaryTree(vector<int> arr) {\n tree = arr;\n }\n\n /* \u8282\u70b9\u6570\u91cf */\n int size() {\n return tree.size();\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\n int val(int i) {\n // \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de INT_MAX \uff0c\u4ee3\u8868\u7a7a\u4f4d\n if (i < 0 || i >= size())\n return INT_MAX;\n return tree[i];\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n int left(int i) {\n return 2 * i + 1;\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n int right(int i) {\n return 2 * i + 2;\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\n int parent(int i) {\n return (i - 1) / 2;\n }\n\n /* \u5c42\u5e8f\u904d\u5386 */\n vector<int> levelOrder() {\n vector<int> res;\n // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\n for (int i = 0; i < size(); i++) {\n if (val(i) != INT_MAX)\n res.push_back(val(i));\n }\n return res;\n }\n\n /* \u524d\u5e8f\u904d\u5386 */\n vector<int> preOrder() {\n vector<int> res;\n dfs(0, \"pre\", res);\n return res;\n }\n\n /* \u4e2d\u5e8f\u904d\u5386 */\n vector<int> inOrder() {\n vector<int> res;\n dfs(0, \"in\", res);\n return res;\n }\n\n /* \u540e\u5e8f\u904d\u5386 */\n vector<int> postOrder() {\n vector<int> res;\n dfs(0, \"post\", res);\n return res;\n }\n\n private:\n vector<int> tree;\n\n /* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n void dfs(int i, string order, vector<int> &res) {\n // \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\n if (val(i) == INT_MAX)\n return;\n // \u524d\u5e8f\u904d\u5386\n if (order == \"pre\")\n res.push_back(val(i));\n dfs(left(i), order, res);\n // \u4e2d\u5e8f\u904d\u5386\n if (order == \"in\")\n res.push_back(val(i));\n dfs(right(i), order, res);\n // \u540e\u5e8f\u904d\u5386\n if (order == \"post\")\n res.push_back(val(i));\n }\n};\n
array_binary_tree.java/* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\nclass ArrayBinaryTree {\n private List<Integer> tree;\n\n /* \u6784\u9020\u65b9\u6cd5 */\n public ArrayBinaryTree(List<Integer> arr) {\n tree = new ArrayList<>(arr);\n }\n\n /* \u8282\u70b9\u6570\u91cf */\n public int size() {\n return tree.size();\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\n public Integer val(int i) {\n // \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de null \uff0c\u4ee3\u8868\u7a7a\u4f4d\n if (i < 0 || i >= size())\n return null;\n return tree.get(i);\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n public Integer left(int i) {\n return 2 * i + 1;\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n public Integer right(int i) {\n return 2 * i + 2;\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\n public Integer parent(int i) {\n return (i - 1) / 2;\n }\n\n /* \u5c42\u5e8f\u904d\u5386 */\n public List<Integer> levelOrder() {\n List<Integer> res = new ArrayList<>();\n // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\n for (int i = 0; i < size(); i++) {\n if (val(i) != null)\n res.add(val(i));\n }\n return res;\n }\n\n /* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n private void dfs(Integer i, String order, List<Integer> res) {\n // \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\n if (val(i) == null)\n return;\n // \u524d\u5e8f\u904d\u5386\n if (\"pre\".equals(order))\n res.add(val(i));\n dfs(left(i), order, res);\n // \u4e2d\u5e8f\u904d\u5386\n if (\"in\".equals(order))\n res.add(val(i));\n dfs(right(i), order, res);\n // \u540e\u5e8f\u904d\u5386\n if (\"post\".equals(order))\n res.add(val(i));\n }\n\n /* \u524d\u5e8f\u904d\u5386 */\n public List<Integer> preOrder() {\n List<Integer> res = new ArrayList<>();\n dfs(0, \"pre\", res);\n return res;\n }\n\n /* \u4e2d\u5e8f\u904d\u5386 */\n public List<Integer> inOrder() {\n List<Integer> res = new ArrayList<>();\n dfs(0, \"in\", res);\n return res;\n }\n\n /* \u540e\u5e8f\u904d\u5386 */\n public List<Integer> postOrder() {\n List<Integer> res = new ArrayList<>();\n dfs(0, \"post\", res);\n return res;\n }\n}\n
array_binary_tree.cs/* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\nclass ArrayBinaryTree(List<int?> arr) {\n List<int?> tree = new(arr);\n\n /* \u8282\u70b9\u6570\u91cf */\n public int Size() {\n return tree.Count;\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\n public int? Val(int i) {\n // \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de null \uff0c\u4ee3\u8868\u7a7a\u4f4d\n if (i < 0 || i >= Size())\n return null;\n return tree[i];\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n public int Left(int i) {\n return 2 * i + 1;\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n public int Right(int i) {\n return 2 * i + 2;\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\n public int Parent(int i) {\n return (i - 1) / 2;\n }\n\n /* \u5c42\u5e8f\u904d\u5386 */\n public List<int> LevelOrder() {\n List<int> res = [];\n // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\n for (int i = 0; i < Size(); i++) {\n if (Val(i).HasValue)\n res.Add(Val(i)!.Value);\n }\n return res;\n }\n\n /* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n void DFS(int i, string order, List<int> res) {\n // \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\n if (!Val(i).HasValue)\n return;\n // \u524d\u5e8f\u904d\u5386\n if (order == \"pre\")\n res.Add(Val(i)!.Value);\n DFS(Left(i), order, res);\n // \u4e2d\u5e8f\u904d\u5386\n if (order == \"in\")\n res.Add(Val(i)!.Value);\n DFS(Right(i), order, res);\n // \u540e\u5e8f\u904d\u5386\n if (order == \"post\")\n res.Add(Val(i)!.Value);\n }\n\n /* \u524d\u5e8f\u904d\u5386 */\n public List<int> PreOrder() {\n List<int> res = [];\n DFS(0, \"pre\", res);\n return res;\n }\n\n /* \u4e2d\u5e8f\u904d\u5386 */\n public List<int> InOrder() {\n List<int> res = [];\n DFS(0, \"in\", res);\n return res;\n }\n\n /* \u540e\u5e8f\u904d\u5386 */\n public List<int> PostOrder() {\n List<int> res = [];\n DFS(0, \"post\", res);\n return res;\n }\n}\n
array_binary_tree.go/* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\ntype arrayBinaryTree struct {\n tree []any\n}\n\n/* \u6784\u9020\u65b9\u6cd5 */\nfunc newArrayBinaryTree(arr []any) *arrayBinaryTree {\n return &arrayBinaryTree{\n tree: arr,\n }\n}\n\n/* \u8282\u70b9\u6570\u91cf */\nfunc (abt *arrayBinaryTree) size() int {\n return len(abt.tree)\n}\n\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\nfunc (abt *arrayBinaryTree) val(i int) any {\n // \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de null \uff0c\u4ee3\u8868\u7a7a\u4f4d\n if i < 0 || i >= abt.size() {\n return nil\n }\n return abt.tree[i]\n}\n\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nfunc (abt *arrayBinaryTree) left(i int) int {\n return 2*i + 1\n}\n\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nfunc (abt *arrayBinaryTree) right(i int) int {\n return 2*i + 2\n}\n\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\nfunc (abt *arrayBinaryTree) parent(i int) int {\n return (i - 1) / 2\n}\n\n/* \u5c42\u5e8f\u904d\u5386 */\nfunc (abt *arrayBinaryTree) levelOrder() []any {\n var res []any\n // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\n for i := 0; i < abt.size(); i++ {\n if abt.val(i) != nil {\n res = append(res, abt.val(i))\n }\n }\n return res\n}\n\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\nfunc (abt *arrayBinaryTree) dfs(i int, order string, res *[]any) {\n // \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\n if abt.val(i) == nil {\n return\n }\n // \u524d\u5e8f\u904d\u5386\n if order == \"pre\" {\n *res = append(*res, abt.val(i))\n }\n abt.dfs(abt.left(i), order, res)\n // \u4e2d\u5e8f\u904d\u5386\n if order == \"in\" {\n *res = append(*res, abt.val(i))\n }\n abt.dfs(abt.right(i), order, res)\n // \u540e\u5e8f\u904d\u5386\n if order == \"post\" {\n *res = append(*res, abt.val(i))\n }\n}\n\n/* \u524d\u5e8f\u904d\u5386 */\nfunc (abt *arrayBinaryTree) preOrder() []any {\n var res []any\n abt.dfs(0, \"pre\", &res)\n return res\n}\n\n/* \u4e2d\u5e8f\u904d\u5386 */\nfunc (abt *arrayBinaryTree) inOrder() []any {\n var res []any\n abt.dfs(0, \"in\", &res)\n return res\n}\n\n/* \u540e\u5e8f\u904d\u5386 */\nfunc (abt *arrayBinaryTree) postOrder() []any {\n var res []any\n abt.dfs(0, \"post\", &res)\n return res\n}\n
array_binary_tree.swift/* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\nclass ArrayBinaryTree {\n private var tree: [Int?]\n\n /* \u6784\u9020\u65b9\u6cd5 */\n init(arr: [Int?]) {\n tree = arr\n }\n\n /* \u8282\u70b9\u6570\u91cf */\n func size() -> Int {\n tree.count\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\n func val(i: Int) -> Int? {\n // \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de null \uff0c\u4ee3\u8868\u7a7a\u4f4d\n if i < 0 || i >= size() {\n return nil\n }\n return tree[i]\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n func left(i: Int) -> Int {\n 2 * i + 1\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n func right(i: Int) -> Int {\n 2 * i + 2\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\n func parent(i: Int) -> Int {\n (i - 1) / 2\n }\n\n /* \u5c42\u5e8f\u904d\u5386 */\n func levelOrder() -> [Int] {\n var res: [Int] = []\n // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\n for i in stride(from: 0, to: size(), by: 1) {\n if let val = val(i: i) {\n res.append(val)\n }\n }\n return res\n }\n\n /* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n private func dfs(i: Int, order: String, res: inout [Int]) {\n // \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\n guard let val = val(i: i) else {\n return\n }\n // \u524d\u5e8f\u904d\u5386\n if order == \"pre\" {\n res.append(val)\n }\n dfs(i: left(i: i), order: order, res: &res)\n // \u4e2d\u5e8f\u904d\u5386\n if order == \"in\" {\n res.append(val)\n }\n dfs(i: right(i: i), order: order, res: &res)\n // \u540e\u5e8f\u904d\u5386\n if order == \"post\" {\n res.append(val)\n }\n }\n\n /* \u524d\u5e8f\u904d\u5386 */\n func preOrder() -> [Int] {\n var res: [Int] = []\n dfs(i: 0, order: \"pre\", res: &res)\n return res\n }\n\n /* \u4e2d\u5e8f\u904d\u5386 */\n func inOrder() -> [Int] {\n var res: [Int] = []\n dfs(i: 0, order: \"in\", res: &res)\n return res\n }\n\n /* \u540e\u5e8f\u904d\u5386 */\n func postOrder() -> [Int] {\n var res: [Int] = []\n dfs(i: 0, order: \"post\", res: &res)\n return res\n }\n}\n
array_binary_tree.js/* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\nclass ArrayBinaryTree {\n #tree;\n\n /* \u6784\u9020\u65b9\u6cd5 */\n constructor(arr) {\n this.#tree = arr;\n }\n\n /* \u8282\u70b9\u6570\u91cf */\n size() {\n return this.#tree.length;\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\n val(i) {\n // \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de null \uff0c\u4ee3\u8868\u7a7a\u4f4d\n if (i < 0 || i >= this.size()) return null;\n return this.#tree[i];\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n left(i) {\n return 2 * i + 1;\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n right(i) {\n return 2 * i + 2;\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\n parent(i) {\n return Math.floor((i - 1) / 2); // \u5411\u4e0b\u6574\u9664\n }\n\n /* \u5c42\u5e8f\u904d\u5386 */\n levelOrder() {\n let res = [];\n // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\n for (let i = 0; i < this.size(); i++) {\n if (this.val(i) !== null) res.push(this.val(i));\n }\n return res;\n }\n\n /* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n #dfs(i, order, res) {\n // \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\n if (this.val(i) === null) return;\n // \u524d\u5e8f\u904d\u5386\n if (order === 'pre') res.push(this.val(i));\n this.#dfs(this.left(i), order, res);\n // \u4e2d\u5e8f\u904d\u5386\n if (order === 'in') res.push(this.val(i));\n this.#dfs(this.right(i), order, res);\n // \u540e\u5e8f\u904d\u5386\n if (order === 'post') res.push(this.val(i));\n }\n\n /* \u524d\u5e8f\u904d\u5386 */\n preOrder() {\n const res = [];\n this.#dfs(0, 'pre', res);\n return res;\n }\n\n /* \u4e2d\u5e8f\u904d\u5386 */\n inOrder() {\n const res = [];\n this.#dfs(0, 'in', res);\n return res;\n }\n\n /* \u540e\u5e8f\u904d\u5386 */\n postOrder() {\n const res = [];\n this.#dfs(0, 'post', res);\n return res;\n }\n}\n
array_binary_tree.ts/* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\nclass ArrayBinaryTree {\n #tree: (number | null)[];\n\n /* \u6784\u9020\u65b9\u6cd5 */\n constructor(arr: (number | null)[]) {\n this.#tree = arr;\n }\n\n /* \u8282\u70b9\u6570\u91cf */\n size(): number {\n return this.#tree.length;\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\n val(i: number): number | null {\n // \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de null \uff0c\u4ee3\u8868\u7a7a\u4f4d\n if (i < 0 || i >= this.size()) return null;\n return this.#tree[i];\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n left(i: number): number {\n return 2 * i + 1;\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n right(i: number): number {\n return 2 * i + 2;\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\n parent(i: number): number {\n return Math.floor((i - 1) / 2); // \u5411\u4e0b\u6574\u9664\n }\n\n /* \u5c42\u5e8f\u904d\u5386 */\n levelOrder(): number[] {\n let res = [];\n // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\n for (let i = 0; i < this.size(); i++) {\n if (this.val(i) !== null) res.push(this.val(i));\n }\n return res;\n }\n\n /* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n #dfs(i: number, order: Order, res: (number | null)[]): void {\n // \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\n if (this.val(i) === null) return;\n // \u524d\u5e8f\u904d\u5386\n if (order === 'pre') res.push(this.val(i));\n this.#dfs(this.left(i), order, res);\n // \u4e2d\u5e8f\u904d\u5386\n if (order === 'in') res.push(this.val(i));\n this.#dfs(this.right(i), order, res);\n // \u540e\u5e8f\u904d\u5386\n if (order === 'post') res.push(this.val(i));\n }\n\n /* \u524d\u5e8f\u904d\u5386 */\n preOrder(): (number | null)[] {\n const res = [];\n this.#dfs(0, 'pre', res);\n return res;\n }\n\n /* \u4e2d\u5e8f\u904d\u5386 */\n inOrder(): (number | null)[] {\n const res = [];\n this.#dfs(0, 'in', res);\n return res;\n }\n\n /* \u540e\u5e8f\u904d\u5386 */\n postOrder(): (number | null)[] {\n const res = [];\n this.#dfs(0, 'post', res);\n return res;\n }\n}\n
array_binary_tree.dart/* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\nclass ArrayBinaryTree {\n late List<int?> _tree;\n\n /* \u6784\u9020\u65b9\u6cd5 */\n ArrayBinaryTree(this._tree);\n\n /* \u8282\u70b9\u6570\u91cf */\n int size() {\n return _tree.length;\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\n int? val(int i) {\n // \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de null \uff0c\u4ee3\u8868\u7a7a\u4f4d\n if (i < 0 || i >= size()) {\n return null;\n }\n return _tree[i];\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n int? left(int i) {\n return 2 * i + 1;\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n int? right(int i) {\n return 2 * i + 2;\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\n int? parent(int i) {\n return (i - 1) ~/ 2;\n }\n\n /* \u5c42\u5e8f\u904d\u5386 */\n List<int> levelOrder() {\n List<int> res = [];\n for (int i = 0; i < size(); i++) {\n if (val(i) != null) {\n res.add(val(i)!);\n }\n }\n return res;\n }\n\n /* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n void dfs(int i, String order, List<int?> res) {\n // \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\n if (val(i) == null) {\n return;\n }\n // \u524d\u5e8f\u904d\u5386\n if (order == 'pre') {\n res.add(val(i));\n }\n dfs(left(i)!, order, res);\n // \u4e2d\u5e8f\u904d\u5386\n if (order == 'in') {\n res.add(val(i));\n }\n dfs(right(i)!, order, res);\n // \u540e\u5e8f\u904d\u5386\n if (order == 'post') {\n res.add(val(i));\n }\n }\n\n /* \u524d\u5e8f\u904d\u5386 */\n List<int?> preOrder() {\n List<int?> res = [];\n dfs(0, 'pre', res);\n return res;\n }\n\n /* \u4e2d\u5e8f\u904d\u5386 */\n List<int?> inOrder() {\n List<int?> res = [];\n dfs(0, 'in', res);\n return res;\n }\n\n /* \u540e\u5e8f\u904d\u5386 */\n List<int?> postOrder() {\n List<int?> res = [];\n dfs(0, 'post', res);\n return res;\n }\n}\n
array_binary_tree.rs/* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\nstruct ArrayBinaryTree {\n tree: Vec<Option<i32>>,\n}\n\nimpl ArrayBinaryTree {\n /* \u6784\u9020\u65b9\u6cd5 */\n fn new(arr: Vec<Option<i32>>) -> Self {\n Self { tree: arr }\n }\n\n /* \u8282\u70b9\u6570\u91cf */\n fn size(&self) -> i32 {\n self.tree.len() as i32\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\n fn val(&self, i: i32) -> Option<i32> {\n // \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de None \uff0c\u4ee3\u8868\u7a7a\u4f4d\n if i < 0 || i >= self.size() {\n None\n } else {\n self.tree[i as usize]\n }\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n fn left(&self, i: i32) -> i32 {\n 2 * i + 1\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n fn right(&self, i: i32) -> i32 {\n 2 * i + 2\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\n fn parent(&self, i: i32) -> i32 {\n (i - 1) / 2\n }\n\n /* \u5c42\u5e8f\u904d\u5386 */\n fn level_order(&self) -> Vec<i32> {\n let mut res = vec![];\n // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\n for i in 0..self.size() {\n if let Some(val) = self.val(i) {\n res.push(val)\n }\n }\n res\n }\n\n /* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n fn dfs(&self, i: i32, order: &str, res: &mut Vec<i32>) {\n if self.val(i).is_none() {\n return;\n }\n let val = self.val(i).unwrap();\n // \u524d\u5e8f\u904d\u5386\n if order == \"pre\" {\n res.push(val);\n }\n self.dfs(self.left(i), order, res);\n // \u4e2d\u5e8f\u904d\u5386\n if order == \"in\" {\n res.push(val);\n }\n self.dfs(self.right(i), order, res);\n // \u540e\u5e8f\u904d\u5386\n if order == \"post\" {\n res.push(val);\n }\n }\n\n /* \u524d\u5e8f\u904d\u5386 */\n fn pre_order(&self) -> Vec<i32> {\n let mut res = vec![];\n self.dfs(0, \"pre\", &mut res);\n res\n }\n\n /* \u4e2d\u5e8f\u904d\u5386 */\n fn in_order(&self) -> Vec<i32> {\n let mut res = vec![];\n self.dfs(0, \"in\", &mut res);\n res\n }\n\n /* \u540e\u5e8f\u904d\u5386 */\n fn post_order(&self) -> Vec<i32> {\n let mut res = vec![];\n self.dfs(0, \"post\", &mut res);\n res\n }\n}\n
array_binary_tree.c/* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7ed3\u6784\u4f53 */\ntypedef struct {\n int *tree;\n int size;\n} ArrayBinaryTree;\n\n/* \u6784\u9020\u51fd\u6570 */\nArrayBinaryTree *newArrayBinaryTree(int *arr, int arrSize) {\n ArrayBinaryTree *abt = (ArrayBinaryTree *)malloc(sizeof(ArrayBinaryTree));\n abt->tree = malloc(sizeof(int) * arrSize);\n memcpy(abt->tree, arr, sizeof(int) * arrSize);\n abt->size = arrSize;\n return abt;\n}\n\n/* \u6790\u6784\u51fd\u6570 */\nvoid delArrayBinaryTree(ArrayBinaryTree *abt) {\n free(abt->tree);\n free(abt);\n}\n\n/* \u8282\u70b9\u6570\u91cf */\nint size(ArrayBinaryTree *abt) {\n return abt->size;\n}\n\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\nint val(ArrayBinaryTree *abt, int i) {\n // \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de INT_MAX \uff0c\u4ee3\u8868\u7a7a\u4f4d\n if (i < 0 || i >= size(abt))\n return INT_MAX;\n return abt->tree[i];\n}\n\n/* \u5c42\u5e8f\u904d\u5386 */\nint *levelOrder(ArrayBinaryTree *abt, int *returnSize) {\n int *res = (int *)malloc(sizeof(int) * size(abt));\n int index = 0;\n // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\n for (int i = 0; i < size(abt); i++) {\n if (val(abt, i) != INT_MAX)\n res[index++] = val(abt, i);\n }\n *returnSize = index;\n return res;\n}\n\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\nvoid dfs(ArrayBinaryTree *abt, int i, char *order, int *res, int *index) {\n // \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\n if (val(abt, i) == INT_MAX)\n return;\n // \u524d\u5e8f\u904d\u5386\n if (strcmp(order, \"pre\") == 0)\n res[(*index)++] = val(abt, i);\n dfs(abt, left(i), order, res, index);\n // \u4e2d\u5e8f\u904d\u5386\n if (strcmp(order, \"in\") == 0)\n res[(*index)++] = val(abt, i);\n dfs(abt, right(i), order, res, index);\n // \u540e\u5e8f\u904d\u5386\n if (strcmp(order, \"post\") == 0)\n res[(*index)++] = val(abt, i);\n}\n\n/* \u524d\u5e8f\u904d\u5386 */\nint *preOrder(ArrayBinaryTree *abt, int *returnSize) {\n int *res = (int *)malloc(sizeof(int) * size(abt));\n int index = 0;\n dfs(abt, 0, \"pre\", res, &index);\n *returnSize = index;\n return res;\n}\n\n/* \u4e2d\u5e8f\u904d\u5386 */\nint *inOrder(ArrayBinaryTree *abt, int *returnSize) {\n int *res = (int *)malloc(sizeof(int) * size(abt));\n int index = 0;\n dfs(abt, 0, \"in\", res, &index);\n *returnSize = index;\n return res;\n}\n\n/* \u540e\u5e8f\u904d\u5386 */\nint *postOrder(ArrayBinaryTree *abt, int *returnSize) {\n int *res = (int *)malloc(sizeof(int) * size(abt));\n int index = 0;\n dfs(abt, 0, \"post\", res, &index);\n *returnSize = index;\n return res;\n}\n
array_binary_tree.zig[class]{ArrayBinaryTree}-[func]{}\n
"},{"location":"chapter_tree/array_representation_of_tree/#733","title":"7.3.3 \u00a0 \u4f18\u70b9\u4e0e\u5c40\u9650\u6027","text":"\u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a\u4e3b\u8981\u6709\u4ee5\u4e0b\u4f18\u70b9\u3002
\u7136\u800c\uff0c\u6570\u7ec4\u8868\u793a\u4e5f\u5b58\u5728\u4e00\u4e9b\u5c40\u9650\u6027\u3002
\u5728\u201c\u4e8c\u53c9\u641c\u7d22\u6811\u201d\u7ae0\u8282\u4e2d\uff0c\u6211\u4eec\u63d0\u5230\uff0c\u5728\u591a\u6b21\u63d2\u5165\u548c\u5220\u9664\u64cd\u4f5c\u540e\uff0c\u4e8c\u53c9\u641c\u7d22\u6811\u53ef\u80fd\u9000\u5316\u4e3a\u94fe\u8868\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6240\u6709\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u5c06\u4ece \\(O(\\log n)\\) \u6076\u5316\u4e3a \\(O(n)\\) \u3002
\u5982\u56fe 7-24 \u6240\u793a\uff0c\u7ecf\u8fc7\u4e24\u6b21\u5220\u9664\u8282\u70b9\u64cd\u4f5c\uff0c\u8fd9\u68f5\u4e8c\u53c9\u641c\u7d22\u6811\u4fbf\u4f1a\u9000\u5316\u4e3a\u94fe\u8868\u3002
\u56fe 7-24 \u00a0 AVL \u6811\u5728\u5220\u9664\u8282\u70b9\u540e\u53d1\u751f\u9000\u5316
\u518d\u4f8b\u5982\uff0c\u5728\u56fe 7-25 \u6240\u793a\u7684\u5b8c\u7f8e\u4e8c\u53c9\u6811\u4e2d\u63d2\u5165\u4e24\u4e2a\u8282\u70b9\u540e\uff0c\u6811\u5c06\u4e25\u91cd\u5411\u5de6\u503e\u659c\uff0c\u67e5\u627e\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e5f\u968f\u4e4b\u6076\u5316\u3002
\u56fe 7-25 \u00a0 AVL \u6811\u5728\u63d2\u5165\u8282\u70b9\u540e\u53d1\u751f\u9000\u5316
1962 \u5e74 G. M. Adelson-Velsky \u548c E. M. Landis \u5728\u8bba\u6587 \"An algorithm for the organization of information\" \u4e2d\u63d0\u51fa\u4e86\u300cAVL \u6811\u300d\u3002\u8bba\u6587\u4e2d\u8be6\u7ec6\u63cf\u8ff0\u4e86\u4e00\u7cfb\u5217\u64cd\u4f5c\uff0c\u786e\u4fdd\u5728\u6301\u7eed\u6dfb\u52a0\u548c\u5220\u9664\u8282\u70b9\u540e\uff0cAVL \u6811\u4e0d\u4f1a\u9000\u5316\uff0c\u4ece\u800c\u4f7f\u5f97\u5404\u79cd\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4fdd\u6301\u5728 \\(O(\\log n)\\) \u7ea7\u522b\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u5728\u9700\u8981\u9891\u7e41\u8fdb\u884c\u589e\u5220\u67e5\u6539\u64cd\u4f5c\u7684\u573a\u666f\u4e2d\uff0cAVL \u6811\u80fd\u59cb\u7ec8\u4fdd\u6301\u9ad8\u6548\u7684\u6570\u636e\u64cd\u4f5c\u6027\u80fd\uff0c\u5177\u6709\u5f88\u597d\u7684\u5e94\u7528\u4ef7\u503c\u3002
"},{"location":"chapter_tree/avl_tree/#751-avl","title":"7.5.1 \u00a0 AVL \u6811\u5e38\u89c1\u672f\u8bed","text":"AVL \u6811\u65e2\u662f\u4e8c\u53c9\u641c\u7d22\u6811\u4e5f\u662f\u5e73\u8861\u4e8c\u53c9\u6811\uff0c\u540c\u65f6\u6ee1\u8db3\u8fd9\u4e24\u7c7b\u4e8c\u53c9\u6811\u7684\u6240\u6709\u6027\u8d28\uff0c\u56e0\u6b64\u4e5f\u88ab\u79f0\u4e3a\u300c\u5e73\u8861\u4e8c\u53c9\u641c\u7d22\u6811 balanced binary search tree\u300d\u3002
"},{"location":"chapter_tree/avl_tree/#1","title":"1. \u00a0 \u8282\u70b9\u9ad8\u5ea6","text":"\u7531\u4e8e AVL \u6811\u7684\u76f8\u5173\u64cd\u4f5c\u9700\u8981\u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6\uff0c\u56e0\u6b64\u6211\u4eec\u9700\u8981\u4e3a\u8282\u70b9\u7c7b\u6dfb\u52a0 height
\u53d8\u91cf\uff1a
class TreeNode:\n \"\"\"AVL \u6811\u8282\u70b9\u7c7b\"\"\"\n def __init__(self, val: int):\n self.val: int = val # \u8282\u70b9\u503c\n self.height: int = 0 # \u8282\u70b9\u9ad8\u5ea6\n self.left: TreeNode | None = None # \u5de6\u5b50\u8282\u70b9\u5f15\u7528\n self.right: TreeNode | None = None # \u53f3\u5b50\u8282\u70b9\u5f15\u7528\n
/* AVL \u6811\u8282\u70b9\u7c7b */\nstruct TreeNode {\n int val{}; // \u8282\u70b9\u503c\n int height = 0; // \u8282\u70b9\u9ad8\u5ea6\n TreeNode *left{}; // \u5de6\u5b50\u8282\u70b9\n TreeNode *right{}; // \u53f3\u5b50\u8282\u70b9\n TreeNode() = default;\n explicit TreeNode(int x) : val(x){}\n};\n
/* AVL \u6811\u8282\u70b9\u7c7b */\nclass TreeNode {\n public int val; // \u8282\u70b9\u503c\n public int height; // \u8282\u70b9\u9ad8\u5ea6\n public TreeNode left; // \u5de6\u5b50\u8282\u70b9\n public TreeNode right; // \u53f3\u5b50\u8282\u70b9\n public TreeNode(int x) { val = x; }\n}\n
/* AVL \u6811\u8282\u70b9\u7c7b */\nclass TreeNode(int? x) {\n public int? val = x; // \u8282\u70b9\u503c\n public int height; // \u8282\u70b9\u9ad8\u5ea6\n public TreeNode? left; // \u5de6\u5b50\u8282\u70b9\u5f15\u7528\n public TreeNode? right; // \u53f3\u5b50\u8282\u70b9\u5f15\u7528\n}\n
/* AVL \u6811\u8282\u70b9\u7ed3\u6784\u4f53 */\ntype TreeNode struct {\n Val int // \u8282\u70b9\u503c\n Height int // \u8282\u70b9\u9ad8\u5ea6\n Left *TreeNode // \u5de6\u5b50\u8282\u70b9\u5f15\u7528\n Right *TreeNode // \u53f3\u5b50\u8282\u70b9\u5f15\u7528\n}\n
/* AVL \u6811\u8282\u70b9\u7c7b */\nclass TreeNode {\n var val: Int // \u8282\u70b9\u503c\n var height: Int // \u8282\u70b9\u9ad8\u5ea6\n var left: TreeNode? // \u5de6\u5b50\u8282\u70b9\n var right: TreeNode? // \u53f3\u5b50\u8282\u70b9\n\n init(x: Int) {\n val = x\n height = 0\n }\n}\n
/* AVL \u6811\u8282\u70b9\u7c7b */\nclass TreeNode {\n val; // \u8282\u70b9\u503c\n height; //\u8282\u70b9\u9ad8\u5ea6\n left; // \u5de6\u5b50\u8282\u70b9\u6307\u9488\n right; // \u53f3\u5b50\u8282\u70b9\u6307\u9488\n constructor(val, left, right, height) {\n this.val = val === undefined ? 0 : val;\n this.height = height === undefined ? 0 : height;\n this.left = left === undefined ? null : left;\n this.right = right === undefined ? null : right;\n }\n}\n
/* AVL \u6811\u8282\u70b9\u7c7b */\nclass TreeNode {\n val: number; // \u8282\u70b9\u503c\n height: number; // \u8282\u70b9\u9ad8\u5ea6\n left: TreeNode | null; // \u5de6\u5b50\u8282\u70b9\u6307\u9488\n right: TreeNode | null; // \u53f3\u5b50\u8282\u70b9\u6307\u9488\n constructor(val?: number, height?: number, left?: TreeNode | null, right?: TreeNode | null) {\n this.val = val === undefined ? 0 : val;\n this.height = height === undefined ? 0 : height; \n this.left = left === undefined ? null : left; \n this.right = right === undefined ? null : right; \n }\n}\n
/* AVL \u6811\u8282\u70b9\u7c7b */\nclass TreeNode {\n int val; // \u8282\u70b9\u503c\n int height; // \u8282\u70b9\u9ad8\u5ea6\n TreeNode? left; // \u5de6\u5b50\u8282\u70b9\n TreeNode? right; // \u53f3\u5b50\u8282\u70b9\n TreeNode(this.val, [this.height = 0, this.left, this.right]);\n}\n
use std::rc::Rc;\nuse std::cell::RefCell;\n\n/* AVL \u6811\u8282\u70b9\u7ed3\u6784\u4f53 */\nstruct TreeNode {\n val: i32, // \u8282\u70b9\u503c\n height: i32, // \u8282\u70b9\u9ad8\u5ea6\n left: Option<Rc<RefCell<TreeNode>>>, // \u5de6\u5b50\u8282\u70b9\n right: Option<Rc<RefCell<TreeNode>>>, // \u53f3\u5b50\u8282\u70b9\n}\n\nimpl TreeNode {\n /* \u6784\u9020\u65b9\u6cd5 */\n fn new(val: i32) -> Rc<RefCell<Self>> {\n Rc::new(RefCell::new(Self {\n val,\n height: 0,\n left: None,\n right: None\n }))\n }\n}\n
/* AVL \u6811\u8282\u70b9\u7ed3\u6784\u4f53 */\nTreeNode struct TreeNode {\n int val;\n int height;\n struct TreeNode *left;\n struct TreeNode *right;\n} TreeNode;\n\n/* \u6784\u9020\u51fd\u6570 */\nTreeNode *newTreeNode(int val) {\n TreeNode *node;\n\n node = (TreeNode *)malloc(sizeof(TreeNode));\n node->val = val;\n node->height = 0;\n node->left = NULL;\n node->right = NULL;\n return node;\n}\n
\n
\u201c\u8282\u70b9\u9ad8\u5ea6\u201d\u662f\u6307\u4ece\u8be5\u8282\u70b9\u5230\u5176\u6700\u8fdc\u53f6\u8282\u70b9\u7684\u8ddd\u79bb\uff0c\u5373\u6240\u7ecf\u8fc7\u7684\u201c\u8fb9\u201d\u7684\u6570\u91cf\u3002\u9700\u8981\u7279\u522b\u6ce8\u610f\u7684\u662f\uff0c\u53f6\u8282\u70b9\u7684\u9ad8\u5ea6\u4e3a \\(0\\) \uff0c\u800c\u7a7a\u8282\u70b9\u7684\u9ad8\u5ea6\u4e3a \\(-1\\) \u3002\u6211\u4eec\u5c06\u521b\u5efa\u4e24\u4e2a\u5de5\u5177\u51fd\u6570\uff0c\u5206\u522b\u7528\u4e8e\u83b7\u53d6\u548c\u66f4\u65b0\u8282\u70b9\u7684\u9ad8\u5ea6\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig avl_tree.pydef height(self, node: TreeNode | None) -> int:\n \"\"\"\u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6\"\"\"\n # \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\n if node is not None:\n return node.height\n return -1\n\ndef update_height(self, node: TreeNode | None):\n \"\"\"\u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\"\"\"\n # \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\n node.height = max([self.height(node.left), self.height(node.right)]) + 1\n
avl_tree.cpp/* \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6 */\nint height(TreeNode *node) {\n // \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\n return node == nullptr ? -1 : node->height;\n}\n\n/* \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6 */\nvoid updateHeight(TreeNode *node) {\n // \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\n node->height = max(height(node->left), height(node->right)) + 1;\n}\n
avl_tree.java/* \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6 */\nint height(TreeNode node) {\n // \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\n return node == null ? -1 : node.height;\n}\n\n/* \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6 */\nvoid updateHeight(TreeNode node) {\n // \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\n node.height = Math.max(height(node.left), height(node.right)) + 1;\n}\n
avl_tree.cs/* \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6 */\nint Height(TreeNode? node) {\n // \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\n return node == null ? -1 : node.height;\n}\n\n/* \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6 */\nvoid UpdateHeight(TreeNode node) {\n // \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\n node.height = Math.Max(Height(node.left), Height(node.right)) + 1;\n}\n
avl_tree.go/* \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6 */\nfunc (t *aVLTree) height(node *TreeNode) int {\n // \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\n if node != nil {\n return node.Height\n }\n return -1\n}\n\n/* \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6 */\nfunc (t *aVLTree) updateHeight(node *TreeNode) {\n lh := t.height(node.Left)\n rh := t.height(node.Right)\n // \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\n if lh > rh {\n node.Height = lh + 1\n } else {\n node.Height = rh + 1\n }\n}\n
avl_tree.swift/* \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6 */\nfunc height(node: TreeNode?) -> Int {\n // \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\n node == nil ? -1 : node!.height\n}\n\n/* \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6 */\nfunc updateHeight(node: TreeNode?) {\n // \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\n node?.height = max(height(node: node?.left), height(node: node?.right)) + 1\n}\n
avl_tree.js/* \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6 */\nheight(node) {\n // \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\n return node === null ? -1 : node.height;\n}\n\n/* \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6 */\n#updateHeight(node) {\n // \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\n node.height =\n Math.max(this.height(node.left), this.height(node.right)) + 1;\n}\n
avl_tree.ts/* \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6 */\nheight(node: TreeNode): number {\n // \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\n return node === null ? -1 : node.height;\n}\n\n/* \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6 */\nupdateHeight(node: TreeNode): void {\n // \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\n node.height =\n Math.max(this.height(node.left), this.height(node.right)) + 1;\n}\n
avl_tree.dart/* \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6 */\nint height(TreeNode? node) {\n // \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\n return node == null ? -1 : node.height;\n}\n\n/* \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6 */\nvoid updateHeight(TreeNode? node) {\n // \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\n node!.height = max(height(node.left), height(node.right)) + 1;\n}\n
avl_tree.rs/* \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6 */\nfn height(node: OptionTreeNodeRc) -> i32 {\n // \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\n match node {\n Some(node) => node.borrow().height,\n None => -1,\n }\n}\n\n/* \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6 */\nfn update_height(node: OptionTreeNodeRc) {\n if let Some(node) = node {\n let left = node.borrow().left.clone();\n let right = node.borrow().right.clone();\n // \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\n node.borrow_mut().height = std::cmp::max(Self::height(left), Self::height(right)) + 1;\n }\n}\n
avl_tree.c/* \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6 */\nint height(TreeNode *node) {\n // \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\n if (node != NULL) {\n return node->height;\n }\n return -1;\n}\n\n/* \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6 */\nvoid updateHeight(TreeNode *node) {\n int lh = height(node->left);\n int rh = height(node->right);\n // \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\n if (lh > rh) {\n node->height = lh + 1;\n } else {\n node->height = rh + 1;\n }\n}\n
avl_tree.zig// \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6\nfn height(self: *Self, node: ?*inc.TreeNode(T)) i32 {\n _ = self;\n // \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\n return if (node == null) -1 else node.?.height;\n}\n\n// \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nfn updateHeight(self: *Self, node: ?*inc.TreeNode(T)) void {\n // \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\n node.?.height = @max(self.height(node.?.left), self.height(node.?.right)) + 1;\n}\n
"},{"location":"chapter_tree/avl_tree/#2","title":"2. \u00a0 \u8282\u70b9\u5e73\u8861\u56e0\u5b50","text":"\u8282\u70b9\u7684\u300c\u5e73\u8861\u56e0\u5b50 balance factor\u300d\u5b9a\u4e49\u4e3a\u8282\u70b9\u5de6\u5b50\u6811\u7684\u9ad8\u5ea6\u51cf\u53bb\u53f3\u5b50\u6811\u7684\u9ad8\u5ea6\uff0c\u540c\u65f6\u89c4\u5b9a\u7a7a\u8282\u70b9\u7684\u5e73\u8861\u56e0\u5b50\u4e3a \\(0\\) \u3002\u6211\u4eec\u540c\u6837\u5c06\u83b7\u53d6\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u7684\u529f\u80fd\u5c01\u88c5\u6210\u51fd\u6570\uff0c\u65b9\u4fbf\u540e\u7eed\u4f7f\u7528\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig avl_tree.pydef balance_factor(self, node: TreeNode | None) -> int:\n \"\"\"\u83b7\u53d6\u5e73\u8861\u56e0\u5b50\"\"\"\n # \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\n if node is None:\n return 0\n # \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\n return self.height(node.left) - self.height(node.right)\n
avl_tree.cpp/* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nint balanceFactor(TreeNode *node) {\n // \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\n if (node == nullptr)\n return 0;\n // \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\n return height(node->left) - height(node->right);\n}\n
avl_tree.java/* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nint balanceFactor(TreeNode node) {\n // \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\n if (node == null)\n return 0;\n // \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\n return height(node.left) - height(node.right);\n}\n
avl_tree.cs/* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nint BalanceFactor(TreeNode? node) {\n // \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\n if (node == null) return 0;\n // \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\n return Height(node.left) - Height(node.right);\n}\n
avl_tree.go/* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nfunc (t *aVLTree) balanceFactor(node *TreeNode) int {\n // \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\n if node == nil {\n return 0\n }\n // \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\n return t.height(node.Left) - t.height(node.Right)\n}\n
avl_tree.swift/* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nfunc balanceFactor(node: TreeNode?) -> Int {\n // \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\n guard let node = node else { return 0 }\n // \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\n return height(node: node.left) - height(node: node.right)\n}\n
avl_tree.js/* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nbalanceFactor(node) {\n // \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\n if (node === null) return 0;\n // \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\n return this.height(node.left) - this.height(node.right);\n}\n
avl_tree.ts/* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nbalanceFactor(node: TreeNode): number {\n // \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\n if (node === null) return 0;\n // \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\n return this.height(node.left) - this.height(node.right);\n}\n
avl_tree.dart/* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nint balanceFactor(TreeNode? node) {\n // \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\n if (node == null) return 0;\n // \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\n return height(node.left) - height(node.right);\n}\n
avl_tree.rs/* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nfn balance_factor(node: OptionTreeNodeRc) -> i32 {\n match node {\n // \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\n None => 0,\n // \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\n Some(node) => {\n Self::height(node.borrow().left.clone()) - Self::height(node.borrow().right.clone())\n }\n }\n}\n
avl_tree.c/* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nint balanceFactor(TreeNode *node) {\n // \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\n if (node == NULL) {\n return 0;\n }\n // \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\n return height(node->left) - height(node->right);\n}\n
avl_tree.zig// \u83b7\u53d6\u5e73\u8861\u56e0\u5b50\nfn balanceFactor(self: *Self, node: ?*inc.TreeNode(T)) i32 {\n // \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\n if (node == null) return 0;\n // \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\n return self.height(node.?.left) - self.height(node.?.right);\n}\n
Note
\u8bbe\u5e73\u8861\u56e0\u5b50\u4e3a \\(f\\) \uff0c\u5219\u4e00\u68f5 AVL \u6811\u7684\u4efb\u610f\u8282\u70b9\u7684\u5e73\u8861\u56e0\u5b50\u7686\u6ee1\u8db3 \\(-1 \\le f \\le 1\\) \u3002
"},{"location":"chapter_tree/avl_tree/#752-avl","title":"7.5.2 \u00a0 AVL \u6811\u65cb\u8f6c","text":"AVL \u6811\u7684\u7279\u70b9\u5728\u4e8e\u201c\u65cb\u8f6c\u201d\u64cd\u4f5c\uff0c\u5b83\u80fd\u591f\u5728\u4e0d\u5f71\u54cd\u4e8c\u53c9\u6811\u7684\u4e2d\u5e8f\u904d\u5386\u5e8f\u5217\u7684\u524d\u63d0\u4e0b\uff0c\u4f7f\u5931\u8861\u8282\u70b9\u91cd\u65b0\u6062\u590d\u5e73\u8861\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u65cb\u8f6c\u64cd\u4f5c\u65e2\u80fd\u4fdd\u6301\u201c\u4e8c\u53c9\u641c\u7d22\u6811\u201d\u7684\u6027\u8d28\uff0c\u4e5f\u80fd\u4f7f\u6811\u91cd\u65b0\u53d8\u4e3a\u201c\u5e73\u8861\u4e8c\u53c9\u6811\u201d\u3002
\u6211\u4eec\u5c06\u5e73\u8861\u56e0\u5b50\u7edd\u5bf9\u503c \\(> 1\\) \u7684\u8282\u70b9\u79f0\u4e3a\u201c\u5931\u8861\u8282\u70b9\u201d\u3002\u6839\u636e\u8282\u70b9\u5931\u8861\u60c5\u51b5\u7684\u4e0d\u540c\uff0c\u65cb\u8f6c\u64cd\u4f5c\u5206\u4e3a\u56db\u79cd\uff1a\u53f3\u65cb\u3001\u5de6\u65cb\u3001\u5148\u53f3\u65cb\u540e\u5de6\u65cb\u3001\u5148\u5de6\u65cb\u540e\u53f3\u65cb\u3002\u4e0b\u9762\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65cb\u8f6c\u64cd\u4f5c\u3002
"},{"location":"chapter_tree/avl_tree/#1_1","title":"1. \u00a0 \u53f3\u65cb","text":"\u5982\u56fe 7-26 \u6240\u793a\uff0c\u8282\u70b9\u4e0b\u65b9\u4e3a\u5e73\u8861\u56e0\u5b50\u3002\u4ece\u5e95\u81f3\u9876\u770b\uff0c\u4e8c\u53c9\u6811\u4e2d\u9996\u4e2a\u5931\u8861\u8282\u70b9\u662f\u201c\u8282\u70b9 3\u201d\u3002\u6211\u4eec\u5173\u6ce8\u4ee5\u8be5\u5931\u8861\u8282\u70b9\u4e3a\u6839\u8282\u70b9\u7684\u5b50\u6811\uff0c\u5c06\u8be5\u8282\u70b9\u8bb0\u4e3a node
\uff0c\u5176\u5de6\u5b50\u8282\u70b9\u8bb0\u4e3a child
\uff0c\u6267\u884c\u201c\u53f3\u65cb\u201d\u64cd\u4f5c\u3002\u5b8c\u6210\u53f3\u65cb\u540e\uff0c\u5b50\u6811\u6062\u590d\u5e73\u8861\uff0c\u5e76\u4e14\u4ecd\u7136\u4fdd\u6301\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u6027\u8d28\u3002
\u56fe 7-26 \u00a0 \u53f3\u65cb\u64cd\u4f5c\u6b65\u9aa4
\u5982\u56fe 7-27 \u6240\u793a\uff0c\u5f53\u8282\u70b9 child
\u6709\u53f3\u5b50\u8282\u70b9\uff08\u8bb0\u4e3a grandChild
\uff09\u65f6\uff0c\u9700\u8981\u5728\u53f3\u65cb\u4e2d\u6dfb\u52a0\u4e00\u6b65\uff1a\u5c06 grandChild
\u4f5c\u4e3a node
\u7684\u5de6\u5b50\u8282\u70b9\u3002
\u56fe 7-27 \u00a0 \u6709 grandChild \u7684\u53f3\u65cb\u64cd\u4f5c
\u201c\u5411\u53f3\u65cb\u8f6c\u201d\u662f\u4e00\u79cd\u5f62\u8c61\u5316\u7684\u8bf4\u6cd5\uff0c\u5b9e\u9645\u4e0a\u9700\u8981\u901a\u8fc7\u4fee\u6539\u8282\u70b9\u6307\u9488\u6765\u5b9e\u73b0\uff0c\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig avl_tree.pydef right_rotate(self, node: TreeNode | None) -> TreeNode | None:\n \"\"\"\u53f3\u65cb\u64cd\u4f5c\"\"\"\n child = node.left\n grand_child = child.right\n # \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\n child.right = node\n node.left = grand_child\n # \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n self.update_height(node)\n self.update_height(child)\n # \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child\n
avl_tree.cpp/* \u53f3\u65cb\u64cd\u4f5c */\nTreeNode *rightRotate(TreeNode *node) {\n TreeNode *child = node->left;\n TreeNode *grandChild = child->right;\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\n child->right = node;\n node->left = grandChild;\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n updateHeight(node);\n updateHeight(child);\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child;\n}\n
avl_tree.java/* \u53f3\u65cb\u64cd\u4f5c */\nTreeNode rightRotate(TreeNode node) {\n TreeNode child = node.left;\n TreeNode grandChild = child.right;\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\n child.right = node;\n node.left = grandChild;\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n updateHeight(node);\n updateHeight(child);\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child;\n}\n
avl_tree.cs/* \u53f3\u65cb\u64cd\u4f5c */\nTreeNode? RightRotate(TreeNode? node) {\n TreeNode? child = node?.left;\n TreeNode? grandChild = child?.right;\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\n child.right = node;\n node.left = grandChild;\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n UpdateHeight(node);\n UpdateHeight(child);\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child;\n}\n
avl_tree.go/* \u53f3\u65cb\u64cd\u4f5c */\nfunc (t *aVLTree) rightRotate(node *TreeNode) *TreeNode {\n child := node.Left\n grandChild := child.Right\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\n child.Right = node\n node.Left = grandChild\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n t.updateHeight(node)\n t.updateHeight(child)\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child\n}\n
avl_tree.swift/* \u53f3\u65cb\u64cd\u4f5c */\nfunc rightRotate(node: TreeNode?) -> TreeNode? {\n let child = node?.left\n let grandChild = child?.right\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\n child?.right = node\n node?.left = grandChild\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n updateHeight(node: node)\n updateHeight(node: child)\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child\n}\n
avl_tree.js/* \u53f3\u65cb\u64cd\u4f5c */\n#rightRotate(node) {\n const child = node.left;\n const grandChild = child.right;\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\n child.right = node;\n node.left = grandChild;\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n this.#updateHeight(node);\n this.#updateHeight(child);\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child;\n}\n
avl_tree.ts/* \u53f3\u65cb\u64cd\u4f5c */\nrightRotate(node: TreeNode): TreeNode {\n const child = node.left;\n const grandChild = child.right;\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\n child.right = node;\n node.left = grandChild;\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n this.updateHeight(node);\n this.updateHeight(child);\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child;\n}\n
avl_tree.dart/* \u53f3\u65cb\u64cd\u4f5c */\nTreeNode? rightRotate(TreeNode? node) {\n TreeNode? child = node!.left;\n TreeNode? grandChild = child!.right;\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\n child.right = node;\n node.left = grandChild;\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n updateHeight(node);\n updateHeight(child);\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child;\n}\n
avl_tree.rs/* \u53f3\u65cb\u64cd\u4f5c */\nfn right_rotate(node: OptionTreeNodeRc) -> OptionTreeNodeRc {\n match node {\n Some(node) => {\n let child = node.borrow().left.clone().unwrap();\n let grand_child = child.borrow().right.clone();\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\n child.borrow_mut().right = Some(node.clone());\n node.borrow_mut().left = grand_child;\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n Self::update_height(Some(node));\n Self::update_height(Some(child.clone()));\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n Some(child)\n }\n None => None,\n }\n}\n
avl_tree.c/* \u53f3\u65cb\u64cd\u4f5c */\nTreeNode *rightRotate(TreeNode *node) {\n TreeNode *child, *grandChild;\n child = node->left;\n grandChild = child->right;\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\n child->right = node;\n node->left = grandChild;\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n updateHeight(node);\n updateHeight(child);\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child;\n}\n
avl_tree.zig// \u53f3\u65cb\u64cd\u4f5c\nfn rightRotate(self: *Self, node: ?*inc.TreeNode(T)) ?*inc.TreeNode(T) {\n var child = node.?.left;\n var grandChild = child.?.right;\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\n child.?.right = node;\n node.?.left = grandChild;\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n self.updateHeight(node);\n self.updateHeight(child);\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child;\n}\n
"},{"location":"chapter_tree/avl_tree/#2_1","title":"2. \u00a0 \u5de6\u65cb","text":"\u76f8\u5e94\u5730\uff0c\u5982\u679c\u8003\u8651\u4e0a\u8ff0\u5931\u8861\u4e8c\u53c9\u6811\u7684\u201c\u955c\u50cf\u201d\uff0c\u5219\u9700\u8981\u6267\u884c\u56fe 7-28 \u6240\u793a\u7684\u201c\u5de6\u65cb\u201d\u64cd\u4f5c\u3002
\u56fe 7-28 \u00a0 \u5de6\u65cb\u64cd\u4f5c
\u540c\u7406\uff0c\u5982\u56fe 7-29 \u6240\u793a\uff0c\u5f53\u8282\u70b9 child
\u6709\u5de6\u5b50\u8282\u70b9\uff08\u8bb0\u4e3a grandChild
\uff09\u65f6\uff0c\u9700\u8981\u5728\u5de6\u65cb\u4e2d\u6dfb\u52a0\u4e00\u6b65\uff1a\u5c06 grandChild
\u4f5c\u4e3a node
\u7684\u53f3\u5b50\u8282\u70b9\u3002
\u56fe 7-29 \u00a0 \u6709 grandChild \u7684\u5de6\u65cb\u64cd\u4f5c
\u53ef\u4ee5\u89c2\u5bdf\u5230\uff0c\u53f3\u65cb\u548c\u5de6\u65cb\u64cd\u4f5c\u5728\u903b\u8f91\u4e0a\u662f\u955c\u50cf\u5bf9\u79f0\u7684\uff0c\u5b83\u4eec\u5206\u522b\u89e3\u51b3\u7684\u4e24\u79cd\u5931\u8861\u60c5\u51b5\u4e5f\u662f\u5bf9\u79f0\u7684\u3002\u57fa\u4e8e\u5bf9\u79f0\u6027\uff0c\u6211\u4eec\u53ea\u9700\u5c06\u53f3\u65cb\u7684\u5b9e\u73b0\u4ee3\u7801\u4e2d\u7684\u6240\u6709\u7684 left
\u66ff\u6362\u4e3a right
\uff0c\u5c06\u6240\u6709\u7684 right
\u66ff\u6362\u4e3a left
\uff0c\u5373\u53ef\u5f97\u5230\u5de6\u65cb\u7684\u5b9e\u73b0\u4ee3\u7801\uff1a
def left_rotate(self, node: TreeNode | None) -> TreeNode | None:\n \"\"\"\u5de6\u65cb\u64cd\u4f5c\"\"\"\n child = node.right\n grand_child = child.left\n # \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\n child.left = node\n node.right = grand_child\n # \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n self.update_height(node)\n self.update_height(child)\n # \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child\n
avl_tree.cpp/* \u5de6\u65cb\u64cd\u4f5c */\nTreeNode *leftRotate(TreeNode *node) {\n TreeNode *child = node->right;\n TreeNode *grandChild = child->left;\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\n child->left = node;\n node->right = grandChild;\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n updateHeight(node);\n updateHeight(child);\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child;\n}\n
avl_tree.java/* \u5de6\u65cb\u64cd\u4f5c */\nTreeNode leftRotate(TreeNode node) {\n TreeNode child = node.right;\n TreeNode grandChild = child.left;\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\n child.left = node;\n node.right = grandChild;\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n updateHeight(node);\n updateHeight(child);\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child;\n}\n
avl_tree.cs/* \u5de6\u65cb\u64cd\u4f5c */\nTreeNode? LeftRotate(TreeNode? node) {\n TreeNode? child = node?.right;\n TreeNode? grandChild = child?.left;\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\n child.left = node;\n node.right = grandChild;\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n UpdateHeight(node);\n UpdateHeight(child);\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child;\n}\n
avl_tree.go/* \u5de6\u65cb\u64cd\u4f5c */\nfunc (t *aVLTree) leftRotate(node *TreeNode) *TreeNode {\n child := node.Right\n grandChild := child.Left\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\n child.Left = node\n node.Right = grandChild\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n t.updateHeight(node)\n t.updateHeight(child)\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child\n}\n
avl_tree.swift/* \u5de6\u65cb\u64cd\u4f5c */\nfunc leftRotate(node: TreeNode?) -> TreeNode? {\n let child = node?.right\n let grandChild = child?.left\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\n child?.left = node\n node?.right = grandChild\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n updateHeight(node: node)\n updateHeight(node: child)\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child\n}\n
avl_tree.js/* \u5de6\u65cb\u64cd\u4f5c */\n#leftRotate(node) {\n const child = node.right;\n const grandChild = child.left;\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\n child.left = node;\n node.right = grandChild;\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n this.#updateHeight(node);\n this.#updateHeight(child);\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child;\n}\n
avl_tree.ts/* \u5de6\u65cb\u64cd\u4f5c */\nleftRotate(node: TreeNode): TreeNode {\n const child = node.right;\n const grandChild = child.left;\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\n child.left = node;\n node.right = grandChild;\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n this.updateHeight(node);\n this.updateHeight(child);\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child;\n}\n
avl_tree.dart/* \u5de6\u65cb\u64cd\u4f5c */\nTreeNode? leftRotate(TreeNode? node) {\n TreeNode? child = node!.right;\n TreeNode? grandChild = child!.left;\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\n child.left = node;\n node.right = grandChild;\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n updateHeight(node);\n updateHeight(child);\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child;\n}\n
avl_tree.rs/* \u5de6\u65cb\u64cd\u4f5c */\nfn left_rotate(node: OptionTreeNodeRc) -> OptionTreeNodeRc {\n match node {\n Some(node) => {\n let child = node.borrow().right.clone().unwrap();\n let grand_child = child.borrow().left.clone();\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\n child.borrow_mut().left = Some(node.clone());\n node.borrow_mut().right = grand_child;\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n Self::update_height(Some(node));\n Self::update_height(Some(child.clone()));\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n Some(child)\n }\n None => None,\n }\n}\n
avl_tree.c/* \u5de6\u65cb\u64cd\u4f5c */\nTreeNode *leftRotate(TreeNode *node) {\n TreeNode *child, *grandChild;\n child = node->right;\n grandChild = child->left;\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\n child->left = node;\n node->right = grandChild;\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n updateHeight(node);\n updateHeight(child);\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child;\n}\n
avl_tree.zig// \u5de6\u65cb\u64cd\u4f5c\nfn leftRotate(self: *Self, node: ?*inc.TreeNode(T)) ?*inc.TreeNode(T) {\n var child = node.?.right;\n var grandChild = child.?.left;\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\n child.?.left = node;\n node.?.right = grandChild;\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n self.updateHeight(node);\n self.updateHeight(child);\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child;\n}\n
"},{"location":"chapter_tree/avl_tree/#3","title":"3. \u00a0 \u5148\u5de6\u65cb\u540e\u53f3\u65cb","text":"\u5bf9\u4e8e\u56fe 7-30 \u4e2d\u7684\u5931\u8861\u8282\u70b9 3 \uff0c\u4ec5\u4f7f\u7528\u5de6\u65cb\u6216\u53f3\u65cb\u90fd\u65e0\u6cd5\u4f7f\u5b50\u6811\u6062\u590d\u5e73\u8861\u3002\u6b64\u65f6\u9700\u8981\u5148\u5bf9 child
\u6267\u884c\u201c\u5de6\u65cb\u201d\uff0c\u518d\u5bf9 node
\u6267\u884c\u201c\u53f3\u65cb\u201d\u3002
\u56fe 7-30 \u00a0 \u5148\u5de6\u65cb\u540e\u53f3\u65cb
"},{"location":"chapter_tree/avl_tree/#4","title":"4. \u00a0 \u5148\u53f3\u65cb\u540e\u5de6\u65cb","text":"\u5982\u56fe 7-31 \u6240\u793a\uff0c\u5bf9\u4e8e\u4e0a\u8ff0\u5931\u8861\u4e8c\u53c9\u6811\u7684\u955c\u50cf\u60c5\u51b5\uff0c\u9700\u8981\u5148\u5bf9 child
\u6267\u884c\u201c\u53f3\u65cb\u201d\uff0c\u518d\u5bf9 node
\u6267\u884c\u201c\u5de6\u65cb\u201d\u3002
\u56fe 7-31 \u00a0 \u5148\u53f3\u65cb\u540e\u5de6\u65cb
"},{"location":"chapter_tree/avl_tree/#5","title":"5. \u00a0 \u65cb\u8f6c\u7684\u9009\u62e9","text":"\u56fe 7-32 \u5c55\u793a\u7684\u56db\u79cd\u5931\u8861\u60c5\u51b5\u4e0e\u4e0a\u8ff0\u6848\u4f8b\u9010\u4e2a\u5bf9\u5e94\uff0c\u5206\u522b\u9700\u8981\u91c7\u7528\u53f3\u65cb\u3001\u5148\u5de6\u65cb\u540e\u53f3\u65cb\u3001\u5148\u53f3\u65cb\u540e\u5de6\u65cb\u3001\u5de6\u65cb\u7684\u64cd\u4f5c\u3002
\u56fe 7-32 \u00a0 AVL \u6811\u7684\u56db\u79cd\u65cb\u8f6c\u60c5\u51b5
\u5982\u4e0b\u8868\u6240\u793a\uff0c\u6211\u4eec\u901a\u8fc7\u5224\u65ad\u5931\u8861\u8282\u70b9\u7684\u5e73\u8861\u56e0\u5b50\u4ee5\u53ca\u8f83\u9ad8\u4e00\u4fa7\u5b50\u8282\u70b9\u7684\u5e73\u8861\u56e0\u5b50\u7684\u6b63\u8d1f\u53f7\uff0c\u6765\u786e\u5b9a\u5931\u8861\u8282\u70b9\u5c5e\u4e8e\u56fe 7-32 \u4e2d\u7684\u54ea\u79cd\u60c5\u51b5\u3002
\u8868 7-3 \u00a0 \u56db\u79cd\u65cb\u8f6c\u60c5\u51b5\u7684\u9009\u62e9\u6761\u4ef6
\u5931\u8861\u8282\u70b9\u7684\u5e73\u8861\u56e0\u5b50 \u5b50\u8282\u70b9\u7684\u5e73\u8861\u56e0\u5b50 \u5e94\u91c7\u7528\u7684\u65cb\u8f6c\u65b9\u6cd5 \\(> 1\\) \uff08\u5de6\u504f\u6811\uff09 \\(\\geq 0\\) \u53f3\u65cb \\(> 1\\) \uff08\u5de6\u504f\u6811\uff09 \\(<0\\) \u5148\u5de6\u65cb\u540e\u53f3\u65cb \\(< -1\\) \uff08\u53f3\u504f\u6811\uff09 \\(\\leq 0\\) \u5de6\u65cb \\(< -1\\) \uff08\u53f3\u504f\u6811\uff09 \\(>0\\) \u5148\u53f3\u65cb\u540e\u5de6\u65cb\u4e3a\u4e86\u4fbf\u4e8e\u4f7f\u7528\uff0c\u6211\u4eec\u5c06\u65cb\u8f6c\u64cd\u4f5c\u5c01\u88c5\u6210\u4e00\u4e2a\u51fd\u6570\u3002\u6709\u4e86\u8fd9\u4e2a\u51fd\u6570\uff0c\u6211\u4eec\u5c31\u80fd\u5bf9\u5404\u79cd\u5931\u8861\u60c5\u51b5\u8fdb\u884c\u65cb\u8f6c\uff0c\u4f7f\u5931\u8861\u8282\u70b9\u91cd\u65b0\u6062\u590d\u5e73\u8861\u3002\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig avl_tree.pydef rotate(self, node: TreeNode | None) -> TreeNode | None:\n \"\"\"\u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861\"\"\"\n # \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\n balance_factor = self.balance_factor(node)\n # \u5de6\u504f\u6811\n if balance_factor > 1:\n if self.balance_factor(node.left) >= 0:\n # \u53f3\u65cb\n return self.right_rotate(node)\n else:\n # \u5148\u5de6\u65cb\u540e\u53f3\u65cb\n node.left = self.left_rotate(node.left)\n return self.right_rotate(node)\n # \u53f3\u504f\u6811\n elif balance_factor < -1:\n if self.balance_factor(node.right) <= 0:\n # \u5de6\u65cb\n return self.left_rotate(node)\n else:\n # \u5148\u53f3\u65cb\u540e\u5de6\u65cb\n node.right = self.right_rotate(node.right)\n return self.left_rotate(node)\n # \u5e73\u8861\u6811\uff0c\u65e0\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\n return node\n
avl_tree.cpp/* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nTreeNode *rotate(TreeNode *node) {\n // \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\n int _balanceFactor = balanceFactor(node);\n // \u5de6\u504f\u6811\n if (_balanceFactor > 1) {\n if (balanceFactor(node->left) >= 0) {\n // \u53f3\u65cb\n return rightRotate(node);\n } else {\n // \u5148\u5de6\u65cb\u540e\u53f3\u65cb\n node->left = leftRotate(node->left);\n return rightRotate(node);\n }\n }\n // \u53f3\u504f\u6811\n if (_balanceFactor < -1) {\n if (balanceFactor(node->right) <= 0) {\n // \u5de6\u65cb\n return leftRotate(node);\n } else {\n // \u5148\u53f3\u65cb\u540e\u5de6\u65cb\n node->right = rightRotate(node->right);\n return leftRotate(node);\n }\n }\n // \u5e73\u8861\u6811\uff0c\u65e0\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\n return node;\n}\n
avl_tree.java/* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nTreeNode rotate(TreeNode node) {\n // \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\n int balanceFactor = balanceFactor(node);\n // \u5de6\u504f\u6811\n if (balanceFactor > 1) {\n if (balanceFactor(node.left) >= 0) {\n // \u53f3\u65cb\n return rightRotate(node);\n } else {\n // \u5148\u5de6\u65cb\u540e\u53f3\u65cb\n node.left = leftRotate(node.left);\n return rightRotate(node);\n }\n }\n // \u53f3\u504f\u6811\n if (balanceFactor < -1) {\n if (balanceFactor(node.right) <= 0) {\n // \u5de6\u65cb\n return leftRotate(node);\n } else {\n // \u5148\u53f3\u65cb\u540e\u5de6\u65cb\n node.right = rightRotate(node.right);\n return leftRotate(node);\n }\n }\n // \u5e73\u8861\u6811\uff0c\u65e0\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\n return node;\n}\n
avl_tree.cs/* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nTreeNode? Rotate(TreeNode? node) {\n // \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\n int balanceFactorInt = BalanceFactor(node);\n // \u5de6\u504f\u6811\n if (balanceFactorInt > 1) {\n if (BalanceFactor(node?.left) >= 0) {\n // \u53f3\u65cb\n return RightRotate(node);\n } else {\n // \u5148\u5de6\u65cb\u540e\u53f3\u65cb\n node!.left = LeftRotate(node!.left);\n return RightRotate(node);\n }\n }\n // \u53f3\u504f\u6811\n if (balanceFactorInt < -1) {\n if (BalanceFactor(node?.right) <= 0) {\n // \u5de6\u65cb\n return LeftRotate(node);\n } else {\n // \u5148\u53f3\u65cb\u540e\u5de6\u65cb\n node!.right = RightRotate(node!.right);\n return LeftRotate(node);\n }\n }\n // \u5e73\u8861\u6811\uff0c\u65e0\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\n return node;\n}\n
avl_tree.go/* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nfunc (t *aVLTree) rotate(node *TreeNode) *TreeNode {\n // \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\n // Go \u63a8\u8350\u77ed\u53d8\u91cf\uff0c\u8fd9\u91cc bf \u6307\u4ee3 t.balanceFactor\n bf := t.balanceFactor(node)\n // \u5de6\u504f\u6811\n if bf > 1 {\n if t.balanceFactor(node.Left) >= 0 {\n // \u53f3\u65cb\n return t.rightRotate(node)\n } else {\n // \u5148\u5de6\u65cb\u540e\u53f3\u65cb\n node.Left = t.leftRotate(node.Left)\n return t.rightRotate(node)\n }\n }\n // \u53f3\u504f\u6811\n if bf < -1 {\n if t.balanceFactor(node.Right) <= 0 {\n // \u5de6\u65cb\n return t.leftRotate(node)\n } else {\n // \u5148\u53f3\u65cb\u540e\u5de6\u65cb\n node.Right = t.rightRotate(node.Right)\n return t.leftRotate(node)\n }\n }\n // \u5e73\u8861\u6811\uff0c\u65e0\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\n return node\n}\n
avl_tree.swift/* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nfunc rotate(node: TreeNode?) -> TreeNode? {\n // \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\n let balanceFactor = balanceFactor(node: node)\n // \u5de6\u504f\u6811\n if balanceFactor > 1 {\n if self.balanceFactor(node: node?.left) >= 0 {\n // \u53f3\u65cb\n return rightRotate(node: node)\n } else {\n // \u5148\u5de6\u65cb\u540e\u53f3\u65cb\n node?.left = leftRotate(node: node?.left)\n return rightRotate(node: node)\n }\n }\n // \u53f3\u504f\u6811\n if balanceFactor < -1 {\n if self.balanceFactor(node: node?.right) <= 0 {\n // \u5de6\u65cb\n return leftRotate(node: node)\n } else {\n // \u5148\u53f3\u65cb\u540e\u5de6\u65cb\n node?.right = rightRotate(node: node?.right)\n return leftRotate(node: node)\n }\n }\n // \u5e73\u8861\u6811\uff0c\u65e0\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\n return node\n}\n
avl_tree.js/* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n#rotate(node) {\n // \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\n const balanceFactor = this.balanceFactor(node);\n // \u5de6\u504f\u6811\n if (balanceFactor > 1) {\n if (this.balanceFactor(node.left) >= 0) {\n // \u53f3\u65cb\n return this.#rightRotate(node);\n } else {\n // \u5148\u5de6\u65cb\u540e\u53f3\u65cb\n node.left = this.#leftRotate(node.left);\n return this.#rightRotate(node);\n }\n }\n // \u53f3\u504f\u6811\n if (balanceFactor < -1) {\n if (this.balanceFactor(node.right) <= 0) {\n // \u5de6\u65cb\n return this.#leftRotate(node);\n } else {\n // \u5148\u53f3\u65cb\u540e\u5de6\u65cb\n node.right = this.#rightRotate(node.right);\n return this.#leftRotate(node);\n }\n }\n // \u5e73\u8861\u6811\uff0c\u65e0\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\n return node;\n}\n
avl_tree.ts/* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nrotate(node: TreeNode): TreeNode {\n // \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\n const balanceFactor = this.balanceFactor(node);\n // \u5de6\u504f\u6811\n if (balanceFactor > 1) {\n if (this.balanceFactor(node.left) >= 0) {\n // \u53f3\u65cb\n return this.rightRotate(node);\n } else {\n // \u5148\u5de6\u65cb\u540e\u53f3\u65cb\n node.left = this.leftRotate(node.left);\n return this.rightRotate(node);\n }\n }\n // \u53f3\u504f\u6811\n if (balanceFactor < -1) {\n if (this.balanceFactor(node.right) <= 0) {\n // \u5de6\u65cb\n return this.leftRotate(node);\n } else {\n // \u5148\u53f3\u65cb\u540e\u5de6\u65cb\n node.right = this.rightRotate(node.right);\n return this.leftRotate(node);\n }\n }\n // \u5e73\u8861\u6811\uff0c\u65e0\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\n return node;\n}\n
avl_tree.dart/* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nTreeNode? rotate(TreeNode? node) {\n // \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\n int factor = balanceFactor(node);\n // \u5de6\u504f\u6811\n if (factor > 1) {\n if (balanceFactor(node!.left) >= 0) {\n // \u53f3\u65cb\n return rightRotate(node);\n } else {\n // \u5148\u5de6\u65cb\u540e\u53f3\u65cb\n node.left = leftRotate(node.left);\n return rightRotate(node);\n }\n }\n // \u53f3\u504f\u6811\n if (factor < -1) {\n if (balanceFactor(node!.right) <= 0) {\n // \u5de6\u65cb\n return leftRotate(node);\n } else {\n // \u5148\u53f3\u65cb\u540e\u5de6\u65cb\n node.right = rightRotate(node.right);\n return leftRotate(node);\n }\n }\n // \u5e73\u8861\u6811\uff0c\u65e0\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\n return node;\n}\n
avl_tree.rs/* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nfn rotate(node: OptionTreeNodeRc) -> OptionTreeNodeRc {\n // \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\n let balance_factor = Self::balance_factor(node.clone());\n // \u5de6\u504f\u6811\n if balance_factor > 1 {\n let node = node.unwrap();\n if Self::balance_factor(node.borrow().left.clone()) >= 0 {\n // \u53f3\u65cb\n Self::right_rotate(Some(node))\n } else {\n // \u5148\u5de6\u65cb\u540e\u53f3\u65cb\n let left = node.borrow().left.clone();\n node.borrow_mut().left = Self::left_rotate(left);\n Self::right_rotate(Some(node))\n }\n }\n // \u53f3\u504f\u6811\n else if balance_factor < -1 {\n let node = node.unwrap();\n if Self::balance_factor(node.borrow().right.clone()) <= 0 {\n // \u5de6\u65cb\n Self::left_rotate(Some(node))\n } else {\n // \u5148\u53f3\u65cb\u540e\u5de6\u65cb\n let right = node.borrow().right.clone();\n node.borrow_mut().right = Self::right_rotate(right);\n Self::left_rotate(Some(node))\n }\n } else {\n // \u5e73\u8861\u6811\uff0c\u65e0\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\n node\n }\n}\n
avl_tree.c/* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nTreeNode *rotate(TreeNode *node) {\n // \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\n int bf = balanceFactor(node);\n // \u5de6\u504f\u6811\n if (bf > 1) {\n if (balanceFactor(node->left) >= 0) {\n // \u53f3\u65cb\n return rightRotate(node);\n } else {\n // \u5148\u5de6\u65cb\u540e\u53f3\u65cb\n node->left = leftRotate(node->left);\n return rightRotate(node);\n }\n }\n // \u53f3\u504f\u6811\n if (bf < -1) {\n if (balanceFactor(node->right) <= 0) {\n // \u5de6\u65cb\n return leftRotate(node);\n } else {\n // \u5148\u53f3\u65cb\u540e\u5de6\u65cb\n node->right = rightRotate(node->right);\n return leftRotate(node);\n }\n }\n // \u5e73\u8861\u6811\uff0c\u65e0\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\n return node;\n}\n
avl_tree.zig// \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861\nfn rotate(self: *Self, node: ?*inc.TreeNode(T)) ?*inc.TreeNode(T) {\n // \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\n var balance_factor = self.balanceFactor(node);\n // \u5de6\u504f\u6811\n if (balance_factor > 1) {\n if (self.balanceFactor(node.?.left) >= 0) {\n // \u53f3\u65cb\n return self.rightRotate(node);\n } else {\n // \u5148\u5de6\u65cb\u540e\u53f3\u65cb\n node.?.left = self.leftRotate(node.?.left);\n return self.rightRotate(node);\n }\n }\n // \u53f3\u504f\u6811\n if (balance_factor < -1) {\n if (self.balanceFactor(node.?.right) <= 0) {\n // \u5de6\u65cb\n return self.leftRotate(node);\n } else {\n // \u5148\u53f3\u65cb\u540e\u5de6\u65cb\n node.?.right = self.rightRotate(node.?.right);\n return self.leftRotate(node);\n }\n }\n // \u5e73\u8861\u6811\uff0c\u65e0\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\n return node;\n}\n
"},{"location":"chapter_tree/avl_tree/#753-avl","title":"7.5.3 \u00a0 AVL \u6811\u5e38\u7528\u64cd\u4f5c","text":""},{"location":"chapter_tree/avl_tree/#1_2","title":"1. \u00a0 \u63d2\u5165\u8282\u70b9","text":"AVL \u6811\u7684\u8282\u70b9\u63d2\u5165\u64cd\u4f5c\u4e0e\u4e8c\u53c9\u641c\u7d22\u6811\u5728\u4e3b\u4f53\u4e0a\u7c7b\u4f3c\u3002\u552f\u4e00\u7684\u533a\u522b\u5728\u4e8e\uff0c\u5728 AVL \u6811\u4e2d\u63d2\u5165\u8282\u70b9\u540e\uff0c\u4ece\u8be5\u8282\u70b9\u5230\u6839\u8282\u70b9\u7684\u8def\u5f84\u4e0a\u53ef\u80fd\u4f1a\u51fa\u73b0\u4e00\u7cfb\u5217\u5931\u8861\u8282\u70b9\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u9700\u8981\u4ece\u8fd9\u4e2a\u8282\u70b9\u5f00\u59cb\uff0c\u81ea\u5e95\u5411\u4e0a\u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u6240\u6709\u5931\u8861\u8282\u70b9\u6062\u590d\u5e73\u8861\u3002\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig avl_tree.pydef insert(self, val):\n \"\"\"\u63d2\u5165\u8282\u70b9\"\"\"\n self._root = self.insert_helper(self._root, val)\n\ndef insert_helper(self, node: TreeNode | None, val: int) -> TreeNode:\n \"\"\"\u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09\"\"\"\n if node is None:\n return TreeNode(val)\n # 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\uff0c\u5e76\u63d2\u5165\u8282\u70b9\n if val < node.val:\n node.left = self.insert_helper(node.left, val)\n elif val > node.val:\n node.right = self.insert_helper(node.right, val)\n else:\n # \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n return node\n # \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n self.update_height(node)\n # 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861\n return self.rotate(node)\n
avl_tree.cpp/* \u63d2\u5165\u8282\u70b9 */\nvoid insert(int val) {\n root = insertHelper(root, val);\n}\n\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nTreeNode *insertHelper(TreeNode *node, int val) {\n if (node == nullptr)\n return new TreeNode(val);\n /* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\uff0c\u5e76\u63d2\u5165\u8282\u70b9 */\n if (val < node->val)\n node->left = insertHelper(node->left, val);\n else if (val > node->val)\n node->right = insertHelper(node->right, val);\n else\n return node; // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n updateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n node = rotate(node);\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n return node;\n}\n
avl_tree.java/* \u63d2\u5165\u8282\u70b9 */\nvoid insert(int val) {\n root = insertHelper(root, val);\n}\n\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nTreeNode insertHelper(TreeNode node, int val) {\n if (node == null)\n return new TreeNode(val);\n /* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\uff0c\u5e76\u63d2\u5165\u8282\u70b9 */\n if (val < node.val)\n node.left = insertHelper(node.left, val);\n else if (val > node.val)\n node.right = insertHelper(node.right, val);\n else\n return node; // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n updateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n node = rotate(node);\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n return node;\n}\n
avl_tree.cs/* \u63d2\u5165\u8282\u70b9 */\nvoid Insert(int val) {\n root = InsertHelper(root, val);\n}\n\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nTreeNode? InsertHelper(TreeNode? node, int val) {\n if (node == null) return new TreeNode(val);\n /* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\uff0c\u5e76\u63d2\u5165\u8282\u70b9 */\n if (val < node.val)\n node.left = InsertHelper(node.left, val);\n else if (val > node.val)\n node.right = InsertHelper(node.right, val);\n else\n return node; // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n UpdateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n node = Rotate(node);\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n return node;\n}\n
avl_tree.go/* \u63d2\u5165\u8282\u70b9 */\nfunc (t *aVLTree) insert(val int) {\n t.root = t.insertHelper(t.root, val)\n}\n\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u51fd\u6570\uff09 */\nfunc (t *aVLTree) insertHelper(node *TreeNode, val int) *TreeNode {\n if node == nil {\n return NewTreeNode(val)\n }\n /* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\uff0c\u5e76\u63d2\u5165\u8282\u70b9 */\n if val < node.Val.(int) {\n node.Left = t.insertHelper(node.Left, val)\n } else if val > node.Val.(int) {\n node.Right = t.insertHelper(node.Right, val)\n } else {\n // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n return node\n }\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n t.updateHeight(node)\n /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n node = t.rotate(node)\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n return node\n}\n
avl_tree.swift/* \u63d2\u5165\u8282\u70b9 */\nfunc insert(val: Int) {\n root = insertHelper(node: root, val: val)\n}\n\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nfunc insertHelper(node: TreeNode?, val: Int) -> TreeNode? {\n var node = node\n if node == nil {\n return TreeNode(x: val)\n }\n /* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\uff0c\u5e76\u63d2\u5165\u8282\u70b9 */\n if val < node!.val {\n node?.left = insertHelper(node: node?.left, val: val)\n } else if val > node!.val {\n node?.right = insertHelper(node: node?.right, val: val)\n } else {\n return node // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n }\n updateHeight(node: node) // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n node = rotate(node: node)\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n return node\n}\n
avl_tree.js/* \u63d2\u5165\u8282\u70b9 */\ninsert(val) {\n this.root = this.#insertHelper(this.root, val);\n}\n\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\n#insertHelper(node, val) {\n if (node === null) return new TreeNode(val);\n /* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\uff0c\u5e76\u63d2\u5165\u8282\u70b9 */\n if (val < node.val) node.left = this.#insertHelper(node.left, val);\n else if (val > node.val)\n node.right = this.#insertHelper(node.right, val);\n else return node; // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n this.#updateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n node = this.#rotate(node);\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n return node;\n}\n
avl_tree.ts/* \u63d2\u5165\u8282\u70b9 */\ninsert(val: number): void {\n this.root = this.insertHelper(this.root, val);\n}\n\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\ninsertHelper(node: TreeNode, val: number): TreeNode {\n if (node === null) return new TreeNode(val);\n /* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\uff0c\u5e76\u63d2\u5165\u8282\u70b9 */\n if (val < node.val) {\n node.left = this.insertHelper(node.left, val);\n } else if (val > node.val) {\n node.right = this.insertHelper(node.right, val);\n } else {\n return node; // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n }\n this.updateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n node = this.rotate(node);\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n return node;\n}\n
avl_tree.dart/* \u63d2\u5165\u8282\u70b9 */\nvoid insert(int val) {\n root = insertHelper(root, val);\n}\n\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nTreeNode? insertHelper(TreeNode? node, int val) {\n if (node == null) return TreeNode(val);\n /* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\uff0c\u5e76\u63d2\u5165\u8282\u70b9 */\n if (val < node.val)\n node.left = insertHelper(node.left, val);\n else if (val > node.val)\n node.right = insertHelper(node.right, val);\n else\n return node; // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n updateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n node = rotate(node);\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n return node;\n}\n
avl_tree.rs/* \u63d2\u5165\u8282\u70b9 */\nfn insert(&mut self, val: i32) {\n self.root = Self::insert_helper(self.root.clone(), val);\n}\n\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nfn insert_helper(node: OptionTreeNodeRc, val: i32) -> OptionTreeNodeRc {\n match node {\n Some(mut node) => {\n /* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\uff0c\u5e76\u63d2\u5165\u8282\u70b9 */\n match {\n let node_val = node.borrow().val;\n node_val\n }\n .cmp(&val)\n {\n Ordering::Greater => {\n let left = node.borrow().left.clone();\n node.borrow_mut().left = Self::insert_helper(left, val);\n }\n Ordering::Less => {\n let right = node.borrow().right.clone();\n node.borrow_mut().right = Self::insert_helper(right, val);\n }\n Ordering::Equal => {\n return Some(node); // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n }\n }\n Self::update_height(Some(node.clone())); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n node = Self::rotate(Some(node)).unwrap();\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n Some(node)\n }\n None => Some(TreeNode::new(val)),\n }\n}\n
avl_tree.c/* \u63d2\u5165\u8282\u70b9 */\nvoid insert(AVLTree *tree, int val) {\n tree->root = insertHelper(tree->root, val);\n}\n\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u51fd\u6570\uff09 */\nTreeNode *insertHelper(TreeNode *node, int val) {\n if (node == NULL) {\n return newTreeNode(val);\n }\n /* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\uff0c\u5e76\u63d2\u5165\u8282\u70b9 */\n if (val < node->val) {\n node->left = insertHelper(node->left, val);\n } else if (val > node->val) {\n node->right = insertHelper(node->right, val);\n } else {\n // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n return node;\n }\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n updateHeight(node);\n /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n node = rotate(node);\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n return node;\n}\n
avl_tree.zig// \u63d2\u5165\u8282\u70b9\nfn insert(self: *Self, val: T) !void {\n self.root = (try self.insertHelper(self.root, val)).?;\n}\n\n// \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09\nfn insertHelper(self: *Self, node_: ?*inc.TreeNode(T), val: T) !?*inc.TreeNode(T) {\n var node = node_;\n if (node == null) {\n var tmp_node = try self.mem_allocator.create(inc.TreeNode(T));\n tmp_node.init(val);\n return tmp_node;\n }\n // 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\uff0c\u5e76\u63d2\u5165\u8282\u70b9\n if (val < node.?.val) {\n node.?.left = try self.insertHelper(node.?.left, val);\n } else if (val > node.?.val) {\n node.?.right = try self.insertHelper(node.?.right, val);\n } else {\n return node; // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n }\n self.updateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n // 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861\n node = self.rotate(node);\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n return node;\n}\n
"},{"location":"chapter_tree/avl_tree/#2_2","title":"2. \u00a0 \u5220\u9664\u8282\u70b9","text":"\u7c7b\u4f3c\u5730\uff0c\u5728\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u5220\u9664\u8282\u70b9\u65b9\u6cd5\u7684\u57fa\u7840\u4e0a\uff0c\u9700\u8981\u4ece\u5e95\u81f3\u9876\u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u6240\u6709\u5931\u8861\u8282\u70b9\u6062\u590d\u5e73\u8861\u3002\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig avl_tree.pydef remove(self, val: int):\n \"\"\"\u5220\u9664\u8282\u70b9\"\"\"\n self._root = self.remove_helper(self._root, val)\n\ndef remove_helper(self, node: TreeNode | None, val: int) -> TreeNode | None:\n \"\"\"\u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09\"\"\"\n if node is None:\n return None\n # 1. \u67e5\u627e\u8282\u70b9\uff0c\u5e76\u5220\u9664\u4e4b\n if val < node.val:\n node.left = self.remove_helper(node.left, val)\n elif val > node.val:\n node.right = self.remove_helper(node.right, val)\n else:\n if node.left is None or node.right is None:\n child = node.left or node.right\n # \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\n if child is None:\n return None\n # \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\n else:\n node = child\n else:\n # \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\n temp = node.right\n while temp.left is not None:\n temp = temp.left\n node.right = self.remove_helper(node.right, temp.val)\n node.val = temp.val\n # \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n self.update_height(node)\n # 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861\n return self.rotate(node)\n
avl_tree.cpp/* \u5220\u9664\u8282\u70b9 */\nvoid remove(int val) {\n root = removeHelper(root, val);\n}\n\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nTreeNode *removeHelper(TreeNode *node, int val) {\n if (node == nullptr)\n return nullptr;\n /* 1. \u67e5\u627e\u8282\u70b9\uff0c\u5e76\u5220\u9664\u4e4b */\n if (val < node->val)\n node->left = removeHelper(node->left, val);\n else if (val > node->val)\n node->right = removeHelper(node->right, val);\n else {\n if (node->left == nullptr || node->right == nullptr) {\n TreeNode *child = node->left != nullptr ? node->left : node->right;\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\n if (child == nullptr) {\n delete node;\n return nullptr;\n }\n // \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\n else {\n delete node;\n node = child;\n }\n } else {\n // \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\n TreeNode *temp = node->right;\n while (temp->left != nullptr) {\n temp = temp->left;\n }\n int tempVal = temp->val;\n node->right = removeHelper(node->right, temp->val);\n node->val = tempVal;\n }\n }\n updateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n node = rotate(node);\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n return node;\n}\n
avl_tree.java/* \u5220\u9664\u8282\u70b9 */\nvoid remove(int val) {\n root = removeHelper(root, val);\n}\n\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nTreeNode removeHelper(TreeNode node, int val) {\n if (node == null)\n return null;\n /* 1. \u67e5\u627e\u8282\u70b9\uff0c\u5e76\u5220\u9664\u4e4b */\n if (val < node.val)\n node.left = removeHelper(node.left, val);\n else if (val > node.val)\n node.right = removeHelper(node.right, val);\n else {\n if (node.left == null || node.right == null) {\n TreeNode child = node.left != null ? node.left : node.right;\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\n if (child == null)\n return null;\n // \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\n else\n node = child;\n } else {\n // \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\n TreeNode temp = node.right;\n while (temp.left != null) {\n temp = temp.left;\n }\n node.right = removeHelper(node.right, temp.val);\n node.val = temp.val;\n }\n }\n updateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n node = rotate(node);\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n return node;\n}\n
avl_tree.cs/* \u5220\u9664\u8282\u70b9 */\nvoid Remove(int val) {\n root = RemoveHelper(root, val);\n}\n\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nTreeNode? RemoveHelper(TreeNode? node, int val) {\n if (node == null) return null;\n /* 1. \u67e5\u627e\u8282\u70b9\uff0c\u5e76\u5220\u9664\u4e4b */\n if (val < node.val)\n node.left = RemoveHelper(node.left, val);\n else if (val > node.val)\n node.right = RemoveHelper(node.right, val);\n else {\n if (node.left == null || node.right == null) {\n TreeNode? child = node.left ?? node.right;\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\n if (child == null)\n return null;\n // \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\n else\n node = child;\n } else {\n // \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\n TreeNode? temp = node.right;\n while (temp.left != null) {\n temp = temp.left;\n }\n node.right = RemoveHelper(node.right, temp.val!.Value);\n node.val = temp.val;\n }\n }\n UpdateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n node = Rotate(node);\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n return node;\n}\n
avl_tree.go/* \u5220\u9664\u8282\u70b9 */\nfunc (t *aVLTree) remove(val int) {\n t.root = t.removeHelper(t.root, val)\n}\n\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u51fd\u6570\uff09 */\nfunc (t *aVLTree) removeHelper(node *TreeNode, val int) *TreeNode {\n if node == nil {\n return nil\n }\n /* 1. \u67e5\u627e\u8282\u70b9\uff0c\u5e76\u5220\u9664\u4e4b */\n if val < node.Val.(int) {\n node.Left = t.removeHelper(node.Left, val)\n } else if val > node.Val.(int) {\n node.Right = t.removeHelper(node.Right, val)\n } else {\n if node.Left == nil || node.Right == nil {\n child := node.Left\n if node.Right != nil {\n child = node.Right\n }\n if child == nil {\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\n return nil\n } else {\n // \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\n node = child\n }\n } else {\n // \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\n temp := node.Right\n for temp.Left != nil {\n temp = temp.Left\n }\n node.Right = t.removeHelper(node.Right, temp.Val.(int))\n node.Val = temp.Val\n }\n }\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n t.updateHeight(node)\n /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n node = t.rotate(node)\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n return node\n}\n
avl_tree.swift/* \u5220\u9664\u8282\u70b9 */\nfunc remove(val: Int) {\n root = removeHelper(node: root, val: val)\n}\n\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nfunc removeHelper(node: TreeNode?, val: Int) -> TreeNode? {\n var node = node\n if node == nil {\n return nil\n }\n /* 1. \u67e5\u627e\u8282\u70b9\uff0c\u5e76\u5220\u9664\u4e4b */\n if val < node!.val {\n node?.left = removeHelper(node: node?.left, val: val)\n } else if val > node!.val {\n node?.right = removeHelper(node: node?.right, val: val)\n } else {\n if node?.left == nil || node?.right == nil {\n let child = node?.left != nil ? node?.left : node?.right\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\n if child == nil {\n return nil\n }\n // \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\n else {\n node = child\n }\n } else {\n // \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\n var temp = node?.right\n while temp?.left != nil {\n temp = temp?.left\n }\n node?.right = removeHelper(node: node?.right, val: temp!.val)\n node?.val = temp!.val\n }\n }\n updateHeight(node: node) // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n node = rotate(node: node)\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n return node\n}\n
avl_tree.js/* \u5220\u9664\u8282\u70b9 */\nremove(val) {\n this.root = this.#removeHelper(this.root, val);\n}\n\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\n#removeHelper(node, val) {\n if (node === null) return null;\n /* 1. \u67e5\u627e\u8282\u70b9\uff0c\u5e76\u5220\u9664\u4e4b */\n if (val < node.val) node.left = this.#removeHelper(node.left, val);\n else if (val > node.val)\n node.right = this.#removeHelper(node.right, val);\n else {\n if (node.left === null || node.right === null) {\n const child = node.left !== null ? node.left : node.right;\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\n if (child === null) return null;\n // \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\n else node = child;\n } else {\n // \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\n let temp = node.right;\n while (temp.left !== null) {\n temp = temp.left;\n }\n node.right = this.#removeHelper(node.right, temp.val);\n node.val = temp.val;\n }\n }\n this.#updateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n node = this.#rotate(node);\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n return node;\n}\n
avl_tree.ts/* \u5220\u9664\u8282\u70b9 */\nremove(val: number): void {\n this.root = this.removeHelper(this.root, val);\n}\n\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nremoveHelper(node: TreeNode, val: number): TreeNode {\n if (node === null) return null;\n /* 1. \u67e5\u627e\u8282\u70b9\uff0c\u5e76\u5220\u9664\u4e4b */\n if (val < node.val) {\n node.left = this.removeHelper(node.left, val);\n } else if (val > node.val) {\n node.right = this.removeHelper(node.right, val);\n } else {\n if (node.left === null || node.right === null) {\n const child = node.left !== null ? node.left : node.right;\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\n if (child === null) {\n return null;\n } else {\n // \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\n node = child;\n }\n } else {\n // \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\n let temp = node.right;\n while (temp.left !== null) {\n temp = temp.left;\n }\n node.right = this.removeHelper(node.right, temp.val);\n node.val = temp.val;\n }\n }\n this.updateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n node = this.rotate(node);\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n return node;\n}\n
avl_tree.dart/* \u5220\u9664\u8282\u70b9 */\nvoid remove(int val) {\n root = removeHelper(root, val);\n}\n\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nTreeNode? removeHelper(TreeNode? node, int val) {\n if (node == null) return null;\n /* 1. \u67e5\u627e\u8282\u70b9\uff0c\u5e76\u5220\u9664\u4e4b */\n if (val < node.val)\n node.left = removeHelper(node.left, val);\n else if (val > node.val)\n node.right = removeHelper(node.right, val);\n else {\n if (node.left == null || node.right == null) {\n TreeNode? child = node.left ?? node.right;\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\n if (child == null)\n return null;\n // \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\n else\n node = child;\n } else {\n // \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\n TreeNode? temp = node.right;\n while (temp!.left != null) {\n temp = temp.left;\n }\n node.right = removeHelper(node.right, temp.val);\n node.val = temp.val;\n }\n }\n updateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n node = rotate(node);\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n return node;\n}\n
avl_tree.rs/* \u5220\u9664\u8282\u70b9 */\nfn remove(&self, val: i32) {\n Self::remove_helper(self.root.clone(), val);\n}\n\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nfn remove_helper(node: OptionTreeNodeRc, val: i32) -> OptionTreeNodeRc {\n match node {\n Some(mut node) => {\n /* 1. \u67e5\u627e\u8282\u70b9\uff0c\u5e76\u5220\u9664\u4e4b */\n if val < node.borrow().val {\n let left = node.borrow().left.clone();\n node.borrow_mut().left = Self::remove_helper(left, val);\n } else if val > node.borrow().val {\n let right = node.borrow().right.clone();\n node.borrow_mut().right = Self::remove_helper(right, val);\n } else if node.borrow().left.is_none() || node.borrow().right.is_none() {\n let child = if node.borrow().left.is_some() {\n node.borrow().left.clone()\n } else {\n node.borrow().right.clone()\n };\n match child {\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\n None => {\n return None;\n }\n // \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\n Some(child) => node = child,\n }\n } else {\n // \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\n let mut temp = node.borrow().right.clone().unwrap();\n loop {\n let temp_left = temp.borrow().left.clone();\n if temp_left.is_none() {\n break;\n }\n temp = temp_left.unwrap();\n }\n let right = node.borrow().right.clone();\n node.borrow_mut().right = Self::remove_helper(right, temp.borrow().val);\n node.borrow_mut().val = temp.borrow().val;\n }\n Self::update_height(Some(node.clone())); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n node = Self::rotate(Some(node)).unwrap();\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n Some(node)\n }\n None => None,\n }\n}\n
avl_tree.c/* \u5220\u9664\u8282\u70b9 */\n// \u7531\u4e8e\u5f15\u5165\u4e86 stdio.h \uff0c\u6b64\u5904\u65e0\u6cd5\u4f7f\u7528 remove \u5173\u952e\u8bcd\nvoid removeItem(AVLTree *tree, int val) {\n TreeNode *root = removeHelper(tree->root, val);\n}\n\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u51fd\u6570\uff09 */\nTreeNode *removeHelper(TreeNode *node, int val) {\n TreeNode *child, *grandChild;\n if (node == NULL) {\n return NULL;\n }\n /* 1. \u67e5\u627e\u8282\u70b9\uff0c\u5e76\u5220\u9664\u4e4b */\n if (val < node->val) {\n node->left = removeHelper(node->left, val);\n } else if (val > node->val) {\n node->right = removeHelper(node->right, val);\n } else {\n if (node->left == NULL || node->right == NULL) {\n child = node->left;\n if (node->right != NULL) {\n child = node->right;\n }\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\n if (child == NULL) {\n return NULL;\n } else {\n // \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\n node = child;\n }\n } else {\n // \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\n TreeNode *temp = node->right;\n while (temp->left != NULL) {\n temp = temp->left;\n }\n int tempVal = temp->val;\n node->right = removeHelper(node->right, temp->val);\n node->val = tempVal;\n }\n }\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n updateHeight(node);\n /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n node = rotate(node);\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n return node;\n}\n
avl_tree.zig// \u5220\u9664\u8282\u70b9\nfn remove(self: *Self, val: T) void {\n self.root = self.removeHelper(self.root, val).?;\n}\n\n// \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09\nfn removeHelper(self: *Self, node_: ?*inc.TreeNode(T), val: T) ?*inc.TreeNode(T) {\n var node = node_;\n if (node == null) return null;\n // 1. \u67e5\u627e\u8282\u70b9\uff0c\u5e76\u5220\u9664\u4e4b\n if (val < node.?.val) {\n node.?.left = self.removeHelper(node.?.left, val);\n } else if (val > node.?.val) {\n node.?.right = self.removeHelper(node.?.right, val);\n } else {\n if (node.?.left == null or node.?.right == null) {\n var child = if (node.?.left != null) node.?.left else node.?.right;\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\n if (child == null) {\n return null;\n // \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\n } else {\n node = child;\n }\n } else {\n // \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\n var temp = node.?.right;\n while (temp.?.left != null) {\n temp = temp.?.left;\n }\n node.?.right = self.removeHelper(node.?.right, temp.?.val);\n node.?.val = temp.?.val;\n }\n }\n self.updateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n // 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861\n node = self.rotate(node);\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n return node;\n}\n
"},{"location":"chapter_tree/avl_tree/#3_1","title":"3. \u00a0 \u67e5\u627e\u8282\u70b9","text":"AVL \u6811\u7684\u8282\u70b9\u67e5\u627e\u64cd\u4f5c\u4e0e\u4e8c\u53c9\u641c\u7d22\u6811\u4e00\u81f4\uff0c\u5728\u6b64\u4e0d\u518d\u8d58\u8ff0\u3002
"},{"location":"chapter_tree/avl_tree/#754-avl","title":"7.5.4 \u00a0 AVL \u6811\u5178\u578b\u5e94\u7528","text":"\u5982\u56fe 7-16 \u6240\u793a\uff0c\u300c\u4e8c\u53c9\u641c\u7d22\u6811 binary search tree\u300d\u6ee1\u8db3\u4ee5\u4e0b\u6761\u4ef6\u3002
1.
\u3002\u56fe 7-16 \u00a0 \u4e8c\u53c9\u641c\u7d22\u6811
"},{"location":"chapter_tree/binary_search_tree/#741","title":"7.4.1 \u00a0 \u4e8c\u53c9\u641c\u7d22\u6811\u7684\u64cd\u4f5c","text":"\u6211\u4eec\u5c06\u4e8c\u53c9\u641c\u7d22\u6811\u5c01\u88c5\u4e3a\u4e00\u4e2a\u7c7b BinarySearchTree
\uff0c\u5e76\u58f0\u660e\u4e00\u4e2a\u6210\u5458\u53d8\u91cf root
\uff0c\u6307\u5411\u6811\u7684\u6839\u8282\u70b9\u3002
\u7ed9\u5b9a\u76ee\u6807\u8282\u70b9\u503c num
\uff0c\u53ef\u4ee5\u6839\u636e\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u6027\u8d28\u6765\u67e5\u627e\u3002\u5982\u56fe 7-17 \u6240\u793a\uff0c\u6211\u4eec\u58f0\u660e\u4e00\u4e2a\u8282\u70b9 cur
\uff0c\u4ece\u4e8c\u53c9\u6811\u7684\u6839\u8282\u70b9 root
\u51fa\u53d1\uff0c\u5faa\u73af\u6bd4\u8f83\u8282\u70b9\u503c cur.val
\u548c num
\u4e4b\u95f4\u7684\u5927\u5c0f\u5173\u7cfb\u3002
cur.val < num
\uff0c\u8bf4\u660e\u76ee\u6807\u8282\u70b9\u5728 cur
\u7684\u53f3\u5b50\u6811\u4e2d\uff0c\u56e0\u6b64\u6267\u884c cur = cur.right
\u3002cur.val > num
\uff0c\u8bf4\u660e\u76ee\u6807\u8282\u70b9\u5728 cur
\u7684\u5de6\u5b50\u6811\u4e2d\uff0c\u56e0\u6b64\u6267\u884c cur = cur.left
\u3002cur.val = num
\uff0c\u8bf4\u660e\u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\u5e76\u8fd4\u56de\u8be5\u8282\u70b9\u3002\u56fe 7-17 \u00a0 \u4e8c\u53c9\u641c\u7d22\u6811\u67e5\u627e\u8282\u70b9\u793a\u4f8b
\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u67e5\u627e\u64cd\u4f5c\u4e0e\u4e8c\u5206\u67e5\u627e\u7b97\u6cd5\u7684\u5de5\u4f5c\u539f\u7406\u4e00\u81f4\uff0c\u90fd\u662f\u6bcf\u8f6e\u6392\u9664\u4e00\u534a\u60c5\u51b5\u3002\u5faa\u73af\u6b21\u6570\u6700\u591a\u4e3a\u4e8c\u53c9\u6811\u7684\u9ad8\u5ea6\uff0c\u5f53\u4e8c\u53c9\u6811\u5e73\u8861\u65f6\uff0c\u4f7f\u7528 \\(O(\\log n)\\) \u65f6\u95f4\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig binary_search_tree.pydef search(self, num: int) -> TreeNode | None:\n \"\"\"\u67e5\u627e\u8282\u70b9\"\"\"\n cur = self._root\n # \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while cur is not None:\n # \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if cur.val < num:\n cur = cur.right\n # \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n elif cur.val > num:\n cur = cur.left\n # \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n else:\n break\n return cur\n
binary_search_tree.cpp/* \u67e5\u627e\u8282\u70b9 */\nTreeNode *search(int num) {\n TreeNode *cur = root;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur != nullptr) {\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur->val < num)\n cur = cur->right;\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else if (cur->val > num)\n cur = cur->left;\n // \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n else\n break;\n }\n // \u8fd4\u56de\u76ee\u6807\u8282\u70b9\n return cur;\n}\n
binary_search_tree.java/* \u67e5\u627e\u8282\u70b9 */\nTreeNode search(int num) {\n TreeNode cur = root;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur != null) {\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur.val < num)\n cur = cur.right;\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else if (cur.val > num)\n cur = cur.left;\n // \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n else\n break;\n }\n // \u8fd4\u56de\u76ee\u6807\u8282\u70b9\n return cur;\n}\n
binary_search_tree.cs/* \u67e5\u627e\u8282\u70b9 */\nTreeNode? Search(int num) {\n TreeNode? cur = root;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur != null) {\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur.val < num) cur =\n cur.right;\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else if (cur.val > num)\n cur = cur.left;\n // \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n else\n break;\n }\n // \u8fd4\u56de\u76ee\u6807\u8282\u70b9\n return cur;\n}\n
binary_search_tree.go/* \u67e5\u627e\u8282\u70b9 */\nfunc (bst *binarySearchTree) search(num int) *TreeNode {\n node := bst.root\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n for node != nil {\n if node.Val.(int) < num {\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n node = node.Right\n } else if node.Val.(int) > num {\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n node = node.Left\n } else {\n // \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n break\n }\n }\n // \u8fd4\u56de\u76ee\u6807\u8282\u70b9\n return node\n}\n
binary_search_tree.swift/* \u67e5\u627e\u8282\u70b9 */\nfunc search(num: Int) -> TreeNode? {\n var cur = root\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while cur != nil {\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if cur!.val < num {\n cur = cur?.right\n }\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else if cur!.val > num {\n cur = cur?.left\n }\n // \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n else {\n break\n }\n }\n // \u8fd4\u56de\u76ee\u6807\u8282\u70b9\n return cur\n}\n
binary_search_tree.js/* \u67e5\u627e\u8282\u70b9 */\nsearch(num) {\n let cur = this.root;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur !== null) {\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur.val < num) cur = cur.right;\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else if (cur.val > num) cur = cur.left;\n // \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n else break;\n }\n // \u8fd4\u56de\u76ee\u6807\u8282\u70b9\n return cur;\n}\n
binary_search_tree.ts/* \u67e5\u627e\u8282\u70b9 */\nsearch(num: number): TreeNode | null {\n let cur = this.root;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur !== null) {\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur.val < num) cur = cur.right;\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else if (cur.val > num) cur = cur.left;\n // \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n else break;\n }\n // \u8fd4\u56de\u76ee\u6807\u8282\u70b9\n return cur;\n}\n
binary_search_tree.dart/* \u67e5\u627e\u8282\u70b9 */\nTreeNode? search(int _num) {\n TreeNode? cur = _root;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur != null) {\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur.val < _num)\n cur = cur.right;\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else if (cur.val > _num)\n cur = cur.left;\n // \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n else\n break;\n }\n // \u8fd4\u56de\u76ee\u6807\u8282\u70b9\n return cur;\n}\n
binary_search_tree.rs/* \u67e5\u627e\u8282\u70b9 */\npub fn search(&self, num: i32) -> Option<TreeNodeRc> {\n let mut cur = self.root.clone();\n\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while let Some(node) = cur.clone() {\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if node.borrow().val < num {\n cur = node.borrow().right.clone();\n }\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else if node.borrow().val > num {\n cur = node.borrow().left.clone();\n }\n // \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n else {\n break;\n }\n }\n // \u8fd4\u56de\u76ee\u6807\u8282\u70b9\n cur\n}\n
binary_search_tree.c/* \u67e5\u627e\u8282\u70b9 */\nTreeNode *search(BinarySearchTree *bst, int num) {\n TreeNode *cur = bst->root;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur != NULL) {\n if (cur->val < num) {\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n cur = cur->right;\n } else if (cur->val > num) {\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n cur = cur->left;\n } else {\n // \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n break;\n }\n }\n // \u8fd4\u56de\u76ee\u6807\u8282\u70b9\n return cur;\n}\n
binary_search_tree.zig// \u67e5\u627e\u8282\u70b9\nfn search(self: *Self, num: T) ?*inc.TreeNode(T) {\n var cur = self.root;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur != null) {\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur.?.val < num) {\n cur = cur.?.right;\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n } else if (cur.?.val > num) {\n cur = cur.?.left;\n // \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n } else {\n break;\n }\n }\n // \u8fd4\u56de\u76ee\u6807\u8282\u70b9\n return cur;\n}\n
"},{"location":"chapter_tree/binary_search_tree/#2","title":"2. \u00a0 \u63d2\u5165\u8282\u70b9","text":"\u7ed9\u5b9a\u4e00\u4e2a\u5f85\u63d2\u5165\u5143\u7d20 num
\uff0c\u4e3a\u4e86\u4fdd\u6301\u4e8c\u53c9\u641c\u7d22\u6811\u201c\u5de6\u5b50\u6811 < \u6839\u8282\u70b9 < \u53f3\u5b50\u6811\u201d\u7684\u6027\u8d28\uff0c\u63d2\u5165\u64cd\u4f5c\u6d41\u7a0b\u5982\u56fe 7-18 \u6240\u793a\u3002
num
\u7684\u5927\u5c0f\u5173\u7cfb\u5faa\u73af\u5411\u4e0b\u641c\u7d22\uff0c\u76f4\u5230\u8d8a\u8fc7\u53f6\u8282\u70b9\uff08\u904d\u5386\u81f3 \\(\\text{None}\\) \uff09\u65f6\u8df3\u51fa\u5faa\u73af\u3002num
\uff0c\u5c06\u8be5\u8282\u70b9\u7f6e\u4e8e \\(\\text{None}\\) \u7684\u4f4d\u7f6e\u3002\u56fe 7-18 \u00a0 \u5728\u4e8c\u53c9\u641c\u7d22\u6811\u4e2d\u63d2\u5165\u8282\u70b9
\u5728\u4ee3\u7801\u5b9e\u73b0\u4e2d\uff0c\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u4e24\u70b9\u3002
pre
\u4fdd\u5b58\u4e0a\u4e00\u8f6e\u5faa\u73af\u7684\u8282\u70b9\u3002\u8fd9\u6837\u5728\u904d\u5386\u81f3 \\(\\text{None}\\) \u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u83b7\u53d6\u5230\u5176\u7236\u8282\u70b9\uff0c\u4ece\u800c\u5b8c\u6210\u8282\u70b9\u63d2\u5165\u64cd\u4f5c\u3002def insert(self, num: int):\n \"\"\"\u63d2\u5165\u8282\u70b9\"\"\"\n # \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\n if self._root is None:\n self._root = TreeNode(num)\n return\n # \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n cur, pre = self._root, None\n while cur is not None:\n # \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\n if cur.val == num:\n return\n pre = cur\n # \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if cur.val < num:\n cur = cur.right\n # \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else:\n cur = cur.left\n # \u63d2\u5165\u8282\u70b9\n node = TreeNode(num)\n if pre.val < num:\n pre.right = node\n else:\n pre.left = node\n
binary_search_tree.cpp/* \u63d2\u5165\u8282\u70b9 */\nvoid insert(int num) {\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\n if (root == nullptr) {\n root = new TreeNode(num);\n return;\n }\n TreeNode *cur = root, *pre = nullptr;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur != nullptr) {\n // \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\n if (cur->val == num)\n return;\n pre = cur;\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur->val < num)\n cur = cur->right;\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else\n cur = cur->left;\n }\n // \u63d2\u5165\u8282\u70b9\n TreeNode *node = new TreeNode(num);\n if (pre->val < num)\n pre->right = node;\n else\n pre->left = node;\n}\n
binary_search_tree.java/* \u63d2\u5165\u8282\u70b9 */\nvoid insert(int num) {\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\n if (root == null) {\n root = new TreeNode(num);\n return;\n }\n TreeNode cur = root, pre = null;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur != null) {\n // \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\n if (cur.val == num)\n return;\n pre = cur;\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur.val < num)\n cur = cur.right;\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else\n cur = cur.left;\n }\n // \u63d2\u5165\u8282\u70b9\n TreeNode node = new TreeNode(num);\n if (pre.val < num)\n pre.right = node;\n else\n pre.left = node;\n}\n
binary_search_tree.cs/* \u63d2\u5165\u8282\u70b9 */\nvoid Insert(int num) {\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\n if (root == null) {\n root = new TreeNode(num);\n return;\n }\n TreeNode? cur = root, pre = null;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur != null) {\n // \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\n if (cur.val == num)\n return;\n pre = cur;\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur.val < num)\n cur = cur.right;\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else\n cur = cur.left;\n }\n\n // \u63d2\u5165\u8282\u70b9\n TreeNode node = new(num);\n if (pre != null) {\n if (pre.val < num)\n pre.right = node;\n else\n pre.left = node;\n }\n}\n
binary_search_tree.go/* \u63d2\u5165\u8282\u70b9 */\nfunc (bst *binarySearchTree) insert(num int) {\n cur := bst.root\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\n if cur == nil {\n bst.root = NewTreeNode(num)\n return\n }\n // \u5f85\u63d2\u5165\u8282\u70b9\u4e4b\u524d\u7684\u8282\u70b9\u4f4d\u7f6e\n var pre *TreeNode = nil\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n for cur != nil {\n if cur.Val == num {\n return\n }\n pre = cur\n if cur.Val.(int) < num {\n cur = cur.Right\n } else {\n cur = cur.Left\n }\n }\n // \u63d2\u5165\u8282\u70b9\n node := NewTreeNode(num)\n if pre.Val.(int) < num {\n pre.Right = node\n } else {\n pre.Left = node\n }\n}\n
binary_search_tree.swift/* \u63d2\u5165\u8282\u70b9 */\nfunc insert(num: Int) {\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\n if root == nil {\n root = TreeNode(x: num)\n return\n }\n var cur = root\n var pre: TreeNode?\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while cur != nil {\n // \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\n if cur!.val == num {\n return\n }\n pre = cur\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if cur!.val < num {\n cur = cur?.right\n }\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else {\n cur = cur?.left\n }\n }\n // \u63d2\u5165\u8282\u70b9\n let node = TreeNode(x: num)\n if pre!.val < num {\n pre?.right = node\n } else {\n pre?.left = node\n }\n}\n
binary_search_tree.js/* \u63d2\u5165\u8282\u70b9 */\ninsert(num) {\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\n if (this.root === null) {\n this.root = new TreeNode(num);\n return;\n }\n let cur = this.root,\n pre = null;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur !== null) {\n // \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\n if (cur.val === num) return;\n pre = cur;\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur.val < num) cur = cur.right;\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else cur = cur.left;\n }\n // \u63d2\u5165\u8282\u70b9\n const node = new TreeNode(num);\n if (pre.val < num) pre.right = node;\n else pre.left = node;\n}\n
binary_search_tree.ts/* \u63d2\u5165\u8282\u70b9 */\ninsert(num: number): void {\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\n if (this.root === null) {\n this.root = new TreeNode(num);\n return;\n }\n let cur: TreeNode | null = this.root,\n pre: TreeNode | null = null;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur !== null) {\n // \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\n if (cur.val === num) return;\n pre = cur;\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur.val < num) cur = cur.right;\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else cur = cur.left;\n }\n // \u63d2\u5165\u8282\u70b9\n const node = new TreeNode(num);\n if (pre!.val < num) pre!.right = node;\n else pre!.left = node;\n}\n
binary_search_tree.dart/* \u63d2\u5165\u8282\u70b9 */\nvoid insert(int _num) {\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\n if (_root == null) {\n _root = TreeNode(_num);\n return;\n }\n TreeNode? cur = _root;\n TreeNode? pre = null;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur != null) {\n // \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\n if (cur.val == _num) return;\n pre = cur;\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur.val < _num)\n cur = cur.right;\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else\n cur = cur.left;\n }\n // \u63d2\u5165\u8282\u70b9\n TreeNode? node = TreeNode(_num);\n if (pre!.val < _num)\n pre.right = node;\n else\n pre.left = node;\n}\n
binary_search_tree.rs/* \u63d2\u5165\u8282\u70b9 */\npub fn insert(&mut self, num: i32) {\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\n if self.root.is_none() {\n self.root = Some(TreeNode::new(num));\n return;\n }\n let mut cur = self.root.clone();\n let mut pre = None;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while let Some(node) = cur.clone() {\n // \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\n if node.borrow().val == num {\n return;\n }\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n pre = cur.clone();\n if node.borrow().val < num {\n cur = node.borrow().right.clone();\n }\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else {\n cur = node.borrow().left.clone();\n }\n }\n // \u63d2\u5165\u8282\u70b9\n let node = TreeNode::new(num);\n let pre = pre.unwrap();\n if pre.borrow().val < num {\n pre.borrow_mut().right = Some(Rc::clone(&node));\n } else {\n pre.borrow_mut().left = Some(Rc::clone(&node));\n }\n}\n
binary_search_tree.c/* \u63d2\u5165\u8282\u70b9 */\nvoid insert(BinarySearchTree *bst, int num) {\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\n if (bst->root == NULL) {\n bst->root = newTreeNode(num);\n return;\n }\n TreeNode *cur = bst->root, *pre = NULL;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur != NULL) {\n // \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\n if (cur->val == num) {\n return;\n }\n pre = cur;\n if (cur->val < num) {\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n cur = cur->right;\n } else {\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n cur = cur->left;\n }\n }\n // \u63d2\u5165\u8282\u70b9\n TreeNode *node = newTreeNode(num);\n if (pre->val < num) {\n pre->right = node;\n } else {\n pre->left = node;\n }\n}\n
binary_search_tree.zig// \u63d2\u5165\u8282\u70b9\nfn insert(self: *Self, num: T) !void {\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\n if (self.root == null) {\n self.root = try self.mem_allocator.create(inc.TreeNode(T));\n return;\n }\n var cur = self.root;\n var pre: ?*inc.TreeNode(T) = null;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur != null) {\n // \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\n if (cur.?.val == num) return;\n pre = cur;\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur.?.val < num) {\n cur = cur.?.right;\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n } else {\n cur = cur.?.left;\n }\n }\n // \u63d2\u5165\u8282\u70b9\n var node = try self.mem_allocator.create(inc.TreeNode(T));\n node.init(num);\n if (pre.?.val < num) {\n pre.?.right = node;\n } else {\n pre.?.left = node;\n }\n}\n
\u4e0e\u67e5\u627e\u8282\u70b9\u76f8\u540c\uff0c\u63d2\u5165\u8282\u70b9\u4f7f\u7528 \\(O(\\log n)\\) \u65f6\u95f4\u3002
"},{"location":"chapter_tree/binary_search_tree/#3","title":"3. \u00a0 \u5220\u9664\u8282\u70b9","text":"\u5148\u5728\u4e8c\u53c9\u6811\u4e2d\u67e5\u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u518d\u5c06\u5176\u5220\u9664\u3002
\u4e0e\u63d2\u5165\u8282\u70b9\u7c7b\u4f3c\uff0c\u6211\u4eec\u9700\u8981\u4fdd\u8bc1\u5728\u5220\u9664\u64cd\u4f5c\u5b8c\u6210\u540e\uff0c\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u201c\u5de6\u5b50\u6811 < \u6839\u8282\u70b9 < \u53f3\u5b50\u6811\u201d\u7684\u6027\u8d28\u4ecd\u7136\u6ee1\u8db3\u3002
\u56e0\u6b64\uff0c\u6211\u4eec\u6839\u636e\u76ee\u6807\u8282\u70b9\u7684\u5b50\u8282\u70b9\u6570\u91cf\uff0c\u5206 0\u30011 \u548c 2 \u4e09\u79cd\u60c5\u51b5\uff0c\u6267\u884c\u5bf9\u5e94\u7684\u5220\u9664\u8282\u70b9\u64cd\u4f5c\u3002
\u5982\u56fe 7-19 \u6240\u793a\uff0c\u5f53\u5f85\u5220\u9664\u8282\u70b9\u7684\u5ea6\u4e3a \\(0\\) \u65f6\uff0c\u8868\u793a\u8be5\u8282\u70b9\u662f\u53f6\u8282\u70b9\uff0c\u53ef\u4ee5\u76f4\u63a5\u5220\u9664\u3002
\u56fe 7-19 \u00a0 \u5728\u4e8c\u53c9\u641c\u7d22\u6811\u4e2d\u5220\u9664\u8282\u70b9\uff08\u5ea6\u4e3a 0 \uff09
\u5982\u56fe 7-20 \u6240\u793a\uff0c\u5f53\u5f85\u5220\u9664\u8282\u70b9\u7684\u5ea6\u4e3a \\(1\\) \u65f6\uff0c\u5c06\u5f85\u5220\u9664\u8282\u70b9\u66ff\u6362\u4e3a\u5176\u5b50\u8282\u70b9\u5373\u53ef\u3002
\u56fe 7-20 \u00a0 \u5728\u4e8c\u53c9\u641c\u7d22\u6811\u4e2d\u5220\u9664\u8282\u70b9\uff08\u5ea6\u4e3a 1 \uff09
\u5f53\u5f85\u5220\u9664\u8282\u70b9\u7684\u5ea6\u4e3a \\(2\\) \u65f6\uff0c\u6211\u4eec\u65e0\u6cd5\u76f4\u63a5\u5220\u9664\u5b83\uff0c\u800c\u9700\u8981\u4f7f\u7528\u4e00\u4e2a\u8282\u70b9\u66ff\u6362\u8be5\u8282\u70b9\u3002\u7531\u4e8e\u8981\u4fdd\u6301\u4e8c\u53c9\u641c\u7d22\u6811\u201c\u5de6\u5b50\u6811 \\(<\\) \u6839\u8282\u70b9 \\(<\\) \u53f3\u5b50\u6811\u201d\u7684\u6027\u8d28\uff0c\u56e0\u6b64\u8fd9\u4e2a\u8282\u70b9\u53ef\u4ee5\u662f\u53f3\u5b50\u6811\u7684\u6700\u5c0f\u8282\u70b9\u6216\u5de6\u5b50\u6811\u7684\u6700\u5927\u8282\u70b9\u3002
\u5047\u8bbe\u6211\u4eec\u9009\u62e9\u53f3\u5b50\u6811\u7684\u6700\u5c0f\u8282\u70b9\uff08\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\uff09\uff0c\u5219\u5220\u9664\u64cd\u4f5c\u6d41\u7a0b\u5982\u56fe 7-21 \u6240\u793a\u3002
tmp
\u3002tmp
\u7684\u503c\u8986\u76d6\u5f85\u5220\u9664\u8282\u70b9\u7684\u503c\uff0c\u5e76\u5728\u6811\u4e2d\u9012\u5f52\u5220\u9664\u8282\u70b9 tmp
\u3002\u56fe 7-21 \u00a0 \u5728\u4e8c\u53c9\u641c\u7d22\u6811\u4e2d\u5220\u9664\u8282\u70b9\uff08\u5ea6\u4e3a 2 \uff09
\u5220\u9664\u8282\u70b9\u64cd\u4f5c\u540c\u6837\u4f7f\u7528 \\(O(\\log n)\\) \u65f6\u95f4\uff0c\u5176\u4e2d\u67e5\u627e\u5f85\u5220\u9664\u8282\u70b9\u9700\u8981 \\(O(\\log n)\\) \u65f6\u95f4\uff0c\u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u540e\u7ee7\u8282\u70b9\u9700\u8981 \\(O(\\log n)\\) \u65f6\u95f4\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig binary_search_tree.pydef remove(self, num: int):\n \"\"\"\u5220\u9664\u8282\u70b9\"\"\"\n # \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n if self._root is None:\n return\n # \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n cur, pre = self._root, None\n while cur is not None:\n # \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n if cur.val == num:\n break\n pre = cur\n # \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if cur.val < num:\n cur = cur.right\n # \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else:\n cur = cur.left\n # \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if cur is None:\n return\n\n # \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\n if cur.left is None or cur.right is None:\n # \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = null / \u8be5\u5b50\u8282\u70b9\n child = cur.left or cur.right\n # \u5220\u9664\u8282\u70b9 cur\n if cur != self._root:\n if pre.left == cur:\n pre.left = child\n else:\n pre.right = child\n else:\n # \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\n self._root = child\n # \u5b50\u8282\u70b9\u6570\u91cf = 2\n else:\n # \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\n tmp: TreeNode = cur.right\n while tmp.left is not None:\n tmp = tmp.left\n # \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\n self.remove(tmp.val)\n # \u7528 tmp \u8986\u76d6 cur\n cur.val = tmp.val\n
binary_search_tree.cpp/* \u5220\u9664\u8282\u70b9 */\nvoid remove(int num) {\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n if (root == nullptr)\n return;\n TreeNode *cur = root, *pre = nullptr;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur != nullptr) {\n // \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n if (cur->val == num)\n break;\n pre = cur;\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur->val < num)\n cur = cur->right;\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else\n cur = cur->left;\n }\n // \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if (cur == nullptr)\n return;\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\n if (cur->left == nullptr || cur->right == nullptr) {\n // \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = nullptr / \u8be5\u5b50\u8282\u70b9\n TreeNode *child = cur->left != nullptr ? cur->left : cur->right;\n // \u5220\u9664\u8282\u70b9 cur\n if (cur != root) {\n if (pre->left == cur)\n pre->left = child;\n else\n pre->right = child;\n } else {\n // \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\n root = child;\n }\n // \u91ca\u653e\u5185\u5b58\n delete cur;\n }\n // \u5b50\u8282\u70b9\u6570\u91cf = 2\n else {\n // \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\n TreeNode *tmp = cur->right;\n while (tmp->left != nullptr) {\n tmp = tmp->left;\n }\n int tmpVal = tmp->val;\n // \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\n remove(tmp->val);\n // \u7528 tmp \u8986\u76d6 cur\n cur->val = tmpVal;\n }\n}\n
binary_search_tree.java/* \u5220\u9664\u8282\u70b9 */\nvoid remove(int num) {\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n if (root == null)\n return;\n TreeNode cur = root, pre = null;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur != null) {\n // \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n if (cur.val == num)\n break;\n pre = cur;\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur.val < num)\n cur = cur.right;\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else\n cur = cur.left;\n }\n // \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if (cur == null)\n return;\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\n if (cur.left == null || cur.right == null) {\n // \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = null / \u8be5\u5b50\u8282\u70b9\n TreeNode child = cur.left != null ? cur.left : cur.right;\n // \u5220\u9664\u8282\u70b9 cur\n if (cur != root) {\n if (pre.left == cur)\n pre.left = child;\n else\n pre.right = child;\n } else {\n // \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\n root = child;\n }\n }\n // \u5b50\u8282\u70b9\u6570\u91cf = 2\n else {\n // \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\n TreeNode tmp = cur.right;\n while (tmp.left != null) {\n tmp = tmp.left;\n }\n // \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\n remove(tmp.val);\n // \u7528 tmp \u8986\u76d6 cur\n cur.val = tmp.val;\n }\n}\n
binary_search_tree.cs/* \u5220\u9664\u8282\u70b9 */\nvoid Remove(int num) {\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n if (root == null)\n return;\n TreeNode? cur = root, pre = null;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur != null) {\n // \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n if (cur.val == num)\n break;\n pre = cur;\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur.val < num)\n cur = cur.right;\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else\n cur = cur.left;\n }\n // \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if (cur == null)\n return;\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\n if (cur.left == null || cur.right == null) {\n // \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = null / \u8be5\u5b50\u8282\u70b9\n TreeNode? child = cur.left ?? cur.right;\n // \u5220\u9664\u8282\u70b9 cur\n if (cur != root) {\n if (pre!.left == cur)\n pre.left = child;\n else\n pre.right = child;\n } else {\n // \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\n root = child;\n }\n }\n // \u5b50\u8282\u70b9\u6570\u91cf = 2\n else {\n // \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\n TreeNode? tmp = cur.right;\n while (tmp.left != null) {\n tmp = tmp.left;\n }\n // \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\n Remove(tmp.val!.Value);\n // \u7528 tmp \u8986\u76d6 cur\n cur.val = tmp.val;\n }\n}\n
binary_search_tree.go/* \u5220\u9664\u8282\u70b9 */\nfunc (bst *binarySearchTree) remove(num int) {\n cur := bst.root\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n if cur == nil {\n return\n }\n // \u5f85\u5220\u9664\u8282\u70b9\u4e4b\u524d\u7684\u8282\u70b9\u4f4d\u7f6e\n var pre *TreeNode = nil\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n for cur != nil {\n if cur.Val == num {\n break\n }\n pre = cur\n if cur.Val.(int) < num {\n // \u5f85\u5220\u9664\u8282\u70b9\u5728\u53f3\u5b50\u6811\u4e2d\n cur = cur.Right\n } else {\n // \u5f85\u5220\u9664\u8282\u70b9\u5728\u5de6\u5b50\u6811\u4e2d\n cur = cur.Left\n }\n }\n // \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if cur == nil {\n return\n }\n // \u5b50\u8282\u70b9\u6570\u4e3a 0 \u6216 1\n if cur.Left == nil || cur.Right == nil {\n var child *TreeNode = nil\n // \u53d6\u51fa\u5f85\u5220\u9664\u8282\u70b9\u7684\u5b50\u8282\u70b9\n if cur.Left != nil {\n child = cur.Left\n } else {\n child = cur.Right\n }\n // \u5220\u9664\u8282\u70b9 cur\n if cur != bst.root {\n if pre.Left == cur {\n pre.Left = child\n } else {\n pre.Right = child\n }\n } else {\n // \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\n bst.root = child\n }\n // \u5b50\u8282\u70b9\u6570\u4e3a 2\n } else {\n // \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d\u5f85\u5220\u9664\u8282\u70b9 cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\n tmp := cur.Right\n for tmp.Left != nil {\n tmp = tmp.Left\n }\n // \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\n bst.remove(tmp.Val.(int))\n // \u7528 tmp \u8986\u76d6 cur\n cur.Val = tmp.Val\n }\n}\n
binary_search_tree.swift/* \u5220\u9664\u8282\u70b9 */\nfunc remove(num: Int) {\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n if root == nil {\n return\n }\n var cur = root\n var pre: TreeNode?\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while cur != nil {\n // \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n if cur!.val == num {\n break\n }\n pre = cur\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if cur!.val < num {\n cur = cur?.right\n }\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else {\n cur = cur?.left\n }\n }\n // \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if cur == nil {\n return\n }\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\n if cur?.left == nil || cur?.right == nil {\n // \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = null / \u8be5\u5b50\u8282\u70b9\n let child = cur?.left != nil ? cur?.left : cur?.right\n // \u5220\u9664\u8282\u70b9 cur\n if cur !== root {\n if pre?.left === cur {\n pre?.left = child\n } else {\n pre?.right = child\n }\n } else {\n // \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\n root = child\n }\n }\n // \u5b50\u8282\u70b9\u6570\u91cf = 2\n else {\n // \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\n var tmp = cur?.right\n while tmp?.left != nil {\n tmp = tmp?.left\n }\n // \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\n remove(num: tmp!.val)\n // \u7528 tmp \u8986\u76d6 cur\n cur?.val = tmp!.val\n }\n}\n
binary_search_tree.js/* \u5220\u9664\u8282\u70b9 */\nremove(num) {\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n if (this.root === null) return;\n let cur = this.root,\n pre = null;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur !== null) {\n // \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n if (cur.val === num) break;\n pre = cur;\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur.val < num) cur = cur.right;\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else cur = cur.left;\n }\n // \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if (cur === null) return;\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\n if (cur.left === null || cur.right === null) {\n // \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = null / \u8be5\u5b50\u8282\u70b9\n const child = cur.left !== null ? cur.left : cur.right;\n // \u5220\u9664\u8282\u70b9 cur\n if (cur !== this.root) {\n if (pre.left === cur) pre.left = child;\n else pre.right = child;\n } else {\n // \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\n this.root = child;\n }\n }\n // \u5b50\u8282\u70b9\u6570\u91cf = 2\n else {\n // \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\n let tmp = cur.right;\n while (tmp.left !== null) {\n tmp = tmp.left;\n }\n // \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\n this.remove(tmp.val);\n // \u7528 tmp \u8986\u76d6 cur\n cur.val = tmp.val;\n }\n}\n
binary_search_tree.ts/* \u5220\u9664\u8282\u70b9 */\nremove(num: number): void {\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n if (this.root === null) return;\n let cur: TreeNode | null = this.root,\n pre: TreeNode | null = null;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur !== null) {\n // \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n if (cur.val === num) break;\n pre = cur;\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur.val < num) cur = cur.right;\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else cur = cur.left;\n }\n // \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if (cur === null) return;\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\n if (cur.left === null || cur.right === null) {\n // \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = null / \u8be5\u5b50\u8282\u70b9\n const child: TreeNode | null =\n cur.left !== null ? cur.left : cur.right;\n // \u5220\u9664\u8282\u70b9 cur\n if (cur !== this.root) {\n if (pre!.left === cur) pre!.left = child;\n else pre!.right = child;\n } else {\n // \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\n this.root = child;\n }\n }\n // \u5b50\u8282\u70b9\u6570\u91cf = 2\n else {\n // \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\n let tmp: TreeNode | null = cur.right;\n while (tmp!.left !== null) {\n tmp = tmp!.left;\n }\n // \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\n this.remove(tmp!.val);\n // \u7528 tmp \u8986\u76d6 cur\n cur.val = tmp!.val;\n }\n}\n
binary_search_tree.dart/* \u5220\u9664\u8282\u70b9 */\nvoid remove(int _num) {\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n if (_root == null) return;\n TreeNode? cur = _root;\n TreeNode? pre = null;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur != null) {\n // \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n if (cur.val == _num) break;\n pre = cur;\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur.val < _num)\n cur = cur.right;\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else\n cur = cur.left;\n }\n // \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\n if (cur == null) return;\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\n if (cur.left == null || cur.right == null) {\n // \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = null / \u8be5\u5b50\u8282\u70b9\n TreeNode? child = cur.left ?? cur.right;\n // \u5220\u9664\u8282\u70b9 cur\n if (cur != _root) {\n if (pre!.left == cur)\n pre.left = child;\n else\n pre.right = child;\n } else {\n // \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\n _root = child;\n }\n } else {\n // \u5b50\u8282\u70b9\u6570\u91cf = 2\n // \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\n TreeNode? tmp = cur.right;\n while (tmp!.left != null) {\n tmp = tmp.left;\n }\n // \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\n remove(tmp.val);\n // \u7528 tmp \u8986\u76d6 cur\n cur.val = tmp.val;\n }\n}\n
binary_search_tree.rs/* \u5220\u9664\u8282\u70b9 */\npub fn remove(&mut self, num: i32) {\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n if self.root.is_none() { \n return; \n }\n let mut cur = self.root.clone();\n let mut pre = None;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while let Some(node) = cur.clone() {\n // \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n if node.borrow().val == num {\n break;\n }\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n pre = cur.clone();\n if node.borrow().val < num {\n cur = node.borrow().right.clone();\n }\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else {\n cur = node.borrow().left.clone();\n }\n }\n // \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if cur.is_none() {\n return;\n }\n let cur = cur.unwrap();\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\n if cur.borrow().left.is_none() || cur.borrow().right.is_none() {\n // \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = nullptr / \u8be5\u5b50\u8282\u70b9\n let child = cur.borrow().left.clone().or_else(|| cur.borrow().right.clone());\n let pre = pre.unwrap();\n let left = pre.borrow().left.clone().unwrap();\n // \u5220\u9664\u8282\u70b9 cur\n if !Rc::ptr_eq(&cur, self.root.as_ref().unwrap()) {\n if Rc::ptr_eq(&left, &cur) {\n pre.borrow_mut().left = child;\n } else {\n pre.borrow_mut().right = child;\n }\n } else {\n // \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\n self.root = child;\n }\n }\n // \u5b50\u8282\u70b9\u6570\u91cf = 2\n else {\n // \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\n let mut tmp = cur.borrow().right.clone();\n while let Some(node) = tmp.clone() {\n if node.borrow().left.is_some() {\n tmp = node.borrow().left.clone();\n } else {\n break;\n }\n }\n let tmpval = tmp.unwrap().borrow().val;\n // \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\n self.remove(tmpval);\n // \u7528 tmp \u8986\u76d6 cur\n cur.borrow_mut().val = tmpval;\n }\n}\n
binary_search_tree.c/* \u5220\u9664\u8282\u70b9 */\n// \u7531\u4e8e\u5f15\u5165\u4e86 stdio.h \uff0c\u6b64\u5904\u65e0\u6cd5\u4f7f\u7528 remove \u5173\u952e\u8bcd\nvoid removeItem(BinarySearchTree *bst, int num) {\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n if (bst->root == NULL)\n return;\n TreeNode *cur = bst->root, *pre = NULL;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur != NULL) {\n // \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n if (cur->val == num)\n break;\n pre = cur;\n if (cur->val < num) {\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 root \u7684\u53f3\u5b50\u6811\u4e2d\n cur = cur->right;\n } else {\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 root \u7684\u5de6\u5b50\u6811\u4e2d\n cur = cur->left;\n }\n }\n // \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if (cur == NULL)\n return;\n // \u5224\u65ad\u5f85\u5220\u9664\u8282\u70b9\u662f\u5426\u5b58\u5728\u5b50\u8282\u70b9\n if (cur->left == NULL || cur->right == NULL) {\n /* \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1 */\n // \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = nullptr / \u8be5\u5b50\u8282\u70b9\n TreeNode *child = cur->left != NULL ? cur->left : cur->right;\n // \u5220\u9664\u8282\u70b9 cur\n if (pre->left == cur) {\n pre->left = child;\n } else {\n pre->right = child;\n }\n } else {\n /* \u5b50\u8282\u70b9\u6570\u91cf = 2 */\n // \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\n TreeNode *tmp = cur->right;\n while (tmp->left != NULL) {\n tmp = tmp->left;\n }\n int tmpVal = tmp->val;\n // \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\n removeItem(bst, tmp->val);\n // \u7528 tmp \u8986\u76d6 cur\n cur->val = tmpVal;\n }\n}\n
binary_search_tree.zig// \u5220\u9664\u8282\u70b9\nfn remove(self: *Self, num: T) void {\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n if (self.root == null) return;\n var cur = self.root;\n var pre: ?*inc.TreeNode(T) = null;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur != null) {\n // \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n if (cur.?.val == num) break;\n pre = cur;\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur.?.val < num) {\n cur = cur.?.right;\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n } else {\n cur = cur.?.left;\n }\n }\n // \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if (cur == null) return;\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\n if (cur.?.left == null or cur.?.right == null) {\n // \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = null / \u8be5\u5b50\u8282\u70b9\n var child = if (cur.?.left != null) cur.?.left else cur.?.right;\n // \u5220\u9664\u8282\u70b9 cur\n if (pre.?.left == cur) {\n pre.?.left = child;\n } else {\n pre.?.right = child;\n }\n // \u5b50\u8282\u70b9\u6570\u91cf = 2\n } else {\n // \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\n var tmp = cur.?.right;\n while (tmp.?.left != null) {\n tmp = tmp.?.left;\n }\n var tmp_val = tmp.?.val;\n // \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\n self.remove(tmp.?.val);\n // \u7528 tmp \u8986\u76d6 cur\n cur.?.val = tmp_val;\n }\n}\n
"},{"location":"chapter_tree/binary_search_tree/#4","title":"4. \u00a0 \u4e2d\u5e8f\u904d\u5386\u6709\u5e8f","text":"\u5982\u56fe 7-22 \u6240\u793a\uff0c\u4e8c\u53c9\u6811\u7684\u4e2d\u5e8f\u904d\u5386\u9075\u5faa\u201c\u5de6 \\(\\rightarrow\\) \u6839 \\(\\rightarrow\\) \u53f3\u201d\u7684\u904d\u5386\u987a\u5e8f\uff0c\u800c\u4e8c\u53c9\u641c\u7d22\u6811\u6ee1\u8db3\u201c\u5de6\u5b50\u8282\u70b9 \\(<\\) \u6839\u8282\u70b9 \\(<\\) \u53f3\u5b50\u8282\u70b9\u201d\u7684\u5927\u5c0f\u5173\u7cfb\u3002
\u8fd9\u610f\u5473\u7740\u5728\u4e8c\u53c9\u641c\u7d22\u6811\u4e2d\u8fdb\u884c\u4e2d\u5e8f\u904d\u5386\u65f6\uff0c\u603b\u662f\u4f1a\u4f18\u5148\u904d\u5386\u4e0b\u4e00\u4e2a\u6700\u5c0f\u8282\u70b9\uff0c\u4ece\u800c\u5f97\u51fa\u4e00\u4e2a\u91cd\u8981\u6027\u8d28\uff1a\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u4e2d\u5e8f\u904d\u5386\u5e8f\u5217\u662f\u5347\u5e8f\u7684\u3002
\u5229\u7528\u4e2d\u5e8f\u904d\u5386\u5347\u5e8f\u7684\u6027\u8d28\uff0c\u6211\u4eec\u5728\u4e8c\u53c9\u641c\u7d22\u6811\u4e2d\u83b7\u53d6\u6709\u5e8f\u6570\u636e\u4ec5\u9700 \\(O(n)\\) \u65f6\u95f4\uff0c\u65e0\u987b\u8fdb\u884c\u989d\u5916\u7684\u6392\u5e8f\u64cd\u4f5c\uff0c\u975e\u5e38\u9ad8\u6548\u3002
\u56fe 7-22 \u00a0 \u4e8c\u53c9\u641c\u7d22\u6811\u7684\u4e2d\u5e8f\u904d\u5386\u5e8f\u5217
"},{"location":"chapter_tree/binary_search_tree/#742","title":"7.4.2 \u00a0 \u4e8c\u53c9\u641c\u7d22\u6811\u7684\u6548\u7387","text":"\u7ed9\u5b9a\u4e00\u7ec4\u6570\u636e\uff0c\u6211\u4eec\u8003\u8651\u4f7f\u7528\u6570\u7ec4\u6216\u4e8c\u53c9\u641c\u7d22\u6811\u5b58\u50a8\u3002\u89c2\u5bdf\u8868 7-2 \uff0c\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u5404\u9879\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u90fd\u662f\u5bf9\u6570\u9636\uff0c\u5177\u6709\u7a33\u5b9a\u4e14\u9ad8\u6548\u7684\u6027\u80fd\u3002\u53ea\u6709\u5728\u9ad8\u9891\u6dfb\u52a0\u3001\u4f4e\u9891\u67e5\u627e\u5220\u9664\u6570\u636e\u7684\u573a\u666f\u4e0b\uff0c\u6570\u7ec4\u6bd4\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u6548\u7387\u66f4\u9ad8\u3002
\u8868 7-2 \u00a0 \u6570\u7ec4\u4e0e\u641c\u7d22\u6811\u7684\u6548\u7387\u5bf9\u6bd4
\u65e0\u5e8f\u6570\u7ec4 \u4e8c\u53c9\u641c\u7d22\u6811 \u67e5\u627e\u5143\u7d20 \\(O(n)\\) \\(O(\\log n)\\) \u63d2\u5165\u5143\u7d20 \\(O(1)\\) \\(O(\\log n)\\) \u5220\u9664\u5143\u7d20 \\(O(n)\\) \\(O(\\log n)\\)\u5728\u7406\u60f3\u60c5\u51b5\u4e0b\uff0c\u4e8c\u53c9\u641c\u7d22\u6811\u662f\u201c\u5e73\u8861\u201d\u7684\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u5728 \\(\\log n\\) \u8f6e\u5faa\u73af\u5185\u67e5\u627e\u4efb\u610f\u8282\u70b9\u3002
\u7136\u800c\uff0c\u5982\u679c\u6211\u4eec\u5728\u4e8c\u53c9\u641c\u7d22\u6811\u4e2d\u4e0d\u65ad\u5730\u63d2\u5165\u548c\u5220\u9664\u8282\u70b9\uff0c\u53ef\u80fd\u5bfc\u81f4\u4e8c\u53c9\u6811\u9000\u5316\u4e3a\u56fe 7-23 \u6240\u793a\u7684\u94fe\u8868\uff0c\u8fd9\u65f6\u5404\u79cd\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e5f\u4f1a\u9000\u5316\u4e3a \\(O(n)\\) \u3002
\u56fe 7-23 \u00a0 \u4e8c\u53c9\u641c\u7d22\u6811\u9000\u5316
"},{"location":"chapter_tree/binary_search_tree/#743","title":"7.4.3 \u00a0 \u4e8c\u53c9\u641c\u7d22\u6811\u5e38\u89c1\u5e94\u7528","text":"\u300c\u4e8c\u53c9\u6811 binary tree\u300d\u662f\u4e00\u79cd\u975e\u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff0c\u4ee3\u8868\u201c\u7956\u5148\u201d\u4e0e\u201c\u540e\u4ee3\u201d\u4e4b\u95f4\u7684\u6d3e\u751f\u5173\u7cfb\uff0c\u4f53\u73b0\u4e86\u201c\u4e00\u5206\u4e3a\u4e8c\u201d\u7684\u5206\u6cbb\u903b\u8f91\u3002\u4e0e\u94fe\u8868\u7c7b\u4f3c\uff0c\u4e8c\u53c9\u6811\u7684\u57fa\u672c\u5355\u5143\u662f\u8282\u70b9\uff0c\u6bcf\u4e2a\u8282\u70b9\u5305\u542b\u503c\u3001\u5de6\u5b50\u8282\u70b9\u5f15\u7528\u548c\u53f3\u5b50\u8282\u70b9\u5f15\u7528\u3002
PythonC++JavaC#GoSwiftJSTSDartRustCZigclass TreeNode:\n \"\"\"\u4e8c\u53c9\u6811\u8282\u70b9\u7c7b\"\"\"\n def __init__(self, val: int):\n self.val: int = val # \u8282\u70b9\u503c\n self.left: TreeNode | None = None # \u5de6\u5b50\u8282\u70b9\u5f15\u7528\n self.right: TreeNode | None = None # \u53f3\u5b50\u8282\u70b9\u5f15\u7528\n
/* \u4e8c\u53c9\u6811\u8282\u70b9\u7ed3\u6784\u4f53 */\nstruct TreeNode {\n int val; // \u8282\u70b9\u503c\n TreeNode *left; // \u5de6\u5b50\u8282\u70b9\u6307\u9488\n TreeNode *right; // \u53f3\u5b50\u8282\u70b9\u6307\u9488\n TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n};\n
/* \u4e8c\u53c9\u6811\u8282\u70b9\u7c7b */\nclass TreeNode {\n int val; // \u8282\u70b9\u503c\n TreeNode left; // \u5de6\u5b50\u8282\u70b9\u5f15\u7528\n TreeNode right; // \u53f3\u5b50\u8282\u70b9\u5f15\u7528\n TreeNode(int x) { val = x; }\n}\n
/* \u4e8c\u53c9\u6811\u8282\u70b9\u7c7b */\nclass TreeNode(int? x) {\n public int? val = x; // \u8282\u70b9\u503c\n public TreeNode? left; // \u5de6\u5b50\u8282\u70b9\u5f15\u7528\n public TreeNode? right; // \u53f3\u5b50\u8282\u70b9\u5f15\u7528\n}\n
/* \u4e8c\u53c9\u6811\u8282\u70b9\u7ed3\u6784\u4f53 */\ntype TreeNode struct {\n Val int\n Left *TreeNode\n Right *TreeNode\n}\n/* \u6784\u9020\u65b9\u6cd5 */\nfunc NewTreeNode(v int) *TreeNode {\n return &TreeNode{\n Left: nil, // \u5de6\u5b50\u8282\u70b9\u6307\u9488\n Right: nil, // \u53f3\u5b50\u8282\u70b9\u6307\u9488\n Val: v, // \u8282\u70b9\u503c\n }\n}\n
/* \u4e8c\u53c9\u6811\u8282\u70b9\u7c7b */\nclass TreeNode {\n var val: Int // \u8282\u70b9\u503c\n var left: TreeNode? // \u5de6\u5b50\u8282\u70b9\u5f15\u7528\n var right: TreeNode? // \u53f3\u5b50\u8282\u70b9\u5f15\u7528\n\n init(x: Int) {\n val = x\n }\n}\n
/* \u4e8c\u53c9\u6811\u8282\u70b9\u7c7b */\nclass TreeNode {\n val; // \u8282\u70b9\u503c\n left; // \u5de6\u5b50\u8282\u70b9\u6307\u9488\n right; // \u53f3\u5b50\u8282\u70b9\u6307\u9488\n constructor(val, left, right) {\n this.val = val === undefined ? 0 : val;\n this.left = left === undefined ? null : left;\n this.right = right === undefined ? null : right;\n }\n}\n
/* \u4e8c\u53c9\u6811\u8282\u70b9\u7c7b */\nclass TreeNode {\n val: number;\n left: TreeNode | null;\n right: TreeNode | null;\n\n constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {\n this.val = val === undefined ? 0 : val; // \u8282\u70b9\u503c\n this.left = left === undefined ? null : left; // \u5de6\u5b50\u8282\u70b9\u5f15\u7528\n this.right = right === undefined ? null : right; // \u53f3\u5b50\u8282\u70b9\u5f15\u7528\n }\n}\n
/* \u4e8c\u53c9\u6811\u8282\u70b9\u7c7b */\nclass TreeNode {\n int val; // \u8282\u70b9\u503c\n TreeNode? left; // \u5de6\u5b50\u8282\u70b9\u5f15\u7528\n TreeNode? right; // \u53f3\u5b50\u8282\u70b9\u5f15\u7528\n TreeNode(this.val, [this.left, this.right]);\n}\n
use std::rc::Rc;\nuse std::cell::RefCell;\n\n/* \u4e8c\u53c9\u6811\u8282\u70b9\u7ed3\u6784\u4f53 */\nstruct TreeNode {\n val: i32, // \u8282\u70b9\u503c\n left: Option<Rc<RefCell<TreeNode>>>, // \u5de6\u5b50\u8282\u70b9\u5f15\u7528\n right: Option<Rc<RefCell<TreeNode>>>, // \u53f3\u5b50\u8282\u70b9\u5f15\u7528\n}\n\nimpl TreeNode {\n /* \u6784\u9020\u65b9\u6cd5 */\n fn new(val: i32) -> Rc<RefCell<Self>> {\n Rc::new(RefCell::new(Self {\n val,\n left: None,\n right: None\n }))\n }\n}\n
/* \u4e8c\u53c9\u6811\u8282\u70b9\u7ed3\u6784\u4f53 */\ntypedef struct TreeNode {\n int val; // \u8282\u70b9\u503c\n int height; // \u8282\u70b9\u9ad8\u5ea6\n struct TreeNode *left; // \u5de6\u5b50\u8282\u70b9\u6307\u9488\n struct TreeNode *right; // \u53f3\u5b50\u8282\u70b9\u6307\u9488\n} TreeNode;\n\n/* \u6784\u9020\u51fd\u6570 */\nTreeNode *newTreeNode(int val) {\n TreeNode *node;\n\n node = (TreeNode *)malloc(sizeof(TreeNode));\n node->val = val;\n node->height = 0;\n node->left = NULL;\n node->right = NULL;\n return node;\n}\n
\n
\u6bcf\u4e2a\u8282\u70b9\u90fd\u6709\u4e24\u4e2a\u5f15\u7528\uff08\u6307\u9488\uff09\uff0c\u5206\u522b\u6307\u5411\u300c\u5de6\u5b50\u8282\u70b9 left-child node\u300d\u548c\u300c\u53f3\u5b50\u8282\u70b9 right-child node\u300d\uff0c\u8be5\u8282\u70b9\u88ab\u79f0\u4e3a\u8fd9\u4e24\u4e2a\u5b50\u8282\u70b9\u7684\u300c\u7236\u8282\u70b9 parent node\u300d\u3002\u5f53\u7ed9\u5b9a\u4e00\u4e2a\u4e8c\u53c9\u6811\u7684\u8282\u70b9\u65f6\uff0c\u6211\u4eec\u5c06\u8be5\u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u53ca\u5176\u4ee5\u4e0b\u8282\u70b9\u5f62\u6210\u7684\u6811\u79f0\u4e3a\u8be5\u8282\u70b9\u7684\u300c\u5de6\u5b50\u6811 left subtree\u300d\uff0c\u540c\u7406\u53ef\u5f97\u300c\u53f3\u5b50\u6811 right subtree\u300d\u3002
\u5728\u4e8c\u53c9\u6811\u4e2d\uff0c\u9664\u53f6\u8282\u70b9\u5916\uff0c\u5176\u4ed6\u6240\u6709\u8282\u70b9\u90fd\u5305\u542b\u5b50\u8282\u70b9\u548c\u975e\u7a7a\u5b50\u6811\u3002\u5982\u56fe 7-1 \u6240\u793a\uff0c\u5982\u679c\u5c06\u201c\u8282\u70b9 2\u201d\u89c6\u4e3a\u7236\u8282\u70b9\uff0c\u5219\u5176\u5de6\u5b50\u8282\u70b9\u548c\u53f3\u5b50\u8282\u70b9\u5206\u522b\u662f\u201c\u8282\u70b9 4\u201d\u548c\u201c\u8282\u70b9 5\u201d\uff0c\u5de6\u5b50\u6811\u662f\u201c\u8282\u70b9 4 \u53ca\u5176\u4ee5\u4e0b\u8282\u70b9\u5f62\u6210\u7684\u6811\u201d\uff0c\u53f3\u5b50\u6811\u662f\u201c\u8282\u70b9 5 \u53ca\u5176\u4ee5\u4e0b\u8282\u70b9\u5f62\u6210\u7684\u6811\u201d\u3002
\u56fe 7-1 \u00a0 \u7236\u8282\u70b9\u3001\u5b50\u8282\u70b9\u3001\u5b50\u6811
"},{"location":"chapter_tree/binary_tree/#711","title":"7.1.1 \u00a0 \u4e8c\u53c9\u6811\u5e38\u89c1\u672f\u8bed","text":"\u4e8c\u53c9\u6811\u7684\u5e38\u7528\u672f\u8bed\u5982\u56fe 7-2 \u6240\u793a\u3002
\u56fe 7-2 \u00a0 \u4e8c\u53c9\u6811\u7684\u5e38\u7528\u672f\u8bed
Tip
\u8bf7\u6ce8\u610f\uff0c\u6211\u4eec\u901a\u5e38\u5c06\u201c\u9ad8\u5ea6\u201d\u548c\u201c\u6df1\u5ea6\u201d\u5b9a\u4e49\u4e3a\u201c\u7ecf\u8fc7\u7684\u8fb9\u7684\u6570\u91cf\u201d\uff0c\u4f46\u6709\u4e9b\u9898\u76ee\u6216\u6559\u6750\u53ef\u80fd\u4f1a\u5c06\u5176\u5b9a\u4e49\u4e3a\u201c\u7ecf\u8fc7\u7684\u8282\u70b9\u7684\u6570\u91cf\u201d\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u9ad8\u5ea6\u548c\u6df1\u5ea6\u90fd\u9700\u8981\u52a0 1 \u3002
"},{"location":"chapter_tree/binary_tree/#712","title":"7.1.2 \u00a0 \u4e8c\u53c9\u6811\u57fa\u672c\u64cd\u4f5c","text":""},{"location":"chapter_tree/binary_tree/#1","title":"1. \u00a0 \u521d\u59cb\u5316\u4e8c\u53c9\u6811","text":"\u4e0e\u94fe\u8868\u7c7b\u4f3c\uff0c\u9996\u5148\u521d\u59cb\u5316\u8282\u70b9\uff0c\u7136\u540e\u6784\u5efa\u5f15\u7528\uff08\u6307\u9488\uff09\u3002
PythonC++JavaC#GoSwiftJSTSDartRustCZig binary_tree.py# \u521d\u59cb\u5316\u4e8c\u53c9\u6811\n# \u521d\u59cb\u5316\u8282\u70b9\nn1 = TreeNode(val=1)\nn2 = TreeNode(val=2)\nn3 = TreeNode(val=3)\nn4 = TreeNode(val=4)\nn5 = TreeNode(val=5)\n# \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\uff08\u6307\u9488\uff09\nn1.left = n2\nn1.right = n3\nn2.left = n4\nn2.right = n5\n
binary_tree.cpp/* \u521d\u59cb\u5316\u4e8c\u53c9\u6811 */\n// \u521d\u59cb\u5316\u8282\u70b9\nTreeNode* n1 = new TreeNode(1);\nTreeNode* n2 = new TreeNode(2);\nTreeNode* n3 = new TreeNode(3);\nTreeNode* n4 = new TreeNode(4);\nTreeNode* n5 = new TreeNode(5);\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\uff08\u6307\u9488\uff09\nn1->left = n2;\nn1->right = n3;\nn2->left = n4;\nn2->right = n5;\n
binary_tree.java// \u521d\u59cb\u5316\u8282\u70b9\nTreeNode n1 = new TreeNode(1);\nTreeNode n2 = new TreeNode(2);\nTreeNode n3 = new TreeNode(3);\nTreeNode n4 = new TreeNode(4);\nTreeNode n5 = new TreeNode(5);\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\uff08\u6307\u9488\uff09\nn1.left = n2;\nn1.right = n3;\nn2.left = n4;\nn2.right = n5;\n
binary_tree.cs/* \u521d\u59cb\u5316\u4e8c\u53c9\u6811 */\n// \u521d\u59cb\u5316\u8282\u70b9\nTreeNode n1 = new(1);\nTreeNode n2 = new(2);\nTreeNode n3 = new(3);\nTreeNode n4 = new(4);\nTreeNode n5 = new(5);\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\uff08\u6307\u9488\uff09\nn1.left = n2;\nn1.right = n3;\nn2.left = n4;\nn2.right = n5;\n
binary_tree.go/* \u521d\u59cb\u5316\u4e8c\u53c9\u6811 */\n// \u521d\u59cb\u5316\u8282\u70b9\nn1 := NewTreeNode(1)\nn2 := NewTreeNode(2)\nn3 := NewTreeNode(3)\nn4 := NewTreeNode(4)\nn5 := NewTreeNode(5)\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\uff08\u6307\u9488\uff09\nn1.Left = n2\nn1.Right = n3\nn2.Left = n4\nn2.Right = n5\n
binary_tree.swift// \u521d\u59cb\u5316\u8282\u70b9\nlet n1 = TreeNode(x: 1)\nlet n2 = TreeNode(x: 2)\nlet n3 = TreeNode(x: 3)\nlet n4 = TreeNode(x: 4)\nlet n5 = TreeNode(x: 5)\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\uff08\u6307\u9488\uff09\nn1.left = n2\nn1.right = n3\nn2.left = n4\nn2.right = n5\n
binary_tree.js/* \u521d\u59cb\u5316\u4e8c\u53c9\u6811 */\n// \u521d\u59cb\u5316\u8282\u70b9\nlet n1 = new TreeNode(1),\n n2 = new TreeNode(2),\n n3 = new TreeNode(3),\n n4 = new TreeNode(4),\n n5 = new TreeNode(5);\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\uff08\u6307\u9488\uff09\nn1.left = n2;\nn1.right = n3;\nn2.left = n4;\nn2.right = n5;\n
binary_tree.ts/* \u521d\u59cb\u5316\u4e8c\u53c9\u6811 */\n// \u521d\u59cb\u5316\u8282\u70b9\nlet n1 = new TreeNode(1),\n n2 = new TreeNode(2),\n n3 = new TreeNode(3),\n n4 = new TreeNode(4),\n n5 = new TreeNode(5);\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\uff08\u6307\u9488\uff09\nn1.left = n2;\nn1.right = n3;\nn2.left = n4;\nn2.right = n5;\n
binary_tree.dart/* \u521d\u59cb\u5316\u4e8c\u53c9\u6811 */\n// \u521d\u59cb\u5316\u8282\u70b9\nTreeNode n1 = new TreeNode(1);\nTreeNode n2 = new TreeNode(2);\nTreeNode n3 = new TreeNode(3);\nTreeNode n4 = new TreeNode(4);\nTreeNode n5 = new TreeNode(5);\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\uff08\u6307\u9488\uff09\nn1.left = n2;\nn1.right = n3;\nn2.left = n4;\nn2.right = n5;\n
binary_tree.rs// \u521d\u59cb\u5316\u8282\u70b9\nlet n1 = TreeNode::new(1);\nlet n2 = TreeNode::new(2);\nlet n3 = TreeNode::new(3);\nlet n4 = TreeNode::new(4);\nlet n5 = TreeNode::new(5);\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\uff08\u6307\u9488\uff09\nn1.borrow_mut().left = Some(n2.clone());\nn1.borrow_mut().right = Some(n3);\nn2.borrow_mut().left = Some(n4);\nn2.borrow_mut().right = Some(n5);\n
binary_tree.c/* \u521d\u59cb\u5316\u4e8c\u53c9\u6811 */\n// \u521d\u59cb\u5316\u8282\u70b9\nTreeNode *n1 = newTreeNode(1);\nTreeNode *n2 = newTreeNode(2);\nTreeNode *n3 = newTreeNode(3);\nTreeNode *n4 = newTreeNode(4);\nTreeNode *n5 = newTreeNode(5);\n// \u6784\u5efa\u8282\u70b9\u4e4b\u95f4\u7684\u5f15\u7528\uff08\u6307\u9488\uff09\nn1->left = n2;\nn1->right = n3;\nn2->left = n4;\nn2->right = n5;\n
binary_tree.zig\n
"},{"location":"chapter_tree/binary_tree/#2","title":"2. \u00a0 \u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9","text":"\u4e0e\u94fe\u8868\u7c7b\u4f3c\uff0c\u5728\u4e8c\u53c9\u6811\u4e2d\u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9\u53ef\u4ee5\u901a\u8fc7\u4fee\u6539\u6307\u9488\u6765\u5b9e\u73b0\u3002\u56fe 7-3 \u7ed9\u51fa\u4e86\u4e00\u4e2a\u793a\u4f8b\u3002
\u56fe 7-3 \u00a0 \u5728\u4e8c\u53c9\u6811\u4e2d\u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9
PythonC++JavaC#GoSwiftJSTSDartRustCZig binary_tree.py# \u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9\np = TreeNode(0)\n# \u5728 n1 -> n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\nn1.left = p\np.left = n2\n# \u5220\u9664\u8282\u70b9 P\nn1.left = n2\n
binary_tree.cpp/* \u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9 */\nTreeNode* P = new TreeNode(0);\n// \u5728 n1 -> n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\nn1->left = P;\nP->left = n2;\n// \u5220\u9664\u8282\u70b9 P\nn1->left = n2;\n
binary_tree.javaTreeNode P = new TreeNode(0);\n// \u5728 n1 -> n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\nn1.left = P;\nP.left = n2;\n// \u5220\u9664\u8282\u70b9 P\nn1.left = n2;\n
binary_tree.cs/* \u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9 */\nTreeNode P = new(0);\n// \u5728 n1 -> n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\nn1.left = P;\nP.left = n2;\n// \u5220\u9664\u8282\u70b9 P\nn1.left = n2;\n
binary_tree.go/* \u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9 */\n// \u5728 n1 -> n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\np := NewTreeNode(0)\nn1.Left = p\np.Left = n2\n// \u5220\u9664\u8282\u70b9 P\nn1.Left = n2\n
binary_tree.swiftlet P = TreeNode(x: 0)\n// \u5728 n1 -> n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\nn1.left = P\nP.left = n2\n// \u5220\u9664\u8282\u70b9 P\nn1.left = n2\n
binary_tree.js/* \u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9 */\nlet P = new TreeNode(0);\n// \u5728 n1 -> n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\nn1.left = P;\nP.left = n2;\n// \u5220\u9664\u8282\u70b9 P\nn1.left = n2;\n
binary_tree.ts/* \u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9 */\nconst P = new TreeNode(0);\n// \u5728 n1 -> n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\nn1.left = P;\nP.left = n2;\n// \u5220\u9664\u8282\u70b9 P\nn1.left = n2;\n
binary_tree.dart/* \u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9 */\nTreeNode P = new TreeNode(0);\n// \u5728 n1 -> n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\nn1.left = P;\nP.left = n2;\n// \u5220\u9664\u8282\u70b9 P\nn1.left = n2;\n
binary_tree.rslet p = TreeNode::new(0);\n// \u5728 n1 -> n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\nn1.borrow_mut().left = Some(p.clone());\np.borrow_mut().left = Some(n2.clone());\n// \u5220\u9664\u8282\u70b9 p\nn1.borrow_mut().left = Some(n2);\n
binary_tree.c/* \u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9 */\nTreeNode *P = newTreeNode(0);\n// \u5728 n1 -> n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\nn1->left = P;\nP->left = n2;\n// \u5220\u9664\u8282\u70b9 P\nn1->left = n2;\n
binary_tree.zig\n
Note
\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u63d2\u5165\u8282\u70b9\u53ef\u80fd\u4f1a\u6539\u53d8\u4e8c\u53c9\u6811\u7684\u539f\u6709\u903b\u8f91\u7ed3\u6784\uff0c\u800c\u5220\u9664\u8282\u70b9\u901a\u5e38\u610f\u5473\u7740\u5220\u9664\u8be5\u8282\u70b9\u53ca\u5176\u6240\u6709\u5b50\u6811\u3002\u56e0\u6b64\uff0c\u5728\u4e8c\u53c9\u6811\u4e2d\uff0c\u63d2\u5165\u4e0e\u5220\u9664\u901a\u5e38\u662f\u7531\u4e00\u5957\u64cd\u4f5c\u914d\u5408\u5b8c\u6210\u7684\uff0c\u4ee5\u5b9e\u73b0\u6709\u5b9e\u9645\u610f\u4e49\u7684\u64cd\u4f5c\u3002
"},{"location":"chapter_tree/binary_tree/#713","title":"7.1.3 \u00a0 \u5e38\u89c1\u4e8c\u53c9\u6811\u7c7b\u578b","text":""},{"location":"chapter_tree/binary_tree/#1_1","title":"1. \u00a0 \u5b8c\u7f8e\u4e8c\u53c9\u6811","text":"\u5982\u56fe 7-4 \u6240\u793a\uff0c\u300c\u5b8c\u7f8e\u4e8c\u53c9\u6811 perfect binary tree\u300d\u6240\u6709\u5c42\u7684\u8282\u70b9\u90fd\u88ab\u5b8c\u5168\u586b\u6ee1\u3002\u5728\u5b8c\u7f8e\u4e8c\u53c9\u6811\u4e2d\uff0c\u53f6\u8282\u70b9\u7684\u5ea6\u4e3a \\(0\\) \uff0c\u5176\u4f59\u6240\u6709\u8282\u70b9\u7684\u5ea6\u90fd\u4e3a \\(2\\) \uff1b\u82e5\u6811\u7684\u9ad8\u5ea6\u4e3a \\(h\\) \uff0c\u5219\u8282\u70b9\u603b\u6570\u4e3a \\(2^{h+1} - 1\\) \uff0c\u5448\u73b0\u6807\u51c6\u7684\u6307\u6570\u7ea7\u5173\u7cfb\uff0c\u53cd\u6620\u4e86\u81ea\u7136\u754c\u4e2d\u5e38\u89c1\u7684\u7ec6\u80de\u5206\u88c2\u73b0\u8c61\u3002
Tip
\u8bf7\u6ce8\u610f\uff0c\u5728\u4e2d\u6587\u793e\u533a\u4e2d\uff0c\u5b8c\u7f8e\u4e8c\u53c9\u6811\u5e38\u88ab\u79f0\u4e3a\u300c\u6ee1\u4e8c\u53c9\u6811\u300d\u3002
\u56fe 7-4 \u00a0 \u5b8c\u7f8e\u4e8c\u53c9\u6811
"},{"location":"chapter_tree/binary_tree/#2_1","title":"2. \u00a0 \u5b8c\u5168\u4e8c\u53c9\u6811","text":"\u5982\u56fe 7-5 \u6240\u793a\uff0c\u300c\u5b8c\u5168\u4e8c\u53c9\u6811 complete binary tree\u300d\u53ea\u6709\u6700\u5e95\u5c42\u7684\u8282\u70b9\u672a\u88ab\u586b\u6ee1\uff0c\u4e14\u6700\u5e95\u5c42\u8282\u70b9\u5c3d\u91cf\u9760\u5de6\u586b\u5145\u3002
\u56fe 7-5 \u00a0 \u5b8c\u5168\u4e8c\u53c9\u6811
"},{"location":"chapter_tree/binary_tree/#3","title":"3. \u00a0 \u5b8c\u6ee1\u4e8c\u53c9\u6811","text":"\u5982\u56fe 7-6 \u6240\u793a\uff0c\u300c\u5b8c\u6ee1\u4e8c\u53c9\u6811 full binary tree\u300d\u9664\u4e86\u53f6\u8282\u70b9\u4e4b\u5916\uff0c\u5176\u4f59\u6240\u6709\u8282\u70b9\u90fd\u6709\u4e24\u4e2a\u5b50\u8282\u70b9\u3002
\u56fe 7-6 \u00a0 \u5b8c\u6ee1\u4e8c\u53c9\u6811
"},{"location":"chapter_tree/binary_tree/#4","title":"4. \u00a0 \u5e73\u8861\u4e8c\u53c9\u6811","text":"\u5982\u56fe 7-7 \u6240\u793a\uff0c\u300c\u5e73\u8861\u4e8c\u53c9\u6811 balanced binary tree\u300d\u4e2d\u4efb\u610f\u8282\u70b9\u7684\u5de6\u5b50\u6811\u548c\u53f3\u5b50\u6811\u7684\u9ad8\u5ea6\u4e4b\u5dee\u7684\u7edd\u5bf9\u503c\u4e0d\u8d85\u8fc7 1 \u3002
\u56fe 7-7 \u00a0 \u5e73\u8861\u4e8c\u53c9\u6811
"},{"location":"chapter_tree/binary_tree/#714","title":"7.1.4 \u00a0 \u4e8c\u53c9\u6811\u7684\u9000\u5316","text":"\u56fe 7-8 \u5c55\u793a\u4e86\u4e8c\u53c9\u6811\u7684\u7406\u60f3\u7ed3\u6784\u4e0e\u9000\u5316\u7ed3\u6784\u3002\u5f53\u4e8c\u53c9\u6811\u7684\u6bcf\u5c42\u8282\u70b9\u90fd\u88ab\u586b\u6ee1\u65f6\uff0c\u8fbe\u5230\u201c\u5b8c\u7f8e\u4e8c\u53c9\u6811\u201d\uff1b\u800c\u5f53\u6240\u6709\u8282\u70b9\u90fd\u504f\u5411\u4e00\u4fa7\u65f6\uff0c\u4e8c\u53c9\u6811\u9000\u5316\u4e3a\u201c\u94fe\u8868\u201d\u3002
\u56fe 7-8 \u00a0 \u4e8c\u53c9\u6811\u7684\u6700\u4f73\u7ed3\u6784\u4e0e\u6700\u5dee\u7ed3\u6784
\u5982\u8868 7-1 \u6240\u793a\uff0c\u5728\u6700\u4f73\u7ed3\u6784\u548c\u6700\u5dee\u7ed3\u6784\u4e0b\uff0c\u4e8c\u53c9\u6811\u7684\u53f6\u8282\u70b9\u6570\u91cf\u3001\u8282\u70b9\u603b\u6570\u3001\u9ad8\u5ea6\u7b49\u8fbe\u5230\u6781\u5927\u503c\u6216\u6781\u5c0f\u503c\u3002
\u8868 7-1 \u00a0 \u4e8c\u53c9\u6811\u7684\u6700\u4f73\u7ed3\u6784\u4e0e\u6700\u5dee\u7ed3\u6784
\u5b8c\u7f8e\u4e8c\u53c9\u6811 \u94fe\u8868 \u7b2c \\(i\\) \u5c42\u7684\u8282\u70b9\u6570\u91cf \\(2^{i-1}\\) \\(1\\) \u9ad8\u5ea6\u4e3a \\(h\\) \u7684\u6811\u7684\u53f6\u8282\u70b9\u6570\u91cf \\(2^h\\) \\(1\\) \u9ad8\u5ea6\u4e3a \\(h\\) \u7684\u6811\u7684\u8282\u70b9\u603b\u6570 \\(2^{h+1} - 1\\) \\(h + 1\\) \u8282\u70b9\u603b\u6570\u4e3a \\(n\\) \u7684\u6811\u7684\u9ad8\u5ea6 \\(\\log_2 (n+1) - 1\\) \\(n - 1\\)"},{"location":"chapter_tree/binary_tree_traversal/","title":"7.2 \u00a0 \u4e8c\u53c9\u6811\u904d\u5386","text":"\u4ece\u7269\u7406\u7ed3\u6784\u7684\u89d2\u5ea6\u6765\u770b\uff0c\u6811\u662f\u4e00\u79cd\u57fa\u4e8e\u94fe\u8868\u7684\u6570\u636e\u7ed3\u6784\uff0c\u56e0\u6b64\u5176\u904d\u5386\u65b9\u5f0f\u662f\u901a\u8fc7\u6307\u9488\u9010\u4e2a\u8bbf\u95ee\u8282\u70b9\u3002\u7136\u800c\uff0c\u6811\u662f\u4e00\u79cd\u975e\u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff0c\u8fd9\u4f7f\u5f97\u904d\u5386\u6811\u6bd4\u904d\u5386\u94fe\u8868\u66f4\u52a0\u590d\u6742\uff0c\u9700\u8981\u501f\u52a9\u641c\u7d22\u7b97\u6cd5\u6765\u5b9e\u73b0\u3002
\u4e8c\u53c9\u6811\u5e38\u89c1\u7684\u904d\u5386\u65b9\u5f0f\u5305\u62ec\u5c42\u5e8f\u904d\u5386\u3001\u524d\u5e8f\u904d\u5386\u3001\u4e2d\u5e8f\u904d\u5386\u548c\u540e\u5e8f\u904d\u5386\u7b49\u3002
"},{"location":"chapter_tree/binary_tree_traversal/#721","title":"7.2.1 \u00a0 \u5c42\u5e8f\u904d\u5386","text":"\u5982\u56fe 7-9 \u6240\u793a\uff0c\u300c\u5c42\u5e8f\u904d\u5386 level-order traversal\u300d\u4ece\u9876\u90e8\u5230\u5e95\u90e8\u9010\u5c42\u904d\u5386\u4e8c\u53c9\u6811\uff0c\u5e76\u5728\u6bcf\u4e00\u5c42\u6309\u7167\u4ece\u5de6\u5230\u53f3\u7684\u987a\u5e8f\u8bbf\u95ee\u8282\u70b9\u3002
\u5c42\u5e8f\u904d\u5386\u672c\u8d28\u4e0a\u5c5e\u4e8e\u300c\u5e7f\u5ea6\u4f18\u5148\u904d\u5386 breadth-first traversal, BFS\u300d\uff0c\u5b83\u4f53\u73b0\u4e86\u4e00\u79cd\u201c\u4e00\u5708\u4e00\u5708\u5411\u5916\u6269\u5c55\u201d\u7684\u9010\u5c42\u904d\u5386\u65b9\u5f0f\u3002
\u56fe 7-9 \u00a0 \u4e8c\u53c9\u6811\u7684\u5c42\u5e8f\u904d\u5386
"},{"location":"chapter_tree/binary_tree_traversal/#1","title":"1. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"\u5e7f\u5ea6\u4f18\u5148\u904d\u5386\u901a\u5e38\u501f\u52a9\u201c\u961f\u5217\u201d\u6765\u5b9e\u73b0\u3002\u961f\u5217\u9075\u5faa\u201c\u5148\u8fdb\u5148\u51fa\u201d\u7684\u89c4\u5219\uff0c\u800c\u5e7f\u5ea6\u4f18\u5148\u904d\u5386\u5219\u9075\u5faa\u201c\u9010\u5c42\u63a8\u8fdb\u201d\u7684\u89c4\u5219\uff0c\u4e24\u8005\u80cc\u540e\u7684\u601d\u60f3\u662f\u4e00\u81f4\u7684\u3002\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig binary_tree_bfs.pydef level_order(root: TreeNode | None) -> list[int]:\n \"\"\"\u5c42\u5e8f\u904d\u5386\"\"\"\n # \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u8282\u70b9\n queue: deque[TreeNode] = deque()\n queue.append(root)\n # \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\n res = []\n while queue:\n node: TreeNode = queue.popleft() # \u961f\u5217\u51fa\u961f\n res.append(node.val) # \u4fdd\u5b58\u8282\u70b9\u503c\n if node.left is not None:\n queue.append(node.left) # \u5de6\u5b50\u8282\u70b9\u5165\u961f\n if node.right is not None:\n queue.append(node.right) # \u53f3\u5b50\u8282\u70b9\u5165\u961f\n return res\n
binary_tree_bfs.cpp/* \u5c42\u5e8f\u904d\u5386 */\nvector<int> levelOrder(TreeNode *root) {\n // \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u8282\u70b9\n queue<TreeNode *> queue;\n queue.push(root);\n // \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\n vector<int> vec;\n while (!queue.empty()) {\n TreeNode *node = queue.front();\n queue.pop(); // \u961f\u5217\u51fa\u961f\n vec.push_back(node->val); // \u4fdd\u5b58\u8282\u70b9\u503c\n if (node->left != nullptr)\n queue.push(node->left); // \u5de6\u5b50\u8282\u70b9\u5165\u961f\n if (node->right != nullptr)\n queue.push(node->right); // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n }\n return vec;\n}\n
binary_tree_bfs.java/* \u5c42\u5e8f\u904d\u5386 */\nList<Integer> levelOrder(TreeNode root) {\n // \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u8282\u70b9\n Queue<TreeNode> queue = new LinkedList<>();\n queue.add(root);\n // \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\n List<Integer> list = new ArrayList<>();\n while (!queue.isEmpty()) {\n TreeNode node = queue.poll(); // \u961f\u5217\u51fa\u961f\n list.add(node.val); // \u4fdd\u5b58\u8282\u70b9\u503c\n if (node.left != null)\n queue.offer(node.left); // \u5de6\u5b50\u8282\u70b9\u5165\u961f\n if (node.right != null)\n queue.offer(node.right); // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n }\n return list;\n}\n
binary_tree_bfs.cs/* \u5c42\u5e8f\u904d\u5386 */\nList<int> LevelOrder(TreeNode root) {\n // \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u8282\u70b9\n Queue<TreeNode> queue = new();\n queue.Enqueue(root);\n // \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\n List<int> list = [];\n while (queue.Count != 0) {\n TreeNode node = queue.Dequeue(); // \u961f\u5217\u51fa\u961f\n list.Add(node.val!.Value); // \u4fdd\u5b58\u8282\u70b9\u503c\n if (node.left != null)\n queue.Enqueue(node.left); // \u5de6\u5b50\u8282\u70b9\u5165\u961f\n if (node.right != null)\n queue.Enqueue(node.right); // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n }\n return list;\n}\n
binary_tree_bfs.go/* \u5c42\u5e8f\u904d\u5386 */\nfunc levelOrder(root *TreeNode) []any {\n // \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u8282\u70b9\n queue := list.New()\n queue.PushBack(root)\n // \u521d\u59cb\u5316\u4e00\u4e2a\u5207\u7247\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\n nums := make([]any, 0)\n for queue.Len() > 0 {\n // \u961f\u5217\u51fa\u961f\n node := queue.Remove(queue.Front()).(*TreeNode)\n // \u4fdd\u5b58\u8282\u70b9\u503c\n nums = append(nums, node.Val)\n if node.Left != nil {\n // \u5de6\u5b50\u8282\u70b9\u5165\u961f\n queue.PushBack(node.Left)\n }\n if node.Right != nil {\n // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n queue.PushBack(node.Right)\n }\n }\n return nums\n}\n
binary_tree_bfs.swift/* \u5c42\u5e8f\u904d\u5386 */\nfunc levelOrder(root: TreeNode) -> [Int] {\n // \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u8282\u70b9\n var queue: [TreeNode] = [root]\n // \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\n var list: [Int] = []\n while !queue.isEmpty {\n let node = queue.removeFirst() // \u961f\u5217\u51fa\u961f\n list.append(node.val) // \u4fdd\u5b58\u8282\u70b9\u503c\n if let left = node.left {\n queue.append(left) // \u5de6\u5b50\u8282\u70b9\u5165\u961f\n }\n if let right = node.right {\n queue.append(right) // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n }\n }\n return list\n}\n
binary_tree_bfs.js/* \u5c42\u5e8f\u904d\u5386 */\nfunction levelOrder(root) {\n // \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u8282\u70b9\n const queue = [root];\n // \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\n const list = [];\n while (queue.length) {\n let node = queue.shift(); // \u961f\u5217\u51fa\u961f\n list.push(node.val); // \u4fdd\u5b58\u8282\u70b9\u503c\n if (node.left) queue.push(node.left); // \u5de6\u5b50\u8282\u70b9\u5165\u961f\n if (node.right) queue.push(node.right); // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n }\n return list;\n}\n
binary_tree_bfs.ts/* \u5c42\u5e8f\u904d\u5386 */\nfunction levelOrder(root: TreeNode | null): number[] {\n // \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u8282\u70b9\n const queue = [root];\n // \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\n const list: number[] = [];\n while (queue.length) {\n let node = queue.shift() as TreeNode; // \u961f\u5217\u51fa\u961f\n list.push(node.val); // \u4fdd\u5b58\u8282\u70b9\u503c\n if (node.left) {\n queue.push(node.left); // \u5de6\u5b50\u8282\u70b9\u5165\u961f\n }\n if (node.right) {\n queue.push(node.right); // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n }\n }\n return list;\n}\n
binary_tree_bfs.dart/* \u5c42\u5e8f\u904d\u5386 */\nList<int> levelOrder(TreeNode? root) {\n // \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u8282\u70b9\n Queue<TreeNode?> queue = Queue();\n queue.add(root);\n // \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\n List<int> res = [];\n while (queue.isNotEmpty) {\n TreeNode? node = queue.removeFirst(); // \u961f\u5217\u51fa\u961f\n res.add(node!.val); // \u4fdd\u5b58\u8282\u70b9\u503c\n if (node.left != null) queue.add(node.left); // \u5de6\u5b50\u8282\u70b9\u5165\u961f\n if (node.right != null) queue.add(node.right); // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n }\n return res;\n}\n
binary_tree_bfs.rs/* \u5c42\u5e8f\u904d\u5386 */\nfn level_order(root: &Rc<RefCell<TreeNode>>) -> Vec<i32> {\n // \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u8282\u70b9\n let mut que = VecDeque::new();\n que.push_back(Rc::clone(&root));\n // \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\n let mut vec = Vec::new();\n\n while let Some(node) = que.pop_front() { // \u961f\u5217\u51fa\u961f\n vec.push(node.borrow().val); // \u4fdd\u5b58\u8282\u70b9\u503c\n if let Some(left) = node.borrow().left.as_ref() {\n que.push_back(Rc::clone(left)); // \u5de6\u5b50\u8282\u70b9\u5165\u961f\n }\n if let Some(right) = node.borrow().right.as_ref() {\n que.push_back(Rc::clone(right)); // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n };\n }\n vec\n}\n
binary_tree_bfs.c/* \u5c42\u5e8f\u904d\u5386 */\nint *levelOrder(TreeNode *root, int *size) {\n /* \u8f85\u52a9\u961f\u5217 */\n int front, rear;\n int index, *arr;\n TreeNode *node;\n TreeNode **queue;\n\n /* \u8f85\u52a9\u961f\u5217 */\n queue = (TreeNode **)malloc(sizeof(TreeNode *) * MAX_SIZE);\n // \u961f\u5217\u6307\u9488\n front = 0, rear = 0;\n // \u52a0\u5165\u6839\u8282\u70b9\n queue[rear++] = root;\n // \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\n /* \u8f85\u52a9\u6570\u7ec4 */\n arr = (int *)malloc(sizeof(int) * MAX_SIZE);\n // \u6570\u7ec4\u6307\u9488\n index = 0;\n while (front < rear) {\n // \u961f\u5217\u51fa\u961f\n node = queue[front++];\n // \u4fdd\u5b58\u8282\u70b9\u503c\n arr[index++] = node->val;\n if (node->left != NULL) {\n // \u5de6\u5b50\u8282\u70b9\u5165\u961f\n queue[rear++] = node->left;\n }\n if (node->right != NULL) {\n // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n queue[rear++] = node->right;\n }\n }\n // \u66f4\u65b0\u6570\u7ec4\u957f\u5ea6\u7684\u503c\n *size = index;\n arr = realloc(arr, sizeof(int) * (*size));\n\n // \u91ca\u653e\u8f85\u52a9\u6570\u7ec4\u7a7a\u95f4\n free(queue);\n return arr;\n}\n
binary_tree_bfs.zig// \u5c42\u5e8f\u904d\u5386\nfn levelOrder(comptime T: type, mem_allocator: std.mem.Allocator, root: *inc.TreeNode(T)) !std.ArrayList(T) {\n // \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u8282\u70b9\n const L = std.TailQueue(*inc.TreeNode(T));\n var queue = L{};\n var root_node = try mem_allocator.create(L.Node);\n root_node.data = root;\n queue.append(root_node); \n // \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\n var list = std.ArrayList(T).init(std.heap.page_allocator);\n while (queue.len > 0) {\n var queue_node = queue.popFirst().?; // \u961f\u5217\u51fa\u961f\n var node = queue_node.data;\n try list.append(node.val); // \u4fdd\u5b58\u8282\u70b9\u503c\n if (node.left != null) {\n var tmp_node = try mem_allocator.create(L.Node);\n tmp_node.data = node.left.?;\n queue.append(tmp_node); // \u5de6\u5b50\u8282\u70b9\u5165\u961f\n }\n if (node.right != null) {\n var tmp_node = try mem_allocator.create(L.Node);\n tmp_node.data = node.right.?;\n queue.append(tmp_node); // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n } \n }\n return list;\n}\n
"},{"location":"chapter_tree/binary_tree_traversal/#2","title":"2. \u00a0 \u590d\u6742\u5ea6\u5206\u6790","text":"\u76f8\u5e94\u5730\uff0c\u524d\u5e8f\u3001\u4e2d\u5e8f\u548c\u540e\u5e8f\u904d\u5386\u90fd\u5c5e\u4e8e\u300c\u6df1\u5ea6\u4f18\u5148\u904d\u5386 depth-first traversal, DFS\u300d\uff0c\u5b83\u4f53\u73b0\u4e86\u4e00\u79cd\u201c\u5148\u8d70\u5230\u5c3d\u5934\uff0c\u518d\u56de\u6eaf\u7ee7\u7eed\u201d\u7684\u904d\u5386\u65b9\u5f0f\u3002
\u56fe 7-10 \u5c55\u793a\u4e86\u5bf9\u4e8c\u53c9\u6811\u8fdb\u884c\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u7684\u5de5\u4f5c\u539f\u7406\u3002\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u5c31\u50cf\u662f\u7ed5\u7740\u6574\u68f5\u4e8c\u53c9\u6811\u7684\u5916\u56f4\u201c\u8d70\u201d\u4e00\u5708\uff0c\u5728\u6bcf\u4e2a\u8282\u70b9\u90fd\u4f1a\u9047\u5230\u4e09\u4e2a\u4f4d\u7f6e\uff0c\u5206\u522b\u5bf9\u5e94\u524d\u5e8f\u904d\u5386\u3001\u4e2d\u5e8f\u904d\u5386\u548c\u540e\u5e8f\u904d\u5386\u3002
\u56fe 7-10 \u00a0 \u4e8c\u53c9\u641c\u7d22\u6811\u7684\u524d\u5e8f\u3001\u4e2d\u5e8f\u3001\u540e\u5e8f\u904d\u5386
"},{"location":"chapter_tree/binary_tree_traversal/#1_1","title":"1. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u901a\u5e38\u57fa\u4e8e\u9012\u5f52\u5b9e\u73b0\uff1a
PythonC++JavaC#GoSwiftJSTSDartRustCZig binary_tree_dfs.pydef pre_order(root: TreeNode | None):\n \"\"\"\u524d\u5e8f\u904d\u5386\"\"\"\n if root is None:\n return\n # \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\n res.append(root.val)\n pre_order(root=root.left)\n pre_order(root=root.right)\n\ndef in_order(root: TreeNode | None):\n \"\"\"\u4e2d\u5e8f\u904d\u5386\"\"\"\n if root is None:\n return\n # \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\n in_order(root=root.left)\n res.append(root.val)\n in_order(root=root.right)\n\ndef post_order(root: TreeNode | None):\n \"\"\"\u540e\u5e8f\u904d\u5386\"\"\"\n if root is None:\n return\n # \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\n post_order(root=root.left)\n post_order(root=root.right)\n res.append(root.val)\n
binary_tree_dfs.cpp/* \u524d\u5e8f\u904d\u5386 */\nvoid preOrder(TreeNode *root) {\n if (root == nullptr)\n return;\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\n vec.push_back(root->val);\n preOrder(root->left);\n preOrder(root->right);\n}\n\n/* \u4e2d\u5e8f\u904d\u5386 */\nvoid inOrder(TreeNode *root) {\n if (root == nullptr)\n return;\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\n inOrder(root->left);\n vec.push_back(root->val);\n inOrder(root->right);\n}\n\n/* \u540e\u5e8f\u904d\u5386 */\nvoid postOrder(TreeNode *root) {\n if (root == nullptr)\n return;\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\n postOrder(root->left);\n postOrder(root->right);\n vec.push_back(root->val);\n}\n
binary_tree_dfs.java/* \u524d\u5e8f\u904d\u5386 */\nvoid preOrder(TreeNode root) {\n if (root == null)\n return;\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\n list.add(root.val);\n preOrder(root.left);\n preOrder(root.right);\n}\n\n/* \u4e2d\u5e8f\u904d\u5386 */\nvoid inOrder(TreeNode root) {\n if (root == null)\n return;\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\n inOrder(root.left);\n list.add(root.val);\n inOrder(root.right);\n}\n\n/* \u540e\u5e8f\u904d\u5386 */\nvoid postOrder(TreeNode root) {\n if (root == null)\n return;\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\n postOrder(root.left);\n postOrder(root.right);\n list.add(root.val);\n}\n
binary_tree_dfs.cs/* \u524d\u5e8f\u904d\u5386 */\nvoid PreOrder(TreeNode? root) {\n if (root == null) return;\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\n list.Add(root.val!.Value);\n PreOrder(root.left);\n PreOrder(root.right);\n}\n\n/* \u4e2d\u5e8f\u904d\u5386 */\nvoid InOrder(TreeNode? root) {\n if (root == null) return;\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\n InOrder(root.left);\n list.Add(root.val!.Value);\n InOrder(root.right);\n}\n\n/* \u540e\u5e8f\u904d\u5386 */\nvoid PostOrder(TreeNode? root) {\n if (root == null) return;\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\n PostOrder(root.left);\n PostOrder(root.right);\n list.Add(root.val!.Value);\n}\n
binary_tree_dfs.go/* \u524d\u5e8f\u904d\u5386 */\nfunc preOrder(node *TreeNode) {\n if node == nil {\n return\n }\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\n nums = append(nums, node.Val)\n preOrder(node.Left)\n preOrder(node.Right)\n}\n\n/* \u4e2d\u5e8f\u904d\u5386 */\nfunc inOrder(node *TreeNode) {\n if node == nil {\n return\n }\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\n inOrder(node.Left)\n nums = append(nums, node.Val)\n inOrder(node.Right)\n}\n\n/* \u540e\u5e8f\u904d\u5386 */\nfunc postOrder(node *TreeNode) {\n if node == nil {\n return\n }\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\n postOrder(node.Left)\n postOrder(node.Right)\n nums = append(nums, node.Val)\n}\n
binary_tree_dfs.swift/* \u524d\u5e8f\u904d\u5386 */\nfunc preOrder(root: TreeNode?) {\n guard let root = root else {\n return\n }\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\n list.append(root.val)\n preOrder(root: root.left)\n preOrder(root: root.right)\n}\n\n/* \u4e2d\u5e8f\u904d\u5386 */\nfunc inOrder(root: TreeNode?) {\n guard let root = root else {\n return\n }\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\n inOrder(root: root.left)\n list.append(root.val)\n inOrder(root: root.right)\n}\n\n/* \u540e\u5e8f\u904d\u5386 */\nfunc postOrder(root: TreeNode?) {\n guard let root = root else {\n return\n }\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\n postOrder(root: root.left)\n postOrder(root: root.right)\n list.append(root.val)\n}\n
binary_tree_dfs.js/* \u524d\u5e8f\u904d\u5386 */\nfunction preOrder(root) {\n if (root === null) return;\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\n list.push(root.val);\n preOrder(root.left);\n preOrder(root.right);\n}\n\n/* \u4e2d\u5e8f\u904d\u5386 */\nfunction inOrder(root) {\n if (root === null) return;\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\n inOrder(root.left);\n list.push(root.val);\n inOrder(root.right);\n}\n\n/* \u540e\u5e8f\u904d\u5386 */\nfunction postOrder(root) {\n if (root === null) return;\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\n postOrder(root.left);\n postOrder(root.right);\n list.push(root.val);\n}\n
binary_tree_dfs.ts/* \u524d\u5e8f\u904d\u5386 */\nfunction preOrder(root: TreeNode | null): void {\n if (root === null) {\n return;\n }\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\n list.push(root.val);\n preOrder(root.left);\n preOrder(root.right);\n}\n\n/* \u4e2d\u5e8f\u904d\u5386 */\nfunction inOrder(root: TreeNode | null): void {\n if (root === null) {\n return;\n }\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\n inOrder(root.left);\n list.push(root.val);\n inOrder(root.right);\n}\n\n/* \u540e\u5e8f\u904d\u5386 */\nfunction postOrder(root: TreeNode | null): void {\n if (root === null) {\n return;\n }\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\n postOrder(root.left);\n postOrder(root.right);\n list.push(root.val);\n}\n
binary_tree_dfs.dart/* \u524d\u5e8f\u904d\u5386 */\nvoid preOrder(TreeNode? node) {\n if (node == null) return;\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\n list.add(node.val);\n preOrder(node.left);\n preOrder(node.right);\n}\n\n/* \u4e2d\u5e8f\u904d\u5386 */\nvoid inOrder(TreeNode? node) {\n if (node == null) return;\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\n inOrder(node.left);\n list.add(node.val);\n inOrder(node.right);\n}\n\n/* \u540e\u5e8f\u904d\u5386 */\nvoid postOrder(TreeNode? node) {\n if (node == null) return;\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\n postOrder(node.left);\n postOrder(node.right);\n list.add(node.val);\n}\n
binary_tree_dfs.rs/* \u524d\u5e8f\u904d\u5386 */\nfn pre_order(root: Option<&Rc<RefCell<TreeNode>>>) -> Vec<i32> {\n let mut result = vec![];\n\n if let Some(node) = root {\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\n result.push(node.borrow().val);\n result.append(&mut pre_order(node.borrow().left.as_ref()));\n result.append(&mut pre_order(node.borrow().right.as_ref()));\n }\n result\n}\n\n/* \u4e2d\u5e8f\u904d\u5386 */\nfn in_order(root: Option<&Rc<RefCell<TreeNode>>>) -> Vec<i32> {\n let mut result = vec![];\n\n if let Some(node) = root {\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\n result.append(&mut in_order(node.borrow().left.as_ref()));\n result.push(node.borrow().val);\n result.append(&mut in_order(node.borrow().right.as_ref()));\n }\n result\n}\n\n/* \u540e\u5e8f\u904d\u5386 */\nfn post_order(root: Option<&Rc<RefCell<TreeNode>>>) -> Vec<i32> {\n let mut result = vec![];\n\n if let Some(node) = root {\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\n result.append(&mut post_order(node.borrow().left.as_ref()));\n result.append(&mut post_order(node.borrow().right.as_ref()));\n result.push(node.borrow().val);\n }\n result\n}\n
binary_tree_dfs.c/* \u524d\u5e8f\u904d\u5386 */\nvoid preOrder(TreeNode *root, int *size) {\n if (root == NULL)\n return;\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\n arr[(*size)++] = root->val;\n preOrder(root->left, size);\n preOrder(root->right, size);\n}\n\n/* \u4e2d\u5e8f\u904d\u5386 */\nvoid inOrder(TreeNode *root, int *size) {\n if (root == NULL)\n return;\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\n inOrder(root->left, size);\n arr[(*size)++] = root->val;\n inOrder(root->right, size);\n}\n\n/* \u540e\u5e8f\u904d\u5386 */\nvoid postOrder(TreeNode *root, int *size) {\n if (root == NULL)\n return;\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\n postOrder(root->left, size);\n postOrder(root->right, size);\n arr[(*size)++] = root->val;\n}\n
binary_tree_dfs.zig// \u524d\u5e8f\u904d\u5386\nfn preOrder(comptime T: type, root: ?*inc.TreeNode(T)) !void {\n if (root == null) return;\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\n try list.append(root.?.val);\n try preOrder(T, root.?.left);\n try preOrder(T, root.?.right);\n}\n\n// \u4e2d\u5e8f\u904d\u5386\nfn inOrder(comptime T: type, root: ?*inc.TreeNode(T)) !void {\n if (root == null) return;\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\n try inOrder(T, root.?.left);\n try list.append(root.?.val);\n try inOrder(T, root.?.right);\n}\n\n// \u540e\u5e8f\u904d\u5386\nfn postOrder(comptime T: type, root: ?*inc.TreeNode(T)) !void {\n if (root == null) return;\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\n try postOrder(T, root.?.left);\n try postOrder(T, root.?.right);\n try list.append(root.?.val);\n}\n
Tip
\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u4e5f\u53ef\u4ee5\u57fa\u4e8e\u8fed\u4ee3\u5b9e\u73b0\uff0c\u6709\u5174\u8da3\u7684\u8bfb\u8005\u53ef\u4ee5\u81ea\u884c\u7814\u7a76\u3002
\u56fe 7-11 \u5c55\u793a\u4e86\u524d\u5e8f\u904d\u5386\u4e8c\u53c9\u6811\u7684\u9012\u5f52\u8fc7\u7a0b\uff0c\u5176\u53ef\u5206\u4e3a\u201c\u9012\u201d\u548c\u201c\u5f52\u201d\u4e24\u4e2a\u9006\u5411\u7684\u90e8\u5206\u3002
\u56fe 7-11 \u00a0 \u524d\u5e8f\u904d\u5386\u7684\u9012\u5f52\u8fc7\u7a0b
"},{"location":"chapter_tree/binary_tree_traversal/#2_1","title":"2. \u00a0 \u590d\u6742\u5ea6\u5206\u6790","text":"\u5bf9\u4e8e\u53ea\u6709\u4e00\u4e2a\u8282\u70b9\u7684\u4e8c\u53c9\u6811\uff0c\u6811\u7684\u9ad8\u5ea6\u548c\u6839\u8282\u70b9\u7684\u6df1\u5ea6\u90fd\u662f \\(0\\) \u5417\uff1f
\u662f\u7684\uff0c\u56e0\u4e3a\u9ad8\u5ea6\u548c\u6df1\u5ea6\u901a\u5e38\u5b9a\u4e49\u4e3a\u201c\u7ecf\u8fc7\u7684\u8fb9\u7684\u6570\u91cf\u201d\u3002
\u4e8c\u53c9\u6811\u4e2d\u7684\u63d2\u5165\u4e0e\u5220\u9664\u4e00\u822c\u7531\u4e00\u5957\u64cd\u4f5c\u914d\u5408\u5b8c\u6210\uff0c\u8fd9\u91cc\u7684\u201c\u4e00\u5957\u64cd\u4f5c\u201d\u6307\u4ec0\u4e48\u5462\uff1f\u53ef\u4ee5\u7406\u89e3\u4e3a\u8d44\u6e90\u7684\u5b50\u8282\u70b9\u7684\u8d44\u6e90\u91ca\u653e\u5417\uff1f
\u62ff\u4e8c\u53c9\u641c\u7d22\u6811\u6765\u4e3e\u4f8b\uff0c\u5220\u9664\u8282\u70b9\u64cd\u4f5c\u8981\u5206\u4e09\u79cd\u60c5\u51b5\u5904\u7406\uff0c\u5176\u4e2d\u6bcf\u79cd\u60c5\u51b5\u90fd\u9700\u8981\u8fdb\u884c\u591a\u4e2a\u6b65\u9aa4\u7684\u8282\u70b9\u64cd\u4f5c\u3002
\u4e3a\u4ec0\u4e48 DFS \u904d\u5386\u4e8c\u53c9\u6811\u6709\u524d\u3001\u4e2d\u3001\u540e\u4e09\u79cd\u987a\u5e8f\uff0c\u5206\u522b\u6709\u4ec0\u4e48\u7528\u5462\uff1f
\u4e0e\u987a\u5e8f\u548c\u9006\u5e8f\u904d\u5386\u6570\u7ec4\u7c7b\u4f3c\uff0c\u524d\u5e8f\u3001\u4e2d\u5e8f\u3001\u540e\u5e8f\u904d\u5386\u662f\u4e09\u79cd\u4e8c\u53c9\u6811\u904d\u5386\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5b83\u4eec\u5f97\u5230\u4e00\u4e2a\u7279\u5b9a\u987a\u5e8f\u7684\u904d\u5386\u7ed3\u679c\u3002\u4f8b\u5982\u5728\u4e8c\u53c9\u641c\u7d22\u6811\u4e2d\uff0c\u7531\u4e8e\u8282\u70b9\u5927\u5c0f\u6ee1\u8db3 \u5de6\u5b50\u8282\u70b9\u503c < \u6839\u8282\u70b9\u503c < \u53f3\u5b50\u8282\u70b9\u503c
\uff0c\u56e0\u6b64\u6211\u4eec\u53ea\u8981\u6309\u7167 \u5de6 $\\rightarrow$ \u6839 $\\rightarrow$ \u53f3
\u7684\u4f18\u5148\u7ea7\u904d\u5386\u6811\uff0c\u5c31\u53ef\u4ee5\u83b7\u5f97\u6709\u5e8f\u7684\u8282\u70b9\u5e8f\u5217\u3002
\u53f3\u65cb\u64cd\u4f5c\u662f\u5904\u7406\u5931\u8861\u8282\u70b9 node
\u3001child
\u3001grand_child
\u4e4b\u95f4\u7684\u5173\u7cfb\uff0c\u90a3 node
\u7684\u7236\u8282\u70b9\u548c node
\u539f\u6765\u7684\u8fde\u63a5\u4e0d\u9700\u8981\u7ef4\u62a4\u5417\uff1f\u53f3\u65cb\u64cd\u4f5c\u540e\u5c82\u4e0d\u662f\u65ad\u6389\u4e86\uff1f
\u6211\u4eec\u9700\u8981\u4ece\u9012\u5f52\u7684\u89c6\u89d2\u6765\u770b\u8fd9\u4e2a\u95ee\u9898\u3002\u53f3\u65cb\u64cd\u4f5c right_rotate(root)
\u4f20\u5165\u7684\u662f\u5b50\u6811\u7684\u6839\u8282\u70b9\uff0c\u6700\u7ec8 return child
\u8fd4\u56de\u65cb\u8f6c\u4e4b\u540e\u7684\u5b50\u6811\u7684\u6839\u8282\u70b9\u3002\u5b50\u6811\u7684\u6839\u8282\u70b9\u548c\u5176\u7236\u8282\u70b9\u7684\u8fde\u63a5\u662f\u5728\u8be5\u51fd\u6570\u8fd4\u56de\u540e\u5b8c\u6210\u7684\uff0c\u4e0d\u5c5e\u4e8e\u53f3\u65cb\u64cd\u4f5c\u7684\u7ef4\u62a4\u8303\u56f4\u3002
\u5728 C++ \u4e2d\uff0c\u51fd\u6570\u88ab\u5212\u5206\u5230 private
\u548c public
\u4e2d\uff0c\u8fd9\u65b9\u9762\u6709\u4ec0\u4e48\u8003\u91cf\u5417\uff1f\u4e3a\u4ec0\u4e48\u8981\u5c06 height()
\u51fd\u6570\u548c updateHeight()
\u51fd\u6570\u5206\u522b\u653e\u5728 public
\u548c private
\u4e2d\u5462\uff1f
\u4e3b\u8981\u770b\u65b9\u6cd5\u7684\u4f7f\u7528\u8303\u56f4\uff0c\u5982\u679c\u65b9\u6cd5\u53ea\u5728\u7c7b\u5185\u90e8\u4f7f\u7528\uff0c\u90a3\u4e48\u5c31\u8bbe\u8ba1\u4e3a private
\u3002\u4f8b\u5982\uff0c\u7528\u6237\u5355\u72ec\u8c03\u7528 updateHeight()
\u662f\u6ca1\u6709\u610f\u4e49\u7684\uff0c\u5b83\u53ea\u662f\u63d2\u5165\u3001\u5220\u9664\u64cd\u4f5c\u4e2d\u7684\u4e00\u6b65\u3002\u800c height()
\u662f\u8bbf\u95ee\u8282\u70b9\u9ad8\u5ea6\uff0c\u7c7b\u4f3c\u4e8e vector.size()
\uff0c\u56e0\u6b64\u8bbe\u7f6e\u6210 public
\u4ee5\u4fbf\u4f7f\u7528\u3002
\u5982\u4f55\u4ece\u4e00\u7ec4\u8f93\u5165\u6570\u636e\u6784\u5efa\u4e00\u68f5\u4e8c\u53c9\u641c\u7d22\u6811\uff1f\u6839\u8282\u70b9\u7684\u9009\u62e9\u662f\u4e0d\u662f\u5f88\u91cd\u8981\uff1f
\u662f\u7684\uff0c\u6784\u5efa\u6811\u7684\u65b9\u6cd5\u5df2\u5728\u4e8c\u53c9\u641c\u7d22\u6811\u4ee3\u7801\u4e2d\u7684 build_tree()
\u65b9\u6cd5\u4e2d\u7ed9\u51fa\u3002\u81f3\u4e8e\u6839\u8282\u70b9\u7684\u9009\u62e9\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u5c06\u8f93\u5165\u6570\u636e\u6392\u5e8f\uff0c\u7136\u540e\u5c06\u4e2d\u70b9\u5143\u7d20\u4f5c\u4e3a\u6839\u8282\u70b9\uff0c\u518d\u9012\u5f52\u5730\u6784\u5efa\u5de6\u53f3\u5b50\u6811\u3002\u8fd9\u6837\u505a\u53ef\u4ee5\u6700\u5927\u7a0b\u5ea6\u4fdd\u8bc1\u6811\u7684\u5e73\u8861\u6027\u3002
\u5728 Java \u4e2d\uff0c\u5b57\u7b26\u4e32\u5bf9\u6bd4\u662f\u5426\u4e00\u5b9a\u8981\u7528 equals()
\u65b9\u6cd5\uff1f
\u5728 Java \u4e2d\uff0c\u5bf9\u4e8e\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff0c==
\u7528\u4e8e\u5bf9\u6bd4\u4e24\u4e2a\u53d8\u91cf\u7684\u503c\u662f\u5426\u76f8\u7b49\u3002\u5bf9\u4e8e\u5f15\u7528\u7c7b\u578b\uff0c\u4e24\u79cd\u7b26\u53f7\u7684\u5de5\u4f5c\u539f\u7406\u662f\u4e0d\u540c\u7684\u3002
==
\uff1a\u7528\u6765\u6bd4\u8f83\u4e24\u4e2a\u53d8\u91cf\u662f\u5426\u6307\u5411\u540c\u4e00\u4e2a\u5bf9\u8c61\uff0c\u5373\u5b83\u4eec\u5728\u5185\u5b58\u4e2d\u7684\u4f4d\u7f6e\u662f\u5426\u76f8\u540c\u3002equals()
\uff1a\u7528\u6765\u5bf9\u6bd4\u4e24\u4e2a\u5bf9\u8c61\u7684\u503c\u662f\u5426\u76f8\u7b49\u3002\u56e0\u6b64\uff0c\u5982\u679c\u8981\u5bf9\u6bd4\u503c\uff0c\u6211\u4eec\u5e94\u8be5\u4f7f\u7528 equals()
\u3002\u7136\u800c\uff0c\u901a\u8fc7 String a = \"hi\"; String b = \"hi\";
\u521d\u59cb\u5316\u7684\u5b57\u7b26\u4e32\u90fd\u5b58\u50a8\u5728\u5b57\u7b26\u4e32\u5e38\u91cf\u6c60\u4e2d\uff0c\u5b83\u4eec\u6307\u5411\u540c\u4e00\u4e2a\u5bf9\u8c61\uff0c\u56e0\u6b64\u4e5f\u53ef\u4ee5\u7528 a == b
\u6765\u6bd4\u8f83\u4e24\u4e2a\u5b57\u7b26\u4e32\u7684\u5185\u5bb9\u3002
\u5e7f\u5ea6\u4f18\u5148\u904d\u5386\u5230\u6700\u5e95\u5c42\u4e4b\u524d\uff0c\u961f\u5217\u4e2d\u7684\u8282\u70b9\u6570\u91cf\u662f \\(2^h\\) \u5417\uff1f
\u662f\u7684\uff0c\u4f8b\u5982\u9ad8\u5ea6 \\(h = 2\\) \u7684\u6ee1\u4e8c\u53c9\u6811\uff0c\u5176\u8282\u70b9\u603b\u6570 \\(n = 7\\) \uff0c\u5219\u5e95\u5c42\u8282\u70b9\u6570\u91cf \\(4 = 2^h = (n + 1) / 2\\) \u3002
"}]} \ No newline at end of file diff --git a/sitemap.xml b/sitemap.xml index e38640703..8ac7e3850 100644 --- a/sitemap.xml +++ b/sitemap.xml @@ -2,522 +2,522 @@